Exemplo n.º 1
0
def euler_to_gsh(el, H, ns, set_id, step, wrt_file):

    start = time.time()

    f = h5py.File("data.hdf5", 'a')
    dset_name = 'euler_%s%s_s%s' % (ns, set_id, step)
    euler = f.get(dset_name)[...]
    euler = euler.swapaxes(1, 2)

    euler_GSH = np.zeros([ns, H, el**3], dtype='complex128')

    for sn in range(ns):

        tmp = gsh.gsh_eval(euler[sn, :, :], np.arange(H))
        euler_GSH[sn, :, :] = tmp.T

    dset_name = 'euler_GSH_%s%s_s%s.npy' % (ns, set_id, step)
    f.create_dataset(dset_name, data=euler_GSH)

    f.close()

    end = time.time()
    timeE = np.round((end - start), 3)

    msg = "Conversion from Euler angles to GSH coefficients completed:"\
          " %s seconds" % timeE
    rr.WP(msg, wrt_file)
Exemplo n.º 2
0
def euler_to_gsh(el, H, ns, set_id, step, wrt_file):

    start = time.time()
    """get the euler angle files"""
    f = h5py.File("ref_%s%s_s%s.hdf5" % (ns, set_id, step), 'r')
    euler = f.get('euler')[...]
    f.close()
    """evaluate the gsh basis functions at the point of interest
    note that we do not keep redundant information due to symmetry in
    the complex arguments"""

    indxvec = gsh.gsh_basis_info()
    euler_GSH = np.zeros([ns, H, el**3], dtype='float64')

    for h in xrange(H):

        tmp = gsh.gsh_eval(euler.swapaxes(1, 2), [h])

        if indxvec[h, 1] >= 0:
            tmp = np.squeeze(tmp).real
        elif indxvec[h, 1] < 0:
            tmp = np.squeeze(tmp).imag

        euler_GSH[:, h, :] = tmp

    # euler_GSH = np.zeros([ns, H, el**3], dtype='complex128')
    # for sn in xrange(ns):
    #     tmp = gsh.gsh_eval(euler[sn, ...].swapaxes(0, 1), np.arange(15))
    #     euler_GSH[sn, :, :] = tmp.swapaxes(0, 1)

    end = time.time()
    timeE = np.round((end - start), 3)

    msg = "Conversion from Euler angles to GSH coefficients completed:" + \
          " %s seconds" % timeE
    rr.WP(msg, wrt_file)

    euler_GSH = euler_GSH.reshape([ns, H, el, el, el])

    # MICROSTRUCTURE FUNCTIONS IN FREQUENCY SPACE
    start = time.time()

    M = np.fft.fftn(euler_GSH, axes=[2, 3, 4])
    del euler_GSH

    size = M.nbytes

    f = h5py.File("ref_%s%s_s%s.hdf5" % (ns, set_id, step), 'a')
    f.create_dataset('M', data=M)
    f.close()

    end = time.time()
    timeE = np.round((end - start), 3)

    msg = "FFT3 conversion of micr to M_%s%s_s%s: %s seconds" % \
          (ns, set_id, step, timeE)
    rr.WP(msg, wrt_file)
    msg = 'Size of M_%s%s_s%s: %s bytes' % (ns, set_id, step, size)
    rr.WP(msg, wrt_file)
