|
[Sponsors] |
![]() |
![]() |
#1 |
New Member
Lei Zhou
Join Date: Nov 2021
Posts: 6
Rep Power: 4 ![]() |
Hello, Foamers:
When I want to use the "tmp" class in openfoam to save memory, it through a compile error. And it is possibly to use tmp in PtrList just like tmp<volScarlarField>? Code:
const Foam::tmp<Foam::PtrList<Foam::volScalarField>> Foam::macroscopicXS::lambda() const { tmp<PtrList<volScalarField>> tlambda ( new PtrList<volScalarField> ( precGroups_ ) ); PtrList<volScalarField>& lambda(tlambda.ref()); forAll(lambda, energyI) { lambda.set ( energyI, new volScalarField ( "macroscopicXS::lambda", lambdaPtr_[energyI] ) ); } return tlambda; } Code:
/home/zhoulei/OpenFOAM/OpenFOAM-v2006/src/OpenFOAM/lnInclude/tmpI.H: In instantiation of ‘void Foam::tmp<T>::clear() const [with T = Foam::PtrList<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> >]’: /home/zhoulei/OpenFOAM/OpenFOAM-v2006/src/OpenFOAM/lnInclude/tmpI.H:185:10: required from ‘Foam::tmp<T>::~tmp() [with T = Foam::PtrList<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> >]’ macroscopicXS/macroscopicXS.C:169:12: required from here /home/zhoulei/OpenFOAM/OpenFOAM-v2006/src/OpenFOAM/lnInclude/tmpI.H:329:19: error: ‘class Foam::PtrList<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> >’ has no member named ‘unique’ if (ptr_->unique()) ~~~~~~^~~~~~ /home/zhoulei/OpenFOAM/OpenFOAM-v2006/src/OpenFOAM/lnInclude/tmpI.H:335:27: error: ‘class Foam::PtrList<Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> >’ has no member named ‘operator--’; did you mean ‘operator=’? ptr_->operator--(); |
|
![]() |
![]() |
![]() |
![]() |
#2 |
Senior Member
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40 ![]() ![]() |
There are a few reasons why what you are attempting doesn't make much sense. Here are some notes (in no particular order).
For anything to be a tmp it must have intrinsic ref counting. So you could wrap a labelField as a tmp, but not a labelList, since List does not have a refCount. If you want the idea like a tmp, then you could consider refPtr instead. In many respects it behaves like a tmp (can hold a reference or its own storage) but without any ref counting. So it makes it somewhat like an autoPtr and a unique_ptr, but with being able to have a reference. So your example could be changed to use a refPtr for the return type. However, you will still need to create the stored item itself as a raw pointer before stuffing that into the refPtr. Otherwise you will be trying to return a reference to the local variable, which then goes out of scope (= crash). Take a git grep through the OpenFOAM codebase for refPtr uses to get an idea of how they work. A few examples come to mind: the PrecisionAdaptor is a fun one for handling float/double conversion for mixed precision compilations without adding overhead for the regular case. There is also some fun bits in the isosurface (iso-topo I think) where it is used to hold a reference to a field or subfield. Saves juggling pointers a bit. Note however for your example with wrapping a PtrList. This would only be useful for a possibly cached value. If it's a regular PtrList, simply returning it will be fine (copy ellision etc). |
|
![]() |
![]() |
![]() |
Tags |
ptrlist, tmp |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Adding dimensioned scalar/scalar field to chtMultiRegionSimpleFoam/chtMultiRegionFoam | Lann | OpenFOAM Programming & Development | 5 | March 10, 2020 05:40 |
Error message | Bruno_Jorge | Main CFD Forum | 1 | February 5, 2019 11:12 |
fvc::interpolate(rAU) at boundary faces | Jesper_Roland | OpenFOAM Programming & Development | 5 | January 30, 2019 08:55 |
question on H operator | gaza | OpenFOAM Programming & Development | 5 | December 10, 2018 04:40 |
Temperature linearly rising after restarting a simulation | Gennaro | OpenFOAM Programming & Development | 2 | September 2, 2014 07:58 |