/* * This file is part of Healpix_cxx. * * Healpix_cxx is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Healpix_cxx 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Healpix_cxx; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * For more information about HEALPix, see http://healpix.sourceforge.net */ /* * Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt * (DLR). */ /*! \file weight_utils.cc * * Functionality for computing ring weights and full map weights * * Helpful literature: * Graef, Kunis, Potts: On the computation of nonnegative quadrature weights * on the sphere * (https://www-user.tu-chemnitz.de/~potts/paper/quadgewS2.pdf) * Lambers: Minimum Norm Solutions of Underdetermined Systems * (http://www.math.usm.edu/lambers/mat419/lecture15.pdf) * Shewchuk: An Introduction to the Conjugate Gradient Method Without the * Agonizing Pain * (https://www.cs.cmu.edu/~quake-papers/painless-conjugate-gradient.pdf) * * Copyright (C) 2016-2019 Max-Planck-Society * \author Martin Reinecke */ #include #include #include #include "healpix_map.h" #include "alm_healpix_tools.h" #include "weight_utils.h" #include "alm.h" #include "libsharp/sharp_cxx.h" #include "lsconstants.h" using namespace std; /* General considerations concerning the full weights and their a_lm coefficients: The Healpix grid geometry has several symmetries: - mirror symmetry with respect to phi=0 - mirror symmetry with respect to the equator - invariance with respect to rotation around the z-axis by multiples of pi/2 This reduces the number of pixels with distinct weights to roughly 1/16 of the total number of map pixels. The geometrical symmetries also reduce the number of a_lm coefficients required to describe the weight map: - the mirror symmetry in phi means that all a_lm are purely real-valued - the mirror symmetry in theta means that all a_lm with odd l are zero - the rotational symmetry means that all a_lm with m not a multiple of 4 vanish as well. These symmetries are used when storing a_lm coefficients and weight maps to save memory. So far, the SHTs themselves are carried out with full maps and a_lm sets, because there is no libsharp support for this kind of symmetries yet. */ namespace weight_utils_detail { using namespace std; // inner product of two vectors double dprod(const vector &a, const vector &b) { return inner_product(a.begin(),a.end(),b.begin(),0.); } tsize n_fullweights (int nside) { return ((3*nside+1)*(nside+1))/4; } template void apply_weight (T &pix, double w, bool setwgt) { if (setwgt) pix=T(w); else if (!approx(pix,Healpix_undef)) pix*=T(1.+w); } template void apply_fullweights (Healpix_Map &map, const vector &wgt, bool setwgt) { planck_assert (map.Scheme()==RING, "bad map ordering scheme"); int nside=map.Nside(); planck_assert(wgt.size()==n_fullweights(nside), "incorrect size of weight array"); int pix=0, vpix=0; for (int i=0; i<2*nside; ++i) { bool shifted = (i>1) + ((odd||shifted) ? 0 : 1); int psouth=map.Npix()-pix-(qpix<<2); for (int j=0; j<(qpix<<2); ++j) { int j4=j%qpix; int rpix=min(j4,qpix - (shifted ? 1:0) - j4); apply_weight (map[pix+j],wgt[vpix+rpix],setwgt); if (i!=2*nside-1) // everywhere except on equator apply_weight(map[psouth+j],wgt[vpix+rpix],setwgt); } pix+=qpix<<2; vpix+=wpix; } } vector extract_fullweights (const Healpix_Map &map) { planck_assert (map.Scheme()==RING, "bad map ordering scheme"); int nside=map.Nside(); vector res; res.reserve(n_fullweights(nside)); int pix=0; for (int i=0; i<2*nside; ++i) { bool shifted = (i>1) + ((odd||shifted) ? 0 : 1); for (int j=0; j>2)+1; return nmsteps * (((lmax+2)>>1) - nmsteps+1); } vector extract_weightalm (const Alm > &alm) { vector res; res.reserve(n_weightalm(alm.Lmax(),alm.Mmax())); for (int m=0; m<=alm.Mmax(); m+=4) for (int l=m; l<=alm.Lmax(); l+=2) res.push_back(real(alm(l,m)) * ((m==0) ? 1 : sqrt(2.))); return res; } void expand_weightalm (const vector &calm, Alm > &alm) { planck_assert(calm.size()==n_weightalm(alm.Lmax(), alm.Mmax()), "incorrect size of weight array"); alm.SetToZero(); tsize idx=0; for (int m=0; m<=alm.Mmax(); m+=4) for (int l=m; l<=alm.Lmax(); l+=2) alm(l,m) = calm[idx++] * ((m==0) ? 1 : sqrt(0.5)); } class STS_hpwgt { private: int lmax, mmax, nside; public: using vectype=vector; STS_hpwgt (int lmax_, int mmax_, int nside_) : lmax(lmax_), mmax(mmax_), nside(nside_) { planck_assert((lmax&1)==0,"lmax must be even"); } vectype S (const vectype &x) const { Alm > ta(lmax,mmax); expand_weightalm(x,ta); Healpix_Map tm(nside,RING, SET_NSIDE); alm2map(ta,tm); return extract_fullweights(tm); } vectype ST (const vectype &x) const { Healpix_Map tm(nside,RING, SET_NSIDE); apply_fullweights(tm,x,true); Alm > ta(lmax,mmax); alm2map_adjoint(tm,ta); return extract_weightalm(ta); } vectype apply (const vectype &x) const { return ST(S(x)); } }; class STS_hpring { private: int lmax, nside; sharp_cxxjob job; public: using vectype=vector; STS_hpring (int lmax_, int nside_) : lmax(lmax_), nside(nside_) { planck_assert((lmax&1)==0,"lmax must be even"); int nring=2*nside; vector dbl0(nring,0.),theta(nring); vector int1(nring,1); vector ofs(nring); Healpix_Base base(nside, RING, SET_NSIDE); for (int i=0; i > alm2(2*alm.size()-1,0.); for (tsize i=0; i > alm2(lmax+1,0.); job.alm2map_adjoint(&map[0],&alm2[0], false); vectype res(lmax/2+1); for (tsize i=0; i muladd (double fct, const vector &a, const vector &b) { planck_assert(a.size()==b.size(),"types not conformable"); vector res(b); for (tsize i=0; i double cg_solve (const M &A, typename M::vectype &x, const typename M::vectype &b, double epsilon, int itmax) { typename M::vectype r=muladd(-1.,A.apply(x),b), d(r); double delta0=dprod(r,r), deltanew=delta0; cout << "res0: " << sqrt(delta0) << endl; for (int iter=0; iter x, rhs, r, d; double delta0, delta_cur; int iter; public: FullWeightImpl(int nside, int lmax) : mat(lmax, lmax, nside), x(n_weightalm(lmax,lmax),0.), iter(0) { planck_assert((lmax&1)==0,"lmax must be even"); rhs = mat.ST(vector(n_fullweights(nside),-1.)); rhs[0]+=12*nside*nside/sqrt(4*pi); r=muladd(-1.,mat.apply(x),rhs); d=r; delta0=dprod(r,r); delta_cur=delta0; } void iterate(int niter) { int iend=iter+niter; for (; iter current_alm() const { return x; } std::vector alm2wgt(const vector &alm) const { return mat.S(alm); } double current_epsilon() const { return sqrt(delta_cur/delta0); } int current_iter() const { return iter; } }; } //namespace weight_utils_detail; using namespace weight_utils_detail; vector get_fullweights(int nside, int lmax, double epsilon, int itmax, double &epsilon_out) { planck_assert((lmax&1)==0,"lmax must be even"); STS_hpwgt mat(lmax, lmax, nside); vector x(n_weightalm(lmax,lmax),0.); vector rhs=mat.ST(vector (n_fullweights(nside),-1.)); rhs[0]+=12*nside*nside/sqrt(4*pi); epsilon_out=cg_solve(mat, x, rhs, epsilon, itmax); return mat.S(x); } FullWeightComputer::FullWeightComputer(int nside, int lmax) : impl(new FullWeightImpl(nside, lmax)) {} FullWeightComputer::~FullWeightComputer() {} void FullWeightComputer::iterate(int niter) { impl->iterate(niter); } std::vector FullWeightComputer::current_alm() const { return impl->current_alm(); } std::vector FullWeightComputer::alm2wgt(const vector &alm) const { return impl->alm2wgt(alm); } double FullWeightComputer::current_epsilon() const { return impl->current_epsilon(); } int FullWeightComputer::current_iter() const { return impl->current_iter(); } template void apply_fullweights (Healpix_Map &map, const vector &wgt) { apply_fullweights (map,wgt,false); } template void apply_fullweights (Healpix_Map &map, const vector &wgt); template void apply_fullweights (Healpix_Map &map, const vector &wgt); vector get_ringweights(int nside, int lmax, double epsilon, int itmax, double &epsilon_out) { planck_assert((lmax&1)==0,"lmax must be even"); STS_hpring mat(lmax,nside); vector nir(2*nside), x(lmax/2+1,0.); for (tsize ith=0; ith(ith+1,nside); nir[2*nside-1]/=2; auto b=mat.ST(nir); for (tsize i=0; i