CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Abs function in OpenFOAM

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By Tobermory

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 19, 2021, 12:42
Default Abs function in OpenFOAM
  #1
Senior Member
 
NotOverUnderated's Avatar
 
ONESP-RO
Join Date: Feb 2021
Location: Somwhere on Planet Earth
Posts: 127
Rep Power: 5
NotOverUnderated is on a distinguished road
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.
NotOverUnderated is offline   Reply With Quote

Old   May 15, 2023, 13:05
Default
  #2
Senior Member
 
Join Date: Sep 2017
Posts: 246
Rep Power: 11
obscureed is on a distinguished road
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?
obscureed is offline   Reply With Quote

Old   May 15, 2023, 16:23
Default
  #3
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 666
Rep Power: 14
Tobermory will become famous soon enough
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.
eduardopaiva likes this.
Tobermory is offline   Reply With Quote

Old   May 16, 2023, 04:48
Default
  #4
Senior Member
 
Join Date: Sep 2017
Posts: 246
Rep Power: 11
obscureed is on a distinguished road
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?
obscureed is offline   Reply With Quote

Old   May 19, 2023, 12:25
Default
  #5
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 666
Rep Power: 14
Tobermory will become famous soon enough
Quote:
Originally Posted by obscureed View Post
Is there any way to warn users if they are making this mistake?
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.
Tobermory is offline   Reply With Quote

Reply


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
[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


All times are GMT -4. The time now is 01:02.