CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Meshing & Mesh Conversion

[Gmsh] 2D Mesh Generation Tutorial for GMSH

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree6Likes
  • 6 Post By aeroslacker

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   September 10, 2010, 12:26
Default 2D Mesh Generation Tutorial for GMSH
  #1
New Member
 
John
Join Date: Sep 2010
Posts: 4
Rep Power: 16
aeroslacker is on a distinguished road
Hello All-

I struggled with creating a 2D mesh with GMSH for use in OpenFOAM. After researching the tools and on this forum, I finally was successful. So, in order to share this knowledge, I have a tutorial that walks you though creating a 2D mesh in GMSH and porting it to OpenFOAM format. I know this information is in various places, but I wanted to consolidate it into a single step-by step tutorial.

The step by step procedure looks intimidating, but it can really be done quite quickly once you are familiar with the commands.

I hope it is helpful, please feel free to add to it or comment. I am sure there are other ways to do things, but this is how I am doing it for now.

-John

Process for Creating 2D Meshes with GMSH for OpenFOAM

This tutorial was created to show how to generate a 2D mesh for OpenFOAM using the GMSH Open Source Mesh Generator. OpenFOAM by default only works with 3D mesh elements, so some special steps need to be applied to create a 2D mesh. This is not meant to be a tutorial on GMSH or OpenFOAM, but just some useful steps to get the two tools to work together. Note that this uses the GMSH GUI to create the mesh, and requires some modifications of the resulting .geo file.

I. Defining the Geometry in GMSH
1. Open GMSH and create a new file.
2. In a single plane (2D), create the geometry by first creating all points, then combining the points into lines, and then the lines into a surface. These can all be done from the Geometry Menu under Elementary entities->Add->New.
3. Extrude the final surface into 3D by selecting Elementary entities->Extrude->Translate under the Geometry menu. Select Surface, then click the surface in the viewer. This will extend the surface into 3D space. The distance and direction of the extension is defined under the Contextual Geometry Definitions window that appears, under the Translate tab.
4. Now that the shape is 3D, the boundaries can be defined. To define a boundary, go to Physical Groups->Add->Surface from Geometry and add each surface that will be a boundary. Remember the order of the boundaries, as the GMSH GUI will label them with numbers that later need to be changed to a text name for OpenFOAM. For example, if the shape is a square, select the bottom, left, top, right, front, and back faces and individually add them as a Physical Group (If your boundary consists of multiple faces, you can select them together and then add the Physical Group). Later the names will be changed from a number to “bottom”, “left”, “top”, etc... These names will be used in OpenFOAM.
5. The volume of the geometry must also be named. Select Physical Groups->Add->Volume from Geometry and select the volume.
6. The shape should be saved as a .geo file automatically, but if not, save it now. Then open the .geo file in a text editor. Rename the Physical Surfaces to the names that will be used in OpenFOAM as the boundary names. For example (the numbers may be different):

Physical Surface("front") = {28};
Physical Surface("back") = {6};
Physical Surface("bottom") = {27};
Physical Surface("left") = {15};
Physical Surface("top") = {19};
Physical Surface("right") = {23};

Do the same for the volume:

Physical Volume("internal") = {1};

7. In the .geo file, the Extrude statement must be modified to have a single layer, and then should be recombined. Do this by adding the Layers{1} and Recombine commands to the Extrude command. This ensures that the mesh blocks are created only in 2D and then extended to 3D, and not divided in the 3rd axis. For example:

Extrude {0, 0, 10} {
Surface{6};
Layers{1};
Recombine;
}

8. Save the .geo file in the text editor and then reload in GMSH using Reload under the Geometry menu. There should be no errors produced by GMSH.

Below is an example of a 2D mesh .geo file of a simple square:

Point(1) = {-100, 100, 0, 1e+22};
Point(2) = {100, 100, 0, 1e+22};
Point(3) = {100, -100, 0, 1e+22};
Point(4) = {-100, -100, 0, 1e+22};
Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};
Line Loop(6) = {4, 1, 2, 3};
Plane Surface(6) = {6};
Physical Volume("internal") = {1};
Extrude {0, 0, 10} {
Surface{6};
Layers{1};
Recombine;
}
Physical Surface("front") = {28};
Physical Surface("back") = {6};
Physical Surface("bottom") = {27};
Physical Surface("left") = {15};
Physical Surface("top") = {19};
Physical Surface("right") = {23};

9. Create the mesh in GMSH by selecting 3D from the Mesh menu. If everything is set up correctly (via the Extrude modifications), the sides of the 3D mesh should only have straight lines connecting the front face to the back face. This is required for OpenFOAM because for 2D analysis the mesh blocks should only change in two dimensions. OpenFOAM will produce an error if the mesh does not meet this requirement.

