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
/**
* @file boundary.c
* @brief Implementation of all boundary conditions.
* @author Hanno Rein <hanno@hanno-rein.de>
*
* @details The code supports different boundary conditions.
*
*
* @section LICENSE
* Copyright (c) 2015 Hanno Rein, Shangfei Liu
*
* This file is part of rebound.
*
* rebound 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.
*
* rebound 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.
*
* You should have received a copy of the GNU General Public License
* along with rebound. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "rebound.h"
#include "rebound_internal.h"
#include <math.h>
#include "particle.h"
#include "boundary.h"
void reb_boundary_check(struct reb_simulation* const r){
struct reb_particle* const particles = r->particles;
int N = r->N;
struct reb_vec3d boxsize = {
.x = r->root_size*(double)r->N_root_x,
.y = r->root_size*(double)r->N_root_y,
.z = r->root_size*(double)r->N_root_z,
};
switch(r->boundary){
case REB_BOUNDARY_OPEN:
for (int i=0;i<N;i++){ // run through loop backwards so we don't have to recheck same index after removing
int removep = 0;
if(particles[i].x>boxsize.x/2.){
removep = 1;
}
if(particles[i].x<-boxsize.x/2.){
removep = 1;
}
if(particles[i].y>boxsize.y/2.){
removep = 1;
}
if(particles[i].y<-boxsize.y/2.){
removep = 1;
}
if(particles[i].z>boxsize.z/2.){
removep = 1;
}
if(particles[i].z<-boxsize.z/2.){
removep = 1;
}
if (removep==1){
if(r->track_energy_offset){
double Ei = reb_simulation_energy(r);
reb_simulation_remove_particle(r, i);
r->energy_offset += Ei - reb_simulation_energy(r);
} else {
reb_simulation_remove_particle(r, i);
}
i--; // need to recheck the particle that replaced the removed one
N--; // This is the local loop N
}
}
break;
case REB_BOUNDARY_SHEAR:
{
// The offset of ghostcell is time dependent.
const double OMEGA = r->OMEGA;
const double offsetp1 = -fmod(-1.5*OMEGA*boxsize.x*r->t+boxsize.y/2.,boxsize.y)-boxsize.y/2.;
const double offsetm1 = -fmod( 1.5*OMEGA*boxsize.x*r->t-boxsize.y/2.,boxsize.y)+boxsize.y/2.;
struct reb_particle* const particles = r->particles;
#pragma omp parallel for schedule(guided)
for (int i=0;i<N;i++){
// Radial
while(particles[i].x>boxsize.x/2.){
particles[i].x -= boxsize.x;
particles[i].y += offsetp1;
particles[i].vy += 3./2.*OMEGA*boxsize.x;
}
while(particles[i].x<-boxsize.x/2.){
particles[i].x += boxsize.x;
particles[i].y += offsetm1;
particles[i].vy -= 3./2.*OMEGA*boxsize.x;
}
// Azimuthal
while(particles[i].y>boxsize.y/2.){
particles[i].y -= boxsize.y;
}
while(particles[i].y<-boxsize.y/2.){
particles[i].y += boxsize.y;
}
// Vertical (there should be no boundary, but periodic makes life easier)
while(particles[i].z>boxsize.z/2.){
particles[i].z -= boxsize.z;
}
while(particles[i].z<-boxsize.z/2.){
particles[i].z += boxsize.z;
}
}
}
break;
case REB_BOUNDARY_PERIODIC:
#pragma omp parallel for schedule(guided)
for (int i=0;i<N;i++){
while(particles[i].x>boxsize.x/2.){
particles[i].x -= boxsize.x;
}
while(particles[i].x<-boxsize.x/2.){
particles[i].x += boxsize.x;
}
while(particles[i].y>boxsize.y/2.){
particles[i].y -= boxsize.y;
}
while(particles[i].y<-boxsize.y/2.){
particles[i].y += boxsize.y;
}
while(particles[i].z>boxsize.z/2.){
particles[i].z -= boxsize.z;
}
while(particles[i].z<-boxsize.z/2.){
particles[i].z += boxsize.z;
}
}
break;
default:
break;
}
}
static const struct reb_vec6d nan_ghostbox = {.x = 0, .y = 0, .z = 0, .vx = 0, .vy = 0, .vz = 0};
struct reb_vec6d reb_boundary_get_ghostbox(struct reb_simulation* const r, int i, int j, int k){
struct reb_vec3d boxsize = {
.x = r->root_size*(double)r->N_root_x,
.y = r->root_size*(double)r->N_root_y,
.z = r->root_size*(double)r->N_root_z,
};
switch(r->boundary){
case REB_BOUNDARY_OPEN:
{
struct reb_vec6d gb;
gb.x = boxsize.x*(double)i;
gb.y = boxsize.y*(double)j;
gb.z = boxsize.z*(double)k;
gb.vx = 0;
gb.vy = 0;
gb.vz = 0;
return gb;
}
case REB_BOUNDARY_SHEAR:
{
const double OMEGA = r->OMEGA;
struct reb_vec6d gb;
// Ghostboxes have a finite velocity.
gb.vx = 0.;
gb.vy = -1.5*(double)i*OMEGA*boxsize.x;
gb.vz = 0.;
// The shift in the y direction is time dependent.
double shift;
if (i==0){
shift = -fmod(gb.vy*r->t,boxsize.y);
}else{
if (i>0){
shift = -fmod(gb.vy*r->t-boxsize.y/2.,boxsize.y)-boxsize.y/2.;
}else{
shift = -fmod(gb.vy*r->t+boxsize.y/2.,boxsize.y)+boxsize.y/2.;
}
}
gb.x = boxsize.x*(double)i;
gb.y = boxsize.y*(double)j-shift;
gb.z = boxsize.z*(double)k;
return gb;
}
case REB_BOUNDARY_PERIODIC:
{
struct reb_vec6d gb;
gb.x = boxsize.x*(double)i;
gb.y = boxsize.y*(double)j;
gb.z = boxsize.z*(double)k;
gb.vx = 0;
gb.vy = 0;
gb.vz = 0;
return gb;
}
default:
return nan_ghostbox;
}
}
int reb_boundary_particle_is_in_box(const struct reb_simulation* const r, struct reb_particle p){
switch(r->boundary){
case REB_BOUNDARY_OPEN:
case REB_BOUNDARY_SHEAR:
case REB_BOUNDARY_PERIODIC:
{
struct reb_vec3d boxsize = {
.x = r->root_size*(double)r->N_root_x,
.y = r->root_size*(double)r->N_root_y,
.z = r->root_size*(double)r->N_root_z,
};
if(p.x>boxsize.x/2.){
return 0;
}
if(p.x<-boxsize.x/2.){
return 0;
}
if(p.y>boxsize.y/2.){
return 0;
}
if(p.y<-boxsize.y/2.){
return 0;
}
if(p.z>boxsize.z/2.){
return 0;
}
if(p.z<-boxsize.z/2.){
return 0;
}
return 1;
}
default:
case REB_BOUNDARY_NONE:
return 1;
}
}