CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Cube root in OpenFOAM (https://www.cfd-online.com/Forums/openfoam-solving/218737-cube-root-openfoam.html)

zcheng July 2, 2019 08:38

Cube root in OpenFOAM
 
Hello everyone,

Does anyone knows how to put cube root in OpenFOAM?

clapointe July 5, 2019 08:59

Yes, this is quite straightforward. Knowing that we can calculate the square root of something e.g. x with :

Code:

sqrtX = sqrt(x)
we might guess that foam would use similar abbreviation for the cube root. So we guess that cbrt is likely and search the code for "cbrt" and see many matches. You could try computing a cube root of x as :

Code:

cbrtX = cbrt(x)
I've not used this myself before, so be sure to make sure it does what we expect. Another straightforward option that I use sometimes is the pow function :

Code:

cbrtX = pow(x,1.0/3.0)
Caelan

zcheng July 5, 2019 09:42

Thanks a lot!

anon_q July 5, 2019 09:43

For a more safe result (including negative numbers) you can use the following:



Code:

// calculate the cube root of abs(x)
cbrtAbsX = pow(abs(x), 1.0/3.0)
 // the cube root is returned according to the sign of x
cbrtX = (x < 0)? -cbrtAbsX:cbrtAbsX


zcheng July 5, 2019 10:24

Thanks for the suggestion!


All times are GMT -4. The time now is 07:03.