The size of the mesh elements can be controlled by setting the Element Size Factor under Mesh in the Options window (select Tools->Options from the main menu). The smaller the number, the smaller the mesh elements.

10. Save the mesh by selecting ‘Save’ from the Mesh menu. This will create a file using the same name as the .geo file, but ending in .msh. This will be used by the OpenFOAM utility gmshToFoam that is shipped with OpenFOAM.

II. Converting the Mesh to OpenFOAM
1. An OpenFOAM case directory with a controlDict file must already be created. Copy the .msh file into the case directory.
2. Open a command console and go to the case directory, and enter gmshToFoam <file.msh>. For example:

gmshToFoam square.msh

This will create the mesh used by OpenFOAM under the constant/polyMesh directory. If there are any errors, ensure that a proper case directory is set up (refer to OpenFOAM documentation, or use a pre-shipped case directory such as icoFoam or simpleFoam). If the errors indicate no 3D elements exist, or there is another mesh problem, make sure all steps were followed in the GMSH section of this tutorial.
3. Check that the mesh conforms to OpenFOAM standards by running checkMesh from the console.
4. To confirm that the boundaries made it to the OpenFOAM mesh, open the boundary file in the case directory under constant/polyMesh. Each boundary is defined here. For the faces that are along the 3rd dimension, set the type to ‘empty’. For example:

back
{
type empty;
nFaces 2858;
startFace 4217;
}
front
{
type empty;
nFaces 2858;
startFace 7075;
}

This will indicate to OpenFOAM that this is a 2D case. Ensure that all other boundaries as defined in the GMSH .geo file are present. There will also be a defaultFaces boundary that should have nFaces = 0 (it is unclear what will happen if nFaces is not 0):

defaultFaces
{
type patch;
nFaces 0;
startFace 10073;
}

5. The 2D mesh is now ready to be used by OpenFOAM. Be sure that the boundaries names in the initial conditions files (for example p, U, etc...) match those if the boundary file. For the empty faces (3rd dimension), set the boundary type to ‘empty’ in these files.

6. If all went well, OpenFOAM should be able to use the new mesh for a 2D case. If things don’t go well... OpenFOAM will let you know.
aeroslacker is offline   Reply With Quote

Old   September 10, 2010, 12:32
Default
  #2
Senior Member
 
Sandeep Menon
Join Date: Mar 2009
Location: Amherst, MA
Posts: 403
Rep Power: 25
deepsterblue will become famous soon enough
Thanks for taking the trouble. If you could post this on the OpenFOAM wiki, a lot of folks would find this really useful.

Thanks again.
__________________
Sandeep Menon
University of Massachusetts Amherst
https://github.com/smenon
deepsterblue is offline   Reply With Quote

Old   September 10, 2010, 14:46
Default
  #3
New Member
 
John
Join Date: Sep 2010
Posts: 4
Rep Power: 16
aeroslacker is on a distinguished road
Thanks for the comments! I have done as you suggest and posted the tutorial on the OpenFOAM Wiki at:

http://openfoamwiki.net/index.php/2D...ial_using_GMSH
aeroslacker is offline   Reply With Quote

Old   October 8, 2010, 12:14
Default
  #4
Senior Member
 
Join Date: Sep 2010
Posts: 226
Rep Power: 17
T.D. is on a distinguished road
Hi
What about these "defaultFaces".
I did a 3D Mesh, and i have these defaultFaces with 0 nFaces always.

but it is making a problem for me
because it requires to be defined in 0/p 0/U files as a patch but i don't know what really it is?

Any ideas?


Thanks
T.D. is offline   Reply With Quote

Old   October 21, 2010, 08:07
Default
  #5
New Member
 
John
Join Date: Sep 2010
Posts: 4
Rep Power: 16
aeroslacker is on a distinguished road
Hi T.D.-

I get the defaultFaces too, I am not sure why gmshToFoam creates those even when there aren't any.

Anyway, in the 'constant/polyMesh/boundary' file that is created, you can simply remove the defaultFaces section completely (assuming nFaces is 0). If you do remove it, subtract 1 from the number at the top of the boundary list, else OpenFOAM will issue an error because it expects that many boundaries.

Alternatively, in the 0/p, 0/U etc... files, you can just set the type to 'empty'. However, in this case you also need to set the defaultFaces type to 'empty' in the boundary file.

I hope this helps!
aeroslacker is offline   Reply With Quote

Old   October 21, 2010, 10:26
Default Thanks
  #6
Senior Member
 
Join Date: Sep 2010
Posts: 226
Rep Power: 17
T.D. is on a distinguished road
Hi aeroslacker,

