2012-10-06 18:49:06 +02:00
|
|
|
#ifndef __COSMOTOOL_FOURIER_EUCLIDIAN_HPP
|
|
|
|
#define __COSMOTOOL_FOURIER_EUCLIDIAN_HPP
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
#include <boost/function.hpp>
|
2012-10-06 18:49:06 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2012-11-10 15:02:08 +01:00
|
|
|
#include <gsl/gsl_randist.h>
|
2012-10-06 18:49:06 +02:00
|
|
|
#include "base_types.hpp"
|
|
|
|
#include "fft/fftw_calls.hpp"
|
2012-11-10 15:02:08 +01:00
|
|
|
#include "../algo.hpp"
|
2012-10-06 18:49:06 +02:00
|
|
|
|
|
|
|
namespace CosmoTool
|
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
template<typename T>
|
|
|
|
class EuclidianSpectrum_1D: public SpectrumFunction<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef boost::function1<T, T> Function;
|
|
|
|
protected:
|
|
|
|
Function f;
|
|
|
|
public:
|
|
|
|
typedef typename SpectrumFunction<T>::FourierMapType FourierMapType;
|
|
|
|
typedef boost::shared_ptr<FourierMapType> ptr_map;
|
|
|
|
|
|
|
|
EuclidianSpectrum_1D(Function P)
|
|
|
|
: f(P)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr_map newRandomFourier(gsl_rng *rng, const FourierMapType& like_map) const;
|
|
|
|
|
|
|
|
void mul(FourierMap<std::complex<T> >& m) const;
|
|
|
|
};
|
2012-10-06 18:49:06 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2012-11-10 15:02:08 +01:00
|
|
|
class EuclidianFourierMapBase: public FourierMap<T>
|
2012-10-06 18:49:06 +02:00
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
public:
|
|
|
|
typedef std::vector<int> DimArray;
|
2012-10-06 18:49:06 +02:00
|
|
|
private:
|
|
|
|
boost::shared_ptr<T> m_data;
|
2012-11-10 15:02:08 +01:00
|
|
|
DimArray m_dims;
|
2012-10-06 18:49:06 +02:00
|
|
|
long m_size;
|
|
|
|
public:
|
2012-11-10 15:02:08 +01:00
|
|
|
|
|
|
|
EuclidianFourierMapBase(boost::shared_ptr<T> indata, const DimArray& indims)
|
2012-10-06 18:49:06 +02:00
|
|
|
{
|
|
|
|
m_data = indata;
|
|
|
|
m_dims = indims;
|
|
|
|
m_size = 1;
|
|
|
|
for (int i = 0; i < m_dims.size(); i++)
|
|
|
|
m_size *= m_dims[i];
|
|
|
|
}
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
virtual ~EuclidianFourierMapBase()
|
2012-10-06 18:49:06 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
const DimArray& getDims() const { return m_dims; }
|
|
|
|
|
2012-10-06 18:49:06 +02:00
|
|
|
virtual const T *data() const { return m_data.get(); }
|
|
|
|
virtual T *data() { return m_data.get(); }
|
|
|
|
virtual long size() const { return m_size; }
|
|
|
|
|
|
|
|
virtual FourierMap<T> *copy() const
|
|
|
|
{
|
|
|
|
FourierMap<T> *m = this->mimick();
|
|
|
|
m->eigen() = this->eigen();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class EuclidianFourierMapReal: public EuclidianFourierMapBase<T>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename EuclidianFourierMapBase<T>::DimArray DimArray;
|
|
|
|
|
|
|
|
EuclidianFourierMapReal(boost::shared_ptr<T> indata, const DimArray& indims)
|
|
|
|
: EuclidianFourierMapBase<T>(indata, indims)
|
|
|
|
{}
|
|
|
|
|
2012-10-06 18:49:06 +02:00
|
|
|
virtual FourierMap<T> *mimick() const
|
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
return new EuclidianFourierMapReal<T>(
|
|
|
|
boost::shared_ptr<T>((T *)fftw_malloc(sizeof(T)*this->size()),
|
2012-10-06 18:49:06 +02:00
|
|
|
std::ptr_fun(fftw_free)),
|
2012-11-10 15:02:08 +01:00
|
|
|
this->getDims());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual T dot_product(const FourierMap<T>& other) const
|
|
|
|
throw(std::bad_cast)
|
|
|
|
{
|
|
|
|
const EuclidianFourierMapReal<T>& m2 = dynamic_cast<const EuclidianFourierMapReal<T>&>(other);
|
|
|
|
if (this->size() != m2.size())
|
|
|
|
throw std::bad_cast();
|
|
|
|
|
|
|
|
return (this->eigen()*m2.eigen()).sum();
|
2012-10-06 18:49:06 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
template<typename T>
|
|
|
|
class EuclidianFourierMapComplex: public EuclidianFourierMapBase<std::complex<T> >
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
typedef boost::shared_ptr<std::complex<T> > ptr_t;
|
|
|
|
std::vector<double> delta_k;
|
|
|
|
long plane_size;
|
|
|
|
public:
|
|
|
|
typedef typename EuclidianFourierMapBase<std::complex<T> >::DimArray DimArray;
|
|
|
|
|
|
|
|
EuclidianFourierMapComplex(ptr_t indata,
|
|
|
|
const DimArray& indims,
|
|
|
|
const std::vector<double>& dk)
|
|
|
|
: EuclidianFourierMapBase<std::complex<T> >(indata, indims), delta_k(dk)
|
|
|
|
{
|
|
|
|
assert(dk.size() == indims.size());
|
|
|
|
plane_size = 1;
|
|
|
|
for (int q = 1; q < indims.size(); q++)
|
|
|
|
plane_size *= indims[q];
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual FourierMap<std::complex<T> > *mimick() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
new EuclidianFourierMapComplex<T>(
|
|
|
|
ptr_t((std::complex<T> *)
|
|
|
|
fftw_malloc(sizeof(std::complex<T>)*this->size()),
|
|
|
|
std::ptr_fun(fftw_free)),
|
|
|
|
this->getDims(),
|
|
|
|
this->delta_k);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Array>
|
|
|
|
double get_K(const Array& ik)
|
|
|
|
{
|
|
|
|
const DimArray& dims = this->getDims();
|
|
|
|
assert(ik.size() == dims.size());
|
|
|
|
double k2 = 0;
|
|
|
|
for (int q = 0; q < ik.size(); q++)
|
|
|
|
{
|
|
|
|
int dk = ik;
|
|
|
|
|
|
|
|
if (dk > dims[q]/2)
|
|
|
|
dk = dk - dims[q];
|
|
|
|
|
|
|
|
k2 += CosmoTool::square(delta_k[q]*dk);
|
|
|
|
}
|
|
|
|
return std::sqrt(k2);
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_K(long p)
|
|
|
|
{
|
|
|
|
const DimArray& dims = this->getDims();
|
|
|
|
DimArray d(delta_k.size());
|
|
|
|
for (int q = 0; q < d.size(); q++)
|
|
|
|
{
|
|
|
|
d[q] = p%dims[q];
|
|
|
|
p = (p-d[q])/dims[q];
|
|
|
|
}
|
|
|
|
return get_K(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::complex<T> dot_product(const FourierMap<std::complex<T> >& other) const
|
|
|
|
throw(std::bad_cast)
|
|
|
|
{
|
|
|
|
const EuclidianFourierMapComplex<T>& m2 = dynamic_cast<const EuclidianFourierMapComplex<T>&>(other);
|
|
|
|
if (this->size() != m2.size())
|
|
|
|
throw std::bad_cast();
|
|
|
|
|
|
|
|
const std::complex<T> *d1 = this->data();
|
|
|
|
const std::complex<T> *d2 = m2.data();
|
|
|
|
const DimArray& dims = this->getDims();
|
|
|
|
int N0 = dims[0];
|
|
|
|
std::complex<T> result = 0;
|
|
|
|
|
|
|
|
for (long q0 = 1; q0 < N0-1; q0++)
|
|
|
|
{
|
|
|
|
for (long p = 0; p < plane_size; p++)
|
|
|
|
{
|
|
|
|
result += 2*(conj(d1[q0+N0*p]) * d2[q0+N0*p]).real();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (long p = 0; p < plane_size; p++)
|
|
|
|
{
|
|
|
|
long q0 = N0*p, q1 = (p+1)*N0-1;
|
|
|
|
result += conj(d1[q0]) * d2[q0];
|
|
|
|
result += conj(d1[q1]) * d2[q1];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2012-10-06 18:49:06 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class EuclidianFourierTransform: public FourierTransform<T>
|
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
public:
|
|
|
|
typedef typename EuclidianFourierMapBase<T>::DimArray DimArray;
|
2012-10-06 18:49:06 +02:00
|
|
|
private:
|
|
|
|
typedef FFTW_Calls<T> calls;
|
2012-11-10 15:02:08 +01:00
|
|
|
EuclidianFourierMapReal<T> *realMap;
|
|
|
|
EuclidianFourierMapComplex<T> *fourierMap;
|
2012-10-06 18:49:06 +02:00
|
|
|
typename calls::plan_type m_analysis, m_synthesis;
|
|
|
|
double volume;
|
|
|
|
long N;
|
2012-11-10 15:02:08 +01:00
|
|
|
DimArray m_dims;
|
2012-10-06 18:49:06 +02:00
|
|
|
std::vector<double> m_L;
|
|
|
|
public:
|
2012-11-10 15:02:08 +01:00
|
|
|
EuclidianFourierTransform(const DimArray& dims, const std::vector<double>& L)
|
2012-10-06 18:49:06 +02:00
|
|
|
{
|
|
|
|
assert(L.size() == dims.size());
|
2012-11-10 15:02:08 +01:00
|
|
|
std::vector<double> dk(L.size());
|
|
|
|
|
2012-10-06 18:49:06 +02:00
|
|
|
m_dims = dims;
|
|
|
|
m_L = L;
|
|
|
|
|
|
|
|
N = 1;
|
|
|
|
volume = 1;
|
|
|
|
for (int i = 0; i < dims.size(); i++)
|
|
|
|
{
|
|
|
|
N *= dims[i];
|
|
|
|
volume *= L[i];
|
2012-11-10 15:02:08 +01:00
|
|
|
dk[i] = 2*M_PI/L[i];
|
2012-10-06 18:49:06 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
realMap = new EuclidianFourierMapReal<T>(
|
2012-10-06 18:49:06 +02:00
|
|
|
boost::shared_ptr<T>(calls::alloc_real(N),
|
|
|
|
std::ptr_fun(calls::free)),
|
|
|
|
dims);
|
2012-11-10 15:02:08 +01:00
|
|
|
fourierMap = new EuclidianFourierMapComplex<T>(
|
2012-10-06 18:49:06 +02:00
|
|
|
boost::shared_ptr<std::complex<T> >((std::complex<T>*)calls::alloc_complex(N),
|
|
|
|
std::ptr_fun(calls::free)),
|
2012-11-10 15:02:08 +01:00
|
|
|
dims, dk);
|
2012-10-06 18:49:06 +02:00
|
|
|
m_analysis = calls::plan_dft_r2c(dims.size(), &dims[0],
|
|
|
|
realMap->data(), (typename calls::complex_type *)fourierMap->data(),
|
|
|
|
FFTW_MEASURE);
|
|
|
|
m_synthesis = calls::plan_dft_c2r(dims.size(), &dims[0],
|
|
|
|
(typename calls::complex_type *)fourierMap->data(), realMap->data(),
|
|
|
|
FFTW_MEASURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~EuclidianFourierTransform()
|
|
|
|
{
|
|
|
|
delete realMap;
|
|
|
|
delete fourierMap;
|
|
|
|
calls::destroy_plan(m_synthesis);
|
|
|
|
calls::destroy_plan(m_analysis);
|
|
|
|
}
|
|
|
|
|
|
|
|
void synthesis()
|
|
|
|
{
|
|
|
|
calls::execute(m_synthesis);
|
|
|
|
realMap->scale(1/volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void analysis()
|
|
|
|
{
|
|
|
|
calls::execute(m_analysis);
|
|
|
|
fourierMap->scale(volume/N);
|
|
|
|
}
|
|
|
|
|
|
|
|
void synthesis_conjugate()
|
|
|
|
{
|
|
|
|
calls::execute(m_analysis);
|
|
|
|
fourierMap->scale(1/volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void analysis_conjugate()
|
|
|
|
{
|
|
|
|
calls::execute(m_synthesis);
|
|
|
|
realMap->scale(volume/N);
|
|
|
|
}
|
|
|
|
|
|
|
|
const FourierMap<std::complex<T> >& fourierSpace() const
|
|
|
|
{
|
|
|
|
return *fourierMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
FourierMap<std::complex<T> >& fourierSpace()
|
|
|
|
{
|
|
|
|
return *fourierMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
const FourierMap<T>& realSpace() const
|
|
|
|
{
|
|
|
|
return *realMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
FourierMap<T>& realSpace()
|
|
|
|
{
|
|
|
|
return *realMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
FourierTransform<T> *mimick() const
|
|
|
|
{
|
|
|
|
return new EuclidianFourierTransform(m_dims, m_L);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class EuclidianFourierTransform_2d: public EuclidianFourierTransform<T>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<typename T2>
|
|
|
|
static std::vector<T2> make_2d_vector(T2 a, T2 b)
|
|
|
|
{
|
|
|
|
T2 arr[2] = { a, b};
|
|
|
|
return std::vector<T2>(&arr[0], &arr[2]);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
EuclidianFourierTransform_2d(int Nx, int Ny, double Lx, double Ly)
|
|
|
|
: EuclidianFourierTransform<T>(make_2d_vector<int>(Nx, Ny), make_2d_vector<double>(Lx, Ly))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~EuclidianFourierTransform_2d() {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class EuclidianFourierTransform_3d: public EuclidianFourierTransform<T>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
template<typename T2>
|
|
|
|
static std::vector<T2> make_3d_vector(T2 a, T2 b, T2 c)
|
|
|
|
{
|
|
|
|
T2 arr[2] = { a, b, c};
|
|
|
|
return std::vector<T2>(&arr[0], &arr[3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
EuclidianFourierTransform_3d(int Nx, int Ny, int Nz, double Lx, double Ly, double Lz)
|
|
|
|
: EuclidianFourierTransform<T>(make_3d_vector<int>(Nx, Ny, Nz), make_3d_vector<double>(Lx, Ly, Lz))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~EuclidianFourierTransform_3d() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
template<typename T>
|
|
|
|
typename EuclidianSpectrum_1D<T>::ptr_map
|
|
|
|
EuclidianSpectrum_1D<T>::newRandomFourier(gsl_rng *rng, const FourierMapType& like_map) const
|
|
|
|
{
|
|
|
|
typedef EuclidianFourierMapComplex<T> MapT;
|
|
|
|
typedef typename MapT::DimArray DimArray;
|
|
|
|
|
|
|
|
MapT& m_c = dynamic_cast<MapT&>(like_map);
|
|
|
|
MapT *rand_map = m_c.mimick();
|
|
|
|
std::complex<T> *d = rand_map->data();
|
|
|
|
long idx;
|
|
|
|
const DimArray& dims = rand_map->getDims();
|
|
|
|
long plane_size;
|
|
|
|
|
|
|
|
for (long p = 1; p < m_c.size(); p++)
|
|
|
|
{
|
|
|
|
double A_k = std::sqrt(0.5*f(rand_map->get_K(p)));
|
|
|
|
d[p] = std::complex<T>(gsl_ran_gaussian(rng, A_k),
|
|
|
|
gsl_ran_gaussian(rng, A_k));
|
|
|
|
}
|
|
|
|
// Generate the mean value
|
|
|
|
d[0] = std::complex<T>(std::sqrt(f(0)), 0);
|
|
|
|
// Correct the Nyquist plane
|
|
|
|
idx = dims[0]-1; // Stick to the last element of the first dimension
|
|
|
|
// 1D is special case
|
|
|
|
if (dims.size() == 1)
|
|
|
|
{
|
|
|
|
d[idx] = std::complex<T>(d[idx].real() + d[idx].imag(), 0);
|
|
|
|
return boost::shared_ptr<EuclidianSpectrum_1D<T>::FourierMapType>(rand_map);
|
|
|
|
}
|
|
|
|
|
|
|
|
plane_size = 1;
|
|
|
|
for (int q = 1; q < dims.size(); q++)
|
|
|
|
{
|
|
|
|
plane_size *= dims[q];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (long p = 1; p < plane_size/2; p++)
|
|
|
|
{
|
|
|
|
long q = (p+1)*dims[0]-1;
|
|
|
|
long q2 = (plane_size-p+1)*dims[0]-1;
|
|
|
|
assert(q < plane_size*dims[0]);
|
|
|
|
assert(q2 < plane_size*dims[0]);
|
|
|
|
d[q] = conj(d[q2]);
|
|
|
|
}
|
|
|
|
long q = dims[0];
|
|
|
|
d[q] = std::complex<T>(d[q].real() + d[q].imag());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void EuclidianSpectrum_1D<T>::mul(FourierMap<std::complex<T> >& m) const
|
|
|
|
{
|
|
|
|
EuclidianFourierMapComplex<T>& m_c = dynamic_cast<EuclidianFourierMapComplex<T>&>(m);
|
|
|
|
std::complex<T> *d = m.data();
|
2012-10-06 18:49:06 +02:00
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
for (long p = 0; p < m_c.size(); p++)
|
|
|
|
d[p] *= f(m.get_K(p));
|
|
|
|
}
|
2012-10-06 18:49:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|