Skip to content

Commit

Permalink
Bug fixes, new tests and increased coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jreniel committed Jan 15, 2020
1 parent 5f64bcb commit d016f57
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 17 deletions.
1 change: 0 additions & 1 deletion pyschism/mesh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
"Gmesh",
"Hgrid",
"Vgrid",
# "Fgrid",
"Mesh"
]
10 changes: 10 additions & 0 deletions pyschism/mesh/friction/fgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ class Fgrid(Gmesh):
"""
Base class for all friction types (e.g. manning.gr3, drag.gr3, etc...)
"""

def __init__(
self,
nodes,
elements,
values=None,
crs=None,
description=None,
):
super().__init__(*self._gr3_to_mesh(nodes, elements), crs, description)
6 changes: 2 additions & 4 deletions pyschism/mesh/friction/manning.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ class ManningsN(Fgrid):
@classmethod
def constant(cls, mesh, value):
return cls(
mesh._coords,
mesh._triangles,
mesh._quads,
values=len(mesh.coords)*[value],
mesh.nodes,
mesh.elements,
crs=mesh.crs,
description=mesh.description + "_mannings"
)
19 changes: 17 additions & 2 deletions pyschism/mesh/gmesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Geomesh:
def __init__(
self,
coords,
triang=None,
triangles=None,
quads=None,
values=None,
crs=None,
description=None,
):
self._coords = coords
self._triangles = triang
self._triangles = triangles
self._quads = quads
self._values = values
self._crs = crs
Expand All @@ -39,6 +39,10 @@ def transform_to(self, dst_crs):
def get_index(self, id):
return self.node_indexes[id]

@classmethod
def from_gr3(cls, nodes, elements):
return cls(*cls._gr3_to_mesh(nodes, elements))

@_figure
def tricontourf(self, axes=None, show=True, figsize=None, **kwargs):
if len(self.triangles) > 0:
Expand Down Expand Up @@ -190,6 +194,17 @@ def _crs(self):
def _description(self):
return self.__description

@staticmethod
def _gr3_to_mesh(nodes, elements):
# cast gr3 inputs into a geomesh structure format
coords = {id: (x, y) for id, ((x, y), value) in nodes.items()}
triangles = {id: geom for id, geom in elements.items()
if len(geom) == 3}
quads = {id: geom for id, geom in elements.items()
if len(geom) == 4}
values = [value for coord, value in nodes.values()]
return coords, triangles, quads, values

@_coords.setter
def _coords(self, coords):
msg = "coords argument must be a dictionary of the form "
Expand Down
10 changes: 3 additions & 7 deletions pyschism/mesh/hgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ def __init__(
description=None,
):
# cast gr3 inputs into a geomesh structure format
coords = {id: (x, y) for id, ((x, y), value) in nodes.items()}
values = [-value for coord, value in nodes.values()]
triangles = {id: geom for id, geom in elements.items()
if len(geom) == 3}
quads = {id: geom for id, geom in elements.items()
if len(geom) == 4}
super().__init__(coords, triangles, quads, values, crs, description)
msh = list(self._gr3_to_mesh(nodes, elements))
msh[3] = [-value for coord, value in nodes.values()]
super().__init__(*msh, crs, description)
self._boundaries = boundaries

@classmethod
Expand Down
6 changes: 5 additions & 1 deletion pyschism/mesh/vgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

class Vgrid:

def dump(self, path, overwrite=False):
@classmethod
def open(cls, path):
pass

def write(self, path, overwrite=False):
path = pathlib.Path(path)
if path.is_file() and not overwrite:
msg = 'File exists, pass overwrite=True to allow overwrite.'
Expand Down
4 changes: 2 additions & 2 deletions tests/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions tests/mesh/test_fgrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! /usr/bin/env python
import tempfile
import pathlib
from pyschism.mesh.friction import Fgrid
import unittest


class FgridTestCase(unittest.TestCase):

