def dis(self): delrc = 200 ztop = self.df_mf['MODEL_TOP'].values.reshape(self.nrows, self.ncols) botms = self.df_mf.filter(like='BOT').T.values.reshape( self.nlays, self.nrows, self.ncols) # prj = NAD_1983_2011_UTM_Zone_18N dis = flomf.ModflowDis(self.mf, self.nlays, self.nrows, self.ncols, delr=delrc, delc=delrc, botm=botms, top=ztop, nper=self.kpers, steady=self.steady, rotation=19, xul=400232.64777, yul=4072436.50165, proj4_str='EPSG:6347', start_datetime=self.params.get( 'START_DATE', '06/30/2011')) return dis
def test_mflist_reference(): import os import numpy as np import shapefile import flopy.modflow as fmf # model_ws = os.path.join('..', 'data', 'freyberg') # ml = fmf.Modflow.load('freyberg.nam', model_ws=model_ws) # make the model ml = fmf.Modflow() assert isinstance(ml, fmf.Modflow) perlen = np.arange(1, 20, 1) nstp = np.flipud(perlen) + 3 tsmult = 1.2 nlay = 10 nrow, ncol = 50, 40 botm = np.arange(0, -100, -10) hk = np.random.random((nrow, ncol)) dis = fmf.ModflowDis(ml, delr=100.0, delc=100.0, nrow=nrow, ncol=ncol, nlay=nlay, nper=perlen.shape[0], perlen=perlen, nstp=nstp, tsmult=tsmult, top=10, botm=botm, steady=False) assert isinstance(dis, fmf.ModflowDis) lpf = fmf.ModflowLpf(ml, hk=hk, vka=10.0, laytyp=1) assert isinstance(lpf, fmf.ModflowLpf) pcg = fmf.ModflowPcg(ml) assert isinstance(pcg, fmf.ModflowPcg) oc = fmf.ModflowOc(ml) assert isinstance(oc, fmf.ModflowOc) ibound = np.ones((nrow, ncol)) ibound[:, 0] = -1 ibound[25:30, 30:39] = 0 bas = fmf.ModflowBas(ml, strt=5.0, ibound=ibound) assert isinstance(bas, fmf.ModflowBas) rch = fmf.ModflowRch(ml, rech={0: 0.00001, 5: 0.0001, 6: 0.0}) assert isinstance(rch, fmf.ModflowRch) wel_dict = {} wel_data = [[9, 25, 20, -200], [0, 0, 0, -400], [5, 20, 32, 500]] wel_dict[0] = wel_data wel_data2 = [[45, 20, 200], [9, 49, 39, 400], [5, 20, 32, 500]] wel_dict[10] = wel_data2 wel = fmf.ModflowWel(ml, stress_period_data={0: wel_data}) assert isinstance(wel, fmf.ModflowWel) ghb_dict = {0: [1, 10, 10, 400, 300]} ghb = fmf.ModflowGhb(ml, stress_period_data=ghb_dict) assert isinstance(ghb, fmf.ModflowGhb) test = os.path.join('temp', 'test3.shp') ml.export(test, kper=0) shp = shapefile.Reader(test) assert shp.numRecords == nrow * ncol
def setup_class(cls): """Make a modflow model.""" print("setting up model...") t0 = time.time() size = 100 nlay = 10 nper = 10 nsfr = int((size**2) / 5) cls.modelname = "junk" cls.model_ws = "temp/t064" external_path = "external/" if not os.path.isdir(cls.model_ws): os.makedirs(cls.model_ws) if not os.path.isdir(os.path.join(cls.model_ws, external_path)): os.makedirs(os.path.join(cls.model_ws, external_path)) m = fm.Modflow(cls.modelname, model_ws=cls.model_ws, external_path=external_path) dis = fm.ModflowDis( m, nper=nper, nlay=nlay, nrow=size, ncol=size, top=nlay, botm=list(range(nlay)), ) rch = fm.ModflowRch( m, rech={k: 0.001 - np.cos(k) * 0.001 for k in range(nper)}) ra = fm.ModflowWel.get_empty(size**2) well_spd = {} for kper in range(nper): ra_per = ra.copy() ra_per["k"] = 1 ra_per["i"] = ((np.ones((size, size)) * np.arange(size)).transpose().ravel().astype(int)) ra_per["j"] = list(range(size)) * size well_spd[kper] = ra wel = fm.ModflowWel(m, stress_period_data=well_spd) # SFR package rd = fm.ModflowSfr2.get_empty_reach_data(nsfr) rd["iseg"] = range(len(rd)) rd["ireach"] = 1 sd = fm.ModflowSfr2.get_empty_segment_data(nsfr) sd["nseg"] = range(len(sd)) sfr = fm.ModflowSfr2(reach_data=rd, segment_data=sd, model=m) cls.init_time = time.time() - t0 cls.m = m
def test_mflist_external(): import flopy.modflow as fmf ml = fmf.Modflow("mflist_test", model_ws="temp", external_path="ref") dis = fmf.ModflowDis(ml, 1, 10, 10, nper=3, perlen=1.0) wel_data = { 0: [[0, 0, 0, -1], [1, 1, 1, -1]], 1: [[0, 0, 0, -2], [1, 1, 1, -1]] } wel = fmf.ModflowWel(ml, stress_period_data=wel_data) ml.write_input() ml1 = fmf.Modflow.load("mflist_test.nam", model_ws=ml.model_ws, verbose=True, forgive=False) assert np.array_equal(ml.wel[0], ml1.wel[0]) assert np.array_equal(ml.wel[1], ml1.wel[1])
z0[0, 0, 30:38] = np.arange(-2.5, -40, -5) z0[0, 0, 38:] = -40 z1[0, 0, 22:30] = np.arange(-2.5, -40, -5) z1[0, 0, 30:] = -40 z = [] z.append(z0) z.append(z1) ssz = 0.2 isource = np.ones((nrow, ncol), 'int') isource[0, 0] = 2 # stratified model modelname = 'swiex2_strat' print('creating...', modelname) ml = mf.Modflow(modelname, version='mf2005', exe_name=mf_name, model_ws=dirs[0]) discret = mf.ModflowDis(ml, nlay=1, ncol=ncol, nrow=nrow, delr=delr, delc=1, top=0, botm=[-40.0], nper=nper, perlen=perlen, nstp=nstp) bas = mf.ModflowBas(ml, ibound=ibound, strt=0.05) bcf = mf.ModflowBcf(ml, laycon=0, tran=2 * 40) swi = mf.ModflowSwi2(ml, nsrf=nsurf, istrat=1, toeslope=0.2, tipslope=0.2, nu=[0, 0.0125, 0.025], zeta=z, ssz=ssz, isource=isource, nsolver=1) oc = mf.ModflowOc88(ml, save_head_every=1000) pcg = mf.ModflowPcg(ml) ml.write_input() # run stratified model if not skipRuns: m = ml.run_model(silent=False) # read stratified results zetafile = os.path.join(dirs[0], '{}.zta'.format(modelname)) zobj = fu.CellBudgetFile(zetafile) zkstpkper = zobj.get_kstpkper() zeta = zobj.get_data(kstpkper=zkstpkper[-1], text=' ZETASRF 1')[0]
isource = np.ones((nrow, ncol), np.int) isource[0, 0] = 2 ocdict = {} for idx in range(49, 200, 50): key = (0, idx) ocdict[key] = ['save head', 'save budget'] key = (0, idx+1) ocdict[key] = [] # create flopy modflow object ml = mf.Modflow(modelname, version='mf2005', exe_name=exe_name) # create flopy modflow package objects discret = mf.ModflowDis(ml, nlay=nlay, nrow=nrow, ncol=ncol, delr=delr, delc=delc, top=0, botm=[-40.0], perlen=400, nstp=200) bas = mf.ModflowBas(ml, ibound=ibound, strt=0.0) lpf = mf.ModflowLpf(ml, hk=2., vka=2.0, vkcb=0, laytyp=0, layavg=0) wel = mf.ModflowWel(ml, stress_period_data={0:[(0, 0, 0, 1)]}) swi = mf.ModflowSwi2(ml, npln=1, istrat=1, toeslope=0.2, tipslope=0.2, nu=[0, 0.025], zeta=z, ssz=0.2, isource=isource, nsolver=1) oc = mf.ModflowOc(ml, stress_period_data=ocdict) pcg = mf.ModflowPcg(ml) # create model files ml.write_input() # run the model m = ml.run_model(silent=False) # read model heads headfile = '{}.hds'.format(modelname) hdobj = fu.HeadFile(headfile)
def main(): ''' This is the main function. ''' # Package all the required file paths into the Paths object mfPaths = Paths() # Package all the required framework specifications into the mfFrame object mfFrame = Frame(Paths=mfPaths, dx_dy=dx_dy) if build_from_gis: # Build the model framework ASCII files from the GIS layers. Note that this # requires a GDAL installation. If you don't want to get into that you # can skip this step and simply build the model from the ASCII files that I've # already created. mfFrame.build_frame(Paths=mfPaths) # --------------------------------------------- # --------------------------------------------- # Now use Flopy to build the MODFLOW model packages # --------------------------------------------- # --------------------------------------------- start_dir = os.getcwd() os.chdir(mfPaths.modflow_dir ) # This is simplest if done inside the MODFLOW directory # Initialize a Flopy model object. This is the base class around which the model # packages are built. Modflow = mf.Modflow(mfFrame.model_name, external_path='./', version=mfPaths.mf_version) # The .oc ('output control') package specifies how the model output is written. # This model includes a single steady state stress period. Save the # distribution of heads as well as the flow budget/mass balance to binaries. # These can be plotted or converted to rasters (the current version of the script # doesn't do any post-processing.) oc = mf.ModflowOc(Modflow, stress_period_data={ (0, 0): ['SAVE HEAD', 'SAVE BUDGET'] }) # The .dis and .bas packages define the model framework. I've already defined # the framework attributes using the mfFrame object and simply pass those # attributes to the constructor. dis = mf.ModflowDis(Modflow,mfFrame.nlay,mfFrame.nrow,mfFrame.ncol,\ delr=mfFrame.delr,delc=mfFrame.delc,\ top=mfFrame.top,botm=mfFrame.bottom) bas = mf.ModflowBas(Modflow, ibound=mfFrame.ibound, strt=mfFrame.top, hnoflo=mfFrame.hnoflo) # The .upw package describes the system properties (e.g., transmissivity/conductivity). # For this model I simply give it a constant hydraulic conductivity field. This model # converges but I have no idea how physically realistic it is. If you would # like to make it more physically realistic (e.g., try to fit head or discharge # data) you would need to estimate the hydraulic conductivity field via # calibration/inverse modeling hk = np.ones(np.shape(mfFrame.ibound)) upw = mf.ModflowUpw(Modflow, laytyp=mfFrame.laytyp, hk=hk, ipakcb=53) # The .nwt package defines the solver specs. Just use the defaults. nwt = mf.ModflowNwt(Modflow) # RECHARGE INPUTS TO THE SYSTEM # ----------------------------- # The .rch packages specifies recharge/precipitation inputs to the water table. # Remember that I have already generated an array from the GIS layer and attached # it to the mfFrame object. rch = mf.ModflowRch(Modflow, nrchop=3, rech={0: mfFrame.rch}, ipakcb=53) # BASEFLOW DISCHARGE FROM THE SYSTEM # ---------------------------------- # The .drn package is one method of simulating the discharge of groundwater as # base-flow in streams in rivers. Define every landsurface cell as a drain # in order to allow the discharge network to emerge from topography. drn_stages = mfFrame.top drn_stages[mfFrame.ibound.squeeze() <= 0] = np.nan drn_input = build_drain_input(mfFrame=mfFrame, stages=drn_stages) drn = mf.ModflowDrn(Modflow, stress_period_data=drn_input, ipakcb=53) # Now write the files. Flopy can also run the model if you tell it where the # binary is, but if I understood your method correctly you will be invoking something # from hydroshare. For convenience I am writing a windows .bat file that # can be used to run the model. Modflow.write_input() os.chdir(start_dir) with open(mfPaths.mf_bat_file, 'w') as fout: fout.write('%s %s' % (binary_path, os.path.basename(mfPaths.nam_file))) folder = mfPaths.scratch_dir for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except Exception as e: print(e) folder = mfPaths.model_frame_dir for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except Exception as e: print(e) folder = mfPaths.data_dir for the_file in os.listdir(folder): file_path = os.path.join(folder, the_file) try: if os.path.isfile(file_path): os.unlink(file_path) except Exception as e: print(e) return
excel_filename=excel_filename, Qmult=1, imb=imb, run_length=rl, years=years, startwinter=startwinter, temp_assigned=temp_assigned) # calculates flows during simulation ''' MODFLOW INPUT files''' # set all the fixed conditions that don't change while running etc. ml = mf.Modflow(name, version='mf2005', exe_name=swtexe_name, model_ws=dirs[0]) discret = mf.ModflowDis(ml, nrow=int(grid_obj.nrow), ncol=int(grid_obj.ncol), nlay=grid_obj.nlay, delr=grid_obj.delr, delc=grid_obj.delc, laycbd=0., top=grid_obj.top, botm=grid_obj.botm, nper=nper, perlen=perlen, nstp=nstp, steady=steady) lpf = mf.ModflowLpf(ml, hk=grid_obj.HK, vka=grid_obj.VK, ss=grid_obj.ss, ipakcb=53, sy=0.15, laytyp=laytyp, layavg=0.)
twords = [i + 1, j + 1] if savebudget == True: twords.append('pbudget') if savehead == True: twords.append('head') savewords.append(twords) solver2params = {'mxiter': 100, 'iter1': 20, 'npcond': 1, 'zclose': 1.0e-6, 'rclose': 3e-3, 'relax': 1.0, 'nbpol': 2, 'damp': 1.0, 'dampt': 1.0} # --create model file and run model modelname = 'swi2ex5' mf_name = 'mf2005' if not skipRuns: ml = mf.Modflow(modelname, version='mf2005', exe_name=mf_name, model_ws=dirs[0]) discret = mf.ModflowDis(ml, nrow=nrow, ncol=ncol, nlay=nlay, delr=delr, delc=delc, top=0, botm=bot, laycbd=0, nper=nper, perlen=perlen, nstp=nstp, steady=steady) bas = mf.ModflowBas(ml, ibound=ibound, strt=ihead) lpf = mf.ModflowLpf(ml, hk=kh, vka=kv, ss=ss, sy=ssz, vkcb=0, laytyp=0, layavg=1) wel = mf.ModflowWel(ml, stress_period_data=well_data) swi = mf.ModflowSwi2(ml, npln=1, istrat=1, toeslope=0.025, tipslope=0.025, nu=[0, 0.025], \ zeta=z, ssz=ssz, isource=isource, nsolver=2, solver2params=solver2params) oc = mf.ModflowOc(ml, words=savewords) pcg = mf.ModflowPcg(ml, hclose=1.0e-6, rclose=3.0e-3, mxiter=100, iter1=50) # --write the modflow files ml.write_input() m = ml.run_model(silent=False) # --read model zeta get_stp = [364, 729, 1094, 1459, 364, 729, 1094, 1459] get_per = [0, 0, 0, 0, 1, 1, 1, 1]
lrchc[:, [0, 1, 3, 4]] = [0, 0, 0., 0.8 / 2.0] lrchc[:, 2] = np.arange(0, 30) #--swi2 data zini = np.hstack( (-9 * np.ones(24), np.arange(-9, -50, -0.5), -50 * np.ones(94)))[np.newaxis, :] iso = np.zeros((1, 200), dtype=np.int) iso[:, :30] = -2 #--model objects ml = mf.Modflow(modelname, version='mf2005', exe_name=exe_name) discret = mf.ModflowDis(ml, nrow=nrow, ncol=ncol, nlay=3, delr=delr, delc=delc, laycbd=[0, 0, 0], top=-9.0, botm=[-29, -30, -50], nper=2, perlen=[365 * 1000, 1000 * 365], nstp=[500, 500]) bas = mf.ModflowBas(ml, ibound=1, strt=1.0) bcf = mf.ModflowBcf(ml, laycon=[0, 0, 0], tran=[40.0, 1, 80.0], vcont=[0.005, 0.005]) wel = mf.ModflowWel(ml, stress_period_data={0: lrcQ1, 1: lrcQ2}) ghb = mf.ModflowGhb(ml, stress_period_data={0: lrchc}) swi = mf.ModflowSwi2(ml, nsrf=1, istrat=1,
path_to_mf2005 = 'C:/Users/Sam/Dropbox/Work/Models/MODFLOW/MF2005.1_12/bin/mf2005.exe' #SCZ model = fpm.Modflow(modelname='gwexample', exe_name=path_to_mf2005) #SCZ #model = fpm.Modflow(modelname = 'gwexample') #3. The discretization of the model is specified with the #discretization file (DIS) of MODFLOW. The aquifer #is divided into 201 cells of length 10m and width 1 m. #The first input of the discretization package is the name #of the model object. All other input arguments are self #explanatory. fpm.ModflowDis(model, nlay=1, nrow=1, ncol=201, delr=10, delc=1, top=50, botm=0) #Active cells and the like are defined with the Basic #package (BAS), which is required for every MODFLOW #model. It contains the ibound array, which #is used to specify which cells are active (value is #positive), inactive (value is 0), or fixed head (value #is negative). The numpy package (aliased as np) can #be used to quickly initialize the ibound array with #values of 1, and then set the ibound value for the #first and last columns to -1. The numpy package #(and Python, in general) uses zero-based indexing and #supports negative indexing so that row 1 and column
delRow = delCol = L / (N - 1) # Number of timePeriods nPer = 100 # Set steady of all periods to false steady = np.zeros(100) # Instantiate the discretization object dis = mf.ModflowDis(ml, nlay=NLay, nrow=N, ncol=N, delr=delRow, delc=delCol, top=0.0, botm=bot, laycbd=0, lenuni=2, itmuni=4, steady=steady, nper=nPer, perlen=1) # helping-variable NHalf = int((N - 1) / 2) # iBound-Configuration iBound = np.ones((NLay, N, N)) # Set all elements in the first row to -1 iBound[:, 0, :] = -1
delc = Ly / nrow # column width of cell, in m # define the stress periods nper = 2 ts = 1 # length of time step, in years nstp = 30 # number of time steps perlen = nstp * ts # length of simulation, in years steady = True # steady state or transient dis = mf.ModflowDis(m, nlay, nrow, ncol, delr=delr, delc=delc, top=ztop, botm=botm, nper=nper, perlen=perlen, nstp=nstp, steady=steady, itmuni=5) # hydraulic parameters (aquifer properties with the bcf-package) hy = 36500 # hydraulic conductivity sf = 0.25 # storage coefficient laycon = 1 # layer type, confined (0), unconfined (1), constant T, variable S (2), variable T, variable S (default is 3) bcf = mf.ModflowBcf(m, hy=hy, sf1=sf, laycon=1) # BAS package (Basic Package) ibound = np.ones((nlay, nrow, ncol)) # # active cells
def calculate_model(z1_hk, z2_hk, z3_hk): # Z1_hk = 15 # 3<Z1_hk<15 # Z2_hk = 15 # 3<Z2_hk<15 # Z3_hk = 3 # 3<Z3_hk<15 hobs = [ [0, 20, 10, 69.52], [0, 40, 10, 71.44], [0, 60, 10, 72.99], [0, 80, 10, 73.86], [0, 20, 45, 58.73], [0, 40, 45, 50.57], [0, 60, 45, 54.31], [0, 80, 45, 58.06], [0, 20, 80, 56.31], [0, 40, 80, 52.32], [0, 60, 80, 46.35], [0, 80, 80, 29.01], [0, 20, 100, 57.24], [0, 40, 100, 54.24], [0, 60, 100, 39.48], [0, 80, 100, 48.47], ] model_path = os.path.join('_model') if os.path.exists(model_path): shutil.rmtree(model_path) modelname = 'parEstMod' version = 'mf2005' exe_name = 'mf2005' if platform.system() == 'Windows': exe_name = 'mf2005.exe' ml = mf.Modflow(modelname=modelname, exe_name=exe_name, version=version, model_ws=model_path) nlay = 1 nrow = 90 ncol = 120 area_width_y = 9000 area_width_x = 12000 delc = area_width_x / ncol delr = area_width_y / nrow nper = 1 top = 100 botm = 0 dis = mf.ModflowDis(ml, nlay=nlay, nrow=nrow, ncol=ncol, delr=delr, delc=delc, top=top, botm=botm, nper=nper, steady=True) ibound = 1 strt = 100 bas = mf.ModflowBas(ml, ibound=ibound, strt=strt) mask_arr = np.zeros((nlay, nrow, ncol)) mask_arr[:, :, 0] = 80 mask_arr[:, :, -1] = 60 ghb_spd = {0: []} for layer_id in range(nlay): for row_id in range(nrow): for col_id in range(ncol): if mask_arr[layer_id][row_id][col_id] > 0: ghb_spd[0].append([ layer_id, row_id, col_id, mask_arr[layer_id][row_id][col_id], 200 ]) ghb = mf.ModflowGhb(ml, stress_period_data=ghb_spd) rch = 0.0002 rech = {} rech[0] = rch rch = mf.ModflowRch(ml, rech=rech, nrchop=3) welSp = {} welSp[0] = [ [0, 20, 20, -20000], [0, 40, 40, -20000], [0, 60, 60, -20000], [0, 80, 80, -20000], [0, 60, 100, -20000], ] wel = mf.ModflowWel(ml, stress_period_data=welSp) hk = np.zeros((nlay, nrow, ncol)) hk[:, :, 0:40] = z1_hk hk[:, :, 40:80] = z2_hk hk[:, :, 80:120] = z3_hk lpf = mf.ModflowLpf(ml, hk=hk, layavg=0, layvka=0, sy=0.3, ipakcb=53) pcg = mf.ModflowPcg(ml, rclose=1e-1) oc = mf.ModflowOc(ml) ml.write_input() ml.run_model(silent=True) hds = fu.HeadFile(os.path.join(model_path, modelname + '.hds')) times = hds.get_times() response = [] for hob in hobs: observed = hob[3] calculated = hds.get_data(totim=times[-1])[hob[0]][hob[1]][hob[2]] response.append(observed - calculated) return json.dumps(response)
iswiobs = 1051 modelname = 'swiex4_s1' if not skipRuns: ml = mf.Modflow(modelname, version='mf2005', exe_name=exe_name, model_ws=workspace) discret = mf.ModflowDis(ml, nlay=nlay, nrow=nrow, ncol=ncol, laycbd=0, delr=delr, delc=delc, top=botm[0], botm=botm[1:], nper=nper, perlen=perlen, nstp=nstp, steady=steady) bas = mf.ModflowBas(ml, ibound=ibound, strt=ihead) lpf = mf.ModflowLpf(ml, laytyp=laytyp, hk=hk, vka=vka) wel = mf.ModflowWel(ml, stress_period_data=base_well_data) ghb = mf.ModflowGhb(ml, stress_period_data=ghb_data) rch = mf.ModflowRch(ml, rech=rch_data) swi = mf.ModflowSwi2(ml, nsrf=1, istrat=1, toeslope=toeslope,
DRN = gg.set_DRN(gr, IBOUND) SEEP = gg.set_seepage(gr, NPER) OC = {(0, 0): ['save head', 'save budget', 'print budget'] for i in range(NPER)} #%% MODEL AND packages ADDED TO IT mf = fm.Modflow(modelname, exe_name=exe_name) dis = fm.ModflowDis(mf, gr.nlay, gr.ny, gr.nx, delr=gr.dx, delc=gr.dy, top=gr.Ztop[0], botm=gr.Zbot, laycbd=gr.LAYCBD, nper=NPER, perlen=PERLEN, nstp=NSTP, steady=STEADY) bas = fm.ModflowBas(mf, ibound=IBOUND, strt=STRTHD) lpf = fm.ModflowLpf(mf, hk=HK, vka=VKA, chani=[1.e-20, 1.e-20], sy=SY, ss=SS, laytyp=LAYTYP, vkcb=VKCB,
(0, 0): [ 'save head', 'save drawdown', 'save budget', 'print head', 'print budget' ] } #%% MODEL AND packages ADDED TO IT mf = fm.Modflow(modelname, exe_name=exe_name) dis = fm.ModflowDis(mf, Nlay, Ny, Nx, delr=dx, delc=dy, top=zTop, botm=zBot, laycbd=LAYCBD, nper=NPER, perlen=PERLEN, nstp=NSTP, steady=STEADY) bas = fm.ModflowBas(mf, ibound=IBOUND, strt=STRTHD) lpf = fm.ModflowLpf(mf, hk=HK, vka=VKA, sy=SY, ss=SS, laytyp=LAYTYP, vkcb=VKCB) ghb = fm.ModflowGhb(mf, stress_period_data=GHB) wel = fm.ModflowWel(mf, stress_period_data=WEL) rch = fm.ModflowRch(mf, nrchop=3, rech=RECH) evt = fm.ModflowEvt(mf, nevtop=3, evtr=EVTR) oc = fm.ModflowOc(mf, stress_period_data=OC, compact=True) pcg = fm.ModflowPcg(mf)
def get_package(self, _mf): content = self.merge() return mf.ModflowDis(_mf, **content)
def create_package(self, name, content): if name == 'mf': model_ws = os.path.realpath(os.path.join(self._data_folder, self._model_id, content['model_ws'])) if not os.path.exists(model_ws): os.makedirs(model_ws) self._mf = mf.Modflow( modelname=content['modelname'], exe_name=content['exe_name'], version=content['version'], model_ws=model_ws ) if name == 'dis': mf.ModflowDis( self._mf, nlay=content['nlay'], nrow=content['nrow'], ncol=content['ncol'], nper=content['nper'], delr=content['delr'], delc=content['delc'], laycbd=content['laycbd'], top=content['top'], botm=content['botm'], perlen=content['perlen'], nstp=content['nstp'], tsmult=content['tsmult'], steady=content['steady'], itmuni=content['itmuni'], lenuni=content['lenuni'], extension=content['extension'], unitnumber=content['unitnumber'], xul=content['xul'], yul=content['yul'], rotation=content['rotation'], proj4_str=content['proj4_str'], start_datetime=content['start_datetime'] ) if name == 'bas': mf.ModflowBas( self._mf, ibound=content['ibound'], strt=content['strt'], ifrefm=content['ifrefm'], ixsec=content['ixsec'], ichflg=content['ichflg'], stoper=content['stoper'], hnoflo=content['hnoflo'], extension=content['extension'], unitnumber=content['unitnumber'] ) if name == 'lpf': mf.ModflowLpf( self._mf, laytyp=content['laytyp'], layavg=content['layavg'], chani=content['chani'], layvka=content['layvka'], laywet=content['laywet'], ipakcb=content['ipakcb'], hdry=content['hdry'], iwdflg=content['iwdflg'], wetfct=content['wetfct'], iwetit=content['iwetit'], ihdwet=content['ihdwet'], hk=content['hk'], hani=content['hani'], vka=content['vka'], ss=content['ss'], sy=content['sy'], vkcb=content['vkcb'], wetdry=content['wetdry'], storagecoefficient=content['storagecoefficient'], constantcv=content['constantcv'], thickstrt=content['thickstrt'], nocvcorrection=content['nocvcorrection'], novfc=content['novfc'], extension=content['extension'], unitnumber=content['unitnumber'] ) if name == 'pcg': mf.ModflowPcg( self._mf, mxiter=content['mxiter'], iter1=content['iter1'], npcond=content['npcond'], hclose=content['hclose'], rclose=content['rclose'], relax=content['relax'], nbpol=content['nbpol'], iprpcg=content['iprpcg'], mutpcg=content['mutpcg'], damp=content['damp'], dampt=content['dampt'], ihcofadd=content['ihcofadd'], extension=content['extension'], unitnumber=content['unitnumber'] ) if name == 'oc': mf.ModflowOc( self._mf, ihedfm=content['ihedfm'], iddnfm=content['iddnfm'], chedfm=content['chedfm'], cddnfm=content['cddnfm'], cboufm=content['cboufm'], compact=content['compact'], stress_period_data=self.get_stress_period_data(content['stress_period_data']), extension=content['extension'], unitnumber=content['unitnumber'] ) if name == 'risdlkfjlv': mf.ModflowRiv( self._mf, ipakcb=content['ipakcb'], stress_period_data=content['stress_period_data'], dtype=content['dtype'], extension=content['extension'], unitnumber=content['unitnumber'], options=content['options'], naux=content['naux'] ) if name == 'wel': mf.ModflowWel( self._mf, ipakcb=content['ipakcb'], stress_period_data=content['stress_period_data'], dtype=content['dtype'], extension=content['extension'], unitnumber=content['unitnumber'], options=content['options'] ) if name == 'rch': mf.ModflowRch( self._mf, ipakcb=content['ipakcb'], nrchop=content['nrchop'], rech=content['rech'], extension=content['extension'], unitnumber=content['unitnumber'] ) if name == 'chd': mf.ModflowChd( self._mf, stress_period_data=content['stress_period_data'], dtype=content['dtype'], options=content['options'], extension=content['extension'], unitnumber=content['unitnumber'] ) if name == 'ghb': mf.ModflowGhb( self._mf, ipakcb=content['ipakcb'], stress_period_data=content['stress_period_data'], dtype=content['dtype'], options=content['options'], extension=content['extension'], unitnumber=content['unitnumber'] )
def run_experiment(self, experiment): ''' Method for running an instantiated model structure. This method should always be implemented. :param case: keyword arguments for running the model. The case is a dict with the names of the uncertainties as key, and the values to which to set these uncertainties. ''' #NetLogo agent attributes to be passed to Python well objects #when new wells are created in NetLogo nl_read_sys_attribs = ['who', 'xcor', 'ycor'] nl_read_well_attribs = [ 'who', 'xcor', 'ycor', 'IsCold', 'z0', 'FilterLength', 'T_inj', 'Q' ] #NetLogo agent attributes to be updated by the Python objects after each period nl_update_well_attribs = ['T_modflow', 'H_modflow'] nl_update_globals = ['ztop', 'Laquifer'] self.netlogo.command('setup') for key, value in experiment.items(): if key in self.NetLogo_uncertainties: try: self.netlogo.command(self.command_format.format( key, value)) except jpype.JavaException as e: warning('Variable {0} throws exception: {}'.format( (key, str(e)))) logging.debug(self.netlogo.report(str(key))) if key in self.SEAWAT_uncertainties: setattr(self, key, value) #Set policy parameters if present if self.policy: for key, value in self.policy.items(): if (key in self.NetLogo_uncertainties and key != 'name'): self.netlogo.command(self.command_format.format( key, value)) elif key in self.SEAWAT_uncertainties: setattr(self, key, value) logging.info('Policy parameters set successfully') #Update NetLogo globals from input parameters for var in nl_update_globals: self.netlogo.command( self.command_format.format(var, getattr(self, var))) #Run the NetLogo setup routine, creating the agents #Create lists of Python objects based on the NetLogo agents self.netlogo.command('init-agents') sys_obj_list = update_runtime_objectlist(self.netlogo, [], nl_read_sys_attribs, breed='system', objclass=PySystem) well_obj_list, newgrid_flag = update_runtime_objectlist( self.netlogo, [], nl_read_well_attribs, breed='well', objclass=PyWell) #Assign values for uncertain NetLogo parameters logging.info('NetLogo parameters set successfully') #self.netlogo.command('init-agents') #Calculate geohydrological parameters linked to variable inputs rho_b = self.rho_solid * (1 - self.PEFF) kT_b = self.kT_s * (1 - self.PEFF) + self.kT_f * self.PEFF dmcoef = kT_b / (self.PEFF * self.rho_f * self.Cp_f) * 24 * 3600 trpt = self.al * self.trp_mult trpv = trpt #Initialize PyGrid object itype = mt3.Mt3dSsm.itype_dict() grid_obj = PyGrid() grid_obj.make_grid(well_obj_list, dmin=self.dmin, dmax=self.dmax, dz=self.dz, ztop=self.ztop, zbot=self.zbot, nstep=self.nstep, grid_extents=self.grid_extents) #Initial arrays for grid values (temperature, head) - for this case, assumes no groundwater flow #and uniform temperature grid_obj.ncol = len(grid_obj.XGR) - 1 grid_obj.delr = np.diff(grid_obj.XGR) grid_obj.nrow = len(grid_obj.YGR) - 1 grid_obj.delc = -np.diff(grid_obj.YGR) grid_obj.top = self.ztop * np.ones([grid_obj.nrow, grid_obj.ncol]) botm_range = np.arange(self.zbot, self.ztop, self.dz)[::-1] botm_2d = np.ones([grid_obj.nrow, grid_obj.ncol]) grid_obj.botm = botm_2d * botm_range[:, None, None] grid_obj.nlay = len(botm_range) grid_obj.IBOUND, grid_obj.ICBUND = boundaries( grid_obj) #Create grid boundaries #Initial arrays for grid values (temperature, head) init_grid = np.ones((grid_obj.nlay, grid_obj.nrow, grid_obj.ncol)) grid_obj.temp = 10. * init_grid grid_obj.HK = self.HK * init_grid grid_obj.VK = self.VK * init_grid #Set initial heads according to groundwater flow (based on mfLab Utrecht model) y_array = np.array([(grid_obj.YGR[:-1] - np.mean(grid_obj.YGR[:-1])) * self.PEFF * -self.gwflow_y / 365 / self.HK]) y_tile = np.array([np.tile(y_array.T, (1, grid_obj.ncol))]) x_array = (grid_obj.XGR[:-1] - np.mean( grid_obj.XGR[:-1])) * self.PEFF * -self.gwflow_x / 365 / self.HK y_tile += x_array grid_obj.head = np.tile(y_tile, (grid_obj.nlay, 1, 1)) #Set times at which to read SEAWAT output for each simulation period timprs = np.array([self.perlen]) nprs = len(timprs) logging.info('SEAWAT parameters set successfully') #Iterate the coupled model for period in range(self.run_length): #Set up the text output from NetLogo commands = [] self.fns = {} for outcome in self.outcomes: #if outcome.time: name = outcome.name fn = r'{0}{3}{1}{2}'.format(self._working_directory, name, '.txt', os.sep) self.fns[name] = fn fn = '"{}"'.format(fn) fn = fn.replace(os.sep, '/') if self.netlogo.report('is-agentset? {}'.format(name)): #If name is name of an agentset, we #assume that we should count the total number of agents nc = r'{2} {0} {3} {4} {1}'.format(fn, name, 'file-open', 'file-write', 'count') else: #It is not an agentset, so assume that it is #a reporter / global variable nc = r'{2} {0} {3} {1}'.format(fn, name, 'file-open', 'file-write') commands.append(nc) c_out = ' '.join(commands) self.netlogo.command(c_out) logging.info(' -- Simulating period {0} of {1}'.format( period, self.run_length)) #Run the NetLogo model for one tick self.netlogo.command('go') logging.debug('NetLogo step completed') #Create placeholder well list - required for MODFLOW WEL package if no wells active in NetLogo well_LRCQ_list = {} well_LRCQ_list[0] = [[0, 0, 0, 0]] ssm_data = {} ssm_data[0] = [[0, 0, 0, 0, itype['WEL']]] #Check the well agents which are active in NetLogo, and update the Python objects if required #The newgrid_flag indicates whether or not the grid should be recalculated to account for changes #in the list of active wells if well_obj_list: well_obj_list, newgrid_flag = update_runtime_objectlist( self.netlogo, well_obj_list, nl_read_well_attribs) if well_obj_list and newgrid_flag: #If the list of active wells has changed and if there are active wells, create a new grid object newgrid_obj = PyGrid() newgrid_obj.make_grid(well_obj_list, dmin=self.dmin, dmax=self.dmax, dz=self.dz, ztop=self.ztop, zbot=self.zbot, nstep=self.nstep, grid_extents=self.grid_extents) #Interpolate the temperature and head arrays to match the new grid newgrid_obj.temp = grid_interpolate(grid_obj.temp[0, :, :], grid_obj, newgrid_obj) newgrid_obj.head = grid_interpolate(grid_obj.head[0, :, :], grid_obj, newgrid_obj) #Use the new simulation grid grid_obj = newgrid_obj logging.debug('Python update completed') if well_obj_list: for i in well_obj_list: #Read well flows from NetLogo and locate each well in the simulation grid i.Q = read_NetLogo_attrib(self.netlogo, 'Q', i.who) i.calc_LRC(grid_obj) #Create well and temperature lists following MODFLOW/MT3DMS format well_LRCQ_list = create_LRCQ_list(well_obj_list, grid_obj) ssm_data = create_conc_list(well_obj_list) #Initialize MODFLOW packages using FloPy #ml = mf.Modflow(self.name, version='mf2005', exe_name=self.swtexe_name, model_ws=self.dirs[0]) swtm = swt.Seawat(self.name, exe_name=self.swtexe_name, model_ws=self.dirs[0]) discret = mf.ModflowDis(swtm, nrow=grid_obj.nrow, ncol=grid_obj.ncol, nlay=grid_obj.nlay, delr=grid_obj.delr, delc=grid_obj.delc, laycbd=0, top=self.ztop, botm=self.zbot, nper=self.nper, perlen=self.perlen, nstp=self.nstp, steady=self.steady) bas = mf.ModflowBas(swtm, ibound=grid_obj.IBOUND, strt=grid_obj.head) lpf = mf.ModflowLpf(swtm, hk=self.HK, vka=self.VK, ss=0.0, sy=0.0, laytyp=0, layavg=0) wel = mf.ModflowWel(swtm, stress_period_data=well_LRCQ_list) words = ['head', 'drawdown', 'budget', 'phead', 'pbudget'] save_head_every = 1 oc = mf.ModflowOc(swtm) pcg = mf.ModflowPcg(swtm, mxiter=200, iter1=200, npcond=1, hclose=0.001, rclose=0.001, relax=1.0, nbpol=0) #ml.write_input() #Initialize MT3DMS packages #mt = mt3.Mt3dms(self.name, 'nam_mt3dms', modflowmodel=ml, model_ws=self.dirs[0]) adv = mt3.Mt3dAdv( swtm, mixelm=0, #-1 is TVD percel=1, nadvfd=1, #Particle based methods nplane=0, mxpart=250000, itrack=3, dceps=1e-4, npl=5, nph=8, npmin=1, npmax=16) btn = mt3.Mt3dBtn(swtm, cinact=-100., icbund=grid_obj.ICBUND, prsity=self.PEFF, sconc=[grid_obj.temp][0], ifmtcn=-1, chkmas=False, nprobs=0, nprmas=1, dt0=0.0, ttsmult=1.5, ttsmax=20000., ncomp=1, nprs=nprs, timprs=timprs, mxstrn=9999) dsp = mt3.Mt3dDsp(swtm, al=self.al, trpt=trpt, trpv=trpv, dmcoef=dmcoef) rct = mt3.Mt3dRct(swtm, isothm=0, ireact=0, igetsc=0, rhob=rho_b) gcg = mt3.Mt3dGcg(swtm, mxiter=50, iter1=50, isolve=1, cclose=1e-3, iprgcg=0) ssm = mt3.Mt3dSsm(swtm, stress_period_data=ssm_data) #mt.write_input() #Initialize SEAWAT packages # mswtf = swt.Seawat(self.name, 'nam_swt', modflowmodel=ml, mt3dmsmodel=mt, # model_ws=self.dirs[0]) swtm.write_input() #Run SEAWAT #m = mswtf.run_model(silent=True) m = swtm.run_model(silent=True) logging.debug('SEAWAT step completed') #Copy Modflow/MT3DMS output to new files shutil.copyfile( os.path.join(self.dirs[0], self.name + '.hds'), os.path.join(self.dirs[0], self.name + str(period) + '.hds')) shutil.copyfile( os.path.join(self.dirs[0], 'MT3D001.UCN'), os.path.join(self.dirs[0], self.name + str(period) + '.UCN')) #Create head file object and read head array for next simulation period h_obj = bf.HeadFile( os.path.join(self.dirs[0], self.name + str(period) + '.hds')) grid_obj.head = h_obj.get_data(totim=self.perlen) #Create concentration file object and read temperature array for next simulation period t_obj = bf.UcnFile( os.path.join(self.dirs[0], self.name + str(period) + '.UCN')) grid_obj.temp = t_obj.get_data(totim=self.perlen) logging.debug('Output processed') if well_obj_list: for i in well_obj_list: #Update each active Python well object with the temperature and head at its grid location i.T_modflow = grid_obj.temp[i.L[0], i.R, i.C] i.H_modflow = grid_obj.head[i.L[0], i.R, i.C] #Update the NetLogo agents from the corresponding Python objects write_NetLogo_attriblist(self.netlogo, well_obj_list, nl_update_well_attribs) #As an example of data exchange, we can calculate the fraction of the simulated grid in which #the temperature change is significant, and send this value to a NetLogo global variable use = subsurface_use(grid_obj, grid_obj.temp) write_NetLogo_global(self.netlogo, 'SubsurfaceUse', use) logging.debug('NetLogo update completed') h_obj.file.close() t_obj.file.close() self.netlogo.command('file-close-all') self._handle_outcomes()
# more information: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.linspace.html bot = np.linspace(-H / Nlay, -H, Nlay) # result is: # array([ -5., -10., -15., -20., -25., -30., -35., -40., -45., -50.]) # calculation of row-width and col-width delrow = delcol = L / (N - 1) # result is: # 4 # instantiante the discretization object dis = mf.ModflowDis(ml, nlay=Nlay, nrow=N, ncol=N, delr=delrow, delc=delcol, top=0.0, botm=bot, laycbd=0) # helping-variable Nhalf = (N - 1) / 2 # iBound-Configuration # http://water.usgs.gov/nrp/gwsoftware/modflow2000/MFDOC/index.html?bas6.htm # If IBOUND(J,I,K) < 0, cell J,I,K has a constant head. # If IBOUND(J,I,K) = 0, cell J,I,K is inactive. # If IBOUND(J,I,K) > 0, cell J,I,K is active. # # every cell in the model has to be defined
# what version of modflow to use? modflow_v = 'mfnwt' # 'mfnwt' or 'mf2005' # where is your MODFLOW executable? if (modflow_v == 'mf2005'): if platform.system() == 'Windows': path2mf = 'C:/Users/Sam/Dropbox/Work/Models/MODFLOW/MF2005.1_12/bin/mf2005.exe' else: path2mf = modflow_v elif (modflow_v == 'mfnwt'): if platform.system() == 'Windows': path2mf = 'C:/Users/Sam/Dropbox/Work/Models/MODFLOW/MODFLOW-NWT_1.1.4/bin/MODFLOW-NWT.exe' else: path2mf = modflow_v # set up super simple model ml = mf.Modflow(modelname="testmodel", exe_name=path2mf, version=modflow_v) dis = mf.ModflowDis(ml) bas = mf.ModflowBas(ml) oc = mf.ModflowOc(ml) # choose solver package depending on modflow version if (modflow_v == 'mf2005'): lpf = mf.ModflowLpf(ml) pcg = mf.ModflowPcg(ml) elif (modflow_v == 'mfnwt'): upw = mf.ModflowUpw(ml) nwt = mf.ModflowNwt(ml) ml.write_input() ml.run_model()