Navigation

Main Page
Download
Support
Installation
Tutorial
Examples
Reference Manual
   Version 1.0.6dev
      Class Index
      File List
   Version 1.0.6
   Version 1.0.5new_solver
   Version 1.0.5dev
   Version 1.0.5b
   Version 1.0.4dev
   Version 1.0.4
Publications


Hosted by Get Ion Beam Simulator at SourceForge.net. Fast, secure and Free Open Source software downloads
particlestepper.hpp
Go to the documentation of this file.
1 
5 /* Copyright (c) 2015,2022 Taneli Kalvas. All rights reserved.
6  *
7  * You can redistribute this software and/or modify it under the terms
8  * of the GNU General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this library (file "COPYING" included in the package);
19  * if not, write to the Free Software Foundation, Inc., 51 Franklin
20  * Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * If you have questions about your rights to use or distribute this
23  * software, please contact Berkeley Lab's Technology Transfer
24  * Department at TTD@lbl.gov. Other questions, comments and bug
25  * reports should be sent directly to the author via email at
26  * taneli.kalvas@jyu.fi.
27  *
28  * NOTICE. This software was developed under partial funding from the
29  * U.S. Department of Energy. As such, the U.S. Government has been
30  * granted for itself and others acting on its behalf a paid-up,
31  * nonexclusive, irrevocable, worldwide license in the Software to
32  * reproduce, prepare derivative works, and perform publicly and
33  * display publicly. Beginning five (5) years after the date
34  * permission to assert copyright is obtained from the U.S. Department
35  * of Energy, and subject to any subsequent five (5) year renewals,
36  * the U.S. Government is granted for itself and others acting on its
37  * behalf a paid-up, nonexclusive, irrevocable, worldwide license in
38  * the Software to reproduce, prepare derivative works, distribute
39  * copies to the public, perform publicly and display publicly, and to
40  * permit others to do so.
41  */
42 
43 
44 #ifndef PARTICLESTEPPER_HPP
45 #define PARTICLESTEPPER_HPP 1
46 
47 
48 #include "geometry.hpp"
49 #include "particles.hpp"
50 #include "vectorfield.hpp"
51 #include "meshscalarfield.hpp"
52 #include "scharge.hpp"
53 #include "particledatabase.hpp"
54 
55 
56 template<class PP> class ParticleStepper {
57 
58  double _dt;
59  bool _surface_collision;
60  uint32_t _trajdiv;
62  bool _mirror[6];
63  MeshScalarField *_scharge;
64  const VectorField *_efield;
65  const VectorField *_bfield;
66  const Geometry *_geom;
67 
68 public:
69 
70 
71  ParticleStepper( double dt, uint32_t trajdiv, bool mirror[6],
72  MeshScalarField *scharge, const VectorField *efield,
73  const VectorField *bfield, const Geometry *geom )
74  : _dt(dt), _surface_collision(false), _trajdiv(trajdiv), _scharge(scharge),
75  _efield(efield), _bfield(bfield), _geom(geom) {
76  // Initialize mirroring
77  _mirror[0] = mirror[0];
78  _mirror[1] = mirror[1];
79  _mirror[2] = mirror[2];
80  _mirror[3] = mirror[3];
81  _mirror[4] = mirror[4];
82  _mirror[5] = mirror[5];
83  }
84 
85 
86  ~ParticleStepper() {
87 
88  }
89 
90 
93  void initialize( Particle<PP> *particle, uint32_t pi ) {
94  if( PP::geom_mode() == MODE_3D ) {
95  Vec3D E, B;
96  Vec3D x = particle->x().location();
97  Vec3D v = particle->x().velocity();
98  if( _efield )
99  E = (*_efield)( x );
100  if( _bfield )
101  B = (*_bfield)( x );
102  Vec3D a = particle->qm()*(E+cross(v,B));
103  v = v - 0.5*a*_dt;
104  (*particle)[2] = v[0];
105  (*particle)[4] = v[1];
106  (*particle)[6] = v[2];
107 
108  } else {
109  throw( ErrorUnimplemented( ERROR_LOCATION, "Particle stepping for geometry modes other than MODE_3D unimplemented" ) );
110  }
111  }
112 
113 
116  void step( Particle<PP> *particle, uint32_t pi ) {
117 
118  // Check particle status
119  if( particle->get_status() != PARTICLE_OK )
120  return;
121 
122  Vec3D x;
123  if( PP::geom_mode() == MODE_3D ) {
124  Vec3D E, B;
125  x = particle->x().location();
126  if( _efield )
127  E = (*_efield)( x );
128  if( _bfield )
129  B = (*_bfield)( x );
130  Vec3D vminus = particle->x().velocity() + 0.5*particle->qm()*E*_dt;
131  Vec3D t = 0.5*particle->qm()*B*_dt;
132  Vec3D vprime = vminus + cross(vminus,t);
133  Vec3D s = 2.0/(1+t.ssqr())*t;
134  Vec3D vplus = vminus + cross(vprime,s);
135  Vec3D v = vplus + 0.5*particle->qm()*E*_dt;
136  x += v*_dt;
137 
138  for( int a = 0; a < 3; a++ ) {
139  if( _mirror[2*a] && x[a] < _geom->origo(a) ) {
140  x[a] = 2.0*_geom->origo(a) - x[a];
141  v[a] *= -1;
142  }
143  if( _mirror[2*a+1] && x[a] > _geom->max(a) ) {
144  x[a] = 2.0*_geom->max(a) - x[a];
145  v[a] *= -1;
146  }
147  }
148 
149  (*particle)[0] += _dt; // Advance time, time follows position, not velocity
150  (*particle)[1] = x[0];
151  (*particle)[2] = v[0];
152  (*particle)[3] = x[1];
153  (*particle)[4] = v[1];
154  (*particle)[5] = x[2];
155  (*particle)[6] = v[2];
156 
157  } else {
158  throw( ErrorUnimplemented( ERROR_LOCATION, "Particle stepping for geometry modes other than MODE_3D unimplemented" ) );
159  }
160 
161  // Collision detection
162  if( _surface_collision ) {
163  if( _geom->surface_inside( x ) ) {
164  particle->set_status( PARTICLE_COLL );
165  return;
166  }
167  } else {
168  if( _geom->inside( x ) ) {
169  particle->set_status( PARTICLE_COLL );
170  return;
171  }
172  }
173 
174  // Space charge deposition
175  scharge_add_step_pic( *_scharge, particle->IQ(), x );
176 
177  // Save trajectory data
178  if( _trajdiv != 0 && pi % _trajdiv == 0 )
179  particle->add_trajectory_point( particle->x() );
180  }
181 
182 
183 
186  void set_surface_collision( bool surface_collision ) {
187  if( surface_collision && _geom->geom_mode() == MODE_2D )
188  throw( Error( ERROR_LOCATION, "2D surface collision not supported" ) );
189  if( surface_collision && !_geom->surface_built() )
190  throw( Error( ERROR_LOCATION, "surface model not built" ) );
191  _surface_collision = surface_collision;
192  }
193 };
194 
195 
196 #endif
197 
Error class to use if requested feature is unimplemented.
Definition: error.hpp:229
Basic error class.
Definition: error.hpp:153
Geometry defining class.
Definition: geometry.hpp:180
bool surface_built(void) const
Is the solid surface representation built?
Definition: geometry.hpp:503
uint32_t inside(const Vec3D &x) const
Return if point is inside solids.
Definition: geometry.cpp:334
uint32_t surface_inside(const Vec3D &x) const
Finds if point is inside surface triangulation.
Definition: geometry.cpp:2243
Scalar field class.
Definition: meshscalarfield.hpp:70
Vec3D max(void) const
Returns vector pointing to the last mesh point opposite of origo.
Definition: mesh.hpp:137
Vec3D origo(void) const
Returns origo vector of geometry.
Definition: mesh.hpp:128
geom_mode_e geom_mode(void) const
Returns geometry mode.
Definition: mesh.hpp:108
void set_status(particle_status_e status)
Set particle status.
Definition: particles.hpp:699
double IQ() const
Return current or charge carried by trajectory or particle cloud [A/C].
Definition: particles.hpp:706
particle_status_e get_status()
Return particle status.
Definition: particles.hpp:695
double qm() const
Return charge per mass ratio (q/m) [C/kg].
Definition: particles.hpp:718
Definition: particlestepper.hpp:56
void initialize(Particle< PP > *particle, uint32_t pi)
Initialize particle stepping velocity backwards by 0.5*dt.
Definition: particlestepper.hpp:93
void set_surface_collision(bool surface_collision)
Enable/disable surface collision model.
Definition: particlestepper.hpp:186
void step(Particle< PP > *particle, uint32_t pi)
Take one dt step forward for particle.
Definition: particlestepper.hpp:116
Particle class in some geometry.
Definition: particles.hpp:740
PP & x()
Return reference to coordinate data.
Definition: particles.hpp:798
void add_trajectory_point(const PP &x)
Add trajectory point to the end of the trajectory.
Definition: particles.hpp:818
Three dimensional vector.
Definition: vec3d.hpp:58
double ssqr(void) const
Returns square of 2-norm of vector.
Definition: vec3d.hpp:220
Vector field.
Definition: vectorfield.hpp:56
#define ERROR_LOCATION
Macro for setting error location when throwing errors.
Definition: error.hpp:83
Geometry definition
Mesh based scalar fields.
Particle databases
Particle and particle point objects
Space charge deposition functions.
@ MODE_3D
3D geometry
Definition: types.hpp:63
@ MODE_2D
2D geometry
Definition: types.hpp:61
Vec4D cross(const Vec4D &vec1, const Vec4D &vec2)
Definition: vec4d.hpp:279
Vector field base.


Reference manual for Ion Beam Simulator 1.0.6dev
Generated by Doxygen 1.9.1 on Thu Sep 11 2025 09:37:24.