summaryrefslogtreecommitdiff
path: root/src/main/Accelerator.cc
blob: ca5de8ac9035885172d205deff2ab97c0ad4a1b5 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Accelerator.cc
 *
 *  Created on: 22 mars 2011
 *      Author: christian
 */

#include <iostream>
#include "Accelerator.h"
#include "exceptions.h"

using namespace std;

namespace vhc {

Accelerator::Accelerator():
		elementCollec(0),
		particleCollec(0),
		allowLinear(false),
		closed(false)
	{};

Accelerator::~Accelerator() {
	clear();
}

void Accelerator::initializeParticles() {
	//rajouter les particules dans leurs elements respectifs
	for (ParticleIterator i = particleCollec.begin(); i != particleCollec.end(); ++i) {

		for (ElementIterator j = elementCollec.begin(); j != elementCollec.end(); ++j) {
			if ((**j).contains(**i)) {
				(**i).setElement(*j);
				break;
			}
		}

		//si une particule n'est pas contenue dans un element elle est supprimee
		if ((**i).getElement() == NULL) {
			delete *i;
			i = particleCollec.erase(i);
			--i;
			//std::cout << "Particle hit wall. Removed from simulation" << std::endl;
		}
	}
}

Element& Accelerator::add(const Element& element) {
	Element* e = element.clone();
	elementCollec.push_back(e);
	closed = false;
	return *e;
}

Particle& Accelerator::add(const Particle& particle) {
	Particle* p = particle.clone();
	particleCollec.push_back(p);
	closed = false;
	return *p;
}

const Accelerator::ElementCollection& Accelerator::getElements() const { return elementCollec;}

const Accelerator::ParticleCollection& Accelerator::getParticles() const { return particleCollec;}

void Accelerator::close() {
	for (ElementIterator current = elementCollec.begin(); current != elementCollec.end(); ++current) {

		ElementIterator next = current;
		++next;
		if (next == elementCollec.end()) next = elementCollec.begin();


		// est-ce que les elements se suivent (sont connectes)?
		if (Vector3D::ae((*current)->getExitPosition(), (*next)->getEntryPosition())) {
			(**current).setNext(*next);
			(**next).setPrevious(*current);

		//sinon est-ce qu'il s'agit du dernier element?
		} else if (next == elementCollec.begin()) {
			if (!allowLinear) throw UnsupportedOperationException("Cannot close accelerator. Linear Accelerators are not allowed.");

		//sinon
		} else throw UnsupportedOperationException("Cannot close accelerator. Two succeeding elements are not physically connected. (not close enough)");
	}

	initializeParticles();

	closed = true;

}

void Accelerator::updateParticles() {
	for (ParticleIterator i = particleCollec.begin(); i != particleCollec.end(); ++i) {
		Particle& particle = **i;
		if (particle.getElement()->isAfter(particle)) {
			if (particle.getElement()->getNext() == NULL)
				if (allowLinear) {
					delete *i;
					i = particleCollec.erase(i);
					--i;
					//cout << "Particle reached end of accelerator. Removed from simulation" << std::endl;
				}
				else throw Exception("Element in accelerator not connected to next.");
			else particle.setElement(particle.getElement()->getNext());
		} else if (particle.getElement()->isBefore(particle)) {
			if (particle.getElement()->getPrevious() == NULL)
				if (allowLinear) {
					delete *i;
					i = particleCollec.erase(i);
					--i;
					//cout << "Particle reached beginning of accelerator. Removed from simulation" << std::endl;
				}
				else throw Exception("Element in accelerator not connected to previous.");
			else particle.setElement(particle.getElement()->getPrevious());
		} else if (particle.getElement()->isBeside(particle)) {
			//std::cout << "Particle hit wall. Removed from simulation" << std::endl;
			delete *i;
			i = particleCollec.erase(i);
			--i;
		}
	}
}

void Accelerator::clear() {
	for (ParticleIterator i = particleCollec.begin(); i != particleCollec.end(); ++i) {
		delete *i;
		*i = NULL;
	}
	particleCollec.clear();

	for (ElementIterator i = elementCollec.begin(); i != elementCollec.end(); ++i) {
		delete *i;
		*i = NULL;
	}
	elementCollec.clear();

	closed = false;
}

void Accelerator::step(double dt) {
	if (!closed) close();

	for (ParticleIterator i = particleCollec.begin(); i != particleCollec.end(); ++i) {
		Particle& particle = **i;

		particle.setForce(Vector3D::Null);

		particle.applyMagneticForce(particle.getElement()->magneticFieldAt(particle.getPosition()), dt);

		Vector3D a = particle.getForce() / (particle.getGamma() * particle.getMass());
		particle.setVelocity(particle.getVelocity() + a * dt);

		particle.translate(particle.getVelocity() * dt);

	}


	updateParticles();
}

void Accelerator::enableLinear(bool value) {
	allowLinear = value;
}

/** Cf. Accelerator.h */
std::string Accelerator::toString() const {
	std::stringstream s;

	if (elementCollec.size() == 0) {
		s << "This accelerator doesn't contain anything."<<"\n";
	} else if (elementCollec.size()==1) {
		s << "This accelerator is made of the following element :" << "\n";
		s << elementCollec.front()->toString()<<"\n";
	} else {
		s << "This accelerator is made of the " << elementCollec.size() << " following elements :" << "\n";
		for (ElementCollection::const_iterator i = elementCollec.begin(); i != elementCollec.end(); ++i) {
			s << (*i)->toString() << "\n";
		}
	}

	if (particleCollec.size() == 0) {
		s << "This accelerator doesn't contain any particle." << "\n";
	} else if (particleCollec.size() == 1) {
		s << "This accelerator contains the following particle :" << "\n";
		s << particleCollec.front()->toString() << "\n";
	}else{
		s << "This accelerator contains the " << particleCollec.size() << " following particles :"<<"\n";
		for (list<Particle*>::const_iterator i = particleCollec.begin(); i != particleCollec.end(); ++i) {
					s << (*i)->toString() << "\n";
		}
	}

	return s.str();
}

} //vhc