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

UDF for collecting Face data of secondary phase

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

Like Tree2Likes
  • 1 Post By obscureed
  • 1 Post By obscureed

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 13, 2018, 00:57
Default UDF for collecting Face data of secondary phase
  #1
New Member
 
Dinushke
Join Date: Aug 2013
Location: Victoria, Australia
Posts: 11
Rep Power: 9
dean993 is on a distinguished road
Hi Everyone,

I am trying to write a UDF to export (fprintf) data (velocity and pressure) of the faces from an interior boundary (ID=3). My model is a two phase steady state VOF Model. I need to only collect the face data from the secondary phase (water) so I have used a "If" function within the "begin_f_loop" to only write face data when the density is over 900. However, I am getting "Error: received a fatal signal (Segmentation fault)" when I run the UDF. I was hoping someone could have a look and help me out. Any help would be appreciated.
Thank you,
Regards,
Dean


#include "udf.h"

DEFINE_ON_DEMAND(densityf)
{
Domain *d;
face_t f;
real dp=0;
real v[3];
d = Get_Domain(1);
Thread *t = Lookup_Thread(d,3);
FILE *fw;
FILE *fa;
fw = fopen("vw.txt","w+");
fa = fopen("va.txt","w+");
thread_loop_f(t,d)
{
begin_f_loop(f,t)
{
dp = F_R(f,t);
v[0]=F_U(f,t);
v[1]=F_V(f,t);
v[2]=F_W(f,t);
if (dp>900)
{
fprintf(fw,"%.2f %.2f %.2f %.2f\n",dp,v[0],v[1],v[2]);
}
else
{
fprintf(fa,"Skip %.2f\n",dp);
}
}
}
end_f_loop(f,t)
fclose(fw);
fclose(fa);
}
dean993 is offline   Reply With Quote

Old   March 13, 2018, 11:24
Default
  #2
Senior Member
 
Join Date: Sep 2017
Posts: 223
Rep Power: 7
obscureed is on a distinguished road
Hi Dean993,

First comment: if Fluent is running in parallel, anything involving fopen and fprintf is difficult or impossible, unless you use preprocessor directives to restrict those commands to one process (such as the host or maybe node0). Maybe you can imagine the trouble when several processes in parallel try to own and write to a single file simultaneously. As a first step, consider Message(...) to send text to the screen, and save that to a transcript and fish out the interesting lines using grep.

Second comment: multiphase is complicated by the fact that the superthread of each cell zone contains some data, but the phase subthreads contain other data. Trying to look up non-existent data is a common cause of segmentation violations. You are currently finding and using the superthread only. The split of which data fields are in super- or sub-threads is highly model-dependent. If I had to guess, I might predict that F_U, F_V and F_W are indeed present at faces in a classic VOF model, but density might not be.

The pedestrian way to debug this, used even by very experienced UDF programmers, is to put lots of lines Message0("reached point 1\n") and so on in your code, and hence find the killer line(s). A slightly more elegant way would be to check whether data is allocated:
if(NULLP(THREAD_STORAGE(ft, SV_U))) Message("trouble expected with F_U\n");
if(NULLP(THREAD_STORAGE(ft, SV_DENSITY))) Message("trouble expected with F_R\n");

Good luck!
Ed
dean993 likes this.
obscureed is offline   Reply With Quote

Old   March 14, 2018, 03:27
Default
  #3
New Member
 
Dinushke
Join Date: Aug 2013
Location: Victoria, Australia
Posts: 11
Rep Power: 9
dean993 is on a distinguished road
Hi Ed,

I really appreciate your advice. As you have mentioned, I wrote a UDF "nullp_check" (see below) in Fluent serial mode, to see if the thread that I am getting has the data (velocity, pressure, density and VOF) I need from the interior face (ID=3). I found that only density was available in that thread and interestingly, if I change the Lookup_thread ID to a wall, I get all the data except the VOF. Which makes sense since VOF is in the phase thread. Anyways, I really have run out of ideas how to tackle this issue. My aim is to get the average water (secondary phase) velocity and pressure from an interior face. Any ideas if this is possible?

Thanks,
Dean


