source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/softwareMadx.cc @ 484

Last change on this file since 484 was 484, checked in by touze, 10 years ago

softawareMadx:: faisceau donné par l'élément beam

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