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

Struggling with if-else statement in UDF

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By AlexanderZ

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 27, 2023, 09:28
Default Struggling with if-else statement in UDF
  #1
New Member
 
Mumin Biyiklioglu
Join Date: Sep 2023
Posts: 3
Rep Power: 2
maggezer is on a distinguished road
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
maggezer is offline   Reply With Quote

Old   October 4, 2023, 01:44
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
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 likes this.
__________________
best regards


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

Old   October 10, 2023, 15:21
Default
  #3
New Member
 
Mumin Biyiklioglu
Join Date: Sep 2023
Posts: 3
Rep Power: 2
maggezer is on a distinguished road
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;
}
maggezer is offline   Reply With Quote

Old   October 11, 2023, 00:26
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
Code:
static real x_pos = 0.0;
why is it static?
__________________
best regards


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

Old   October 11, 2023, 06:50
Default
  #5
New Member
 
Mumin Biyiklioglu
Join Date: Sep 2023
Posts: 3
Rep Power: 2
maggezer is on a distinguished road
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
maggezer is offline   Reply With Quote

Reply

Tags
ifelse, pulse, udf


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
Call defined property by UDF to another UDF Selawe97 FLUENT 0 December 27, 2022 08:57
Understanding of contact detection UDF Silence Fluent UDF and Scheme Programming 0 June 10, 2021 04:30
Unable to use if statement in UDF AGP Fluent UDF and Scheme Programming 22 June 6, 2016 10:03
Opening a file and if statement before a DPM UDF anthony05 Fluent UDF and Scheme Programming 4 August 14, 2014 00:39
UDF "AND" Statement Curtis FLUENT 1 December 11, 2002 21:34


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