def test_rod2U2rod(self): rodvec = n.array([0.23, -0.34, 0.7]) Umat = tools.rod_to_u(rodvec) rodvec2 = tools.u_to_rod(Umat) diff = n.abs(rodvec - rodvec2).sum() self.assertAlmostEqual(diff, 0, 9)
def test_U2rod2U(self): phi1 = 0.2 PHI = 0.4 phi2 = 0.1 Umat = tools.euler_to_u(phi1, PHI, phi2) rodvec = tools.u_to_rod(Umat) Umat2 = tools.rod_to_u(rodvec) diff = n.abs(Umat - Umat2).sum() self.assertAlmostEqual(diff, 0, 9)
def write_errors(lsqr, i): """ Save the fitted grain error parameters, pos, U and eps INPUT: The error set from the final fitting OUTPUT: grainno mean_IA grainvolume x y z rodx rody rodz U11 U12 U13 U21 U22 U23 U31 U32 U33 eps11 eps22 eps33 eps23 eps13 eps12 eps11_s eps22_s eps33_s eps23_s eps13_s eps12_s sig11 sig22 sig33 sig23 sig13 sig12 sig11_s sig22_s sig33_s sig23_s sig13_s sig12_s sig_tth sig_eta Jette Oddershede, Risoe DTU, June 23 2008 """ # error transformation into other coordinate system and from strain to stress under construction U = tools.rod_to_u([ lsqr.inp.rod[i][0] + lsqr.m.values['rodx%s' % i], lsqr.inp.rod[i][1] + lsqr.m.values['rody%s' % i], lsqr.inp.rod[i][2] + lsqr.m.values['rodz%s' % i], ]) eps_ref = False rodx_err = 0 rody_err = 0 rodz_err = 0 for key in lsqr.mg.covariance.keys(): if 'epsaa%s' % i in key: eps_ref = True break if eps_ref == False: cov_eps = n.zeros((6, 6)) else: try: free = lsqr.mg.fixed.values().count(False) except: free = lsqr.mg.fitarg.values().count(False) assert free >= 6, 'wrong dimensions of covariance matrix' covariance = n.zeros((free, free)) j1 = 0 for entry1 in lsqr.grains[i]: try: if lsqr.mg.fixed[entry1] == False: j2 = 0 for entry2 in lsqr.grains[i]: if lsqr.mg.fixed[entry2] == False: covariance[j1][j2] = lsqr.mg.covariance[('%s' % entry1, '%s' % entry2)] j2 = j2 + 1 j1 = j1 + 1 except: if lsqr.mg.fitarg["fix_%s" % entry1] == False: j2 = 0 for entry2 in lsqr.grains[i]: if lsqr.mg.fitarg["fix_%s" % entry2] == False: covariance[j1][j2] = lsqr.mg.covariance[('%s' % entry1, '%s' % entry2)] j2 = j2 + 1 j1 = j1 + 1 if free == 6: cov_eps = covariance else: #derivatives = n.linalg.inv(covariance) #derivatives_rod = derivatives[len(derivatives)-9:len(derivatives)-6,len(derivatives)-9:len(derivatives)-6] #cov_rod = n.linalg.inv(derivatives_rod) #vars_rod = n.linalg.eig(cov_rod)[0] #derivatives = derivatives[len(derivatives)-6:,len(derivatives)-6:] #cov_eps = n.linalg.inv(derivatives) cov_rod = covariance[len(covariance) - 9:len(covariance) - 6, len(covariance) - 9:len(covariance) - 6] vars_rod = n.linalg.eig(cov_rod)[0] cov_eps = covariance[len(covariance) - 6:, len(covariance) - 6:] # old way of doing it including covariances with orientations in strain errors try: cov_eps = n.array( [[ lsqr.mg.covariance[('epsaa%s' % i, 'epsaa%s' % i)], lsqr.mg.covariance[('epsaa%s' % i, 'epsbb%s' % i)], lsqr.mg.covariance[('epsaa%s' % i, 'epscc%s' % i)], lsqr.mg.covariance[('epsaa%s' % i, 'epsbc%s' % i)], lsqr.mg.covariance[('epsaa%s' % i, 'epsac%s' % i)], lsqr.mg.covariance[('epsaa%s' % i, 'epsab%s' % i)] ], [ lsqr.mg.covariance[('epsbb%s' % i, 'epsaa%s' % i)], lsqr.mg.covariance[('epsbb%s' % i, 'epsbb%s' % i)], lsqr.mg.covariance[('epsbb%s' % i, 'epscc%s' % i)], lsqr.mg.covariance[('epsbb%s' % i, 'epsbc%s' % i)], lsqr.mg.covariance[('epsbb%s' % i, 'epsac%s' % i)], lsqr.mg.covariance[('epsbb%s' % i, 'epsab%s' % i)] ], [ lsqr.mg.covariance[('epscc%s' % i, 'epsaa%s' % i)], lsqr.mg.covariance[('epscc%s' % i, 'epsbb%s' % i)], lsqr.mg.covariance[('epscc%s' % i, 'epscc%s' % i)], lsqr.mg.covariance[('epscc%s' % i, 'epsbc%s' % i)], lsqr.mg.covariance[('epscc%s' % i, 'epsac%s' % i)], lsqr.mg.covariance[('epscc%s' % i, 'epsab%s' % i)] ], [ lsqr.mg.covariance[('epsbc%s' % i, 'epsaa%s' % i)], lsqr.mg.covariance[('epsbc%s' % i, 'epsbb%s' % i)], lsqr.mg.covariance[('epsbc%s' % i, 'epscc%s' % i)], lsqr.mg.covariance[('epsbc%s' % i, 'epsbc%s' % i)], lsqr.mg.covariance[('epsbc%s' % i, 'epsac%s' % i)], lsqr.mg.covariance[('epsbc%s' % i, 'epsab%s' % i)] ], [ lsqr.mg.covariance[('epsac%s' % i, 'epsaa%s' % i)], lsqr.mg.covariance[('epsac%s' % i, 'epsbb%s' % i)], lsqr.mg.covariance[('epsac%s' % i, 'epscc%s' % i)], lsqr.mg.covariance[('epsac%s' % i, 'epsbc%s' % i)], lsqr.mg.covariance[('epsac%s' % i, 'epsac%s' % i)], lsqr.mg.covariance[('epsac%s' % i, 'epsab%s' % i)] ], [ lsqr.mg.covariance[('epsab%s' % i, 'epsaa%s' % i)], lsqr.mg.covariance[('epsab%s' % i, 'epsbb%s' % i)], lsqr.mg.covariance[('epsab%s' % i, 'epscc%s' % i)], lsqr.mg.covariance[('epsab%s' % i, 'epsbc%s' % i)], lsqr.mg.covariance[('epsab%s' % i, 'epsac%s' % i)], lsqr.mg.covariance[('epsab%s' % i, 'epsab%s' % i)] ]]) except: cov_eps = n.zeros((6, 6)) cov_eps_s = conversion.CovarianceRotation(cov_eps, U) cov_sig = conversion.CovarianceTransformation(cov_eps, lsqr.inp.C) cov_sig_s = conversion.CovarianceRotation(cov_sig, U) filename = '%s/%s_errors.gff' % (lsqr.inp.fit['direc'], lsqr.inp.fit['stem']) filename2 = '%s/%s_%s_errors.gff' % ( lsqr.inp.fit['direc'], lsqr.inp.fit['stem'], lsqr.inp.fit['goon']) try: f = open(filename, 'r') lines = f.readlines() f.close() except: f = open(filename, 'w') out = "# grainno mean_IA grainvolume x y z rodx rody rodz U11 U12 U13 U21 U22 U23 U31 U32 U33 eps11 eps22 eps33 eps23 eps13 eps12 " out = out + "eps11_s eps22_s eps33_s eps23_s eps13_s eps12_s sig11 sig22 sig33 sig23 sig13 sig12 sig11_s sig22_s sig33_s sig23_s sig13_s sig12_s sig_tth sig_eta\n" f.write(out) f.close() f = open(filename, 'r') lines = f.readlines() f.close() index = 0 for j in range(1, len(lines)): if i + 1 == eval(split(lines[j])[0]): index = j break if index == 0: index = len(lines) lines.append('') format = "%d " * 1 + "%f " * 8 + "%0.12f " * 9 + "%e " * 24 + "%f " * 2 + "\n" lines[index] = format % ( i + 1, reject.spread(lsqr.inp.mean_ia[i]), #0 # reject.spread(lsqr.inp.volume[i]), reject.median_absolute_deviation(lsqr.inp.volume[i]), lsqr.mg.errors['x%s' % i] / 1000, lsqr.mg.errors['y%s' % i] / 1000, lsqr.mg.errors['z%s' % i] / 1000, lsqr.mg.errors['rodx%s' % i], lsqr.mg.errors['rody%s' % i], lsqr.mg.errors['rodz%s' % i], 0, 0, 0, 0, 0, 0, 0, 0, 0, n.sqrt(cov_eps[0][0]), n.sqrt(cov_eps[1][1]), n.sqrt(cov_eps[2][2]), n.sqrt(cov_eps[3][3]), n.sqrt(cov_eps[4][4]), n.sqrt(cov_eps[5][5]), n.sqrt(cov_eps_s[0][0]), n.sqrt(cov_eps_s[1][1]), n.sqrt(cov_eps_s[2][2]), n.sqrt(cov_eps_s[3][3]), n.sqrt(cov_eps_s[4][4]), n.sqrt(cov_eps_s[5][5]), n.sqrt(cov_sig[0][0]), n.sqrt(cov_sig[1][1]), n.sqrt(cov_sig[2][2]), n.sqrt(cov_sig[3][3]), n.sqrt(cov_sig[4][4]), n.sqrt(cov_sig[5][5]), n.sqrt(cov_sig_s[0][0]), n.sqrt(cov_sig_s[1][1]), n.sqrt(cov_sig_s[2][2]), n.sqrt(cov_sig_s[3][3]), n.sqrt(cov_sig_s[4][4]), n.sqrt(cov_sig_s[5][5]), reject.median_absolute_deviation(lsqr.inp.spr_tth[i]), reject.median_absolute_deviation(lsqr.inp.spr_eta[i])) f = open(filename, 'w') for j in range(len(lines)): f.write(lines[j]) f.close() f = open(filename2, 'w') for j in range(len(lines)): f.write(lines[j]) f.close() # Additional writing of file containing covariances filename = '%s/%s_cov_eps_sig.gff' % (lsqr.inp.fit['direc'], lsqr.inp.fit['stem']) try: f = open(filename, 'r') lines = f.readlines() f.close() except: f = open(filename, 'w') out = "# grainno e11e11 e11e22 e11e33 e11e23 e11e13 e11e12 " out = out + "e22e22 e22e33 e22e23 e22e13 e22e12 " out = out + "e33e33 e33e23 e33e13 e33e12 " out = out + "e23e23 e23e13 e23e12 e13e13 e13e12 e12e12 " out = out + "e11e11_s e11e22_s e11e33_s e11e23_s e11e13_s e11e12_s " out = out + "e22e22_s e22e33_s e22e23_s e22e13_s e22e12_s " out = out + "e33e33_s e33e23_s e33e13_s e33e12_s " out = out + "e23e23_s e23e13_s e23e12_s e13e13_s e13e12_s e12e12_s " out = out + "s11s11 s11s22 s11s33 s11s23 s11s13 s11s12 " out = out + "s22s22 s22s33 s22s23 s22s13 s22s12 " out = out + "s33s33 s33s23 s33s13 s33s12 " out = out + "s23s23 s23s13 s23s12 s13s13 s13s12 s12s12 " out = out + "s11s11_s s11s22_s s11s33_s s11s23_s s11s13_s s11s12_s " out = out + "s22s22_s s22s33_s s22s23_s s22s13_s s22s12_s " out = out + "s33s33_s s33s23_s s33s13_s s33s12_s " out = out + "s23s23_s s23s13_s s23s12_s s13s13_s s13s12_s s12s12_s \n" f.write(out) f.close() f = open(filename, 'r') lines = f.readlines() f.close() index = 0 for j in range(1, len(lines)): if i + 1 == eval(split(lines[j])[0]): index = j break if index == 0: index = len(lines) lines.append('') format = "%d " * 1 + "%e " * 84 + "\n" lines[index] = format % ( i + 1, cov_eps[0][0], cov_eps[0][1], cov_eps[0][2], cov_eps[0][3], cov_eps[0][4], cov_eps[0][5], cov_eps[1][1], cov_eps[1][2], cov_eps[1][3], cov_eps[1][4], cov_eps[1][5], cov_eps[2][2], cov_eps[2][3], cov_eps[2][4], cov_eps[2][5], cov_eps[3][3], cov_eps[3][4], cov_eps[3][5], cov_eps[4][4], cov_eps[4][5], cov_eps[5][5], cov_eps_s[0][0], cov_eps_s[0][1], cov_eps_s[0][2], cov_eps_s[0][3], cov_eps_s[0][4], cov_eps_s[0][5], cov_eps_s[1][1], cov_eps_s[1][2], cov_eps_s[1][3], cov_eps_s[1][4], cov_eps_s[1][5], cov_eps_s[2][2], cov_eps_s[2][3], cov_eps_s[2][4], cov_eps_s[2][5], cov_eps_s[3][3], cov_eps_s[3][4], cov_eps_s[3][5], cov_eps_s[4][4], cov_eps_s[4][5], cov_eps_s[5][5], cov_sig[0][0], cov_sig[0][1], cov_sig[0][2], cov_sig[0][3], cov_sig[0][4], cov_sig[0][5], cov_sig[1][1], cov_sig[1][2], cov_sig[1][3], cov_sig[1][4], cov_sig[1][5], cov_sig[2][2], cov_sig[2][3], cov_sig[2][4], cov_sig[2][5], cov_sig[3][3], cov_sig[3][4], cov_sig[3][5], cov_sig[4][4], cov_sig[4][5], cov_sig[5][5], cov_sig_s[0][0], cov_sig_s[0][1], cov_sig_s[0][2], cov_sig_s[0][3], cov_sig_s[0][4], cov_sig_s[0][5], cov_sig_s[1][1], cov_sig_s[1][2], cov_sig_s[1][3], cov_sig_s[1][4], cov_sig_s[1][5], cov_sig_s[2][2], cov_sig_s[2][3], cov_sig_s[2][4], cov_sig_s[2][5], cov_sig_s[3][3], cov_sig_s[3][4], cov_sig_s[3][5], cov_sig_s[4][4], cov_sig_s[4][5], cov_sig_s[5][5], ) f = open(filename, 'w') for j in range(len(lines)): f.write(lines[j]) f.close()
def write_values(lsqr): """ Save the fitted grain parameters, pos, U and eps INPUT: The parameter set from the final fitting OUTPUT: grainno mean_IA grain_volume x y z rodx rody rodz U11 U12 U13 U21 U22 U23 U31 U32 U33 eps11 eps22 eps33 eps23 eps13 eps12 eps11_s eps22_s eps33_s eps23_s eps13_s eps12_s sig11 sig22 sig33 sig23 sig13 sig12 sig11_s sig22_s sig33_s sig23_s sig13_s sig12_s sig_tth sig_eta Jette Oddershede, Risoe DTU, April 21 2008 """ filename = '%s/%s_%s.gff' % (lsqr.inp.fit['direc'], lsqr.inp.fit['stem'], lsqr.inp.fit['goon']) f = open(filename, 'w') format = "%d " * 1 + "%f " * 8 + "%0.12f " * 9 + "%e " * 24 + "%f " * 2 + "\n" out = "# grainno mean_IA grainvolume x y z rodx rody rodz U11 U12 U13 U21 U22 U23 U31 U32 U33 eps11 eps22 eps33 eps23 eps13 eps12 " out = out + "eps11_s eps22_s eps33_s eps23_s eps13_s eps12_s sig11 sig22 sig33 sig23 sig13 sig12 sig11_s sig22_s sig33_s sig23_s sig13_s sig12_s sig_tth sig_eta\n" f.write(out) for i in range(lsqr.inp.no_grains): if i + 1 in lsqr.inp.fit['skip']: pass else: U = tools.rod_to_u([ lsqr.inp.rod[i][0] + lsqr.m.values['rodx%s' % i], lsqr.inp.rod[i][1] + lsqr.m.values['rody%s' % i], lsqr.inp.rod[i][2] + lsqr.m.values['rodz%s' % i], ]) eps = n.array([[ lsqr.m.values['epsaa%s' % i], lsqr.m.values['epsab%s' % i], lsqr.m.values['epsac%s' % i] ], [ lsqr.m.values['epsab%s' % i], lsqr.m.values['epsbb%s' % i], lsqr.m.values['epsbc%s' % i] ], [ lsqr.m.values['epsac%s' % i], lsqr.m.values['epsbc%s' % i], lsqr.m.values['epscc%s' % i] ]]) eps_s = conversion.grain2sample(eps, U) sig = conversion.strain2stress(eps, lsqr.inp.C) sig_s = conversion.grain2sample(sig, U) out = format % ( i + 1, n.sum(lsqr.inp.mean_ia[i]) / len(lsqr.inp.mean_ia[i]), #0, # sum(lsqr.inp.volume[i])/lsqr.inp.nrefl[i], reject.median(lsqr.inp.volume[i]), lsqr.m.values['x%s' % i] / 1000, lsqr.m.values['y%s' % i] / 1000, lsqr.m.values['z%s' % i] / 1000, lsqr.inp.rod[i][0] + lsqr.m.values['rodx%s' % i], lsqr.inp.rod[i][1] + lsqr.m.values['rody%s' % i], lsqr.inp.rod[i][2] + lsqr.m.values['rodz%s' % i], U[0, 0], U[0, 1], U[0, 2], U[1, 0], U[1, 1], U[1, 2], U[2, 0], U[2, 1], U[2, 2], eps[0][0], eps[1][1], eps[2][2], eps[1][2], eps[0][2], eps[0][1], eps_s[0][0], eps_s[1][1], eps_s[2][2], eps_s[1][2], eps_s[0][2], eps_s[0][1], sig[0][0], sig[1][1], sig[2][2], sig[1][2], sig[0][2], sig[0][1], sig_s[0][0], sig_s[1][1], sig_s[2][2], sig_s[1][2], sig_s[0][2], sig_s[0][1], reject.median(lsqr.inp.spr_tth[i]), reject.median(lsqr.inp.spr_eta[i])) f.write(out) f.close()
def setup_odf(self): odf_scale = self.graindata.param['odf_scale'] if self.graindata.param['odf_type'] == 1: odf_spread = self.graindata.param['mosaicity'] / 4 odf_spread_grid = odf_spread / odf_scale sigma = odf_spread_grid * n.ones(3) r1_max = int(n.ceil(3 * odf_spread_grid)) r1_range = r1_max * 2 + 1 r2_range = r1_max * 2 + 1 r3_range = r1_max * 2 + 1 mapsize = r1_range * n.ones(3) odf_center = r1_max * n.ones(3) print('size of ODF map', mapsize) self.odf = generate_grains.gen_odf(sigma, odf_center, mapsize) #from pylab import * #imshow(self.odf[:,:,odf_center[2]]) #show() elif self.graindata.param['odf_type'] == 3: odf_spread = self.graindata.param['mosaicity'] / 4 odf_spread_grid = odf_spread / odf_scale r1_max = n.ceil(3 * odf_spread_grid) r2_max = n.ceil(3 * odf_spread_grid) r3_max = n.ceil(3 * odf_spread_grid) r1_range = r1_max * 2 + 1 r2_range = r2_max * 2 + 1 r3_range = r3_max * 2 + 1 print('size of ODF map', r1_range * n.ones(3)) odf_center = r1_max * n.ones(3) self.odf = n.zeros((r1_range, r2_range, r3_range)) # Makes spheric ODF for debug purpuses for i in range(self.odf.shape[0]): for j in range(self.odf.shape[1]): for k in range(self.odf.shape[2]): r = [i - (r1_max), j - (r2_max), k - (r3_max)] if n.linalg.norm(r) > r1_max: self.odf[i, j, k] = 0 else: self.odf[i, j, k] = 1 #from pylab import * #imshow(self.odf[:,:,r3_max],interpolation=None) #show() elif self.graindata.param['odf_type'] == 2: file = self.graindata.param['odf_file'] print('Read ODF from file_ %s' % file) file = open(file, 'r') (r1_range, r2_range, r3_range) = file.readline()[9:].split() r1_range = int(r1_range) r2_range = int(r2_range) r3_range = int(r3_range) odf_scale = float(file.readline()[10:]) oneD_odf = n.fromstring(file.readline(), sep=' ') elements = r1_range * r2_range * r3_range self.odf = oneD_odf[:elements].reshape(r1_range, r2_range, r3_range) if self.graindata.param['odf_sub_sample'] > 1: sub = self.graindata.param['odf_sub_sample'] print('subscale =', sub) r1_range_sub = r1_range * self.graindata.param['odf_sub_sample'] r2_range_sub = r2_range * self.graindata.param['odf_sub_sample'] r3_range_sub = r3_range * self.graindata.param['odf_sub_sample'] odf_fine = n.zeros((r1_range_sub, r2_range_sub, r3_range_sub)) for i in range(r1_range): for j in range(r2_range): for k in range(r3_range): odf_fine[i * sub:(i + 1) * sub, j * sub:(j + 1) * sub, k * sub:(k + 1) * sub] = self.odf[i, j, k] self.odf = odf_fine.copy() / (sub * sub * sub) r1_range = r1_range_sub r2_range = r2_range_sub r3_range = r3_range_sub odf_scale = odf_scale / sub print('odf_scale', odf_scale) #[r1_range, r2_range, r3_range] = self.odf.shape odf_center = [(r1_range) / 2, r2_range / 2, r3_range / 2] print(odf_center) #self.odf[:,:,:] = 0.05 print(self.odf.shape) #from pylab import * #imshow(self.odf[:,:,odf_center[2]]) #show() self.Uodf = n.zeros(r1_range*r2_range*r3_range*9).\ reshape(r1_range,r2_range,r3_range,3,3) if self.graindata.param['odf_cut'] != None: self.odf_cut = self.odf.max() * self.graindata.param['odf_cut'] else: self.odf_cut = 0.0 for i in range(self.odf.shape[0]): for j in range(self.odf.shape[1]): for k in range(self.odf.shape[2]): r = odf_scale*n.pi/180.*\ n.array([i-odf_center[0], j-odf_center[1], k-odf_center[2]]) self.Uodf[i, j, k, :, :] = tools.rod_to_u(r) if self.graindata.param['odf_type'] != 2: file = open(self.graindata.param['stem'] + '.odf', 'w') file.write('ODF size: %i %i %i\n' % (r1_range, r2_range, r3_range)) file.write('ODF scale: %f\n' % (odf_scale)) for i in range(int(r1_range)): self.odf[i, :, :].tofile(file, sep=' ', format='%f') file.write(' ') file.close() return self.Uodf
def find_refl(inp): """ From U, (x,y,z) and B determined for the far-field case calculate the possible reflection on the near-field detector output[grainno][reflno]=[h,k,l,omega,dety,detz,tth,eta] """ S = n.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) R = tools.detect_tilt(inp.param['tilt_x'], inp.param['tilt_y'], inp.param['tilt_z']) inp.possible = [] # if structure info is given use this if inp.files['structure_file'] != None: inp.param['structure_phase_0'] = inp.files['structure_file'] xtal_structure = reflections.open_structure(inp.param,0) HKL = reflections.gen_miller(inp.param,0) else: inp.param['unit_cell_phase_0'] = inp.unit_cell inp.param['sgno_phase_0'] = inp.fit['sgno'] HKL = reflections.gen_miller(inp.param,0) for grainno in range(inp.no_grains): inp.possible.append([]) if grainno+1 not in inp.fit['skip']: B = tools.epsilon_to_b(n.array([inp.values['epsaa%s' %grainno], inp.values['epsab%s' %grainno], inp.values['epsac%s' %grainno], inp.values['epsbb%s' %grainno], inp.values['epsbc%s' %grainno], inp.values['epscc%s' %grainno]]), inp.unit_cell) U = tools.rod_to_u([inp.rod[grainno][0]+inp.values['rodx%s' %grainno], inp.rod[grainno][1]+inp.values['rody%s' %grainno], inp.rod[grainno][2]+inp.values['rodz%s' %grainno]]) gr_pos = n.array([inp.values['x%s' %grainno], inp.values['y%s' %grainno], inp.values['z%s' %grainno]]) for hkl in HKL: Gc = n.dot(B,hkl[0:3]) Gw = n.dot(S,n.dot(U,Gc)) tth = tools.tth2(Gw,inp.param['wavelength']) # print hkl[0:3],tth*180./n.pi costth = n.cos(tth) (Omega, Eta) = tools.find_omega_general(inp.param['wavelength']/(4.*n.pi)*Gw, tth, inp.values['wx']*n.pi/180, inp.values['wy']*n.pi/180) if len(Omega) > 0: for solution in range(len(Omega)): omega = Omega[solution] eta = Eta[solution] for i in range(len(inp.fit['w_limit'])//2): if (inp.fit['w_limit'][2*i]*n.pi/180) < omega and\ omega < (inp.fit['w_limit'][2*i+1]*n.pi/180): # form Omega rotation matrix Om = tools.form_omega_mat_general(omega,inp.values['wx']*n.pi/180,inp.values['wy']*n.pi/180) Gt = n.dot(Om,Gw) # Calc crystal position at present omega [tx,ty,tz]= n.dot(Om,gr_pos) # Calc detector coordinate for peak (dety, detz) = detector.det_coor(Gt,costth, inp.param['wavelength'], inp.param['distance'], inp.param['y_size'], inp.param['z_size'], inp.param['y_center'], inp.param['z_center'], R,tx,ty,tz) #If peak within detector frame store it in possible if (-0.5 < dety) and\ (dety < inp.fit['dety_size']-0.5) and\ (-0.5 < detz) and\ (detz < inp.fit['detz_size']-0.5): inp.possible[grainno].append([hkl[0], hkl[1], hkl[2], omega*180/n.pi, dety, detz, tth, eta])
def read(file, sym, minimum, maximum, step): data = ic.columnfile('%s.gff' % file) #grain sizes if "grainno" not in data.titles: data.addcolumn(data.grain_id, 'grainno') if "grainvolume" in data.titles: scale = max(data.grainvolume**0.3333) data.addcolumn(data.grainvolume**0.3333 / scale * 100, 'size') else: data.addcolumn(100. * np.ones(data.nrows), 'size') rodx = [] rody = [] rodz = [] if "rodx" not in data.titles: for i in range(data.nrows): U = np.array([[data.U11[i], data.U12[i], data.U13[i]], [data.U21[i], data.U22[i], data.U23[i]], [data.U31[i], data.U32[i], data.U33[i]]]) rod = tools.u_to_rod(U) rodx.append(rod[0]) rody.append(rod[1]) rodz.append(rod[2]) data.addcolumn(np.array(rodx), 'rodx') data.addcolumn(np.array(rody), 'rody') data.addcolumn(np.array(rodz), 'rodz') if sym == "standard": #grain colours, so that all orientations in Cubic space are allowed maxhx = 62.8 * 3.14 / 180 minhx = -62.8 * 3.14 / 180 maxhy = 62.8 * 3.14 / 180 minhy = -62.8 * 3.14 / 180 maxhz = 62.8 * 3.14 / 180 minhz = -62.8 * 3.14 / 180 rr = data.rodx**2 + data.rody**2 + data.rodz**2 rr = np.sqrt(rr) theta = 2 * np.arctan(rr) r1 = data.rodx / rr r2 = data.rody / rr r3 = data.rodz / rr # normalise colours red = (r1 * theta - minhx) / (maxhx - minhx) green = (r2 * theta - minhy) / (maxhy - minhy) blue = (r3 * theta - minhz) / (maxhz - minhz) elif sym == "orthorhombic": # Fill orthorhombic stereographic triangle red = [] green = [] blue = [] fig = pl.figure(10, frameon=False, figsize=pl.figaspect(1.0)) ax = pl.Axes(fig, [.2, .2, .7, .7]) ax.set_axis_off() fig.add_axes(ax) #plot triangle xa = np.zeros((101)) ya = np.zeros((101)) for i in range(101): xa[i] = np.cos(i * np.pi / 200.) ya[i] = np.sin(i * np.pi / 200.) pl.plot(xa, ya, 'black') # Curved edge pl.plot([0, 1], [0, 0], 'black', linewidth=2) #lower line pl.plot([0, 0], [1, 0], 'black', linewidth=2) #left line pl.text(-0.02, -0.04, '[001]') pl.text(0.95, -0.04, '[100]') pl.text(-0.02, 1.01, '[010]') # Grains for i in range(data.nrows): U = tools.rod_to_u([data.rodx[i], data.rody[i], data.rodz[i]]) axis = abs(U[2, :]) colour = np.zeros((3)) colour[0] = (2 * np.arcsin(abs(axis[2])) / np.pi)**1 colour[1] = (2 * np.arcsin(abs(axis[0])) / np.pi)**1 colour[2] = (2 * np.arcsin(abs(axis[1])) / np.pi)**1 mx = max(colour) colour = colour / mx red.append(colour[0]) green.append(colour[1]) blue.append(colour[2]) X = axis[0] / (1 + axis[2]) Y = axis[1] / (1 + axis[2]) pl.plot(X, Y, 'o', color=colour) pl.xlim() elif sym == "cubic": # Fill cubic stereographic triangle red = [] green = [] blue = [] fig = pl.figure(10, frameon=False, figsize=pl.figaspect(.9)) ax = pl.Axes(fig, [.2, .2, .7, .7]) ax.set_axis_off() fig.add_axes(ax) #plot triangle xa = np.zeros((21)) ya = np.zeros((21)) for i in range(21): ua = np.array([i / 20., 1., 1.]) UA = np.linalg.norm(ua) za = ua[2] + UA xa[i] = ua[1] / za ya[i] = ua[0] / za pl.plot(xa, ya, 'black') # Curved edge pl.plot([0, xa[0]], [0, 0.0001], 'black', linewidth=2) #lower line pl.plot([xa[20], 0], [ya[20], 0], 'black') # upper line pl.text(-0.01, -0.02, '[100]') pl.text(xa[0] - 0.01, -0.02, '[110]') pl.text(xa[-1] - 0.01, ya[-1] + 0.005, '[111]') # Grains for i in range(data.nrows): U = tools.rod_to_u([data.rodx[i], data.rody[i], data.rodz[i]]) axis = abs(U[2, :]) colour = np.zeros((3)) for j in range(3): for k in range(j + 1, 3): if (axis[j] > axis[k]): colour[0] = axis[j] axis[j] = axis[k] axis[k] = colour[0] rr = np.sqrt(axis[0] * axis[0] / ((axis[2] + 1)) / ((axis[2] + 1)) + (axis[1] / (axis[2] + 1) + 1) * (axis[1] / (axis[2] + 1) + 1)) if axis[1] == 0: beta = 0 else: beta = np.arctan(axis[0] / axis[1]) colour[0] = ((np.sqrt(2.0) - rr) / (np.sqrt(2.0) - 1))**.5 colour[1] = ((1 - 4 * beta / np.pi) * ((rr - 1) / (np.sqrt(2.0) - 1)))**.5 colour[2] = (4 * beta / np.pi * ((rr - 1) / (np.sqrt(2.0) - 1)))**.5 mx = max(colour) colour = colour / mx red.append(colour[0]) green.append(colour[1]) blue.append(colour[2]) X = axis[1] / (1 + axis[2]) Y = axis[0] / (1 + axis[2]) pl.plot(X, Y, 'o', color=colour) pl.xlim() elif sym == "hexagonal": ## Fill hexagonal stereographic triangle A = np.array([[0, 1, 0], [0, -np.sqrt(3), 1], [1, 0, 0]]) a0 = 1. / np.sqrt(3.) a1 = 1. a2 = 1. red = [] green = [] blue = [] fig = pl.figure(10, frameon=False, figsize=pl.figaspect(0.5)) ax = pl.Axes(fig, [0.2, .2, 0.6, 0.6]) ax.set_axis_off() fig.add_axes(ax) ## Plot triangle ya = np.array(list(range(51))) / 100. xa = np.sqrt(1 - ya**2) pl.plot(xa, ya, 'black') # Curved edge pl.plot([0, xa[0]], [0, 0.0001], 'black', linewidth=2) #lower line pl.plot([xa[-1], 0], [ya[-1], 0], 'black') # upper line ## Label crystalographic directions pl.text(-0.01, -0.02, '[0001]') pl.text(xa[0] - 0.03, -0.02, '[2-1-10]') pl.text(xa[-1] - 0.03, ya[-1] + 0.005, '[10-10]') ## Grains r = symmetry.rotations(6) for i in range(data.nrows): U = tools.rod_to_u([data.rodx[i], data.rody[i], data.rodz[i]]) square = 1 angle = 0 frac = 1. / np.sqrt(3.) for k in range(len(r)): g = np.dot(U, r[k]) a = np.arccos((np.trace(g) - 1) / 2) if g[2, 2] > 0: uvw = g[2, :] else: uvw = -g[2, :] ## needed to switch these indices to get correct color and inv pf location switch1 = uvw[0] switch2 = uvw[1] uvw[0] = switch2 uvw[1] = switch1 x = uvw[0] / (1 + uvw[2]) y = uvw[1] / (1 + uvw[2]) f = y / x s = x * x + y * y ## Finds r (symmetry) which plots grain into triangle if f <= frac and s <= square and x >= 0 and y >= 0: angle = a frac = f square = s UVW = uvw X = x Y = y colour = np.dot(np.transpose(A), np.transpose(UVW))**0.7 colour[0] = colour[0] / a2 colour[1] = colour[1] / a1 colour[2] = colour[2] / a0 mx = max(colour) colour = colour / mx red.append(colour[0]) green.append(colour[1]) blue.append(colour[2]) pl.plot(X, Y, 'o', color=colour) pl.xlim() elif sym == "e11": try: colourbar(minimum, maximum, step, 'e-3') norm = colors.normalize(minimum * 1e-3, maximum * 1e-3) except: colourbar(-1, 1, 1, 'e-3') norm = colors.normalize(-1e-3, 1e-3) color = cm.jet(norm(data.eps11_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "transverse strain:", np.sum(data.grainvolume * data.eps11_s) / np.sum(data.grainvolume)) elif sym == "e22": try: colourbar(minimum, maximum, step, 'e-3') norm = colors.normalize(minimum * 1e-3, maximum * 1e-3) except: colourbar(-1, 1, 1, 'e-3') norm = colors.normalize(-1e-3, 1e-3) color = cm.jet(norm(data.eps22_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "transverse strain:", np.sum(data.grainvolume * data.eps22_s) / np.sum(data.grainvolume)) elif sym == "e33": try: colourbar(minimum, maximum, step, 'e-3') norm = colors.normalize(minimum * 1e-3, maximum * 1e-3) except: colourbar(-1, 1, 1, 'e-3') norm = colors.normalize(-1e-3, 1e-3) color = cm.jet(norm(data.eps33_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "axial strain:", np.sum(data.grainvolume * data.eps33_s) / np.sum(data.grainvolume)) elif sym == "e12": try: colourbar(minimum, maximum, step, 'e-3') norm = colors.normalize(minimum * 1e-3, maximum * 1e-3) except: colourbar(-1, 1, 1, 'e-3') norm = colors.normalize(-1e-3, 1e-3) color = cm.jet(norm(data.eps12_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "shear strain:", np.sum(data.grainvolume * data.eps12_s) / np.sum(data.grainvolume)) elif sym == "e13": try: colourbar(minimum, maximum, step, 'e-3') norm = colors.normalize(minimum * 1e-3, maximum * 1e-3) except: colourbar(-1, 1, 1, 'e-3') norm = colors.normalize(-1e-3, 1e-3) color = cm.jet(norm(data.eps13_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "shear strain:", np.sum(data.grainvolume * data.eps13_s) / np.sum(data.grainvolume)) elif sym == "e23": try: colourbar(minimum, maximum, step, 'e-3') norm = colors.normalize(minimum * 1e-3, maximum * 1e-3) except: colourbar(-1, 1, 1, 'e-3') norm = colors.normalize(-1e-3, 1e-3) color = cm.jet(norm(data.eps23_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "shear strain:", np.sum(data.grainvolume * data.eps23_s) / np.sum(data.grainvolume)) elif sym == "s33": try: colourbar(minimum, maximum, step, 'MPa') norm = colors.normalize(minimum, maximum) except: colourbar(-50, 150, 50, 'MPa') norm = colors.normalize(-50, 150) color = cm.jet(norm(data.sig33_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "axial stress:", np.sum(data.grainvolume * data.sig33_s) / np.sum(data.grainvolume), "MPa") elif sym == "s11": try: colourbar(minimum, maximum, step, 'MPa') norm = colors.normalize(minimum, maximum) except: colourbar(-50, 150, 50, 'MPa') norm = colors.normalize(-50, 150) color = cm.jet(norm(data.sig11_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "transverse s11 stress:", np.sum(data.grainvolume * data.sig11_s) / np.sum(data.grainvolume), "MPa") elif sym == "s22": try: colourbar(minimum, maximum, step, 'MPa') norm = colors.normalize(minimum, maximum) except: colourbar(-50, 150, 50, 'MPa') norm = colors.normalize(-50, 150) color = cm.jet(norm(data.sig22_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "transverse s22 stress:", np.sum(data.grainvolume * data.sig22_s) / np.sum(data.grainvolume), "MPa") elif sym == "s12": try: colourbar(minimum, maximum, step, 'MPa') norm = colors.normalize(minimum, maximum) except: colourbar(-50, 50, 50, 'MPa') norm = colors.normalize(-50, 50) color = cm.jet(norm(data.sig12_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "shear s12 stress:", np.sum(data.grainvolume * data.sig12_s) / np.sum(data.grainvolume), "MPa") elif sym == "s13": try: colourbar(minimum, maximum, step, 'MPa') norm = colors.normalize(minimum, maximum) except: colourbar(-50, 50, 50, 'MPa') norm = colors.normalize(-50, 50) color = cm.jet(norm(data.sig13_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "shear s13 stress:", np.sum(data.grainvolume * data.sig13_s) / np.sum(data.grainvolume), "MPa") elif sym == "s23": try: colourbar(minimum, maximum, step, 'MPa') norm = colors.normalize(minimum, maximum) except: colourbar(-50, 50, 50, 'MPa') norm = colors.normalize(-50, 50) color = cm.jet(norm(data.sig23_s)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print( "shear s23 stress:", np.sum(data.grainvolume * data.sig23_s) / np.sum(data.grainvolume), "MPa") elif sym == "latt_rot": norm = colors.normalize(0, 0.5) color = cm.jet(norm(data.latt_rot)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] colourbar(0.0, 0.5, 0.1, 'deg') elif sym == "tz": norm = colors.normalize(-0.1, 0.1) color = cm.jet(norm(data.tz)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] elif sym == "vol": norm = colors.normalize(0, 10) color = cm.jet(norm(data.grainvolume / data.d_grainvolume)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] elif sym == "tth": norm = colors.normalize(0.007, 0.009) color = cm.jet(norm(data.sig_tth / data.grainvolume**.2)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print(min(data.sig_tth / data.grainvolume**.2), max(data.sig_tth / data.grainvolume**.2)) # pl.figure(8) # pl.plot(np.log(data.grainvolume),np.log(data.sig_tth),'.') elif sym == "eta": norm = colors.normalize(0.08, 0.15) color = cm.jet(norm(data.sig_eta / data.grainvolume**.2)) red = color[:, 0] green = color[:, 1] blue = color[:, 2] print(min(data.sig_eta / data.grainvolume**.2), max(data.sig_eta / data.grainvolume**.2)) # pl.figure(8) # pl.plot(np.log(data.grainvolume),np.log(data.sig_eta),'.') else: np.random.seed(0) red = np.random.rand(data.nrows) np.random.seed(1) green = np.random.rand(data.nrows) np.random.seed(2) blue = np.random.rand(data.nrows) data.addcolumn(red, 'red') data.addcolumn(green, 'green') data.addcolumn(blue, 'blue') return (data)