[cig-commits] r3910 - short/3D/PyLith/branches/pylith-0.8/tutorials/reversenog/archive

baagaard at geodynamics.org baagaard at geodynamics.org
Mon Jul 3 12:38:36 PDT 2006


Author: baagaard
Date: 2006-07-03 12:38:36 -0700 (Mon, 03 Jul 2006)
New Revision: 3910

Modified:
   short/3D/PyLith/branches/pylith-0.8/tutorials/reversenog/archive/tutor.py
Log:
Tutorial. Finished converting tutor shell script to Python. This removed reliance on bash shell for tutor.

Modified: short/3D/PyLith/branches/pylith-0.8/tutorials/reversenog/archive/tutor.py
===================================================================
--- short/3D/PyLith/branches/pylith-0.8/tutorials/reversenog/archive/tutor.py	2006-07-03 16:24:36 UTC (rev 3909)
+++ short/3D/PyLith/branches/pylith-0.8/tutorials/reversenog/archive/tutor.py	2006-07-03 19:38:36 UTC (rev 3910)
@@ -14,119 +14,189 @@
 root="bmrsnog"
 
 # ----------------------------------------------------------------------
-def showUsage():
-  print "usage: tutor.py MODE STEP"
-  print "  Modes:"
-  print "    check     Check to make sure files necessary for step exist."
-  print "    clean     Remove old files that will be overwritten in this step."
-  print "    retrieve  Copy files (as needed) from archive needed in " \
-        "this step."
-  print "  Steps:"
-  print "    mesh      Generating the finite-element mesh."
-  print "    setup     Setting up the PyLith input files."
-  print "    run1      Running the simulation on 1 processor."
-  print "    viz1      Visualizing the output from a simulation on " \
-        "1 processor."
-  print "    run2      Running the simulation on 2 processors."
-  print "    viz2      Visualizing the output from a simulation on " \
-        "2 processors."
-  print "    all       Apply mode to each step in succession."
-  return
+def assembleStrings(fragments):
+  """Assemble a list of strings from a list of list of fragments."""
 
-
-# ----------------------------------------------------------------------
-def assembleStrings(prefixes, suffixes):
-  """Assemble list of strings from lists of prefixes and suffixes."""
-
-  values = []
-  for prefix in prefixes:
-    for suffix in suffixes:
-      values.append(prefix+suffix)
+  values = fragments[0]
+  for fragset in fragments[1:]:
+    newvalues = []
+    for value in values:
+      for fragment in fragset:
+        newvalues.append(value + fragment)
+    values = newvalues
   return values
 
 
 # ----------------------------------------------------------------------
-def getStepFiles(curstep):
+def getStepFiles(step):
 
   files = {'input': [],
            'output': []}
-  if "mesh" == curstep:
-    files['input'] += assembleStrings(prefixes=[root], suffixes=[".geo"])
-    files['output'] += assembleStrings(prefixes=[root], suffixes=[".netgen"])
+  if "mesh" == step:
+    fragments = [ [root], [".geo"] ]
+    files['input'] += assembleStrings(fragments)
+
+    fragments = [ [root], [".netgen" ] ]
+    files['output'] += assembleStrings(fragments)
     
-  elif "setup" == curstep:
-    suffixes = [".netgen", ".fault.par", ".par", ".aux"]
-    files["input"] += assembleStrings(prefixes=[root], suffixes=suffixes)
+  elif "setup" == step:
+    fragments = [ [root], [".netgen", ".fault.par", ".par", ".aux"] ]
+    files['input'] += assembleStrings(fragments)
 
-    suffixes = [".coord", ".connect", ".bc", ".w01.wink", ".1.fcoord",
-                ".1.fbc", ".split"]
-    files['output'] += assembleStrings(prefixes=[root], suffixes)
-    files['output'] += "runbm5.sh"
+    fragments = [ [root],
+                  [".coord", ".connect", ".split", ".bc", ".w01.wink",
+                   ".1.fcoord", ".1.fbc"] ]
+    files['output'] += assembleStrings(fragments)
 
-  elif "run1" == curstep:
-    suffixes = [".coord", ".coonect", ".split", ".bc", ".fuldat", ".prop", \
-                ".statevar", ".time"]
-    files['input'] += assembleStrings(prefixes=[root], suffixes=suffixes)
+  elif "run1" == step:
+    fragments = [ [root], [".coord", ".connect", ".split", ".bc",
+                           ".fuldat", ".prop", ".statevar", ".time"] ]
+    files['input'] += assembleStrings(fragments)
+    files['input'] += ["runbm5.sh"]
 
