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

Is DEFINE_ADJUST playing same role as main() in this context ?

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 21, 2022, 09:05
Default Is DEFINE_ADJUST playing same role as main() in this context ?
  #1
Member
 
Odisha
Join Date: Jan 2020
Posts: 59
Rep Power: 6
Siba11 is on a distinguished road
Hi all,

I know that Ansys UDF's are written in C language.
Consider two cases: Case 1 is simple C code, and case 2 is UDF code.

(1)
Code:
#include <stdio.h>

int function (argument)
{
statements;
}

void main()
{
statements;
}

(2)
Code:
#include "udf.h"

int function (arguments)
{
statements;
}

DEFINE_ADJUST(name,d)
{
statements;
}

I just want to ask if void main() and DEFINE_ADJUST are playing the same role here ? Are they both the entry point for the execution of the code.

Thanks in advance!
Siba11 is offline   Reply With Quote

Old   October 23, 2022, 21:36
Default
  #2
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
DEFINE_ADJUST is a macro, which will be executed before each iteration or timestep depends on solver settings

look into ansys fluent customization manual
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   July 23, 2023, 09:38
Default
  #3
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
I want to update the UDM variable which is stored in some function then should I use DEFINE_ADJUST macro? or they will update automatically while I already stored the value in C_UDMI (C, t ,int)?
Chhotelal1234 is offline   Reply With Quote

Old   July 23, 2023, 19:44
Default
  #4
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
it depends on how which functions you are using
show your code to get response
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   July 24, 2023, 00:45
Default How to updates all value which is stored in UDMI.
  #5
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
#include "udf.h"

DEFINE_SOURCE(mass_source, cell, thread, dS, eqn)
{
real Xp = 0.25;
real Xf = 0.03;
real Xcr = 0.16;
real epsilon_f = 0.37;
real Tp = 300;
real Tf = 338;
real Tref = 298.15;
real dp = 0.000525;

real hs, P_sat;
real Re_p, Sc, Sh, f1_Tp, f2_Xp, Xp_star, Dv, m_dot;
Thread *t;
cell_t c;

/* Get the thread and cell pointers */
t = thread;
c = cell;

/* Time loop to update UDM values */
while (t != NULL)
{
Tp = C_T(c, t); // Temperature of particles
Xp = C_UDMI(c, t, 0);

Dv = 2.6e-5 * pow(Tp / Tref, 1.5);

C_UDMI(c, t, 0) = Xp;

// Store the value in UDM
C_UDMI(c, t, 1) = Dv;


P_sat = 1.0e5 * exp(13.869 - 5173 / Tp);

// Store the value in UDM
C_UDMI(c, t, 2) = P_sat;

Re_p = (C_R(cell, thread) * C_U(cell, thread) * dp) / C_MU_L(cell, thread);
Sc = C_MU_L(cell, thread) / (C_R(cell, thread) * Dv);

// Store the value in UDM
C_UDMI(cell, thread, 3) = Re_p;
C_UDMI(cell, thread, 4) = Sc;

Sh = 7 - 10 * epsilon_f + 5 * pow(epsilon_f, 2) * (1 + 0.7 * pow(Re_p, 0.2) * pow(Sc, 1.0 / 3.0)) + (1.33 - 2.4 * epsilon_f + 1.2 * pow(epsilon_f, 2)) * pow(Re_p, 0.7) * pow(Sc, 1.0 / 3.0);

// Store the value in UDM
C_UDMI(c, t, 5) = Sh;

hs = Sh * Dv / 0.000525;

// Store the value in UDM
C_UDMI(c, t, 6) = hs;


f1_Tp = 0.622 * P_sat / (101325 - P_sat);

// Store the value in UDM
C_UDMI(c, t, 7) = f1_Tp;

int n = 3;
real K = 0.01;
if (Xp > Xcr)
{
f2_Xp = 1;
}
else
{
f2_Xp = pow(Xp, n) / (pow(Xp, n) + K);
}
Xp_star = f1_Tp * f2_Xp;

// Store the value in UDM
C_UDMI(c, t, 8) = f2_Xp;
C_UDMI(c, t, 9) = Xp_star;


m_dot = hs * (6 / dp) * (Xp_star - Xf);

// Store the value in UDM
C_UDMI(c, t, 10) = m_dot;

/* Move to the next thread */
t = THREAD_NEXT(t);
}

dS[eqn] = 0;
return m_dot;
}



I want to update all the values with respect to the time that is stored in UDMI.
should I need to make the other macos ADJUST or it will change same funtio.
Chhotelal1234 is offline   Reply With Quote

