domain_box.c 7.11 KB
Newer Older
Rainer Weinberger's avatar
Rainer Weinberger committed
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
/*!
 * \copyright   This file is part of the public version of the AREPO code.
 * \copyright   Copyright (C) 2009-2019, Max-Planck Institute for Astrophysics
 * \copyright   Developed by Volker Springel (vspringel@MPA-Garching.MPG.DE) and
 *              contributing authors.
 * \copyright   Arepo is free software: you can redistribute it and/or modify
 *              it under the terms of the GNU General Public License as published by
 *              the Free Software Foundation, either version 3 of the License, or
 *              (at your option) any later version.
 *
 *              Arepo is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU General Public License for more details.
 *
 *              A copy of the GNU General Public License is available under
 *              LICENSE as part of this program.  See also
 *              <https://www.gnu.org/licenses/>.
 *
 * \file        src/domain_box.c
 * \date        05/2018
 * \brief       Routines that determine domain box and do periodic wrapping.
 * \details     contains files:
 *                void domain_findExtent(void)
 *                void do_box_wrapping(void)
 * 
 * 
 * \par Major modifications and contributions:
 * 
 * - DD.MM.YYYY Description
 * - 05.05.2018 Prepared file for public release -- Rainer Weinberger
 */


#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <math.h>


#include "../main/allvars.h"
#include "../main/proto.h"
#include "domain.h"
#include "../mesh/voronoi/voronoi.h"


/*! \brief Finds the extent of the global domain grid.
 *
 *  The minimum extent is the box size.
 *
 *  \return void
 */
