In [1]:
import os
import sys
import shutil
import numpy as np
import flopy
print(sys.version)
print('numpy version: {}'.format(np.__version__))
print('flopy version: {}'.format(flopy.__version__))
In [2]:
# make a model
nlay,nrow,ncol = 10,20,5
model_ws = os.path.join("data","external_demo")
if os.path.exists(model_ws):
shutil.rmtree(model_ws)
# the place for all of your hand made and costly model inputs
array_dir = os.path.join("data","array_dir")
if os.path.exists(array_dir):
shutil.rmtree(array_dir)
os.mkdir(array_dir)
ml = flopy.modflow.Modflow(model_ws=model_ws)
dis = flopy.modflow.ModflowDis(ml,nlay=nlay,nrow=nrow,ncol=ncol,steady=False,nper=2)
make an hk
and vka
array. We'll save hk
to files - pretent that you spent months making this important model property. Then make an lpf
In [3]:
hk = np.zeros((nlay,nrow,ncol)) + 5.0
vka = np.zeros_like(hk)
fnames = []
for i,h in enumerate(hk):
fname = os.path.join(array_dir,"hk_{0}.ref".format(i+1))
fnames.append(fname)
np.savetxt(fname,h)
vka[i] = i+1
lpf = flopy.modflow.ModflowLpf(ml,hk=fnames,vka=vka)
Let's also have some recharge with mixed args as well. Pretend the recharge in the second stress period is very important and precise
In [4]:
warmup_recharge = np.ones((nrow,ncol))
important_recharge = np.random.random((nrow,ncol))
fname = os.path.join(array_dir,"important_recharge.ref")
np.savetxt(fname,important_recharge)
rch = flopy.modflow.ModflowRch(ml,rech={0:warmup_recharge,1:fname})
In [5]:
ml.write_input()
Let's look at the files that were created
In [6]:
print("model_ws:",ml.model_ws)
print('\n'.join(os.listdir(ml.model_ws)))
We see that a copy of the hk
files as well as the important recharge file were made in the model_ws
.Let's looks at the lpf
file
In [7]:
open(os.path.join(ml.model_ws,ml.name+".lpf"),'r').readlines()[:20]
Out[7]:
We see that the open/close
approach was used - this is because ml.array_free_format
is True
. Notice that vka
is written internally
In [8]:
ml.array_free_format
Out[8]:
Now change model_ws
In [9]:
print(ml.model_ws)
ml.model_ws = os.path.join("data","new_external_demo_dir")
Now when we call write_input()
, a copy of external files are made in the current model_ws
In [10]:
ml.write_input()
In [11]:
# list the files in model_ws that have 'hk' in the name
print('\n'.join([name for name in os.listdir(ml.model_ws) if "hk" in name or "impor" in name]))
In [12]:
# make a model - same code as before except for the model constructor
nlay,nrow,ncol = 10,20,5
model_ws = os.path.join("data","external_demo")
if os.path.exists(model_ws):
shutil.rmtree(model_ws)
# the place for all of your hand made and costly model inputs
array_dir = os.path.join("data","array_dir")
if os.path.exists(array_dir):
shutil.rmtree(array_dir)
os.mkdir(array_dir)
# lets make an external path relative to the model_ws
ml = flopy.modflow.Modflow(model_ws=model_ws, external_path="ref")
dis = flopy.modflow.ModflowDis(ml,nlay=nlay,nrow=nrow,ncol=ncol,steady=False,nper=2)
hk = np.zeros((nlay,nrow,ncol)) + 5.0
vka = np.zeros_like(hk)
fnames = []
for i,h in enumerate(hk):
fname = os.path.join(array_dir,"hk_{0}.ref".format(i+1))
fnames.append(fname)
np.savetxt(fname,h)
vka[i] = i+1
lpf = flopy.modflow.ModflowLpf(ml,hk=fnames,vka=vka)
warmup_recharge = np.ones((nrow,ncol))
important_recharge = np.random.random((nrow,ncol))
fname = os.path.join(array_dir,"important_recharge.ref")
np.savetxt(fname,important_recharge)
rch = flopy.modflow.ModflowRch(ml,rech={0:warmup_recharge,1:fname})
We can see that the model constructor created both model_ws
and external_path
which is _relative to the modelws
In [13]:
os.listdir(ml.model_ws)
Out[13]:
Now, when we call write_input()
, any array properties that were specified as np.ndarray
will be written externally. If a scalar was passed as the argument, the value remains internal to the model input files
In [14]:
ml.write_input()
open(os.path.join(ml.model_ws,ml.name+".lpf"),'r').readlines()[:20]
Out[14]:
Now, vka
was also written externally, but not the storage properties.Let's verify the contents of the external path directory. We see our hard-fought hk
and important_recharge
arrays, as well as the vka
arrays.
In [15]:
ml.lpf.ss.how = "internal"
ml.write_input()
open(os.path.join(ml.model_ws,ml.name+".lpf"),'r').readlines()[:20]
Out[15]:
In [16]:
print('\n'.join(os.listdir(os.path.join(ml.model_ws,ml.external_path))))
In [17]:
# make a model - same code as before except for the model constructor
nlay,nrow,ncol = 10,20,5
model_ws = os.path.join("data","external_demo")
if os.path.exists(model_ws):
shutil.rmtree(model_ws)
# the place for all of your hand made and costly model inputs
array_dir = os.path.join("data","array_dir")
if os.path.exists(array_dir):
shutil.rmtree(array_dir)
os.mkdir(array_dir)
# lets make an external path relative to the model_ws
ml = flopy.modflow.Modflow(model_ws=model_ws, external_path="ref")
# explicitly reset the free_format flag BEFORE ANY PACKAGES ARE MADE!!!
ml.array_free_format = False
dis = flopy.modflow.ModflowDis(ml,nlay=nlay,nrow=nrow,ncol=ncol,steady=False,nper=2)
hk = np.zeros((nlay,nrow,ncol)) + 5.0
vka = np.zeros_like(hk)
fnames = []
for i,h in enumerate(hk):
fname = os.path.join(array_dir,"hk_{0}.ref".format(i+1))
fnames.append(fname)
np.savetxt(fname,h)
vka[i] = i+1
lpf = flopy.modflow.ModflowLpf(ml,hk=fnames,vka=vka)
ml.lpf.ss.how = "internal"
warmup_recharge = np.ones((nrow,ncol))
important_recharge = np.random.random((nrow,ncol))
fname = os.path.join(array_dir,"important_recharge.ref")
np.savetxt(fname,important_recharge)
rch = flopy.modflow.ModflowRch(ml,rech={0:warmup_recharge,1:fname})
ml.write_input()
We see that now the external arrays are being handled through the name file. Let's look at the name file
In [18]:
open(os.path.join(ml.model_ws,ml.name+".nam"),'r').readlines()
Out[18]:
In [19]:
ml.dis.botm[0].format.binary = True
ml.write_input()
In [20]:
open(os.path.join(ml.model_ws,ml.name+".nam"),'r').readlines()
Out[20]:
In [21]:
open(os.path.join(ml.model_ws,ml.name+".dis"),'r').readlines()
Out[21]:
In [22]:
ml.lpf.hk[0].how
Out[22]:
This will raise an error since our model does not support free format...
In [23]:
ml.lpf.hk[0].how = "openclose"
ml.lpf.hk[0].how
ml.write_input()
So let's reset hk layer 1 back to external...
In [24]:
ml.lpf.hk[0].how = "external"
ml.lpf.hk[0].how
Out[24]:
In [25]:
ml.dis.top.how = "external"
In [26]:
ml.write_input()
In [27]:
open(os.path.join(ml.model_ws,ml.name+".dis"),'r').readlines()
Out[27]:
In [28]:
open(os.path.join(ml.model_ws,ml.name+".lpf"),'r').readlines()
Out[28]:
In [29]:
open(os.path.join(ml.model_ws,ml.name+".nam"),'r').readlines()
Out[29]:
In [ ]: