def get_pred(sn, el, ns, set_id, step, compl): """read the file for euler angle, total strain and plastic strain fields""" f = h5py.File("ref_%s%s_s%s.hdf5" % (ns, set_id, step), 'r') print f.get('euler').shape euler = f.get('euler')[sn, ...] euler = euler.swapaxes(0, 1) print euler.shape et = np.zeros((el**3, 6)) ep = np.zeros((el**3, 6)) for ii in xrange(6): comp = compl[ii] tmp = f.get('r%s_epsilon_t' % comp)[sn, ...] et[:, ii] = tmp.reshape(el**3) tmp = f.get('r%s_epsilon_p' % comp)[sn, ...] ep[:, ii] = tmp.reshape(el**3) f.close() epn_max = np.argmax(tensnorm(ep)) """find the deviatoric strain tensor""" print np.all(np.isclose(np.sum(et[:, 0:3]), np.zeros(el**3))) et_ = np.zeros(et.shape) et_[:, 0:3] = et[:, 0:3] - (1. / 3.) * np.expand_dims(np.sum(et[:, 0:3], 1), 1) et_[:, 3:] = et[:, 3:] print et_[epn_max, :] print np.all(np.isclose(np.sum(et_[:, 0:3]), np.zeros(el**3))) """find the norm of the tensors""" en = tensnorm(et_) print "sn: %s" % sn print "min(en): %s" % en.min() print "max(en): %s" % en.max() epn = tensnorm(ep) orig = epn """normalize the deviatoric strain tensor""" et_n = et_ / np.expand_dims(en, 1) print np.all(np.isclose(tensnorm(et_n), np.ones(el**3))) """write the normalized deviatioric total strain and plastic strains in matrix form""" et_m = np.zeros((el**3, 3, 3)) et_m[:, 0, 0] = et_n[:, 0] et_m[:, 1, 1] = et_n[:, 1] et_m[:, 2, 2] = et_n[:, 2] et_m[:, 0, 1] = et_n[:, 3] et_m[:, 1, 0] = et_n[:, 3] et_m[:, 0, 2] = et_n[:, 4] et_m[:, 2, 0] = et_n[:, 4] et_m[:, 1, 2] = et_n[:, 5] et_m[:, 2, 1] = et_n[:, 5] print epn[epn_max] print euler[epn_max, ...] print et[epn_max, ...] print et_[epn_max, ...] print et_n[epn_max, ...] """find the eigenvalues of the normalized tensor""" eigval, g_p2s = LA.eigh(et_m) del et_m print eigval[:5, :] """find the deformation mode""" theta = np.arccos(-np.sqrt(3. / 2.) * eigval[:, 0]) print "min(theta): %s" % np.str(theta.min() * 180. / np.pi) print "mean(theta): %s" % np.str(theta.mean() * 180. / np.pi) print "max(theta): %s" % np.str(theta.max() * 180. / np.pi) """find g_p2c = g_p2s*g_s2c""" g_s2c = ef.bunge2g(euler[:, 0], euler[:, 1], euler[:, 2]) """this application of einsum is validated vs loop with np.dot()""" g_p2c = np.einsum('...ij,...jk', g_s2c, g_p2s) phi1, phi, phi2 = ef.g2bunge(g_p2c) # X = np.vstack([phi1, phi, phi2]).T # X = np.array(ef.g2bunge(g_p2s.swapaxes(1, 2))).T # X = np.array(ef.g2bunge(g_p2s)).T # X = np.array(ef.g2bunge(g_s2c.swapaxes(1, 2))).T # X = np.array(ef.g2bunge(g_s2c)).T # X = np.array(ef.g2bunge(g_p2c.swapaxes(1, 2))).T X = np.array(ef.g2bunge(g_s2c)).T del phi1, phi, phi2 pred = rr.eval_func(theta, X, en).real print "min(orig): %s" % orig.min() print "min(pred): %s" % pred.min() print "max(orig): %s" % orig.max() print "max(pred): %s" % pred.max() return orig, pred
n_sym = symop.shape[0] print "number of symmetry operators: %s" % n_sym euler_sym = np.zeros((n_sym, n_tot, 3)) g_orig = ef.bunge2g(euler[:, 0], euler[:, 1], euler[:, 2]) # find the symmetric equivalents to the euler angle within the FZ for sym in xrange(n_sym): op = symop[sym, ...] # g_sym: array of orientation matrices transformed with a # symmetry operator g_sym = np.einsum('ik,...kj', op, g_orig) tmp = np.array(ef.g2bunge(g_sym)).transpose() if sym == 0: print "g_sym shape: %s" % str(g_sym.shape) print "tmp shape: %s" % str(tmp.shape) del g_sym euler_sym[sym, ...] = tmp del tmp # make sure all of the euler angles within the appropriate # ranges (eg. not negative) print "initial: euler angles less than zero: %s" % np.sum(euler_sym < 0) lt = euler_sym < 0.0 euler_sym += 2 * np.pi * lt print "final: euler angles less than zero: %s" % np.sum(euler_sym < 0)
def fip(sn, el, ns, set_id, step, typ, compl): f = h5py.File("ref_%s%s_s%s.hdf5" % (ns, set_id, step), 'r') euler = f.get('euler')[sn, ...].reshape(3, el**3) euler = euler.swapaxes(0, 1) et = np.zeros((el**3, 6)) for ii in xrange(6): comp = compl[ii] tmp = f.get('r%s_%s' % (comp, typ))[sn, ...] et[:, ii] = tmp.reshape(el**3) f.close() """ find the deviatoric strain tensor """ et_ = et et_[:, 0:3] = et_[:, 0:3] - (1./3.)*np.expand_dims(np.sum(et[:, 0:3], 1), 1) # print np.all(np.isclose(np.sum(et[:, 0:3]), np.zeros(el**3))) """ find the norm of the deviatoric strain tensor """ en = np.sqrt(np.sum(et_[:, 0:3]**2+2*et_[:, 3:]**2, 1)) print "sn: %s" % sn print "min(en): %s" % en.min() print "max(en): %s" % en.max() """ normalize the deviatoric strain tensor """ et_n = et_/np.expand_dims(en, 1) # print np.all(np.isclose(np.sqrt(np.sum(et_n[:, 0:3]**2+2*et_n[:, 3:]**2, 1)), np.ones(el**3))) et_m = np.zeros((el**3, 3, 3)) et_m[:, 0, 0] = et_n[:, 0] et_m[:, 1, 1] = et_n[:, 1] et_m[:, 2, 2] = et_n[:, 2] et_m[:, 0, 1] = et_n[:, 3] et_m[:, 1, 0] = et_n[:, 3] et_m[:, 0, 2] = et_n[:, 4] et_m[:, 2, 0] = et_n[:, 4] et_m[:, 1, 2] = et_n[:, 5] et_m[:, 2, 1] = et_n[:, 5] """ find the eigenvalues of the normalized tensor""" eigval, g_s2p = LA.eigh(et_m) # print eigval[:5, :] # """ sort the principal strains in descending order """ # indx = np.argsort(eigval) # indx = indx[::-1] # eigval = eigval[indx] """ find the deformation mode """ # theta1 = np.arccos(np.sqrt(3./2.)*eigval[:, 2])+(np.pi/3.) # theta2 = np.arccos(np.sqrt(3./2.)*eigval[:, 1])-(np.pi/3.) # theta3 = np.arccos(-np.sqrt(3./2.)*eigval[:, 0]) # print theta1[:10]*(180./np.pi) # print theta2[:10]*(180./np.pi) # print theta3[:10]*(180./np.pi) theta = np.arccos(-np.sqrt(3./2.)*eigval[:, 0]) print "min(theta): %s" % theta.min() print "max(theta): %s" % theta.max() """ find g_p2c = g_p2s*g_s2c """ g_s2c = ef.bunge2g(euler[:, 0], euler[:, 1], euler[:, 2]) g_p2s = g_s2p.swapaxes(1, 2) del g_s2p g_p2c = np.einsum('...ik,...kj', g_p2s, g_s2c) del g_s2c, g_p2s phi1, phi, phi2 = ef.g2bunge(g_p2c) X = np.vstack([phi1, phi, phi2]).T del phi1, phi, phi2 fip = rr.eval_func(theta, X, en).real print "min(FIP): %s" % fip.min() print "max(FIP): %s" % fip.max() return fip
def get_pred(sn, el, ns, set_id, step, compl): """read the file for euler angle, total strain and plastic strain fields""" rcell = np.random.randint(0, el**3) f = h5py.File("ref_%s%s_s%s.hdf5" % (ns, set_id, step), 'r') print f.get('euler').shape euler = f.get('euler')[sn, ...] euler = euler.swapaxes(0, 1) print euler.shape """check to make sure that the Euler manipulations are functioning""" # ef.check_euler_op(euler) """load et and ep""" et = np.zeros((el**3, 6)) ep = np.zeros((el**3, 6)) for ii in xrange(6): comp = compl[ii] tmp = f.get('r%s_epsilon_t' % comp)[sn, ...] et[:, ii] = tmp.reshape(el**3) tmp = f.get('r%s_epsilon_p' % comp)[sn, ...] ep[:, ii] = tmp.reshape(el**3) f.close() """write et and ep in matrix form""" et = tens2mat(et) ep = tens2mat(ep) print "\net for cell #%s:" % rcell print et[rcell, ...] """find the deviatoric strain tensor""" isdev = np.all(np.abs(thydro(et)) < 1e-5) print "\nis hydro(et) == 0?: %s" % isdev et_ = np.zeros(et.shape) Id = np.zeros(et.shape) Id[:, 0, 0] = 1 Id[:, 1, 1] = 1 Id[:, 2, 2] = 1 et_ = et - thydro(et)[:, None, None]*Id isdev = np.all(np.abs(thydro(et_)) < 1e-5) print "is hydro(et_) == 0?: %s\n" % isdev print "\net_ for cell #%s:" % rcell print et_[rcell, ...] """find the norm of the tensors""" en = tensnorm(et_) print "\nen for cell #%s: %s" % (rcell, en[rcell]) print "min(en): %s" % en.min() print "max(en): %s" % en.max() """normalize the deviatoric strain tensor""" et_n = et_/en[:, None, None] print "\net_n for cell #%s:" % rcell print et_n[rcell, ...] isnorm = np.all(np.isclose(tensnorm(et_n), np.ones(el**3))) print "is norm(et_n) == 0?: %s\n" % isnorm """find the eigenvalues of the normalized tensor""" eigval_, g_p2s_ = LA.eigh(et_n) print "eigval_ (before sort) for cell #%s: %s" % (rcell, str(eigval_[rcell, :])) print "g_p2s_ (before sort) for cell #%s:" % rcell print g_p2s_[rcell, ...] """sort the eigenvalues/vectors by highest to lowest magnitude eigenvalue""" esort = np.argsort(np.abs(eigval_))[:, ::-1] eigval = np.zeros(eigval_.shape) g_p2s = np.zeros(g_p2s_.shape) for ii in xrange(el**3): eigval[ii, :] = eigval_[ii, esort[ii, :]] for jj in xrange(3): g_p2s[ii, jj, :] = g_p2s_[ii, jj, esort[ii, :]] print "\neigval (after sort) for cell #%s: %s" % (rcell, str(eigval[rcell, :])) print "g_p2s (after sort) for cell #%s:\n" % rcell print g_p2s[rcell, ...] """show that the matrix of eigenvectors is g_p2s""" print "use the backwards tensor transformation to " +\ "get et_n_prinicipal using g_p2s for cell# %s\n" % rcell +\ "(et_n_principal = g_p2s^t * et_n * g_p2s)" tmp = np.dot(np.dot(g_p2s[rcell, ...].T, et_n[rcell, ...]), g_p2s[rcell, ...]) print np.round(tmp, 6) """find the deformation mode""" theta = np.arctan2(-2*eigval[:, 0]-eigval[:, 2], np.sqrt(3)*eigval[:, 2]) theta += np.pi*(theta < 0) print "\nmin(theta): %s" % np.str(theta.min()*180./np.pi) print "mean(theta): %s" % np.str(theta.mean()*180./np.pi) print "max(theta): %s\n" % np.str(theta.max()*180./np.pi) """recover the principal values from the deformation mode""" print "principal values of et_n recovered from theta for cell #%s:" % rcell print theta2et_P(theta[rcell]) """find g_p2c = g_p2s*g_s2c""" g_s2c = ef.bunge2g(euler) """this application of einsum is validated vs loop with np.dot()""" g_p2c = np.einsum('...ij,...jk', g_s2c, g_p2s) tmp = ef.g2bunge(g_p2c) euler_p2c = np.zeros((el**3, 3)) for ii in xrange(el**3): euler_p2c[ii, :] = return2fz(tmp[ii, :]) """try to reconstruct et_""" g_p2c_ = ef.bunge2g(euler_p2c) print "euler angles for cell#%s: %s" % (rcell, str(euler_p2c[rcell, ...])) geq = np.all(np.isclose(g_p2c[rcell, ...], g_p2c_[rcell, ...])) print "are g_p2c and g_p2c_ equal for cell #%s?: %s" % (rcell, geq) is_equiv = is_equiv_g(g_p2c[rcell, ...], g_p2c_[rcell, ...]) print "are g_p2c and g_p2c_ equivalent for cell #%s?: %s\n" % (rcell, is_equiv) et_n_P = theta2et_P(theta) et_n_C = np.einsum('...ij,...jk,...lk', g_p2c_, et_n_P, g_p2c_) g_c2s = g_s2c.swapaxes(1, 2) et_n_S = np.einsum('...ij,...jk,...lk', g_c2s, et_n_C, g_c2s) et_recon = et_n_S * en[:, None, None] print "et_ for cell #%s:" % rcell print et_[rcell, ...] print "et_ reconstructed from theta, euler_p2c and en same cell:" print et_recon[rcell, ...] # X = np.vstack([phi1, phi, phi2]).T # X = np.array(ef.g2bunge(g_p2c)).T # X = np.array(ef.g2bunge(g_p2c.swapaxes(1, 2))).T # X = np.array(ef.g2bunge(g_p2s)).T # X = np.array(ef.g2bunge(g_p2s.swapaxes(1, 2))).T # X = np.array(ef.g2bunge(g_s2c)).T # X = np.array(ef.g2bunge(g_s2c.swapaxes(1, 2))).T orig = tensnorm(ep) pred = rr.eval_func(theta, euler_p2c, en).real print "\nmin(orig): %s" % orig.min() print "min(pred): %s" % pred.min() print "max(orig): %s" % orig.max() print "max(pred): %s" % pred.max() return orig, pred
randloc = np.array([ 2 * np.pi * np.random.rand(), 0.5 * np.pi * np.random.rand(), (1. / 3.) * np.pi * np.random.rand() ]) randloc = randloc[:, np.newaxis] g = ef.bunge2g(randloc[0], randloc[1], randloc[2]) tmp = np.zeros([12, 3]) for ii in xrange(12): g_sym = np.dot(symhex[ii, :, :], g[0, ...]) g_sym = g_sym[np.newaxis, ...] tmp[ii, :] = ef.g2bunge(g_sym) euler = np.zeros([12, 3]) euler[:, 0] = tmp[:, 0] ltz = euler[:, 0] < 0 euler[:, 0] = euler[:, 0] + 2 * np.pi * ltz euler[:, 1] = tmp[:, 1] ltz = euler[:, 1] < 0 euler[:, 1] = euler[:, 1] + 2 * np.pi * ltz euler[:, 2] = tmp[:, 2] ltz = euler[:, 2] < 0 euler[:, 2] = euler[:, 2] + 2 * np.pi * ltz
def get_pred(sn, el, ns, set_id, step, compl): """read the file for euler angle, total strain and plastic strain fields""" f = h5py.File("ref_%s%s_s%s.hdf5" % (ns, set_id, step), 'r') print f.get('euler').shape euler = f.get('euler')[sn, ...] euler = euler.swapaxes(0, 1) print euler.shape et = np.zeros((el**3, 6)) ep = np.zeros((el**3, 6)) for ii in xrange(6): comp = compl[ii] tmp = f.get('r%s_epsilon_t' % comp)[sn, ...] et[:, ii] = tmp.reshape(el**3) tmp = f.get('r%s_epsilon_p' % comp)[sn, ...] ep[:, ii] = tmp.reshape(el**3) f.close() """find the deviatoric strain tensor""" isdev = np.all(np.isclose(np.sum(et[:, 0:3]), np.zeros(el**3))) print "is trace(et) == 0?: %s" % isdev et_ = np.zeros(et.shape) et_[:, 0:3] = et[:, 0:3] - (1. / 3.) * np.expand_dims(np.sum(et[:, 0:3], 1), 1) et_[:, 3:] = et[:, 3:] isdev = np.all(np.isclose(np.sum(et_[:, 0:3]), np.zeros(el**3))) print "is trace(et_) == 0?: %s" % isdev """find the norm of the tensors""" en = tensnorm(et_) print "sn: %s" % sn print "min(en): %s" % en.min() print "max(en): %s" % en.max() """normalize the deviatoric strain tensor""" et_n = et_ / np.expand_dims(en, 1) isnorm = np.all(np.isclose(tensnorm(et_n), np.ones(el**3))) print "is norm(et_n) == 0?: %s" % isnorm """write the normalized deviatioric total strain and plastic strains in matrix form""" et_m = tens2mat(et_n) epn = tensnorm(ep) # epn_max = np.argmax(epn) orig = epn # print "max(norm(ep)): %s" % epn[epn_max] # print "euler @ max(norm(ep)): %s" % str(euler[epn_max, ...]) # print et[epn_max, ...] # print et_[epn_max, ...] # print et_n[epn_max, ...] """find the eigenvalues of the normalized tensor""" eigval_, g_p2s_ = LA.eigh(et_m) del et_m print "eigval_ example (before sort): %s" % str(eigval_[0, :]) print "g_p2s_ example (before sort):" print g_p2s_[0, ...] """sort the eigenvalues/vectors by highest to lowest magnitude eigenvalue""" esort = np.argsort(np.abs(eigval_))[:, ::-1] eigval = np.zeros(eigval_.shape) g_p2s = np.zeros(g_p2s_.shape) for ii in xrange(el**3): eigval[ii, :] = eigval_[ii, esort[ii, :]] for jj in xrange(3): g_p2s[ii, jj, :] = g_p2s_[ii, jj, esort[ii, :]] print "eigval example (after sort): %s" % str(eigval[0, :]) print "g_p2s example (after sort):" print g_p2s[0, ...] """find the deformation mode""" theta = np.arctan2(-2 * eigval[:, 0] - eigval[:, 2], np.sqrt(3) * eigval[:, 2]) theta += np.pi * (theta < 0) print "min(theta): %s" % np.str(theta.min() * 180. / np.pi) print "mean(theta): %s" % np.str(theta.mean() * 180. / np.pi) print "max(theta): %s" % np.str(theta.max() * 180. / np.pi) """find g_p2c = g_p2s*g_s2c""" g_s2c = ef.bunge2g(euler[:, 0], euler[:, 1], euler[:, 2]) """this application of einsum is validated vs loop with np.dot()""" g_p2c = np.einsum('...ij,...jk', g_s2c, g_p2s) phi1, phi, phi2 = ef.g2bunge(g_p2c) X = np.vstack([phi1, phi, phi2]).T # X = np.array(ef.g2bunge(g_p2c)).T # X = np.array(ef.g2bunge(g_p2c.swapaxes(1, 2))).T # X = np.array(ef.g2bunge(g_p2s)).T # X = np.array(ef.g2bunge(g_p2s.swapaxes(1, 2))).T # X = np.array(ef.g2bunge(g_s2c)).T # X = np.array(ef.g2bunge(g_s2c.swapaxes(1, 2))).T del phi1, phi, phi2 pred = rr.eval_func(theta, X, en).real print "min(orig): %s" % orig.min() print "min(pred): %s" % pred.min() print "max(orig): %s" % orig.max() print "max(pred): %s" % pred.max() return orig, pred