Chi-Tech
MeshTutorial_06_2DTrianglePython.h
Go to the documentation of this file.
1
/** \page MeshTutorial_06 Mesh Tutorial 6: A 2D Unstructured Mesh
2
3
Here we provide an example of a 2D unstructured mesh generated using the
4
2D ```Triangle``` mesh generator, with is available in Python via the ```MeshPy``` module.
5
We export the resulting mesh using ```meshio``` in both ```.obj``` and ```.vtu```
6
formats. Note that only the export as VTU contains material IDs.
7
8
\code
9
import numpy as np
10
import meshpy.triangle as triangle
11
import meshio
12
13
n_circumferential_points =20
14
radius = 1.0
15
width = 3.0
16
17
points = []
18
# Define the vertices of the square domain (centered on 0)
19
points = [(-width/2,-width/2), (width/2,-width/2), (width/2,width/2), (-width/2,width/2),]
20
# Create a list of points for the circle (centered on 0)
21
for i in range(n_circumferential_points):
22
angle = i * 2 * np.pi / n_circumferential_points
23
points.append((radius * np.cos(angle), radius * np.sin(angle)))
24
25
# Define the segments of the square domain
26
segments = [(0,1), (1,2), (2,3), (3,0)]
27
markers = [1,2,3,4]
28
Nbeg = len(markers)
29
# Define the segments for the circle
30
for i in range(Nbeg,n_circumferential_points+Nbeg):
31
if i+1 == n_circumferential_points+Nbeg:
32
ip1 = Nbeg
33
else:
34
ip1 = i+1
35
segments.extend([(i,ip1)])
36
markers.extend([0])
37
38
# Create a mesh info object
39
mesh_info = triangle.MeshInfo()
40
# Add points
41
mesh_info.set_points(points)
42
# Add facets
43
mesh_info.set_facets(segments, facet_markers=markers)
44
# Add regions
45
mesh_info.regions.resize(2)
46
# inside circle region
47
mesh_info.regions[0] = [0.0, 0.0, 1, 0.1] # 2d pt + ID + max_vol
48
# outside circle region
49
eps = 1e-2
50
mesh_info.regions[1] = [-width/2+eps,-width/2+eps, 2, 0.1] # 2d pt + ID + max_vol
51
52
# Refine the mesh
53
mesh = triangle.build(mesh_info, verbose=True, max_volume=0.02, min_angle=25,
54
attributes=True, generate_faces=True)
55
56
# Add a third dimension to the points
57
points_3d = [(x, y, 0) for x, y in mesh.points]
58
59
directory = ""
60
filename = "pin_01"
61
meshio.write_points_cells(directory + filename + ".obj",
62
points_3d,
63
{"triangle": mesh.elements})
64
65
# Save the mesh to an .vtk file
66
meshio.write_points_cells(directory + filename + ".vtu",
67
points_3d,
68
{"triangle": mesh.elements},
69
cell_data={"attribute": [mesh.element_attributes]})
70
71
\endcode
72
73
74
*/
doc
PAGES
MeshTutorials
MeshTutorial_06_2DTrianglePython.h
Generated by
1.9.3