def setUp(self):
self.nodes = {
'1': ((0., 0.), -5.),
'2': ((.5, 0.), -4.),
'3': ((0., 1.), -3.),
'4': ((1., 1.), -2.),
'5': ((0., 1.), -1.),
'6': ((.5, 1.5), 0.),
'7': ((.33, .33), 1.),
'8': ((.66, .33), 2.),
'9': ((.5, .66), 3.),
'10': ((-1., 1.), 4.),
'11': ((-1., 0.), 5.),
}
self.elements = {
'1': ['5', '7', '9'],
'2': ['1', '2', '7'],
'3': ['2', '3', '8'],
'4': ['8', '7', '2'],
'5': ['3', '4', '8'],
'6': ['4', '9', '8'],
'7': ['4', '6', '5'],
'8': ['5', '10', '11', '1'],
'9': ['9', '4', '5'],
'10': ['5', '1', '7']
}

self.fgrid = {
'nodes': self.nodes,
'elements': self.elements,
}

# def test_open(self):
# f = Fgrid.open()

7 changes: 7 additions & 0 deletions tests/mesh/test_gmesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ def test_default_description(self):
gmsh.description = 'test'
self.assertEqual(gmsh.description, 'test')

def test_from_gr3(self):
nodes = {id: (coord, -99999.) for id, coord in self.coords.items()}
elements = {id: index for id, index in self.triangles.items()}
elements.update({id: index for id, index in self.quads.items()})
gmsh = Gmesh.from_gr3(nodes, elements)
self.assertIsInstance(gmsh, Gmesh)


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


class MeshTestCase(unittest.TestCase):

def setUp(self):
self.nodes = {
'1': ((0., 0.), -5.),
'2': ((.5, 0.), -4.),
'3': ((0., 1.), -3.),
'4': ((1., 1.), -2.),
'5': ((0., 1.), -1.),
'6': ((.5, 1.5), 0.),
'7': ((.33, .33), 1.),
'8': ((.66, .33), 2.),
'9': ((.5, .66), 3.),
'10': ((-1., 1.), 4.),
'11': ((-1., 0.), 5.),
}
self.elements = {
'1': ['5', '7', '9'],
'2': ['1', '2', '7'],
'3': ['2', '3', '8'],
'4': ['8', '7', '2'],
'5': ['3', '4', '8'],
'6': ['4', '9', '8'],
'7': ['4', '6', '5'],
'8': ['5', '10', '11', '1'],
'9': ['9', '4', '5'],
'10': ['5', '1', '7']
}

self.boundaries = dict()

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

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

self.boundaries[1] = {0: ['7', '8', '9']} # "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"{id} ")
f.write(f"{x} ")
f.write(f"{y} ")
f.write(f"{z}\n")
for id, geom in self.elements.items():
f.write(f"{id} ")
f.write(f"{len(geom)} ")
for idx in geom:
f.write(f"{idx} ")
f.write(f"\n")
vgrid = pathlib.Path(tmpdir.name) / 'vgrid.gr3'
with open(vgrid, 'w') as f:
f.write("2 !ivcor\n")
f.write("2 1 1.e6 !nvrt, kz (# of Z-levels); h_s (transition depth between S and Z)\n")
f.write("Z levels\n")
f.write("1 -1.e6\n")
f.write("S levels\n")
f.write("40. 1. 1.e-4 !h_c, theta_b, theta_f\n")
f.write(" 1 -1.\n")
f.write(" 2 0.\n")

self.assertIsInstance(
Mesh.open(
hgrid,
vgrid
),
Mesh
)
28 changes: 28 additions & 0 deletions tests/mesh/test_vgrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env python
import tempfile
import pathlib
import unittest
from pyschism.mesh import Vgrid


class VgridTestCase(unittest.TestCase):

def test_init(self):
v = Vgrid()
v.boilerplate_2D
self.assertIsInstance(v, Vgrid)

def test_write(self):
tmpdir = tempfile.TemporaryDirectory()
v = Vgrid()
v.write(pathlib.Path(tmpdir.name) / 'vgrid.in')
self.assertIsInstance(v, Vgrid)

def test_write_overwrite_raises(self):
tmpfile = tempfile.NamedTemporaryFile()
v = Vgrid()
self.assertRaises(
Exception,
v.write,
pathlib.Path(tmpfile.name)
)

0 comments on commit d016f57

Please sign in to comment.