Skip to content

Commit

Permalink
refactor(discretization.fill_cells_vertically): only return bottom ar…
Browse files Browse the repository at this point in the history
…ray, since top shouldn't be modified; refactor(sourcedata.setup_array): move resetting of model top to branch where the top modified by supplied lake bathymetry (since this should be the only situation where this is needed)
  • Loading branch information
aleaf committed Sep 23, 2024
1 parent cc70d27 commit f1a8683
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
6 changes: 3 additions & 3 deletions mfsetup/discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def fill_cells_vertically(top, botm):
filled += np.nanmin(all_surfaces, axis=0) # botm[-1]
# append the model bottom elevations
filled = np.append(filled, [np.nanmin(all_surfaces, axis=0)], axis=0)
return filled[0].copy(), filled[1:].copy()
return filled[1:].copy()


def fix_model_layer_conflicts(top_array, botm_array,
Expand Down Expand Up @@ -748,6 +748,6 @@ def voxels_to_layers(voxel_array, z_edges, model_top=None, model_botm=None, no_d

# finally, fill any remaining nans with next layer elevation (going upward)
# might still have nans in areas where there are no voxel values, but model top and botm values
top, botm = fill_cells_vertically(layers[0], layers[1:])
layers = np.vstack([np.reshape(top, (1, *top.shape)), botm])
botm = fill_cells_vertically(layers[0], layers[1:])
layers = np.vstack([np.reshape(layers[0], (1, *layers[0].shape)), botm])
return layers
53 changes: 25 additions & 28 deletions mfsetup/sourcedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,29 @@ def setup_array(model, package, var, data=None,
if model.version == 'mf6':
# reset the model top to the lake bottom
top[bathy != 0] -= bathy[bathy != 0]

# update the top in the model
# todo: refactor this code to be consolidated with _set_idomain and setup_dis
model._setup_array('dis', 'top',
data={0: top},
datatype='array2d', resample_method='linear',
write_fmt='%.2f', dtype=float)
if hasattr(model, 'dis') and model.dis is not None:
if model.version == 'mf6':
model.dis.top = model.cfg['dis']['griddata']['top']
else:
model.dis.top = model.cfg['dis']['top'][0]

# write the top array again
top_filepath = model.setup_external_filepaths(package, 'top',
model.cfg[package]['top_filename_fmt'])[0]
save_array(top_filepath, top,
nodata=write_nodata,
fmt=write_fmt)
if model.version == 'mf6':
src = filepaths[i]['filename']
dst = model.cfg['intermediate_data'][var][i]
shutil.copy(src, dst)
# if loading the model; use the model top that was just loaded in
else:
top_filename = model.cfg['dis']['griddata'].get('top')
Expand Down Expand Up @@ -1502,19 +1525,8 @@ def setup_array(model, package, var, data=None,
raise Exception('Model layers less than {} {} thickness'.format(min_thickness,
model.length_units))
# fill any nan values that are above or below active cells to avoid cell thickness errors
top, botm = fill_cells_vertically(top, botm)
# the top may have been modified by fill_cells_vertically
# update the top in the model
# todo: refactor this code to be consolidated with _set_idomain and setup_dis
model._setup_array('dis', 'top',
data={0: top},
datatype='array2d', resample_method='linear',
write_fmt='%.2f', dtype=float)
if hasattr(model, 'dis') and model.dis is not None:
if model.version == 'mf6':
model.dis.top = model.cfg['dis']['griddata']['top']
else:
model.dis.top = model.cfg['dis']['top'][0]
botm = fill_cells_vertically(top, botm)

data = {i: arr for i, arr in enumerate(botm)}

# special case of LGR models
Expand Down Expand Up @@ -1613,21 +1625,6 @@ def setup_array(model, package, var, data=None,
dst = model.cfg['intermediate_data'][var][i]
shutil.copy(src, dst)

# write the top array again, because top was filled
# with botm array above
# NOTE: that tends to corrupt the top array,
# is that only applicable to simulate_high_k_lakes ?)
if var == 'botm' and apply_lake_bathymetry_to_model_top:
top_filepath = model.setup_external_filepaths(package, 'top',
model.cfg[package]['top_filename_fmt'])[0]
save_array(top_filepath, top,
nodata=write_nodata,
fmt=write_fmt)
if model.version == 'mf6':
src = filepaths[i]['filename']
dst = model.cfg['intermediate_data'][var][i]
shutil.copy(src, dst)


def get_source_data_file_ext(cfg_data, package, var):
if 'filenames' in cfg_data:
Expand Down
4 changes: 2 additions & 2 deletions mfsetup/tests/test_discretization.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_fill_na(all_layers):
botm[-2] = 2
botm[-2, 2, 2] = np.nan

top, botm = fill_cells_vertically(top, botm)
botm = fill_cells_vertically(top, botm)
filled = all_layers.copy()
filled[0] = top
filled[1:] = botm
Expand All @@ -134,7 +134,7 @@ def test_make_ibound(all_layers):
botm[-2] = 2
botm[-2, 2, 2] = np.nan
botm[:, 3, 3] = np.arange(1, 10)[::-1] # column of cells all eq. to min thickness
filled_top, filled_botm = fill_cells_vertically(top, botm)
filled_botm = fill_cells_vertically(top, botm)
ibound = make_ibound(top, botm, nodata=nodata,
minimum_layer_thickness=1,
drop_thin_cells=True,
Expand Down

0 comments on commit f1a8683

Please sign in to comment.