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

Temperature and Time based UDF

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 19, 2016, 04:04
Smile Temperature and Time based UDF
  #1
New Member
 
Simon
Join Date: Dec 2016
Posts: 14
Rep Power: 9
jchan033 is on a distinguished road
Hi everyone,

I'm new to writing UDFs, and my current problem is (refer to image)

I want to have a square wave, where at a specific edge to have an increment of temperature (or addition of heat flux) for 10 seconds, and the following 10 seconds to have no heat addition. Cycle to repeat.

My thoughts are wrapping around a sinusoidal equation.

This is what I've tried

/************************************************** *********************
UDF for specifying an varying temp with time profile boundary profile
************************************************** **********************/

#include "udf.h"
#define A 1000

DEFINE_PROFILE(temp, thread, position)
{
face_t f;
real t = CURRENT_TIME;

begin_f_loop(f, thread)
{
F_PROFILE(f, thread, position) = Asin(0.05*t);
}
end_f_loop(f, thread)
}

I know there should be more edits to the sine, perhaps 'absoluting' it in Excel terms, but I'm unsure of the conventions in ANSYS UDF. And a square wave is not just a simple sine as I have written.

Thanks for reading, and looking forward to any input!

Simon
Attached Images
File Type: jpg Question.jpg (27.8 KB, 21 views)
jchan033 is offline   Reply With Quote

Old   December 19, 2016, 07:56
Default
  #2
Senior Member
 
Kevin
Join Date: Dec 2016
Posts: 138
Rep Power: 9
KevinZ09 is on a distinguished road
I'm not completely getting your question but if you're asking how to generate a square wave I'd suggest using the modulus operator. You can do for example:

#include "udf.h"
#define A 1000

DEFINE_PROFILE(temp, thread, position)
{
face_t f;
real t = CURRENT_TIME;
real per = 20.0; /* period of your oscillation */

begin_f_loop(f, thread)
{
if ( (t % p)/p < 0.5)
{
F_PROFILE(f,thread,position) = A; /* on for the first 10 seconds of your period */
}
else
{
F_PROFILE (f,thread,position) = 0; /* off for the 2nd half */
}
}
end_f_loop(f, thread)
}

I'm not totally sure you can use modulus on reals; in some languages you can and some later versions allow you to use it too. If it doesn't just write your own version of it, which is quite straightforward.
KevinZ09 is offline   Reply With Quote

Old   December 19, 2016, 09:26
Default
  #3
New Member
 
Simon
Join Date: Dec 2016
Posts: 14
Rep Power: 9
jchan033 is on a distinguished road
Hi Kevin,

Thank you for replying to my question!

Though, may I know what does "if ( (t % p)/p < 0.5)" mean?

I understand t is the current time, but what is p? and the significance of '%' in this line?
jchan033 is offline   Reply With Quote

Old   December 19, 2016, 09:40
Default
  #4
Senior Member
 
Kevin
Join Date: Dec 2016
Posts: 138
Rep Power: 9
KevinZ09 is on a distinguished road
Oh, sorry, on my paper I scrabbled it as p, but in the code I posted I used "per" (short for period). But yeah, it should read if ( (t % per)/per < 0.5).

This if-statement basically checks if you are in the first 10 seconds of your 20 seconds period (10 seconds flux on, then 10 seconds flux off) or in the second half of it.
- "t % per" means modulus of t with per, which calculates the remainder of the division of t/p. For example, if t = 5 and p = 2, the remainder is 1 (and the quotient is 2). Google for remainder and quotient if you're not familiar with it.
- Knowing the remainder, you can know if you're in the 1st or 2nd half of your period by dividing by the period itself.

You can generalize the code in case your period isn't half on then half off, simply by changing the 0.5 to your desired fraction of the period you want the heat flux to be on (or off).

Hope this is more clear?
KevinZ09 is offline   Reply With Quote

Old   December 19, 2016, 10:55
Default
  #5
New Member
 
Simon
Join Date: Dec 2016
Posts: 14
Rep Power: 9
jchan033 is on a distinguished road
Hi Kevin,

Thank you very much! You have been very informative, I really appreciate your time and effort!

Cheers!
jchan033 is offline   Reply With Quote

Old   December 21, 2016, 22:07
Default
  #6
New Member
 
Simon
Join Date: Dec 2016
Posts: 14
Rep Power: 9
jchan033 is on a distinguished road
Quote:
Originally Posted by KevinZ09 View Post
Oh, sorry, on my paper I scrabbled it as p, but in the code I posted I used "per" (short for period). But yeah, it should read if ( (t % per)/per < 0.5).

This if-statement basically checks if you are in the first 10 seconds of your 20 seconds period (10 seconds flux on, then 10 seconds flux off) or in the second half of it.
- "t % per" means modulus of t with per, which calculates the remainder of the division of t/p. For example, if t = 5 and p = 2, the remainder is 1 (and the quotient is 2). Google for remainder and quotient if you're not familiar with it.
- Knowing the remainder, you can know if you're in the 1st or 2nd half of your period by dividing by the period itself.

You can generalize the code in case your period isn't half on then half off, simply by changing the 0.5 to your desired fraction of the period you want the heat flux to be on (or off).

Hope this is more clear?
Hi Kevin,

I have encountered an issue.

Error: C:\\Users\\hallo\\Desktop\\Stuff\\NTU\\FYP\\Stage 2\\temp&time.c: line 16: invalid type for integral binary expression: double % double.

Can you assist?

Thanks!
jchan033 is offline   Reply With Quote

Old   December 22, 2016, 03:25
Default
  #7
Senior Member
 
Kevin
Join Date: Dec 2016
Posts: 138
Rep Power: 9
KevinZ09 is on a distinguished road
That's because the modulus needs integers as operands. There's two alternatives you can try:

1: Use C's real version of %, fmod. Then "if ( (t % per)/per < 0.5)" becomes "if ( (fmod(t,per))/per < 0.5 )"
2: Or write your own version of the modulus operator: "if ( (t % per)/per < 0.5)" becomes "if ( (t - per*int(t/per))/per) < 0.5 )"

The latter one should definitely work. The first one depends again on your compiler.
KevinZ09 is offline   Reply With Quote

Reply

Tags
udf time temperature


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
UDF to get the average temperature of a face and use it to define a property gdb Fluent UDF and Scheme Programming 19 November 3, 2022 03:11
UDF for a time varying temperature boundary conditions peppelmb FLUENT 0 October 22, 2015 04:20
UDF problem- time dependent temperature at inlet kaeran FLUENT 1 June 16, 2015 21:48
UDF for variable temperature with time CaglarCoskun Fluent UDF and Scheme Programming 8 January 15, 2014 09:15
Inlet won't apply UDF and has temperature at 0K! tccruise Fluent UDF and Scheme Programming 2 September 14, 2012 06:08


All times are GMT -4. The time now is 16:41.