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 save data to a file!! (https://www.cfd-online.com/Forums/fluent-udf/72509-udf-save-data-file.html)

IvanCFD May 19, 2010 06:38

Hi,

I've checked many times that what there is in that journal file is what I want Fluent to pick up...

What I'm gonna do is to smash both the .cas and .dat files and start over. I'll even delete the folder where they're located.

I haven't run your script because I don't understand the meaning of
13 () no pressure () no
I'll let you know where I end up.

dmoroian May 19, 2010 07:25

Quote:

Originally Posted by IvanCFD (Post 259442)
Hi,

I've checked many times that what there is in that journal file is what I want Fluent to pick up...

What I'm gonna do is to smash both the .cas and .dat files and start over. I'll even delete the folder where they're located.

I haven't run your script because I don't understand the meaning of
13 () no pressure () no
I'll let you know where I end up.

Before you continue with your gui script, try to type in the text interface:
Code:

/file/export
then you will understand the meaning of the entire command from above.
The GUI record macro was just a pedagogical exercise.

IvanCFD May 19, 2010 09:58

2 Attachment(s)
I know what you mean.

Well it doesn't seem to work either.

I've created the journal file attached to this post, following your instructions ad you'll be able to see. However, I keep getting the same error message related to out of malloc memory (see picture attached).

On the other hand, just to see if that malloc memory issue could be solved somehow, I'm running a single precision simulation based on the same case and it seems to work.

I don't know what else I can do.

Do you have idea how many extra iterations per time step I have to run compared to a double precision case?

Cheers,

Ivan.

dmoroian May 19, 2010 11:36

The journal file looks ok, at first glance. Most likely there is something wrong with the case file.
You could ask fluent support for that error.
I don't think it matters how many iterations you run in single precision, if both calculations are converged, the double precision should always be more accurate. However, the difference may sometimes be insignificant.

DarrenC March 8, 2011 08:28

HI dmoroian

I am having a slightly different problem and I was wondering if you know of any other way around it. I need to monitor approximately 5000 points in my simulation per time step.

1.Surface point monitors do not work as they crash due to too many individual points

2. I tried file/transient-export TUI, this works in my PC. But when I load the simulation into a cluster, it does not give me the option to write it into 'ascii' format. Further investigation points out that Fluent Parallel dosent support transient-export in 'ascii' format.

Do you have any idea how else I can monitor so many individual points??

Thanks in advance.

dmoroian March 9, 2011 04:13

Code:

void mapCell(real *x, cell_t *cell, Thread **thread)
{
  Domain *d = Get_Domain(1);
  Thread *t;
  cell_t c;
  real centroid[ND_ND];
  real dist2[ND_ND];
  real min = 1.0e6;

  thread_loop_c(t,d)
  {
      begin_c_loop(c,t)
      {
        C_CENTROID(centroid,c,t);
        NV_VV(dist2,=,centroid,-,x);
        if (min > NV_MAG2(dist2))
        {
            min = NV_MAG2(dist2);
            *cell = c;
            *thread = t;
        }
      }
  }
}

DEFINE_EXECUTE_AT_END(bubu)
{
  static cell_c *mapCells = NULL;
  static Thread **mapThreads = NULL;
  static real points[5000][3] = {{0,0,0},{1,1,1}......};/*this is the vector defining the monitoring points*/
  int i;
  char name[100];
 
  if(NULL == (mapCells = (cell_c*)calloc(5000,sizeof(cell_c))))
      Error("Could not allocate the memory!\n");
  if(NULL == (mapThreads = (Thread**)calloc(5000,sizeof(Thread*))))
      Error("Could not allocate the memory!\n");
 
  for(i = 0; i < 5000; i++)
  {
      mapCell(points[i], &mapCells[i], &mapThreads[i]);
      sprintf(name,"m-%05d.out",i);
      pf = fopen(name,"a");
      fprintf(pf,"%f\n",C_T(mapCells[i],mapThreads[i]));
      fclose(pf);
  } 
}

I didn't know about the limitation with the high number of monitors. So here it is an example using an udf.
Take the above code as it is "just an example". I did not try it, it is not optimized, nor it correctly works in parallel.
Basically it finds the corresponding cell for each monitor and prints out the temperature in a separate file.

I hope this is helpful!

ian.maes October 16, 2012 04:23

Hi,

thank you for providing this example here!
Can I ask you 2 questions about it?
- How do you choose the value of 'min'? 1.0e6 seems like a very big value!
- Is it necessary to retreive the cell location and thread (filled in by mapmyCell) every time-step, or could it be possible to do this upon loading?

Thank you in advance,

Kind regards,

Ian

dmoroian October 16, 2012 15:44

Quote:

Originally Posted by ian.maes (Post 386819)
Hi,
- How do you choose the value of 'min'? 1.0e6 seems like a very big value!

The chosen value ensures that I will find at least one cell.

Quote:

- Is it necessary to retreive the cell location and thread (filled in by mapmyCell) every time-step, or could it be possible to do this upon loading?
All three arrays are declared as "static", so if you fill them once, they will keep their values as long as the udf is loaded into memory.

ian.maes October 18, 2012 04:58

Hi dmoroian,

Thanks for the answers!
This is the code that works, where the coordinates of a line across my domain are read in from xcord.txt.

Code:

#include "udf.h"
static real points[56][3];

DEFINE_EXECUTE_ON_LOADING(print_line,libname)
{
  int m1, m2, i, j;
  FILE *fin;
  m1=56;
  m2=3;
  fin=fopen("xcord.txt","r");
  for (i=0;i<m1;i++)
  {        for (j=0;j<m2;j++)
                fscanf(fin,"%lf\n",&points[i][j]);
  }
  fclose(fin);
  Message ("points is loaded!\n");
}

 
void mapCell(real *x, cell_t *cell, Thread **thread)
{
  Domain *d = Get_Domain(1);
  Thread *t;
  cell_t c;
  real centroid[ND_ND];
  real dist2[ND_ND];
  real min = 1.0e6;

  thread_loop_c(t,d)
  {
      begin_c_loop(c,t)
      {
        C_CENTROID(centroid,c,t);
        NV_VV(dist2,=,centroid,-,x);
        if (min > NV_MAG2(dist2))
        {
            min = NV_MAG2(dist2);
            *cell = c;
            *thread = t;
        }
      }
  end_c_loop(c,t)
  }
}

DEFINE_EXECUTE_AT_END(bubi)
{
  static cell_t *mapCells = NULL;
  static Thread **mapThreads = NULL;
  int i;
  FILE *pf;
 
  if(NULL == (mapCells = (cell_t*)calloc(56,sizeof(cell_t))))
      Error("Could not allocate the memory!\n");
  if(NULL == (mapThreads = (Thread**)calloc(56,sizeof(Thread*))))
      Error("Could not allocate the memory!\n");
 
  if(NULL == (pf = fopen("Ux.txt","a")))
      Error("Could not open file!\n");

  for(i = 0; i < 56; i++)
  {
      mapCell(points[i], &mapCells[i], &mapThreads[i]);
      fprintf(pf,"%f\t",C_T(mapCells[i],mapThreads[i]));
  }
  fprintf(pf,"\n");
  fclose(pf);
}

This works for a serial case, but when I run my case with this code on parallel cores, I get the following error at the end of the first time-step:

Code:

==============================================================================
Stack backtrace generated for node id 999999 (pid = 26808) on signal 11 :
/opt/Fluent13.0.0/ansys_inc/v130/fluent/fluent13.0.0/lnamd64/3ddp_host/fluent.13.0.0[0x129da24]
/opt/Fluent13.0.0/ansys_inc/v130/fluent/fluent13.0.0/lnamd64/3ddp_host/fluent.13.0.0[0x129e7a8]
/lib64/libpthread.so.0[0x37ffa0f500]
libudf/lnamd64/3ddp_host/libudf.so(bubi+0xcb)[0x7f39bd7a1876]
Check the file fluenterror.log for details.
Please include this information with any bug report you file on this issue!
==============================================================================

Can anyone give some suggestions on the origin of this problem and how I can get this to work on a parallel platform, please?

Thank you!

Ian

dmoroian October 18, 2012 08:03

Input/Ouput in parallel is not trivial, and certainly doesn't work straight from the serial implementation.
You'll have to look at the Fluent UDF Manual, chapter 7 "Parallel Considerations" for some more details.
There is a sample of udf debugging on cfd-online wiki: http://www.cfd-online.com/Wiki/Fluen..._udf_using_gdb

A.L.Verminburger June 29, 2013 15:14

Quote:

Originally Posted by dmoroian (Post 258867)
Hi Ivan,
Check chapter 4.1.7 in the user manual for automatic numbering of files, and 4.13 for exporting solution data (I think you're interested in the ASCII format).
The simplest way is to record a macro (check chap. 4.10.1) that exports data from your planes into ASCII format with automatic file naming, and run it periodically into "Calculation activities".

Hi Dragos,

I added the -%t for automated data export at each time step, but it seems to export only once filename-0000 and stops. Do I need to include some sort of a loop structure?

I am using:
Code:

file/export/ascii directory/filename-%t surface () no air-vof () yes
Thanks,
Alfred

dmoroian June 29, 2013 15:52

I would use the command within "execute-commands" and run it periodically every time-step or every iteration.

A.L.Verminburger June 29, 2013 16:44

Quote:

Originally Posted by dmoroian (Post 436808)
I would use the command within "execute-commands" and run it periodically every time-step or every iteration.

Worked like a charm. Thank you, Dragos.

Future reference for others. The full syntax is:
Code:

/solve/execute-commands/add-edit command-1 1 "time-step" "file/export/ascii directory/filename-%t surface () no parameter () yes"

pranab_jha July 17, 2013 09:15

UDF time step in file name
 
Hello friends,

On a related topic, I have been having trouble with saving files from a UDF every n time steps, while running Fluent in parallel in batch mode. Fluent does not allow me to save files in ascii format in parallel mode, so I chose to go with a UDF instead. I found part of the UDF online and from the Fluent manual and have modified the rest to suit my purpose.

Without the time part (between /*** ... ***/) the UDF runs well and gives me the output in desired format. But I want to add a timestep to my filename so that every time step, I have a new file. This does not work. My guess is that the time data is stored somewhere else, while I am trying to get it back from the host.

If you can spare a few minutes, it would be of great help.
Thanks.

Below is the UDF:

#include "udf.h"

#define FNAME "datafile"
#define TSTEP(t) (t % 2)

DEFINE_EXECUTE_AT_END(dod_writer)
{
FILE* fp;

#if !RP_HOST
int cell_counter = 0, dummy, len;
double pressure, cell_center[ND_ND] ;
Domain *d;
Thread *t;
cell_t c;
d = Get_Domain(1); /* Get the domain using Fluent utility */

#else
/***************************
int cur_ts = RP_Get_Integer("time-step");
char str1[] = "file-";
char str2[] = ".dat";

strcat (str1, cur_ts);
strcat (str1, str2);
len = strlen(str1);

Message("\n 1. current time step = %d, name = %s \n", cur_ts, str1);

/* Send time-step and filename to all nodes */
node_to_host_int_1(cur_ts);
host_to_node_string(str1,len); /* remember terminating NUL character ***************************************/
#endif

#if RP_NODE
if(!I_AM_NODE_ZERO_P)PRF_CRECV_INT(myid-1,&dummy,1,myid-1);
fp = fopen(filename,(I_AM_NODE_ZERO_P?"w":"a"));
#else
fp = fopen(filename,"w");
#endif /* for RP_NODE*/

#if !RP_NODE
if (fp!=NULL)
{
Message("\nThe file opened in the working directory \n");
}
else
{
Message("Error in opening file \n");
}
#endif /* for !RP_NODE*/

#if RP_NODE
fprintf(fp,"cell-number X-cord Y-cord Z-cord Pressure");
#endif

#if !RP_HOST
thread_loop_c(t,d)
{
/*Message0("starting the loopn");*/
begin_c_loop_int(c,t)
{
cell_counter++;
C_CENTROID(cell_center,c,t);
pressure=C_P(c,t);

#if RP_2D
fprintf(fp,"n %d %10.3e %10.3e %10.3e",cell_counter, cell_center[0], cell_center[1], pressure);
#else
fprintf(fp,"n %d %10.3e %10.3e %10.3e %10.3e",cell_counter, cell_center[0], cell_center[1], cell_center[2], pressure);
#endif
}
end_c_loop_int(c,t)
}
#endif /* for !RP_HOST*/

#if RP_NODE
fclose(fp);
if(!I_AM_NODE_LAST_P)PRF_CSEND_INT(myid+1,&dummy,1 ,myid);
#else
fclose(fp);
Message("\n The loop ends\n");
#endif /* for RP_NODE*/
}

dmoroian July 17, 2013 14:03

Stack overflow
 
This is a classical example of stack overflow programming error:
Code:

char str1[] = "file-";
char str2[] = ".dat";

strcat (str1, cur_ts);
strcat (str1, str2);

You allocate 6 bytes on the stack for str1, then put a lot more (the content of cur_ts and str2), overwriting some extra memory that most likely contains the code of your function. This generates a random behavior usually ending with a null pointer assignment or segmentation fault.
To solve the problem, just allocate enough memory in str1:
Code:

char str1[200];
sprintf(str1,"file-%d.dat",cur_ts);



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