
[Sponsors] 
December 25, 2013, 16:37 
Please Help! Periodic Function that does not use sin/cos

#1 
New Member
Mike
Join Date: Dec 2013
Posts: 7
Rep Power: 5 
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. 

December 30, 2013, 08:59 

#2 
Senior Member
Join Date: Nov 2013
Posts: 1,005
Rep Power: 14 
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; 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; 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. 

December 30, 2013, 16:11 
It worked

#3 
New Member
Mike
Join Date: Dec 2013
Posts: 7
Rep Power: 5 
Thank you very much for this! My logic for programming is sub par and I doubt I would have figured this one out.


December 31, 2013, 06:13 

#4 
Senior Member
Join Date: Nov 2013
Posts: 1,005
Rep Power: 14 
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! 

Thread Tools  
Display Modes  


Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
swak4Foam installation problem  Claudio87  OpenFOAM  9  May 8, 2013 10:20 
nonorthogonal faces and incorrect orientation?  nennbs  OpenFOAM Native Meshers: blockMesh  7  April 17, 2013 05:42 
Periodic sinusoidal wall motion  Field Function  smorls  STARCCM+  0  March 6, 2013 13:27 
OpenFOAM static build on Cray XT5  asaijo  OpenFOAM Installation  9  April 6, 2011 12:21 
Version 15 on Mac OS X  gschaider  OpenFOAM Installation  120  December 2, 2009 11:23 