def test_angles(): # 3-4-5 triangle points = numpy.array([[0.0, 0.0, 0.0], [3.0, 0.0, 0.0], [0.0, 4.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 assert near_equal( mesh.angles, [[numpy.pi / 2], [numpy.arcsin(4.0 / 5.0)], [numpy.arcsin(3.0 / 5.0)]], tol, ) # 30-60-90 triangle a = 1.0 points = numpy.array( [[0.0, 0.0, 0.0], [a / 2, 0.0, 0.0], [0.0, a / 2 * numpy.sqrt(3.0), 0.0]] ) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) ic = mesh.angles / numpy.pi * 180 assert near_equal(ic, [[90], [60], [30]], tol) return
def test_degenerate_flip(): # almost degenerate points = [ [0.0, 0.0], [0.5, -1.0e-5], [1.0, 0.0], [0.5, 0.5], ] cells = [[0, 2, 1], [0, 2, 3]] mesh = meshplex.MeshTri(points, cells) num_flips = mesh.flip_until_delaunay() assert num_flips == 1 ref = np.array([[1, 0, 3], [1, 3, 2]]) assert np.all(mesh.cells("points") == ref) # make sure the same thing happens if the cell is exactly degenerate points = [ [0.0, 0.0], [0.5, 0.0], [1.0, 0.0], [0.5, 0.5], ] cells = [[0, 2, 1], [0, 2, 3]] mesh = meshplex.MeshTri(points, cells) num_flips = mesh.flip_until_delaunay() assert num_flips == 1 ref = np.array([[1, 0, 3], [1, 3, 2]]) assert np.all(mesh.cells("points") == ref)
def test_flip_delaunay(): filename = download_mesh("pacman.msh", "2da8ff96537f844a95a83abb48471b6a") mesh = meshio.read(filename) numpy.random.seed(123) mesh.points[:, :2] += 5.0e-2 * numpy.random.rand(*mesh.points[:, :2].shape) mesh = meshplex.MeshTri(mesh.points, mesh.cells["triangle"]) assert mesh.num_delaunay_violations() == 16 mesh.flip_until_delaunay() assert mesh.num_delaunay_violations() == 0 # Assert edges_cells integrity for cell_gid, edge_gids in enumerate(mesh.cells["edges"]): for edge_gid in edge_gids: num_adj_cells, edge_id = mesh._edge_gid_to_edge_list[edge_gid] assert cell_gid in mesh._edges_cells[num_adj_cells][edge_id] new_cells = mesh.cells["nodes"].copy() new_coords = mesh.node_coords.copy() # Assert that some key values are updated properly mesh2 = meshplex.MeshTri(new_coords, new_cells) assert numpy.all(mesh.idx_hierarchy == mesh2.idx_hierarchy) tol = 1.0e-15 assert near_equal(mesh.half_edge_coords, mesh2.half_edge_coords, tol) assert near_equal(mesh.cell_volumes, mesh2.cell_volumes, tol) assert near_equal(mesh.ei_dot_ej, mesh2.ei_dot_ej, tol) return
def test_flip_delaunay(): mesh = meshio.read(this_dir / "meshes" / "pacman.vtk") numpy.random.seed(123) mesh.points[:, :2] += 5.0e-2 * numpy.random.rand(*mesh.points[:, :2].shape) mesh = meshplex.MeshTri(mesh.points, mesh.get_cells_type("triangle")) assert mesh.num_delaunay_violations() == 16 mesh.flip_until_delaunay() assert mesh.num_delaunay_violations() == 0 # Assert edges_cells integrity for cell_gid, edge_gids in enumerate(mesh.cells["edges"]): for edge_gid in edge_gids: num_adj_cells, edge_id = mesh._edge_gid_to_edge_list[edge_gid] assert cell_gid in mesh._edges_cells[num_adj_cells][edge_id] new_cells = mesh.cells["nodes"].copy() new_coords = mesh.node_coords.copy() # Assert that some key values are updated properly mesh2 = meshplex.MeshTri(new_coords, new_cells) assert numpy.all(mesh.idx_hierarchy == mesh2.idx_hierarchy) tol = 1.0e-15 assert near_equal(mesh.half_edge_coords, mesh2.half_edge_coords, tol) assert near_equal(mesh.cell_volumes, mesh2.cell_volumes, tol) assert near_equal(mesh.ei_dot_ej, mesh2.ei_dot_ej, tol)
def test_set_points(): points = np.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]) cells = np.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) mesh.set_points([0.1, 0.1], [0]) ref = mesh.cell_volumes.copy() mesh2 = meshplex.MeshTri(mesh.points, mesh.cells("points")) assert np.all(np.abs(ref - mesh2.cell_volumes) < 1.0e-10)
def setup(n): radius = 1.0 k = np.arange(n) boundary_pts = radius * np.column_stack( [np.cos(2 * np.pi * k / n), np.sin(2 * np.pi * k / n)]) # Compute the number of interior points such that all triangles can be somewhat # equilateral. edge_length = 2 * np.pi * radius / n domain_area = np.pi - n * (radius**2 / 2 * (edge_length - np.sin(edge_length))) cell_area = np.sqrt(3) / 4 * edge_length**2 target_num_cells = domain_area / cell_area # Euler: # 2 * num_points - num_boundary_edges - 2 = num_cells # <=> # num_interior_points ~= 0.5 * (num_cells + num_boundary_edges) + 1 - num_boundary_points m = int(0.5 * (target_num_cells + n) + 1 - n) # generate random points in circle; # <https://mathworld.wolfram.com/DiskPointPicking.html> for seed in range(0, 255): np.random.seed(seed) r = np.random.rand(m) alpha = 2 * np.pi * np.random.rand(m) interior_pts = np.column_stack( [np.sqrt(r) * np.cos(alpha), np.sqrt(r) * np.sin(alpha)]) pts = np.concatenate([boundary_pts, interior_pts]) tri = Delaunay(pts) # Make sure there are exactly `n` boundary points mesh0 = meshplex.MeshTri(pts, tri.simplices) mesh1 = meshplex.MeshTri(pts, tri.simplices) if np.sum(mesh0.is_boundary_point) == n: break mesh0.create_edges() mesh1.create_edges() num_interior_edges = np.sum(mesh0.is_interior_edge) idx = random.sample(range(num_interior_edges), n // 10) print(num_interior_edges, len(idx), len(idx) / num_interior_edges) # # move interior points a little bit such that we have edges to flip # max_step = np.min(mesh.cell_inradius) / 2 # mesh.points = mesh.points + max_step * np.random.rand(*mesh.points.shape) # print(mesh.num_delaunay_violations) return mesh0, mesh1, idx
def test_update_point_coordinates(): mesh = meshio.read(this_dir / ".." / "meshes" / "pacman.vtu") assert np.all(np.abs(mesh.points[:, 2]) < 1.0e-15) mesh1 = meshplex.MeshTri(mesh.points, mesh.get_cells_type("triangle")) np.random.seed(123) X2 = mesh.points + 1.0e-2 * np.random.rand(*mesh.points.shape) mesh2 = meshplex.MeshTri(X2, mesh.get_cells_type("triangle")) mesh1.points = X2 tol = 1.0e-12 assert is_near_equal(mesh1.cell_volumes, mesh2.cell_volumes, tol)
def test_degenerate_small2(h): points = numpy.array([[0, 0, 0], [1, 0, 0], [0.5, h, 0.0], [0.5, -h, 0.0]]) cells = numpy.array([[0, 1, 2], [0, 1, 3]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-11 # ce_ratios alpha = h - 1.0 / (4 * h) beta = 1.0 / (4 * h) assert near_equal(mesh.ce_ratios_per_interior_edge, [alpha], tol) alpha2 = (h - 1.0 / (4 * h)) / 2 assert near_equal(mesh.ce_ratios, [[beta, beta], [beta, beta], [alpha2, alpha2]], tol) # control volumes alpha1 = 0.125 * (3 * h - 1.0 / (4 * h)) alpha2 = 0.125 * (h + 1.0 / (4 * h)) assert near_equal(mesh.control_volumes, [alpha1, alpha1, alpha2, alpha2], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [[0.5, -12.495, 0.0], [0.5, +12.495, 0.0]], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h, 0.5 * h], tol) assert mesh.num_delaunay_violations() == 1 return
def get_disk_mesh(k): import meshzoo points, cells = meshzoo.disk(6, k + 1) out = meshplex.MeshTri(points, cells) # out.show() return out
def test_jacobian(): from test_ginzburg_landau import GinzburgLandau a = 10.0 n = 10 points, cells = meshzoo.rectangle(-a / 2, a / 2, -a / 2, a / 2, n, n) mesh = meshplex.MeshTri(points, cells) gl = GinzburgLandau(mesh) glr = GinzburgLandauReal(mesh) n = points.shape[0] numpy.random.seed(123) for _ in range(10): psi = numpy.random.rand(n) + 1j * numpy.random.rand(n) mu = numpy.random.rand(1)[0] jac0 = gl.jacobian(psi, mu) jac1 = glr.jacobian(to_real(psi), mu) for _ in range(10): phi = numpy.random.rand(n) + 1j * numpy.random.rand(n) out0 = (jac0 * phi) * mesh.control_volumes out1 = to_complex(jac1 * to_real(phi)) assert numpy.all(numpy.abs(out0 - out1) < 1.0e-12)
def test_jacobian(): from test_ginzburg_landau import GinzburgLandau a = 10.0 n = 10 points, cells = meshzoo.rectangle_tri((-a / 2, -a / 2), (a / 2, a / 2), n) # add column with zeros for magnetic potential points = np.column_stack([points, np.zeros(points.shape[0])]) mesh = meshplex.MeshTri(points, cells) gl = GinzburgLandau(mesh) glr = GinzburgLandauReal(mesh) n = points.shape[0] np.random.seed(123) for _ in range(10): psi = np.random.rand(n) + 1j * np.random.rand(n) mu = np.random.rand(1)[0] jac0 = gl.jacobian(psi, mu) jac1 = glr.jacobian(to_real(psi), mu) for _ in range(10): phi = np.random.rand(n) + 1j * np.random.rand(n) out0 = (jac0 * phi) * mesh.control_volumes out1 = to_complex(jac1 * to_real(phi)) assert np.all(np.abs(out0 - out1) < 1.0e-12)
def gosplElev(coords, cells, elev, gmesh, visvtk=False): Gmesh = meshplex.MeshTri(coords, cells) s = Gmesh.idx_hierarchy.shape a = np.sort(Gmesh.idx_hierarchy.reshape(s[0], -1).T) if meshplex.__version__ >= "0.16.0": Gmesh.edges = {"points": np.unique(a, axis=0)} ngbNbs, ngbID = definegtin(len(coords), Gmesh.cells("points"), Gmesh.edges["points"]) elif meshplex.__version__ >= "0.14.0": Gmesh.edges = {"points": np.unique(a, axis=0)} ngbNbs, ngbID = definegtin(len(coords), Gmesh.cells["points"], Gmesh.edges["points"]) else: Gmesh.edges = {"nodes": np.unique(a, axis=0)} ngbNbs, ngbID = definegtin(len(coords), Gmesh.cells["nodes"], Gmesh.edges["nodes"]) np.savez_compressed(gmesh, v=coords, c=cells, n=ngbID[:, :8].astype(int), z=elev) if visvtk: paleovtk = gmesh + ".vtk" vis_mesh = meshio.Mesh(coords, {"triangle": cells}, point_data={"z": elev}) meshio.write(paleovtk, vis_mesh) print("Writing VTK file {}".format(paleovtk)) return
def run_SeismicMesh(ef, HMIN=75.0): bbox = (-12000, 0.0, 0.0, 67000.0) rectangle = Rectangle(bbox) t1 = time.time() points, cells = generate_mesh( domain=rectangle, edge_length=ef, h0=HMIN, max_iter=25, delta_t=0.3, ) points, cells = geometry.delete_boundary_entities(points, cells, dim=2, min_qual=0.10) elapsed = time.time() - t1 # meshio.write_points_cells( # "BP2004_sm.vtk", # points, # [("triangle", cells)], # file_format="vtk", # ) plex = meshplex.MeshTri(points, cells) quality = numpy.abs(plex.cell_quality) num_cells = len(cells) num_vertices = len(points) return quality, elapsed, num_vertices, num_cells
def test_update_node_coordinates(): mesh = meshio.read(this_dir / "meshes" / "pacman.vtk") assert numpy.all(numpy.abs(mesh.points[:, 2]) < 1.0e-15) mesh1 = meshplex.MeshTri(mesh.points, mesh.get_cells_type("triangle")) numpy.random.seed(123) X2 = mesh.points + 1.0e-2 * numpy.random.rand(*mesh.points.shape) mesh2 = meshplex.MeshTri(X2, mesh.get_cells_type("triangle")) mesh1.node_coords = X2 mesh1.update_values() tol = 1.0e-12 assert near_equal(mesh1.ei_dot_ej, mesh2.ei_dot_ej, tol) assert near_equal(mesh1.cell_volumes, mesh2.cell_volumes, tol)
def test_flip_simple_opposite_orientation(): # 3 3 # A A # /|\ / \ # 1/ | \4 1/ 1 \4 # / | \ / \ # 0/ 0 3 \2 ==> 0/___3___\2 # \ | 1 / \ / # \ | / \ / # 0\ | /2 0\ 0 /2 # \|/ \ / # V V # 1 1 # points = np.array([[-0.1, 0.0], [0.0, -1.0], [0.1, 0.0], [0.0, 1.1]]) cells = np.array([[0, 1, 3], [1, 3, 2]]) mesh = meshplex.MeshTri(points, cells) mesh.create_facets() assert mesh.num_delaunay_violations == 1 assert np.array_equal(mesh.edges["points"], [[0, 1], [0, 3], [1, 2], [1, 3], [2, 3]]) assert np.array_equal(mesh.cells("edges"), [[3, 1, 0], [4, 2, 3]]) assert_mesh_consistency(mesh) # mesh.show() mesh.flip_until_delaunay() assert_mesh_consistency(mesh) assert mesh.num_delaunay_violations == 0 assert np.array_equal(mesh.edges["points"], [[0, 1], [0, 3], [1, 2], [0, 2], [2, 3]]) assert np.array_equal(mesh.cells("points"), [[0, 1, 2], [0, 2, 3]]) assert np.array_equal(mesh.cells("edges"), [[2, 3, 0], [4, 1, 3]])
def test_flip_orientation(): points = np.array([[0.0, +0.0], [0.5, -0.1], [1.0, +0.0], [0.5, +0.1]]) # preserve positive orientation cells = np.array([[0, 1, 2], [0, 2, 3]]) mesh = meshplex.MeshTri(points, cells) assert np.all(mesh.signed_cell_volumes > 0.0) mesh.flip_until_delaunay() assert np.all(mesh.signed_cell_volumes > 0.0) # also preserve negative orientation cells = np.array([[0, 2, 1], [0, 3, 2]]) mesh = meshplex.MeshTri(points, cells) assert np.all(mesh.signed_cell_volumes < 0.0) mesh.flip_until_delaunay() assert np.all(mesh.signed_cell_volumes < 0.0)
def test(): class EnergyEdgeKernel: def __init__(self): self.subdomains = [None] return def eval(self, mesh, cell_mask): edge_ce_ratio = mesh.ce_ratios[..., cell_mask] beta = 1.0 return numpy.array([ [edge_ce_ratio, -edge_ce_ratio * numpy.exp(1j * beta)], [-edge_ce_ratio * numpy.exp(-1j * beta), edge_ce_ratio], ]) vertices, cells = meshzoo.rectangle(0.0, 2.0, 0.0, 1.0, 101, 51) mesh = meshplex.MeshTri(vertices, cells) matrix = pyfvm.get_fvm_matrix(mesh, [EnergyEdgeKernel()], [], [], []) rhs = mesh.control_volumes.copy() sa = pyamg.smoothed_aggregation_solver(matrix, smooth="energy") u = sa.solve(rhs, tol=1e-10) # Cannot write complex data ot VTU; split real and imaginary parts first. # <http://stackoverflow.com/a/38902227/353337> mesh.write("out.vtk", point_data={"u": u.view("(2,)float")}) return
def test_flip_same_edge_twice(): points = numpy.array([[0.0, +0.0, 0.0], [0.5, -0.1, 0.0], [1.0, +0.0, 0.0], [0.5, +0.1, 0.0]]) cells = numpy.array([[0, 1, 2], [0, 2, 3]]) mesh = meshplex.MeshTri(points, cells) assert mesh.num_delaunay_violations() == 1 mesh.flip_until_delaunay() assert mesh.num_delaunay_violations() == 0 # Assert edges_cells integrity for cell_gid, edge_gids in enumerate(mesh.cells["edges"]): for edge_gid in edge_gids: num_adj_cells, edge_id = mesh._edge_gid_to_edge_list[edge_gid] assert cell_gid in mesh._edges_cells[num_adj_cells][edge_id] new_points = numpy.array([[0.0, +0.0, 0.0], [0.1, -0.5, 0.0], [0.2, +0.0, 0.0], [0.1, +0.5, 0.0]]) mesh.node_coords = new_points mesh.update_values() assert mesh.num_delaunay_violations() == 1 mesh.flip_until_delaunay() assert mesh.num_delaunay_violations() == 0 mesh.show() return
def test_flip_delaunay_near_boundary_preserve_boundary_count(): # This test is to make sure meshplex preserves the boundary node count. points = numpy.array([ [+0.0, +0.0, 0.0], [+0.5, -0.5, 0.0], [+0.5, +0.5, 0.0], [+0.0, +0.6, 0.0], [-0.5, +0.5, 0.0], [-0.5, -0.5, 0.0], ]) cells = numpy.array([[0, 1, 2], [0, 2, 4], [0, 4, 5], [0, 5, 1], [2, 3, 4]]) mesh = meshplex.MeshTri(points, cells) mesh.create_edges() assert mesh.num_delaunay_violations() == 1 mesh.mark_boundary() is_boundary_node_ref = [False, True, True, True, True, True] assert numpy.array_equal(mesh.is_boundary_node, is_boundary_node_ref) mesh.flip_until_delaunay() mesh.mark_boundary() assert numpy.array_equal(mesh.is_boundary_node, is_boundary_node_ref) return
def test_shell(): points = numpy.array( [ [+0.0, +0.0, +1.0], [+1.0, +0.0, +0.0], [+0.0, +1.0, +0.0], [-1.0, +0.0, +0.0], [+0.0, -1.0, +0.0], ] ) cells = numpy.array([[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 1, 4]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 ce_ratios = 0.5 / numpy.sqrt(3.0) * numpy.ones((4, 3)) assert near_equal(mesh.ce_ratios.T, ce_ratios, tol) cv = numpy.array([2.0, 1.0, 1.0, 1.0, 1.0]) / numpy.sqrt(3.0) assert near_equal(mesh.control_volumes, cv, tol) cell_vols = numpy.sqrt(3.0) / 2.0 * numpy.ones(4) assert near_equal(mesh.cell_volumes, cell_vols, tol) assert mesh.num_delaunay_violations() == 0 return
def test(): class D1(Subdomain): def is_inside(self, x): return x[1] < 0.5 is_boundary_only = True class Poisson: def apply(self, u): return (integrate(lambda x: -n_dot_grad(u(x)), dS) + integrate(lambda x: 3.0, dGamma) - integrate(lambda x: 1.0, dV)) def dirichlet(self, u): return [(u, D1())] vertices, cells = meshzoo.rectangle(0.0, 1.0, 0.0, 1.0, 51, 51) mesh = meshplex.MeshTri(vertices, cells) matrix, rhs = pyfvm.discretize_linear(Poisson(), mesh) u = linalg.spsolve(matrix, rhs) mesh.write("out.vtk", point_data={"u": u}) return
def test(): class Bratu: def apply(self, u): return integrate(lambda x: -n_dot_grad(u(x)), dS) - integrate( lambda x: 2.0 * exp(u(x)), dV) def dirichlet(self, u): return [(u, Boundary())] vertices, cells = meshzoo.rectangle(0.0, 2.0, 0.0, 1.0, 101, 51) mesh = meshplex.MeshTri(vertices, cells) f, jac_u = pyfvm.discretize(Bratu(), mesh) def jacobian_solver(u0, rhs): from scipy.sparse import linalg jac = jac_u.get_linear_operator(u0) return linalg.spsolve(jac, rhs) u0 = numpy.zeros(len(vertices)) u = pyfvm.newton(lambda u: f.eval(u), jacobian_solver, u0) # import scipy.optimize # u = scipy.optimize.newton_krylov(f_eval, u0) mesh.write("out.vtk", point_data={"u": u}) return
def run_gmsh(ef, HMIN=75.0): with pygmsh.geo.Geometry() as geom: geom.add_polygon([ [-12e3, 0.0], [-12e3, 67e3], [0.0, 67e3], [0.0, 0.0], ]) geom.set_mesh_size_callback(lambda dim, tag, x, y, z: (ef.eval([x, y]))) gmsh.option.setNumber("Mesh.Algorithm", 5) gmsh.option.setNumber("Mesh.CharacteristicLengthExtendFromBoundary", 0) t1 = time.time() mesh = geom.generate_mesh() elapsed = time.time() - t1 points = mesh.points cells = mesh.cells[1].data num_cells = len(cells) num_vertices = len(points) plex = meshplex.MeshTri(points, cells) quality = numpy.abs(plex.cell_quality) # mesh.write("BP2004_gmsh.vtk") return quality, elapsed, num_vertices, num_cells
def compute(fun, h): tic = time.time() points, cells = fun(h) toc = time.time() if cells.shape[1] == 3: mesh = meshplex.MeshTri(points, cells) else: assert cells.shape[1] == 4 mesh = meshplex.MeshTetra(points, cells) if fun.__name__ == "sphere" or numpy.min(mesh.q_radius_ratio) < 1.0e-5: num_poisson_steps = numpy.nan else: poisson_tol = 1.0e-10 # num_poisson_steps = get_poisson_steps_dolfin(points, cells, poisson_tol) num_poisson_steps = get_poisson_steps_scikitfem(points, cells, poisson_tol) return { "time": toc - tic, # convert to python float types to JSON compatibility "quality_min": float(numpy.min(mesh.q_radius_ratio)), "quality_avg": float(numpy.average(mesh.q_radius_ratio)), "energy": energy(mesh), "num_nodes": mesh.points.shape[0], "num_poisson_steps": num_poisson_steps, }
def test_regular_tri_order(): points = numpy.array([[0.0, 1.0, 0.0], [0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) assert all((mesh.cells["nodes"] == [0, 1, 2]).flat) tol = 1.0e-14 # ce_ratios assert near_equal(mesh.ce_ratios.T, [0.5, 0.0, 0.5], tol) # control volumes assert near_equal(mesh.control_volumes, [0.125, 0.25, 0.125], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.5, 0.5, 0.0], tol) # centroids assert near_equal( mesh.control_volume_centroids, [[1.0 / 6.0, 2.0 / 3.0, 0.0], [0.25, 0.25, 0.0], [2.0 / 3.0, 1.0 / 6.0, 0.0]], tol, ) assert mesh.num_delaunay_violations() == 0 return
def test_regular_tri2(a): points = (numpy.array([ [-0.5, -0.5 * numpy.sqrt(3.0), 0], [-0.5, +0.5 * numpy.sqrt(3.0), 0], [1, 0, 0], ]) / numpy.sqrt(3) * a) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 # ce_ratios val = 0.5 / numpy.sqrt(3.0) assert near_equal(mesh.ce_ratios, [val, val, val], tol) # control volumes vol = numpy.sqrt(3.0) / 4 * a**2 assert near_equal(mesh.control_volumes, [vol / 3.0, vol / 3.0, vol / 3.0], tol) # cell volumes assert near_equal(mesh.cell_volumes, [vol], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.0, 0.0, 0.0], tol) return
def run_SeismicMesh(ef, HMIN=75.0): bbox = (-12000.0, 0.0, 0.0, 67000.0) rectangle = Rectangle(bbox) t1 = time.time() points, cells = generate_mesh( domain=rectangle, edge_length=ef, verbose=0, max_iter=25, ) elapsed = time.time() - t1 # import meshio # meshio.write_points_cells( # "BP2004_sm" + str(HMIN) + ".vtk", # points, # [("triangle", cells)], # file_format="vtk", # ) plex = meshplex.MeshTri(points, cells) quality = numpy.abs(plex.cell_quality) num_cells = len(cells) num_vertices = len(points) return quality, elapsed, num_vertices, num_cells
def test_degenerate_small0b(h): points = numpy.array([[0, 0, 0], [1, 0, 0], [0.5, h, 0.0]]) cells = numpy.array([[0, 1, 2]]) mesh = meshplex.MeshTri(points, cells) tol = 1.0e-14 # edge lengths el = numpy.sqrt(0.5**2 + h**2) assert near_equal(mesh.edge_lengths.T, [el, el, 1.0], tol) # ce_ratios ce0 = 0.5 / h * (h**2 - 0.25) ce12 = 0.25 / h assert near_equal(mesh.ce_ratios.T, [ce12, ce12, ce0], tol) # control volumes cv12 = 0.25 * (1.0**2 * ce0 + (0.25 + h**2) * ce12) cv0 = 0.5 * (0.25 + h**2) * ce12 assert near_equal(mesh.control_volumes, [cv12, cv12, cv0], tol) # cell volumes assert near_equal(mesh.cell_volumes, [0.5 * h], tol) # circumcenters assert near_equal(mesh.cell_circumcenters, [0.5, 0.375, 0.0], tol) assert mesh.num_delaunay_violations() == 0 return
def run_SeismicMesh(HMIN=0.01): bbox = (-1.0, 1.0, -1.0, 1.0) disk = SeismicMesh.geometry.Disk([0, 0], 1) def fh(x): return numpy.array([HMIN] * len(x)) t1 = time.time() points, cells = SeismicMesh.generate_mesh( bbox=bbox, h0=HMIN, domain=disk, edge_length=fh, max_iter=25, delta_t=0.3, ) elapsed = time.time() - t1 # meshio.write_points_cells( # "SeismicMesh_circle.vtk", # points, # [("triangle", cells)], # ) plex = meshplex.MeshTri(points, cells) quality = numpy.abs(plex.cell_quality) num_cells = len(cells) num_vertices = len(points) return quality, elapsed, num_vertices, num_cells
def circle_random2(n, radius, seed=0): """Boundary points are random, too.""" # generate random points in circle; <https://mathworld.wolfram.com/DiskPointPicking.html> np.random.seed(seed) r = np.random.rand(n) alpha = 2 * np.pi * np.random.rand(n) pts = np.column_stack([np.sqrt(r) * np.cos(alpha), np.sqrt(r) * np.sin(alpha)]) tri = Delaunay(pts) # Make sure there are exactly `n` boundary points mesh = meshplex.MeshTri(pts, tri.simplices) # inflate the mesh such that the boundary points average around the radius boundary_pts = pts[mesh.is_boundary_point] dist = np.sqrt(np.einsum("ij,ij->i", boundary_pts, boundary_pts)) avg_dist = np.sum(dist) / len(dist) mesh.points = pts / avg_dist # boundary_pts = pts[mesh.is_boundary_point] # dist = np.sqrt(np.einsum("ij,ij->i", boundary_pts, boundary_pts)) # avg_dist = np.sum(dist) / len(dist) # print(avg_dist) # now move all boundary points to the circle # bpts = pts[mesh.is_boundary_point] # pts[mesh.is_boundary_point] = ( # bpts.T / np.sqrt(np.einsum("ij,ij->i", bpts, bpts)) # ).T # bpts = pts[mesh.is_boundary_point] # print(np.sqrt(np.einsum("ij,ij->i", bpts, bpts))) # mesh = meshplex.MeshTri(pts, tri.simplices) # mesh.show() return pts, tri.simplices