示例#1
0
文件: ParamsFit.py 项目: puwe/DAMASK
def damask_run(geomfile, loadfile):
    # simulation
    check = subprocess.call(
        shlex.split("DAMASK_spectral --geometry %s --load %s" %
                    (geomfile, loadfile)))
    jobname = geomfile.split('.')[0] + '_' + loadfile.split('.')[0]
    if check == 0:
        subprocess.call(
            shlex.split("postResults --cr f,p %s.spectralOut" %
                        (jobname)))  #--co totalshear
        postfn = jobname + ".txt"
        subprocess.call(shlex.split("addCauchy ./postProc/%s" % (postfn)))
        subprocess.call(
            shlex.split("addStrainTensors -0 -v ./postProc/%s" % (postfn)))
        subprocess.call(
            shlex.split("addMises -s Cauchy -e ln'('V')' ./postProc/%s" %
                        (postfn)))

        table = damask.ASCIItable("./postProc/%s" % (postfn), readonly=True)
        table.head_read()
        table.data_readArray(["4_ln(V)",
                              "4_Cauchy"])  #(["Mises(ln(V))","Mises(Cauchy)"])
        # or (["totalshear","Mises(Cauchy)"])

        sim_flow = FlowCurve(data=table.data, Unit='Pa')
        sim_data_n = len(sim_flow.data)
        sim_data_acc = [[sim_flow.acc_strain[i], sim_flow.von_stress[i]]
                        for i in range(sim_data_n)]
        np.savetxt("./postProc/%s_acc.dat" % (jobname), sim_data_acc)
        sim_data_plastic = [[sim_flow.strain[i], sim_flow.stress[i]]
                            for i in range(sim_data_n)]
        np.savetxt("./postProc/%s_plastic.dat" % (jobname), sim_data_plastic)
        #return sim_data_acc
    else:
        print("damask failure")
示例#2
0
文件: test.py 项目: puwe/DAMASK
    def compare_TablesStatistically(
            self,
            files=[None, None],  # list of file names
            columns=[None],  # list of list of column labels (per file)
            meanTol=1.0e-4,
            stdTol=1.0e-6,
            preFilter=1.0e-9):
        """
    calculate statistics of tables

    threshold can be used to ignore small values (a negative number disables this feature)
    """
        if not (isinstance(files, Iterable) and not isinstance(files, str)
                ):  # check whether list of files is requested
            files = [str(files)]

        tables = [
            damask.ASCIItable(name=filename, readonly=True)
            for filename in files
        ]
        for table in tables:
            table.head_read()

        columns += [columns[0]] * (len(files) - len(columns)
                                   )  # extend to same length as files
        columns = columns[:len(files)]  # truncate to same length as files

        for i, column in enumerate(columns):
            if column is None:
                columns[i] = tables[i].labels(
                    raw=True)  # if no column is given, read all

        logging.info('comparing ASCIItables statistically')
        for i in xrange(len(columns)):
            columns[i] = columns[0]  if not columns[i] else \
                       ([columns[i]] if not (isinstance(columns[i], Iterable) and not isinstance(columns[i], str)) else \
                         columns[i]
                       )
            logging.info(files[i] + ':' + ','.join(columns[i]))

        if len(files) < 2:
            return True  # single table is always close to itself...

        data = []
        for table, labels in zip(tables, columns):
            table.data_readArray(labels)
            data.append(table.data)
            table.close()

        for i in xrange(1, len(data)):
            delta = data[i] - data[i - 1]
            normBy = (np.abs(data[i]) + np.abs(data[i - 1])) * 0.5
            normedDelta = np.where(normBy > preFilter, delta / normBy, 0.0)
            mean = np.amax(np.abs(np.mean(normedDelta, 0)))
            std = np.amax(np.std(normedDelta, 0))
            logging.info('mean: {:f}'.format(mean))
            logging.info('std: {:f}'.format(std))

        return (mean < meanTol) & (std < stdTol)
示例#3
0
文件: test.py 项目: puwe/DAMASK
    def compare_Array(self, File1, File2):

        import numpy as np
        logging.info('\n '.join(['comparing', File1, File2]))
        table1 = damask.ASCIItable(name=File1, readonly=True)
        table1.head_read()
        len1 = len(table1.info) + 2
        table2 = damask.ASCIItable(name=File2, readonly=True)
        table2.head_read()
        len2 = len(table2.info) + 2

        refArray = np.nan_to_num(
            np.genfromtxt(File1,
                          missing_values='n/a',
                          skip_header=len1,
                          autostrip=True))
        curArray = np.nan_to_num(
            np.genfromtxt(File2,
                          missing_values='n/a',
                          skip_header=len2,
                          autostrip=True))

        if len(curArray) == len(refArray):
            refArrayNonZero = refArray[refArray.nonzero()]
            curArray = curArray[refArray.nonzero()]
            max_err = np.max(
                abs(refArrayNonZero[curArray.nonzero()] /
                    curArray[curArray.nonzero()] - 1.))
            max_loc = np.argmax(
                abs(refArrayNonZero[curArray.nonzero()] /
                    curArray[curArray.nonzero()] - 1.))
            refArrayNonZero = refArrayNonZero[curArray.nonzero()]
            curArray = curArray[curArray.nonzero()]
            print(
                ' ********\n * maximum relative error {} between {} and {}\n ********'
                .format(max_err, refArrayNonZero[max_loc], curArray[max_loc]))
            return max_err
        else:
            raise Exception('mismatch in array size to compare')
示例#4
0
    defgrad='f',
    pos='pos',
    nodal=False,
)

