CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Meshing & Mesh Conversion (https://www.cfd-online.com/Forums/openfoam-meshing/)
-   -   [blockMesh] #codeStream loop inside a blockMeshDict (https://www.cfd-online.com/Forums/openfoam-meshing/151479-codestream-loop-inside-blockmeshdict.html)

francois April 11, 2015 12:08

#codeStream loop inside a blockMeshDict
 
1 Attachment(s)
Hi all,

I would like to use #codeStream to define the points of splines in a blockMeshDict.

Here is the code snippet I use:

Code:

spline 0 1 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$L/(nbPoints-1);
      scalar yi = $Ri - ($Re-$Ri) * (6*pow(xi/$L,5) - 15*pow(xi/$L,4) + 10*pow(xi/$L,3));
        os  << point(xi, -yi, 0) << endl;
        // Info  << point(xi, -yi, 0) << endl;
    }
  #};
  };
  )

The full blockMeshDict file is attached in this post.

I've got this error message:

Code:

--> FOAM FATAL IO ERROR:
Expected a '(' while reading VectorSpace<Form, Cmpt, nCmpt>, found on line 79 the punctuation token ';'

file: /home/beaubert/OpenFOAM/beaubert-2.3.0/run/convergentMarie/convergentCase/constant/polyMesh/blockMeshDict.edges at line 79.

    From function Istream::readBegin(const char*)
    in file db/IOstreams/IOstreams/Istream.C at line 94.

FOAM exiting

The mesh is fine if I directly put the points coordinates (retrieved with Info) into the blockMeshDict.

Any idea ?
Thanks a lot for your help

Happy foaming :)
François

hk318i April 15, 2015 07:18

Just remove the red semicolon ;)



Quote:

Originally Posted by francois (Post 541171)
Hi all,

I would like to use #codeStream to define the points of splines in a blockMeshDict.

Here is the code snippet I use:

Code:

spline 0 1 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$L/(nbPoints-1);
      scalar yi = $Ri - ($Re-$Ri) * (6*pow(xi/$L,5) - 15*pow(xi/$L,4) + 10*pow(xi/$L,3));
        os  << point(xi, -yi, 0) << endl;
        // Info  << point(xi, -yi, 0) << endl;
    }
  #};
  };
  )

The full blockMeshDict file is attached in this post.

I've got this error message:

Code:

--> FOAM FATAL IO ERROR:
Expected a '(' while reading VectorSpace<Form, Cmpt, nCmpt>, found on line 79 the punctuation token ';'

file: /home/beaubert/OpenFOAM/beaubert-2.3.0/run/convergentMarie/convergentCase/constant/polyMesh/blockMeshDict.edges at line 79.

    From function Istream::readBegin(const char*)
    in file db/IOstreams/IOstreams/Istream.C at line 94.

FOAM exiting

The mesh is fine if I directly put the points coordinates (retrieved with Info) into the blockMeshDict.

Any idea ?
Thanks a lot for your help

Happy foaming :)
François


francois April 17, 2015 08:36

1 Attachment(s)
Thank you very much hk318i ! :)

Note for myself: always read twice before posting, especially if it's in front of my nose :D

Here is a working example if someone wants to try this #codeStream feature:

Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  2.3.0                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.org                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.001;

// Geometry parameters

D 44.45; // Pipe diameter
Lc 263.36625; // Length of the contraction
Re #calc "$D/2.0"; // Exit radius
Ri 67.230625; // Inlet radius
H #calc "0.1*$D"; // Depth
Lp 200; // Length of the pipe
Ld #calc "$Lc+$Lp";

// Mesh parameters

Nx 20;
Ny 20;
Nz 1;

Gx 0.5;
Gy 1;
Gz 1;

// Vertices of the geometry

