def refine(self, density_discr, refiner, refine_flags, factory, debug): """ Refine the underlying mesh and discretization. """ if isinstance(refine_flags, cl.array.Array): refine_flags = refine_flags.get(self.queue) refine_flags = refine_flags.astype(np.bool) with ProcessLogger(logger, "refine mesh"): refiner.refine(refine_flags) from meshmode.discretization.connection import make_refinement_connection conn = make_refinement_connection(refiner, density_discr, factory) return conn
def refine(self, density_discr, refiner, refine_flags, factory, debug): """ Refine the underlying mesh and discretization. """ if isinstance(refine_flags, cl.array.Array): refine_flags = refine_flags.get(self.queue) refine_flags = refine_flags.astype(np.bool) with ProcessLogger(logger, "refine mesh"): refiner.refine(refine_flags) from meshmode.discretization.connection import make_refinement_connection conn = make_refinement_connection(refiner, density_discr, factory) return conn
def create_refined_connection(queue, discr, threshold=0.3): from meshmode.mesh.refinement import RefinerWithoutAdjacency from meshmode.discretization.connection import make_refinement_connection from meshmode.discretization.poly_element import \ InterpolatoryQuadratureSimplexGroupFactory flags = np.random.rand(discr.mesh.nelements) < threshold refiner = RefinerWithoutAdjacency(discr.mesh) refiner.refine(flags) discr_order = discr.groups[0].order connection = make_refinement_connection(refiner, discr, InterpolatoryQuadratureSimplexGroupFactory(discr_order)) return connection
def create_refined_connection(queue, discr, threshold=0.3): from meshmode.mesh.refinement import RefinerWithoutAdjacency from meshmode.discretization.connection import make_refinement_connection from meshmode.discretization.poly_element import \ InterpolatoryQuadratureSimplexGroupFactory flags = np.random.rand(discr.mesh.nelements) < threshold refiner = RefinerWithoutAdjacency(discr.mesh) refiner.refine(flags) discr_order = discr.groups[0].order connection = make_refinement_connection(refiner, discr, InterpolatoryQuadratureSimplexGroupFactory(discr_order)) return connection
def refine(self, density_discr, refiner, refine_flags, factory, debug): """ Refine the underlying mesh and discretization. """ actx = self.array_context if isinstance(refine_flags, actx.array_types): refine_flags = actx.to_numpy(refine_flags) refine_flags = refine_flags.astype(bool) with ProcessLogger(logger, "refine mesh"): refiner.refine(refine_flags) from meshmode.discretization.connection import make_refinement_connection conn = make_refinement_connection(actx, refiner, density_discr, factory) return conn
def test_refinement_connection(actx_factory, refiner_cls, group_factory, mesh_name, dim, mesh_pars, mesh_order, refine_flags, visualize=False): group_cls = group_factory.mesh_group_class if issubclass(group_cls, TensorProductElementGroup): if mesh_name in ["circle", "blob"]: pytest.skip("mesh does not have tensor product support") from random import seed seed(13) actx = actx_factory() # discretization order order = 5 from meshmode.discretization import Discretization from meshmode.discretization.connection import (make_refinement_connection, check_connection) from pytools.convergence import EOCRecorder eoc_rec = EOCRecorder() for mesh_par in mesh_pars: # {{{ get mesh if mesh_name == "circle": assert dim == 1 h = 1 / mesh_par mesh = make_curve_mesh(partial(ellipse, 1), np.linspace(0, 1, mesh_par + 1), order=mesh_order) elif mesh_name == "blob": if mesh_order == 5: pytest.xfail( "https://gitlab.tiker.net/inducer/meshmode/issues/2") assert dim == 2 mesh = get_blob_mesh(mesh_par, mesh_order) h = float(mesh_par) elif mesh_name == "warp": mesh = mgen.generate_warped_rect_mesh(dim, order=mesh_order, n=mesh_par, group_cls=group_cls) h = 1 / mesh_par else: raise ValueError("mesh_name not recognized") # }}} from meshmode.mesh.processing import find_bounding_box mesh_bbox_low, mesh_bbox_high = find_bounding_box(mesh) mesh_ext = mesh_bbox_high - mesh_bbox_low def f(x): result = 1 if mesh_name == "blob": factor = 15 else: factor = 9 for iaxis in range(len(x)): result = result * actx.np.sin(factor * (x[iaxis] / mesh_ext[iaxis])) return result discr = Discretization(actx, mesh, group_factory(order)) refiner = refiner_cls(mesh) flags = refine_flags(mesh) refiner.refine(flags) connection = make_refinement_connection(actx, refiner, discr, group_factory(order)) check_connection(actx, connection) fine_discr = connection.to_discr x = thaw(actx, discr.nodes()) x_fine = thaw(actx, fine_discr.nodes()) f_coarse = f(x) f_interp = connection(f_coarse) f_true = f(x_fine) if visualize == "dots": import matplotlib.pyplot as plt x = x.get(actx.queue) err = np.array( np.log10(1e-16 + np.abs((f_interp - f_true).get(actx.queue))), dtype=float) import matplotlib.cm as cm cmap = cm.ScalarMappable(cmap=cm.jet) cmap.set_array(err) plt.scatter(x[0], x[1], c=cmap.to_rgba(err), s=20, cmap=cmap) plt.colorbar(cmap) plt.show() elif visualize == "vtk": from meshmode.discretization.visualization import make_visualizer fine_vis = make_visualizer(actx, fine_discr, mesh_order) fine_vis.write_vtk_file( "refine-fine-%s-%dd-%s.vtu" % (mesh_name, dim, mesh_par), [ ("f_interp", f_interp), ("f_true", f_true), ]) err = actx.np.linalg.norm(f_interp - f_true, np.inf) eoc_rec.add_data_point(h, err) order_slack = 0.5 if mesh_name == "blob" and order > 1: order_slack = 1 print(eoc_rec) assert (eoc_rec.order_estimate() >= order - order_slack or eoc_rec.max_error() < 1e-14)
def test_refinement_connection(ctx_getter, group_factory, mesh_name, dim, mesh_pars, mesh_order, refine_flags, plot_mesh=False): from random import seed seed(13) # Discretization order order = 5 cl_ctx = ctx_getter() queue = cl.CommandQueue(cl_ctx) from meshmode.discretization import Discretization from meshmode.discretization.connection import (make_refinement_connection, check_connection) from pytools.convergence import EOCRecorder eoc_rec = EOCRecorder() def f(x): from six.moves import reduce return 0.1 * reduce(lambda x, y: x * cl.clmath.sin(5 * y), x) for mesh_par in mesh_pars: # {{{ get mesh if mesh_name == "circle": assert dim == 1 h = 1 / mesh_par mesh = make_curve_mesh(partial(ellipse, 1), np.linspace(0, 1, mesh_par + 1), order=mesh_order) elif mesh_name == "blob": if mesh_order == 5: pytest.xfail( "https://gitlab.tiker.net/inducer/meshmode/issues/2") assert dim == 2 h = mesh_par mesh = gen_blob_mesh(h, mesh_order) elif mesh_name == "warp": from meshmode.mesh.generation import generate_warped_rect_mesh mesh = generate_warped_rect_mesh(dim, order=mesh_order, n=mesh_par) h = 1 / mesh_par else: raise ValueError("mesh_name not recognized") # }}} discr = Discretization(cl_ctx, mesh, group_factory(order)) refiner = Refiner(mesh) flags = refine_flags(mesh) refiner.refine(flags) connection = make_refinement_connection(refiner, discr, group_factory(order)) check_connection(connection) fine_discr = connection.to_discr x = discr.nodes().with_queue(queue) x_fine = fine_discr.nodes().with_queue(queue) f_coarse = f(x) f_interp = connection(queue, f_coarse).with_queue(queue) f_true = f(x_fine).with_queue(queue) if plot_mesh: import matplotlib.pyplot as plt x = x.get(queue) err = np.array(np.log10(1e-16 + np.abs((f_interp - f_true).get(queue))), dtype=float) import matplotlib.cm as cm cmap = cm.ScalarMappable(cmap=cm.jet) cmap.set_array(err) plt.scatter(x[0], x[1], c=cmap.to_rgba(err), s=20, cmap=cmap) plt.colorbar(cmap) plt.show() import numpy.linalg as la err = la.norm((f_interp - f_true).get(queue), np.inf) eoc_rec.add_data_point(h, err) print(eoc_rec) assert (eoc_rec.order_estimate() >= order - 0.5 or eoc_rec.max_error() < 1e-14)
def test_refinement_connection( ctx_getter, refiner_cls, group_factory, mesh_name, dim, mesh_pars, mesh_order, refine_flags, visualize=False): from random import seed seed(13) # Discretization order order = 5 cl_ctx = ctx_getter() queue = cl.CommandQueue(cl_ctx) from meshmode.discretization import Discretization from meshmode.discretization.connection import ( make_refinement_connection, check_connection) from pytools.convergence import EOCRecorder eoc_rec = EOCRecorder() for mesh_par in mesh_pars: # {{{ get mesh if mesh_name == "circle": assert dim == 1 h = 1 / mesh_par mesh = make_curve_mesh( partial(ellipse, 1), np.linspace(0, 1, mesh_par + 1), order=mesh_order) elif mesh_name == "blob": if mesh_order == 5: pytest.xfail("https://gitlab.tiker.net/inducer/meshmode/issues/2") assert dim == 2 mesh = get_blob_mesh(mesh_par, mesh_order) h = float(mesh_par) elif mesh_name == "warp": from meshmode.mesh.generation import generate_warped_rect_mesh mesh = generate_warped_rect_mesh(dim, order=mesh_order, n=mesh_par) h = 1/mesh_par else: raise ValueError("mesh_name not recognized") # }}} from meshmode.mesh.processing import find_bounding_box mesh_bbox_low, mesh_bbox_high = find_bounding_box(mesh) mesh_ext = mesh_bbox_high-mesh_bbox_low def f(x): result = 1 if mesh_name == "blob": factor = 15 else: factor = 9 for iaxis in range(len(x)): result = result * cl.clmath.sin(factor * (x[iaxis]/mesh_ext[iaxis])) return result discr = Discretization(cl_ctx, mesh, group_factory(order)) refiner = refiner_cls(mesh) flags = refine_flags(mesh) refiner.refine(flags) connection = make_refinement_connection( refiner, discr, group_factory(order)) check_connection(connection) fine_discr = connection.to_discr x = discr.nodes().with_queue(queue) x_fine = fine_discr.nodes().with_queue(queue) f_coarse = f(x) f_interp = connection(queue, f_coarse).with_queue(queue) f_true = f(x_fine).with_queue(queue) if visualize == "dots": import matplotlib.pyplot as plt x = x.get(queue) err = np.array(np.log10( 1e-16 + np.abs((f_interp - f_true).get(queue))), dtype=float) import matplotlib.cm as cm cmap = cm.ScalarMappable(cmap=cm.jet) cmap.set_array(err) plt.scatter(x[0], x[1], c=cmap.to_rgba(err), s=20, cmap=cmap) plt.colorbar(cmap) plt.show() elif visualize == "vtk": from meshmode.discretization.visualization import make_visualizer fine_vis = make_visualizer(queue, fine_discr, mesh_order) fine_vis.write_vtk_file( "refine-fine-%s-%dd-%s.vtu" % (mesh_name, dim, mesh_par), [ ("f_interp", f_interp), ("f_true", f_true), ]) import numpy.linalg as la err = la.norm((f_interp - f_true).get(queue), np.inf) eoc_rec.add_data_point(h, err) order_slack = 0.5 if mesh_name == "blob" and order > 1: order_slack = 1 print(eoc_rec) assert ( eoc_rec.order_estimate() >= order-order_slack or eoc_rec.max_error() < 1e-14)