CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   Initializing constants (http://www.cfd-online.com/Forums/openfoam/75299-initializing-constants.html)

gruber April 21, 2010 09:59

Initializing constants
 
Hello,

I'm looking for a solution of my problem.

I have defined constant dimensioned scalars and vectors in the transportProperties dictionary, eg:

HTML Code:

Sct        Sct      [0 2 -1 0 0 0 0] 2.0e-09;
g          g        [ 0  1 -2  0  0  0  0] (0 0 9.81);

The class looks like this:

HTML Code:

# include "IOdictionary.H"
class A
{
  const IOdictionary transportProperties_;
  const dimensionedScalar Sct_;
  const dimensionedScalar g_;
public:
  A;
}

The constructor looks like this:

HTML Code:

A::A
 :
 transportProperties_( IOobject(
    "transportProperties",
    U.time().constant(),
    U.db(),
    IOobject::MUST_READ,
    IOobject::NO_WRITE)),

  Sct_ ( dimensionedScalar( transportProperties_.lookup("Sct") ) ),
  g_    ( dimensionedVector( transportProperties_.lookup("g") ) )
{}

The compiler doesn't report any bugs, but when I try to run a testcase the following error is displayed during the constructor call

HTML Code:

#0  Foam::error::printStack(Foam::Ostream&) in  "/home/of/OpenFOAM/OpenFOAM-1.5/lib/linux64GccDPOpt/libOpenFOAM.so"
#1  Foam::sigSegv::sigSegvHandler(int) in  "/home/of/OpenFOAM/OpenFOAM-1.5/lib/linux64GccDPOpt/libOpenFOAM.so"
#2  ?? in "/lib64/libc.so.6"
#3  std::string::assign(std::string const&) in  "/home/of/OpenFOAM/ThirdParty/gcc-4.3.1/platforms/linux64/lib64/libstdc++.so.6"
#4  Foam::IOerror::operator()(char const*, char const*, int,  Foam::string const&, int, int) in  "/home/of/OpenFOAM/OpenFOAM-1.5/lib/linux64GccDPOpt/libOpenFOAM.so"
#5  Foam::IOerror::operator()(char const*, char const*, int,  Foam::dictionary const&) in  "/home/of/OpenFOAM/OpenFOAM-1.5/lib/linux64GccDPOpt/libOpenFOAM.so"
#6  Foam::dictionary::lookupEntry(Foam::word const&, bool) const in  "/home/of/OpenFOAM/OpenFOAM-1.5/lib/linux64GccDPOpt/libOpenFOAM.so"
#7  Foam::dictionary::lookup(Foam::word const&, bool) const in  "/home/of/OpenFOAM/OpenFOAM-1.5/lib/linux64GccDPOpt/libOpenFOAM.so"
#8  Foam::IncompressibleCloud::IncompressibleCloud(Foam::volPointInterpolation  const&, Foam::GeometricField<Foam::Vector<double>,  Foam::fvPatchField, Foam::volMesh> const&,  Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField,  Foam::volMesh> const&,  Foam::GeometricField<Foam::Vector<double>, Foam::fvPatchField,  Foam::volMesh> const&, Foam::GeometricField<double,  Foam::fvPatchField, Foam::volMesh> const&,  Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>  const&, Foam::GeometricField<double, Foam::fvPatchField,  Foam::volMesh> const&) in  "/home/of/OpenFOAM/of-1.5/applications/bin/linux64GccDPOpt/icoRLF"
#9  main in  "/home/of/OpenFOAM/of-1.5/applications/bin/linux64GccDPOpt/icoRLF"
#10  __libc_start_main in "/lib64/libc.so.6"
#11  Foam::regIOobject::readIfModified() in  "/home/of/OpenFOAM/of-1.5/applications/bin/linux64GccDPOpt/icoRLF"
Segmentation fault

Can someone please tell me the error in my program?

Thank you very much.

CedricVH April 21, 2010 10:35

A segmentation error usually has to something to do with an array being out of bounds.

In your class, you have defined const dimensionedScalar g_; instead of const dimensionedVector g_; which is different from your constructor.

gruber April 21, 2010 10:52

Thank you for your answer.

You are right. I have posted it wrongly. Certainly it should mean:

HTML Code:

# include "IOdictionary.H"
class A
{
  const IOdictionary transportProperties_;
  const dimensionedScalar Sct_;
  const dimensionedVector g_;
public:
  A();
}

I modified a working solver like I wrote above to initialize a transportProperty parameter and it failed. So there should be something wrong with my initializing algorithm.

niklas April 21, 2010 12:34

How can that compile?

How does class A know about U when it has no arguments in the constructor?

and you dont need to do the typecast
Sct_ (transportProperties_.lookup("Sct"))

is sufficient for dimensionedScalars.

gruber April 22, 2010 05:40

I found a workaround for my problem:

I read in the parameters from the dictionary transportProperties as global parameters and assign them to the class by the constructor call:

HTML Code:

transportProperties_( IOobject(
    "transportProperties",
    U.time().constant(),
    U.db(),
    IOobject::MUST_READ,
    IOobject::NO_WRITE));

dimensionedScalar Sct
(
 transportProperties_.lookup("Sct")
);
 
dimensionedVector g   
(
 transportProperties_.lookup("g")
)

A(Sct, g);

for the class

HTML Code:

# include "IOdictionary.H"
class A
{
  const dimensionedScalar& Sct_;
  const dimensionedVector& g_;
public:
  A(Sct, g);
}

with the constructor

HTML Code:

A::A(Sct, g) : Sct_( Sct ), g_( g ) {}


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