Skip to content

Commit

Permalink
coverage updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jreniel committed Jan 15, 2020
1 parent 8d7697f commit b7dacbb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 48 deletions.
7 changes: 6 additions & 1 deletion pyschism/mesh/hgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ def boundaries(self):

@property
def fgrid(self):
return self._fgrid
try:
return self.__fgrid
except AttributeError:
self._fgrid = ManningsN.constant(self, 0.025)
return self.__fgrid

@property
def _boundaries(self):
Expand Down Expand Up @@ -223,4 +227,5 @@ def _boundaries(self, boundaries):

@_fgrid.setter
def _fgrid(self, fgrid):
assert isinstance(fgrid, Fgrid)
self.__fgrid = fgrid
19 changes: 4 additions & 15 deletions pyschism/mesh/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ def __init__(

@classmethod
def open(cls, hgrid, vgrid=None, fgrid=None, crs=None):
if vgrid:
if vgrid is not None:
vgrid = Vgrid.open(vgrid)

m = cls(
Hgrid.open(hgrid, crs),
vgrid,
)

if fgrid:
if fgrid is not None:
fgrid = Fgrid.open(fgrid, crs)
m.hgrid.set_friction(fgrid)

return m

def make_plot(self, **kwargs):
if not self.vgrid:
if self.vgrid is None:
self.hgrid.make_plot(**kwargs)
else:
msg = "Plotting not yet supported for 3D meshes."
Expand All @@ -51,7 +51,7 @@ def vgrid(self):

@property
def fgrid(self):
return self._fgrid
return self.hgrid.fgrid

@property
def _hgrid(self):
Expand All @@ -61,10 +61,6 @@ def _hgrid(self):
def _vgrid(self):
return self.__vgrid

@property
def _fgrid(self):
return self.__fgrid

@_hgrid.setter
def _hgrid(self, hgrid):
assert isinstance(hgrid, Hgrid)
Expand All @@ -75,10 +71,3 @@ def _vgrid(self, vgrid):
if vgrid is not None:
assert isinstance(vgrid, Vgrid)
self.__vgrid = vgrid

@_fgrid.setter
def _fgrid(self, fgrid):
if fgrid is None:
fgrid = Fgrid.constant_manning(0.025)
assert isinstance(fgrid, Fgrid)
self.__fgrid = fgrid
2 changes: 1 addition & 1 deletion pyschism/mesh/vgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Vgrid:

@classmethod
def open(cls, path):
pass
return cls()

def write(self, path, overwrite=False):
path = pathlib.Path(path)
Expand Down
4 changes: 2 additions & 2 deletions tests/mesh/test_gr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def setUp(self):
self.nodes = {
'1': ((0., 0.), -99999.),
'2': ((.5, 0.), -99999.),
'3': ((0., 1.), -99999.),
'3': ((1., 0.), -99999.),
'4': ((1., 1.), -99999.),
'5': ((0., 1.), -99999.),
'6': ((.5, 1.5), -99999.),
Expand Down Expand Up @@ -46,7 +46,7 @@ def setUp(self):
1: ['6', '5', '10']
}

self.boundaries[1] = {0: ['7', '8', '9']} # "interior" boundary
self.boundaries[1] = {0: ['7', '8', '9', '7']} # "interior" boundary

self.grd = {
'nodes': self.nodes,
Expand Down
13 changes: 11 additions & 2 deletions tests/mesh/test_hgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setUp(self):
self.nodes = {
'1': ((0., 0.), -5.),
'2': ((.5, 0.), -4.),
'3': ((0., 1.), -3.),
'3': ((1., 0.), -3.),
'4': ((1., 1.), -2.),
'5': ((0., 1.), -1.),
'6': ((.5, 1.5), 0.),
Expand Down Expand Up @@ -48,7 +48,7 @@ def setUp(self):
1: ['6', '5', '10']
}

self.boundaries[1] = {0: ['7', '8', '9']} # "interior" boundary
self.boundaries[1] = {0: ['7', '8', '9', '7']} # "interior" boundary

self.grd = {
'nodes': self.nodes,
Expand Down Expand Up @@ -184,6 +184,15 @@ def test_plot_boundaries(self):
h = Hgrid(self.nodes, self.elements, self.boundaries)
h.plot_boundaries()

def test_triplot(self):
h = Hgrid(self.nodes, self.elements, self.boundaries)
h.triplot()

def test__fgrid_getter(self):
h = Hgrid(self.nodes, self.elements)
h.fgrid
assert isinstance(h._fgrid, Fgrid)


if __name__ == '__main__':
unittest.main()
68 changes: 41 additions & 27 deletions tests/mesh/test_mesh.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#! /usr/bin/env python
import tempfile
import pathlib
from pyschism.mesh import Mesh, Vgrid
from pyschism.mesh.friction import Fgrid
from pyschism.mesh import Mesh
import unittest


class MeshTestCase(unittest.TestCase):

def setUp(self):
self.nodes = {
nodes = {
'1': ((0., 0.), -5.),
'2': ((.5, 0.), -4.),
'3': ((0., 1.), -3.),
'3': ((1., 0.), -3.),
'4': ((1., 1.), -2.),
'5': ((0., 1.), -1.),
'6': ((.5, 1.5), 0.),
Expand All @@ -21,7 +22,7 @@ def setUp(self):
'10': ((-1., 1.), 4.),
'11': ((-1., 0.), 5.),
}
self.elements = {
elements = {
'1': ['5', '7', '9'],
'2': ['1', '2', '7'],
'3': ['2', '3', '8'],
Expand All @@ -34,41 +35,33 @@ def setUp(self):
'10': ['5', '1', '7']
}

self.boundaries = dict()
boundaries = dict()

self.boundaries[None] = { # "open" boundaries
boundaries[None] = { # "open" boundaries
0: ['10', '11', '1', '2'],
1: ['2', '3', '4']
}

self.boundaries[0] = { # "land" boundaries
boundaries[0] = { # "land" boundaries
0: ['4', '6'],
1: ['6', '5', '10']
}

self.boundaries[1] = {0: ['7', '8', '9']} # "interior" boundary
boundaries[1] = {0: ['7', '8', '9', '7']} # "interior" boundary

self.grd = {
'nodes': self.nodes,
'elements': self.elements,
'boundaries': self.boundaries,
'description': 'gr3_unittest'
}

def test_open(self):
# write hgrid
tmpdir = tempfile.TemporaryDirectory()
hgrid = pathlib.Path(tmpdir.name) / 'hgrid.gr3'
with open(hgrid, 'w') as f:
f.write('\n')
f.write(f'{len(self.elements):d} ')
f.write(f'{len(self.nodes):d}\n')
for id, ((x, y), z) in self.nodes.items():
f.write(f'{len(elements):d} ')
f.write(f'{len(nodes):d}\n')
for id, ((x, y), z) in nodes.items():
f.write(f"{id} ")
f.write(f"{x} ")
f.write(f"{y} ")
f.write(f"{z}\n")
for id, geom in self.elements.items():
for id, geom in elements.items():
f.write(f"{id} ")
f.write(f"{len(geom)} ")
for idx in geom:
Expand All @@ -87,24 +80,45 @@ def test_open(self):
fgrid = pathlib.Path(tmpdir.name) / 'fgrid.gr3'
with open(fgrid, 'w') as f:
f.write('generic_friction\n')
f.write(f'{len(self.elements):d} ')
f.write(f'{len(self.nodes):d}\n')
for id, ((x, y), z) in self.nodes.items():
f.write(f'{len(elements):d} ')
f.write(f'{len(nodes):d}\n')
for id, ((x, y), z) in nodes.items():
f.write(f"{id} ")
f.write(f"{x} ")
f.write(f"{y} ")
f.write(f"{z}\n")
for id, geom in self.elements.items():
for id, geom in elements.items():
f.write(f"{id} ")
f.write(f"{len(geom)} ")
for idx in geom:
f.write(f"{idx} ")
f.write(f"\n")
self.tmpdir = tmpdir
self.hgrid = hgrid
self.vgrid = vgrid
self.fgrid = fgrid

def test_open(self):
self.assertIsInstance(
Mesh.open(
hgrid,
vgrid,
fgrid
self.hgrid,
self.vgrid,
self.fgrid
),
Mesh
)

def test_make_plot(self):
m = Mesh.open(self.hgrid)
m.make_plot()

def test_make_plot_3D_raise(self):
m = Mesh.open(self.hgrid, self.vgrid)
self.assertRaises(
NotImplementedError,
m.make_plot
)

def test_default_fgrid(self):
m = Mesh.open(self.hgrid)
assert isinstance(m.fgrid, Fgrid)

0 comments on commit b7dacbb

Please sign in to comment.