|
[Sponsors] |
September 27, 2023, 10:28 |
Struggling with if-else statement in UDF
|
#1 |
New Member
Mumin Biyiklioglu
Join Date: Sep 2023
Posts: 3
Rep Power: 3 |
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; } 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 |
|
October 4, 2023, 02:44 |
|
#2 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
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
__________________
best regards ****************************** press LIKE if this message was helpful |
|
October 10, 2023, 16:21 |
|
#3 |
New Member
Mumin Biyiklioglu
Join Date: Sep 2023
Posts: 3
Rep Power: 3 |
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; } |
|
October 11, 2023, 01:26 |
|
#4 |
Senior Member
Alexander
Join Date: Apr 2013
Posts: 2,363
Rep Power: 34 |
Code:
static real x_pos = 0.0;
__________________
best regards ****************************** press LIKE if this message was helpful |
|
October 11, 2023, 07:50 |
|
#5 |
New Member
Mumin Biyiklioglu
Join Date: Sep 2023
Posts: 3
Rep Power: 3 |
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 |
|
Tags |
ifelse, pulse, udf |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Call defined property by UDF to another UDF | Selawe97 | FLUENT | 0 | December 27, 2022 09:57 |
Understanding of contact detection UDF | Silence | Fluent UDF and Scheme Programming | 0 | June 10, 2021 05:30 |
Unable to use if statement in UDF | AGP | Fluent UDF and Scheme Programming | 22 | June 6, 2016 11:03 |
Opening a file and if statement before a DPM UDF | anthony05 | Fluent UDF and Scheme Programming | 4 | August 14, 2014 01:39 |
UDF "AND" Statement | Curtis | FLUENT | 1 | December 11, 2002 22:34 |