def f(x): fun_val = [] ubs = x.reshape(nelm, 3, 3) for i in range(nelm): ubi1 = np.linalg.inv(ubs[i]) u1, eps1 = tools.ubi_to_u_and_eps(ubi1, cell) for indx in connectivity[i]: ubi2 = np.linalg.inv(ubs[indx]) u2, eps2 = tools.ubi_to_u_and_eps(ubi2, cell) deps = np.asarray(eps1) - np.asarray(eps2) dang1 = np.dot(u1[:, 0], u2[:, 0]) dang2 = np.dot(u1[:, 1], u2[:, 1]) dang3 = np.dot(u1[:, 2], u2[:, 2]) fun_val.extend(deps) fun_val.extend([dang1, dang2, dang3]) return np.asarray(fun_val)
def test_strain_ref(self): from xfab.tools import ubi_to_u_and_eps dzero_cell = indexing.ubitocellpars( self.ubi0 ) u, eps_xfab_ref_e6 = ubi_to_u_and_eps( self.ubi, dzero_cell ) dgt = finite_strain.DeformationGradientTensor( self.ubi, self.ub0 ) eps_new = dgt.finite_strain_ref( m=0.5 ) self.assertTrue( np.allclose( dgt.U, u ) ) self.assertTrue( np.allclose( dgt.U, self.u ) ) self.assertTrue( np.allclose( finite_strain.symm_to_e6(eps_new), eps_xfab_ref_e6 ) ) self.assertTrue( np.allclose( finite_strain.e6_to_symm(eps_xfab_ref_e6), eps_new ) )
def to_strain_and_angs(s_tilde, cell, nelm): fun_val = [] ubs = s_tilde.reshape(nelm, 3, 3) for i in range(nelm): ubi = np.linalg.inv(ubs[i]) u, eps = tools.ubi_to_u_and_eps(ubi, cell) euler = tools.u_to_euler(u) fun_val.extend(eps) fun_val.extend(list(euler)) return np.asarray(fun_val)
def test_strain_lab(self): from xfab.tools import ubi_to_u_and_eps dzero_cell = indexing.ubitocellpars( self.ubi0 ) u, eps_xfab_ref_e6 = ubi_to_u_and_eps( self.ubi, dzero_cell ) emat = finite_strain.e6_to_symm( eps_xfab_ref_e6 ) Exfab = np.dot(np.dot( u, emat), u.T ) dgt = finite_strain.DeformationGradientTensor( self.ubi, self.ub0 ) eps_new = dgt.finite_strain_lab( m=0.5 ) self.assertTrue( np.allclose( dgt.U, u ) ) self.assertTrue( np.allclose( dgt.U, self.u ) ) self.assertTrue( np.allclose( eps_new, Exfab ) )
def compute_write_eps_sig(self,outputfile): """ Compute strain and stress in crystal and sample co-ordinates system """ if self.ubis is not None: writestress = True f = open(outputfile,'w') ''' the used parameters will be the header of the output file''' for k,v in sorted(self.parameterobj.parameters.iteritems()): f.write(("%s %s\n")%(k,v)) ''' write titles''' f.write("##############################################\n") f.write("cell__a cell__b cell__c cell_alpha cell_beta cell_gamma u11 u12 u13 u21 u22 u23 u31 u32 u33 ") f.write("eps11_c eps22_c eps33_c eps12_c eps13_c eps23_c eps11_s eps22_s eps33_s eps12_s eps13_s eps23_s ") f.write("sig11_c sig22_c sig33_c sig12_c sig13_c sig23_c sig11_s sig22_s sig33_s sig12_s sig13_s sig23_s\n") '''this is the part where we compute strain and stress''' for ubi in self.ubis: U, eps = ubi_to_u_and_eps(ubi, self.unitcell()) #U, eps = ubi_to_u_and_eps(ubi, ubi_to_cell(self.ubis[0])) epsM = [ [ eps[0], eps[1], eps[2] ], #write the strain tensor list as a matrix [ eps[1], eps[3], eps[4] ], [ eps[2], eps[4], eps[5] ] ] epsS = np.dot( U, np.dot( epsM, U.T ) ) #epsilon in sample co-ordinates sigM= np.empty((3,3)) sigS= np.empty((3,3)) try: sigM = strain2stress( np.array(epsM), self.MVStiffness() ) #write the stress tensor as a symmetric matrix in crystal co-ordinates sigS = np.dot( U, np.dot( sigM, U.T ) ) #sigma in sample co-ordinates except: print "couldn't compute stress! please check the crystal_symmetry parameters and elastic constants" writestress = False pass ''' writing down the results''' f.write(("%f "*6)%tuple(ubi_to_cell(ubi))) f.write(("%f "*9)%tuple(U.ravel())) ligne = "" for i,j in [(0,0),(1,1),(2,2),(0,1),(0,2),(1,2)]: ligne = ligne + " " + str(100.*np.array(epsM)[i,j]) for i,j in [(0,0),(1,1),(2,2),(0,1),(0,2),(1,2)]: ligne = ligne + " " + str(100.*epsS[i,j]) if writestress==True: for i,j in [(0,0),(1,1),(2,2),(0,1),(0,2),(1,2)]: ligne = ligne + " " + str(np.array(sigM)[i,j]) for i,j in [(0,0),(1,1),(2,2),(0,1),(0,2),(1,2)]: ligne = ligne + " " + str(sigS[i,j]) f.write(ligne[1:]) f.write("\n") f.close()
def run_pcr(self, cif_file, grain_mask, flt, gr, number_y_scans, ymin, ystep): no_voxels = sum(grain_mask[grain_mask == 1]) unit_cell = self.field_converter.extract_cell(self.params) UBI = gr.ubi.copy() cell = tools.ubi_to_cell(UBI) U, strain = tools.ubi_to_u_and_eps(UBI, unit_cell) euler = tools.u_to_euler(U) average = np.concatenate((strain, euler), axis=None) initial_guess = np.concatenate((average, average), axis=None) for i in range(no_voxels - 2): initial_guess = np.concatenate((initial_guess, average), axis=None) th_min, th_max = setup_grain.get_theta_bounds(flt, gr) param = setup_grain.setup_experiment(self.params, cif_file, no_voxels, ystep, th_min, th_max) measured_data = setup_grain.get_measured_data(gr, flt, number_y_scans, ymin, ystep, param) hkl = setup_grain.get_hkls(param, flt, gr) voxel_positions, C, constraint = setup_grain.get_positions( grain_mask, ystep) voxel_data = find_refl_func.find_refl_func(param, hkl, voxel_positions, measured_data, unit_cell, initial_guess, ymin, number_y_scans, C, constraint) bounds_low, bounds_high = setup_grain.get_bounds_strain(initial_guess) solution = voxel_data.steepest_descent(bounds_low, bounds_high, initial_guess) voxels_as_grain_objects = setup_grain.map_solution_to_voxels( solution, grain_mask, no_voxels, voxel_data, var_choice='strain') return voxels_as_grain_objects
def add_voxel_to_field(self, voxel, field_recons, row, col, params): cell = indexing.ubitocellpars(voxel.ubi) field_recons['a'][row, col] = cell[0] field_recons['b'][row, col] = cell[1] field_recons['c'][row, col] = cell[2] field_recons['alpha'][row, col] = cell[3] field_recons['beta'][row, col] = cell[4] field_recons['gamma'][row, col] = cell[5] rod = voxel.Rod field_recons['rod1'][row, col] = rod[0] field_recons['rod2'][row, col] = rod[1] field_recons['rod3'][row, col] = rod[2] ubi = np.ravel(voxel.ubi, order='C') field_recons['UBI11'][row, col] = ubi[0] field_recons['UBI12'][row, col] = ubi[1] field_recons['UBI13'][row, col] = ubi[2] field_recons['UBI21'][row, col] = ubi[3] field_recons['UBI22'][row, col] = ubi[4] field_recons['UBI23'][row, col] = ubi[5] field_recons['UBI31'][row, col] = ubi[6] field_recons['UBI32'][row, col] = ubi[7] field_recons['UBI33'][row, col] = ubi[8] U, eps_cry = tools.ubi_to_u_and_eps(voxel.ubi, self.extract_cell(params)) eps_cry_33 = np.array([[eps_cry[0], eps_cry[1], eps_cry[2]], [eps_cry[1], eps_cry[3], eps_cry[4]], [eps_cry[2], eps_cry[4], eps_cry[5]]]) sample_strain = np.dot(U, np.dot(eps_cry_33, np.transpose(U))) field_recons['E11'][row, col] = sample_strain[0, 0] field_recons['E12'][row, col] = sample_strain[0, 1] field_recons['E13'][row, col] = sample_strain[0, 2] field_recons['E22'][row, col] = sample_strain[1, 1] field_recons['E23'][row, col] = sample_strain[1, 2] field_recons['E33'][row, col] = sample_strain[2, 2]
eps33 = [] 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])
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