Old   July 25, 2023, 19:49
Default
  #6
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
1. Don't see the reason to use while loop. DEFINE_SOURCE(mass_source, cell, thread, dS, eqn) has link to thread, it's always unique
Code:
while (t != NULL)
2. you don't need adjust function for your code as DEFINE_SOURCE(mass_source, cell, thread, dS, eqn) is executed on each iteration
3.However, I can see here problem with Xp variable
Code:
Xp = C_UDMI(c, t, 0);
you have to define C_UDMI(c, t, 0) for the first iteration, otherwise it contains random value inside. You may patch its value, for example after initialization, before first iteration
Or any other way youll find
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   July 26, 2023, 01:22
Default
  #7
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
when I compiled this code and run the simulation floating point exception error is coming. some value is not updating
the moisture content of the particle: 0.000000
Water vapor diffusion coefficient: 0.000031
Raynold number: -0.044023
Schmidt number: 0.465750
Vapor pressure: 23639.429882
Sherwood number: -nan(ind)
Mass transfer coefficient: -nan(ind)
Temperature function: 0.189272
Moisture function: 0.000000
Equilibrium moisture content: 0.000000
Mass source: -nan(ind)

I could not understand why these values are not coming or getting negative.
also if I want to add a function to this code. is it right or wrong:
DEFINE_ADJUST(moisture_update, d)
{
real n_ts, mi, time, dMdt, time_step, Mnew, E_evp;
real Ex_evp, V, Hfg, t_E_evp, d_E_evp;

real mi_prev = 0.25; // Initialize previous moisture content (adjust this based on your setup)
real ti_prev = 0.0; // Initialize the previous time step (adjust this based on your setup)
real m_dot; // Declare m_dot variable

n_ts = RP_Get_Integer("time-step");
time_step = RP_Get_Real("physical-time-step");
time = RP_Get_Real("flow-time");

if (last_timestep != n_ts)
{
last_timestep = n_ts;
d_E_evp = 0.0;
t_E_evp = 0.0;

Thread *t;
cell_t c;

/* Loop over all cells and threads to update UDM values */
thread_loop_c(t, d)
{
begin_c_loop_all(c, t)
{
// Calculate the drying rate (DR) using the drying rate formula
mi = C_UDMI(c, t, 0);
real Tp = C_T(c, t); // Temperature of particles
real ti = CURRENT_TIME;
if (THREAD_ID(t) == 0) // Assuming THREAD_ID(t) == 0 represents the main domain thread
{
dMdt = (mi - mi_prev) / (ti - ti_prev);
}

// Update previous moisture content (mi_prev) and previous time step (ti_prev) for the next iteration
mi_prev = mi;
ti_prev = ti;

Mnew = (time_step * dMdt) + mi;
C_UDMI(c, t, 11) = Mnew;
C_UDMI(c, t, 12) = dMdt; // Store the value in UDM

V = C_VOLUME(c, t);
Hfg = 3168 - (2.4364 * Tp);
m_dot = C_UDMI(c, t, 10); // Get the value of m_dot from UDM
E_evp = m_dot * V * Hfg;

if (E_evp < 0)
{
E_evp = 0;
}

C_UDMI(c, t, 13) = E_evp;

t_E_evp += E_evp;
}
end_c_loop(c, t)
}

d_E_evp += t_E_evp;
}
}
Chhotelal1234 is offline   Reply With Quote

Old   July 27, 2023, 00:29
Default
  #8
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
define your variables as global
Code:
real n_ts, mi, time, dMdt, time_step, Mnew, E_evp;
real Ex_evp, V, Hfg, t_E_evp, d_E_evp;

real mi_prev = 0.25; // Initialize previous moisture content (adjust this based on your setup)
real ti_prev = 0.0; // Initialize the previous time step (adjust this based on your setup)
real m_dot; // Declare m_dot variable
DEFINE_ADJUST(moisture_update, d)
{....}
last_timestep is not defined
mi = C_UDMI(c, t, 0); is not defined as I mentioned earlier

COMPILE code and read log, fix errors
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   July 27, 2023, 08:03
Default
  #9
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
I applied the same condition as you said but still simulation is not running, floating point exception error is coming.
also, the initial moisture content is defined in the starting global variable
#define Xp_initial 0.25 // initial moisture content

and inside the macro is stored
real Xp = C_UDMI(c, t, 0); // Moisture content
can you suggest to me where is doing wrong?

