CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   Access a volScalarField from BC code (

Hisham November 24, 2011 08:10

Access a volScalarField from BC code
Dear Foamers,

I am trying to access a volScalarField values at cells next to a patch from the BC code. I tried the following:

const fvPatchField<scalar>& Mu =
      patch().lookupPatchField<volScalarField, scalar>("solidShearMod");

The code compiles fine but does not work properly ... The value passed is different from what the solver outputs. I have tried that with a uniform value for the volScalarField. Nevertheless, the solver should have nonuniform values in practice.

I would appreciate any hint on what may be wrong. I did not provide more data because I frankly do not know what may cause this.

Best regards,

marupio November 24, 2011 10:44

Hi Hisham,

I just did this myself. Are you trying to access its own values, or another volScalarField? If you are in... say... T's boundary condition, and you want T's values *next* to the boundary patch, then I can tell you what to do. Otherwise, you need to use the object registry to look up the other volScalarField.

This gives you a list of cell indices that correspond to the internal field cells next to the patch:


    labelList cellNeighbours(patch().faceCells());
Then it's one to one:


corresponds with:

or whatever you are using.

Hisham November 24, 2011 11:27

Hi David,

Thanks a lot for the quick feedback. The volScalarField I am trying to access is not the one for the BC. The approach I posted is used in the tractionDisplacementFvPatchVectorField.C BC line 173 to access the sigmaD volSymmTensorField from the BC that is used for displacement volVectorField in the solidDisplacementFoam solver.

With regard to what you posted, I understand that in my case I should pass the whole volField from the registry then find cells with faces on patch and extract values individually ... Is that well understood?

Best regards,

marupio November 24, 2011 11:29

Yes, use the object registry to gain a const reference to the field in question. The cellNeighbours list should be the same for the local GeometricField as it is for the volScalarField you look up.

Hisham November 25, 2011 15:27

Hi David,

I found a reference to internal field in the fvPatchField class (similar to the patch() reference). I will try to use it then if still not successful I will try the objectRegistry.

Thanks a lot for you help!

Best regards,

marupio November 25, 2011 15:54

The internalField() reference or dimensionedInternalField() will give you direct access to the data in the GeometricField your boundary condition applies to, not any others. To access others, I think you have to use lookupObject in the objectRegistry.

Hisham November 25, 2011 16:33

OK ... Thanks again!

All times are GMT -4. The time now is 09:26.