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 for volume fraction (https://www.cfd-online.com/Forums/fluent-udf/140797-udf-volume-fraction.html)

sirpolar August 23, 2014 05:54

UDF for volume fraction
 
Hi dear all
I want an UDF that computes volume fraction of second phase in two phase flow at each cell as fluid flows.
I would be appreciated if you could help me on this

Thankyou very much

Sun August 26, 2014 03:17

If you use any multiphase modeling approaches, by default, Fluent will solves one transport equation for the volume fraction. So, you don't need to specifically define a UDF to compute the volume fraction of second phase.
Cheers!

sirpolar August 26, 2014 14:13

Quote:

Originally Posted by Sun (Post 507801)
If you use any multiphase modeling approaches, by default, Fluent will solves one transport equation for the volume fraction. So, you don't need to specifically define a UDF to compute the volume fraction of second phase.
Cheers!

Dear Sun
I want the volume fraction of second or third phase as an input for an equation That I am about to define by UDF. So I should write an UDF to computes volume fraction of phases at different positions and different times at each cell .

Sun August 27, 2014 01:58

Now I understand your question better. I think you can use
Code:

C_VOF(cell,cell_thread)
cell macro to get the volume fraction of each phase.
The other point that you should consider is to loop over all sub-domains (the phases that you need their volume fractions) in the mixture domain. For example, you want to compute F=a*b where b is a constant and a is the secondary phase volume fraction, and F is varying by time and spatial location:
Code:

sub_domain_loop(subdomain, mixture_domain, phase_domain_index)
{
    thread_loop_c (cell_thread,subdomain)
    {
        begin_c_loop
        {
          /* get the secondary phase volume fraction*/
         
          /* compute F = C_VOF(cell,cell_thread)*b */
         
          /*store F in an UDMI */
        }
    }
} /*end of subdomain loop*/

something like this might help you, but cross-check the syntax first.
cheers!

sirpolar August 27, 2014 15:53

Quote:

Originally Posted by Sun (Post 507938)
Now I understand your question better. I think you can use
Code:

C_VOF(cell,cell_thread)
cell macro to get the volume fraction of each phase.
The other point that you should consider is to loop over all sub-domains (the phases that you need their volume fractions) in the mixture domain. For example, you want to compute F=a*b where b is a constant and a is the secondary phase volume fraction, and F is varying by time and spatial location:
Code:

sub_domain_loop(subdomain, mixture_domain, phase_domain_index)
{
    thread_loop_c (cell_thread,subdomain)
    {
        begin_c_loop
        {
          /* get the secondary phase volume fraction*/
         
          /* compute F = C_VOF(cell,cell_thread)*b */
         
          /*store F in an UDMI */
        }
    }
} /*end of subdomain loop*/

something like this might help you, but cross-check the syntax first.
cheers!


Dear Sun

For example for the function that multiplies volume fraction of second phase in each cell by 5 as second fluid flows in the domain, would be the following UDF Okay? Please let me know if there is some thing wrong with it.

Thank you for your kindness.

#include "udf.h"
DEFINE_SOURCE (function,cell,thread,mixture_domain, phase_domain_index)
{
Real Source, F;
int phase_domain_index;
cell_t cell;
Thread *cell_thread;
Domain *subdomain;

sub_domain_loop(subdomain, mixture_domain, phase_domain_index)
{
thread_loop_c (cell_thread,subdomain)
{
begin_c_loop
{
/* get the secondary phase volume fraction*/

F = C_VOF(c, pt[2])*5

/*store F in an UDMI */
}
}
} /*end of subdomain loop*/

Sun August 28, 2014 02:18

If you are defining a source term
Code:

DEFINE_SOURCE
you need the derivatives of the source term. this is the syntax for source:
Code:

DEFINE_SOURCE( name, c, t, dS, eqn)
you also need the pointer to the mixture domain, because i don't think DEFINE_SOURCE is getting it directly from solver. And one minor thing is that if F = Source, there is no need to store it in an UDMI you can simply return it at the end of your code.

But if all these calculations are not for a source term and you are trying to calculate some term which is dependent on secondary phase volume fraction you can use a general macro like
Code:

DEFINE_ADJUST
.
cheers!

sirpolar August 28, 2014 12:06

Dear Sun
I am really confused with this UDF
I would be appreciated if you could help me more

Here is another UDF , please tell me what is wrong with it and correct it if you can
Fluent says line 13 parse error

#include "udf.h"
DEFINE_ADJUST(vis_res, domain)
{
Thread **pt;
Thread *thread;
real a, b;
mp_thread_loop_c(thread, domain, pt)
{
cell_t cell;
begin_c_loop_int(cell, thread)
{
a = C_VOF(cell, pt[1]);
b = 1- ((0.95-a)-0.25)^3;
}
end_c_loop_int(cell, thread)
}
}

Sun August 29, 2014 02:16

I am not sure what is:
Code:

mp_thread_loop_c(thread, domain, pt)
and if it needs a closing like end_mp_thread_loop.
Please try this one instead of line 13:
Code:

b = 1- ((0.95-C_VOF(cell, pt[1]))-0.25)^3;
and even better, if you want to monitor "b" just store it in an UDMI, so you'll have its contour and etc.
Also put cell_t cell outside the thread loop, it is being defined every time that solver loops through the thread.
cheers!

sirpolar August 31, 2014 07:03

Dear sun
I think we should first clarify which macro is more suitable for my case (define profile- define source or define adjust)
In fact I am about to write UDF for viscous resistance (a variable) that must be change according to a function that uses volume fraction of second phase in each cell.
I would be really appreciated if you could help me more

Sun September 1, 2014 07:27

Yes as you said the type of macro you want to use should be defined beforehand. DEFINE_SOURCE, as the name says, is for defining a source term at the RHS of any transport equations. For example, let's say you have some kind of force at the right hand side of momentum equation for which you can use DEFINE_SOURCE. However, DEFINE_ADJUST, is for general computations, for instance you want to calculate some variable which is dependent on the volume fraction of secondary phase. The last macro, DEFINE_PROFILE is for costume boundary conditions.
Since I don't know the details of your simulation and the problem that you are trying to solve, I cannot tell you surely which macro is the optimized choice for your problem. If you can please provide more details, I'll be able to recommend a suitable macro.
cheers!

sirpolar September 1, 2014 11:16

Dear sun
Thank you for your guidance
Here is some explanation about what I am about to do:
Modeling of flows through a porous medium requires a modified formulation of the Navier-Stokes equations, which reduces to their classical form and includes additional body force terms (resistance terms) induced by the porous region (F).
For homogenous porous media:

http://www.cfd-online.com/Forums/dat...AASUVORK5CYII=http://www.cfd-online.com/Forums/dat...AASUVORK5CYII=
F = -((μvi/α) + (C2ρvvi/2)}
where 1/α is viscous resistance.
I want the UDF that uses the volume fraction of secondary phases in each cell as fluids flows in porous region to be inserted to an equation for determination of viscous resistance
for example : Viscous resistance = 5 * volume fraction of secondary phase in the cell








Sun September 1, 2014 11:54

Just a question, F is the resistance term NOT the force itself, right? but in the calculation of the body force due to porous region you need to have the "F"?

sirpolar September 1, 2014 13:36

Hello sun
Yes F is a resistance term and its parameters (1/α and C2) should inserted in the FLUENT as constant value or user define function.

Sun September 1, 2014 15:02

Alright if "F" is not a force term and it is not supposed to be a source term in N_S equation, I think you can use DEFINE_ADJUST.

sirpolar September 1, 2014 15:20

I think the "F" doesnt matter here, beacuse the udf must be written for viscouse resistance (1/α) and this parameter is defined separately. Dont you think so?
Dont you think Define_profile is more suitable?

p.s. I found UDF example for viscous resistance in a manual
/* Viscous Resistance Profile UDF in a Porous Zone that utilizes F_PROFILE*/

#include "udf.h"

DEFINE_PROFILE(vis_res,t,i)
{
real x[ND_ND];
real a;
cell_t c;
begin_c_loop(c,t)
{
C_CENTROID(x,c,t);
if(x[1] < (x[0]-0.01))
a = 1e9;
else
a = 1.0;
F_PROFILE(c,t,i) = a;
}
end_c_loop(c,t)
}


Best regards

Sun September 2, 2014 02:32

OK this is perfect, now you have a good example to create your own UDF. But please have a look at the "solution procedure for the pressure-based solver" in the UDF manual. You can see DEFINE_PROFILE is being only calculated outside the time loop. Probably in your case the viscous resistance term is varying by the volume fraction of the second phase and time. So I think DEFINE_ADJUST would be a better choice.
cheers!

sirpolar September 2, 2014 04:13

Dear sun
thank you for your useful comment
the second step is how to create a loop for second phase determination in each cell and how to define the second phase

sirpolar September 2, 2014 14:50

Dear Sun and other friends
would you please tell me what is wrong with this UDF
Fluent says: line 13 c_VOF: undeclared variable

#include "udf.h"
DEFINE_ADJUST(viscouse_function, d)
{
cell_t cell;
Thread **pt;
Thread *cell_threads;
Domain *mixture_domain;
mp_thread_loop_c(cell_threads, mixture_domain, pt)
{
begin_c_loop(cell,pt[1])
{
real visc;
visc = 1- ((0.95-(c_VOF(c,pt[1])))-0.25);
}
end_c_loop(c,pt[1])
}

Sun September 2, 2014 15:44

required changes:

take out "real visc" from inside the loop and define it outside mp_thread_loop,

c_VOF(c,pt[1]) -----> C_VOF(cell,pt[1])

end_c_loop(c,pt[1]) -----> end_c_loop(cell,pt[1])

sirpolar September 5, 2014 14:20

Dear sun the other friends
I have written the UDF as follows, when I interpreted it to the fluent it is ok
But when I want to run (after initialization) the fluent gives this error:
FLUENT received fatal signal (ACCESS_VIOLATION)
I was wondering if you know what is wrong with it?

#include "udf.h"
DEFINE_ADJUST(viscouse_function, d)
{
real visc;
cell_t cell;
Thread **pt;
Thread *cell_threads;
Domain *mixture_domain;
mp_thread_loop_c(cell_threads, mixture_domain, pt)
{
begin_c_loop(cell,pt[1])
{
visc = 1- ((0.95-(C_VOF(cell,pt[1])))-0.25);
}
end_c_loop(cell,pt[1])
}
}

hossein65 March 1, 2016 08:19

I compiled your code, but I am trying to use it for DEFINE_PROPERTY. For the error you get, I put mixture_domain=Get_Domain(1). It compiles and runs well

hossein65 March 1, 2016 08:37

Quote:

Originally Posted by sirpolar (Post 509256)
Dear sun the other friends
I have written the UDF as follows, when I interpreted it to the fluent it is ok
But when I want to run (after initialization) the fluent gives this error:
FLUENT received fatal signal (ACCESS_VIOLATION)
I was wondering if you know what is wrong with it?

#include "udf.h"
DEFINE_ADJUST(viscouse_function, d)
{
real visc;
cell_t cell;
Thread **pt;
Thread *cell_threads;
Domain *mixture_domain;
mp_thread_loop_c(cell_threads, mixture_domain, pt)
{
begin_c_loop(cell,pt[1])
{
visc = 1- ((0.95-(C_VOF(cell,pt[1])))-0.25);
}
end_c_loop(cell,pt[1])
}
}

I put mixture_domain=Get_Domain(1) to initialize the domain, and it perfectly compiles and runs with DEFINE_PROPERTY macro that I am using. But the problem is that I get ridiculous constant VOF values which do not match the actual VOF values for my model. I have a two phase Eulerian mixture (Water+Carbon (as fluid)) and cannot get the real carbon VOF values. The funny thing is that when I comment out the line containing the visc function (1-((0.95-(C_VOF(cell,pt[1])))-0.25)) and put a constant number (say 1.0), I get the water VOF !! which doesn't make sense because I am removing the C_VOF (cell,pt[1]) variable. I mean when I say visc=1.0, I get the water VOF. However, I cannot define any functions based on these values unless, and only can multiply the returned value by some numbers and get the result. When I do subtract, add or something else, it doesn't work. Any ideas?

ebtedaei October 9, 2017 07:23

The same problem !
 
Dear Ebrahim,

I have a same problem like your case and just my viscosity function is different. Did you solve your UDF's problem?
I would be appreciated if you could help me on this.

Thanks in advance,
Ali

ebtedaei October 9, 2017 07:25

Dear Ebrahim,

I have a same problem like your case and just my viscosity function is different. Did you solve your UDF's problem?
I would be appreciated if you could help me on this.

Thanks in advance,
Ali

ebtedaei October 9, 2017 07:56

The same problem !
 
Dear Ebrahim,
I have a same problem with you problem. Just my viscosity function is different.
Did you solve the UDF's problem(viscous function vs volume of fraction)?

I would be appreciated if you could help me on this.

Thanks in advance,
Ali

Quote:

Originally Posted by sirpolar (Post 509256)
Dear sun the other friends
I have written the UDF as follows, when I interpreted it to the fluent it is ok
But when I want to run (after initialization) the fluent gives this error:
FLUENT received fatal signal (ACCESS_VIOLATION)
I was wondering if you know what is wrong with it?

#include "udf.h"
DEFINE_ADJUST(viscouse_function, d)
{
real visc;
cell_t cell;
Thread **pt;
Thread *cell_threads;
Domain *mixture_domain;
mp_thread_loop_c(cell_threads, mixture_domain, pt)
{
begin_c_loop(cell,pt[1])
{
visc = 1- ((0.95-(C_VOF(cell,pt[1])))-0.25);
}
end_c_loop(cell,pt[1])
}
}


pakk October 9, 2017 08:23

The solution was already given by Hossein Amini, just above your message.

If you are interested in what was the reason for the error message: the variable "mixture_domain" is defined, but never given a value. You ask the computer to say something about a mixture domain, but you don't tell him which mixture domain (even though you probably only have one), so Fluent is confused.

ebtedaei October 9, 2017 09:29

Hi,
What is [**pt] in 6 line and[*] in 7 , 8 lines?

Thanks,
Ali

Quote:

Originally Posted by sirpolar (Post 509256)
Dear sun the other friends
I have written the UDF as follows, when I interpreted it to the fluent it is ok
But when I want to run (after initialization) the fluent gives this error:
FLUENT received fatal signal (ACCESS_VIOLATION)
I was wondering if you know what is wrong with it?

#include "udf.h"
DEFINE_ADJUST(viscouse_function, d)
{
real visc;
cell_t cell;
Thread **pt;
Thread *cell_threads;
Domain *mixture_domain;
mp_thread_loop_c(cell_threads, mixture_domain, pt)
{
begin_c_loop(cell,pt[1])
{
visc = 1- ((0.95-(C_VOF(cell,pt[1])))-0.25);
}
end_c_loop(cell,pt[1])
}
}


pakk October 9, 2017 09:50

The star indicates that the variable is a pointer.

ebtedaei October 23, 2017 10:21

Add UDF in Fluent's materials section?
 
Hi All,
I wrote UDF code in Fluent and I added it instead of the constant amount of water viscosity, but I have not answered so far! Should special settings be made?

Thanks,
Ali

pakk October 24, 2017 04:41

I don't understand what you are asking.

ebtedaei October 24, 2017 15:02

Quote:

Originally Posted by pakk (Post 668968)
I don't understand what you are asking.

I want to write a UDF for Viscosity function of mixture phase vs. Volume of fraction of second phase.

ebtedaei October 24, 2017 15:05

I have a twophase flow :
Water and a other fluid phase

pakk October 25, 2017 02:38

And you already wrote a UDF or not?

What is your problem? Do you not know how to write it? Or do you get errors? Or do you get unexpected results?

If you want help, don't let us guess at what you are doing...

Sun October 25, 2017 04:26

Hi ali, as pakk said your question is bit unclear.
Please share your UDF, or maybe the function you are trying to code for the viscosity. In case you are getting unexpected results, please share the results and tell us why you think they are wrong. So, everyone will have a better understanding of the problem.
cheers

ebtedaei November 4, 2017 00:29

UDF for viscosity
 
3 Attachment(s)
Thank you for your reply.
I use a mix model that the water and the other phase (X) are mixed, which phase water and phase X have individual properties (density and viscosity).
In the Inlet Boundary Condition, the phase X with a certain amount of volume of fraction (alphaX) is mixed with the water phase However, in different points of the domain and at different times, the volume of fraction of the X phase changes, and subsequently the viscosity of the mixed phase (m) also changes according to the formula:

Viscosity (m) = 3.8 * viscosity (water) * (1-(alphaX/0.62))^(-1.55)

Where, the viscosity function of the mixture phase (m) varies according to the volume of fraction (alpha X) of the phase X, and also the μc is the constant value of the viscosity of water.

A very similar example to my problem that I found on the cfd-online site is as follows:

https://www.cfd-online.com/Forums/fl...-fraction.html

It is important that based on a study on related articles, I have to replace the UDF file on the constant amount of water viscosity in the Materials substitute of fluent to vary the viscosity of the mix phase at different points and at different times in the domain.

Of course, I have already written three UDF codes, but I have not responded to any of these.
Thank you in advance for your commenting on any of these fallowing codes:

Sun November 4, 2017 05:29

Hi,
Maybe you can use both
Code:

DEFINE_ADJUST
and
Code:

DEFINE_PROPERTY
. Here is an example, you might need to modify it based on your needs:
Code:

DEFINE_PROPERTY(viscosity_mix,c,t)
{
        real viscosity;
   
    viscosity = C_UDMI(c,t,1);
   
    return viscosity;               
}

DEFINE_ADJUST(my_adjust,mixture_domain)
{
        Thread *t;
        cell_t c;
        Thread **pt;
               
        real mu_water = 0.00103;

        mp_thread_loop_c(t,mixture_domain,pt)
        {
          begin_c_loop(c,t)
          {
               
                C_UDMI(c,t,1) = 3.8*mu_water*(pow((1.0 - C_VOF(c,pt[1])/0.62), -1.55));
               
          }
        end_c_loop(c,t)         
        }
}

Please remember to define a user define memory before compiling, because the values are stored in an UDM.

hope it helps
cheers!

ebtedaei November 5, 2017 09:17

Thanks for your reply.

What do you mean by last statement:

"Please remember to define a user define memory before compiling, because the values are stored in an UDM."

How can I define a user define memory before compiling?

I isn't professional in writing UDF!!!

ebtedaei November 5, 2017 10:16

1 Attachment(s)
What numbers do I enter in this window?

pakk November 6, 2017 04:37

If you want to have one User Defined Memory, change the first zero (0) into a one (1).

ebtedaei November 6, 2017 06:39

1 Attachment(s)
Quote:

Originally Posted by Sun (Post 670371)
Hi,
Maybe you can use both
Code:

DEFINE_ADJUST
and
Code:

DEFINE_PROPERTY
. Here is an example, you might need to modify it based on your needs:
Code:

DEFINE_PROPERTY(viscosity_mix,c,t)
{
        real viscosity;
   
    viscosity = C_UDMI(c,t,1);
   
    return viscosity;               
}

DEFINE_ADJUST(my_adjust,mixture_domain)
{
        Thread *t;
        cell_t c;
        Thread **pt;
               
        real mu_water = 0.00103;

        mp_thread_loop_c(t,mixture_domain,pt)
        {
          begin_c_loop(c,t)
          {
               
                C_UDMI(c,t,1) = 3.8*mu_water*(pow((1.0 - C_VOF(c,pt[1])/0.62), -1.55));
               
          }
        end_c_loop(c,t)         
        }
}

Please remember to define a user define memory before compiling, because the values are stored in an UDM.

hope it helps
cheers!

Hi,
As you see, In my case two fluids are mixed together: water and another fluid called ferrosilicon (Other than Air).
In udf code that you wrote, how do I understand the software that the volume fraction (vof) is related to the ferrosilicon fluid?


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