From f7f1b26861f207e5582f6c457798244d6ab1a1ec Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Mon, 1 Jun 2020 19:11:47 -0500 Subject: [PATCH 1/4] add test for eval_code vs discr_code overwritten expression The issue is that the same expression (dx0/dr0) appears in both a user provided expression and the discretization metrics and the code generator gets confused. --- test/test_grudge.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/test_grudge.py b/test/test_grudge.py index 50cb3d0b..9cec210d 100644 --- a/test/test_grudge.py +++ b/test/test_grudge.py @@ -648,6 +648,31 @@ def test_function_symbol_array(ctx_factory, array_type): assert isinstance(norm, float) +def test_operator_compiler_overwrite(ctx_factory): + """Tests that the same expression in ``eval_code`` and ``discr_code`` + does not confuse the OperatorCompiler in grudge/symbolic/compiler.py. + """ + + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + + ambient_dim = 2 + target_order = 4 + + from meshmode.mesh.generation import generate_regular_rect_mesh + mesh = generate_regular_rect_mesh( + a=(-0.5,)*ambient_dim, b=(0.5,)*ambient_dim, + n=(8,)*ambient_dim, order=1) + discr = DGDiscretizationWithBoundaries(ctx, mesh, order=target_order) + + sym_u = sym.nodes(ambient_dim) + sym_div_u = sum(d(u) for d, u in zip(sym.nabla(ambient_dim), sym_u)) + + div_u = bind(discr, sym_div_u)(queue) + error = la.norm(div_u.get(queue) - discr.dim) + logger.info("error: %.5e", error) + + # You can test individual routines by typing # $ python test_grudge.py 'test_routine()' -- GitLab From 250ef171d150ee650769892351cd113f2b9e8711 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Thu, 2 Jul 2020 14:27:38 -0500 Subject: [PATCH 2/4] add failing test for assignment aggregation --- test/test_grudge.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/test_grudge.py b/test/test_grudge.py index 9cec210d..9d2ea515 100644 --- a/test/test_grudge.py +++ b/test/test_grudge.py @@ -665,6 +665,8 @@ def test_operator_compiler_overwrite(ctx_factory): n=(8,)*ambient_dim, order=1) discr = DGDiscretizationWithBoundaries(ctx, mesh, order=target_order) + # {{{ test + sym_u = sym.nodes(ambient_dim) sym_div_u = sum(d(u) for d, u in zip(sym.nabla(ambient_dim), sym_u)) @@ -672,6 +674,44 @@ def test_operator_compiler_overwrite(ctx_factory): error = la.norm(div_u.get(queue) - discr.dim) logger.info("error: %.5e", error) + # }}} + + +@pytest.mark.parametrize("ambient_dim", [2, 3]) +def test_incorrect_assignment_aggregation(ctx_factory, ambient_dim): + """Tests that the greedy assignemnt aggregation code works on a non-trivial + expression (on which it didn't work at the time of writing). + """ + + ctx = ctx_factory() + queue = cl.CommandQueue(ctx) + + target_order = 4 + + from meshmode.mesh.generation import generate_regular_rect_mesh + mesh = generate_regular_rect_mesh( + a=(-0.5,)*ambient_dim, b=(0.5,)*ambient_dim, + n=(8,)*ambient_dim, order=1) + discr = DGDiscretizationWithBoundaries(ctx, mesh, order=target_order) + + # {{{ test + + dd = sym.DD_VOLUME + sym_y = sym.make_sym_array("y", ambient_dim, dd=dd) + sym_minv_y = sym.cse(sym.InverseMassOperator()(sym_y), "minv_y") + + sym_u = make_obj_array([0.5 * sym.Ones(dd), 0.0, 0.0])[:ambient_dim] + sym_div_u = sum(d(u) for d, u in zip(sym.nabla(ambient_dim), sym_u)) + + sym_op = sym.MassOperator(dd)(sym_u) \ + + sym.MassOperator(dd)(sym_minv_y * sym_div_u) + logger.info("%s", sym.pretty(sym_op)) + + y = make_obj_array(discr.discr_from_dd(dd).nodes()) + bind(discr, sym_op)(queue, y=y) + + # }}} + # You can test individual routines by typing # $ python test_grudge.py 'test_routine()' -- GitLab From 36ea1778a5d9d7c94132505654f5776afde59ade Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Tue, 14 Jul 2020 20:50:09 -0500 Subject: [PATCH 3/4] port to array-context --- test/test_grudge.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/test_grudge.py b/test/test_grudge.py index d3b58682..20d3631e 100644 --- a/test/test_grudge.py +++ b/test/test_grudge.py @@ -652,6 +652,7 @@ def test_operator_compiler_overwrite(ctx_factory): ctx = ctx_factory() queue = cl.CommandQueue(ctx) + actx = PyOpenCLArrayContext(queue) ambient_dim = 2 target_order = 4 @@ -660,15 +661,15 @@ def test_operator_compiler_overwrite(ctx_factory): mesh = generate_regular_rect_mesh( a=(-0.5,)*ambient_dim, b=(0.5,)*ambient_dim, n=(8,)*ambient_dim, order=1) - discr = DGDiscretizationWithBoundaries(ctx, mesh, order=target_order) + discr = DGDiscretizationWithBoundaries(actx, mesh, order=target_order) # {{{ test sym_u = sym.nodes(ambient_dim) sym_div_u = sum(d(u) for d, u in zip(sym.nabla(ambient_dim), sym_u)) - div_u = bind(discr, sym_div_u)(queue) - error = la.norm(div_u.get(queue) - discr.dim) + div_u = bind(discr, sym_div_u)(actx) + error = bind(discr, sym.norm(2, sym.var("x")))(actx, x=div_u - discr.dim) logger.info("error: %.5e", error) # }}} @@ -682,6 +683,7 @@ def test_incorrect_assignment_aggregation(ctx_factory, ambient_dim): ctx = ctx_factory() queue = cl.CommandQueue(ctx) + actx = PyOpenCLArrayContext(queue) target_order = 4 @@ -689,7 +691,7 @@ def test_incorrect_assignment_aggregation(ctx_factory, ambient_dim): mesh = generate_regular_rect_mesh( a=(-0.5,)*ambient_dim, b=(0.5,)*ambient_dim, n=(8,)*ambient_dim, order=1) - discr = DGDiscretizationWithBoundaries(ctx, mesh, order=target_order) + discr = DGDiscretizationWithBoundaries(actx, mesh, order=target_order) # {{{ test @@ -704,8 +706,7 @@ def test_incorrect_assignment_aggregation(ctx_factory, ambient_dim): + sym.MassOperator(dd)(sym_minv_y * sym_div_u) logger.info("%s", sym.pretty(sym_op)) - y = make_obj_array(discr.discr_from_dd(dd).nodes()) - bind(discr, sym_op)(queue, y=y) + bind(discr, sym_op)(actx, y=discr.discr_from_dd(dd).nodes()) # }}} -- GitLab From d4b15ed20b5c7a6d62a7fbb69dbcedd8fba1df73 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Mon, 17 Aug 2020 11:33:35 -0500 Subject: [PATCH 4/4] add another example of a failing aggregation --- test/test_grudge.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/test/test_grudge.py b/test/test_grudge.py index 20d3631e..bc0a5740 100644 --- a/test/test_grudge.py +++ b/test/test_grudge.py @@ -693,10 +693,25 @@ def test_incorrect_assignment_aggregation(ctx_factory, ambient_dim): n=(8,)*ambient_dim, order=1) discr = DGDiscretizationWithBoundaries(actx, mesh, order=target_order) - # {{{ test + # {{{ test with a relative norm dd = sym.DD_VOLUME + sym_x = sym.make_sym_array("y", ambient_dim, dd=dd) sym_y = sym.make_sym_array("y", ambient_dim, dd=dd) + + sym_norm_y = sym.norm(2, sym_y, dd=dd) + sym_norm_d = sym.norm(2, sym_x - sym_y, dd=dd) + sym_op = sym_norm_d / sym_norm_y + logger.info("%s", sym.pretty(sym_op)) + + # FIXME: this shouldn't raise a RuntimeError + with pytest.raises(RuntimeError): + bind(discr, sym_op)(actx, x=1.0, y=discr.discr_from_dd(dd).nodes()) + + # }}} + + # {{{ test with repeated mass inverses + sym_minv_y = sym.cse(sym.InverseMassOperator()(sym_y), "minv_y") sym_u = make_obj_array([0.5 * sym.Ones(dd), 0.0, 0.0])[:ambient_dim] @@ -706,6 +721,7 @@ def test_incorrect_assignment_aggregation(ctx_factory, ambient_dim): + sym.MassOperator(dd)(sym_minv_y * sym_div_u) logger.info("%s", sym.pretty(sym_op)) + # FIXME: this shouldn't raise a RuntimeError either bind(discr, sym_op)(actx, y=discr.discr_from_dd(dd).nodes()) # }}} -- GitLab