CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   PtrList with shape of mesh or something similar (

maybee April 9, 2014 15:54

PtrList with shape of mesh or something similar

in one of the equations I am implementing is a really complex source term where one equation part returns 4 values for each cell of the mesh that are currently stored in an object of type "Mat<double>" (Matrix class of library armadillo) :/.

Is it possible to create a PtrList with the shape of mesh where each pointer points to a matrix or something similar?

The main reason is, that I need to multiplicate these matrices with another part of the equation for each cell and if I want to use matrix operations it would be best if I could keep the matrices in a kind of PtrList or List with the shape of mesh.

greetings maybee

chrisb2244 April 10, 2014 01:13

Not quite sure what you mean here, but I'm guessing you have a matrix for each cell in your mesh, and you want to be able to easily find each matrix?

My (third or fourth) thought on this is that you might be able to use a

PtrList<Matrix> pointerList (nCells)
forAll(pointerList, i)
    pointerList[i] = MatrixList[i] // Not sure if you can
    // get a list of your matrices very easily - perhaps not or else this
    // would be fairly trivial?
    // If you create the matrices by formula which depends on your value
    // of i, you could use pointerList.append(matrixI) instead, and probably
    // construct the PtrList as null?

and write a function with something like the code used in Kmesh for referencing, where nn is the size of the dimensions, eg 3(2 4 5) for a 2x4x5 matrix.

inline Foam::label Foam::Kmesh::index
    const label i,
    const label j,
    const label k,
    const labelList& nn
    return (k + j*nn[2] + i*nn[1]*nn[2]);

Taken a step further, you could probably define an operator overload for the [] operator? Not sure, but seems doable in principle?

maybee April 10, 2014 06:36


thx for the help.

1.I also thought about doing something like your first code, but there is one specific reason I asked this question. Will it be sufficient to create a PtrList with the size of the mesh cells? I thought it would also be necessary to pass some kind of "global cell structure" (shape of the mesh) to the list (?). How is the structure of the mesh preserved within fields like volScalarField, surfaceScalarField, i.e. how does OpenFOAM know which value of the field (in my case PtrList) will belong to which cell? -> Therefore I was not sure if it will be sufficient just to create the PtrList with the cell number.

To me it generally would be nice to know how OpenFOAM handles its field, especially how it assigns the values to the right cells/surfaces.

2. What exactly do you mean by your second code :D ? If I have stored the matrices in a PtrList where each matrice is "stored in the right cell" I can already access them like


forAll (PtrListMatrices, celli)

-> mission accomplished :D.


chrisb2244 April 10, 2014 20:35

I wouldn't swear to it, but I'm pretty sure that the fields have no knowledge of the shape of the mesh by themselves. The mesh class (whichever you're using, eg fvMesh) holds the positions of cells, along with their connectivity (connected-ness? I know that isn't a word, but seems like connectivity might describe a different concept?). The owners and neighbours of faces, at least :) This allows the calculation of fluxes, I suppose?

The fields are as far as I know, just lists of vectors, values, etc.

chrisb2244 April 10, 2014 20:37

Oh, and as for the 2nd piece of code, I thought you were wanting to access the members by coordinate, like

matrix[1, 4, 2]
or something - the second piece of code would allow

or similar, whereas an overloading of the []operator might allow you to directly use the first example, matrix[x,y,z].

All times are GMT -4. The time now is 18:33.