From 16c6499c231cd9bee75270b99c962f9ead1342e9 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Wed, 27 Nov 2024 19:44:07 +0000 Subject: [PATCH 1/7] Write vtk output also for cofunction --- firedrake/output/vtk_output.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/firedrake/output/vtk_output.py b/firedrake/output/vtk_output.py index 2dc7ab0235..26228c0369 100644 --- a/firedrake/output/vtk_output.py +++ b/firedrake/output/vtk_output.py @@ -452,7 +452,7 @@ def __init__(self, filename, project_output=False, comm=None, mode="w", @no_annotations def _prepare_output(self, function, max_elem): from firedrake import FunctionSpace, VectorFunctionSpace, \ - TensorFunctionSpace, Function + TensorFunctionSpace, Function, Cofunction name = function.name() # Need to project/interpolate? @@ -477,8 +477,14 @@ def _prepare_output(self, function, max_elem): shape=shape) else: raise ValueError("Unsupported shape %s" % (shape, )) - output = Function(V) + if isinstance(function, Function): + output = Function(V) + elif isinstance(function, Cofunction): + output = Function(V.dual()) + if self.project: + if isinstance(function, Cofunction): + raise ValueError("Can not project Cofunctions") output.project(function) else: output.interpolate(function) @@ -487,6 +493,7 @@ def _prepare_output(self, function, max_elem): def _write_vtu(self, *functions): from firedrake.function import Function + from firedrake.cofunction import Cofunction # Check if the user has requested to write out a plain mesh if len(functions) == 1 and isinstance(functions[0], ufl.Mesh): @@ -496,7 +503,7 @@ def _write_vtu(self, *functions): functions = [Function(V)] for f in functions: - if not isinstance(f, Function): + if not isinstance(f, (Function, Cofunction)): raise ValueError("Can only output Functions or a single mesh, not %r" % type(f)) meshes = tuple(extract_unique_domain(f) for f in functions) if not all(m == meshes[0] for m in meshes): From 00279b8dd34f50b1500e5acede04502fd9407219 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Thu, 28 Nov 2024 06:21:05 +0000 Subject: [PATCH 2/7] minor change --- firedrake/output/vtk_output.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/firedrake/output/vtk_output.py b/firedrake/output/vtk_output.py index 26228c0369..82e2f34f60 100644 --- a/firedrake/output/vtk_output.py +++ b/firedrake/output/vtk_output.py @@ -492,8 +492,7 @@ def _prepare_output(self, function, max_elem): return OFunction(array=get_array(output), name=name, function=output) def _write_vtu(self, *functions): - from firedrake.function import Function - from firedrake.cofunction import Cofunction + from firedrake import Function, Cofunction # Check if the user has requested to write out a plain mesh if len(functions) == 1 and isinstance(functions[0], ufl.Mesh): From 942ff2867e6cd515897b0cc53546e88766d57343 Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Thu, 28 Nov 2024 06:56:58 +0000 Subject: [PATCH 3/7] Testing --- tests/firedrake/output/test_pvd_output.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/firedrake/output/test_pvd_output.py b/tests/firedrake/output/test_pvd_output.py index 6f17bcbe48..c85a418270 100644 --- a/tests/firedrake/output/test_pvd_output.py +++ b/tests/firedrake/output/test_pvd_output.py @@ -79,11 +79,16 @@ def test_bad_file_name(tmpdir): VTKFile(str(tmpdir.join("foo.vtu"))) -def test_different_functions(mesh, pvd): +@pytest.mark.parametrize("space", + ["primal", "dual"]) +def test_different_functions(mesh, pvd, space): V = FunctionSpace(mesh, "DG", 0) - - f = Function(V, name="foo") - g = Function(V, name="bar") + if space == "primal": + f = Function(V, name="foo") + g = Function(V, name="bar") + else: + f = Cofunction(V.dual(), name="foo") + g = Cofunction(V.dual(), name="bar") pvd.write(f) @@ -136,9 +141,14 @@ def test_not_function(mesh, pvd): pvd.write(grad(f)) -def test_append(mesh, tmpdir): +@pytest.mark.parametrize("space", + ["primal", "dual"]) +def test_append(mesh, tmpdir, space): V = FunctionSpace(mesh, "DG", 0) - g = Function(V) + if space == "primal": + g = Function(V) + else: + g = Cofunction(V.dual()) outfile = VTKFile(str(tmpdir.join("restart_test.pvd"))) outfile.write(g) From 20763529911264df868c92e399f60115c9ee56dc Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Thu, 28 Nov 2024 07:00:30 +0000 Subject: [PATCH 4/7] Update value error message --- firedrake/output/vtk_output.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firedrake/output/vtk_output.py b/firedrake/output/vtk_output.py index 82e2f34f60..488022bc07 100644 --- a/firedrake/output/vtk_output.py +++ b/firedrake/output/vtk_output.py @@ -503,7 +503,7 @@ def _write_vtu(self, *functions): for f in functions: if not isinstance(f, (Function, Cofunction)): - raise ValueError("Can only output Functions or a single mesh, not %r" % type(f)) + raise ValueError("Can only output Functions, Cofunctions or a single mesh, not %r" % type(f)) meshes = tuple(extract_unique_domain(f) for f in functions) if not all(m == meshes[0] for m in meshes): raise ValueError("All functions must be on same mesh") From 0eaed791f78a095ad036b9ce14aa4a37603b087f Mon Sep 17 00:00:00 2001 From: Iglesia Dolci Date: Thu, 28 Nov 2024 08:00:09 +0000 Subject: [PATCH 5/7] lint --- tests/firedrake/output/test_pvd_output.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/firedrake/output/test_pvd_output.py b/tests/firedrake/output/test_pvd_output.py index c85a418270..69c7243b7f 100644 --- a/tests/firedrake/output/test_pvd_output.py +++ b/tests/firedrake/output/test_pvd_output.py @@ -80,7 +80,7 @@ def test_bad_file_name(tmpdir): @pytest.mark.parametrize("space", - ["primal", "dual"]) + ["primal", "dual"]) def test_different_functions(mesh, pvd, space): V = FunctionSpace(mesh, "DG", 0) if space == "primal": @@ -142,7 +142,7 @@ def test_not_function(mesh, pvd): @pytest.mark.parametrize("space", - ["primal", "dual"]) + ["primal", "dual"]) def test_append(mesh, tmpdir, space): V = FunctionSpace(mesh, "DG", 0) if space == "primal": From b3d8705b688dab2574608909e7dcb5977870eb28 Mon Sep 17 00:00:00 2001 From: Daiane Iglesia Dolci <63597005+Ig-dolci@users.noreply.github.com> Date: Thu, 28 Nov 2024 16:41:22 +0000 Subject: [PATCH 6/7] Move to type(f).__name__ Co-authored-by: Connor Ward --- firedrake/output/vtk_output.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firedrake/output/vtk_output.py b/firedrake/output/vtk_output.py index 488022bc07..3b4141d0a7 100644 --- a/firedrake/output/vtk_output.py +++ b/firedrake/output/vtk_output.py @@ -503,7 +503,7 @@ def _write_vtu(self, *functions): for f in functions: if not isinstance(f, (Function, Cofunction)): - raise ValueError("Can only output Functions, Cofunctions or a single mesh, not %r" % type(f)) + raise ValueError(f"Can only output Functions, Cofunctions or a single mesh, not {type(f).__name__}") meshes = tuple(extract_unique_domain(f) for f in functions) if not all(m == meshes[0] for m in meshes): raise ValueError("All functions must be on same mesh") From 21715bd9d6174a785221d8619bd39fb31f3c4b9e Mon Sep 17 00:00:00 2001 From: Daiane Iglesia Dolci <63597005+Ig-dolci@users.noreply.github.com> Date: Fri, 29 Nov 2024 09:09:32 +0000 Subject: [PATCH 7/7] Update firedrake/output/vtk_output.py Co-authored-by: Connor Ward --- firedrake/output/vtk_output.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/firedrake/output/vtk_output.py b/firedrake/output/vtk_output.py index 3b4141d0a7..23072c5019 100644 --- a/firedrake/output/vtk_output.py +++ b/firedrake/output/vtk_output.py @@ -479,7 +479,8 @@ def _prepare_output(self, function, max_elem): raise ValueError("Unsupported shape %s" % (shape, )) if isinstance(function, Function): output = Function(V) - elif isinstance(function, Cofunction): + else: + assert isinstance(function, Cofunction) output = Function(V.dual()) if self.project: