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

Add basic tests #12

Merged
merged 7 commits into from
Jun 25, 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
32 changes: 32 additions & 0 deletions .github/workflows/build_docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Build documentation

on:
pull_request:
branches:
- "main"


concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
html:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -e ".[dev]"
- name: Build documentation
run: |
cd docs
make html SPHINXOPTS="-W --keep-going"
17 changes: 17 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Static analysis

on:
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
- uses: pre-commit/[email protected]
52 changes: 52 additions & 0 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Unit tests

on:
pull_request:
branches:
- "main"
paths:
- '**.py'


concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
pytest-linux:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -e ".[dev]"
- name: Test with pytest
run: |
pytest

pytest-windows:
runs-on: windows-latest
strategy:
matrix:
python-version: ["3.11"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
pip install -e ".[dev]"
- name: Test with pytest
run: |
pytest
16 changes: 16 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "2"

build:
os: "ubuntu-22.04"
tools:
python: "3.11"

python:
install:
- method: pip
path: .
extra_requirements:
- dev

sphinx:
configuration: docs/conf.py
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
8 changes: 2 additions & 6 deletions docs/source/api/numerical_methods.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
Numerical Methods
=================

.. autofunction:: conjugate_gradient
.. autofunction:: pbeis.bicgstab

.. autofunction:: bicgstab

.. autofunction:: prebicgstab

.. autofunction:: matrix_rescale
.. autofunction:: pbeis.prebicgstab
2 changes: 1 addition & 1 deletion examples/compare_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def current_function(t):
print("Time domain method: ", time_elapsed, "s")

# Frequency domain
methods = ["direct"]
methods = ["direct", "bicgstab", "prebicgstab"]
impedances_freqs = []
for method in methods:
start_time = timer.time()
Expand Down
4 changes: 3 additions & 1 deletion pbeis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pbeis.version import __version__

from .eis_simulation import EISSimulation
from .numerical_methods import bicgstab, conjugate_gradient, prebicgstab
from .numerical_methods import bicgstab, prebicgstab
from .plotting import nyquist_plot
from .utils import SymbolReplacer
29 changes: 13 additions & 16 deletions pbeis/eis_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def set_up_model_for_eis(self, model):
model : :class:`pybamm.BaseModel`
Model to set up for EIS.
"""
pybamm.logger.info("Start setting up {} for EIS".format(self.model_name))
pybamm.logger.info(f"Start setting up {self.model_name} for EIS")

# Make a new copy of the model
new_model = model.new_copy()
Expand Down Expand Up @@ -132,7 +132,7 @@ def set_up_model_for_eis(self, model):
new_model.algebraic[I_var] = I_ - I_applied
new_model.initial_conditions[I_var] = 0

pybamm.logger.info("Finish setting up {} for EIS".format(self.model_name))
pybamm.logger.info(f"Finish setting up {self.model_name} for EIS")

return new_model

Expand Down Expand Up @@ -179,9 +179,9 @@ def solve(self, frequencies, method="direct", inputs=None):
Compute the impedance at the given frequencies by solving problem

.. math::
i \omega \tau M x = J x + b
i \\omega \tau M x = J x + b

where i is the imagianary unit, \omega is the frequency, \tau is the model
where i is the imagianary unit, \\omega is the frequency, \tau is the model
timescale, M is the mass matrix, J is the Jacobian, x is the state vector,
and b gives a periodic forcing in the current.

Expand All @@ -191,7 +191,7 @@ def solve(self, frequencies, method="direct", inputs=None):
The frequencies at which to compute the impedance.
method : str, optional
The method used to calculate the impedance. Can be 'direct', 'prebicgstab',
'bicgstab' or 'cg'. Default is 'direct'.
or 'bicgstab'. Default is 'direct'.
inputs : dict, optional
Any input parameters to pass to the model when solving

Expand All @@ -216,11 +216,11 @@ def solve(self, frequencies, method="direct", inputs=None):
# entry and the current density variable is the final entry
z = -x[-2][0] / x[-1][0]
zs.append(z)
elif method in ["prebicgstab", "bicgstab", "cg"]:
elif method in ["prebicgstab", "bicgstab"]:
zs = self.iterative_method(frequencies, method=method)
else:
raise NotImplementedError(
"'method' must be 'direct', 'prebicgstab', 'bicgstab' or 'cg', ",
raise ValueError(
"'method' must be 'direct', 'prebicgstab' or 'bicgstab', ",
f"but is '{method}'",
)

Expand All @@ -238,9 +238,9 @@ def iterative_method(self, frequencies, method="prebicgstab"):
Compute the impedance at the given frequencies by solving problem

.. math::
i \omega \tau M x = J x + b
i \\omega \tau M x = J x + b

using an iterative method, where i is the imagianary unit, \omega
using an iterative method, where i is the imagianary unit, \\omega
is the frequency, \tau is the model timescale, M is the mass matrix,
J is the Jacobian, x is the state vector, and b gives a periodic
forcing in the current.
Expand All @@ -251,7 +251,6 @@ def iterative_method(self, frequencies, method="prebicgstab"):
The frequencies at which to compute the impedance.
method : str, optional
The method used to calculate the impedance. Can be:
'cg' - conjugate gradient - only use for Hermitian matrices
'bicgstab' - use bicgstab with no preconditioner
'prebicgstab' - use bicgstab with a preconditioner, this is
the default.
Expand Down Expand Up @@ -303,10 +302,8 @@ def callback(xk):
)
solve_time = time.process_time() - solve_start_time

elif method == "cg":
sol = pbeis.conjugate_gradient(
A, self.b, start_point=sol, callback=callback
)
else:
raise ValueError

# Store number of iterations at this frequency
iters_per_frequency.append(num_iters)
Expand All @@ -316,7 +313,7 @@ def callback(xk):
z = -sol[-2][0] / sol[-1][0]
zs.append(z)

return self.zs
return zs

def nyquist_plot(self, **kwargs):
"""
Expand Down
Loading
Loading