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 | dataManager::dataManager(PspaApplication* pspa) : |
---|
16 | currentBeam_(NULL), |
---|
17 | pspa_ (pspa) |
---|
18 | {} |
---|
19 | |
---|
20 | dataManager::~dataManager() |
---|
21 | { |
---|
22 | unsigned k; |
---|
23 | for (k=0; k < jobList_.size();k++) { |
---|
24 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
25 | } |
---|
26 | if ( currentBeam_ == NULL ) delete currentBeam_; |
---|
27 | } |
---|
28 | |
---|
29 | void dataManager::consoleMessage(string message) { |
---|
30 | GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console")); |
---|
31 | if (console) console->addConsoleMessage(message); |
---|
32 | pspa_->processEvents(); |
---|
33 | } |
---|
34 | |
---|
35 | string dataManager::getLabelFromElementNumero(int numero) |
---|
36 | { |
---|
37 | abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1); |
---|
38 | if ( ptr == NULL ) return ""; |
---|
39 | return ptr->getLabel(); |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | int dataManager::getNumeroFromElementLabel(string label) |
---|
44 | { |
---|
45 | int index = -1; |
---|
46 | |
---|
47 | for (int k = 0; k < getBeamLineSize() ; k++) |
---|
48 | { |
---|
49 | if (pspa_->getBeamLine()->getAbstractElement(k) != NULL){ |
---|
50 | if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label ) |
---|
51 | { |
---|
52 | index = (int)k + 1; |
---|
53 | return index; |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | return index; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | abstractElement* dataManager::getElementPointerFromNumero(int k) |
---|
62 | { |
---|
63 | return pspa_->getBeamLine()->getAbstractElement(k-1); |
---|
64 | } |
---|
65 | |
---|
66 | void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog) |
---|
67 | { |
---|
68 | jobList_.push_back(new sectionToExecute); |
---|
69 | jobList_.back()->firstElement = debut; |
---|
70 | jobList_.back()->lastElement = fin; |
---|
71 | jobList_.back()->software = prog; |
---|
72 | } |
---|
73 | |
---|
74 | void dataManager::clearSectionToExecute() |
---|
75 | { |
---|
76 | unsigned k; |
---|
77 | for(k = 0; k < jobList_.size(); k++) |
---|
78 | { |
---|
79 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
80 | } |
---|
81 | jobList_.clear(); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | void dataManager::initializeExecution() |
---|
86 | { |
---|
87 | string workingDir = pspa_->getWorkingDir(); |
---|
88 | if (workingDir == "") { |
---|
89 | return; |
---|
90 | } |
---|
91 | removeFile(workingDir + "parmdesz"); |
---|
92 | removeFile(workingDir + "parmin"); |
---|
93 | removeFile(workingDir + "parin.input0"); |
---|
94 | removeFile(workingDir + "transport.input"); |
---|
95 | removeFile(workingDir + "transport.output"); |
---|
96 | removeFile(workingDir + "generator.in"); |
---|
97 | removeFile(workingDir + "faisceau.ini"); |
---|
98 | removeFile(workingDir + "generator.output"); |
---|
99 | diagnosticBeam_.clear(); |
---|
100 | currentBeam_ = NULL; |
---|
101 | firstComputedElemNumero_ = getBeamLineSize(); |
---|
102 | lastComputedElemNumero_ = 1; |
---|
103 | clearSectionToExecute(); |
---|
104 | } |
---|
105 | |
---|
106 | void dataManager::removeFile(string nameOfFile) |
---|
107 | { |
---|
108 | ifstream fileExists; |
---|
109 | fileExists.open(nameOfFile.c_str(), ios::in); |
---|
110 | if (fileExists) { |
---|
111 | fileExists.close(); |
---|
112 | remove(nameOfFile.c_str()); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | bool dataManager::executeAll() |
---|
117 | { |
---|
118 | bool success = true; |
---|
119 | abstractSoftware* softw = NULL; |
---|
120 | string workingDir = pspa_->getWorkingDir(); |
---|
121 | |
---|
122 | for(unsigned k = 0; k < jobList_.size(); k++) |
---|
123 | { |
---|
124 | cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl; |
---|
125 | |
---|
126 | int debut = jobList_[k]->firstElement; |
---|
127 | int fin = jobList_[k]->lastElement; |
---|
128 | softw = createSoftwareConnexion(jobList_[k]->software); |
---|
129 | |
---|
130 | if (softw == NULL) { |
---|
131 | success = false; |
---|
132 | consoleMessage("dataManager::executeAll : unknown software"); |
---|
133 | break; |
---|
134 | } |
---|
135 | |
---|
136 | success = softw->createInputFile(currentBeam_,debut,fin,workingDir); |
---|
137 | if ( success ) { |
---|
138 | success = softw->execute(debut,fin,workingDir); |
---|
139 | if ( success ) { |
---|
140 | success = softw->buildBeamAfterElements(debut,fin,diagnosticBeam_,workingDir); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | delete softw; |
---|
145 | if ( success ) { |
---|
146 | currentBeam_ = &diagnosticBeam_.back(); |
---|
147 | cout << " execute termine avec succes " << endl; |
---|
148 | } else { |
---|
149 | currentBeam_ = NULL; |
---|
150 | cout << " execute termine en ECHEC " << endl; |
---|
151 | } |
---|
152 | if ( !success ) break; |
---|
153 | |
---|
154 | |
---|
155 | if ( debut < firstComputedElemNumero_ ) firstComputedElemNumero_ = debut; |
---|
156 | if ( fin > lastComputedElemNumero_ ) lastComputedElemNumero_ = fin; |
---|
157 | |
---|
158 | //debug |
---|
159 | cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl; |
---|
160 | cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl; |
---|
161 | for (int j = debut; j <= fin; j++) { |
---|
162 | abstractElement* elPtr= getElementPointerFromNumero(j); |
---|
163 | cout << "[" << j << "] " << elPtr->getNomdElement().getElementName() << endl; |
---|
164 | } |
---|
165 | } //k |
---|
166 | |
---|
167 | cout << " dataManager::executeAll premier element : " << firstComputedElemNumero_ << " dernier : " << lastComputedElemNumero_ << endl; |
---|
168 | |
---|
169 | return success; |
---|
170 | } |
---|
171 | |
---|
172 | void dataManager::saveConfiguration(string folder, string nameOfFile) |
---|
173 | { |
---|
174 | ofstream outfile; |
---|
175 | // string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save"; |
---|
176 | cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl; |
---|
177 | cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl; |
---|
178 | cout << " et qu'on puisse restaurer normalement" << endl; |
---|
179 | string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; |
---|
180 | |
---|
181 | // make dir if not exist |
---|
182 | boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/"); |
---|
183 | |
---|
184 | outfile.open(name.c_str(), ios::out); |
---|
185 | if (!outfile) { |
---|
186 | cerr << " error opening output file for persistency " << name << endl; |
---|
187 | } |
---|
188 | |
---|
189 | outfile << globParam_.FileOutputFlow(); |
---|
190 | abstractElement* elPtr; |
---|
191 | for(unsigned k = 0; k < getBeamLineSize() ; k++) |
---|
192 | { |
---|
193 | elPtr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
194 | outfile << elPtr->FileOutputFlow(); |
---|
195 | } |
---|
196 | outfile.close(); |
---|
197 | } |
---|
198 | |
---|
199 | bool dataManager::restoreElements( string inputFileName) |
---|
200 | { |
---|
201 | cout << "dataManager::restoreElements() fichier " << inputFileName << endl; |
---|
202 | ifstream infile; |
---|
203 | string name = inputFileName; |
---|
204 | infile.open(name.c_str(), ios::in); |
---|
205 | if (!infile) { |
---|
206 | cerr << " error opening input stream " << name << endl; |
---|
207 | return false; |
---|
208 | } |
---|
209 | else cout << " successful opening input stream " << name << endl; |
---|
210 | |
---|
211 | string globalTitle; |
---|
212 | if ( infile >> globalTitle ) { |
---|
213 | if ( globalTitle == string("globals") ) { |
---|
214 | globParam_.raz(); |
---|
215 | globParam_.FileInput(infile); |
---|
216 | } else { |
---|
217 | cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl; |
---|
218 | return false; |
---|
219 | } |
---|
220 | } else { |
---|
221 | cerr << " dataManager::restoreElements ERROR : reading data save file" << endl; |
---|
222 | return false; |
---|
223 | } |
---|
224 | |
---|
225 | pspa_->getBeamLine()->clear(); |
---|
226 | |
---|
227 | typedElement elementType; |
---|
228 | string elementLabel; |
---|
229 | while (infile >> elementLabel) { |
---|
230 | elementType = nomdElements::getTypeFromLabel(elementLabel); |
---|
231 | |
---|
232 | GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType); |
---|
233 | |
---|
234 | if ( nouveau == NULL ) { |
---|
235 | cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl; |
---|
236 | return false; |
---|
237 | } |
---|
238 | nouveau->FileInput(infile); |
---|
239 | } |
---|
240 | infile.close(); |
---|
241 | |
---|
242 | // debug |
---|
243 | // unsigned k; |
---|
244 | // for(k = 0; k < getBeamLineSize(); k++) { |
---|
245 | // abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
246 | // cout << "reupere " << ptr->getLabel() << endl; |
---|
247 | // } |
---|
248 | return true; |
---|
249 | } |
---|
250 | |
---|
251 | particleBeam* dataManager::getDiagnosticBeam(unsigned index) |
---|
252 | { |
---|
253 | if (index >= diagnosticBeam_.size()) { |
---|
254 | return NULL; |
---|
255 | } else { |
---|
256 | return &diagnosticBeam_.at(index); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | |
---|
262 | // on ne tient pas compte des elements "snapshot" presents dans la beamLine |
---|
263 | void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor) |
---|
264 | { |
---|
265 | double longueur = 0.0; |
---|
266 | double valeur = 0.0; |
---|
267 | xcor.clear(); |
---|
268 | ycor.clear(); |
---|
269 | |
---|
270 | if ( type == "x" ) { |
---|
271 | unsigned i = 0; |
---|
272 | cout << " dataManager::donneesRmsEnveloppe BeamLineSize = " << getBeamLineSize() << endl; |
---|
273 | // for (unsigned k = 0; k < getBeamLineSize(); k++) { |
---|
274 | for (unsigned k = firstComputedElemNumero_ -1 ; k < lastComputedElemNumero_ ; k++) { |
---|
275 | abstractElement* elPtr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
276 | if(elPtr->getNomdElement().getElementType() == snapshot) continue; |
---|
277 | if(elPtr->getNomdElement().getElementType() == fit) continue; |
---|
278 | |
---|
279 | if ( !diagnosticBeam_.at(i).momentRepresentationOk() ) { |
---|
280 | diagnosticBeam_.at(i).buildMomentRepresentation(); |
---|
281 | } |
---|
282 | |
---|
283 | longueur += elPtr->getLenghtOfElement(); |
---|
284 | valeur = diagnosticBeam_.at(i).getXmaxRms(); |
---|
285 | cout << " dataManager::donneesRmsEnveloppe index = " << k << " longueur = " << longueur << " enveloppe : " << valeur << endl; |
---|
286 | xcor.push_back(longueur); |
---|
287 | ycor.push_back(valeur); |
---|
288 | i++; |
---|
289 | } |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | int dataManager::getBeamLineSize() { |
---|
294 | return pspa_->getBeamLine()->getBeamLineSize(); |
---|
295 | } |
---|
296 | |
---|
297 | abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi) |
---|
298 | { |
---|
299 | string inputFileName; |
---|
300 | if(logi == nomDeLogiciel::parmela) { |
---|
301 | inputFileName = "parmin"; |
---|
302 | return new softwareParmela(inputFileName, &globParam_, this); |
---|
303 | } else if (logi == nomDeLogiciel::transport) { |
---|
304 | inputFileName = "transport.input"; |
---|
305 | return new softwareTransport(inputFileName, &globParam_, this); |
---|
306 | } else if (logi == nomDeLogiciel::generator) { |
---|
307 | inputFileName = "generator.in"; |
---|
308 | return new softwareGenerator(inputFileName, &globParam_, this); |
---|
309 | } else if (logi == nomDeLogiciel::test) { |
---|
310 | return new softwareTest(inputFileName, &globParam_, this); |
---|
311 | } else { |
---|
312 | return NULL; |
---|
313 | } |
---|
314 | } |
---|