Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PointValue class #655

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/api/exports.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Exports
:members:
:show-inheritance:

.. autoclass:: PointValue
:members:
:show-inheritance:

.. autoclass:: AverageSurface
:members:
:show-inheritance:
Expand Down
1 change: 1 addition & 0 deletions festim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions festim/exports/derived_quantities/point_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from festim import DerivedQuantity


class PointValue(DerivedQuantity):
"""DerivedQuantity relative to a point

Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
KulaginVladimir marked this conversation as resolved.
Show resolved Hide resolved
point (int, float, list): the point coordinates
"""

def __init__(self, field: str or int, x: int or float or list) -> None:
super().__init__(field)
if not hasattr(x, "__iter__"):
KulaginVladimir marked this conversation as resolved.
Show resolved Hide resolved
x = [x]
self.x = x
self.title = "{} value at {}".format(field, x)

def compute(self):
"""The value at the point"""
return self.function(self.x)
34 changes: 34 additions & 0 deletions test/unit/test_exports/test_derived_quantities/test_point_value.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge these two tests using pytest parametrized tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we modify all other tests (e.g. test_total_volume, test_total_surface, etc.) in the same way?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally yes but let's do this in a separate dedicated PR.



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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also test try to test this on a multi-dimensional mesh?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RemDelaporteMathurin you mean 2D/3D?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it me done as:

@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)

    my_value = PointValue("solute", x)
    my_value.function = c

    expected = c(x)
    produced = my_value.compute()
    assert produced == expected

or the TestCompute class should remain?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this you work just fine!

Loading