CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Custom von Neumann boundary condition

Register Blogs Community New Posts Updated Threads Search

Like Tree6Likes
  • 1 Post By Gerry Kan
  • 3 Post By RGS
  • 2 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   August 11, 2020, 03:57
Default Custom von Neumann boundary condition
  #1
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
Hello,


I am trying to implement the following boundary condition in OpenFOAM:



\nabla{}u\cdot{}n = e\cdot{}n


where,
  • u is a field variable
  • n is the face normal of the surface
  • e is a unit vector
I have implemented this in OpenFOAM 7 using groovyBC (Swak4FOAM) using the following code:

Code:
FSI
    {
        type             groovyBC;
        value uniform     0;
        valueExpression "0";
        fractionExpression "0";  //0..Neumann, 1..Dirichlet
        //Neumann with value equal to e_vec*normalvector
        variables "e_dot_n=vector(1,0,0) & normal()/mag(normal());";
        gradientExpression "e_dot_n";
    }


I wasn't able to install Swak4FOAM with OpenFOAM 8, and in general I would learn to know how to code a boundary condition by myself. I found some tutorials for Dirichlet boundary conditions. Could someone please help me with implementing this BC?


Thanks in advance!
RGS is offline   Reply With Quote

Old   August 11, 2020, 05:24
Default
  #2
New Member
 
Jesper R. K. Qwist
Join Date: Dec 2017
Posts: 22
Rep Power: 8
Jesper_Roland is on a distinguished road
Hi,

Search for programming course by Pavanakumar Mohanamuraly or look in the book by Moukalled about OpenFOAM. You can find what need in these references.

https://pavanakumar.github.io/compre...am/of_tut.html

https://www.amazon.com/Finite-Method.../dp/3319168738

Good luck,

Jesper
Jesper_Roland is offline   Reply With Quote

Old   August 12, 2020, 04:02
Default
  #3
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
Quote:
Originally Posted by Jesper_Roland View Post
Hi,

Search for programming course by Pavanakumar Mohanamuraly or look in the book by Moukalled about OpenFOAM. You can find what need in these references.

https://pavanakumar.github.io/compre...am/of_tut.html

https://www.amazon.com/Finite-Method.../dp/3319168738

Good luck,

Jesper

Hello Jesper,

Thank you for the references. I already have the book by Moukalled. Starting from page 747, the book explains how to define a custom boundary condition. In my case, I do not want the whole patch to have the same constant gradient. The value on each face in the patch is dependent on the orientation of the face, and I do not know how to implement this. The tutorials by Pavanakumar do not cover custom boundary conditions at all.
RGS is offline   Reply With Quote

Old   August 12, 2020, 14:21
Default
  #4
Senior Member
 
Gerry Kan's Avatar
 
Gerry Kan
Join Date: May 2016
Posts: 347
Rep Power: 10
Gerry Kan is on a distinguished road
Dear Rojit:

At the risk of telling you something you might already know ...

If you have to introduce your own gradient, may I suggest using the fixedGradient boundary condition as reference (FOAMHOME/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient).

The member functions responsible for altering the gradients are located in either evaluate() or updateCoeffs().

fixedGradientFvPatchField is inherited from fvPatchField, which in turn is inherited from fvPatch. In fvPatch there is a variable nf, which returns the face normal vector of each surface belonging to the same patch.

I assume your gradient is fixed, only the dot product needs changing. In that case you only need to calculate the direction cosine and multiply it with your gradient magnitude.

Here is a call graph of fvPatch, as well as the declaration of fixedGradient for reference.

Again if this is something you know already please accept my apologies for the repetition.

Hope that helps, Gerry.
RGS likes this.
Gerry Kan is offline   Reply With Quote

Old   October 29, 2020, 07:22
Default
  #5
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
Quote:
Originally Posted by Gerry Kan View Post
Dear Rojit:

At the risk of telling you something you might already know ...

If you have to introduce your own gradient, may I suggest using the fixedGradient boundary condition as reference (FOAMHOME/src/finiteVolume/fields/fvPatchFields/basic/fixedGradient).

The member functions responsible for altering the gradients are located in either evaluate() or updateCoeffs().

fixedGradientFvPatchField is inherited from fvPatchField, which in turn is inherited from fvPatch. In fvPatch there is a variable nf, which returns the face normal vector of each surface belonging to the same patch.

I assume your gradient is fixed, only the dot product needs changing. In that case you only need to calculate the direction cosine and multiply it with your gradient magnitude.

Here is a call graph of fvPatch, as well as the declaration of fixedGradient for reference.

Again if this is something you know already please accept my apologies for the repetition.

Hope that helps, Gerry.

Hello Gerry, sorry for the late reply. Thank you for your suggestions, and for the very good summary on what/where to look at to modify boundary conditions in OpenFOAM. I think what you mentioned with modifying fixedGradient is the easy approach if the gradient is fixed. Unfortunately, I need to apply this boundary condition on a curved surface and hence the angle between the vectors will be different for each cell.
RGS is offline   Reply With Quote

