Salome HOME
Porting to Python 3
[plugins/ghs3dprlplugin.git] / bin / mg-tetra_hpc.py
index ca6f4edae54cae75be3726d758a52b0fa4b83375..e20f654bea0fad73e5c44f258e9c0b419d1a890b 100644 (file)
@@ -3,21 +3,21 @@
 
 # %% LICENSE_SALOME_CEA_BEGIN
 # Copyright (C) 2008-2016  CEA/DEN
-# 
+#
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
 # version 2.1 of the License, or (at your option) any later version.
-# 
+#
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # Lesser General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU Lesser General Public
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-# 
+#
 # See http://www.salome-platform.org or email : webmaster.salome@opencascade.com
 # %% LICENSE_END
 
@@ -32,24 +32,26 @@ example linux usage:
 """
 
 import os
+import platform
 import sys
 import time
-import platform
+
 import argparse as AP
-import pprint as PP #pretty print
-import subprocess as SP #Popen
-import multiprocessing as MP #cpu_count
+import multiprocessing as MP  # cpu_count
+import pprint as PP  # pretty print
+import subprocess as SP  # Popen
+
 
 verbose = False
 
 OK = "ok"
 KO = "KO"
-OKSYS = 0 #for linux
-KOSYS = 1 #for linux
+OKSYS = 0  # for linux
+KOSYS = 1  # for linux
+
+NB_PROCS = MP.cpu_count()  # current cpu number of proc
+NAME_OS = platform.system()  # 'Linux' or 'Windows'
 
-NB_PROCS = MP.cpu_count()   #current cpu number of proc
-NAME_OS = platform.system() #'Linux' or 'Windows'
 
 ##########################################################################
 # utilities
@@ -57,10 +59,10 @@ NAME_OS = platform.system() #'Linux' or 'Windows'
 
 def okToSys(aResult, verbose=False):
   """to get windows or linux result of script"""
-  
+
   def extendList(alist):
     """utility extend list of lists of string results with ok or KO"""
-    #bad: list(itertools.chain.from_list(alist)) iterate on str
+    # bad: list(itertools.chain.from_list(alist)) iterate on str
     res = []
     if type(alist) != list:
       return [alist]
@@ -71,19 +73,19 @@ def okToSys(aResult, verbose=False):
         else:
            res.extend(extendList(i))
     return res
-      
+
   resList = extendList(aResult)
   if resList == []:
     if verbose: print("WARNING: result no clear: []")
     return KOSYS
-    
+
   rr = OK
   for ri in resList:
     if ri[0:2] != OK:
-      if verbose: print ri
+      if verbose: print(ri)
       rr = KO
 
-  if verbose: print("INFO: result: %s" % rr)
+  if verbose: print(("INFO: result: %s" % rr))
   if rr == OK:
     return OKSYS
   else:
@@ -116,22 +118,22 @@ class ArgRange(object):
 def exec_command(cmd, verbose=False):
   """Exec ONE command with popen"""
 
-  time.sleep(3) #wait for (MPI) flush files
-  if verbose: print("launch process:\n  %s" % cmd)
+  time.sleep(3)  # wait for (MPI) flush files
+  if verbose: print(("launch process:\n  %s" % cmd))
   try:
-    pipe = SP.Popen(cmd, shell=True, stdout=SP.PIPE, stderr=SP.PIPE )
+    pipe = SP.Popen(cmd, shell=True, stdout=SP.PIPE, stderr=SP.PIPE)
   except Exception as e:
     result = KO + " ERROR: we have a problem popen on: %s" % PP.pformat(cmd)
     return result
-  
+
   (out, error) = pipe.communicate()
   pipe.wait()
-  
+
   print(out)
   print(error)
+
   result = OK + " INFO: seems final ok for: %s" % PP.pformat(cmd)
-  time.sleep(3) #wait for (MPI) flush files
+  time.sleep(3)  # wait for (MPI) flush files
   return result
 
 
@@ -158,7 +160,7 @@ def force_DISTENE_LICENSE_FILE():
     #export PATH=/export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing/bin:$PATH
     #do not work prefer set before salome launch
     OPENMPI_INSTALL_DIR = "/export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing/bin"
-    sys.path.insert(0, OPENMPI_INSTALL_DIR) 
+    sys.path.insert(0, OPENMPI_INSTALL_DIR)
     #INSTALL_DIR = /export/home/prerequisites_SALOME_780_LAURENT/openmpi-1.8.4/FROM_nothing
     for i in sys.path[0:10]: print "PATH",i
     """
