Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
boundaries.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This file is part of Vlasiator.
4 * Copyright 2010-2016 Finnish Meteorological Institute
5 *
6 * For details of usage, see the COPYING file and read the "Rules of the Road"
7 * at http://www.physics.helsinki.fi/vlasiator/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#include "particles.h"
25
27{
28 // Handle a particle's boundary behaviour.
29 // returns "true" if the particle is still part of the simulation
30 // afterwards, or "false" if it is to be removed.
31 virtual bool handleParticle(Particle& p) = 0;
32
33 // Handle cell coordinate in the spatial dimension this boundary
34 // object cares about (for example: wrap in a periodic direction)
35 virtual int cellCoordinate(int c) = 0;
36
37 Boundary(int _dimension) : dimension(_dimension) {};
38 virtual void setExtent(double _min, double _max, int _cells) {
39 min=_min;
40 max=_max;
41 cells=_cells;
42 }
43
44 virtual ~Boundary(){};
45
46 // Which spatial dimension to handle
48
49 // Minimum and maximum spatial extents in this dimension
50 double min, max;
51
52 // Number of cells in this dimension
53 int cells;
54};
55
56// Boundary for a spatial dimension that is only 1 cell thick (pseudo-periodic)
58{
59 virtual bool handleParticle(Particle& p) {
60 // This boundary does not affect particles
61 return true;
62 }
63 virtual int cellCoordinate(int c) {
64 // Actual cell coordinates in this direction are
65 // always mapped to 0.
66 return 0;
67 }
68 CompactSpatialDimension(int _dimension) : Boundary(_dimension){};
69};
70
71// Open boundary, which removes particles if they fly out
72struct OpenBoundary : public Boundary
73{
74 virtual bool handleParticle(Particle& p) {
75
76 // Delete particles that are outside our boundaries.
77 if(p.x[dimension] <= min || p.x[dimension] >= max) {
78 return false;
79 } else {
80 // Leave all others be.
81 return true;
82 }
83 }
84
85 virtual int cellCoordinate(int c) {
86 // Cell coordinates are clamped
87 // TODO: Should this print warnings?
88 if(c < 0) {
89 return 0;
90 } else if(c >= cells) {
91 return cells-1;
92 } else {
93 return c;
94 }
95 }
96
97 virtual void setExtent(double _min, double _max, int _cells) {
98 double dx = (_max-_min)/((double)_cells);
99 min=_min+2*dx; // 2 Cells border.
100 max=_max-2*dx;
101 cells=_cells;
102 }
103 OpenBoundary(int _dimension) : Boundary(_dimension){};
104};
105
107{
108 virtual bool handleParticle(Particle& p) {
109 // Particles outside of bounds get their velocities flipped
110 if(p.x[dimension] <= min || p.x[dimension] >= max) {
111 p.v = p.v.cwiseProduct(flip_v);
112 }
113 return true;
114 }
115
116 virtual int cellCoordinate(int c) {
117 // Cell coordinates are clamped
118 // TODO: Should this print warnings?
119 if(c < 0) {
120 return 0;
121 } else if(c >= cells) {
122 return cells-1;
123 } else {
124 return c;
125 }
126 }
127
128 // Constructor
129 ReflectBoundary(int _dimension) : Boundary(_dimension) {
130 Vec3d flip = {1.,1.,1.};
131 flip[dimension] = -1.;
132 flip_v=flip;
133 }
134 virtual void setExtent(double _min, double _max, int _cells) {
135 double dx = (_max-_min)/((double)_cells);
136 min=_min+2*dx; // 2 Cells border.
137 max=_max-2*dx;
138 cells=_cells;
139 }
140
141 // Vector to multiply with in order to flip velocity
142 // vectors for our dimension
144
145};
146
148{
149 virtual bool handleParticle(Particle& p) {
150 if(p.x[dimension] < min) {
151 p.x += offset_p;
152 } else if(p.x[dimension] >= max) {
153 p.x -= offset_p;
154 }
155 return true;
156 }
157
158 virtual int cellCoordinate(int c) {
159 return c % cells;
160 }
161
162 // Constructor
163 PeriodicBoundary(int _dimension) : Boundary(_dimension) {
164 }
165 virtual void setExtent(double _min, double _max, int _cells) {
166 min=_min;
167 max=_max;
168 cells=_cells;
169
170 Vec3d offset = {0.,0.,0.};
171 offset[dimension] = max-min;
172 offset_p = offset;
173 }
174
175 // Vector to offset particle positions that leave through
176 // one boundary with, to come out the other end
178
179};
180
181template<typename T> Boundary* createBoundary(int dimension)
182{
183 return new T(dimension);
184}
dx
Definition Dispersion.m:38
Constants c
Definition Dispersion.m:45
Boundary * createBoundary(int dimension)
Definition boundaries.h:181
#define Vec3d
double min
Definition boundaries.h:50
int dimension
Definition boundaries.h:47
virtual int cellCoordinate(int c)=0
virtual ~Boundary()
Definition boundaries.h:44
Boundary(int _dimension)
Definition boundaries.h:37
virtual bool handleParticle(Particle &p)=0
double max
Definition boundaries.h:50
virtual void setExtent(double _min, double _max, int _cells)
Definition boundaries.h:38
int cells
Definition boundaries.h:53
virtual int cellCoordinate(int c)
Definition boundaries.h:63
virtual bool handleParticle(Particle &p)
Definition boundaries.h:59
CompactSpatialDimension(int _dimension)
Definition boundaries.h:68
virtual int cellCoordinate(int c)
Definition boundaries.h:85
OpenBoundary(int _dimension)
Definition boundaries.h:103
virtual void setExtent(double _min, double _max, int _cells)
Definition boundaries.h:97
virtual bool handleParticle(Particle &p)
Definition boundaries.h:74
virtual bool handleParticle(Particle &p)
Definition boundaries.h:149
PeriodicBoundary(int _dimension)
Definition boundaries.h:163
virtual void setExtent(double _min, double _max, int _cells)
Definition boundaries.h:165
virtual int cellCoordinate(int c)
Definition boundaries.h:158
virtual bool handleParticle(Particle &p)
Definition boundaries.h:108
virtual void setExtent(double _min, double _max, int _cells)
Definition boundaries.h:134
ReflectBoundary(int _dimension)
Definition boundaries.h:129
virtual int cellCoordinate(int c)
Definition boundaries.h:116