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 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <fstream> |
---|
12 | //#include <algorithm> |
---|
13 | |
---|
14 | |
---|
15 | dataManager::dataManager(PspaApplication* pspa) : |
---|
16 | currentBeam_(NULL) |
---|
17 | ,workingDir_("") |
---|
18 | ,pspa_ ( pspa) |
---|
19 | { |
---|
20 | } |
---|
21 | |
---|
22 | |
---|
23 | dataManager::~dataManager() { |
---|
24 | unsigned k; |
---|
25 | for (k=0; k < jobList_.size();k++) { |
---|
26 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
27 | } |
---|
28 | if ( currentBeam_ == NULL ) delete currentBeam_; |
---|
29 | } |
---|
30 | |
---|
31 | void dataManager::consoleMessage(string message) { |
---|
32 | GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console")); |
---|
33 | if (console) console->addConsoleMessage(message); |
---|
34 | } |
---|
35 | |
---|
36 | string dataManager::getLabelFromElementNumero(int numero) |
---|
37 | { |
---|
38 | abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1); |
---|
39 | if ( ptr == NULL ) return ""; |
---|
40 | return ptr->getLabel(); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | int dataManager::getNumeroFromElementLabel(string label){ |
---|
45 | int index = -1; |
---|
46 | unsigned int k; |
---|
47 | |
---|
48 | for (k = 0; k < getBeamLineSize() ; k++) |
---|
49 | { |
---|
50 | if (pspa_->getBeamLine()->getAbstractElement(k) != NULL){ |
---|
51 | if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label ) |
---|
52 | { |
---|
53 | index = (int)k + 1; |
---|
54 | return index; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | return index; |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | abstractElement* dataManager::getElementPointerFromNumero(int k){ |
---|
63 | return pspa_->getBeamLine()->getAbstractElement(k-1); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog) |
---|
68 | { |
---|
69 | jobList_.push_back(new sectionToExecute); |
---|
70 | jobList_.back()->firstElement = debut; |
---|
71 | jobList_.back()->lastElement = fin; |
---|
72 | jobList_.back()->software = prog; |
---|
73 | } |
---|
74 | |
---|
75 | void dataManager::clearSectionToExecute() |
---|
76 | { |
---|
77 | unsigned k; |
---|
78 | for(k = 0; k < jobList_.size(); k++) |
---|
79 | { |
---|
80 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
81 | } |
---|
82 | jobList_.clear(); |
---|
83 | } |
---|
84 | |
---|
85 | trivaluedBool dataManager::checkExecute(string& diagnostic) |
---|
86 | { |
---|
87 | cout << "dataManager::checkExecute()" << endl; |
---|
88 | consoleMessage(" VERIFICATION AVANT EXECUTION "); |
---|
89 | // console_ = static_cast<GWt_console*> (wApp->findWidget ("console")); |
---|
90 | // if (console_) console_->addConsoleMessage(" VERIFICATION AVANT EXECUTION "); |
---|
91 | |
---|
92 | trivaluedBool resul = ok; |
---|
93 | unsigned k,j; |
---|
94 | diagnostic.clear(); |
---|
95 | unsigned numeroDeb, numeroFin; |
---|
96 | cout << "controle execution : " << endl; |
---|
97 | for(k = 0; k < jobList_.size(); k++) { |
---|
98 | numeroDeb = jobList_[k]->firstElement; |
---|
99 | numeroFin = jobList_[k]->lastElement; |
---|
100 | if ( numeroFin < numeroDeb ) |
---|
101 | { |
---|
102 | diagnostic += " last element before first for job " + (jobList_[k]->software).getString() + " \n"; |
---|
103 | resul = error; |
---|
104 | break; |
---|
105 | } |
---|
106 | cout << " numeroDeb= " << numeroDeb << " numeroFin= " << numeroFin << endl; |
---|
107 | // indexDeb--; |
---|
108 | // indexFin--; |
---|
109 | |
---|
110 | abstractElement* elPtr; |
---|
111 | string checkCompatibility; |
---|
112 | resul = checkElementsForExec(jobList_[k]->software, numeroDeb, numeroFin, checkCompatibility); |
---|
113 | diagnostic += checkCompatibility; |
---|
114 | } |
---|
115 | cout << " data manager renvoie resul = " << resul << endl; |
---|
116 | return resul; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | trivaluedBool dataManager::checkElementsForExec(nomDeLogiciel logiciel, unsigned numeroDeb, unsigned numeroFin, string& diagnostic) |
---|
121 | { |
---|
122 | trivaluedBool resul = ok; |
---|
123 | diagnostic.clear(); |
---|
124 | trivaluedBool accepted; |
---|
125 | abstractElement* elPtr; |
---|
126 | for(unsigned j = numeroDeb; j <= numeroFin; j++) { |
---|
127 | elPtr = pspa_->getBeamLine()->getAbstractElement(j-1); |
---|
128 | accepted = elPtr->is_accepted_by_software(logiciel); |
---|
129 | if(accepted == error ) { |
---|
130 | diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " is not allowed with " + logiciel.getString() + " \n"; |
---|
131 | resul = error; |
---|
132 | } else if ( accepted == warning ) { |
---|
133 | diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " will be ignored by " + logiciel.getString() + " \n"; |
---|
134 | if ( resul != error ) resul = warning; |
---|
135 | } |
---|
136 | } |
---|
137 | return resul; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | void dataManager::initializeExecution() |
---|
144 | { |
---|
145 | if (workingDir_ == "") { |
---|
146 | return; |
---|
147 | } |
---|
148 | removeFile(workingDir_ + "parmdesz"); |
---|
149 | removeFile(workingDir_ + "parmin"); |
---|
150 | removeFile(workingDir_ + "transport.input"); |
---|
151 | removeFile(workingDir_ + "transport.output"); |
---|
152 | diagnosticBeam_.clear(); |
---|
153 | currentBeam_ = NULL; |
---|
154 | clearSectionToExecute(); |
---|
155 | } |
---|
156 | |
---|
157 | void dataManager::removeFile(string nameOfFile) |
---|
158 | { |
---|
159 | ifstream fileExists; |
---|
160 | fileExists.open(nameOfFile.c_str(), ios::in); |
---|
161 | if (fileExists) { |
---|
162 | fileExists.close(); |
---|
163 | remove(nameOfFile.c_str()); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | bool dataManager::executeAll() |
---|
168 | { |
---|
169 | bool success = true; |
---|
170 | // resul.clear(); |
---|
171 | // string resultatPartiel; |
---|
172 | unsigned k; |
---|
173 | abstractSoftware* softw =NULL; |
---|
174 | |
---|
175 | for(k = 0; k < jobList_.size(); k++) |
---|
176 | { |
---|
177 | int debut = jobList_[k]->firstElement; |
---|
178 | int fin = jobList_[k]->lastElement; |
---|
179 | // resultatPartiel.clear(); |
---|
180 | cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl; |
---|
181 | |
---|
182 | string inputFileName; |
---|
183 | if(jobList_[k]->software == nomDeLogiciel::parmela) { |
---|
184 | softw = new softwareParmela(&globParam_, this); |
---|
185 | inputFileName = "parmin"; |
---|
186 | } else if (jobList_[k]->software == nomDeLogiciel::transport) { |
---|
187 | softw = new softwareTransport(&globParam_, this); |
---|
188 | inputFileName = "transport.input"; |
---|
189 | } else if (jobList_[k]->software == nomDeLogiciel::generator) { |
---|
190 | softw = new softwareGenerator(&globParam_, this); |
---|
191 | inputFileName = "generator.in"; |
---|
192 | } else { |
---|
193 | success = false; |
---|
194 | consoleMessage("dataManager::executeAll : unknown software"); |
---|
195 | // resultatPartiel = " unknown software -- STOP \n "; |
---|
196 | break; |
---|
197 | } |
---|
198 | |
---|
199 | success = softw->createInputFile(inputFileName,currentBeam_, debut, fin, workingDir_); |
---|
200 | // if ( !success ) { |
---|
201 | // resultatPartiel += " error creating input file \n"; |
---|
202 | // } else { |
---|
203 | if ( success ) { |
---|
204 | success = softw->execute(inputFileName, debut,fin,workingDir_); |
---|
205 | if ( success ) { |
---|
206 | success = softw->buildBeamAfterElements(debut, fin, diagnosticBeam_, workingDir_); |
---|
207 | // if ( !success ) { |
---|
208 | // resultatPartiel += " reading results failed \n"; |
---|
209 | // } |
---|
210 | } |
---|
211 | } |
---|
212 | delete softw; |
---|
213 | if ( success ) { |
---|
214 | currentBeam_ = &diagnosticBeam_.back(); |
---|
215 | cout << " execute termine avec succes " << endl; |
---|
216 | } else { |
---|
217 | currentBeam_ = NULL; |
---|
218 | cout << " execute termine en ECHEC " << endl; |
---|
219 | } |
---|
220 | // resul += resultatPartiel; |
---|
221 | if ( !success ) break; |
---|
222 | } |
---|
223 | return success; |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | void dataManager::saveConfiguration( string nameOfFile) |
---|
228 | { |
---|
229 | ofstream outfile; |
---|
230 | string name = workingDir_ + nameOfFile + ".save"; |
---|
231 | outfile.open(name.c_str(), ios::out); |
---|
232 | if (!outfile) { |
---|
233 | cerr << " error opening output file for persistency " << name << endl; |
---|
234 | } |
---|
235 | |
---|
236 | outfile << globParam_.FileOutputFlow(); |
---|
237 | unsigned k; |
---|
238 | abstractElement* elPtr; |
---|
239 | for ( k=0 ; k < getBeamLineSize() ; k++) |
---|
240 | { |
---|
241 | elPtr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
242 | outfile << elPtr->FileOutputFlow(); |
---|
243 | } |
---|
244 | outfile.close(); |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | bool dataManager::restoreElements( string inputFileName) |
---|
249 | { |
---|
250 | cout << "dataManager::restoreElements() fichier " << inputFileName << endl; |
---|
251 | ifstream infile; |
---|
252 | string name = workingDir_ + inputFileName + ".save"; |
---|
253 | infile.open(name.c_str(), ios::in); |
---|
254 | if (!infile) { |
---|
255 | cerr << " error opening input stream " << name << endl; |
---|
256 | return false; |
---|
257 | } |
---|
258 | else cout << " successful opening input stream " << name << endl; |
---|
259 | |
---|
260 | string globalTitle; |
---|
261 | if ( infile >> globalTitle ) { |
---|
262 | if ( globalTitle == string("globals") ) { |
---|
263 | globParam_.raz(); |
---|
264 | globParam_.FileInput(infile); |
---|
265 | } else { |
---|
266 | cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl; |
---|
267 | return false; |
---|
268 | } |
---|
269 | } else { |
---|
270 | cerr << " dataManager::restoreElements ERROR : reading data save file" << endl; |
---|
271 | return false; |
---|
272 | } |
---|
273 | |
---|
274 | pspa_->getBeamLine()->clear(); |
---|
275 | |
---|
276 | typedElement elementType; |
---|
277 | string elementLabel; |
---|
278 | while (infile >> elementLabel) { |
---|
279 | elementType = nomdElements::getTypeFromLabel(elementLabel); |
---|
280 | |
---|
281 | GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType); |
---|
282 | |
---|
283 | if ( nouveau == NULL ) { |
---|
284 | cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl; |
---|
285 | return false; |
---|
286 | } |
---|
287 | nouveau->FileInput(infile); |
---|
288 | } |
---|
289 | infile.close(); |
---|
290 | |
---|
291 | // debug |
---|
292 | unsigned k; |
---|
293 | for(k = 0; k < getBeamLineSize(); k++) { |
---|
294 | abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k); |
---|
295 | |
---|
296 | cout << "reupere " << ptr->getLabel() << endl; |
---|
297 | } |
---|
298 | return true; |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | particleBeam* dataManager::getDiagnosticBeam(unsigned index) |
---|
303 | { |
---|
304 | |
---|
305 | |
---|
306 | if ( index >= diagnosticBeam_.size() ) { |
---|
307 | return NULL; |
---|
308 | } |
---|
309 | else { |
---|
310 | return &diagnosticBeam_.at(index); |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor) |
---|
316 | { |
---|
317 | unsigned k; |
---|
318 | if ( numeroDeb < 1 ) numeroDeb = 1; |
---|
319 | if ( numeroFin > diagnosticBeam_.size() ) numeroFin = diagnosticBeam_.size(); |
---|
320 | |
---|
321 | double longueur = 0.0; |
---|
322 | double valeur = 0.0; |
---|
323 | xcor.clear(); |
---|
324 | ycor.clear(); |
---|
325 | if ( type == "x" ) |
---|
326 | { |
---|
327 | for (k= numeroDeb; k <= numeroFin; k++) |
---|
328 | { |
---|
329 | if ( !diagnosticBeam_.at(k-1).momentRepresentationOk() ) diagnosticBeam_.at(k-1).buildMomentRepresentation(); |
---|
330 | longueur += pspa_->getBeamLine()->getAbstractElement(k-1)->getLenghtOfElement(); |
---|
331 | valeur = diagnosticBeam_.at(k-1).getXmaxRms(); |
---|
332 | xcor.push_back(longueur); |
---|
333 | ycor.push_back(valeur); |
---|
334 | } |
---|
335 | } |
---|
336 | else |
---|
337 | { |
---|
338 | cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl; |
---|
339 | return; |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | int dataManager::getBeamLineSize() { |
---|
344 | return pspa_->getBeamLine()->getBeamLineSize(); |
---|
345 | } |
---|