Skip to content

Commit 7aed028

Browse files
committed
plot stretch factors using high-order vtk
1 parent 966edba commit 7aed028

File tree

1 file changed

+80
-49
lines changed

1 file changed

+80
-49
lines changed

test/test_symbolic.py

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -422,30 +422,6 @@ def idx(i, j):
422422
return mm.Mesh(vertices, [grp.copy(nodes=nodes)], is_conforming=True)
423423

424424

425-
def make_simplex_stretch_factors(ambient_dim):
426-
from pytential.symbolic.primitives import \
427-
_equilateral_parametrization_derivative_matrix
428-
equi_pder = _equilateral_parametrization_derivative_matrix(ambient_dim)
429-
equi_form1 = sym.cse(equi_pder.T @ equi_pder, "pd_mat_jtj")
430-
431-
from pytential.symbolic.primitives import _small_mat_eigenvalues
432-
return [
433-
sym.cse(sym.sqrt(s), f"mapping_singval_{i}")
434-
for i, s in enumerate(_small_mat_eigenvalues(4 * equi_form1))
435-
]
436-
437-
438-
def make_quad_stretch_factors(ambient_dim):
439-
pder = sym.parametrization_derivative_matrix(ambient_dim, ambient_dim - 1)
440-
form1 = sym.cse(pder.T @ pder, "pd_mat_jtj")
441-
442-
from pytential.symbolic.primitives import _small_mat_eigenvalues
443-
return [
444-
sym.cse(sym.sqrt(s), f"mapping_singval_{i}")
445-
for i, s in enumerate(_small_mat_eigenvalues(4 * form1))
446-
]
447-
448-
449425
def make_gmsh_sphere(order: int, cls: type):
450426
from meshmode.mesh.io import ScriptSource
451427
from meshmode.mesh import SimplexElementGroup, TensorProductElementGroup
@@ -524,49 +500,104 @@ def make_gmsh_torus(order: int, cls: type):
524500
)
525501

526502

503+
def make_simplex_stretch_factors(ambient_dim):
504+
from pytential.symbolic.primitives import \
505+
_equilateral_parametrization_derivative_matrix
506+
equi_pder = _equilateral_parametrization_derivative_matrix(ambient_dim)
507+
equi_form1 = sym.cse(equi_pder.T @ equi_pder, "pd_mat_jtj")
508+
509+
from pytential.symbolic.primitives import _small_mat_eigenvalues
510+
return [
511+
sym.cse(sym.sqrt(s), f"mapping_singval_{i}")
512+
for i, s in enumerate(_small_mat_eigenvalues(4 * equi_form1))
513+
]
514+
515+
516+
def make_quad_stretch_factors(ambient_dim):
517+
pder = sym.parametrization_derivative_matrix(ambient_dim, ambient_dim - 1)
518+
form1 = sym.cse(pder.T @ pder, "pd_mat_jtj")
519+
520+
from pytential.symbolic.primitives import _small_mat_eigenvalues
521+
return [
522+
sym.cse(sym.sqrt(s), f"mapping_singval_{i}")
523+
for i, s in enumerate(_small_mat_eigenvalues(4 * form1))
524+
]
525+
526+
527527
@pytest.mark.parametrize("order", [4, 8])
528-
def test_stretch_factor(actx_factory, order, mesh_name="torus", visualize=False):
528+
def test_stretch_factor(actx_factory, order,
529+
mesh_name="torus", metric_type="eigs",
530+
visualize=False):
529531
logging.basicConfig(level=logging.INFO)
530532
actx = actx_factory()
531533

532534
from meshmode.mesh import SimplexElementGroup, TensorProductElementGroup
533535
if mesh_name == "torus":
534-
square_mesh = make_torus_mesh(order, TensorProductElementGroup)
536+
quad_mesh = make_torus_mesh(order, TensorProductElementGroup)
535537
simplex_mesh = make_torus_mesh(order, SimplexElementGroup)
536538
elif mesh_name == "twisted":
537-
square_mesh = make_twisted_mesh(order, TensorProductElementGroup)
539+
quad_mesh = make_twisted_mesh(order, TensorProductElementGroup)
538540
simplex_mesh = make_twisted_mesh(order, SimplexElementGroup)
539541
elif mesh_name == "sphere":
540542
from meshmode.mesh.generation import generate_sphere
541543
simplex_mesh = generate_sphere(1, order=order,
542544
uniform_refinement_rounds=1,
543545
group_cls=SimplexElementGroup)
544-
square_mesh = generate_sphere(1, order=order,
546+
quad_mesh = generate_sphere(1, order=order,
545547
uniform_refinement_rounds=1,
546548
group_cls=TensorProductElementGroup)
547549
elif mesh_name == "gmsh_sphere":
548550
simplex_mesh = make_gmsh_sphere(order, cls=SimplexElementGroup)
549-
square_mesh = make_gmsh_sphere(order, cls=TensorProductElementGroup)
551+
quad_mesh = make_gmsh_sphere(order, cls=TensorProductElementGroup)
550552
elif mesh_name == "gmsh_torus":
551553
simplex_mesh = make_gmsh_torus(order, cls=SimplexElementGroup)
552-
square_mesh = make_gmsh_torus(order, cls=TensorProductElementGroup)
554+
quad_mesh = make_gmsh_torus(order, cls=TensorProductElementGroup)
553555
else:
554556
raise ValueError(f"unknown mesh: '{mesh_name}'")
555557

558+
ambient_dim = 3
559+
assert simplex_mesh.ambient_dim == ambient_dim
560+
assert quad_mesh.ambient_dim == ambient_dim
561+
556562
from meshmode.discretization import Discretization
557563
import meshmode.discretization.poly_element as mpoly
558564
simplex_discr = Discretization(actx, simplex_mesh,
559-
mpoly.InterpolatoryQuadratureGroupFactory(order))
560-
square_discr = Discretization(actx, square_mesh,
561-
mpoly.InterpolatoryQuadratureGroupFactory(order))
565+
mpoly.InterpolatoryEquidistantGroupFactory(order))
566+
quad_discr = Discretization(actx, quad_mesh,
567+
mpoly.InterpolatoryEquidistantGroupFactory(order))
562568

