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

Referencing variable from different code

Register Blogs Community New Posts Updated Threads Search

Like Tree2Likes
  • 1 Post By Tobermory
  • 1 Post By songyi719

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   March 2, 2024, 03:27
Default Referencing variable from different code
  #1
Member
 
Song Young Ik
Join Date: Apr 2022
Location: South Korea
Posts: 57
Rep Power: 4
songyi719 is on a distinguished road
Hello, I am modifying scalartransportFoam to make some multiple scalar properties I need. I first modified to create scalar named 'zeta', by modifying PDE from scalarTransport.c and changing name of scalar from 's' to 'zeta'. Then, i made another code which is also modified version of scalarTransport.c, which uses 'zeta' scalar for transport equation.


So, I tried to make the code gets data of 'zeta' as it does to rho and phi, so I wrote the line 'zetaName_ = dict.lookupOrDefault<word>("zeta", "zeta");' and 'mesh_.lookupObject<volScalarField>(zetaName_);' each below the part they reference rho and phi. However it seems like the code doesn't compile well.



Below is the error code



Code:
avgageTransport.C: In member function ‘virtual bool Foam::functionObjects::avgageTransport::read(const Foam::dictionary&)’:
avgageTransport.C:212:5: error: ‘zetaName_’ was not declared in this scope
  212 |     zetaName_ = dict.lookupOrDefault<word>("zeta", "zeta");
      |     ^~~~~~~~~
avgageTransport.C: In member function ‘virtual bool Foam::functionObjects::avgageTransport::execute()’:
avgageTransport.C:252:44: error: ‘zetaName_’ was not declared in this scope
  252 |         mesh_.lookupObject<volScalarField>(zetaName_);
      |                                            ^~~~~~~~~
avgageTransport.C:263:27: warning: unused variable ‘fvModels’ [-Wunused-variable]
  263 |     const Foam::fvModels& fvModels(Foam::fvModels::New(mesh_));
      |                           ^~~~~~~~
make: *** [/opt/openfoam11/wmake/rules/General/transform:26: Make/linux64GccDPInt32Opt/avgageTransport.o] Error 1

What may have caused the problem? Do these lookupOrDefault and lookupObject functions work different to rho/phi and zeta? I am not sure about how OF saves and load variable data
songyi719 is offline   Reply With Quote

Old   March 2, 2024, 07:10
Default
  #2
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 668
Rep Power: 14
Tobermory will become famous soon enough
Quote:
error: ‘zetaName_’ was not declared in this scope
Looks like you forgot to declare the variable, zetaName_.
Tobermory is offline   Reply With Quote

Old   March 2, 2024, 08:59
Default
  #3
Member
 
Song Young Ik
Join Date: Apr 2022
Location: South Korea
Posts: 57
Rep Power: 4
songyi719 is on a distinguished road
Do I have to externally declare the 'zetaName_'?

I thought the dict.lookupOrDefault is the function that creates the variable, since the function was doing the same job for rho and phi on original code, and they didn't have declared themselves before the 'rhoName_ = dict.lookupOrDefault<word>("rho", "rho");" stuffs... I am confused how the rhoName_ and phiName_ managed to compile without error but zetaName didn't.



Also, how does this function actually work? It seems like it searches for field named "zeta" from the dictionary, and uses the latter "zeta", i have no idea what actually means by the way.


Since zeta is also created from my another code which is modification of scalartransport, I'm not sure if it is actually stored in the so called 'dict', since I don't know how they do input on to the dictionary. Sorry for my low understanding in OF structure since I've just started OF programming...
songyi719 is offline   Reply With Quote

Old   March 2, 2024, 09:31
Default
  #4
Senior Member
 
Join Date: Apr 2020
Location: UK
Posts: 668
Rep Power: 14
Tobermory will become famous soon enough
Lots to unpack here!
Quote:
Do I have to externally declare the 'zetaName_'?
Yes - you have to declare it somewhere!

Quote:
I thought the dict.lookupOrDefault is the function that creates the variable, since the function was doing the same job for rho and phi on original code, and they didn't have declared themselves before the 'rhoName_ = dict.lookupOrDefault<word>("rho", "rho");" stuffs...
Nope. The lookupOrDefault function is just that - a function that returns an object. Look earlier in the coding and you'll see that both rho and phi are explicitly declared, eg
Code:
      const volScalarField& rho =
             mesh_.lookupObject<volScalarField>(rhoName_);
Quote:
I am confused how the rhoName_ and phiName_ managed to compile without error but zetaName didn't.
Check in scalarTransport.H and you'll find that they are defined as private members of the class.


Quote:
Also, how does this function actually work? It seems like it searches for field named "zeta" from the dictionary, and uses the latter "zeta", i have no idea what actually means by the way.
I assume you are talking about the lines like:
Code:
rhoName_ = dict.lookupOrDefault<word>("rho", "rho");
This line searches dictionary dict for keyword "rho" ... if it finds it, it reads the value and sets rhoName_ to it (i.e. uses that as the name for the rho field); if it doesn't find it, it uses the default name, which is the second "rho" in the brackets. i.e. it's just setting the name for the field. It's an optional dictionary entry, as you'll see from the .H file.

Quote:
Since zeta is also created from my another code which is modification of scalartransport, I'm not sure if it is actually stored in the so called 'dict', since I don't know how they do input on to the dictionary. Sorry for my low understanding in OF structure since I've just started OF programming...
Don't worry - you're on a long journey, but keep at it and gradually it will become easier. There is a LOT to take in, so my suggestions are:

1. brush up on your C++ programming skills
2. make extensive use of the Doxygen pages (eg https://cpp.openfoam.org/v8/scalarTr...8H_source.html)
3. Start to get your head around how OpenFOAM does things ... classes, dictionaries etc.
4. Google lots, to find helpful resources - there's plenty of training material, MSc theses etc. out there
5. try to work out the answers as much as you can, yourself - you'll learn enormously that way; come back to the forum when you get stuck


Good luck!
songyi719 likes this.
Tobermory is offline   Reply With Quote

Old   March 2, 2024, 09:50
Default
  #5
Member
 
Song Young Ik
Join Date: Apr 2022
Location: South Korea
Posts: 57
Rep Power: 4
songyi719 is on a distinguished road
Oh god you are my livesaver! I had a bit experience on C, but never worked on such large software, so I've never thought of seeking variables in header file before.



Thank you very much. Now I got another clue about how OF works. Also, thanks for the last tip!
Tobermory likes this.
songyi719 is offline   Reply With Quote

Reply

Tags
lookup, scalartransport, variable, zeta


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
general ARCHITECTURAL structure of the code of a CFD code mecobio Main CFD Forum 0 August 12, 2013 08:04
The FOAM Documentation Project - SHUT-DOWN holger_marschall OpenFOAM 242 March 7, 2013 12:30
Problems with additional variable Krishna Premi CFX 1 October 29, 2007 08:19
Design Integration with CFD? John C. Chien Main CFD Forum 19 May 17, 2001 15:56
own Code vs. commercial code Bernhard Mueck Main CFD Forum 10 February 16, 2000 10:07


All times are GMT -4. The time now is 08:30.