CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   Calling a method from derived BC (

psosnows September 6, 2012 11:04

Calling a method from derived BC
Dear Foamers,

I implemented a derived boundary condition, and added a new method. Now I am trying to use this new method "from the main code".

In general, the derived patch field is:

class myDerivedPatchField
    public fixedGradientFvPatchScalarField
//(...) some standard overloading of updateCoeffs(), constructors, etc
  bool myMethod()
      return true;

Now supposing I created a volScalarField F, which has as one of its boundaries myDerivedPatchField, I want to call that method in the main code of the solver:

int main(int argc, char *argv[])
  //(...) declarations, loading, creating volScalarField F
        Info << refCast<myDerivedPatchField>(F.boundaryField()[b]).myMethod() <<endl;

Unfortunately isA<myDerivedPatchField>(F.boundaryField()[b]) returns always false. From what I get, F.boundaryField()[b] at this level of the code is a fvPatchField, and does not match myDerivedPatchField. At the same time, without the isA<>() check, refCast breaks as it encounters some other boundary type (like zeroGradient).

What should I do to cast an added method in the main code?

Thank you in advance!

hua1015 December 4, 2013 04:48

Dear Psosnows,
I also met this problem but failed to correct it. Have you got the right way?I will be thankful for your suggestions.

wyldckat December 26, 2013 15:38

Greetings to all!

Can either one of you attach the 3 things necessary for replicating this issue? Namely:
  1. The derived boundary condition.
  2. The modified solver.
  3. A test case to use them.
Because I don't know the answer, but if I have access to the 3, I can easily solve your problem.

Best regards,

ngj December 26, 2013 18:44

Good evening,

I do not have access to a prober computer, so I do not have the means of checking the hypothesis, but...:

You should carefully check the "isA<>()" command, because if it take a patchField and not a fvPatchField it makes all the difference. Your boundary condition is derived from a patchField deep down, so you might encounter this problem, because the many derived levels are merely ignored.

Good luck,


ngj December 26, 2013 19:02

Also, it might be that isA asks for the physical type of the boundary condition, i.e. you might want to check on the typeName instead,. Something like this:


if (F.boundaryField()[p]::typeName == Foam::myDerivedFvPatchField::typeName)
Note that I have not tried to compile this, so it might fail. Especially, I am uncertain of the need of "()" after typeName.

Kind regards,


All times are GMT -4. The time now is 00:53.