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
print princ_vals et_n_P = np.zeros((3, 3)) et_n_P[0, 0] = princ_vals[0] et_n_P[1, 1] = princ_vals[1] et_n_P[2, 2] = princ_vals[2] et_n_C = np.dot(np.dot(g_p2c, et_n_P), g_p2c.T) g_c2s = g_s2c.T et_n_S = np.dot(np.dot(g_c2s, et_n_C), g_c2s.T) et_dev_ = et_n_S * en print "et_dev reconstructed from theta, euler_p2c and en" print et_dev_ X = np.vstack([phi1, phi, phi2]).T # X = euler epn_SPECTRAL = rr.eval_func(theta, X, en).real print "CPFEM plastic strain magnitude: %s" % epn_CPFEM print "predicted plastic strain magnitude: %s" % epn_SPECTRAL[0] """check database predction with single point data: tensor components are as follows: 11, 22, 33, 12, 13, 23 the array structure is detailed here: rawdata[:, 0] = time rawdata[:, 1:7] = stress tensor rawdata[:, 7:13] = total strain tensor rawdata[:, 13:19] = plastic strain tensor""" rawdata = np.loadtxt("single_pt.txt") et_P_vec = rawdata[:, 7:10] ep_C_vec = rawdata[:, 13:19]
for ii in xrange(el**3): if np.mod(ii, 100) == 0: print ii CPFEM, theta, X, en, eigval = get_pred(el, etv[ii, :], epv[ii, :], euler[ii, :]) CPFEM_m[ii] = CPFEM theta_m[ii] = theta X_m[ii, :] = X en_m[ii] = en eigval_m[ii, :] = eigval """write file for matthew""" # tempm = np.hstack([eigval_m*en_m[:, None], X_m]) # np.savetxt('et_file.txt', tempm) SPECTRAL_m = rr.eval_func(theta_m, X_m, en_m).real SPCP_m = get_sp_answer(el) SPECTRAL_m = SPECTRAL_m.reshape(el, el, el) SPCP_m = SPCP_m.reshape(el, el, el) CPFEM_m = CPFEM_m.reshape(el, el, el) maxindx = np.unravel_index(np.argmax(np.abs(CPFEM_m)), CPFEM_m.shape) slc = maxindx[0] field_std(el, ns, SPECTRAL_m, SPCP_m, CPFEM_m, slc, "$|\epsilon^{p}|$", 1) plt.show()
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
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