Exemplo n.º 3
0
def get_M(el, H, ns, set_id, step, wrt_file):

    start = time.time()
    """get the euler angle files"""
    f = h5py.File("spatial_stats.hdf5", 'a')
    euler = f.get('euler_%s' % set_id)[...]

    indxvec = gsh.gsh_basis_info()
    """evaluate the gsh basis functions at the point of interest
    note that we do not keep redundant information due to symmetry in
    the complex arguments"""

    # mf = np.zeros([ns, H, el**3], dtype='float64')
    # for h in xrange(H):
    #     tmp = gsh.gsh_eval(euler.swapaxes(1, 2), [h])
    #     if indxvec[h, 1] >= 0:
    #         tmp = np.squeeze(tmp).real
    #     elif indxvec[h, 1] < 0:
    #         tmp = np.squeeze(tmp).imag
    #     mf[:, h, :] = (2*indxvec[h, 0]+1)*tmp.conj()

    mf = np.zeros([ns, H, el**3], dtype='complex128')
    for h in xrange(H):
        tmp = gsh.gsh_eval(euler.swapaxes(1, 2), [h])
        tmp = np.squeeze(tmp)
        mf[:, h, :] = (2 * indxvec[h, 0] + 1) * tmp.conj()

    end = time.time()
    timeE = np.round((end - start), 3)

    msg = "Conversion from Euler angles to GSH coefficients completed:" + \
          " %s seconds" % timeE
    rr.WP(msg, wrt_file)

    mf = mf.reshape([ns, H, el, el, el])

    # MICROSTRUCTURE FUNCTIONS IN FREQUENCY SPACE
    start = time.time()

    M = np.fft.fftn(mf, axes=[2, 3, 4])
    del mf

    f.create_dataset('M_%s' % set_id, data=M)
    f.close()

    end = time.time()
    timeE = np.round((end - start), 3)

    msg = "FFT3 conversion of mf to M for %s: %s seconds" % \
          (set_id, timeE)
    rr.WP(msg, wrt_file)

    msg = 'Size of M: %s mb' % str(M.nbytes / (1e6))
    rr.WP(msg, wrt_file)
Exemplo n.º 4
0
def get_M(el, H, ns, set_id, step, wrt_file):

    start = time.time()
    """get the euler angle files"""
    f = h5py.File("spatial.hdf5", 'a')
    euler = f.get('euler_%s' % set_id)[...]

    indxvec = gsh.gsh_basis_info()

    mf = np.zeros([ns, H, el**3], dtype='complex128')
    for h in xrange(H):
        tmp = gsh.gsh_eval(euler.swapaxes(1, 2), [h])
        tmp = np.squeeze(tmp)
        mf[:, h, :] = (2 * indxvec[h, 0] + 1) * tmp.conj()

    end = time.time()
    timeE = np.round((end - start), 3)

    f.create_dataset('mf_%s' % set_id, data=mf)
    f.close()

    msg = "Conversion from Euler angles to GSH coefficients completed:" + \
          " %s seconds" % timeE
    rr.WP(msg, wrt_file)
Exemplo n.º 5
0
X[:, 0] = var_set[:, 1]  # phi1
X[:, 1] = var_set[:, 2]  # phi
X[:, 2] = var_set[:, 3]  # phi2

et_norm = var_set[:, 4]
Y = var_set[:, 5]

f.close()
"""Evaluate the parts of the basis function individually"""

st = time.time()

all_basis_p = np.zeros([N_pts, N_p], dtype='complex128')
for p in xrange(N_p):

    all_basis_p[:, p] = np.squeeze(gsh.gsh_eval(X, [p]))

all_basis_q = np.zeros([N_pts, N_q], dtype='complex128')
for q in xrange(N_q):

    all_basis_q[:, q] = np.cos(q * np.pi * theta / L_th)

all_basis_r = np.zeros([N_pts, N_r], dtype='complex128')
for r in xrange(N_r):

    all_basis_r[:, r] = np.cos(r * np.pi * (et_norm - a) / L_en)
"""Select the desired set of coefficients"""

cmax = N_p * N_q * N_r  # total number of permutations of basis functions

fn.WP(str(cmax), filename)
Exemplo n.º 6
0
coeff = np.zeros(N_L, dtype='complex128')

indxvec = gsh.gsh_basis_info()

fzsz = 3. / (2. * np.pi**2)
bsz = phi1max * phimax * phi2max / n_tot
print "esz: %s" % bsz
print "n_tot: %s" % n_tot
print "Y size: %s" % Y.size

