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

access PtrList from chemistryModel

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By einstein_zee

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   January 13, 2020, 12:34
Default access PtrList from chemistryModel
  #1
Member
 
ssa
Join Date: Sep 2018
Posts: 93
Rep Power: 7
ssa_cfd is on a distinguished road
Hi all,



I created a PtrList to create multiple volScalarField in createFields.H


The code looks like this: (in createFields.H)
Code:
label N = 325;
    
    PtrList<volScalarField> kf(N);

for(int i = 0; i < N; i++)
{
word kfi ("kf" + name(i));
    kf.set
    (
        i,
        new volScalarField 
    (
            IOobject
            (
                kfi,
                runTime.timeName(),
                mesh,
                IOobject::NO_READ,
                IOobject::AUTO_WRITE
            ),
            mesh,
    dimensionedScalar(kfi, dimless, 0.0)
        )
    );
}
Now I want to access this volScalarField from chemistryModel.c.
currently my code looks like below in chemistryModel.c
Code:
word kfi ("kf" + name(i));
volScalarField tkf = mesh1.lookupObject<volScalarField>(kfi);

tkf[celli] = R.kf(pkf, Tkf, c2);
Now it stores the caluclated values only in "tkf[celli]" but doesn't change the value of "kfi" globally.



What should I do to modify the "kfi" values globally.
ssa_cfd is offline   Reply With Quote

Old   January 14, 2020, 06:12
Default
  #2
Senior Member
 
Yogesh Bapat
Join Date: Oct 2010
Posts: 102
Rep Power: 15
ybapat is on a distinguished road
Hello,


You have created local variable volScalarField. You need to use reference to original volScalarField then only it will be updated globally.



Regards,
-Yogesh
ybapat is offline   Reply With Quote

Old   January 14, 2020, 07:36
Default
  #3
Member
 
ssa
Join Date: Sep 2018
Posts: 93
Rep Power: 7
ssa_cfd is on a distinguished road
I tried like this

Code:
const volScalarField& tkf = mesh1.lookupObject<volScalarField>(kfi);
tkf[celli] = R.kf(pkf, Tkf, c2);
and I got the below error

Code:
lnInclude/chemistryModel.C: In instantiation of ‘Foam::scalar Foam::chemistryModel<CompType, ThermoType>::solve(const DeltaTType&) [with DeltaTType = Foam::Field<double>; CompType = Foam::psiChemistryModel; ThermoType = Foam::constTransport<Foam::species::thermo<Foam::hConstThermo<Foam::perfectGas<Foam::specie> >, Foam::sensibleEnthalpy> >; Foam::scalar = double]’:
lnInclude/chemistryModel.C:1079:43:   required from ‘Foam::scalar Foam::chemistryModel<CompType, ThermoType>::solve(const scalarField&) [with CompType = Foam::psiChemistryModel; ThermoType = Foam::constTransport<Foam::species::thermo<Foam::hConstThermo<Foam::perfectGas<Foam::specie> >, Foam::sensibleEnthalpy> >; Foam::scalar = double; Foam::scalarField = Foam::Field<double>]’
chemistrySolver/chemistrySolver/makeChemistrySolvers.C:91:1:   required from here
lnInclude/chemistryModel.C:929:13: error: assignment of read-only location ‘(& tkf)->Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>::<anonymous>.Foam::DimensionedField<double, Foam::volMesh>::<anonymous>.Foam::Field<double>::<anonymous>.Foam::List<double>::<anonymous>.Foam::UList<T>::operator[]<double>(celli)’

Then I tried like this
Code:
volScalarField& tkf = mesh1.lookupObject<volScalarField>(kfi);
and then I got the below error
Code:
lnInclude/chemistryModel.C: In instantiation of ‘Foam::scalar Foam::chemistryModel<CompType, ThermoType>::solve(const DeltaTType&) [with DeltaTType = Foam::Field<double>; CompType = Foam::psiChemistryModel; ThermoType = Foam::constTransport<Foam::species::thermo<Foam::hConstThermo<Foam::perfectGas<Foam::specie> >, Foam::sensibleEnthalpy> >; Foam::scalar = double]’:
lnInclude/chemistryModel.C:1079:43:   required from ‘Foam::scalar Foam::chemistryModel<CompType, ThermoType>::solve(const scalarField&) [with CompType = Foam::psiChemistryModel; ThermoType = Foam::constTransport<Foam::species::thermo<Foam::hConstThermo<Foam::perfectGas<Foam::specie> >, Foam::sensibleEnthalpy> >; Foam::scalar = double; Foam::scalarField = Foam::Field<double>]’
chemistrySolver/chemistrySolver/makeChemistrySolvers.C:91:1:   required from here
lnInclude/chemistryModel.C:917:62: error: invalid initialization of reference of type ‘Foam::volScalarField& {aka Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>&}’ from expression of type ‘const Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>’
ssa_cfd is offline   Reply With Quote

Old   January 14, 2020, 08:11
Default
  #4
Member
 
Hosein
Join Date: Nov 2011
Location: Germany
Posts: 93
Rep Power: 14
einstein_zee is on a distinguished road
lookupObjectRef
einstein_zee is offline   Reply With Quote

Old   January 14, 2020, 08:26
Default
  #5
Member
 
ssa
Join Date: Sep 2018
Posts: 93
Rep Power: 7
ssa_cfd is on a distinguished road
I think lookupObjectRef is available only in latest versions. I use version 2.4 because the solver was previously written in this version 2.4
ssa_cfd is offline   Reply With Quote

Old   January 14, 2020, 09:47
Default
  #6
Member
 
Hosein
Join Date: Nov 2011
Location: Germany
Posts: 93
Rep Power: 14
einstein_zee is on a distinguished road
oh, that is too old. There might be a better solution but I think you can use const_cast. sth like this const_cast<volScalarField&>(tkf)[celli] ....
ssa_cfd likes this.
einstein_zee is offline   Reply With Quote

Old   January 15, 2020, 07:17
Default
  #7
Member
 
ssa
Join Date: Sep 2018
Posts: 93
Rep Power: 7
ssa_cfd is on a distinguished road
Thanks Hosein. const_cast solved the problem.!!
ssa_cfd 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
[DesignModeler] DesignModeler Scripting: How to get Full Command Access ANT ANSYS Meshing & Geometry 53 February 16, 2020 15:13
Why is access to turbulence fields provided as const? mrishi OpenFOAM Programming & Development 3 January 23, 2020 12:51
Is there a way to access the gradient limiter in Fluent ? CFDYourself FLUENT 1 February 16, 2016 05:49
Temperature linearly rising after restarting a simulation Gennaro OpenFOAM Programming & Development 2 September 2, 2014 07:58
Newbie Question on PtrList usage chyczewski OpenFOAM Programming & Development 6 October 1, 2013 16:39


All times are GMT -4. The time now is 18:04.