CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   Accessing Patch Objects from Solver (

nilsl January 24, 2011 08:48

Accessing Patch Objects from Solver

I am using OpenFOAM to solve an electrostatic problem involving several different potentials, represented by boundaries, that need to be optimized during the solution process. I am able to write optimization routines inside each boundary condition and change their values independently, but since the problem I am working on is quite strongly coupled this is not enough.

I need to optimize all the boundary conditions together. My plan is to find the pointers to all the relevant boundaries objects inside the main solver and write a routine there that handles the boundary value optimization. However, I have not been able to figure out how to do this.

Is there a way of accessing all the pointers to the boundaries (patchFields) inside the main solver? Maybe extracting them from the associated volField or from the fvMatrix? Do you know any other way to do something similar?


Misha January 25, 2011 10:29

Hi Niels

I am not sure exactly how you want to access the boundary values from the main script, so maybe my suggestion is a bit off, however I give it a try:

I know you can fetch the values of for instance the internal field next to a boundary patch (here number 1) as a patch field for the volScalarField T through the fvMesh using:

mesh.objectRegistry::lookupObject<volScalarField>( "T").boundaryField()[1].patchInternalField()

Hope it helps.
~ Misha

herbert January 25, 2011 11:15

Hi Nils,

it's hard to tell you what to do without knowing which kind of boundary conditions you are using. But if you want to set the gradient of a fixedGradient-BC of a volScalarField p at boundary no. 5 for example you can use

fixedGradientFvPatchScalarField& pPatch = refCast<fixedGradientFvPatchScalarField>(p.boundaryField()[5]);
scalarField& pGrad = pPatch.gradient();

and set for the gradient (pGrad) whatever you want. Note that you have to add

#include "fixedGradientFvPatchFields.H"
inside your header. Using other boundary conditions is working in a similar way.


@Misha: Your method is working if you need to know the internal field from within boundary conditions, but I think Nils is going to edit boundary condition from within top level solver.

nilsl January 26, 2011 03:50

Hi Misha and Stefan,

Stefan, the code you provided is exactly along the lines I was looking for. The exact implementation is not important, what I was looking for is how to get the patch objects. The real problem contains mostly fixed value boundary conditions, which I will probably modify using access functions.

Looking at the doxygen I realize how the whole thing works. p.boundaryField() gives me an object that has inherited a FieldField, which in turn has inherited a container with all the patch objects. Then I simply have to do a cast and I get what I was looking for.

Misha, your code also seems to get me the same objects. If I needed to get the same information and I didn't have access to the GeometricField object containing the electric potential, this would probably be the way to go.

Thanks a lot for your help.


Misha January 26, 2011 06:33

Hi Nils and Stefan

Nils, good that you figured out how to access the object you needed!

Stefan, I tested my code in the top level solver, and it actually accesses the internal patch field. But of course, you can also use it within a boundary condition.

Best regards,

All times are GMT -4. The time now is 08:50.