for ii in xrange(N_L):
    if np.mod(ii, 10) == 0:
        print ii

    l = indxvec[ii, 0]
    X_ = gsh.gsh_eval(euler, [ii])[:, 0]
    tmp = (1. / (2. * l + 1.)) * np.sum(
        Y * X_.conj() * np.sin(euler[:, 1])) * bsz * fzsz
    coeff[ii] = tmp
""" plot a visual representation of the coefficients """

fig = plt.figure(num=1, figsize=[12, 8])
plt.bar(np.arange(N_L), coeff.real)
plt.title("coefficients from integration")
plt.grid(True)

plt.xticks(np.arange(0, N_L, 10), rotation='vertical')
# plt.yticks(np.arange(-12, 42, 2))
""" Check the regression accuracy """

Y_ = np.zeros(n_tot, dtype='complex128')
Exemplo n.º 7
0
phi1 = phi1.reshape(phi1.size)
phi = phi.reshape(phi.size)
phi2 = phi2.reshape(phi2.size)

euler = np.zeros((phi1.shape[0], 3), dtype='float64')
euler[:, 0] = phi1
euler[:, 1] = phi
euler[:, 2] = phi2

print euler.shape
""" Calculate X """

st = time.time()

X = gsh.gsh_eval(euler, np.arange(N_L))

print "basis evaluation complete: %ss" % np.round(time.time() - st, 3)
""" Generate Y """

bvec = [0, 1, 5, 8, 10, 12]
bval = [5., 3., 3., 1., 2., 1.]

Y = np.zeros(phi1.shape, dtype='complex128')

for ii in xrange(len(bvec)):
    Y += bval[ii] * X[:, bvec[ii]]

print np.mean(Y.real)
print np.mean(Y.imag)
print np.std(Y.imag)
Exemplo n.º 8
0
X[:, 0] = var_set[:, 1]  # phi1
X[:, 1] = var_set[:, 2]  # phi
X[:, 2] = var_set[:, 3]  # phi2

et_norm = var_set[:, 4]

f.close

f = h5py.File('X_parts.hdf5', 'a')
""" first evalute the GSH basis functions """

st = time.time()

for p in xrange(N_p):

    vec = gsh.gsh_eval(X, [p])

    set_id = 'p_%s' % p
    f.create_dataset(set_id, data=vec)
    fn.WP(set_id, filename)

msg = "GSH basis evaluation complete: %ss" % np.round(time.time() - st, 3)
fn.WP(msg, filename)
""" second evalute the cosine basis functions for theta"""

st = time.time()

for q in xrange(N_q):

    vec = np.cos(q * np.pi * theta / L_th)
cmax = N_p * N_q
cmat = np.unravel_index(np.arange(cmax), [N_p, N_q])
cmat = np.array(cmat).T
""" Generate Y """

bvec = [0, 15, 25, 60, 85, 115]
bval = [40., 20., -10., 6., -4., 2.]

Y = np.zeros(n_tot, dtype='complex128')

for ii in xrange(len(bvec)):

    p, q = cmat[bvec[ii], :]

    basis_p = gsh.gsh_eval(angles[:, 1:4], [p])
    basis_q = np.cos(q * np.pi * angles[:, 0] / L_th)

    Y += bval[ii] * np.squeeze(basis_p) * basis_q
""" Perform the integration """

coeff = np.zeros(cmax, dtype='complex128')

indxvec = gsh.gsh_basis_info()

# constants for integration
bsz_gsh = ((np.pi**3) / 3) / n_eul
bsz_cos = L_th / n_th

for ii in xrange(cmax):
    if np.mod(ii, 10) == 0:
Exemplo n.º 10
0
print emat.shape
""" calculate XhX """

lmax = 50

XhX = np.zeros((lmax, lmax), dtype='complex128')

indxvec = gsh.gsh_basis_info()

