diff options
author | Jakob Odersky <jodersky@gmail.com> | 2011-04-17 23:03:45 +0000 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2011-04-17 23:03:45 +0000 |
commit | 9d9da8b4ec09741c7482bea0da0ee8ac8c4366a4 (patch) | |
tree | 5b9850db16d9196a45c8a5fba47fc5a84e14f1e6 | |
parent | 5c7b4e1372ec25a8df3af20afedb63349dddfdca (diff) | |
download | vhc-9d9da8b4ec09741c7482bea0da0ee8ac8c4366a4.tar.gz vhc-9d9da8b4ec09741c7482bea0da0ee8ac8c4366a4.tar.bz2 vhc-9d9da8b4ec09741c7482bea0da0ee8ac8c4366a4.zip |
Ajoute possibilite de simuler!
-rw-r--r-- | src/gui/Main.cc | 103 | ||||
-rw-r--r-- | src/gui/ParticleRenderer.cc | 4 | ||||
-rw-r--r-- | src/gui/Stage.cc | 13 | ||||
-rw-r--r-- | src/gui/Stage.h | 3 | ||||
-rw-r--r-- | src/main/Accelerator.h | 33 | ||||
-rw-r--r-- | src/main/CurvedElement.h | 2 | ||||
-rw-r--r-- | src/main/Particle.cc | 10 | ||||
-rw-r--r-- | src/main/Particle.h | 4 |
8 files changed, 125 insertions, 47 deletions
diff --git a/src/gui/Main.cc b/src/gui/Main.cc index 012c6ef..cfef31e 100644 --- a/src/gui/Main.cc +++ b/src/gui/Main.cc @@ -8,6 +8,7 @@ #include <iostream> #include <QApplication> #include <QWidget> +#include <cmath> #include "Stage.h" #include "Accelerator.h" #include "StraightElement.h" @@ -15,29 +16,28 @@ #include "Particle.h" #include "FODO.h" #include "Vector3D.h" +#include "constants.h" #include <vector> using namespace vhc; -std::vector< Particle > createParticles(const Vector3D& position, int n) { +std::vector< Particle > createParticles(const Vector3D& position, int n, double mass = constants::ELECTRON_MASS, double charge = constants::E, double energy = 0, Vector3D direction = Vector3D::Null) { std::vector< Particle > v; + double r = 0.1; + for (int i = 0; i < n; ++i) { - double x = (rand() % 100) / 1000.0; - double y = (rand() % 100) / 1000.0; - double z = (rand() % 100) / 1000.0; - v.push_back(Particle(position + Vector3D(x, y, z), 1, 1)); + double x = (rand() % 1000) / 1000.0 * r; + double y = (rand() % 1000) / 1000.0 * sqrt(r * r - x * x); + double z = (rand() % 1000) / 1000.0 * sqrt(r * r - y * y - x * x);; + v.push_back(Particle(position + Vector3D(x, y, z), mass, charge, energy, direction)); } return v; } - -int main(int argc, char *argv[]) -{ - QApplication app(argc, argv); - +Accelerator* standardAccelerator() { FODO e1 = FODO(Vector3D(3, 2, 0), Vector3D(3, -2, 0), 0.2, 1.0, 5.0); Dipole e2 = Dipole(e1.getExitPosition(), Vector3D(2, -3, 0), 0.2, 1); FODO e3 = FODO(e2.getExitPosition(), Vector3D(-2, -3, 0), 0.2, 1, 5.0); @@ -46,29 +46,77 @@ int main(int argc, char *argv[]) Dipole e6 = Dipole(e5.getExitPosition(), Vector3D(-2, 3, 0), 0.2, 1); FODO e7 = FODO(e6.getExitPosition(), Vector3D(2, 3, 0), 0.2, 1.0, 5.0); Dipole e8 = Dipole(e7.getExitPosition(), e1.getEntryPosition(), 0.2, 1); - Accelerator acc; - acc.add(e1); - acc.add(e2); - acc.add(e3); - acc.add(e4); - acc.add(e5); - acc.add(e6); - acc.add(e7); - acc.add(e8); + Accelerator* acc = new Accelerator(); + acc->add(e1); + acc->add(e2); + acc->add(e3); + acc->add(e4); + acc->add(e5); + acc->add(e6); + acc->add(e7); + acc->add(e8); + + std::vector< Particle > ps = createParticles(e1.getEntryPosition(), 1000); + + for (int i = 0; i < ps.size(); ++i) { + acc->add(ps[i]); + } + + return acc; +} + +Accelerator* linear() { + FODO element = FODO(Vector3D(0, 0, 0), Vector3D(4, 0, 0), 0.2, 1.0, 5E9); + Accelerator* acc = new Accelerator(); + Element* celement = acc->add(element); + Particle e(Vector3D(0, 0.05, 0.01), constants::ELECTRON_MASS, constants::E, 14E9 * constants::E, Vector3D::i); + Particle* ce = acc->add(e); + ce->setElement(celement); + + return acc; +} + +Accelerator* singleDipole() { + Vector3D entry = Vector3D(0, 2, 0); + Vector3D exit = Vector3D(2, 0, 0); + double sectionRadius = 0.2; + double curvature = 0.5; + Vector3D direction = entry.cross(Vector3D::k); + double mass = constants::ELECTRON_MASS; + double charge = constants::E; + double energy = 14 * 1E9 * constants::E; - vhc::Particle p(e1.getEntryPosition(), 1, 1); - acc.add(p); - std::vector< Particle > ps = createParticles(e1.getEntryPosition(), 1000); + Particle particle = Particle(entry, mass, charge, energy, direction); + double Bz = particle.getGamma() * particle.getMass() * curvature * particle.getVelocity().norm() / particle.getCharge(); + std::cout << Bz << std::endl; + Dipole element = Dipole(entry, exit, sectionRadius, curvature, Vector3D::k * Bz); - for (int i = 0; i < ps.size(); ++i) { - acc.add(ps[i]); - } + Accelerator* acc = new Accelerator(); + Element* celement = acc->add(element); + + std::vector< Particle > ps = createParticles(element.getEntryPosition(), 10, constants::ELECTRON_MASS, constants::E, energy, direction); + + for (int i = 0; i < ps.size(); ++i) { + Particle* cparticle = acc->add(ps[i]); + cparticle->setElement(celement); + } + + Particle* cparticle = acc->add(particle); + cparticle->setElement(celement); + return acc; +} + + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); vhc::Stage window; - window.accelerator = &acc; + Accelerator* acc = singleDipole(); + window.accelerator = acc; window.showFullScreen(); //window.resize(QSize(500, 500)); @@ -76,4 +124,7 @@ int main(int argc, char *argv[]) window.show(); return app.exec(); + + delete acc; acc = NULL; + } diff --git a/src/gui/ParticleRenderer.cc b/src/gui/ParticleRenderer.cc index 0674d5b..38736c7 100644 --- a/src/gui/ParticleRenderer.cc +++ b/src/gui/ParticleRenderer.cc @@ -24,8 +24,8 @@ void ParticleRenderer::render(const Particle& particle) const { glPushMatrix(); glTranslated(particle.getPosition().getX(), particle.getPosition().getY(), particle.getPosition().getZ()); glBegin(GL_POINTS); - glVertex3d(0, 0, 0); - //util::sphere(0.001); + //glVertex3d(0, 0, 0); + util::sphere(0.01); glEnd(); glPopMatrix(); } diff --git a/src/gui/Stage.cc b/src/gui/Stage.cc index 8f5f729..6928b29 100644 --- a/src/gui/Stage.cc +++ b/src/gui/Stage.cc @@ -5,6 +5,7 @@ * Author: jakob */ +#include <iostream> #include "util.h" #include "Stage.h" @@ -20,7 +21,9 @@ Stage::Stage(QWidget* parent): particleRenderer(), displayMode(FILL), keys(0), - frameTime(0) { + frameTime(0), + h(1E-11), + paused(true) { timer = new QTimer(this); @@ -120,7 +123,7 @@ void Stage::paintGL() { camera.move(mv); } - + if (!paused) accelerator->step(h * frameTime / 1000 * 100); glColor3d(1,1,0); util::crosshair(); @@ -184,6 +187,12 @@ void Stage::keyPressEvent (QKeyEvent* event) { case Qt::Key_3: displayMode = POINTS; break; + case Qt::Key_Return: + accelerator->step(h); + break; + case Qt::Key_Space: + paused = !paused; + break; default: break; } diff --git a/src/gui/Stage.h b/src/gui/Stage.h index 6fbad10..445ce63 100644 --- a/src/gui/Stage.h +++ b/src/gui/Stage.h @@ -58,6 +58,9 @@ private: QTime time; int frameTime; //ms + double h; + bool paused; + ElementRenderer elementRenderer; ParticleRenderer particleRenderer; diff --git a/src/main/Accelerator.h b/src/main/Accelerator.h index 2c096fd..9b61922 100644 --- a/src/main/Accelerator.h +++ b/src/main/Accelerator.h @@ -7,14 +7,16 @@ #ifndef ACCELERATOR_H_ #define ACCELERATOR_H_ +#include <iostream> #include <vector> +#include "exceptions.h" #include "Vector3D.h" #include "Particle.h" #include "Element.h" namespace vhc { -// TODO Do we need any destructor ? + class Accelerator { private : @@ -55,13 +57,17 @@ public: virtual std::string toString() const; /** Copie un élément dans l'accélérateur. */ - void add(const Element& element) { - elementCollec.push_back(element.clone()); + Element* add(const Element& element) { + Element* e = element.clone(); + elementCollec.push_back(e); + return e; } /** Copie une particule dans l'accélérateur. */ - void add(const Particle& particle){ - particleCollec.push_back(particle.clone()); + Particle* add(const Particle& particle){ + Particle* p = particle.clone(); + particleCollec.push_back(p); + return p; } /** Efface tous les éléments et les particules. */ @@ -92,17 +98,26 @@ public: void step(double dt) { for (int i = 0; i < particleCollec.size(); ++i) { - Particle& particle = *(particleCollec[i]); - particle.applyMagneticForce(particle.getElement()->magneticFieldAt(particle.getPosition()), dt); + Particle* particle = particleCollec[i]; + + particle->applyMagneticForce(particle->getElement()->magneticFieldAt(particle->getPosition()), dt); - Vector3D a = particle.getForce() / (particle.getGamma() * particle.getMass()); - particle.setVelocity(particle.getVelocity() + a * dt); + Vector3D a = particle->getForce() / (particle->getGamma() * particle->getMass()); + particle->setVelocity(particle->getVelocity() + a * dt); } + + for (int i = 0; i < particleCollec.size(); ++i) { Particle& particle = *(particleCollec[i]); particle.setPosition(particle.getPosition() + particle.getVelocity() * dt); particle.setForce(Vector3D::Null); + + if (particle.getElement()->hasHit(particle)) std::cout << "Particle hit wall!" << std::endl; + if (particle.getElement()->isPast(particle)) { // si la particule est passee, qui sait si elle est dans l'element suivant? + if (!particle.getElement()->isConnected()) throw Exception("Element in accelerator not connected."); + else particle.setElement(particle.getElement()->getNext()); + } } } diff --git a/src/main/CurvedElement.h b/src/main/CurvedElement.h index 03be522..f11ff34 100644 --- a/src/main/CurvedElement.h +++ b/src/main/CurvedElement.h @@ -45,8 +45,10 @@ public: virtual ~CurvedElement(); //virtual CurvedElement* clone() const {return new CurvedElement(*this);} + //TODO !!! erreurs dans l'algorithme virtual bool hasHit(const Particle& particle) const; + //TODO !!! erreurs dans l'algorithme virtual bool isPast(const Particle& particle) const; /** Retourne la courbure. */ diff --git a/src/main/Particle.cc b/src/main/Particle.cc index c6c2af9..6598fbf 100644 --- a/src/main/Particle.cc +++ b/src/main/Particle.cc @@ -14,8 +14,8 @@ Particle::Particle(const Vector3D& position, double mass, double charge): velocity(0, 0, 0), force(0, 0, 0), mass(mass), - charge(charge) - //element(NULL) + charge(charge), + element(NULL) {}; @@ -25,8 +25,8 @@ Particle::Particle(const Vector3D& position, double mass, double charge, double gamma(energy / (mass * constants::C2)), force(0, 0, 0), mass(mass), - charge(charge) -// element(NULL) + charge(charge), + element(NULL) {}; Vector3D Particle::getPosition() const {return position;} @@ -79,7 +79,7 @@ Particle* Particle::clone() const {return new Particle(*this);} Element* Particle::getElement() const {return element;} -void Particle::setElement(Element* element) {element = element;} +void Particle::setElement(Element* element) {this->element = element;} } //vhc diff --git a/src/main/Particle.h b/src/main/Particle.h index dbd5a6d..78ee905 100644 --- a/src/main/Particle.h +++ b/src/main/Particle.h @@ -20,9 +20,7 @@ namespace vhc { class Element; //forward declaration /** Classe représentant une particule - * TODO !!! changer le unites en SI pour la representation interne. - * TODO poser question sur l'energie, gamma, qdm - * TODO rajouter un pointeur sur l'element dans lequel la particule se trouve*/ + * TODO poser question sur l'energie, gamma, qdm*/ class Particle: public Printable, public Cloneable { private: |