@@ -168,18 +170,18 @@ def force_DISTENE_LICENSE_FILE():
 
 ##########################################################################
 def launchMultithread(args):
-  if verbose: print("INFO: launchMultithread for %s" % NAME_OS)
-  
+  if verbose: print(("INFO: launchMultithread for %s" % NAME_OS))
+
   if NAME_OS == 'Linux':
-    #--out is ONE file: basename_tetra_hpc.mesh
-    outputMulti = os.path.splitext(args.inputFile)[0] + "_tetra_hpc.mesh" #only one file if Multithread
+    # --out is ONE file: basename_tetra_hpc.mesh
+    outputMulti = os.path.splitext(args.inputFile)[0] + "_tetra_hpc.mesh"  # only one file if Multithread
     outputs = os.path.splitext(args.outputFiles)[0]
-    outputMultiAsMpi = os.path.splitext(args.outputFiles)[0] + ".000001.mesh" #create one output file named as only one from mpi 
+    outputMultiAsMpi = os.path.splitext(args.outputFiles)[0] + ".000001.mesh"  # create one output file named as only one from mpi
     cmd = "mg-tetra_hpc.exe --max_number_of_threads %i --in %s --gradation %s --max_size %s --min_size %s; cp %s %s; ls -alt %s*; " % \
           (args.number, args.inputFile, args.gradation, args.max_size, args.min_size, outputMulti, outputMultiAsMpi, outputs)
   else:
-    return KO +  " ERROR: unknown operating system: %s" % NAME_OS
-  
+    return KO + " ERROR: unknown operating system: %s" % NAME_OS
+
   result = exec_command(cmd, verbose=True)
   return result
 
@@ -187,11 +189,11 @@ def launchMultithread(args):
 
 ##########################################################################
 def launchMpi(args):
