src/grid/cartesian.h

    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
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    
    #define GRIDNAME "Cartesian"
    #define dimension 2
    #define GHOSTS 1
    
    #define _I     (point.i - 1)
    #define _J     (point.j - 1)
    #define _DELTA (1./point.n)
    
    typedef struct {
      Grid g;
      char * d;
      int n;
    } Cartesian;
    
    struct _Point {
      int i, j, level, n;
    };
    static Point last_point;
    
    #define cartesian ((Cartesian *)grid)
    
    @def data(k,l,m) ((double *)&cartesian->d[((point.i + k)*(point.n + 2) +
    					 (point.j + l))*datasize]) @
    @define allocated(...) true
    
    @define POINT_VARIABLES VARIABLES
    
    @def foreach()
      OMP_PARALLEL() {
      int ig = 0, jg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg);
      Point point;
      point.n = cartesian->n;
      int _k;
      OMP(omp for schedule(static))
      for (_k = 1; _k <= point.n; _k++) {
        point.i = _k;
        for (point.j = 1; point.j <= point.n; point.j++) {
          POINT_VARIABLES
    @
    @define end_foreach() }}}
    
    @def foreach_face_generic()
      OMP_PARALLEL() {
      int ig = 0, jg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg);
      Point point;
      point.n = cartesian->n;
      int _k;
      OMP(omp for schedule(static))
      for (_k = 1; _k <= point.n + 1; _k++) {
        point.i = _k;
        for (point.j = 1; point.j <= point.n + 1; point.j++) {
          POINT_VARIABLES
    @
    @define end_foreach_face_generic() }}}
    
    @def foreach_vertex()
    foreach_face_generic() {
      x -= Delta/2.; y -= Delta/2.;
    @
    @define end_foreach_vertex() } end_foreach_face_generic()
    
    #define foreach_edge() foreach_face(y,x)
    
    @define is_face_x() { int ig = -1; VARIABLES; if (point.j <= point.n) {
    @define end_is_face_x() }}
    @define is_face_y() { int jg = -1; VARIABLES; if (point.i <= point.n) {
    @define end_is_face_y() }}
    
    @if TRASH
    @ undef trash
    @ define trash(list) reset(list, undefined)
    @endif
    
    #include "neighbors.h"
    
    void reset (void * alist, double val)
    {
      scalar * list = (scalar *) alist;
      for (int i = 0; i < sq(cartesian->n + 2); i++)
        for (scalar s in list)
          if (!is_constant(s))
    	((double *)(&cartesian->d[i*datasize]))[s.i] = val;
    }
    
    // Boundaries
    
    @def foreach_boundary_dir(l,d)
      OMP_PARALLEL() {
      int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
      Point point;
      point.n = cartesian->n;
      int * _i = &point.j;
      if (d == left) {
        point.i = GHOSTS;
        ig = -1;
      }
      else if (d == right) {
        point.i = point.n + GHOSTS - 1;
        ig = 1;
      }
      else if (d == bottom) {
        point.j = GHOSTS;
        _i = &point.i;
        jg = -1;
      }
      else if (d == top) {
        point.j = point.n + GHOSTS - 1;
        _i = &point.i;
        jg = 1;
      }
      int _l;
      OMP(omp for schedule(static))
      for (_l = 0; _l < point.n + 2*GHOSTS; _l++) {
        *_i = _l;
        {
          POINT_VARIABLES
    @
    @def end_foreach_boundary_dir()
        }
      }
      }
    @
    
    @define neighbor(o,p,q) ((Point){point.i+o, point.j+p, point.level, point.n})
    @def is_boundary(point) (point.i < GHOSTS || point.i >= point.n + GHOSTS ||
    			 point.j < GHOSTS || point.j >= point.n + GHOSTS)
    @
    
    // ghost cell coordinates for each direction
    static int _ig[] = {1,-1,0,0}, _jg[] = {0,0,1,-1};
    
    static void box_boundary_level_normal (const Boundary * b, scalar * list, int l)
    {
      int d = ((BoxBoundary *)b)->d;
    
      OMP_PARALLEL() {
        Point point;
        point.n = cartesian->n;
        if (d % 2)
          ig = jg = 0;
        else {
          ig = _ig[d]; jg = _jg[d];
        }
        int _start = GHOSTS, _end = point.n + GHOSTS, _k;  
        OMP(omp for schedule(static))
          for (_k = _start; _k < _end; _k++) {
    	point.i = d > left ? _k : d == right ? point.n + GHOSTS - 1 : GHOSTS;
    	point.j = d < top  ? _k : d == top   ? point.n + GHOSTS - 1 : GHOSTS;
    	Point neighbor = {point.i + ig, point.j + jg};
    	for (scalar s in list) {
    	  scalar b = s.v.x;
    	  val(s,ig,jg) = b.boundary[d] (point, neighbor, s, NULL);
    	}
          }
      }
    }
    
    static void box_boundary_level_tangent (const Boundary * b, 
    					scalar * list, int l)
    {
      int d = ((BoxBoundary *)b)->d;
    
      OMP_PARALLEL() {
        Point point;
        point.n = cartesian->n;
        ig = _ig[d]; jg = _jg[d];
        int _start = GHOSTS, _end = point.n + 2*GHOSTS, _k;
    
        OMP(omp for schedule(static))
          for (_k = _start; _k < _end; _k++) {
    	point.i = d > left ? _k : d == right ? point.n + GHOSTS - 1 : GHOSTS;
    	point.j = d < top  ? _k : d == top   ? point.n + GHOSTS - 1 : GHOSTS;
    	Point neighbor = {point.i + ig, point.j + jg};
    	for (scalar s in list) {
    	  scalar b = s.v.y;
    	  val(s,ig,jg) = b.boundary[d] (point, neighbor, s, NULL);
    	}
          }
      }
    }
    
    @def foreach_boundary(b)
      if (default_scalar_bc[b] != periodic_bc)
        foreach_boundary_dir (depth(), b)
          if (!is_boundary(point)) {
    @
    @define end_foreach_boundary() } end_foreach_boundary_dir()
    
    static double periodic_bc (Point point, Point neighbor, scalar s, void * data);
    
    static void box_boundary_level (const Boundary * b, scalar * list, int l)
    {
      int d = ((BoxBoundary *)b)->d;
      scalar * centered = NULL, * normal = NULL, * tangent = NULL;
    
      int component = d/2;
      for (scalar s in list)
        if (!is_constant(s)) {
          if (s.face) {
    	if ((&s.d.x)[component]) {
    	  scalar b = s.v.x;
    	  if (b.boundary[d] && b.boundary[d] != periodic_bc)
    	    normal = list_add (normal, s);
    	}
    	else {
    	  scalar b = s.v.y;
    	  if (b.boundary[d] && b.boundary[d] != periodic_bc)
    	    tangent = list_add (tangent, s);
    	}
          }	
          else if (s.boundary[d] && s.boundary[d] != periodic_bc)
    	centered = list_add (centered, s);
        }
    
      OMP_PARALLEL() {
        Point point;
        point.n = cartesian->n;
        ig = _ig[d]; jg = _jg[d];
        int _start = 1, _end = point.n, _k;
        /* traverse corners only for top and bottom */
        if (d > left) { _start--; _end++; }
        OMP(omp for schedule(static))
          for (_k = _start; _k <= _end; _k++) {
    	point.i = d > left ? _k : d == right ? point.n : 1;
    	point.j = d < top  ? _k : d == top   ? point.n : 1;
    	Point neighbor = {point.i + ig, point.j + jg};
    	for (scalar s in centered) {
    	  scalar b = (s.v.x.i < 0 ? s :
    		      s.i == s.v.x.i && d < top ? s.v.x :
    		      s.i == s.v.y.i && d >= top ? s.v.x :
    		      s.v.y);
    	  val(s,ig,jg) = b.boundary[d] (point, neighbor, s, NULL);
    	}
          }
      }
      free (centered);
    
      box_boundary_level_normal (b, normal, l);
      free (normal);
      box_boundary_level_tangent (b, tangent, l);
      free (tangent);
    }
    
    /* Periodic boundaries */
    
    #if !_MPI
    
    @define VT _attribute[s.i].v.y
    
    foreach_dimension()
    static void periodic_boundary_level_x (const Boundary * b, scalar * list, int l)
    {
      scalar * list1 = NULL;
      for (scalar s in list)
        if (!is_constant(s) && s.block > 0) {
          if (s.face) {
    	scalar vt = VT;
    	if (vt.boundary[right] == periodic_bc)
    	  list1 = list_add (list1, s);
          }
          else if (s.boundary[right] == periodic_bc)
    	list1 = list_add (list1, s);
        }
      if (!list1)
        return;
    
      OMP_PARALLEL() {
        Point point = {0};
        point.n = cartesian->n;
        int j;
        OMP(omp for schedule(static))
          for (j = 0; j < point.n + 2*GHOSTS; j++) {
    	for (int i = 0; i < GHOSTS; i++)
    	  for (scalar s in list1)
    	    memcpy (&s[i,j], &s[i + point.n,j], s.block*sizeof(double));
    	for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
    	  for (scalar s in list1)
    	    memcpy (&s[i,j], &s[i - point.n,j], s.block*sizeof(double));
          }
      }
      free (list1);
    }
    
    @undef VT
    
    #endif // !_MPI
    
    void free_grid (void)
    {
      if (!grid)
        return;
      free_boundaries();
      free (cartesian->d);
      free (cartesian);
      grid = NULL;
    }
    
    void init_grid (int n)
    {
      if (cartesian && n == cartesian->n)
        return;
      free_grid();
      Cartesian * p = qmalloc (1, Cartesian);
      size_t len = (n + 2)*(n + 2)*datasize;
      p->n = N = n;
      p->d = qmalloc (len, char);
      /* trash the data just to make sure it's either explicitly
         initialised or never touched */
      double * v = (double *) p->d;
      for (int i = 0; i < len/sizeof(double); i++)
        v[i] = undefined;
      grid = (Grid *) p;
      reset (all, 0.);
      for (int d = 0; d < nboundary; d++) {
        BoxBoundary * box = qcalloc (1, BoxBoundary);
        box->d = d;
        Boundary * b = (Boundary *) box;
        b->level   = box_boundary_level;
        add_boundary (b);
      }
      // periodic boundaries
      foreach_dimension() {
        Boundary * b = qcalloc (1, Boundary);
        b->level = periodic_boundary_level_x;
        add_boundary (b);
      }
      // mesh size
      grid->n = grid->tn = sq(n);
    }
    
    void realloc_scalar (int size)
    {
      Cartesian * p = cartesian;
      size_t len = (p->n + 2)*(p->n + 2);
      qrealloc (p->d, len*(datasize + size), char);
      char * data = p->d + (len - 1)*datasize;
      for (int i = len - 1; i > 0; i--, data -= datasize)
        memmove (data + i*size, data, datasize);
      datasize += size;
    }
    
    Point locate (double xp = 0, double yp = 0, double zp = 0)
    {
      Point point;
      point.n = cartesian->n;
      point.i = (xp - X0)/L0*point.n + 1;
      point.j = (yp - Y0)/L0*point.n + 1;
      point.level = (point.i >= 1 && point.i <= point.n &&
    		 point.j >= 1 && point.j <= point.n) ? 0 : - 1;
      return point;
    }
    
    #include "cartesian-common.h"