def test_point_skew_2d(niters=1, **kwargs): '''Not necesarily conform''' mesh1d = _1d2d_mesh(4) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_points', gmsh_args=[], niters=niters, debug=False, save_geo='', **kwargs) skewed = embedding.nc_edge_encoding.as_vertices assert skewed x = embedding.embedding_mesh.coordinates() embedding.embedding_mesh.init(1, 0) embedding.embedding_mesh.init(0, 1) E2V, V2E = embedding.embedding_mesh.topology()(1, 0), embedding.embedding_mesh.topology()(0, 1) compute_star = lambda v: set(np.hstack([E2V(ei) for ei in V2E(v)])) y = mesh1d.coordinates() e2v = mesh1d.topology()(1, 0) for edge in skewed: y0, y1 = y[e2v(edge)] for piece in skewed[edge]: if not len(piece) == 3: continue x0, x2, x1 = x[piece] # End points were inserted correctly assert is_on_line(x0, y0, y1) v0, v2, v1 = piece # Mid point is in the star mids = compute_star(v0) & compute_star(v1) assert v2 in mids # If there are more this way we get a shortest path if len(mids) > 1: path_length = lambda v: np.linalg.norm(x[v0] - x[v], 2) + np.linalg.norm(x[v1] - x[v], 2) assert v2 == min(mids, key=path_length) vmap = embedding.vertex_map encode = embedding.edge_encoding.as_edges # We can combine the maps to pick correctly embedded edges for edge in range(mesh1d.num_cells()): if edge not in skewed: v0, v1 = e2v(edge) e_ = encode[edge] print e_, '<--' vs = set(vmap[e2v(e_[0])]) assert v0 not in vs vs = set(vmap[e2v(e_[-1])]) assert v1 in vs
def test_line_2d(): '''Not skew''' mesh1d = _1d2d_mesh(3) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_lines', gmsh_args=[], debug=False, save_geo='') # assert not status assert _embed_vertex(mesh1d, embedding) assert _embed_edgecolor(mesh1d, embedding) assert _embed_edgeencode(mesh1d, embedding)
def test_point_stl_3d(): '''Not skew''' mesh1d = _1d3d_mesh(3) embedding = embed_mesh1d(mesh1d, bounding_shape='box.stl', how='as_points', gmsh_args=[], debug=False, save_geo='') # assert not status assert _embed_vertex(mesh1d, embedding) assert _embed_edgecolor(mesh1d, embedding) assert _embed_edgeencode(mesh1d, embedding)
def test_line_3d(): '''Not skew''' mesh1d = _1d3d_mesh(3) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_lines', gmsh_args=[], debug=False, save_geo='') f = df.MeshFunction('size_t', mesh1d, 1, 0) f.array()[:] = np.random.randint(1, 5, f.size()) assert _edge_transfer(f, embedding)
def test_point_3d_vertex(): '''Not skew''' mesh1d = _1d3d_mesh(3) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_points', gmsh_args=[], debug=False, save_geo='') f = df.MeshFunction('double', mesh1d, 0, 0) f.array()[:] = mesh1d.coordinates()[:, 0] assert _vertex_transfer(f, embedding)
def test_line_2d_vertex(): '''Not skew''' mesh1d = _1d2d_mesh(3) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_lines', gmsh_args=[], debug=False, save_geo='') f = df.MeshFunction('double', mesh1d, 0, 0) f.array()[:] = np.linalg.norm(mesh1d.coordinates(), np.inf, axis=1) assert _vertex_transfer(f, embedding)
def test_point_skew_stl_3d(): '''Not necesarily conform''' mesh1d = _1d3d_mesh(4) embedding = embed_mesh1d(mesh1d, bounding_shape='box.stl', how='as_points', gmsh_args=[], niters=1, debug=False, save_geo='') skewed = embedding.nc_edge_encoding.as_vertices assert skewed x = embedding.embedding_mesh.coordinates() embedding.embedding_mesh.init(1, 0) embedding.embedding_mesh.init(0, 1) E2V, V2E = embedding.embedding_mesh.topology()(1, 0), embedding.embedding_mesh.topology()(0, 1) compute_star = lambda v: set(np.hstack([E2V(ei) for ei in V2E(v)])) y = mesh1d.coordinates() e2v = mesh1d.topology()(1, 0) for edge in skewed: y0, y1 = y[e2v(edge)] for piece in skewed[edge]: assert len(piece) == 3 x0, x2, x1 = x[piece] # End points were inserted correctly assert is_on_line(x0, y0, y1) v0, v2, v1 = piece # Mid point is in the star mids = compute_star(v0) & compute_star(v1) assert v2 in mids # If there are more this way we get a shortest path if len(mids) > 1: path_length = lambda v: np.linalg.norm(x[v0] - x[v], 2) + np.linalg.norm(x[v1] - x[v], 2) assert v2 == min(mids, key=path_length)
def test_save(): '''Not necesarily conform''' mesh1d = _1d3d_mesh(4) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_points', gmsh_args=[], niters=1, debug=False, save_geo='') save_embedding(embedding, 'foo/bar') paths = [ os.path.join('foo/bar', buz) for buz in ('mesh.h5', 'vertex_map.txt', 'edge_encoding_0.pkl', 'edge_encoding_1.pkl', 'nc_edge_encoding_0.pkl', 'nc_edge_encoding_1.pkl') ] assert all(map(os.path.exists, paths))
def test_load(): mesh1d = _1d3d_mesh(4) embedding = embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_points', gmsh_args=[], niters=1, debug=False, save_geo='') embedding0 = load_embedding(save_embedding(embedding, 'foo/bar')) assert embedding.edge_encoding == embedding0.edge_encoding assert embedding.nc_edge_encoding == embedding0.nc_edge_encoding assert np.linalg.norm(embedding.vertex_map - embedding0.vertex_map) < 1E-13 assert np.linalg.norm(embedding.edge_coloring.array() - embedding0.edge_coloring.array()) < 1E-13 assert np.linalg.norm(embedding.embedding_mesh.coordinates() - embedding0.embedding_mesh.coordinates()) < 1E-13 assert np.linalg.norm(embedding.embedding_mesh.cells() - embedding0.embedding_mesh.cells()) < 1E-13
df.File('original.pvd') << original_coloring # Getting thick enough vessels radius_filter = lambda v, x: v > 4.5 # 0) Just filter on radius if True: idx = find_edges(radius, predicate=radius_filter) rmesh, rcmap, rlmap = make_submesh(mesh1d, idx) df.File('meshr.pvd') << rmesh embedding = embed_mesh1d(rmesh, bounding_shape=0.01, how='as_lines', gmsh_args=list(sys.argv), save_geo='model', save_msh='model', save_embedding='rat_timo') # This is a check that mesh is okay for solving edge_f = embedding.edge_coloring df.File('rat_timo/embedded_r.pvd') << edge_f edge_f.array()[edge_f.array() > 0] = 1 from perfusion.boundary_conditions import poisson_solve, EdgeDirichletBC V = df.FunctionSpace(embedding.embedding_mesh, 'CG', 1) f = df.Constant(1) bcs = EdgeDirichletBC(V, 2, edge_f, 1)
from mbed.generation import make_line_mesh from mbed.meshing import embed_mesh1d import numpy as np import sys coords = np.array([[0, 0], [1, 0], [1, 1], [0, 1.]]) mesh1d = make_line_mesh(coords, close_path=True) embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_lines', gmsh_args=sys.argv, save_geo='model', save_msh='model', save_embedding='test_embed_line') print() embed_mesh1d(mesh1d, bounding_shape=0.1, how='as_points', gmsh_args=sys.argv, save_geo='model', save_msh='model', niters=2, save_embedding='test_embed_point')