(options, filenames) = parser.parse_args()

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
    outname = (os.path.splitext(name)[0] + '_nodal' +
               os.path.splitext(name)[1]) if (options.nodal and name) else None
    try:
        table = damask.ASCIItable(name=name, outname=outname, buffered=False)
    except:
        continue
    damask.util.report(
        scriptName,
        '{}{}'.format(name, ' --> {}'.format(outname) if outname else ''))

    # ------------------------------------------ read header ------------------------------------------

    table.head_read()

    # ------------------------------------------ sanity checks ----------------------------------------

    errors = []
    remarks = []
示例#5
0
(options,filenames) = parser.parse_args()

options.pix_size = eval(options.pix_size)
options.pix_size *= 1e6
# --- loop over input files ------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
  try:
    out = name.split('.000')[0]
    ext = name.split('.')[1]
    table = damask.ASCIItable(name = name,
                              outname = out+"_geom."+ext,
                              labeled = False,
                              buffered = False,
                              readonly = True)
  except: continue
  damask.util.report(scriptName,name)
  table.head_read()
  table.data_readArray()
  table.labels_clear()
  table.info_clear()
  table.info_append([
    scriptID + ' ' + ' '.join(sys.argv[1:]),
    "grid\ta {}\tb {}\tc 1".format(table.data.shape[0], table.data.shape[1]),
    "size\tx {}\ty {}\tz 1".format(options.pix_size * (table.data.shape[0] - 1), options.pix_size * (table.data.shape[1] - 1)),
    "origin\tx 0.0\ty 0.0\tz 0.0",
    "homogenization\t1",
    ])
示例#6
0
from scipy import signal
from mpl_toolkits.mplot3d import Axes3D
import math
"""  Does a cross correlation between simulated spheroconical indenter tip and tip impression from AFM data """

scriptID = string.replace(
    '$Id: Correlation_AFM.py 157 2015-11-06 14:37:59Z chakra34 $', '\n', '\\n')

AFM_filename = sys.argv[1]  # AFM file name
filename = sys.argv[2]  # simulated tip file name
tip_data = string(sys.argv[3]) if len(sys.argv) > 3 else "3_displacement"
N = int(sys.argv[4]) if len(sys.argv) > 4 else 11  # patch dimension in pixel

#------------------- Reading the simulated tip from file -----------------

table1 = damask.ASCIItable(name=filename, buffered=False)
table1.head_read()
table1.data_readArray()

file = open(filename)
index = table1.label_index(tip_data)
tip = np.array(table1.data[:, index]).reshape(N, N)

#------------------- Reading AFM Data ------------------------------------

table = damask.ASCIItable(name=AFM_filename,
                          buffered=False,
                          labeled=False,
                          readonly=True)
table.head_read()
table.data_readArray()
示例#7
0
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [geomfile(s)]', description = """
Translate geom description into ASCIItable containing position and microstructure.

