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

Fluent UDF to interpolate material properties from Temp and Pressure dependent table

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By Aurelien Thinat

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 10, 2016, 15:54
Default Fluent UDF to interpolate material properties from Temp and Pressure dependent table
  #1
New Member
 
Lauren Blanchette
Join Date: Mar 2016
Posts: 9
Rep Power: 10
lablan is on a distinguished road
Hello,

I am trying to write a UDF for Fluent to define the material properties through interpolation of a 2-D temperature and pressure dependent table. I am unsure if this is possible or if it is possible to write a subroutine to call to MATLAB for the interpolation. Any help is greatly appreciated.

Thank you,

Lauren
lablan is offline   Reply With Quote

Old   August 11, 2016, 06:11
Default
  #2
Senior Member
 
Aurelien Thinat
Join Date: Jul 2010
Posts: 165
Rep Power: 15
Aurelien Thinat is on a distinguished road
Hi Lauren,

Yes it's possible in full UDF. First you have to write a DEFINE_INIT or DEFINE_EXECUTE_ON_DEMAND to put in memory the density (or viscosity, etc...) matrix from an external file :

/ T0 T1 T2 (...)
P0 rho(0,0) rho(1,0) rho(2,0) (...)
P1 rho(0,1) rho(1,1) rho(2,1) (...)
(...)