for ii in xrange(lmax):
    print ii
    for jj in xrange(lmax):

        l = indxvec[ii, 0]

        xii = gsh.gsh_eval(emat, [ii])[:, 0]
        xjj = gsh.gsh_eval(emat, [jj])[:, 0]

        XhX[ii,
            jj] = (2 * l + 1) * np.sum(xii.conj() * xjj * np.sin(emat[:, 1]))
    # XhX[ii, jj] = np.dot(xii.conj(), xjj)
""" plot the XhX matrix """

plt.figure(1)

plt.subplot(121)

ax = plt.imshow(np.real(XhX), origin='lower', interpolation='none', cmap='jet')
plt.title("real(XhX): GSH")
plt.colorbar(ax)
    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)

"""Calculate the GSH coefficients for each symmetric zone"""

st = time.time()

X = gsh.gsh_eval(euler_sym, np.arange(1, N_L))

print "basis evaluation complete: %ss" % np.round(time.time()-st, 3)
print "size of X: %sgb" % str(X.nbytes/(1E9))
print "X shape: %s" % str(X.shape)

"""Error check the GSH coefficients in terms of symmetry"""

error = np.zeros((n_sym-1, n_tot, N_L - 1))

for sym in xrange(n_sym-1):
    error[sym, ...] = np.abs(np.real(X[0, ...]) - np.real(X[sym, ...]))

print "coefficient magnitude closest to zero: %s" % str(np.abs(X).min())

"""Plot histogram for coefficient magnitude"""
    bval.append(val)

bvec = np.array(bvec)
bval = np.array(bval)

indxsort = np.argsort(bvec)

bvec = bvec[indxsort]
bval = bval[indxsort]

print bvec
print bval

Y = np.zeros(euler.shape[0], dtype='complex128')
for ii in xrange(len(bvec)):
    Xtmp = np.squeeze(gsh_old.gsh_eval(euler, [bvec[ii]]))
    Y += bval[ii]*Xtmp

Y = Y.real

""" Perform the regression """

coef = np.zeros(N_L_new, dtype='complex128')

indxvec = gsh_new.gsh_basis_info()

# domain_eul_sz is the integration domain in radians
domain_sz = phi1max*phimax*phi2max*(np.pi/180.)**3
# full_eul_sz is the size of euler space in radians
full_sz = (2*np.pi)*(np.pi)*(2*np.pi)
eul_frac = domain_sz/full_sz
# euler, n_tot = euler_grid_center(inc, phi1max, phimax, phi2max)

inc = 5.
euler, n_tot = euler_grid(inc, phi1max, phimax, phi2max)
""" Generate Y """

bvec = [0, 10, 20, 60, 140, 290, 320, 460]
bval = [40., 20., -10., 6., -4., 1., 2., -1.]

# bvec = [0, 1, 5, 8, 10, 12]
# bval = [5., 3., 3., 1., 2., 1.]

Y = np.zeros(n_tot, dtype='complex128')

for ii in xrange(len(bvec)):
    Y += bval[ii] * gsh.gsh_eval(euler, [bvec[ii]])[:, 0]
""" Perform the regression """

coeff = np.zeros(N_L, dtype='complex128')

indxvec = gsh.gsh_basis_info()

fzsz = 3. / (2. * np.pi**2)
bsz = phi1max * phimax * phi2max / n_tot
print "esz: %s" % bsz
print "n_tot: %s" % n_tot
print "Y size: %s" % Y.size

for ii in xrange(N_L):
    if np.mod(ii, 10) == 0:
        print ii
