Difference between revisions of "HowTo blockMesh with m4"

From OpenFOAMWiki
(Did some formatting fixes and some tuning up)
 
Line 1: Line 1:
 
=== How to quickly change the mesh with blockMesh ===
 
=== How to quickly change the mesh with blockMesh ===
  
The Gnu preprocessor <b>m4</b> is handy for parametric refinement to the mesh. It is also possible to change position of multiple vertices at the same time, thus reducing the possibility of errors when altering the mesh.  
+
The GNU preprocessor <b>m4</b> is handy for parametric refinement to the mesh. It is also possible to change position of multiple vertices at the same time, thus reducing the possibility of errors when altering the mesh.  
  
 
Example:
 
Example:
<python>
+
<pre>// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
+
 
version  1.2;
 
version  1.2;
    `format'          ascii;
+
format   ascii;
  
 
root    "";
 
root    "";
Line 36: Line 35:
  
 
convertToMeters 1;
 
convertToMeters 1;
</python>
+
</pre>
The first lines before the commented section is just the foam header, which needs to be a part of every blockMesh file. The actual m4-coding start at the four line of codes where we define a calculator, sets a variable <i>VCOUNT = 0</i> and defines a new way of defining vertices. The last four are for defining 2D cells, as well as patches for the boundaries. The b is short for the bottom and t for top, that is the bottom and top of the 2D cell set. Foam also requires a <i>convertToMeters</i> value. 
+
  
So imagine we want to create a 2D channel with an inlet outlet and wall boundary conditions, first off we define the coordinates of the channel:
+
The first lines before the commented section is just the FOAM header, which needs to be a part of every {{tt|blockMeshDict}} file. The actual m4-coding starts at the fourth line of code where we define a calculator, sets a variable ''VCOUNT = 0'' and defines a new way of defining vertices. The last four are for defining 2D cells, as well as patches for the boundaries. The b is short for the bottom and t for top, that is the bottom and top of the 2D cell set. Foam also requires a {{tt|convertToMeters}} value. 
  
<cpp>
+
So imagine we want to create a 2D channel with an inlet outlet and wall boundary conditions, first off we define the coordinates of the channel:
define(side, 100.0)
+
<pre>define(side, 100.0)
 
define(height, 300.0)
 
define(height, 300.0)
 
define(nrcellsx, 10)
 
define(nrcellsx, 10)
Line 53: Line 51:
 
define(Z1, 0.0)
 
define(Z1, 0.0)
 
define(Z2, 1.0)
 
define(Z2, 1.0)
</cpp>
+
</pre>
  
 
Then we want to define the actual vertices:
 
Then we want to define the actual vertices:
<cpp>
+
<pre>vertices
 
+
(
 
+
vertices
+
    (
+
 
     (origox origoy Z1) vlabel(a1b)
 
     (origox origoy Z1) vlabel(a1b)
 
     (coordinatesidex origoy Z1) vlabel(a2b)
 
     (coordinatesidex origoy Z1) vlabel(a2b)
Line 70: Line 65:
 
     (coordinatesidex coordinatesidey Z2) vlabel(a3t)
 
     (coordinatesidex coordinatesidey Z2) vlabel(a3t)
 
     (origox coordinatesidey Z2) vlabel(a4t)
 
     (origox coordinatesidey Z2) vlabel(a4t)
    )
+
);
</cpp>
+
</pre>
  
 
We can now define the block:
 
We can now define the block:
 
+
<pre>blocks
<cpp>
+
blocks
+
 
(
 
(
 
     // block0
 
     // block0
Line 83: Line 76:
 
     simpleGrading (1 1 1)
 
     simpleGrading (1 1 1)
 
);
 
);
 +
</pre>
  
</cpp>
 
 
Then, the boundary conditions is simply:
 
Then, the boundary conditions is simply:
 
+
<pre>patches
<cpp>
+
patches
+
 
+
 
(
 
(
 
     patch inlet
 
     patch inlet
Line 111: Line 101:
 
     )
 
     )
  
     wall
+
     wall wall
    wall
+
 
     (
 
     (
 
quad2D(a1,a4)
 
quad2D(a1,a4)
Line 118: Line 107:
 
     )
 
     )
 
);
 
);
 +
</pre>
  
</cpp>
+
When the file is finished, assuming it's called {{tt|blockMeshDict.m4}}, one simply types:
 +
<bash>m4 blockMeshDict.m4 > blockMeshDict</bash>
  
When the file is finished, assuming it's called blockMeshDict.m4, one simply types
+
Before running <tt>blockMesh</tt> as usual.
  
<i>m4 blockMeshDict.m4 > blockMeshDict</i>
+
When looking at the examples provided in OpenFOAM's {{tt|tutorials}} folder, make sure to have a look into the script {{tt|Allrun}}, to see how those tutorials use '''m4'''.
 
+
Before running <tt>blockMesh</tt> as usual.
+
  
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]
 
[[Category:Mesh generation utilities]]
 
[[Category:Mesh generation utilities]]

Latest revision as of 14:33, 19 April 2014

How to quickly change the mesh with blockMesh

The GNU preprocessor m4 is handy for parametric refinement to the mesh. It is also possible to change position of multiple vertices at the same time, thus reducing the possibility of errors when altering the mesh.

Example:

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
version  1.2;
format   ascii;

root     "";
case     "";
instance "";
local    "";

class dictionary;
object blockMeshDict;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

changecom(//)changequote([,])

define(calc, [esyscmd(perl -e 'printf ($1)')])

define(VCOUNT, 0)

define(vlabel, [[// ]Vertex $1 = VCOUNT define($1, VCOUNT)define([VCOUNT], incr(VCOUNT))])


define(hex2D, hex ($1b $2b $3b $4b $1t $2t $3t $4t))

define(quad2D, ($1b $2b $2t $1t))

define(frontQuad, ($1t $2t $3t $4t))

define(backQuad, ($1b $4b $3b $2b))

convertToMeters 1;

The first lines before the commented section is just the FOAM header, which needs to be a part of every blockMeshDict file. The actual m4-coding starts at the fourth line of code where we define a calculator, sets a variable VCOUNT = 0 and defines a new way of defining vertices. The last four are for defining 2D cells, as well as patches for the boundaries. The b is short for the bottom and t for top, that is the bottom and top of the 2D cell set. Foam also requires a convertToMeters value.

So imagine we want to create a 2D channel with an inlet outlet and wall boundary conditions, first off we define the coordinates of the channel:

define(side, 100.0)
define(height, 300.0)
define(nrcellsx, 10)
define(nrcellsy, 30)
define(origox, 0.0)
define(origoy, 0.0)
define(coordinatesidex, calc(origox+side))
define(coordinatesidey, calc(origoy+height))

define(Z1, 0.0)
define(Z2, 1.0)

Then we want to define the actual vertices:

vertices
(
    (origox origoy Z1) vlabel(a1b)
    (coordinatesidex origoy Z1) vlabel(a2b)
    (coordinatesidex coordinatesidey Z1) vlabel(a3b)
    (origox coordinatesidey Z1) vlabel(a4b)

    (origox origoy Z2) vlabel(a1t)
    (coordinatesidex origoy Z2) vlabel(a2t)
    (coordinatesidex coordinatesidey Z2) vlabel(a3t)
    (origox coordinatesidey Z2) vlabel(a4t)
);

We can now define the block:

blocks
(
    // block0
    hex2D(a1, a2, a3, a4)
    (nrcellsx nrcellsy 1)
    simpleGrading (1 1 1)
);

Then, the boundary conditions is simply:

patches
(
    patch inlet
    (
	quad2D(a1,a2)
    )

    patch outlet
    (
	quad2D(a3,a4)
    )	

    empty back
    (
	backQuad(a1,a2,a3,a4)
    )

    empty front
    (
	frontQuad(a1,a2,a3,a4)
    )

    wall wall
    (
	quad2D(a1,a4)
	quad2D(a2,a3)
    )
);

When the file is finished, assuming it's called blockMeshDict.m4, one simply types:

m4 blockMeshDict.m4 > blockMeshDict

Before running blockMesh as usual.

When looking at the examples provided in OpenFOAM's tutorials folder, make sure to have a look into the script Allrun, to see how those tutorials use m4.