-    prefixes = [root + "_1"]
-    suffixes = [".coord", ".connect", ".split", ".bc"]
-    files['output'] += assembleStrings(prefixes=prefixes, suffixes=suffixes)
+    root1 = root + "_1"
+    fragments = [ [root1], [".coord", ".connect", ".split", ".bc"] ]
+    files['output'] += assembleStrings(fragments)
 
-    prefixes = [root + "_1.[0-9]*"]
-    suffixes = [".bc", ".connect", ".coord", ".split", ".ascii", ".vtk", \
-                ".fuldat", ".prop", ".statevar", ".time"]
-    files['output'] += assembleStrings(prefixes=[root1], suffixes=suffixes)
+    fragments = [ [root1], [".0"],
+                  [".coord", ".connect", ".split", ".bc",
+                   ".ascii", ".vtk",
+                   ".fuldat", ".prop", ".statevar", ".time"] ]
+    files['output'] += assembleStrings(fragments)
+    
+    fragments = [ [root1], [".0"],
+                  [".gmesh", ".mesh", ".mesh.split"],
+                  ["", \
+                   ".time.00000", ".time.00010", ".time.00050", ".time.00100"],
+                  [".inp"] ]
+    files['output'] += assembleStrings(fragments)
+  elif "viz1" == step:
+    root1 = root + "_1"
+    fragments = [ [root1], [".0"],
+                  [".mesh", ".mesh.time.00010"], [".inp"] ]
+    files['input'] += assembleStrings(fragments)
 
-    prefixes = root + "_1.[0-9]*.gmesh"
-    suffixes = ["", ".t10", ".time"]
-    prefixesA = assembleStrings(prefixes=prefixes, suffixes=suffixes)
-    prefixes = ["", ".t10", ".time"]
-    suffixes = [".00000", ".00010", ".00050", ".00100"]
-    suffixes = assembleStrings(prefixes=prefixes, suffixes=suffixes)
-    suffixes = assembleString(prefixes=suffixes, suffixes=".inp")
-    files['ouput'].append(assembleStrings(prefixes=prefixesA,
-                                          suffixes=suffixes))
+    fragments = [ [root1], [".0"],
+                  ["t00010"], [".inp"] ]
+    files['output'] += assembleStrings(fragments)
+  elif "run2" == step:
+    fragments = [ [root], [".coord", ".connect", ".split", ".bc",
+                           ".fuldat", ".prop", ".statevar", ".time"] ]
+    files['input'] += assembleStrings(fragments)
+    files['input'] += ["runbm5.sh"]
+
+    root1 = root + "_2"
+    fragments = [ [root1], [".coord", ".connect", ".split", ".bc"] ]
+    files['output'] += assembleStrings(fragments)
+
+    fragments = [ [root1], [".0", ".1"],
+                  [".coord", ".connect", ".split", ".bc",
+                   ".ascii", ".vtk",
+                   ".fuldat", ".prop", ".statevar", ".time"] ]
+    files['output'] += assembleStrings(fragments)
+    
+    fragments = [ [root1], [".0", ".1"],
+                  [".gmesh", ".mesh", ".mesh.split"],
+                  ["", \
+                   ".time.00000", ".time.00010", ".time.00050", ".time.00100"],
+                  [".inp"] ]
+    files['output'] += assembleStrings(fragments)
+  elif "viz2" == step:
+    root1 = root + "_2"
+    fragments = [ [root1], [".0", ".1"],
+                  [".mesh", ".mesh.time.00010"], [".inp"] ]
+    files['input'] += assembleStrings(fragments)
+
+    fragments = [ [root1], [".0", ".1"],
+                  [".t00010"], [".inp"] ]
+    files['output'] += assembleStrings(fragments)
+    print files
   else:
-    error
-
+    raise ValueError("Unknown tutor step: %s" % step)
   return files
-                          
 
-  elif [ "run1" == $curstep ]; then
 
-    files_output="$files_output "`echo ${root}_1.[0-9]*.gmesh{,.t10,.time.{00000,00010,00050,00100}}.inp`
-    files_output="$files_output "`echo ${root}_1.[0-9]*.mesh{,.t10,{.time.{00000,00010,00050,00100}}}.inp`
-    files_output="$files_output "`echo ${root}_1.[0-9]*.mesh.split.time.{00000,00010,00050,00100}.inp`
+# ----------------------------------------------------------------------
+def tutor(step, mode):
+  files = getStepFiles(step)
 
