Page MenuHomec4science

test_fftransform.py
No OneTemporary

File Metadata

Created
Tue, May 28, 18:06

test_fftransform.py

#!/usr/bin/env python
# coding: utf-8
# -----------------------------------------------------------------------------
# @author Lucas Frérot <lucas.frerot@epfl.ch>
#
# @section LICENSE
#
# Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
# Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
# Solides)
#
# Tamaas is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------
from __future__ import print_function
import sys
import tamaas as tm
import numpy as np
from numpy.linalg import norm
def main():
tm.initialize()
size = 128
surface = np.random.randn(128, 128)
# Compute fourier transform through numpy
numpy_spectral = np.fft.rfft2(surface)
# Compute fourier tranform through tamaas
tamaas_spectral = np.zeros((size, size/2+1), np.complex)
transform = tm.FFTransformFFTWReal(surface, tamaas_spectral)
transform.forwardTransform()
spectral = tamaas_spectral
error_npy = np.abs(np.sum((spectral-numpy_spectral)**2))
surface_copy = surface.copy()
surface[:, :] = 0
transform.backwardTransform()
error_back = np.abs(np.sum((surface_copy-surface)**2))
print(error_back)
print(error_npy)
if error_back > 1e-16 or error_npy > 1e-16:
return 1
# Testing multi-component FFT
multi = np.zeros((size, size, 3))
hermit = np.zeros((size, size/2+1, 3), np.complex)
transform = tm.FFTransformFFTWReal(multi, hermit)
norms = []
# Checking no overflow in other components of spectral representation
multi.fill(0.)
multi[:, :, 0] = surface[:, :]
transform.forwardTransform()
norms.append(norm(hermit[:, :, (1, 2)]))
multi.fill(0.)
multi[:, :, 1] = surface[:, :]
transform.forwardTransform()
norms.append(norm(hermit[:, :, (0, 2)]))
multi.fill(0.)
multi[:, :, 2] = surface[:, :]
transform.forwardTransform()
norms.append(norm(hermit[:, :, (0, 1)]))
for i in range(3):
if norms[i] != 0:
print("Error in multi-component data separation")
return 1
multi[:, :, i] = surface[:, :]
# Multi is full of surface
transform.forwardTransform()
for i in range(3):
error = norm(hermit[:, :, i] - numpy_spectral) / norm(numpy_spectral)
if error > 1e-10:
print("Error in multi-component spectral data : {} => {}".format(i, error))
return 1
tm.finalize()
return 0
if __name__ == '__main__':
sys.exit(main())

Event Timeline