Old   October 29, 2020, 07:27
Default
  #6
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
It took some time, but I managed to implement the boundary condition. And I get the same results I did with Swak4Foam. I will post my code here with as many details as possible in the comments in case it will help someone else.


Code:
    FSI    //name of patch
    {
        type            codedMixed;
        
        // Constructs a new boundary condition on-the-fly.
        // This boundary condition (BC) is derived from the 'mixed' BC and is a 
        // Robin BC, which a linear blend of 'fixedValue' (Dirichlet) and 
        // 'gradient' (von Neumann) BCs. The blending is specified using the 
        // 'valueFraction' entry according to the equation given at:
        // https://www.openfoam.com/documentation/guides/latest/doc/guide-bcs-mixed.html
        // Header file: https://cpp.openfoam.org/v7/codedMixedFvPatchField_8H_source.html
        
        
        refValue        uniform 0;
        refGradient        uniform 0;
        valueFraction    uniform 0;        //1 - pure Neumann, 0 - pure Dirichlet
        
        name            customBC;        //The name of the dynamically created BC
        
        code
        #{
               // Explanation:
            // Our starting position (this pointer) is a fvsPatchField, so we 
            // have to use the methods made available by this class.
            // patch () returns an fvPatch object, which contains all the 
            // geometrical information about the patch, such as the surface 
            // normals
            // The method nf() does not return a vector field, but a 
            // 'tmp<vectorField>' object which points to the relevant 
            // information, but are not themselves the information we need.
            // The normals can be obtained by using the () operator (redefined 
            // in OpenFOAM)
            
            // Get the reference of the mesh
            const fvMesh& mesh = patch().boundaryMesh().mesh();
                        
            // Set the unit vector
            const vector e(1, 0, 0);
            
            // Get the temporal object nf
            const tmp<vectorField> tmpNf = patch().nf();
            
            // Get the vector field associated with the location specified by 
            // tmpNf
            const vectorField& nf_p = tmpNf();
            // Note that the () operator has been redefined in OpenFOAM. We use 
            // this operator to get the vector field that we need.
            // Link: https://cpp.openfoam.org/v7/classFoam_1_1tmp.html#afdc36f6a5716b29a5d81a2dd93376d82
             
            volVectorField e_feld
            (
                IOobject
                (
                    "e_feld",
                    db().time().timePath(),
                    db(),
                    IOobject::NO_READ,
                    IOobject::NO_WRITE
                ),
                mesh,
                dimensionedVector
                (
                       "e0",
                    dimensionSet(0, 0, 0, 0, 0, 0, 0),
                       e
                )
            );
            
            // Get a reference of the vector field in current patch
            const vectorField& e_feld_p = e_feld.boundaryField()[patch().index()];
            
            // Setting the gradient field for the boundary condition
            this->refGrad() = e_feld_p & nf_p;
        #};
         
        codeInclude
        #{
            #include "fvCFD.H"
        #};

        codeOptions
        #{
            -I$(LIB_SRC)/finiteVolume/lnInclude \
            -I$(LIB_SRC)/meshTools/lnInclude \
            -I$(LIB_SRC)/sampling/lnInclude
        #};
    }
My many thanks to fellow forum member Carlos Rubio Abujas who helped me figure most of this out.
Farid, Gerry Kan and saladbowl like this.

Last edited by RGS; October 29, 2020 at 18:00.
RGS is offline   Reply With Quote

Old   October 29, 2020, 09:48
Default
  #7
Senior Member
 
Santiago Lopez Castano
Join Date: Nov 2012
Posts: 354
Rep Power: 15
Santiago is on a distinguished road
Quote:
Originally Posted by RGS View Post
Hello,


I am trying to implement the following boundary condition in OpenFOAM:



\nabla{}u\cdot{}n = e\cdot{}n


where,
  • u is a field variable
  • n is the face normal of the surface
  • e is a unit vector
I have implemented this in OpenFOAM 7 using groovyBC (Swak4FOAM) using the following code:

Code:
FSI
    {
        type             groovyBC;
        value uniform     0;
        valueExpression "0";
        fractionExpression "0";  //0..Neumann, 1..Dirichlet
        //Neumann with value equal to e_vec*normalvector
        variables "e_dot_n=vector(1,0,0) & normal()/mag(normal());";
        gradientExpression "e_dot_n";
    }


I wasn't able to install Swak4FOAM with OpenFOAM 8, and in general I would learn to know how to code a boundary condition by myself. I found some tutorials for Dirichlet boundary conditions. Could someone please help me with implementing this BC?


Thanks in advance!
I'm sorry, but why do you need to implement a new BC? This is simply a fixedGradient...
Santiago is offline   Reply With Quote

