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

Last change on this file since 455 was 455, checked in by garnier, 11 years ago

grosse modification pour intégrer les sections

File size: 6.6 KB
Line 
1
2#include "softwareMadx.h"
3#include "dataManager.h"
4
5softwareMadx::softwareMadx() : abstractSoftware()
6{
7  nameOfSoftware_ = nomDeLogiciel("madx");
8}
9
10softwareMadx::softwareMadx(string inputFileName,sectionToExecute* sect) : abstractSoftware(inputFileName,sect)
11{
12  nameOfSoftware_ = nomDeLogiciel("madx");
13
14  registerElement(nomdElements::drift,TBoolOk);
15  registerElement(nomdElements::mpole,TBoolOk);
16}
17
18bool softwareMadx::createInputFile(particleBeam* beamBefore, string workingDir)
19{
20  cout << "***********************************" << endl;
21  cout << " softwareMadx::createInputFile(...) " << endl << endl;
22
23  dataManager_->consoleMessage(" softwareMadx::createInputFile");
24
25  string name = workingDir + inputFileName_;
26  ofstream outfile;
27  outfile.open(name.c_str(),ios::out);
28  if(!outfile) {
29    dataManager_->consoleMessage(" softwareMadx::createInputFile : error opening output stream ");
30    cerr << " error opening output stream " << name << endl;
31    return false;
32  }
33
34  cout << " softwareMadx::createInputFile(...) lecture des elts " << endl;
35 
36  // element label //////////////////////////
37
38  outfile << endl; // saut de ligne
39
40  abstractElement* elPtr;
41  std::vector <abstractElement*> elements = getSectionToExecute()->getElements();
42  for(unsigned k = 0; k < elements.size(); k++)
43    {
44      elPtr = elements[k];
45      //cout << " debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl;
46      vector<statements> v= elPtr->parametersToSoftware();
47      outfile << inputFormat(v);
48    }
49
50  // sublines ///////////////////////////////
51
52  outfile << endl; // saut de ligne
53
54  //_______________________________________________________
55  // FIXME Francois !!!!!
56  //_______________________________________________________
57 /*
58  vector<statements> u = sectParamPtr_->getSectors();
59  for(unsigned int i = 0; i < u.size(); i++) {
60    ostringstream os;
61    os << u.at(i).first << ": line=(";
62    unsigned ai = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(0));
63    unsigned bi = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(1));
64
65    std::vector <abstractElement*> elements = getSectionToExecute()->getElements();
66    for(unsigned k = 0; k < elements.size(); k++) {
67      elPtr = elements[k];
68     
69      if(k >= ai && k <= bi) {
70        if(k == bi)
71          os << elPtr->getLabel() <<");";
72        else
73          os << elPtr->getLabel() <<",";
74      }
75    }// k
76
77    outfile << os.str() << endl;
78  }// i
79
80  // relection and repetition ///////////////
81
82  outfile << endl; // saut de ligne
83
84  string *str= NULL;
85  if(u.size() > 0) {
86    str = new string[u.size()];
87    for(unsigned int i = 0; i < u.size(); i++) str[i]= "false";
88  }
89
90  ostringstream os;
91  os << "all: " << "line=(";
92  for(unsigned k = 0; k < elements.size(); k++) {
93    elPtr = elements[k];
94    bool isMember = false;
95
96    for(unsigned int i = 0; i < u.size(); i++) {
97      unsigned ai = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(0));
98      unsigned bi = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(1));
99      if(k >= ai && k <= bi) {
100        isMember = true;
101
102        if(str[i] == "false") {
103          //int reflection = atoi(u.at(i).second.at(2).c_str());
104          int reflection = ( (u.at(i).second.at(2) == "Unchecked") ? 0:1 );
105          int repeat = atoi(u.at(i).second.at(3).c_str());
106          if(reflection > 0)
107            os << "-" << repeat << "*" << u.at(i).first;
108          else
109            os << repeat << "*" << u.at(i).first;
110
111          if(i < u.size()-1) os << ", ";
112        }
113
114        str[i] = "true";
115        break;
116      }
117    }// i
118   
119    if(isMember && k == (elements.size()-1)) os << ");";
120   
121    if(!isMember) {
122      if(k == (elements.size()-1)) os << elPtr->getLabel() << ");";
123      else os << elPtr->getLabel() << ",";
124    }
125  }// k
126
127  outfile << os.str() << endl;
128  ///////////////////////////////////////////
129     
130  outfile << endl; // saut de ligne
131
132  outfile << "beam;" << endl; // beam data p46
133  outfile << "use, period = all;" << endl << endl; // action commands p45
134 
135  outfile << "select,flag = twiss,column = name,s,betx,bety;" << endl; //p48
136  outfile << "twiss,save,centre,file = "+workingDir+"twiss.out;" << endl; //p51
137  outfile << "stop;" << endl;
138
139  */
140  //_______________________________________________________
141  // FIXME Francois !!!!!
142  //_______________________________________________________
143
144  outfile.close();
145  dataManager_->consoleMessage("input file done for madx");
146  return true;
147}
148
149string softwareMadx::inputFormat(const vector<statements>& v) const 
150{
151  // v.at(0).first = "labelsGenericSpecific"
152  // v.at(0).second = vector<string> de 2 elements (type de l'element,label)
153  string keyword= v.at(0).second.at(0);
154  string label= v.at(0).second.at(1);
155  ostringstream os;
156  if(keyword == "drift") {
157    double length = atof(v.at(1).second.at(0).c_str());
158    os << label << ":" << " drift, l=" << 0.01*length<< ";" << endl;
159
160  } else if(keyword == "mpole") {
161    double x[9]= {0.0}; // 0 <= n <= 9 in user's manual of MAD program 
162    int n= atoi(v.at(1).second.at(0).c_str());
163    x[n]= atof(v.at(1).second.at(1).c_str());
164    os << label << ":" << " multipole, knl={" << x[0];
165    for(int i = 1; i <= n; i++) {
166      os << "," << x[i];
167    }
168    os <<"};" << endl;
169   
170  } else {
171    cout << "softwareMadx::inputFormat ERROR : element type= " << keyword << " not defined" << endl;
172  }
173 
174  return os.str();
175}
176
177bool softwareMadx::execute(string workingDir)
178{
179  cout << "***********************************" << endl;
180  cout << " softwareMadx::execute(...) " << endl << endl;
181
182  dataManager_->consoleMessage(" softwareMadx::execute");
183  bool status= true;
184
185  ostringstream sortie;
186  sortie << " run madx " << endl;
187
188  string mjob = workingDir + "madx64";
189  mjob += string(" <  ");
190  mjob += workingDir + inputFileName_;
191  cout << " job madx = " << mjob << endl;
192 
193  string resultOfRun;
194  bool success = launchJob(mjob,resultOfRun);
195  // xx sortie << resultOfRun << endl;
196  if ( !success ) {
197    //sortie << " launching of madx failed " << endl;
198    status = false;
199  } else {
200    sortie << " madx finished normally" << endl;
201    string nameOut = workingDir + "madx.output";
202    ofstream outfile;
203    outfile.open(nameOut.c_str(),ios::out);
204    if (!outfile) {
205      sortie << " error in opening madx output stream " << nameOut << endl;
206      status = false;
207    } else {
208      // on copie la sortie dans un fichier 'madx.out'
209      outfile << resultOfRun << endl;
210      outfile.close();
211    }
212  }
213
214  dataManager_->consoleMessage(sortie.str());
215  return status;
216}
217
218bool softwareMadx::buildBeamAfterElements(string workingDir)
219{
220  cout << "***********************************" << endl;
221  cout << " softwareMadx::buildBeamAfterElements(...) " << endl << endl;
222
223  dataManager_->consoleMessage(" softwareMadx::buildBeamAfterElements");
224  return false;
225}
Note: See TracBrowser for help on using the repository browser.