1 | #include <stdio.h> |
---|
2 | #include <fstream> |
---|
3 | |
---|
4 | #include "dataManager.h" |
---|
5 | #include "mathematicalConstants.h" |
---|
6 | #include "PhysicalConstants.h" |
---|
7 | |
---|
8 | #include "GWt_pspaApplication.h" |
---|
9 | #include "GWt_console.h" |
---|
10 | |
---|
11 | #include "softwareParmela.h" |
---|
12 | #include "softwareTransport.h" |
---|
13 | #include "softwareGenerator.h" |
---|
14 | #include "softwareGenerator.h" |
---|
15 | #include "softwareUsersprogram.h" |
---|
16 | #include "softwareTest.h" |
---|
17 | #include "softwareMadx.h" //xx |
---|
18 | #include "softwareUnknown.h" //xx |
---|
19 | |
---|
20 | #include <boost/filesystem.hpp> |
---|
21 | |
---|
22 | #include "UAP/UAPUtilities.hpp" |
---|
23 | #include "AML/AMLReader.hpp" |
---|
24 | |
---|
25 | #define BAVARD 0 |
---|
26 | |
---|
27 | dataManager::dataManager(PspaApplication* pspa) : |
---|
28 | currentBeam_(NULL), |
---|
29 | pspa_ (pspa) |
---|
30 | {} |
---|
31 | |
---|
32 | dataManager::~dataManager() |
---|
33 | { |
---|
34 | unsigned k; |
---|
35 | for (k=0; k < jobList_.size();k++) { |
---|
36 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
37 | } |
---|
38 | if ( currentBeam_ == NULL ) delete currentBeam_; |
---|
39 | } |
---|
40 | |
---|
41 | void dataManager::consoleMessage(string message) |
---|
42 | { |
---|
43 | GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console")); |
---|
44 | if (console) console->addConsoleMessage(message + "\n"); |
---|
45 | pspa_->processEvents(); |
---|
46 | } |
---|
47 | |
---|
48 | string dataManager::getLabelFromElementNumero(int numero) |
---|
49 | { |
---|
50 | abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1); |
---|
51 | if ( ptr == NULL ) return ""; |
---|
52 | return ptr->getLabel(); |
---|
53 | } |
---|
54 | |
---|
55 | int dataManager::getNumeroFromElementLabel(string label) |
---|
56 | { |
---|
57 | int index = -1; |
---|
58 | for (int k = 0; k < getBeamLineSize() ; k++) { |
---|
59 | if (pspa_->getBeamLine()->getAbstractElement(k) != NULL) { |
---|
60 | if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label ) |
---|
61 | { |
---|
62 | index = (int)k + 1; |
---|
63 | return index; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | return index; |
---|
68 | } |
---|
69 | |
---|
70 | abstractElement* dataManager::getElementPointerFromNumero(int k) |
---|
71 | { |
---|
72 | return pspa_->getBeamLine()->getAbstractElement(k-1); |
---|
73 | } |
---|
74 | |
---|
75 | sectionToExecute* dataManager::addSectionToExecute(abstractElement* debut,int debutIndex, abstractElement* fin, int finIndex, nomDeLogiciel logiciel) |
---|
76 | { |
---|
77 | #if BAVARD > 0 |
---|
78 | cout << "***********************************" << endl; |
---|
79 | cout << " dataManager::addSectionToExecute " << endl << endl; |
---|
80 | #endif |
---|
81 | |
---|
82 | abstractSoftware* prog; |
---|
83 | string inputFileName; |
---|
84 | if(logiciel == nomDeLogiciel::parmela) { |
---|
85 | inputFileName = "parmin"; |
---|
86 | prog = new softwareParmela(inputFileName, &globParam_, this); |
---|
87 | } else if (logiciel == nomDeLogiciel::transport) { |
---|
88 | inputFileName = "transport.input"; |
---|
89 | prog = new softwareTransport(inputFileName, &globParam_, this); |
---|
90 | } else if (logiciel == nomDeLogiciel::generator) { |
---|
91 | inputFileName = "generator.in"; |
---|
92 | prog = new softwareGenerator(inputFileName, &globParam_, this); |
---|
93 | } else if (logiciel == nomDeLogiciel::madx) { |
---|
94 | inputFileName = "madx.input"; |
---|
95 | prog = new softwareMadx(inputFileName,§orParam_,this); |
---|
96 | } else if (logiciel == nomDeLogiciel::usersprogram) { |
---|
97 | inputFileName = "dummy"; |
---|
98 | prog = new softwareUsersprogram(inputFileName, &globParam_, this); |
---|
99 | } else if (logiciel == nomDeLogiciel::test) { |
---|
100 | prog = new softwareTest(inputFileName, &globParam_, this); |
---|
101 | } else { |
---|
102 | prog = new softwareUnknown(); // xx |
---|
103 | } |
---|
104 | |
---|
105 | jobList_.push_back(new sectionToExecute(debut,debutIndex,fin,finIndex,prog)); |
---|
106 | return jobList_.back(); |
---|
107 | } |
---|
108 | |
---|
109 | void dataManager::clearSectionToExecute() |
---|
110 | { |
---|
111 | unsigned k; |
---|
112 | for(k = 0; k < jobList_.size(); k++) |
---|
113 | { |
---|
114 | if ( jobList_[k] != NULL ) clearSectionToExecute(k); |
---|
115 | } |
---|
116 | jobList_.clear(); |
---|
117 | } |
---|
118 | |
---|
119 | void dataManager::clearSectionToExecute(int a) |
---|
120 | { |
---|
121 | cout << " dataManager::clearSectionToExecute : effacement de la section d'index = " << a << endl; |
---|
122 | if (a < 0) return; |
---|
123 | if (a >= (int)jobList_.size()) return; |
---|
124 | |
---|
125 | // lors de la creation de la section on a fait un 'new' d'un |
---|
126 | // softwareXXXX : on fait ici le 'delete' |
---|
127 | |
---|
128 | const abstractSoftware* soft = jobList_.at(a)->getSoftware(); |
---|
129 | if ( soft != NULL ) delete soft; |
---|
130 | jobList_.erase (jobList_.begin()+a); |
---|
131 | } |
---|
132 | |
---|
133 | void dataManager::initializeExecution() |
---|
134 | { |
---|
135 | string workingDir = pspa_->getWorkingDir(); |
---|
136 | if (workingDir == "") { |
---|
137 | return; |
---|
138 | } |
---|
139 | removeFile(workingDir + "parmdesz"); |
---|
140 | removeFile(workingDir + "parmin"); |
---|
141 | removeFile(workingDir + "parin.input0"); |
---|
142 | removeFile(workingDir + "transport.input"); |
---|
143 | removeFile(workingDir + "transport.output"); |
---|
144 | removeFile(workingDir + "generator.in"); |
---|
145 | removeFile(workingDir + "faisceau.ini"); |
---|
146 | removeFile(workingDir + "generator.output"); |
---|
147 | diagnosticBeam_.clear(); |
---|
148 | indexElementToIndexDiag_.clear(); |
---|
149 | currentBeam_ = NULL; |
---|
150 | firstComputedElemNumero_ = getBeamLineSize(); |
---|
151 | lastComputedElemNumero_ = 1; |
---|
152 | clearSectionToExecute(); |
---|
153 | } |
---|
154 | |
---|
155 | void dataManager::removeFile(string nameOfFile) |
---|
156 | { |
---|
157 | ifstream fileExists; |
---|
158 | fileExists.open(nameOfFile.c_str(), ios::in); |
---|
159 | if (fileExists) { |
---|
160 | fileExists.close(); |
---|
161 | remove(nameOfFile.c_str()); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | bool dataManager::executeAll() |
---|
166 | { |
---|
167 | #if BAVARD > 0 |
---|
168 | cout << "***********************************" << endl; |
---|
169 | cout << " dataManager::executeAll() " << endl << endl; |
---|
170 | #endif |
---|
171 | |
---|
172 | bool success = true; |
---|
173 | abstractSoftware* softw = NULL; |
---|
174 | string workingDir = pspa_->getWorkingDir(); |
---|
175 | |
---|
176 | // on verifie la consecution des sections |
---|
177 | int lastel = 0; |
---|
178 | for(unsigned k = 0; k < jobList_.size(); k++) { |
---|
179 | if ( jobList_[k]->getElementNumberInSection() != lastel +1 ) { |
---|
180 | consoleMessage("dataManager::executeAll ERROR : sections should be consecutive "); |
---|
181 | return false; |
---|
182 | } else { |
---|
183 | lastel = jobList_[k]->getLastElementNumberInSection(); |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | unsigned debut; |
---|
188 | unsigned fin; |
---|
189 | for(unsigned k = 0; k < jobList_.size(); k++) { |
---|
190 | |
---|
191 | #if BAVARD > 0 |
---|
192 | cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl; |
---|
193 | #endif |
---|
194 | |
---|
195 | debut = jobList_[k]->getElementNumberInSection(); |
---|
196 | fin = jobList_[k]->getLastElementNumberInSection(); |
---|
197 | softw = jobList_[k]->getSoftware(); |
---|
198 | |
---|
199 | //cout << "k= " << k << ", de " << jobList_[k]->getFirstElement()->getLabel() << " Ã " << jobList_[k]->getLastElement()->getLabel() << " avec " << softw->getName() << endl; |
---|
200 | |
---|
201 | if (softw == NULL) { |
---|
202 | success = false; |
---|
203 | consoleMessage("dataManager::executeAll : unknown software"); |
---|
204 | break; |
---|
205 | } |
---|
206 | |
---|
207 | success = softw->createInputFile(currentBeam_,debut,fin,workingDir); |
---|
208 | if ( success ) { |
---|
209 | success = softw->execute(workingDir); |
---|
210 | if ( success ) { |
---|
211 | success = softw->buildBeamAfterElements(workingDir); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | if ( success ) { |
---|
216 | currentBeam_ = &diagnosticBeam_.at(indexElementToIndexDiag_.back()); |
---|
217 | cout << " execute termine avec succes " << endl; |
---|
218 | } else { |
---|
219 | currentBeam_ = NULL; |
---|
220 | cout << " execute termine en ECHEC " << endl; |
---|
221 | } |
---|
222 | if ( !success ) break; |
---|
223 | |
---|
224 | if ( debut < firstComputedElemNumero_ ) firstComputedElemNumero_ = debut; |
---|
225 | if ( fin > lastComputedElemNumero_ ) lastComputedElemNumero_ = fin; |
---|
226 | |
---|
227 | #if BAVARD > 0 |
---|
228 | cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl; |
---|
229 | cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl; |
---|
230 | for (unsigned j = debut; j <= fin; j++) { |
---|
231 | abstractElement* elPtr= getElementPointerFromNumero(j); |
---|
232 | cout << "[" << j << "] " << elPtr->getNomdElement().getExpandedName() << endl; |
---|
233 | } |
---|
234 | #endif |
---|
235 | |
---|
236 | } //k |
---|
237 | // if ( currentBeam_ ) { |
---|
238 | // string aml_file = workingDir + "/" + "faisceau_final" + ".aml"; |
---|
239 | // currentBeam_->writeToAMLFile(aml_file); |
---|
240 | // // TESTS |
---|
241 | // currentBeam_->readFromAMLFile(aml_file); |
---|
242 | // } |
---|
243 | cout << " dataManager::executeAll() : premier element = " << firstComputedElemNumero_ << " dernier = " << lastComputedElemNumero_ << endl; |
---|
244 | |
---|
245 | return success; |
---|
246 | } |
---|
247 | |
---|
248 | void dataManager::saveConfiguration(string folder, string nameOfFile) |
---|
249 | { |
---|
250 | ofstream outfile; |
---|
251 | //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save"; |
---|
252 | cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl; |
---|
253 | cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl; |
---|
254 | cout << " et qu'on puisse restaurer normalement" << endl; |
---|
255 | |
---|
256 | string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; |
---|
257 | // make dir if not exist |
---|
258 | boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/"); |
---|
259 | |
---|
260 | outfile.open(name.c_str(), ios::out); |
---|
261 | if (!outfile) { |
---|
262 | cerr<<" ERROR opening output file for persistency "<<name<<endl; |
---|
263 | } |
---|
264 | |
---|
265 | outfile << globParam_.FileOutputFlow(); |
---|
266 | outfile << sectorParam_.FileOutputFlow(); |
---|
267 | |
---|
268 | abstractElement* elPtr; |
---|
269 | for(int k = 0; k < getBeamLineSize() ; k++) |
---|
270 | { |
---|
271 | elPtr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
272 | outfile << elPtr->FileOutputFlow(); |
---|
273 | } |
---|
274 | outfile.close(); |
---|
275 | } |
---|
276 | |
---|
277 | // ecriture sur fichier AML |
---|
278 | void dataManager::writeToAMLFile(string fileName) |
---|
279 | { |
---|
280 | UAPNode* uap_root = NULL; |
---|
281 | uap_root = new UAPNode("UAP_root"); |
---|
282 | UAPNode* rep = uap_root->addChild("AML_representation"); |
---|
283 | |
---|
284 | // root node in the hierarchy |
---|
285 | UAPNode* lab = rep->addChild("laboratory"); |
---|
286 | lab->addAttribute("name","PSPA lab"); |
---|
287 | |
---|
288 | // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077 |
---|
289 | globParam_.InputRep(lab); |
---|
290 | |
---|
291 | // accelerator or section of accelerator |
---|
292 | //UAPNode* acc = lab->addChild("machine"); |
---|
293 | //acc->addAttribute("name",fileName); |
---|
294 | |
---|
295 | //sectorParam_.InputRep(lab); |
---|
296 | |
---|
297 | // sequence of elements |
---|
298 | UAPNode* sect = lab->addChild("sector"); |
---|
299 | sect->addAttribute("name","a_sect"); |
---|
300 | |
---|
301 | abstractElement* elPtr; |
---|
302 | for(int k = 0; k < getBeamLineSize() ; k++) { |
---|
303 | elPtr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
304 | elPtr->InputRep(sect); |
---|
305 | } |
---|
306 | |
---|
307 | cout << "!Create the AML file ---------------------------" << endl; |
---|
308 | AMLReader reader; |
---|
309 | string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml"; |
---|
310 | reader.AMLRepToAMLFile (uap_root, aml_file); |
---|
311 | } |
---|
312 | |
---|
313 | bool dataManager::restoreElements(string inputFileName) |
---|
314 | { |
---|
315 | cout << "***********************************" << endl; |
---|
316 | cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl; |
---|
317 | |
---|
318 | ifstream infile; |
---|
319 | string name = inputFileName; |
---|
320 | infile.open(name.c_str(), ios::in); |
---|
321 | if (!infile) { |
---|
322 | cerr << " error opening input stream " << name << endl; |
---|
323 | return false; |
---|
324 | } |
---|
325 | else cout << " successful opening input stream : " << name << endl; |
---|
326 | |
---|
327 | //NOTE:: je ne sais pas si les "secteurs" seront conservés, aussi pour ne pas trop changer les fichiers je lis les données aprÚs "globals" dans la boucle |
---|
328 | |
---|
329 | pspa_->getBeamLine()->clear(); |
---|
330 | nomdElements::typedElement elementType; |
---|
331 | string elementLabel; |
---|
332 | while (infile >> elementLabel) { |
---|
333 | if (elementLabel == string("globals") ) { |
---|
334 | globParam_.raz(); |
---|
335 | globParam_.FileInput(infile); |
---|
336 | } else if (elementLabel == string("sectors") ) { |
---|
337 | sectorParam_.raz(); |
---|
338 | sectorParam_.FileInput(infile); |
---|
339 | } else { |
---|
340 | elementType = nomdElements::getTypeFromLabel(elementLabel); |
---|
341 | GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType); |
---|
342 | if ( nouveau == NULL ) { |
---|
343 | cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl; |
---|
344 | return false; |
---|
345 | } else { |
---|
346 | nouveau->FileInput(infile); |
---|
347 | } |
---|
348 | } |
---|
349 | }// while |
---|
350 | |
---|
351 | #if BAVARD > 1 |
---|
352 | unsigned k; |
---|
353 | for(k = 0; k < getBeamLineSize(); k++) { |
---|
354 | abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
355 | cout << "recupere " << ptr->getLabel() << endl; |
---|
356 | } |
---|
357 | #endif |
---|
358 | |
---|
359 | infile.close(); |
---|
360 | return true; |
---|
361 | } |
---|
362 | |
---|
363 | particleBeam* dataManager::getDiagnosticBeam(unsigned index) |
---|
364 | { |
---|
365 | if (index >= indexElementToIndexDiag_.size() ) { |
---|
366 | return NULL; |
---|
367 | } else { |
---|
368 | int indDiag = indexElementToIndexDiag_.at(index); |
---|
369 | return &diagnosticBeam_.at(indDiag); |
---|
370 | } |
---|
371 | } |
---|
372 | |
---|
373 | particleBeam* dataManager::getDiagnosticBeam(string elementLabel) |
---|
374 | { |
---|
375 | return getDiagnosticBeam( getNumeroFromElementLabel(elementLabel) -1); |
---|
376 | } |
---|
377 | |
---|
378 | |
---|
379 | // on ne tient pas compte des elements "snapshot" presents dans la beamLine |
---|
380 | void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy) |
---|
381 | { |
---|
382 | double longueur = 0.0; |
---|
383 | double valeur = 0.0; |
---|
384 | xcor.clear(); |
---|
385 | ycor.clear(); |
---|
386 | titre.clear(); |
---|
387 | titre = " rms enveloppe "; |
---|
388 | legendx.clear(); |
---|
389 | legendx = " z (m)"; |
---|
390 | if ( type == "x" ) { |
---|
391 | unsigned i = 0; |
---|
392 | cout << " dataManager::donneesRmsEnveloppe BeamLineSize = " << getBeamLineSize() << endl; |
---|
393 | // for (unsigned k = 0; k < getBeamLineSize(); k++) { |
---|
394 | for (unsigned k = firstComputedElemNumero_ -1 ; k < lastComputedElemNumero_ ; k++) { |
---|
395 | abstractElement* elPtr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
396 | // if(elPtr->getNomdElement().getElementType() == snapshot) continue; |
---|
397 | // if(elPtr->getNomdElement().getElementType() == fit) continue; |
---|
398 | particleBeam* beamToDraw = getDiagnosticBeam(i); |
---|
399 | if ( !beamToDraw->momentRepresentationOk() ) { |
---|
400 | beamToDraw->buildMomentRepresentation(); |
---|
401 | } |
---|
402 | |
---|
403 | longueur += elPtr->getLenghtOfElement(); |
---|
404 | valeur = beamToDraw->getXmaxRms(); |
---|
405 | cout << " dataManager::donneesRmsEnveloppe index = " << k << " longueur = " << longueur << " enveloppe : " << valeur << endl; |
---|
406 | xcor.push_back(0.01*longueur); // on passe en metres |
---|
407 | ycor.push_back(valeur); |
---|
408 | i++; |
---|
409 | } |
---|
410 | legendy.clear(); |
---|
411 | legendy = " x (cm) "; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | int dataManager::getBeamLineSize() { |
---|
416 | return pspa_->getBeamLine()->getBeamLineSize(); |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi) |
---|
421 | { |
---|
422 | string inputFileName; |
---|
423 | if(logi == nomDeLogiciel::parmela) { |
---|
424 | cout << " dataManager::createSoftwareConnexion : logiciel identifie : parmela " << endl; |
---|
425 | inputFileName = "parmin"; |
---|
426 | return new softwareParmela(inputFileName, &globParam_, this); |
---|
427 | } else if (logi == nomDeLogiciel::transport) { |
---|
428 | cout << " dataManager::createSoftwareConnexion : logiciel identifie : transport " << endl; |
---|
429 | inputFileName = "transport.input"; |
---|
430 | return new softwareTransport(inputFileName, &globParam_, this); |
---|
431 | } else if (logi == nomDeLogiciel::generator) { |
---|
432 | cout << " dataManager::createSoftwareConnexion : logiciel identifie : generator " << endl; |
---|
433 | inputFileName = "generator.in"; |
---|
434 | return new softwareGenerator(inputFileName, &globParam_, this); |
---|
435 | } else if (logi == nomDeLogiciel::test) { |
---|
436 | cout << " dataManager::createSoftwareConnexion : logiciel identifie : test " << endl; |
---|
437 | return new softwareTest(inputFileName, &globParam_, this); |
---|
438 | } else { |
---|
439 | cout << " dataManager::createSoftwareConnexion : logiciel identifie : null " << endl; |
---|
440 | return NULL; |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | sector* dataManager::addNewSector() { |
---|
445 | std::stringstream str; |
---|
446 | str << getSectors().size()+1; |
---|
447 | std::string result; |
---|
448 | str >> result; |
---|
449 | |
---|
450 | sector* sect = new sector(std::string("sector ") + result); |
---|
451 | sectors_.push_back(sect); |
---|
452 | return sect; |
---|
453 | } |
---|
454 | |
---|