-  if verbose: print("INFO: launchMpi for %s" % NAME_OS)
-  
-  if NAME_OS == 'Linux':  
-    cmd  = ""
-    
+  if verbose: print(("INFO: launchMpi for %s" % NAME_OS))
+
+  if NAME_OS == 'Linux':
+    cmd = ""
+
     """ compile libmeshgems_mpi.so: no needs
     COMPILDIR=os.getenv("MESHGEMSHOME") + "/stubs"
     TARGETDIR=os.getenv("MESHGEMSHOME") + "/lib/Linux_64"
@@ -203,8 +205,8 @@ def launchMpi(args):
     cmd += "mpirun -n %i mg-tetra_hpc_mpi.exe --in %s --out %s --gradation %s --max_size %s --min_size %s; ls -alt %s*; " % \
           (args.number, args.inputFile, args.outputFiles, args.gradation, args.max_size, args.min_size, outputs)
   else:
-    return KO +  " ERROR: unknown operating system: %s" % NAME_OS
-  
+    return KO + " ERROR: unknown operating system: %s" % NAME_OS
+
   result = exec_command(cmd, verbose=True)
   return result
 
@@ -215,15 +217,15 @@ def launchMpi(args):
 
 if __name__ == '__main__':
   parser = AP.ArgumentParser(description='launch tetra_hpc.exe or tetra_hpc_mpi.exe mesh computation', argument_default=None)
-  #./mg-tetra_hpc.py -n 3 --in=/tmp/GHS3DPRL.mesh --out=/tmp/GHS3DPRL_out.mesh --gradation=1.05 --min_size=0.001 --max_size=1.1 --multithread no > /tmp/tetrahpc.log
+  # ./mg-tetra_hpc.py -n 3 --in=/tmp/GHS3DPRL.mesh --out=/tmp/GHS3DPRL_out.mesh --gradation=1.05 --min_size=0.001 --max_size=1.1 --multithread no > /tmp/tetrahpc.log
 
   parser.add_argument(
-    '-v', '--verbose', 
+    '-v', '--verbose',
     help='set verbose, for debug',
     action='store_true',
   )
   parser.add_argument(
-    '-n', '--number', 
+    '-n', '--number',
     help='if multithread: number of threads, else distributed: number of processes MPI',
     choices=[ArgRange(1, 999999)],
     type=int,
@@ -231,13 +233,13 @@ if __name__ == '__main__':
     default=1,
   )
   parser.add_argument(
-    '-m', '--multithread', 
+    '-m', '--multithread',
     help='launch tetra_hpc multithread instead tetra_hpc distributed (MPI)',
     choices=["no", "yes"],
     default='no',
   )
   parser.add_argument(
-    '-g', '--gradation', 
+    '-g', '--gradation',
     help='size ratio adjacent cell, default 0 is 1.05',
     type=float,
     choices=[ArgRange(0.0, 3.0)],
@@ -245,7 +247,7 @@ if __name__ == '__main__':
     default='0'
   )
   parser.add_argument(
-    '-si', '--min_size', 
+    '-si', '--min_size',
     help='min size cell, default 0 is no constraint',
     type=float,
     choices=[ArgRange(0.0, 9e99)],
@@ -253,7 +255,7 @@ if __name__ == '__main__':
     default='0'
   )
   parser.add_argument(
-    '-sa', '--max_size', 
+    '-sa', '--max_size',
     help='max size cell, default 0 is no constraint',
     type=float,
     choices=[ArgRange(0.0, 9e99)],
@@ -261,20 +263,20 @@ if __name__ == '__main__':
     default='0'
   )
   parser.add_argument(
-    '-i', '--inputFile', 
+    '-i', '--inputFile',
     help='input file name',
-    #nargs='?',
+    # nargs='?',
     metavar='.../inputFile.mesh'
   )
   parser.add_argument(
-    '-o', '--outputFiles', 
+    '-o', '--outputFiles',
     help='output basename file(s) name',
-    #nargs='?',
+    # nargs='?',
     metavar='.../outputFile.mesh'
   )
   """
   parser.add_argument(
-    '-x', '--xoneargument', 
+    '-x', '--xoneargument',
     nargs='?',
     metavar='0|1',
     choices=['0', '1'],
@@ -282,39 +284,39 @@ if __name__ == '__main__':
     default='0'
   )
   """
-  
-    
+
+
   """
-  args is Namespace, may use it as global to store 
+  args is Namespace, may use it as global to store
   parameters, data, used arrays and results and other...
   """
   args = parser.parse_args()
-  
+
   verbose = args.verbose
-  if verbose: print("INFO: args:\n%s" % PP.pformat(args.__dict__))
+  if verbose: print(("INFO: args:\n%s" % PP.pformat(args.__dict__)))
 
-  if len(sys.argv) == 1: #no args as --help
+  if len(sys.argv) == 1:  # no args as --help
     parser.print_help()
     sys.exit(KOSYS)
 
   if args.inputFile == None:
-    print("\nERROR: Nothing to do: no input files\n\n%s\n" % PP.pformat(args))
+    print(("\nERROR: Nothing to do: no input files\n\n%s\n" % PP.pformat(args)))
     parser.print_help()
     sys.exit(KOSYS)
 
   if args.outputFiles == None:
     tmp, _ = os.path.splitext(args.inputFile)
     args.outputFiles = tmp + "_out.mesh"
-    print("\nWARNING: Default ouput files: %s" % args.outputFiles)
-   
+    print(("\nWARNING: Default ouput files: %s" % args.outputFiles))
+
   force_DISTENE_LICENSE_FILE()
 
-  print("INFO: mg-tetra_hpc.py assume licence file set:\n  DLIM8VAR=%s\n  DISTENE_LICENSE_FILE=%s" % \
-       (os.getenv("DLIM8VAR"), os.getenv("DISTENE_LICENSE_FILE")))
-  if args.multithread == "yes": 
+  print(("INFO: mg-tetra_hpc.py assume licence file set:\n  DLIM8VAR=%s\n  DISTENE_LICENSE_FILE=%s" % \
+       (os.getenv("DLIM8VAR"), os.getenv("DISTENE_LICENSE_FILE"))))
+
+  if args.multithread == "yes":
     result = launchMultithread(args)
   else:
     result = launchMpi(args)
   sys.exit(okToSys(result, verbose=True))
-  
+