Commit cec7a481 authored by Renato Bellotti's avatar Renato Bellotti
Browse files

Improved code readibility

parent 68c546d5
...@@ -9,28 +9,41 @@ Script that launches OPAL simulations ...@@ -9,28 +9,41 @@ Script that launches OPAL simulations
""" """
import sys,os,shutil,glob import sys
import subprocess import os
import shutil
import glob
from simulation import Simulation from simulation import Simulation
from opaldict import OpalDict from opaldict import OpalDict
def getPaths(path, pattern, name): def getTemplatePaths(parentDir, pattern):
result = glob.glob(os.path.join(path,pattern)) '''
Return the paths to template files.
The template files are files in the parent directory
that follow the given pattern.
Returns
=======
The paths to the template files.
'''
result = glob.glob(os.path.join(parentDir, pattern))
if not result: if not result:
print('No '+name+' file ('+pattern+') found') print(f'No template file ({pattern}) found')
sys.exit() sys.exit()
return result return result
def getBaseName(inputfilePath): def getBaseName(inputfilePath):
templates = getPaths(inputfilePath, '*.tmpl', 'template') templates = getTemplatePaths(inputfilePath, '*.tmpl')
name = templates[0].split('/')[-1][:-5] #NOTE: choose first (alphanumeric order) *.tmpl file by default # NOTE: choose first (alphanumeric order) *.tmpl file by default
if os.path.isfile(os.path.join('.',name+'.data')): name = templates[0].split('/')[-1][:-5]
if os.path.isfile(os.path.join('.', name + '.data')):
return name return name
print('Template and data filename do not match, '+name+'.data expected') print(f'Template and data filename do not match, {name}.data expected')
sys.exit() sys.exit()
...@@ -57,8 +70,9 @@ def printUsage(): ...@@ -57,8 +70,9 @@ def printUsage():
print("Important: runOPAL is currently not compatible with the command SAMPLE") print("Important: runOPAL is currently not compatible with the command SAMPLE")
def checkCompat(tmplFile, incompatible): #NOTE: SAMPLE command not compatible with runOPAL (issue #8) # NOTE: SAMPLE command not compatible with runOPAL (issue #8)
templateFile = open(tmplFile,'r') def checkCompat(tmplFile, incompatible):
templateFile = open(tmplFile, 'r')
for line in templateFile: for line in templateFile:
if line.startswith('//'): if line.startswith('//'):
continue continue
...@@ -70,52 +84,53 @@ def checkCompat(tmplFile, incompatible): #NOTE: SAMPLE command not compatible wi ...@@ -70,52 +84,53 @@ def checkCompat(tmplFile, incompatible): #NOTE: SAMPLE command not compatible wi
def traverseRanges(list, opaldict, args, doNobatch): def traverseRanges(list, opaldict, args, doNobatch):
""" """
Traverse all possible combinations of range variable values. Start simulation Traverse all possible combinations of range variable values.
once all range variables are fixed to a value. A list entry has the following Start simulation once all range variables are fixed to a value.
structure: A list entry has the following structure:
['name of var', start_value, end_value, step_value] ['name of var', start_value, end_value, step_value]
""" """
head = list[0] head = list[0]
tail = list[1:] tail = list[1:]
curval = head[1][0] curval = head[1][0]
endval = head[1][1] endval = head[1][1]
step = head[1][2] step = head[1][2]
qid = -1 qid = -1
if curval > endval: if curval > endval:
print('range is empty, start value',curval,'needs to be higher than end value',endval) print('range is empty, start value', curval,
'needs to be higher than end value', endval)
while curval <= endval: while curval <= endval:
opaldict[head[0]] = curval opaldict[head[0]] = curval
if len(tail) == 0: if len(tail) == 0:
#run simulation # run simulation
sim = Simulation(opaldict) sim = Simulation(opaldict)
qid = sim.run(*args) qid = sim.run(*args)
if doNobatch: if doNobatch:
print("... finished!\n") print("... finished!\n")
else:
print("SGE-ID= {}\n".format(qid))
else: else:
traverseRanges(tail, opaldict, args, doNobatch) print("SGE-ID= {}\n".format(qid))
curval = curval + step else:
traverseRanges(tail, opaldict, args, doNobatch)
curval = curval + step
def main(argv): def main(argv):
""" """
main method main method
""" """
N = -1 # a running number; if given use it to label directory! N = -1 # a running number; if given use it to label directory!
quiet = False quiet = False
doTest = False doTest = False
doKeep = False doKeep = False
doNobatch = False doNobatch = False
doOptimize = True #NOTE: this flag is opposite of --noopt doOptimize = True # NOTE: this flag is opposite of --noopt
queue = "" queue = ""
info = 6 info = 6
hypert = 0 hypert = 0
qid = -1 qid = -1
inputfilePath = None inputfilePath = None
baseFileName = None baseFileName = None
for arg in argv: for arg in argv:
if arg.startswith("--help"): if arg.startswith("--help"):
printUsage() printUsage()
...@@ -139,47 +154,47 @@ def main(argv): ...@@ -139,47 +154,47 @@ def main(argv):
elif arg.startswith("--hypert"): elif arg.startswith("--hypert"):
hypert = int(arg.split("=")[1]) hypert = int(arg.split("=")[1])
elif arg.startswith("-"): elif arg.startswith("-"):
print(arg,'is not a valid option, see --help for the available options') print(arg, 'is not a valid option, see --help for the available options')
exit() exit()
# safety check # safety check
if os.getcwd() == os.environ.get('TEMPLATES') or os.getcwd() == os.environ.get('OPTIMIZER'): if os.getcwd() == os.environ.get('TEMPLATES') or os.getcwd() == os.environ.get('OPTIMIZER'):
print('Working directory is the same as the TEMPLATES or OPTIMIZER directory! This is not allowed... bye!') print('Working directory is the same as the TEMPLATES or OPTIMIZER directory! This is not allowed... bye!')
sys.exit() sys.exit()
# determine what kind of job should be ran, simulation by default # determine what kind of job should be ran, simulation by default
if doOptimize and os.environ.get('OPTIMIZER'): if doOptimize and os.environ.get('OPTIMIZER'):
if quiet == False: if not quiet:
print('job type: OPTIMIZATION') print('job type: OPTIMIZATION')
inputfilePath = os.environ.get('OPTIMIZER') inputfilePath = os.environ.get('OPTIMIZER')
if not (inputfilePath and glob.glob(os.path.join(inputfilePath,'*.tmpl'))): if not (inputfilePath and glob.glob(os.path.join(inputfilePath, '*.tmpl'))):
if quiet == False: if not quiet:
print('job type: SIMULATION') print('job type: SIMULATION')
doOptimize = False doOptimize = False
if os.environ.get('TEMPLATES'): if os.environ.get('TEMPLATES'):
inputfilePath = os.environ.get('TEMPLATES') inputfilePath = os.environ.get('TEMPLATES')
elif (glob.glob(os.path.join('.','*.tmpl'))): elif (glob.glob(os.path.join('.', '*.tmpl'))):
inputfilePath = '../' inputfilePath = '../'
else: else:
print('Template file unknown -> exiting ...') print('Template file unknown -> exiting ...')
sys.exit() sys.exit()
#check that tmpl and data files can be found or guessed # check that tmpl and data files can be found or guessed
if not baseFileName: if not baseFileName:
baseFileName = getBaseName(inputfilePath) baseFileName = getBaseName(inputfilePath)
elif not os.path.isfile(os.path.join(inputfilePath,baseFileName+'.tmpl')): elif not os.path.isfile(os.path.join(inputfilePath, baseFileName+'.tmpl')):
print(baseFileName+'.tmpl cannot be found! Check if it exists in '+inputfilePath) print(f'{baseFileName}.tmpl cannot be found! Check if it exists in {inputfilePath}')
sys.exit() sys.exit()
if quiet == False: if not quiet:
print('baseFileName = '+baseFileName) print('baseFileName = '+baseFileName)
dataFile = baseFileName + '.data' dataFile = baseFileName + '.data'
tmplFile = os.path.join(inputfilePath,baseFileName+'.tmpl') tmplFile = os.path.join(inputfilePath, baseFileName + '.tmpl')
oinpFile = baseFileName + '.in' # the resulting OPAL input file oinpFile = baseFileName + '.in' # the resulting OPAL input file
checkCompat(tmplFile, ['SAMPLE']) # check compatibility checkCompat(tmplFile, ['SAMPLE']) # check compatibility
#create the dictionary # create the dictionary
opaldict = OpalDict(dataFile) opaldict = OpalDict(dataFile)
# check if template values must be changed # check if template values must be changed
# if so add update the dictionary with the default values # if so add update the dictionary with the default values
...@@ -188,16 +203,18 @@ def main(argv): ...@@ -188,16 +203,18 @@ def main(argv):
if not opaldict.hasRanges(): if not opaldict.hasRanges():
sim = Simulation(opaldict) sim = Simulation(opaldict)
qid = sim.run(N, baseFileName, inputfilePath, tmplFile, oinpFile, doTest, doKeep, doNobatch, doOptimize, info, queue, hypert, quiet) qid = sim.run(N, baseFileName, inputfilePath, tmplFile, oinpFile,
doTest, doKeep, doNobatch, doOptimize,
info, queue, hypert, quiet)
if doNobatch: if doNobatch:
if quiet == False: if not quiet:
print( "... finished!\n") print("... finished!\n")
#else: #else:
# print( "SGE-ID= {}\n".format(qid)) # print( "SGE-ID= {}\n".format(qid))
else: else:
ranges = opaldict.Range() ranges = opaldict.Range()
#create range toplevel dir # create range toplevel dir
dirname = baseFileName dirname = baseFileName
for p in opaldict.uservars: for p in opaldict.uservars:
dirname += "_" + str(p[0]) + "=" + str(p[1]) dirname += "_" + str(p[0]) + "=" + str(p[1])
...@@ -206,27 +223,29 @@ def main(argv): ...@@ -206,27 +223,29 @@ def main(argv):
# If there's already a directory remove it... # If there's already a directory remove it...
if os.path.isdir(dirname): if os.path.isdir(dirname):
if doKeep: if doKeep:
print( 'KEEP existing directory ', dirname) print('KEEP existing directory ', dirname)
else: else:
print( 'REMOVE existing directory', dirname) print('REMOVE existing directory', dirname)
shutil.rmtree(dirname) shutil.rmtree(dirname)
# create directory and change to the directory # create directory and change to the directory
os.mkdir(dirname) os.mkdir(dirname)
else: else:
os.mkdir(dirname) os.mkdir(dirname)
os.chdir(dirname) os.chdir(dirname)
print(ranges) print(ranges)
#run simulations of all possible combinations # run simulations of all possible combinations
args = [N, baseFileName, inputfilePath, tmplFile, oinpFile, doTest, doKeep, doNobatch, doOptimize, info, queue, hypert, quiet] args = [N, baseFileName, inputfilePath, tmplFile, oinpFile,
doTest, doKeep, doNobatch, doOptimize,
info, queue, hypert, quiet]
traverseRanges(list(ranges.items()), opaldict, args, doNobatch) traverseRanges(list(ranges.items()), opaldict, args, doNobatch)
# clean up # clean up
os.system("rm -f *.bak ") os.system("rm -f *.bak ")
os.chdir("..") os.chdir("..")
#call main
# call main
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv[1:]) main(sys.argv[1:])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment