CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Community Contributions (https://www.cfd-online.com/Forums/openfoam-community-contributions/)
-   -   [swak4Foam] and atan2 (https://www.cfd-online.com/Forums/openfoam-community-contributions/182893-atan2.html)

-luk- January 21, 2017 11:56

and atan2
 
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- January 22, 2017 05:43

atan2 in funkySetFieldsDict
 
1 Attachment(s)
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


All times are GMT -4. The time now is 00:48.