pydfnWorks
python wrapper for dfnWorks
transport.py
Go to the documentation of this file.
1 import os
2 import sys
3 import shutil
4 from time import time
5 import subprocess
6 
7 
8 def dfn_trans(self):
9  """Primary driver for dfnTrans.
10 
11  Parameters
12  ---------
13  self : object
14  DFN Class
15 
16  Returns
17  --------
18  None
19  """
20  print('=' * 80)
21  print("\ndfnTrans Starting\n")
22  print('=' * 80)
23  tic = time()
24  self.copy_dfn_trans_files()
25  self.check_dfn_trans_run_files()
26  self.run_dfn_trans()
27  delta_time = time() - tic
28  self.dump_time('Process: dfnTrans', delta_time)
29  print('=' * 80)
30  print("\ndfnTrans Complete\n")
31  print("Time Required for dfnTrans: %0.2f Seconds\n" % delta_time)
32  print('=' * 80)
33 
34 
36  """Creates symlink to dfnTrans Execuateble and copies input files for dfnTrans into working directory
37 
38  Parameters
39  ---------
40  self : object
41  DFN Class
42 
43  Returns
44  --------
45  None
46  """
47 
48  print("Attempting to Copy %s\n" % self.dfnTrans_file)
49  try:
50  shutil.copy(self.dfnTrans_file, os.path.abspath(os.getcwd()))
51  except OSError:
52  print("--> Problem copying %s file" % self.local_dfnTrans_file)
53  print("--> Trying to delete and recopy")
54  os.remove(self.local_dfnTrans_file)
55  shutil.copy(self.dfnTrans_file, os.path.abspath(os.getcwd()))
56  except:
57  print("--> ERROR: Problem copying %s file\n" % self.dfnTrans_file)
58  error = "Unable to replace. Exiting Program\n"
59  sys.stderr.write(error)
60  sys.exit(1)
61 
62 
63 def run_dfn_trans(self):
64  """ Execute dfnTrans
65 
66  Parameters
67  ---------
68  self : object
69  DFN Class
70 
71  Returns
72  --------
73  None
74  """
75  tic = time()
76  failure = subprocess.call(os.environ['DFNTRANS_EXE'] + ' ' +
77  self.local_dfnTrans_file,
78  shell=True)
79  self.dump_time("Function: DFNTrans ", time() - tic)
80  if failure != 0:
81  error = "--> ERROR: dfnTrans did not complete\n"
82  sys.stderr.write(error)
83  sys.exit(1)
84 
85 
86 def create_dfn_trans_links(self, path='../'):
87  """ Create symlinks to files required to run dfnTrans that are in another directory.
88 
89  Parameters
90  ---------
91  self : object
92  DFN Class
93  path : string
94  Absolute path to primary directory.
95 
96  Returns
97  --------
98  None
99 
100  Notes
101  -------
102  Typically, the path is DFN.path, which is set by the command line argument -path
103 
104  """
105  files = [
106  'params.txt', 'allboundaries.zone', 'full_mesh.stor', 'poly_info.dat',
107  'full_mesh.inp', 'aperture.dat'
108  ]
109  if self.flow_solver == 'PFLOTRAN':
110  files.append('cellinfo.dat')
111  files.append('darcyvel.dat')
112  files.append('full_mesh_vol_area.uge')
113  if self.flow_solver == 'FEHM':
114  files.append('tri_frac.fin')
115 
116  for f in files:
117  try:
118  os.symlink(path + f, f)
119  except:
120  print("--> Error Creating link for %s\n" % f)
121 
122 
124  """ Ensures that all files required for dfnTrans run are in the current directory
125 
126  Parameters
127  ---------
128  self : object
129  DFN Class
130 
131  Returns
132  --------
133  None
134 
135  Notes
136  -------
137  None
138  """
139  cwd = os.getcwd()
140  print(
141  "\nChecking that all files required for dfnTrans are in the current directory"
142  )
143  print("--> Current Working Directory: %s" % cwd)
144  print("--> dfnTrans is running from: %s" % self.local_dfnTrans_file)
145 
146  print("--> Checking DFNTrans Parameters")
147  params = {
148  "param:": None,
149  "poly:": None,
150  "inp:": None,
151  "stor:": None,
152  "boundary:": None,
153  "out_grid:": None,
154  "out_3dflow:": None,
155  "out_init:": None,
156  "out_tort:": None,
157  "out_curv:": None,
158  "out_avs:": None,
159  "out_traj:": None,
160  "out_fract:": None,
161  "out_filetemp:": None,
162  "out_dir:": None,
163  "out_path:": None,
164  "out_time:": None,
165  "ControlPlane:": None,
166  "control_out:": None,
167  "delta_Control:": None,
168  "flowdir:": None,
169  "init_nf:": None,
170  "init_partn:": None,
171  "init_eqd:": None,
172  "init_npart:": None,
173  "init_fluxw:": None,
174  "init_totalnumber:": None,
175  "init_oneregion:": None,
176  "in_partn:": None,
177  "init_well:": None,
178  "init_nodepart:": None,
179  "in_xmin:": None,
180  "in_xmax:": None,
181  "in_ymin:": None,
182  "in_ymax:": None,
183  "in_zmin:": None,
184  "in_zmax:": None,
185  "init_random:": None,
186  "in_randpart:": None,
187  "init_matrix:": None,
188  "inm_coord:": None,
189  "inm_nodeID:": None,
190  "inm_porosity:": None,
191  "inm_diffcoeff:": None,
192  "streamline_routing:": None,
193  "tdrw:": None,
194  "tdrw_porosity:": None,
195  "tdrw_diffcoeff:": None,
196  "timesteps:": None,
197  "time_units:": None,
198  "flux_weight:": None,
199  "seed:": None,
200  "in-flow-boundary:": None,
201  "out-flow-boundary:": None,
202  "aperture:": None,
203  "aperture_type:": None,
204  "aperture_file:": None,
205  "porosity": None,
206  "density:": None,
207  "satur:": None,
208  "thickness:": None
209  }
210 
211  files = ["param:", "poly:", "inp:", "stor:", "boundary:"]
212 
213  if self.flow_solver == "PFLOTRAN":
214  params["PFLOTRAN_vel:"] = None
215  files.append("PFLOTRAN_vel:")
216 
217  params["PFLOTRAN_cell:"] = None
218  files.append("PFLOTRAN_cell:")
219 
220  params["PFLOTRAN_uge:"] = None
221  files.append("PFLOTRAN_uge:")
222 
223  if self.flow_solver == "FEHM":
224  params["FEHM_fin:"] = None
225  files.append("FEHM_fin:")
226 
227  # Parse DFNTrans input and fill dictionary
228  keys = params.keys()
229  with open(self.local_dfnTrans_file) as fp:
230 
231  for line in fp.readlines():
232  if "/*" in line:
233  comment = line
234  line = line[:line.index(
235  "/*")]
236  if "//" in line:
237  line = line[:line.index("//")]
238 
239  if len(line) > 0:
240  for key in keys:
241  if key in line:
242  if params[key] == None:
243  params[key] = line.split()[1]
244 
245  #for p in params.keys():
246  # print(p,params[p])
247 
248  # Check if file required for the run are in the directory and are not empty
249  for key in files:
250  if params[key] is None:
251  error = f"ERROR!!!!!\nRequired file {key} was not provided.\nPlease check DFNTrans control file\nExiting Program\n"
252  sys.stderr.write(error)
253  sys.exit(1)
254  elif not os.path.isfile(params[key]):
255  error = "ERROR!!!!!\nRequired file %s is not the current directory.\nPlease check required files\nExiting Program\n" % params[
256  key]
257  sys.stderr.write(error)
258  sys.exit(1)
259 
260  print(
261  "--> All files required for dfnTrans have been found in current directory\n\n"
262  )
263 
264  for required in [
265  "out_grid:", "out_3dflow:", "out_init:", "out_tort:", "out_curv:",
266  "out_avs:", "out_traj:", "out_fract:", "out_filetemp:", "out_dir:",
267  "out_path:", "out_time:", "timesteps:", "time_units:",
268  "flux_weight:", "seed:", "in-flow-boundary:", "out-flow-boundary:",
269  "aperture:", "porosity", "density:", "satur:",
270  "streamline_routing:"
271  ]:
272  if params[required] == None:
273  error = "ERROR!!!\n%s not provided. Exiting\n\n" % (required)
274  sys.stderr.write(error)
275  sys.exit(1)
276 
277  # Check Initial conditions, make sure only 1 Initial condition is selected and that
278  # required parameters have been defined
279  print("--> Checking Initial Conditions")
280  initial_conditions = [
281  ("init_nf:", "init_partn:"), ("init_eqd:", "init_npart:"),
282  ("init_fluxw:", "init_totalnumber:"), ("init_random:", "in_randpart:"),
283  ("init_oneregion:", "in_partn:", "in_xmin:", "in_ymin:", "in_zmin:",
284  "in_xmax:", "in_ymax:", "in_zmax:"),
285  ("init_matrix:", "inm_coord:", "inm_nodeID:", "inm_porosity:"),
286  ("init_well:", "init_nodepart:")
287  ]
288  ic_selected = []
289  for ic in initial_conditions:
290  #print(ic,params[ic[0]])
291  if params[ic[0]] == "yes":
292  ic_selected.append(ic[0])
293  for i in ic:
294  if params[i] == None:
295  error = "Initial condition %s selected but %s not provided\n" % (
296  ic[0], i)
297  sys.stderr.write(error)
298  sys.exit(1)
299  if len(ic_selected) > 1:
300  error = "ERROR!!! More than one initial condition defined\nExiting\n"
301  sys.stderr.write(error)
302  print("Selected Initial Conditions:\n:")
303  for ic in ic_selected:
304  print(ic)
305  print()
306  sys.exit(1)
307  elif len(ic_selected) == 0:
308  error = "ERROR!!! No initial condition defined\nExiting\n"
309  sys.stderr.write(error)
310  sys.exit(1)
311 
312  if params["ControlPlane:"] != None:
313  for required in ["control_out:", "delta_Control:", "flowdir:"]:
314  if params[required] == None:
315  error = "Parameter %s required for ControlPlane\n" % required
316  sys.stderr.write(error)
317  sys.exit(1)
318 
319  if params["tdrw:"] == "yes":
320  if params["time_units:"] != "seconds":
321  error = "ERROR!!!You must use seconds for the time_units to run TDRW"
322  sys.stderr.write(error)
323  sys.exit(1)
324 
325  for required in ["tdrw_porosity:", "tdrw_diffcoeff:"]:
326  if params[required] == None:
327  error = "Parameter %s required for tdrw\n" % required
328  sys.stderr.write(error)
329  sys.exit(1)
330 
331  if params["aperture:"] == "yes":
332  if params["aperture_type:"] == None:
333  error = "Parameter aperture_type: required for aperture: yes\n"
334  sys.stderr.write(error)
335  sys.exit(1)
336 
337  else:
338  if not os.path.isfile(params["aperture_file:"]) or os.stat(
339  params["aperture_file:"]).st_size == 0:
340  error = "aperture_file: %s not found or empty\n" % params[
341  "aperture_file:"]
342  sys.stderr.write(error)
343  sys.exit(1)
344 
345  else:
346  if params["thickness:"] == None:
347  error = "Parameter thickness: required for aperture: no:\n"
348  sys.stderr.write(error)
349  sys.exit(1)
350 
351  print("--> Checking Initial Conditions Complete")
def create_dfn_trans_links(self, path='../')
Definition: transport.py:86