Note
Go to the end to download the full example code.
.geo File: Three point bending test#
This example demonstrates the procedure for generating a mesh for a Three point bending test. Note that Phasefieldx can import external meshes in the .msh format. This can be achieved by using Gmsh.
Files with the .geo format define both the geometry and the mesh parameters, such as element types and sizes. Gmsh can then generate the mesh based on the input from these .geo files.
Below is an example of the .geo file used for mesh generation.
.geo file#
// -----------------------------------------------------------------------------
//
// Gmsh GEO
//
// Mesh size fields
//
// -----------------------------------------------------------------------------
//Use the following line to generate the mesh (.inp (abaqus))
//gmsh ThreePointBending.geo -3 -o mesh.msh
h = 1e-1;
hcrack= 0.005;
// ------------------------------------------------------
// ------------------------------------------------------
// A)Geometry Definition: 1)Points
// 2)Lines
// 3)Curve
// 4)Surface
// ------------------------------------------------------
// A1)Points Definitions:
//
// 7*-------------------------------*6
// | |
// | |
// | |
// | *3 |
// | /\ |
// *--------------* *-------------*
// 1 2 4 5
// |Y
// |
// ---X Dimensions: 8 x 2 x 0.01
// Z /
// -----Coordinates--
//Points: ----X,------Y,---Z,
Point(1) ={ -4.0, 0.0, 0, h};
Point(2) ={ -0.2, 0.0, 0, h};
Point(3) ={ 0.0, 0.4, 0, h};
Point(4) ={ 0.2, 0.0, 0, h};
Point(5) ={ 4.0, 0.0, 0, h};
Point(6) ={ 4.0, 2.0, 0, h};
Point(7) ={ -4.0, 2.0, 0, h};
// ------------------------------------------------------
// A2)Lines Definition
//
// <-L6
// *-------------------------------*
// | |
// | | ^
// L7| | L5
// \/| |
// | L2 ^ /\ L3\/ |
// *--------------* *-------------*
// L1-> L4->
Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 5};
Line(5) = {5, 6};
Line(6) = {6, 7};
Line(7) = {7, 1};
// ------------------------------------------------------
// A3)Curve Definition
//
// *---------------<---------------*
// | |
// | |
// \/ /\
// | |
// | /\ |
// *------>-------* *------>------*
//
Curve Loop(5) = {1,2,3,4,5,6,7}; //C5: through lines L1,L2,...,L7
// ------------------------------------------------------
// A4)Surface Definition
//
// *-------------------------------*
// | |
// | |
// | S6 |
// | |
// | /\ |
// *--------------* *-------------*
//
Plane Surface(6) = {5};
// ------------------------------------------------------
// ------------------------------------------------------
// B)Mesh Definition: 1)Mesh size Box1
// 2)Mesh size Box2
// 3)Mesh size Box3
// 4)Mesh size Box4
// 5)Mesh min(Box1,Box2)
// 6)Extrude Mesh
// 7)Mesh Algorithm
// ------------------------------------------------------
// B1) Mesh size Box1
//
// *-------------------------------*
// | |A | |
// | | | | A: Field[6] hcrack
// | | | |
// | | | |
// | |/\| |
// *--------------* *-------------*
//
Field[6] = Box;
Field[6].VIn = hcrack;
Field[6].VOut = h;
Field[6].XMin = -0.2;
Field[6].XMax = 0.2;
Field[6].YMin = 0.0;
Field[6].YMax = 2.0;
// ------------------------------------------------------
// B2) Mesh size Box2
//
// *-------------------------------*
// | |B | |
// | | | | B: Field[7] hcrack*10
// | | | |
// | | | |
// | | /\ | |
// *--------------* *-------------*
//
Field[7] = Box;
Field[7].VIn = hcrack*10;
Field[7].VOut = h;
Field[7].XMin = -0.4;
Field[7].XMax = 0.4;
Field[7].YMin = 0.0;
Field[7].YMax = 2.0;
// ------------------------------------------------------
// B3) Mesh size Box3
//
// *-------------------------------*
// | |
// | | C: Field[8] hcrack
// | |
// |___ |
// | C | /\ |
// *--------------* *-------------*
//
Field[8] = Box;
Field[8].VIn = hcrack;
Field[8].VOut = h;
Field[8].XMin = -4.0;
Field[8].XMax = -3.9;
Field[8].YMin = 0.0;
Field[8].YMax = 0.1;
// ------------------------------------------------------
// B4) Mesh size Box4
//
// *-------------------------------*
// | |
// | | D: Field[8] hcrack
// | |
// | ___|
// | /\ | D |
// *--------------* *-------------*
//
Field[9] = Box;
Field[9].VIn = hcrack;
Field[9].VOut = h;
Field[9].XMin = 3.9;
Field[9].XMax = 4.0;
Field[9].YMin = 0.0;
Field[9].YMax = 0.1;
// ------------------------------------------------------
// B5) Mesh min(Box1,Box2,Box3,Box4)
//
// *-------------------------------*
// | |B| A| | |
// | | | | | |
// | | | | | | Field[10]
// |___ | | | | ___|
// | C | | |/\| | | D |
// *--------------* *-------------*
//
Field[10] = Min;
Field[10].FieldsList = {6,7,8,9};
Background Field = 10;
// ------------------------------------------------------
// B4)Extrude Mesh
// {X, Y, Z} Surface
Extrude {0, 0, 0.01}{Surface{6}; Layers{1};Recombine;}
// ------------------------------------------------------
// B5)Mesh Algorithm
Geometry.Tolerance = 1e-12;
Mesh.Algorithm3D = 1;
For more information about Gmsh and the .geo file format, please refer to the official Gmsh website.
To generate the mesh in the .msh format from the terminal, use the following Gmsh command:
Here, -3 specifies that the mesh is 3-dimensional, and -o mesh.msh tells Gmsh to save the mesh to a file named mesh.msh with the .msh extension.
Alternatively, you can generate the mesh using the Gmsh Python API, which allows for programmatic mesh generation within Python scripts.
Mesh Visualization#
The purpose of this code is to visualize the mesh. The mesh is generated from the .geo file and saved as output_mesh_for_view.vtu. It is then loaded and visualized using PyVista.
import os
import gmsh
import pyvista as pv
folder = "9103_ThreePointBendingTest"
Initialize Gmsh
gmsh.initialize()
Open the .geo file
geo_file = os.path.join(folder, "file.geo")
gmsh.open(geo_file)
Generate the mesh (2D example, for 3D use generate(3))
gmsh.model.mesh.generate(3)
Write the mesh to a .vtu file
vtu_file = os.path.join(folder, "output_mesh_for_view.vtk")
gmsh.write(vtu_file)
Finalize Gmsh
gmsh.finalize()
print(f"Mesh successfully written to {vtu_file}")
file_vtu = pv.read(vtu_file)
file_vtu.plot(cpos='xy', color='white', show_edges=True)

Mesh successfully written to 9103_ThreePointBendingTest/output_mesh_for_view.vtk
Total running time of the script: (0 minutes 3.875 seconds)