nifty_gridder.cc 13.7 KB
Newer Older
Martin Reinecke's avatar
import  
Martin Reinecke committed
1
2
3
4
5
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <iostream>
#include <algorithm>

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
6
7
#ifdef __GNUC__
#define RESTRICT __restrict__
Martin Reinecke's avatar
Martin Reinecke committed
8
#define NOINLINE __attribute__ ((noinline))
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
9
10
11
12
#else
#define RESTRICT
#endif

Martin Reinecke's avatar
import  
Martin Reinecke committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using namespace std;

namespace py = pybind11;

namespace {

constexpr double pi = 3.141592653589793238462643383279502884197;

static const uint16_t utab[] = {
#define Z(a) 0x##a##0, 0x##a##1, 0x##a##4, 0x##a##5
#define Y(a) Z(a##0), Z(a##1), Z(a##4), Z(a##5)
#define X(a) Y(a##0), Y(a##1), Y(a##4), Y(a##5)
X(0),X(1),X(4),X(5)
#undef X
#undef Y
#undef Z
};

uint32_t coord2morton2D_32 (uint32_t x, uint32_t y)
  {
  typedef uint32_t I;
  return  (I)(utab[x&0xff])     | ((I)(utab[(x>>8)&0xff])<<16)
       | ((I)(utab[y&0xff])<<1) | ((I)(utab[(y>>8)&0xff])<<17);
  }

static const uint8_t m2p2D_1[4][4] = {
{ 4, 1, 11, 2},{0,15, 5, 6},{10,9,3,12},{14,7,13,8}};
static uint8_t m2p2D_3[4][64];
static const uint8_t p2m2D_1[4][4] = {
{ 4, 1, 3, 10},{0,6,7,13},{15,9,8,2},{11,14,12,5}};
static uint8_t p2m2D_3[4][64];
static int peano2d_done=0;

static void init_peano2d (void)
  {
  peano2d_done=1;

  for (int d=0; d<4; ++d)
    for (uint32_t p=0; p<64; ++p)
      {
      unsigned rot = d;
      uint32_t v = p<<26;
      uint32_t res = 0;
      for (int i=0; i<3; ++i)
        {
        unsigned tab=m2p2D_1[rot][v>>30];
        v<<=2;
        res = (res<<2) | (tab&0x3);
        rot = tab>>2;
        }
      m2p2D_3[d][p]=res|(rot<<6);
      }
  for (int d=0; d<4; ++d)
    for (uint32_t p=0; p<64; ++p)
      {
      unsigned rot = d;
      uint32_t v = p<<26;
      uint32_t res = 0;
      for (int i=0; i<3; ++i)
        {
        unsigned tab=p2m2D_1[rot][v>>30];
        v<<=2;
        res = (res<<2) | (tab&0x3);
        rot = tab>>2;
        }
      p2m2D_3[d][p]=res|(rot<<6);
      }
  }

uint32_t morton2peano2D_32(uint32_t v, int bits)
  {
  if (!peano2d_done) init_peano2d();
  unsigned rot = 0;
  uint32_t res = 0;
  v<<=32-(bits<<1);
  int i=0;
  for (; i<bits-2; i+=3)
    {
    unsigned tab=m2p2D_3[rot][v>>26];
    v<<=6;
    res = (res<<6) | (tab&0x3fu);
    rot = tab>>6;
    }
  for (; i<bits; ++i)
    {
    unsigned tab=m2p2D_1[rot][v>>30];
    v<<=2;
    res = (res<<2) | (tab&0x3);
    rot = tab>>2;
    }
  return res;
  }

void myassert(bool cond, const char *msg)
  {
  if (cond) return;
Martin Reinecke's avatar
Martin Reinecke committed
109
  throw runtime_error(msg);
Martin Reinecke's avatar
import  
Martin Reinecke committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  }

template<typename It, typename Comp> class IdxComp__
  {
  private:
    It begin;
    Comp comp;
  public:
    IdxComp__ (It begin_, Comp comp_): begin(begin_), comp(comp_) {}
    bool operator() (std::size_t a, std::size_t b) const
      { return comp(*(begin+a),*(begin+b)); }
  };
/*! Performs an indirect sort on the supplied iterator range and returns in
    \a idx a \a vector containing the indices of the smallest, second smallest,
    third smallest, etc. element, according to \a comp. */
template<typename It, typename T2, typename Comp>
  inline void buildIndex (It begin, It end, std::vector<T2> &idx, Comp comp)
  {
  using namespace std;
  T2 num=end-begin;
  idx.resize(num);
  for (T2 i=0; i<num; ++i) idx[i] = i;
  sort (idx.begin(),idx.end(),IdxComp__<It,Comp>(begin,comp));
  }

/*! Performs an indirect sort on the supplied iterator range and returns in
    \a idx a \a vector containing the indices of the smallest, second smallest,
    third smallest, etc. element. */
template<typename It, typename T2> inline void buildIndex (It begin, It end,
  std::vector<T2> &idx)
  {
  using namespace std;
  typedef typename iterator_traits<It>::value_type T;
  buildIndex(begin,end,idx,less<T>());
  }

146
147
148
149
150
151
152
153
154
155
156
157
/*! Returns the remainder of the division \a v1/v2.
    The result is non-negative.
    \a v1 can be positive or negative; \a v2 must be positive. */
inline double fmodulo (double v1, double v2)
  {
  if (v1>=0)
    return (v1<v2) ? v1 : fmod(v1,v2);
  double tmp=fmod(v1,v2)+v2;
  return (tmp==v2) ? 0. : tmp;
//  return (v1>=0) ? ((v1<v2) ? v1 : fmod(v1,v2)) : (fmod(v1,v2)+v2);
  }

Martin Reinecke's avatar
Martin Reinecke committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
static inline double one_minus_x2 (double x)
  { return (fabs(x)>0.1) ? (1.+x)*(1.-x) : 1.-x*x; }

void legendre_prep(int n, vector<double> &x, vector<double> &w)
  {
  constexpr double pi = 3.141592653589793238462643383279502884197;
  constexpr double eps = 3e-14;
  int m = (n+1)>>1;
  x.resize(m);
  w.resize(m);

  double t0 = 1 - (1-1./n) / (8.*n*n);
  double t1 = 1./(4.*n+2.);

#pragma omp parallel
{
  int i;
#pragma omp for schedule(dynamic,100)
  for (i=1; i<=m; ++i)
    {
    double x0 = cos(pi * ((i<<2)-1) * t1) * t0;

    int dobreak=0;
    int j=0;
    double dpdx;
    while(1)
      {
      double P_1 = 1.0;
      double P0 = x0;
      double dx, x1;

      for (int k=2; k<=n; k++)
        {
        double P_2 = P_1;
        P_1 = P0;
//        P0 = ((2*k-1)*x0*P_1-(k-1)*P_2)/k;
        P0 = x0*P_1 + (k-1.)/k * (x0*P_1-P_2);
        }

      dpdx = (P_1 - x0*P0) * n / one_minus_x2(x0);

      /* Newton step */
      x1 = x0 - P0/dpdx;
      dx = x0-x1;
      x0 = x1;
      if (dobreak) break;

      if (abs(dx)<=eps) dobreak=1;
      if (++j>=100) throw runtime_error("convergence problem");
      }

    x[m-i] = x0;
    w[m-i] = 2. / (one_minus_x2(x0) * dpdx * dpdx);
    }
} // end of parallel region
  }

Martin Reinecke's avatar
import  
Martin Reinecke committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using a_i_c = py::array_t<int, py::array::c_style | py::array::forcecast>;
using a_d_c = py::array_t<double, py::array::c_style | py::array::forcecast>;
using a_c_c = py::array_t<complex<double>,
  py::array::c_style | py::array::forcecast>;

a_i_c peanoindex(const a_d_c &uv_, int nu, int nv)
  {
  myassert(uv_.ndim()==2, "uv array must be 2D");
  myassert(uv_.shape(1)==2, "uv.shape[1] must be 2");
  int nvis = uv_.shape(0);
  auto uv = uv_.data();

  int npmax = max(nu, nv);
  int nbits = 0;
  for (int istart = npmax-1; istart!=0; istart>>=1, ++nbits);
  vector<int> ipeano(nvis);
  for (int i=0; i<nvis; ++i)
    {
233
    auto u = fmodulo(uv[2*i], 1.)*nu;
Martin Reinecke's avatar
import  
Martin Reinecke committed
234
    auto iu = min(nu-1, int(u));
235
    auto v = fmodulo(uv[2*i+1], 1.)*nv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
236
237
238
239
240
241
242
243
244
245
246
247
248
    auto iv = min(nv-1, int(v));
    ipeano[i] = morton2peano2D_32(coord2morton2D_32(iu,iv),nbits);
    }
  vector<int> newind;
  buildIndex(ipeano.begin(), ipeano.end(), newind);
  int odim[] = {nvis};
  a_i_c res(odim);
  auto iout = res.mutable_data();
  for (int i=0; i<nvis; ++i)
    iout[i] = newind[i];
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
249
int get_w(double epsilon)
Martin Reinecke's avatar
Martin Reinecke committed
250
  { return int(-log10(epsilon)+2.9999); }
Martin Reinecke's avatar
Martin Reinecke committed
251

Martin Reinecke's avatar
import  
Martin Reinecke committed
252
253
class Helper
  {
Martin Reinecke's avatar
Martin Reinecke committed
254
255
  protected:
    int nu, nv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
256
  public:
Martin Reinecke's avatar
Martin Reinecke committed
257
258
    int w;
    double beta;
Martin Reinecke's avatar
Martin Reinecke committed
259
  protected:
Martin Reinecke's avatar
Martin Reinecke committed
260
261
262
    int nsafe, su;
  public:
    int sv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
263

Martin Reinecke's avatar
Martin Reinecke committed
264
265
266
267
    vector<double> kernel;
    int iu0, iv0; // start index of the current visibility
    int bu0, bv0; // start index of the current buffer

Martin Reinecke's avatar
Martin Reinecke committed
268
    void NOINLINE update(double u_in, double v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
269
      {
270
      auto u = fmodulo(u_in, 1.)*nu;
Martin Reinecke's avatar
Martin Reinecke committed
271
272
      iu0 = int(u-w*0.5 + 1 + nu) - nu;
      if (iu0+w>nu+nsafe) iu0 = nu+nsafe-w;
273
      auto v = fmodulo(v_in, 1.)*nv;
Martin Reinecke's avatar
Martin Reinecke committed
274
275
276
277
      iv0 = int(v-w*0.5 + 1 + nv) - nv;
      if (iv0+w>nv+nsafe) iv0 = nv+nsafe-w;
      double xw=2./w;
      for (int i=0; i<w; ++i)
Martin Reinecke's avatar
import  
Martin Reinecke committed
278
        {
Martin Reinecke's avatar
Martin Reinecke committed
279
280
281
        kernel[i  ] = xw*(iu0+i-u);
        kernel[i+w] = xw*(iv0+i-v);
        }
Martin Reinecke's avatar
Martin Reinecke committed
282
283
      for (auto &k : kernel)
        k = exp(beta*sqrt(1.-k*k));
Martin Reinecke's avatar
import  
Martin Reinecke committed
284
285
      }

Martin Reinecke's avatar
Martin Reinecke committed
286
287
    bool need_to_move() const
      { return (iu0<bu0) || (iv0<bv0) || (iu0+w>bu0+su) || (iv0+w>bv0+sv); }
Martin Reinecke's avatar
import  
Martin Reinecke committed
288

Martin Reinecke's avatar
Martin Reinecke committed
289
    void update_position()
Martin Reinecke's avatar
import  
Martin Reinecke committed
290
      {
Martin Reinecke's avatar
Martin Reinecke committed
291
292
      bu0=max(-nsafe, min(nu+nsafe-su, iu0+nsafe-su/2));
      bv0=max(-nsafe, min(nv+nsafe-sv, iv0+nsafe-sv/2));
Martin Reinecke's avatar
import  
Martin Reinecke committed
293
294
      }

Martin Reinecke's avatar
Martin Reinecke committed
295
296
297
  protected:
    Helper(int nu_, int nv_, double epsilon)
      : nu(nu_), nv(nv_),
Martin Reinecke's avatar
Martin Reinecke committed
298
299
        w(get_w(epsilon)), beta(2.3*w), nsafe((w+1)/2),
        su(min(max(2*w,80), nu)), sv(min(max(2*w,80), nv)),
Martin Reinecke's avatar
Martin Reinecke committed
300
301
        kernel(2*w),
        bu0(-1000000), bv0(-1000000)
Martin Reinecke's avatar
import  
Martin Reinecke committed
302
      {
Martin Reinecke's avatar
Martin Reinecke committed
303
      if (min(nu,nv)<2*nsafe) throw runtime_error("grid dimensions too small");
Martin Reinecke's avatar
import  
Martin Reinecke committed
304
305
306
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
307
class WriteHelper: public Helper
Martin Reinecke's avatar
import  
Martin Reinecke committed
308
309
310
311
312
313
314
  {
  private:
    vector<complex<double>> data;
    complex<double> *grid;

    void dump()
      {
Martin Reinecke's avatar
Martin Reinecke committed
315
      if (bu0<-nsafe) return; // nothing written into buffer yet
Martin Reinecke's avatar
import  
Martin Reinecke committed
316
317
#pragma omp critical
{
Martin Reinecke's avatar
Martin Reinecke committed
318
319
320
      int idxu = (bu0+nu)%nu;
      int idxv0 = (bv0+nv)%nv;
      for (int iu=0; iu<su; ++iu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
321
322
        {
        int idxv = idxv0;
Martin Reinecke's avatar
Martin Reinecke committed
323
        for (int iv=0; iv<sv; ++iv)
Martin Reinecke's avatar
import  
Martin Reinecke committed
324
          {
Martin Reinecke's avatar
Martin Reinecke committed
325
          grid[idxu*nv + idxv] += data[iu*sv + iv];
Martin Reinecke's avatar
import  
Martin Reinecke committed
326
327
328
329
330
331
332
333
334
          if (++idxv>=nv) idxv=0;
          }
        if (++idxu>=nu) idxu=0;
        }
}
      }

  public:
    complex<double> *p0;
Martin Reinecke's avatar
Martin Reinecke committed
335
336
337
    WriteHelper(int nu_, int nv_, double epsilon, complex<double> *grid_)
      : Helper(nu_, nv_, epsilon), data(su*sv, 0.), grid(grid_) {}
    ~WriteHelper() { dump(); }
Martin Reinecke's avatar
import  
Martin Reinecke committed
338

Martin Reinecke's avatar
Martin Reinecke committed
339
    void prep_write(double u_in, double v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
340
      {
Martin Reinecke's avatar
Martin Reinecke committed
341
342
      update(u_in, v_in);
      if (need_to_move())
Martin Reinecke's avatar
import  
Martin Reinecke committed
343
344
        {
        dump();
Martin Reinecke's avatar
Martin Reinecke committed
345
        update_position();
Martin Reinecke's avatar
import  
Martin Reinecke committed
346
347
        fill(data.begin(), data.end(), 0.);
        }
Martin Reinecke's avatar
Martin Reinecke committed
348
      p0 = data.data() + sv*(iu0-bu0) + iv0-bv0;
Martin Reinecke's avatar
import  
Martin Reinecke committed
349
350
351
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
352
class ReadHelper: public Helper
Martin Reinecke's avatar
import  
Martin Reinecke committed
353
354
355
356
357
358
359
  {
  private:
    vector<complex<double>> data;
    const complex<double> *grid;

    void load()
      {
Martin Reinecke's avatar
Martin Reinecke committed
360
361
362
      int idxu = (bu0+nu)%nu;
      int idxv0 = (bv0+nv)%nv;
      for (int iu=0; iu<su; ++iu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
363
364
        {
        int idxv = idxv0;
Martin Reinecke's avatar
Martin Reinecke committed
365
        for (int iv=0; iv<sv; ++iv)
Martin Reinecke's avatar
import  
Martin Reinecke committed
366
          {
Martin Reinecke's avatar
Martin Reinecke committed
367
          data[iu*sv + iv] = grid[idxu*nv + idxv];
Martin Reinecke's avatar
import  
Martin Reinecke committed
368
369
370
371
372
373
374
375
          if (++idxv>=nv) idxv=0;
          }
        if (++idxu>=nu) idxu=0;
        }
      }

  public:
    const complex<double> *p0;
Martin Reinecke's avatar
Martin Reinecke committed
376
377
    ReadHelper(int nu_, int nv_, double epsilon, const complex<double> *grid_)
      : Helper(nu_, nv_, epsilon), data(su*sv,0.), grid(grid_), p0(nullptr) {}
Martin Reinecke's avatar
import  
Martin Reinecke committed
378

Martin Reinecke's avatar
Martin Reinecke committed
379
    void prep_read(double u_in, double v_in)
Martin Reinecke's avatar
import  
Martin Reinecke committed
380
      {
Martin Reinecke's avatar
Martin Reinecke committed
381
382
      update(u_in, v_in);
      if (need_to_move())
Martin Reinecke's avatar
import  
Martin Reinecke committed
383
        {
Martin Reinecke's avatar
Martin Reinecke committed
384
        update_position();
Martin Reinecke's avatar
import  
Martin Reinecke committed
385
386
        load();
        }
Martin Reinecke's avatar
Martin Reinecke committed
387
      p0 = data.data() + sv*(iu0-bu0) + iv0-bv0;
Martin Reinecke's avatar
import  
Martin Reinecke committed
388
389
390
      }
  };

Martin Reinecke's avatar
Martin Reinecke committed
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
a_d_c complex2hartley (const a_c_c &grid_)
  {
  myassert(grid_.ndim()==2, "grid array must be 2D");
  int nu = grid_.shape(0), nv = grid_.shape(1);
  auto grid = grid_.data();

  int odim[] = {nu,nv};
  a_d_c res(odim);
  auto grid2 = res.mutable_data();
  for (int u=0; u<nu; ++u)
    {
    int xu = (u==0) ? 0 : nu-u;
    for (int v=0; v<nv; ++v)
      {
      int xv = (v==0) ? 0 : nv-v;
      int i1 = u*nv+v;
      int i2 = xu*nv+xv;
      grid2[i1] = 0.5*(grid[i1].real()+grid[i1].imag()+
                       grid[i2].real()-grid[i2].imag());
      }
    }
  return res;
  }

a_d_c to_grid (const a_d_c &uv_, const a_c_c &vis_,
Martin Reinecke's avatar
Martin Reinecke committed
416
               int nu, int nv, double epsilon)
Martin Reinecke's avatar
import  
Martin Reinecke committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  {
  myassert(uv_.ndim()==2, "uv array must be 2D");
  myassert(uv_.shape(1)==2, "uv.shape[1] must be 2");
  int nvis = uv_.shape(0);
  myassert(vis_.ndim()==1, "vis array must be 1D");
  myassert(vis_.shape(0)==nvis, "array size mismatch");
  auto uv = uv_.data();
  auto vis = vis_.data();

  int odim[] = {nu,nv};
  a_c_c res(odim);
  auto grid = res.mutable_data();
  for (int i=0; i<nu*nv; ++i) grid[i] = 0.;

#pragma omp parallel
{
Martin Reinecke's avatar
Martin Reinecke committed
433
  WriteHelper hlp(nu, nv, epsilon, grid);
Martin Reinecke's avatar
Martin Reinecke committed
434
  double emb = exp(-2*hlp.beta);
Martin Reinecke's avatar
import  
Martin Reinecke committed
435
436
437
438
439

  // Loop over sampling points
#pragma omp for schedule(dynamic,10000)
  for (int ipart=0; ipart<nvis; ++ipart)
    {
Martin Reinecke's avatar
Martin Reinecke committed
440
    hlp.prep_write(uv[2*ipart], uv[2*ipart+1]);
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
441
442
    auto * RESTRICT ptr = hlp.p0;
    int w = hlp.w;
Martin Reinecke's avatar
Martin Reinecke committed
443
    auto v = vis[ipart]*emb;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
444
445
446
    const double * RESTRICT ku = hlp.kernel.data();
    const double * RESTRICT kv = hlp.kernel.data()+hlp.w;
    for (int cu=0; cu<w; ++cu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
447
      {
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
448
449
450
451
      complex<double> tmp = v*ku[cu];
      for (int cv=0; cv<w; ++cv)
        ptr[cv] += tmp*kv[cv];
      ptr+=hlp.sv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
452
453
454
      }
    }
} // end of parallel region
Martin Reinecke's avatar
Martin Reinecke committed
455
  return complex2hartley(res);
Martin Reinecke's avatar
import  
Martin Reinecke committed
456
457
  }

Martin Reinecke's avatar
Martin Reinecke committed
458
a_c_c hartley2complex (const a_d_c &grid_)
Martin Reinecke's avatar
Martin Reinecke committed
459
460
461
462
463
464
  {
  myassert(grid_.ndim()==2, "grid array must be 2D");
  int nu = grid_.shape(0), nv = grid_.shape(1);
  auto grid = grid_.data();

  int odim[] = {nu,nv};
Martin Reinecke's avatar
Martin Reinecke committed
465
  a_c_c res(odim);
Martin Reinecke's avatar
Martin Reinecke committed
466
467
468
469
470
471
472
473
474
  auto grid2 = res.mutable_data();
  for (int u=0; u<nu; ++u)
    {
    int xu = (u==0) ? 0 : nu-u;
    for (int v=0; v<nv; ++v)
      {
      int xv = (v==0) ? 0 : nv-v;
      int i1 = u*nv+v;
      int i2 = xu*nv+xv;
Martin Reinecke's avatar
Martin Reinecke committed
475
476
477
      double v1 = 0.5*grid[i1];
      double v2 = 0.5*grid[i2];
      grid2[i1] = complex<double>(v1+v2, v1-v2);
Martin Reinecke's avatar
Martin Reinecke committed
478
479
480
481
482
      }
    }
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
483
a_c_c from_grid (const a_d_c &uv_, const a_d_c &grid0_, double epsilon)
Martin Reinecke's avatar
import  
Martin Reinecke committed
484
485
486
  {
  myassert(uv_.ndim()==2, "uv array must be 2D");
  myassert(uv_.shape(1)==2, "uv.shape[1] must be 2");
Martin Reinecke's avatar
Martin Reinecke committed
487
488
  myassert(grid0_.ndim()==2, "grid array must be 2D");
  auto grid_ = hartley2complex(grid0_);
Martin Reinecke's avatar
import  
Martin Reinecke committed
489
490
491
492
  int nvis = uv_.shape(0);
  auto uv = uv_.data();
  auto grid = grid_.data();

Martin Reinecke's avatar
Martin Reinecke committed
493
494
  int nu=grid_.shape(0),
      nv=grid_.shape(1);
Martin Reinecke's avatar
import  
Martin Reinecke committed
495
496
497
498
499
500
501
502

  int odim[] = {nvis};
  a_c_c res(odim);
  auto vis = res.mutable_data();

  // Loop over sampling points
#pragma omp parallel
{
Martin Reinecke's avatar
Martin Reinecke committed
503
  ReadHelper hlp(nu, nv, epsilon, grid);
Martin Reinecke's avatar
Martin Reinecke committed
504
  double emb = exp(-2*hlp.beta);
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
505

Martin Reinecke's avatar
import  
Martin Reinecke committed
506
507
508
#pragma omp for schedule(dynamic,10000)
  for (int ipart=0; ipart<nvis; ++ipart)
    {
Martin Reinecke's avatar
Martin Reinecke committed
509
    hlp.prep_read(uv[2*ipart], uv[2*ipart+1]);
Martin Reinecke's avatar
import  
Martin Reinecke committed
510
    complex<double> r = 0.;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
511
512
513
514
515
    auto * RESTRICT ptr = hlp.p0;
    int w = hlp.w;
    const double * RESTRICT ku = hlp.kernel.data();
    const double * RESTRICT kv = hlp.kernel.data()+hlp.w;
    for (int cu=0; cu<w; ++cu)
Martin Reinecke's avatar
import  
Martin Reinecke committed
516
517
      {
      complex<double> tmp = 0.;
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
518
519
520
521
      for (int cv=0; cv<w; ++cv)
        tmp += ptr[cv] * kv[cv];
      r += tmp*ku[cu];
      ptr += hlp.sv;
Martin Reinecke's avatar
import  
Martin Reinecke committed
522
      }
Martin Reinecke's avatar
Martin Reinecke committed
523
    vis[ipart] = r*emb;
Martin Reinecke's avatar
import  
Martin Reinecke committed
524
525
526
527
528
    }
}
  return res;
  }

Martin Reinecke's avatar
Martin Reinecke committed
529
530
a_d_c correction_factors (size_t n, size_t nval, double epsilon)
  {
Martin Reinecke's avatar
Martin Reinecke committed
531
  auto w = get_w(epsilon);
Martin Reinecke's avatar
Martin Reinecke committed
532
533
534
535
536
537
538
539
  auto beta = 2.3*w;
  auto p = int(1.5*w+2);
  double alpha = pi*w/n;
  vector<double> x, wgt;
  legendre_prep(2*p,x,wgt);
  auto psi = x;
  for (auto &v:psi)
    v = exp(beta*(sqrt(1-v*v)-1.));
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
540
  int odim[] = {int(nval)};
Martin Reinecke's avatar
Martin Reinecke committed
541
542
543
544
545
546
547
548
549
550
551
552
  a_d_c res(odim);
  auto val = res.mutable_data();
  for (size_t k=0; k<nval; ++k)
    {
    double tmp=0;
    for (int i=0; i<p; ++i)
      tmp += wgt[i]*psi[i]*cos(alpha*k*x[i]);
    val[k] = 1./(w*tmp);
    }
  return res;
  }

Martin Reinecke's avatar
import  
Martin Reinecke committed
553
554
555
556
557
} // unnamed namespace

PYBIND11_MODULE(nifty_gridder, m)
  {
  m.def("peanoindex",&peanoindex);
Martin Reinecke's avatar
Martin Reinecke committed
558
  m.def("get_w",&get_w);
Martin Reinecke's avatar
import  
Martin Reinecke committed
559
560
  m.def("to_grid",&to_grid);
  m.def("from_grid",&from_grid);
Martin Reinecke's avatar
Martin Reinecke committed
561
  m.def("correction_factors",&correction_factors);
Martin Reinecke's avatar
import  
Martin Reinecke committed
562
  }