Skip to content

Commit

Permalink
Merge pull request #3656 from Saransh-cpp/ruff-format
Browse files Browse the repository at this point in the history
Migrate to ruff format
  • Loading branch information
Saransh-cpp authored Dec 24, 2023
2 parents 691057f + d8eedf1 commit 8dc0421
Show file tree
Hide file tree
Showing 145 changed files with 3,624 additions and 3,030 deletions.
4 changes: 3 additions & 1 deletion .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
a63e49ece0f9336d1f5c2562f7459e555c6e6693
# activated standard pre-commits - https://github.com/pybamm-team/PyBaMM/pull/3192
5273214b585c5a4286609aed40e0b092d0e05f42
# migrate config to pyproject.toml - https://github.com/pybamm-team/PyBaMM/pull/3557
# migrated config to pyproject.toml - https://github.com/pybamm-team/PyBaMM/pull/3557
12c5d77203bd93542785d237bac00bad5ed5469a
# activated pyupgrade - https://github.com/pybamm-team/PyBaMM/pull/3579
ff6d81c01331c7d269303b4a8321d9881bdf98fa
# migrated to ruff-format - https://github.com/pybamm-team/PyBaMM/pull/3655
60ebd4148059a95428a496f4f55c1175ead362d3
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ repos:
- id: ruff
args: [--fix, --show-fixes]
types_or: [python, pyi, jupyter]
- id: ruff-format
types_or: [python, pyi, jupyter]

