Skip to content

Commit

Permalink
Merge branch 'pybamm-team:develop' into ocvr_ecm
Browse files Browse the repository at this point in the history
  • Loading branch information
parkec3 authored Aug 9, 2024
2 parents f3faca4 + b1fc595 commit 80428c9
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 212 deletions.
2 changes: 1 addition & 1 deletion docs/source/api/parameters/parameter_sets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ package (``cell_parameters``) should consist of the following::
The actual parameter set is defined within ``cell_alpha.py``, as shown below.
For an example, see the `Marquis2019`_ parameter sets.

.. _Marquis2019: https://github.com/pybamm-team/PyBaMM/blob/develop/pybamm/input/parameters/lithium_ion/Marquis2019.py
.. _Marquis2019: https://github.com/pybamm-team/PyBaMM/blob/develop/src/pybamm/input/parameters/lithium_ion/Marquis2019.py

.. code-block:: python
:linenos:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@
"source": [
"Note how, when we pass a function as a parameter, we pass the object without calling it, i.e. we pass `cube` rather than `cube(t)`. This new `parameter_values` variable could now be passed to a simulation, but note that it is incomplete as it does not include all the parameters that the model needs to run (see the parameters needed by calling `model.print_parameter_info()`, as done above). \n",
"\n",
"It is often convenient to define the parameter set in a separate file, and then call the parameters into your notebook or script. You can find some examples on how to do so in [PyBaMM's parameter library](https://github.com/pybamm-team/PyBaMM/tree/develop/pybamm/input/parameters/lithium_ion). You can copy one of the parameter sets available into a new file and modify it accordingly for the new parameter set. Then, whenever the set is needed, one can import the `get_parameter_values` method from the corresponding file and call it to obtain a copy of the parameter values."
"It is often convenient to define the parameter set in a separate file, and then call the parameters into your notebook or script. You can find some examples on how to do so in [PyBaMM's parameter library](https://github.com/pybamm-team/PyBaMM/tree/develop/src/pybamm/input/parameters/lithium_ion). You can copy one of the parameter sets available into a new file and modify it accordingly for the new parameter set. Then, whenever the set is needed, one can import the `get_parameter_values` method from the corresponding file and call it to obtain a copy of the parameter values."
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class Expression {
/**
* @brief Returns row indices in COO format (where the output data represents sparse matrix elements)
*/
virtual std::vector<expr_int> get_row() = 0;
virtual const std::vector<expr_int>& get_row() = 0;

/**
* @brief Returns column indices in COO format (where the output data represents sparse matrix elements)
*/
virtual std::vector<expr_int> get_col() = 0;
virtual const std::vector<expr_int>& get_col() = 0;

public: // data members
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ CasadiFunction::CasadiFunction(const BaseFunctionType &f) : Expression(), m_func
m_res.resize(sz_res, nullptr);
m_iw.resize(sz_iw, 0);
m_w.resize(sz_w, 0);

if (m_func.n_out() > 0) {
casadi::Sparsity casadi_sparsity = m_func.sparsity_out(0);
m_rows = casadi_sparsity.get_row();
m_cols = casadi_sparsity.get_col();
}
}

// only call this once m_arg and m_res have been set appropriately
Expand All @@ -45,24 +51,14 @@ expr_int CasadiFunction::nnz_out() {
return static_cast<expr_int>(m_func.nnz_out());
}

std::vector<expr_int> CasadiFunction::get_row() {
return get_row(0);
}

std::vector<expr_int> CasadiFunction::get_row(expr_int ind) {
const std::vector<expr_int>& CasadiFunction::get_row() {
DEBUG("CasadiFunction get_row(): " << m_func.name());
casadi::Sparsity casadi_sparsity = m_func.sparsity_out(ind);
return casadi_sparsity.get_row();
}

std::vector<expr_int> CasadiFunction::get_col() {
return get_col(0);
return m_rows;
}

std::vector<expr_int> CasadiFunction::get_col(expr_int ind) {
const std::vector<expr_int>& CasadiFunction::get_col() {
DEBUG("CasadiFunction get_col(): " << m_func.name());
casadi::Sparsity casadi_sparsity = m_func.sparsity_out(ind);
return casadi_sparsity.get_col();
return m_cols;
}

void CasadiFunction::operator()(const std::vector<realtype*>& inputs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ class CasadiFunction : public Expression
expr_int out_shape(int k) override;
expr_int nnz() override;
expr_int nnz_out() override;
std::vector<expr_int> get_row() override;
std::vector<expr_int> get_row(expr_int ind);
std::vector<expr_int> get_col() override;
std::vector<expr_int> get_col(expr_int ind);
const std::vector<expr_int>& get_row() override;
const std::vector<expr_int>& get_col() override;

public:
/*
Expand All @@ -43,6 +41,8 @@ class CasadiFunction : public Expression
private:
std::vector<expr_int> m_iw; // cppcheck-suppress unusedStructMember
std::vector<double> m_w; // cppcheck-suppress unusedStructMember
std::vector<expr_int> m_rows; // cppcheck-suppress unusedStructMember
std::vector<expr_int> m_cols; // cppcheck-suppress unusedStructMember
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class IREEFunction : public Expression
expr_int out_shape(int k) override;
expr_int nnz() override;
expr_int nnz_out() override;
std::vector<expr_int> get_col() override;
std::vector<expr_int> get_row() override;
const std::vector<expr_int>& get_col() override;
const std::vector<expr_int>& get_row() override;

/*
* @brief Evaluate the MLIR function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ expr_int IREEFunction::nnz_out() {
return m_func.nnz;
}

std::vector<expr_int> IREEFunction::get_row() {
const std::vector<expr_int>& IREEFunction::get_row() {
DEBUG("IreeFunction get_row" << m_func.row.size());
return m_func.row;
}

std::vector<expr_int> IREEFunction::get_col() {
const std::vector<expr_int>& IREEFunction::get_col() {
DEBUG("IreeFunction get_col" << m_func.col.size());
return m_func.col;
}
Expand Down
8 changes: 4 additions & 4 deletions src/pybamm/solvers/c_solvers/idaklu/IDAKLUSolverOpenMP.inl
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ void IDAKLUSolverOpenMP<ExprSet>::CalcVarsSensitivities(
DEBUG("IDAKLUSolver::CalcVarsSensitivities");
// Calculate sensitivities
std::vector<realtype> dens_dvar_dp = std::vector<realtype>(number_of_parameters, 0);
for (size_t dvar_k=0; dvar_k<functions->dvar_dy_fcns.size(); dvar_k++) {
for (size_t dvar_k = 0; dvar_k < functions->dvar_dy_fcns.size(); dvar_k++) {
// Isolate functions
Expression* dvar_dy = functions->dvar_dy_fcns[dvar_k];
Expression* dvar_dp = functions->dvar_dp_fcns[dvar_k];
Expand All @@ -306,15 +306,15 @@ void IDAKLUSolverOpenMP<ExprSet>::CalcVarsSensitivities(
// Calculate dvar/dp and convert to dense array for indexing
(*dvar_dp)({tret, yval, functions->inputs.data()}, {&res_dvar_dp[0]});
for (int k=0; k<number_of_parameters; k++) {
dens_dvar_dp[k]=0;
dens_dvar_dp[k] = 0;
}
for (int k=0; k<dvar_dp->nnz_out(); k++) {
dens_dvar_dp[dvar_dp->get_row()[k]] = res_dvar_dp[k];
}
// Calculate sensitivities
for (int paramk=0; paramk<number_of_parameters; paramk++) {
for (int paramk = 0; paramk < number_of_parameters; paramk++) {
yS_return[*ySk] = dens_dvar_dp[paramk];
for (int spk=0; spk<dvar_dy->nnz_out(); spk++) {
for (int spk = 0; spk < dvar_dy->nnz_out(); spk++) {
yS_return[*ySk] += res_dvar_dy[spk] * ySval[paramk][dvar_dy->get_col()[spk]];
}
(*ySk)++;
Expand Down
16 changes: 2 additions & 14 deletions tests/integration/test_experiments.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#
# Test some experiments
#

import pybamm
import numpy as np
import unittest


class TestExperiments(unittest.TestCase):
class TestExperiments:
def test_discharge_rest_charge(self):
experiment = pybamm.Experiment(
[
Expand Down Expand Up @@ -78,7 +76,7 @@ def test_infeasible(self):
)
sol = sim.solve()
# this experiment fails during the third cycle (i.e. is infeasible)
self.assertEqual(len(sol.cycles), 3)
assert len(sol.cycles) == 3

def test_drive_cycle(self):
drive_cycle = np.array([np.arange(100), 5 * np.ones(100)]).T
Expand All @@ -100,13 +98,3 @@ def test_drive_cycle(self):
)
sol = sim.solve()
assert np.all(sol["Terminal voltage [V]"].entries >= 4.00)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
pybamm.settings.debug_mode = True
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ def test_with_experiment(self):

class TestBasicSPM(BaseBasicModelTest):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.BasicSPM()


class TestBasicDFN(BaseBasicModelTest):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.BasicDFN()


class TestBasicDFNComposite(BaseBasicModelTest):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.BasicDFNComposite()


class TestBasicDFNHalfCell(BaseBasicModelTest):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
options = {"working electrode": "positive"}
self.model = pybamm.lithium_ion.BasicDFNHalfCell(options)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#
import pybamm
import numpy as np
import unittest


class TestCompareOutputsTwoPhase(unittest.TestCase):
class TestCompareOutputsTwoPhase:
def compare_outputs_two_phase_graphite_graphite(self, model_class):
"""
Check that a two-phase graphite-graphite model gives the same results as a
Expand Down Expand Up @@ -158,7 +157,7 @@ def compare_outputs_two_phase_silicon_graphite(self, model_class):
)

# More silicon means longer sim
self.assertLess(sol[0]["Time [s]"].data[-1], sol[1]["Time [s]"].data[-1])
assert sol[0]["Time [s]"].data[-1] < sol[1]["Time [s]"].data[-1]

def test_compare_SPM_silicon_graphite(self):
model_class = pybamm.lithium_ion.SPM
Expand All @@ -171,12 +170,3 @@ def test_compare_SPMe_silicon_graphite(self):
def test_compare_DFN_silicon_graphite(self):
model_class = pybamm.lithium_ion.DFN
self.compare_outputs_two_phase_silicon_graphite(model_class)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#
# Tests for the lithium-ion DFN model
#

import pybamm
import tests
import numpy as np
import unittest
from tests import BaseIntegrationTestLithiumIon
import pytest


class TestDFN(BaseIntegrationTestLithiumIon, unittest.TestCase):
def setUp(self):
class TestDFN(BaseIntegrationTestLithiumIon):
@pytest.fixture(autouse=True)
def setup(self):
self.model = pybamm.lithium_ion.DFN

def test_particle_distribution_in_x(self):
Expand All @@ -35,8 +35,9 @@ def positive_radius(x):
self.run_basic_processing_test({}, parameter_values=param)


class TestDFNWithSizeDistribution(unittest.TestCase):
def setUp(self):
class TestDFNWithSizeDistribution:
@pytest.fixture(autouse=True)
def setup(self):
params = pybamm.ParameterValues("Marquis2019")
self.params = pybamm.get_size_distribution_parameters(params)

Expand Down Expand Up @@ -120,12 +121,3 @@ def test_conservation_each_electrode(self):
# compare
np.testing.assert_array_almost_equal(neg_Li[0], neg_Li[1], decimal=12)
np.testing.assert_array_almost_equal(pos_Li[0], pos_Li[1], decimal=12)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

class TestDFNHalfCell(BaseIntegrationTestLithiumIonHalfCell):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.DFN
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#
# Tests for the lithium-ion MPM model
#

import pybamm
import tests
import numpy as np
import unittest


class TestMPM(unittest.TestCase):
class TestMPM:
def test_basic_processing(self):
options = {"thermal": "isothermal"}
model = pybamm.lithium_ion.MPM(options)
Expand Down Expand Up @@ -124,12 +122,3 @@ def test_conservation_each_electrode(self):
# compare
np.testing.assert_array_almost_equal(neg_Li[0], neg_Li[1], decimal=13)
np.testing.assert_array_almost_equal(pos_Li[0], pos_Li[1], decimal=13)


if __name__ == "__main__":
print("Add -v for more debug output")
import sys

if "-v" in sys.argv:
debug = True
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestNewmanTobias(BaseIntegrationTestLithiumIon):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.NewmanTobias

def test_basic_processing(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

class TestSPM(BaseIntegrationTestLithiumIon):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.SPM
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

class TestSPMHalfCell(BaseIntegrationTestLithiumIonHalfCell):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.SPM
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class TestSPMe(BaseIntegrationTestLithiumIon):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.SPMe

def test_integrated_conductivity(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

class TestSPMeHalfCell(BaseIntegrationTestLithiumIonHalfCell):
@pytest.fixture(autouse=True)
def setUp(self):
def setup(self):
self.model = pybamm.lithium_ion.SPMe
Loading

0 comments on commit 80428c9

Please sign in to comment.