Skip to content

Commit

Permalink
#617 fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimms committed Sep 26, 2019
1 parent c86ec66 commit e0bbb96
Show file tree
Hide file tree
Showing 49 changed files with 730 additions and 523 deletions.
4 changes: 2 additions & 2 deletions docs/source/meshes/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Meshes
.. toctree::

meshes
one_dimensional_submeshes
scikits_fem_submeshes
zero_dimensional_submeshes
one_dimensional_submeshes
two_dimensional_submeshes
16 changes: 11 additions & 5 deletions docs/source/meshes/one_dimensional_submeshes.rst
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
1D Sub Meshes
=============

.. autoclass:: pybamm.SubMesh1D
.. autoclass:: pybamm.one_dimensional_meshes.SubMesh1D
:members:

.. autoclass:: pybamm.Uniform1DSubMesh
.. autoclass:: pybamm.one_dimensional_meshes.Uniform1DSubMesh
:members:

.. autoclass:: pybamm.Chebyshev1DSubMesh
.. autoclass:: pybamm.one_dimensional_meshes.Chebyshev1DSubMesh
:members:

.. autoclass:: pybamm.GetExponential1DSubMesh
.. autoclass:: pybamm.one_dimensional_meshes.GetExponential1DSubMesh
:members:

.. autoclass:: pybamm.Exponential1DSubMesh
.. autoclass:: pybamm.one_dimensional_meshes.Exponential1DSubMesh
:members:

.. autoclass:: pybamm.one_dimensional_meshes.GetUserSupplied1DSubMesh
:members:

.. autoclass:: pybamm.one_dimensional_meshes.UserSupplied1DSubMesh
:members:
14 changes: 0 additions & 14 deletions docs/source/meshes/scikits_fem_submeshes.rst

This file was deleted.

14 changes: 14 additions & 0 deletions docs/source/meshes/two_dimensional_submeshes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2D Sub Meshes
=============

.. autoclass:: pybamm.two_dimensional_meshes.ScikitSubMesh2D
:members:

.. autoclass:: pybamm.two_dimensional_meshes.ScikitUniform2DSubMesh
:members:

.. autoclass:: pybamm.two_dimensional_meshes.ScikitChebyshev2DSubMesh
:members:

.. autoclass:: pybamm.two_dimensional_meshes.ScikitTopExponential2DSubMesh
:members:
2 changes: 1 addition & 1 deletion docs/source/meshes/zero_dimensional_submeshes.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
0D Sub Mesh
===========

