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

Test dataframe entries #22

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 11 additions & 10 deletions pybamm_tea/tea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions tests/capacities_and_potentials.csv
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions tests/stack_breakdown.csv
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions tests/stack_energy_densities.csv
Original file line number Diff line number Diff line change
@@ -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
110 changes: 88 additions & 22 deletions tests/test_tea.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -17,33 +18,98 @@
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

it might be cleaner to have the reference data frames just defined at the top of this file rather than reading from csv. happy to leave it as is for now though

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(

Check warning on line 43 in tests/test_tea.py

View check run for this annotation

Codecov / codecov/patch

tests/test_tea.py#L43

Added line #L43 was not covered by tests
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):
Expand Down
Loading