Skip to content

Commit 679a6d8

Browse files
authored
Merge pull request #77 from simpeg/gridHfunction
GridH function
2 parents a3f50e7 + 918b6da commit 679a6d8

File tree

8 files changed

+115
-6
lines changed

8 files changed

+115
-6
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[bumpversion]
2-
current_version = 0.1.11
2+
current_version = 0.1.12
33
files = setup.py discretize/__init__.py docs/conf.py
44

discretize/TensorMesh.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ def gridN(self):
159159
"""Nodal grid."""
160160
return self._getTensorGrid('N')
161161

162+
@property
163+
def h_gridded(self):
164+
"""
165+
Returns an (nC, dim) numpy array with the widths of all cells in order
166+
"""
167+
168+
if self.dim == 1:
169+
return np.reshape(self.h, (self.nC, 1))
170+
elif self.dim == 2:
171+
hx = np.kron(np.ones(self.nCy), self.h[0])
172+
hy = np.kron(self.h[1], np.ones(self.nCx))
173+
return np.c_[hx, hy]
174+
elif self.dim == 3:
175+
hx = np.kron(np.ones(self.nCy*self.nCz), self.h[0])
176+
hy = np.kron(np.ones(self.nCz), np.kron(self.h[1], np.ones(self.nCx)))
177+
hz = np.kron(self.h[2], np.ones(self.nCx*self.nCy))
178+
return np.c_[hx, hy, hz]
179+
162180
@property
163181
def gridFx(self):
164182
"""Face staggered grid in the x direction."""

discretize/TreeMesh.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def __dirty__(self, val):
170170
'_faceDiv', '_edgeCurl', '_nodalGrad',
171171
'_aveFx2CC', '_aveFy2CC', '_aveFz2CC', '_aveF2CC', '_aveF2CCV',
172172
'_aveEx2CC', '_aveEy2CC', '_aveEz2CC', '_aveE2CC', '_aveE2CCV',
173-
'_aveN2CC',
173+
'_aveN2CC', '_h_gridded'
174174
]
175175
for p in deleteThese:
176176
if hasattr(self, p): delattr(self, p)
@@ -715,6 +715,12 @@ def balance(self, recursive=True, cells=None, verbose=False, _inRecursion=False)
715715

716716
@property
717717
def gridCC(self):
718+
"""
719+
Returns an M by N numpy array with the center locations of all cells
720+
in order. M is the number of cells and N=1,2,3 is the dimension of the
721+
mesh.
722+
"""
723+
718724
if getattr(self, '_gridCC', None) is None:
719725
self._gridCC = np.zeros((len(self._cells),self.dim))
720726
for ii, ind in enumerate(self._sortedCells):
@@ -724,10 +730,28 @@ def gridCC(self):
724730

725731
@property
726732
def gridN(self):
733+
"""
734+
Returns an M by N numpy array with the widths of all cells in order.
735+
M is the number of nodes and N=1,2,3 is the dimension of the mesh.
736+
"""
737+
727738
self.number()
728739
R = self._deflationMatrix('N', withHanging=False)
729740
return R.T * self._gridN + np.repeat([self.x0],self.nN,axis=0)
730741

742+
@property
743+
def h_gridded(self):
744+
"""
745+
Returns an (nC, dim) numpy array with the widths of all cells in order
746+
"""
747+
748+
if getattr(self, '_h_gridded', None) is None:
749+
self._h_gridded = np.zeros((len(self._cells), self.dim))
750+
for ii, ind in enumerate(self._sortedCells):
751+
p = self._asPointer(ind)
752+
self._h_gridded[ii, :] = self._cellH(p)
753+
return self._h_gridded
754+
731755
@property
732756
def gridFx(self):
733757
self.number()

