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

Why is access to turbulence fields provided as const?

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By Santiago

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 18, 2019, 05:27
Default Why is access to turbulence fields provided as const?
  #1
Member
 
Rishikesh
Join Date: Apr 2016
Posts: 63
Rep Power: 9
mrishi is on a distinguished road
Short version:
In OpenFOAM-6, I am trying to work on some mesh-to-mesh interpolation of fields (using meshToMesh.H -> imDirect). The turbulence variable return const tmp access to members, which lead to problem in interpolation. How should I go about it?


Details:

For the kEpsilon model, the turbulence variables k and epsilon, members of turbulenceModel (src/TurbulenceModels/turbulenceModels/RAS), are protected:

Code:
            volScalarField k_;
            volScalarField epsilon_;
These can be accessed through the functions:
Code:
//- Return the turbulence kinetic energy
         virtual tmp<volScalarField> k() const
        {
            return k_;
        }

        //- Return the turbulence kinetic energy dissipation rate
        virtual tmp<volScalarField> epsilon() const
        {
            return epsilon_;
        }
So, I try to access them for interpolation as so:
Code:
//source

    tmp<volScalarField> tksrc = turbulenceFluid[i].k();
    volScalarField& ksrc = tksrc.ref();
//tgt

    tmp<volScalarField> tktgt = turbulenceFluid[1-i].k();
    volScalarField& ktgt = tktgt.ref();
and similarly for epsilon.

Because of the use of tmp and const nature of access, appears to cause troubles when I try to interpolate the fields from one mesh to the other.
Code:
Attempt to acquire non-const reference to const object from a tmp<N4Foam14GeometricFieldIdNS_12fvPatchFieldENS_7volMeshEEE>tionThermoMoleFractions
sets
surfaces
)
My question is should I remove the const from the function, virtual tmp<volScalarField> const k() ? Or is their some reasoning behind putting it there that needs to be considered? Is there a correct way to implement access to these so that finally a volScalarField to volScalarField interpolation can be carried out by manipulating these datatypes correctly, without having to add additional non-const access function to the kEpsilon model? Would much appreciate feedback on this!!


PS: The idea of adding non-const access function comes, because I also did interpolation of volume fraction fields (src/transportModels/twoPhaseMixture model), but there the reference to alpha fields is returned via two methods, which allows manipulation via interpolation.


Code:
        //- Return the phase-fraction of phase 1
        const volScalarField& alpha1() const
        {
            return alpha1_;
        }

        //- Return the phase-fraction of phase 1
        volScalarField& alpha1()
        {
            return alpha1_;
        }
PS: the mapping operation is elementary, going as:
Code:
    mapF2C.mapSrcToTgt
    (
        epsilonsrc,
        plusEqOp<scalar> (),
        epsilontgt
    );
It seems to work correctly for other fields.
mrishi is offline   Reply With Quote

Old   December 18, 2019, 07:49
Default
  #2
Senior Member
 
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15
Santiago is on a distinguished road
Quote:
Originally Posted by mrishi View Post
Short version:
In OpenFOAM-6, I am trying to work on some mesh-to-mesh interpolation of fields (using meshToMesh.H -> imDirect). The turbulence variable return const tmp access to members, which lead to problem in interpolation. How should I go about it?


Details:

For the kEpsilon model, the turbulence variables k and epsilon, members of turbulenceModel (src/TurbulenceModels/turbulenceModels/RAS), are protected:

Code:
            volScalarField k_;
            volScalarField epsilon_;
These can be accessed through the functions:
Code:
//- Return the turbulence kinetic energy
         virtual tmp<volScalarField> k() const
        {
            return k_;
        }

        //- Return the turbulence kinetic energy dissipation rate
        virtual tmp<volScalarField> epsilon() const
        {
            return epsilon_;
        }
So, I try to access them for interpolation as so:
Code:
//source

    tmp<volScalarField> tksrc = turbulenceFluid[i].k();
    volScalarField& ksrc = tksrc.ref();
//tgt

    tmp<volScalarField> tktgt = turbulenceFluid[1-i].k();
    volScalarField& ktgt = tktgt.ref();
and similarly for epsilon.

Because of the use of tmp and const nature of access, appears to cause troubles when I try to interpolate the fields from one mesh to the other.
Code:
Attempt to acquire non-const reference to const object from a tmp<N4Foam14GeometricFieldIdNS_12fvPatchFieldENS_7volMeshEEE>tionThermoMoleFractions
sets
surfaces
)
My question is should I remove the const from the function, virtual tmp<volScalarField> const k() ? Or is their some reasoning behind putting it there that needs to be considered? Is there a correct way to implement access to these so that finally a volScalarField to volScalarField interpolation can be carried out by manipulating these datatypes correctly, without having to add additional non-const access function to the kEpsilon model? Would much appreciate feedback on this!!


PS: The idea of adding non-const access function comes, because I also did interpolation of volume fraction fields (src/transportModels/twoPhaseMixture model), but there the reference to alpha fields is returned via two methods, which allows manipulation via interpolation.


Code:
        //- Return the phase-fraction of phase 1
        const volScalarField& alpha1() const
        {
            return alpha1_;
        }

        //- Return the phase-fraction of phase 1
        volScalarField& alpha1()
        {
            return alpha1_;
        }
PS: the mapping operation is elementary, going as:
Code:
    mapF2C.mapSrcToTgt
    (
        epsilonsrc,
        plusEqOp<scalar> (),
        epsilontgt
    );
It seems to work correctly for other fields.
use const_cast<> to convert to non-const reference...
mrishi likes this.
Santiago is offline   Reply With Quote

Old   December 24, 2019, 06:47
Default
  #3
Member
 
Rishikesh
Join Date: Apr 2016
Posts: 63
Rep Power: 9
mrishi is on a distinguished road
Thanks!!! worked like a charm
mrishi is offline   Reply With Quote

Old   January 23, 2020, 12:51
Default
  #4
aot
New Member
 
Andreas Otto
Join Date: Sep 2009
Posts: 12
Rep Power: 16
aot is on a distinguished road
How did you solve the problem. I'm facing the same issue.
Could you please post the code snippet.
aot 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
Foam::error::printStack(Foam::Ostream&) with simpleFoam -parallel U.Golling OpenFOAM Running, Solving & CFD 52 September 23, 2023 03:35
Help with if statement CHARLES OpenFOAM Programming & Development 17 August 22, 2021 03:14
[snappyHexMesh] snappyHexMesh error "Cannot determine normal vector from patches." lethu OpenFOAM Meshing & Mesh Conversion 1 June 3, 2020 07:49
[snappyHexMesh] Error in SnappyHexMesh gooya_kabir OpenFOAM Meshing & Mesh Conversion 2 October 23, 2013 04:41
RunTim Error for simpleFoam Djub OpenFOAM Running, Solving & CFD 2 April 12, 2013 11:51


All times are GMT -4. The time now is 14:24.