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

Last change on this file since 495 was 493, checked in by lemeur, 10 years ago

refection generale des secteurs et applications de softwares (suite)

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