CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   FLUENT (https://www.cfd-online.com/Forums/fluent/)
-   -   Print data from UDF to txt file (https://www.cfd-online.com/Forums/fluent/235773-print-data-udf-txt-file.html)

fazzesco April 28, 2021 05:30

Print data from UDF to txt file
 
Hi everybody,
I am writing an UDF in order to update the contact angle between droplet phases of a droplet impacting a wall.
The UDF seems to do not work properly, so I tried to print in a txt file the informations I ned, but the the UDF does not print anything. Does anyone know what can be the problem? Is there a specific folder where I Should save the txt file?

PS if anyone has some other idea for the UDF, please share :)
Thank you all :)

My UDF is this
#include "udf.h"
double contact_line_position=0;
double time=0;
double dynamic_contact_angle=15.0;
double contact_velocity=0;
FILE *file_export;
double sum=0.0,R;

DEFINE_ADJUST(Contact_Angle_Update, domain)
{
Thread *thread = Lookup_Thread(domain, 12);
Thread **pt = THREAD_SUB_THREADS(thread);
cell_t cell;
face_t f;
real x[ND_ND];
double max_x=0;
FILE *file_export_2;
int n;
sum=0.0;

begin_c_loop_all (cell,pt[1])
{
if(C_VOF(cell,pt[1])!=0)
{
C_CENTROID(x,cell,pt[1]);
//printf("## x=%f y=%f z=%f vof=%f\n",x[0],x[1],x[2],C_VOF(cell,pt[1]));
if(x[1]>max_x)
max_x=x[1];
}
sum+=C_VOF(cell,pt[1]);
}
end_c_loop_all (cell,pt[1])

R=sqrt(sum*1.0e-08/M_PI);
printf("max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f sum=%f\n",max_x,R,contact_velocity,time,dynamic_co ntact_angle,sum);

contact_velocity= (R-contact_line_position)/(RP_Get_Real("flow-time")-time);
contact_line_position = R;
time=RP_Get_Real("flow-time");

if(contact_velocity>=0)
dynamic_contact_angle= (120.0*M_PI/180.0) ;
else
dynamic_contact_angle= (65.0*M_PI/180.0) ;


file_export = fopen("file_export.txt", "a+");
fprintf(file_export,"max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f\n",max_x,R,contact_veloci ty,time,dynamic_contact_angle);
fclose(file_export);


file_export_2 = fopen("file_export_2.txt", "a+");
fprintf(file_export_2,"max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f\n",max_x,R,contact_veloci ty,time,dynamic_contact_angle);
fclose(file_export_2);

}

DEFINE_PROFILE(Contact_Angle_Set_Profile,t,i)
{
face_t f;
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = dynamic_contact_angle;
}
end_f_loop(f,t)
}

AlexanderZ April 29, 2021 01:12

if you are using fluent version 2019 and above you must make code in parallel

compile following code:
Code:

#include "udf.h"
real contact_line_position=0;
real time=0;
real dynamic_contact_angle=15.0;
real contact_velocity=0;
real sum=0.0,R;

DEFINE_ADJUST(Contact_Angle_Update, domain)
{
Thread *thread = Lookup_Thread(domain, 12);
Thread **pt = THREAD_SUB_THREADS(thread);
cell_t cell;
face_t f;
real x[ND_ND],cur_time;
real max_x=0;
FILE *file_export, *file_export_2;
int n;
sum=0.0;
cur_time = CURRENT_TIME;
begin_c_loop_all (cell,pt[1])
{
if(C_VOF(cell,pt[1])!=0)
{
C_CENTROID(x,cell,pt[1]);
//printf("## x=%f y=%f z=%f vof=%f\n",x[0],x[1],x[2],C_VOF(cell,pt[1]));
if(x[1]>max_x)
max_x=x[1];
}
sum+=C_VOF(cell,pt[1]);
}
end_c_loop_all (cell,pt[1])
#if RP_NODE
        sum = PRF_GRSUM1(sum);
#endif
R=sqrt(sum*1.0e-08/M_PI);
Message0("max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f sum=%f\n",max_x,R,contact_velocity,time,dynamic_contact_angle,sum);

contact_velocity= (R-contact_line_position)/(cur_time-time);
contact_line_position = R;
time=cur_time;

if(contact_velocity>=0)
dynamic_contact_angle= (120.0*M_PI/180.0) ;
else
dynamic_contact_angle= (65.0*M_PI/180.0) ;

node_to_host_real_5(max_x,R,contact_velocity,time,dynamic_contact_angle);

#if RP_HOST
file_export = fopen("file_export.txt", "a+");
fprintf(file_export,"max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f\n",max_x,R,contact_velocity,time,dynamic_contact_angle);
fclose(file_export);


file_export_2 = fopen("file_export_2.txt", "a+");
fprintf(file_export_2,"max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f\n",max_x,R,contact_velocity,time,dynamic_contact_angle);
fclose(file_export_2);
#endif
}

