|
[Sponsors] |
October 19, 2021, 12:42 |
Abs function in OpenFOAM
|
#1 |
Senior Member
ONESP-RO
Join Date: Feb 2021
Location: Somwhere on Planet Earth
Posts: 127
Rep Power: 5 |
I am using OpenFOAM 7 to program a custom function object. Somewhere in the code, I needed to compute the absolute value of a scalar.
The code never worked as expected, and I had to spend approximately 2 hours in order to find the bug which turns out to be: abs doesn't return the absolute value. for example abs(5.222) will always return 5. * I am wondering why abs function in OpenFOAM behaves like that? what is the purpose? (unless I am missing something, I find it misleading). * How to compute the absolute value of a scalar in OpenFOAM? (I know I can do: value = (value < 0)? -value: value) but I would like to know if there is a function to do that) BTW, I needed to use abs from cmath which returns the expected value. Could you please explain that? Thank you d.f Update: To compute the absolute value of a scalar in OpenFOAM use mag function, e.g mag(-7.556) will return 7.556. Last edited by NotOverUnderated; October 19, 2021 at 14:36. |
|
May 15, 2023, 13:05 |
|
#2 |
Senior Member
Join Date: Sep 2017
Posts: 246
Rep Power: 11 |
This is a very valid question (though old and unanswered). Can someone point to the documentation of abs(), please, and an explanation of why it does not calculate the absolute value?
|
|
May 15, 2023, 16:23 |
|
#3 |
Senior Member
Join Date: Apr 2020
Location: UK
Posts: 666
Rep Power: 14 |
There is no abs() function in OpenFOAM ... it's a C++ function that returns the absolute value of an integer (eg see https://cplusplus.com/reference/cstdlib/abs/). If called with a double, abs() will covert the double to an int and then return its absolute value. So all working as intended. As the OP points out - mag was the function that he wanted.
|
|
May 16, 2023, 04:48 |
|
#4 |
Senior Member
Join Date: Sep 2017
Posts: 246
Rep Power: 11 |
Ah, OK, thanks. Is there any way to warn users if they are making this mistake? -- As the OP mentioned, it's unexpected and difficult to detect.
From the C++ documentation, abs(float) will work as expected if <cmath> is included. What would this look like in a coded runtime function? |
|
May 19, 2023, 12:25 |
|
#5 |
Senior Member
Join Date: Apr 2020
Location: UK
Posts: 666
Rep Power: 14 |
I am afraid not. Again - this has nothing to do with OpenFOAM. You'd have the same issue if you were writing your own standalone C++ program ... My suggestion, if you are coding, is to check out any maths function that you are using in the OpenFOAM DOxygen literature. I am often doing that, for functions like pow() etc.
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
[Other] refineWallLayer Error | Yuby | OpenFOAM Meshing & Mesh Conversion | 2 | November 11, 2021 11:04 |
whats the cause of error? | immortality | OpenFOAM Running, Solving & CFD | 13 | March 24, 2021 07:15 |
How to contribute to the community of OpenFOAM users and to the OpenFOAM technology | wyldckat | OpenFOAM | 17 | November 10, 2017 15:54 |
OpenFOAM 4.0 Released | CFDFoundation | OpenFOAM Announcements from OpenFOAM Foundation | 2 | October 6, 2017 05:40 |
[blockMesh] Errors during blockMesh meshing | Madeleine P. Vincent | OpenFOAM Meshing & Mesh Conversion | 51 | May 30, 2016 10:51 |