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

"Other" changes from the julia branch #1497

Merged
merged 134 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 132 commits
Commits
Show all changes
134 commits
Select commit Hold shift + click to select a range
bdca623
get simple multiplication working
valentinsulzer Aug 19, 2020
d2ad43a
#1129 get more simple scalars and functions working
valentinsulzer Aug 19, 2020
aa0b0b4
#1129 start matrix operations
valentinsulzer Aug 20, 2020
f613bc2
#1129 add sparse arrays
valentinsulzer Aug 20, 2020
0e3d72e
#1129 test div and grad
valentinsulzer Aug 20, 2020
7c7cd6a
#1129 flatten column vectors
valentinsulzer Aug 20, 2020
5efb13b
Merge branch 'develop' into issue-1129-julia
valentinsulzer Aug 27, 2020
7b8d093
#1129 tests
valentinsulzer Aug 28, 2020
7ad9c4b
Merge branch 'develop' into issue-1129-julia
valentinsulzer Sep 1, 2020
770b206
Merge branch 'develop' into issue-1129-julia
valentinsulzer Sep 22, 2020
84333f9
Merge branch 'develop' into issue-1129-julia
valentinsulzer Sep 30, 2020
21b9f99
test notebook
valentinsulzer Oct 2, 2020
bb62953
#1129 generating MTK model
valentinsulzer Oct 15, 2020
9d63be0
Merge branch 'develop' into issue-1129-julia
valentinsulzer Oct 15, 2020
5494ea3
#1129 cleaning up a bit
valentinsulzer Oct 15, 2020
cbf7acb
#1129 adding tests
valentinsulzer Oct 15, 2020
88c217f
#1129 add a test using parameters
valentinsulzer Oct 16, 2020
831fe2e
#1129 add support for DAE model
valentinsulzer Oct 16, 2020
e40104f
#1129 flake8
valentinsulzer Oct 16, 2020
ffe05f4
Merge branch 'develop' into issue-1129-julia
valentinsulzer Oct 21, 2020
4496993
#1129 move constants outside of function
valentinsulzer Oct 23, 2020
77d03d4
Merge branch 'develop' into issue-1129-julia
valentinsulzer Oct 28, 2020
3dff3bb
#1129 starting to optimize generated function
valentinsulzer Oct 28, 2020
b71ee46
#1129 testing some inplace stuff
valentinsulzer Oct 28, 2020
60db480
#1129 investigating pyjulia issue
valentinsulzer Oct 28, 2020
c7d1753
#1129 use mul! and cache variables
valentinsulzer Oct 29, 2020
9086148
#1129 inlining operations and starting vcat alternative
valentinsulzer Oct 29, 2020
da47078
#1129 fix concatenations, start adding tests
valentinsulzer Oct 30, 2020
af06122
#1129 working through more cases
valentinsulzer Oct 30, 2020
1ec94f5
Merge branch 'issue-1219-smooth-approximations' into issue-1129-julia
valentinsulzer Nov 3, 2020
08a9415
#1219 fix basic tests
valentinsulzer Nov 3, 2020
6929b32
#1129 all tests pass but not simplifying enough
valentinsulzer Nov 3, 2020
5fa4f8d
#1129 all evaluate_julia tests passing
valentinsulzer Nov 3, 2020
f595ae2
#1129 working on battery models
valentinsulzer Nov 4, 2020
a7cc4d7
Merge branch 'develop' into issue-1129-julia
valentinsulzer Nov 4, 2020
f415d55
#1129 debugging julia models
valentinsulzer Nov 4, 2020
592d4c8
#1129 merge #1230
valentinsulzer Nov 11, 2020
bec5ce3
#1129 running more tests, DFN is wrong
valentinsulzer Nov 11, 2020
f3e4936
#1129 running more julia tests
valentinsulzer Nov 11, 2020
f619f64
#1129 merge develop
valentinsulzer Nov 17, 2020
fef5d0c
#1129 debugging DFN
valentinsulzer Nov 18, 2020
0e16baf
Merge branch 'issue-1230-simplify' into issue-1129-julia
valentinsulzer Nov 18, 2020
dbeab17
#1129 fix typos
valentinsulzer Nov 18, 2020
497a04b
Merge branch 'issue-1230-simplify' into issue-1129-julia
valentinsulzer Nov 18, 2020
e78e988
#1129 working on MTK PDEs
valentinsulzer Nov 20, 2020
d30b0bc
Merge branch 'develop' into issue-1129-julia
valentinsulzer Nov 30, 2020
f379a38
#1129 get simple heat equation working
valentinsulzer Dec 2, 2020
1709b9f
#1129 merge master
valentinsulzer Dec 8, 2020
28f68ad
#1129 get all mtk tests working
valentinsulzer Dec 9, 2020
64dfc8e
#1129 add spherical coordinates and input parameters
valentinsulzer Dec 9, 2020
90b4fb3
#1129 fix flake8 and start setting up workflow
valentinsulzer Dec 9, 2020
5b53a11
#1129 update workflow
valentinsulzer Dec 9, 2020
a6976c7
#1129 merge develop
valentinsulzer Dec 10, 2020
3b1c067
#1129 try julia install
valentinsulzer Dec 10, 2020
7d74c2a
#1129 fix evaluator_python
valentinsulzer Dec 10, 2020
1d477e6
Merge branch 'develop' into issue-1129-julia
valentinsulzer Dec 10, 2020
60bde7d
#1129 remove temp files and fix overwritten commits
valentinsulzer Dec 10, 2020
57a02f5
#1129 try new tox.ini
valentinsulzer Dec 10, 2020
2b73ab7
#1129 another attempt for tox.ini
valentinsulzer Dec 10, 2020
06a004a
#1129 don't install scikits.odes with julia
valentinsulzer Dec 10, 2020
350eaea
Add specific tox factor for scikits.odes
tlestang Dec 11, 2020
7fab240
Command for julia factor is ran when running tests
tlestang Dec 11, 2020
296517f
#1129 remove Sundials from project.toml
valentinsulzer Dec 12, 2020
1945786
#1129 minimal Project.toml
valentinsulzer Dec 12, 2020
57fce5b
Merge branch 'develop' into issue-1129-julia
valentinsulzer Dec 23, 2020
3a3945e
#1129 merge develop
valentinsulzer Feb 5, 2021
c7e3740
#1129 merge develop
valentinsulzer Feb 26, 2021
b831c11
Merge branch 'develop' into issue-1129-julia
valentinsulzer Feb 26, 2021
2c686ce
#1129 julia tests run (but don't all pass) locally
valentinsulzer Feb 26, 2021
e9224b5
#1129 only run julia tests
valentinsulzer Feb 27, 2021
534d8cd
#1129 install pyjulia in github actions
valentinsulzer Feb 27, 2021
e8ddfd1
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 1, 2021
86ecb35
#1129 try separate testenv for julia
valentinsulzer Mar 1, 2021
6139293
#1129 add diffeqpy as a dependency
valentinsulzer Mar 1, 2021
de0470e
#1129 don't use diffeqpy
valentinsulzer Mar 1, 2021
1d698ef
#1129 install right package
valentinsulzer Mar 1, 2021
f893417
#1129 skip test on windows
valentinsulzer Mar 1, 2021
b5e5e1a
#1129 flake8
valentinsulzer Mar 1, 2021
0434ab5
#1129 add example
valentinsulzer Mar 2, 2021
8baee33
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 2, 2021
291d419
#1129 install julia on mac
valentinsulzer Mar 2, 2021
54ac518
#1129 flake8
valentinsulzer Mar 2, 2021
29131c1
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 4, 2021
dc0df3e
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 11, 2021
7087b04
#1129 allow input parameters in rhs equations
valentinsulzer Mar 12, 2021
401d4c1
testing mtk
valentinsulzer Mar 12, 2021
d482392
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 12, 2021
217b96a
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 13, 2021
de974c8
#1129 allow input parameters in initial conditions
valentinsulzer Mar 13, 2021
107aae9
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 17, 2021
cb0e4b6
#1129 ignore Broadcast and fix 0D ics
valentinsulzer Mar 17, 2021
b7a27d3
#1129 running some tests
valentinsulzer Mar 18, 2021
242eae2
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 18, 2021
0b0d795
#1129 SPM -> MTK now working
valentinsulzer Mar 18, 2021
ca9cac3
#1129 SPMe -> MTK works but needs fine tuning
valentinsulzer Mar 18, 2021
155fcd9
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 18, 2021
af1f7c7
Merge branch 'issue-1437-concatenation-variable' into issue-1129-julia
valentinsulzer Mar 18, 2021
6da520b
#1129 allow dae model
valentinsulzer Mar 21, 2021
296710b
#1129 merge develop
valentinsulzer Mar 23, 2021
911f41f
#1129 generate SPMe in MTK
valentinsulzer Mar 24, 2021
05688e0
#1129 working on DFN to MTK
valentinsulzer Mar 25, 2021
432851f
#1229 working on dfn to mtk
valentinsulzer Mar 26, 2021
4a41527
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 26, 2021
3b54890
#1129 working on DFN to MTK
valentinsulzer Mar 26, 2021
22abc25
Merge branch 'develop' into issue-1129-julia
valentinsulzer Mar 26, 2021
24516ff
#1129 more dfn mtk
valentinsulzer Mar 26, 2021
f9e6bfd
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 26, 2021
df3da7c
#1129 reformat for daes
valentinsulzer Mar 27, 2021
ae96e84
#1129 debugging dfn
valentinsulzer Mar 27, 2021
bc6f289
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 27, 2021
2df89f6
Merge branch 'issue-1429-simplifications' into issue-1129-julia
valentinsulzer Mar 27, 2021
ac31d01
#1129 fix DFN model generation, now runs (but slow)
valentinsulzer Mar 27, 2021
18e8265
Merge branch 'develop' into issue-1129-julia
valentinsulzer Apr 1, 2021
6e5af06
Merge branch 'develop' of github.com:pybamm-team/PyBaMM into issue-11…
valentinsulzer Apr 1, 2021
1764770
Merge branch 'develop' into issue-1129-julia
valentinsulzer Apr 16, 2021
a896a17
#1129 merge develop, run flake8
valentinsulzer Apr 16, 2021
c703250
#1129 testing
valentinsulzer Apr 19, 2021
5f1339f
#1129 merge develop
valentinsulzer Apr 28, 2021
a6e8080
#1129 minor fixes
valentinsulzer Apr 29, 2021
e2e0edd
#1129 merge develop
valentinsulzer May 5, 2021
b57299f
Merge branch 'develop' into issue-1129-julia
valentinsulzer May 25, 2021
8071cce
Merge branch 'develop' into issue-1129-julia
valentinsulzer May 26, 2021
2d99135
#1129 fix windows test
valentinsulzer May 26, 2021
6220292
Merge branch 'develop' into issue-1129-julia
valentinsulzer May 28, 2021
a8beb0e
remove Julia for now
valentinsulzer May 28, 2021
5916134
use tox.ini and setup.py from develop branch
valentinsulzer May 28, 2021
ec6aaa6
revert SPMe example changes
valentinsulzer May 28, 2021
fc1fda9
coverage
valentinsulzer May 31, 2021
d06e77b
flake8
valentinsulzer Jun 1, 2021
02e261d
try removing print name for benchmarks
valentinsulzer Jun 1, 2021
7136cc7
try faster inspect
valentinsulzer Jun 1, 2021
6454df3
don't recalculate print name if not needed
valentinsulzer Jun 1, 2021
5956cb1
Merge branch 'develop' into other-changes-julia-branch
valentinsulzer Jun 3, 2021
2e4f5e4
review comments
valentinsulzer Jun 3, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

