Contrib/funkySetFields

From OpenFOAMWiki
< Contrib
Revision as of 10:55, 14 September 2007 by Bgschaid (Talk | contribs)

Valid versions: OF version 13.png OF version 14.png


1 Short description

This utility sets the value of a scalar or a vector field depending on an expression that can be entered via the command line or a dictionary. It can also be used to set the value of fields on selected patches. It's like the setFields-utility on steroids.

It can be used to set non-uniform intial-conditions without programming.

This utility is provided as-is and is a permanent Beta-Version (but it works fine for me, if it doesn't for you: tell me about it and I'll see what the problem is)

1.1 Words of warning

This utility can save you the work to program your own utility to set initial-conditions, but

  • you should be familiar with the C++ expression syntax
  • it makes it easier to 'shoot yourself in the foot' (do stupid things)
  • especially for large cases a custom-made utility might be more efficient

(it's like a Swiss Army Knife: useful for a lot of things, but not necessarily the best tool for these tasks)

2 Pre-requisites

To compile this utility at least version 2.1 of Bison has to be installed. Check with

bison -V

on the command line before trying to compile it.

2.1 For those who don't have bison 2.1

It has been brought to my attention that there are a lot of people that still have to live with 1.x bisons. For those there is a script provided that inserts the generated sources. Just go to the source directory and call

./insertGenerated.sh

It copies the generated files to the directory and modifies Make/files

3 Usage

The utility can be used from the command line or with a dictionary. In both forms an option -time is needed to select the time step.

3.1 Basic ommand line usage

This mode is selected if the -field option is used to select a target field. If the target doesn't yet exist the option -create has to be added.

In addition an option -expression is needed with the expression that is to be written to the target field (Syntax see below).

An optional option is -condition: only cells for which this expression evaluates to true are overwritten with the expression. For all other cells the old value is kept.

expression and condition should be enclosed in quotes to keep the shell from interpreting special characters.

An example of the usage would be:

funkySetFields . cavity -field U -expression '(grad(dist())^vector(0,0,-1))*mag(pos()-vector(0.05,0.05,0))/0.05'  -time 0 

This sets the velocity field to a 'circle' around the center. In the most simple case the velocity field can be set to zero

funkySetFields . cavity -field U -expression 'vector(0,0,0)' -time 0

Same for the pressure field:

funkySetFields . cavity -field p -expression '0' -time 0

The damBreak-tutorial could be initialized with

funkySetFields . damBreak -time 0 -field gamma -expression " pos().x <= 0.1461 && pos().y <= 0.292 ? 1 : 0"

or (if you don't want to overwrite the whole gamma field):

funkySetFields . damBreak -time 0 -field gamma -expression 1 -condition "pos().x <= 0.1461 && pos().y <= 0.292"

3.2 Advanced command line usage

For a newly created field the dimension can be specified with the -dimension-option.

Usually all the patches of the field (not the symmetry/wedge/...-stuff) are set to the type zeroGradient. With the option -keepPatches the patches are preserved from what was previously on the disk.

The option -valuePatches specifies a list of patches which shall be set to fixed value. The values are taken from the interior of the field.

For instance to get a similar behaviour as the setHotRoom that sets the boundary conditions for the hotRoom-tutorial these commands are issued:

funkySetFields . hotRoom -time 0 -field T -keepPatches -valuePatches "floor ceiling" -condition "pos().x>4.5 && pos().x<5.5 && pos().z>4.5 && pos().z<5.5 && pos().y<2" -expression "600."
funkySetFields . hotRoom -time 0 -field T -keepPatches -expression "300."

The first one sets the values on the patches, the second one clears the interior field.

3.3 Dictionary usage

In the dictionary funkySetFieldsDict a list of dictionaries named expressions is read and one dictionary is evaluated after another. The title of the dictionaries is only used for documentation. In each dictionary there can be 3 entries:

field
the target field
expression
the expression to write to the field
condition
select a subset of the cells (this is optional)
keepPatches
see command line options (optional)
create
see command line options (optional)
valuePatches
see command line options (optional)
dimesion
see command line options (optional)

An example dictionary would be:

 
 expressions
 (
	circleVel
	{
		field U;
		expression "(grad(dist())^vector(0,0,-1))*mag(pos()-vector(0.05,0.05,0))/0.05";
        }
	pressure1
    	{
		field p;
		expression "10.*(0.1-pos().y)";
	}
	pressure2
	{
		field p;
		expression "p+U&U";
		condition "pos().x > (max(pos().x)-min(pos().x))/2";
	}
 );

This sets the velocity field as in the command-line example and then sets the pressure field to a rather strange value.

4 Expression syntax

The most complete documentation of the expression syntax is the source file for the Bison-grammar (*.yy and *.ll). Sorry.

These C++ operators are implemented:

+,-,*,/ 
Arithmetic operators. Can be used for vectors and scalars (only if useful. For instance: vectors can't be added to scalars)
&,^ 
The vector operators as defined by OpenFOAM
<,>,<=,>=,!=,== 
Comparison operators (only defined for scalars)
&&,|| 
Logical Operators
Conditional operator
The conditional operator ( test ? val1 : val2) is defined for scalars and vectors

Operator precedence should be the same as for C++.

All the fields in the current time-step can be used. If the field is also the target field the old value is used. Then the field is written using the new value.

These pseudo-variables are defined:

pi 
Guess ;)

These functions are defined:

pow,log,exp,sqr,sqrt,sin,cos,tan 
Only defined for scalars
mag 
defined for scalars and vectors
grad 
gradient of a scalar-field
curl 
curl of a vector-field
snGrad
surface normal gradient
div 
divergence of a vector-field
laplaction 
laplacian of a field (with an optional "coefficient"-field)
min,max 
minimum and maximum of a scalar field
average,integrate,sum,reconstruct 
reconstruct a face field (yielding a volume field)

These pseudo-functions are defined:

pos 
Vector field with the cell-centers
fpos 
face-vectorField with the positions of the faces
face 
surface field with the face-vectors
area 
surface field with the face-areas
dist 
Scalar field that gives the distance to the nearest wall (using wallDist)
rdist
a field with the distances from a given vector (save mag(pos()-v))
rand 
Scalars-field with random numbers from [0,1]
randNormal 
Random-number scalar field that is Gauss-distributed
vol 
Volume of the cells
deltaT 
a field that returns the time-step
time 
a field that returns the current time

5 Technical

  • the utility only writes volScalar- and volVectorFields. It can read and operate on surfaceFields
  • no dimension checking is done (but the dimension of the target field is preserved, if not overwritten by the option)

5.1 Known Bugs

This utility is still under development. Therefore there is a number of known bugs (and also some unknown ones)

  • Numbers without a decimal point are not correctly recognized
  • Subtracting two numbers does not work (but subtracting fields does)

6 Download

The most recent version of the sources can always be downloaded via Subversion:

svn checkout https://openfoam-extend.svn.sourceforge.net/svnroot/openfoam-extend/trunk/Breeder/utilities/postProcessing/FunkySetFields/

These are the tarballs of the sources: funkySetFields.r7568.tar.gz: The tar file with the sources and included generated sources. Works with 1.4
funkySetFields.r7241.tar.gz: The last version that works with 1.3

6.1 Ancient history

funkySetFields.tar.gz: The original Version that worked with OF version 13.pngfunkySetFields-generatedSources.diff.gz: The diff file with bison-generated (by bison-2.1) sources and patches to compile funkySetFields under OpenFOAM-1.3 and without bison 2.1 (the current bison version 2.3 generates .C files which can't be compiled by gcc-4.1.0 included in OpenFOAM-1.3). OF version 13.png (To Bgschaid: Feel free to remove this link upon completion of the new release. 7islands 00:47, 18 Feb 2007 (CET))

funkySetFields.r7106.tar.gz: A new version of funkySetFields. Not described yet on this page. Some of the new features include

  • new fields are created
  • ability to write fixedValue-patches
  • work with cell sets
  • work with surfaceFields (on writing)

An update of this page will follow in the next days. The generated files for those laking the right bison-Version will also be added OF version 13.png

7 Plans

  • factor out the parsing in a library to make it usable from other utilities
  • make expression evaluation more efficient

8 History

  • 2006-03-26: First upload of the utility
  • 2007-02-02: New Version uploaded. Has more features than described on this page
  • 2007-03-02: New Version with included generated Files uploaded and new features descibed
  • 2007-06-20: New Version that works with 1.4 and addtions by Takuya Oshima (rdist and deltaT)

--Bgschaid 22:53, 26 Mar 2006 (CEST)