def refine_by_delaunay(self, triangles): """refines given elements by the delaunay refinement method""" p = self.node_list t = self.face_list for tr in triangles: pmid = (p[t[tr][0]] + p[t[tr][1]] + p[t[tr][2]]) / 3 p = np.append(p, [pmid], axis=0) self.edge_length /= 2 dp = self.edge_length for i in range(1, int(ceil(1 / dp))): p = np.append(p, [[i * dp, 0]], axis=0) for i in range(int(ceil(.5 / dp))): p = np.append(p, [[1, i * dp]], axis=0) for i in range(int(ceil(.5 / dp))): p = np.append(p, [[1 - i * dp, .5]], axis=0) for i in range(int(ceil(.5 / dp))): p = np.append(p, [[.5, .5 + i * dp]], axis=0) for i in range(int(ceil(.5 / dp))): p = np.append(p, [[.5 - i * dp, 1]], axis=0) for i in range(int(ceil(1 / dp))): p = np.append(p, [[0, 1 - i * dp]], axis=0) p = unique2d(p) t = spspatial.Delaunay(p).vertices # remove triangles where centroid outside boundary (good enough...) pmid = p[t].sum(1) / 3 t = t[self.fd(pmid) < 1e-15] t = [Face(t[i]) for i in range(len(t))] self.node_list = p self.face_list = t
def refine_by_delaunay(self, triangles): """refines given elements by the delaunay refinement method""" p = self.node_list t = self.face_list for tr in triangles: pmid = (p[t[tr][0]] + p[t[tr][1]] + p[t[tr][2]]) / 3 p = np.append(p, [pmid], axis=0) self.edge_length /= 2 dp = self.edge_length for i in range(1,int(ceil(1/dp))): p = np.append(p, [[i*dp, 0]], axis=0) for i in range(int(ceil(.5/dp))): p = np.append(p, [[1, i*dp]], axis=0) for i in range(int(ceil(.5/dp))): p = np.append(p, [[1-i*dp, .5]], axis=0) for i in range(int(ceil(.5/dp))): p = np.append(p, [[.5, .5+i*dp]], axis=0) for i in range(int(ceil(.5/dp))): p = np.append(p, [[.5-i*dp, 1]], axis=0) for i in range(int(ceil(1/dp))): p = np.append(p, [[0, 1-i*dp]], axis=0) p = unique2d(p) t = spspatial.Delaunay(p).vertices # remove triangles where centroid outside boundary (good enough...) pmid = p[t].sum(1) / 3 t = t[self.fd(pmid) < 1e-15] t = [Face(t[i]) for i in range(len(t))] self.node_list = p self.face_list = t
def create_equilateral_uniform_mesh(distance_function, bounding_box, edge_length, fixed_points=None): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] edge_length = desired edge length of triangle fixed points = clear """ geps = .001 * edge_length xmin, xmax, ymin, ymax = bounding_box mesh = TriangularMesh() # make grid x, y = np.mgrid[xmin:(xmax + edge_length):edge_length, ymin:(ymax + edge_length * np.sqrt(3) / 2):edge_length * np.sqrt(3) / 2] # shift even rows #x[:, 1::2] += edge_length / 2 p = np.vstack((x.flat, y.flat)).T for i in range( int(ceil((ymax - ymin) / (edge_length * np.sqrt(3) / 2)))): p = np.append(p, [[xmin, i * np.sqrt(3) / 2 * edge_length]], axis=0) p = np.append(p, [[xmax, i * np.sqrt(3) / 2 * edge_length]], axis=0) p = np.append(p, [[0.5, i * np.sqrt(3) / 2 * edge_length]], axis=0) for i in range(int(ceil((xmax - xmin) / edge_length))): p = np.append(p, [[xmin + 0.5 + i * edge_length, ymin + 0.5]], axis=0) # remove points outside boundary p = p[distance_function(p) < -geps] # add fixed points if fixed_points is not None: fixed_points = np.array(fixed_points, dtype='d') p = np.vstack((fixed_points, p)) p = unique2d(p) # triangulation t = spspatial.Delaunay(p).vertices # remove triangles where centroid outside boundary (good enough...) pmid = p[t].sum(1) / 3 t = t[distance_function(pmid) < -1e-12] # print distance_function(p[t].sum(1)/3) mesh.set_mesh(p, t) mesh.fd = distance_function return mesh
def create_random_mesh(distance_function, bounding_box, number_of_points, dist=0, fixed_points=None): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] number of points = clear dist = minimum distance for each new node to keep from all other points, default 0 fixed points = clear if finding a new point fails more than 50 times, dist is set to dist=dist/sqrt(2) """ geps = 1e-10 n = number_of_points xmin, xmax, ymin, ymax = bounding_box mesh = TriangularMesh() p = fixed_points i = 0 failures = 0 while i < n: pv = [np.random.uniform(xmin, xmax), np.random.uniform(ymin, ymax)] if (dist == 0 or closest_node(pv, p) >= dist ) and distance_function([pv]) < -geps: p = np.append(p, [pv], axis=0) i += 1 else: failures += 1 if failures > 50: failures = 0 dist = dist / sqrt(2) # remove duplicates p = unique2d(p) # triangulation t = spspatial.Delaunay(p).vertices # remove triangles where centroid outside boundary (good enough...) pmid = p[t].sum(1) / 3 t = t[distance_function(pmid) < geps] mesh.set_mesh(p, t) mesh.fd = distance_function return mesh
def create_equilateral_uniform_mesh(distance_function, bounding_box, edge_length, fixed_points=None): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] edge_length = desired edge length of triangle fixed points = clear """ geps = .001 * edge_length xmin, xmax, ymin, ymax = bounding_box mesh = TriangularMesh() # make grid x, y = np.mgrid[xmin:(xmax + edge_length):edge_length, ymin:(ymax + edge_length * np.sqrt(3) / 2):edge_length * np.sqrt(3) / 2] # shift even rows #x[:, 1::2] += edge_length / 2 p = np.vstack((x.flat, y.flat)).T for i in range(int(ceil((ymax - ymin) / (edge_length * np.sqrt(3) / 2)))): p = np.append(p, [[xmin, i * np.sqrt(3) / 2 * edge_length]], axis=0) p = np.append(p, [[xmax, i * np.sqrt(3) / 2 * edge_length]], axis=0) p = np.append(p, [[0.5, i * np.sqrt(3) / 2 * edge_length]], axis=0) for i in range(int(ceil((xmax - xmin) / edge_length))): p = np.append(p, [[xmin + 0.5 + i * edge_length, ymin + 0.5]], axis=0) # remove points outside boundary p = p[distance_function(p) < -geps] # add fixed points if fixed_points is not None: fixed_points = np.array(fixed_points, dtype='d') p = np.vstack((fixed_points, p)) p = unique2d(p) # triangulation t = spspatial.Delaunay(p).vertices # remove triangles where centroid outside boundary (good enough...) pmid = p[t].sum(1) / 3 t = t[distance_function(pmid) < -1e-12] # print distance_function(p[t].sum(1)/3) mesh.set_mesh(p, t) mesh.fd = distance_function return mesh
def create_bad_uniform_mesh(distance_function, bounding_box, edge_length, fixed_points=None): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] edge_length = desired edge length of triangle fixed points = clear """ geps = .001 * edge_length xmin, xmax, ymin, ymax = bounding_box mesh = TriangularMesh() dx = edge_length / 5. dy = edge_length * 5. # make grid x, y = np.mgrid[xmin:(xmax + dx):dx, ymin:ymax + dy:dy] # shift even rows #x[:, 0::2] += edge_length / 2 p = np.vstack((x.flat, y.flat)).T # remove points outside boundary p = p[distance_function(p) < geps] # add fixed points if fixed_points is not None: fixed_points = np.array(fixed_points, dtype='d') p = np.vstack((fixed_points, p)) p = unique2d(p) t = spspatial.Delaunay(p).vertices pmid = p[t].sum(1) / 3 t = t[distance_function(pmid) < geps] # print distance_function(p[t].sum(1)/3) mesh.set_mesh(p, t) mesh.fd = distance_function mesh.edge_length = edge_length return mesh
def create_random_mesh(distance_function, bounding_box, number_of_points, dist=0, fixed_points=None): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] number of points = clear dist = minimum distance for each new node to keep from all other points, default 0 fixed points = clear if finding a new point fails more than 50 times, dist is set to dist=dist/sqrt(2) """ geps = 1e-10 n = number_of_points xmin, xmax, ymin, ymax = bounding_box mesh = TriangularMesh() p = fixed_points i = 0 failures = 0 while i < n: pv = [np.random.uniform(xmin, xmax), np.random.uniform(ymin, ymax)] if (dist == 0 or closest_node(pv, p) >= dist) and distance_function([pv]) < -geps: p = np.append(p, [pv], axis=0) i += 1 else: failures += 1 if failures > 50: failures = 0 dist = dist / sqrt(2) # remove duplicates p = unique2d(p) # triangulation t = spspatial.Delaunay(p).vertices # remove triangles where centroid outside boundary (good enough...) pmid = p[t].sum(1) / 3 t = t[distance_function(pmid) < geps] mesh.set_mesh(p, t) mesh.fd = distance_function return mesh
def create_bad_uniform_mesh(distance_function, bounding_box, edge_length, fixed_points=None): """ distance_function > 0 outside, <0 inside, =0 boundary bounding box = [xmin, xmax, ymin, ymax, ...] edge_length = desired edge length of triangle fixed points = clear """ geps = .001 * edge_length xmin, xmax, ymin, ymax = bounding_box mesh = TriangularMesh() dx = edge_length/5. dy = edge_length*5. # make grid x, y = np.mgrid[xmin:(xmax + dx):dx, ymin:ymax + dy:dy] # shift even rows #x[:, 0::2] += edge_length / 2 p = np.vstack((x.flat, y.flat)).T # remove points outside boundary p = p[distance_function(p) < geps] # add fixed points if fixed_points is not None: fixed_points = np.array(fixed_points, dtype='d') p = np.vstack((fixed_points, p)) p = unique2d(p) t = spspatial.Delaunay(p).vertices pmid = p[t].sum(1) / 3 t = t[distance_function(pmid) < geps] # print distance_function(p[t].sum(1)/3) mesh.set_mesh(p, t) mesh.fd = distance_function mesh.edge_length = edge_length return mesh