summaryrefslogtreecommitdiff
path: root/src/main/Particle.cc
blob: 6e5eb0b01b13f8bf1bedfecad08d4c68565c7a26 (plain) (blame)
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
/*
 * Particle.cc
 *
 *  Created on: Mar 9, 2011
 *      Author: jakob
 */

#include "Particle.h"

namespace vhc {

Particle::Particle(const Vector3D& position, double mass, double charge):
		position(position),
		velocity(0, 0, 0),
		force(0, 0, 0),
		mass(mass),
		charge(charge),
		element(NULL)
	{};


Particle::Particle(const Vector3D& position, double mass, double charge, double energy, const Vector3D& direction):
		position(position),
		velocity(constants::C * sqrt(1 - (mass * mass * constants::C2 * constants::C2) / (energy * energy)) * direction.unit()),
		gamma(energy / (mass * constants::C2)),
		force(0, 0, 0),
		mass(mass),
		charge(charge),
		element(NULL)
	{};

Vector3D Particle::getPosition() const {return position;}

void Particle::setPosition(const Vector3D& pos) {position = pos;}

void Particle::translate(const Vector3D& dx) {position += dx;}

Vector3D Particle::getForce() const {return force;}

void Particle::setForce(const Vector3D& f) {force = f;}

void Particle::applyForce(const Vector3D& f) {force += f;}

void Particle::applyMagneticForce(const Vector3D& b, double dt) {
	if (dt != 0 && b != Vector3D::Null) {
		Vector3D f = charge * velocity.cross(b);
		applyForce(f.rotate(velocity.cross(f), asin(1.0 * (dt * f.norm()) / (2 * gamma * mass * velocity.norm()))));
	}
}

double Particle::getMass() const {return mass;}

double Particle::getCharge() const {return charge;}


Vector3D Particle::getVelocity() const {return velocity;}

void Particle::setVelocity(const Vector3D& v) {
	velocity = v;
	gamma = 1.0 / sqrt(1.0 - v.normSquare() / constants::C2);
}

double Particle::getEnergy() const {return gamma * mass * constants::C2;}

double Particle::getGamma() const {return gamma;}

std::string Particle::toString() const {
	std::stringstream s;
	s << "Particle:"	<< "\n";
	s << "\tposition: "	<< position	<< "\n";
	s << "\tvelocity: "	<< velocity;
	s << "\tnorm: "		<< velocity.norm() << "\n";
	s << "\tenergy:   "	<< getEnergy()	<< "\n";
	s << "\tforce:    "	<< force << "\n";
	s << "\tgamma: "	<< gamma << "\n";
	s << "\tmass:     "	<< mass		<< "\n";
	s << "\tcharge:   "	<< charge	<< "\n";
	return s.str();
}

Particle* Particle::clone() const {return new Particle(*this);}

Element* Particle::getElement() const {return element;}

void Particle::setElement(Element* element) {this->element = element;}


} //vhc