Repository: RemDelaporteMathurin/h-transport-materials

Issue:
Pint for temperature range and data_T data_y

Comments:

Patch:
diff --git a/h_transport_materials/property.py b/h_transport_materials/property.py
index d33a6f3..0f16ab4 100644
--- a/h_transport_materials/property.py
+++ b/h_transport_materials/property.py
@@ -236,6 +236,11 @@ class ArrheniusProperty(Property):
         if value is None:
             self._data_T = value
             return
+        if isinstance(value, pint.Quantity):
+            # convert to K
+            value = value.to(ureg.K).magnitude
+        else:
+            warnings.warn(f"no units were given with data_T, assuming {self.units:~}")
         if not isinstance(value, (list, np.ndarray)):
             raise TypeError("data_T accepts list or np.ndarray")
         elif isinstance(value, list):
@@ -255,6 +260,11 @@ class ArrheniusProperty(Property):
         if value is None:
             self._data_y = value
             return
+        if isinstance(value, pint.Quantity):
+            # convert to right units
+            value = value.to(self.units).magnitude
+        else:
+            warnings.warn(f"no units were given with data_y, assuming {self.units:~}")
         if not isinstance(value, (list, np.ndarray)):
             raise TypeError("data_y accepts list or np.ndarray")
         elif isinstance(value, list):
@@ -266,7 +276,11 @@ class ArrheniusProperty(Property):
             self._data_y = value[~np.isnan(value)]
 
     def fit(self):
-        self.pre_exp, self.act_energy = fit_arhenius(self.data_y, self.data_T)
+        pre_exp, act_energy = fit_arhenius(self.data_y, self.data_T)
+        self.pre_exp, self.act_energy = (
+            pre_exp * self.units,
+            act_energy * DEFAULT_ENERGY_UNITS,
+        )
         self.range = (self.data_T.min(), self.data_T.max())
 
     def value(self, T, exp=np.exp):


Test patch:
diff --git a/tests/test_diffusivity.py b/tests/test_diffusivity.py
index abd97a8..00658d9 100644
--- a/tests/test_diffusivity.py
+++ b/tests/test_diffusivity.py
@@ -16,3 +16,39 @@ def test_act_energy_can_be_quantity():
         E_D=0.5 * htm.ureg.kJ * htm.ureg.mol**-1,
     )
     assert prop.act_energy != 0.5
+
+
+def test_fit_with_units_data_y():
+    """Checks that units are taken into account in data_y"""
+    data_T = [1, 2, 3] * htm.ureg.K
+    data_y_ref = [1, 2, 3] * htm.ureg.m**2 * htm.ureg.s**-1
+    my_prop_ref = htm.Diffusivity(
+        data_T=data_T,
+        data_y=data_y_ref,
+    )
+
+    my_prop_other_units = htm.Diffusivity(
+        data_T=data_T,
+        data_y=data_y_ref.to(htm.ureg.cm**2 * htm.ureg.s**-1),
+    )
+
+    assert my_prop_other_units.pre_exp == my_prop_ref.pre_exp
+    assert my_prop_other_units.act_energy == my_prop_ref.act_energy
+
+
+def test_fit_with_units_data_T():
+    """Checks that units are taken into account in data_T"""
+    data_T_ref = [1, 2, 3] * htm.ureg.K
+    data_y = [1, 2, 3] * htm.ureg.m**2 * htm.ureg.s**-1
+    my_prop_ref = htm.Diffusivity(
+        data_T=data_T_ref,
+        data_y=data_y,
+    )
+
+    my_prop_other_units = htm.Diffusivity(
+        data_T=data_T_ref.to("degC"),
+        data_y=data_y,
+    )
+
+    assert my_prop_other_units.pre_exp == my_prop_ref.pre_exp
+    assert my_prop_other_units.act_energy == my_prop_ref.act_energy