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

UDF save data to a file!!

Register Blogs Community New Posts Updated Threads Search

Like Tree5Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   May 19, 2010, 06:38
Default
  #21
Member
 
Ivan
Join Date: Aug 2009
Posts: 63
Rep Power: 16
IvanCFD is on a distinguished road
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.
IvanCFD is offline   Reply With Quote

Old   May 19, 2010, 07:25
Default
  #22
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
Quote:
Originally Posted by IvanCFD View Post
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.
dmoroian is offline   Reply With Quote

Old   May 19, 2010, 09:58
Default
  #23
Member
 
Ivan
Join Date: Aug 2009
Posts: 63
Rep Power: 16
IvanCFD is on a distinguished road
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.
Attached Images
File Type: jpg error3.jpg (68.9 KB, 49 views)
Attached Files
File Type: zip store_data_ascii.jou.zip (1.3 KB, 17 views)
IvanCFD is offline   Reply With Quote

Old   May 19, 2010, 11:36
Default
  #24
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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.
dmoroian is offline   Reply With Quote

Old   March 8, 2011, 08:28
Default
  #25
Member
 
Join Date: Sep 2009
Posts: 69
Rep Power: 16
DarrenC is on a distinguished road
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.
DarrenC is offline   Reply With Quote

Old   March 9, 2011, 04:13
Default
  #26
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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!
mm.abdollahzadeh likes this.
dmoroian is offline   Reply With Quote

Old   October 16, 2012, 04:23
Default
  #27
New Member
 
Ian Maes
Join Date: Oct 2011
Posts: 10
Rep Power: 14
ian.maes is on a distinguished road
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
athalia likes this.
ian.maes is offline   Reply With Quote

Old   October 16, 2012, 15:44
Default
  #28
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
Quote:
Originally Posted by ian.maes View Post
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.
dmoroian is offline   Reply With Quote

Old   October 18, 2012, 04:58
Default
  #29
New Member
 
Ian Maes
Join Date: Oct 2011
Posts: 10
Rep Power: 14
ian.maes is on a distinguished road
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
ian.maes is offline   Reply With Quote

Old   October 18, 2012, 08:03
Default
  #30
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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
dmoroian is offline   Reply With Quote

Old   June 29, 2013, 15:14
Default
  #31
New Member
 
Join Date: Jun 2013
Posts: 7
Rep Power: 12
A.L.Verminburger is on a distinguished road
Quote:
Originally Posted by dmoroian View Post
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
A.L.Verminburger is offline   Reply With Quote

Old   June 29, 2013, 15:52
Default
  #32
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
I would use the command within "execute-commands" and run it periodically every time-step or every iteration.
dmoroian is offline   Reply With Quote

Old   June 29, 2013, 16:44
Default
  #33
New Member
 
Join Date: Jun 2013
Posts: 7
Rep Power: 12
A.L.Verminburger is on a distinguished road
Quote:
Originally Posted by dmoroian View Post
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"
mm.abdollahzadeh likes this.
A.L.Verminburger is offline   Reply With Quote

Old   July 17, 2013, 09:15
Default UDF time step in file name
  #34
Member
 
pranab_jha's Avatar
 
Pranab N Jha
Join Date: Nov 2009
Location: Houston, TX
Posts: 86
Rep Power: 16
pranab_jha is on a distinguished road
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*/
}
pranab_jha is offline   Reply With Quote

Old   July 17, 2013, 14:03
Default Stack overflow
  #35
Senior Member
 
dmoroian's Avatar
 
Dragos
Join Date: Mar 2009
Posts: 648
Rep Power: 20
dmoroian is on a distinguished road
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);
dmoroian 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
Working directory via command line Luiz CFX 4 March 6, 2011 20:02
UDF Getting data from file jreig FLUENT 0 July 10, 2009 08:07
Smallest binary file format to save large data Zonexo Main CFD Forum 2 June 2, 2008 20:25
[blockMesh] Axisymmetrical mesh Rasmus Gjesing (Gjesing) OpenFOAM Meshing & Mesh Conversion 10 April 2, 2007 14:00
UDF (write a data file) problem lichun Dong FLUENT 2 July 29, 2005 11:39


All times are GMT -4. The time now is 01:48.