2012-10-07 20:58:01 +02:00
|
|
|
#ifndef __COSMOTOOL_FOURIER_HEALPIX_HPP
|
|
|
|
#define __COSMOTOOL_FOURIER_HEALPIX_HPP
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
#include <gsl/gsl_rng.h>
|
|
|
|
#include <gsl/gsl_randist.h>
|
|
|
|
#include <cmath>
|
2012-11-10 18:22:37 +01:00
|
|
|
#include <vector>
|
2012-10-07 20:58:01 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2012-11-10 15:02:08 +01:00
|
|
|
#include <exception>
|
2012-11-10 18:22:37 +01:00
|
|
|
#include "base_types.hpp"
|
|
|
|
#include <sharp_lowlevel.h>
|
|
|
|
#include <sharp_geomhelpers.h>
|
|
|
|
#include <sharp_almhelpers.h>
|
2012-10-07 20:58:01 +02:00
|
|
|
|
|
|
|
namespace CosmoTool
|
|
|
|
{
|
|
|
|
template<typename T>
|
2012-11-10 18:22:37 +01:00
|
|
|
class HealpixSpectrum: public SpectrumFunction<T>
|
2012-11-10 15:02:08 +01:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
std::vector<T> cls;
|
|
|
|
public:
|
2012-11-10 18:22:37 +01:00
|
|
|
typedef typename SpectrumFunction<T>::FourierMapType FourierMapType;
|
|
|
|
typedef boost::shared_ptr<FourierMapType> ptr_map;
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
|
|
|
|
HealpixSpectrum(long Lmax)
|
|
|
|
: cls(Lmax+1) {}
|
|
|
|
|
|
|
|
T *data() { return &cls[0]; }
|
|
|
|
const T *data() const { return &cls[0]; }
|
|
|
|
long size() const { return cls.size(); }
|
|
|
|
|
2012-11-11 21:46:03 +01:00
|
|
|
void newRandomFourier(gsl_rng *rng, FourierMapType& like_map) const;
|
2012-11-10 23:35:27 +01:00
|
|
|
|
|
|
|
void mul(FourierMapType& m) const;
|
|
|
|
void mul_sqrt(FourierMapType& m) const;
|
|
|
|
void mul_inv(FourierMapType& m) const;
|
|
|
|
void mul_inv_sqrt(FourierMapType& m) const;
|
|
|
|
};
|
2012-11-10 15:02:08 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class HealpixFourierMap: public FourierMap<T>
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
T *m_data;
|
2012-11-10 18:22:37 +01:00
|
|
|
long Npix, m_Nside;
|
2012-10-07 20:58:01 +02:00
|
|
|
Eigen::aligned_allocator<T> alloc;
|
|
|
|
public:
|
|
|
|
HealpixFourierMap(long nSide)
|
2012-11-10 18:22:37 +01:00
|
|
|
: Npix(12*nSide*nSide), m_Nside(nSide)
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
|
|
|
m_data = alloc.allocate(Npix);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~HealpixFourierMap()
|
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
alloc.deallocate(m_data, Npix);
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
long Nside() const { return m_Nside; }
|
2012-10-07 20:58:01 +02:00
|
|
|
virtual const T* data() const { return m_data; }
|
|
|
|
virtual T *data() { return m_data; }
|
2012-11-10 15:02:08 +01:00
|
|
|
virtual long size() const { return Npix; }
|
|
|
|
|
|
|
|
virtual T dot_product(const FourierMap<T>& other) const
|
|
|
|
throw(std::bad_cast)
|
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
typedef typename FourierMap<T>::MapType MapType;
|
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
const HealpixFourierMap<T>& mfm = dynamic_cast<const HealpixFourierMap<T>&>(other);
|
2012-11-10 18:22:37 +01:00
|
|
|
if (Npix != mfm.size())
|
2012-11-10 15:02:08 +01:00
|
|
|
throw std::bad_cast();
|
|
|
|
|
|
|
|
MapType m1(m_data, Npix);
|
|
|
|
MapType m2(mfm.m_data, mfm.Npix);
|
|
|
|
|
|
|
|
return (m1*m2).sum();
|
|
|
|
}
|
2012-10-07 20:58:01 +02:00
|
|
|
|
|
|
|
virtual FourierMap<T> *mimick() const
|
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
return new HealpixFourierMap<T>(m_Nside);
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2012-11-10 15:02:08 +01:00
|
|
|
class HealpixFourierALM: public FourierMap<std::complex<T> >
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::complex<T> *alms;
|
2012-11-10 18:22:37 +01:00
|
|
|
long m_size;
|
2012-11-10 15:02:08 +01:00
|
|
|
long Lmax_, Mmax_, TVal_;
|
2012-10-07 20:58:01 +02:00
|
|
|
Eigen::aligned_allocator<std::complex<T> > alloc;
|
|
|
|
public:
|
2012-11-10 15:02:08 +01:00
|
|
|
typedef unsigned long LType;
|
|
|
|
|
|
|
|
LType Lmax() const { return Lmax_; }
|
|
|
|
LType Mmax() const { return Mmax_; }
|
|
|
|
|
|
|
|
LType Num_Alms() const
|
|
|
|
{
|
|
|
|
return ((Mmax_+1)*(Mmax_+2))/2 + (Mmax_+1)*(Lmax_-Mmax_);
|
|
|
|
}
|
|
|
|
|
|
|
|
LType index_l0(LType m) const
|
|
|
|
{
|
|
|
|
return ((m*(TVal_-m))/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
LType index(LType l, LType m) const
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
return index_l0(m) + l;
|
|
|
|
}
|
|
|
|
|
|
|
|
HealpixFourierALM(LType lmax, LType mmax)
|
|
|
|
: Lmax_(lmax), Mmax_(mmax), TVal_(2*lmax+1)
|
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
m_size = Num_Alms();
|
|
|
|
alms = alloc.allocate(m_size);
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~HealpixFourierALM()
|
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
alloc.deallocate(alms, m_size);
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
virtual const std::complex<T>* data() const { return alms; }
|
|
|
|
virtual std::complex<T> * data() { return alms;}
|
|
|
|
virtual long size() const { return m_size; }
|
2012-10-07 20:58:01 +02:00
|
|
|
|
2012-11-10 15:02:08 +01:00
|
|
|
virtual FourierMap<std::complex<T> > *mimick() const
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
return new HealpixFourierALM<T>(Lmax_, Mmax_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::complex<T> dot_product(const FourierMap<std::complex<T> >& other) const
|
|
|
|
throw(std::bad_cast)
|
|
|
|
{
|
|
|
|
const HealpixFourierALM<T>& mfm = dynamic_cast<const HealpixFourierALM<T>&>(other);
|
2012-11-10 18:22:37 +01:00
|
|
|
typedef typename FourierMap<std::complex<T> >::MapType MapType;
|
2012-11-10 15:02:08 +01:00
|
|
|
std::complex<T> S;
|
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
if (m_size != mfm.m_size)
|
2012-11-10 15:02:08 +01:00
|
|
|
throw std::bad_cast();
|
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
MapType m1(alms, m_size);
|
|
|
|
MapType m2(mfm.alms, mfm.m_size);
|
2012-11-10 15:02:08 +01:00
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
S = (m1.block(0,0,1,Lmax_+1).conjugate() * m2.block(0,0,1,Lmax_+1)).sum();
|
|
|
|
S += std::complex<T>(2,0)*(m1.block(0,1+Lmax_,1,m_size-1-Lmax_).conjugate() * m2.block(0,1+Lmax_,1,m_size-1-Lmax_)).sum();
|
2012-11-10 15:02:08 +01:00
|
|
|
return S;
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
template<typename T> struct HealpixJobHelper__ {};
|
|
|
|
|
|
|
|
template<> struct HealpixJobHelper__<double>
|
|
|
|
{ enum {val=1}; };
|
|
|
|
|
|
|
|
template<> struct HealpixJobHelper__<float>
|
|
|
|
{ enum {val=0}; };
|
|
|
|
|
|
|
|
|
2012-10-07 20:58:01 +02:00
|
|
|
template<typename T>
|
2012-11-10 18:22:37 +01:00
|
|
|
class HealpixFourierTransform: public FourierTransform<T>
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
|
|
|
private:
|
2012-11-10 18:22:37 +01:00
|
|
|
sharp_alm_info *ainfo;
|
|
|
|
sharp_geom_info *ginfo;
|
2012-10-07 20:58:01 +02:00
|
|
|
HealpixFourierMap<T> realMap;
|
|
|
|
HealpixFourierALM<T> fourierMap;
|
2012-11-10 18:22:37 +01:00
|
|
|
int m_iterate;
|
2012-10-07 20:58:01 +02:00
|
|
|
public:
|
2012-11-10 18:22:37 +01:00
|
|
|
HealpixFourierTransform(long nSide, long Lmax, long Mmax, int iterate = 0)
|
|
|
|
: realMap(nSide), fourierMap(Lmax, Mmax), ainfo(0), ginfo(0), m_iterate(iterate)
|
2012-10-07 20:58:01 +02:00
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
sharp_make_healpix_geom_info (nSide, 1, &ginfo);
|
|
|
|
sharp_make_triangular_alm_info (Lmax, Mmax, 1, &ainfo);
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 18:22:37 +01:00
|
|
|
virtual ~HealpixFourierTransform()
|
|
|
|
{
|
|
|
|
sharp_destroy_geom_info(ginfo);
|
|
|
|
sharp_destroy_alm_info(ainfo);
|
|
|
|
}
|
2012-10-07 20:58:01 +02:00
|
|
|
|
|
|
|
virtual const FourierMap<std::complex<T> >& fourierSpace() const { return fourierMap; }
|
|
|
|
|
|
|
|
virtual FourierMap<std::complex<T> >& fourierSpace() { return fourierMap; }
|
|
|
|
|
|
|
|
virtual const FourierMap<T>& realSpace() const { return realMap; }
|
|
|
|
|
|
|
|
virtual FourierMap<T>& realSpace() { return realMap; }
|
|
|
|
|
|
|
|
virtual FourierTransform<T> *mimick() const
|
|
|
|
{
|
2012-11-10 18:22:37 +01:00
|
|
|
return new HealpixFourierTransform<T>(realMap.Nside(), fourierMap.Lmax(), fourierMap.Mmax());
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void analysis()
|
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
void *aptr=reinterpret_cast<void *>(fourierMap.data()), *mptr=reinterpret_cast<void *>(realMap.data());
|
|
|
|
|
|
|
|
sharp_execute (SHARP_MAP2ALM, 0, 0, &aptr, &mptr, ginfo, ainfo, 1,
|
2012-11-10 18:22:37 +01:00
|
|
|
HealpixJobHelper__<T>::val,0,0,0);
|
|
|
|
for (int i = 0; i < m_iterate; i++)
|
|
|
|
{
|
|
|
|
HealpixFourierMap<T> tmp_map(realMap.Nside());
|
|
|
|
void *tmp_ptr=reinterpret_cast<void *>(tmp_map.data());
|
|
|
|
typename HealpixFourierMap<T>::MapType m0 = tmp_map.eigen();
|
|
|
|
typename HealpixFourierMap<T>::MapType m1 = realMap.eigen();
|
|
|
|
|
|
|
|
sharp_execute (SHARP_ALM2MAP, 0, 0, &aptr, &tmp_ptr, ginfo, ainfo, 1,
|
|
|
|
HealpixJobHelper__<T>::val,0,0,0);
|
|
|
|
m0 = m1 - m0;
|
|
|
|
sharp_execute (SHARP_MAP2ALM, 0, 1, &aptr, &tmp_ptr, ginfo, ainfo, 1,
|
|
|
|
HealpixJobHelper__<T>::val,0,0,0);
|
|
|
|
}
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void synthesis()
|
|
|
|
{
|
2012-11-10 15:02:08 +01:00
|
|
|
void *aptr=reinterpret_cast<void *>(fourierMap.data()), *mptr=reinterpret_cast<void *>(realMap.data());
|
|
|
|
|
|
|
|
sharp_execute (SHARP_ALM2MAP, 0, 0, &aptr, &mptr, ginfo, ainfo, 1,
|
2012-11-10 18:22:37 +01:00
|
|
|
HealpixJobHelper__<T>::val,0,0,0);
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void analysis_conjugate()
|
|
|
|
{
|
|
|
|
synthesis();
|
2012-11-10 18:22:37 +01:00
|
|
|
realMap.scale(4*M_PI/realMap.size());
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void synthesis_conjugate()
|
|
|
|
{
|
|
|
|
analysis();
|
2012-11-10 18:22:37 +01:00
|
|
|
fourierMap.scale(realMap.size()/(4*M_PI));
|
2012-10-07 20:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2012-11-10 15:02:08 +01:00
|
|
|
|
|
|
|
template<typename T>
|
2012-11-11 21:46:03 +01:00
|
|
|
void HealpixSpectrum<T>::newRandomFourier(gsl_rng *rng, FourierMapType& out_map) const
|
2012-11-10 15:02:08 +01:00
|
|
|
{
|
2012-11-11 21:46:03 +01:00
|
|
|
HealpixFourierALM<T>& alms = dynamic_cast<HealpixFourierALM<T>&>(out_map);
|
2012-11-10 15:02:08 +01:00
|
|
|
long lmaxGen = std::min(cls.size()-1, alms.Lmax());
|
2012-11-11 21:46:03 +01:00
|
|
|
std::complex<T> *new_data = alms.data();
|
2012-11-10 15:02:08 +01:00
|
|
|
|
2012-11-10 23:35:27 +01:00
|
|
|
for (long l = 0; l <= lmaxGen; l++)
|
2012-11-10 15:02:08 +01:00
|
|
|
{
|
|
|
|
double Al = std::sqrt(cls[l]);
|
|
|
|
|
|
|
|
new_data[alms.index(l,0)] = gsl_ran_gaussian(rng, Al);
|
|
|
|
Al *= M_SQRT1_2;
|
2012-11-10 23:35:27 +01:00
|
|
|
for (long m = 1; m <= std::min(l,alms.Mmax()); m++)
|
2012-11-10 15:02:08 +01:00
|
|
|
{
|
|
|
|
std::complex<T>& c = new_data[alms.index(l,m)];
|
|
|
|
c.real() = gsl_ran_gaussian(rng, Al);
|
|
|
|
c.imag() = gsl_ran_gaussian(rng, Al);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-10 23:35:27 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void HealpixSpectrum<T>::mul(FourierMapType& like_map) const
|
|
|
|
{
|
|
|
|
HealpixFourierALM<T>& alms = dynamic_cast<HealpixFourierALM<T>&>(like_map);
|
|
|
|
std::complex<T> *data = alms.data();
|
|
|
|
|
|
|
|
for (long l = 0; l <= alms.Lmax(); l++)
|
|
|
|
{
|
|
|
|
double Al = cls[l];
|
|
|
|
|
|
|
|
for (long m = 0; m <= std::min(l,alms.Mmax()); m++)
|
|
|
|
{
|
|
|
|
data[alms.index(l,m)] *= Al;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void HealpixSpectrum<T>::mul_sqrt(FourierMapType& like_map) const
|
|
|
|
{
|
|
|
|
HealpixFourierALM<T>& alms = dynamic_cast<const HealpixFourierALM<T>&>(like_map);
|
|
|
|
std::complex<T> *data = alms.data();
|
|
|
|
|
|
|
|
for (long l = 0; l <= alms.Lmax(); l++)
|
|
|
|
{
|
|
|
|
double Al = std::sqrt(cls[l]);
|
|
|
|
|
|
|
|
for (long m = 0; m <= std::min(l,alms.Mmax()); m++)
|
|
|
|
{
|
|
|
|
data[alms.index(l,m)] *= Al;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void HealpixSpectrum<T>::mul_inv(FourierMapType& like_map) const
|
|
|
|
{
|
|
|
|
HealpixFourierALM<T>& alms = dynamic_cast<HealpixFourierALM<T>&>(like_map);
|
|
|
|
std::complex<T> *data = alms.data();
|
|
|
|
|
|
|
|
for (long l = 0; l <= alms.Lmax(); l++)
|
|
|
|
{
|
|
|
|
double Al = (cls[l] <= 0) ? 0 : (1/cls[l]);
|
|
|
|
|
|
|
|
for (long m = 0; m <= std::min(l,alms.Mmax()); m++)
|
|
|
|
{
|
|
|
|
data[alms.index(l,m)] *= Al;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void HealpixSpectrum<T>::mul_inv_sqrt(FourierMapType& like_map) const
|
|
|
|
{
|
|
|
|
HealpixFourierALM<T>& alms = dynamic_cast<HealpixFourierALM<T>&>(like_map);
|
|
|
|
std::complex<T> *data = alms.data();
|
|
|
|
|
|
|
|
for (long l = 0; l <= alms.Lmax(); l++)
|
|
|
|
{
|
|
|
|
double Al = (cls[l] <= 0) ? 0 : std::sqrt(1/cls[l]);
|
|
|
|
|
|
|
|
for (long m = 0; m <= std::min(l,alms.Mmax()); m++)
|
|
|
|
{
|
|
|
|
data[alms.index(l,m)] *= Al;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-07 20:58:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|