CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Community Contributions

[swak4Foam] and atan2

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 21, 2017, 12:56
Default and atan2
  #1
New Member
 
Join Date: Feb 2012
Posts: 5
Rep Power: 14
-luk- is on a distinguished road
Hello all,

I'm trying to initialise a field depending on the polar angle and just noticed, there is no atan2 in swak4foam. The workaround would be to use atan with some conditions as shown here:
https://en.wikipedia.org/wiki/Atan2

Atan is not going from -pi to pi.

Does somebody have a solution to this problem already? Am I overseening a trivial solution?

Regards, Luk
-luk- is offline   Reply With Quote

Old   January 22, 2017, 06:43
Default atan2 in funkySetFieldsDict
  #2
New Member
 
Join Date: Feb 2012
Posts: 5
Rep Power: 14
-luk- is on a distinguished road
I found a solution to the problem. I used a sequence of ifs written down as ternary operators. The math is taken from here:
https://de.mathworks.com/matlabcentr...from-0-to-2-pi

This is the snippet to use in funkySetFieldsDict. Please note, I'm in the y-z-plane instead of the x-y-plane used in the explanation. I simply replaced x by z. Please check if the angle definition fits your coordinate system before use.

Code:
myVariables (
    // code for y-z-plane polar angle
    "theta=0;"

    //if x>0
    //    v=atan(y/x);
    //end
    "theta=(pos().z>0)?atan(pos().y/pos().z):theta;"

    //if y>=0 & x<0
    //    v=pi+atan(y/x);
    //end
    "theta=((pos().y>=0)&&(pos().z<0))?pi+atan(pos().y/pos().z):theta;"

    //if y<0 & x<0
    //    v=-pi+atan(y/x);
    //end
    "theta=((pos().y<0)&&(pos().z<0))?-pi+atan(pos().y/pos().z):theta;"

    //if y>0 & x==0
    //    v=pi/2;
    //end
    "theta=((pos().y>0)&&(pos().z==0))?pi/2:theta;"

    //if y<0 & x==0
    //    v=-pi/2;
    //end
    "theta=((pos().y<0)&&(pos().z==0))?-pi/2:theta;"

    //if v<0
    //    v=v+2*pi;
    //end
    // uncomment if you need the angle range to be 0:2pi instead of -pi:pi
    //"theta=(theta<0)?theta+2*pi:theta;"
);
Best regards, Luk
Attached Images
File Type: jpg theta.jpg (121.1 KB, 31 views)
-luk- is offline   Reply With Quote

Reply

Tags
atan2, swak4foam

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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



All times are GMT -4. The time now is 04:06.