Skip to content

Commit

Permalink
Contextmanager for mesh hfun constraints application
Browse files Browse the repository at this point in the history
  • Loading branch information
SorooshMani-NOAA committed Jan 17, 2024
1 parent f37b424 commit 318860d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ocsmesh/hfun/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import operator
import warnings
from collections import defaultdict
from contextlib import contextmanager
from typing import Union, Optional, Iterable, Literal
from multiprocessing import cpu_count, Pool
from time import time
Expand Down Expand Up @@ -111,6 +112,8 @@ def __init__(self, mesh: Mesh) -> None:
self._mesh = mesh
self._crs = mesh.crs
self._constraints = []
self._hold_const = False
self._hold_const_hit = 0

def msh_t(self) -> jigsaw_msh_t:
"""Return the size function specified on the underlying mesh
Expand Down Expand Up @@ -472,6 +475,18 @@ def add_feature(
self.mesh.msh_t.value = values


@contextmanager
def hold_applying_added_constratins(self):
try:
self._hold_const = True
yield
finally:
self._hold_const = False
if self._hold_const_hit > 0:
self.apply_added_constraints()
self._hold_const_hit = 0


def apply_added_constraints(self) -> None:
"""Apply all the added constraints
Expand All @@ -487,6 +502,9 @@ def apply_added_constraints(self) -> None:
None
"""

if self._hold_const:
self._hold_const_hit += 1
return
self.apply_constraints(self._constraints)


Expand Down
10 changes: 10 additions & 0 deletions tests/api/hfun.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,5 +1064,15 @@ def test_hfun_collector_fast(self):
self.assertTrue(np.isclose(np.mean(clipped_hfun.value), 1000, rtol=0.25))
self.assertTrue(np.isclose(np.mean(inv_clipped_hfun.value), 500, rtol=0.1))


class HfunConstraintApplyHold(unittest.TestCase):
def test_placeholder_rasterhfun(self):
self.assertTrue(False)


def test_placeholder_meshhfun(self):
self.assertTrue(False)


if __name__ == '__main__':
unittest.main()

0 comments on commit 318860d

Please sign in to comment.