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