- name: Install Linux system dependencies
if: matrix.os == 'ubuntu-latest'
run: |
Expand Down
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,5 @@ test.c
# tox
.tox/

# julia
Manifest.toml

# vcpkg
vcpkg_installed/
vcpkg_installed/
8 changes: 4 additions & 4 deletions pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,16 @@ def version(formatted=False):
from .expression_tree.exceptions import *

# Operations
from .expression_tree.operations.evaluate import (
from .expression_tree.operations.evaluate_python import (
find_symbols,
id_to_python_variable,
to_python,
EvaluatorPython,
)

if system() != "Windows":
from .expression_tree.operations.evaluate import EvaluatorJax
from .expression_tree.operations.evaluate import JaxCooMatrix
from .expression_tree.operations.evaluate_python import EvaluatorJax
from .expression_tree.operations.evaluate_python import JaxCooMatrix

from .expression_tree.operations.jacobian import Jacobian
from .expression_tree.operations.convert_to_casadi import CasadiConverter
Expand All @@ -115,7 +115,7 @@ def version(formatted=False):
# Model classes
#
from .models.base_model import BaseModel
from .models import standard_variables
from .models.standard_variables import standard_variables
from .models.event import Event
from .models.event import EventType

Expand Down
10 changes: 10 additions & 0 deletions pybamm/expression_tree/concatenations.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ def __init__(self, *children):
np.min([child.bounds[1] for child in children]),
)

