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

Is it possible to use Info inside #calc to print a string?

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 1 Post By olesen
  • 1 Post By olesen
  • 2 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 14, 2021, 09:59
Question Is it possible to use Info inside #calc to print a string?
  #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
Hello,

I have a question about #calc directive in OpenFOAM v2106.
I know how to print a numeric value as follow:

Code:
myVariable      42;
#calc "Info << $myVariable << endl";
I know also that #calc is wrapper around #codeStream so if one needs to do more advanced computations, he/she must use #codeStream. (There are expressions also but I don't think they are relevant in my context ).

When I try to print a string message:

Code:
msg     "This is a message";
#calc "Info << $msg << endl";
But that doesn't work, here is the error message:
Code:
codeStreamTemplate.C:59:20: error: ‘This’ was not declared in this scope
   59 |     os << (Info << This is a message << endl);
      |                    ^~~~
codeStreamTemplate.C:59:24: error: expected ‘)’ before ‘is’
   59 |     os << (Info << This is a message << endl);
      |           ~            ^~~
I have tried several tricks to escape that string to include double quotes but none has worked so far.

My question:

Is it possible to print a string message using ONLY #code directive (without using #codeStream explicitly)?

Thank you
NotOverUnderated is offline   Reply With Quote

Old   November 20, 2021, 06:17
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
If this is a common enough use case, it might be worthwhile having a "#message" directive in OpenFOAM, otherwise you are throwing a rather expensive compile/link cycle for each "#calc" and just to get some information.

Since the dollar expansion occurs before the string is sent through, you are indeed trying to print bare words. The only way I can see would be to include quotes (haven't tried any of these though)
Code:
msg     #{"This is a message"#};
#calc "Info << $msg << endl";
Code:
msg     "This is a message";
#calc "Info << \"$msg\" << endl";
Or
Code:
msg     "\"This is a message\"";
#calc "Info << $msg << endl";
olesen is offline   Reply With Quote

Old   November 21, 2021, 00:06
Default
  #3
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 would like to thank you for your reply.

I made a mistake when I wrote the post: the #calc in openfoam 2106 doesn't show numeric values. For example:

Code:
myVariable      42;
#calc "Info << $myVariable << endl";
will not work but will work with openfoam8. I think "Info" doesn't work inside openfoam 2106.

I have tried your proposition to display text inside Info and found that the third one works (on openfoam8):

Code:
msg     "\"This is a message\"";
#calc "Info << $msg << endl";
regards

Last edited by NotOverUnderated; November 21, 2021 at 06:56.
NotOverUnderated is offline   Reply With Quote

Old   November 23, 2021, 11:26
Default
  #4
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by NotOverUnderated View Post
I would like to thank you for your reply.

I made a mistake when I wrote the post: the #calc in openfoam 2106 doesn't show numeric values. For example:

Code:
myVariable      42;
#calc "Info << $myVariable << endl";
will not work but will work with openfoam8. I think "Info" doesn't work inside openfoam 2106.

If you look at how calc is implemented:
https://develop.openfoam.com/Develop...alcEntry.C#L85
there is very little difference to explain why it should work at all.


If you expand it out, you are essentially doing this:
Code:
OStringStream os;

os << Info << "something" << nl;
So doing a '<<' operation with two streams. You should note that "Info" is just a wrapper around either cout, or essentially /dev/null (in parallel). I don't see how it should produce anything.


However, I assume that you are actually looking for a '#message' directive, if I am not mistaken.
olesen is offline   Reply With Quote

Old   November 23, 2021, 14:33
Default
  #5
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by olesen View Post
However, I assume that you are actually looking for a '#message' directive, if I am not mistaken.

I have taken a closer look, and opened a corresponding issue.

https://develop.openfoam.com/Develop.../-/issues/2276


We can refine the requirements there (as needed).
/mark
NotOverUnderated likes this.
olesen is offline   Reply With Quote

Old   November 23, 2021, 19:42
Default
  #6
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
Quote:
Originally Posted by olesen View Post
I have taken a closer look, and opened a corresponding issue.

https://develop.openfoam.com/Develop.../-/issues/2276


We can refine the requirements there (as needed).
/mark
Thank you very much for your time.

I think a #message directive would be better since I cannot see how one can easily debug #calc when it is used extensively.
NotOverUnderated is offline   Reply With Quote

Old   November 24, 2021, 03:59
Default
  #7
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by NotOverUnderated View Post
Thank you very much for your time.

I think a #message directive would be better since I cannot see how one can easily debug #calc when it is used extensively.

If you are using '#calc' for actual calculations, I would really suggest taking a look at the '#eval' directive as an alternative. It uses a builtin parser evaluation, which means that you save the entire compile/load cycle. In the OpenFOAM tutorials/ we have managed to replace all instances of '#calc' with '#eval', so there are a reasonable number of examples.
NotOverUnderated likes this.
olesen is offline   Reply With Quote

Old   November 26, 2021, 13:25
Default
  #8
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by NotOverUnderated View Post
Thank you very much for your time.

I think a #message directive would be better since I cannot see how one can easily debug #calc when it is used extensively.

Added into develop, will be in OpenFOAM-v2112.


https://develop.openfoam.com/Develop.../-/issues/2276
https://develop.openfoam.com/Develop...a6c44f7daabd2b
guin and NotOverUnderated like this.
olesen is offline   Reply With Quote

Old   November 27, 2021, 00:19
Default
  #9
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
Quote:
Originally Posted by olesen View Post
Amazing!

Please accept my deepest thanks.
NotOverUnderated is offline   Reply With Quote

Reply

Tags
openfoam, programming


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
decomposePar problem: Cell 0contains face labels out of range vaina74 OpenFOAM Pre-Processing 37 July 20, 2020 05:38
[snappyHexMesh] SHM error: inside mesh not possible Naresh yathuru OpenFOAM Meshing & Mesh Conversion 1 January 10, 2017 23:58
OpenFOAM on MinGW crosscompiler hosted on Linux allenzhao OpenFOAM Installation 127 January 30, 2009 19:08
Modelling the Heat flow inside the curing oven Marios Vlad CFX 1 February 6, 2008 07:11
meshing F1 front wing Steve FLUENT 0 April 17, 2003 12:37


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