-  elif [ "viz1" == $curstep ]; then
-    files_input=`echo ${root}_1.0.mesh{,.time.00010}.inp`
-    files_output="${root}_1.0.mesh.t00010.inp"
+  import os
+  dirFiles = os.listdir(os.getcwd())
 
-  elif [ "run2" == $curstep ]; then
-    files_input="runbm5.sh"
-    files_input="$files_input "`echo $root.{coord,connect,split,bc}`
-    files_input="$files_input "`echo $root.{fuldat,prop,statevar,time}`
+  if "check" == mode:
+    print "Checking to make sure you have the following input files " \
+          "for step '%s':" % step
+    for filename in files['input']:
+      if filename in dirFiles:
+        print "  %s...found" % filename
+      else:
+        print "  %s...MISSING" % filename
+  elif "clean" == mode:
+    print "Removing any old output files from step '%s':" % step
+    for filename in files['output']:
+      if filename in dirFiles:
+        os.remove(filename)
+        print "  %s...removed" % filename
+  elif "retrieve" == mode:
+    import shutil
+    print "Retrieving input files from archive for step '%s':" % step
+    for filename in files['input']:
+      if filename in dirFiles:
+        print "  %s...already present" % filename
+      else:
+        shutil.copyfile("../archive/%s" % filename,
+                        "./%s" % filename)
+        print "  %s...retrieved from archive" % filename
+  else:
+    raise ValueError("Unrecognized mode: %s" % mode)
+  return
 
-    files_output=`echo ${root}_2.{coord,connect,split,bc}`
-    files_output="$files_output "`echo ${root}_2.[0-9]*.{bc,connect,coord,split}`
-    files_output="$files_output "`echo ${root}_2.[0-9]*.{ascii,vtk}`
-    files_output="$files_output "`echo ${root}_2.[0-9]*.{fuldat,prop,statevar,time}`
-    files_output="$files_output "`echo ${root}_2.[0-9]*.gmesh{,.t10,.time.{00000,00010,00050,00100}}.inp`
-    files_output="$files_output "`echo ${root}_2.[0-9]*.mesh{,.t10,{.time.{00000,00010,00050,00100}}}.inp`
-    files_output="$files_output "`echo ${root}_2.[0-9]*.mesh.split.time.{00000,00010,00050,00100}.inp`
 
-  elif [ "viz2" == $curstep ]; then
-    files_input=`echo ${root}_2.{0,1}.mesh{,.time.00010}.inp`
-    files_output=`echo ${root}_2.{0,1}.mesh.t00010.inp`
+# ----------------------------------------------------------------------
+if __name__ == "__main__":
+  from optparse import OptionParser
 
-  else
-    echo "Unrecognized step: $step"
-    show_usage
-    exit 1
-  fi
-}
+  usage = "usage: %prog -m MODE -s STEP\n\n" \
+          "Modes:\n" \
+          "  check     Check to make sure files necessary for step " \
+          "exist.\n" \
+          "  clean     Remove old files that will be overwritten in " \
+          "this step.\n" \
+          "  retrieve  Copy files (as needed) from archive needed in " \
+          "this step.\n" \
+          "Steps:\n" \
+          "  mesh      Generating the finite-element mesh.\n" \
+          "  setup     Setting up the PyLith input files.\n" \
+          "  run1      Running the simulation on 1 processor.\n" \
+          "  viz1      Visualizing the output from a simulation on " \
+          "1 processor.\n" \
+          "  run2      Running the simulation on 2 processors.\n" \
+          "  viz2      Visualizing the output from a simulation on " \
+          "2 processors.\n" \
+          "  all       Apply mode to each step in succession."
 
+  parser = OptionParser(usage=usage)
+  parser.add_option("-m", "--mode", dest="mode",
+                    type="string", metavar="MODE",
+                    help="Set tutor to MODE.")
+  parser.add_option("-s", "--step", dest="step",
+                    type="string", metavar="STEP",
+                    help="Perform mode operation for STEP.")
+  (options, args) = parser.parse_args()
+  if len(args) != 0:
+    parser.error("Incorrent number of arguments.")
 
+  if options.step is None or options.mode is None:
+    parser.error("Both step and mode are required options.")
+
+  allsteps = "mesh setup run1 viz1 run2 viz2"
+  if "all" == options.step:
+    for istep in allsteps.split():
+      tutor(istep, options.mode)
+  else:
+    tutor(options.step, options.mode)
+
+
 # version
 __id__ = "$Id$"
 



More information about the cig-commits mailing list