.. autoclass:: pybamm.SubMesh0D
.. autoclass:: pybamm.zero_dimensional_meshes.SubMesh0D
:members:
2 changes: 1 addition & 1 deletion docs/tutorials/add-model.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Events will stop the solver whenever they return 0.
Setting defaults
~~~~~~~~~~~~~~~~
It can be useful for testing, and quickly running a model to have a default setup. Each of the defaults listed above should adhere to the API requirements but in short, we require ``self.default_geometry`` to be an instance of :class:`pybamm.Geometry`, ``self.default_solver`` to be an instance of :class:`pybamm.BaseSolver`, and
``self.default_parameter_values`` to be an instance of :class:`pybamm.ParameterValues`. We also require that ``self.default_submesh_types`` is a dictionary with keys which are strings corresponding to the regions of the battery (e.g. "negative electrode") and values which are an instance of :class:`pybamm.SubMesh1D`. The ``self.default_spatial_methods`` attribute is also required to be a dictionary with keys corresponding to the regions of the battery but with values which are an instance of
``self.default_parameter_values`` to be an instance of :class:`pybamm.ParameterValues`. We also require that ``self.default_submesh_types`` is a dictionary with keys which are strings corresponding to the regions of the battery (e.g. "negative electrode") and values which are an instance of :class:`pybamm.one_dimensional_meshes.SubMesh1D`. The ``self.default_spatial_methods`` attribute is also required to be a dictionary with keys corresponding to the regions of the battery but with values which are an instance of
:class:`pybamm.SpatialMethod`. Finally, ``self.default_var_pts`` is required to be a dictionary with keys which are an instance of :class:`pybamm.SpatialVariable` and values which are integers.


Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/create-model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@
],
"source": [
"# mesh and discretise\n",
"submesh_types = {\"SEI layer\": pybamm.Uniform1DSubMesh}\n",
"submesh_types = {\"SEI layer\": pybamm.one_dimensional_meshes.Uniform1DSubMesh}\n",
"var_pts = {x: 100}\n",
"mesh = pybamm.Mesh(geometry, submesh_types, var_pts)\n",
" \n",
Expand Down
12 changes: 6 additions & 6 deletions examples/notebooks/spatial_methods/finite-volumes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@
"outputs": [],
"source": [
"submesh_types = {\n",
" \"negative electrode\": pybamm.Uniform1DSubMesh,\n",
" \"separator\": pybamm.Uniform1DSubMesh,\n",
" \"positive electrode\": pybamm.Uniform1DSubMesh,\n",
" \"negative particle\": pybamm.Uniform1DSubMesh,\n",
" \"positive particle\": pybamm.Uniform1DSubMesh,\n",
" \"current collector\": pybamm.SubMesh0D,\n",
" \"negative electrode\": pybamm.one_dimensional_meshes.Uniform1DSubMesh,\n",
" \"separator\": pybamm.one_dimensional_meshes.Uniform1DSubMesh,\n",
" \"positive electrode\": pybamm.one_dimensional_meshes.Uniform1DSubMesh,\n",
" \"negative particle\": pybamm.one_dimensional_meshes.Uniform1DSubMesh,\n",
" \"positive particle\": pybamm.one_dimensional_meshes.Uniform1DSubMesh,\n",
" \"current collector\": pybamm.zero_dimensional_meshes.SubMesh0D,\n",
"}\n",
"\n",
"var = pybamm.standard_spatial_vars\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/unsteady_heat_equation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
"metadata": {},
"outputs": [],
"source": [
"submesh_types = {\"rod\": pybamm.Uniform1DSubMesh}\n",
"submesh_types = {\"rod\": pybamm.one_dimensional_meshes.Uniform1DSubMesh}\n",
"var_pts = {x: 30}\n",
"mesh = pybamm.Mesh(geometry, submesh_types, var_pts)\n",
"spatial_methods = {\"rod\": pybamm.FiniteVolume}\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/scripts/SPM_compare_particle_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
# set mesh
submesh_types = models[0].default_submesh_types
particle_meshes = [
pybamm.Uniform1DSubMesh,
pybamm.Chebyshev1DSubMesh,
pybamm.GetExponential1DSubMesh(side="right"),
pybamm.one_dimensional_meshes.Uniform1DSubMesh,
pybamm.one_dimensional_meshes.Chebyshev1DSubMesh,
pybamm.one_dimensional_meshes.GetExponential1DSubMesh(side="right"),
]
meshes = [None] * len(models)
# discretise models
Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/create-model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def Diffusivity(cc):
param.process_geometry(geometry)

# mesh and discretise
submesh_types = {"SEI layer": pybamm.Uniform1DSubMesh}
submesh_types = {"SEI layer": pybamm.one_dimensional_meshes.Uniform1DSubMesh}
var_pts = {x: 50}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)

Expand Down
2 changes: 1 addition & 1 deletion examples/scripts/heat_equation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
param.process_geometry(geometry)

# Pick mesh, spatial method, and discretise
submesh_types = {"rod": pybamm.Uniform1DSubMesh}
submesh_types = {"rod": pybamm.one_dimensional_meshes.Uniform1DSubMesh}
var_pts = {x: 30}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
spatial_methods = {"rod": pybamm.FiniteVolume}
Expand Down
32 changes: 18 additions & 14 deletions pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,24 @@ def version(formatted=False):
#
from .discretisations.discretisation import Discretisation
from .meshes.meshes import Mesh
from .meshes.zero_dimensional_submesh import SubMesh0D
from .meshes.one_dimensional_submeshes import (
SubMesh1D,
Uniform1DSubMesh,
Chebyshev1DSubMesh,
GetExponential1DSubMesh,
Exponential1DSubMesh,
)
from .meshes.scikit_fem_submeshes import (
ScikitSubMesh2D,
ScikitUniform2DSubMesh,
ScikitChebyshev2DSubMesh,
ScikitTopExponential2DSubMesh,
)
from .meshes import zero_dimensional_meshes
from .meshes import one_dimensional_meshes
from .meshes import two_dimensional_meshes

#from .meshes.zero_dimensional_submesh import SubMesh0D
#from .meshes.one_dimensional_submeshes import (
# SubMesh1D,
# Uniform1DSubMesh,
# Chebyshev1DSubMesh,
# GetExponential1DSubMesh,
# Exponential1DSubMesh,
#)
#from .meshes.scikit_fem_submeshes import (
# ScikitSubMesh2D,
# ScikitUniform2DSubMesh,
# ScikitChebyshev2DSubMesh,
# ScikitTopExponential2DSubMesh,
#)

#
# Spatial Methods
Expand Down
4 changes: 2 additions & 2 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def check_tab_conditions(self, symbol, bcs):
)

# Replace keys with "left" and "right" as appropriate for 1D meshes
if isinstance(mesh, pybamm.SubMesh1D):
if isinstance(mesh, pybamm.one_dimensional_meshes.SubMesh1D):
# replace negative and/or positive tab
for tab in ["negative tab", "positive tab"]:
if any(tab in side for side in list(bcs.keys())):
Expand Down Expand Up @@ -633,7 +633,7 @@ def _process_symbol(self, symbol):
# "left" or "right" as appropriate
if symbol.side in ["negative tab", "positive tab"]:
mesh = self.mesh[symbol.children[0].domain[0]][0]
if isinstance(mesh, pybamm.SubMesh1D):
if isinstance(mesh, pybamm.one_dimensional_meshes.SubMesh1D):
symbol.side = mesh.tabs[symbol.side]
return child_spatial_method.boundary_value_or_flux(symbol, disc_child)

Expand Down
22 changes: 16 additions & 6 deletions pybamm/meshes/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, geometry, submesh_types, var_pts):
submesh_pts = {}
for domain in geometry:
# Zero dimensional submesh case (only one point)
if submesh_types[domain] == pybamm.SubMesh0D:
if submesh_types[domain] == pybamm.zero_dimensional_meshes.SubMesh0D:
submesh_pts[domain] = 1
# other cases
else:
Expand Down Expand Up @@ -97,7 +97,7 @@ def __init__(self, geometry, submesh_types, var_pts):
for domain in geometry:
if (
domain == "current collector"
and submesh_types[domain] != pybamm.SubMesh0D
and submesh_types[domain] != pybamm.zero_dimensional_meshes.SubMesh0D
):
self[domain] = [
submesh_types[domain](
Expand Down Expand Up @@ -160,7 +160,9 @@ def combine_submeshes(self, *submeshnames):
+ [self[submeshname][i].edges[1:] for submeshname in submeshnames[1:]]
)
coord_sys = self[submeshnames[0]][i].coord_sys
submeshes[i] = pybamm.SubMesh1D(combined_submesh_edges, coord_sys)
submeshes[i] = pybamm.one_dimensional_meshes.SubMesh1D(
combined_submesh_edges, coord_sys
)
return submeshes

def add_ghost_meshes(self):
Expand All @@ -174,7 +176,11 @@ def add_ghost_meshes(self):
(domain, submesh_list)
for domain, submesh_list in self.items()
if not isinstance(
submesh_list[0], (pybamm.SubMesh0D, pybamm.ScikitSubMesh2D)
submesh_list[0],
(
pybamm.zero_dimensional_meshes.SubMesh0D,
pybamm.two_dimensional_meshes.ScikitSubMesh2D,
),
)
]
for domain, submesh_list in submeshes:
Expand All @@ -186,13 +192,17 @@ def add_ghost_meshes(self):