""", version = scriptID)

(options, filenames) = parser.parse_args()

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
  try:    table = damask.ASCIItable(name = name,
                                    outname = os.path.splitext(name)[0]+'.txt' if name else name,
                                    buffered = False,
                                    labeled = False,
                                   )
  except: continue
  damask.util.report(scriptName,name)

# --- interpret header ----------------------------------------------------------------------------

  table.head_read()
  info,extra_header = table.head_getGeom()
  
  damask.util.croak(['grid     a b c:  %s'%(' x '.join(map(str,info['grid']))),
                     'size     x y z:  %s'%(' x '.join(map(str,info['size']))),
                     'origin   x y z:  %s'%(' : '.join(map(str,info['origin']))),
                     'homogenization:  %i'%info['homogenization'],
                     'microstructures: %i'%info['microstructures'],
示例#8
0
if options.float_pix:
    options.pix_size = (options.float_pix)
else:
    options.pix_size = eval(options.pix_size)
options.alpha = np.radians(options.alpha)
grid_y, grid_x = np.meshgrid(
    options.pix_size * np.linspace(-0.5 * options.dimension[0],
                                   0.5 * options.dimension[0],
                                   num=options.dimension[0],
                                   endpoint=True),
    options.pix_size * np.linspace(-0.5 * options.dimension[0],
                                   0.5 * options.dimension[0],
                                   num=options.dimension[0],
                                   endpoint=True))
table = damask.ASCIItable()

#--------------------------- append header and info ----------------------------------------

table.info_append(repr(options.pix_size))
table.info_append((options.dimension[0], options.dimension[1]))
table.labels_append(['{}_pos'.format(1+i) for i in range(3)]+ \
                   ['{}_displacement'.format(1+i) for i in range(3)])

table.head_write()

#---------------------------- output result ------------------------------------------------

tip = np.zeros(np.shape(grid_x), dtype=float)
for j in xrange(len(grid_y)):
    for i in xrange(len(grid_x)):
示例#9
0
parser.set_defaults(rmin  = 0,
                    afm   = False,
                    positive = False,
                   )

(options,filenames) = parser.parse_args()
if not options.new_d:
  options.new_d = [options.rmax,options.rmax]

#------------------- loop over input files ------------------------------------ 
if filenames == []: filenames = [None]

for name in filenames:
  try:
    table = damask.ASCIItable(name = name,
                              outname = os.path.join(os.path.dirname(name),"subset_"+os.path.basename(name)),
                              readonly = True,
                             )
  except: continue

  damask.util.report(scriptName,name)

  table.head_read()
  table.data_readArray(options.label)
  if options.afm == False :
    if options.dimension[0] * options.dimension[1] != table.data.shape[0] :
      if options.dimension[0] * options.dimension[1] != table.data.shape[1]:
        print("dimensions do not match")
        sys.exit()
  else :
    options.dimension = np.array([table.data.shape[1], table.data.shape[0]])
  if options.center == None:
示例#10
0
    flipud=False,
)

(options, filenames) = parser.parse_args()

if options.multiplier == None:
    print("There has to be a multiplier!!!, aborting")
    sys.exit()

if not options.sim_file or not os.path.exists(options.sim_file):
    parser.error('No file selected for comparison')

#------------------- Reading the simulated tip from file -----------------

table1 = damask.ASCIItable(name=options.sim_file,
                           buffered=False,
                           readonly=True)
table1.head_read()
table1.data_readArray()
if options.flipud == True: table1.data = np.flipud(table1.data)

#------------------- loop over input files ------------------------------------
if filenames == []: filenames = [None]

for name in filenames:
    try:
        table = damask.ASCIItable(
            name=name,
            readonly=True,
        )
    except:
示例#11
0
def damask_sim(params,geomfile,loadfile,materialfile,sections):
    material = MaterialFile(materialfile) # read from previous file
    # plastic_phenopowerlaw
    # slip
    params_write = {}
    j = 0 # params count
    #s0 = 0 # params section count start
    
    for section in sections:
        phenopowerlaw_slip = material.data["phase"][section.lower()]
        if "phenopowerlaw" not in phenopowerlaw_slip["plasticity"]:
            print("section %s requires plasticty phenopowerlaw")
            exit
        
        nslip_a = [ i for i,n in enumerate(phenopowerlaw_slip["nslip"]) if n!=0] # active slip systems
        params_index =[["gdot0_slip",[0]],
                       ["n_slip",[0]],
                       ["tau0_slip",nslip_a],
                       ["tausat_slip",nslip_a],
                       ["a_slip",[0]],
                       ["h0_slipslip",[0]],
                       ["taub_c",[0]],
                       ["taub_d",[0]],]
        
        #s1 = s0 + sum([len(v[1]) for v in params_index])
        params_check = {}
        params_write[section] = phenopowerlaw_slip
        for p in params_index:
            k = p[0]
            v = p[1]
            for i in v:
                params_check.setdefault(k,[]).append(params[j]) 
                params_write[section][k][i] = params[j]
                j += 1
        # check
        #try:
        #    assert(params_check["gdot0_slip"]>0)
        #    assert(params_check["n_slip"]>0)
        #    for i in range(len(nslip_a)):
        #        assert(params_check["tau0_slip"][i]>0)
        #        assert(params_check["tausat_slip"][i]>params_check["tau0_slip"][i])
        #    assert(params_check["a_slip"]>0)
        #    assert(params_check["taub_c"]>0)
        #    assert(params_check["taub_d"]>0)
        #except AssertionError, e:
        #    raise Exception(e.args)
        
    material.change_params(params_write)

    # simulation
    check = subprocess.call(shlex.split("DAMASK_spectral --geometry %s --load %s" %(geomfile,loadfile)))
    jobname = geomfile.split('.')[0] + '_' + loadfile.split('.')[0]
    if check == 0:
        subprocess.call(shlex.split("postResults --cr f,p %s.spectralOut" %(jobname)))
        postfn = jobname+".txt"
        subprocess.call(shlex.split("addCauchy ./postProc/%s" %(postfn)))
        subprocess.call(shlex.split("addStrainTensors -0 -v ./postProc/%s" %(postfn)))
        subprocess.call(shlex.split("addMises -s Cauchy -e ln'('V')' ./postProc/%s" %(postfn)))

        table = damask.ASCIItable("./postProc/%s" %(postfn),readonly=True)
        table.head_read()
        table.data_readArray(["Mises(ln(V))","Mises(Cauchy)"])

        sim_flow = FlowCurve(data=table.data,Unit='Pa')
        sim_data_n = len(sim_flow.data)
        sim_data_acc = [[sim_flow.acc_strain[i],sim_flow.von_stress[i]] for i in range(sim_data_n)]
        np.savetxt("./postProc/%s_acc.dat"%(jobname),sim_data_acc)
        sim_data_plastic =[[sim_flow.strain[i],sim_flow.stress[i]] for i in range(sim_data_n)]
        np.savetxt("./postProc/%s_plastic.dat"%(jobname),sim_data_plastic)
        return sim_data_plastic
    else:
        print("damask failure")
        return [[float("inf"),float("inf")]]
示例#12
0
文件: test.py 项目: puwe/DAMASK
    def compare_Table(self,
                      headings0,
                      file0,
                      headings1,
                      file1,
                      normHeadings='',
                      normType=None,
                      absoluteTolerance=False,
                      perLine=False,
                      skipLines=[]):

        import numpy as np
        logging.info('\n '.join(['comparing ASCII Tables', file0, file1]))
        if normHeadings == '': normHeadings = headings0

        # check if comparison is possible and determine lenght of columns
        if len(headings0) == len(headings1) == len(normHeadings):
            dataLength = len(headings0)
            length = [1 for i in xrange(dataLength)]
            shape = [[] for i in xrange(dataLength)]
            data = [[] for i in xrange(dataLength)]
            maxError = [0.0 for i in xrange(dataLength)]
            absTol = [absoluteTolerance for i in xrange(dataLength)]
            column = [[1 for i in xrange(dataLength)] for j in xrange(2)]

            norm = [[] for i in xrange(dataLength)]
            normLength = [1 for i in xrange(dataLength)]
            normShape = [[] for i in xrange(dataLength)]
            normColumn = [1 for i in xrange(dataLength)]

            for i in xrange(dataLength):
                if headings0[i]['shape'] != headings1[i]['shape']:
                    raise Exception('shape mismatch between {} and {} '.format(
                        headings0[i]['label'], headings1[i]['label']))
                shape[i] = headings0[i]['shape']
                for j in xrange(np.shape(shape[i])[0]):
                    length[i] *= shape[i][j]
                normShape[i] = normHeadings[i]['shape']
                for j in xrange(np.shape(normShape[i])[0]):
                    normLength[i] *= normShape[i][j]
        else:
            raise Exception(
                'trying to compare {} with {} normed by {} data sets'.format(
                    len(headings0), len(headings1), len(normHeadings)))

        table0 = damask.ASCIItable(name=file0, readonly=True)
        table0.head_read()
        table1 = damask.ASCIItable(name=file1, readonly=True)
        table1.head_read()

        for i in xrange(dataLength):
            key0 = ('1_' if length[i] > 1 else '') + headings0[i]['label']
            key1 = ('1_' if length[i] > 1 else '') + headings1[i]['label']
            normKey = ('1_'
                       if normLength[i] > 1 else '') + normHeadings[i]['label']
            if key0 not in table0.labels(raw=True):
                raise Exception(
                    'column {} not found in 1. table...\n'.format(key0))
            elif key1 not in table1.labels(raw=True):
                raise Exception(
                    'column {} not found in 2. table...\n'.format(key1))
            elif normKey not in table0.labels(raw=True):
                raise Exception(
                    'column {} not found in 1. table...\n'.format(normKey))
            else:
                column[0][i] = table0.label_index(key0)
                column[1][i] = table1.label_index(key1)
                normColumn[i] = table0.label_index(normKey)

        line0 = 0
        while table0.data_read():  # read next data line of ASCII table
            if line0 not in skipLines:
                for i in xrange(dataLength):
                    myData = np.array(map(float,table0.data[column[0][i]:\
                                                            column[0][i]+length[i]]),'d')
                    normData = np.array(map(float,table0.data[normColumn[i]:\
                                                              normColumn[i]+normLength[i]]),'d')
                    data[i] = np.append(data[i], np.reshape(myData, shape[i]))
                    if normType == 'pInf':
                        norm[i] = np.append(norm[i], np.max(np.abs(normData)))
                    else:
                        norm[i] = np.append(
                            norm[i],
                            np.linalg.norm(np.reshape(normData, normShape[i]),
                                           normType))
            line0 += 1

        for i in xrange(dataLength):
            if not perLine:
                norm[i] = [
                    np.max(norm[i]) for j in xrange(line0 - len(skipLines))
                ]
            data[i] = np.reshape(data[i], [line0 - len(skipLines), length[i]])
            if any(norm[i]) == 0.0 or absTol[i]:
                norm[i] = [1.0 for j in xrange(line0 - len(skipLines))]
                absTol[i] = True
                if perLine:
                    logging.warning(
                        'At least one norm of {} in 1. table is 0.0, using absolute tolerance'
                        .format(headings0[i]['label']))
                else:
                    logging.warning(
                        'Maximum norm of {} in 1. table is 0.0, using absolute tolerance'
                        .format(headings0[i]['label']))

        line1 = 0
        while table1.data_read():  # read next data line of ASCII table
            if line1 not in skipLines:
                for i in xrange(dataLength):
                    myData = np.array(map(float,table1.data[column[1][i]:\
                                                               column[1][i]+length[i]]),'d')
                    maxError[i] = max(
                        maxError[i],
                        np.linalg.norm(
                            np.reshape(
                                myData - data[i][line1 - len(skipLines), :],
                                shape[i])) / norm[i][line1 - len(skipLines)])
            line1 += 1

        if (line0 != line1):
            raise Exception(
                'found {} lines in 1. table but {} in 2. table'.format(
                    line0, line1))

        logging.info(' ********')
        for i in xrange(dataLength):
            if absTol[i]:
                logging.info(
                    ' * maximum absolute error {} between {} and {}'.format(
                        maxError[i], headings0[i]['label'],
                        headings1[i]['label']))
            else:
                logging.info(
                    ' * maximum relative error {} between {} and {}'.format(
                        maxError[i], headings0[i]['label'],
                        headings1[i]['label']))
        logging.info(' ********')
        return maxError
示例#13
0
文件: addEhkl.py 项目: hrh741/DAMASK
                  metavar='int int int',
                  help='direction of elastic modulus [%default]')
parser.set_defaults(hkl=(1, 1, 1), )

(options, filenames) = parser.parse_args()

if options.stiffness is None:
    parser.error('no data column specified...')

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
    try:
        table = damask.ASCIItable(name=name)
    except IOError:
        continue
    damask.util.report(scriptName, name)

    # ------------------------------------------ read header ------------------------------------------

    table.head_read()

    # ------------------------------------------ sanity checks ----------------------------------------

    remarks = []
    columns = []

    for i, column in enumerate(table.label_index(options.stiffness)):
        if column < 0:
示例#14
0
            'output': sys.stdout,
            'croak': sys.stderr
        }
        file['croak'].write('\033[1m' + scriptName + '\033[0m\n')
    else:
        if not os.path.exists(name): continue
        file = {
            'name': name,
            'input': open(name),
            'output': open(name + '_tmp', 'w'),
            'croak': sys.stderr
        }
        file['croak'].write('\033[1m' + scriptName + '\033[0m: ' +
                            file['name'] + '\n')

    table = damask.ASCIItable(file['input'], file['output'],
                              buffered=False)  # make unbuffered ASCII_table
    table.head_read()  # read ASCII header info
    table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))

    # --------------- figure out columns to process  ---------------------------------------------------
    active = []
    column = {}
    columnMissing = False

    for label in datainfo['label']:
        key = label
        if key in table.labels(raw=True):
            active.append(label)
            column[label] = table.labels.index(
                key)  # remember columns of requested data
        else:
示例#15
0
                    grid = (512,512),
                    homogenization = 1,
                    crystallite = 1)

(options,filename) = parser.parse_args()

if np.any(options.grid < 2):
  parser('invalid grid a b c.')
if np.any(options.size <= 0.0):
  parser('invalid size x y z.')

# --- open input files ----------------------------------------------------------------------------

if filename == []: filename = [None]

table = damask.ASCIItable(outname = filename[0],
                          buffered = False)

damask.util.report(scriptName,filename[0])

options.omega  *= math.pi/180.0                                                                     # rescale ro radians
rotation = np.array([[ math.cos(options.omega),math.sin(options.omega),],
                     [-math.sin(options.omega),math.cos(options.omega),]],'d')

box = np.dot(np.array([[options.canal,0.],[0.,options.aspect*options.canal]]).transpose(),rotation)


info = {
        'grid':   np.ones(3,'i'),
        'size':   np.ones(3,'d'),
        'origin': np.zeros(3,'d'),
        'microstructures': 3,
示例#16
0
if options.z > 0: options.z -= 1                                                                    # adjust to 0-based indexing

# --- color palette ---------------------------------------------------------------------------------

theMap = damask.Colormap(predefined=options.color)
if options.invert: theMap = theMap.invert()
theColors = np.uint8(np.array(theMap.export(format='list',steps=256))*255)

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
  try:
    table = damask.ASCIItable(name = name,
                              buffered = False,
                              labeled = options.label is not None,
                              readonly = True)
  except: continue
  table.report_name(scriptName,name)

# ------------------------------------------ read header ------------------------------------------

  table.head_read()

# --------------- figure out columns to process  ---------------------------------------------------
  
  errors = []
  if table.label_dimension(options.label) != 1:
    errors.append('no scalar data ({}) found.'.format(options.label))
  if table.label_dimension(options.defgrad) != 9:
    errors.append('no deformation gradient tensor (1..9_{}) found.'.format(options.defgrad))
示例#17
0
parser.set_defaults(pix_size='20.0e-6/255', )

(options, filenames) = parser.parse_args()

options.pix_size = eval(options.pix_size)

#------------------- loop over input files ------------------------------------
if filenames == []: filenames = [None]

for name in filenames:
    out_file = "out_" + name
    try:
        table = damask.ASCIItable(
            name=name,
            outname="out_" + name,
            buffered=False,
        )
    except:
        continue
    table.croak(damask.util.emph(scriptName) + (': ' + name if name else ''))

    #-------------------------- reading head and generating grid -----------------------------

    table.head_read()
    table.data_readArray()
    grid = np.array([table.data.shape[1], table.data.shape[0]], dtype=int)

    #-------------------------------process and output result  --------------------------------

    table.info_clear()
示例#18
0
文件: addTable.py 项目: puwe/DAMASK
                  '--add',
                  '--table',
                  dest='table',
                  action='extend',
                  metavar='<string LIST>',
                  help='tables to add')

(options, filenames) = parser.parse_args()

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
    try:
        table = damask.ASCIItable(name=name, buffered=False)
    except:
        continue

    damask.util.report(scriptName, name)

    tables = []
    for addTable in options.table:
        try:
            tables.append(
                damask.ASCIItable(name=addTable, buffered=False,
                                  readonly=True))
        except:
            continue

# ------------------------------------------ read headers ------------------------------------------
示例#19
0
parser.set_defaults(d = 1)
parser.set_defaults(N = 1)
parser.set_defaults(renumber = False)
parser.set_defaults(immutable = [])

(options, filenames) = parser.parse_args()

options.immutable = map(int,options.immutable)

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
  try:
    table = damask.ASCIItable(name = name,
                              buffered = False, labeled = False)
  except: continue
  damask.util.report(scriptName,name)

# --- interpret header ----------------------------------------------------------------------------

  table.head_read()
  info,extra_header = table.head_getGeom()
  
  damask.util.croak(['grid     a b c:  %s'%(' x '.join(map(str,info['grid']))),
               'size     x y z:  %s'%(' x '.join(map(str,info['size']))),
               'origin   x y z:  %s'%(' : '.join(map(str,info['origin']))),
               'homogenization:  %i'%info['homogenization'],
               'microstructures: %i'%info['microstructures'],
              ])
示例#20
0
(options,filenames) = parser.parse_args()


for i in options.axes:
    if i.lower() not in ['x','+x','-x','y','+y','-y','z','+z','-z']:
        parser.error('invalid axes %s %s %s' %(options.axes[0],options.axes[1],options.axes[2]))

# --- loop over input files -------------------------------------------------------------------------
if filenames == []: 
    print 'missing input files'
    exit()

for filename in filenames:
    try:
        table = damask.ASCIItable(name = filename,
                                  outname = os.path.splitext(filename)[-2]+'.geom',
                                  buffered = False, labeled = False)
    except: continue
    damask.util.report(scriptName,filename)

#   open file and read data
    fopen = open(filename, 'r')
    if os.path.splitext(filename)[-1] == '.ang':
        eulerangles, coords, phase, geomHeader = readAngFile(fopen, options.phase, options.column, options.threshold)

        info['grid'][:] = ebsdInfo['ncols_odd'], ebsdInfo['nrows'], 1
        info['size'][:] = ebsdInfo['ncols_odd']*ebsdInfo['xstep'], ebsdInfo['nrows']*ebsdInfo['ystep'], min(ebsdInfo['xstep'], ebsdInfo['ystep'])
    elif os.path.splitext(filename)[-1] == '.ctf':
        eulerangles, coords, phase, geomHeader = readCtfFile(fopen)

        info['grid'][:] = ebsdInfo['xcells'], ebsdInfo['ycells'], 1
示例#21
0
    parser.error("Either dimension or size has to be specified if pixel size is given.")
else:
  if options.size and options.grid:
    options.pix_size = options.size/options.grid
  else:
    parser.error("Both dimension and size has to be specified if pixel size is not given.")

# --------------------------------------- loop over input files -------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
  out_file = "out_"+os.path.basename(name)
  try:
    table = damask.ASCIItable(name     = name, 
                              outname  = os.path.join(os.path.dirname(name),out_file),
                              buffered = False)
  except: continue
  damask.util.report(scriptName,name)
# ------------------------------------------ read header and data ------------------------------------------

  table.head_read()
  table.data_readArray([options.coords])
  if len(table.data[0]) != 3:
    continue 

#-------------------------------------------- process and store output ---------------------------------------
  if options.tilt != 0.0:                                                                   #tilt before coordinates rotation
    ang_tilt = np.radians(options.tilt)
    tilt = np.zeros((len(table.data),2),dtype=float)
    tilt[:,0] = table.data[:,0]*np.cos(ang_tilt) + table.data[:,2]*np.sin(ang_tilt)
示例#22
0
文件: test.py 项目: puwe/DAMASK
    def compare_Tables(
            self,
            files=[None, None],  # list of file names
            columns=[None],  # list of list of column labels (per file)
            rtol=1e-5,
            atol=1e-8,
            preFilter=-1.0,
            postFilter=-1.0,
            debug=False):
        """
    compare tables with np.allclose

    threshold can be used to ignore small values (a negative number disables this feature)
    """
        if not (isinstance(files, Iterable) and not isinstance(files, str)
                ):  # check whether list of files is requested
            files = [str(files)]

        tables = [
            damask.ASCIItable(name=filename, readonly=True)
            for filename in files
        ]
        for table in tables:
            table.head_read()

        columns += [columns[0]] * (len(files) - len(columns)
                                   )  # extend to same length as files
        columns = columns[:len(files)]  # truncate to same length as files

        for i, column in enumerate(columns):
            if column is None:
                columns[i] = tables[i].labels(
                    raw=True)  # if no column is given, read all

        logging.info('comparing ASCIItables')
        for i in xrange(len(columns)):
            columns[i] = columns[0]  if not columns[i] else \
                       ([columns[i]] if not (isinstance(columns[i], Iterable) and not isinstance(columns[i], str)) else \
                         columns[i]
                       )
            logging.info(files[i] + ':' + ','.join(columns[i]))

        if len(files) < 2:
            return True  # single table is always close to itself...

        maximum = np.zeros(len(columns[0]), dtype='f')
        data = []
        for table, labels in zip(tables, columns):
            table.data_readArray(labels)
            data.append(
                np.where(
                    np.abs(table.data) < preFilter, np.zeros_like(table.data),
                    table.data))
            maximum += np.abs(table.data).max(axis=0)
            table.close()

        maximum /= len(tables)
        maximum = np.where(maximum > 0.0, maximum,
                           1)  # avoid div by zero for empty columns
        for i in xrange(len(data)):
            data[i] /= maximum

        mask = np.zeros_like(table.data, dtype='bool')

        for table in data:
            mask |= np.where(np.abs(table) < postFilter, True,
                             False)  # mask out (all) tiny values

        allclose = True  # start optimistic
        for i in xrange(1, len(data)):
            if debug:
                t0 = np.where(mask, 0.0, data[i - 1])
                t1 = np.where(mask, 0.0, data[i])
                j = np.argmin(np.abs(t1) * rtol + atol - np.abs(t0 - t1))
                logging.info('{:f}'.format(
                    np.amax(np.abs(t0 - t1) / (np.abs(t1) * rtol + atol))))
                logging.info('{:f} {:f}'.format((t0 * maximum).flatten()[j],
                                                (t1 * maximum).flatten()[j]))
            allclose &= np.allclose(np.where(mask, 0.0, data[i - 1]),
                                    np.where(mask, 0.0, data[i]), rtol,
                                    atol)  # accumulate "pessimism"

        return allclose
示例#23
0
parser.set_defaults(
    mode='cell',
    pos='pos',
)

(options, filenames) = parser.parse_args()

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
    try:
        table = damask.ASCIItable(
            name=name,
            buffered=False,
            labeled=True,
            readonly=True,
        )
    except:
        continue
    damask.util.report(scriptName, name)

    # --- interpret header ----------------------------------------------------------------------------

    table.head_read()

    remarks = []
    errors = []
    coordDim = table.label_dimension(options.pos)
    if not 3 >= coordDim >= 1:
        errors.append(
示例#24
0
damask.util.report(scriptName, options.seedFile)

if options.randomSeed is None:
    options.randomSeed = int(os.urandom(4).encode('hex'), 16)
damask.util.croak(options.randomSeed)
delta = (options.scale / options.grid[0], options.scale / options.grid[1],
         options.scale / options.grid[2])
baseFile = os.path.splitext(os.path.basename(options.seedFile))[0]
points = float(reduce(mul, options.grid))

# ----------- calculate target distribution and bin edges
targetGeomFile = os.path.splitext(os.path.basename(
    options.target))[0] + '.geom'
targetGeomTable = damask.ASCIItable(targetGeomFile,
                                    None,
                                    labeled=False,
                                    readonly=True)
targetGeomTable.head_read()
info, devNull = targetGeomTable.head_getGeom()
nMicrostructures = info['microstructures']
targetVolFrac = np.bincount(targetGeomTable.microstructure_read(info['grid']))[1:nMicrostructures+1]/\
                                                                       float(info['grid'].prod())
target = []
for i in xrange(1, nMicrostructures + 1):
    targetHist, targetBins = np.histogram(targetVolFrac,
                                          bins=i)  #bin boundaries
    target.append({'histogram': targetHist, 'bins': targetBins})

# ----------- create initial seed file or open existing one
bestSeedsVFile = StringIO()
if os.path.isfile(os.path.splitext(options.seedFile)[0] + '.seeds'):
示例#25
0
                  dest='simfile',
                  type='string',
                  metavar='string',
                  help='simulated file to interpolate')

parser.add_option('--show',
                  action="store_true",
                  dest="show",
                  help="showing the interpolated result [False]")

parser.set_defaults(show=False, )

(options, filenames) = parser.parse_args()

table_exp = damask.ASCIItable(name=options.expfile,
                              buffered=False,
                              readonly=True)
table_exp.head_read()
table_exp.data_readArray((options.expX, options.expY))
x_exp_index = table_exp.label_index(options.expX)
y_exp_index = table_exp.label_index(options.expY)
x_exp_data = table_exp.data[:, x_exp_index]
y_exp_data = table_exp.data[:, y_exp_index]

table_sim = damask.ASCIItable(name=options.simfile,
                              buffered=False,
                              readonly=True)
table_sim.head_read()
table_sim.data_readArray((options.simX, options.simY))
x_sim_index = table_sim.label_index(options.simX)
y_sim_index = table_sim.label_index(options.simY)
示例#26
0
    def run(self):
        global bestSeedsUpdate
        global bestSeedsVFile
        global nMicrostructures
        global delta
        global points
        global target
        global match
        global baseFile
        global maxSeeds

        s.acquire()
        bestMatch = match
        s.release()

        random.seed(options.randomSeed +
                    self.threadID)  # initializes to given seeds
        knownSeedsUpdate = bestSeedsUpdate - 1.0  # trigger update of local best seeds
        randReset = True  # aquire new direction

        myBestSeedsVFile = StringIO()  # store local copy of best seeds file
        perturbedSeedsVFile = StringIO()  # perturbed best seeds file
        perturbedGeomVFile = StringIO()  # tessellated geom file

        #--- still not matching desired bin class ----------------------------------------------------------
        while bestMatch < options.threshold:
            s.acquire()  # ensure only one thread acces global data
            if bestSeedsUpdate > knownSeedsUpdate:  # write best fit to virtual file
                knownSeedsUpdate = bestSeedsUpdate
                bestSeedsVFile.reset()
                myBestSeedsVFile.close()
                myBestSeedsVFile = StringIO()
                i = 0
                for line in bestSeedsVFile:
                    myBestSeedsVFile.write(line)
            s.release()

            if randReset:  # new direction because current one led to worse fit

                randReset = False

                NmoveGrains = random.randrange(1, maxSeeds)
                selectedMs = []
                direction = []
                for i in xrange(NmoveGrains):
                    selectedMs.append(random.randrange(1, nMicrostructures))

                    direction.append(
                        np.array(((random.random() - 0.5) * delta[0],
                                  (random.random() - 0.5) * delta[1],
                                  (random.random() - 0.5) * delta[2])))

            perturbedSeedsVFile.close()  # reset virtual file
            perturbedSeedsVFile = StringIO()
            myBestSeedsVFile.reset()

            perturbedSeedsTable = damask.ASCIItable(
                myBestSeedsVFile, perturbedSeedsVFile,
                labeled=True)  # write best fit to perturbed seed file
            perturbedSeedsTable.head_read()
            perturbedSeedsTable.head_write()
            outputAlive = True
            ms = 1
            i = 0
            while outputAlive and perturbedSeedsTable.data_read(
            ):  # perturbe selected microstructure
                if ms in selectedMs:
                    newCoords = np.array(
                        tuple(map(float, perturbedSeedsTable.data[0:3])) +
                        direction[i])
                    newCoords = np.where(
                        newCoords >= 1.0, newCoords - 1.0,
                        newCoords)  # ensure that the seeds remain in the box
                    newCoords = np.where(newCoords < 0.0, newCoords + 1.0,
                                         newCoords)
                    perturbedSeedsTable.data[0:3] = [
                        format(f, '8.6f') for f in newCoords
                    ]
                    direction[i] *= 2.
                    i += 1
                ms += 1
                perturbedSeedsTable.data_write()
#--- do tesselation with perturbed seed file ----------------------------------------------------------
            perturbedGeomVFile.close()
            perturbedGeomVFile = StringIO()
            perturbedSeedsVFile.reset()
            perturbedGeomVFile.write(
                damask.util.execute('geom_fromVoronoiTessellation ' + ' -g ' +
                                    ' '.join(map(str, options.grid)),
                                    streamIn=perturbedSeedsVFile)[0])
            perturbedGeomVFile.reset()

            #--- evaluate current seeds file ----------------------------------------------------------------------
            perturbedGeomTable = damask.ASCIItable(perturbedGeomVFile,
                                                   None,
                                                   labeled=False,
                                                   readonly=True)
            perturbedGeomTable.head_read()
            for i in perturbedGeomTable.info:
                if i.startswith('microstructures'):
                    myNmicrostructures = int(i.split('\t')[1])
            perturbedGeomTable.data_readArray()
            perturbedGeomTable.output_flush()
            currentData = np.bincount(
                perturbedGeomTable.data.astype(int).ravel())[1:] / points
            currentError = []
            currentHist = []
            for i in xrange(
                    nMicrostructures
            ):  # calculate the deviation in all bins per histogram
                currentHist.append(
                    np.histogram(currentData, bins=target[i]['bins'])[0])
                currentError.append(
                    np.sqrt(
                        np.square(
                            np.array(target[i]['histogram'] -
                                     currentHist[i])).sum()))

# as long as not all grains are within the range of the target, use the deviation to left and right as error
            if currentError[0] > 0.0:
                currentError[0] *= (
                    (target[0]['bins'][0] - np.min(currentData))**2.0 +
                    (target[0]['bins'][1] - np.max(currentData))**2.0
                )**0.5  # norm of deviations by number of usual bin deviation
            s.acquire()  # do the evaluation serially
            bestMatch = match
            #--- count bin classes with no mismatch ----------------------------------------------------------------------
            myMatch = 0
            for i in xrange(nMicrostructures):
                if currentError[i] > 0.0: break
                myMatch = i + 1

            if myNmicrostructures == nMicrostructures:
                for i in xrange(min(nMicrostructures, myMatch + options.bins)):
                    if currentError[i] > target[i][
                            'error']:  # worse fitting, next try
                        randReset = True
                        break
                    elif currentError[i] < target[i]['error']:  # better fit
                        bestSeedsUpdate = time.time(
                        )  # save time of better fit
                        damask.util.croak('Thread {:d}: Better match ({:d} bins, {:6.4f} --> {:6.4f})'\
                                                      .format(self.threadID,i+1,target[i]['error'],currentError[i]))
                        damask.util.croak('          target: ' +
                                          np.array_str(target[i]['histogram']))
                        damask.util.croak('          best:   ' +
                                          np.array_str(currentHist[i]))
                        currentSeedsName = baseFile + '_' + str(
                            bestSeedsUpdate
                        ).replace(
                            '.', '-'
                        )  # name of new seed file (use time as unique identifier)
                        perturbedSeedsVFile.reset()
                        bestSeedsVFile.close()
                        bestSeedsVFile = StringIO()
                        sys.stdout.flush()
                        with open(
                                currentSeedsName + '.seeds',
                                'w') as currentSeedsFile:  # write to new file
                            for line in perturbedSeedsVFile:
                                currentSeedsFile.write(line)
                                bestSeedsVFile.write(line)
                        for j in xrange(nMicrostructures
                                        ):  # save new errors for all bins
                            target[j]['error'] = currentError[j]
                        if myMatch > match:  # one or more new bins have no deviation
                            damask.util.croak(
                                'Stage {:d}  cleared'.format(myMatch))
                            match = myMatch
                            sys.stdout.flush()
                        break
                    if i == min(
                            nMicrostructures, myMatch + options.bins
                    ) - 1:  # same quality as before: take it to keep on moving
                        bestSeedsUpdate = time.time()
                        perturbedSeedsVFile.reset()
                        bestSeedsVFile.close()
                        bestSeedsVFile = StringIO()
                        for line in perturbedSeedsVFile:
                            bestSeedsVFile.write(line)
                        for j in xrange(nMicrostructures):
                            target[j]['error'] = currentError[j]
                        randReset = True
            else:  #--- not all grains are tessellated
                damask.util.croak('Thread {:d}: Microstructure mismatch ({:d} microstructures mapped)'\
                                                                 .format(self.threadID,myNmicrostructures))
                randReset = True

            s.release()
示例#27
0
  parser.error('only one from aliases "biplane" and "boundary" possible.')

feature_list = []
for i,feature in enumerate(features):
  for name in feature['names']:
    for myType in options.type:
      if name.startswith(myType):
        feature_list.append(i)                                                                      # remember valid features
        break

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
  try:    table = damask.ASCIItable(name = name, buffered = False)
  except: continue
  damask.util.report(scriptName,name)

# ------------------------------------------ read header ------------------------------------------

  table.head_read()

# ------------------------------------------ sanity checks ----------------------------------------

  errors  = []
  remarks = []
  column = {}
  
  coordDim = table.label_dimension(options.pos)
  if not 3 >= coordDim >= 1:
示例#28
0
                  help='column label for grouping rows')

(options, filenames) = parser.parse_args()

if options.label is None:
    parser.error('no grouping column specified.')

# --- loop over input files -------------------------------------------------------------------------

if filenames == []: filenames = [None]

for name in filenames:
    try:
        table = damask.ASCIItable(
            name=name,
            outname=os.path.join(
                os.path.split(name)[0], options.label + '_averaged_' +
                os.path.split(name)[1]) if name else name,
            buffered=False)
    except:
        continue
    damask.util.report(scriptName, name)

    # ------------------------------------------ sanity checks ---------------------------------------

    table.head_read()
    if table.label_dimension(options.label) != 1:
        damask.util.croak('column {} is not of scalar dimension.'.format(
            options.label))
        table.close(dismiss=True)  # close ASCIItable and remove empty file
        continue
示例#29
0
parser.set_defaults(homogenization=1)
parser.set_defaults(phase=[1, 2])
parser.set_defaults(crystallite=1)
parser.set_defaults(config=False)
parser.set_defaults(compress=False)
parser.set_defaults(precision='2')

(options, filenames) = parser.parse_args()

if filenames == []: filenames = [None]

for name in filenames:
    try:
        table = damask.ASCIItable(name=name,
                                  outname=os.path.splitext(name)[-2] +
                                  '.geom' if name else name,
                                  buffered=False,
                                  labeled=False)
    except:
        continue
    damask.util.report(scriptName, name)

    info = {
        'grid': np.zeros(3, 'i'),
        'size': np.zeros(3, 'd'),
        'origin': np.zeros(3, 'd'),
        'microstructures': 0,
        'homogenization': options.homogenization
    }

    coords = [{}, {}, {}]
示例#30
0
                  help='linked ASCIItable')

parser.set_defaults()

(options, filenames) = parser.parse_args()

if options.label is None:
    parser.error('no data columns specified.')
if options.link is None:
    parser.error('no linking columns given.')

# -------------------------------------- process linked ASCIItable --------------------------------

if options.asciitable is not None and os.path.isfile(options.asciitable):

    linkedTable = damask.ASCIItable(name=options.asciitable, readonly=True)
    linkedTable.head_read()  # read ASCII header info of linked table
    linkDim = linkedTable.label_dimension(
        options.link[1])  # dimension of linking column

    missing_labels = linkedTable.data_readArray(
        [options.link[1]] + options.label)  # try reading linked ASCII table
    linkedTable.close()  # close linked ASCII table

    if len(missing_labels) > 0:
        damask.util.croak('column{} {} not found...'.format(
            's' if len(missing_labels) > 1 else '', ', '.join(missing_labels)))
    if len(missing_labels) >= len(options.label):
        damask.util.croak('aborting...')
        sys.exit()