void domain_findExtent(void)
{
  int i, j;
  double len, xmin[3], xmax[3], xmin_glob[3], xmax_glob[3];

  /* determine local extension */
  for(j = 0; j < 3; j++)
    {
      /* preset to simulation box */
      xmin[j] = 0;
      xmax[j] = boxSize;
    }
  // Take care of stretched box
#ifdef LONG_X
  xmax[0] = boxSize_X;
#endif /* #ifdef LONG_X */
#ifdef LONG_Y
  xmax[1] = boxSize_Y;
#endif /* #ifdef LONG_Y */
#ifdef LONG_Z
  xmax[2] = boxSize_Z;
#endif /* #ifdef LONG_Z */

  for(i = 0; i < NumPart; i++)
    {
#ifdef ADDBACKGROUNDGRID
      if(P[i].Type != 0)
        continue;
#endif /* #ifdef ADDBACKGROUNDGRID */
      for(j = 0; j < 3; j++)
        {
          if(xmin[j] > P[i].Pos[j])
            xmin[j] = P[i].Pos[j];

          if(xmax[j] < P[i].Pos[j])
            xmax[j] = P[i].Pos[j];
        }
    }

  MPI_Allreduce(xmin, xmin_glob, 3, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
  MPI_Allreduce(xmax, xmax_glob, 3, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);

#ifdef ADDBACKGROUNDGRID
  for(j = 0; j < 3; j++)
    if(xmax_glob[j] < All.BoxSize)
      xmax_glob[j] = All.BoxSize;

  for(j = 0; j < 3; j++)
    if(xmin_glob[j] > 0)
      xmin_glob[j] = 0;
#endif /* #ifdef ADDBACKGROUNDGRID */

  len = 0;
  for(j = 0; j < 3; j++)
    if(xmax_glob[j] - xmin_glob[j] > len)
      len = xmax_glob[j] - xmin_glob[j];

#if defined(GRAVITY_NOT_PERIODIC) && !defined(ADDBACKGROUNDGRID)
  len *= 1.2;                   /* enlarge box a bit to avoid triggering of an out of box recovery */
#else /* #if defined(GRAVITY_NOT_PERIODIC) && !defined(ADDBACKGROUNDGRID) */
  len *= 1.00001;
#endif /* #if defined(GRAVITY_NOT_PERIODIC) && !defined(ADDBACKGROUNDGRID) #else */


#if !defined(RANDOMIZE_DOMAINCENTER)
  for(j = 0; j < 3; j++)
    {
      DomainCenter[j] = 0.5 * (xmin_glob[j] + xmax_glob[j]);
      DomainCorner[j] = 0.5 * (xmin_glob[j] + xmax_glob[j]) - 0.5 * len;
    }
#else /* #if !defined(RANDOMIZE_DOMAINCENTER) */
  for(j = 0; j < 3; j++)
    {
      DomainCenter[j] = 0.5 * (xmin_glob[j] + xmax_glob[j]);
      DomainCenter[j] += (2. * get_random_number() - 1.) * 0.5 * len;
    }

  MPI_Bcast(DomainCenter, 3, MPI_DOUBLE, 0, MPI_COMM_WORLD);

  len *= 2;

  for(j = 0; j < 3; j++)
    DomainCorner[j] = DomainCenter[j] - 0.5 * len;
#endif /* #if !defined(RANDOMIZE_DOMAINCENTER) #else */

  DomainLen = len;

  DomainInverseLen = 1.0 / DomainLen;
  DomainFac = 1.0 / len * (((peanokey) 1) << (BITS_PER_DIMENSION));
  DomainBigFac = (DomainLen / (((long long) 1) << 52));
}


/*! \brief Makes sure all particles are within box.
 *
 *  This function makes sure that all particle coordinates (Pos) are
 *  periodically mapped onto the interval [0, BoxSize].  After this function
 *  has been called, a new domain decomposition should be done, which will
 *  also force a new tree construction.
 *
 *  \return void
 */
void do_box_wrapping(void)
{
  int j;
  double boxsize[3];

#ifdef ADDBACKGROUNDGRID
  return;
#endif /* #ifdef ADDBACKGROUNDGRID */

  for(j = 0; j < 3; j++)
    boxsize[j] = All.BoxSize;

#ifdef LONG_X
  boxsize[0] *= LONG_X;
#endif /* #ifdef LONG_X */
#ifdef LONG_Y
  boxsize[1] *= LONG_Y;
#endif /* #ifdef LONG_Y */
#ifdef LONG_Z
  boxsize[2] *= LONG_Z;
#endif /* #ifdef LONG_Z */

  int i;
  for(i = 0; i < NumPart; i++)
    {
      if(i < NumGas)
        trans_table[i].wrapped = 0;

#if defined(GRAVITY_NOT_PERIODIC)
      if(P[i].Type != 0)
        continue;
#endif /* #if defined(GRAVITY_NOT_PERIODIC) */

#if !defined(REFLECTIVE_X)
      while(P[i].Pos[0] < 0)
        {
          P[i].Pos[0] += boxsize[0];
          if(i < NumGas)
            trans_table[i].wrapped |= 1;
        }

      while(P[i].Pos[0] >= boxsize[0])
        {
          P[i].Pos[0] -= boxsize[0];
          if(i < NumGas)
            trans_table[i].wrapped |= 2;
        }

#else /* #if !defined(REFLECTIVE_X) */
      if(P[i].Pos[0] < 0 || P[i].Pos[0] >= boxsize[0])
        {
          char buf[1000];

          sprintf(buf, "i=%d ID=%d type=%d moved out of box. x=%g", i, P[i].ID, P[i].Type, P[i].Pos[0]);
          terminate(buf);
        }
#endif /* #if !defined(REFLECTIVE_X) #else */

#if !defined(REFLECTIVE_Y)
      while(P[i].Pos[1] < 0)
        {
          P[i].Pos[1] += boxsize[1];
          if(i < NumGas)
            trans_table[i].wrapped |= 4;
        }

      while(P[i].Pos[1] >= boxsize[1])
        {
          P[i].Pos[1] -= boxsize[1];
          if(i < NumGas)
            trans_table[i].wrapped |= 8;
        }

#else /* #if !defined(REFLECTIVE_Y) */
      if(P[i].Pos[1] < 0 || P[i].Pos[1] >= boxsize[1])
        {
          char buf[1000];

          sprintf(buf, "i=%d ID=%d type=%d moved out of box. y=%g", i, P[i].ID, P[i].Type, P[i].Pos[1]);
          terminate(buf);
        }
#endif /* #if !defined(REFLECTIVE_Y) #else */

#if !defined(REFLECTIVE_Z)
      while(P[i].Pos[2] < 0)
        {
          P[i].Pos[2] += boxsize[2];
          if(i < NumGas)
            trans_table[i].wrapped |= 16;
        }

      while(P[i].Pos[2] >= boxsize[2])
        {
          P[i].Pos[2] -= boxsize[2];
          if(i < NumGas)
            trans_table[i].wrapped |= 32;
        }

#else /* #if !defined(REFLECTIVE_Z) */
      if(P[i].Pos[2] < 0 || P[i].Pos[2] >= boxsize[2])
        {
          char buf[1000];

          sprintf(buf, "i=%d ID=%d type=%d moved out of box. z=%g", i, P[i].ID, P[i].Type, P[i].Pos[2]);
          terminate(buf);
        }
#endif /* #if !defined(REFLECTIVE_Z) #else */

    }
}