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
9import numpy as np
10import meshpy.triangle as triangle
11import meshio
12
13n_circumferential_points =20
14radius = 1.0
15width = 3.0
16
17points = []
18# Define the vertices of the square domain (centered on 0)
19points = [(-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)
21for 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
26segments = [(0,1), (1,2), (2,3), (3,0)]
27markers = [1,2,3,4]
28Nbeg = len(markers)
29# Define the segments for the circle
30for 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
39mesh_info = triangle.MeshInfo()
40# Add points
41mesh_info.set_points(points)
42# Add facets
43mesh_info.set_facets(segments, facet_markers=markers)
44# Add regions
45mesh_info.regions.resize(2)
46# inside circle region
47mesh_info.regions[0] = [0.0, 0.0, 1, 0.1] # 2d pt + ID + max_vol
48# outside circle region
49eps = 1e-2
50mesh_info.regions[1] = [-width/2+eps,-width/2+eps, 2, 0.1] # 2d pt + ID + max_vol
51
52# Refine the mesh
53mesh = 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
57points_3d = [(x, y, 0) for x, y in mesh.points]
58
59directory = ""
60filename = "pin_01"
61meshio.write_points_cells(directory + filename + ".obj",
62 points_3d,
63 {"triangle": mesh.elements})
64
65# Save the mesh to an .vtk file
66meshio.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*/