From b7dacbb58d0c50f760148478ad212fc091041952 Mon Sep 17 00:00:00 2001 From: "J.R. Calzada" Date: Wed, 15 Jan 2020 15:57:08 -0500 Subject: [PATCH] coverage updates --- pyschism/mesh/hgrid.py | 7 ++++- pyschism/mesh/mesh.py | 19 +++-------- pyschism/mesh/vgrid.py | 2 +- tests/mesh/test_gr3.py | 4 +-- tests/mesh/test_hgrid.py | 13 ++++++-- tests/mesh/test_mesh.py | 68 ++++++++++++++++++++++++---------------- 6 files changed, 65 insertions(+), 48 deletions(-) diff --git a/pyschism/mesh/hgrid.py b/pyschism/mesh/hgrid.py index 1dd7156f..a0b80056 100644 --- a/pyschism/mesh/hgrid.py +++ b/pyschism/mesh/hgrid.py @@ -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): @@ -223,4 +227,5 @@ def _boundaries(self, boundaries): @_fgrid.setter def _fgrid(self, fgrid): + assert isinstance(fgrid, Fgrid) self.__fgrid = fgrid diff --git a/pyschism/mesh/mesh.py b/pyschism/mesh/mesh.py index e64ff208..b4b96b42 100644 --- a/pyschism/mesh/mesh.py +++ b/pyschism/mesh/mesh.py @@ -20,7 +20,7 @@ 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( @@ -28,14 +28,14 @@ def open(cls, hgrid, vgrid=None, fgrid=None, crs=None): 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." @@ -51,7 +51,7 @@ def vgrid(self): @property def fgrid(self): - return self._fgrid + return self.hgrid.fgrid @property def _hgrid(self): @@ -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) @@ -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 diff --git a/pyschism/mesh/vgrid.py b/pyschism/mesh/vgrid.py index 523d4162..0d41f10f 100644 --- a/pyschism/mesh/vgrid.py +++ b/pyschism/mesh/vgrid.py @@ -6,7 +6,7 @@ class Vgrid: @classmethod def open(cls, path): - pass + return cls() def write(self, path, overwrite=False): path = pathlib.Path(path) diff --git a/tests/mesh/test_gr3.py b/tests/mesh/test_gr3.py index 0f541db7..c18143a8 100755 --- a/tests/mesh/test_gr3.py +++ b/tests/mesh/test_gr3.py @@ -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.), @@ -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, diff --git a/tests/mesh/test_hgrid.py b/tests/mesh/test_hgrid.py index df816fe2..44ba1b9f 100755 --- a/tests/mesh/test_hgrid.py +++ b/tests/mesh/test_hgrid.py @@ -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.), @@ -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, @@ -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() diff --git a/tests/mesh/test_mesh.py b/tests/mesh/test_mesh.py index fbffb672..0b00663f 100644 --- a/tests/mesh/test_mesh.py +++ b/tests/mesh/test_mesh.py @@ -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.), @@ -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'], @@ -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: @@ -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)