source: ZHANGProjects/ICOSIM/CPP/trunk/source/FlukaCollimator.cc @ 2

Last change on this file since 2 was 2, checked in by zhangj, 10 years ago

Initial import

File size: 4.9 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <stdbool.h>
4#include <unistd.h>
5#include <signal.h>
6#include <assert.h>
7#include <iostream>
8#include <fstream>
9#include <vector>
10#include <string>
11#include <cmath>
12#include "FlukaCollimator.h"
13using namespace std;
14
15FlukaCollimator::FlukaCollimator(const double& ALFX, const double& ALFY, const double& APER_1, const double& APER_2, const double& APER_3, const double& APER_4, const string& APERTYPE, const double& BETX, const double& BETY, const double& DPX, const double& DPY, const double& DX, const double& DY, const string& KEYWORD, const double& L, const double& MUX, const double& MUY, const string& NAME, const double& PTC, const double& PXC, const double& PYC, const double& S, const double& TC, const double& XC, const double& YC, const double& K0L, const double& K0SL, const double& K1L, const double& K1SL, const double& K2L, const double& K2SL, const string& PARENT, const string& meth, const long double& hgap, const long double& hgap2, const double& collang, const long double& pdepth, const long double& pdepth2, const double& tcang, const double& nsig)
16    : Collimator(ALFX, ALFY, APER_1, APER_2, APER_3, APER_4, APERTYPE, BETX, BETY, DPX, DPY, DX, DY, KEYWORD, L, MUX, MUY, NAME, PTC, PXC, PYC, S, TC, XC, YC, K0L, K0SL, K1L, K1SL, K2L, K2SL, PARENT, meth, hgap, hgap2, collang, pdepth, pdepth2, tcang, nsig)
17{};
18
19FlukaCollimator::FlukaCollimator(Element elt, const double& tcang, const double& nsig, const string& meth)
20    : Collimator(elt, tcang, nsig, meth)
21{};
22
23FlukaCollimator::FlukaCollimator(const FlukaCollimator& obj)
24    : Collimator(obj)
25{};
26
27void FlukaCollimator::affiche()
28{
29
30    cout << "Fluka collimator: " << endl;
31
32    this->Collimator::affiche();
33};
34
35#if defined(FLUKA)
36void FlukaCollimator::convertToFluka(const int& turn, particle_info_t& part, const Particle& p1, const double& p0)
37{
38
39    double Mion0(0.93827231);//mass of an ion [GeV]
40
41    part.t = p1.coordonnees[0][5];
42    part.id = p1.getidentification();
43    part.gen = 1;
44    part.weight = 1e0;
45    part.x = p1.coordonnees[0][0] * 1e2;//change from [m] to [cm]
46    part.y = p1.coordonnees[0][2] * 1e2;
47    part.z = 0e0;
48    part.tz = 1e0 / sqrt(p1.coordonnees[0][1] * p1.coordonnees[0][1] + p1.coordonnees[0][3] * p1.coordonnees[0][3] + 1e0);
49    part.tx = p1.coordonnees[0][1] * part.tz;
50    part.ty = p1.coordonnees[0][3] * part.tz;
51    part.aa = (int)p1.Ap0;
52    part.zz = (int)p1.Zp0;
53    part.m = Mion0;
54    part.pc = p0 * (1e0 + p1.coordonnees[0][4]);//linear momentum
55
56};
57
58
59void FlukaCollimator::convertFromFluka(Particle& p1, const particle_info_t& particle, double& p0)
60{
61
62    p1.setidentification(particle.id - 1);
63
64    cout << "En sortant de Fluka:" <<  p1.getidentification() << endl;
65
66    p1.coordonnees[0][0] = particle.x * 1e-2;//change from [cm] to [m]
67    p1.coordonnees[0][2] = particle.y * 1e-2;
68
69    p1.coordonnees[0][1] = particle.tx / particle.tz;
70    p1.coordonnees[0][3] = particle.ty / particle.tz;
71
72    p1.Ap0 = particle.aa;
73    p1.Zp0 = particle.zz;
74
75    p1.coordonnees[0][4] = (particle.pc - p0) / p0; //(p-p0)/p0
76    p1.coordonnees[0][5] = particle.t;
77
78};
79
80void FlukaCollimator::die(char* msg, flukaio_connection_t* conn)
81{
82    cerr << msg << endl;
83
84    if (conn) {
85        flukaio_disconnect(conn);
86        conn = NULL;
87    }
88
89    exit(1);
90}
91
92
93
94void FlukaCollimator::collipassfluka(vector <Particle>& bunchstart, vector <Particle>& bunchend, flukaio_connection_t* connection, const int& turn, const double& momentum)
95{
96
97    long i;
98    int n;
99    flukaio_message_t msg;
100    particle_info_t part;
101
102    Particle temp;
103    double p0;
104
105    p0 = momentum / 1e09;
106
107
108    for (i = 0; i < bunchstart.size(); ++i) {
109        convertToFluka(turn, part, bunchstart[i], p0);
110        part.id = (unsigned int)i + 1;
111        cout << ">";
112
113        n = flukaio_send_particle(connection, &part);
114        if (n < 0) {
115            die("Error occurred sending P", connection);
116            return;
117        }
118
119        // Read incomming messages
120        while (flukaio_receive_message(connection, &msg) > 0) {
121            if (msg.type == N_PART) {
122                convertFromFluka(temp, msg.data.particle, p0);
123                bunchend.push_back(temp);
124                cout << "<";
125            }
126        }
127    }
128
129    n = flukaio_send_eot(connection, turn + 1);
130
131    if (n < 0) {
132        die("Error occurred sending T", connection);
133        return;
134    }
135
136    // Read incomming messages until end of turn
137    do {
138        n = flukaio_wait_message(connection, &msg);
139        if (n == -1) {
140            cout << connection->in_buffer_len << endl;
141            cout << connection->out_buffer_len << endl;
142            die("Server timeout when waiting end of computation.", connection);
143        }
144        if (msg.type == N_PART) {
145            convertFromFluka(temp, msg.data.particle, p0);
146            bunchend.push_back(temp);
147            cout << "<";
148        }
149    } while (msg.type != N_TURN);
150
151    cout << "Turn finished" << endl;
152
153    cout << "Done sending particles" << endl;
154
155};
156#endif
Note: See TracBrowser for help on using the repository browser.