if not any(c.print_name is None for c in children):
print_name = intersect(children[0].print_name, children[1].print_name)
for child in children[2:]:
print_name = intersect(print_name, child.print_name)
if print_name.endswith("_"):
print_name = print_name[:-1]
else:
print_name = None
self.print_name = print_name


def substrings(s):
for i in range(len(s)):
Expand Down
18 changes: 16 additions & 2 deletions pybamm/expression_tree/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,20 +358,34 @@ def log10(child):
return log(child, base=10)


class Max(SpecificFunction):
""" Max function """

def __init__(self, child):
super().__init__(np.max, child)


def max(child):
"""
Returns max function of child. Not to be confused with :meth:`pybamm.maximum`, which
returns the larger of two objects.
"""
return pybamm.simplify_if_constant(Function(np.max, child))
return pybamm.simplify_if_constant(Max(child))


class Min(SpecificFunction):
""" Min function """

def __init__(self, child):
super().__init__(np.min, child)


def min(child):
"""
Returns min function of child. Not to be confused with :meth:`pybamm.minimum`, which
returns the smaller of two objects.
"""
return pybamm.simplify_if_constant(Function(np.min, child))
return pybamm.simplify_if_constant(Min(child))


def sech(child):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,23 @@ def find_symbols(symbol, constant_symbols, variable_symbols, output_jax=False):

