src/grid/cartesian1D.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
    
    #define GRIDNAME "Cartesian 1D"
    #define dimension 1
    #define GHOSTS 1
    
    #define _I     (point.i - 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)
    
    @define data(k,l,m) ((double *)&cartesian->d[(point.i + k)*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;
        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;
        POINT_VARIABLES
    @
    @define end_foreach_face_generic() }}
    
    @def foreach_vertex()
    foreach_face_generic() {
      x -= Delta/2.;
    @
    @define end_foreach_vertex() } end_foreach_face_generic()
    
    @define is_face_x() (true)
    @define is_face_y() (point.i <= point.n)
    
    // ghost cell coordinates for each direction
    static int _ig[] = {1,-1};
    
    // Box boundaries
    
    static void box_boundary_level_normal (const Boundary * b, scalar * list, int l)
    {
      if (!list)
        return;
    
      int d = ((BoxBoundary *)b)->d;
    
      Point point;
      point.n = cartesian->n;
      ig = _ig[d];
      assert (d <= left);
      point.i = d == right ? point.n + GHOSTS : GHOSTS;
      Point neighbor = {point.i + ig};
      for (scalar s in list) {
        scalar b = s.v.x;
        val(s,ig) = b.boundary[d] (point, neighbor, s, NULL);
      }
    }
    
    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;
    
      int component = d/2;
      for (scalar s in list)
        if (!is_constant(s) && s.boundary[d] != periodic_bc) {
          if (s.face) {
    	if ((&s.d.x)[component]) {
    	  scalar b = s.v.x;
    	  if (b.boundary[d])
    	    normal = list_add (normal, s);
    	}
          }	
          else if (s.boundary[d])
    	centered = list_add (centered, s);
        }
    
      if (centered) {
        Point point;
        point.n = cartesian->n;
        ig = _ig[d];
        point.i = d == right ? point.n + GHOSTS - 1 : GHOSTS;
        Point neighbor = {point.i + ig};
        for (scalar s in centered)
          val(s,ig) = s.boundary[d] (point, neighbor, s, NULL);
        free (centered);
      }
    
      box_boundary_level_normal (b, normal, l);
      free (normal);
    }
    
    // periodic boundaries
    
    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.boundary[right] == periodic_bc)
          list1 = list_add (list1, s);
      if (!list1)
        return;
    
      Point point = *((Point *)grid);
      point.i = 0, point.n = N;
      for (int i = 0; i < GHOSTS; i++)
        for (scalar s in list1)
          s[i] = s[i + point.n];
      for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
        for (scalar s in list1)
          s[i] = s[i - point.n];
    
      free (list1);
    }
    
    void free_grid (void)
    {
      if (!grid)
        return;
      free_boundaries();
      free (cartesian->d);
      free (cartesian);
      grid = NULL;
    }
    
    @if TRASH
    @ undef trash
    @ define trash(list) reset(list, undefined)
    @endif
    
    void reset (void * alist, double val)
    {
      scalar * list = (scalar *) alist;
      char * data = cartesian->d;
      for (int i = 0; i < cartesian->n + 2; i++, data += datasize) {
        double * v = (double *) data;
        for (scalar s in list)
          if (!is_constant(s))
    	v[s.i] = val;
      }
    }
    
    void init_grid (int n)
    {
      if (cartesian && n == cartesian->n)
        return;
      free_grid();
      Cartesian * p = qmalloc (1, Cartesian);
      size_t len = (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.);
      // box boundaries
      for (int d = 0; d < 2; d++) {
        BoxBoundary * box = qcalloc (1, BoxBoundary);
        box->d = d;
        Boundary * b = (Boundary *) box;
        b->level   = box_boundary_level;
        add_boundary (b);
      }
      // periodic boundaries
      Boundary * b = qcalloc (1, Boundary);
      b->level = periodic_boundary_level_x;
      add_boundary (b);
      // mesh size
      grid->n = grid->tn = n;
    }
    
    void realloc_scalar (int size)
    {
      Cartesian * p = cartesian;
      size_t len = (p->n + 2);
      qrealloc (p->d, len*(datasize + size), char);
      char * data = p->d + (len - 1)*datasize;
      for (int i = p->n + 1; i > 0; i--, data -= datasize)
        memmove (data + i*size, data, datasize);
      datasize += size;
    }
    
    struct _locate { double x, y, z; };
    
    Point locate (struct _locate p)
    {
      Point point;
      point.n = cartesian->n;
      double a = (p.x - X0)/L0*point.n;
      point.i = a + 1;
      point.level = (a > -0.5 && a < point.n + 0.5) ? 0 : - 1;
      return point;
    }
    
    #include "cartesian-common.h"
    
    void cartesian1D_methods()
    {
      cartesian_methods();
    }