Exemplo n.º 14
0
def eval_func(theta, X, et_norm):
    """
    Summary: performs evaluation of calibrated function
    Inputs:
        theta ([el**3], float): vector of deformation modes for each SVE
        X ([el**3, 3], float): array of euler angles for each SVE
        et_norm ([el**3], float): vector of norm(et_dev) for each SVE
    Outputs:
        Y ([el**3], float): vector of values calculated by function
    """

    thr = 0.00001  # threshold on coefs w/rt maximum magnitude coef
    # thr = 0.0  # threshold on coefs w/rt maximum magnitude coef

    # LL_p = 16  # LL_p: gsh truncation level

    a = 0.00485  # start for en range
    b = 0.00905  # end for en range

    # N_p: number of GSH bases to evaluate
    # indxvec = gsh.gsh_basis_info()
    # N_p = np.sum(indxvec[:, 0] <= LL_p)

    N_p = 500

    N_q = 40  # number of cosine bases to evaluate for theta
    N_r = 14  # number of cosine bases to evaluate for en

    L_th = np.pi / 3.
    L_en = b - a

    # filename = 'log_final_results9261.txt'

    f = h5py.File('coeff_total.hdf5', 'r')
    coeff = f.get('coeff')[...]
    f.close()

    N_pts = theta.size
    """Select the desired set of coefficients"""

    cmax = N_p * N_q * N_r  # total number of permutations of basis functions

    # fn.WP(str(cmax), filename)

    cmat = np.unravel_index(np.arange(cmax), [N_p, N_q, N_r])
    cmat = np.array(cmat).T

    cuttoff = thr * np.abs(coeff).max()
    indxvec = np.arange(cmax)[np.abs(coeff) > cuttoff]

    # N_coef = indxvec.size
    # pct_coef = 100.*N_coef/cmax
    # fn.WP("number of coefficients retained: %s" % N_coef, filename)
    # fn.WP("percentage of coefficients retained %s%%"
    #       % np.round(pct_coef, 4), filename)
    """Evaluate the parts of the basis function individually"""

    p_U = np.unique(cmat[indxvec, 0])
    q_U = np.unique(cmat[indxvec, 1])
    r_U = np.unique(cmat[indxvec, 2])

    # fn.WP("number of p basis functions used: %s" % p_U.size, filename)
    # fn.WP("number of q basis functions used: %s" % q_U.size, filename)
    # fn.WP("number of r basis functions used: %s" % r_U.size, filename)

    all_basis_p = np.zeros([N_pts, N_p], dtype='complex128')
    for p in p_U:
        all_basis_p[:, p] = np.squeeze(gsh.gsh_eval(X, [p]))

    all_basis_q = np.zeros([N_pts, N_q], dtype='complex128')
    for q in q_U:
        all_basis_q[:, q] = np.cos(q * np.pi * theta / L_th)

    all_basis_r = np.zeros([N_pts, N_r], dtype='complex128')
    for r in r_U:
        all_basis_r[:, r] = np.cos(r * np.pi * (et_norm - a) / L_en)
    """Perform the prediction"""

    Y_ = np.zeros(theta.size, dtype='complex128')

    for ii in indxvec:

        p, q, r = cmat[ii, :]
        basis_p = all_basis_p[:, p]
        basis_q = all_basis_q[:, q]
        basis_r = all_basis_r[:, r]

        ep_set = basis_p * basis_q * basis_r

        Y_ += coeff[ii] * ep_set

    return Y_
Exemplo n.º 15
0
X = data[ang_sel, 1:4]
Y = data[ang_sel, 5]

"""perform the integration and prediction"""

indxvec = gsh.gsh_basis_info()

bsz_eul = ((np.pi**3)/3)/n_eul

Y_ = np.zeros(Y.shape, dtype='complex128')

coeff = np.zeros(N_p, dtype='complex128')

for p in xrange(N_p):

    ep_set = np.squeeze(gsh.gsh_eval(X, [p]))

    l = indxvec[p, 0]
    c_eul = (1./(2.*l+1.))*(3./(2.*np.pi**2))

    c_tot = c_eul*bsz_eul

    tmp = c_tot*np.sum(Y*ep_set.conj()*np.sin(X[:, 1]))
    coeff[p] = tmp

    Y_ += tmp*ep_set

print "\nmin(Y): %s" % np.min(Y)
print "mean(Y): %s" % np.mean(Y)
print "max(Y): %s" % np.max(Y)