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 | #include "GWt_ligneFaisceau.h" // FIXME to be removed ! |
---|
11 | |
---|
12 | #include <boost/filesystem.hpp> |
---|
13 | |
---|
14 | #include "UAP/UAPUtilities.hpp" |
---|
15 | #include "AML/AMLReader.hpp" |
---|
16 | |
---|
17 | #define BAVARD 0 |
---|
18 | |
---|
19 | dataManager::dataManager(PspaApplication* pspa) : |
---|
20 | pspa_ (pspa) |
---|
21 | {} |
---|
22 | |
---|
23 | dataManager::~dataManager() |
---|
24 | { |
---|
25 | unsigned k; |
---|
26 | for (k=0; k < jobList_.size();k++) { |
---|
27 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | void dataManager::consoleMessage(string message) |
---|
32 | { |
---|
33 | GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console")); |
---|
34 | if (console) console->addConsoleMessage(message + "\n"); |
---|
35 | pspa_->processEvents(); |
---|
36 | } |
---|
37 | |
---|
38 | string dataManager::getLabelFromElementNumero_deprecated(int numero) |
---|
39 | { |
---|
40 | /* |
---|
41 | abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(numero-1); |
---|
42 | if ( ptr == NULL ) return ""; |
---|
43 | return ptr->getLabel(); |
---|
44 | */ |
---|
45 | return ""; |
---|
46 | } |
---|
47 | |
---|
48 | int dataManager::getNumeroFromElementLabel_deprecated(string label) |
---|
49 | { |
---|
50 | /* int index = -1; |
---|
51 | for (int k = 0; k < getBeamLineSize() ; k++) { |
---|
52 | if (pspa_->getBeamLine_deprecated()->getAbstractElement(k) != NULL) { |
---|
53 | if ( pspa_->getBeamLine_deprecated()->getAbstractElement(k)->getLabel() == label ) |
---|
54 | { |
---|
55 | index = (int)k + 1; |
---|
56 | return index; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | return index; |
---|
61 | */ |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | void dataManager::initializeExecution() |
---|
67 | { |
---|
68 | string workingDir = pspa_->getWorkingDir(); |
---|
69 | if (workingDir == "") { |
---|
70 | return; |
---|
71 | } |
---|
72 | removeFile(workingDir + "parmdesz"); |
---|
73 | removeFile(workingDir + "parmin"); |
---|
74 | removeFile(workingDir + "parin.input0"); |
---|
75 | removeFile(workingDir + "transport.input"); |
---|
76 | removeFile(workingDir + "transport.output"); |
---|
77 | removeFile(workingDir + "generator.in"); |
---|
78 | removeFile(workingDir + "faisceau.ini"); |
---|
79 | removeFile(workingDir + "generator.output"); |
---|
80 | diagnosticBeam_.clear(); |
---|
81 | indexElementToIndexDiag_.clear(); |
---|
82 | for (unsigned int a=0; a< jobList_.size(); a++) { |
---|
83 | jobList_[a]->clearSectionToExecute(); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | void dataManager::removeFile(string nameOfFile) |
---|
88 | { |
---|
89 | ifstream fileExists; |
---|
90 | fileExists.open(nameOfFile.c_str(), ios::in); |
---|
91 | if (fileExists) { |
---|
92 | fileExists.close(); |
---|
93 | remove(nameOfFile.c_str()); |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | bool dataManager::executeAll() |
---|
98 | { |
---|
99 | #if BAVARD > 0 |
---|
100 | cout << "***********************************" << endl; |
---|
101 | cout << " dataManager::executeAll() " << endl << endl; |
---|
102 | #endif |
---|
103 | |
---|
104 | bool success = true; |
---|
105 | string workingDir = pspa_->getWorkingDir(); |
---|
106 | |
---|
107 | |
---|
108 | // Main loop ! |
---|
109 | for(unsigned k = 0; k < jobList_.size(); k++) { |
---|
110 | sector* sector = jobList_[k]; |
---|
111 | |
---|
112 | // for the moment, we put everything in one line without loops |
---|
113 | for (unsigned int l=0; l <sector->getSectionsToExecute().size(); l++) { |
---|
114 | sectionToExecute* section = sector->getSectionsToExecute()[l]; |
---|
115 | abstractSoftware* softw = section->getSoftware(); |
---|
116 | |
---|
117 | #if BAVARD > 0 |
---|
118 | cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl; |
---|
119 | #endif |
---|
120 | |
---|
121 | //cout << "k= " << k << ", de " << jobList_[k]->getFirstElement()->getLabel() << " Ã " << jobList_[k]->getLastElement()->getLabel() << " avec " << softw->getName() << endl; |
---|
122 | |
---|
123 | if (softw == NULL) { |
---|
124 | success = false; |
---|
125 | consoleMessage("dataManager::executeAll : unknown software"); |
---|
126 | break; |
---|
127 | } |
---|
128 | // END OF FIXME |
---|
129 | success = softw->createInputFile(sector->getParticleBeam(),workingDir); |
---|
130 | if ( success ) { |
---|
131 | success = softw->execute(workingDir); |
---|
132 | if ( success ) { |
---|
133 | success = softw->buildBeamAfterElements(workingDir); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | if ( success && (diagnosticBeam_.size() > 0)) { |
---|
138 | sector->setParticleBeam(&diagnosticBeam_.at(indexElementToIndexDiag_.back())); |
---|
139 | cout << " execute termine avec succes " << endl; |
---|
140 | } else { |
---|
141 | sector->setParticleBeam(NULL); |
---|
142 | cout << " execute termine en ECHEC " << endl; |
---|
143 | } |
---|
144 | if ( !success ) break; |
---|
145 | |
---|
146 | #if BAVARD > 0 |
---|
147 | cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl; |
---|
148 | cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl; |
---|
149 | std::vector< abstractElement* > elements = section->getElements(); |
---|
150 | for (unsigned j = 0; j < elements.size(); j++) { |
---|
151 | cout << "[" << j << "] " << elements[j]->getNomdElement().getExpandedName() << endl; |
---|
152 | } |
---|
153 | #endif |
---|
154 | } //l |
---|
155 | } //k |
---|
156 | // if ( currentBeam_deprecated_ ) { |
---|
157 | // string aml_file = workingDir + "/" + "faisceau_final" + ".aml"; |
---|
158 | // currentBeam_deprecated_->writeToAMLFile(aml_file); |
---|
159 | // // TESTS |
---|
160 | // currentBeam_deprecated_->readFromAMLFile(aml_file); |
---|
161 | // } |
---|
162 | cout << " dataManager::executeAll() " << endl; |
---|
163 | |
---|
164 | return success; |
---|
165 | } |
---|
166 | |
---|
167 | void dataManager::saveConfiguration(string folder, string nameOfFile) |
---|
168 | { |
---|
169 | ofstream outfile; |
---|
170 | //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save"; |
---|
171 | cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl; |
---|
172 | cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl; |
---|
173 | cout << " et qu'on puisse restaurer normalement" << endl; |
---|
174 | |
---|
175 | string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; |
---|
176 | // make dir if not exist |
---|
177 | boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/"); |
---|
178 | |
---|
179 | outfile.open(name.c_str(), ios::out); |
---|
180 | if (!outfile) { |
---|
181 | cerr<<" ERROR opening output file for persistency "<<name<<endl; |
---|
182 | } |
---|
183 | |
---|
184 | outfile << globParam_.FileOutputFlow(); |
---|
185 | |
---|
186 | // FIXME Francois: Comment sauver la configuration des section ? |
---|
187 | for (unsigned int a=0; a< jobList_.size(); a++) { |
---|
188 | outfile << jobList_[a]->getSectorParameters().FileOutputFlow(); |
---|
189 | } |
---|
190 | // END |
---|
191 | |
---|
192 | abstractElement* elPtr; |
---|
193 | for (unsigned k = 0; k < jobList_.size(); k++) { |
---|
194 | sector* sector = jobList_[k]; |
---|
195 | for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { |
---|
196 | sectionToExecute* section = sector->getSectionsToExecute()[l]; |
---|
197 | for (unsigned m = 0; m < section->getElements().size(); m++) { |
---|
198 | elPtr = section->getElements()[m]; |
---|
199 | outfile << elPtr->FileOutputFlow(); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | outfile.close(); |
---|
204 | } |
---|
205 | |
---|
206 | // ecriture sur fichier AML |
---|
207 | void dataManager::writeToAMLFile(string fileName) |
---|
208 | { |
---|
209 | UAPNode* uap_root = NULL; |
---|
210 | uap_root = new UAPNode("UAP_root"); |
---|
211 | UAPNode* rep = uap_root->addChild("AML_representation"); |
---|
212 | |
---|
213 | // root node in the hierarchy |
---|
214 | UAPNode* lab = rep->addChild("laboratory"); |
---|
215 | lab->addAttribute("name","PSPA lab"); |
---|
216 | |
---|
217 | // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077 |
---|
218 | globParam_.InputRep(lab); |
---|
219 | |
---|
220 | // accelerator or section of accelerator |
---|
221 | //UAPNode* acc = lab->addChild("machine"); |
---|
222 | //acc->addAttribute("name",fileName); |
---|
223 | |
---|
224 | //sectorParam_.InputRep(lab); |
---|
225 | |
---|
226 | // sequence of elements |
---|
227 | UAPNode* sect = lab->addChild("sector"); |
---|
228 | sect->addAttribute("name","a_sect"); |
---|
229 | |
---|
230 | abstractElement* elPtr; |
---|
231 | for (unsigned k = 0; k < jobList_.size(); k++) { |
---|
232 | sector* sector = jobList_[k]; |
---|
233 | for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { |
---|
234 | sectionToExecute* section = sector->getSectionsToExecute()[l]; |
---|
235 | for (unsigned m = 0; m < section->getElements().size(); m++) { |
---|
236 | elPtr = section->getElements()[m]; |
---|
237 | elPtr->InputRep(sect); |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | cout << "!Create the AML file ---------------------------" << endl; |
---|
242 | AMLReader reader; |
---|
243 | string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml"; |
---|
244 | reader.AMLRepToAMLFile (uap_root, aml_file); |
---|
245 | } |
---|
246 | |
---|
247 | bool dataManager::restoreElements(string inputFileName) |
---|
248 | { |
---|
249 | cout << "***********************************" << endl; |
---|
250 | cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl; |
---|
251 | |
---|
252 | ifstream infile; |
---|
253 | string name = inputFileName; |
---|
254 | infile.open(name.c_str(), ios::in); |
---|
255 | if (!infile) { |
---|
256 | cerr << " error opening input stream " << name << endl; |
---|
257 | return false; |
---|
258 | } |
---|
259 | else cout << " successful opening input stream : " << name << endl; |
---|
260 | |
---|
261 | //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 |
---|
262 | |
---|
263 | // Clear the BeamLine |
---|
264 | jobList_.clear(); |
---|
265 | |
---|
266 | // FIXME : For test purpose !!! |
---|
267 | sector* mySect = addNewSector(); |
---|
268 | |
---|
269 | nomdElements::typedElement elementType; |
---|
270 | string elementLabel; |
---|
271 | abstractElement* nouveau = NULL; |
---|
272 | while (infile >> elementLabel) { |
---|
273 | if (elementLabel == string("globals") ) { |
---|
274 | globParam_.raz(); |
---|
275 | globParam_.FileInput(infile); |
---|
276 | } else if (elementLabel == string("sectors") ) { |
---|
277 | // FIXME Francois !!!!!!! |
---|
278 | // sectorParam_.raz(); |
---|
279 | // sectorParam_.FileInput(infile); |
---|
280 | // END |
---|
281 | } else { |
---|
282 | elementType = nomdElements::getTypeFromLabel(elementLabel); |
---|
283 | nouveau = mySect->addElementAfter(elementType,nouveau); |
---|
284 | if ( nouveau == NULL ) { |
---|
285 | cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl; |
---|
286 | return false; |
---|
287 | } else { |
---|
288 | nouveau->FileInput(infile); |
---|
289 | } |
---|
290 | } |
---|
291 | }// while |
---|
292 | |
---|
293 | #if BAVARD > 1 |
---|
294 | /* unsigned k; |
---|
295 | for(k = 0; k < getBeamLineSize(); k++) { |
---|
296 | abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k); |
---|
297 | cout << "recupere " << ptr->getLabel() << endl; |
---|
298 | } |
---|
299 | */ |
---|
300 | #endif |
---|
301 | |
---|
302 | infile.close(); |
---|
303 | return true; |
---|
304 | } |
---|
305 | |
---|
306 | particleBeam* dataManager::getDiagnosticBeam(unsigned index) |
---|
307 | { |
---|
308 | if (index >= indexElementToIndexDiag_.size() ) { |
---|
309 | return NULL; |
---|
310 | } else { |
---|
311 | int indDiag = indexElementToIndexDiag_.at(index); |
---|
312 | return &diagnosticBeam_.at(indDiag); |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel) |
---|
317 | { |
---|
318 | // FIXME : Devra etre changé par une récupération par "abstractElement" et non pas par label |
---|
319 | unsigned int number = 0; |
---|
320 | abstractElement* elPtr; |
---|
321 | for (unsigned k = 0; k < jobList_.size(); k++) { |
---|
322 | sector* sector = jobList_[k]; |
---|
323 | for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { |
---|
324 | sectionToExecute* section = sector->getSectionsToExecute()[l]; |
---|
325 | for (unsigned m = 0; m < section->getElements().size(); m++) { |
---|
326 | elPtr = section->getElements()[m]; |
---|
327 | if (elPtr->getLabel() == elementLabel) { |
---|
328 | return getDiagnosticBeam(number); |
---|
329 | } |
---|
330 | number ++; |
---|
331 | } |
---|
332 | } |
---|
333 | } |
---|
334 | return NULL; |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | // on ne tient pas compte des elements "snapshot" presents dans la beamLine |
---|
339 | void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy) |
---|
340 | { |
---|
341 | double longueur = 0.0; |
---|
342 | double valeur = 0.0; |
---|
343 | xcor.clear(); |
---|
344 | ycor.clear(); |
---|
345 | titre.clear(); |
---|
346 | titre = " rms enveloppe "; |
---|
347 | legendx.clear(); |
---|
348 | legendx = " z (m)"; |
---|
349 | if ( type == "x" ) { |
---|
350 | unsigned i = 0; |
---|
351 | cout << " dataManager::donneesRmsEnveloppe " << endl; |
---|
352 | // for (unsigned k = 0; k < getBeamLineSize(); k++) { |
---|
353 | for (unsigned k = 0; k < jobList_.size(); k++) { |
---|
354 | sector* sector = jobList_[k]; |
---|
355 | for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { |
---|
356 | sectionToExecute* section = sector->getSectionsToExecute()[l]; |
---|
357 | for (unsigned m = 0; m < section->getElements().size(); m++) { |
---|
358 | abstractElement* elPtr = section->getElements()[m]; |
---|
359 | // if(elPtr->getNomdElement().getElementType() == snapshot) continue; |
---|
360 | // if(elPtr->getNomdElement().getElementType() == fit) continue; |
---|
361 | particleBeam* beamToDraw = getDiagnosticBeam(i); |
---|
362 | if ( !beamToDraw->momentRepresentationOk() ) { |
---|
363 | beamToDraw->buildMomentRepresentation(); |
---|
364 | } |
---|
365 | |
---|
366 | longueur += elPtr->getLenghtOfElement(); |
---|
367 | valeur = beamToDraw->getXmaxRms(); |
---|
368 | cout << " dataManager::donneesRmsEnveloppe index = " << k << " longueur = " << longueur << " enveloppe : " << valeur << endl; |
---|
369 | xcor.push_back(0.01*longueur); // on passe en metres |
---|
370 | ycor.push_back(valeur); |
---|
371 | i++; |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | legendy.clear(); |
---|
376 | legendy = " x (cm) "; |
---|
377 | } |
---|
378 | } |
---|
379 | |
---|
380 | int dataManager::getBeamLineSize_deprecated() { |
---|
381 | // FIXME : Cette methode ne doit pas exister, mis a part pour savoir quelle est la taille de la beamLine |
---|
382 | unsigned int i = 0; |
---|
383 | for (unsigned k = 0; k < jobList_.size(); k++) { |
---|
384 | sector* sector = jobList_[k]; |
---|
385 | for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) { |
---|
386 | i += sector->getSectionsToExecute()[l]->getElements().size(); |
---|
387 | } |
---|
388 | } |
---|
389 | return i; |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | sector* dataManager::addNewSector() { |
---|
394 | std::stringstream str; |
---|
395 | str << getSectors().size()+1; |
---|
396 | std::string result; |
---|
397 | str >> result; |
---|
398 | |
---|
399 | sector* sect = new sector(this, std::string("sector ") + result); |
---|
400 | jobList_.push_back(sect); |
---|
401 | return sect; |
---|
402 | } |
---|
403 | |
---|