From bf943af340f0d49b48dbafd6539d05dc10242c41 Mon Sep 17 00:00:00 2001 From: KulaginVladimir Date: Tue, 28 Nov 2023 20:56:36 +0300 Subject: [PATCH 1/5] Added PointValue class --- docs/source/api/exports.rst | 4 +++ festim/__init__.py | 1 + .../exports/derived_quantities/point_value.py | 19 +++++++++++ .../test_point_value.py | 34 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 festim/exports/derived_quantities/point_value.py create mode 100644 test/unit/test_exports/test_derived_quantities/test_point_value.py diff --git a/docs/source/api/exports.rst b/docs/source/api/exports.rst index 0ce8e4019..a064f1fd6 100644 --- a/docs/source/api/exports.rst +++ b/docs/source/api/exports.rst @@ -31,6 +31,10 @@ Exports :members: :show-inheritance: +.. autoclass:: PointValue + :members: + :show-inheritance: + .. autoclass:: AverageSurface :members: :show-inheritance: diff --git a/festim/__init__.py b/festim/__init__.py index 7f6e96839..2d1134f65 100644 --- a/festim/__init__.py +++ b/festim/__init__.py @@ -78,6 +78,7 @@ from .exports.derived_quantities.total_surface import TotalSurface from .exports.derived_quantities.total_volume import TotalVolume from .exports.derived_quantities.average_surface import AverageSurface +from .exports.derived_quantities.point_value import PointValue from .exports.derived_quantities.derived_quantities import DerivedQuantities diff --git a/festim/exports/derived_quantities/point_value.py b/festim/exports/derived_quantities/point_value.py new file mode 100644 index 000000000..9fd70febd --- /dev/null +++ b/festim/exports/derived_quantities/point_value.py @@ -0,0 +1,19 @@ +from festim import DerivedQuantity + + +class PointValue(DerivedQuantity): + """DerivedQuantity relative to a point + + Args: + field (str, int): the field ("solute", 0, 1, "T", "retention") + point (int, float, list): the point coordinates + """ + + def __init__(self, field: str or int, x: float or list) -> None: + super().__init__(field) + self.x = x + self.title = "{} value at {}".format(field, x) + + def compute(self): + """The value at the point""" + return self.function(self.x) diff --git a/test/unit/test_exports/test_derived_quantities/test_point_value.py b/test/unit/test_exports/test_derived_quantities/test_point_value.py new file mode 100644 index 000000000..06b383183 --- /dev/null +++ b/test/unit/test_exports/test_derived_quantities/test_point_value.py @@ -0,0 +1,34 @@ +from festim import PointValue +import fenics as f +import pytest + + +def test_title_H(): + x = 1 + field = "solute" + my_value = PointValue(field, x) + assert my_value.title == "{} value at {}".format(field, x) + + +def test_title_T(): + x = 1 + field = "T" + my_value = PointValue(field, x) + assert my_value.title == "{} value at {}".format(field, x) + + +class TestCompute: + mesh = f.UnitIntervalMesh(10) + V = f.FunctionSpace(mesh, "P", 1) + + c = f.interpolate(f.Expression("x[0]", degree=1), V) + + x = 1 + my_value = PointValue("solute", x) + my_value.function = c + + def test_minimum(self): + expected = self.c(self.x) + + produced = self.my_value.compute() + assert produced == expected From 6f77e5653528486990590e8e1204f1bce34b3cc9 Mon Sep 17 00:00:00 2001 From: KulaginVladimir Date: Tue, 28 Nov 2023 21:36:29 +0300 Subject: [PATCH 2/5] x should be iterable --- festim/exports/derived_quantities/point_value.py | 4 +++- .../test_exports/test_derived_quantities/test_point_value.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/festim/exports/derived_quantities/point_value.py b/festim/exports/derived_quantities/point_value.py index 9fd70febd..c1a0542d5 100644 --- a/festim/exports/derived_quantities/point_value.py +++ b/festim/exports/derived_quantities/point_value.py @@ -9,8 +9,10 @@ class PointValue(DerivedQuantity): point (int, float, list): the point coordinates """ - def __init__(self, field: str or int, x: float or list) -> None: + def __init__(self, field: str or int, x: int or float or list) -> None: super().__init__(field) + if not hasattr(x, "__iter__"): + x = [x] self.x = x self.title = "{} value at {}".format(field, x) diff --git a/test/unit/test_exports/test_derived_quantities/test_point_value.py b/test/unit/test_exports/test_derived_quantities/test_point_value.py index 06b383183..9a412e862 100644 --- a/test/unit/test_exports/test_derived_quantities/test_point_value.py +++ b/test/unit/test_exports/test_derived_quantities/test_point_value.py @@ -7,14 +7,14 @@ def test_title_H(): x = 1 field = "solute" my_value = PointValue(field, x) - assert my_value.title == "{} value at {}".format(field, x) + assert my_value.title == "{} value at [{}]".format(field, x) def test_title_T(): x = 1 field = "T" my_value = PointValue(field, x) - assert my_value.title == "{} value at {}".format(field, x) + assert my_value.title == "{} value at [{}]".format(field, x) class TestCompute: From 11446789d3ad555e62f341b6e43217a5bb34a015 Mon Sep 17 00:00:00 2001 From: Vladimir Kulagin <59147151+KulaginVladimir@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:30:35 +0300 Subject: [PATCH 3/5] Update festim/exports/derived_quantities/point_value.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Delaporte-Mathurin <40028739+RemDelaporteMathurin@users.noreply.github.com> --- festim/exports/derived_quantities/point_value.py | 1 + 1 file changed, 1 insertion(+) diff --git a/festim/exports/derived_quantities/point_value.py b/festim/exports/derived_quantities/point_value.py index c1a0542d5..742616c6d 100644 --- a/festim/exports/derived_quantities/point_value.py +++ b/festim/exports/derived_quantities/point_value.py @@ -11,6 +11,7 @@ class PointValue(DerivedQuantity): def __init__(self, field: str or int, x: int or float or list) -> None: super().__init__(field) + # make sure x is an iterable if not hasattr(x, "__iter__"): x = [x] self.x = x From ce6ec1e014915d25bd83eadc41e3be220d7cd2d9 Mon Sep 17 00:00:00 2001 From: Vladimir Kulagin <59147151+KulaginVladimir@users.noreply.github.com> Date: Tue, 28 Nov 2023 22:30:41 +0300 Subject: [PATCH 4/5] Update festim/exports/derived_quantities/point_value.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rémi Delaporte-Mathurin <40028739+RemDelaporteMathurin@users.noreply.github.com> --- festim/exports/derived_quantities/point_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/festim/exports/derived_quantities/point_value.py b/festim/exports/derived_quantities/point_value.py index 742616c6d..1baf48b2b 100644 --- a/festim/exports/derived_quantities/point_value.py +++ b/festim/exports/derived_quantities/point_value.py @@ -5,7 +5,7 @@ class PointValue(DerivedQuantity): """DerivedQuantity relative to a point Args: - field (str, int): the field ("solute", 0, 1, "T", "retention") + field (str, int): the field ("solute", 0, 1, "T", "retention") point (int, float, list): the point coordinates """ From d632fc09c0ce5a2773e73b147307c39d3a973d65 Mon Sep 17 00:00:00 2001 From: KulaginVladimir Date: Wed, 29 Nov 2023 11:05:01 +0300 Subject: [PATCH 5/5] Update tests --- .../exports/derived_quantities/point_value.py | 4 +-- .../test_point_value.py | 27 +++++++------------ 2 files changed, 11 insertions(+), 20 deletions(-) diff --git a/festim/exports/derived_quantities/point_value.py b/festim/exports/derived_quantities/point_value.py index 1baf48b2b..10b046560 100644 --- a/festim/exports/derived_quantities/point_value.py +++ b/festim/exports/derived_quantities/point_value.py @@ -6,10 +6,10 @@ class PointValue(DerivedQuantity): Args: field (str, int): the field ("solute", 0, 1, "T", "retention") - point (int, float, list): the point coordinates + point (int, float, tuple, list): the point coordinates """ - def __init__(self, field: str or int, x: int or float or list) -> None: + def __init__(self, field: str or int, x: int or float or tuple or list) -> None: super().__init__(field) # make sure x is an iterable if not hasattr(x, "__iter__"): diff --git a/test/unit/test_exports/test_derived_quantities/test_point_value.py b/test/unit/test_exports/test_derived_quantities/test_point_value.py index 9a412e862..7c0fa4f31 100644 --- a/test/unit/test_exports/test_derived_quantities/test_point_value.py +++ b/test/unit/test_exports/test_derived_quantities/test_point_value.py @@ -3,32 +3,23 @@ import pytest -def test_title_H(): +@pytest.mark.parametrize("field", ["solute", "T"]) +def test_title(field): x = 1 - field = "solute" my_value = PointValue(field, x) assert my_value.title == "{} value at [{}]".format(field, x) -def test_title_T(): - x = 1 - field = "T" - my_value = PointValue(field, x) - assert my_value.title == "{} value at [{}]".format(field, x) - - -class TestCompute: - mesh = f.UnitIntervalMesh(10) +@pytest.mark.parametrize( + "mesh,x", [(f.UnitIntervalMesh(10), 1), (f.UnitCubeMesh(10, 10, 10), (1, 0, 1))] +) +def test_point_compute(mesh, x): V = f.FunctionSpace(mesh, "P", 1) - c = f.interpolate(f.Expression("x[0]", degree=1), V) - x = 1 my_value = PointValue("solute", x) my_value.function = c - def test_minimum(self): - expected = self.c(self.x) - - produced = self.my_value.compute() - assert produced == expected + expected = c(x) + produced = my_value.compute() + assert produced == expected