11"""
22 mesh_basic(coordinates_min, coordinates_max, n_points_x, n_points_y)
33
4- Create points in a regular grid.
4+ Creates points for a regular grid. Shifting every second column of points to avoid a simple
5+ mesh with bisected rectangles. This results in a unique triangulation.
56"""
67function mesh_basic (coordinates_min, coordinates_max, n_points_x, n_points_y)
8+ @assert n_points_x > 1 " n_points_x has to be at least 2."
9+ @assert n_points_y > 1 " n_points_y has to be at least 2."
10+
711 dx = (coordinates_max[1 ] - coordinates_min[1 ]) / (n_points_x - 1 )
812 dy = (coordinates_max[2 ] - coordinates_min[2 ]) / (n_points_y - 1 )
913
@@ -32,3 +36,101 @@ function mesh_basic(coordinates_min, coordinates_max, n_points_x, n_points_y)
3236
3337 return points
3438end
39+
40+ """
41+ mesh_bisected_rectangle(coordinates_min, coordinates_max, n_points_x, n_points_y;
42+ symmetric_shift = false)
43+
44+ Creates points in a regular manner. The resulting non-unique triangulation consists of bisected
45+ rectangles. To allow periodic boundaries for the resulting polygon mesh, it is possible to enable
46+ a symmetric shift.
47+ """
48+ function mesh_bisected_rectangle (coordinates_min, coordinates_max, n_elements_x, n_elements_y;
49+ symmetric_shift = false )
50+ @assert n_elements_x > 0 " n_elements_x has to be at least 1."
51+ @assert n_elements_y > 0 " n_elements_y has to be at least 1."
52+
53+ dx = (coordinates_max[1 ] - coordinates_min[1 ]) / n_elements_x
54+ dy = (coordinates_max[2 ] - coordinates_min[2 ]) / n_elements_y
55+
56+ n_points = (n_elements_x + 1 ) * (n_elements_y + 1 )
57+ points = Matrix {eltype(coordinates_min)} (undef, 2 , n_points)
58+ for j in 0 : n_elements_y
59+ for i = 0 : n_elements_x
60+ k = j * (n_elements_x + 1 ) + i + 1
61+ points[:, k] = [coordinates_min[1 ] + i * dx, coordinates_min[2 ] + j * dy]
62+ end
63+ end
64+
65+ # Symmetric shift to get unique triangulation and therefore possible periodic boundaries in
66+ # the polygon mesh
67+ if symmetric_shift
68+ domain_center = 0.5 * [coordinates_min[1 ] + coordinates_max[1 ],
69+ coordinates_min[2 ] + coordinates_max[2 ]]
70+ s = [dx, dy]
71+ for i in axes (points, 2 )
72+ # Do not move boundary points with boundary_distance <= 10^-6
73+ boundary_distance = min (abs (coordinates_min[1 ] - points[1 , i]),
74+ abs (coordinates_max[1 ] - points[1 , i]),
75+ abs (coordinates_min[2 ] - points[2 , i]),
76+ abs (coordinates_max[2 ] - points[2 , i]))
77+ if boundary_distance > 1.0e-8 # inner point
78+ d = sqrt (sum ((domain_center .- points[:,i]). ^ 2 ))
79+ points[:, i] .+ = 1.0e-6 * d * s .* (domain_center .- points[:, i])
80+ end
81+ end
82+
83+ if isodd (n_elements_x)
84+ for i in axes (points, 2 )
85+ # Do not move boundary points with boundary_distance <= 10^-6
86+ boundary_distance = min (abs (coordinates_min[1 ] - points[1 , i]),
87+ abs (coordinates_max[1 ] - points[1 , i]),
88+ abs (coordinates_min[2 ] - points[2 , i]),
89+ abs (coordinates_max[2 ] - points[2 , i]))
90+ if boundary_distance > 1.0e-8 # inner point
91+ # Only move the two most inner points columns
92+ distance_center_x = abs (domain_center[1 ] - points[1 , i])
93+ if distance_center_x <= dx
94+ points[1 , i] += 1.0e-6 * dx
95+ end
96+ end
97+ end
98+ end
99+ if isodd (n_elements_y)
100+ for i in axes (points, 2 )
101+ # Do not move boundary points with boundary_distance <= 10^-6
102+ boundary_distance = min (abs (coordinates_min[1 ] - points[1 , i]),
103+ abs (coordinates_max[1 ] - points[1 , i]),
104+ abs (coordinates_min[2 ] - points[2 , i]),
105+ abs (coordinates_max[2 ] - points[2 , i]))
106+ if boundary_distance > 1.0e-8 # inner point
107+ # Only move the two most inner points rows
108+ distance_center_y = abs (domain_center[2 ] - points[2 , i])
109+ if distance_center_y <= dy
110+ points[2 , i] += 1.0e-6 * dy
111+ end
112+ end
113+ end
114+ end
115+ end
116+
117+ # This directly creates the connectivity of a triangulation. Every rectangle is bisected
118+ # in the same direction.
119+ # n_triangles = 2 * n_elements_x * n_elements_y
120+ # vertices = Matrix{Cint}(undef, 3, n_triangles)
121+ # k = 0
122+ # for j in 1:n_elements_y
123+ # for i in 1:n_elements_x
124+ # k = k + 1
125+ # vertices[:, k] .= [(j - 1) * (n_elements_x + 1) + i,
126+ # (j - 1) * (n_elements_x + 1) + i + 1,
127+ # j * (n_elements_x + 1) + i]
128+ # k = k + 1
129+ # vertices[:, k] .= [(j - 1) * (n_elements_x + 1) + i + 1,
130+ # j * (n_elements_x + 1) + i + 1,
131+ # j * (n_elements_x + 1) + i]
132+ # end
133+ # end
134+
135+ return points
136+ end
0 commit comments