vertices
(

 (0  #calc "-$Ri" 0)  // Point 0
 ($Lc #calc "-$Re" 0)  // Point 1
 ($Lc $Re 0)          // Point 2
 (0  $Ri 0)            // Point 3

 (0  #calc "-$Ri" $H)  // Point 4
 ($Lc #calc "-$Re" $H) // Point 5
 ($Lc $Re $H)          // Point 6
 (0  $Ri $H)          // Point 7

 ($Ld #calc "-$Re" 0)  // Point 8
 ($Ld $Re 0)            // Point 9
 ($Ld #calc "-$Re" $H)  // Point 10
 ($Ld $Re $H)          // Point 11

);

// Blocks definition

blocks
(
 hex (0 1 2 3 4 5 6 7) ($Nx $Ny $Nz) simpleGrading ($Gx $Gy $Gz)
 hex (1 8 9 2 5 10 11 6) ($Nx $Ny $Nz) simpleGrading ($Gx $Gy $Gz)
);

edges
(

 BSpline 0 1 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re - $Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, -yi, 0) << endl;
      Info  << point(xi, -yi, 0) << endl;
    }
  #};
  }
  )

 BSpline 4 5 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re - $Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, -yi, $H) << endl;
      Info  << point(xi, -yi, $H) << endl;
    }
  #};
  }
  )

 BSpline 3 2 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re - $Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, yi, 0) << endl;
      Info  << point(xi, yi, 0) << endl;
    }
  #};
  }
  )

 BSpline 7 6 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re-$Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, yi, $H) << endl;
      Info  << point(xi, yi, $H) << endl;
    }
  #};
  }
  )
);

// Boundaries

boundary
(
 inlet
 {
  type patch;
  faces
    (
      (0 4 7 3)
      );
 }
 outlet
 {
  type patch;
  faces
    (
      (8 10 11 9)
      );
 }
 upperWallUpstream
 {
  type wall;
  faces
    (
      (3 2 6 7)
      );
 }
 lowerWallUpstream
 {
  type wall;
  faces
    (
      (0 1 5 4)
      );
 }
 upperWallDownstream
 {
  type wall;
  faces
    (
      (2 9 11 6)
      );
 }
 lowerWallDownstream
 {
  type wall;
  faces
    (
      (1 8 10 5)
      );
 }
 frontAndBack
 {
  type empty;
  faces
    (
      (0 1 2 3)
      (4 5 6 7)
      (1 8 9 2)
      (5 10 11 6)
      );
 }
 );

mergePatchPairs
(
);

And the result is attached below

hk318i April 17, 2015 09:44

Don't worry, it happens with every foamer :)
I have only one comment about your code, which could be useful for someone else in the future. Instead of repeating the code for each edge, you can use the codeStream directly inside edges.

Code:

edges (#codeStream {
  code
  #{
        Calculate all the points....;
      Then
          os << "edgetype1 A B" << pointsList ;
          os << "edgetype2 A B" << pointsList ;
 
  #}
}
);

or you can define new function in separate file myfun.H and include it the you call the function in codeStream.

Code:

#codeStream
{
    codeInclude
    #{
        #include "pointField.H"
        #include "myfun.H"
    #};
            codeOptions
            #{
                -I$(FOAM_CASE)/constant/polyMesh        <-- location of myfun.H
            #};
            codeLibs
            #{

            #};
    code
    #{
          type y = myfunName(inputs);
  os <<  ........ ;
    #}


myfun.H

Code:


using namespace Foam;
type myfunName(type inputs)
{

  forAll(y, i)
  {
      y[i] = function;
  }

  return y;

}



Hopefully these tips will be useful for someone coming directly from google search.




Best wishes,
Hassan





Quote:

Originally Posted by francois (Post 542302)
Thank you very much hk318i ! :)

Note for myself: always read twice before posting, especially if it's in front of my nose :D

Here is a working example if someone wants to try this #codeStream feature:

Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  2.3.0                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.org                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.001;

// Geometry parameters

D 44.45; // Pipe diameter
Lc 263.36625; // Length of the contraction
Re #calc "$D/2.0"; // Exit radius
Ri 67.230625; // Inlet radius
H #calc "0.1*$D"; // Depth
Lp 200; // Length of the pipe
Ld #calc "$Lc+$Lp";

