load an existing model with external arrays and explicit model_ws

First using free_format which results in open/close


In [1]:
import os
import numpy as np
import flopy

starting_dir = os.getcwd()
base_dir = "base_model_dir"
if not os.path.exists(base_dir):
    os.mkdir(base_dir)
# change to base_dir    
os.chdir(base_dir)    
nlay, nrow, ncol = 1, 1, 10
ml = flopy.modflow.Modflow(modelname="base",external_path="ref")
dis = flopy.modflow.ModflowDis(ml, nrow=1, ncol=10,
                               nlay=nlay, nper=2, perlen=[1,1],
                               steady=[True,True])
bas = flopy.modflow.ModflowBas(ml, ibound=1)
hk = np.zeros((nlay, nrow, ncol))+2.5
lpf = flopy.modflow.ModflowLpf(ml, hk=hk,vka=hk)

well_data = {}
well_data[0] = [0, 0, 9, 0.5]
well_data[1] = [0, 0, 9, 1.0]
wel = flopy.modflow.ModflowWel(ml, stress_period_data=well_data)

ghb_data = {}
ghb_data[0] = [0, 0, 0, 0.5, 1000.0]
ghb = flopy.modflow.ModflowGhb(ml, stress_period_data=ghb_data)
ml.write_input()
# change back 
os.chdir(starting_dir)

Now load the model using base_dir as the model_ws arg


In [2]:
ml_loaded = flopy.modflow.Modflow.load("base.nam",model_ws=base_dir)
print ml_loaded.get_package("lpf").hk.array
ml_loaded.change_model_ws("new_dir")
ml_loaded.write_input()


[[[ 2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5]]]

creating model workspace...
   new_dir

changing model workspace...
   new_dir

Now without free_format which results in old-style control records and unit numbers in the nam file


In [3]:
base_dir = "base_model_dir"
if not os.path.exists(base_dir):
    os.mkdir(base_dir)
# change to base_dir    
os.chdir(base_dir)    
nlay, nrow, ncol = 1, 1, 10
ml = flopy.modflow.Modflow(modelname="base",external_path="ref")
ml.array_free_format = False
dis = flopy.modflow.ModflowDis(ml, nrow=1, ncol=10,
                               nlay=nlay, nper=2, perlen=[1,1],
                               steady=[True,True])
bas = flopy.modflow.ModflowBas(ml, ibound=1)
hk = np.zeros((nlay, nrow, ncol))+2.5
lpf = flopy.modflow.ModflowLpf(ml, hk=hk,vka=hk)

well_data = {}
well_data[0] = [0, 0, 9, 0.5]
well_data[1] = [0, 0, 9, 1.0]
wel = flopy.modflow.ModflowWel(ml, stress_period_data=well_data)

ghb_data = {}
ghb_data[0] = [0, 0, 0, 0.5, 1000.0]
ghb = flopy.modflow.ModflowGhb(ml, stress_period_data=ghb_data)
ml.write_input()
# change back 
os.chdir("..")


Note: external_path ref already exists

In [4]:
ml_loaded = flopy.modflow.Modflow.load("base.nam",model_ws=base_dir)
print ml_loaded.get_package("lpf").hk.array
ml_loaded.change_model_ws("new_dir2")
ml_loaded.write_input()


Creating new model with name: base
--------------------------------------------------

   DIS  package load...success
   LIST package load...skipped
   DATA file load...skipped
      hk_Layer_1.ref
   DATA file load...skipped
      vka_Layer_1.ref
   BAS6 package load...success
   LPF  package load...success
   WEL  package load...success
   GHB  package load...success


   The following 5 packages were successfully loaded.
      base.dis
      base.bas
      base.lpf
      base.wel
      base.ghb
   The following 1 packages were not loaded.
      base.list


[[[ 2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5  2.5]]]

creating model workspace...
   new_dir2

changing model workspace...
   new_dir2

In [ ]: