CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Is it possible to use Info inside #calc to print a string? (https://www.cfd-online.com/Forums/openfoam-programming-development/239595-possible-use-info-inside-calc-print-string.html)

NotOverUnderated November 14, 2021 09:59

Is it possible to use Info inside #calc to print a string?
 
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

olesen November 20, 2021 06:17

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";


NotOverUnderated November 21, 2021 00:06

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

olesen November 23, 2021 11:26

Quote:

Originally Posted by NotOverUnderated (Post 816956)
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 November 23, 2021 14:33

Quote:

Originally Posted by olesen (Post 817185)
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 November 23, 2021 19:42

Quote:

Originally Posted by olesen (Post 817203)
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.

olesen November 24, 2021 03:59

Quote:

Originally Posted by NotOverUnderated (Post 817220)
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.

olesen November 26, 2021 13:25

Quote:

Originally Posted by NotOverUnderated (Post 817220)
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

NotOverUnderated November 27, 2021 00:19

Quote:

Originally Posted by olesen (Post 817450)

Amazing!

Please accept my deepest thanks.


All times are GMT -4. The time now is 15:49.