// Mesh parameters

Nx 20;
Ny 20;
Nz 1;

Gx 0.5;
Gy 1;
Gz 1;

// Vertices of the geometry

vertices
(

 (0  #calc "-$Ri" 0)  // Point 0
 ($Lc #calc "-$Re" 0)  // Point 1
 ($Lc $Re 0)          // Point 2
 (0  $Ri 0)            // Point 3

 (0  #calc "-$Ri" $H)  // Point 4
 ($Lc #calc "-$Re" $H) // Point 5
 ($Lc $Re $H)          // Point 6
 (0  $Ri $H)          // Point 7

 ($Ld #calc "-$Re" 0)  // Point 8
 ($Ld $Re 0)            // Point 9
 ($Ld #calc "-$Re" $H)  // Point 10
 ($Ld $Re $H)          // Point 11

);

// Blocks definition

blocks
(
 hex (0 1 2 3 4 5 6 7) ($Nx $Ny $Nz) simpleGrading ($Gx $Gy $Gz)
 hex (1 8 9 2 5 10 11 6) ($Nx $Ny $Nz) simpleGrading ($Gx $Gy $Gz)
);

edges
(

 BSpline 0 1 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re - $Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, -yi, 0) << endl;
      Info  << point(xi, -yi, 0) << endl;
    }
  #};
  }
  )

 BSpline 4 5 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re - $Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, -yi, $H) << endl;
      Info  << point(xi, -yi, $H) << endl;
    }
  #};
  }
  )

 BSpline 3 2 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re - $Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, yi, 0) << endl;
      Info  << point(xi, yi, 0) << endl;
    }
  #};
  }
  )

 BSpline 7 6 ( #codeStream
 {
  codeInclude
  #{
      #include "pointField.H"
  #};

  code
  #{
    label nbPoints = 20;
    for (label i = 0; i < nbPoints; i++)
    {
      scalar xi = 0 + i*$Lc/(nbPoints-1);
      scalar yi = $Ri + ($Re-$Ri) * (6*pow(xi/$Lc,5) - 15*pow(xi/$Lc,4) + 10*pow(xi/$Lc,3) );
      os  << point(xi, yi, $H) << endl;
      Info  << point(xi, yi, $H) << endl;
    }
  #};
  }
  )
);

// Boundaries

boundary
(
 inlet
 {
  type patch;
  faces
    (
      (0 4 7 3)
      );
 }
 outlet
 {
  type patch;
  faces
    (
      (8 10 11 9)
      );
 }
 upperWallUpstream
 {
  type wall;
  faces
    (
      (3 2 6 7)
      );
 }
 lowerWallUpstream
 {
  type wall;
  faces
    (
      (0 1 5 4)
      );
 }
 upperWallDownstream
 {
  type wall;
  faces
    (
      (2 9 11 6)
      );
 }
 lowerWallDownstream
 {
  type wall;
  faces
    (
      (1 8 10 5)
      );
 }
 frontAndBack
 {
  type empty;
  faces
    (
      (0 1 2 3)
      (4 5 6 7)
      (1 8 9 2)
      (5 10 11 6)
      );
 }
 );

mergePatchPairs
(
);

And the result is attached below


francois April 17, 2015 10:03

Thanks Hassan for your kind and very relevant suggestions. :)

I was thinking myself of refactoring the code which was submitted here only as proof of concept for myself or other newcomers to #codeStream.

Anyway, those are indeed very nice additions to put into the code, thanks !
I may put all this stuff on the wiki when I'll find the time.

You're a good example that illustrates why I like so much the OpenFOAM community.
Happy foaming :)

hk318i April 29, 2015 13:25

Hello!
I hit one of the codeStream limitations today. I would like to share it with everyone here.
Code:

string "
        int N = 100;
        scalar cw = $cw*$ftTom; // chord
        scalar..."
    is too long (max. 8000 characters)

