def test_inverse_metric(actx_factory, dim): actx = actx_factory() mesh = mgen.generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, nelements_per_axis=(6, ) * dim, order=4) def m(x): result = np.empty_like(x) result[0] = (1.5 * x[0] + np.cos(x[0]) + 0.1 * np.sin(10 * x[1])) result[1] = (0.05 * np.cos(10 * x[0]) + 1.3 * x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] return result from meshmode.mesh.processing import map_mesh mesh = map_mesh(mesh, m) discr = DiscretizationCollection(actx, mesh, order=4) sym_op = (sym.forward_metric_derivative_mat(mesh.dim).dot( sym.inverse_metric_derivative_mat(mesh.dim)).reshape(-1)) op = bind(discr, sym_op) mat = op(actx).reshape(mesh.dim, mesh.dim) for i in range(mesh.dim): for j in range(mesh.dim): tgt = 1 if i == j else 0 err = actx.np.linalg.norm(mat[i, j] - tgt, ord=np.inf) logger.info("error[%d, %d]: %.5e", i, j, err) assert err < 1.0e-12, (i, j, err)
def test_inverse_metric(actx_factory, dim): actx = actx_factory() mesh = mgen.generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, nelements_per_axis=(6, ) * dim, order=4) def m(x): result = np.empty_like(x) result[0] = (1.5 * x[0] + np.cos(x[0]) + 0.1 * np.sin(10 * x[1])) result[1] = (0.05 * np.cos(10 * x[0]) + 1.3 * x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] return result from meshmode.mesh.processing import map_mesh mesh = map_mesh(mesh, m) dcoll = DiscretizationCollection(actx, mesh, order=4) from grudge.geometry import \ forward_metric_derivative_mat, inverse_metric_derivative_mat mat = forward_metric_derivative_mat(actx, dcoll).dot( inverse_metric_derivative_mat(actx, dcoll)) for i in range(mesh.dim): for j in range(mesh.dim): tgt = 1 if i == j else 0 err = flat_norm(mat[i, j] - tgt, ord=np.inf) logger.info("error[%d, %d]: %.5e", i, j, err) assert err < 1.0e-12, (i, j, err)
def generate_warped_rect_mesh(dim, order, *, nelements_side=None, npoints_side=None, group_cls=None, n=None): """Generate a mesh of a warped square/cube. Mainly useful for testing functionality with curvilinear meshes. """ if n is not None: from warnings import warn warn( "n parameter to generate_warped_rect_mesh is deprecated. Use " "nelements_side or npoints_side instead. n will disappear " "in 2022.", DeprecationWarning, stacklevel=2) if nelements_side is not None: raise TypeError("cannot specify both nelements_side and n") if npoints_side is not None: raise TypeError("cannot specify both npoints_side and n") npoints_side = n else: if npoints_side is not None: if nelements_side is not None: raise TypeError("cannot specify both nelements_side and " "npoints_side") elif nelements_side is not None: npoints_side = nelements_side + 1 assert dim in [2, 3] npoints_per_axis = ( npoints_side, ) * dim if npoints_side is not None else None mesh = generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, npoints_per_axis=npoints_per_axis, order=order, group_cls=group_cls) def m(x): result = np.empty_like(x) result[0] = (1.5 * x[0] + np.cos(x[0]) + 0.1 * np.sin(10 * x[1])) result[1] = (0.05 * np.cos(10 * x[0]) + 1.3 * x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] + np.sin(x[0] / 2) / 2 return result from meshmode.mesh.processing import map_mesh return map_mesh(mesh, m)
def _generate_cross_warped_rect_mesh(dim, order, nelements_side): mesh = mgen.generate_regular_rect_mesh( a=(0, ) * dim, b=(1, ) * dim, nelements_per_axis=(nelements_side, ) * dim, order=order) def m(x): results = np.empty_like(x) results[0] = 1 + 1.5 * (x[0] + 0.25) * (x[1] + 0.3) results[1] = x[1] return results from meshmode.mesh.processing import map_mesh return map_mesh(mesh, m)
def generate_warped_rect_mesh(dim, order, n): """Generate a mesh of a warped line/square/cube. Mainly useful for testing functionality with curvilinear meshes. """ assert dim in [1, 2, 3] mesh = generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, n=(n, ) * dim, order=order) def m(x): result = np.empty_like(x) result[0] = (1.5 * x[0] + np.cos(x[0]) + 0.1 * np.sin(10 * x[1])) result[1] = (0.05 * np.cos(10 * x[0]) + 1.3 * x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] + np.sin(x[0] / 2) / 2 return result from meshmode.mesh.processing import map_mesh return map_mesh(mesh, m)
def test_inverse_metric(ctx_factory, dim): cl_ctx = ctx_factory() queue = cl.CommandQueue(cl_ctx) actx = PyOpenCLArrayContext(queue) from meshmode.mesh.generation import generate_regular_rect_mesh mesh = generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, n=(6, ) * dim, order=4) def m(x): result = np.empty_like(x) result[0] = (1.5 * x[0] + np.cos(x[0]) + 0.1 * np.sin(10 * x[1])) result[1] = (0.05 * np.cos(10 * x[0]) + 1.3 * x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] return result from meshmode.mesh.processing import map_mesh mesh = map_mesh(mesh, m) discr = DGDiscretizationWithBoundaries(actx, mesh, order=4) sym_op = (sym.forward_metric_derivative_mat(mesh.dim).dot( sym.inverse_metric_derivative_mat(mesh.dim)).reshape(-1)) op = bind(discr, sym_op) mat = op(actx).reshape(mesh.dim, mesh.dim) for i in range(mesh.dim): for j in range(mesh.dim): tgt = 1 if i == j else 0 err = flat_norm(mat[i, j] - tgt, np.inf) logger.info("error[%d, %d]: %.5e", i, j, err) assert err < 1.0e-12, (i, j, err)
def test_inverse_metric(ctx_factory, dim): cl_ctx = cl.create_some_context() queue = cl.CommandQueue(cl_ctx) from meshmode.mesh.generation import generate_regular_rect_mesh mesh = generate_regular_rect_mesh(a=(-0.5, ) * dim, b=(0.5, ) * dim, n=(6, ) * dim, order=4) def m(x): result = np.empty_like(x) result[0] = (1.5 * x[0] + np.cos(x[0]) + 0.1 * np.sin(10 * x[1])) result[1] = (0.05 * np.cos(10 * x[0]) + 1.3 * x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] return result from meshmode.mesh.processing import map_mesh mesh = map_mesh(mesh, m) discr = DGDiscretizationWithBoundaries(cl_ctx, mesh, order=4) sym_op = (sym.forward_metric_derivative_mat(mesh.dim).dot( sym.inverse_metric_derivative_mat(mesh.dim)).reshape(-1)) op = bind(discr, sym_op) mat = op(queue).reshape(mesh.dim, mesh.dim) for i in range(mesh.dim): for j in range(mesh.dim): tgt = 1 if i == j else 0 err = np.max(np.abs((mat[i, j] - tgt).get(queue=queue))) print(i, j, err) assert err < 1e-12, (i, j, err)
def generate_warped_rect_mesh(dim, order, n): """Generate a mesh of a warped line/square/cube. Mainly useful for testing functionality with curvilinear meshes. """ assert dim in [1, 2, 3] mesh = generate_regular_rect_mesh( a=(-0.5,)*dim, b=(0.5,)*dim, n=(n,)*dim, order=order) def m(x): result = np.empty_like(x) result[0] = ( 1.5*x[0] + np.cos(x[0]) + 0.1*np.sin(10*x[1])) result[1] = ( 0.05*np.cos(10*x[0]) + 1.3*x[1] + np.sin(x[1])) if len(x) == 3: result[2] = x[2] + np.sin(x[0]) return result from meshmode.mesh.processing import map_mesh return map_mesh(mesh, m)