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/)
-   -   Please Help! Periodic Function that does not use sin/cos (https://www.cfd-online.com/Forums/fluent-udf/127915-please-help-periodic-function-does-not-use-sin-cos.html)

mabbo009 December 25, 2013 15:37

Please Help! Periodic Function that does not use sin/cos
 
Im having a really tough time producing this periodic function. essentially, i want the pressure pulse to be turned on for .005 seconds and for that to occur every .01 seconds. This is my code.

/************************************************** *********************
stepfunction.c
UDF for specifying step pressure rise boundary condition
************************************************** **********************/
#include "udf.h"

DEFINE_PROFILE(unsteady_pressure, thread, position)
{
face_t f;
real t_1 = CURRENT_TIME;

int i=0;
float t1, t2, pressure;
t1=0.01*i;
t2=t1+0.005;

if (t_1>=t1 && t_1<=t2)
{
pressure = 101325.0*8.0;
}
else
{
pressure = 101325.0*1.0;
i++;
}


begin_f_loop(f,thread)
{
F_PROFILE(f, thread, position) = pressure;
}
end_f_loop(f, thread)

}


In this code I am getting the initial pulse, but after that nothing happens. This is what happens when an undergrad mech eng is assigned out of there comfort zone tasks. Any and all help is greatly appreciated.

pakk December 30, 2013 07:59

The problem is in this part:
Code:

real t_1 = CURRENT_TIME;

int i=0;
float t1, t2, pressure;
t1=0.01*i;
t2=t1+0.005;

I suspect that your intention was that variable "i" would loop over all integers, but this is not what happens; "i" is given the value 0, and never changes. That is why you only see one pulse. (Pulse number 0).

What you want, is to remove multiples of 0.01 seconds (your period) from the current time. One way to do this is to replace the code above by:

Code:

real t_1 = CURRENT_TIME;

float t1, t2, pressure;

t_1 -= (int)(t_1/0.01)*0.01;
t1=0;
t2=0.005;

What this code does: suppose that CURRENT_TIME=0.028.
Then at first, t_1=0.028.
If you would calculate t_1/0.01, you would get 2.8.
To round this down: (int)(t_1/0.01) gives 2 (which is 2.8 rounded down).
This means that there are two periods of 0.01 second before the current time. To remove them, subtract them from your initial time:
t_1=t_1 - (int)(t_1/0.01)*0.01.
This would give t_1=0.008 in the rest of your calculation.
Another way to write this in c, what I did in the code above, is t_1 -= (int)(t_1/0.01)*0.01.

Beware: I only derived this, I did not test it.

mabbo009 December 30, 2013 15:11

It worked
 
Thank you very much for this! My logic for programming is sub par and I doubt I would have figured this one out.

pakk December 31, 2013 05:13

You're welcome. You were very close to the solution by the way; now that I look at it again, I think your way of programming would have worked if you would have defined "int i=0" outside of your function, making it global. In that way it is not reinitialized to 0, every time that the function is called.

Anyway, it is nice to be able to help somebody who already thought about the problem!


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