Newbie Question on PtrList usage
I recently received the following information from another post:
If N, you need
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
I get a fatal error: hanging pointer, cannot dereference at runtime.
If I try
I get a number of compilation errors. Any suggestions?
What are you trying to do with the line ks=1.0? I'm not an expert on PtrLists, but I believe that ks 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 you would need to do something like:
ks->internalField()[cellI] = 1.0 (because ks is a pointer you need the '->' instead of the '.' to call a member function)
*ks[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.
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.0;
base operand of '->' has non-pointer type 'Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh>'
ks->internalField() = 1.0;
The second error suggests that PrtList doesn't return a pointer. If I
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
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?
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.internalField[cellI] = 1
ks[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.size() should return nCells, because ks is a reference to the first volScalarField pointed to by ks; likewise ks.size(), ks.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.
I guess(!) the problem is that you have to actually initialize the volScalarFields in your PtrList by iterating through it.
Does it make sense?
To give you an example of a code I recently produced and which is working for me:
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 :
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?
|All times are GMT -4. The time now is 11:35.|