From d31bcb80619f095dd68e3aabe6cd44c9c76a9b17 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 8 Oct 2024 13:31:54 -0400 Subject: [PATCH] Refactor derived quantity titles to include export units --- festim/exports/derived_quantities/average_surface.py | 12 ++++++++---- festim/exports/derived_quantities/average_volume.py | 12 ++++++++---- festim/exports/derived_quantities/maximum_surface.py | 12 ++++++++---- festim/exports/derived_quantities/maximum_volume.py | 12 ++++++++---- festim/exports/derived_quantities/minimum_surface.py | 12 ++++++++---- festim/exports/derived_quantities/minimum_volume.py | 12 ++++++++---- festim/exports/derived_quantities/point_value.py | 12 ++++++++---- festim/exports/derived_quantities/surface_flux.py | 12 +++++------- .../test_adsorbed_hydrogen.py | 1 + .../test_derived_quantities/test_average_surface.py | 5 ++++- .../test_derived_quantities/test_average_volume.py | 5 ++++- .../test_derived_quantities/test_hydrogen_flux.py | 3 ++- .../test_derived_quantities/test_maximum_surface.py | 2 +- .../test_derived_quantities/test_maximum_volume.py | 2 +- .../test_derived_quantities/test_minimum_surface.py | 2 +- .../test_derived_quantities/test_minimum_volume.py | 2 +- .../test_derived_quantities/test_point_value.py | 2 +- .../test_derived_quantities/test_surface_flux.py | 11 ++++++++++- .../test_derived_quantities/test_thermal_flux.py | 3 ++- .../test_derived_quantities/test_total_surface.py | 3 ++- .../test_derived_quantities/test_total_volume.py | 3 ++- 21 files changed, 93 insertions(+), 47 deletions(-) diff --git a/festim/exports/derived_quantities/average_surface.py b/festim/exports/derived_quantities/average_surface.py index c45f67218..cb7bddad0 100644 --- a/festim/exports/derived_quantities/average_surface.py +++ b/festim/exports/derived_quantities/average_surface.py @@ -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" + @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 diff --git a/festim/exports/derived_quantities/average_volume.py b/festim/exports/derived_quantities/average_volume.py index 7bc0e07c2..a91967e3d 100644 --- a/festim/exports/derived_quantities/average_volume.py +++ b/festim/exports/derived_quantities/average_volume.py @@ -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 diff --git a/festim/exports/derived_quantities/maximum_surface.py b/festim/exports/derived_quantities/maximum_surface.py index 4601e2faf..e83bc0e36 100644 --- a/festim/exports/derived_quantities/maximum_surface.py +++ b/festim/exports/derived_quantities/maximum_surface.py @@ -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 diff --git a/festim/exports/derived_quantities/maximum_volume.py b/festim/exports/derived_quantities/maximum_volume.py index cabf82ab5..9fc835833 100644 --- a/festim/exports/derived_quantities/maximum_volume.py +++ b/festim/exports/derived_quantities/maximum_volume.py @@ -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 diff --git a/festim/exports/derived_quantities/minimum_surface.py b/festim/exports/derived_quantities/minimum_surface.py index 55b576452..ec814821f 100644 --- a/festim/exports/derived_quantities/minimum_surface.py +++ b/festim/exports/derived_quantities/minimum_surface.py @@ -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 diff --git a/festim/exports/derived_quantities/minimum_volume.py b/festim/exports/derived_quantities/minimum_volume.py index 47025118f..aee07ea48 100644 --- a/festim/exports/derived_quantities/minimum_volume.py +++ b/festim/exports/derived_quantities/minimum_volume.py @@ -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 diff --git a/festim/exports/derived_quantities/point_value.py b/festim/exports/derived_quantities/point_value.py index aec7fd39d..6a4e0cc94 100644 --- a/festim/exports/derived_quantities/point_value.py +++ b/festim/exports/derived_quantities/point_value.py @@ -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 diff --git a/festim/exports/derived_quantities/surface_flux.py b/festim/exports/derived_quantities/surface_flux.py index 511485797..d951626d7 100644 --- a/festim/exports/derived_quantities/surface_flux.py +++ b/festim/exports/derived_quantities/surface_flux.py @@ -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}" + if self.show_units: return quantity_title + f" ({self.export_unit})" - else: return quantity_title diff --git a/test/unit/test_exports/test_derived_quantities/test_adsorbed_hydrogen.py b/test/unit/test_exports/test_derived_quantities/test_adsorbed_hydrogen.py index 58e001a43..50f009cba 100644 --- a/test/unit/test_exports/test_derived_quantities/test_adsorbed_hydrogen.py +++ b/test/unit/test_exports/test_derived_quantities/test_adsorbed_hydrogen.py @@ -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" diff --git a/test/unit/test_exports/test_derived_quantities/test_average_surface.py b/test/unit/test_exports/test_derived_quantities/test_average_surface.py index 556dd7865..5c7f97a2e 100644 --- a/test/unit/test_exports/test_derived_quantities/test_average_surface.py +++ b/test/unit/test_exports/test_derived_quantities/test_average_surface.py @@ -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: diff --git a/test/unit/test_exports/test_derived_quantities/test_average_volume.py b/test/unit/test_exports/test_derived_quantities/test_average_volume.py index 172f9b5e6..14905ca07 100644 --- a/test/unit/test_exports/test_derived_quantities/test_average_volume.py +++ b/test/unit/test_exports/test_derived_quantities/test_average_volume.py @@ -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: diff --git a/test/unit/test_exports/test_derived_quantities/test_hydrogen_flux.py b/test/unit/test_exports/test_derived_quantities/test_hydrogen_flux.py index 96aa175e4..f86316a67 100644 --- a/test/unit/test_exports/test_derived_quantities/test_hydrogen_flux.py +++ b/test/unit/test_exports/test_derived_quantities/test_hydrogen_flux.py @@ -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" diff --git a/test/unit/test_exports/test_derived_quantities/test_maximum_surface.py b/test/unit/test_exports/test_derived_quantities/test_maximum_surface.py index b97a707d6..e7c95e59b 100644 --- a/test/unit/test_exports/test_derived_quantities/test_maximum_surface.py +++ b/test/unit/test_exports/test_derived_quantities/test_maximum_surface.py @@ -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: diff --git a/test/unit/test_exports/test_derived_quantities/test_maximum_volume.py b/test/unit/test_exports/test_derived_quantities/test_maximum_volume.py index 2dffe5d97..9a4d0eae8 100644 --- a/test/unit/test_exports/test_derived_quantities/test_maximum_volume.py +++ b/test/unit/test_exports/test_derived_quantities/test_maximum_volume.py @@ -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: diff --git a/test/unit/test_exports/test_derived_quantities/test_minimum_surface.py b/test/unit/test_exports/test_derived_quantities/test_minimum_surface.py index c2b7801a2..8a7c66df1 100644 --- a/test/unit/test_exports/test_derived_quantities/test_minimum_surface.py +++ b/test/unit/test_exports/test_derived_quantities/test_minimum_surface.py @@ -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: diff --git a/test/unit/test_exports/test_derived_quantities/test_minimum_volume.py b/test/unit/test_exports/test_derived_quantities/test_minimum_volume.py index 61fd983bb..af9c36e26 100644 --- a/test/unit/test_exports/test_derived_quantities/test_minimum_volume.py +++ b/test/unit/test_exports/test_derived_quantities/test_minimum_volume.py @@ -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: 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 fc49133f4..3040fa034 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 @@ -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( diff --git a/test/unit/test_exports/test_derived_quantities/test_surface_flux.py b/test/unit/test_exports/test_derived_quantities/test_surface_flux.py index e2b2e4015..5ba066ba4 100644 --- a/test/unit/test_exports/test_derived_quantities/test_surface_flux.py +++ b/test/unit/test_exports/test_derived_quantities/test_surface_flux.py @@ -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})" + assert my_h_flux.title == expected_title class TestCompute: @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/test/unit/test_exports/test_derived_quantities/test_thermal_flux.py b/test/unit/test_exports/test_derived_quantities/test_thermal_flux.py index 92776a1b9..fdaa509a1 100644 --- a/test/unit/test_exports/test_derived_quantities/test_thermal_flux.py +++ b/test/unit/test_exports/test_derived_quantities/test_thermal_flux.py @@ -30,5 +30,6 @@ def test_title_with_units(function, expected_title): def test_title_without_units(): my_flux = ThermalFlux(5) + my_flux.show_units = False - assert my_flux.title == "Flux surface 5: T" + assert my_flux.title == "Heat flux surface 5" diff --git a/test/unit/test_exports/test_derived_quantities/test_total_surface.py b/test/unit/test_exports/test_derived_quantities/test_total_surface.py index 4047b58c4..e11a2f131 100644 --- a/test/unit/test_exports/test_derived_quantities/test_total_surface.py +++ b/test/unit/test_exports/test_derived_quantities/test_total_surface.py @@ -17,7 +17,8 @@ def test_title(field, surface): """ my_total = TotalSurface(field, surface) - assert my_total.title == "Total {} surface {}".format(field, surface) + my_total.function = c_2D + assert my_total.title == f"Total {field} surface {surface} ({my_total.export_unit})" class TestCompute: diff --git a/test/unit/test_exports/test_derived_quantities/test_total_volume.py b/test/unit/test_exports/test_derived_quantities/test_total_volume.py index 5fd3a8cb3..1f6929977 100644 --- a/test/unit/test_exports/test_derived_quantities/test_total_volume.py +++ b/test/unit/test_exports/test_derived_quantities/test_total_volume.py @@ -17,7 +17,8 @@ def test_title(field, volume): """ my_total = TotalVolume(field, volume) - assert my_total.title == "Total {} volume {}".format(field, volume) + my_total.function = c_2D + assert my_total.title == f"Total {field} volume {volume} ({my_total.export_unit})" class TestCompute: