Skip to content

Commit

Permalink
Add show_plot option for *Solution.plot() that can register plt.show(…
Browse files Browse the repository at this point in the history
…) for non-interactive environments
  • Loading branch information
c-randall committed Dec 26, 2024
1 parent e9245f7 commit 64a022f
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 29 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
### Optimizations

### Bug Fixes
- `Solution.plot` now blocks at the end of a program ([#5](https://github.com/NREL/thevenin/pull/5)). Figures no longer automatically close when programs are run from scripts. Blocking only occurs at the end of a program so that opening figures do not stop other lines of code from running.
- `*Solution.plot()` now has a `show_plot` option to register `plt.show()` and block at the end of a program ([#5](https://github.com/NREL/thevenin/pull/5)). This keeps figures from auto-closing at the end of scripts run in non-interactive environments. Interactive environments (IPython, Jupyter Notebook) are not affected. When set to `False`, users running in non-interactive environments must manually call `plt.show()`. The default is `True`.

### Breaking Changes
- New Coulombic efficiency option means users will need to update old `params` inputs to also include `ce`
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 23 additions & 17 deletions docs/jupyter_execute/examples/dict_inputs.ipynb

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion docs/jupyter_execute/examples/yaml_inputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"num_RC_pairs: 1\n",
"soc0: 1.\n",
"capacity: 75.\n",
"ce: 1.\n",
"mass: 1.9\n",
"isothermal: False\n",
"Cp: 745.\n",
Expand All @@ -47,7 +48,7 @@
"R1: !eval |\n",
" lambda soc, T_cell: 1e-5 + soc/1e5 - T_cell/3e9\n",
"C1: !eval |\n",
" lambda soc, T_cell: 1e4 + soc*1e4 + np.exp(T_cell/300.)\n",
" lambda soc, T_cell: 1e2 + soc*1e4 + np.exp(T_cell/300.)\n",
"```\n",
"\n",
"Although this example only uses a single RC pair, `num_RC_pairs` can be as low as 0 and can be as high as $N$. The number of defined `Rj` and `Cj` elements in the '.yaml' file should be consistent with `num_RC_pairs`. For example, if `num_RC_pairs=0` then only `R0` should be defined, with no other resistors or capacitors. However, if `num_RC_pairs=3` then the user should specify `R0`, `R1`, `R2`, `R3`, `C1`, `C2`, and `C3`. Note that the series resistor element `R0` is always included, even when there are no RC pairs. "
Expand Down
2 changes: 1 addition & 1 deletion docs/jupyter_execute/user_guide/basic_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"output_type": "stream",
"text": [
"CycleSolution(\n",
" solvetime=0.011 s,\n",
" solvetime=0.006 s,\n",
" success=[True, True],\n",
" status=[2, 1],\n",
" nfev=[257, 69],\n",
Expand Down
12 changes: 12 additions & 0 deletions docs/source/_templates/copyright.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{# Displays the copyright information (which is defined in conf.py). #}
{% if show_copyright and copyright %}
<p class="copyright">
{% if hasdoc('copyright') %}
© <a href="{{ pathto('copyright') }}">{% trans copyright=copyright|e %}Copyright {{ copyright }}{% endtrans %}</a>.
<br/>
{% else %}
{% trans copyright=copyright|e %}© {{ copyright }}.{% endtrans %}
<br/>
{% endif %}
</p>
{% endif %}
18 changes: 16 additions & 2 deletions docs/source/api/thevenin/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,21 @@ Package Contents



.. py:method:: plot(x, y, **kwargs)
.. py:method:: plot(x, y, show_plot = True, **kwargs)
Plot any two variables in 'vars' against each other.

:param x: A variable key in 'vars' to be used for the x-axis.
:type x: str
:param y: A variable key in 'vars' to be used for the y-axis.
:type y: str
:param show_plot: For non-interactive environments only. When True (default) this
registers `plt.show()` to run at the end of the program. If False,
you must call `plt.show()` manually.
:type show_plot: bool, optional
:param \*\*kwargs: Keyword arguments to pass through to `plt.plot()`. For more info
please refer to documentation for `maplotlib.pyplot.plot()`.
:type \*\*kwargs: dict, optional

:returns: *None.*

Expand Down Expand Up @@ -796,14 +803,21 @@ Package Contents
:type timer: float


.. py:method:: plot(x, y, **kwargs)
.. py:method:: plot(x, y, show_plot = True, **kwargs)
Plot any two variables in 'vars' against each other.

:param x: A variable key in 'vars' to be used for the x-axis.
:type x: str
:param y: A variable key in 'vars' to be used for the y-axis.
:type y: str
:param show_plot: For non-interactive environments only. When True (default) this
registers `plt.show()` to run at the end of the program. If False,
you must call `plt.show()` manually.
:type show_plot: bool, optional
:param \*\*kwargs: Keyword arguments to pass through to `plt.plot()`. For more info
please refer to documentation for `maplotlib.pyplot.plot()`.
:type \*\*kwargs: dict, optional

:returns: *None.*

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import thevenin as thev

project = 'thevenin'
copyright = '2024, Corey R. Randall'
copyright = 'Alliance for Sustainable Energy, LLC'
author = 'Corey R. Randall'
version = thev.__version__
release = thev.__version__
Expand Down
16 changes: 12 additions & 4 deletions docs/source/examples/dict_inputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -32,7 +32,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -134,6 +134,7 @@
" 'num_RC_pairs': 1,\n",
" 'soc0': 1.,\n",
" 'capacity': 75.,\n",
" 'ce': 1.,\n",
" 'mass': 1.9,\n",
" 'isothermal': False,\n",
" 'Cp': 745.,\n",
Expand Down Expand Up @@ -161,7 +162,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -193,6 +194,13 @@
"soln = model.run(expr)\n",
"soln.plot('time_h', 'voltage_V')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -211,7 +219,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
3 changes: 2 additions & 1 deletion docs/source/examples/yaml_inputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"num_RC_pairs: 1\n",
"soc0: 1.\n",
"capacity: 75.\n",
"ce: 1.\n",
"mass: 1.9\n",
"isothermal: False\n",
"Cp: 745.\n",
Expand All @@ -47,7 +48,7 @@
"R1: !eval |\n",
" lambda soc, T_cell: 1e-5 + soc/1e5 - T_cell/3e9\n",
"C1: !eval |\n",
" lambda soc, T_cell: 1e4 + soc*1e4 + np.exp(T_cell/300.)\n",
" lambda soc, T_cell: 1e2 + soc*1e4 + np.exp(T_cell/300.)\n",
"```\n",
"\n",
"Although this example only uses a single RC pair, `num_RC_pairs` can be as low as 0 and can be as high as $N$. The number of defined `Rj` and `Cj` elements in the '.yaml' file should be consistent with `num_RC_pairs`. For example, if `num_RC_pairs=0` then only `R0` should be defined, with no other resistors or capacitors. However, if `num_RC_pairs=3` then the user should specify `R0`, `R1`, `R2`, `R3`, `C1`, `C2`, and `C3`. Note that the series resistor element `R0` is always included, even when there are no RC pairs. "
Expand Down
2 changes: 1 addition & 1 deletion src/thevenin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from . import loadfns
from . import plotutils

__version__ = '0.1.2.dev'
__version__ = '0.2.0.dev'

__all__ = [
'IDASolver',
Expand Down
3 changes: 3 additions & 0 deletions src/thevenin/_solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def plot(self, x: str, y: str, show_plot: bool = True, **kwargs) -> None:
For non-interactive environments only. When True (default) this
registers `plt.show()` to run at the end of the program. If False,
you must call `plt.show()` manually.
**kwargs : dict, optional
Keyword arguments to pass through to `plt.plot()`. For more info
please refer to documentation for `maplotlib.pyplot.plot()`.
Returns
-------
Expand Down

0 comments on commit 64a022f

Please sign in to comment.