# left ghost cell: two edges, one node, to the left of existing submesh
lgs_edges = np.array([2 * edges[0] - edges[1], edges[0]])
self[domain + "_left ghost cell"][i] = pybamm.SubMesh1D(
self[domain + "_left ghost cell"][
i
] = pybamm.one_dimensional_meshes.SubMesh1D(
lgs_edges, submesh.coord_sys
)

# right ghost cell: two edges, one node, to the right of
# existing submesh
rgs_edges = np.array([edges[-1], 2 * edges[-1] - edges[-2]])
self[domain + "_right ghost cell"][i] = pybamm.SubMesh1D(
self[domain + "_right ghost cell"][
i
] = pybamm.one_dimensional_meshes.SubMesh1D(
rgs_edges, submesh.coord_sys
)
6 changes: 6 additions & 0 deletions pybamm/meshes/one_dimensional_meshes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .base_submesh import SubMesh1D

from .chebyshev_submesh import Chebyshev1DSubMesh
from .exponential_submesh import GetExponential1DSubMesh, Exponential1DSubMesh
from .uniform_submesh import Uniform1DSubMesh
from .user_supplied_submesh import GetUserSupplied1DSubMesh, UserSupplied1DSubMesh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ def near(x, point, tol=3e-16):
return abs(x - point) < tol

for tab in ["negative", "positive"]:
if near(tabs[tab]["z_centre"], 0):
tab_location = tabs[tab]["z_centre"]
if near(tab_location, 0):
self.tabs[tab + " tab"] = "left"
elif near(tabs[tab]["z_centre"], l_z):
elif near(tab_location, l_z):
self.tabs[tab + " tab"] = "right"
else:
raise pybamm.GeometryError(
"""{} tab located at {}, but must be at either 0 or {}
(in dimensionless coordinates).""".format(
tab, tab_location, l_z
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# Chebyshev one-dimensional submesh
#
import pybamm
from .base_submesh import SubMesh1D

import numpy as np


class Chebyshev1DSubMesh(pybamm.SubMesh1D):
class Chebyshev1DSubMesh(SubMesh1D):
"""
A class to generate a submesh on a 1D domain using Chebyshev nodes on the
interval (a, b), given by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Exponential formula for one-dimensional submesh
#
import pybamm
from .base_submesh import SubMesh1D

import numpy as np

Expand Down Expand Up @@ -36,7 +37,7 @@ def __call__(self, lims, npts, tabs=None):
return Exponential1DSubMesh(lims, npts, tabs, self.side, self.stretch)


class Exponential1DSubMesh(pybamm.SubMesh1D):
class Exponential1DSubMesh(SubMesh1D):
"""
A class to generate a submesh on a 1D domain in which the points are clustered
close to one or both of boundaries using an exponential formula on the interval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# Uniform one-dimensional submesh
#
import pybamm
from .base_submesh import SubMesh1D

import numpy as np


class Uniform1DSubMesh(pybamm.SubMesh1D):
class Uniform1DSubMesh(SubMesh1D):
"""
A class to generate a uniform submesh on a 1D domain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# User supplied one-dimensional submesh
#
import pybamm
from .base_submesh import SubMesh1D


class GetUserSupplied1DSubMesh:
Expand All @@ -21,7 +22,7 @@ def __call__(self, lims, npts, tabs=None):
return UserSupplied1DSubMesh(lims, npts, tabs, self.nodes)


class UserSupplied1DSubMesh(pybamm.SubMesh1D):
class UserSupplied1DSubMesh(SubMesh1D):
"""
A class to generate a submesh on a 1D domain from a user supplied array of
nodes.
Expand Down
Loading

0 comments on commit e0bbb96

Please sign in to comment.