Could I have defined the moisture content in the Scalar term.
such as Xp= C_UDSI (c, t, o)
and also in the patch, the Scalar term XP defines

value print "Moisture content of the particle: 0.000000
Water vapor diffusion coefficient: 0.000031
Reynold's number: -0.032074
Schmidt number: 0.465534
Vapor pressure: 23751.255430
Sherwood number: -nan(ind)
Mass transfer coefficient: -nan(ind)
Temperature function: 0.190442
Moisture function: 0.000000
Equilibrium moisture content: 0.000000
Mass source: -nan(find)
Chhotelal1234 is offline   Reply With Quote

Old   July 28, 2023, 00:21
Default
  #10
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
show the most recent code
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Old   July 28, 2023, 02:47
Default
  #11
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
Quote:
Originally Posted by AlexanderZ View Post
show the most recent code
I have sent you private mail in the CFD forum. kindly check it
Chhotelal1234 is offline   Reply With Quote

Old   July 30, 2023, 09:06
Default
  #12
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
I do not understand when given the initial T value and velocity the Reynold number why coming to a negative or zero value. also, I have given moisture content value m=0.25 even then related to this value is coming to Zero. I think temperature and moisture content value is not going inside the UDF loop. please let me know if you know the reason.
Chhotelal1234 is offline   Reply With Quote

Old   July 31, 2023, 10:48
Default
  #13
Senior Member
 
Join Date: Sep 2017
Posts: 246
Rep Power: 11
obscureed is on a distinguished road
Hi Chhotelal1234,

I think you are correct to note that Reynolds number cannot be negative, so there is something wrong in your code to calculate that. So you should debug your code:
• Put in an if statement to select a visit where Re_p is calculated as negative, and print out the quantities used to calculate Re_p.
• If there are huge numbers of visits to the UDF, you can also reduce the number of printouts. For example, c is just an integer, so you can arbitrarily print out only the cells that match (c % 100000 == 37), where 37 could be any other arbitrary integer.

I won't do this for you, but I will point out that C_U is the x-component of the velocity vector. There is nothing stopping it from being negative. You probably want the magnitude of the velocity vector.

I cannot be certain that you have typed the formulas correctly (for example, the complicated one for Sh), and it is extremely inefficient to debug these by running a UDF. Test each one outside the UDF. Similarly, explore the robustness of every expression that goes into your UDF -- for example, will the expression for P_sat give overflow or zero results for extreme temperatures? If any expressions could give nonsensical results, you need to guard against these in the UDF. (Is it a problem if Re==0.0?)

In the bigger picture, you are being much too ambitious in writing the complicated UDF all at once.
• First get the UDF working with a constant evaporation rate, hard-coded into the UDF, not dependent on temperature, velocity or any other variables. Make this rate tiny, so that not much changes in the model. Run this and check that everything works.
• Build up the complication gradually. You can work out for yourself the order of this, but it could be a fixed saturation pressure, then a temperature-dependent saturation pressure but a fixed Sh, then variable Sh but fixed Re_p and so on.

I think you have a problem in assuming that you can save mi_prev as a moisture content from the previous timestep. The calculations are performed for every cell in the thread, so the value left in mi_prev at the end of a timestep is the value calculated for the last cell visited. This is then used for the first cell visited in the next timestep. You need to store mi_prev cell-by-cell (for example, in another UDMI). I have not seen any particles involved yet, so this is all cell-based.

Good luck!

Ed
obscureed is offline   Reply With Quote

Old   August 1, 2023, 05:41
Default
  #14
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
Thank you for your response,
I have also tried with simple UDF, even getting zero value after the initialization and flopping point error is coming.
could you tell me what I am doing wrong?

#include "udf.h"

#define M 0.25
#define E 0.4
#define Tin 338



DEFINE_INIT(initial_humidity, d)
{
real Tabs, w, a, b, C, Pvs, RH;
Thread *t;
cell_t c;

{
thread_loop_c(t, d)
{
begin_c_loop_all(c, t)
{
C_T(c, t) = Tin;

Tabs = C_T(c, t);

Pvs = 0.1 * exp(27.0214 - (6887 / Tabs) - (5.31 * log(Tabs / 273.16))); /* Eq(25) */
b = 2.667e-7 * pow((1 - (Tabs / 641.7)), -23.438);
C = 1 / (4e5 * pow(Tabs, -2.1166));
a = b * pow(1000 * M, 1 / C);
RH = 1 - exp(-a / Tabs); /* Eq (15) */

if (RH > 0.99)
{
RH = 0.99;
}

w = (Pvs * RH * 0.62189) / (101.3 - Pvs * RH); /* Eq (26) */

Message ("realative humidity:%f\n", w);
Message ("Pv:%f\n", Pvs);
Message ("RH:%f\n", RH);

}
end_c_loop(c, t)

}
}

}
Chhotelal1234 is offline   Reply With Quote

