-
Notifications
You must be signed in to change notification settings - Fork 9
/
particle.cpp
68 lines (59 loc) · 1.9 KB
/
particle.cpp
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
#include "particle.h"
Particle::Particle( int id, double charge, double mass, Vec3d position, Vec3d momentum ) :
id( id ),
charge( charge ),
mass( mass ),
position( position ),
momentum( momentum ),
momentum_is_half_time_step_shifted( false )
{ }
void Particle::update_position( double dt )
{
Vec3d pos_shift;
pos_shift = vec3d_times_scalar( momentum, dt / mass );
position = vec3d_add( position, pos_shift );
}
Vec3d Particle::field_at_point( Vec3d &point )
{
Vec3d dist = vec3d_sub( point, position );
double dist_len = vec3d_length( dist );
if ( dist_len == 0 ){
std::cout << "0 distance while computing Particle::field_at_point. Aborting."
<< std::endl;
exit( EXIT_FAILURE );
}
double dist_len_cube = dist_len * dist_len * dist_len;
return vec3d_times_scalar( dist, charge / dist_len_cube );
}
void Particle::print()
{
std::cout.setf( std::ios::scientific );
std::cout.precision( 3 );
std::cout << "Particle: ";
std::cout << "id: " << id << ", ";
std::cout << "charge = " << charge << " mass = " << mass << ", ";
std::cout << "pos(x,y,z) = ("
<< vec3d_x( position ) << ", "
<< vec3d_y( position ) << ", "
<< vec3d_z( position ) << "), ";
std::cout << "momentum(px,py,pz) = ("
<< vec3d_x( momentum ) << ", "
<< vec3d_y( momentum ) << ", "
<< vec3d_z( momentum ) << ")";
std::cout << std::endl;
return;
}
void Particle::print_short()
{
std::cout.setf( std::ios::scientific );
std::cout.precision( 2 );
std::cout << "id: " << id << " "
<< "x = " << vec3d_x( position ) << " "
<< "y = " << vec3d_y( position ) << " "
<< "z = " << vec3d_z( position ) << " "
<< "px = " << vec3d_x( momentum ) << " "
<< "py = " << vec3d_y( momentum ) << " "
<< "pz = " << vec3d_z( momentum ) << " "
<< std::endl;
return;
}