interpol_ng.cc 8.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 *  Copyright (C) 2020 Max-Planck-Society
 *  Author: Martin Reinecke
 */

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <vector>
#include <complex>
#include "mr_util/math/constants.h"
#include "mr_util/math/gl_integrator.h"
#include "mr_util/math/es_kernel.h"
#include "mr_util/infra/mav.h"
#include "mr_util/sharp/sharp.h"
#include "mr_util/sharp/sharp_almhelpers.h"
#include "mr_util/sharp/sharp_geomhelpers.h"
#include "alm.h"
#include "mr_util/math/fft.h"
#include "mr_util/bindings/pybind_utils.h"
Martin Reinecke's avatar
Martin Reinecke committed
20

21
22
23
24
25
26
27
using namespace std;
using namespace mr;

namespace py = pybind11;

namespace {

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
28
29
constexpr double ofmin=1.5;

30
31
32
template<typename T> class Interpolator
  {
  protected:
Martin Reinecke's avatar
Martin Reinecke committed
33
    size_t lmax, kmax, nphi0, ntheta0, nphi, ntheta;
Martin Reinecke's avatar
Martin Reinecke committed
34
    int nthreads;
Martin Reinecke's avatar
fix    
Martin Reinecke committed
35
36
    double ofactor;
    size_t supp;
37
38
39
    ES_Kernel kernel;
    mav<T,3> cube; // the data cube (theta, phi, 2*mbeam+1[, IQU])

40
    void correct(mav<T,2> &arr, int spin)
41
      {
42
      double sfct = (spin&1) ? -1 : 1;
43
      mav<T,2> tmp({nphi,nphi});
Martin Reinecke's avatar
Martin Reinecke committed
44
      tmp.apply([](T &v){v=0.;});
Martin Reinecke's avatar
Martin Reinecke committed
45
      auto tmp0=tmp.template subarray<2>({0,0},{nphi0, nphi0});
Martin Reinecke's avatar
Martin Reinecke committed
46
47
48
49
      fmav<T> ftmp0(tmp0);
      for (size_t i=0; i<ntheta0; ++i)
        for (size_t j=0; j<nphi0; ++j)
          tmp0.v(i,j) = arr(i,j);
50
      // extend to second half
Martin Reinecke's avatar
Martin Reinecke committed
51
52
      for (size_t i=1, i2=2*ntheta0-3; i+1<ntheta0; ++i,--i2)
        for (size_t j=0,j2=nphi0/2; j<nphi0; ++j,++j2)
53
          {
Martin Reinecke's avatar
Martin Reinecke committed
54
          if (j2>=nphi0) j2-=nphi0;
Martin Reinecke's avatar
Martin Reinecke committed
55
          tmp0.v(i2,j) = sfct*tmp0(i,j2);
56
          }
Martin Reinecke's avatar
Martin Reinecke committed
57
      // FFT to frequency domain on minimal grid
58
      r2r_fftpack(ftmp0,ftmp0,{1,0},true,true,1./(nphi0*nphi0),nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
59
      auto fct = kernel.correction_factors(nphi, nphi0/2+1, nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
60
61
62
      for (size_t i=0; i<nphi0; ++i)
        for (size_t j=0; j<nphi0; ++j)
          tmp0.v(i,j) *= fct[(i+1)/2] * fct[(j+1)/2];
Martin Reinecke's avatar
Martin Reinecke committed
63
      auto tmp1=tmp.template subarray<2>({0,0},{nphi, nphi0});
Martin Reinecke's avatar
Martin Reinecke committed
64
      fmav<T> ftmp1(tmp1);
Martin Reinecke's avatar
Martin Reinecke committed
65
      // zero-padded FFT in theta direction
Martin Reinecke's avatar
Martin Reinecke committed
66
      r2r_fftpack(ftmp1,ftmp1,{0},false,false,1.,nthreads);
Martin Reinecke's avatar
Martin Reinecke committed
67
      auto tmp2=tmp.template subarray<2>({0,0},{ntheta, nphi});
Martin Reinecke's avatar
Martin Reinecke committed
68
69
      fmav<T> ftmp2(tmp2);
      fmav<T> farr(arr);
Martin Reinecke's avatar
Martin Reinecke committed
70
      // zero-padded FFT in phi direction
Martin Reinecke's avatar
Martin Reinecke committed
71
      r2r_fftpack(ftmp2,farr,{1},false,false,1.,nthreads);
72
73
74
75
      }

  public:
    Interpolator(const Alm<complex<T>> &slmT, const Alm<complex<T>> &blmT,
Martin Reinecke's avatar
Martin Reinecke committed
76
      double epsilon, int nthreads_)
Martin Reinecke's avatar
fix    
Martin Reinecke committed
77
      : lmax(slmT.Lmax()),
78
        kmax(blmT.Mmax()),
Martin Reinecke's avatar
Martin Reinecke committed
79
80
        nphi0(2*good_size_real(lmax+1)),
        ntheta0(nphi0/2+1),
81
        nphi(max<size_t>(20,2*good_size_real(size_t((2*lmax+1)*ofmin/2.)))),
Martin Reinecke's avatar
Martin Reinecke committed
82
        ntheta(nphi/2+1),
Martin Reinecke's avatar
Martin Reinecke committed
83
        nthreads(nthreads_),
Martin Reinecke's avatar
Martin Reinecke committed
84
        ofactor(double(nphi)/(2*lmax+1)),
Martin Reinecke's avatar
fix    
Martin Reinecke committed
85
        supp(ES_Kernel::get_supp(epsilon, ofactor)),
Martin Reinecke's avatar
Martin Reinecke committed
86
        kernel(supp, ofactor, nthreads),
87
88
89
90
91
92
        cube({ntheta+2*supp, nphi+2*supp, 2*kmax+1})
      {
      MR_assert((supp<=ntheta) && (supp<=nphi), "support too large!");
      MR_assert(slmT.Mmax()==lmax, "Sky lmax must be equal to Sky mmax");
      MR_assert(blmT.Lmax()==lmax, "Sky and beam lmax must be equal");
      Alm<complex<T>> a1(lmax, lmax), a2(lmax,lmax);
Martin Reinecke's avatar
Martin Reinecke committed
93
      auto ginfo = sharp_make_cc_geom_info(ntheta0,nphi0,0.,cube.stride(1),cube.stride(0));
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
      auto ainfo = sharp_make_triangular_alm_info(lmax,lmax,1);

      vector<double>lnorm(lmax+1);
      for (size_t i=0; i<=lmax; ++i)
        lnorm[i]=sqrt(4*pi/(2*i+1.));

      for (size_t k=0; k<=kmax; ++k)
        {
        double spinsign = (k==0) ? 1. : -1.;
        for (size_t m=0; m<=lmax; ++m)
          for (size_t l=m; l<=lmax; ++l)
            {
            if (l<k)
              a1(l,m)=a2(l,m)=0.;
            else
              {
Martin Reinecke's avatar
Martin Reinecke committed
110
111
              auto tmp = blmT(l,k)*T(spinsign*lnorm[l]);
              a1(l,m) = slmT(l,m)*tmp.real();
112
              if (k>0)
Martin Reinecke's avatar
Martin Reinecke committed
113
                a2(l,m) = slmT(l,m)*tmp.imag();
114
115
116
117
              }
            }
        size_t kidx1 = (k==0) ? 0 : 2*k-1,
               kidx2 = (k==0) ? 0 : 2*k;
Martin Reinecke's avatar
Martin Reinecke committed
118
119
        auto m1 = cube.template subarray<2>({supp,supp,kidx1},{ntheta,nphi,0});
        auto m2 = cube.template subarray<2>({supp,supp,kidx2},{ntheta,nphi,0});
120
        if (k==0)
121
          sharp_alm2map(a1.Alms().data(), m1.vdata(), *ginfo, *ainfo, 0, nthreads);
122
        else
123
          sharp_alm2map_spin(k, a1.Alms().data(), a2.Alms().data(), m1.vdata(), m2.vdata(), *ginfo, *ainfo, 0, nthreads);
124
125
126
127
        correct(m1,k);
        if (k!=0) correct(m2,k);
        if (k!=0)
          { m1.apply([](T &v){v*=2;}); m2.apply([](T &v){v*=2;}); }
128
129
130
131
132
133
        }
      // fill border regions
      for (size_t i=0; i<supp; ++i)
        for (size_t j=0, j2=nphi/2; j<nphi; ++j,++j2)
          for (size_t k=0; k<cube.shape(2); ++k)
            {
134
            double fct = (((k+1)/2)&1) ? -1 : 1;
135
            if (j2>=nphi) j2-=nphi;
136
137
            cube.v(supp-1-i,j2+supp,k) = fct*cube(supp+1+i,j+supp,k);
            cube.v(supp+ntheta+i,j2+supp,k) = fct*cube(supp+ntheta-2-i, j+supp,k);
138
139
140
141
142
143
144
145
146
147
148
149
150
151
            }
      for (size_t i=0; i<ntheta+2*supp; ++i)
        for (size_t j=0; j<supp; ++j)
          for (size_t k=0; k<cube.shape(2); ++k)
            {
            cube.v(i,j,k) = cube(i,j+nphi,k);
            cube.v(i,j+nphi+supp,k) = cube(i,j+supp,k);
            }
      }

    void interpolx (const mav<T,2> &ptg, mav<T,1> &res) const
      {
      MR_assert(ptg.shape(0)==res.shape(0), "dimension mismatch");
      MR_assert(ptg.shape(1)==3, "second dimension must have length 3");
Martin Reinecke's avatar
Martin Reinecke committed
152
153
154
      double delta = 2./supp;
      double xdtheta = (ntheta-1)/pi,
             xdphi = nphi/(2*pi);
Martin Reinecke's avatar
Martin Reinecke committed
155
      vector<size_t> idx(ptg.shape(0));
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
156
#if 1
Martin Reinecke's avatar
Martin Reinecke committed
157
      {
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
158
159
160
161
162
163
      constexpr size_t cellsize=16;
      size_t nct = ntheta/cellsize+1,
             ncp = nphi/cellsize+1;
      vector<vector<size_t>> mapper(nct*ncp);
      for (size_t i=0; i<ptg.shape(0); ++i)
        {
164
165
        size_t itheta=min(nct-1,size_t(ptg(i,0)/pi*nct)),
               iphi=min(ncp-1,size_t(ptg(i,1)/(2*pi)*ncp));
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
166
167
        mapper[itheta*ncp+iphi].push_back(i);
        }
Martin Reinecke's avatar
Martin Reinecke committed
168
      size_t cnt=0;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
169
      for (const auto &vec: mapper)
Martin Reinecke's avatar
Martin Reinecke committed
170
171
172
        for (auto i:vec)
          idx[cnt++] = i;
      }
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
173
#else
Martin Reinecke's avatar
Martin Reinecke committed
174
175
      for (size_t i=0; i<idx.size(); ++i)
        idx[i]=i;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
176
#endif
Martin Reinecke's avatar
Martin Reinecke committed
177
      execStatic(idx.size(), nthreads, 0, [&](Scheduler &sched)
178
        {
Martin Reinecke's avatar
Martin Reinecke committed
179
180
181
        vector<T> wt(supp), wp(supp);
        vector<T> psiarr(2*kmax+1);
        while (auto rng=sched.getNext()) for(auto ind=rng.lo; ind<rng.hi; ++ind)
182
          {
Martin Reinecke's avatar
Martin Reinecke committed
183
184
185
186
187
188
189
190
191
192
193
          size_t i=idx[ind];
          double f0=0.5*supp+ptg(i,0)*xdtheta;
          size_t i0 = size_t(f0+1.);
          for (size_t t=0; t<supp; ++t)
            wt[t] = kernel((t+i0-f0)*delta - 1);
          double f1=0.5*supp+ptg(i,1)*xdphi;
          size_t i1 = size_t(f1+1.);
          for (size_t t=0; t<supp; ++t)
            wp[t] = kernel((t+i1-f1)*delta - 1);
          double val=0;
          psiarr[0]=1.;
194
195
          double psi=ptg(i,2);
          double cpsi=cos(psi), spsi=sin(psi);
Martin Reinecke's avatar
Martin Reinecke committed
196
197
198
199
          double cnpsi=cpsi, snpsi=spsi;
          for (size_t l=1; l<=kmax; ++l)
            {
            psiarr[2*l-1]=cnpsi;
200
            psiarr[2*l]=snpsi;
Martin Reinecke's avatar
Martin Reinecke committed
201
202
203
204
205
206
207
208
209
            const double tmp = snpsi*cpsi + cnpsi*spsi;
            cnpsi=cnpsi*cpsi - snpsi*spsi;
            snpsi=tmp;
            }
          for (size_t j=0; j<supp; ++j)
            for (size_t k=0; k<supp; ++k)
              for (size_t l=0; l<2*kmax+1; ++l)
                val += cube(i0+j,i1+k,l)*wt[j]*wp[k]*psiarr[l];
          res.v(i) = val;
210
          }
Martin Reinecke's avatar
Martin Reinecke committed
211
        });
212
213
214
215
216
217
      }
  };

template<typename T> class PyInterpolator: public Interpolator<T>
  {
  public:
Martin Reinecke's avatar
Martin Reinecke committed
218
    PyInterpolator(const py::array &slmT, const py::array &blmT, int64_t lmax, int64_t kmax, double epsilon, int nthreads=0)
219
220
      : Interpolator<T>(Alm<complex<T>>(to_mav<complex<T>,1>(slmT), lmax, lmax),
                        Alm<complex<T>>(to_mav<complex<T>,1>(blmT), lmax, kmax),
Martin Reinecke's avatar
Martin Reinecke committed
221
                        epsilon, nthreads) {}
222
223
224
225
226
227
228
229
230
231
232
    using Interpolator<T>::interpolx;
    py::array interpol(const py::array &ptg)
      {
      auto ptg2 = to_mav<T,2>(ptg);
      auto res = make_Pyarr<double>({ptg2.shape(0)});
      auto res2 = to_mav<double,1>(res,true);
      interpolx(ptg2, res2);
      return res;
      }
  };

233
#if 1
Martin Reinecke's avatar
Martin Reinecke committed
234
235
236
template<typename T> py::array pyrotate_alm(const py::array &alm_, int64_t lmax,
  double psi, double theta, double phi)
  {
237
238
239
240
241
242
243
  auto a1 = to_mav<complex<T>,1>(alm_);
  auto alm = make_Pyarr<complex<T>>({a1.shape(0)});
  auto a2 = to_mav<complex<T>,1>(alm,true);
  for (size_t i=0; i<a1.shape(0); ++i) a2.v(i)=a1(i);
  auto blah = Alm<complex<T>>(a2,lmax,lmax);
  rotate_alm(blah, psi, theta, phi);
  return alm;
Martin Reinecke's avatar
Martin Reinecke committed
244
245
246
  }
#endif

247
248
249
250
251
252
253
} // unnamed namespace

PYBIND11_MODULE(interpol_ng, m)
  {
  using namespace pybind11::literals;

  py::class_<PyInterpolator<double>> (m, "PyInterpolator")
Martin Reinecke's avatar
Martin Reinecke committed
254
255
    .def(py::init<const py::array &, const py::array &, int64_t, int64_t, double, int>(),
      "sky"_a, "beam"_a, "lmax"_a, "kmax"_a, "epsilon"_a, "nthreads"_a)
256
    .def ("interpol", &PyInterpolator<double>::interpol, "ptg"_a);
257
#if 1
Martin Reinecke's avatar
Martin Reinecke committed
258
259
260
  m.def("rotate_alm", &pyrotate_alm<double>, "alm"_a, "lmax"_a, "psi"_a, "theta"_a,
    "phi"_a);
#endif
261
  }