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

Snippet for redefining fixedGradient boundary condition for a patch inside solver

Register Blogs Community New Posts Updated Threads Search

Like Tree12Likes

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   April 6, 2018, 05:54
Default
  #41
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Try the following:

Code:
const polyPatchList& patches = mesh.boundaryMesh();
forAll(patches,patchI)
{
    if (patches[patchI].name() == "midup")
    {
        fixedGradientFvPatchScalarField& gradcMinusPatch =
            refCast<fixedGradientFvPatchScalarField>(cMinus.boundaryField()[patchI]);
        scalarField& gradcMinusField = gradcMinusPatch.gradient();

        forAll(gradcMinusField, faceI)
        {                                           
            if (cMinus.boundaryField()[patchI][faceI] < 0)
            {
                // Set gradient for this face to zero
                gradcMinusField[faceI] = 0;
            }
            else
            {
                // Set gradient for this face to some calculated value; I assume eKbT, Dinv, and currentj are all scalars (not scalarFields)
                gradcMinusField[faceI] = eKbT*cMinus.boundaryField()[patchI][faceI]*Ue.boundaryField()[patchI].snGrad()[faceI] + Dinv*currentj;
            }
        }        
    }    
}
Also, I suggest reading the OpenFOAM coding style: following it will make your code more readable for others

Philip
babakflame likes this.

Last edited by bigphil; April 7, 2018 at 11:29. Reason: Fix: changed "forAll(patchI, faceI)" to "forAll(gradcMinusField, faceI)"
bigphil is offline   Reply With Quote

Old   April 6, 2018, 13:53
Default
  #42
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Many Thanks Philip

I am working on your proposed snippet and appologize for the caused confusions.

I had read that page, but maybe I need to re-read again

Regards
babakflame is offline   Reply With Quote

Old   April 6, 2018, 16:07
Default
  #43
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Philip,

thanks for your reply. I think the second forAll loop

Code:
forAll(patchI, faceI){
                 if(cMinus.boundaryField()[patchI][faceI] < 0){     
                    gradcMinusField [faceI] = 0;
                  }
                  else{
                  gradcMinusField [faceI] = eKbT * cMinus.boundaryField()[patchI][faceI] * Ue.boundaryField()    [patchI][faceI].snGrad() + Dinv * currentj; //3.13e13;   //midup
               }
         }
brings this error:

Code:
Boundary.H: In function ‘int main(int, char**)’:
/home/babak/OpenFOAM/OpenFOAM-2.1.x/src/OpenFOAM/lnInclude/UList.H:394:36: error: request for member ‘size’ in ‘patchI’, which is of non-class type ‘Foam::label {aka int}’
     for (Foam::label i=0; i<(list).size(); i++)
                                    ^
