Example #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
Example #2
0
def div(X):
    if X.shape==(1,):
        shape=()
    else:
        shape=X.shape[:-1]
    assert(X.shape[-1]==X.dim)
    assert(X.order==1)

    dX=Tensor(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)
        FX=F(X)

    dim=len(X.N)
    freq=Grid.get_freq(X.N, X.Y, fft_form=FX.fft_form)
    strfreq='xyz'
    coef=2*np.pi*1j

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

    if not X.Fourier:
        iF=DFT(N=X.N, inverse=True, fft_form=dX.fft_form)
        dX=iF(dX)
    dX.name='div({0})'.format(X.name[:10])
    return dX
Example #3
0
def div(X):
    if X.shape == (1, ):
        shape = ()
    else:
        shape = X.shape[:-1]
    assert (X.shape[-1] == X.dim)
    assert (X.order == 1)

    dX = Tensor(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)
        FX = F(X)

    dim = len(X.N)
    freq = Grid.get_freq(X.N, X.Y, fft_form=FX.fft_form)
    strfreq = 'xyz'
    coef = 2 * np.pi * 1j

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

    if not X.Fourier:
        iF = DFT(N=X.N, inverse=True, fft_form=dX.fft_form)
        dX = iF(dX)
    dX.name = 'div({0})'.format(X.name[:10])
    return dX
Example #4
0
    def shrink(self):  # shrink spectrum to size N+1 from 2N+1
        if not self.Fourier:
            raise ('Tensor is in Physical space')

        #self.dct()

        smaller = Tensor(name='gradient',
                         N=np.array(np.divide(np.add(self.N, 1), 2),
                                    dtype=np.int),
                         shape=self.shape,
                         multype='grad')
        for d in np.ndindex(self.shape):  #range(self.dim):
            smaller.val[d] = decrease_spectrum(
                self.val[d],
                tuple(np.array(np.divide(np.add(self.N, 1), 2), dtype=np.int)))
        # smaller.val[d] =

#       weights = lagrange_weights(self.N)
#       for index in np.ndindex(self.shape):
#          self.val[index] = np.einsum('...,...->...', self.val[index], weights)

        self.val = copy.deepcopy(smaller.val)

        self.N = tuple(np.array(np.divide(np.add(self.N, 1), 2), dtype=np.int))
        #self.idct()

        return
Example #5
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
Example #6
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)
Example #7
0
def matrix2tensor(M):
    return Tensor(name=M.name,
                  val=M.val,
                  order=2,
                  multype=21,
                  Fourier=M.Fourier,
                  fft_form=fft_form_default)
Example #8
0
    def grad_ortho(self):

        if not self.Fourier:
            print('Tensor {} is not in Fourier space'.format(self.name))
            print('Tensor {} is transformed due to gradient'.format(self.name))
            self.dct_ortho()

        grad = Tensor(name='gradient',
                      N=self.N,
                      shape=[self.dim],
                      multype='grad')

        for d in range(self.dim):
            grad.val[d] = grad_ortho_(copy.deepcopy(self.val), d)

        self.val = copy.deepcopy(grad.val)
        self.shape = (self.dim, )
        return
Example #9
0
    def matvec(self, x):
        """
        Provides the __call__ for operand recast into one-dimensional vector.
        This is suitable for e.g. iterative solvers when trigonometric
        polynomials are recast into one-dimensional numpy.arrays.

        Parameters
        ----------
        x : one-dimensional numpy.array
        """
        X = Tensor(val=self.revec(x), order=self.X_order, N=self.X_N)
        AX = self.__call__(X)
        return AX.vec()
Example #10
0
def grad_tensor(N, Y=None, fft_form=fft_form_default):
    if Y is None:
        Y = np.ones_like(N)
    # scalar valued versions of gradient and divergence
    N = np.array(N, dtype=np.int)
    dim = N.size

    freq = Grid.get_xil(N, Y, fft_form=fft_form)
    N_fft=tuple(freq[i].size for i in range(dim))
    hGrad = np.zeros((dim,)+ N_fft) # zero initialize
    for ind in itertools.product(*[list(range(n)) for n in N_fft]):
        for i in range(dim):
            hGrad[i][ind] = freq[i][ind[i]]
    hGrad = hGrad*2*np.pi*1j
    return Tensor(name='hgrad', val=hGrad, order=1, N=N, multype='grad',
                  Fourier=True, fft_form=fft_form)
Example #11
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)
Example #12
0
def vector2tensor(V):
    return Tensor(name=V.name, val=V.val, order=1, Fourier=V.Fourier)