CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Pre-Processing (https://www.cfd-online.com/Forums/openfoam-pre-processing/)
-   -   Non-uniform Boundary Conditions (https://www.cfd-online.com/Forums/openfoam-pre-processing/114356-non-uniform-boundary-conditions.html)

amir.a.aliabadi March 9, 2013 16:20

Non-uniform Boundary Conditions
 
Hello There,

I am new to OpenFoam, modelling natural air convection in an enclosed box. A few walls in my boundaries have non-uniform temperatures. I like to impose a boundary condition with a temperature profile as a function of height on my walls (e.g. T=2*z+298 [K]). How do I changed the T boundary condition file for this purpose? Do I need to add any other files in any directories that this problem is set up?

Soheyl March 12, 2013 13:40

AAA,

If you are using OF 2.0+, take a look at codedFixedValue BC. If you are using older versions, or want more flexibility, search for swak4Foam.

Good luck and Tarif kon!

amir.a.aliabadi March 12, 2013 14:47

Thank You Soheil,

I found many such header and C files codedFixedValue*.* under openfoam sub directories. Can you recommend which files, exactly, have to be modified? and do you have an example code that modifies fixed value boundary conditions?

Thanks,
aaa

Soheyl March 12, 2013 16:06

You only need to modify your BC files, i.e. 0/T. Take a look at tutorials/incompressible/simpleFoam/pipeCyclic/0.org/U to get an idea, and also search for codedFixedValue in this forum. It is not the most user-friendly syntax, but you should be able to get it working.

chegdan March 12, 2013 18:14

As another suggestion, maybe you would want to look into the utility funkSetBoundaryField or groovyBC that is part of the tool set "swiss army knife for foam" (a.k.a. swak4Foam).

amir.a.aliabadi March 14, 2013 17:27

Hello FOAMers,

After much investigation, I was finally able to implement codedFixedValue boundary condition for my problem. I decided to provide a "how to" instruction so others can benefit from my experience. Such examples are lacking in the OpenFOAM documentation as well as online forums, so hopefully more complete examples will emerge.

The most recent versions of OpenFOAM (2.x.x and later) are able to construct, on-the-fly, new boundary conditions (e.g. codedFixedValueFvPatchField which is derived from fixedValueFvPatchField). This is very useful since you only need to make changes to your boundary condition file (e.g. 0/T for temperature). This saves some trouble so you don't have to install third party packages like Swak4Foam or GroovyBC which at times are incompatible with your version of OpenFOAM.

Step 1: codedFixedValue boundary condition method attempts to load a shared library using case-supplied C++ code, so you need to allow your FOAM to execute user written code. You should switch "allowSystemOperations" in .../etc/controlDict from "0" to "1". So edit controlDict before proceeding to the next step. (e.g. sudo gedit /opt/openfoam211/etc/controlDict if OpenFOAM is installed under root directory)

Step 2: modify the boundary condition file (e.g. 0/T for temperature) and supply as many patches with source code as you need. An example is provided below:

FoamFile
{
version 2.0;
format ascii;
class volScalarField;
location "0";
object T;
}

dimensions [0 0 0 1 0 0 0];
internalField uniform 253;
boundaryField
{
leftWindow
{
type codedFixedValue;
value $internalField;
redirectType leftWindowBC;

code
#{
vector dir=vector(0,0,1);
scalarField var=patch().Cf()&dir;
scalarField value=var*40+245;
operator==(value);
#}
}
rightWindow
{
type codedFixedValue;
value $internalField;
redirectType rightWindowBC;

code
#{
vector dir=vector(0,0,1);
scalarField var=patch().Cf()&dir;
scalarField value=var*40+250;
operator==(value);
#}
}
insulation
{
type zeroGradient;
}
}

In the above example the temperature profiles on vertical windows are evaluated as T=40z+245 [K] for left window and T=40z+250 [K] on right window. "type codedFixedValue" is essential to define so the compiler understands what to do with the code. "value $internalField" deserves a little explanation. Although you are calculating the boundary value using the code, you need this specified so the compiler at least has a starting value to work with (e.g. when plotting boundary values using paraView at time 0). In this case the "uniform 253" value is substituted for the very start. But for later times the code is used to evaluate the boundary. "redirectType leftWindowBC" assigns a name for this boundary patch. It is absolutely critical you use a different name for each new boundary patch definition. The "code #{ ... #}" part is just C++ language and you have to make sure the code is consistent with the libraries you want to load it on. Take a look at examples elsewhere or the libraries themselves to ensure your code is compatible.

Step 3: call your desired solver in the case directory (e.g. buoyantBoussinesqPimpleFoam). The solver automatically compiles coded boundaries and creates a folder called "dynamicCode". If you examine the contents, you see for each boundary patch that you defined, there is a folder with the same assigned name and all the required *.C, *.H, and other files necessary. If your custom code is not consistent with the OpenFOAM libraries, then you get an error message why. Then the solver iterations automatically begin.

Note: it is absolutely important that you are comfortable with coding a patch that the OpenFOAM libraries understand. Otherwise you are in trouble!

Good Luck and thanks to everyone who contributed to this post!

ancolli January 30, 2014 10:27

Sorry. I am new in openFoam. Your example is not working for mi. I have obtained the following error: FOAM FATAL IO ERROR: Cannot find patchField entry for...
Could you please post your entire code?. Thank you very much

amir.a.aliabadi January 30, 2014 18:31

Hi Ancolli,
It has been a long time since I used OpenFOAM and I do not have it on my current system to provide the entire case directory. I suggest you follow my previous instructions carefully, especially the beginning 2 steps. Also make sure your version of OpenFOAM supports codedFixedValue boundary conditions.
I hope this helps!
aaa

ajnewman February 4, 2014 12:21

When you use this approach, should the boundary be specified as leftWindow or leftWindowBC in the blockMeshDict file?

Thanks!

ajnewman February 4, 2014 16:59

Also, to make the code run properly I believe there needs to be a ; after #} i.e.
#};

amir.a.aliabadi February 4, 2014 18:52

Hello,
As far as I remember both leftWindow or leftWindowBC are just names and not C++ programming syntax, so as long as you are consistent to use the same name in the linked files you should be fine. As far as syntax, be my guest, it has been a long time since I did any C++ programming
Cheers
aaa

lynzhung March 19, 2014 02:32

hi
 
Quote:

Originally Posted by ancolli (Post 472578)
Sorry. I am new in openFoam. Your example is not working for mi. I have obtained the following error: FOAM FATAL IO ERROR: Cannot find patchField entry for...
Could you please post your entire code?. Thank you very much


I am a new too and face the similar error as you .did you solve the problem ?

Thanks
Lyn

ancolli March 19, 2014 07:02

Quote:

Originally Posted by lynzhung (Post 480795)
I am a new too and face the similar error as you .did you solve the problem ?

Thanks
Lyn

yes, but I do not remember. Searching on my folder I have found a slightly different problem. With only one boundary condition with variable Temperature. It works!.

FoamFile
{
version 2.0;
format ascii;
class volScalarField;
location "0"; //nuevo//
object T;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions [0 0 0 1 0 0 0];

internalField uniform 253;

boundaryField
{
inlet
{
type fixedValue;
value uniform 253;
}

outlet
{
type zeroGradient;
}



upperWall
{
type zeroGradient;
}

lowerWall
{
type codedFixedValue;
value $internalField;
redirectType lowerWallBC;

code
#{
vector dir=vector(1,0,0);
scalarField var=patch().Cf()&dir;
scalarField value=var*40+250;
operator==(value);
#}
}
frontAndBack
{
type empty;
}
}

// ************************************************** *********************** //

lynzhung March 19, 2014 21:53

Quote:

Originally Posted by ancolli (Post 480854)
yes, but I do not remember. Searching on my folder I have found a slightly different problem. With only one boundary condition with variable Temperature. It works!.

FoamFile
{
version 2.0;
format ascii;
class volScalarField;
location "0"; //nuevo//
object T;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

dimensions [0 0 0 1 0 0 0];

internalField uniform 253;

boundaryField
{
inlet
{
type fixedValue;
value uniform 253;
}

outlet
{
type zeroGradient;
}



upperWall
{
type zeroGradient;
}

lowerWall
{
type codedFixedValue;
value $internalField;
redirectType lowerWallBC;

code
#{
vector dir=vector(1,0,0);
scalarField var=patch().Cf()&dir;
scalarField value=var*40+250;
operator==(value);
#}
}
frontAndBack
{
type empty;
}
}

// ************************************************** *********************** //

Thank you for your replying .I can not find any different between your code and mine.And it does not work.
Maybe the trouble is not from the code .Anything else setting should I make ?

Best wishes
lyn

f0208secretx June 6, 2014 21:28

Hello all,

I am running icoFoam on flow over cylinder at Re=120 case and am expecting Karman vortex street. I need a inlet velocity perturbation of any kind to trigger the onset of this shedding event. OpenFOAM 2.3 is used.

I've been trying to implement the linear profile on my inlet velocity boundary conditions but so far no luck. I simply followed the steps and change the initial 0/U file to:
Code:

  inlet
  { 
      type          codedFixedValue;
      value          $internalField;
      redirectType  inletBC;
   
      code #{
                vector dir=vector(0,0,1);
                scalarField var=patch().Cf()&dir;
                scalarField value=var*10+10;
                operator==(value);
          #};
  }

During decomposePar I got the following error:
Code:

Using dynamicCode for patch inlet on field U at line 19 in "/tmp/jhwang/1_cylinder_v10_D0.01_timeVaryBC/0/U.boundaryField.inlet"
Creating new library in "dynamicCode/inletBC/platforms/linux64Gcc48DPOpt/lib/libinletBC_a19a66754309cd299efbab21ef5acdb0b4c99e71.so"
Invoking "wmake -s libso /tmp/jhwang/1_cylinder_v10_D0.01_timeVaryBC/dynamicCode/inletBC"
wmakeLnInclude: linking include files to ./lnInclude
Making dependency list for source file fixedValueFvPatchFieldTemplate.C
/tmp/jhwang/1_cylinder_v10_D0.01_timeVaryBC/0/U.boundaryField.inlet: In member function ‘virtual void Foam::inletBCFixedValueFvPatchVectorField::updateCoeffs()’:
/tmp/jhwang/1_cylinder_v10_D0.01_timeVaryBC/0/U.boundaryField.inlet:31:19: error: no matching function for call to ‘Foam::inletBCFixedValueFvPatchVectorField::operator==(Foam::scalarField&)’
/tmp/jhwang/1_cylinder_v10_D0.01_timeVaryBC/0/U.boundaryField.inlet:31:19: note: candidates are:
In file included from /home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.H:588:0,
                from /home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fixedValueFvPatchField.H:58,
                from /home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fixedValueFvPatchFields.H:29,
                from fixedValueFvPatchFieldTemplate.H:38,
                from fixedValueFvPatchFieldTemplate.C:26:
/home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.C:558:6: note: void Foam::fvPatchField<Type>::operator==(const Foam::fvPatchField<Type>&) [with Type = Foam::Vector<double>]
 void Foam::fvPatchField<Type>::operator==
      ^ 
/home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.C:558:6: note:  no known conversion for argument 1 from ‘Foam::scalarField {aka Foam::Field<double>}’ to ‘const Foam::fvPatchField<Foam::Vector<double> >&’
/home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.C:568:6: note: void Foam::fvPatchField<Type>::operator==(const Foam::Field<Type>&) [with Type = Foam::Vector<double>]
 void Foam::fvPatchField<Type>::operator==
      ^ 
/home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.C:568:6: note:  no known conversion for argument 1 from ‘Foam::scalarField {aka Foam::Field<double>}’ to ‘const Foam::Field<Foam::Vector<double> >&’
/home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.C:578:6: note: void Foam::fvPatchField<Type>::operator==(const Type&) [with Type = Foam::Vector<double>]
 void Foam::fvPatchField<Type>::operator==
      ^
/home/jw969/OpenFOAM/OpenFOAM-2.3.0/src/finiteVolume/lnInclude/fvPatchField.C:578:6: note:  no known conversion for argument 1 from ‘Foam::scalarField {aka Foam::Field<double>}’ to ‘const Foam::Vector<double>&’
make: *** [Make/linux64Gcc48DPOpt/fixedValueFvPatchFieldTemplate.o] Error 1
--> FOAM FATAL IO ERROR:
Failed wmake "dynamicCode/inletBC/platforms/linux64Gcc48DPOpt/lib/libinletBC_a19a66754309cd299efbab21ef5acdb0b4c99e71.so"


file: /tmp/jhwang/1_cylinder_v10_D0.01_timeVaryBC/0/U.boundaryField.inlet from line 19 to line 23.

    From function codedBase::createLibrary(..)
    in file db/dynamicLibrary/codedBase/codedBase.C at line 213.

Does this technique work in OF 2.3? Any idea on the problem? Thanks for any advise.


JHW

ajnewman June 6, 2014 22:01

I think your problem is at var*10+10

You are trying to add one double to a field. I would try generating a scalarField of the same size as var where every element is 10 then adding it to var. If you're not sure how to initialize a new scalarField you can just always just make one by copying var again like scalarField var2 = var; then do int num = var2.size() and loop over the elements of var2 making each one of them 10. I'm fairly certain this is not the best way to do this but it will work.

f0208secretx June 7, 2014 00:52

Quote:

Originally Posted by ajnewman (Post 495986)
I think your problem is at var*10+10

You are trying to add one double to a field. I would try generating a scalarField of the same size as var where every element is 10 then adding it to var. If you're not sure how to initialize a new scalarField you can just always just make one by copying var again like scalarField var2 = var; then do int num = var2.size() and loop over the elements of var2 making each one of them 10. I'm fairly certain this is not the best way to do this but it will work.

I simply tried to remove the +10 part but it still doesn't work. Thanks for the answer but its probably not the problem.

JHW

ajnewman June 7, 2014 01:05

I re-read your post and if I understand correctly this is a velocity boundary condition so operator needs to be set equal to a 3 component vectorField rather than a scalarField.

f0208secretx June 7, 2014 09:58

Hi ajnewman,

Thanks a lot for the reply. I think that is very much the problem.

Can you be so kind and point me to some documentation I can read in order to understand how to write a velocity boundary condition to a patch? Or where is the class vectorField defined? I am new to OpenFOAM and rusty on C++. Thanks

Sincerely,

JHW

ajnewman June 7, 2014 11:37

Assuming that the x_3 component is the one you wish to specify do something like

operator == (vector(0,0,1)*10 + vector(0,0,10));

You may also want to look at

http://www.cfd-online.com/Forums/ope...onditions.html

zahraa June 16, 2014 18:11

Hello,
I am new to openFoam and C++ .I want to use codedFixedValue for a wall with convection boundary condition. for this boundary i have used energy balance and with convection BC i have found following equation.


Ts= c1*(c2* Ta+Ti )

In this equation Ts is the temperature of the boundary and Ta is the ambient temperature and Ti is the internal adjacent cell temperature(internal adjacent cell which is next to the boundary) and c1 and c2 are constant.
now i do not know how i can find Ti and how i should define it here in codedFixedValue . and i should know normal distance between internal adjacent cell center and the boundary, how should i do this?

Tom123 September 8, 2015 07:41

Hello ajnewman,

I found these replies may provide a solution for the problem that I am struggling with, but I need your help as I am inexperienced in C++.

The non uniform inlet boundary condition for my problem could not be expressed in a mathmatical fomula, but only listed in a excel table that provides the coordinates and the corresponding velocity components. It is something like below:
x y z ux uy uz
1 0 0 0.1 0.6 0.8
2.5 0.1 0 0.1 0.5 2
3 0.2 0 0.2 0.7 2.8
...

The first three components tells location (z component is 0 for this surface) and the remaining three components are the velocity components.
I do not know how to apply the data to the inlet boundary condition. Any help would be greatly appreciated.

Tom

Divyaprakash October 5, 2015 03:31

timeVaryingMappedFixedValue
 
Why can't we use timeVaryingMappedFixedValue here? Although I am not too sure what does interpolation in time mean here.

AoKiji August 2, 2017 04:56

TemperatureGradient
 
Hey Guys,

im trying to solve a similar problem. My geometry is a rectengular channel with an inlet and outlet. I want to have a rising temperature on the channel walls along the channel (up and down). This is my BC 0/T:

dimensions [0 0 0 1 0 0 0];

internalField uniform 0;

boundaryField
{
inlet
{
type fixedValue;
value uniform 293.15;
}
outlet
{
type fixedValue;
value uniform 473.15;
}
up
{
type codedFixedValue;
value uniform 383.15;
redirectType UpBC;
code
#{
vector dir=vector(1,0,0);
scalarField var=patch().Cf()&dir;
scalarField value=var*100+273;
operator==(value);
#};
}
down
{
type fixedValue;
value uniform 383.15;
}
front
{
type symmetryPlane;
}
back
{
type symmetryPlane;
}
}

I only typed the code in for the upper wall but it will be the same i think for the lower wall. Now i have the Problem, that the code is compiling and running but it wont do anything while running. The up wall always got the uniform 383.15 temperature. What is wrong with the code? Do i need to start a for(....) argument?

I hope you can help me :)

AoKiji

hakolekar November 14, 2017 22:18

Hi,

I am trying to use a pressureDirectedInletVelocity as an inlet boundary condition!

However, for the same I need a non-uniform inlet angle distribution.

Is that possible in OF2.4?

It keeps on throwing an error -
compound has already been transfered from token
on line 51 the empty compound of type List<vector>


Thank you
Regards,
Harshal


All times are GMT -4. The time now is 10:43.