Old   August 1, 2023, 09:30
Default
  #15
Senior Member
 
Join Date: Sep 2017
Posts: 246
Rep Power: 11
obscureed is on a distinguished road
You call this a simple UDF, but it has a lot of formulas. You need to debug it yourself:

• Inside your UDF, check that the values supplied by Fluent are in the range you are expecting, and in the range that the formulas are suitable for. If they are outside the range, react suitably. During debugging, you probably want to print a message to screen. During and after debugging, you must find a way to continue with sensible values. This is good practice for all UDFs.

• Outside your UDF, check that the formulas are correct. It is hugely inefficient to debug this inside your UDF.
-- When your formula uses log, is that log10 or natural logarithm? Many older textbooks vary notation without warning.
-- Any formula that raises something to a power of 23 will have a restricted range of applicability, and is likely to cause underflow or overflow outside that range.
-- Work out a suitable sequence of values outside the UDF (in a spreadsheet or standalone C program), for a fixed temperature. Apply that temperature to Tabs in the UDF (but do not change C_T). Check, line by line, that your UDF is generating the expected values -- print a message to screen if it fails. Do this for the full range of temperatures.

I would still say that even this "simple" UDF is trying to take too many steps all at once. Get Pvs working first, then Pvs and b, and so on.
obscureed is offline   Reply With Quote

Old   August 12, 2023, 01:25
Default source value is getting nagative and nan
  #16
Member
 
Chhotelal
Join Date: Jun 2023
Posts: 31
Rep Power: 2
Chhotelal1234 is on a distinguished road
Dear Friends,
when I upload the source function and print the value my source function is not coming right from what I calculated manually. and after that floating point error is coming.
my source value is getting after some time getting nan and zero.
My source value gets a huge value -5458756322145.00 while i calculate manually it should be approx. 60.25
Could you please tell me what is wrong?

#include "udf.h"

#define Xp_initial 0.25 // initial moisture content
#define Xf_initial 0.02 // moisture content of air
#define Xcr 0.16 // critical moisture content
#define epsilon_f 0.37 // gas volume fraction
#define Tp_initial 300 // initial temperature of particles
#define dp 0.000380 // diameter of the particle (m)

// Define the reference temperature constant outside the macro
const real Tref = 298.15;


DEFINE_SOURCE(mass_source_gas, cell, thread, dS, eqn)
{
real hs, P_sat;
real Re_p, Sc, Sh, f1_Tp, f2_Xp, Xp_star, Dv, m_dot;
Thread *t;
cell_t c;

/* Get the thread and cell pointers */
t = thread;
c = cell;

/* Obtain the necessary parameters (Tp, Xp) */
real Tp = C_T(c, t); // Temperature of particles
real Xp = C_UDMI(c, t, 0); // Moisture content
real Xf = C_UDSI(c, t, 0); // air moisture content

// Calculate water vapor diffusion coefficient
Dv = 2.6e-5 * pow((Tp / Tref), 1.5);

// Store the value in UDM
C_UDMI(c, t, 1) = Dv;

// Obtain the necessary parameters (hs, P_sat)
P_sat = 1e5 * exp(13.869 - (5173 / Tp));

// Store the value in UDM
C_UDMI(c, t, 2) = P_sat;

// Calculate Sherwood number
Re_p = (C_R(c, t) * C_V(c, t) * dp) / C_MU_L(c, t);
Sc = C_MU_L(c, t) / (C_R(c, t) * Dv);

// Store the value in UDM
C_UDMI(c, t, 3) = Re_p;
C_UDMI(c, t, 4) = Sc;

// Calculate Sherwood number
Sh = (7 - 10 * epsilon_f + 5 * pow(epsilon_f, 2))* (1 + 0.7 * pow(Re_p, 0.2) * pow(Sc, 1.0 / 3.0)) + ((1.33 - 2.4 * epsilon_f + 1.2 * pow(epsilon_f, 2)) * pow(Re_p, 0.7) * pow(Sc, 1.0 / 3.0));

// Store the value in UDM
C_UDMI(c, t, 5) = Sh;

hs = (Sh * Dv) / dp;

// Store the value in UDM
C_UDMI(c, t, 6) = hs;

// Calculate equilibrium moisture content
f1_Tp = 0.622 * (P_sat / (101325 - P_sat));

// Store the value in UDM
C_UDMI(c, t, 7) = f1_Tp;

int n = 3;
real K = 0.01;
if (Xp > Xcr)
{
f2_Xp = 1;
}
else
{
f2_Xp = pow(Xp, n) / (pow(Xp, n) + K);
}
Xp_star = f1_Tp * f2_Xp;

// Store the value in UDM
C_UDMI(c, t, 8) = f2_Xp;
C_UDMI(c, t, 9) = Xp_star;

// Calculate moisture source term
m_dot = hs * (6 / dp) * (Xp_star - Xf);

// Store the value in UDM
C_UDMI(c, t, 10) = m_dot;

dS[eqn] = 0;
return m_dot;
}

