|
[Sponsors] |
August 3, 2020, 10:15 |
Foam::DynamicList<Type>.append( new Type );
|
#1 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 348
Rep Power: 10 |
Howdy Foamers:
I have a C++ question. I think I know the answer empirically, but I don't know if my conception is correct. I have created a DynamicList of some data type (let's call it "Type"). Its starts off with zero elements and I would like to append new terms in it. Currently I would have to declare a new instance in a dummy variable of this "Type" and append this into the list: Code:
Foam::DynamicList<Type> myList(0); Type dummy; myList.append ( dummy ); Code:
Foam::DynamicList<Type> myList(0); myList.append ( new Type ); Thanks in advance, Gerry. Last edited by Gerry Kan; August 4, 2020 at 03:20. |
|
August 3, 2020, 15:54 |
|
#2 |
Senior Member
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25 |
Unfortunately, what you're looking for isn't possible with the way DynamicList is currently written. You're effectively looking for functionality identical to "emplace_back" with std::vector. My suggestion would be to ask the "powers that be" to put this functionality in. It isn't hard - just requires a new member function with variadic template arguments which uses std::forward to pass constructor arguments to the element you're trying to construct in-place.
The alternative is to use a DynamicList with an std::auto_ptr<Type> template argument (or, equivalently, std::shared_ptr / std::unique_ptr since auto_ptr was deprecated). But unless the cost of you copy constructor is large, you should question whether this entire effort is worth it, and maybe you should simply live with the temporary. |
|
August 4, 2020, 03:23 |
|
#3 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 348
Rep Power: 10 |
Dear Sandeep:
Thanks for your clarification. My dummy variable is local, and the function in question is only called at initialization. So there is not a lot of overhead (for now). I can live with an extra line of code. The autoPtr thing sounds like something I should explore for some other things I am considering, and I should in the mean time dig a little deeper into it. Sincerely yours, Gerry. |
|
August 4, 2020, 05:04 |
|
#4 |
Senior Member
Gerry Kan
Join Date: May 2016
Posts: 348
Rep Power: 10 |
At the risk of hijacking my own thread ...
Howdy Foamers: Does anyone know where volScalarField is defined? I see GeometricField, which is similar, but no volScalarField. I am asking because I am trying to create a DynamicList<volScalarField>. Code:
DynamicList<volScalarField> myList(0); volScalarField dummy ( IOobject ( "dummy", runTime().timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), mesh, dimensionedScalar ( "dummy", dimensionSet(0,0,0,0,0,0,0), 0.0 ) ); myList.append ( dummy ); Code:
lnInclude/List.C:294:21: error: no matching function for call to ‘Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>::GeometricField()’ 294 | T* nv = new T[label(newSize)]; | ^~~~~~~~~~~~~~~~~~~~~ ... /worknb/users/ech/DEV/OpenFOAM/OpenFOAM-7/src/OpenFOAM/lnInclude/GeometricField.C:697:1: note: candidate: ‘Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField(const Foam::IOobject&, const Foam::tmp<Foam::GeometricField<Type, PatchField, GeoMesh> >&, const wordList&, const wordList&) [with Type = double; PatchField = Foam::fvPatchField; GeoMesh = Foam::volMesh; Foam::wordList = Foam::List<Foam::word>]’ 697 | Foam::GeometricField<Type, PatchField, GeoMesh>::GeometricField | ^~~~ /worknb/users/ech/DEV/OpenFOAM/OpenFOAM-7/src/OpenFOAM/lnInclude/GeometricField.C:697:1: note: candidate expects 4 arguments, 0 provided Code:
// initialization DynamicList<Field<scalar>> myList(0); Field<scalar> dummy(100); // size of 100 is placeholder for this example myList.append(dummy); // time loop volScalarField dummyVolScalarField ( IOobject ( "dummy", runTime().timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE ), mesh, dimensionedScalar ( "dummy", dimensionSet(0,0,0,0,0,0,0), 0.0 ) ); forAll ( dummyVolScalarField.internalField(), iCell ) dummyVolScalarField.ref()[iCell] = dummy[iCell]; Sandeep, this is where I would like to ask for your opinion. Would there be any benefit of making dummyVolScalarField into an autoPtr. Ultimately it would more efficient if I could just reference dummyVolScalarField.ref() to dummy directly. Would this be possible? Thanks in advance, Gerry. |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
2DOF Planning hull resistance study with overset | CFD_fan | OpenFOAM Running, Solving & CFD | 7 | July 13, 2021 05:03 |
[OpenFOAM.org] compile error in dynamicMesh and thermophysicalModels libraries | NickG | OpenFOAM Installation | 3 | December 30, 2019 00:21 |
rhoPimpleFoam hardship | petrus | OpenFOAM Running, Solving & CFD | 0 | October 7, 2016 02:41 |
interFoam/kOmegaSST tank filling with printStackError/Mules | simpomann | OpenFOAM Running, Solving & CFD | 3 | February 17, 2014 17:06 |
[swak4Foam] Air Conditioned room groovyBC | Sebaj | OpenFOAM Community Contributions | 7 | October 31, 2012 14:16 |