Skip to content

Commit 15d2578

Browse files
committed
clean up stretch factor example test
1 parent 83c45af commit 15d2578

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

test/test_symbolic.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -334,21 +334,19 @@ def test_node_reduction(actx_factory):
334334

335335
# {{{ test_stretch_factor
336336

337-
def make_stretch_mesh(order, cls):
337+
def make_twisted_mesh(order, cls):
338338
vertices = np.array([
339-
[-1, -1, 0], [1, -1, 0], [-1, 1, 0], [1, 1, 0], [3, -1, 0], [3, 1, 0],
339+
[-1, -1, 0], [1, -1, 0], [-1, 1, 0], [1, 1, 0],
340340
], dtype=np.float64).T
341341

342342
import meshmode.mesh as mm
343343
if issubclass(cls, mm.SimplexElementGroup):
344344
vertex_indices = np.array([
345345
(0, 1, 2), (1, 3, 2),
346-
# (1, 4, 3), (4, 5, 3),
347346
], dtype=np.int32)
348347
elif issubclass(cls, mm.TensorProductElementGroup):
349348
vertex_indices = np.array([
350349
(0, 1, 2, 3),
351-
# (1, 4, 3, 5)
352350
], dtype=np.int32)
353351
else:
354352
raise ValueError
@@ -359,7 +357,21 @@ def make_stretch_mesh(order, cls):
359357
unit_nodes=None,
360358
group_cls=cls)
361359

362-
return mm.Mesh(vertices, [grp], is_conforming=True)
360+
def wobble(x):
361+
rx, ry = 2, 0.5
362+
theta = np.pi / 4
363+
364+
result = np.empty_like(x)
365+
result[0] = rx * (np.cos(theta) * x[0] - np.sin(theta) * x[1])
366+
result[1] = np.sin(ry * (np.sin(theta) * x[0] + np.cos(theta) * x[1]))
367+
result[2] = x[2]
368+
# result[2] = np.sin(x[1]) * np.sin(x[0])
369+
370+
return result
371+
372+
from meshmode.mesh.processing import map_mesh
373+
mesh = mm.Mesh(vertices, [grp], is_conforming=True)
374+
return map_mesh(mesh, wobble)
363375

364376

365377
def make_torus_mesh(order, cls, a=2.0, b=1.0, n_major=12, n_minor=6):
@@ -414,7 +426,6 @@ def make_simplex_stretch_factors(ambient_dim):
414426
from pytential.symbolic.primitives import \
415427
_equilateral_parametrization_derivative_matrix
416428
equi_pder = _equilateral_parametrization_derivative_matrix(ambient_dim)
417-
# equi_pder = sym.parametrization_derivative_matrix(ambient_dim, ambient_dim - 1)
418429
equi_form1 = sym.cse(equi_pder.T @ equi_pder, "pd_mat_jtj")
419430

420431
from pytential.symbolic.primitives import _small_mat_eigenvalues
@@ -426,7 +437,7 @@ def make_simplex_stretch_factors(ambient_dim):
426437

427438
def make_quad_stretch_factors(ambient_dim):
428439
pder = sym.parametrization_derivative_matrix(ambient_dim, ambient_dim - 1)
429-
form1 = sym.cse(pder.T @ pder + 1.0e-14, "pd_mat_jtj")
440+
form1 = sym.cse(pder.T @ pder, "pd_mat_jtj")
430441