Although I used the method which I mentioned above, I had to write a very very long codeStream. Fortunately, I found a solution for this problem which is dividing the code stream to many .H files and includes directly inside the code not as a header file.

Code:

    code
    #{
#include "myLongCode.H"
    #};

BUT, there is a drawback for this method, you cannot use the macro substitutions ($parameter). I desperately tried "#inputMode merge" but it doesn't work.
So to read any variable from the blockMeshDict in this case, you have to lookup it.

Code:

scalar a = readScalar(dict.lookup("a"));
Next time, I have to FOAM responsibly :)

arieljeds October 1, 2015 07:07

On using codestream... I understand the syntax to duplicate points but I want to then see the points so I can construct the blocks... Maybe this is a stupid question but I'm very very new to CFD and meshing so I don't understand how, once I've duplicated the points, I "know" where each one is and how the block structure should be using the new points... can anyone advise on the best practice for this?

hk318i October 1, 2015 09:09

Quote:

Originally Posted by arieljeds (Post 566071)
On using codestream... I understand the syntax to duplicate points but I want to then see the points so I can construct the blocks... Maybe this is a stupid question but I'm very very new to CFD and meshing so I don't understand how, once I've duplicated the points, I "know" where each one is and how the block structure should be using the new points... can anyone advise on the best practice for this?

You can use
Code:

paraFoam -block
It shows the points so you could create the blocks which is really helpful.
I am not sure if that what you are looking for or not. Maybe you mean if you have list called points and you want use points[5] in blocks.
In this case, based on my experience, you cannot do that directly because the variables are limited to codeStream scope.

BUT there is a way around this problem which is including the blocks section inside the same codeStream as points. Then use os stream to print blocks as well. Or you can write a script (using python or octave or m4 .) to create blockMesh file.

arieljeds October 1, 2015 09:18

Hi there,

Thanks for that. Actually I wasn't sure if that would work without running blockMesh first...

Ok just tried and how is it possible to do this without first building the blocks? Or do I just put:

Code:

blocks
(
);

And leave the blocks blank?

hk318i October 1, 2015 09:26

It works without executing blockMesh, just make sure that boundary is empty as well. It will show you the points and edges

arieljeds October 1, 2015 09:28

perfect! Thanks for that... very difficult to find something so simple online!

arieljeds October 1, 2015 09:55

Sorry, last question.. say I'm trying to duplicate both the z points (as done in the cylinder tutorial) and the y points. I tried to just include a second loop as follows:

Code:

label sy = points.size();
points.setSize(2*sy);
for (label i = 0; i < sy; i++)
{
    const point& pt = points[i];
    points[i+sy] = point(pt.x(), -pt.y(), pt.z());
}

os << points;

But this (perhaps obviously) didn't work... Can you offer any guidance on the correct syntax to use?

hk318i October 2, 2015 03:26

What is pt? This expression looks wrong.

arieljeds October 2, 2015 05:23

I took that directly from the cylinder tutorial (uses potentialFoam) but I believe pt the name of the pointer that points to the location of that point?

hk318i October 2, 2015 05:36

Sorry, I did see the first expression in the loop. The code should work without errors.

arieljeds October 2, 2015 05:38

The code works without errors when I have the second loop to duplicate the y-values but it doesn't actually duplicate the y-values. It does duplicate the z-values successfully but I'm not sure why it isnt' fully working to duplicate everything. Any thoughts?

hk318i October 2, 2015 05:41

Try to print the points to see the values.
Code:

Info << points << endl;

ninoleum October 22, 2015 15:43

Why negative volumes?
 
2 Attachment(s)
Hi all,
taking inspiration from this thread, I tried to generate my geometry with #codestream directive inside blockMeshDict (the method is really smart indeed and overcomes the difficulty of "manual meshing - the trappist way :rolleyes: " with plain text blockMeshDict, so thanks Francois and Hassan for sharing this conversation!).
The mesh I obtain is apparently correct but if I run checkMesh against it, the situation is much different:
Code:

