Skip to content

Commit 9865564

Browse files
committed
clean up stretch factor example test
1 parent c66cb74 commit 9865564

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
@@ -337,21 +337,19 @@ def test_node_reduction(actx_factory):
337337

338338
# {{{ test_stretch_factor
339339

340-
def make_stretch_mesh(order, cls):
340+
def make_twisted_mesh(order, cls):
341341
vertices = np.array([
342-
[-1, -1, 0], [1, -1, 0], [-1, 1, 0], [1, 1, 0], [3, -1, 0], [3, 1, 0],
342+
[-1, -1, 0], [1, -1, 0], [-1, 1, 0], [1, 1, 0],
343343
], dtype=np.float64).T
344344

345345
import meshmode.mesh as mm
346346
if issubclass(cls, mm.SimplexElementGroup):
347347
vertex_indices = np.array([
348348
(0, 1, 2), (1, 3, 2),
349-
# (1, 4, 3), (4, 5, 3),
350349
], dtype=np.int32)
351350
elif issubclass(cls, mm.TensorProductElementGroup):
352351
vertex_indices = np.array([
353352
(0, 1, 2, 3),
354-
# (1, 4, 3, 5)
355353
], dtype=np.int32)
356354
else:
357355
raise ValueError
@@ -362,7 +360,21 @@ def make_stretch_mesh(order, cls):
362360
unit_nodes=None,
363361
group_cls=cls)
364362

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

367379

368380
def make_torus_mesh(order, cls, a=2.0, b=1.0, n_major=12, n_minor=6):
@@ -417,7 +429,6 @@ def make_simplex_stretch_factors(ambient_dim):
417429
from pytential.symbolic.primitives import \
418430
_equilateral_parametrization_derivative_matrix
419431
equi_pder = _equilateral_parametrization_derivative_matrix(ambient_dim)
420-
# equi_pder = sym.parametrization_derivative_matrix(ambient_dim, ambient_dim - 1)
421432
equi_form1 = sym.cse(equi_pder.T @ equi_pder, "pd_mat_jtj")
422433

423434
from pytential.symbolic.primitives import _small_mat_eigenvalues
@@ -429,7 +440,7 @@ def make_simplex_stretch_factors(ambient_dim):
429440

430441
def make_quad_stretch_factors(ambient_dim):
431442
pder = sym.parametrization_derivative_matrix(ambient_dim, ambient_dim - 1)
432-
form1 = sym.cse(pder.T @ pder + 1.0e-14, "pd_mat_jtj")
443+
form1 = sym.cse(pder.T @ pder, "pd_mat_jtj")
433444

434445
from pytential.symbolic.primitives import _small_mat_eigenvalues
435446
return [
@@ -442,46 +453,29 @@ def make_quad_stretch_factors(ambient_dim):
442453
def test_stretch_factor(actx_factory, order, visualize=False):
443454
actx = actx_factory()
444455

445-
def wobble(x):
446-
rx, ry = 2, 0.5
447-
theta = np.pi / 4
448-
449-
result = np.empty_like(x)
450-
result[0] = rx * (np.cos(theta) * x[0] - np.sin(theta) * x[1])
451-
result[1] = np.sin(ry * (np.sin(theta) * x[0] + np.cos(theta) * x[1]))
452-
result[2] = x[2]
453-
# result[2] = np.sin(x[1]) * np.sin(x[0])
454-
455-
return result
456-
457456
from meshmode.mesh import SimplexElementGroup, TensorProductElementGroup
458457
if True:
459458
square_mesh = make_torus_mesh(order, TensorProductElementGroup)
460459
simplex_mesh = make_torus_mesh(order, SimplexElementGroup)
461460
else:
462-
from meshmode.mesh.processing import map_mesh
463-
square_mesh = map_mesh(
464-
make_stretch_mesh(order, TensorProductElementGroup),
465-
wobble)
466-
simplex_mesh = map_mesh(
467-
make_stretch_mesh(order, SimplexElementGroup),
468-
wobble)
461+
square_mesh = make_twisted_mesh(order, TensorProductElementGroup)
462+
simplex_mesh = make_twisted_mesh(order, SimplexElementGroup)
469463

470464
from meshmode.discretization import Discretization
471465
import meshmode.discretization.poly_element as mpoly
472466
simplex_discr = Discretization(actx, simplex_mesh,
473-
mpoly.InterpolatoryEdgeClusteredGroupFactory(order))
467+
mpoly.InterpolatoryQuadratureGroupFactory(order))
474468
square_discr = Discretization(actx, square_mesh,
475-
mpoly.InterpolatoryEdgeClusteredGroupFactory(order))
469+
mpoly.InterpolatoryQuadratureGroupFactory(order))
470+
471+
print(f"simplex_discr.ndofs: {simplex_discr.ndofs}")
472+
print(f"square_discr.ndofs: {square_discr.ndofs}")
476473

477474
s0, s1 = bind(simplex_discr,
478475
make_simplex_stretch_factors(simplex_discr.ambient_dim))(actx)
479476
q0, q1 = bind(square_discr,
480477
make_quad_stretch_factors(square_discr.ambient_dim))(actx)
481478

482-
# s0 = (1 + (np.sqrt(3) / 4) / 4) * s0
483-
# s1 = (1 + (np.sqrt(3) / 4) / 4) * s1
484-
485479
print(actx.to_numpy(actx.np.min(s0))[()], actx.to_numpy(actx.np.max(s0))[()])
486480
print(actx.to_numpy(actx.np.min(q0))[()], actx.to_numpy(actx.np.max(q0))[()])
487481
print(actx.to_numpy(actx.np.min(s1))[()], actx.to_numpy(actx.np.max(s1))[()])
@@ -490,18 +484,24 @@ def wobble(x):
490484
if not visualize:
491485
return
492486

487+
suffix = f"stretch_factors_{order:02d}"
488+
489+
# {{{ plot vtk
490+
493491
from meshmode.discretization.visualization import make_visualizer
494492
vis = make_visualizer(actx, simplex_discr, order)
495-
vis.write_vtk_file(f"stretch_factor_simplex_{order:02d}.vtu", [
493+
vis.write_vtk_file(f"simplex_{suffix}.vtu", [
496494
("s0", s0), ("s1", s1),
497495
], overwrite=True)
498496

499497
vis = make_visualizer(actx, square_discr, order)
500-
vis.write_vtk_file(f"stretch_factor_square_{order:02d}.vtu", [
498+
vis.write_vtk_file(f"square_{suffix}.vtu", [
501499
("s0", q0), ("s1", q1),
502500
], overwrite=True)
503501

504-
# {{{ plot simplex
502+
# }}}
503+
504+
# {{{ plot reference simplex
505505

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

519519
# }}}
520520

521-
# {{{ plot square
521+
# {{{ plot reference square
522522

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

533533
# }}}

0 commit comments

Comments
 (0)