Esempio n. 1
0
def grad(X):
    if X.shape==(1,):
        shape=(X.dim,)
    else:
        shape=X.shape+(X.dim,)
    name='grad({0})'.format(X.name[:10])
    gX=Tensor(name=name, shape=shape, N=X.N,
              Fourier=True, fft_form=X.fft_form)
    if X.Fourier:
        FX=X
    else:
        F=DFT(N=X.N, fft_form=X.fft_form) # TODO:change to X.fourier()
        FX=F(X)

    dim=len(X.N)
    freq=Grid.get_freq(X.N, X.Y, fft_form=X.fft_form)
    strfreq='xyz'
    coef=2*np.pi*1j
    val=np.empty((X.dim,)+X.shape+X.N_fft, dtype=np.complex)

    for ii in range(X.dim):
        mul_str='{0},...{1}->...{1}'.format(strfreq[ii], strfreq[:dim])
        val[ii]=np.einsum(mul_str, coef*freq[ii], FX.val, dtype=np.complex)

    if X.shape==(1,):
        gX.val=np.squeeze(val)
    else:
        gX.val=np.moveaxis(val, 0, X.order)

    if not X.Fourier:
        iF=DFT(N=X.N, inverse=True, fft_form=gX.fft_form)
        gX=iF(gX)
    gX.name='grad({0})'.format(X.name[:10])
    return gX
Esempio n. 2
0
def grad(X):
    if X.shape==(1,):
        shape=(X.dim,)
    else:
        shape=X.shape+(X.dim,)
    name='grad({0})'.format(X.name[:10])
    gX=Tensor(name=name, shape=shape, N=X.N,
              Fourier=True, fft_form=X.fft_form)
    if X.Fourier:
        FX=X
    else:
        F=DFT(N=X.N, fft_form=X.fft_form) # TODO:change to X.fourier()
        FX=F(X)

    dim=len(X.N)
    freq=Grid.get_freq(X.N, X.Y, fft_form=X.fft_form)
    strfreq='xyz'
    coef=2*np.pi*1j
    val=np.empty((X.dim,)+X.shape+X.N_fft, dtype=np.complex)

    for ii in range(X.dim):
        mul_str='{0},...{1}->...{1}'.format(strfreq[ii], strfreq[:dim])
        val[ii]=np.einsum(mul_str, coef*freq[ii], FX.val, dtype=np.complex)

    if X.shape==(1,):
        gX.val=np.squeeze(val)
    else:
        gX.val=np.moveaxis(val, 0, X.order)

    if not X.Fourier:
        iF=DFT(N=X.N, inverse=True, fft_form=gX.fft_form)
        gX=iF(gX)
    gX.name='grad({0})'.format(X.name[:10])
    return gX
Esempio n. 3
0
def potential(X, small_strain=False):
    if X.Fourier:
        FX = X
    else:
        F = DFT(N=X.N, fft_form=X.fft_form)
        FX = F(X)

    freq = Grid.get_freq(X.N, X.Y, fft_form=FX.fft_form)
    if X.order == 1:
        assert (X.dim == X.shape[0])
        iX = Tensor(name='potential({0})'.format(X.name[:10]),
                    shape=(1, ),
                    N=X.N,
                    Fourier=True,
                    fft_form=FX.fft_form)
        iX.val[0] = potential_scalar(FX.val,
                                     freq=freq,
                                     mean_index=FX.mean_index())

    elif X.order == 2:
        assert (X.dim == X.shape[0])
        assert (X.dim == X.shape[1])
        iX = Tensor(name='potential({0})'.format(X.name[:10]),
                    shape=(X.dim, ),
                    N=X.N,
                    Fourier=True,
                    fft_form=FX.fft_form)
        if not small_strain:
            for ii in range(X.dim):
                iX.val[ii] = potential_scalar(FX.val[ii],
                                              freq=freq,
                                              mean_index=FX.mean_index())

        else:
            assert ((X - X.transpose()).norm() < 1e-14)  # symmetricity
            omeg = FX.zeros_like()  # non-symmetric part of the gradient
            gomeg = Tensor(name='potential({0})'.format(X.name[:10]),
                           shape=FX.shape + (X.dim, ),
                           N=X.N,
                           Fourier=True)
            grad_ep = grad(FX)  # gradient of strain
            gomeg.val = np.einsum('ikj...->ijk...', grad_ep.val) - np.einsum(
                'jki...->ijk...', grad_ep.val)
            for ij in itertools.product(list(range(X.dim)), repeat=2):
                omeg.val[ij] = potential_scalar(gomeg.val[ij],
                                                freq=freq,
                                                mean_index=FX.mean_index())

            gradu = FX + omeg
            iX = potential(gradu, small_strain=False)

    if X.Fourier:
        return iX
    else:
        iF = DFT(N=X.N, inverse=True, fft_form=FX.fft_form)
        return iF(iX)
Esempio n. 4
0
def potential(X, small_strain=False):
    if X.Fourier:
        FX=X
    else:
        F=DFT(N=X.N, fft_form=X.fft_form)
        FX=F(X)

    freq=Grid.get_freq(X.N, X.Y, fft_form=FX.fft_form)
    if X.order==1:
        assert(X.dim==X.shape[0])
        iX=Tensor(name='potential({0})'.format(X.name[:10]), shape=(1,), N=X.N,
                  Fourier=True, fft_form=FX.fft_form)
        iX.val[0]=potential_scalar(FX.val, freq=freq, mean_index=FX.mean_index())

    elif X.order==2:
        assert(X.dim==X.shape[0])
        assert(X.dim==X.shape[1])
        iX=Tensor(name='potential({0})'.format(X.name[:10]), shape=(X.dim,), N=X.N,
                  Fourier=True, fft_form=FX.fft_form)
        if not small_strain:
            for ii in range(X.dim):
                iX.val[ii]=potential_scalar(FX.val[ii], freq=freq, mean_index=FX.mean_index())

        else:
            assert((X-X.transpose()).norm()<1e-14) # symmetricity
            omeg=FX.zeros_like() # non-symmetric part of the gradient
            gomeg=Tensor(name='potential({0})'.format(X.name[:10]),
                           shape=FX.shape+(X.dim,), N=X.N, Fourier=True)
            grad_ep=grad(FX) # gradient of strain
            gomeg.val=np.einsum('ikj...->ijk...', grad_ep.val)-np.einsum('jki...->ijk...', grad_ep.val)
            for ij in itertools.product(range(X.dim), repeat=2):
                omeg.val[ij]=potential_scalar(gomeg.val[ij], freq=freq, mean_index=FX.mean_index())

            gradu=FX+omeg
            iX=potential(gradu, small_strain=False)

    if X.Fourier:
        return iX
    else:
        iF=DFT(N=X.N, inverse=True, fft_form=FX.fft_form)
        return iF(iX)