elif isinstance(symbol, pybamm.Concatenation):

# don't bother to concatenate if there is only a single child
# no need to concatenate if there is only a single child
if isinstance(symbol, pybamm.NumpyConcatenation):
if len(children_vars) > 1:
symbol_str = "np.concatenate(({}))".format(",".join(children_vars))
if len(children_vars) == 1:
symbol_str = children_vars[0]
else:
symbol_str = "{}".format(",".join(children_vars))
symbol_str = "np.concatenate(({}))".format(",".join(children_vars))

elif isinstance(symbol, pybamm.SparseStack):
if len(children_vars) > 1:
if len(children_vars) == 1:
symbol_str = children_vars[0]
else:
if output_jax:
raise NotImplementedError
else:
symbol_str = "scipy.sparse.vstack(({}))".format(
",".join(children_vars)
)
else:
symbol_str = "{}".format(",".join(children_vars))

# DomainConcatenation specifies a particular ordering for the concatenation,
# which we must follow
Expand Down Expand Up @@ -376,7 +376,9 @@ def find_symbols(symbol, constant_symbols, variable_symbols, output_jax=False):

else:
raise NotImplementedError(
"Not implemented for a symbol of type '{}'".format(type(symbol))
"Conversion to python not implemented for a symbol of type '{}'".format(
type(symbol)
)
)

variable_symbols[symbol.id] = symbol_str
Expand Down
38 changes: 34 additions & 4 deletions pybamm/expression_tree/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numbers
import numpy as np
import pybamm
import sys


