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 test_ubi2rod(self): phi1 = 0.13 PHI = 0.4 phi2 = 0.21 cell = [3, 4, 5, 80, 95, 100] Umat = tools.euler_to_u(phi1, PHI, phi2) ubi = n.linalg.inv(n.dot(Umat, tools.form_b_mat(cell))) * 2 * n.pi rodubi = tools.ubi_to_rod(ubi) rodU = tools.u_to_rod(Umat) diff = n.abs(rodubi - rodU).sum() self.assertAlmostEqual(diff, 0, 9)
eps23 = [] eps13 = [] eps12 = [] titles = [ "grainno", "x", "y", "z", "rodx", "rody", "rodz", "U11", "U12", "U13", "U21", "U22", "U23", "U31", "U32", "U33", "eps11", "eps22", "eps33", "eps23", "eps13", "eps12" ] for i in range(len(list_of_grains)): grainno.append(eval(split(list_of_grains[i].name, ':')[0])) x.append(list_of_grains[i].translation[0] / 1000.) y.append(list_of_grains[i].translation[1] / 1000.) z.append(list_of_grains[i].translation[2] / 1000.) ubi = list_of_grains[i].ubi (U, eps) = tools.ubi_to_u_and_eps(ubi, uc) rod = tools.u_to_rod(U) rodx.append(rod[0]) rody.append(rod[1]) rodz.append(rod[2]) U11.append(U[0, 0]) U12.append(U[0, 1]) U13.append(U[0, 2]) U21.append(U[1, 0]) U22.append(U[1, 1]) U23.append(U[1, 2]) U31.append(U[2, 0]) U32.append(U[2, 1]) U33.append(U[2, 2]) eps11.append(eps[0]) eps12.append(eps[1]) eps13.append(eps[2])
def ubi_to_gff(ubi,par): from ImageD11 import grain as ig from ImageD11 import parameters as ip from string import split from xfab import tools from six.moves import range list_of_grains = ig.read_grain_file(ubi) p = ip.parameters() p.loadparameters(par) uc = [p.parameters['cell__a'],p.parameters['cell__b'],p.parameters['cell__c'],p.parameters['cell_alpha'],p.parameters['cell_beta'],p.parameters['cell_gamma']] #print(uc) grainno = [] x = [] y = [] z = [] rodx = [] rody = [] rodz = [] unitcell_a = [] unitcell_b = [] unitcell_c = [] unitcell_alpha = [] unitcell_beta = [] unitcell_gamma = [] U11 = [] U12 = [] U13 = [] U21 = [] U22 = [] U23 = [] U31 = [] U32 = [] U33 = [] eps11 = [] eps22 = [] eps33 = [] eps23 = [] eps13 = [] eps12 = [] titles = ["grainno","x","y","z","rodx","rody","rodz","U11","U12","U13","U21","U22","U23","U31","U32","U33","unitcell_a","unitcell_b","unitcell_c","unitcell_alpha","unitcell_beta","unitcell_gamma","eps11","eps22","eps33","eps23","eps13","eps12"] for i in range(len(list_of_grains)): if hasattr(list_of_grains,'name') == False: grainno.append(i) elif hasattr(list_of_grains,'name') == True: grainno.append(eval(split(list_of_grains[i].name,':')[0])) x.append(list_of_grains[i].translation[0]/1000.) y.append(list_of_grains[i].translation[1]/1000.) z.append(list_of_grains[i].translation[2]/1000.) ubi = list_of_grains[i].ubi (U,eps) = tools.ubi_to_u_and_eps(ubi,uc) uc_a, uc_b, uc_c, uc_alpha, uc_beta, uc_gamma = tools.ubi_to_cell(ubi) unitcell_a.append(uc_a) unitcell_b.append(uc_b) unitcell_c.append(uc_c) unitcell_alpha.append(uc_alpha) unitcell_beta.append(uc_beta) unitcell_gamma.append(uc_gamma) rod = tools.u_to_rod(U) rodx.append(rod[0]) rody.append(rod[1]) rodz.append(rod[2]) U11.append(U[0,0]) U12.append(U[0,1]) U13.append(U[0,2]) U21.append(U[1,0]) U22.append(U[1,1]) U23.append(U[1,2]) U31.append(U[2,0]) U32.append(U[2,1]) U33.append(U[2,2]) eps11.append(eps[0]) eps12.append(eps[1]) eps13.append(eps[2]) eps22.append(eps[3]) eps23.append(eps[4]) eps33.append(eps[5]) gff = cl.newcolumnfile(titles) gff.ncols = len(titles) gff.nrows = len(grainno) gff.bigarray = np.zeros((gff.ncols,gff.nrows)) gff.set_attributes() gff.addcolumn(grainno,"grainno") gff.addcolumn(x,"x") gff.addcolumn(y,"y") gff.addcolumn(z,"z") gff.addcolumn(rodx,"rodx") gff.addcolumn(rody,"rody") gff.addcolumn(rodz,"rodz") gff.addcolumn(U11,"U11") gff.addcolumn(U12,"U12") gff.addcolumn(U13,"U13") gff.addcolumn(U21,"U21") gff.addcolumn(U22,"U22") gff.addcolumn(U23,"U23") gff.addcolumn(U31,"U31") gff.addcolumn(U32,"U32") gff.addcolumn(U33,"U33") gff.addcolumn(unitcell_a,"unitcell_a") gff.addcolumn(unitcell_b,"unitcell_b") gff.addcolumn(unitcell_c,"unitcell_c") gff.addcolumn(unitcell_alpha,"unitcell_alpha") gff.addcolumn(unitcell_beta,"unitcell_beta") gff.addcolumn(unitcell_gamma,"unitcell_gamma") gff.addcolumn(eps11,"eps11") gff.addcolumn(eps22,"eps22") gff.addcolumn(eps33,"eps33") gff.addcolumn(eps23,"eps23") gff.addcolumn(eps13,"eps13") gff.addcolumn(eps12,"eps12") return gff, uc
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)