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/)
-   -   Struggling with if-else statement in UDF (https://www.cfd-online.com/Forums/fluent-udf/252100-struggling-if-else-statement-udf.html)

maggezer September 27, 2023 09:28

Struggling with if-else statement in UDF
 
Hi I am writing for udf pulsed laser source. But I got problem with changing the velocity(it needs to be 0 when heating and become 1 when waiting time). So I discrete my code understand that origin of problem. So I seperated if statement for source and velocity. If statement for source is working very well. But if statement for vel has a problem. The "else" part is working when condition met but for if part it uses the predefined(0.0) value.





Code:

#include "udf.h"

DEFINE_SOURCE(Goldak7, cell, thread, dS, eqn)
{
real x[ND_ND];
real current_time;
real LayerThickness = 0.00005;
real LaserSpotRadii = 0.000075;
real K = -3.0;
real F = 0.6;
real Q = 2000;
real vel = 0.0;
real x0 = 0.0;
real PI = acos(-1);
real KGoldak;
real x_pos;
real source;
current_time = CURRENT_TIME;
KGoldak = (6 * sqrt(3) * F * Q) / (LaserSpotRadii * LaserSpotRadii * LayerThickness * PI *sqrt(PI));   

C_CENTROID(x, cell, thread);

if ( current_time > 0.01)
{
vel = 0.99;
}
else
{
vel = 0.1;
}

x_pos = vel * current_time;

if ( current_time > 0.01)
{
source  = KGoldak*exp(K*(pow((x[0] - x_pos),2.)/pow(LaserSpotRadii,2.) +  pow(x[1],2.)/pow(LaserSpotRadii,2.) + pow(x[2],2.) /  pow(LayerThickness,2.)));
dS[eqn] = 0.0;
}
else
{
source =  KGoldak*exp(K*(pow((x[0] - x_pos),2.)/pow(LaserSpotRadii,2.) +  pow(x[1],2.)/pow(LaserSpotRadii,2.) + pow(x[2],2.) /  pow(LayerThickness,2.)));
dS[eqn] = 0.0;
}
return source;
}

I am struggling this code for 3 days. I will be really appreciate if you can help.
P.S; I notice while writing this post, I can use initial value like if condition value. But still I am wandering where is my mistake

AlexanderZ October 4, 2023 01:44

vel = 0.1 for time 0 -> 0.01
vel = 0.99 after that
code is correct

your statements for sources are identical, so you don't really need if statement there

maggezer October 10, 2023 15:21

Ah I understand what you mean and thx for help. sorry I was made a lot of change and the second part will be zero.
And also I solved half of my problem. Vel is changing but x_pos still become 0 because of calculation. So I made changes and improvements however while it compiled without problem "x_pos = x_pos + displacement;" line didn't work properly;
Code:

#include "udf.h"
#include "math.h"

real x[ND_ND];
real current_time;
real timestep;
real LaserSpotRadii = 0.000075;
real LayerThickness = 0.00005;
real K = -3.0;
real F = 0.6;

real Q = 600;
real x0 = 0.0;

real pulse_time = 0.006;
real period = 0.008;
real KGoldak;
real vel = 0.0;
static real x_pos = 0.0;
real source = 0.0;
real displacement = 0.0;

DEFINE_SOURCE(PulsedLaser, cell, thread, dS, eqn)
{
real PI = acos(-1);
current_time = CURRENT_TIME;
timestep = CURRENT_TIMESTEP;
KGoldak = (6 * sqrt(3) * F * Q) / (LaserSpotRadii * LaserSpotRadii * LayerThickness * PI *sqrt(PI));

C_CENTROID(x, cell, thread);

if (fmod(current_time, period) < pulse_time)
{
vel = 0.0;
displacement = vel * timestep;
x_pos = x_pos + displacement;
}
else
{
vel = 0.2;
displacement = vel * timestep;
x_pos = x_pos + displacement;
}

if (fmod(current_time, period) < pulse_time)
{
source= KGoldak*exp(K*(pow((x[0]-x_pos),2.)/pow(LaserSpotRadii,2.) + pow(x[1],2.)/pow(LaserSpotRadii,2.) + pow(x[2],2.) / pow(LayerThickness,2.)));
dS[eqn] = 0.0;
}
else
{
source = 0.0;
dS[eqn] = 0.0;
}
return source;
}


AlexanderZ October 11, 2023 00:26

Code:

static real x_pos = 0.0;
why is it static?

maggezer October 11, 2023 06:50

Because of the following part of code, it is incremental and need to change every timestep.
Code:

vel = 0.0;
displacement = vel * timestep;
 x_pos = x_pos + displacement


I solved now with execute at end macro. I get that hint from your answers in another problem thread. Thanks AlexanderZ:D:D


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