Skip to content

Commit

Permalink
#617 mesh generator class
Browse files Browse the repository at this point in the history
  • Loading branch information
rtimms committed Oct 4, 2019
1 parent 4775e42 commit 4085dd5
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# don't ignore important .txt files
!requirements*
!LICENSE.txt
!CMakeLists.txt

# running files
*.pyc
Expand Down Expand Up @@ -54,6 +55,7 @@ pyproject.toml
# virtual enviroment
venv/
venv3.5/
PyBaMM-env/
bin/
etc/
lib/
Expand All @@ -64,3 +66,5 @@ pyvenv.cfg

# sundials
sundials
sundials4
SuiteSparse
3 changes: 3 additions & 0 deletions docs/source/meshes/meshes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ Meshes

.. autoclass:: pybamm.Mesh
:members:

.. autoclass:: pybamm.MeshGenerator
:members:
4 changes: 2 additions & 2 deletions docs/source/meshes/one_dimensional_submeshes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
.. autoclass:: pybamm.Exponential1DSubMesh
:members:

.. autoclass:: pybamm.GetExponential1DSubMesh
.. autoclass:: pybamm.Exponential1DSubMeshGenerator
:members:

.. autoclass:: pybamm.Chebyshev1DSubMesh
Expand All @@ -19,5 +19,5 @@
.. autoclass:: pybamm.UserSupplied1DSubMesh
:members:

.. autoclass:: pybamm.GetUserSupplied1DSubMesh
.. autoclass:: pybamm.UserSupplied1DSubMeshGenerator
:members:
2 changes: 1 addition & 1 deletion docs/source/meshes/two_dimensional_submeshes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
.. autoclass:: pybamm.UserSupplied2DSubMesh
:members:

.. autoclass:: pybamm.GetUserSupplied2DSubMesh
.. autoclass:: pybamm.UserSupplied2DSubMeshGenerator
:members:
2 changes: 1 addition & 1 deletion examples/scripts/SPM_compare_particle_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
particle_meshes = [
pybamm.Uniform1DSubMesh,
pybamm.Chebyshev1DSubMesh,
pybamm.GetExponential1DSubMesh(side="right"),
pybamm.Exponential1DSubMeshGenerator(side="right"),
]
meshes = [None] * len(models)
# discretise models
Expand Down
8 changes: 4 additions & 4 deletions pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,24 @@ def version(formatted=False):
# Mesh and Discretisation classes
#
from .discretisations.discretisation import Discretisation
from .meshes.meshes import Mesh
from .meshes.meshes import Mesh, MeshGenerator
from .meshes.zero_dimensional_submesh import SubMesh0D
from .meshes.one_dimensional_submeshes import (
SubMesh1D,
Uniform1DSubMesh,
Exponential1DSubMesh,
GetExponential1DSubMesh,
Exponential1DSubMeshGenerator,
Chebyshev1DSubMesh,
UserSupplied1DSubMesh,
GetUserSupplied1DSubMesh,
UserSupplied1DSubMeshGenerator,
)
from .meshes.scikit_fem_submeshes import (
ScikitSubMesh2D,
ScikitUniform2DSubMesh,
ScikitTopExponential2DSubMesh,
ScikitChebyshev2DSubMesh,
UserSupplied2DSubMesh,
GetUserSupplied2DSubMesh,
UserSupplied2DSubMeshGenerator,
)

#
Expand Down
17 changes: 17 additions & 0 deletions pybamm/meshes/meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,20 @@ def add_ghost_meshes(self):
self[domain + "_right ghost cell"][i] = pybamm.SubMesh1D(
rgs_edges, submesh.coord_sys
)


class MeshGenerator:
"""
Base class for mesh generator objects that are used to generate submeshes
that require input paramaters.
"""

def __init__(self):
pass

def __call__(self):
"""
Each Mesh Generator should implemented a call method which returns
an instance of a submesh.
"""
raise NotImplementedError
16 changes: 9 additions & 7 deletions pybamm/meshes/one_dimensional_submeshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# One-dimensional submeshes
#
import pybamm
from .meshes import MeshGenerator

