1 | |
---|
2 | #include "softwareMadx.h" |
---|
3 | #include "dataManager.h" |
---|
4 | |
---|
5 | #include "mathematicalConstants.h" |
---|
6 | #include "PhysicalConstants.h" |
---|
7 | |
---|
8 | softwareMadx::softwareMadx() : abstractSoftware() |
---|
9 | { |
---|
10 | nameOfSoftware_ = nomDeLogiciel("madx"); |
---|
11 | } |
---|
12 | |
---|
13 | softwareMadx::softwareMadx(string inputFileName,sectionToExecute* sect, dataManager* data) : abstractSoftware(inputFileName,sect,data) |
---|
14 | { |
---|
15 | nameOfSoftware_ = nomDeLogiciel("madx"); |
---|
16 | |
---|
17 | registerElement(nomdElements::RFgun,TBoolOk); |
---|
18 | registerElement(nomdElements::drift,TBoolOk); |
---|
19 | registerElement(nomdElements::mpole,TBoolOk); |
---|
20 | } |
---|
21 | |
---|
22 | bool softwareMadx::createInputFile(particleBeam* beamBefore,string workingDir) |
---|
23 | { |
---|
24 | cout << "***********************************" << endl; |
---|
25 | cout << " softwareMadx::createInputFile(...) " << endl << endl; |
---|
26 | dataManager_->consoleMessage(" softwareMadx::createInputFile"); |
---|
27 | |
---|
28 | string name = workingDir + inputFileName_; |
---|
29 | ofstream outfile; |
---|
30 | outfile.open(name.c_str(),ios::out); |
---|
31 | if(!outfile) { |
---|
32 | dataManager_->consoleMessage(" softwareMadx::createInputFile : error opening output stream "); |
---|
33 | cerr << " error opening output stream " << name << endl; |
---|
34 | return false; |
---|
35 | } |
---|
36 | |
---|
37 | sector* sector= getSectionToExecute()->getSector(); |
---|
38 | cout << " softwareMadx::createInputFile sector " << sector->getName() << endl; |
---|
39 | /////////////////////////////////////////// |
---|
40 | |
---|
41 | unsigned firstIndex= 0; |
---|
42 | string sbeam; |
---|
43 | abstractElement* elPtr; |
---|
44 | elPtr = getSectionToExecute()->getElements().front(); |
---|
45 | |
---|
46 | // le 1er elt n'est pas RFGun |
---|
47 | if(elPtr->getNomdElement().getElementType() != nomdElements::RFgun) { |
---|
48 | |
---|
49 | if(beamBefore == NULL) { |
---|
50 | // il n'y a pas de faisceau : erreur |
---|
51 | dataManager_->consoleMessage(" softwareMadx::createInputFile : no input beam, input file not created"); |
---|
52 | return false; |
---|
53 | } else { |
---|
54 | // il y a un faisceau : on le met au format madx |
---|
55 | dataManager_->consoleMessage(" softwareMadx::createInputFile : extract from input beam the quantities to be supplied to the madx BEAM command"); |
---|
56 | |
---|
57 | if (beamBefore->particleRepresentationOk()) { |
---|
58 | // le faisceau est représenté à la "parmela" |
---|
59 | cout << "passsage de la représentation macroparticules à la representation moments" << endl; |
---|
60 | beamBefore->buildMomentRepresentation(); |
---|
61 | } else if(beamBefore->momentRepresentationOk()) { |
---|
62 | // le faisceau est représenté à la "transport" |
---|
63 | cout << "le faisceau est représenté par moments" << endl; |
---|
64 | } else { |
---|
65 | // représentation ni "macroparticules" ni "moments" |
---|
66 | dataManager_->consoleMessage(" softwareMadx::createInputFile : that's unclear: the beam is represented neither by macroparticles neither by moments"); |
---|
67 | return false; |
---|
68 | } |
---|
69 | |
---|
70 | sbeam = BEAMcommand(beamBefore); |
---|
71 | } |
---|
72 | |
---|
73 | // le 1er elt est RFGun |
---|
74 | } else { |
---|
75 | firstIndex = 1; |
---|
76 | dataManager_->consoleMessage(" softwareMadx::createInputFile : set from RF cavity the quantities to be supplied to the madx BEAM command"); |
---|
77 | sbeam = RFGunData(elPtr->parametersToSoftware()); |
---|
78 | } |
---|
79 | /////////////////////////////////////////// |
---|
80 | |
---|
81 | // element label ////////////////////////// |
---|
82 | outfile << endl; // saut de ligne |
---|
83 | |
---|
84 | ostringstream os; |
---|
85 | os << "L: line=("; |
---|
86 | unsigned nElts= getSectionToExecute()->getElements().size(); |
---|
87 | for(unsigned k = firstIndex; k < nElts; k++) |
---|
88 | { |
---|
89 | elPtr = getSectionToExecute()->getElements()[k]; |
---|
90 | cout << " debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl; |
---|
91 | vector<statements> v= elPtr->parametersToSoftware(); |
---|
92 | outfile << inputFormat(v); |
---|
93 | |
---|
94 | if(k < nElts-1) |
---|
95 | os << elPtr->getLabel() << ","; |
---|
96 | else |
---|
97 | os << elPtr->getLabel() << ");" << endl; |
---|
98 | } |
---|
99 | |
---|
100 | // lines/sublines /////////////////////////////// |
---|
101 | outfile << endl; // saut de ligne |
---|
102 | |
---|
103 | // relection and repetition /////////////// |
---|
104 | os << "all: " << "line=(" << sector->getRepetitionNumber() << "*L);"; |
---|
105 | /////////////////////////////////////////// |
---|
106 | |
---|
107 | outfile << os.str() << endl; |
---|
108 | outfile << endl; // saut de ligne |
---|
109 | /////////////////////////////////////////// |
---|
110 | |
---|
111 | outfile << sbeam << endl; // beam commnd p46 |
---|
112 | outfile << "use, period = all;" << endl << endl; // action commands p45 |
---|
113 | |
---|
114 | outfile << "select,flag = twiss,column = name,s,betx,bety;" << endl; //p48 |
---|
115 | outfile << "twiss,save,centre,file = "+workingDir+"twiss.out;" << endl; //p51 |
---|
116 | outfile << "stop;" << endl; |
---|
117 | |
---|
118 | outfile.close(); |
---|
119 | dataManager_->consoleMessage("input file done for madx"); |
---|
120 | return true; |
---|
121 | } |
---|
122 | |
---|
123 | string softwareMadx::BEAMcommand(particleBeam* beam) const |
---|
124 | { |
---|
125 | // le 20 décembre : les commandes BEAM de madx sont : |
---|
126 | // PARTICLE (defaut POSITRON) |
---|
127 | // ENERGY (defaut 1 GeV) ou bien PC GeV/c |
---|
128 | // EX et EY (defaut 1 m.rad) |
---|
129 | // ET (defaut 1 m) |
---|
130 | |
---|
131 | ostringstream os; |
---|
132 | os << "beam, PARTICLE = ELECTRON"; |
---|
133 | |
---|
134 | double pc = beam->getP0Transport(); |
---|
135 | os << ", ENERGY = " << pc; // en GeV |
---|
136 | |
---|
137 | const beam2Moments& mts = beam->getTransportMoments(); |
---|
138 | const vector< vector<double> > rij= mts.getMoments(); |
---|
139 | |
---|
140 | // emittance (x,x') |
---|
141 | double r10= rij.at(1).at(0); // r10 et s10 = s01= r10*r00*r11 |
---|
142 | double rac= 0.0; |
---|
143 | if(r10*r10 < 1.0) rac= sqrt(1.0-r10*r10); |
---|
144 | double EX = rij.at(0).at(0)*rij.at(1).at(1)*rac; // r00*r11*sqrt(1-s01*s10) |
---|
145 | // r00 en cm (= 1E-02 m) et r11 en mrad (= 1E-03 rad) |
---|
146 | os << ", EX = " << EX*1.E-05; |
---|
147 | |
---|
148 | // emittance (y,y') |
---|
149 | double r32= rij.at(3).at(2); // r32 et s32 = s23= r32*r22*r33 |
---|
150 | rac= 0.0; |
---|
151 | if(r32*r32 < 1.0) rac= sqrt(1.0-r32*r32); |
---|
152 | double EY = rij.at(2).at(2)*rij.at(3).at(3)*rac; // r22*r33*sqrt(1-s23*s32) |
---|
153 | os << ", EY = " << EY*1.E-05; |
---|
154 | |
---|
155 | os << ";"; |
---|
156 | return os.str(); |
---|
157 | } |
---|
158 | |
---|
159 | string softwareMadx::RFGunData(const vector<statements>& v) const |
---|
160 | { |
---|
161 | //NPART = v.at(1).second.at(0); |
---|
162 | //SIGT (m) = v.at(2).second.at(0) (ps) |
---|
163 | //?? = sigma_r = v.at(2).second.at(1); en cm |
---|
164 | //EX (m.rad) = v.at(3).second.at(0) (pi.mm.mrad) |
---|
165 | //EY (m.rad) = v.at(3).second.at(1) (pi.mm.mrad) |
---|
166 | //ENERGY (GeV) = v.at(4).second.at(0) (MeV) |
---|
167 | //SIGE (GeV) = v.at(4).second.at(1) (MeV) |
---|
168 | //CHARGE = v.at(6).second.at(0); |
---|
169 | |
---|
170 | ostringstream os; |
---|
171 | os << "beam, PARTICLE = ELECTRON"; |
---|
172 | |
---|
173 | // masse au repos (en GeV) E0 = 1.E-03*EREST_MeV |
---|
174 | // energie cinetique (en GeV) T = 1.E-03*E_cin |
---|
175 | // l'energie totale (en Gev) W = E0+T |
---|
176 | double W = 1.E-03*(EREST_MeV + atof(v.at(4).second.at(0).c_str())); |
---|
177 | os << ", ENERGY = " << W; // total energy in [Gev] |
---|
178 | |
---|
179 | // pi*EX = 1E-06*emit_x en pi.m.rad |
---|
180 | double EX = 1.E-06*atof(v.at(3).second.at(0).c_str())/PI; |
---|
181 | os << ", EX = " << EX; // horizontal emittance in [rad.m] |
---|
182 | double EY = 1.E-06*atof(v.at(3).second.at(1).c_str())/PI; |
---|
183 | os << ", EY = " << EY; // vertical emittance in [rad.m] |
---|
184 | |
---|
185 | // SIGT = c*sigma_t*1E-12 |
---|
186 | double SIGT = 1.E-04*CLIGHT_E8*atof(v.at(2).second.at(0).c_str()); |
---|
187 | os << ", SIGT = " << SIGT; // bunch length in [m] |
---|
188 | |
---|
189 | // SIGE = sigma_Ecin/(p0*c) ou p0 impulsion de la paricule de réf. |
---|
190 | // os << ", SIGE = " << atof(v.at(4).second.at(1).c_str()); |
---|
191 | |
---|
192 | os << ";"; |
---|
193 | return os.str(); |
---|
194 | } |
---|
195 | |
---|
196 | string softwareMadx::inputFormat(const vector<statements>& v) const |
---|
197 | { |
---|
198 | // v.at(0).first = "labelsGenericSpecific" |
---|
199 | // v.at(0).second = vector<string> de 2 elements (type de l'element,label) |
---|
200 | string keyword= v.at(0).second.at(0); |
---|
201 | string label= v.at(0).second.at(1); |
---|
202 | ostringstream os; |
---|
203 | if(keyword == "drift") { |
---|
204 | double length = atof(v.at(1).second.at(0).c_str()); |
---|
205 | os << label << ":" << " drift, l=" << 0.01*length<< ";" << endl; |
---|
206 | |
---|
207 | } else if(keyword == "mpole") { |
---|
208 | double x[9]= {0.0}; // 0 <= n <= 9 in user's manual of MAD program |
---|
209 | int n= atoi(v.at(1).second.at(0).c_str()); |
---|
210 | x[n]= atof(v.at(1).second.at(1).c_str()); |
---|
211 | os << label << ":" << " multipole, knl={" << x[0]; |
---|
212 | for(int i = 1; i <= n; i++) { |
---|
213 | os << "," << x[i]; |
---|
214 | } |
---|
215 | os <<"};" << endl; |
---|
216 | |
---|
217 | } else { |
---|
218 | cout << "softwareMadx::inputFormat ERROR : element type= " << keyword << " not defined" << endl; |
---|
219 | } |
---|
220 | |
---|
221 | return os.str(); |
---|
222 | } |
---|
223 | |
---|
224 | bool softwareMadx::execute(string workingDir) |
---|
225 | { |
---|
226 | cout << "***********************************" << endl; |
---|
227 | cout << " softwareMadx::execute(...) " << endl << endl; |
---|
228 | |
---|
229 | dataManager_->consoleMessage(" softwareMadx::execute"); |
---|
230 | bool status= true; |
---|
231 | |
---|
232 | ostringstream sortie; |
---|
233 | sortie << " run madx " << endl; |
---|
234 | |
---|
235 | string mjob = workingDir + "madx64"; |
---|
236 | mjob += string(" < "); |
---|
237 | mjob += workingDir + inputFileName_; |
---|
238 | cout << " job madx = " << mjob << endl; |
---|
239 | |
---|
240 | string resultOfRun; |
---|
241 | bool success = launchJob(mjob,resultOfRun); |
---|
242 | // xx sortie << resultOfRun << endl; |
---|
243 | if ( !success ) { |
---|
244 | //sortie << " launching of madx failed " << endl; |
---|
245 | status = false; |
---|
246 | } else { |
---|
247 | sortie << " madx finished normally" << endl; |
---|
248 | string nameOut = workingDir + "madx.output"; |
---|
249 | ofstream outfile; |
---|
250 | outfile.open(nameOut.c_str(),ios::out); |
---|
251 | if (!outfile) { |
---|
252 | sortie << " error in opening madx output stream " << nameOut << endl; |
---|
253 | status = false; |
---|
254 | } else { |
---|
255 | // on copie la sortie dans un fichier 'madx.out' |
---|
256 | outfile << resultOfRun << endl; |
---|
257 | outfile.close(); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | dataManager_->consoleMessage(sortie.str()); |
---|
262 | return status; |
---|
263 | } |
---|
264 | |
---|
265 | // bool softwareMadx::execute(string workingDir) |
---|
266 | // { |
---|
267 | // cout << "***********************************" << endl; |
---|
268 | // cout << " softwareMadx::execute(...) " << endl << endl; |
---|
269 | |
---|
270 | // dataManager_->consoleMessage(" softwareMadx::execute"); |
---|
271 | // return false; |
---|
272 | // } |
---|
273 | |
---|
274 | bool softwareMadx::buildBeamAfterElements(string workingDir) |
---|
275 | { |
---|
276 | cout << "***********************************" << endl; |
---|
277 | cout << " softwareMadx::buildBeamAfterElements(...) " << endl << endl; |
---|
278 | |
---|
279 | dataManager_->consoleMessage(" softwareMadx::buildBeamAfterElements: not programmed :O(("); |
---|
280 | return false; |
---|
281 | } |
---|