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] Writing a blockMeshDict file with variables (https://www.cfd-online.com/Forums/openfoam-meshing/164502-writing-blockmeshdict-file-variables.html)

arieljeds December 23, 2015 07:39

Writing a blockMeshDict file with variables
 
Hi there,
I'm trying to write a blockmesh file using variables for the vertices and the number of cells but I'm not quite sure how to do this. At the moment, I'm trying this:

Code:

convertToMeters 1;

x  20.0;        // Length of tank
y1  -3.0;        // Width of tank/2
y2  3.0;        // Width of tank/2
zf  -0.4;        // Water depth
za  0.2;            // Distance above free surface
L    3.6942;    // Wavelength
n    5;                // Number of cells per wavelength

xn  ($x/$L)*$n;        // Calculating the number of cells
//yn   
//zn

vertices       
(
    ( 0  $y1  $zf )        // 0               
    ( $x  $y1  $zf )    // 1
    ( $x  $y2  $zf )        // 2
    ( 0  $y2  $zf )        // 3
 
    ( 0  $y1  $za )        // 4
    ( $x  $y1  $za )        // 5
    ( $x  $y2  $za )        // 6
    ( 0  $y2  $za )        // 7   
);

blocks
       
(
  hex (0 1 2 3 4 5 6 7) ($xn 10 10) simpleGrading (1 1 1) //(1 10 0.1)
);

edges         
(
);

boundary       
(
  inlet
  {
      type patch;
      faces
          (
                ( 0 3 7 4 )
          );
  }
 
  outlet
  {
      type patch;
      faces
          (
          ( 1 2 6 5 )
          );
  }
 
  atmosphere
  {
      type patch;
      faces
          (
          ( 4 5 6 7 )
          );
  }
 
  front
  {
      type symmetryPlane;
      faces
          (
                ( 3 2 6 7 )
        );
  }

Perhaps unsurprisingly, I'm running into problems with the line where I am attempting to calculate xn. Does anyone know the syntax how I can do this? Or if it's possible at all to do a simple calculation in the blockMeshDict?

Thanks in advance for any advice

alexeym December 23, 2015 07:44

Hi,

Code:

$ cd $FOAM_TUTORIALS
$ grep -r '#calc' *
incompressible/simpleFoam/pipeCyclic/system/blockMeshDict:radHalfAngle    #calc "degToRad($halfAngle)";
incompressible/simpleFoam/pipeCyclic/system/blockMeshDict:y              #calc "$radius*sin($radHalfAngle)";
incompressible/simpleFoam/pipeCyclic/system/blockMeshDict:minY            #calc "-1.0*$y";
incompressible/simpleFoam/pipeCyclic/system/blockMeshDict:z              #calc "$radius*cos($radHalfAngle)";
incompressible/simpleFoam/pipeCyclic/system/blockMeshDict:minZ            #calc "-1.0*$z";
multiphase/interDyMFoam/ras/floatingObject/constant/dynamicMeshDict:    mass            #calc "$rho*$Lx*$Ly*$Lz";
$ less incompressible/simpleFoam/pipeCyclic/system/blockMeshDict
...
//- Half angle of wedge in degrees
halfAngle 45.0;

//- Radius of pipe [m]
radius 0.5;


radHalfAngle    #calc "degToRad($halfAngle)";
y              #calc "$radius*sin($radHalfAngle)";
minY            #calc "-1.0*$y";
z              #calc "$radius*cos($radHalfAngle)";
minZ            #calc "-1.0*$z";
...


arieljeds December 23, 2015 07:49

@alexeym - Thanks a lot. That's what I was looking for

arieljeds December 23, 2015 07:58

Ok one more question on this.. Because the calculation I am making is going to be for the number of cells, I need to convert it to an integer. I'm having trouble finding an example of how to do this.

Any ideas?

Thanks again!

alexeym December 23, 2015 08:11

Well, if you take a look into src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.H, there is a note:

Code:

Note
    Internally this is just a wrapper around codeStream functionality - the
    #calc string gets used to construct a dictionary for codeStream.

So you can use usual C++ functions, for example, std::floor or std::ceil.

arieljeds December 23, 2015 08:17

Hi Alexey, thanks again for getting back to me so quickly. Ok so as I'm understanding it, the use of calc would be:

Code:

x    20.0;
n = 5;
L = 3.69;

xn  #calc "($x/$L)*n";
xn1 #calc "std::floor(float $xn)";

...

blocks
{
    hex ( 0 1 2 3 4 5 6 7 ) (xn1 10 10) simpleGrading (1 1 1)
}

...

Is this correct?

alexeym December 23, 2015 08:34

Just

Code:

std::floor($xn)
If we take an example from tutorials (incompressible/simpleFoam/pipeCyclic) and add the lines:

Code:

test            #calc "std::floor($halfAngle/4)";
#calc "Info<< $test << endl";

output of blockMesh would be

Code:

... codeStream object compilation output ...
11
Creating curved edges
Creating topology blocks
...

So in your expressions you forget $ before n and used unnecessary float in function call.

arieljeds December 23, 2015 08:35

Hi Alexey,

I should've written back quicker... I messed around with it a bit and found my way to that syntax and have it working now.

thanks a lot again for your help!

Ariel

Luchini December 14, 2016 11:25

Hello,

I was trying to implement something similar, but i have problems with negative variables.
It seems i can decleare them but as soon i use them in the calc expression the blockMesh gives me an error.

So in

Code:

x1 -30;
//x1 30;
x2 50;

dx #calc "$x1";
//dx 10;

If x1 is declared as positive, everything is ok.
If x1 is declared as negative and dx =10 , everything is ok.
if x1 is declared as negative and dx as ´ #calc "$x1";´ i get an error.

Does anybody knows the reason and a work around?

Note that the real operation that i would like to do is something like

Code:

nx #calc "std::floor( ($x2-$x1)*$n )";

Thank you in advance.

Antimony December 14, 2016 21:17

Hi,

You need to have a space between the variables and the operator for it to recognize it as subtraction.

Instead of: $x2-$x1

So it should be: $x2 - $x1

Not sure if this is documented, but this was what I found out when I was playing around with this feature.

Hope this helps.

Cheers,
Antimony

Luchini December 15, 2016 05:47

Correct,

This worked.

Thank you

LeeRuns July 20, 2017 19:21

Using Regular C++ syntax to write Blockmesh file
 
hello everyone,
if i wanted to use regular C++ or objective C syntax to organize my code could I do that?
for example
Code:


class cPlane
{ // whatever you need
};

class cSphere
{
int MyVariable;
// whatever else you need
};

class cObject
{ cPlane MyPlane;
  cSphere MySphere;

//  whetever else you need
};

int main()
{
  cObject MyObjects[99];

// whatever  you need

}

i mostly write in python. So please don't fill me in on how there are better languages for writing text files than C++. I am WELL AWARE LOL! But i like learning languages by writing in them. So this is a good way for me to improve.

i want to get practice writing in C++11 so that I can more easily ready the source code.
I am working on a pretty complex blockmesh right now and would like to use oop in my code to try to help organize the shapes.

Has anyone done this? Would I use #codestream for this. I am working my way through the manual. But found the codestream section a bit confusing...

LeeRuns August 22, 2017 22:38

Figured out how to do this. #include. The of manual says it all

Sent from my SM-G930V using CFD Online Forum mobile app

Jack_Landis November 12, 2018 10:56

Error while using calc
 
Hi everyone!


I'm having lots of trouble with this functionality...Can someone help me please?


I started with a very basic expression:


xA 30;
xU 20;
acaso #calc "xA + xU";


and it gives the following error:


Creating block mesh from
"/home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/system/blockMeshDict"
Using #calcEntry at line 33 in file "/home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/system/blockMeshDict"
Using #codeStream with "/home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/platforms/linux64GccDPInt32Opt/lib/libcodeStream_a9cfc2467f9de95ca529e46e98786f31497c cff1.so"
Creating new library in "dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1/platforms/linux64GccDPInt32Opt/lib/libcodeStream_a9cfc2467f9de95ca529e46e98786f31497c cff1.so"
Invoking "wmake -s libso /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1"
wmake libso /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1
/opt/openfoam6/wmake/wmake: riga 410: make: command not found
/opt/openfoam6/wmake/wmake: riga 413: make: command not found
wmake error: file 'Make/linux64GccDPInt32Opt/sourceFiles' could not be created in /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1




--> FOAM FATAL IO ERROR:
Failed wmake "dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1/platforms/linux64GccDPInt32Opt/lib/libcodeStream_a9cfc2467f9de95ca529e46e98786f31497c cff1.so"


file: /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/system/blockMeshDict from line 17 to line 32.

From function static void (* Foam::functionEntries::codeStream::getFunction(con st Foam::dictionary&, const Foam::dictionary&))(Foam::Ostream&, const Foam::dictionary&)
in file db/dictionary/functionEntries/codeStream/codeStream.C at line 218.

FOAM exiting





Any clue?


Thank you very much!

yambanshee November 13, 2018 01:40

Quote:

Originally Posted by Jack_Landis (Post 714978)
Hi everyone!


I'm having lots of trouble with this functionality...Can someone help me please?


I started with a very basic expression:


xA 30;
xU 20;
acaso #calc "xA + xU";


and it gives the following error:


Creating block mesh from
"/home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/system/blockMeshDict"
Using #calcEntry at line 33 in file "/home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/system/blockMeshDict"
Using #codeStream with "/home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/platforms/linux64GccDPInt32Opt/lib/libcodeStream_a9cfc2467f9de95ca529e46e98786f31497c cff1.so"
Creating new library in "dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1/platforms/linux64GccDPInt32Opt/lib/libcodeStream_a9cfc2467f9de95ca529e46e98786f31497c cff1.so"
Invoking "wmake -s libso /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1"
wmake libso /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1
/opt/openfoam6/wmake/wmake: riga 410: make: command not found
/opt/openfoam6/wmake/wmake: riga 413: make: command not found
wmake error: file 'Make/linux64GccDPInt32Opt/sourceFiles' could not be created in /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1




--> FOAM FATAL IO ERROR:
Failed wmake "dynamicCode/_a9cfc2467f9de95ca529e46e98786f31497ccff1/platforms/linux64GccDPInt32Opt/lib/libcodeStream_a9cfc2467f9de95ca529e46e98786f31497c cff1.so"


file: /home/laj/OpenFOAM/laj-6/run/interFoam/expo/expoBCTest/system/blockMeshDict from line 17 to line 32.

From function static void (* Foam::functionEntries::codeStream::getFunction(con st Foam::dictionary&, const Foam::dictionary&))(Foam::Ostream&, const Foam::dictionary&)
in file db/dictionary/functionEntries/codeStream/codeStream.C at line 218.

FOAM exiting





Any clue?


Thank you very much!


you need a dollar sign before all your variables when you call them, so:
Code:

xA 30;
xU 20;
acaso #calc "$xA + $xU";


Jack_Landis November 13, 2018 04:55

I tried but...
 
Hello!


I tried but it gave me the same error...Anyother ideas?

LeeRuns April 2, 2019 22:36

Make sure 2 add spaces aroundathematical characters such as -+×÷/.

pooyanni April 18, 2019 21:06

Creating a parametric array of geometries
 
Hi everyone,

I am relatively new to OpenFOAM. I want to create an array of rectangular grooves in my geometry that are defined by some parameters (width, height, and spacing). From what I learned in this thread, I am able to use a while loop to create the required vertices for the grooves. However, I am having some issues when intend to create the blocks using hex inside a while loop. Below is my code;

Code:

convertToMeters 0.001;

plate_length 96;
domain_height 30;
a 1; //defines the spacing between each two grooves
b 7; // defines the width of the grooves
c 2; // defines the height of the grooves

vertices
(
    (0 0 0)
    ($plate_length 0 0)
    ($plate_length $domain_height 0)
    (0 $domain_height 0)
    (0 0 1)
    ($plate_length 0 1)
    ($plate_length $domain_height 1)
    (0 $domain_height 1)
   
    #codeStream
    {
        codeInclude
        #{
          #include "pointField.H"
        #};

        code
        #{
       
        label trenchNo =1;
       

        while (trenchNo <= $plate_length/($a+$b))
          //the total number of the grooves is equal to $plate_length/($a+$b)
        {
          os << point ((trenchNo-1)*($a+$b)+$a, 0, 0) << endl;
          os << point (trenchNo*($a+$b), 0, 0) << endl;
          os << point (trenchNo*($a+$b), -$c, 0) << endl;
          os << point ((trenchNo-1)*($a+$b)+$a, -$c, 0) << endl;
          os << point ((trenchNo-1)*($a+$b)+$a, 0, 1) << endl;
          os << point (trenchNo*($a+$b), 0, 1) << endl;
          os << point (trenchNo*($a+$b), -$c, 1) << endl;
          os << point ((trenchNo-1)*($a+$b)+$a, -$c, 1) << endl;

          ++trenchNo;
        }
        #};
    }

);


blocks
(
    hex (0 1 2 3 4 5 6 7) (100 100 1) simpleGrading (1 1 1)
    #codeStream
    {
        codeInclude
        #{
          #include "pointField.H"

        #};

        code
        #{
       
        label trenchNo = 1;
       
         
        while (trenchNo <= $plate_length/($a+$b)) //Here I want to create the block of each groove
        {

          label vertexNo {(trenchNo-1)*8 + 8};
         
        hex (vertexNo, vertexNo+1, vertexNo+2, vertexNo+3, vertexNo+4, vertexNo+5,  vertexNo+6, vertexNo+7) (10 10 1) simpleGrading (1 1 1)


          ++trenchNo;

        }
        #};
    }

);

Once I execute the blockMesh, I receive the following error messages;

Code:

error: invalid initialization of reference of type ‘Foam::IOstream&’ from expression of type ‘Foam::label {aka int}’
In file included from /opt/openfoam6/src/OpenFOAM/lnInclude/Ostream.H:39:0,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/UILListIO.C:27,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/UILList.C:91,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/UILList.H:383,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/ILList.H:39,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/IDLList.H:35,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/entry.H:45,
                from /opt/openfoam6/src/OpenFOAM/lnInclude/dictionary.H:53,
                from codeStreamTemplate.C:29:
/opt/openfoam6/src/OpenFOAM/lnInclude/IOstream.H:565:18: note: in passing argument 1 of ‘Foam::IOstream& Foam::hex(Foam::IOstream&)’
 inline IOstream& hex(IOstream& io)
                  ^~~
/home/pooyanni/OpenFOAM/pooyanni-6/run/a1b1c1/system/blockMeshDict.#codeStream:101:107: error: expected ‘)’ before numeric constant
/opt/openfoam6/wmake/rules/General/transform:25: recipe for target 'Make/linux64GccDPInt32Opt/codeStreamTemplate.o' failed
make: *** [Make/linux64GccDPInt32Opt/codeStreamTemplate.o] Error 1


--> FOAM FATAL IO ERROR:
Failed wmake "dynamicCode/_7bf46a550e1a99fa827dba743e26405dd82708a9/platforms/linux64GccDPInt32Opt/lib/libcodeStream_7bf46a550e1a99fa827dba743e26405dd82708a9.so"


file: /home/pooyanni/OpenFOAM/pooyanni-6/run/a1b1c1/system/blockMeshDict from line 17 to line 73.

    From function static void (* Foam::functionEntries::codeStream::getFunction(const Foam::dictionary&, const Foam::dictionary&))(Foam::Ostream&, const Foam::dictionary&)
    in file db/dictionary/functionEntries/codeStream/codeStream.C at line 218.

FOAM exiting

I would appreciate if anyone has any idea how I can create the blocks for my mesh in a while loop.:)


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