|
[Sponsors] |
September 20, 2013, 14:56 |
Newbie Question on PtrList usage
|
#1 |
New Member
Tom Chyczewski
Join Date: Mar 2009
Location: Bethpage, New York, USA
Posts: 15
Rep Power: 17 |
I recently received the following information from another post:
If N, you need PtrList<volScalarField> ks(N); PtrList<volScalarField> thetas(N); You will access them as ks[i] and thetas[i]. If NxM, make a PtrList<PtrList<volScalarField> > bigOne(N); forAll (bigOne, i) { bigOne.set(i, new PtrList<volScalarField>(M)); } and then fill the lot. You will access them as bigOne[i][j]; I am able to create the arrays via PtrList as described above but I am not able to access them. If I try ks[1]=1.0; I get a fatal error: hanging pointer, cannot dereference at runtime. If I try ks.set(1,1.0); I get a number of compilation errors. Any suggestions? |
|
September 24, 2013, 16:03 |
|
#2 |
New Member
Matt Laurita
Join Date: Jan 2012
Location: New York
Posts: 8
Rep Power: 14 |
What are you trying to do with the line ks[1]=1.0? I'm not an expert on PtrLists, but I believe that ks[1] is a pointer to a volScalarField, so you can't just assign a value to it.
For example, if you were trying to set a value in a particular grid cell for the field pointed to by ks[1] you would need to do something like: ks[1]->internalField()[cellI] = 1.0 (because ks[1] is a pointer you need the '->' instead of the '.' to call a member function) or maybe... *ks[1][cellI] = 1.0 (I'm not 100% sure about this one, but the thinking is that you would have to dereference the pointer to access the volScalarField, and then assign a value to the desired cell) Hope this helps a little. MJL |
|
September 25, 2013, 09:13 |
|
#3 |
New Member
Tom Chyczewski
Join Date: Mar 2009
Location: Bethpage, New York, USA
Posts: 15
Rep Power: 17 |
Matt,
Thanks for posting a reply. It helped but didn't get me there yet. I tried your suggestions and they unfortunately led to compiler errors: invalid type argument of unary '*' (have 'double') *ks[1][1] = 1.0; ^ base operand of '->' has non-pointer type 'Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>' ks[1]->internalField()[1] = 1.0; ^ The second error suggests that PrtList doesn't return a pointer. If I PtrList<volScalarField> ks(10); then ks.size() returns 0 and ks.empty() returns true. I was expecting the size to be equal to 10 times the number of cells. If I first ks.setSize(mesh.C().size()); then ks.size() returns #cells and ks.empty() returns false. But I was expecting ks.size() to return #cells*10. I am at a bit of a loss. Any other suggestions? |
|
September 25, 2013, 10:19 |
|
#4 |
New Member
Matt Laurita
Join Date: Jan 2012
Location: New York
Posts: 8
Rep Power: 14 |
My mistake... a look at PtrList.H reveals that the operator [] returns a reference to the list element, not a pointer, so I think you could do something like:
ks[1].internalField[cellI] = 1 or just ks[1][cellI] = 1 to set/access the value of volScalarField #1 in cell #cellI I'm not sure why PtrList<volScalarField> ks(10) is returning a size of 0. It should return 10, because ks is a list of pointers to 10 volScalarFields. You shouldn't expect it to be 10*nCells. Each one of the elements of the PtrList is a volScalarField of size nCells (e.g. ks[0].size() should return nCells, because ks[0] is a reference to the first volScalarField pointed to by ks; likewise ks[1].size(), ks[2].size(), etc, would all give you nCells). Don't forget that you would need to assign each pointer in the list to actually point to something (i.e. an existing volScalarField) before you can set/access any field values. Makes sense? MJL |
|
September 28, 2013, 12:27 |
|
#5 | |
New Member
Join Date: Apr 2013
Posts: 24
Rep Power: 13 |
Quote:
I guess(!) the problem is that you have to actually initialize the volScalarFields in your PtrList by iterating through it. E.g.: Code:
for (int n=1; n==10, n++) { ks.set(n, volScalarField(....)); } Does it make sense? //Edit: To give you an example of a code I recently produced and which is working for me: Code:
forAll(moleculeTypeIds_, tI) { adsorbedParcels_.set ( tI, volScalarField ( IOobject ( "adsorbedParcels_" + moleculeTypeIds_[tI], this->owner().mesh().time().timeName(), this->owner().mesh(), IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE ), this->owner().mesh(), dimensionedScalar("zero", dimensionSet(0, 0, 0, 0, 0), 0.0) ) ); } |
||
October 1, 2013, 11:46 |
|
#6 |
New Member
Tom Chyczewski
Join Date: Mar 2009
Location: Bethpage, New York, USA
Posts: 15
Rep Power: 17 |
Thanks Matt and GPesch! What you are saying makes sense and the code is behaving as you suggest it would. For example, here is my code to generate a 2D array of volScalarField's :
Code:
PtrList<PtrList<volScalarField> > wasd(imax); forAll (wasd,n) { wasd.set(n, new PtrList<volScalarField>(jmax)); } forAll (wasd, n) { forAll (wasd[n], m) { wasd[n].set(m, new volScalarField ( IOobject ( "amn", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE ), mesh, dimensionedScalar("amn", dimensionSet(0,4,1,0,0,0,0),0.0) )); } } |
|
October 1, 2013, 17:39 |
|
#7 |
New Member
Join Date: Apr 2013
Posts: 24
Rep Power: 13 |
Actually "amn" is the name of the dimensioned scalar - I do not know what OpenFOAM does with it. I have never experienced, that it matters what name you set for the dimensioned scalar.
Although I am quite interested in the answer to your question: What influence has the name of the dimensionedScalar? |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Newbie Question IcoFoam - Courant Number explodes | sprobst76 | OpenFOAM Running, Solving & CFD | 12 | March 1, 2018 08:35 |
question about uds | tanven | FLUENT | 2 | July 5, 2015 12:22 |
internal field question - PitzDaily Case | atareen64 | OpenFOAM Running, Solving & CFD | 2 | January 26, 2011 16:26 |
Question about Table applicaiton. | universez | OpenFOAM Running, Solving & CFD | 0 | January 12, 2010 21:31 |
CHANNEL FLOW: a question and a request | Carlos | Main CFD Forum | 4 | August 23, 2002 06:55 |