Skip to content

Commit

Permalink
fix(SFRData.create_modflow_sfr2): if no model is specified but the SF…
Browse files Browse the repository at this point in the history
…R package is MODFLOW 2005-style and transient (for example, with inflows specified by stress period), add stress periods and model grid information to flopy model instance (that is created so that flopy can be used to write the SFR package).
  • Loading branch information
aleaf committed Sep 21, 2023
1 parent a5be8ee commit 48d10aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/tylerforks/inflows.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
end_datetime,inflow_m3d,SP,line_id_COMID
10/31/1990,15000.0000,0,1815035
11/30/1990,16000.0000,1,1815035
6 changes: 6 additions & 0 deletions examples/tylerforks/tf_sfrmaker_config2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ flowlines:
PlusFlowlineVAA: NHDPlus/NHDPlusAttributes/PlusFlowlineVAA.dbf
PlusFlow: NHDPlus/NHDPlusAttributes/PlusFlow.dbf
elevslope: NHDPlus/NHDPlusAttributes/elevslope.dbf
inflows: # see sfrmaker.data.add_to_perioddata for arguments
filename: inflows.csv
#segment_column: Segment
line_id_column: line_id_COMID
period_column: SP # column with model stress periods
data_column: inflow_m3d # column with flow values
dem:
filename: dem_26715.tif
elevation_units: meters
Expand Down
5 changes: 5 additions & 0 deletions sfrmaker/sfrdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,11 @@ def create_modflow_sfr2(self, model=None, const=None,
model_ws = '.' if model is None else model.model_ws
m = fm.Modflow(modelname=self.package_name, model_ws=model_ws,
structured=self.structured)
nper = self.segment_data['per'].max() + 1
if nper > 1:
dis = fm.ModflowDis(m, nrow=self.grid.nrow, ncol=self.grid.ncol,
nlay=self.grid.nlay, nper=nper)

if model is not None:
nper = 1
if 'tdis' in model.simulation.package_key_dict.keys():
Expand Down
22 changes: 22 additions & 0 deletions sfrmaker/test/test_from_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pytest
import gisutils
import sfrmaker
import flopy
from sfrmaker.fileio import read_mf6_block


Expand Down Expand Up @@ -179,3 +180,24 @@ def test_tylerforks_from_config(config_file, dem):
rd = sfrdata.reach_data.dropna(subset=['asum'], axis=0)
assert np.allclose(rd.strtop.values.min(), 1125, rtol=0.01)
assert sfrdata.grid.crs.srs == 'EPSG:26715'

# check that inflows were written correctly
if 'inflows' in cfg:
m1 = sfrdata.modflow_sfr2.parent
m2 = flopy.modflow.Modflow(model_ws='.')
nper = sfrdata.segment_data['per'].max() + 1
dis = flopy.modflow.ModflowDis(m2,
nrow=sfrdata.grid.nrow, ncol=sfrdata.grid.ncol,
nlay=sfrdata.grid.nlay, nper=nper)
sfr = flopy.modflow.ModflowSfr2.load('tf.sfr', m2)
for i in range(nper):
assert np.allclose(sfr.segment_data[i]['flow'],
sfrdata.modflow_sfr2.segment_data[i]['flow'])
original_inflow_data = pd.read_csv(cfg['inflows']['filename'])
for i in range(nper):
per_col = cfg['inflows']['period_column']
q_col = cfg['inflows']['data_column']
q_orig = original_inflow_data.loc[original_inflow_data[per_col] == i, q_col].values[0]
idx = np.where(sfr.segment_data[i]['flow'] > 0)[0][0]
q_input = sfr.segment_data[i]['flow'][idx]
assert np.allclose(q_input, q_orig)

0 comments on commit 48d10aa

Please sign in to comment.