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. |
The problem is in this part:
Code:
real t_1 = CURRENT_TIME; 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; 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. |
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.
|
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. |