From 3c0a5ee51479eaad48784dfe746e13b76b7e3674 Mon Sep 17 00:00:00 2001 From: julian-evers <133691040+julian-evers@users.noreply.github.com> Date: Sat, 21 Oct 2023 01:30:22 +0200 Subject: [PATCH] better testing --- pybamm_tea/tea.py | 21 +++--- tests/capacities_and_potentials.csv | 25 +++++++ tests/stack_breakdown.csv | 17 +++++ tests/stack_energy_densities.csv | 7 ++ tests/test_tea.py | 110 ++++++++++++++++++++++------ 5 files changed, 148 insertions(+), 32 deletions(-) create mode 100644 tests/capacities_and_potentials.csv create mode 100644 tests/stack_breakdown.csv create mode 100644 tests/stack_energy_densities.csv diff --git a/pybamm_tea/tea.py b/pybamm_tea/tea.py index 791d548..2b81f6a 100644 --- a/pybamm_tea/tea.py +++ b/pybamm_tea/tea.py @@ -106,26 +106,27 @@ def initialize(self): fractions. """ pava = self.parameter_values + # rho_sep=rho_elyte*por_sep+rho_mat*(1-por_sep) + # calculate separator density from porosity, electrolyte density and seperaor if pava.get("Separator dry density [kg.m-3]") is not None: pava["Separator density [kg.m-3]"] = pava.get( "Separator porosity" - ) * pava.get("Electrolyte density [kg.m-3]", 0) + pava.get( - "Separator dry density [kg.m-3]", 0 - ) + ) * pava.get("Electrolyte density [kg.m-3]") + pava.get( + "Separator dry density [kg.m-3]") if pava.get("Separator material density [kg.m-3]") is not None: pava["Separator density [kg.m-3]"] = pava.get( "Separator porosity" - ) * pava.get("Electrolyte density [kg.m-3]", 0) + ( + ) * pava.get("Electrolyte density [kg.m-3]") + ( 1 - pava.get("Separator porosity") ) * pava.get( - "Separator material density [kg.m-3]", 0 + "Separator material density [kg.m-3]" ) electrodes = ["Negative electrode", "Positive electrode"] for electrode in electrodes: if ( ( - pava.get(f"{electrode} active material volume fraction", 0) - + pava.get(f"{electrode} porosity", 0) + pava.get(f"{electrode} active material volume fraction") + + pava.get(f"{electrode} porosity") == 1 ) and pava.get(f"{electrode} binder dry mass fraction") is None @@ -136,7 +137,7 @@ def initialize(self): pava[f"{electrode} dry density [kg.m-3]"] = pava.get( f"{electrode} density [kg.m-3]" ) - pava.get(f"{electrode} porosity") * pava.get( - "Electrolyte density [kg.m-3]", 0 + "Electrolyte density [kg.m-3]" ) pava[f"{electrode} active material density [kg.m-3]"] = pava.get( f"{electrode} dry density [kg.m-3]" @@ -286,8 +287,8 @@ def initialize(self): * pava.get("Maximum concentration in positive electrode [mol.m-3]") ) warnings.warn( - "Warning: 'Negative electrode thickness [m]' has been calculated from " - "'Theoretical n/p ratio' and 'Positive electrode thickness [m]'" + "Warning: 'Positive electrode thickness [m]' has been calculated from " + "'Theoretical n/p ratio' and 'Negative electrode thickness [m]'" ) if ( pava.get("Negative electrode thickness [m]") is not None diff --git a/tests/capacities_and_potentials.csv b/tests/capacities_and_potentials.csv new file mode 100644 index 0000000..e3c9507 --- /dev/null +++ b/tests/capacities_and_potentials.csv @@ -0,0 +1,25 @@ +,Parameter,Unit,Value +0,Stack average OCP,V,3.7566884932653757 +1,Minimal OCP,V,2.5002934432759223 +2,Maximal OCP,V,4.199991638671754 +3,Negative electrode average OCP,V,0.16911648682881586 +4,Positive electrode average OCP,V,3.9258049800941914 +5,Practical n/p ratio,,1.1794155043339016 +6,Theoretical n/p ratio,,0.8212130653900627 +7,Volumetric stack capacity,A.h.L-1,122.57415815387185 +8,Gravimetric stack capacity,A.h.kg-1,53.181271493272135 +9,Capacity,mA.h.cm-2,1.991830070000417 +10,Negative electrode theoretical capacity,mA.h.cm-2,2.3575725906940668 +11,Positive electrode theoretical capacity,mA.h.cm-2,2.8708415514239998 +12,Negative electrode volumetric capacity,A.h.L-1,269.16622567573205 +13,Positive electrode volumetric capacity,A.h.L-1,368.85742037044764 +14,Negative electrode gravimetric capacity,A.h.kg-1,173.0972512384129 +15,Positive electrode gravimetric capacity,A.h.kg-1,127.41188959255531 +16,Negative electrode active material practical capacity,A.h.kg-1,318.9682739774891 +17,Positive electrode active material practical capacity,A.h.kg-1,190.1797456950419 +18,Negative electrode active material capacity,A.h.kg-1,377.53765813474547 +19,Positive electrode active material capacity,A.h.kg-1,274.1076783625731 +20,Negative electrode stoichiometry at 0% SoC,,0.0035533684817003956 +21,Negative electrode stoichiometry at 100% SoC,,0.8484181577410738 +22,Positive electrode stoichiometry at 100% SoC,,0.23526436281608434 +23,Positive electrode stoichiometry at 0% SoC,,0.9290783662439037 diff --git a/tests/stack_breakdown.csv b/tests/stack_breakdown.csv new file mode 100644 index 0000000..9bdbb05 --- /dev/null +++ b/tests/stack_breakdown.csv @@ -0,0 +1,17 @@ +,Volume loading [uL.cm-2],Mass loading [mg.cm-2],Density [mg.uL-1] +Negative electrode electrolyte,2.4346,3.1065496,1.276 +Negative electrode active material,2.7557821999999996,6.244602465199999,2.266 +Negative electrode inactive material,2.2096178,2.1558479347999993,0.9756655358225297 +Negative electrode dry,7.3999999999999995,8.400450399999999,1.1351959999999999 +Negative electrode,7.3999999999999995,11.506999999999998,1.555 +Positive electrode electrolyte,1.5983999999999998,2.0395583999999998,1.276 +Positive electrode active material,2.204928,10.473408000000001,4.75 +Positive electrode inactive material,1.5966719999999996,3.1200336,1.9540854978354985 +Positive electrode dry,5.3999999999999995,13.5934416,2.517304 +Positive electrode,5.3999999999999995,15.633,2.895 +Separator electrolyte,1.016,1.296416,1.276 +Separator material,0.9840000000000001,0.7375840000000001,0.7495772357723578 +Separator,2.0,2.034,1.017 +Separator dry,2.0,0.737584,0.368792 +Negative current collector,1.4,12.5062,8.933 +Positive current collector,1.5,4.053,2.702 diff --git a/tests/stack_energy_densities.csv b/tests/stack_energy_densities.csv new file mode 100644 index 0000000..24e7056 --- /dev/null +++ b/tests/stack_energy_densities.csv @@ -0,0 +1,7 @@ +,Parameter,Unit,Value +0,Volumetric stack energy density,W.h.L-1,460.4729295083407 +1,Gravimetric stack energy density,W.h.kg-1,199.78547067599737 +2,Stack average OCP,V,3.7566884932653757 +3,Capacity,mA.h.cm-2,1.991830070000417 +4,Stack thickness,um,162.49999999999997 +5,Stack density,kg.L-1,2.3048369230769237 diff --git a/tests/test_tea.py b/tests/test_tea.py index 57e7547..97469d8 100644 --- a/tests/test_tea.py +++ b/tests/test_tea.py @@ -2,7 +2,8 @@ import pybamm from pybamm_tea import TEA import matplotlib.pyplot as plt -import pandas as pd +import pandas as pd +import numpy as np class TestTEA(unittest.TestCase): @@ -17,33 +18,98 @@ def ExampleModel(self): param_nco.update(nco_input_data, check_already_exists=False) tea_nco = TEA(param_nco, nco_input_data) return tea_nco + + # test stack breakdown + def test_stack_breakdown(self): + + # load example model + tea_nco = self.ExampleModel() + + # check if pandas dataframe and dict + self.assertIsInstance(tea_nco.stack_breakdown, dict) + self.assertIsInstance(tea_nco.stack_breakdown_dataframe, pd.DataFrame) - # test stack energy densities dict + # import stack breakdown dataframe from csv + ref_stack_breakdown_dataframe = pd.read_csv("tests/stack_breakdown.csv", index_col=0) + + # replace NaN values with empty string + ref_stack_breakdown_dataframe.replace(np.nan, "", inplace=True) + tea_nco.stack_breakdown_dataframe.replace(np.nan, "", inplace=True) + + # compare each cell of the dataframe + for i in ref_stack_breakdown_dataframe.columns: + for j in ref_stack_breakdown_dataframe.index: + if isinstance(tea_nco.stack_breakdown_dataframe[i][j], str): + self.assertEqual( + tea_nco.stack_breakdown_dataframe[i][j], + ref_stack_breakdown_dataframe[i][j], + ) + else: + self.assertAlmostEqual( + round(tea_nco.stack_breakdown_dataframe[i][j], 7), + round(ref_stack_breakdown_dataframe[i][j], 7), + ) + + # test stack energy densities def test_stack_energy_densities(self): + + # load example model tea_nco = self.ExampleModel() + + # check if pandas dataframe and dict self.assertIsInstance(tea_nco.stack_energy_densities, dict) - self.assertEqual(len(list(tea_nco.stack_energy_densities.keys())), 34) - self.assertEqual( - tea_nco.stack_energy_densities.get( - "Gravimetric stack energy density [W.h.kg-1]" - ).round(), - 200.0, - ) - - # test stack breakdown dict - def test_stack_breakdown(self): - tea_nco = self.ExampleModel() - self.assertIsInstance(tea_nco.stack_breakdown, dict) - self.assertEqual(len(list(tea_nco.stack_breakdown.keys())), 62) + self.assertIsInstance(tea_nco.stack_energy_densities_dataframe, pd.DataFrame) + + # import stack energy densities dataframe from csv + ref_stack_energy_densities_dataframe = pd.read_csv("tests/stack_energy_densities.csv", index_col=0) + + # replace NaN values with empty string + ref_stack_energy_densities_dataframe.replace(np.nan, "", inplace=True) + tea_nco.stack_energy_densities_dataframe.replace(np.nan, "", inplace=True) + + # compare each cell of the dataframe + for i in ref_stack_energy_densities_dataframe.columns: + for j in ref_stack_energy_densities_dataframe.index: + if isinstance(tea_nco.stack_energy_densities_dataframe[i][j], str): + self.assertEqual( + tea_nco.stack_energy_densities_dataframe[i][j], + ref_stack_energy_densities_dataframe[i][j], + ) + else: + self.assertAlmostEqual( + round(tea_nco.stack_energy_densities_dataframe[i][j], 7), + round(ref_stack_energy_densities_dataframe[i][j], 7), + ) + + def test_capacities_and_potentials(self): - # test stack breakdown dataframe - def test_stack_breakdown_dataframe(self): + # load example model tea_nco = self.ExampleModel() - self.assertIsInstance(tea_nco.stack_breakdown_dataframe, pd.DataFrame) - self.assertEqual( - list(tea_nco.stack_breakdown_dataframe.columns), - ["Volume loading [uL.cm-2]", "Mass loading [mg.cm-2]", "Density [mg.uL-1]"], - ), "stack_breakdown_dataframe columns are correct" + + # check if pandas dataframe and dict + self.assertIsInstance(tea_nco.capacities_and_potentials_dataframe, pd.DataFrame) + + # import stack breakdown dataframe from csv + ref_capacities_and_potentials = pd.read_csv("tests/capacities_and_potentials.csv", index_col=0) + + # replace NaN values with empty string + ref_capacities_and_potentials.replace(np.nan, "", inplace=True) + tea_nco.capacities_and_potentials_dataframe.replace(np.nan, "", inplace=True) + + # compare each cell of the dataframe + for i in ref_capacities_and_potentials.columns: + for j in ref_capacities_and_potentials.index: + if isinstance(tea_nco.capacities_and_potentials_dataframe[i][j], str): + self.assertEqual( + tea_nco.capacities_and_potentials_dataframe[i][j], + ref_capacities_and_potentials[i][j], + ) + else: + self.assertAlmostEqual( + round(tea_nco.capacities_and_potentials_dataframe[i][j], 7), + round(ref_capacities_and_potentials[i][j], 7), + ) + # test stack breakdown plot def test_plot_stack_breakdown(self):