# Variation of boundary condition using codedFixedValue

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

 June 15, 2023, 09:14 Variation of boundary condition using codedFixedValue #1 New Member   Clément Pélissier Join Date: May 2023 Posts: 4 Rep Power: 2 Hello, I am on a project on which one I want build a variation of the boundary condition of the slope. For this, I use codedFixedValue and want to retrieve the time of the simulation and the endtime of the simulation as a scalar as it runs. But I can't find the good function or syntaxe to retrieve these both values. I work on OpenFOAM 9. Here is the code to let you understand what I want to explain. I hope that someone can help me. Thank you very much in advance, Clément Code: ```/*--------------------------------*- C++ -*----------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Version: 9 \\/ M anipulation | \*-----------------------------------------------------------------------------*/ FoamFile { format ascii; class volScalarField; location "0"; object T; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // dimensions [ 0 0 0 1 0 0 0 ]; internalField uniform 296.15; boundaryField { ground { type fixedValue; value uniform 296.15; } slope { type codedFixedValue; value uniform 0; // initial value name variationHeatFlux; code #{ #include "createTime.H" #include "Time.H" const fvPatch& patch = this->patch(); const vectorField& cf = patch.Cf(); scalarField& field = *this; forAll(cf, i) { const scalar currentTime = runTime.time(); // current time of the simulation const scalar endTime = runTime.endtime(); // end of the simulation const scalar tStart = 0; // initial time const scalar gradStart = 0; // initial gradient const scalar gradMax = 758.99; // maximal gradient const scalar tMax = endTime/2; // maximal gradient time (half of the simulation) const scalar gradDecrease = gradMax/tMax; // decrease step after reach maximum scalar grad; if (currentTime <= tMax) { grad = gradStart + (gradMax - gradStart) * (currentTime - tStart) / (tMax - tStart); } else { grad = gradMax - gradDecrease * (currentTime - tMax) / (endTime - tMax); } field[i] = grad; } #}; } ceiling { type fixedValue; value uniform 296.15; } front { type symmetryPlane; } back //maybe here we need to use frontandback with zerogradient { type fixedValue; value uniform 296.15; } right //maybe here we need to use sideWalls with zerogradient { type cyclic; } left { type cyclic; } }```

 July 5, 2023, 03:45 #2 New Member   Join Date: Jun 2023 Location: Brest Posts: 21 Rep Power: 2 I am new to openfoam, but if you set a good writing step in controlDict, shouldn't it be enough ? I notice that you might forgot a "const" before "scalar grad".

 July 7, 2023, 03:56 #3 Senior Member   Mark Olesen Join Date: Mar 2009 Location: https://olesenm.github.io/ Posts: 1,613 Rep Power: 39 You mostly have the right idea except for the fatal mistake of using 'include "createTime.H"' within your code. This is definitely not what you want! It goes off and tries to create TIme by reading the system/controlDict (every time that you call the boundary condition). It also means that that particular Time you have created corresponds to the time state at the start of the simulation. What you do want is to go from the *this (the boundary condition) back upwards, for example: Code: ```// Starting from the fvPatchField (*this) const Time& runTIme = this->db().time(); ...``` https://www.openfoam.com/documentati...atchField.html Oh, and it would be cleaner to grab the current/end times outside of the loop! If you rework the code you'll see that it reduces down to a single value, which you can then assign to the entire field in one step.

 Tags codedfixedvalue, openfoam9, runtime