- repo: https://github.com/adamchainz/blacken-docs
rev: "1.16.0"
hooks:
- id: blacken-docs
additional_dependencies: [black==22.12.0]
additional_dependencies: [black==23.*]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand Down
54 changes: 34 additions & 20 deletions docs/source/examples/notebooks/batch_study.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@
"parameter_values = {\"Chen2020\": pybamm.ParameterValues(\"Chen2020\")}\n",
"\n",
"# creating a BatchStudy object and solving the simulation\n",
"batch_study = pybamm.BatchStudy(models=models, parameter_values=parameter_values, permutations=True)\n",
"batch_study = pybamm.BatchStudy(\n",
" models=models, parameter_values=parameter_values, permutations=True\n",
")\n",
"batch_study.solve(t_eval=[0, 3600])\n",
"batch_study.plot()"
]
Expand Down Expand Up @@ -195,13 +197,17 @@
"# different values for \"Current function [A]\"\n",
"current_values = [4.5, 4.75, 5]\n",
"\n",
"# changing the value of \"Current function [A]\" in all the parameter values present in the \n",
"# changing the value of \"Current function [A]\" in all the parameter values present in the\n",
"# parameter_values dictionary\n",
"for k, v, current_value in zip(parameter_values.keys(), parameter_values.values(), current_values):\n",
" v[\"Current function [A]\"] = current_value \n",
"for k, v, current_value in zip(\n",
" parameter_values.keys(), parameter_values.values(), current_values\n",
"):\n",
" v[\"Current function [A]\"] = current_value\n",
"\n",
"# creating a BatchStudy object with permutations set to True to create a cartesian product\n",
"batch_study = pybamm.BatchStudy(models=model, parameter_values=parameter_values, permutations=True)\n",
"batch_study = pybamm.BatchStudy(\n",
" models=model, parameter_values=parameter_values, permutations=True\n",
")\n",
"batch_study.solve(t_eval=[0, 3600])\n",
"\n",
"# generating the required labels and plotting\n",
Expand Down Expand Up @@ -474,19 +480,19 @@
"# using the cccv experiment with 10 cycles\n",
"cccv = pybamm.Experiment(\n",
" [\n",
" (\"Discharge at C/10 for 10 hours or until 3.3 V\",\n",
" \"Rest for 1 hour\",\n",
" \"Charge at 1 A until 4.1 V\",\n",
" \"Hold at 4.1 V until 50 mA\",\n",
" \"Rest for 1 hour\")\n",
" (\n",
" \"Discharge at C/10 for 10 hours or until 3.3 V\",\n",
" \"Rest for 1 hour\",\n",
" \"Charge at 1 A until 4.1 V\",\n",
" \"Hold at 4.1 V until 50 mA\",\n",
" \"Rest for 1 hour\",\n",
" )\n",
" ]\n",
" * 10,\n",
")\n",
"\n",
"# creating the experiment dict\n",
"experiment = {\n",
" \"cccv\": cccv\n",
"}\n",
"experiment = {\"cccv\": cccv}\n",
"\n",
"# populating a dictionary with 3 same parameter values (Mohtat2020 chemistry)\n",
"parameter_values = {\n",
Expand All @@ -499,23 +505,31 @@
"inner_sei_oc_v_values = [2.0e-4, 2.7e-4, 3.4e-4]\n",
"\n",
"# updating the value of \"Inner SEI open-circuit potential [V]\" in all the dictionary items\n",
"for k, v, inner_sei_oc_v in zip(parameter_values.keys(), parameter_values.values(), inner_sei_oc_v_values):\n",
"for k, v, inner_sei_oc_v in zip(\n",
" parameter_values.keys(), parameter_values.values(), inner_sei_oc_v_values\n",
"):\n",
" v.update(\n",
" {\n",
" \"Inner SEI open-circuit potential [V]\": inner_sei_oc_v\n",
" },\n",
" {\"Inner SEI open-circuit potential [V]\": inner_sei_oc_v},\n",
" )\n",
"\n",
"# creating a Single Particle Model with \"electron-mitigation limited\" SEI\n",
"model = {\"spm\": pybamm.lithium_ion.SPM({\"SEI\": \"electron-migration limited\"})}\n",
"\n",
"# creating a BatchStudy object with the given experimen, model and parameter_values\n",
"batch_study = pybamm.BatchStudy(models=model, experiments=experiment, parameter_values=parameter_values, permutations=True)\n",
"batch_study = pybamm.BatchStudy(\n",
" models=model,\n",
" experiments=experiment,\n",
" parameter_values=parameter_values,\n",
" permutations=True,\n",
")\n",
"\n",
"#solving and plotting the result\n",
"# solving and plotting the result\n",
"batch_study.solve(initial_soc=1)\n",
"\n",
"labels = [f\"Inner SEI open-circuit potential [V]: {inner_sei_oc_v}\" for inner_sei_oc_v in inner_sei_oc_v_values]\n",
"labels = [\n",
" f\"Inner SEI open-circuit potential [V]: {inner_sei_oc_v}\"\n",
" for inner_sei_oc_v in inner_sei_oc_v_values\n",
"]\n",
"batch_study.plot(labels=labels)"
]
},
Expand Down
23 changes: 13 additions & 10 deletions docs/source/examples/notebooks/change-settings.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"import numpy as np\n",
"import os\n",
"import matplotlib.pyplot as plt\n",
"os.chdir(pybamm.__path__[0]+'/..')\n",
"\n",
"os.chdir(pybamm.__path__[0] + \"/..\")\n",
"\n",
"# create the model\n",
"model = pybamm.lithium_ion.SPM()\n",
Expand Down Expand Up @@ -378,9 +379,9 @@
}
],
"source": [
"format_str = '{:<75} {:>20}'\n",
"print(format_str.format('PARAMETER', 'VALUE'))\n",
"print(\"-\"*97)\n",
"format_str = \"{:<75} {:>20}\"\n",
"print(format_str.format(\"PARAMETER\", \"VALUE\"))\n",
"print(\"-\" * 97)\n",
"for key, value in model.default_parameter_values.items():\n",
" try:\n",
" print(format_str.format(key, value))\n",
Expand Down Expand Up @@ -417,8 +418,8 @@
"old_value = param[variable]\n",
"param[variable] = 1.4\n",
"new_value = param[variable]\n",
"print(variable,'was',old_value)\n",
"print(variable,'now is',param[variable])"
"print(variable, \"was\", old_value)\n",
"print(variable, \"now is\", param[variable])"
]
},
{
Expand Down Expand Up @@ -514,8 +515,8 @@
}
],
"source": [
"print(format_str.format('DOMAIN', 'DISCRETISED BY'))\n",
"print(\"-\"*82)\n",
"print(format_str.format(\"DOMAIN\", \"DISCRETISED BY\"))\n",
"print(\"-\" * 82)\n",
"for key, value in model.default_spatial_methods.items():\n",
" print(format_str.format(key, value.__class__.__name__))"
]
Expand Down Expand Up @@ -553,7 +554,9 @@
"outputs": [],
"source": [
"submesh_types = model.default_submesh_types\n",
"submesh_types[\"negative particle\"] = pybamm.MeshGenerator(pybamm.SpectralVolume1DSubMesh)"
"submesh_types[\"negative particle\"] = pybamm.MeshGenerator(\n",
" pybamm.SpectralVolume1DSubMesh\n",
")"
]
},
{
Expand Down Expand Up @@ -621,7 +624,7 @@
}
],
"source": [
"print('Default solver for SPM model:',type(model.default_solver).__name__)"
"print(\"Default solver for SPM model:\", type(model.default_solver).__name__)"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"metadata": {},
"outputs": [],
"source": [
"model.rhs = {x: dxdt, y: dydt} "
"model.rhs = {x: dxdt, y: dydt}"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@
"r = pybamm.SpatialVariable(\n",
" \"r\", domain=[\"negative particle\"], coord_sys=\"spherical polar\"\n",
")\n",
"geometry = {\"negative particle\": {r: {\"min\": pybamm.Scalar(0), \"max\": pybamm.Scalar(1)}}}"
"geometry = {\n",
" \"negative particle\": {r: {\"min\": pybamm.Scalar(0), \"max\": pybamm.Scalar(1)}}\n",
"}"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@
"# governing equations\n",
"N = -D * pybamm.grad(c) # flux\n",
"dcdt = -pybamm.div(N)\n",
"model.rhs = {c: dcdt} \n",
"model.rhs = {c: dcdt}\n",
"\n",
"# boundary conditions \n",
"# boundary conditions\n",
"lbc = pybamm.Scalar(0)\n",
"rbc = -j / F / D\n",
"model.boundary_conditions = {c: {\"left\": (lbc, \"Neumann\"), \"right\": (rbc, \"Neumann\")}}\n",
"\n",
"# initial conditions \n",
"# initial conditions\n",
"model.initial_conditions = {c: c0}"
]
},
Expand Down Expand Up @@ -193,7 +193,9 @@
"metadata": {},
"outputs": [],
"source": [
"r = pybamm.SpatialVariable(\"r\", domain=[\"negative particle\"], coord_sys=\"spherical polar\")\n",
"r = pybamm.SpatialVariable(\n",
" \"r\", domain=[\"negative particle\"], coord_sys=\"spherical polar\"\n",
")\n",
"geometry = {\"negative particle\": {r: {\"min\": pybamm.Scalar(0), \"max\": R}}}"
]
},
Expand Down Expand Up @@ -305,7 +307,7 @@
"ax1.set_xlabel(\"Time [s]\")\n",
"ax1.set_ylabel(\"Surface concentration [mol.m-3]\")\n",
"\n",
"r = mesh[\"negative particle\"].nodes # radial position\n",
"r = mesh[\"negative particle\"].nodes # radial position\n",
"time = 1000 # time in seconds\n",
"ax2.plot(r * 1e6, c(t=time, r=r), label=f\"t={time}[s]\")\n",
"ax2.set_xlabel(\"Particle radius [microns]\")\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@
"# governing equations for full model\n",
"N = -D * pybamm.grad(c) # flux\n",
"dcdt = -pybamm.div(N)\n",
"full_model.rhs = {c: dcdt} \n",
"full_model.rhs = {c: dcdt}\n",
"\n",
"# governing equations for reduced model\n",
"dc_avdt = -3 * j / R / F\n",
"reduced_model.rhs = {c_av: dc_avdt} \n",
"reduced_model.rhs = {c_av: dc_avdt}\n",
"\n",
"# initial conditions (these are the same for both models)\n",
"full_model.initial_conditions = {c: c0}\n",
Expand All @@ -157,7 +157,9 @@
"# boundary conditions (only required for full model)\n",
"lbc = pybamm.Scalar(0)\n",
"rbc = -j / F / D\n",
"full_model.boundary_conditions = {c: {\"left\": (lbc, \"Neumann\"), \"right\": (rbc, \"Neumann\")}}"
"full_model.boundary_conditions = {\n",
" c: {\"left\": (lbc, \"Neumann\"), \"right\": (rbc, \"Neumann\")}\n",
"}"
]
},
{
Expand Down Expand Up @@ -186,7 +188,7 @@
"# reduced model\n",
"reduced_model.variables = {\n",
" \"Concentration [mol.m-3]\": pybamm.PrimaryBroadcast(c_av, \"negative particle\"),\n",
" \"Surface concentration [mol.m-3]\": c_av, # in this model the surface concentration is just equal to the scalar average concentration \n",
" \"Surface concentration [mol.m-3]\": c_av, # in this model the surface concentration is just equal to the scalar average concentration\n",
" \"Average concentration [mol.m-3]\": c_av,\n",
"}"
]
Expand Down Expand Up @@ -239,7 +241,9 @@
"outputs": [],
"source": [
"# geometry\n",
"r = pybamm.SpatialVariable(\"r\", domain=[\"negative particle\"], coord_sys=\"spherical polar\")\n",
"r = pybamm.SpatialVariable(\n",
" \"r\", domain=[\"negative particle\"], coord_sys=\"spherical polar\"\n",
")\n",
"geometry = {\"negative particle\": {r: {\"min\": pybamm.Scalar(0), \"max\": R}}}\n",
"param.process_geometry(geometry)\n",
"\n",
Expand Down Expand Up @@ -273,7 +277,7 @@
"\n",
"# process models\n",
"for model in models:\n",
" disc.process_model(model);"
" disc.process_model(model)"
]
},
{
Expand Down Expand Up @@ -346,38 +350,38 @@
"c_av_reduced = solutions[1][\"Average concentration [mol.m-3]\"]\n",
"\n",
"# plot\n",
"r = mesh[\"negative particle\"].nodes # radial position\n",
"r = mesh[\"negative particle\"].nodes # radial position\n",
"\n",
"\n",
"def plot(t):\n",
" fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(13, 4))\n",
" \n",
"\n",
" # Plot concetration as a function of r\n",
" ax1.plot(r * 1e6, c_full(t=t,r=r), label=\"Full Model\")\n",
" ax1.plot(r * 1e6, c_reduced(t=t,r=r), label=\"Reduced Model\") \n",
" ax1.plot(r * 1e6, c_full(t=t, r=r), label=\"Full Model\")\n",
" ax1.plot(r * 1e6, c_reduced(t=t, r=r), label=\"Reduced Model\")\n",
" ax1.set_xlabel(\"Particle radius [microns]\")\n",
" ax1.set_ylabel(\"Concentration [mol.m-3]\")\n",
" ax1.legend()\n",
" \n",
"\n",
" # Plot average concentration over time\n",
" t_hour = np.linspace(0, 3600, 600) # plot over full hour\n",
" c_min = c_av_reduced(t=3600) * 0.98 # minimum axes limit \n",
" c_max = param[\"Initial concentration [mol.m-3]\"] * 1.02 # maximum axes limit \n",
" \n",
" c_min = c_av_reduced(t=3600) * 0.98 # minimum axes limit\n",
" c_max = param[\"Initial concentration [mol.m-3]\"] * 1.02 # maximum axes limit\n",
"\n",
" ax2.plot(t_hour, c_av_full(t=t_hour), label=\"Full Model\")\n",
" ax2.plot(t_hour, c_av_reduced(t=t_hour), label=\"Reduced Model\") \n",
" ax2.plot(t_hour, c_av_reduced(t=t_hour), label=\"Reduced Model\")\n",
" ax2.plot([t, t], [c_min, c_max], \"k--\") # plot line to track time\n",
" ax2.set_xlabel(\"Time [s]\")\n",
" ax2.set_ylabel(\"Average concentration [mol.m-3]\") \n",
" ax2.set_ylabel(\"Average concentration [mol.m-3]\")\n",
" ax2.legend()\n",
"\n",
" plt.tight_layout()\n",
" plt.show()\n",
" \n",
"\n",
"\n",
"import ipywidgets as widgets\n",
"widgets.interact(plot, t=widgets.FloatSlider(min=0,max=3600,step=1,value=0));\n",
" "
"\n",
"widgets.interact(plot, t=widgets.FloatSlider(min=0, max=3600, step=1, value=0));"
]
},
{
Expand Down
Loading

0 comments on commit 8dc0421

Please sign in to comment.