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

Adding new lookup coefficient to fvOptions source error

Register Blogs Community New Posts Updated Threads Search

Like Tree4Likes
  • 4 Post By cbcoutinho

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   October 20, 2017, 14:31
Default Adding new lookup coefficient to fvOptions source error
  #1
New Member
 
Chris Coutinho
Join Date: Jan 2015
Location: Netherlands
Posts: 28
Rep Power: 11
cbcoutinho is on a distinguished road
I'm creating a new fvOptions source option based on the `meanVelocityForce` derived source in OpenFOAM 4.x. When I added a new scalar to the .H and .C files, I get a compilation error related to an incorrect conversion occurring in the constructor. More specifically, the error message I'm seeing is:

Code:
meanMassSource.C: In constructor ‘Foam::fv::meanMassSource::meanMassSource(const Foam::word&, const Foam::word&, const Foam::dictionary&, const Foam::fvMesh&)’:
meanMassSource.C:95:16: error: cannot convert ‘Foam::ITstream’ to ‘Foam::scalar {aka double}’ in initialization
     rAPtr_(NULL)
                ^
/home/redclient04/OpenFOAM/OpenFOAM-4.x/wmake/rules/General/transform:8: recipe for target 'Make/linux64GccDPInt32Opt/meanMassSource.o' failed
make: *** [Make/linux64GccDPInt32Opt/meanMassSource.o] Error 1
If I comment out the new scalar addition to the dictionary that is created in the constructor, the code compiles without an issue. Below are snippets of my fvOption files named meanMassSource:

meanMassSource.H:

Code:
#ifndef meanMassSource_H
#define meanMassSource_H

