aboutsummaryrefslogtreecommitdiff
path: root/CarpetDev/CarpetPETSc/src/solve.cc
blob: 64778e7a05cefa408b467288c4a32879f2eb6593 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <cassert>
#include <vector>

#include "cctk.h"
#include "cctk_Parameters.h"

#include "util_ErrorCodes.h"
#include "util_Table.h"

#include "carpet.hh"
#include "TATelliptic.h"

#include "dist.hh"

#include <petscvec.h>



#ifdef CCTK_CXX_RESTRICT
#  define restrict CCTK_CXX_RESTRICT
#endif



namespace CarpetPETSc {
  
  using namespace std;
  using namespace Carpet;
  
  
  
  char const * const solver_name = "CarpetPETSc";
  
  
  
  struct options_t {
    
    // Width of outer boundary
    CCTK_INT bndwidth;
    
    // Symmetry information
    CCTK_INT symmetry_handle[2*dim];
    CCTK_INT symmetry_zone_width[2*dim];
  };
  
  
  
  void
  copy_in (cGH const * restrict const cctkGH,
           Vec dst,
           vector<CCTK_INT> const & src,
           options_t const & options);
  
  void
  copy_out (cGH const * restrict const cctkGH,
            vector<CCTK_INT> const & dst,
            Vec const src,
            options_t const & options);
  
  
  
  int
  solve (cGH const * const cctkGH,
         int const * const var,
         int const * const val,
         int const nvars,
         int const options_table,
         int (* const fun) (cGH const * cctkGH,
                            int options_table,
                            void * data),
         int (* const bnd) (cGH const * cctkGH,
                            int options_table,
                            void * data),
         void * const data)
  {
    DECLARE_CCTK_PARAMETERS;
  }
  
  
  
  void
  copy_in (cGH const * restrict const cctkGH,
           Vec dst,
           vector<CCTK_INT> const & src,
           options_t const & options)
  {
    double * restrict dstptr;
    VecGetArray (dst, & dstptr);
    PetscInt dstsize;
    VecGetSize (dst, & dstsize);
    
    int dstind = 0;
    
    BEGIN_MAP_LOOP (cctkGH, CCTK_GF) {
      BEGIN_LOCAL_COMPONENT_LOOP (cctkGH, CCTK_GF) {
        DECLARE_CCTK_ARGUMENTS;
        
        vector <CCTK_REAL const *> srcptrs (src.size());
        for (int n = 0; n < src.size(); ++ n) {
          srcptrs.at(n) =
            static_cast<CCTK_REAL const *>
            (CCTK_VarDataPtrI (cctkGH, 0, src.at(n)));
          assert (srcptrs.at(n));
        }
        
        int imin[3], imax[3];
        interior_shape (cctkGH, options, imin, imax);
        for (int k=imin[2]; k<imax[2]; ++k) {
          for (int j=imin[1]; j<imax[1]; ++j) {
            for (int i=imin[0]; i<imax[0]; ++i) {
              int const srcind = CCTK_GFINDEX3D (cctkGH, i, j, k);
              
              for (int n = 0; n < src.size(); ++ n) {
                assert (dstind < dstsize);
                dstptr[dstind] = srcptr[srcind];
                ++ dstind;
              }
              
            }
          }
        }
        
      } END_LOCAL_COMPONENT_LOOP;
    } END_MAP_LOOP;
  }
  
  
  
  void
  copy_out (cGH const * restrict const cctkGH,
            vector<CCTK_INT> const & dst,
            Vec const src,
            options_t const & options)
  {
    double const * restrict srcptr;
    VecGetArray (src, & srcptr);
    PetscInt srcsize;
    VecGetSize (dst, & srcsize);
    
    int srcind = 0;
    
    BEGIN_MAP_LOOP (cctkGH, CCTK_GF) {
      BEGIN_LOCAL_COMPONENT_LOOP (cctkGH, CCTK_GF) {
        DECLARE_CCTK_ARGUMENTS;
        
        vector <CCTK_REAL *> dstptrs (dst.size());
        for (int n = 0; n < dst.size(); ++ n) {
          dstptrs.at(n) =
            static_cast<CCTK_REAL *> (CCTK_VarDataPtrI (cctkGH, 0, dst.at(n)));
          assert (dstptrs.at(n));
        }
        
        int imin[3], imax[3];
        interior_shape (cctkGH, options, imin, imax);
        for (int k=imin[2]; k<imax[2]; ++k) {
          for (int j=imin[1]; j<imax[1]; ++j) {
            for (int i=imin[0]; i<imax[0]; ++i) {
              int const dstind = CCTK_GFINDEX3D (cctkGH, i, j, k);
              
              for (int n = 0; n < src.size(); ++ n) {
                assert (srcind < srcsize);
                dstptr[dstind] = srcptr[srcind];
                ++ srcind;
              }
              
            }
          }
        }
        
      } END_LOCAL_COMPONENT_LOOP;
    } END_MAP_LOOP;
  }
  
  
  
  // Calculate the shape of the interior
  void
  interior_shape (cGH const * restrict const cctkGH,
                  options_t const & options,
                  int * restrict const imin,
                  int * restrict const imax)
  {
    DECLARE_CCTK_ARGUMENTS;
    
    for (int d=0; d<dim; ++d) {
      
      imin[d] = 0;
      if (cctk_bbox[2*d]) {
        if (options.symmetry_handle[2*d] >= 0) {
          imin[d] += options.symmetry_zone_width[2*d];
        } else {
          imin[d] += options.bndwidth;
        }
      } else {
        imin[d] += cctk_nghostzones[d];
      }
      
      imax[d] = cctk_lsh[d];
      if (cctk_bbox[2*d+1]) {
        if (options.symmetry_handle[2*d+1] >= 0) {
          imax[d] -= options.symmetry_zone_width[2*d+1];
        } else {
          imax[d] -= options.bndwidth;
        }
      } else {
        imax[d] -= cctk_nghostzones[d];
      }
      
      assert (imin[d] <= imax[d]);
      
    } // for d
  }
  
  
  
} // namespace CarpetPETSc