Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
particles.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#include <vector>
23#include "particles.h"
24#include "physconst.h"
25#include "relativistic_math.h"
26#include "vlsv_writer.h"
27
28
29/* Particle propagation given E- and B-Field at the particle location
30 * with the Boris-Method */
31void Particle::push(Vec3d& B, Vec3d& E, double dt) {
32
33 Vec3d uminus = v + (q * E * dt)/(2. * m);
34 Vec3d h = (q * B * dt)/(2. * m * gamma(uminus));
35 Vec3d uprime = uminus + cross_product(uminus, h);
36 h = (2.* h)/(1. + dot_product(h,h));
37 Vec3d uplus = uminus + cross_product(uprime, h);
38
39 v = uplus + (q * E * dt)/(2. * m);
40 x += dt * v;
41}
42
43void writeParticles(const ParticleContainer& p,const char* filename) {
44
45 vlsv::Writer vlsvWriter;
46 vlsvWriter.open(filename,MPI_COMM_WORLD,0);
47
48 std::vector<double> writebuf(p.size() * 3);
49
50 /* First, store particle positions */
51 uint writable_particles=0;
52 for(unsigned int i=0; i < p.size(); i++) {
53 if(vector_length(p[i].x) == 0) {
54 continue;
55 }
56
57 for(int j=0; j<3; j++) {
58 writebuf[3*writable_particles+j] = p[i].x[j];
59 }
60 writable_particles++;
61 }
62
63 std::map<std::string,std::string> attribs;
64 attribs["name"] = "proton_position";
65 attribs["type"] = vlsv::mesh::STRING_POINT;
66 if (vlsvWriter.writeArray("MESH",attribs,writable_particles,3,writebuf.data()) == false) {
67 std::cerr << "\t ERROR failed to write particle positions!" << std::endl;
68 }
69
70 /* Then, velocities */
71 writable_particles=0;
72 for(unsigned int i=0; i < p.size(); i++) {
73 if(vector_length(p[i].x) == 0) {
74 continue;
75 }
76 for(int j=0; j<3; j++) {
77 writebuf[3*writable_particles+j] = p[i].v[j];
78 }
79 writable_particles++;
80 }
81
82 attribs["name"] = "proton_velocity";
83 if (vlsvWriter.writeArray("MESH",attribs,writable_particles,3,writebuf.data()) == false) {
84 std::cerr << "\t ERROR failed to write particle velocities!" << std::endl;
85 }
86 vlsvWriter.close();
87}
for i
Definition Dispersion.m:24
dt
Definition Dispersion.m:39
gamma
Definition Dispersion.m:52
#define dot_product(av, bv)
#define cross_product(av, bv)
#define vector_length(v)
const int j
void writeParticles(const ParticleContainer &p, const char *filename)
Definition particles.cpp:43
std::vector< Particle, aligned_allocator< Particle, 32 > > ParticleContainer
Definition particles.h:45
#define Vec3d
Definition particles.h:26
Real m
Definition particles.h:33
Vec3d v
Definition particles.h:32
void push(Vec3d &B, Vec3d &E, double dt)
Definition particles.cpp:31
Real q
Definition particles.h:34
Vec3d x
Definition particles.h:31