/*---------------------------------------------------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  2.3.0                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.org                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
Build  : 2.3.0-f5222ca19ce6
Exec  : checkMesh -constant
Date  : Oct 22 2015
Time  : 21:18:35
Host  : "rocchigi1npge"
PID    : 2009
Case  : /home1/rok/Cases/helix
nProcs : 1
sigFpe : Enabling floating point exception trapping (FOAM_SIGFPE).
fileModificationChecking : Monitoring run-time modified files using timeStampMaster
allowSystemOperations : Allowing user-supplied system call operations

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create time

Create polyMesh for time = constant

Time = constant

Mesh stats
    points:          63529
    faces:            178576
    internal faces:  167024
    cells:            57600
    faces per cell:  6
    boundary patches: 1
    point zones:      0
    face zones:      0
    cell zones:      2

Overall number of cells of each type:
    hexahedra:    57600
    prisms:        0
    wedges:        0
    pyramids:      0
    tet wedges:    0
    tetrahedra:    0
    polyhedra:    0

Checking topology...
    Boundary definition OK.
 ***Total number of faces on empty patches is not divisible by the number of cells in the mesh. Hence this mesh is not 1D or 2D.
    Cell to face addressing OK.
    Point usage OK.
    Upper triangular ordering OK.
    Face vertices OK.
    Number of regions: 1 (OK).

Checking patch topology for multiply connected surfaces...
    Patch              Faces    Points  Surface topology                 
    defaultFaces        11552    11554    ok (closed singly connected)     

Checking geometry...
    Overall domain bounding box (-0.07529996 -0.008 -0.07529993) (0.07529996 0 0.0753)
    Mesh (non-empty, non-wedge) directions (0 0 0)
    Mesh (non-empty) directions (0 0 0)
 ***Number of edges not aligned with or perpendicular to non-empty directions: 122876
  <<Writing 63529 points on non-aligned edges to set nonAlignedEdges
    Boundary openness (-4.773914e-18 -8.73355e-16 6.456257e-18) OK.
    Max cell openness = 3.404553e-16 OK.
    Max aspect ratio = -1 OK.
    Minimum face area = 1.547883e-08. Maximum face area = 1.802198e-05.  Face area magnitudes OK.
 ***Zero or negative cell volume detected.  Minimum negative volume: -5.256213e-09, Number of negative volume cells: 57600
  <<Writing 57600 zero volume cells to set zeroVolumeCells
    Mesh non-orthogonality Max: 180 average: 178.9199
 ***Number of non-orthogonality errors: 167024.
  <<Writing 167024 non-orthogonal faces to set nonOrthoFaces
 ***Error in face pyramids: 345600 faces are incorrectly oriented.
  <<Writing 178576 faces with incorrect orientation to set wrongOrientedFaces
    Max skewness = 0.1232568 OK.
    Coupled point location match (average 0) OK.

Failed 4 mesh checks.

End

Please note that checkMesh complains about the negative volume for each and every one of the 57600 cells which compose the mesh and about the non-orthogonality of each and every of the 167024 internal faces.
I checked several time the definition of each block for the vertexes sequence without finding any error.
I attached here the blockMeshDict for your reference.
So: what I am doing wrong?

Thank You

Gianluca

wyldckat October 22, 2015 15:49

Quick answer: Negative volume is usually related to the vertices being order in the wrong direction.

hk318i October 23, 2015 07:12

1 Attachment(s)
I totally agree with Bruno, most probably one there is a block is not following the right had rule.

I tried to run your code using OpenFOAM2.3.x and I got few errors. I don't know if you are facing the same errors or not. I had to modify minor things to run it. I tested it also on OpenFOAM-dev hoping that the new updates will overcome your problem but unfortunately not. The new updates are related to boundary definition only.

I attached the modified file here in case you needed it. I just modified the x and y type to scalarField. Also changed the int to label (which is exactly the same (just a habit)).

Best Wishes
Hassan


All times are GMT -4. The time now is 06:52.