#include "autoPtr.H"
#include "topoSetSource.H"
#include "cellSet.H"
#include "fvMesh.H"
#include "volFields.H"
#include "cellSetOption.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
namespace fv
{

/*---------------------------------------------------------------------------*\
               Class meanMassSource Declaration
\*---------------------------------------------------------------------------*/

class meanMassSource
:
    public cellSetOption
{
protected:

    // Protected data
        vector Ubar_;                       //- Average velocity
        scalar gradC0_;                     //- Concentration gradient before correction
        scalar dGradC_;                     //- Change in concentration gradient
// vvv molarFlux_ is the new scalar I want to add vvv
        scalar molarFlux_;                  //- Molar flux at membrane walls in [mol/m2.s]
        vector flowDir_;                    //- Flow direction
        scalar relaxation_;                 //- Relaxation factor
        autoPtr<volScalarField> rAPtr_;     //- Matrix 1/A coefficients field pointer

.
.
.

And the associated meanMassSource.C file:

Code:
#include "meanMassSource.H"
#include "fvMatrices.H"
#include "DimensionedField.H"
#include "IFstream.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

namespace Foam
{
namespace fv
{
    defineTypeNameAndDebug(meanMassSource, 0);

    addToRunTimeSelectionTable
    (
        option,
        meanMassSource,
        dictionary
    );
}
}


// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::fv::meanMassSource::writeProps
(
    const scalar gradC
) const
{
    // Only write on output time
    if (mesh_.time().writeTime())
    {
        IOdictionary propsDict
        (
            IOobject
            (
                name_ + "Properties",
                mesh_.time().timeName(),
                "uniform",
                mesh_,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            )
        );
        propsDict.add("gradient", gradC);
        propsDict.regIOobject::write();
    }
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::fv::meanMassSource::meanMassSource
(
    const word& sourceName,
    const word& modelType,
    const dictionary& dict,
    const fvMesh& mesh
)
:
    cellSetOption(sourceName, modelType, dict, mesh),
    Ubar_(coeffs_.lookup("Ubar")),
    gradC0_(0.0),
    dGradC_(0.0),
    molarFlux_(coeffs_.lookup("molarFlux")),    // Again, here is where the new scalar is being looked up and put into the dictionary
    flowDir_(Ubar_/mag(Ubar_)),
    relaxation_(coeffs_.lookupOrDefault<scalar>("relaxation", 1.0)),
    rAPtr_(NULL)
.
.
.
I snipped the remaining source of both files because they are so far exactly the same as meanVelocityForce - the implementation hasn't been added yet. If I comment out the two offending lines where molarFlux_ is defined in the header file and is placed into the dictionary in the .C file, the fvOption compiles fine.

I've noticed that various derived sources have differing number of entries in their dictionaries, so I didn't think the number of entries is hard-coded somewhere. Since both the header and source files match up (at least to my untrained eyes), I can't tell where the problem is.

Can someone tell me where I'm missing something?

Last edited by cbcoutinho; October 20, 2017 at 14:32. Reason: Put full error message in
cbcoutinho is offline   Reply With Quote

Old   October 20, 2017, 15:02
Default
  #2
New Member
 
Chris Coutinho
Join Date: Jan 2015
Location: Netherlands
Posts: 28
Rep Power: 11
cbcoutinho is on a distinguished road
I spent a little more time digging around, and I found a fantastic answer to my question by Prof. Jasak - see his post here

Essentially the issue is that the lookup function involves a constructor of a class, but scalars are just doubles, which don't have constructors because doubles are primitive types. You need to wrap the lookup function with a readScalar function:

Change the offending line:

Code:
molarFlux_(coeffs_.lookup("molarFlux")),
to:

Code:
molarFlux_(readScalar(coeffs_.lookup("molarFlux"))),
cbcoutinho is offline   Reply With Quote

Old   September 1, 2021, 00:13
Default
  #3
Member
 
Francisco T
Join Date: Nov 2011
Location: Melbourne, Australia
Posts: 64
Blog Entries: 1
Rep Power: 14
frantov is on a distinguished road
Thanks Chris


for my case, this would be the solution assuming we are reading the scalar "height" from liquidDict



OF7
const scalar height(readScalar(liquidDict.lookup("height")));



OF8 (and onwards)

const scalar height(liquidDict.lookup<scalar>("height"));
frantov is offline   Reply With Quote

Old   January 18, 2022, 07:48
Default
  #4
New Member
 
XieDaxia
Join Date: Oct 2021
Posts: 1
Rep Power: 0
XieDaxia is on a distinguished road
Quote:
Originally Posted by cbcoutinho View Post
I'm creating a new fvOptions source option based on the `meanVelocityForce` derived source in OpenFOAM 4.x. When I added a new scalar to the .H and .C files, I get a compilation error related to an incorrect conversion occurring in the constructor. More specifically, the error message I'm seeing is:

Code:
meanMassSource.C: In constructor ‘Foam::fv::meanMassSource::meanMassSource(const Foam::word&, const Foam::word&, const Foam::dictionary&, const Foam::fvMesh&)’:
meanMassSource.C:95:16: error: cannot convert ‘Foam::ITstream’ to ‘Foam::scalar {aka double}’ in initialization
     rAPtr_(NULL)
                ^
/home/redclient04/OpenFOAM/OpenFOAM-4.x/wmake/rules/General/transform:8: recipe for target 'Make/linux64GccDPInt32Opt/meanMassSource.o' failed
make: *** [Make/linux64GccDPInt32Opt/meanMassSource.o] Error 1
If I comment out the new scalar addition to the dictionary that is created in the constructor, the code compiles without an issue. Below are snippets of my fvOption files named meanMassSource:

meanMassSource.H:

Code:
#ifndef meanMassSource_H
#define meanMassSource_H

#include "autoPtr.H"
#include "topoSetSource.H"
#include "cellSet.H"
#include "fvMesh.H"
#include "volFields.H"
#include "cellSetOption.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
namespace fv
{

/*---------------------------------------------------------------------------*\
               Class meanMassSource Declaration
\*---------------------------------------------------------------------------*/

class meanMassSource
:
    public cellSetOption
{
protected:

    // Protected data
        vector Ubar_;                       //- Average velocity
        scalar gradC0_;                     //- Concentration gradient before correction
        scalar dGradC_;                     //- Change in concentration gradient
// vvv molarFlux_ is the new scalar I want to add vvv
        scalar molarFlux_;                  //- Molar flux at membrane walls in [mol/m2.s]
        vector flowDir_;                    //- Flow direction
        scalar relaxation_;                 //- Relaxation factor
        autoPtr<volScalarField> rAPtr_;     //- Matrix 1/A coefficients field pointer

.
.
.

And the associated meanMassSource.C file:

Code:
#include "meanMassSource.H"
#include "fvMatrices.H"
#include "DimensionedField.H"
#include "IFstream.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //

namespace Foam
{
namespace fv
{
    defineTypeNameAndDebug(meanMassSource, 0);

    addToRunTimeSelectionTable
    (
        option,
        meanMassSource,
        dictionary
    );
}
}


// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

void Foam::fv::meanMassSource::writeProps
(
    const scalar gradC
) const
{
    // Only write on output time
    if (mesh_.time().writeTime())
    {
        IOdictionary propsDict
        (
            IOobject
            (
                name_ + "Properties",
                mesh_.time().timeName(),
                "uniform",
                mesh_,
                IOobject::NO_READ,
                IOobject::NO_WRITE
            )
        );
        propsDict.add("gradient", gradC);
        propsDict.regIOobject::write();
    }
}


// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

Foam::fv::meanMassSource::meanMassSource
(
    const word& sourceName,
    const word& modelType,
    const dictionary& dict,
    const fvMesh& mesh
)
:
    cellSetOption(sourceName, modelType, dict, mesh),
    Ubar_(coeffs_.lookup("Ubar")),
    gradC0_(0.0),
    dGradC_(0.0),
    molarFlux_(coeffs_.lookup("molarFlux")),    // Again, here is where the new scalar is being looked up and put into the dictionary
    flowDir_(Ubar_/mag(Ubar_)),
    relaxation_(coeffs_.lookupOrDefault<scalar>("relaxation", 1.0)),
    rAPtr_(NULL)
.
.
.
I snipped the remaining source of both files because they are so far exactly the same as meanVelocityForce - the implementation hasn't been added yet. If I comment out the two offending lines where molarFlux_ is defined in the header file and is placed into the dictionary in the .C file, the fvOption compiles fine.

I've noticed that various derived sources have differing number of entries in their dictionaries, so I didn't think the number of entries is hard-coded somewhere. Since both the header and source files match up (at least to my untrained eyes), I can't tell where the problem is.

Can someone tell me where I'm missing something?


Hi~ I'm trying to simulating a periodic case with heat transfer. I have tried to use a constant heat sink for the Temperature(scalar) field, but the temperature keep increasing, so I also want to use a method similar to the meanvelocityforce for the scalar field.
Could you plase show me the complete code for study? Thanks a lot, whether yes or not.
XieDaxia is offline   Reply With Quote

Reply

Tags
dictionary, fvoptions, itstream


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
[swak4Foam] funkyDoCalc with OF2.3 massflow NiFl OpenFOAM Community Contributions 14 November 25, 2020 03:30
[blockMesh] blockMesh with double grading. spwater OpenFOAM Meshing & Mesh Conversion 92 January 12, 2019 09:00
[OpenFOAM.org] Error creating ParaView-4.1.0 OpenFOAM 2.3.0 tlcoons OpenFOAM Installation 13 April 20, 2016 17:34
[swak4Foam] GroovyBC the dynamic cousin of funkySetFields that lives on the suburb of the mesh gschaider OpenFOAM Community Contributions 300 October 29, 2014 18:00
Compiling problems with hello worldC fw407 OpenFOAM Installation 21 January 6, 2008 17:38


All times are GMT -4. The time now is 06:29.