CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > ANSYS > FLUENT > Fluent UDF and Scheme Programming

Fluent crashing due to DEFINE_ON_DEMAND to import csv

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 28, 2021, 14:39
Default Fluent crashing due to DEFINE_ON_DEMAND to import csv
  #1
jhz
New Member
 
Join Date: Dec 2020
Posts: 19
Rep Power: 6
jhz is on a distinguished road
Hello,



The following UDF code was working fine with no problems over multiple runs but suddenly started crashing Fluent - it did not generate any insightful error logs. For example, if I compile + load this UDF and then execute it manually, I can see the success messages. However, if I proceed to solve it can crash upon initialization or soon after starting the solver.


Code:
#include "udf.h"
#include <math.h>
#include <stdlib.h>

// data read
int NPts_f = 117; // row size of each coeff data file
int str_len = 25; // length of char within each string
int NPts_Re = 11; // rows in the meta file corresponding to number of files
float **alpha_arr, **cl_arr, **cd_arr; // each are 2D arrays where arr[f index][data row]
float *Re_arr; // 1D array to store Re values
char **fname_arr; // 2D array for storing strings
//

DEFINE_ON_DEMAND(read_coefficients)
{
    int i = 0, j = 0; // iterators
    float a_data, cl_data, cd_data; // intermediate vars for data storage
    float Re_data;
    char fname_str[25];
    FILE* fp;

    // declaration/malloc for arrays
    Re_arr = malloc(NPts_Re*sizeof(float));

    fname_arr = malloc(NPts_Re*sizeof(char*));
    for (i = 0; i <= NPts_Re; i++) {
        fname_arr[i] = malloc(str_len*sizeof(char));
    }

    alpha_arr = malloc(NPts_Re*sizeof(float));
    cl_arr = malloc(NPts_Re*sizeof(float));
    cd_arr = malloc(NPts_Re*sizeof(float));
    for (i = 0; i <= NPts_Re; i++) {
        alpha_arr[i] = malloc(NPts_f*sizeof(float));
        cl_arr[i] = malloc(NPts_f*sizeof(float));
        cd_arr[i] = malloc(NPts_f*sizeof(float));
    }

    // open meta csv file
    fp = fopen("import_metadata.csv","r");
    if (fp != NULL)
        Message("##### CSV file opened successfully.\n");
    else
        Message("##### *Error opening file.\n");

    for (i = 1; i <= NPts_Re; i++) {
        fscanf(fp, "%f\t%s\n", &Re_data, fname_str);
        Re_arr[i] = Re_data;
        // Message("%s\n", fname_str);
        strcpy(fname_arr[i], fname_str);
    }

    fclose(fp);

    // read coeff data files
    for (i = 1; i <= NPts_Re; i++) {
        fp = fopen(fname_arr[i],"r");
        if (fp != NULL)
            Message("##### coeff CSV file #%i opened successfully.\n", i);
        else
            Message("##### *Error opening coeff file #%i.\n", i);

        for (j = 1; j <= NPts_f; j++) {
            fscanf(fp, "%f,%f,%f\n", &a_data, &cl_data, &cd_data);
            alpha_arr[i][j] = a_data;
            cl_arr[i][j] = cl_data;
            cd_arr[i][j] = cd_data;
        }

        fclose(fp);
    }
}
The purpose of this code is essentially to import and store data from several csv files into arrays for later access in DEFINE_ADJUST macros etc. I'm surprised this is causing a crash after it worked consistently with different projects without issues, but I suspect it may be due to how I'm handling memory. I'm using 'malloc' here to allocate memory because this was used in a UDF tutorial with similar purpose, but I'm not sure if it is correct since there also exists UDMs and you can simply declare global arrays without malloc.



So, my questions would be:
1. Is there any way to get more meaningful debug messages or logs from Fluent when it crashes due to UDFs? It crashes quickly and I don't have a chance to glimpse the error message and there's no meaningful log besides 'abnormal exit'.


2. For the described purpose (read csv -> store data for usage in DEFINE_ADJUST, DEFINE_SOURCE macros), is the current approach correct? If not, what is the recommended method?


Any help is appreciated, thanks!
jhz is offline   Reply With Quote

Old   January 29, 2021, 14:52
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,965
Rep Power: 27
pakk will become famous soon enough
for (i = 0; i <= NPts_Re; i++)

I did not analyze in detail, but the line above smells like an off-by-one error. Normal usage is one of the two below:

for (i = 0; i < NPts_Re; i++)
for (i = 1; i <= NPts_Re; i++)

In your code you sometimes start counting at 0, and sometimes at 1. That makes your life difficult, you have to keep track of what you are doing. Make it easy: start at zero.
pakk is offline   Reply With Quote

Old   February 1, 2021, 19:41
Default
  #3
jhz
New Member
 
Join Date: Dec 2020
Posts: 19
Rep Power: 6
jhz is on a distinguished road
Quote:
Originally Posted by pakk View Post
for (i = 0; i <= NPts_Re; i++)

I did not analyze in detail, but the line above smells like an off-by-one error. Normal usage is one of the two below:

for (i = 0; i < NPts_Re; i++)
for (i = 1; i <= NPts_Re; i++)

In your code you sometimes start counting at 0, and sometimes at 1. That makes your life difficult, you have to keep track of what you are doing. Make it easy: start at zero.

Hello, thanks for your reply! After doing some debugging, I believe the off by 1 index is exactly the cause of the error/crashes -> I only allocated for size of NPts_Re but will actually use NPts_Re + 1. I will revise my code to start at 0 index.


I'm assuming the crash was caused by some memory leak. In these cases where my code 'breaks' the memory allocation, would restarting the PC fix any lingering issues assuming the new UDF does things correctly?


Thanks!
jhz is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using PengRobinsonGas EoS with sprayFoam Jabo OpenFOAM Running, Solving & CFD 36 July 16, 2024 04:52
[swak4Foam] Installation Problem with OF 6 version Aurel OpenFOAM Community Contributions 14 November 18, 2020 17:18
hybrid mesh created by pointwise fails mesh check in fluent due to left-handed faces Hamid.de Pointwise & Gridgen 4 November 9, 2016 12:20
Running UDF with Supercomputer roi247 FLUENT 4 October 15, 2015 14:41
mesh missing after import in fluent morteza08 FLUENT 0 July 23, 2010 03:22


All times are GMT -4. The time now is 19:02.