def plotValues(fittedValues, modelScript, expData): baseName = os.path.dirname(os.path.abspath(__file__)) workspace = toolbox.getWorkspace(modelScript,baseName) os.chdir(workspace) figFilePng = os.path.join(workspace,'fittedResults2.png') figFilePdf = os.path.join(workspace,'fittedResults2.pdf') import matplotlib.pyplot as plt plt.plot(expData[0],expData[1],'o',fittedValues[0],fittedValues[1],'x') plt.legend(['Data', 'Fit']) plt.title('Least-squares fit to data') plt.savefig(figFilePng, bbox_inches='tight') plt.savefig(figFilePdf, bbox_inches='tight') if not verbose:plt.show() return fittedValues
def plotValues(fittedValues, modelScript, expData): baseName = os.path.dirname(os.path.abspath(__file__)) workspace = toolbox.getWorkspace(modelScript, baseName) os.chdir(workspace) figFilePng = os.path.join(workspace, 'fittedResults2.png') figFilePdf = os.path.join(workspace, 'fittedResults2.pdf') import matplotlib.pyplot as plt plt.plot(expData[0], expData[1], 'o', fittedValues[0], fittedValues[1], 'x') plt.legend(['Data', 'Fit']) plt.title('Least-squares fit to data') plt.savefig(figFilePng, bbox_inches='tight') plt.savefig(figFilePdf, bbox_inches='tight') if not verbose: plt.show() return fittedValues
def cleanFiles(file, baseName=os.getcwd(), verb=False): workspace = toolbox.getWorkspace(file, baseName, verb) if os.path.isdir(workspace): if verb: print "workspace: %s found" % (workspace) for path, subdirs, names in os.walk(workspace): for name in names: if name.split('.')[-1] not in extToKeep: try: os.remove(os.path.join(path, name)) if verb: print 'removing "%s"' % os.path.join(path, name) except: print 'warning: "%s" is locked' % os.path.join( path, name) pass else: raise Exception('workspace %s not found' % (workspace))
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 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 runPostPro(file, workspace=None): baseName = os.path.dirname(os.path.abspath(__file__)) if workspace is None: workspace = toolbox.getWorkspace(file, baseName) if not os.path.isdir(workspace): raise Exception("cannot run postPro on empty workspace!") else: odbName = None for name in os.listdir(workspace): if name.split('.')[-1] == 'odb': odbName = name break if odbName is None: raise Exception("no odb file in %s!" % (workspace)) versionInfo = sys.version_info assert versionInfo[0] == 2, "need to use python 2.x version" module = toolbox.fileToModule(file) if versionInfo[1] < 7: _temp = __import__(module, globals(), locals(), ['postPro'], -1) else: # in python 2.7 and above, __import__ should be replaced by importlib.import_module import importlib _temp = importlib.import_module(module) os.chdir(workspace) _temp.postPro(odbName) os.chdir(baseName)
def runAbaqus(file, runType, keep=False, gui=False): baseName = os.path.dirname(os.path.abspath(__file__)) workspace = toolbox.getWorkspace(file, baseName) if runType == 'cae': runCae(file, baseName, workspace, keep=keep, gui=gui) elif runType == 'inp': runInp(file, baseName, workspace, keep=keep)
def cleanWorkspace(file, baseName=os.getcwd(), verb=False): toolbox.cleanDir(toolbox.getWorkspace(file, baseName, verb))