CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (http://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Read in a new constant to solver (http://www.cfd-online.com/Forums/openfoam-solving/60439-read-new-constant-solver.html)

hsieh November 4, 2005 22:09

Hi, I would like to read a
 
Hi,

I would like to read a new constant into the solver from a data file. Say, I want to scale the body force of the momentum equation with this new constant. In stead of putting this constant in the main solver, I would like to read it in from a data file so that I do not have to re-compile the solver every time I change the constant. What is the easiest way to do this? Many thanks!

pei-Ying

hjasak November 5, 2005 05:30

Take a look at how icoFoam rea
 
Take a look at how icoFoam reads nu and do it in the same way. You've got a choice of dictionaries to do it from, so make sure you put it into the right one.

Enjoy,

Hrv

hsieh November 5, 2005 10:48

Hi, Hrv, I already tried it
 
Hi, Hrv,

I already tried it the way you suggested, but, failed. I declared the new constant, say amplitude, as double. I think nu is a field variable (tie to mesh?). I will give it a try again.

Pei-Ying

hjasak November 5, 2005 10:56

This is not hard: 1) Open y
 
This is not hard:

1) Open your favourite dictionary, or find one already open:

IOdictionary environmentalProperties
(
IOobject
(
"environmentalProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);


2) read the thing:

scalar amp(readScalar(environmentalProperties.lookup("amp litude")));

3) there's no 3! :-)

Enjoy,

Hrv

P.S. Any news on the topo tutorials we talked about?

hsieh November 5, 2005 22:13

Hi, Hrv, Thank you very muc
 
Hi, Hrv,

Thank you very much for the kind answer.

I must missed something and still cannot get it to work. The following is what I did:

in file readEnvironmentalProperties.H I have:
-------------------
Info << "\nReading environmentalProperties" << endl;

IOdictionary environmentalProperties
(
IOobject
(
"environmentalProperties",
runTime.constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
)
);

scalar amp(readScalar(environmentalProperties.lookup("amp litude")));
----------------------

in the main code (turbFoam.C in this case), I have
------------------------
..
float amplitude;

# include "readEnvironmentalProperties.H"
----------------

in environmentalProperties file, I have:
--------------

amp amplitude 1.0;

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

Everything compiled without a problem. But, I got an error when I run the case:
-----------
Reading environmentalProperties


--> FOAM FATAL IO ERROR : keyword amplitude is undefined in dictionary "/home/phsieh/OpenFOAM/phsieh-1.2/run/turbFoamVibration/CP-S-vibration/constant/ environmentalProperties"

file: /home/phsieh/OpenFOAM/phsieh-1.2/run/turbFoamVibration/CP-S-vibration/constant/e nvironmentalProperties from line 25 to line 25.

From function dictionary::lookupEntry(const word& keyword) const
in file db/dictionary/dictionary.C at line 152.

FOAM exiting
--------------------

Unfortunately, the topo tutorials are going slowly. I updated the attachDetach stuff to OpenFOAM-1.1, but, I cannot get it to run under OpenFOAM-1.2 just yet. Also, after attaching/detaching, the flow field seemed odd. I need to do some more checking. But, 3 tutorials will be done just after Thanksgiving. I apologize for draging this so long. I wondered into some other areas of OpenFOAM (and enjoyed it too much).

Pei-Ying

hjasak November 6, 2005 06:03

The environmentalProperties fi
 
The environmentalProperties file is wrong: it should say just:

amplitude 1.0;

Pls try again,

Hrv

wikstrom November 6, 2005 06:05

Try changing in the dictionary
 
Try changing in the dictionary file:

from
amp amplitude 1.0;

to
amplitude 1.0;


/Niklas

hsieh November 6, 2005 19:33

Thanks a lot Hrv and Niklas!
 
Thanks a lot Hrv and Niklas!

I had to make 3 changes to get it to work:

1. changed the environmentalProperties file:
amplitude 1.0;

2. in the main code (turbFoam.C):
only added #include "readEnvironmentalProperties.H"
(should not add: float amplitude;)

3. in the readEnvironmentalProperties.H:
scalar amplitude(readScalar(environmentalProperties.looku p("amplitude")));
---------------------

If in readEnvironmentalProperties.H, I used
scalar amp(readScalar(environmentalProperties.lookup("amp litude")));
Then, it failed to read amplitude correctly.

Pei-ying

hjasak November 7, 2005 05:29

OK, here's a little C++/FOAM l
 
OK, here's a little C++/FOAM lesson for you:

- when you look up a keyword from a dictionary, you use:

ITstream& lookup(const word&) const;

This returns and Input Token Stream, ITstream, which is derived from Istream.

- the statement like

dimensionedScalar a1(readScalar(environmentalProperties.lookup("ampl itude")));

calls the constructor for a dimensionedScalar from an Istream and all is well.

- however, doing the same with a scalar (= typedef double) does not work! This is because double is NOT a class, but a primitive type and therefore does not have a constructor from Istream. for this reason, you need a helper function readScalar, which takes an Istream and returns a scalar, right? Thus:

scalar readScalar(Istream& is)
{
scalar rs;
is >> rs;

return rs;
}

and

scalar amp(readScalar(environmentalProperties.lookup("amp litude")));

Enjoy,

Hrv


All times are GMT -4. The time now is 12:26.