def test_main(self):

        with h5py.File('Cartesian3d_random.hdf5', 'r') as data:

            shape = data['/shape'][...]

            i = np.eye(3)
            I = np.einsum('xy,ij', np.ones(shape), i)
            I4 = np.einsum('xy,ijkl->xyijkl', np.ones(shape),
                           np.einsum('il,jk', i, i))
            I4rt = np.einsum('xy,ijkl->xyijkl', np.ones(shape),
                             np.einsum('ik,jl', i, i))
            I4s = (I4 + I4rt) / 2.0

            mat = GMat.Matrix(shape[0], shape[1])

            I = data['/LinearHardening/I'][...]
            idx = data['/LinearHardening/idx'][...]
            K = data['/LinearHardening/K'][...]
            G = data['/LinearHardening/G'][...]
            tauy0 = data['/LinearHardening/tauy0'][...]
            H = data['/LinearHardening/H'][...]

            mat.setLinearHardening(I, idx, K, G, tauy0, H)

            I = data['/Elastic/I'][...]
            idx = data['/Elastic/idx'][...]
            K = data['/Elastic/K'][...]
            G = data['/Elastic/G'][...]

            mat.setElastic(I, idx, K, G)

            for i in range(20):

                mat.increment()

                F = data['/random/{0:d}/F'.format(i)][...]

                self.assertTrue(
                    np.allclose(mat.Stress(F),
                                data['/random/{0:d}/Stress'.format(i)][...],
                                1e-3))
                self.assertTrue(
                    np.allclose(
                        mat.Tangent(F)[1],
                        data['/random/{0:d}/Tangent'.format(i)][...], 1e-3))
                self.assertTrue(
                    np.allclose(mat.Epsp(),
                                data['/random/{0:d}/Epsp'.format(i)][...],
                                1e-3))
def StressStrain(mat, plastic):

    ninc = 301

    epseq = np.zeros(ninc)
    sigeq = np.zeros(ninc)

    for ilam, lam in enumerate(np.linspace(0.0, 1.0, ninc)):

        if plastic:
            mat.increment()

        F = np.array([
            [1.0 + lam, 0.0, 0.0],
            [0.0, 1.0 / (1.0 + lam), 0.0],
            [0.0, 0.0, 1.0],
        ])

        mat.setDefGrad(F)

        epseq[ilam] = gmat.Epseq(gmat.Strain(F))
        sigeq[ilam] = gmat.Sigeq(mat.Stress())

    return (epseq, sigeq)
    def test_Elastic(self):

        K = 12.3
        G = 45.6

        gamma = 0.02

        F = np.array([
                [1.0 + gamma, 0.0, 0.0],
                [0.0, 1.0 / (1.0 + gamma), 0.0],
                [0.0, 0.0, 1.0]])

        Sig = np.array([
            [G * 2.0 * np.log(1.0 + gamma), 0.0, 0.0],
            [0.0, - G * 2.0 * np.log(1.0 + gamma), 0.0],
            [0.0, 0.0, 0.0]])

        mat = GMat.Elastic(K, G)
        mat.setDefGrad(F)
        Sig = mat.Stress()

        self.assertTrue(np.allclose(mat.Stress(), Sig))
    def test_Array2d(self):

        K = 12.3
        G = 45.6

        gamma = 0.02

        F = np.array([
                [1.0 + gamma, 0.0, 0.0],
                [0.0, 1.0 / (1.0 + gamma), 0.0],
                [0.0, 0.0, 1.0]])

        Sig = np.array([
            [G * 2.0 * np.log(1.0 + gamma), 0.0, 0.0],
            [0.0, - G * 2.0 * np.log(1.0 + gamma), 0.0],
            [0.0, 0.0, 0.0]])

        nelem = 3
        nip = 2
        mat = GMat.Array2d([nelem, nip])
        ndim = 3

        I = np.ones([nelem, nip], dtype='int')
        mat.setElastic(I, K, G)

        f = np.zeros((nelem, nip, ndim, ndim))
        sig = np.zeros((nelem, nip, ndim, ndim))

        for e in range(nelem):
            for q in range(nip):
                f[e, q, :, :] = F
                sig[e, q, :, :] = Sig

        mat.setDefGrad(f)

        self.assertTrue(np.allclose(mat.Stress(), sig))
        Tau = Sig * np.linalg.det(F)

        P = A2_dot_B2(Tau, np.linalg.inv(F).T)

        dP = P - P0

        y[i] = norm(dP - (A4_ddot_B2(K, dF.T)).T) / norm(dP)

    return (x, y)


