Contrib/funkySetFields

From OpenFOAMWiki
< Contrib
Revision as of 17:42, 2 February 2007 by Bgschaid (Talk | contribs)

Valid versions: OF version 12.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 be used to set non-uniform intial-conditions without programming.

This utility is still under development and should only be considered as a Beta-Version

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

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.

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 Command line usage

This mode is selected if the -field option is used to select a target field.

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 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)

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
div 
divergence of a vector-field
min,max 
minimum and maximum of a scalar field

These pseudo-functions are defined:

pos 
Vector field with the cell-centers
dist 
Scalar field that gives the distance to the nearest wall (using wallDist)
rand 
Scalars-field with random numbers from [0,1]
randNormal 
Random-number scalar field that is Gauss-distributed

5 Technical

  • the utility only works on volScalar- and volVectorFields.
  • no dimension checking is done (but the dimension of the target field is preserved)

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

funkySetFields.tar.gz: The tar file with the sources

funkySetFields-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.pngfunkySetFields.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

  • create a new field from an expression
  • 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

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