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

Enhance plot_voltage_components.py for Simulation and Solution classes #3723

Conversation

AlessioBugetti
Copy link
Contributor

Description

This commit extends the functionality of the plot_voltage_components method, enhancing its integration within the Simulation and Solution classes.

Key Changes:

  • Modified the plot_voltage_components function in plot_voltage_components.py to handle both Simulation and Solution objects.

  • Simulation Class: Added a new method plot_voltage_components. This method calls the standalone plot_voltage_components function and utilizes the solution attribute of the Simulation instance to generate the plot.

  • Solution Class: Introduced a similar plot_voltage_components method to the Solution class, enabling direct plotting of voltage components from Solution instances.

  • Modified test_plot_voltage_components.py

Fixes #3722

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All tests pass: $ python run-tests.py --all (or $ nox -s tests)
  • The documentation builds: $ python run-tests.py --doctest (or $ nox -s doctests)

You can run integration tests, unit tests, and doctests together at once, using $ python run-tests.py --quick (or $ nox -s quick).

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

This commit extends the functionality of the `plot_voltage_components` method in PyBaMM, enhancing its integration within the `Simulation` and `Solution` classes.

Key Changes:

- Modified the standalone `plot_voltage_components` function in `plot_voltage_components.py` to handle both `Simulation` and `Solution` objects.

- `Simulation` Class: Added a new method `plot_voltage_components`. This method calls the standalone `plot_voltage_components` function and utilizes the solution attribute of the `Simulation` instance to generate the plot.

- `Solution` Class: Introduced a similar `plot_voltage_components` method to the `Solution` class, enabling direct plotting of voltage components from `Solution` instances.

- Modified `test_plot_voltage_components.py`
Copy link
Member

@Saransh-cpp Saransh-cpp left a comment

Choose a reason for hiding this comment

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

Thanks for working on this, @AlessioBugetti!

It would be nice to comb through the example notebooks once and see if the new sim.plot_voltage_components and sol.plot_voltage_components can be added somewhere.

Comment on lines 44 to 45
else:
raise ValueError("Input must be a Solution or Simulation object")
Copy link
Member

Choose a reason for hiding this comment

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

IMO specifying the type of the variable in the docs (docstring) should be enough.

Suggested change
else:
raise ValueError("Input must be a Solution or Simulation object")

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@brosaplanella
Copy link
Sponsor Member

It would be nice to comb through the example notebooks once and see if the new sim.plot_voltage_components and sol.plot_voltage_components can be added somewhere.

I am reviewing the Getting Started notebooks so will include these in Tutorial 3 once they are merged.

Copy link
Sponsor Member

@brosaplanella brosaplanella left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! Just add a line to the CHANGELOG before we merge.

Copy link

codecov bot commented Jan 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (5900ada) 99.59% compared to head (d8d8414) 99.59%.
Report is 2 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #3723   +/-   ##
========================================
  Coverage    99.59%   99.59%           
========================================
  Files          258      258           
  Lines        20811    20823   +12     
========================================
+ Hits         20726    20738   +12     
  Misses          85       85           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Saransh-cpp
Copy link
Member

@all-contributors please add @AlessioBugetti for code, docs, tests

Copy link
Contributor

@Saransh-cpp

I've put up a pull request to add @AlessioBugetti! 🎉

CHANGELOG.md Outdated
Comment on lines 5 to 6
- Fixed a bug where if the first step(s) in a cycle are skipped then the cycle solution started from the model's initial conditions instead of from the last state of the previous cycle ([#3708](https://github.com/pybamm-team/PyBaMM/pull/3708))
- Updated `plot_voltage_components.py` to support both `Simulation` and `Solution` objects. Added new methods in both `Simulation` and `Solution` classes for allow the syntax `simulation.plot_voltage_components` and `solution.plot_voltage_components`. Updated `test_plot_voltage_components.py` to reflect these changes ([#3723](https://github.com/pybamm-team/PyBaMM/pull/3723)).
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Suggested change
- Fixed a bug where if the first step(s) in a cycle are skipped then the cycle solution started from the model's initial conditions instead of from the last state of the previous cycle ([#3708](https://github.com/pybamm-team/PyBaMM/pull/3708))
- Updated `plot_voltage_components.py` to support both `Simulation` and `Solution` objects. Added new methods in both `Simulation` and `Solution` classes for allow the syntax `simulation.plot_voltage_components` and `solution.plot_voltage_components`. Updated `test_plot_voltage_components.py` to reflect these changes ([#3723](https://github.com/pybamm-team/PyBaMM/pull/3723)).
- Updated `plot_voltage_components.py` to support both `Simulation` and `Solution` objects. Added new methods in both `Simulation` and `Solution` classes for allow the syntax `simulation.plot_voltage_components` and `solution.plot_voltage_components`. Updated `test_plot_voltage_components.py` to reflect these changes ([#3723](https://github.com/pybamm-team/PyBaMM/pull/3723)).
- Fixed a bug where if the first step(s) in a cycle are skipped then the cycle solution started from the model's initial conditions instead of from the last state of the previous cycle ([#3708](https://github.com/pybamm-team/PyBaMM/pull/3708))

Small comment: for the CHANGELOG we sort contributions by decreasing PR number (i.e. newer PRs first).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I just corrected this oversight

Commit here

Copy link
Contributor

Choose a reason for hiding this comment

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

@AlessioBugetti Just as an FYI, you can accept suggestions too so you don't have to do a separate commit. You just need to press the "commit suggestion" button

@Saransh-cpp
Copy link
Member

The CHANGELOG in develop was updated for v24.1rc1, so you might have to fix that (sorry for the conflicts).

@AlessioBugetti
Copy link
Contributor Author

The CHANGELOG in develop was updated for v24.1rc1, so you might have to fix that (sorry for the conflicts).

I've updated the CHANGELOG, conflicts resolved

@Saransh-cpp Saransh-cpp merged commit 73b470f into pybamm-team:develop Jan 18, 2024
39 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API improvements to plot_voltage_components
4 participants