aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/interpolate_3d_2tl.cc
blob: 27b290733184002f06807b7b958f49972982dbae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
109
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
146
147
148
149
150
151
152
153
154
155
156
157
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
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdlib>

#include <cctk.h>
#include <cctk_Parameters.h>

#include "operator_prototypes.hh"
#include "typeprops.hh"

using namespace std;



namespace CarpetLib {


  
#define SRCIND3(i,j,k)                                  \
  index3 (srcioff + (i), srcjoff + (j), srckoff + (k),  \
          srciext, srcjext, srckext)
#define DSTIND3(i,j,k)                                  \
  index3 (dstioff + (i), dstjoff + (j), dstkoff + (k),  \
          dstiext, dstjext, dstkext)
  
  
  
  template <typename T>
  void
  interpolate_3d_2tl (T const * restrict const src1,
                      CCTK_REAL const t1,
                      T const * restrict const src2,
                      CCTK_REAL const t2,
                      ivect3 const & srcext,
                      T * restrict const dst,
                      CCTK_REAL const t,
                      ivect3 const & dstext,
                      ibbox3 const & srcbbox,
                      ibbox3 const & dstbbox,
                      ibbox3 const & regbbox)
  {
    typedef typename typeprops<T>::real RT;
    
    
    
#if 0
    // This is already guaranteed by bbox
    if (any (srcbbox.stride() == 0 or
             dstbbox.stride() == 0 or
             regbbox.stride() == 0))
    {
      CCTK_WARN (0, "Internal error: stride is zero");
    }
#endif
    
    if (any (srcbbox.stride() != regbbox.stride() or
             dstbbox.stride() != regbbox.stride()))
    {
      CCTK_WARN (0, "Internal error: strides disagree");
    }
    
    if (any (srcbbox.stride() != dstbbox.stride())) {
      CCTK_WARN (0, "Internal error: strides disagree");
    }
    
#if 0
    // This needs to be allowed for cell centring
    if (any (srcbbox.lower() % srcbbox.stride() != 0 or
             dstbbox.lower() % dstbbox.stride() != 0 or
             regbbox.lower() % regbbox.stride() != 0))
    {
      CCTK_WARN (0, "Internal error: array origins are not integer multiples of the strides");
    }
#endif
    
    // This could be handled, but is likely to point to an error
    // elsewhere
    if (regbbox.empty()) {
      CCTK_WARN (0, "Internal error: region extent is empty");
    }
    
#if 0
    // This is already guaranteed by bbox
    if (any ((srcbbox.upper() - srcbbox.lower()) % srcbbox.stride() != 0 or
             (dstbbox.upper() - dstbbox.lower()) % dstbbox.stride() != 0 or
             (regbbox.upper() - regbbox.lower()) % regbbox.stride() != 0))
    {
      CCTK_WARN (0, "Internal error: array extents are not integer multiples of the strides");
    }
#endif
    
    if (not regbbox.is_contained_in(srcbbox) or
        not regbbox.is_contained_in(dstbbox))
    {
      CCTK_WARN (0, "Internal error: region extent is not contained in array extent");
    }
    
    if (any (srcext != srcbbox.shape() / srcbbox.stride() or
             dstext != dstbbox.shape() / dstbbox.stride()))
    {
      CCTK_WARN (0, "Internal error: array sizes don't agree with bounding boxes");
    }
    
    
    
    ivect3 const regext = regbbox.shape() / regbbox.stride();
    assert (all ((regbbox.lower() - srcbbox.lower()) % srcbbox.stride() == 0));
    ivect3 const srcoff = (regbbox.lower() - srcbbox.lower()) / srcbbox.stride();
    assert (all ((regbbox.lower() - dstbbox.lower()) % dstbbox.stride() == 0));
    ivect3 const dstoff = (regbbox.lower() - dstbbox.lower()) / dstbbox.stride();
    
    
    
    size_t const srciext = srcext[0];
    size_t const srcjext = srcext[1];
    size_t const srckext = srcext[2];
    
    size_t const dstiext = dstext[0];
    size_t const dstjext = dstext[1];
    size_t const dstkext = dstext[2];
    
    size_t const regiext = regext[0];
    size_t const regjext = regext[1];
    size_t const regkext = regext[2];
    
    size_t const srcioff = srcoff[0];
    size_t const srcjoff = srcoff[1];
    size_t const srckoff = srcoff[2];
    
    size_t const dstioff = dstoff[0];
    size_t const dstjoff = dstoff[1];
    size_t const dstkoff = dstoff[2];
    
    
    
    // Linear (first order) interpolation
    
    RT const eps = 1.0e-10;
    if (abs (t1 - t2) < eps) {
      CCTK_WARN (0, "Internal error: arrays have same time");
    }
    if (t < min (t1, t2) - eps or t > max (t1, t2) + eps) {
      CCTK_WARN (0, "Internal error: extrapolation in time");
    }
    
    RT const s1fac = (t - t2) / (t1 - t2);
    RT const s2fac = (t - t1) / (t2 - t1);
    
    
    
    // Loop over region
    for (size_t k=0; k<regkext; ++k) {
      for (size_t j=0; j<regjext; ++j) {
        for (size_t i=0; i<regiext; ++i) {
          
          dst [DSTIND3(i, j, k)] =
            + s1fac * src1 [SRCIND3(i, j, k)]
            + s2fac * src2 [SRCIND3(i, j, k)];
          
        }
      }
    }
    
  }
  
  
  
  template
  void
  interpolate_3d_2tl (CCTK_REAL const * restrict const src1,
                      CCTK_REAL const t1,
                      CCTK_REAL const * restrict const src2,
                      CCTK_REAL const t2,
                      ivect3 const & srcext,
                      CCTK_REAL * restrict const dst,
                      CCTK_REAL const t,
                      ivect3 const & dstext,
                      ibbox3 const & srcbbox,
                      ibbox3 const & dstbbox,
                      ibbox3 const & regbbox);
  
  template
  void
  interpolate_3d_2tl (CCTK_COMPLEX const * restrict const src1,
                      CCTK_REAL const t1,
                      CCTK_COMPLEX const * restrict const src2,
                      CCTK_REAL const t2,
                      ivect3 const & srcext,
                      CCTK_COMPLEX * restrict const dst,
                      CCTK_REAL const t,
                      ivect3 const & dstext,
                      ibbox3 const & srcbbox,
                      ibbox3 const & dstbbox,
                      ibbox3 const & regbbox);
  
  
  
} // namespace CarpetLib