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

chore: update pre-commit hooks #3890

Merged
merged 3 commits into from
Mar 13, 2024
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ a63e49ece0f9336d1f5c2562f7459e555c6e6693
ff6d81c01331c7d269303b4a8321d9881bdf98fa
# migrated to ruff-format - https://github.com/pybamm-team/PyBaMM/pull/3655
60ebd4148059a95428a496f4f55c1175ead362d3
# implemented cleaner string formatting via f-strings - https://github.com/pybamm-team/PyBaMM/pull/3890
f395819d1c874071b7e76e32ec4f0bbe42462b48
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.2.2"
rev: "v0.3.2"
hooks:
- id: ruff
args: [--fix, --show-fixes]
Expand Down
78 changes: 40 additions & 38 deletions docs/source/examples/notebooks/models/using-submodels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@
"metadata": {},
"outputs": [],
"source": [
"model.submodels[\n",
" \"negative primary particle\"\n",
"] = pybamm.particle.XAveragedPolynomialProfile(\n",
" model.param, \"negative\", options={**model.options, \"particle\": \"uniform profile\"}\n",
"model.submodels[\"negative primary particle\"] = (\n",
" pybamm.particle.XAveragedPolynomialProfile(\n",
" model.param,\n",
" \"negative\",\n",
" options={**model.options, \"particle\": \"uniform profile\"},\n",
" )\n",
")"
]
},
Expand Down Expand Up @@ -431,19 +433,19 @@
"outputs": [],
"source": [
"options = {**model.options, \"particle\": \"uniform profile\"}\n",
"model.submodels[\n",
" \"negative primary particle\"\n",
"] = pybamm.particle.XAveragedPolynomialProfile(model.param, \"negative\", options)\n",
"model.submodels[\n",
" \"positive primary particle\"\n",
"] = pybamm.particle.XAveragedPolynomialProfile(model.param, \"positive\", options)\n",
"model.submodels[\"negative primary particle\"] = (\n",
" pybamm.particle.XAveragedPolynomialProfile(model.param, \"negative\", options)\n",
")\n",
"model.submodels[\"positive primary particle\"] = (\n",
" pybamm.particle.XAveragedPolynomialProfile(model.param, \"positive\", options)\n",
")\n",
"\n",
"model.submodels[\n",
" \"negative total particle concentration\"\n",
"] = pybamm.particle.TotalConcentration(model.param, \"negative\", options)\n",
"model.submodels[\n",
" \"positive total particle concentration\"\n",
"] = pybamm.particle.TotalConcentration(model.param, \"positive\", options)"
"model.submodels[\"negative total particle concentration\"] = (\n",
" pybamm.particle.TotalConcentration(model.param, \"negative\", options)\n",
")\n",
"model.submodels[\"positive total particle concentration\"] = (\n",
" pybamm.particle.TotalConcentration(model.param, \"positive\", options)\n",
")"
]
},
{
Expand All @@ -459,31 +461,31 @@
"metadata": {},
"outputs": [],
"source": [
"model.submodels[\n",
" \"negative open-circuit potential\"\n",
"] = pybamm.open_circuit_potential.SingleOpenCircuitPotential(\n",
" model.param, \"negative\", \"lithium-ion main\", options=model.options\n",
"model.submodels[\"negative open-circuit potential\"] = (\n",
" pybamm.open_circuit_potential.SingleOpenCircuitPotential(\n",
" model.param, \"negative\", \"lithium-ion main\", options=model.options\n",
" )\n",
")\n",
"model.submodels[\n",
" \"positive open-circuit potential\"\n",
"] = pybamm.open_circuit_potential.SingleOpenCircuitPotential(\n",
" model.param, \"positive\", \"lithium-ion main\", options=model.options\n",
"model.submodels[\"positive open-circuit potential\"] = (\n",
" pybamm.open_circuit_potential.SingleOpenCircuitPotential(\n",
" model.param, \"positive\", \"lithium-ion main\", options=model.options\n",
" )\n",
")\n",
"model.submodels[\"negative interface\"] = pybamm.kinetics.InverseButlerVolmer(\n",
" model.param, \"negative\", \"lithium-ion main\", options=model.options\n",
")\n",
"model.submodels[\"positive interface\"] = pybamm.kinetics.InverseButlerVolmer(\n",
" model.param, \"positive\", \"lithium-ion main\", options=model.options\n",
")\n",
"model.submodels[\n",
" \"negative interface current\"\n",
"] = pybamm.kinetics.CurrentForInverseButlerVolmer(\n",
" model.param, \"negative\", \"lithium-ion main\"\n",
"model.submodels[\"negative interface current\"] = (\n",
" pybamm.kinetics.CurrentForInverseButlerVolmer(\n",
" model.param, \"negative\", \"lithium-ion main\"\n",
" )\n",
")\n",
"model.submodels[\n",
" \"positive interface current\"\n",
"] = pybamm.kinetics.CurrentForInverseButlerVolmer(\n",
" model.param, \"positive\", \"lithium-ion main\"\n",
"model.submodels[\"positive interface current\"] = (\n",
" pybamm.kinetics.CurrentForInverseButlerVolmer(\n",
" model.param, \"positive\", \"lithium-ion main\"\n",
" )\n",
")\n",
"model.submodels[\"negative interface utilisation\"] = pybamm.interface_utilisation.Full(\n",
" model.param, \"negative\", model.options\n",
Expand Down Expand Up @@ -545,12 +547,12 @@
"metadata": {},
"outputs": [],
"source": [
"model.submodels[\n",
" \"electrolyte diffusion\"\n",
"] = pybamm.electrolyte_diffusion.ConstantConcentration(model.param)\n",
"model.submodels[\n",
" \"electrolyte conductivity\"\n",
"] = pybamm.electrolyte_conductivity.LeadingOrder(model.param)"
"model.submodels[\"electrolyte diffusion\"] = (\n",
" pybamm.electrolyte_diffusion.ConstantConcentration(model.param)\n",
")\n",
"model.submodels[\"electrolyte conductivity\"] = (\n",
" pybamm.electrolyte_conductivity.LeadingOrder(model.param)\n",
")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,9 +920,7 @@
"int_v_over_r2 = pybamm.Integral(v / r_var**2, r_var)\n",
"int_v_over_r2_disc = disc.process_symbol(int_v_over_r2)\n",
"print(\n",
" \"int(v/r^2) = {} is approximately equal to 4 * pi * sin(1), {}\".format(\n",
" int_v_over_r2_disc.evaluate(y=y), 4 * np.pi * np.sin(1)\n",
" )\n",
" f\"int(v/r^2) = {int_v_over_r2_disc.evaluate(y=y)} is approximately equal to 4 * pi * sin(1), {4 * np.pi * np.sin(1)}\"\n",
")"
]
},
Expand Down
70 changes: 35 additions & 35 deletions examples/scripts/custom_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,61 +17,61 @@
model.submodels["current collector"] = pybamm.current_collector.Uniform(model.param)
model.submodels["thermal"] = pybamm.thermal.isothermal.Isothermal(model.param)
model.submodels["porosity"] = pybamm.porosity.Constant(model.param, model.options)
model.submodels[
"electrolyte diffusion"
] = pybamm.electrolyte_diffusion.ConstantConcentration(model.param)
model.submodels[
"electrolyte conductivity"
] = pybamm.electrolyte_conductivity.LeadingOrder(model.param)
model.submodels["electrolyte diffusion"] = (
pybamm.electrolyte_diffusion.ConstantConcentration(model.param)
)
model.submodels["electrolyte conductivity"] = (
pybamm.electrolyte_conductivity.LeadingOrder(model.param)
)

# Loop over negative and positive electrode domains for some submodels
for domain in ["negative", "positive"]:
model.submodels[f"{domain} active material"] = pybamm.active_material.Constant(
model.param, domain, model.options
)
model.submodels[
f"{domain} electrode potential"
] = pybamm.electrode.ohm.LeadingOrder(model.param, domain)
model.submodels[f"{domain} electrode potential"] = (
pybamm.electrode.ohm.LeadingOrder(model.param, domain)
)
model.submodels[f"{domain} particle"] = pybamm.particle.XAveragedPolynomialProfile(
model.param,
domain,
options={**model.options, "particle": "uniform profile"},
phase="primary",
)
model.submodels[
f"{domain} total particle concentration"
] = pybamm.particle.TotalConcentration(
model.param, domain, model.options, phase="primary"
model.submodels[f"{domain} total particle concentration"] = (
pybamm.particle.TotalConcentration(
model.param, domain, model.options, phase="primary"
)
)

model.submodels[
f"{domain} open-circuit potential"
] = pybamm.open_circuit_potential.SingleOpenCircuitPotential(
model.param,
domain,
"lithium-ion main",
options=model.options,
phase="primary",
model.submodels[f"{domain} open-circuit potential"] = (
pybamm.open_circuit_potential.SingleOpenCircuitPotential(
model.param,
domain,
"lithium-ion main",
options=model.options,
phase="primary",
)
)
model.submodels[f"{domain} interface"] = pybamm.kinetics.InverseButlerVolmer(
model.param, domain, "lithium-ion main", options=model.options
)
model.submodels[
f"{domain} interface utilisation"
] = pybamm.interface_utilisation.Full(model.param, domain, model.options)
model.submodels[
f"{domain} interface current"
] = pybamm.kinetics.CurrentForInverseButlerVolmer(
model.param, domain, "lithium-ion main"
model.submodels[f"{domain} interface utilisation"] = (
pybamm.interface_utilisation.Full(model.param, domain, model.options)
)
model.submodels[
f"{domain} surface potential difference [V]"
] = pybamm.electrolyte_conductivity.surface_potential_form.Explicit(
model.param, domain, model.options
model.submodels[f"{domain} interface current"] = (
pybamm.kinetics.CurrentForInverseButlerVolmer(
model.param, domain, "lithium-ion main"
)
)
model.submodels[f"{domain} surface potential difference [V]"] = (
pybamm.electrolyte_conductivity.surface_potential_form.Explicit(
model.param, domain, model.options
)
)
model.submodels[f"{domain} particle mechanics"] = (
pybamm.particle_mechanics.NoMechanics(model.param, domain, model.options)
)
model.submodels[
f"{domain} particle mechanics"
] = pybamm.particle_mechanics.NoMechanics(model.param, domain, model.options)
model.submodels[f"{domain} sei"] = pybamm.sei.NoSEI(
model.param, domain, model.options
)
Expand Down
1 change: 0 additions & 1 deletion pybamm/citations.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class Citations:

"""Entry point to citations management.
This object may be used to record BibTeX citation information and then register that
a particular citation is relevant for a particular simulation.
Expand Down
24 changes: 7 additions & 17 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,7 @@ def process_boundary_conditions(self, model):
if bcs["left"][0].value != 0 or bcs["left"][1] != "Neumann":
raise pybamm.ModelError(
"Boundary condition at r = 0 must be a homogeneous "
"Neumann condition for {} coordinates".format(
self.mesh[subdomain].coord_sys
)
f"Neumann condition for {self.mesh[subdomain].coord_sys} coordinates"
)

# Handle any boundary conditions applied on the tabs
Expand Down Expand Up @@ -890,11 +888,11 @@ def _process_symbol(self, symbol):
y_slices = self.y_slices[symbol]
except KeyError:
raise pybamm.ModelError(
"""
No key set for variable '{}'. Make sure it is included in either
f"""
No key set for variable '{symbol.name}'. Make sure it is included in either
model.rhs or model.algebraic in an unmodified form
(e.g. not Broadcasted)
""".format(symbol.name)
"""
)
# Add symbol's reference and multiply by the symbol's scale
# so that the state vector is of order 1
Expand Down Expand Up @@ -1033,20 +1031,14 @@ def check_initial_conditions(self, model):
raise pybamm.ModelError(
"rhs and initial conditions must have the same shape after "
"discretisation but rhs.shape = "
"{} and initial_conditions.shape = {} for variable '{}'.".format(
model.rhs[var].shape, model.initial_conditions[var].shape, var
)
f"{model.rhs[var].shape} and initial_conditions.shape = {model.initial_conditions[var].shape} for variable '{var}'."
)
for var in model.algebraic.keys():
if model.algebraic[var].shape != model.initial_conditions[var].shape:
raise pybamm.ModelError(
"algebraic and initial conditions must have the same shape after "
"discretisation but algebraic.shape = "
"{} and initial_conditions.shape = {} for variable '{}'.".format(
model.algebraic[var].shape,
model.initial_conditions[var].shape,
var,
)
f"{model.algebraic[var].shape} and initial_conditions.shape = {model.initial_conditions[var].shape} for variable '{var}'."
)

def check_variables(self, model):
Expand Down Expand Up @@ -1080,9 +1072,7 @@ def check_variables(self, model):
raise pybamm.ModelError(
"variable and its eqn must have the same shape after "
"discretisation but variable.shape = "
"{} and rhs.shape = {} for variable '{}'. ".format(
var.shape, model.rhs[rhs_var].shape, var
)
f"{var.shape} and rhs.shape = {model.rhs[rhs_var].shape} for variable '{var}'. "
)

def is_variable_independent(self, var, all_vars_in_eqns):
Expand Down
4 changes: 1 addition & 3 deletions pybamm/expression_tree/binary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def _preprocess_binary(
# Check both left and right are pybamm Symbols
if not (isinstance(left, pybamm.Symbol) and isinstance(right, pybamm.Symbol)):
raise NotImplementedError(
"""BinaryOperator not implemented for symbols of type {} and {}""".format(
type(left), type(right)
)
f"""BinaryOperator not implemented for symbols of type {type(left)} and {type(right)}"""
)

# Do some broadcasting in special cases, to avoid having to do this manually
Expand Down
4 changes: 1 addition & 3 deletions pybamm/expression_tree/input_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@ def _base_evaluate(
return input_eval
else:
raise ValueError(
"Input parameter '{}' was given an object of size '{}'".format(
self.name, input_size
)
f"Input parameter '{self.name}' was given an object of size '{input_size}'"
+ f" but was expecting an object of size '{self._expected_size}'."
)

Expand Down
6 changes: 3 additions & 3 deletions pybamm/expression_tree/operations/convert_to_casadi.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def _convert(self, symbol, t, y, y_dot, inputs):

else:
raise TypeError(
"""
Cannot convert symbol of type '{}' to CasADi. Symbols must all be
f"""
Cannot convert symbol of type '{type(symbol)}' to CasADi. Symbols must all be
'linear algebra' at this stage.
""".format(type(symbol))
"""
)
8 changes: 2 additions & 6 deletions pybamm/expression_tree/operations/evaluate_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ def find_symbols(

else:
raise NotImplementedError(
"Conversion to python not implemented for a symbol of type '{}'".format(
type(symbol)
)
f"Conversion to python not implemented for a symbol of type '{type(symbol)}'"
)

variable_symbols[symbol.id] = symbol_str
Expand Down Expand Up @@ -406,9 +404,7 @@ def to_python(

if debug: # pragma: no cover
variable_lines = [
"print('{}'); ".format(
line_format.format(id_to_python_variable(symbol_id, False), symbol_line)
)
f"print('{line_format.format(id_to_python_variable(symbol_id, False), symbol_line)}'); "
+ line_format.format(id_to_python_variable(symbol_id, False), symbol_line)
+ "; print(type({0}),np.shape({0}))".format(
id_to_python_variable(symbol_id, False)
Expand Down
8 changes: 1 addition & 7 deletions pybamm/expression_tree/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,7 @@ def __str__(self):

def __repr__(self):
"""returns the string `__class__(id, name, children, domain)`"""
return ("{!s}({}, {!s}, children={!s}, domains={!s})").format(
self.__class__.__name__,
hex(self.id),
self._name,
[str(child) for child in self.children],
{k: v for k, v in self.domains.items() if v != []},
)
return f"{self.__class__.__name__!s}({hex(self.id)}, {self._name!s}, children={[str(child) for child in self.children]!s}, domains={({k: v for k, v in self.domains.items() if v != []})!s})"

def __add__(self, other: ChildSymbol) -> pybamm.Addition:
"""return an :class:`Addition` object."""
Expand Down
4 changes: 1 addition & 3 deletions pybamm/expression_tree/unary_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,7 @@ class BackwardIndefiniteIntegral(BaseIndefiniteIntegral):
def __init__(self, child, integration_variable):
super().__init__(child, integration_variable)
# Overwrite the name
self.name = "{} integrated backward w.r.t {}".format(
child.name, self.integration_variable[0].name
)
self.name = f"{child.name} integrated backward w.r.t {self.integration_variable[0].name}"
if isinstance(integration_variable, pybamm.SpatialVariable):
self.name += f" on {self.integration_variable[0].domain}"

Expand Down
Loading
Loading