discretize/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"""
1919
)
2020

21-
__version__ = '0.1.11'
21+
__version__ = '0.1.12'
2222
__author__ = 'SimPEG Team'
2323
__license__ = 'MIT'
2424
__copyright__ = '2013 - 2017, SimPEG Developers, http://simpeg.xyz'

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@
6060
# built documents.
6161
#
6262
# The short X.Y version.
63-
version = '0.1.11'
63+
version = '0.1.12'
6464
# The full version, including alpha/beta/rc tags.
65-
release = '0.1.11'
65+
release = '0.1.12'
6666

6767
# The language for content autogenerated by Sphinx. Refer to documentation
6868
# for a list of supported languages.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def configuration(parent_package='', top_path=None):
5656

5757
setup(
5858
name="discretize",
59-
version="0.1.11",
59+
version="0.1.12",
6060
install_requires=[
6161
'numpy>=1.7',
6262
'scipy>=0.13',

tests/base/test_tensor.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ def setUp(self):
1616
self.mesh2 = discretize.TensorMesh([a, b], [3, 5])
1717
self.mesh3 = discretize.TensorMesh([a, b, c])
1818

19+
def test_gridded_2D(self):
20+
H = self.mesh2.h_gridded
21+
test_hx = np.all(H[:, 0] == np.r_[1., 1., 1., 1., 1., 1.])
22+
test_hy = np.all(H[:, 1] == np.r_[1., 1., 1., 2., 2., 2.])
23+
self.assertTrue(test_hx and test_hy)
24+
25+
def test_gridded_3D(self):
26+
H = self.mesh3.h_gridded
27+
test_hx = np.all(H[:, 0] == np.r_[1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
28+
test_hy = np.all(H[:, 1] == np.r_[1., 1., 1., 2., 2., 2., 1., 1., 1., 2., 2., 2.])
29+
test_hz = np.all(H[:, 2] == np.r_[1., 1., 1., 1., 1., 1., 4., 4., 4., 4., 4., 4.])
30+
self.assertTrue(test_hx and test_hy and test_hz)
31+
1932
def test_vectorN_2D(self):
2033
testNx = np.array([3, 4, 5, 6])
2134
testNy = np.array([5, 6, 8])

tests/tree/test_tree.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,40 @@ def test_corsen(self):
107107
assert M._index([0, 2, 2]) not in M
108108
assert M._index([2, 2, 2]) not in M
109109

110+
def test_h_gridded_2D(self):
111+
hx, hy = np.ones(4), np.r_[1., 2., 3., 4.]
112+
113+
M = discretize.TreeMesh([hx, hy])
114+
115+
def refinefcn(cell):
116+
xyz = cell.center
117+
d = (xyz**2).sum()**0.5
118+
if d < 3:
119+
return 2
120+
return 1
121+
122+
M.refine(refinefcn)
123+
H = M.h_gridded
124+
125+
test_hx = np.all(H[:, 0] == np.r_[1., 1., 1., 1., 2., 2., 2.])
126+
test_hy = np.all(H[:, 1] == np.r_[1., 1., 2., 2., 3., 7., 7.])
127+
128+
self.assertTrue(test_hx and test_hy)
129+
130+
def test_h_gridded_updates(self):
131+
mesh = discretize.TreeMesh([8, 8])
132+
mesh.refine(1)
133+
134+
H = mesh.h_gridded
135+
self.assertTrue(np.all(H[:, 0] == 0.5*np.ones(4)))
136+
self.assertTrue(np.all(H[:, 1] == 0.5*np.ones(4)))
137+
138+
# refine the mesh and make sure h_gridded is updated
139+
mesh.refine(2)
140+
H = mesh.h_gridded
141+
self.assertTrue(np.all(H[:, 0] == 0.25*np.ones(16)))
142+
self.assertTrue(np.all(H[:, 1] == 0.25*np.ones(16)))
143+
110144
def test_faceDiv(self):
111145

112146
hx, hy = np.r_[1., 2, 3, 4], np.r_[5., 6, 7, 8]
@@ -233,6 +267,26 @@ def test_VectorIdenties(self):
233267
assert np.max(np.abs((M.faceDiv * M.edgeCurl).todense().flatten())) < TOL
234268
assert np.max(np.abs((Mr.faceDiv * Mr.edgeCurl).todense().flatten())) < TOL
235269

270+
def test_h_gridded_3D(self):
271+
hx, hy, hz = np.ones(4), np.r_[1., 2., 3., 4.], 2*np.ones(4)
272+
273+
M = discretize.TreeMesh([hx, hy, hz])
274+
275+
def refinefcn(cell):
276+
xyz = cell.center
277+
d = (xyz**2).sum()**0.5
278+
if d < 3:
279+
return 2
280+
return 1
281+
282+
M.refine(refinefcn)
283+
H = M.h_gridded
284+
285+
test_hx = np.all(H[:, 0] == np.r_[1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.])
286+
test_hy = np.all(H[:, 1] == np.r_[1., 1., 2., 2., 1., 1., 2., 2., 3., 7., 7., 3., 3., 7., 7.])
287+
test_hz = np.all(H[:, 2] == np.r_[2., 2., 2., 2., 2., 2., 2., 2., 4., 4., 4., 4., 4., 4., 4.])
288+
289+
self.assertTrue(test_hx and test_hy and test_hz)
236290

237291
class Test2DInterpolation(unittest.TestCase):
238292

0 commit comments

Comments
 (0)