DEFINE_PROFILE(Contact_Angle_Set_Profile,t,i)
{
face_t f;
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = dynamic_contact_angle;
}
end_f_loop(f,t)
}


fazzesco April 29, 2021 02:49

Quote:

Originally Posted by AlexanderZ (Post 802773)
if you are using fluent version 2019 and above you must make code in parallel

compile following code:
Code:

#include "udf.h"
real contact_line_position=0;
real time=0;
real dynamic_contact_angle=15.0;
real contact_velocity=0;
real sum=0.0,R;

DEFINE_ADJUST(Contact_Angle_Update, domain)
{
Thread *thread = Lookup_Thread(domain, 12);
Thread **pt = THREAD_SUB_THREADS(thread);
cell_t cell;
face_t f;
real x[ND_ND],cur_time;
real max_x=0;
FILE *file_export, *file_export_2;
int n;
sum=0.0;
cur_time = CURRENT_TIME;
begin_c_loop_all (cell,pt[1])
{
if(C_VOF(cell,pt[1])!=0)
{
C_CENTROID(x,cell,pt[1]);
//printf("## x=%f y=%f z=%f vof=%f\n",x[0],x[1],x[2],C_VOF(cell,pt[1]));
if(x[1]>max_x)
max_x=x[1];
}
sum+=C_VOF(cell,pt[1]);
}
end_c_loop_all (cell,pt[1])
#if RP_NODE
        sum = PRF_GRSUM1(sum);
#endif
R=sqrt(sum*1.0e-08/M_PI);
Message0("max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f sum=%f\n",max_x,R,contact_velocity,time,dynamic_contact_angle,sum);

contact_velocity= (R-contact_line_position)/(cur_time-time);
contact_line_position = R;
time=cur_time;

if(contact_velocity>=0)
dynamic_contact_angle= (120.0*M_PI/180.0) ;
else
dynamic_contact_angle= (65.0*M_PI/180.0) ;

node_to_host_real_5(max_x,R,contact_velocity,time,dynamic_contact_angle);

#if RP_HOST
file_export = fopen("file_export.txt", "a+");
fprintf(file_export,"max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f\n",max_x,R,contact_velocity,time,dynamic_contact_angle);
fclose(file_export);


file_export_2 = fopen("file_export_2.txt", "a+");
fprintf(file_export_2,"max_x=%f R=%f contact_velocity=%f time=%f dynamic_contact_angle=%f\n",max_x,R,contact_velocity,time,dynamic_contact_angle);
fclose(file_export_2);
#endif
}

DEFINE_PROFILE(Contact_Angle_Set_Profile,t,i)
{
face_t f;
begin_f_loop(f,t)
{
F_PROFILE(f,t,i) = dynamic_contact_angle;
}
end_f_loop(f,t)
}


Hi Alexander, thank you for your help.
Yes I am using Fluent 19.2. At the program strating, I set it for use in parallele. But, when I try to load the new library with your UDF code, Fluent shows me this error:

Error: The UDF library you are trying to load (libudf) is not compiled for parallel use on the current platform (win64).\n\nImpossibile trovare il file specificato.
\n\nC:\Users\giova\Desktop\Prova UDF da forum\libudf\win64\2ddp_host\libudf.dll
Error Object: #f

Do you know what can be the mistake? Thank you for you help

fazzesco April 29, 2021 03:13

Sorry, the error was due to the having saved the UDF with a name with spaces.
Now I loaded it, but still does not save the data in the txt files. :(
Do you have any idea?


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