# Plot

fig, ax = plt.subplots()

ax.plot(*consistency(gmat.Elastic(10.0, 1.0), False), c='b', label=r'Elastic')

ax.plot(*consistency(gmat.LinearHardening(10.0, 1.0, 1.0, 1.0), True),
        c='r',
        label=r'LinearHardening')

ax.set_xscale('log')
ax.set_yscale('log')

ax.set_xlim([1e-16, 1e0])
ax.set_ylim([1e-16, 1e0])

ax.set_xlabel(r'$|| \delta \bm{\varepsilon} ||$')
ax.set_ylabel(r'$\eta$')

gplt.plot_powerlaw(
            [0.0, 0.0, 1.0],
        ])

        mat.setDefGrad(F)

        epseq[ilam] = gmat.Epseq(gmat.Strain(F))
        sigeq[ilam] = gmat.Sigeq(mat.Stress())

    return (epseq, sigeq)


# Plot

fig, ax = plt.subplots()

elas = gmat.Elastic(10.0, 1.0)
plas = gmat.LinearHardening(10.0, 1.0, 1.0, 1.0)

ax.plot(*StressStrain(elas, False), c='b', label=r'Elastic')
ax.plot(*StressStrain(plas, True), c='r', label=r'LinearHardening')

ax.plot(ax.get_xlim(), plas.tauy0() * np.ones(2), c='r', ls='--', lw=1.)
ax.plot(plas.tauy0() / (3.0 * plas.G()) * np.ones(2),
        ax.get_ylim(),
        c='r',
        ls='--',
        lw=1.)

ax.set_xlabel(r'$\varepsilon_\mathrm{eq}$')
ax.set_ylabel(r'$\sigma_\mathrm{eq}$')
import GMatElastoPlasticFiniteStrainSimo.Cartesian3d as GMat

with h5py.File('Cartesian3d_random.hdf5', 'w') as data:

    nelem = 1000
    nip = 4
    iden = 2.0 * np.random.random([nelem, nip])
    iden = np.where(iden < 1.0, 0.0, iden)
    iden = np.where(iden >= 1.0, 1.0, iden)
    iden = iden.astype(np.int)

    shape = np.array([nelem, nip], np.int)

    data['/shape'] = shape

    mat = GMat.Array2d(shape)

    I = np.where(iden == 0, 1, 0).astype(np.int)
    n = np.sum(I)
    idx = np.zeros(I.size, np.int)
    idx[np.argwhere(I.ravel() == 1).ravel()] = np.arange(n)
    idx = idx.reshape(I.shape)
    K = np.ones(n)
    G = np.ones(n)
    tauy0 = 0.1 * np.ones(n)
    H = np.ones(n)

    data['/LinearHardening/I'] = I
    data['/LinearHardening/idx'] = idx
    data['/LinearHardening/K'] = K
    data['/LinearHardening/G'] = G
phase[1, 0, 0] = 1.
# function to convert material parameters to grid of scalars
param  = lambda M0,M1: M0*np.ones([Nx,Ny,Nz])*(1.-phase)+\
                       M1*np.ones([Nx,Ny,Nz])*    phase
# material parameters
K = param(0.833, 0.833)  # bulk      modulus
mu = param(0.386, 0.386)  # shear     modulus
H = param(0.004, 0.008)  # hardening modulus
tauy0 = param(1000., 0.003)  # initial yield stress

# --------------------------------------- C++ IMPLEMENTATION ---------------------------------------

Isoft = (1. - phase).astype(np.uint).reshape(2, 1)
Ihard = (phase).astype(np.uint).reshape(2, 1)

mat = GMat.Array2d([2, 1])
mat.setElastic(Isoft, K[0, 0, 0], mu[0, 0, 0])
mat.setLinearHardening(Ihard, K[1, 0, 0], mu[1, 0, 0], tauy0[1, 0, 0], H[1, 0,
                                                                         0])

# -------------------------------------------- LOADING ---------------------------------------------

# stress, deformation gradient, plastic strain, elastic Finger tensor
# NB "_t" signifies that it concerns the value at the previous increment
ep_t = np.zeros([Nx, Ny, Nz])
P = np.zeros([3, 3, Nx, Ny, Nz])
F = np.array(I, copy=True)
F_t = np.array(I, copy=True)
be_t = np.array(I, copy=True)

# initialize macroscopic incremental loading