Skip to content

Commit

Permalink
Merge pull request #562 from matthewhoffman/landice/subdomain_extractor
Browse files Browse the repository at this point in the history
Create test for extracting a MALI subdomain from a larger domain

This merge adds a test case for a subdomain extractor. It allows a regional domain to be quickly created from an existing whole-ice-sheet domain using a mask. The regional mask can come from a region_mask MPAS netCDF file or a geojson file. Optionally, ancillary files (e.g. forcing or parameter files) can be remapped at the same time.
  • Loading branch information
matthewhoffman authored Feb 28, 2024
2 parents 4b6f369 + b9ec3ac commit 11aa8ae
Show file tree
Hide file tree
Showing 11 changed files with 656 additions and 0 deletions.
2 changes: 2 additions & 0 deletions compass/landice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from compass.landice.tests.isunnguata_sermia import IsunnguataSermia
from compass.landice.tests.kangerlussuaq import Kangerlussuaq
from compass.landice.tests.koge_bugt_s import KogeBugtS
from compass.landice.tests.mesh_modifications import MeshModifications
from compass.landice.tests.mismipplus import MISMIPplus
from compass.landice.tests.thwaites import Thwaites
from compass.mpas_core import MpasCore
Expand Down Expand Up @@ -46,5 +47,6 @@ def __init__(self):
self.add_test_group(IsunnguataSermia(mpas_core=self))
self.add_test_group(Kangerlussuaq(mpas_core=self))
self.add_test_group(KogeBugtS(mpas_core=self))
self.add_test_group(MeshModifications(mpas_core=self))
self.add_test_group(MISMIPplus(mpas_core=self))
self.add_test_group(Thwaites(mpas_core=self))
56 changes: 56 additions & 0 deletions compass/landice/mesh.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import time

import jigsawpy
Expand All @@ -12,6 +13,61 @@
from netCDF4 import Dataset


def mpas_flood_fill(seed_mask, grow_mask, cellsOnCell, nEdgesOnCell,
grow_iters=sys.maxsize):
"""
Flood-fill for mpas meshes using mpas cells.
Parameters
----------
seed_mask : numpy.ndarray
Integer array of locations from which to flood fill
0 = invalid, 1 = valid
grow_mask : numpy.ndarray
Integer array of locations valid for growing into
0 = invalid, 1 = valid
cellsOnCell : numpy.ndarray
cellsOnCell array from the mpas mesh
nEdgesOnCell : numpy.ndarray
nEdgesOnCell array from the mpas mesh
grow_iters : integer
optional argument limiting the number of iterations
over which to extend the mask
Returns
-------
keep_mask : numpy.ndarray
mask calculated by the flood fill routine,
where cells connected to seed_mask
are 1 and everything else is 0.
"""

iter = 0
keep_mask = seed_mask.copy()
n_mask_cells = keep_mask.sum()
for iter in range(grow_iters):
mask_ind = np.nonzero(keep_mask == 1)[0]
print(f'iter={iter}, keep_mask size={keep_mask.sum()}')
new_keep_mask = keep_mask.copy()
for iCell in mask_ind:
neighs = cellsOnCell[iCell, :nEdgesOnCell[iCell]] - 1
neighs = neighs[neighs >= 0] # drop garbage cell
for jCell in neighs:
if grow_mask[jCell] == 1:
new_keep_mask[jCell] = 1
keep_mask = new_keep_mask.copy()
n_mask_cells_new = keep_mask.sum()
if n_mask_cells_new == n_mask_cells:
break
n_mask_cells = n_mask_cells_new
iter += 1
return keep_mask


def gridded_flood_fill(field, iStart=None, jStart=None):
"""
Generic flood-fill routine to create mask of connected elements
Expand Down
19 changes: 19 additions & 0 deletions compass/landice/tests/mesh_modifications/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from compass.landice.tests.mesh_modifications.subdomain_extractor import (
SubdomainExtractor,
)
from compass.testgroup import TestGroup


class MeshModifications(TestGroup):
"""
A test group for automating modifications to existing meshes
"""
def __init__(self, mpas_core):
"""
mpas_core : compass.landice.Landice
the MPAS core that this test group belongs to
"""
super().__init__(mpas_core=mpas_core,
name='mesh_modifications')

self.add_test_case(SubdomainExtractor(test_group=self))
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sys
from importlib import resources

import numpy as np

from compass.landice.tests.mesh_modifications.subdomain_extractor.extract_region import ( # noqa
ExtractRegion,
)
from compass.testcase import TestCase


class SubdomainExtractor(TestCase):
"""
A class for a test case that extracts a subdomain from a larger domain
"""

def __init__(self, test_group):
"""
Create the test case
Parameters
----------
test_group : compass.landice.tests.mesh_modifications.MeshModifications
The test group that this test case belongs to
"""
name = 'subdomain_extractor'
super().__init__(test_group=test_group, name=name)

self.add_step(ExtractRegion(test_case=self))

# no configure() method is needed

# no run() method is needed

# no validate() method is needed
Loading

0 comments on commit 11aa8ae

Please sign in to comment.