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/)
-   -   code for product of rho and velocity (https://www.cfd-online.com/Forums/fluent-udf/193117-code-product-rho-velocity.html)

aravind vashista September 17, 2017 13:50

code for product of rho and velocity
 
Hii all
I am simulation 3D compressible supersonic flow with hydrogen as fuel and air as oxidizer. I want to write a UDF for mixing efficiency which involves surface integral of (Y**u). where y is species mass fraction, ρ is density , u is velocity. and plot the same along axial length. Since i am learning to write Udf i started of with writing a simple code to find product of ρ*u and allocate these scalar to a memory. so thst i can use the in postprocessing. The code looks as follows. The code is able to interpret but getting fatal signal (segmentation error) while executing. Can u guys please help me in correcting my code.

#include "udf.h"
DEFINE_ON_DEMAND(density)
{
Thread *t;
cell_t c;
real rho, u;
domain= Get_Domain(1);
thread_loop_c (t,domain)
{
begin_c_loop (c,t)
{

rho = C_R(c,t);
u = C_U(c,t);
C_UDSI(c,t,0)= rho*u;
}
end_c_loop(c,t)

begin_c_loop(c,t)
{

C_UDMI(c,t,0)=rho*u;

}
end_c_loop(c,t)

}
}

Please help me in correcting my code

AlexanderZ September 17, 2017 21:12

Hello.

to get the value of rho*u you do not need scalar (UDSI), you need variable (UDMI) only.

UDSI is used to define User-Defined Scalar (UDS) Transport Equations, see
ANSYS Fluent Customization Manual:
2.7. User-Defined Scalar (UDS) Transport Equation DEFINE Macros
ANSYS Fluent Theory Guide:
1.3. User-Defined Scalar (UDS) Transport Equations

Code:

#include "udf.h"
DEFINE_ON_DEMAND(density)
{
Thread *t;
cell_t c;
real rho, u;
domain= Get_Domain(1);
thread_loop_c (t,domain)
{
begin_c_loop (c,t)
{

rho = C_R(c,t);
u = C_U(c,t);
C_UDMI(c,t,0)=rho*u;
}
end_c_loop(c,t)
}
}

Do not forget to change User Defined Memory locations in Fluent GUI
Go to User-Defined -> Memory -> User Defined Memory locations -> 1 (was 0). This is number of udm variables that you use in UDF.

Now you can plot rho*u the same way, as you plot for example velocity, find User Defined Memory... in the drop down list

Best regards

aravind vashista September 18, 2017 01:20

Quote:

Originally Posted by AlexanderZ (Post 664699)
Hello.

to get the value of rho*u you do not need scalar (UDSI), you need variable (UDMI) only.

UDSI is used to define User-Defined Scalar (UDS) Transport Equations, see
ANSYS Fluent Customization Manual:
2.7. User-Defined Scalar (UDS) Transport Equation DEFINE Macros
ANSYS Fluent Theory Guide:
1.3. User-Defined Scalar (UDS) Transport Equations

Code:

#include "udf.h"
DEFINE_ON_DEMAND(density)
{
Thread *t;
cell_t c;
real rho, u;
domain= Get_Domain(1);
thread_loop_c (t,domain)
{
begin_c_loop (c,t)
{

rho = C_R(c,t);
u = C_U(c,t);
C_UDMI(c,t,0)=rho*u;
}
end_c_loop(c,t)
}
}

Do not forget to change User Defined Memory locations in Fluent GUI
Go to User-Defined -> Memory -> User Defined Memory locations -> 1 (was 0). This is number of udm variables that you use in UDF.

Now you can plot rho*u the same way, as you plot for example velocity, find User Defined Memory... in the drop down list

Best regards

hii Alexander,
i have allocated user defined memory location as 1, and implemented the code u had suggested (i.e the same code u wrote above). but still i am getting error as '' Received a fatal signal (Segmentation fault)''. Can u please help me in resolving this issue.
I am modelling 3D supersonic compressible flows using pressure based and k-e for turbulence. and i use species transport to define mixture properties for h2-air.

AlexanderZ September 18, 2017 23:41

Hello

Try this code
Code:

#include "udf.h"

DEFINE_ON_DEMAND(my_density)
{
Domain *domain;
Thread *t;
cell_t c;
real rho, u;
real x[ND_ND];
domain= Get_Domain(1);

thread_loop_c (t,domain)
        {
        begin_c_loop (c,t)
                {
                C_CENTROID(x,c,t);
                rho = C_R(c,t);
                u = C_U(c,t);
                C_UDMI(c,t,0)=rho;
                C_UDMI(c,t,1)=u;
                C_UDMI(c,t,2)=rho*u;
                }
        end_c_loop(c,t)
        }
        Message0("\n On demand executed\n");
}

Best regards

sbaffini September 19, 2017 03:59

A segmentation fault inevitably means you are accessing some memory location you are not supposed to access.

My first shot would be that you either have not initialized the solution or that something related to the species transport has to be considered (just guessing, I expect the density to depend on the species concentration, so accessing it in a DEFINE_ON_DEMAND udf might be dangerous as you might try to access it when it is not yet available).

Usually, the best way to debug something is to make it more simple, not the opposite.

Start with just the cell centroid (useful for debug, because always present). Then try just with the velocity (no species effect should be present). Then the density (maybe some species effect is present for DEFINE_ON_DEMAND). According to where the problem comes out you take further steps.

aravind vashista September 21, 2017 06:11

Quote:

Originally Posted by AlexanderZ (Post 664833)
Hello

Try this code
Code:

#include "udf.h"

DEFINE_ON_DEMAND(my_density)
{
Domain *domain;
Thread *t;
cell_t c;
real rho, u;
real x[ND_ND];
domain= Get_Domain(1);

thread_loop_c (t,domain)
        {
        begin_c_loop (c,t)
                {
                C_CENTROID(x,c,t);
                rho = C_R(c,t);
                u = C_U(c,t);
                C_UDMI(c,t,0)=rho;
                C_UDMI(c,t,1)=u;
                C_UDMI(c,t,2)=rho*u;
                }
        end_c_loop(c,t)
        }
        Message0("\n On demand executed\n");
}

Best regards

Yes i will try and comeback to u
Thank you

aravind vashista September 21, 2017 06:17

Quote:

Originally Posted by sbaffini (Post 664855)
A segmentation fault inevitably means you are accessing some memory location you are not supposed to access.

My first shot would be that you either have not initialized the solution or that something related to the species transport has to be considered (just guessing, I expect the density to depend on the species concentration, so accessing it in a DEFINE_ON_DEMAND udf might be dangerous as you might try to access it when it is not yet available).

Usually, the best way to debug something is to make it more simple, not the opposite.

Start with just the cell centroid (useful for debug, because always present). Then try just with the velocity (no species effect should be present). Then the density (maybe some species effect is present for DEFINE_ON_DEMAND). According to where the problem comes out you take further steps.

yes this was a good way for debugging, i wrote a new code and it interpreted and executed sucessfully but while compiling i am able to sucessfully build library file but unable to load it. I will get the following error in doing so

Error: Error code: 193\n
Error Object: #f

can u tell me y i am getting this error?
I have installed microsoft visual studio v17.0.

sbaffini September 21, 2017 06:23

So, interpreting works with all the variables?

I have no idea of what that error means. If interpreting works, I would first stick to that. Only later I would bother with compiling, as any troubles can then be related just to the compilation process and not the udf in itself.

aravind vashista September 22, 2017 02:27

Quote:

Originally Posted by sbaffini (Post 665127)
So, interpreting works with all the variables?

I have no idea of what that error means. If interpreting works, I would first stick to that. Only later I would bother with compiling, as any troubles can then be related just to the compilation process and not the udf in itself.

I resolved it, it was a visual studio error. i installed visual studio 2012 instead of visual studio 2017 and it got compiled.
Thank you


All times are GMT -4. The time now is 22:51.