Then you can define a function performing a bilinear interpolation (https://en.wikipedia.org/wiki/Bilinear_interpolation) .

And finaly you can writea "DEFINE_PROPERTY" that will call this function knowing the cell temperature and pressure and return the cell density.

Aurelien
Aurelien Thinat is offline   Reply With Quote

Old   August 11, 2016, 11:47
Default
  #3
New Member
 
Lauren Blanchette
Join Date: Mar 2016
Posts: 9
Rep Power: 10
lablan is on a distinguished road
Hello Aurelien,

Thank you for your response. I have never written a UDF before and am still trying to understand how they operate. The DEFINE_EXECUTE_ON_DEMAND will call to the external file in which has the property matrix? Does this need to be in any type of format (i.e. excel, cvs, txt.)? Then when this is executed in Fluent, it will save the matrix to later call to in another UDF in which has the bilinear interpolation function?

Thank you again,

Lauren
lablan is offline   Reply With Quote

Old   August 16, 2016, 15:58
Default
  #4
New Member
 
Lauren Blanchette
Join Date: Mar 2016
Posts: 9
Rep Power: 10
lablan is on a distinguished road
Hello,

I have started to write the DEFINE_EXECUTE_ON_DEMAND file to read in the tables, so far I am just using an example table that is a 3X5 matrix and I have saved this as a text document file. I can compile and load the file fine but when I try to execute this UDF, I receive an error stating 'r6002 floating point support not loaded' any help would be greatly appreciated!!

Here is my current define on demand code:

#include "udf.h"

DEFINE_ON_DEMAND(exampledemand)
{
real data[15];
int i;
FILE *rfile; /* declare a FILE pointer */

rfile = fopen("example.txt", "r"); /* open file for reading */
Message("file opened\n");

/* loop to read file */
i=0; /* initialize before to loop */
for (i=0;i<15;i++)
{
fscanf (rfile,"%g",&data[i]); /* that supposes one value per line */
}

fclose(rfile);
Message("file closed\n");
}

I have loaded and successfully executed other UDF for examples so I am unsure why this one is giving me this error

Thanks!

Lauren
lablan is offline   Reply With Quote

Old   August 17, 2016, 09:17
Default
  #5
Senior Member
 
Aurelien Thinat
Join Date: Jul 2010
Posts: 165
Rep Power: 15
Aurelien Thinat is on a distinguished road
Hi Lauren,

Try something like this :

"
float data_temp= 0.;

fscanf(rfile, "%f",&data_temp);

data[i] = data_temp;
"
Aurelien Thinat is offline   Reply With Quote

Old   August 17, 2016, 09:30
Default
  #6
New Member
 
Lauren Blanchette
Join Date: Mar 2016
Posts: 9
Rep Power: 10
lablan is on a distinguished road
Thank you for your reply! I successfully compiled the txt file I believe but now I am unsure how to define the temperature column and pressure row through the compiled file since I am unsure how to view what format it was saved as (matrix or vector?) to use within the bilinear interpolation, using
"
float data_temp= 0.;

fscanf(rfile, "%f",&data_temp);

data[i] = data_temp;
"

I believe this will make the matrix into a vector array, is this correct?
lablan is offline   Reply With Quote

Old   August 18, 2016, 05:29
Default
  #7
Senior Member
 
Aurelien Thinat
Join Date: Jul 2010
Posts: 165
Rep Power: 15
Aurelien Thinat is on a distinguished road
Hi Lauren,

data[Nc] is an array of 1 line and N colum (or 1 column and N lines).

If you want to store a matrix you need to define data as :
Code:
float data[Nc][Nl]
and to store something :

Code:
for i   in 0 to Nc
    for j in 0 to Nl
        data[i][j] = myvalue;
   end
end
Aurelien Thinat is offline   Reply With Quote

Old   August 24, 2016, 17:03
Default
  #8
New Member
 
Lauren Blanchette
Join Date: Mar 2016
Posts: 9
Rep Power: 10
lablan is on a distinguished road
Hello,

So I believe I have been able to write the UDF for one of the properties (density). Now I am just using the same code to try and define the rest of the properties (Cp, Speed of Sound, Viscosity, and Thermal Conductivity) but when I go to upload another UDF it does not seem to let me do this. Is it possible to create more than one UDF to choose from?

Also, I have tried it out with just Density defined through the UDF with all other properties constant and it seems to be running quite slow. I was wondering if anyone had any suggestions on how to make the process computationally faster? I am new at creates UDFs and writing in C so I might have taken the long way around.

Aurelien,

your process seemed to work in terms of created a Temperature and pressure dependent table and then using bilinear interpolation but the only question I have is if it is possible to use a define on demand UDF to upload the tables and then save them for further use? So that at every iteration the tables dont have to be re-read and then interpolated?

Here is the code I am currently using:



#include "udf.h"
#include <stdio.h>
#include <stdlib.h>


DEFINE_PROPERTY(cell_density, c, t)
{

int iP;
int iT;
int NP;
int NT;
int i;
int n;
int m;
//int j;
int k;
double* P;
double* T;
double** H;
double TT = C_T(c, t);
double PP = C_P(c, t);
double H11;
double H12;
double H21;
double H22;
double T1;
double T2;
double P1;
double P2;
double T2T1;
double T2TT;
double P2PP;
double PPP1;
double P2P1;
double TTT1;
double interpD;
double R1;
double R2;


/*matrix
/*Use double , you have floating numbers not int*/
FILE *file;
file = fopen("MetastableDensity.txt", "r");

fscanf(file, "%i %i", &NP, &NT);
fscanf(file, "%i", &k);

P = malloc(NP * sizeof(double*));
T = malloc(NT * sizeof(double*));
H = malloc(NT * sizeof(double*));
for (iT = 0; iT < NT; ++iT)
H[iT] = malloc(NT * sizeof(double));





for (iP = 0; iP < NP; iP++)
{

//Use lf format specifier, %c is for character
if (!fscanf(file, "%lf", &P[iP]))
break;

//printf("%lf ", P[iP]); //Use lf format specifier, \n is for new line
}
//printf("\n");

for (iT = 0; iT < NT; iT++)
{
if (!fscanf(file, "%lf", &T[iT]))
break;
for (iP = 0; iP < NP; iP++)
{
if (!fscanf(file, "%lf", &H[iT][iP]))
break;
//printf("%lf ", H[iT][iP]);
}
//printf("\n");
//printf("%lf ", T[iT]);
}


//printf("\n");
//printf("\n %i \n", NP);
//printf("\n %i \n", NT);
//printf("%lf \n", T[2]);
//printf("%lf \n", H[(NT - 1)][(NP - 1)]);


if (TT < T[0]) {
printf("error - low temp");
}
for (iT = 0; iT < NT; iT++)
{
n = 0 + iT;
if (TT < T[iT])
break;
}

printf("\n %i", n);
printf("\n %lf \n", T[n]);

for (iP = 0; iP < NP; iP++)
{
m = 0 + iP;
if (PP < P[iP])
break;
}

//printf("\n %i", m);
//printf("\n %lf \n", P[m]);
//printf("\n %lf", H[n][m]);

T2 = T[n - 1];
T1 = T[n];
P2 = P[m - 1];
P1 = P[m];
H11 = H[n - 1][m - 1];
H12 = H[n - 1][m];
H21 = H[n][m - 1];
H22 = H[n][m];
T2T1 = T2 - T1;
P2P1 = P2 - P1;
T2TT = T2 - TT;
P2PP = P2 - PP;
PPP1 = PP - P1;
TTT1 = TT - T1;

//printf("\n %lf", TTT1);

R1 = (T2TT / T2T1) * H11 + (TTT1 / T2T1) * H21;
R2 = (T2TT / T2T1) * H12 + (TTT1 / T2T1) * H22;

interpD = (P2PP / P2P1) * R1 + (PPP1 / P2P1) * R2;



return interpD;

fclose(file);

}

Thank you and I appreciate any feedback!


Lauren
lablan is offline   Reply With Quote

Old   August 25, 2016, 07:16
Default
  #9
Senior Member
 
Aurelien Thinat
Join Date: Jul 2010
Posts: 165
Rep Power: 15
Aurelien Thinat is on a distinguished road
Hi Lauren,

Of course it is possible.

In the header of your UDF, you can define global variables :
Code:
#include "udf.h"
#include <stdio.h>
#include <stdlib.h>

/* global variables */

 static int my_global_integer = 18;

DEFINE_PROPERTY(cell_density, c, t)
{
  /* local variables */
  int my_local_integer ;

  my_local_integer = my_global_integer;
 
 (...) 

}
This way, you can define global tables, initialize them with an execute on demand function, and use them in the define_property function.

_____________

For the other question, you can't use several udf files without a header. But you can just write several define_property functions inside 1 file, compile it, and hook all the functions in your model.

Aurelien
holzkiste likes this.
Aurelien Thinat is offline   Reply With Quote

Old   August 25, 2016, 09:14
Default
  #10
New Member
 
Lauren Blanchette
Join Date: Mar 2016
Posts: 9
Rep Power: 10
lablan is on a distinguished road
Thank you for your reply. I will try this technique out. I have to first figure out another dilemma. Is there a way to make specific heat dependent on pressure as well? I believe the DEFINE_SPECIFIC_HEAT option does not allow it since you can't use cell location to get pressure measurement but I didn't know if anyone has figured out how to get around this?
lablan is offline   Reply With Quote

Old   August 25, 2016, 09:27
Default
  #11
Senior Member
 
Aurelien Thinat
Join Date: Jul 2010
Posts: 165
Rep Power: 15
Aurelien Thinat is on a distinguished road
Well it's exact that the Cp is not easy to manipulate. Take a look at the section "8.2.6. User-Defined Real Gas Models (UDRGM)" of the ANSYS Fluent UDF guide. You should be able to properly define your fluid this way, but I have never tried this out.
Aurelien Thinat is offline   Reply With Quote

Old   March 14, 2017, 10:55
Default
  #12
New Member
 
Twan Verweij
Join Date: Feb 2012
Location: Rotterdam, the Netherlands
Posts: 1
Rep Power: 0
twan.verweij is on a distinguished road
Quote:
Originally Posted by Aurelien Thinat View Post
Hi Lauren,

Yes it's possible in full UDF. First you have to write a DEFINE_INIT or DEFINE_EXECUTE_ON_DEMAND to put in memory the density (or viscosity, etc...) matrix from an external file :

/ T0 T1 T2 (...)
P0 rho(0,0) rho(1,0) rho(2,0) (...)
P1 rho(0,1) rho(1,1) rho(2,1) (...)
(...)

Then you can define a function performing a bilinear interpolation (https://en.wikipedia.org/wiki/Bilinear_interpolation) .

And finaly you can writea "DEFINE_PROPERTY" that will call this function knowing the cell temperature and pressure and return the cell density.

Aurelien
Dear Aurelien,

Very interesting comment for us CFX-users who have been able to use RGP-property look-up tables and are looking for something similar in Fluent.
I have a question though with which you might be able to help. Is there a fluent memory structure (like in CFX) to save the data globally at initialisation or does something like that have to be included into a UDF library?

I am guessing the latter is most likely. Let's assume serial solvers for the moment, I would like to try the following:
1. write a udf-script to initialise the data from an external database and store that in a udf-library (~100MB)
2. use subroutines to access the library, apply bilinear interpolation (p-T dependence) and send the property out as a fluent-RGP set for each cell for each timestep.

It is a little hard to figure out if this is possible in fluent, i.e. if udf can be used both as a "database" (initialized at the start) and as the database-interface with the fluid-domain.
The thing I'm trying to avoid here is to load the database at every timestep.
Any thoughts?

Thanks,
Twan
twan.verweij is offline   Reply With Quote

Old   March 20, 2017, 17:49
Default write udf
  #13
New Member
 
fatma badji
Join Date: Jan 2017
Posts: 3
Rep Power: 9
fatma badji is on a distinguished road
hello
i want to write UDF , i have idea who can i write it but my probleme is in witch programme i will write my function can i use fortrant ?
fatma badji is offline   Reply With Quote

Old   June 6, 2019, 06:39
Default
  #14
Member
 
Join Date: Jun 2017
Posts: 39
Rep Power: 8
Large Epic Simulations is on a distinguished road
Does anyone experienced problems about thermodynamic consistency while operating such kind of interpolations?
Large Epic Simulations is offline   Reply With Quote

Old   June 21, 2021, 22:56
Thumbs up
  #15
New Member
 
wangyang
Join Date: Jun 2021
Posts: 3
Rep Power: 4
根瘤囧 is on a distinguished road
Quote:
Originally Posted by lablan View Post
Hello,

So I believe I have been able to write the UDF for one of the properties (density). Now I am just using the same code to try and define the rest of the properties (Cp, Speed of Sound, Viscosity, and Thermal Conductivity) but when I go to upload another UDF it does not seem to let me do this. Is it possible to create more than one UDF to choose from?

Also, I have tried it out with just Density defined through the UDF with all other properties constant and it seems to be running quite slow. I was wondering if anyone had any suggestions on how to make the process computationally faster? I am new at creates UDFs and writing in C so I might have taken the long way around.

Aurelien,

your process seemed to work in terms of created a Temperature and pressure dependent table and then using bilinear interpolation but the only question I have is if it is possible to use a define on demand UDF to upload the tables and then save them for further use? So that at every iteration the tables dont have to be re-read and then interpolated?

Here is the code I am currently using:



#include "udf.h"
#include <stdio.h>
#include <stdlib.h>


DEFINE_PROPERTY(cell_density, c, t)
{

int iP;
int iT;
int NP;
int NT;
int i;
int n;
int m;
//int j;
int k;
double* P;
double* T;
double** H;
double TT = C_T(c, t);
double PP = C_P(c, t);
double H11;
double H12;
double H21;
double H22;
double T1;
double T2;
double P1;
double P2;
double T2T1;
double T2TT;
double P2PP;
double PPP1;
double P2P1;
double TTT1;
double interpD;
double R1;
double R2;


/*matrix
/*Use double , you have floating numbers not int*/
FILE *file;
file = fopen("MetastableDensity.txt", "r");

fscanf(file, "%i %i", &NP, &NT);
fscanf(file, "%i", &k);

P = malloc(NP * sizeof(double*));
T = malloc(NT * sizeof(double*));
H = malloc(NT * sizeof(double*));
for (iT = 0; iT < NT; ++iT)
H[iT] = malloc(NT * sizeof(double));





for (iP = 0; iP < NP; iP++)
{

//Use lf format specifier, %c is for character
if (!fscanf(file, "%lf", &P[iP]))
break;

//printf("%lf ", P[iP]); //Use lf format specifier, \n is for new line
}
//printf("\n");

for (iT = 0; iT < NT; iT++)
{
if (!fscanf(file, "%lf", &T[iT]))
break;
for (iP = 0; iP < NP; iP++)
{
if (!fscanf(file, "%lf", &H[iT][iP]))
break;
//printf("%lf ", H[iT][iP]);
}
//printf("\n");
//printf("%lf ", T[iT]);
}


//printf("\n");
//printf("\n %i \n", NP);
//printf("\n %i \n", NT);
//printf("%lf \n", T[2]);
//printf("%lf \n", H[(NT - 1)][(NP - 1)]);


if (TT < T[0]) {
printf("error - low temp");
}
for (iT = 0; iT < NT; iT++)
{
n = 0 + iT;
if (TT < T[iT])
break;
}

printf("\n %i", n);
printf("\n %lf \n", T[n]);

for (iP = 0; iP < NP; iP++)
{
m = 0 + iP;
if (PP < P[iP])
break;
}

//printf("\n %i", m);
//printf("\n %lf \n", P[m]);
//printf("\n %lf", H[n][m]);

T2 = T[n - 1];
T1 = T[n];
P2 = P[m - 1];
P1 = P[m];
H11 = H[n - 1][m - 1];
H12 = H[n - 1][m];
H21 = H[n][m - 1];
H22 = H[n][m];
T2T1 = T2 - T1;
P2P1 = P2 - P1;
T2TT = T2 - TT;
P2PP = P2 - PP;
PPP1 = PP - P1;
TTT1 = TT - T1;

//printf("\n %lf", TTT1);

R1 = (T2TT / T2T1) * H11 + (TTT1 / T2T1) * H21;
R2 = (T2TT / T2T1) * H12 + (TTT1 / T2T1) * H22;

interpD = (P2PP / P2P1) * R1 + (PPP1 / P2P1) * R2;



return interpD;

fclose(file);

}

Thank you and I appreciate any feedback!


Lauren
This question is very helpful for us, but at the same time it is not easy to understand because the 'MetastableDensity.txt' is unknown. Therefore, I sincerely hope that you can shown me the content of the 'MetastableDensity.txt'. Thank you very much!
根瘤囧 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
Ansys CFX problem: unexpected very high temperatures in premix laminar combustion faizan_habib7 CFX 4 February 1, 2016 17:00
unsteady pressure, temp profile without a udf mahi FLUENT 0 November 10, 2008 03:53


All times are GMT -4. The time now is 14:10.