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 read and file flow variables (https://www.cfd-online.com/Forums/fluent-udf/145240-udf-read-file-flow-variables.html)

pramode77 December 1, 2014 15:18

UDF to read and file flow variables
 
Dear friends

I wish to do the following in Fluent.

1) A certain region of the mesh (called pod_plane) is to be marked out in the geometry/mesh

2)After every 1 sec,say, I wish to write down the flow variables (u and v) in the pod_plane to a file 'pod_file_%f', where %f is the current time value. This means that my file will have rows equal to the number of cells in the pod_plane (say, 20000)and 4 cloumns corrosponding to x, y, u, and v (x and y: cell centroid). In other words, after 10 seconds I will have 10 files of size 20000X4 each.

Accordingly, I have done the following:


1)Created a new zone in the mesh called pod_plane(zoneID = 11).


2) Wrote a UDF to extract the relevant variables at the end of every one second and write them down to files with names as mentioned above. The UDF is attached for your kind perusal.


However I face the following problem: Simulation runs smoothly for the 1st one second after which and before writing the file at the end of it, Fluent shows the error for Segmentation Violation and stops the simulation and generates an empty file. I tried to remedy this by changing the User Defined Memory to higher values and modifications of the UDF etc, but to no avail. Also, due to some strange reason, it disregards the if-loop but executes commands within the loop without looping. I tried changing the if-loop to while, but still the problem persists. I am sure the problem is with the UDF. Can anyone please help me?


Thanks in advance and regards

Given below is my UDF:

#include "udf.h"
#include "dynamesh_tools.h"
#include "stdio.h" /*for input output*/

static int zoneID = 11; /*plane for pod_data*/

DEFINE_EXECUTE_AT_END(Calculation_function)
{
Domain *d = Get_Domain(1);
Thread *t_pod = Lookup_Thread(d,zoneID);
cell_t c_pod;
face_t f_pod;
real time_val = RP_Get_Real("flow-time");
real time_step = RP_Get_Real("physical-time-step");
real x, y, u, v, X[ND_ND], time_previous = time_val;
char buffer[32];

FILE *fp;

if (time_val==time_previous)
{
snprintf(buffer, 32, "pod_file_%3.2f", time_val);
fp = fopen(buffer,"w");
begin_c_loop(c_pod,t_pod)
{
C_CENTROID(X, c_pod, t_pod);
ND_SET(x, y, v, X[0], X[1], C_V(c_pod, t_pod));
u = C_U(c_pod, t_pod);
fprintf (fp, "%lf\t %lf\t %lf\t %lf\n", x, y, u, v);
}
end_c_loop(c_pod,t_pod)
fclose(fp);
time_previous=time_previous+1.000000;
}
}



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