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

Nest and Flatten Dictionaries #4377

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
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
89 changes: 89 additions & 0 deletions src/pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,3 +930,92 @@ def print_evaluated_parameters(self, evaluated_parameters, output_file):
file.write((s + " : {:10.4g}\n").format(name, value))
else:
file.write((s + " : {:10.3E}\n").format(name, value))


EXPERIMENT_PARAMS = [
"Reference temperature [K]",
"Total heat transfer coefficient [W.m-2.K-1]",
"Ambient temperature [K]",
"Number of electrodes connected in parallel to make a cell",
"Number of cells connected in series to make a battery",
"Lower voltage cut-off [V]",
"Upper voltage cut-off [V]",
"Open-circuit voltage at 0% SOC [V]",
"Open-circuit voltage at 100% SOC [V]",
"Initial concentration in negative electrode [mol.m-3]",
"Initial concentration in positive electrode [mol.m-3]",
"Initial temperature [K]",
]

CELL_PARAMS = [
"Electrode height [m]",
"Electrode width [m]",
"Cell volume [m3]",
"Cell cooling surface area [m2]",
"Nominal cell capacity [A.h]",
]

categories = [
"Negative current collector",
"Negative electrode",
"Separator",
"Positive electrode",
"Positive current collector",
"Electrolyte",
]


def nest_dict(params):
nested_dict = defaultdict(dict)

for key, value in params.items():
# First catch special cases
if key in EXPERIMENT_PARAMS:
nested_dict["Experiment"][key] = value
elif key in CELL_PARAMS:
nested_dict["Cell"][key] = value
else:
# then loop through categories
param_added = False
key_lower = key.lower().replace(
"particle", "electrode"
) # catch "particle" in the electrode category
for category in categories:
if category.lower() in key_lower:
short_key = (
key.replace("particle", "electrode")
.replace(category, "")
.strip()
)
short_key = short_key[0].upper() + short_key[1:]
nested_dict[category][short_key] = value
param_added = True
break
if not param_added:
nested_dict["Misc."][key] = value

return dict(nested_dict)


def flat_dictionary(nested_dict):
flat_dict = {}

for category, params in nested_dict.items():
for key, value in params.items():
# Replace "electrode" with "particle" in key and remove category name
if category != "Experiment" and category != "Cell" and category != "Misc.":
key = key.replace(category, "").strip()
if "OCP" and "SEI" in key:
new_key = f"{category} {key[0]}{key[1:]}"
elif any(p in key for p in ["Diffusivity", "Radius"]):
new_key = f"{category.replace("electrode", "particle")} {key[0].lower()}{key[1:]}"
else:
# Combine category and key, capitalize the first letter of the key
new_key = f"{category} {key[0].lower()}{key[1:]}"
else:
new_key = key

# Save to flat_dict
flat_dict[new_key] = value

return flat_dict