def writeErrorFile(workspace,modelScript,p,pCall1,pCall2='not run yet'): feErrorFile = os.path.join(workspace,'notRun.txt') import counter with open(feErrorFile, 'w') as file: file.write('running abaqus cae on %s returned %s\n'%(toolbox.getFileName(modelScript), pCall1)) file.write('running post pro on %s returned %s\n'%(toolbox.getFileName(modelScript), pCall2)) file.write('parameter inputs: %s\n'%(p)) file.write('run number: %s\n'%(counter.NFeval))
def writeErrorFile(workspace,modelScript,p,pCall1,pCall2='not run yet'): feErrorFile = os.path.join(workspace,'notRun.txt') global NFeval with open(feErrorFile, 'w') as file: file.write('running abaqus cae on %s returned %s\n'%(toolbox.getFileName(modelScript), pCall1)) file.write('running post pro on %s returned %s\n'%(toolbox.getFileName(modelScript), pCall2)) file.write('parameter inputs: %s\n'%(p)) file.write('run number: %s\n'%(NFeval))
def runAbaqusCmd(dirs, cmd, keep=False, gui=False): keepFName = ' ' for file in loopOn(dirs): if not os.path.isfile(file): print "module %s not found!" % (file) else: pyKeep = keepFName + '.py' if pyKeep in file: continue runType = 'cae' if (toolbox.getFileExt(file) == 'inp'): runType = 'inp' keepFName = toolbox.getFileName(file) pyfile = file.replace('.inp', '.py') else: pyfile = file if cmd == 'rerun': cleanWorkspace(file) runAbaqus(file, runType, keep=keep, gui=gui) elif cmd == 'run': runAbaqus(file, runType, keep=keep, gui=gui) elif cmd == 'clean': cleanWorkspace(file) elif cmd == 'postPro': if os.path.isfile(pyfile): runPostPro(pyfile) elif cmd == 'runPost': runAbaqus(file, runType, keep=keep, gui=gui) if os.path.isfile(pyfile): runPostPro(pyfile) elif cmd == 'rerunPost': cleanWorkspace(file) runAbaqus(file, runType, keep=keep, gui=gui) if os.path.isfile(pyfile): runPostPro(pyfile) else: raise Exception("How the hell did I get here?")
def getParameters(_p={}): import specificProjectsTools.beanShaped as beanShaped param = {} param['modelName'] = toolbox.getFileName(__modpath__) param['interfaceType'] = 'CohesiveFriction' #'Frictionless', 'Friction' param['matType'] = 'Holzapfel' #or 'Holzapfel' or 'neoHooke' param.update(_p) return beanShaped.getParameters(param)
def runModel(p,modelScript,modelsDir,verbose): ''' run abaqus models: 1/ create a working directory for abaqus (in workspace\name_of_modelsDir_from_common_path_with_current_directory) 2/ runs an abaqus cae analysis in the working directory (all abaqus files written in that directory): abaqus cae noGUI=path_to_modelScript -- p 3/ runs an abaqus post-processing analysis by looking for the postPro function defined in the modelScript with argument the odb file of the previously run model ''' #1/ create working directory baseName = os.getcwd() import sys if baseName not in sys.path:sys.path.append(baseName) filePath = os.path.join(modelsDir,modelScript) workspace = toolbox.getWorkspace(filePath,baseName=baseName) if not(os.path.isdir(workspace)): try: os.makedirs(workspace) except WindowsError: print("file(s) probably locked!\n") #2/ runs abaqus cae # run abaqus analysis (function of parameters p) in workspace os.chdir(workspace) if verbose: print "running abaqus cae on %s"%(toolbox.getFileName(filePath)) cmd = 'abaqus cae noGUI=%s'%(filePath) try:#multiparam opti paramString = ' '.join(map(str,p)) except(TypeError):#scalar opti paramString = str(p) cmd += ' -- %s > %s 2>&1'%(paramString,'exeCalls.txt') if verbose: print 'cmd= ',cmd pCall1 = subprocess.call(cmd, shell=True) with open('exeCalls.txt', 'r') as file: lastLine = file.readlines()[-1] os.chdir(baseName) #3/ run abaqus postPro -- needs to be called with abaqus python as abaqus-specific modules are needed!! # solution: run in a new subprocess the file runPostPro.py called with the appropriate modelScript and working directory cmd = r"abaqus python opti4AbqTools\runPostPro.py %s %s"%(filePath,workspace) pCall2 = subprocess.call(cmd, shell=True) if pCall2:#the post pro function has not run properly --> writes an error file writeErrorFile(workspace,modelScript,p,pCall1,pCall2) raise Exception("!! something has gone wrong, check notRun.txt") else:# reads the written output of the post-processing function as a float feOutputFile = os.path.join(workspace,'output.dat')#could be generalised to allow the user to input a fileName! with open(feOutputFile, 'r') as file: output = zip(*(map(float,line.split()) for line in file)) return output
def runCae(file, baseName, workspace, keep=False, gui=False): resFile = os.path.join(workspace, 'abaqus.rpy') if runNeeded(file, workspace, resFile): os.chdir(workspace) print "running abaqus cae on %s" % (toolbox.getFileName(file)) cmd = 'abaqus cae ' if (gui): cmd += 'script=%s -- %s' % (file, str(baseName)) else: cmd += 'noGUI=%s -- %s > %s 2>&1' % (file, str(baseName), 'exeCalls.txt') import subprocess p = subprocess.call(cmd, shell=True) if p: print( "!! something has gone wrong, check exeCalls.txt and/or rpy file" ) if not keep: cleanFiles(file, baseName=baseName) os.chdir(baseName)
def runModel(p,modelScript,modelsDir): ''' run abaqus models: 1/ create a working directory for abaqus (in workspace\name_of_modelsDir_from_common_path_with_current_directory) 2/ runs an abaqus cae analysis in the working directory (all abaqus files written in that directory): abaqus cae noGUI=path_to_modelScript -- p 3/ runs an abaqus post-processing analysis by looking for the postPro function defined in the modelScript with argument the odb file of the previously run model ''' #1/ create working directory baseName = os.getcwd()#os.path.dirname(os.path.abspath(__file__)) import sys if baseName not in sys.path:sys.path.append(baseName) filePath = os.path.join(modelsDir,modelScript) workspace = toolbox.getWorkspace(filePath,baseName=baseName) if not(os.path.isdir(workspace)): try: os.makedirs(workspace) except WindowsError: print("file(s) probably locked!\n") # run abaqus analysis (function of parameters p) in workspace os.chdir(workspace) #2/ runs abaqus cae if verbose: print "running abaqus cae on %s"%(toolbox.getFileName(filePath)) cmd = 'abaqus cae noGUI=%s'%(filePath) paramString = str(p) cmd += ' -- %s > %s 2>&1'%(paramString,'exeCalls.txt') if verbose: print 'cmd= ',cmd pCall1 = subprocess.call(cmd, shell=True) os.chdir(baseName) #3/ run abaqus postPro -- needs to be called with abaqus python as abaqus-specific modules are needed!! # solution: run in a new subprocess the file runPostPro.py called with the appropriate modelScript and working directory cmd = 'abaqus python runPostPro.py %s %s'%(filePath,workspace) pCall2 = subprocess.call(cmd, shell=True) if pCall2:#the post pro function has not run properly --> writes an error file writeErrorFile(workspace,modelScript,p,pCall1,pCall2) raise Exception("!! something has gone wrong, check notRun.txt") else:# reads the written output of the post-processing function as a float feOutputFile = os.path.join(workspace,'output.ascii')#could be generalised to allow the user to input a fileName! with open(feOutputFile, 'r') as file: output = zip(*(map(float,line.split()) for line in file)) return output
def runInp(file, baseName, workspace, keep=False): resFile = os.path.join( workspace, 'exeCalls.txt') # no abaqus.rpy file for this method!! if (toolbox.getFileExt(file) == 'inp') and (runNeeded( file, workspace, resFile)): shutil.copy(file, workspace) jobName = toolbox.getFileName(file) os.chdir(workspace) print "running abaqus on %s" % (jobName) cmd = 'abaqus job=%s interactive > %s 2>&1' % (jobName, 'exeCalls.txt') import subprocess if toolbox.isUnix(): p = subprocess.call(cmd, close_fds=True) else: p = subprocess.call(cmd, shell=True) if p: print( "!! something has gone wrong, check exeCalls.txt and /or msg file" ) if not keep: cleanFiles(file, baseName=baseName) os.chdir(baseName)