我尝试使用flopy,因此我可以尝试使用python和modflow运行一些优化过程。 Modflow需要使用大量数据,我们使用不同的文件提供该信息。
我们提供输入和flopy运行modflow
我的麻烦在于,无论我给出什么输入,flopy似乎都会忽略输入文件并给出相同的结果。
以下是代码:
nper = 10
class BASreader:
def __init__(self):
self.ibound = None
self.head = None
with open("inps/bas/ibound", "r") as f:
data = f.read().replace("-", " -").split()
data = [int(x) for x in data]
data = np.array(data).reshape(1, 71, 24)
self.ibound = data
with open("inps/bas/head", "r") as f:
data = f.read().split()
data = [float(x) for x in data]
self.head = np.array(data).reshape(1, 71, 24)
class Modflow:
def __init__(self):
self.modelname = 'outs/gen1'
self.mf = flopy.modflow.Modflow(self.modelname, exe_name=r'G:\Program Files (x86)\Visual MODFLOW\mf2005.exe')
dis = flopy.modflow.ModflowDis.load("inps/yo.dis", self.mf)
basreader = BASreader()
bas = flopy.modflow.ModflowBas(self.mf, ibound=basreader.ibound, strt=basreader.head)
self.prev_headdata = basreader.head
wel = flopy.modflow.ModflowWel(self.mf, stress_period_data=WellProvider(nper).wells)
fname = 'inps/yo.evt'
fhandle = open(fname, 'r')
packages = []
ext_unit_dict = {22: flopy.utils.mfreadnam.NamData('EVT', fname, fhandle, packages)}
evt = flopy.modflow.ModflowEvt.load(fhandle, self.mf, ext_unit_dict=ext_unit_dict)
fhandle.close()
rech = {}
for x in range(nper):
rech[x] = 1.44e-5
rch = flopy.modflow.ModflowRch(self.mf, rech=rech)
stress_period_data = {}
for kper in range(nper):
for kstp in range(int(nper/10)):
stress_period_data[(kper, kstp)] = ['save head',
'save drawdown',
'save budget',
'print head',
'print drawdown',
'print budget']
oc = flopy.modflow.ModflowOc(self.mf, stress_period_data=stress_period_data, compact=True)
chd = flopy.modflow.ModflowChd.load("inps/yo.chd", self.mf)
lpf = flopy.modflow.ModflowLpf(self.mf, hk=14.44, vka=14.44, ipakcb=53, sy=0.22)
self.mf.write_input()
def run(self):
success, buff = self.mf.run_model(silent=True)
headobj = bf.HeadFile(self.modelname + '.hds')
newheaddata = headobj.get_alldata()[-1][0]
return newheaddata
mdflw = Modflow()
mdflw.run()
现在,即使我更改了EVT,RCH或WEL信息,结果也是一样的。 我甚至试图不包括上面的文件,结果仍然相同。 有什么指针吗?
答案 0 :(得分:1)
此脚本的修订版会导致不同的结果(因为使用了不同的地下水补给值)。在修改后的示例中,我将bcf2ss MODFLOW-2005输入文件用作基本文件。
不确定由于输入文件不可用而导致脚本无法运行的原因。
import os
import numpy as np
import flopy
class BASreader:
def __init__(self):
self.ibound = None
self.head = None
with open("inps/bas/ibound", "r") as f:
data = f.read().replace("-", " -").split()
data = [int(x) for x in data]
data = np.array(data).reshape(2, 10, 15)
self.ibound = data
with open("inps/bas/head", "r") as f:
data = f.read().split()
data = [float(x) for x in data]
self.head = np.array(data).reshape(2, 10, 15)
class Modflow:
def __init__(self, ws='outs', rech_val=.0040, wel_data=None):
self.model_ws = ws
self.modelname = 'gen1'
self.mf = flopy.modflow.Modflow(self.modelname, exe_name='mf2005',
model_ws=self.model_ws)
dis = flopy.modflow.ModflowDis.load("inps/yo.dis", self.mf, check=False)
nper = dis.nper
nstp = dis.nstp.array
basreader = BASreader()
bas = flopy.modflow.ModflowBas(self.mf, ibound=basreader.ibound,
strt=basreader.head)
self.prev_headdata = basreader.head
bcf = flopy.modflow.ModflowBcf.load("inps/yo.bc6", self.mf)
if wel_data is None:
wel_data = [[1, 2, 3, -35000.],
[1, 7, 3, -35000.]]
wel_spd = {1: wel_data}
wel = flopy.modflow.ModflowWel(self.mf,
stress_period_data=wel_spd)
riv = flopy.modflow.ModflowRiv.load('inps/yo.riv', self.mf, check=False)
rech = {}
for x in range(nper):
rech[x] = rech_val
rch = flopy.modflow.ModflowRch(self.mf, rech=rech)
pcg = flopy.modflow.ModflowPcg.load('inps/yo.pcg', self.mf)
stress_period_data = {}
for kper in range(nper):
for kstp in range(nstp[kper]):
stress_period_data[(kper, kstp)] = ['save head',
'save drawdown',
'save budget',
'print head',
'print drawdown',
'print budget']
oc = flopy.modflow.ModflowOc(self.mf,
stress_period_data=stress_period_data,
compact=True)
self.mf.write_input()
def run(self):
success, buff = self.mf.run_model(silent=True)
fpth = os.path.join(self.model_ws, self.modelname + '.hds')
headobj = flopy.utils.HeadFile(fpth)
newheaddata = headobj.get_data(idx=1)
return newheaddata
if __name__ == "__main__":
m1 = Modflow()
h1 = m1.run()
m2 = Modflow(rech_val=.0041)
h2 = m2.run()
assert np.array_equal(h1, h2), 'h1 does not equal h2'