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

New default value for DerivedQuantities.show_units #892

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/average_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ def __init__(self, field, surface) -> None:
def allowed_meshes(self):
return ["cartesian"]

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

Comment on lines +35 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you're going to add this new property everywhere, it should also go in the doc strings

@property
def title(self):
quantity_title = f"Average {self.field} surface {self.surface}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/average_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ def __init__(self, field, volume: int) -> None:
def allowed_meshes(self):
return ["cartesian"]

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

@property
def title(self):
quantity_title = f"Average {self.field} volume {self.volume}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
9 changes: 2 additions & 7 deletions festim/exports/derived_quantities/derived_quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DerivedQuantities(list):
iterations between each export. If None, the file will be
exported at the last timestep. Defaults to None.
show_units (bool, optional): will show the units of each
derived quantity in the title in export
derived quantity in the title in export. Defaults to True.

Attributes:
filename (str): the filename.
Expand All @@ -45,7 +45,7 @@ def __init__(
filename: str = None,
nb_iterations_between_compute: int = 1,
nb_iterations_between_exports: int = None,
show_units=False,
show_units=True,
) -> None:
# checks that input is list
if len(args) == 0:
Expand Down Expand Up @@ -125,11 +125,6 @@ def make_header(self):
header = ["t(s)"]
for quantity in self:
quantity.show_units = self.show_units
if self.show_units is False:
warnings.warn(
"The current derived_quantities title style will be deprecated in a future release, please use show_units=True instead",
DeprecationWarning,
)
header.append(quantity.title)
return header

Expand Down
2 changes: 1 addition & 1 deletion festim/exports/derived_quantities/derived_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, field) -> None:
self.T = None
self.data = []
self.t = []
self.show_units = False
self.show_units = True
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could maybe add this new default to the doc string

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well if it's not an argument then it's not really a default value. Like we don't document the fact that data has a "default" value of [] in the docstrings


@property
def allowed_meshes(self):
Expand Down
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/maximum_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ class MaximumSurface(SurfaceQuantity):
def __init__(self, field, surface) -> None:
super().__init__(field=field, surface=surface)

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

@property
def title(self):
quantity_title = f"Maximum {self.field} surface {self.surface}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/maximum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ class MaximumVolume(VolumeQuantity):
def __init__(self, field, volume) -> None:
super().__init__(field=field, volume=volume)

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

@property
def title(self):
quantity_title = f"Maximum {self.field} volume {self.volume}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/minimum_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ class MinimumSurface(SurfaceQuantity):
def __init__(self, field, surface) -> None:
super().__init__(field=field, surface=surface)

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

@property
def title(self):
quantity_title = f"Minimum {self.field} surface {self.surface}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/minimum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ class MinimumVolume(VolumeQuantity):
def __init__(self, field, volume) -> None:
super().__init__(field=field, volume=volume)

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

@property
def title(self):
quantity_title = f"Minimum {self.field} volume {self.volume}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
12 changes: 8 additions & 4 deletions festim/exports/derived_quantities/point_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ def __init__(self, field: str or int, x: int or float or tuple or list) -> None:
x = [x]
self.x = x

@property
def export_unit(self):
if self.field == "T":
return "K"
else:
return "H m-3"

@property
def title(self):
quantity_title = f"{self.field} value at {self.x}"
if self.show_units:
if self.field == "T":
return quantity_title + " (K)"
else:
return quantity_title + " (H m-3)"
return quantity_title + f" ({self.export_unit})"
else:
return quantity_title

Expand Down
12 changes: 5 additions & 7 deletions festim/exports/derived_quantities/surface_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ def export_unit(self):

@property
def title(self):
quantity_title = f"Flux surface {self.surface}: {self.field}"
if self.show_units:
if self.field == "T":
quantity_title = f"Heat flux surface {self.surface}"
else:
quantity_title = f"{self.field} flux surface {self.surface}"
if self.field == "T":
quantity_title = f"Heat flux surface {self.surface}"
else:
quantity_title = f"{self.field} flux surface {self.surface}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, seen as only the mobile solute can used in the flux calculation, is it worth hard coding this?

Suggested change
quantity_title = f"{self.field} flux surface {self.surface}"
quantity_title = f"solute flux surface {self.surface}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well I wouldn't cause we may move away from "solute" at some point


if self.show_units:
return quantity_title + f" ({self.export_unit})"

else:
return quantity_title

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_title_with_units():

def test_title_without_units():
my_quantity = AdsorbedHydrogen(1)
my_quantity.show_units = False
assert my_quantity.title == "Adsorbed H on surface 1"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def test_title(field, surface):
"""

my_average = AverageSurface(field, surface)
assert my_average.title == "Average {} surface {}".format(field, surface)
assert (
my_average.title
== f"Average {field} surface {surface} ({my_average.export_unit})"
)


class TestCompute:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def test_title(field, volume):
"""

my_average = AverageVolume(field, volume)
assert my_average.title == "Average {} volume {}".format(field, volume)
assert (
my_average.title
== f"Average {field} volume {volume} ({my_average.export_unit})"
)


class TestCompute:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ class TestMakeHeader:
sph_surface_flux_2 = SurfaceFluxSpherical("T", 6)
ads_h = AdsorbedHydrogen(1)

mesh = f.UnitIntervalMesh(10)
V = f.FunctionSpace(mesh, "P", 1)
Comment on lines +50 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

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

could import the mesh and function space from .tools.py? not necessary though


def test_simple(self):
"""
Tests that a proper header is made for the output .csv file
when there is only one festim.DerivedQuantity object
"""
my_derv_quant = DerivedQuantities([self.surface_flux_1])

for quantity in my_derv_quant:
quantity.function = f.Function(self.V)
RemDelaporteMathurin marked this conversation as resolved.
Show resolved Hide resolved

header = my_derv_quant.make_header()

expected_header = ["t(s)", self.surface_flux_1.title]
assert header == expected_header

Expand All @@ -68,6 +76,9 @@ def test_two_quantities(self):
self.tot_surf_1,
]
)
for quantity in my_derv_quant:
quantity.function = f.Function(self.V)

header = my_derv_quant.make_header()
expected_header = ["t(s)", self.surface_flux_1.title, self.tot_surf_1.title]
assert header == expected_header
Expand All @@ -89,6 +100,11 @@ def test_all_quantities(self):
self.ads_h,
]
)

# to compute the units of the quantities they need a function
for quantity in my_derv_quant:
quantity.function = f.Function(self.V)

header = my_derv_quant.make_header()
expected_header = ["t(s)"] + [
self.surface_flux_1.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ def test_title_with_units(function, expected_title):

def test_title_without_units():
my_flux = HydrogenFlux(4)
assert my_flux.title == "Flux surface 4: solute"
my_flux.show_units = False
assert my_flux.title == "solute flux surface 4"
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_title(field, surface):
"""

my_max = MaximumSurface(field, surface)
assert my_max.title == "Maximum {} surface {}".format(field, surface)
assert my_max.title == f"Maximum {field} surface {surface} ({my_max.export_unit})"


class TestCompute:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_title(field, volume):
"""

my_max = MaximumVolume(field, volume)
assert my_max.title == "Maximum {} volume {}".format(field, volume)
assert my_max.title == f"Maximum {field} volume {volume} ({my_max.export_unit})"


class TestCompute:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_title(field, surface):
"""

my_min = MinimumSurface(field, surface)
assert my_min.title == "Minimum {} surface {}".format(field, surface)
assert my_min.title == f"Minimum {field} surface {surface} ({my_min.export_unit})"


class TestCompute:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_title(field, volume):
"""

my_min = MinimumVolume(field, volume)
assert my_min.title == "Minimum {} volume {}".format(field, volume)
assert my_min.title == f"Minimum {field} volume {volume} ({my_min.export_unit})"


class TestCompute:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_title(field):
"""
x = 1
my_value = PointValue(field, x)
assert my_value.title == "{} value at [{}]".format(field, x)
assert my_value.title == f"{field} value at [{x}] ({my_value.export_unit})"


@pytest.mark.parametrize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def test_title(field, surface):
"""

my_h_flux = SurfaceFlux(field, surface)
assert my_h_flux.title == "Flux surface {}: {}".format(surface, field)
my_h_flux.function = c_1D
if field == "T":
expected_title = f"Heat flux surface {surface} ({my_h_flux.export_unit})"
else:
expected_title = f"{field} flux surface {surface} ({my_h_flux.export_unit})"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again see previous comment on surface flux. Might be mistaken though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

see previous comment

assert my_h_flux.title == expected_title


class TestCompute:
Expand Down Expand Up @@ -324,6 +329,7 @@ def test_cylindrical_flux_title_no_units_solute():
festim.CylindricalSurfaceFlux with a solute field without units"""

my_h_flux = SurfaceFluxCylindrical("solute", 2)
my_h_flux.show_units = False
assert my_h_flux.title == "solute flux surface 2"


Expand All @@ -332,6 +338,7 @@ def test_cylindrical_flux_title_no_units_temperature():
festim.CylindricalSurfaceFlux with a T field without units"""

my_heat_flux = SurfaceFluxCylindrical("T", 4)
my_heat_flux.show_units = False
assert my_heat_flux.title == "Heat flux surface 4"


Expand All @@ -357,6 +364,7 @@ def test_spherical_flux_title_no_units_solute():
festim.SphericalSurfaceFlux with a solute field without units"""

my_h_flux = SurfaceFluxSpherical("solute", 3)
my_h_flux.show_units = False
assert my_h_flux.title == "solute flux surface 3"


Expand All @@ -365,6 +373,7 @@ def test_spherical_flux_title_no_units_temperature():
festim.CSphericalSurfaceFlux with a T field without units"""

my_heat_flux = SurfaceFluxSpherical("T", 5)
my_heat_flux.show_units = False
assert my_heat_flux.title == "Heat flux surface 5"


Expand Down
Loading
Loading