import numpy as np

Expand Down Expand Up @@ -82,7 +83,8 @@ 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
[a,b]. Note: this mesh should be created using :class:`GetExponential1DSubMesh`.
[a,b]. Note: this mesh should be created using
:class:`Exponential1DSubMeshGenerator`.
If side is "left", the gridpoints are given by
Expand Down Expand Up @@ -125,7 +127,7 @@ class Exponential1DSubMesh(SubMesh1D):
A dictionary that contains information about the size and location of
the tabs
stretch : float
The factor which appears in the exponential.
The factor (alpha) which appears in the exponential.
"""

def __init__(self, lims, npts, tabs, side, stretch):
Expand Down Expand Up @@ -176,7 +178,7 @@ def __init__(self, lims, npts, tabs, side, stretch):
super().__init__(edges, coord_sys=coord_sys, tabs=tabs)


class GetExponential1DSubMesh:
class Exponential1DSubMeshGenerator(MeshGenerator):
"""
A class to generate a submesh on a 1D domain in which the points are clustered
close to one or both boundaries using an exponential formula on the interval [a,b].
Expand All @@ -188,8 +190,8 @@ class GetExponential1DSubMesh:
or both boundaries. Can be "left", "right" or "symmetric". Defualt is
"symmetric".
stretch : float, optional
The factor which appears in the exponential, defualt is 1.15 is side is
"symmetric" and 2.3 is side is "left" or "right".
The factor (alpha) which appears in the exponential, defualt is 1.15 is
side is "symmetric" and 2.3 is side is "left" or "right".
"""

Expand Down Expand Up @@ -261,7 +263,7 @@ def __init__(self, lims, npts, tabs=None):
class UserSupplied1DSubMesh(SubMesh1D):
"""
A class to generate a submesh on a 1D domain from a user supplied array of
Note: this mesh should be created using :class:`GetUserSupplied1DSubMesh`.
Note: this mesh should be created using :class:`UserSupplied1DSubMeshGenerator`.
edges.
Parameters
Expand Down Expand Up @@ -319,7 +321,7 @@ def __init__(self, lims, npts, tabs, edges):
super().__init__(edges, coord_sys=coord_sys, tabs=tabs)


class GetUserSupplied1DSubMesh:
class UserSupplied1DSubMeshGenerator(MeshGenerator):
"""
A class to generate a submesh on a 1D domain using a user supplied vector of
edges.
Expand Down
5 changes: 3 additions & 2 deletions pybamm/meshes/scikit_fem_submeshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# scikit-fem meshes for use in PyBaMM
#
import pybamm
from .meshes import MeshGenerator

import skfem
import numpy as np
Expand Down Expand Up @@ -325,7 +326,7 @@ class UserSupplied2DSubMesh(ScikitSubMesh2D):
"""
A class to generate a tensor product submesh on a 2D domain by using two user
supplied vectors of edges: one for the y-direction and one for the z-direction.
Note: this mesh should be created using :class:`GetUserSupplied2DSubMesh`.
Note: this mesh should be created using :class:`UserSupplied2DSubMeshGenerator`.
Parameters
----------
Expand Down Expand Up @@ -401,7 +402,7 @@ def __init__(self, lims, npts, tabs, y_edges, z_edges):
super().__init__(edges, coord_sys=coord_sys, tabs=tabs)