Old   October 29, 2020, 10:26
Default
  #8
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by RGS View Post
It took some time, but I managed to implement the boundary condition. And I get the same results I did with Swak4Foam. I will post my code here with as many details as possible in the comments in case it will help someone else.
Thanks for the share, but I would caution about being wasteful with resources. You have created an entire volume field, only in order to extract some patch information back out of it. With the comments stripped out (for the sake of the overview), your actually code appears to correspond to a one-liner (at least for the code part):
Code:
    FSI
     {
        type            codedMixed;
        name          customBC;
        refValue      uniform 0;
        refGradient uniform 0;
        valueFraction uniform 0;

        code
        #{
            // Set gradient field for the boundary condition
            this->refGrad() = vector(1,0,0) & patch().nf();
        #};

        codeInclude
        #{
            #include "fvCFD.H"
        #};

        codeOptions
        #{
            -I$(LIB_SRC)/finiteVolume/lnInclude \
            -I$(LIB_SRC)/meshTools/lnInclude
        #};
    }
If you still prefer swak4Foam-type of syntax, could still use the exprMixed BC from OpenFOAM-v1912 or OpenFOAM-v2006.
https://www.openfoam.com/documentati...d.html#details
RGS and saladbowl like this.
olesen is offline   Reply With Quote

Old   October 30, 2020, 04:26
Default
  #9
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
Quote:
Originally Posted by olesen View Post
Thanks for the share, but I would caution about being wasteful with resources. You have created an entire volume field, only in order to extract some patch information back out of it. With the comments stripped out (for the sake of the overview), your actually code appears to correspond to a one-liner (at least for the code part):
Code:
    FSI
     {
        type            codedMixed;
        name          customBC;
        refValue      uniform 0;
        refGradient uniform 0;
        valueFraction uniform 0;

        code
        #{
            // Set gradient field for the boundary condition
            this->refGrad() = vector(1,0,0) & patch().nf();
        #};

        codeInclude
        #{
            #include "fvCFD.H"
        #};

        codeOptions
        #{
            -I$(LIB_SRC)/finiteVolume/lnInclude \
            -I$(LIB_SRC)/meshTools/lnInclude
        #};
    }
If you still prefer swak4Foam-type of syntax, could still use the exprMixed BC from OpenFOAM-v1912 or OpenFOAM-v2006.
https://www.openfoam.com/documentati...d.html#details

Thank you for simplifying my code. I did not know that it can be written in one line! And yes, I know that I created an entire volume field to get some information about a patch, but I did not know how else I can do it. I am still quite a novice OpenFOAM user and I think I make mistakes like this very often. The simplified code you posted is also much easier to understand.

Regarding OpenFOAM from ESI - I came across this feature (and some others) last week and I spoke to my boss about shifting to the ESI distribution. Unfortunately I do not get to decide which version of OpenFOAM we use. But thank you for mentioning it.
RGS is offline   Reply With Quote

Old   October 30, 2020, 04:29
Default
  #10
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
Quote:
Originally Posted by Santiago View Post
I'm sorry, but why do you need to implement a new BC? This is simply a fixedGradient...

I don't know how to implement this with the fixedGradient BC. The patch is a curved surface, and so each cell face has a normal pointing in a different direction. Maybe it is possible to implement it using a dictionary? Or maybe I am missing something that should be very obvious, because Gerry also suggested using fixedGradient.
RGS is offline   Reply With Quote

Old   October 30, 2020, 18:11
Default
  #11
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,686
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by RGS View Post
... The simplified code you posted is also much easier to understand.

Regarding OpenFOAM from ESI - I came across this feature (and some others) last week and I spoke to my boss about shifting to the ESI distribution. Unfortunately I do not get to decide which version of OpenFOAM we use. But thank you for mentioning it.
I personally think the following aspect to be important in the decision as well:
Original OpenFOAM author(s)?
olesen is offline   Reply With Quote

Old   November 1, 2020, 08:20
Default
  #12
RGS
Member
 
Rohit George Sebastian
Join Date: May 2017
Posts: 41
Rep Power: 8
RGS is on a distinguished road
Quote:
Originally Posted by olesen View Post
I personally think the following aspect to be important in the decision as well:
Original OpenFOAM author(s)?

I saw your post in Linkedin a few days back. I just didn't realise that you were the author of the post. Thank you for sharing a link to the thread.
RGS is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Table bounds warnings at: END OF TIME STEP CFXer CFX 4 July 16, 2020 23:44
My radial inflow turbine Abo Anas CFX 27 May 11, 2018 01:44
several fields modified by single boundary condition schröder OpenFOAM Programming & Development 3 April 21, 2015 05:09
Question about heat transfer coefficient setting for CFX Anna Tian CFX 1 June 16, 2013 06:28
CFX fails to calculate a diffuser pipe flow shenying0710 CFX 7 March 26, 2013 04:13


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