source: ZHANGProjects/ICOSIM/CPP/trunk/source/CrystalCollimator.cc @ 15

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

(1) Modify the bug to connect to the Fortran files. (2) Fix the bug of the definition type of the random functions used in Fortran file crystal_dan_FINAL....f.

File size: 4.8 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4#include <vector>
5#include <string>
6#include <cmath>
7#include "CrystalCollimator.h"
8using namespace std;
9
10int CrystalCollimator::crystaloutflag = 0;
11
12
13CrystalCollimator::CrystalCollimator(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 double& hgap, const double& hgap2, const double& collang, const double& pdepth, const double& pdepth2, const double& tcang, const double& nsig)
14    : 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)
15{};
16
17CrystalCollimator::CrystalCollimator(Element elt, const double& tcang, const double& nsig, const string& meth)
18    : Collimator(elt, tcang, nsig, meth)
19{};
20
21CrystalCollimator::CrystalCollimator(const CrystalCollimator& obj)
22    : Collimator(obj)
23{};
24
25void CrystalCollimator::affiche()
26{
27
28    cout << "Crystal collimator: " << endl;
29
30    this->Collimator::affiche();
31};
32
33
34void CrystalCollimator::collipassCrystal(vector <Particle>& bunch, const double& betgam, const int& pas, string outputpath)
35{
36
37    //Calculate proton energy
38    double Mproton(0.93827231); //rest mass [GeV]
39
40    vector <double> betgamProton;
41    vector <double> betaProton;
42    vector <double> Eproton;
43
44    for (int j(0); j < bunch.size(); ++j) {
45        betgamProton.push_back(betgam * (1 + bunch[j].coordonnees[0][4])); //relativistic beta * relativistic gamma
46        betaProton.push_back(sqrt((betgamProton[j]*betgamProton[j]) / ((betgamProton[j]*betgamProton[j]) + 1))); //relativistic beta
47        Eproton.push_back(Mproton * betgamProton[j] / betaProton[j]); //total energy [GeV]
48    }
49
50
51    ofstream sortie;
52    string filename;
53    filename = outputpath + "/ascii_before.csv";
54
55    sortie.open(filename.c_str());
56
57    if (sortie.fail()) {
58        cerr << "Warning: problem with the file " << filename << "!" << endl;
59    } else {
60
61        int col(15);
62
63        for (int k(0); k < bunch.size(); ++k) {
64
65            sortie  << bunch[k].coordonnees[0][0] << "," << bunch[k].coordonnees[0][2] << "," << bunch[k].coordonnees[0][1] << "," << bunch[k].coordonnees[0][3] << "," << Eproton[k] << endl;
66        }
67
68        sortie.close();
69
70    }
71
72    ofstream out;
73    string file;
74    file = outputpath + "/crystal_output.out";
75
76    if (crystaloutflag == 0) {
77        out.open(file.c_str());
78        crystaloutflag = 1;
79    } else {
80        out.open(file.c_str(), ios::out | ios::app);
81    }
82
83    if (out.fail()) {
84        cerr << "Warning: problem writing in the file: " << file << "!" << endl;
85    } else {
86        out << this->NAME << endl;
87        out.close();
88    }
89
90
91    SimCrys sim(Crystal (C_orient,  IS, C_xmax, C_ymax, Cry_length, Rcurv), Partcrys (0)); //simulation of the passage through the crystal
92
93   // sim.general(sim, pas, outputpath, NAME, emitx0, emity0,enum, C_rotation, C_aperture, C_offset, C_tilt, Cry_tilt);
94   
95    sim.general(sim, pas, outputpath, Mirror, C_rotation, C_aperture, C_offset, C_tilt, Cry_tilt);
96
97    ifstream enter;
98    string file_in;
99    file_in = outputpath + "/ascii_after.csv";
100
101    enter.open(file_in.c_str());
102
103    if (enter.fail()) {
104        cerr << "Warning: problem reading the file " << file_in << "!" << endl;
105    } else {
106
107        for (int k(0); k < bunch.size(); ++k) {
108
109            string rest;
110
111            getline(enter, rest, ',');
112            bunch[k].coordonnees[1][0] = atof(rest.c_str());
113
114            getline(enter, rest, ',');
115            bunch[k].coordonnees[1][2] = atof(rest.c_str());
116
117            getline(enter, rest, ',');
118            bunch[k].coordonnees[1][1] = atof(rest.c_str());
119
120            getline(enter, rest, ',');
121            bunch[k].coordonnees[1][3] = atof(rest.c_str());
122
123            getline(enter, rest);
124            Eproton[k] = atof(rest.c_str());
125        }
126
127        enter.close();
128    }
129
130    vector <double> gamProton;
131    vector <double> betProton;
132
133    //we return to the initial referential
134    for (int j(0); j < Eproton.size(); ++j) {
135        gamProton.push_back(Eproton[j] / Mproton);
136        betProton.push_back(sqrt(1 - (1 / (gamProton[j]*gamProton[j]))));
137        bunch[j].coordonnees[1][4] = gamProton[j] * betProton[j] / betgam - 1;
138    }
139
140};
Note: See TracBrowser for help on using the repository browser.