563569
print(f"simplex_discr.ndofs: {simplex_discr.ndofs}")
564-
print(f"square_discr.ndofs: {square_discr.ndofs}")
570+
print(f"quad_discr.ndofs: {quad_discr.ndofs}")
571+
572+
if metric_type == "eigs":
573+
sym_simplex = make_simplex_stretch_factors(ambient_dim)
574+
sym_quad = make_quad_stretch_factors(ambient_dim)
575+
elif metric_type == "det":
576+
form1 = sym.first_fundamental_form(ambient_dim)
577+
sym_simplex = np.array([
578+
form1[0, 0] * form1[1, 1] - form1[0, 1] * form1[1, 0],
579+
sym.Ones(),
580+
], dtype=object)
581+
sym_quad = sym_simplex
582+
elif metric_type == "trace":
583+
form1 = sym.first_fundamental_form(ambient_dim)
584+
sym_simplex = np.array([
585+
form1[0, 0] + form1[1, 1],
586+
sym.Ones(),
587+
], dtype=object)
588+
sym_quad = sym_simplex
589+
elif metric_type == "norm":
590+
form1 = sym.first_fundamental_form(ambient_dim)
591+
sym_simplex = np.array([
592+
sym.sqrt(sum(form1 * form1)),
593+
sym.Ones(),
594+
], dtype=object)
595+
sym_quad = sym_simplex
596+
else:
597+
raise ValueError(f"unknown metric type: '{metric_type}'")
565598

566-
s0, s1 = bind(simplex_discr,
567-
make_simplex_stretch_factors(simplex_discr.ambient_dim))(actx)
568-
q0, q1 = bind(square_discr,
569-
make_quad_stretch_factors(square_discr.ambient_dim))(actx)
599+
s0, s1 = bind(simplex_discr, sym_simplex)(actx)
600+
q0, q1 = bind(quad_discr, sym_quad)(actx)
570601

571602
print("s0")
572603
print(actx.to_numpy(actx.np.min(s0))[()], actx.to_numpy(actx.np.max(s0))[()])
@@ -578,26 +609,26 @@ def test_stretch_factor(actx_factory, order, mesh_name="torus", visualize=False)
578609
if not visualize:
579610
return
580611

581-
suffix = f"{mesh_name}_stretch_factors_{order:02d}"
612+
suffix = f"{mesh_name}_{metric_type}_{order:02d}"
582613

583614
# {{{ plot vtk
584615

585616
from meshmode.discretization.visualization import make_visualizer
586-
vis = make_visualizer(actx, simplex_discr, order)
617+
vis = make_visualizer(actx, simplex_discr, order, force_equidistant=True)
587618
vis.write_vtk_file(f"simplex_{suffix}.vtu", [
588619
("s0", s0), ("s1", s1),
589-
], overwrite=True)
620+
], overwrite=True, use_high_order=True)
590621

591-
vis = make_visualizer(actx, square_discr, order)
592-
vis.write_vtk_file(f"square_{suffix}.vtu", [
622+
vis = make_visualizer(actx, quad_discr, order, force_equidistant=True)
623+
vis.write_vtk_file(f"quad_{suffix}.vtu", [
593624
("s0", q0), ("s1", q1),
594-
], overwrite=True)
625+
], overwrite=True, use_high_order=True)
595626

596627
# }}}
597628

598629
# {{{ plot reference simplex
599630

600-
if square_discr.mesh.nelements <= 2:
631+
if quad_discr.mesh.nelements <= 2:
601632
import matplotlib.pyplot as plt
602633
fig = plt.figure(figsize=(12, 10), dpi=300)
603634

@@ -613,17 +644,17 @@ def test_stretch_factor(actx_factory, order, mesh_name="torus", visualize=False)
613644

614645
# }}}
615646

616-
# {{{ plot reference square
647+
# {{{ plot reference quad
617648

618-
if square_discr.mesh.nelements <= 2:
619-
xi = square_discr.groups[0].unit_nodes
649+
if quad_discr.mesh.nelements <= 2:
650+
xi = quad_discr.groups[0].unit_nodes
620651
for name, sv in zip(["q0", "q1"], [q0, q1]):
621652
sv = actx.to_numpy(sv[0])[0]
622653

623654
ax = fig.gca()
624655
im = ax.tricontourf(xi[0], xi[1], sv, levels=32)
625656
fig.colorbar(im, ax=ax)
626-
fig.savefig(f"square_{suffix}_{name}")
657+
fig.savefig(f"quad_{suffix}_{name}")
627658
fig.clf()
628659

629660
# }}}

0 commit comments

Comments
 (0)