**************************
#include "udf.h"
#include "mem.h"
DEFINE_ON_DEMAND(nullp_check)
{
Domain *d;
face_t f;
d = Get_Domain(1);
Thread *t = Lookup_Thread(d,3);
Message("\nBefore_Storage_Check\n");

/*begin_f_loop(f,t)*/
/*{*/
if(NULLP(THREAD_STORAGE(t, SV_U)))
{
Message("NO U-velocity\n");
}
if(NULLP(THREAD_STORAGE(t, SV_V)))
{
Message("NO V-velocity\n");
}
if(NULLP(THREAD_STORAGE(t, SV_W)))
{
Message("NO W-velocity\n");
}
if(NULLP(THREAD_STORAGE(t, SV_VOF)))
{
Message("NO VOF\n");
}
if(NULLP(THREAD_STORAGE(t, SV_P)))
{
Message("NO Pressure\n");
}
if(NULLP(THREAD_STORAGE(t, SV_DENSITY)))
{
Message("NO Density\n");
}
/*}*/
/*end_f_loop(f,t)*/
Message("After_Storage_Check\n");
}
**************************


Quote:
Originally Posted by obscureed View Post
Hi Dean993,

First comment: if Fluent is running in parallel, anything involving fopen and fprintf is difficult or impossible, unless you use preprocessor directives to restrict those commands to one process (such as the host or maybe node0). Maybe you can imagine the trouble when several processes in parallel try to own and write to a single file simultaneously. As a first step, consider Message(...) to send text to the screen, and save that to a transcript and fish out the interesting lines using grep.

Second comment: multiphase is complicated by the fact that the superthread of each cell zone contains some data, but the phase subthreads contain other data. Trying to look up non-existent data is a common cause of segmentation violations. You are currently finding and using the superthread only. The split of which data fields are in super- or sub-threads is highly model-dependent. If I had to guess, I might predict that F_U, F_V and F_W are indeed present at faces in a classic VOF model, but density might not be.

The pedestrian way to debug this, used even by very experienced UDF programmers, is to put lots of lines Message0("reached point 1\n") and so on in your code, and hence find the killer line(s). A slightly more elegant way would be to check whether data is allocated:
if(NULLP(THREAD_STORAGE(ft, SV_U))) Message("trouble expected with F_U\n");
if(NULLP(THREAD_STORAGE(ft, SV_DENSITY))) Message("trouble expected with F_R\n");

Good luck!
Ed
dean993 is offline   Reply With Quote

Old   March 14, 2018, 06:01
Default
  #4
Senior Member
 
Join Date: Sep 2017
Posts: 223
Rep Power: 7
obscureed is on a distinguished road
OK, you're making progress.

So I was 100% wrong in my predictions of which variables would be available? Oh well, never mind -- it is very model-dependent, and this is why you are doing the checks. Maybe this is not a classic VOF model, but some more modern variant, or maybe I was thinking of cell-based data (which is something different again), or maybe I was just wrong.

Anyway, the next step is to start looking at the phase sub-threads: primary phase and secondary phase(s). You can run the same checks on these phase sub-thread pointers that you ran on the super-thread. From memory, the commands are something like:
Code:
Thread *pt; /* primary phase */
Thread *st; /* secondary phase */
/* ... get t, the superthread, as before ... */
pt = THREAD_SUB_THREAD(t,0); /* primary phase is always 0 */
if(NULLP(pt)) Message("failed to get pt!!!!\n");
/* ... test what fields are available in pt ... */
st = THREAD_SUB_THREAD(t,1); /* [I *think*] secondary phases are always 1,... */
if(NULLP(st)) Message("failed to get st!!!!\n");
/* ... test what fields are available in st ... */
Alternatively, look in the manual for THREAD_SUB_THREADS to get an array of sub-threads that you can loop over. (But still test both primary subthread and secondary subthread(s) -- they may be different.)

Once you know which fields are available and where, then doing the actual calculation should be easy.
dean993 likes this.
obscureed is offline   Reply With Quote

Old   March 15, 2018, 04:54
Default
  #5
New Member
 
Dinushke
Join Date: Aug 2013
Location: Victoria, Australia
Posts: 11
Rep Power: 9
dean993 is on a distinguished road
Hey Ed,

As you mentioned previously, I wrote a UDF (see below) to check if the required variables are available in the primary and secondary phase threads. Initially I used the lookup thread to find the thread in the interior surface (ID=3) that I am interested in. I found that in the primary and secondary phase thread, VOF was the only variable available, while velocity components, pressure and density was unavailable. This was strange so I change the Lookup thread ID to a wall ID (ID=1). The phase thread in that situation contained the velocity, vof and pressure. I don't understand why collecting a thread of cells/faces at the interior is any different to a standard boundary like a wall. Any idea's?

Regards,
Dean