class GetUserSupplied2DSubMesh:
class UserSupplied2DSubMeshGenerator(MeshGenerator):
"""
A class to generate a tensor product submesh on a 2D domain by using two user
supplied vectors of edges: one for the y-direction and one for the z-direction.
Expand Down
2 changes: 1 addition & 1 deletion results/2plus1D/user_mesh_spm_1plus1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
# set mesh using user-supplied edges in z
z_edges = np.array([0, 0.03, 0.1, 0.3, 0.47, 0.5, 0.73, 0.8, 0.911, 1])
submesh_types = model.default_submesh_types
submesh_types["current collector"] = pybamm.GetUserSupplied1DSubMesh(z_edges)
submesh_types["current collector"] = pybamm.UserSupplied1DSubMeshGenerator(z_edges)
# Need to make sure var_pts for z is one less than number of edges (variables are
# evaluated at cell centres)
npts_z = len(z_edges) - 1
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_meshes/test_meshes.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ def test_1plus1D_tabs_right_left(self):
self.assertEqual(mesh["current collector"][0].tabs["positive tab"], "left")


class TestMeshGenerator(unittest.TestCase):
def test_not_implemented(self):
generator = pybamm.MeshGenerator()
with self.assertRaises(NotImplementedError):
generator()


if __name__ == "__main__":
print("Add -v for more debug output")
import sys
Expand Down
14 changes: 8 additions & 6 deletions tests/unit/test_meshes/test_one_dimensional_submesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_exceptions(self):
class TestExponential1DSubMesh(unittest.TestCase):
def test_exceptions(self):
lims = [[0, 1], [0, 1]]
mesh = pybamm.GetExponential1DSubMesh()
mesh = pybamm.Exponential1DSubMeshGenerator()
with self.assertRaises(pybamm.GeometryError):
mesh(lims, None)

Expand All @@ -44,7 +44,7 @@ def test_symmetric_mesh_creation_no_parameters(self):
}

submesh_types = {
"negative particle": pybamm.GetExponential1DSubMesh(
"negative particle": pybamm.Exponential1DSubMeshGenerator(
side="symmetric", stretch=1.5
)
}
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_left_mesh_creation_no_parameters(self):
}

submesh_types = {
"negative particle": pybamm.GetExponential1DSubMesh(side="left")
"negative particle": pybamm.Exponential1DSubMeshGenerator(side="left")
}
var_pts = {r: 21}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_right_mesh_creation_no_parameters(self):
}

submesh_types = {
"negative particle": pybamm.GetExponential1DSubMesh(side="right")
"negative particle": pybamm.Exponential1DSubMeshGenerator(side="right")
}
var_pts = {r: 20}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)
Expand Down Expand Up @@ -168,7 +168,7 @@ class TestUser1DSubMesh(unittest.TestCase):
def test_exceptions(self):
lims = [[0, 1], [0, 1]]
edges = np.array([0, 0.3, 1])
mesh = pybamm.GetUserSupplied1DSubMesh(edges)
mesh = pybamm.UserSupplied1DSubMeshGenerator(edges)
# test too many lims
with self.assertRaises(pybamm.GeometryError):
mesh(lims, None)
Expand Down Expand Up @@ -205,7 +205,9 @@ def test_mesh_creation_no_parameters(self):
}

edges = np.array([0, 0.3, 1])
submesh_types = {"negative particle": pybamm.GetUserSupplied1DSubMesh(edges)}
submesh_types = {
"negative particle": pybamm.UserSupplied1DSubMeshGenerator(edges)
}
var_pts = {r: len(edges) - 1}
mesh = pybamm.Mesh(geometry, submesh_types, var_pts)

Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_meshes/test_scikit_fem_submesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ def test_mesh_creation(self):
"negative electrode": pybamm.Uniform1DSubMesh,
"separator": pybamm.Uniform1DSubMesh,
"positive electrode": pybamm.Uniform1DSubMesh,
"current collector": pybamm.GetUserSupplied2DSubMesh(y_edges, z_edges),
"current collector": pybamm.UserSupplied2DSubMeshGenerator(
y_edges, z_edges
),
}

mesh_type = pybamm.Mesh
Expand Down Expand Up @@ -404,7 +406,7 @@ def test_exceptions(self):
lims = {var.y: {"min": 0, "max": 1}}
y_edges = np.array([0, 0.3, 1])
z_edges = np.array([0, 0.3, 1])
mesh = pybamm.GetUserSupplied2DSubMesh(y_edges, z_edges)
mesh = pybamm.UserSupplied2DSubMeshGenerator(y_edges, z_edges)
# test not enough lims
with self.assertRaises(pybamm.GeometryError):
mesh(lims, None)
Expand Down

0 comments on commit 4085dd5

Please sign in to comment.