Last edited by Chhotelal1234; August 13, 2023 at 03:01.
Chhotelal1234 is offline   Reply With Quote

Old   August 14, 2023, 02:16
Default
  #17
Senior Member
 
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34
AlexanderZ will become famous soon enoughAlexanderZ will become famous soon enough
compile code and fix error, it is that simple
Code:
#include "udf.h"

#define Xp_initial 0.25 // initial moisture content
#define Xf_initial 0.02 // moisture content of air
#define Xcr 0.16 // critical moisture content
#define epsilon_f 0.37 // gas volume fraction
#define Tp_initial 300 // initial temperature of particles
#define dp 0.000380 // diameter of the particle (m)

// Define the reference temperature constant outside the macro
const real Tref = 298.15;


DEFINE_SOURCE(mass_source_gas, c, t, dS, eqn)
{
real hs, P_sat;
real Re_p, Sc, Sh, f1_Tp, f2_Xp, Xp_star, Dv, m_dot;
int n = 3;
real K = 0.01;
/* Obtain the necessary parameters (Tp, Xp) */
real Tp = C_T(c, t); // Temperature of particles
real Xp = C_UDMI(c, t, 0); // Moisture content
real Xf = C_UDSI(c, t, 0); // air moisture content

// Calculate water vapor diffusion coefficient
Dv = 2.6e-5 * pow((Tp / Tref), 1.5);

// Store the value in UDM
C_UDMI(c, t, 1) = Dv;

// Obtain the necessary parameters (hs, P_sat)
P_sat = 1e5 * exp(13.869 - (5173 / Tp));

// Store the value in UDM
C_UDMI(c, t, 2) = P_sat;

// Calculate Sherwood number
Re_p = (C_R(c, t) * C_V(c, t) * dp) / C_MU_L(c, t);
Sc = C_MU_L(c, t) / (C_R(c, t) * Dv);

// Store the value in UDM
C_UDMI(c, t, 3) = Re_p;
C_UDMI(c, t, 4) = Sc;

// Calculate Sherwood number
Sh = (7 - 10 * epsilon_f + 5 * pow(epsilon_f, 2))* (1 + 0.7 * pow(Re_p, 0.2) * pow(Sc, 1.0 / 3.0)) + ((1.33 - 2.4 * epsilon_f + 1.2 * pow(epsilon_f, 2)) * pow(Re_p, 0.7) * pow(Sc, 1.0 / 3.0));

// Store the value in UDM
C_UDMI(c, t, 5) = Sh;

hs = (Sh * Dv) / dp;

// Store the value in UDM
C_UDMI(c, t, 6) = hs;

// Calculate equilibrium moisture content
f1_Tp = 0.622 * (P_sat / (101325 - P_sat));

// Store the value in UDM
C_UDMI(c, t, 7) = f1_Tp;


if (Xp > Xcr)
{
f2_Xp = 1;
}
else
{
f2_Xp = pow(Xp, n) / (pow(Xp, n) + K);
}
Xp_star = f1_Tp * f2_Xp;

// Store the value in UDM
C_UDMI(c, t, 8) = f2_Xp;
C_UDMI(c, t, 9) = Xp_star;

// Calculate moisture source term
m_dot = hs * (6 / dp) * (Xp_star - Xf);

// Store the value in UDM
C_UDMI(c, t, 10) = m_dot;

dS[eqn] = 0;
return m_dot;
}
__________________
best regards


******************************
press LIKE if this message was helpful
AlexanderZ is offline   Reply With Quote

Reply


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
Parallel Error in ANSYS FLUENT 12 zeusxx FLUENT 25 July 17, 2015 04:40


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