diff --git a/.gitignore b/.gitignore index 9d79ca0b45..59aaf84253 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # don't ignore important .txt files !requirements* !LICENSE.txt +!CMakeLists.txt # running files *.pyc @@ -54,6 +55,7 @@ pyproject.toml # virtual enviroment venv/ venv3.5/ +PyBaMM-env/ bin/ etc/ lib/ @@ -64,3 +66,5 @@ pyvenv.cfg # sundials sundials +sundials4 +SuiteSparse diff --git a/docs/source/meshes/meshes.rst b/docs/source/meshes/meshes.rst index 42e4a9a410..124bcfc03b 100644 --- a/docs/source/meshes/meshes.rst +++ b/docs/source/meshes/meshes.rst @@ -3,3 +3,6 @@ Meshes .. autoclass:: pybamm.Mesh :members: + +.. autoclass:: pybamm.MeshGenerator + :members: diff --git a/docs/source/meshes/one_dimensional_submeshes.rst b/docs/source/meshes/one_dimensional_submeshes.rst index c1a5e8862d..55887d1e69 100644 --- a/docs/source/meshes/one_dimensional_submeshes.rst +++ b/docs/source/meshes/one_dimensional_submeshes.rst @@ -10,7 +10,7 @@ .. autoclass:: pybamm.Exponential1DSubMesh :members: -.. autoclass:: pybamm.GetExponential1DSubMesh +.. autoclass:: pybamm.Exponential1DSubMeshGenerator :members: .. autoclass:: pybamm.Chebyshev1DSubMesh @@ -19,5 +19,5 @@ .. autoclass:: pybamm.UserSupplied1DSubMesh :members: -.. autoclass:: pybamm.GetUserSupplied1DSubMesh +.. autoclass:: pybamm.UserSupplied1DSubMeshGenerator :members: diff --git a/docs/source/meshes/two_dimensional_submeshes.rst b/docs/source/meshes/two_dimensional_submeshes.rst index 9a66ac5891..03fda6b145 100644 --- a/docs/source/meshes/two_dimensional_submeshes.rst +++ b/docs/source/meshes/two_dimensional_submeshes.rst @@ -16,5 +16,5 @@ .. autoclass:: pybamm.UserSupplied2DSubMesh :members: -.. autoclass:: pybamm.GetUserSupplied2DSubMesh +.. autoclass:: pybamm.UserSupplied2DSubMeshGenerator :members: diff --git a/examples/scripts/SPM_compare_particle_grid.py b/examples/scripts/SPM_compare_particle_grid.py index fe2717dec3..2571681d60 100644 --- a/examples/scripts/SPM_compare_particle_grid.py +++ b/examples/scripts/SPM_compare_particle_grid.py @@ -34,7 +34,7 @@ particle_meshes = [ pybamm.Uniform1DSubMesh, pybamm.Chebyshev1DSubMesh, - pybamm.GetExponential1DSubMesh(side="right"), + pybamm.Exponential1DSubMeshGenerator(side="right"), ] meshes = [None] * len(models) # discretise models diff --git a/pybamm/__init__.py b/pybamm/__init__.py index f94a406d55..1a4bf4cae8 100644 --- a/pybamm/__init__.py +++ b/pybamm/__init__.py @@ -208,16 +208,16 @@ 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, @@ -225,7 +225,7 @@ def version(formatted=False): ScikitTopExponential2DSubMesh, ScikitChebyshev2DSubMesh, UserSupplied2DSubMesh, - GetUserSupplied2DSubMesh, + UserSupplied2DSubMeshGenerator, ) # diff --git a/pybamm/meshes/meshes.py b/pybamm/meshes/meshes.py index 3eb7a7317b..52160d3ec9 100644 --- a/pybamm/meshes/meshes.py +++ b/pybamm/meshes/meshes.py @@ -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 diff --git a/pybamm/meshes/one_dimensional_submeshes.py b/pybamm/meshes/one_dimensional_submeshes.py index a604f667aa..89875e8222 100644 --- a/pybamm/meshes/one_dimensional_submeshes.py +++ b/pybamm/meshes/one_dimensional_submeshes.py @@ -2,6 +2,7 @@ # One-dimensional submeshes # import pybamm +from .meshes import MeshGenerator import numpy as np @@ -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 @@ -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): @@ -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]. @@ -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". """ @@ -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 @@ -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. diff --git a/pybamm/meshes/scikit_fem_submeshes.py b/pybamm/meshes/scikit_fem_submeshes.py index e146f4282d..f0cc6a6d4b 100644 --- a/pybamm/meshes/scikit_fem_submeshes.py +++ b/pybamm/meshes/scikit_fem_submeshes.py @@ -2,6 +2,7 @@ # scikit-fem meshes for use in PyBaMM # import pybamm +from .meshes import MeshGenerator import skfem import numpy as np @@ -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 ---------- @@ -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. diff --git a/results/2plus1D/user_mesh_spm_1plus1D.py b/results/2plus1D/user_mesh_spm_1plus1D.py index 173cf5c2a7..1e6ecaa95f 100644 --- a/results/2plus1D/user_mesh_spm_1plus1D.py +++ b/results/2plus1D/user_mesh_spm_1plus1D.py @@ -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 diff --git a/tests/unit/test_meshes/test_meshes.py b/tests/unit/test_meshes/test_meshes.py index 7012c1813c..0a083173db 100644 --- a/tests/unit/test_meshes/test_meshes.py +++ b/tests/unit/test_meshes/test_meshes.py @@ -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 diff --git a/tests/unit/test_meshes/test_one_dimensional_submesh.py b/tests/unit/test_meshes/test_one_dimensional_submesh.py index d45aff4b16..e3fd627435 100644 --- a/tests/unit/test_meshes/test_one_dimensional_submesh.py +++ b/tests/unit/test_meshes/test_one_dimensional_submesh.py @@ -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) @@ -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 ) } @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/tests/unit/test_meshes/test_scikit_fem_submesh.py b/tests/unit/test_meshes/test_scikit_fem_submesh.py index 6cc0f7f300..6ec271789d 100644 --- a/tests/unit/test_meshes/test_scikit_fem_submesh.py +++ b/tests/unit/test_meshes/test_scikit_fem_submesh.py @@ -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 @@ -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)