431442
from pytential.symbolic.primitives import _small_mat_eigenvalues
432443
return [
@@ -439,46 +450,29 @@ def make_quad_stretch_factors(ambient_dim):
439450
def test_stretch_factor(actx_factory, order, visualize=False):
440451
actx = actx_factory()
441452

442-
def wobble(x):
443-
rx, ry = 2, 0.5
444-
theta = np.pi / 4
445-
446-
result = np.empty_like(x)
447-
result[0] = rx * (np.cos(theta) * x[0] - np.sin(theta) * x[1])
448-
result[1] = np.sin(ry * (np.sin(theta) * x[0] + np.cos(theta) * x[1]))
449-
result[2] = x[2]
450-
# result[2] = np.sin(x[1]) * np.sin(x[0])
451-
452-
return result
453-
454453
from meshmode.mesh import SimplexElementGroup, TensorProductElementGroup
455454
if True:
456455
square_mesh = make_torus_mesh(order, TensorProductElementGroup)
457456
simplex_mesh = make_torus_mesh(order, SimplexElementGroup)
458457
else:
459-
from meshmode.mesh.processing import map_mesh
460-
square_mesh = map_mesh(
461-
make_stretch_mesh(order, TensorProductElementGroup),
462-
wobble)
463-
simplex_mesh = map_mesh(
464-
make_stretch_mesh(order, SimplexElementGroup),
465-
wobble)
458+
square_mesh = make_twisted_mesh(order, TensorProductElementGroup)
459+
simplex_mesh = make_twisted_mesh(order, SimplexElementGroup)
466460

467461
from meshmode.discretization import Discretization
468462
import meshmode.discretization.poly_element as mpoly
469463
simplex_discr = Discretization(actx, simplex_mesh,
470-
mpoly.InterpolatoryEdgeClusteredGroupFactory(order))
464+
mpoly.InterpolatoryQuadratureGroupFactory(order))
471465
square_discr = Discretization(actx, square_mesh,
472-
mpoly.InterpolatoryEdgeClusteredGroupFactory(order))
466+
mpoly.InterpolatoryQuadratureGroupFactory(order))
467+
468+
print(f"simplex_discr.ndofs: {simplex_discr.ndofs}")
469+
print(f"square_discr.ndofs: {square_discr.ndofs}")
473470

474471
s0, s1 = bind(simplex_discr,
475472
make_simplex_stretch_factors(simplex_discr.ambient_dim))(actx)
476473
q0, q1 = bind(square_discr,
477474
make_quad_stretch_factors(square_discr.ambient_dim))(actx)
478475

479-
# s0 = (1 + (np.sqrt(3) / 4) / 4) * s0
480-
# s1 = (1 + (np.sqrt(3) / 4) / 4) * s1
481-
482476
print(actx.to_numpy(actx.np.min(s0))[()], actx.to_numpy(actx.np.max(s0))[()])
483477
print(actx.to_numpy(actx.np.min(q0))[()], actx.to_numpy(actx.np.max(q0))[()])
484478
print(actx.to_numpy(actx.np.min(s1))[()], actx.to_numpy(actx.np.max(s1))[()])
@@ -487,18 +481,24 @@ def wobble(x):
487481
if not visualize:
488482
return
489483

484+
suffix = f"stretch_factors_{order:02d}"
485+
486+
# {{{ plot vtk
487+
490488
from meshmode.discretization.visualization import make_visualizer
491489
vis = make_visualizer(actx, simplex_discr, order)
492-
vis.write_vtk_file(f"stretch_factor_simplex_{order:02d}.vtu", [
490+
vis.write_vtk_file(f"simplex_{suffix}.vtu", [
493491
("s0", s0), ("s1", s1),
494492
], overwrite=True)
495493

496494
vis = make_visualizer(actx, square_discr, order)
497-
vis.write_vtk_file(f"stretch_factor_square_{order:02d}.vtu", [
495+
vis.write_vtk_file(f"square_{suffix}.vtu", [
498496
("s0", q0), ("s1", q1),
499497
], overwrite=True)
500498

501-
# {{{ plot simplex
499+
# }}}
500+
501+
# {{{ plot reference simplex
502502

503503
import matplotlib.pyplot as plt
504504
fig = plt.figure(figsize=(12, 10), dpi=300)
@@ -510,12 +510,12 @@ def wobble(x):
510510
ax = fig.gca()
511511
im = ax.tricontourf(xi[0], xi[1], sv, levels=32)
512512
fig.colorbar(im, ax=ax)
513-
fig.savefig(f"stretch_factor_simplex_{order:02d}_{name}")
513+
fig.savefig(f"simplex_{suffix}_{name}")
514514
fig.clf()
515515

516516
# }}}
517517

518-
# {{{ plot square
518+
# {{{ plot reference square
519519

520520
xi = square_discr.groups[0].unit_nodes
521521
for name, sv in zip(["q0", "q1"], [q0, q1]):
@@ -524,7 +524,7 @@ def wobble(x):
524524
ax = fig.gca()
525525
im = ax.tricontourf(xi[0], xi[1], sv, levels=32)
526526
fig.colorbar(im, ax=ax)
527-
fig.savefig(f"stretch_factor_square_{order:02d}_{name}")
527+
fig.savefig(f"square_{suffix}_{name}")
528528
fig.clf()
529529

530530
# }}}

0 commit comments

Comments
 (0)