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

dimensionedScalar of createField declaration and initialization

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 7, 2017, 19:11
Default dimensionedScalar of createField declaration and initialization
  #1
rsa
New Member
 
Reza
Join Date: Jun 2012
Posts: 27
Rep Power: 14
rsa is on a distinguished road
Hi,

lets say I have a file called "myProproperties" in the constant directory of my case file where the file has an entry like:

myProp
{
myVariable [0 1 0 0 0 0 0] 1;
}

now, in the solver (for simplicity lets say myIcoFoam) I should add the following:

IOdictionary myProperties
(
IOobject
(
"myProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ_IF_MODIFIED,
IOobject::NO_WRITE
)
);
const dictionary& myVariable(myProperties.subDict("myProp"))
dimensionedScalar myVar
(
"myVar",
dimLength,
myVariable.lookup("myVar")
);
So, when I do it like that, I am declaring the variable myVar and initialize it directly.

Now lets say I just want to declare my variables and initialize it later. I looked into the "dimensionedType.C" which I assume is the template class for dimensionedScalar and I found the following constructor:
dimensioned (const word &, const dimensioned< Type > &)

so I declared my variable as:
myVar("myVar", dimLength)

up to here everything was OK but then I needed to initialize the variable and the problem appeared here!!! Firstly, I could not find a member function which can initialize the variable just created!
Looking at the member function of the class it suggest there is a function called "lookupOrAddToDict" but whatever I tried did not worked!

so basically I am wondering how one can keep the initialization of the variable separate from declaration.

Thank you.
rsa is offline   Reply With Quote

Old   October 8, 2017, 06:31
Default
  #2
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

What is wrong with value method (https://cpp.openfoam.org/v5/classFoa...ba781bfdf927)?

I.e. you do something like:

Code:
myVar.value() = 10;
Or you would like to initialise variable with a value from dictionary?
alexeym is offline   Reply With Quote

Old   October 8, 2017, 06:43
Default
  #3
rsa
New Member
 
Reza
Join Date: Jun 2012
Posts: 27
Rep Power: 14
rsa is on a distinguished road
Hi Alexey,

the value() function as it is stated, returns a reference (const or non-const) to value but in my case I want to initialize it with the value aviable in the dictionary (the value 1 in "myProperties" [0 1 0 0 0 0 0] 1).
rsa is offline   Reply With Quote

Old   October 8, 2017, 07:59
Default
  #4
Senior Member
 
Alexey Matveichev
Join Date: Aug 2011
Location: Nancy, France
Posts: 1,938
Rep Power: 39
alexeym has a spectacular aura aboutalexeym has a spectacular aura about
Send a message via Skype™ to alexeym
Hi,

You can assign through non-const reference.

There is read method (https://cpp.openfoam.org/v5/classFoa...a32b6551dc7847), if you would like to read value from dictionary. I.e.

Code:
myVar.read(myProperties)
Though name of myVar should be the same as in dictionary (in your examples, myVar has name myVar, in dictionary it is called myVariable).
alexeym is offline   Reply With Quote

Reply

Tags
createfields.h, dimensioned< type >, dimensionedscalar, dimensionedtype class


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
Pointer Initialization SprayStef OpenFOAM Programming & Development 0 December 17, 2014 14:58
Variable Declaration And Initialization mart.hein OpenFOAM Programming & Development 2 March 8, 2011 11:42


All times are GMT -4. The time now is 17:39.