yes indeed i had solved the problem by erasing the defaultFaces part and remove one from the number of faces, and it is always working now like this.

Any way thanks a lot

Do you have any idea how to apply a rotating disk BC in a cylinder?

thanks a lot

yours,

T.D.
T.D. is offline   Reply With Quote

Old   October 26, 2010, 09:36
Default
  #7
New Member
 
John
Join Date: Sep 2010
Posts: 4
Rep Power: 16
aeroslacker is on a distinguished road
Hi T.D.-

Unfortunately, I do not know directly. I suppose it may be possible, if the cylinder is broken up to a small resolution, to apply a velocity boundary condition (moving wall) to each facet. The cylinder would not actually rotate, but I think the fluid around it would act as though it is rotating.

I have not tried something like this, but it is just a thought. I think the 'cavity' tutorial shows how to make a moving wall boundary condition.

-John
aeroslacker is offline   Reply With Quote

Old   October 26, 2010, 10:00
Default Hi
  #8
Senior Member
 
Join Date: Sep 2010
Posts: 226
Rep Power: 17
T.D. is on a distinguished road
Hi,
Thanks a lot
I tried something called "rotatingWallVelocity" BC type.But it is not working, i don't know why !

My flow is a torsional flow between two parallel disks, where one disk is rotating.

??
T.D. is offline   Reply With Quote

Old   November 8, 2010, 15:55
Default empty patch
  #9
New Member
 
shyam
Join Date: Oct 2010
Posts: 17
Rep Power: 16
ksp1717 is on a distinguished road
Hi,

I have a question regarding the boundaries.

I have read in one of the tutorials which says that in order to create a 2D simulation i need to make a 3D model and given the 3rd dimension i.e into the screen or paper to be "empty ".But in your steps you did not do any such thing. I have finished creating the model i want but did not run the simulation yet but just asking to make sure that what i have said is somehow automatically done by this process.

Thanks
Shyam
ksp1717 is offline   Reply With Quote

Old   November 8, 2010, 15:56
Default Hi
  #10
New Member
 
shyam
Join Date: Oct 2010
Posts: 17
Rep Power: 16
ksp1717 is on a distinguished road
Hi


Sorry for some reason I have missed that part in your tutorial. I found it thank you.


Shyam
ksp1717 is offline   Reply With Quote

Old   July 5, 2011, 07:10
Default
  #11
New Member
 
Richard Moser
Join Date: Aug 2009
Posts: 29
Rep Power: 17
moser_r is on a distinguished road
I have tried using the 2D tutorial, copying the .geo file exactly, creating a mesh in gmsh, then using gmshToFoam. It all works, apart from the fact that none of the patches are created ("front", "back", etc.) - I just get patch0 and defaultFaces in the boundary file. I am using OpenFOAM 1.7.1 and gmsh 2.5.0. Is there something I am missing? I am trying to automate the gmsh input into OpenFOAM, so really don't want to have to manually edit the boundary file for every case.
moser_r is offline   Reply With Quote

Old   July 7, 2011, 02:29
Default
  #12
New Member
 
Richard Moser
Join Date: Aug 2009
Posts: 29
Rep Power: 17
moser_r is on a distinguished road
I've managed to sort the problem by downloading the latest (nightly) build of gmsh (2.5.1). All patches now come through to OpenFOAM properly.
moser_r is offline   Reply With Quote

Old   January 19, 2012, 03:52
Default
  #13
Member
 
Join Date: Apr 2010
Posts: 51
Rep Power: 16
bephi is on a distinguished road
thank you very much for the nice tutorial.
everything is well described but unfortunately I get no "Extrude" part in the geo file when I select Elementary entities->Extrude->Translate under the Geometry menu. Instead it creates 4 more points, 8 more lines and the corresponding "Ruled surfaces".
I tried it with GMSH 2.4 and 2.5.

Do you have any idea what went wrong?
Thank you in advance.
bephi is offline   Reply With Quote

Reply

Tags
2d mesh, empty boundaries, gmsh, gmshtofoam

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set periodic boundary conditions Ganesh FLUENT 15 November 18, 2020 06:09
[snappyHexMesh] No layers in a small gap bobburnquist OpenFOAM Meshing & Mesh Conversion 6 August 26, 2015 09:38
[ICEM] PistonVale tutorial - issues about mesh quality Andrea1984 ANSYS Meshing & Geometry 5 October 11, 2013 09:30
[ANSYS Meshing] Question about mesh generation learning lnk ANSYS Meshing & Geometry 2 July 7, 2012 06:45
ISAAC code mesh generation morteza08 Main CFD Forum 0 June 15, 2012 09:26


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