CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   Fluent crashing with no error message when initialising simulation with new UDF's (https://www.cfd-online.com/Forums/fluent-udf/239219-fluent-crashing-no-error-message-when-initialising-simulation-new-udfs.html)

ham551 October 26, 2021 07:45

Fluent crashing with no error message when initialising simulation with new UDF's
 
I wrote some new UDF's to give Fluent more accurate dynamical viscosity and thermal conductivity values for Hydrogen. Previously I had a more basic UDF just giving data for 1atm, now I am providing data for a large range of pressures and temperatures. However, when attempting to initialize the simulation with the new UDF's compiled, Fluent crashes almost instantly and doesn't give any message.

If I do a hybrid initialization first with the old UDF, then another with the new UDF's, it manages 6-7 iterations during initialization before crashing.

Seems likely it's the new UDF's causing the crash but I don't know why. Any ideas?

The UDF reads in data taken from a NASA CEA simulation using a .csv file. All the conductivity data is merged into a single column. So for each pressure, there exist 43 conductivity values for the 43 temperature intervals tested. The UDF checks the pressure from fluent, then fills a temporary array of the corresponding 43 conductivity values. Then the UDF checks the temperature from fluent and linearly interpolates the conductivity from that temporary array.

The code seems to work perfectly during testing, outputting the correct values and compiling in Fluent.

Code:

/******* UDF that simulates the thermal conductivity****************
***************of Hydrogen at pressures between 1Pa-6Bar and 300-4500K
                              ************************/
#include <udf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
DEFINE_PROPERTY(conductivity, c,t)
{
real conductivity = 0.3;
real T = C_T(c, t);
//Convert pressure[Pa] to [Bar].
real P = (C_P(c, t)*1E-05);
int pressureSize = 627;
int tempSize = 43;
int csvlength = 26961;
//Initialise arrays for temperatures, pressures and conductivities.
real temp[43] = {300,400 ... 4400,4500};
real pressure[627] = {1e-05 ... 0.0001 ... 0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.1,0.11 ... 6};
real conductivities[26961];
char line[2048];
int i =0;

//Open and read in .csv file of conductivities.
FILE *f = fopen("h2conductivity.csv", "r");
    if(f == NULL)
    {
        perror("Unable to open the file");
        exit(1);
    }
    while(fgets(line,sizeof(line),f))
    {
        char *token;
        token = strtok(line,",");
        conductivities[i] = atof(token);
        i=i+1;   
    }
    // Check for any 0 values and replace with set value.
    for(int i = 0; i < csvlength;i++)
    {
        if(conductivities[i] == 0){
        conductivities[i] = 3E-13;
        }
    }
    // Initialise an array for temporarily storing conductivity values
    //Check pressure from fluent against pressure values used to collect conductivity values and fill
      temporary cond. array with corresponding conductivity values
    real tempConductivities[tempSize];
    for(int i = 0; i < pressureSize ; i++)
    {
        if(P>pressure[i] && P<((pressure[i]+pressure[i+1])/2))
        {
            int start = i*tempSize;
            int stop = start + tempSize;
            for(int j = start; j < stop; j++)
            {
            int q = j - start;
            tempConductivities[q] = conductivities[j];
            }
        }

        if(P>=(pressure[i]+pressure[i+1])/2)
        {
            int start = (i+1)*tempSize;
            int stop = start + tempSize;
            for(int j = start; j < stop; j++)
            {
            int q = j - start;
            tempConductivities[q] = conductivities[j];
            }
        }

        if(P == pressure[i])
        {
            int start = i*tempSize;
            int stop = start + tempSize;
            for(int j = start; j < stop; j++)
            {
            int q = j - start;
            tempConductivities[q] = conductivities[j];
            }
        }
    }
    //Compare temperature from fluent and linearly interpolate between conductivities in temp cond. array
    //Convert units to S.I by multiplying by 0.1 -> (mW/cmK) to (W/mK)
    for(int op=0;op<tempSize;op++)
    {
        if(T>temp[op] && T<temp[op+1])
        {
            conductivity = (tempConductivities[op] + ((tempConductivities[op+1]-
            tempConductivities[op])/(temp[op+1]-temp[op]))*(T-temp[op]))*0.1;
        }
        if(T == temp[op])
        {
            conductivity = tempConductivities[op]*0.1;
        }
    }

    return conductivity;
}


pakk October 27, 2021 13:54

It crashes instantly because you use the temperature, which is not yet defined during initialization. If you initialize with another method, you avoid that problem.

And you open a file for every cell. That is very inefficient: the proper way is to read the file once (with a DEFINE_ON_DEMAND), store the info in a global array, and read from that global array.
But what is causing the crash is that you never close files. Soon you have millions of open files, and the system is not designed for that.


All times are GMT -4. The time now is 23:40.