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

Constructors in OpenFOAM

Register Blogs Community New Posts Updated Threads Search

Like Tree3Likes
  • 2 Post By geth03
  • 1 Post By geth03

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 18, 2020, 08:13
Default Constructors in OpenFOAM
  #1
New Member
 
Join Date: Nov 2020
Posts: 6
Rep Power: 5
user92 is on a distinguished road
Hello!

I appreciate any direct help with the topic below ... or a reference/guide/video which explains the full story for beginners
________________________________________________
my understanding of constructors in C++ is the following:

Class name (parameter list)
:
Member initializer list
{statement}

I tried to match with OpenFOAM using Lines 37 to 47 below from the code here

-------------------------------------------------
Foam:: prghPressureFvPatchScalarField::
prghPressureFvPatchScalarField
(
const fvPatch& p,
const DimensionedField<scalar, volMesh>& iF
)
:
fixedValueFvPatchScalarField(p, iF),
rhoName_("rho"),
p_(p.size(), Zero)
{}

-------------------------------------------------

My guess (please correct me if I'm wrong)
- Class name is prghPressureFvPatchScalarField

- fvPatch and DimensionedField<scalar, volMesh> are parameters and they were given aliases p and iF

my questions
- what is fixedValueFvPatchScalarField(p, iF)

- {} is empty, I'm not sure what the constructor is doing overall

- what is the first line Foam::X::X (I've seen this frequently in different codes)

Thank you
user92 is offline   Reply With Quote

Old   November 20, 2020, 04:39
Default
  #2
Senior Member
 
Join Date: Dec 2019
Location: Cologne, Germany
Posts: 355
Rep Power: 8
geth03 is on a distinguished road
i commented the lines for you

Foam:: prghPressureFvPatchScalarField:: //Foam is the namespace :: yourClassName :: yourConstructorAsFunction
prghPressureFvPatchScalarField
(
const fvPatch& p,
const DimensionedField<scalar, volMesh>& iF
)
:
fixedValueFvPatchScalarField(p, iF), //your class inherits from this class, so
//this must be included, you cannot ignore
//the parent class, if multiple parents exist, do same for other parents
rhoName_("rho"), // next two lines overwrite attributes
p_(p.size(), Zero)
{} //you already overwrote attributes, so
// nothing to do for that class here,
// other classes might need it to do stuff
Reptider and user92 like this.
geth03 is offline   Reply With Quote

Old   November 20, 2020, 05:16
Default
  #3
New Member
 
Join Date: Nov 2020
Posts: 6
Rep Power: 5
user92 is on a distinguished road
Thank you I have a few more clarifications if you don't mind

Quote:
Originally Posted by geth03 View Post

fixedValueFvPatchScalarField(p, iF), //your class inherits from this class,
I'm used to seeing braces {} used with classes, so I wonder what would className() mean? what are p & iF in this case? are they arguments? objects?


Quote:
Originally Posted by geth03 View Post
rhoName_("rho"), // next two lines overwrite attributes
p_(p.size(), Zero)
to make sure I understood this, can I generalize it as variablename_(update)

where the update could be a new name or some changes on how the variable is defined
user92 is offline   Reply With Quote

Old   November 24, 2020, 08:46
Default
  #4
Senior Member
 
Join Date: Dec 2019
Location: Cologne, Germany
Posts: 355
Rep Power: 8
geth03 is on a distinguished road
i don't quite get what you mean by className(),
but let my clarify few things.

p and iF are arguments for the function fixedValueFvPatchScalarField,
fixedValueFvPatchScalarField(p, iF) is a parameter constructor bc it takes arguments to construct an instance. what p and iF mean you can look up and decipher from the code (p means pressure, don't know what iF means, didnt look it up, maybe initialField?).

if you have a constructor without an argument (maybe you mean that by className()) you can create instances from that class without providing input.

next point:
if you want to update an attribute automatically when creating an instance of a class you can do this like
rhoName_("rho") // so rhoName_-value will be updated with rho-value.
user92 likes this.
geth03 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
Frequently Asked Questions about Installing OpenFOAM wyldckat OpenFOAM Installation 3 November 14, 2023 11:58
OpenFOAM Foundation Releases OpenFOAM v2.3.0 opencfd OpenFOAM Announcements from OpenFOAM Foundation 3 December 23, 2014 03:43
Suggestion for a new sub-forum at OpenFOAM's Forum wyldckat Site Help, Feedback & Discussions 20 October 28, 2014 09:04
[Gmsh] 2D Mesh Generation Tutorial for GMSH aeroslacker OpenFOAM Meshing & Mesh Conversion 12 January 19, 2012 03:52
The OpenFOAM extensions project mbeaudoin OpenFOAM 16 October 9, 2007 09:33


All times are GMT -4. The time now is 18:04.