*********************
#include "udf.h"
#include "mem.h"
DEFINE_ON_DEMAND(nullp_phaset)
{
Domain *d;
face_t f;
d = Get_Domain(1);
Thread *t = Lookup_Thread(d,3);
Message("\nBefore_Storage_Check\n");
Thread *pt = THREAD_SUB_THREAD(t,0);
Thread *st = THREAD_SUB_THREAD(t,1);

if(NULLP(t))
{Message("No Mixture Thread\n");
}
if(NULLP(THREAD_STORAGE(t, SV_DENSITY)))
{Message("NO M_Density\n");
}
if(NULLP(THREAD_STORAGE(t, SV_U)))
{Message("NO M_U-velocity\n");
}
if(NULLP(THREAD_STORAGE(t, SV_V)))
{Message("NO M_V-velocity\n");
}
if(NULLP(THREAD_STORAGE(t, SV_W)))
{Message("NO M_W-velocity\n");
}
if(NULLP(THREAD_STORAGE(t, SV_VOF)))
{Message("NO M_VOF\n");
}
if(NULLP(THREAD_STORAGE(t, SV_P)))
{Message("NO M_Pressure\n");
}

if(NULLP(pt))
{Message("No Primary Thread\n");
}
if(NULLP(THREAD_STORAGE(pt, SV_U)))
{Message("NO P_U-velocity\n");
}
if(NULLP(THREAD_STORAGE(pt, SV_V)))
{Message("NO P_V-velocity\n");
}
if(NULLP(THREAD_STORAGE(pt, SV_W)))
{Message("NO P_W-velocity\n");
}
if(NULLP(THREAD_STORAGE(pt, SV_VOF)))
{Message("NO P_VOF\n");
}
if(NULLP(THREAD_STORAGE(pt, SV_P)))
{Message("NO P_Pressure\n");
}
if(NULLP(THREAD_STORAGE(pt, SV_DENSITY)))
{Message("NO P_Density\n");
}

if(NULLP(st))
{Message("No Secondary Thread\n");
}
if(NULLP(THREAD_STORAGE(st, SV_U)))
{Message("NO S_U-velocity\n");
}
if(NULLP(THREAD_STORAGE(st, SV_V)))
{Message("NO S_V-velocity\n");
}
if(NULLP(THREAD_STORAGE(st, SV_W)))
{Message("NO S_W-velocity\n");
}
if(NULLP(THREAD_STORAGE(st, SV_VOF)))
{Message("NO S_VOF\n");
}
if(NULLP(THREAD_STORAGE(st, SV_P)))
{Message("NO S_Pressure\n");
}
if(NULLP(THREAD_STORAGE(st, SV_DENSITY)))
{Message("NO S_Density\n");
}
Message("After_Storage_Check\n");
}

*********************



Quote:
Originally Posted by obscureed View Post
OK, you're making progress.

So I was 100% wrong in my predictions of which variables would be available? Oh well, never mind -- it is very model-dependent, and this is why you are doing the checks. Maybe this is not a classic VOF model, but some more modern variant, or maybe I was thinking of cell-based data (which is something different again), or maybe I was just wrong.

Anyway, the next step is to start looking at the phase sub-threads: primary phase and secondary phase(s). You can run the same checks on these phase sub-thread pointers that you ran on the super-thread. From memory, the commands are something like:
Code:
Thread *pt; /* primary phase */
Thread *st; /* secondary phase */
/* ... get t, the superthread, as before ... */
pt = THREAD_SUB_THREAD(t,0); /* primary phase is always 0 */
if(NULLP(pt)) Message("failed to get pt!!!!\n");
/* ... test what fields are available in pt ... */
st = THREAD_SUB_THREAD(t,1); /* [I *think*] secondary phases are always 1,... */
if(NULLP(st)) Message("failed to get st!!!!\n");
/* ... test what fields are available in st ... */
Alternatively, look in the manual for THREAD_SUB_THREADS to get an array of sub-threads that you can loop over. (But still test both primary subthread and secondary subthread(s) -- they may be different.)

Once you know which fields are available and where, then doing the actual calculation should be easy.
dean993 is offline   Reply With Quote

Reply

Tags
segmentation fault, udf and programming, vof model

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to access only one phase in multiphase model by UDF wersoe Fluent UDF and Scheme Programming 1 January 4, 2017 07:11
[mesh manipulation] Importing Multiple Meshes thomasnwalshiii OpenFOAM Meshing & Mesh Conversion 18 December 19, 2015 18:57
[General] 2 datas on one plot Akuji ParaView 46 December 1, 2013 14:06
[Commercial meshers] fluentMeshToFoam multidomain mesh conversion problem Attesz OpenFOAM Meshing & Mesh Conversion 12 May 2, 2013 10:52
fluent add additional zones for the mesh file SSL FLUENT 2 January 26, 2008 11:55


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