|
[Sponsors] |
September 21, 2014, 08:30 |
How to set BCs programmatically
|
#1 |
New Member
Join Date: Oct 2013
Posts: 11
Rep Power: 13 |
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]; } Is there something like: Code:
q.boundaryField().gradient()[patchi] = x // ??? 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. |
|
September 23, 2014, 07:09 |
|
#2 |
New Member
Join Date: Sep 2011
Posts: 16
Rep Power: 15 |
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 |
|
September 23, 2014, 08:33 |
|
#3 |
New Member
Join Date: Oct 2013
Posts: 11
Rep Power: 13 |
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? |
|
September 24, 2014, 07:14 |
|
#4 |
New Member
Join Date: Sep 2011
Posts: 16
Rep Power: 15 |
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(); Regards Philipp |
|
September 24, 2014, 07:34 |
|
#5 |
New Member
Join Date: Oct 2013
Posts: 11
Rep Power: 13 |
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, |
|
September 27, 2014, 05:57 |
|
#6 |
New Member
Join Date: Oct 2013
Posts: 11
Rep Power: 13 |
That seems to work for me either, thank you!
|
|
November 20, 2017, 03:47 |
|
#7 |
Member
behzad Ghasemi
Join Date: Sep 2013
Location: Iran
Posts: 56
Rep Power: 13 |
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 |
|
|
|
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 17:30 |
How to set up BCs for k epsilon model | Julian K. | OpenFOAM Pre-Processing | 44 | October 26, 2015 10:23 |
How to set a initial temperature and constant heat flux at same BC's?? | Jane | FLUENT | 4 | October 25, 2015 13:01 |
Help with GNUPlot | Renato. | Main CFD Forum | 6 | June 6, 2007 20:51 |
Problems with installation | jonititan | OpenFOAM Installation | 4 | November 6, 2005 05:16 |