-
-
May 15, 2025 at 4:13 pm
tmkhare
SubscriberHello all,
I'm trying to write a simple UDF to extract the face area magnitude and outlet flow rate for the outlet face of a straight pipe. It is a transient simulation. The problem I'm facing is that the UDF is giving output of 0.0 for both parameters mentioned above, randomly for certain timesteps. I agree that the outlet flow rate value can be zero at certain time steps but the face area magnitude cannot be zero ever, and that's what is puzzling me as to why it is giving a 0.0 value as output.
The UDF is executed at the end of every time step. I print the values extracted by the UDF in a text file at every time step. Eventually I want to use these values extracted by the UDF for calculations performed by a 3-element Windkessel model at every time step, and therefore it is important that the UDF extracts correct values at every time step.
Pipe diameter = 4cm, length = 30cm
Inlet BC: transient velocity profile
Outlet BC: transient pressure profile
Time step = 0.001 s
I have attached photos of the geometry, the inlet and outlet boundary conditions profiles, the UDF code written in C that I'm using, and a photo of the output file which shows the erroneous output values (highlighted in yellow). I would really appreciate if someone can point what is going wrong with the UDF.You can access th UDF code written in C from the link: UDF Code for Outlet Face Data Extraction
Looking forward to hearing from you all.
Thank you.
-
May 16, 2025 at 8:00 am
Rob
Forum ModeratorStaff aren't allowed to follow links or download files. I've not seen any similar issues so suspect there's something in your code. Why are you using a UDF rather than an Expression/Report Definition and monitor?
-
May 16, 2025 at 2:07 pm
tmkhare
SubscriberHi Rob,
I'm using a UDF to extract the flow rate information for outlet face as a trial. I eventually want to implement a 3-element Windkessel model at the outlet using a UDF and that model requires flow rate information from outlet face as an input at every time step. Therefore, this is a trial run to see if I can extract values of flow rate correctly using a UDF. Next step would be to jump to a more complex UDF. Considering you can't download the UDF code, I have inserted it below. Please let me know if you find anything is wrong in it. Thank you.
#include "udf.h"
#includeDEFINE_EXECUTE_AT_END(print_outlet_flow)
{
Domain *d;
Thread *t;
face_t f;
real area_vec[ND_ND];
real face_area;
real vol_flow;
real total_area = 0.0;
real total_vol_flow = 0.0;
static int first = 1;
FILE *fp;
int outlet_zone_id = 6; /*zone-ID of outlet face*//* get the domain and the outlet thread */
d = Get_Domain(1);
t = Lookup_Thread(d, outlet_zone_id);/* open file once for header, then append thereafter */
if (first)
{
fp = fopen("outlet_flow.txt", "w");
if (!fp) { Message("Error: could not open outlet_flow.txt for writing\n"); return; }
fprintf(fp, "Time\tTotalArea\tTotalVolFlow\n");
first = 0;
}
else
{
fp = fopen("outlet_flow.txt", "a");
if (!fp) { Message("Error: could not open outlet_flow.txt for appending\n"); return; }
}/* loop over all faces in the outlet thread */
begin_f_loop(f, t)
{
/* get face‐area vector and magnitude */
F_AREA(area_vec, f, t);
face_area = NV_MAG(area_vec);/* get volumetric flux through this face */
vol_flow = F_FLUX(f, t);total_area = total_area + face_area;
total_vol_flow = total_vol_flow + vol_flow;
}
end_f_loop(f, t);/* write time, total area, and total volumetric flow rate */
fprintf(fp, "%e\t%e\t%e\n",
CURRENT_TIME,
total_area,
total_vol_flow);fclose(fp);
}
-
-
May 16, 2025 at 2:22 pm
Rob
Forum ModeratorYou may want to read up on parallel effects as that may alter how you write some of this. Looking at the code, it looks like the area & flow loop isn't being triggered so zero is returned to the fprintf section. Not sure why, and debugging isn't something staff will get involved with: the wider community are free to do so.
-
- You must be logged in to reply to this topic.
-
3139
-
1007
-
918
-
858
-
792
© 2025 Copyright ANSYS, Inc. All rights reserved.