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

how to use tmp in PtrList

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   November 30, 2021, 03:03
Default how to use tmp in PtrList
  #1
New Member
 
Lei Zhou
Join Date: Nov 2021
Posts: 6
Rep Power: 4
zhoulei_3c is on a distinguished road
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;
}
Following are the error messages:
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--();
Attached Images
File Type: jpg errorMessages.jpg (51.0 KB, 2 views)
zhoulei_3c is offline   Reply With Quote

Old   December 2, 2021, 15:06
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,684
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
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).
zhoulei_3c likes this.
olesen is offline   Reply With Quote

Reply

Tags
ptrlist, tmp


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
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


All times are GMT -4. The time now is 20:44.