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

store/retrieve using DEFINE_RW_FILE

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 13, 2019, 07:46
Default store/retrieve using DEFINE_RW_FILE
  #1
Member
 
South Yorkshire
Join Date: May 2018
Posts: 32
Rep Power: 4
asking is on a distinguished road
Hi,

I am trying to save some values I calculated using a compiled UDF. For this, I used the DEFINE_RW_MACRO to save the values in the data file (.das). The ideas is to retrieve these values when I open an intermediate time step (.cas and .das file) in Fluent. The problem is that the values are correctly stored in the .das file but when I try to retrieve them they are all zero.

This is the part of my UDF

Code:
FILE *fp;
static real x = 0.0;
static real vx = 0.0;
static real y = 0.0;
static real vy = 0.0;

DEFINE_RW_FILE(read_data, fp)
{
Message0("\nReading user defined data from the data file...\n");
#if PARALLEL
#if RP_HOST
fscanf(fp, "%f", &x);
fscanf(fp, "\n%f", &vx);
fscanf(fp, "\n%f", &y);
fscanf(fp, "\n%f", &vy);
#endif
#else
fscanf(fp, "\n%f", &x);
fscanf(fp, "\n%f", &vx);
fscanf(fp, "\n%f", &y);
fscanf(fp, "\n%f", &vy);
#endif

Message("READER INLOOP: %f %f %f %f\n",x,vx,y,vy);
}

DEFINE_RW_FILE(write_data, fp)
{
Message0("\nWriting user defined data to the data file...\n");
#if PARALLEL
#if RP_HOST
fprintf(fp, "%f", x);
fprintf(fp, "\n%f", vx);
fprintf(fp, "\n%f", y);
fprintf(fp, "\n%f", vy);
#endif
#else
fprintf(fp, "%f", x);
fprintf(fp, "\n%f", vx);
fprintf(fp, "\n%f", y);
fprintf(fp, "\n%f", vy);
#endif
}
The "read_data" is hooked into the Read Data section and the "write_data" is hooked into the Write Data section in Fluent. I checked the .das file and the values are correctly saved at the end of the file:

Code:
(0 "User defined section:")
(3307
5.703351e-05
1.468713e-03
-9.805568e-08
-1.883227e-06)
Here, I the number 3307 is not part of the saved parameters and I don't know what it means but besides that, the other 4 values are the correct stored values. Now, when I open on FLUENT the .cas and .dat files of the last saved time step, DEFINE_RW_FILE(read_data, fp) does not read any of the saved values. The printed message shows that all the values are all equal to zero.

Any ideas of why this is happening? Any ideas would be really appreciated.

Regards,
asking is offline   Reply With Quote

Old   November 14, 2019, 14:09
Default
  #2
Senior Member
 
Join Date: Nov 2013
Posts: 1,472
Rep Power: 20
pakk will become famous soon enough
I think you don't need the newline in your fscan.
pakk is offline   Reply With Quote

Old   November 18, 2019, 08:37
Default
  #3
Senior Member
 
Join Date: Sep 2017
Posts: 223
Rep Power: 8
obscureed is on a distinguished road
One idea is a workaround rather than a solution, but possibly better than exporting to file: you could store the values in RP-variables, and then they are automatically saved with the .cas file.

Did you see in the manual that DEFINE_RW_FILE is not suitable for Windows? (This seems crazy to me, as whatever C compiler is being used will have access to file-IO libraries.)

Another workaround: you could declare your own "static FILE*fp;" as a global variable, with DEFINE_ON_DEMAND routines to fopen and fclose it. In parallel, you would need to make sure that only one process accessed a file -- the host process is the obvious candidate, but you could consider "#if I_AM_NODE_ZERO_P".

If the session is parallel, you will probably want to read the results only into the host process, and then broadcast them using host_to_node_real_4 or whatever. But if the fscanf commands were working, you would expect one of those Message calls to report the correct values.

You can check whether fscanf commands return the expected values, and report which line is in error if they do not.

Good luck!
Ed
obscureed is offline   Reply With Quote

Reply

Tags
fluent - udf, udf code

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



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