From 61090c955d6590e98d4392b04cb3e9173f549106 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Fri, 9 Oct 2015 05:51:58 +0000 Subject: [PATCH] removed code doubling --- lib/damask/test/test.py | 9 +----- lib/damask/util.py | 21 ++++++++++-- processing/misc/yieldSurface.py | 41 +++++------------------- processing/pre/seeds_fromDistribution.py | 21 ++---------- 4 files changed, 31 insertions(+), 61 deletions(-) diff --git a/lib/damask/test/test.py b/lib/damask/test/test.py index 1820a2d5e..08e659648 100644 --- a/lib/damask/test/test.py +++ b/lib/damask/test/test.py @@ -227,15 +227,8 @@ class Test(): def execute_inCurrentDir(self,cmd,streamIn=None): - initialPath=os.getcwd() - os.chdir(self.dirCurrent()) logging.info(cmd) - process = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE,stderr = subprocess.PIPE,stdin=subprocess.PIPE) - if streamIn != None: - out,error = process.communicate(streamIn.read()) - else: - out,error = process.communicate() - os.chdir(initialPath) + out,error = damask.util.execute(cmd,streamIn,self.dirCurrent()) logging.info(error) logging.debug(out) diff --git a/lib/damask/util.py b/lib/damask/util.py index 2985130ad..f142df2b8 100644 --- a/lib/damask/util.py +++ b/lib/damask/util.py @@ -1,9 +1,9 @@ # -*- coding: UTF-8 no BOM -*- # damask utility functions -import sys,time,random,threading +import sys,time,random,threading,os,subprocess,shlex import numpy as np -from optparse import OptionParser, Option +from optparse import Option # ----------------------------- def croak(what, @@ -398,3 +398,20 @@ def curve_fit_bound(f, xdata, ydata, p0=None, sigma=None, bounds=None, **kw): return popt, pcov, infodict, errmsg, ier else: return popt, pcov + + +def execute(cmd,streamIn=None,wd='./'): + ''' + executes a command in given directory and returns stdout and stderr for optional stdin + ''' + initialPath=os.getcwd() + os.chdir(wd) + process = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE,stderr = subprocess.PIPE,stdin=subprocess.PIPE) + if streamIn != None: + out,error = process.communicate(streamIn.read()) + else: + out,error = process.communicate() + os.chdir(initialPath) + + return out,error + diff --git a/processing/misc/yieldSurface.py b/processing/misc/yieldSurface.py index 9fb17e617..b320d20cd 100755 --- a/processing/misc/yieldSurface.py +++ b/processing/misc/yieldSurface.py @@ -9,7 +9,7 @@ import damask from damask.util import leastsqBound scriptID = string.replace('$Id$','\n','\\n') -scriptName = scriptID.split()[1][:-3] +scriptName = os.path.splitext(scriptID.split()[1])[0] def runFit(exponent, eqStress, dimension, criterion): global s, threads, myFit @@ -48,21 +48,6 @@ def runFit(exponent, eqStress, dimension, criterion): threads[i].join() print fitResidual -def execute(cmd,streamIn=None,wd='./'): - ''' - executes a command in given directory and returns stdout and stderr for optional stdin - ''' - initialPath=os.getcwd() - os.chdir(wd) - process = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE,stderr = subprocess.PIPE,stdin=subprocess.PIPE) - if streamIn != None: - out,error = process.communicate(streamIn.read()) - else: - out,error = process.communicate() - os.chdir(initialPath) - - return out,error - def principalStresses(sigmas): ''' computes principal stresses (i.e. eigenvalues) for a set of Cauchy stresses. @@ -163,16 +148,6 @@ def sym6to33(sigma6): sigma33[2,0] = sigma6[5]; sigma33[0,2] = sigma6[5] return sigma33 -def array2tuple(array): - '''transform numpy.array into tuple''' - try: - return tuple(array2tuple(i) for i in array) - except TypeError: - return array -def get_weight(ndim): -#more to do - return np.ones(ndim) - class Criteria(object): ''' @@ -1243,7 +1218,7 @@ def doSim(delay,thread): if not os.path.isfile('%s_%i.spectralOut'%(options.geometry,loadNo)): print('starting simulation %s from %s'%(loadNo,thread)) s.release() - execute('DAMASK_spectral -g %s -l %i'%(options.geometry,loadNo)) + damask.util.execute('DAMASK_spectral -g %s -l %i'%(options.geometry,loadNo)) else: s.release() s.acquire() @@ -1251,12 +1226,12 @@ def doSim(delay,thread): print('starting post processing for sim %i from %s'%(loadNo,thread)) s.release() try: - execute('postResults --cr f,p --co totalshear %s_%i.spectralOut'%(options.geometry,loadNo)) + damask.util.execute('postResults --cr f,p --co totalshear %s_%i.spectralOut'%(options.geometry,loadNo)) except: - execute('postResults --cr f,p %s_%i.spectralOut'%(options.geometry,loadNo)) - execute('addCauchy ./postProc/%s_%i.txt'%(options.geometry,loadNo)) - execute('addStrainTensors -l -v ./postProc/%s_%i.txt'%(options.geometry,loadNo)) - execute('addMises -s Cauchy -e ln(V) ./postProc/%s_%i.txt'%(options.geometry,loadNo)) + damask.util.execute('postResults --cr f,p %s_%i.spectralOut'%(options.geometry,loadNo)) + damask.util.execute('addCauchy ./postProc/%s_%i.txt'%(options.geometry,loadNo)) + damask.util.execute('addStrainTensors -l -v ./postProc/%s_%i.txt'%(options.geometry,loadNo)) + damask.util.execute('addMises -s Cauchy -e ln(V) ./postProc/%s_%i.txt'%(options.geometry,loadNo)) else: s.release() s.acquire() @@ -1264,7 +1239,7 @@ def doSim(delay,thread): print('reading values for sim %i from %s'%(loadNo,thread)) s.release() - refFile = open('./postProc/%s_%i.txt'%(options.geometry,loadNo)) + refFile = './postProc/%s_%i.txt'%(options.geometry,loadNo) table = damask.ASCIItable(refFile) table.head_read() if options.fitting =='equivalentStrain': diff --git a/processing/pre/seeds_fromDistribution.py b/processing/pre/seeds_fromDistribution.py index 3589e7fbf..569bb3e2f 100755 --- a/processing/pre/seeds_fromDistribution.py +++ b/processing/pre/seeds_fromDistribution.py @@ -14,21 +14,6 @@ scriptName = os.path.splitext(scriptID.split()[1])[0] mismatch = None currentSeedsName = None -def execute(cmd,streamIn=None,dir='./'): - ''' - executes a command in given directory and returns stdout and stderr for optional stdin - ''' - initialPath=os.getcwd() - os.chdir(dir) - process = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE,stderr = subprocess.PIPE,stdin=subprocess.PIPE) - if streamIn != None: - out,error = process.communicate(streamIn.read()) - else: - out,error = process.communicate() - os.chdir(initialPath) - - return out,error - #--------------------------------------------------------------------------------------------------- class myThread (threading.Thread): #--------------------------------------------------------------------------------------------------- @@ -113,7 +98,7 @@ class myThread (threading.Thread): perturbedGeomVFile.close() perturbedGeomVFile = StringIO() perturbedSeedsVFile.reset() - perturbedGeomVFile.write(execute('geom_fromVoronoiTessellation '+ + perturbedGeomVFile.write(damask.util.execute('geom_fromVoronoiTessellation '+ ' -g '+' '.join(map(str, options.grid)),streamIn=perturbedSeedsVFile)[0]) perturbedGeomVFile.reset() @@ -257,7 +242,7 @@ if os.path.isfile(os.path.splitext(options.seedFile)[0]+'.seeds'): with open(os.path.splitext(options.seedFile)[0]+'.seeds') as initialSeedFile: for line in initialSeedFile: bestSeedsVFile.write(line) else: - bestSeedsVFile.write(execute('seeds_fromRandom'+\ + bestSeedsVFile.write(damask.util.execute('seeds_fromRandom'+\ ' -g '+' '.join(map(str, options.grid))+\ ' -r %i'%options.randomSeed+\ ' -N '+str(nMicrostructures))[0]) @@ -266,7 +251,7 @@ bestSeedsUpdate = time.time() # ----------- tessellate initial seed file to get and evaluate geom file bestSeedsVFile.reset() initialGeomVFile = StringIO() -initialGeomVFile.write(execute('geom_fromVoronoiTessellation '+ +initialGeomVFile.write(damask.util.execute('geom_fromVoronoiTessellation '+ ' -g '+' '.join(map(str, options.grid)),bestSeedsVFile)[0]) initialGeomVFile.reset() initialGeomTable = damask.ASCIItable(initialGeomVFile,None,labeled=False,readonly=True)