class Parameter(pybamm.Symbol):
Expand Down Expand Up @@ -62,13 +63,17 @@ class FunctionParameter(pybamm.Symbol):
if diff_variable is specified, the FunctionParameter node will be replaced by a
:class:`pybamm.Function` and then differentiated with respect to diff_variable.
Default is None.
print_name : str, optional
The name to show when printing. Default is 'calculate', in which case the name
is calculated using sys._getframe().
"""

def __init__(
self,
name,
inputs,
diff_variable=None,
print_name="calculate",
):
# assign diff variable
self.diff_variable = diff_variable
Expand All @@ -90,6 +95,23 @@ def __init__(

self.input_names = list(inputs.keys())

# Use the inspect module to find the function's "short name" from the
# Parameters module that called it
if print_name != "calculate":
self.print_name = print_name
else:
frame = sys._getframe().f_back
print_name = frame.f_code.co_name
if print_name.startswith("_"):
self.print_name = None
else:
if print_name.endswith("_dimensional"):
self.print_name = print_name[: -len("_dimensional")]
elif print_name.endswith("_dim"):
self.print_name = print_name[: -len("_dim")]
else:
self.print_name = print_name

@property
def input_names(self):
return self._input_names
Expand Down Expand Up @@ -159,9 +181,14 @@ def diff(self, variable):

def new_copy(self):
""" See :meth:`pybamm.Symbol.new_copy()`. """
return self._function_parameter_new_copy(self._input_names, self.orphans)
out = self._function_parameter_new_copy(
self._input_names, self.orphans, print_name=self.print_name
)
return out

def _function_parameter_new_copy(self, input_names, children):
def _function_parameter_new_copy(
self, input_names, children, print_name="calculate"
):
"""Returns a new copy of the function parameter.

Inputs
Expand All @@ -173,14 +200,17 @@ def _function_parameter_new_copy(self, input_names, children):

Returns
-------
: :pybamm.FunctionParameter
:class:`pybamm.FunctionParameter`
A new copy of the function parameter
"""

input_dict = {input_names[i]: children[i] for i in range(len(input_names))}

return FunctionParameter(
self.name, input_dict, diff_variable=self.diff_variable
self.name,
input_dict,
diff_variable=self.diff_variable,
print_name=print_name,
)

def _evaluate_for_shape(self):
Expand Down
3 changes: 3 additions & 0 deletions pybamm/expression_tree/scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __init__(self, value, name=None, domain=[]):

super().__init__(name, domain=domain)

def __str__(self):
return str(self.value)

@property
def value(self):
"""the value returned by the node when evaluated"""
Expand Down
5 changes: 4 additions & 1 deletion pybamm/expression_tree/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ def __init__(self, name, domain=None, auxiliary_domains=None, bounds=None):
+ "Lower bound should be strictly less than upper bound."
)
self.bounds = bounds
self.print_name = None

def new_copy(self):
""" See :meth:`pybamm.Symbol.new_copy()`. """

return self.__class__(
out = self.__class__(
self.name, self.domain, self.auxiliary_domains, self.bounds
)
out.print_name = self.print_name
return out

def _evaluate_for_shape(self):
""" See :meth:`pybamm.Symbol.evaluate_for_shape_using_domain()` """
Expand Down
29 changes: 17 additions & 12 deletions pybamm/models/base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,22 @@ def info(self, symbol_name):

print(div)

def check_discretised_or_discretise_inplace_if_0D(self):
"""
Discretise model if it isn't already discretised
This only works with purely 0D models, as otherwise the mesh and spatial
method should be specified by the user
"""
if self.is_discretised is False:
try:
disc = pybamm.Discretisation()
disc.process_model(self)
except pybamm.DiscretisationError as e:
raise pybamm.DiscretisationError(
"Cannot automatically discretise model, model should be "
"discretised before exporting casadi functions ({})".format(e)
)

def export_casadi_objects(self, variable_names, input_parameter_order=None):
"""
Export the constituent parts of the model (rhs, algebraic, initial conditions,
Expand All @@ -793,18 +809,7 @@ def export_casadi_objects(self, variable_names, input_parameter_order=None):
Dictionary of {str: casadi object} pairs representing the model in casadi
format
"""
# Discretise model if it isn't already discretised
# This only works with purely 0D models, as otherwise the mesh and spatial
# method should be specified by the user
if self.is_discretised is False:
try:
disc = pybamm.Discretisation()
disc.process_model(self)
except pybamm.DiscretisationError as e:
raise pybamm.DiscretisationError(
"Cannot automatically discretise model, model should be "
"discretised before exporting casadi functions ({})".format(e)
)
self.check_discretised_or_discretise_inplace_if_0D()

# Create casadi functions for the model
t_casadi = casadi.MX.sym("t")
Expand Down
Loading