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/)
-   -   UDF to record FLUENT Solver variables... (https://www.cfd-online.com/Forums/fluent-udf/72227-udf-record-fluent-solver-variables.html)

mariachi January 29, 2010 06:09

UDF to record FLUENT Solver variables...
 
dear friends,

Fluent provides macros to access solution variables like velocity etc. These macros are used in writing UDF to access any variable of interest.

I am doing LES, and i want to capture the fluctuations in velocity. If i write a UDF to access the velocity variable provided in these macros, i can observe the fluctuating component of velocity which will offcourse be not observed once the same UDF is linked to URANS case. Is that right? Correct me if i am wrong.

Plz anyone help me in writing the UDF or give me if someone has done some similar work, i dont know how to write the UDF!

Waiting patiently for your quick reply :)

DoHander January 30, 2010 13:00

Maybe this will help you:

http://www.cfd-online.com/Forums/flu...ome-nodes.html

it is actually a Scheme macro and not a UDF, but you can use this in Fluent if you want to save some data at every time step.

I suspect you want to record the velocity fluctuations in some particular region and not on your entire mesh (if you have a 3d problem and try to save these at every point at each time step you will end up with some GB of data).

Do

mariachi January 31, 2010 05:46

Hey Do thanks for ur input,

Yeah exactly this is the problem, i want to capture velocity fluctuations at a single cell or face or node, not the entire domain or the entire zone (mesh is 10 mill so hard disk is gonna start crying haha).

I saw ur link, i havent looked into scheme programming before so ill do check it out as well. But i have heard that for my particular problem, i have to write UDF. Nobody mentioned scheme macro, but surely ill see them as well.

I have been studying UDF for the past couple of days, i can use the DEFINE_EXECUTE_AT_END macro which executes the UDF at the end of each time step. Now i am facing 2 problems in the UDF,

1. How to tell Fluent to pick up say x property at y cell? I have used UDF's which do this, but they either do it for the entire domain or the entire zone. I couldnt find any which does the operation on a single cell...

2. After that i want the UDF to write that value to say data.out or data.txt file on the hard disk? I have used UDF's that print the data onto ur Fluent screen after every time step, but i dont want that...

Can u help...

DoHander January 31, 2010 13:02

"2. After that i want the UDF to write that value to say data.out or data.txt file on the hard disk? I have used UDF's that print the data onto ur Fluent screen after every time step, but i dont want that..."

Open a file and use fprintf instead of Message or printf ... This should be basic for any C programmer (this will write an integer to data.txt):

FILE *fp;
fp=fopen("data.txt","w");
fprintf(fp,"%d\n",int_test);
..............

You should probably move the part that opens the file in a EXECUTE_ON_DEMAND and the part that actually writes in EXECUTE_AT_END, and keep *fp as a global variable in your UDF.

Do

mariachi February 1, 2010 11:30

Thankyou so much,
Ill check it out and give u the input, hope i dont have to ask any more questions :)

dmoroian February 1, 2010 13:37

Monitors
 
Quote:

Originally Posted by mariachi (Post 244348)
... i want to capture velocity fluctuations at a single cell or face or node...

Hi Mariachi,
Why don't you use monitors for this purpose? You will avoid writing udf's!

Dragos

mariachi February 2, 2010 12:54

Hey Dragos,

If u read my original post, i want to do this to show the difference of how the NS equations are solved for LES and URANS. In URANS equations, although we do take the time derivative, but its the time derivative of mean velocity "u bar". But in LES, time derivative of actual velocity is taken which includes the fluctuating part as well.

So i want to access this velocity variable "u" solved in the equations. for URANS it will show no fluctuations, for LES it will show fluctuations.Since Fluent calculates all values at the centroid of each cell, i want to record this value at a single cell at each time step. No such option is available in the monitors.

Also i have heard from everybody that u have to write a UDF to accomplish this task. Now i dont exactly know that the approach which i am taking, which i described above, is correct because my theoretical knowledge about the background equations which Fluent solves is not so good. Plz tell me if im wrong???

Thanks

dmoroian February 4, 2010 06:06

I still think that you may use monitors for this, but if you want udf's...here it comes an example:
Code:

#include "udf.h"
DEFINE_ON_DEMAND(bubu)
{
  real pos[ND_ND] = {1,2,3};/*the position of the monitoring point*/
  static cell_t c0 = -1;/*index of the cell containing the monitoring point*/
  cell_t c;
  static Thread *t0 = NULL;/*thread of the cell containing the monitoring point*/
  Thread *t;
  real x[ND_ND], d2 = 1.0e9, d20 = 1.0e9;
  FILE *pf;
 

  Domain *d = Get_Domain(1);
 
  /*find once the thread and index of the cell containing the monitoring point*/
  if(NULL == t0)
      thread_loop_c(t,d)
      {
        begin_c_loop(c,t)
        {
            C_CENTROID(x,c,t);
            NV_V(x,-=,pos);
            d2 = NV_MAG2(x);
            if(d2 < d20)
            {
              d20 = d2;
              t0 = t;
              c0 = c;
            }
        }
        end_c_loop(c,t)
      }
  if(NULL == (pf = fopen("Ux.txt","a")))
      Error("Could not open file!\n");
  fprintf(pf,"%e\t%e\n",CURRENT_TIME, C_U(c0,t0));
  fclose(pf);
}

Note: I did not try even to compile this, so use it as guidance!

sbaffini February 9, 2010 06:39

As stated by dmoroian, the most obvious way to do this is by defining a monitor point (unless you don't want the velocity in a whole region). There is no difference in the turbulence model you are going to use, you just need to monitor the velocity in a specified point...and of course you have to write the result in a file, instead of printing it on the console

mariachi February 19, 2010 12:25

Thanks for ur guidance,

I have used point monitors and it seems its working well as i wanted. So far so good :)

athalia September 23, 2019 14:34

Quote:

Originally Posted by dmoroian (Post 244918)
I still think that you may use monitors for this, but if you want udf's...here it comes an example:
Code:

#include "udf.h"
DEFINE_ON_DEMAND(bubu)
{
  real pos[ND_ND] = {1,2,3};/*the position of the monitoring point*/
  static cell_t c0 = -1;/*index of the cell containing the monitoring point*/
  cell_t c;
  static Thread *t0 = NULL;/*thread of the cell containing the monitoring point*/
  Thread *t;
  real x[ND_ND], d2 = 1.0e9, d20 = 1.0e9;
  FILE *pf;
 

  Domain *d = Get_Domain(1);
 
  /*find once the thread and index of the cell containing the monitoring point*/
  if(NULL == t0)
      thread_loop_c(t,d)
      {
        begin_c_loop(c,t)
        {
            C_CENTROID(x,c,t);
            NV_V(x,-=,pos);
            d2 = NV_MAG2(x);
            if(d2 < d20)
            {
              d20 = d2;
              t0 = t;
              c0 = c;
            }
        }
        end_c_loop(c,t)
      }
  if(NULL == (pf = fopen("Ux.txt","a")))
      Error("Could not open file!\n");
  fprintf(pf,"%e\t%e\n",CURRENT_TIME, C_U(c0,t0));
  fclose(pf);
}

Note: I did not try even to compile this, so use it as guidance!


Hello @dmoroian,

it's been over 9 years! But I hope you could answer two questions about your code.

1. I believe in

Code:

C_CENTROID(x,c,t);
            NV_V(x,-=,pos);
            d2 = NV_MAG2(x);
            if(d2 < d20)
            {
              d20 = d2;
              t0 = t;
              c0 = c;
            }

you calculate the square of the magnitude of vector "x - pos". In order to find the cell with given coordinates "pos", why do you compare the square of the magnitude to a million?! Is is a typo and you meant 1e-9?

2. I want to use your code to print out the UDF output in a particular cell. How can I adapt the following line it your code? If there isn't a macro defined for the value, I want to store (like C_U(c0, t0) ), how can I store if to a text data?

Code:

DEFINE_EXCHANGE_PROPERTY(drag, cell, mix_thread, s_col, f_col)
{
/*calculate drag coefficient drag_coeff*/
                return drag_coeff;
}

DEFINE_ON_DEMAND(bubu)
{
/*[...]
*/
        fprintf(pf, "%e\t%e\n", CURRENT_TIME, C_U(c0, t0));
/*[...]
*/
}


AlexanderZ September 24, 2019 00:07

write to file
https://www.programiz.com/c-programm...e-input-output

best regards


All times are GMT -4. The time now is 08:04.