Boundary.H:18:4: note: in expansion of macro ‘forAll’
    forAll(patchI, faceI){
    ^
In file included from interFoamEHDIon5New.C:97:0:
Boundary.H:16:17: warning: unused variable ‘gradcMinusField’ [-Wunused-variable]
    scalarField& gradcMinusField = gradcMinusPatch.gradient();
                 ^
In file included from interFoamEHDIon5New.C:77:0:
/home/babak/OpenFOAM/OpenFOAM-2.1.x/src/finiteVolume/lnInclude/readTimeControls.H:38:8: warning: unused variable ‘maxDeltaT’ [-Wunused-variable]
 scalar maxDeltaT =
        ^
make: *** [Make/linux64GccDPOpt/interFoamEHDIon5New.o] Error 1
Regards
babakflame is offline   Reply With Quote

Old   April 7, 2018, 11:33
Default
  #44
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Sorry, "forAll(patchI, faceI)" should have been "forAll(gradcMinusField, faceI)": I have editted this in my previous post.

Also, for your second error, please be careful, you have written:
Code:
else
{
    gradcMinusField [faceI] =
        eKbT*cMinus.boundaryField()[patchI][faceI]*Ue.boundaryField()[patchI][faceI].snGrad() + Dinv * currentj;
}
whereas I have proposed:
Code:
else
{
    gradcMinusField[faceI] =
        eKbT*cMinus.boundaryField()[patchI][faceI]*Ue.boundaryField()[patchI].snGrad()[faceI] + Dinv*currentj;
}
Notice the differences.
bigphil is offline   Reply With Quote

Old   April 7, 2018, 11:38
Default
  #45
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Actually, as "snGrad()" constructs and returns the full patch field, it will be more efficient to store the snGrad as follows:
Code:
const polyPatchList& patches = mesh.boundaryMesh();
forAll(patches,patchI)
{
    if (patches[patchI].name() == "midup")
    {
        fixedGradientFvPatchScalarField& gradcMinusPatch =
            refCast<fixedGradientFvPatchScalarField>(cMinus.boundaryField()[patchI]);
        scalarField& gradcMinusField = gradcMinusPatch.gradient();
        const scalarField UeSnGrad = Ue.boundaryField()[patchI].snGrad();

        forAll(gradcMinusField, faceI)
        {                                           
            if (cMinus.boundaryField()[patchI][faceI] < 0)
            {
                // Set gradient for this face to zero
                gradcMinusField[faceI] = 0;
            }
            else
            {
                // Set gradient for this face to some calculated value; I assume eKbT, Dinv, and currentj are all scalars (not scalarFields)
                gradcMinusField[faceI] = eKbT*cMinus.boundaryField()[patchI][faceI]*UeSnGrad[faceI] + Dinv*currentj;
            }
        }        
    }    
}
This will avoid the full Ue patch snGrad field being created for every time a face value is set.
babakflame likes this.
bigphil is offline   Reply With Quote

Old   April 7, 2018, 17:48
Default
  #46
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Thank you philip for the comments.

I understood the points you raised in the snippet. The code compiles successfully.



I am running my cases in parallel. Do I need to add reduce function after the last line of each section (after gradcMinusField=.. and gradcPlusField=.. lines) to make sure that the master processor gets the correct value??

sth like this:

Code:
reduce(gradcMinusField = maxOp<scalar>());
Regards

Last edited by babakflame; April 8, 2018 at 01:02.
babakflame is offline   Reply With Quote

Old   April 9, 2018, 06:13
Default
  #47
Super Moderator
 
bigphil's Avatar
 
Philip Cardiff
Join Date: Mar 2009
Location: Dublin, Ireland
Posts: 1,097
Rep Power: 34
bigphil will become famous soon enoughbigphil will become famous soon enough
Without seeing the rest of your code, I cannot say whether you need to explicitly perform a parallel sync; however, from looking at your code snippet, the only terms that might need it are those you have defined before the code snippet e.g. eKbT, Dinv, currentj.

How do you calculate these terms?
bigphil is offline   Reply With Quote

Old   April 9, 2018, 14:19
Default
  #48
Senior Member
 
Bobby
Join Date: Oct 2012
Location: Michigan
Posts: 454
Rep Power: 16
babakflame is on a distinguished road
Philip,

These terms are all constant scalars (eKbT, Dinv) or constant outward vector (currentj) with known values given in the case folder (defined this way in createFields.H file)

Regards

Last edited by babakflame; April 9, 2018 at 16:01.
babakflame 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
Problem with cyclic boundaries in Openfoam 1.5 fs82 OpenFOAM 37 November 29, 2024 11:15
Wind turbine simulation Saturn CFX 60 July 17, 2024 06:45
Error finding variable "THERMX" sunilpatil CFX 8 April 26, 2013 08:00
[blockMesh] Cyclic BC's: Possible face ordering problem? (Channel flow) sega OpenFOAM Meshing & Mesh Conversion 3 September 28, 2010 13:46
[Gmsh] Import gmsh msh to Foam adorean OpenFOAM Meshing & Mesh Conversion 24 April 27, 2005 09:19


All times are GMT -4. The time now is 20:15.