Page MenuHomec4science

test_fft.cpp
No OneTemporary

File Metadata

Created
Tue, Jul 30, 21:14

test_fft.cpp

/**
*
* @author Lucas Frérot <lucas.frerot@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2017 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/>.
*
*/
/* -------------------------------------------------------------------------- */
#include "test.hh"
#include "grid.hh"
#include "grid_hermitian.hh"
#include "fft_plan_manager.hh"
#include <array>
#include <algorithm>
#include <numeric>
#include <fftw3.h>
using namespace tamaas;
/* -------------------------------------------------------------------------- */
TestSuite(fft_interface, .init = tamaas_init, .fini = tamaas_teardown);
Test(fft_1d, fft_interface) {
constexpr UInt size = 20;
double data[size] = {0};
fftw_complex solution[size/2 + 1] = {{0}};
std::iota(std::begin(data), std::end(data), 0);
fftw_plan solution_plan = fftw_plan_dft_r2c_1d(size, data, solution, FFTW_ESTIMATE);
fftw_execute(solution_plan);
Grid<Real, 1> grid({size}, 1);
std::iota(grid.begin(), grid.end(), 0);
GridHermitian<Real, 1> result({size/2 + 1}, 1);
FFTPlanManager::get().createPlan(grid, result).forwardTransform();
#ifdef USE_CUDA
cudaDeviceSynchronize();
#endif
cr_assert(compare(result, solution, AreComplexEqual()), "1D FFTW transform failed");
fftw_destroy_plan(solution_plan);
FFTPlanManager::get().destroyPlan(grid, result);
}
Test(fft_2d, fft_interface) {
constexpr UInt size = 10;
double data[size * size] = {0};
fftw_complex solution[size * (size/2 + 1)] = {{0}};
std::iota(std::begin(data), std::end(data), 0);
fftw_plan solution_plan = fftw_plan_dft_r2c_2d(size, size, data, solution, FFTW_ESTIMATE);
fftw_execute(solution_plan);
Grid<Real, 2> grid({size, size}, 1);
std::iota(grid.begin(), grid.end(), 0);
GridHermitian<Real, 2> result({size, size/2 + 1}, 1);
FFTPlanManager::get().createPlan(grid, result).forwardTransform();
#ifdef USE_CUDA
cudaDeviceSynchronize();
#endif
cr_assert(compare(result, solution, AreComplexEqual()), "2D FFTW transform failed");
fftw_destroy_plan(solution_plan);
FFTPlanManager::get().destroyPlan(grid, result);
}

Event Timeline