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

How to set BCs programmatically

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 21, 2014, 07:30
Default How to set BCs programmatically
  #1
New Member
 
Join Date: Oct 2013
Posts: 11
Rep Power: 12
incompressible is on a distinguished road
Hi all,

I want to set BCs programmatically within my own solver code (so not from the txt files such as U, p etc.). I managed it for setting BCs as fixedValue, but I couldn't figure it out, how to do it for fixedGradient.

Lets assume that we have a scalar field q and assume that its BCs are defined as fixedValue and all initially set to zero from the file "q". After that, within the code, certain staff has been calculated and now I want to set the correct fixedValue BCs for "q" (instead of using the initial zero values from the file). That works fine if I do something like this:

Code:
        const fvPatchList& patches = mesh.boundary();

        forAll(patches,patchi){

            q.boundaryField()[patchi]
            =
            mesh.Sf().boundaryField()[patchi]
            & U.boundaryField()[patchi];

        }
Now, I actually would like to exactly do the same for fixedGardient. I change the BCs type to fixedGradient in the "q" file and initialise it with "gradient 0". In the code, after certain staff has been calculated, I want to change these gradient BCs from zero to their intended values programmatically, as the example code above. But I couldn't figure it out how to do it correctly...

Is there something like:

Code:
q.boundaryField().gradient()[patchi] = x // ???
where x is a scalar field defined on the boundary patch, e.g.

x = mesh.Sf().boundaryField()[patchi] & U.boundaryField()[patchi].

Is there also a way to programmatically set the normal and tangential components of the gradient separately?

In fact I want to set (at the boundary) the normal component of the gradient of "q" to a value x, such as

n_i (dot) grad(q)_i = x, where, "n_i" is the normal vector of the face of the cell i on the boundary, "(dot)" is the scalar product and grad(q)_i is the gradient of q on cell i at the boundary. And do the same for tangential components as well, e.g setting them to zero gradient.

I have studied the code to check how OpenFOAM sets these gradient BCs programmatically if one uses the type fixedGradient from file. There I figured out, that they use .readFields() whenever one wants to read from a file to set the BCs. On the boundary, they call _boundaryField.readField() in the class GeometricField. I think this is the place where certain patch objects are then created (regarding the type of the BC given in the txt file) such as "fvFixedGradientPatchField" or similar and the values are set from the file... However, I got lost in the code and could not really figure out, how they exactly put these BCs programmatically in their code.

Any help would be pretty much appreciated.
incompressible is offline   Reply With Quote

Old   September 23, 2014, 06:09
Default
  #2
New Member
 
Join Date: Sep 2011
Posts: 16
Rep Power: 14
Philipp Dietrich is on a distinguished road
Hi,

I am currently trying to solve a similar problem. (Changing the fixed gradient depending on the local temperature in the patch face).

Have you already found a solution to the problem?

Regards
Philipp
Philipp Dietrich is offline   Reply With Quote

Old   September 23, 2014, 07:33
Default
  #3
New Member
 
Join Date: Oct 2013
Posts: 11
Rep Power: 12
incompressible is on a distinguished road
Unfortunately, I have not found a solution for it yet. In my case I want to set the fixedGrads from certain measurements, which I load into the solver by acquiring first the right coordinates at boundaries and then setting them. As described above, I can only set fixedValues programmatically, but how to set the fixedGrads?

Anyone else can help us?
incompressible is offline   Reply With Quote

Old   September 24, 2014, 06:14
Default
  #4
New Member
 
Join Date: Sep 2011
Posts: 16
Rep Power: 14
Philipp Dietrich is on a distinguished road
Hi again,

maybe I found a solution; at least it seems to work for my problem...

Searching the forum, I found this thread:
http://www.cfd-online.com/Forums/ope...-groovybc.html

According to the approach in the second post in order to change a fixed gradient boundary condition by the solver, if I understand this correctly, the following should be done:

Code:
// Find the label of the patch that shall be changed
label patchID = mesh.boundaryMesh().findPatchID("Patch-Name");

// Define new patch-field for the variable at the patch (in my case temperature T)
fixedGradientFvPatchScalarField& Tpatch = refCast<fixedGradientFvPatchScalarField>(T.boundaryField()[patchID]);

// Create scalar Field for the gradient at that patch
scalarField& gradTpatch = Tpatch.gradient();

// Loop over every gradient in this patch
forAll(gradTpatch, faceI)
{
     // Perform calculations to determine the new gradient "newGrad"
     // Store the new value in the scalar field
     gradTpatch[faceI] = newGrad;
}

// Write the new gradient to the temperature field.
T.write();
I hope this helps to solve your problem, too.
Regards
Philipp
Philipp Dietrich is offline   Reply With Quote

Old   September 24, 2014, 06:34
Default
  #5
New Member
 
Join Date: Oct 2013
Posts: 11
Rep Power: 12
incompressible is on a distinguished road
Hi Philipp,

Thank you very much for the reply. I have read that thread yesterday too and was going to give it a try too. However, you were faster than me . In this thread, they explain the problem again for the fixedValue situation, but you have already provided the solution for the gradient case. I hope that works, I will try it and will post once again if it works also in my case. The key point was actually to perform type casting in of BC patchFields, such as fixedGradientFvPatchScalarField in a correct way...

In my case, I will have to set the normal component and something else for tangential components of the gradient. I assume, that your suggestion already sets the component of the gradient normal to the boundary face only ?!?

Best,
incompressible is offline   Reply With Quote

Old   September 27, 2014, 04:57
Default
  #6
New Member
 
Join Date: Oct 2013
Posts: 11
Rep Power: 12
incompressible is on a distinguished road
That seems to work for me either, thank you!
incompressible is offline   Reply With Quote

Old   November 20, 2017, 02:47
Default
  #7
Member
 
behzad Ghasemi
Join Date: Sep 2013
Location: Iran
Posts: 56
Rep Power: 12
behzad-cfd is on a distinguished road
Hi,

I need to set tangential gradient of a scalar field on a patch as boundary condition. Would you please share your code?
Thanks in advance,
Behzad
behzad-cfd is offline   Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
[mesh manipulation] Add / Remove faces from patch , create new djh2 OpenFOAM Meshing & Mesh Conversion 9 November 8, 2020 16:30
How to set up BCs for k epsilon model Julian K. OpenFOAM Pre-Processing 44 October 26, 2015 09:23
How to set a initial temperature and constant heat flux at same BC's?? Jane FLUENT 4 October 25, 2015 12:01
Help with GNUPlot Renato. Main CFD Forum 6 June 6, 2007 19:51
Problems with installation jonititan OpenFOAM Installation 4 November 6, 2005 04:16


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