From 65fbc5332951257b206303e9bc21d28b9f295415 Mon Sep 17 00:00:00 2001 From: nbouziani Date: Sun, 10 Dec 2023 06:16:20 +0000 Subject: [PATCH 1/3] Update tests --- .circleci/config.yml | 2 + tests/firedrake_adjoint/test_assignment.py | 37 ++++++++++--------- .../test_disk_checkpointing.py | 7 ++-- tests/firedrake_adjoint/test_projection.py | 21 ++++++----- .../test_shape_derivatives.py | 5 ++- tests/firedrake_adjoint/test_tag.py | 6 --- 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6b29bc45..b9fbb0b4 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -106,6 +106,8 @@ jobs: - run: name: install dependencies command: | + cd /home/firedrake/firedrake/src/firedrake + git checkout interp_dual source /home/firedrake/firedrake/bin/activate pip3 install -e .[test] diff --git a/tests/firedrake_adjoint/test_assignment.py b/tests/firedrake_adjoint/test_assignment.py index 06b52ef7..5d9ba404 100644 --- a/tests/firedrake_adjoint/test_assignment.py +++ b/tests/firedrake_adjoint/test_assignment.py @@ -2,6 +2,7 @@ pytest.importorskip("firedrake") from firedrake import * +from firedrake.__future__ import * from firedrake.adjoint import * from numpy.random import rand @@ -13,8 +14,8 @@ def test_assign_linear_combination(): V = FunctionSpace(mesh, "CG", 1) x, = SpatialCoordinate(mesh) - f = interpolate(x, V) - g = interpolate(sin(x), V) + f = assemble(Interpolate(x, V)) + g = assemble(Interpolate(sin(x), V)) u = Function(V) u.assign(3*f + g) @@ -33,8 +34,8 @@ def test_assign_vector_valued(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V) - g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V) + f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.assign(f - 0.5*g) @@ -53,8 +54,8 @@ def test_assign_tlm(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V) - g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V) + f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.assign(f - 0.5*g) @@ -78,20 +79,20 @@ def test_assign_tlm_with_constant(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = interpolate(x[0], V) - g = interpolate(sin(x[0]), V) + f = assemble(Interpolate(x[0], V)) + g = assemble(Interpolate(sin(x[0]), V)) c = Constant(5.0, domain=mesh) u = Function(V) u.interpolate(c * f**2) - c.block_variable.tlm_value = Constant(0.3) + c.block_variable.tlm_value = Constant(0.3, domain=mesh) tape = get_working_tape() tape.evaluate_tlm() assert_allclose(u.block_variable.tlm_value.dat.data, 0.3 * f.dat.data ** 2) tape.reset_tlm_values() - c.block_variable.tlm_value = Constant(0.4) + c.block_variable.tlm_value = Constant(0.4, domain=mesh) f.block_variable.tlm_value = g tape.evaluate_tlm() assert_allclose(u.block_variable.tlm_value.dat.data, 0.4 * f.dat.data ** 2 + 10. * f.dat.data * g.dat.data) @@ -103,8 +104,8 @@ def test_assign_hessian(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V) - g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V) + f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.assign(f - 0.5*g) @@ -125,8 +126,8 @@ def test_assign_nonlincom(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = interpolate(x[0], V) - g = interpolate(sin(x[0]), V) + f = assemble(Interpolate(x[0], V)) + g = assemble(Interpolate(sin(x[0]), V)) u = Function(V) u.interpolate(f*g) @@ -144,7 +145,7 @@ def test_assign_with_constant(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = interpolate(x[0], V) + f = assemble(Interpolate(x[0], V)) c = Constant(3.0, domain=mesh) d = Constant(2.0, domain=mesh) u = Function(V) @@ -166,8 +167,8 @@ def test_assign_nonlin_changing(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = interpolate(x[0], V) - g = interpolate(sin(x[0]), V) + f = assemble(Interpolate(x[0], V)) + g = assemble(Interpolate(sin(x[0]), V)) control = Control(g) test = TestFunction(V) @@ -201,7 +202,7 @@ def test_assign_constant_scale(): f = Function(V) c = Constant(2.0, domain=mesh) x, y = SpatialCoordinate(mesh) - g = interpolate(as_vector([sin(y)+x, cos(x)*y]), V) + g = assemble(Interpolate(as_vector([sin(y)+x, cos(x)*y]), V)) f.assign(c * g) diff --git a/tests/firedrake_adjoint/test_disk_checkpointing.py b/tests/firedrake_adjoint/test_disk_checkpointing.py index 45e59bf5..8341dc16 100644 --- a/tests/firedrake_adjoint/test_disk_checkpointing.py +++ b/tests/firedrake_adjoint/test_disk_checkpointing.py @@ -2,6 +2,7 @@ pytest.importorskip("firedrake") from firedrake import * +from firedrake.__future__ import * from firedrake.adjoint import * import numpy as np import os @@ -15,8 +16,8 @@ def adjoint_example(fine, coarse): w = Function(W) x, y = SpatialCoordinate(fine) - # InterpolateBlock - m = interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space) + # AssembleBlock + m = assemble(Interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)) u, v = w.split() # FunctionAssignBlock, FunctionMergeBlock @@ -37,7 +38,7 @@ def adjoint_example(fine, coarse): Jhat = ReducedFunctional(J, Control(m)) with stop_annotating(): - m_new = interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space) + m_new = assemble(Interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)) checkpointer = get_working_tape()._package_data["firedrake"] init_file_timestamp = os.stat(checkpointer.init_checkpoint_file.name).st_mtime current_file_timestamp = os.stat(checkpointer.current_checkpoint_file.name).st_mtime diff --git a/tests/firedrake_adjoint/test_projection.py b/tests/firedrake_adjoint/test_projection.py index a01e7cdc..9359dddf 100644 --- a/tests/firedrake_adjoint/test_projection.py +++ b/tests/firedrake_adjoint/test_projection.py @@ -2,6 +2,7 @@ pytest.importorskip("firedrake") from firedrake import * +from firedrake.__future__ import * from firedrake.adjoint import * from numpy.random import rand @@ -13,8 +14,8 @@ def test_project_vector_valued(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V) - g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V) + f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.project(f - 0.5*g) @@ -33,8 +34,8 @@ def test_project_tlm(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V) - g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V) + f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.project(f - 0.5*g) @@ -58,8 +59,8 @@ def test_project_hessian(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V) - g = interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V) + f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.project(f - 0.5*g) @@ -85,8 +86,8 @@ def test_project_nonlincom(): V3 = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = interpolate(x[0], V1) - g = interpolate(sin(x[0]), V2) + f = assemble(Interpolate(x[0], V1)) + g = assemble(Interpolate(sin(x[0]), V2)) u = Function(V3) u.project(f*g) @@ -104,8 +105,8 @@ def test_project_nonlin_changing(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = interpolate(x[0], V) - g = interpolate(sin(x[0]), V) + f = assemble(Interpolate(x[0], V)) + g = assemble(Interpolate(sin(x[0]), V)) control = Control(g) test = TestFunction(V) diff --git a/tests/firedrake_adjoint/test_shape_derivatives.py b/tests/firedrake_adjoint/test_shape_derivatives.py index 0537be71..8e5ea3c9 100644 --- a/tests/firedrake_adjoint/test_shape_derivatives.py +++ b/tests/firedrake_adjoint/test_shape_derivatives.py @@ -3,6 +3,7 @@ import numpy as np from firedrake import * +from firedrake.__future__ import * from firedrake.adjoint import * from pyadjoint import taylor_to_dict @@ -221,7 +222,7 @@ def test_multiple_assignments(): dJdm = Jhat.derivative() pert = as_vector((x * y, sin(x))) - pert = interpolate(pert, S) + pert = assemble(Interpolate(pert, S)) results = taylor_to_dict(Jhat, s, pert) assert min(results["R0"]["Rate"]) > 0.9 @@ -252,7 +253,7 @@ def test_multiple_assignments(): dJdm.vector().get_local()) pert = as_vector((x * y, sin(x))) - pert = interpolate(pert, S) + pert = assemble(Interpolate(pert, S)) results = taylor_to_dict(Jhat, s, pert) assert min(results["R0"]["Rate"]) > 0.9 diff --git a/tests/firedrake_adjoint/test_tag.py b/tests/firedrake_adjoint/test_tag.py index 95b3ff10..afc28712 100644 --- a/tests/firedrake_adjoint/test_tag.py +++ b/tests/firedrake_adjoint/test_tag.py @@ -8,10 +8,8 @@ @pytest.fixture(params=[ "constant assign", "function assign", - "interpolate", "project", "supermesh project", - "interpolate method", "project method", "supermesh project method", ]) @@ -36,10 +34,6 @@ def test_tags(tag): f2.assign(1.0) if tag == "function assign": f1.assign(f2, ad_block_tag=tag) - elif tag == "interpolate": - f1 = interpolate(f2, V, ad_block_tag=tag) - elif tag == "interpolate method": - f1.interpolate(f2, ad_block_tag=tag) elif "project method" in tag: f1.project(f2, ad_block_tag=tag) elif "project" in tag: From e0856aac5503bb634baa371f76503e24c675c030 Mon Sep 17 00:00:00 2001 From: nbouziani Date: Thu, 14 Dec 2023 19:55:53 +0000 Subject: [PATCH 2/3] Lower case Interpolate --- tests/firedrake_adjoint/test_assignment.py | 32 +++++++++---------- .../test_disk_checkpointing.py | 4 +-- tests/firedrake_adjoint/test_projection.py | 20 ++++++------ .../test_shape_derivatives.py | 4 +-- 4 files changed, 30 insertions(+), 30 deletions(-) diff --git a/tests/firedrake_adjoint/test_assignment.py b/tests/firedrake_adjoint/test_assignment.py index 5d9ba404..a9c04449 100644 --- a/tests/firedrake_adjoint/test_assignment.py +++ b/tests/firedrake_adjoint/test_assignment.py @@ -14,8 +14,8 @@ def test_assign_linear_combination(): V = FunctionSpace(mesh, "CG", 1) x, = SpatialCoordinate(mesh) - f = assemble(Interpolate(x, V)) - g = assemble(Interpolate(sin(x), V)) + f = assemble(interpolate(x, V)) + g = assemble(interpolate(sin(x), V)) u = Function(V) u.assign(3*f + g) @@ -34,8 +34,8 @@ def test_assign_vector_valued(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) - g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) + f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.assign(f - 0.5*g) @@ -54,8 +54,8 @@ def test_assign_tlm(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) - g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) + f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.assign(f - 0.5*g) @@ -79,8 +79,8 @@ def test_assign_tlm_with_constant(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(x[0], V)) - g = assemble(Interpolate(sin(x[0]), V)) + f = assemble(interpolate(x[0], V)) + g = assemble(interpolate(sin(x[0]), V)) c = Constant(5.0, domain=mesh) u = Function(V) @@ -104,8 +104,8 @@ def test_assign_hessian(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) - g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) + f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.assign(f - 0.5*g) @@ -126,8 +126,8 @@ def test_assign_nonlincom(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(x[0], V)) - g = assemble(Interpolate(sin(x[0]), V)) + f = assemble(interpolate(x[0], V)) + g = assemble(interpolate(sin(x[0]), V)) u = Function(V) u.interpolate(f*g) @@ -145,7 +145,7 @@ def test_assign_with_constant(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(x[0], V)) + f = assemble(interpolate(x[0], V)) c = Constant(3.0, domain=mesh) d = Constant(2.0, domain=mesh) u = Function(V) @@ -167,8 +167,8 @@ def test_assign_nonlin_changing(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(x[0], V)) - g = assemble(Interpolate(sin(x[0]), V)) + f = assemble(interpolate(x[0], V)) + g = assemble(interpolate(sin(x[0]), V)) control = Control(g) test = TestFunction(V) @@ -202,7 +202,7 @@ def test_assign_constant_scale(): f = Function(V) c = Constant(2.0, domain=mesh) x, y = SpatialCoordinate(mesh) - g = assemble(Interpolate(as_vector([sin(y)+x, cos(x)*y]), V)) + g = assemble(interpolate(as_vector([sin(y)+x, cos(x)*y]), V)) f.assign(c * g) diff --git a/tests/firedrake_adjoint/test_disk_checkpointing.py b/tests/firedrake_adjoint/test_disk_checkpointing.py index 8341dc16..e7daaf50 100644 --- a/tests/firedrake_adjoint/test_disk_checkpointing.py +++ b/tests/firedrake_adjoint/test_disk_checkpointing.py @@ -17,7 +17,7 @@ def adjoint_example(fine, coarse): x, y = SpatialCoordinate(fine) # AssembleBlock - m = assemble(Interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)) + m = assemble(interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)) u, v = w.split() # FunctionAssignBlock, FunctionMergeBlock @@ -38,7 +38,7 @@ def adjoint_example(fine, coarse): Jhat = ReducedFunctional(J, Control(m)) with stop_annotating(): - m_new = assemble(Interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)) + m_new = assemble(interpolate(sin(4*pi*x)*cos(4*pi*y), cg_space)) checkpointer = get_working_tape()._package_data["firedrake"] init_file_timestamp = os.stat(checkpointer.init_checkpoint_file.name).st_mtime current_file_timestamp = os.stat(checkpointer.current_checkpoint_file.name).st_mtime diff --git a/tests/firedrake_adjoint/test_projection.py b/tests/firedrake_adjoint/test_projection.py index 9359dddf..df19e897 100644 --- a/tests/firedrake_adjoint/test_projection.py +++ b/tests/firedrake_adjoint/test_projection.py @@ -14,8 +14,8 @@ def test_project_vector_valued(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) - g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) + f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.project(f - 0.5*g) @@ -34,8 +34,8 @@ def test_project_tlm(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) - g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) + f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.project(f - 0.5*g) @@ -59,8 +59,8 @@ def test_project_hessian(): V = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) - g = assemble(Interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) + f = assemble(interpolate(as_vector((x[0]*x[1], x[0]+x[1])), V)) + g = assemble(interpolate(as_vector((sin(x[1])+x[0], cos(x[0])*x[1])), V)) u = Function(V) u.project(f - 0.5*g) @@ -86,8 +86,8 @@ def test_project_nonlincom(): V3 = FunctionSpace(mesh, element) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(x[0], V1)) - g = assemble(Interpolate(sin(x[0]), V2)) + f = assemble(interpolate(x[0], V1)) + g = assemble(interpolate(sin(x[0]), V2)) u = Function(V3) u.project(f*g) @@ -105,8 +105,8 @@ def test_project_nonlin_changing(): V = FunctionSpace(mesh, "CG", 1) x = SpatialCoordinate(mesh) - f = assemble(Interpolate(x[0], V)) - g = assemble(Interpolate(sin(x[0]), V)) + f = assemble(interpolate(x[0], V)) + g = assemble(interpolate(sin(x[0]), V)) control = Control(g) test = TestFunction(V) diff --git a/tests/firedrake_adjoint/test_shape_derivatives.py b/tests/firedrake_adjoint/test_shape_derivatives.py index 8e5ea3c9..10782469 100644 --- a/tests/firedrake_adjoint/test_shape_derivatives.py +++ b/tests/firedrake_adjoint/test_shape_derivatives.py @@ -222,7 +222,7 @@ def test_multiple_assignments(): dJdm = Jhat.derivative() pert = as_vector((x * y, sin(x))) - pert = assemble(Interpolate(pert, S)) + pert = assemble(interpolate(pert, S)) results = taylor_to_dict(Jhat, s, pert) assert min(results["R0"]["Rate"]) > 0.9 @@ -253,7 +253,7 @@ def test_multiple_assignments(): dJdm.vector().get_local()) pert = as_vector((x * y, sin(x))) - pert = assemble(Interpolate(pert, S)) + pert = assemble(interpolate(pert, S)) results = taylor_to_dict(Jhat, s, pert) assert min(results["R0"]["Rate"]) > 0.9 From 361e920360a7b3a31406019d3a9424bdb299639b Mon Sep 17 00:00:00 2001 From: Nacime Bouziani <48448063+nbouziani@users.noreply.github.com> Date: Sun, 14 Jan 2024 11:45:17 +0000 Subject: [PATCH 3/3] Update .circleci/config.yml --- .circleci/config.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index b9fbb0b4..6b29bc45 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -106,8 +106,6 @@ jobs: - run: name: install dependencies command: | - cd /home/firedrake/firedrake/src/firedrake - git checkout interp_dual source /home/firedrake/firedrake/bin/activate pip3 install -e .[test]