1 | #include "dataManager.h" |
---|
2 | #include "mathematicalConstants.h" |
---|
3 | #include "PhysicalConstants.h" |
---|
4 | #include "softwareParmela.h" |
---|
5 | #include "softwareTransport.h" |
---|
6 | |
---|
7 | #include <stdio.h> |
---|
8 | #include <fstream> |
---|
9 | //#include <algorithm> |
---|
10 | |
---|
11 | abstractElement* dataManager::addElement(typedElement elemType) |
---|
12 | { |
---|
13 | return elementsGallery_.addElement(elemType); |
---|
14 | } |
---|
15 | |
---|
16 | string dataManager::getLabelFromElementNumero(int numero) |
---|
17 | { |
---|
18 | abstractElement* ptr = elementsGallery_.getElementPointerFromNumero(numero); |
---|
19 | if ( ptr == NULL ) return ""; |
---|
20 | return ptr->getLabel(); |
---|
21 | } |
---|
22 | |
---|
23 | |
---|
24 | void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog) |
---|
25 | { |
---|
26 | jobList_.push_back(new sectionToExecute); |
---|
27 | jobList_.back()->firstElement = debut; |
---|
28 | jobList_.back()->lastElement = fin; |
---|
29 | jobList_.back()->software = prog; |
---|
30 | } |
---|
31 | |
---|
32 | void dataManager::clearSectionToExecute() |
---|
33 | { |
---|
34 | unsigned k; |
---|
35 | for(k = 0; k < jobList_.size(); k++) |
---|
36 | { |
---|
37 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
38 | } |
---|
39 | jobList_.clear(); |
---|
40 | } |
---|
41 | |
---|
42 | trivaluedBool dataManager::checkExecute(string& diagnostic) |
---|
43 | { |
---|
44 | cout << "dataManager::checkExecute()" << endl; |
---|
45 | trivaluedBool resul = ok; |
---|
46 | unsigned k,j; |
---|
47 | diagnostic.clear(); |
---|
48 | unsigned indexDeb, indexFin; |
---|
49 | cout << "controle execution : " << endl; |
---|
50 | for(k = 0; k < jobList_.size(); k++) { |
---|
51 | indexDeb = jobList_[k]->firstElement; |
---|
52 | indexFin = jobList_[k]->lastElement; |
---|
53 | if ( indexFin <= indexDeb ) |
---|
54 | { |
---|
55 | diagnostic += " first and last elements are the same for job " + (jobList_[k]->software).getString() + " \n"; |
---|
56 | resul = error; |
---|
57 | break; |
---|
58 | } |
---|
59 | cout << " indexDeb= " << indexDeb << " indexFin= " << indexFin << endl; |
---|
60 | indexDeb--; |
---|
61 | indexFin--; |
---|
62 | |
---|
63 | abstractElement* elPtr; |
---|
64 | string checkCompatibility; |
---|
65 | resul = checkElementsForExec(jobList_[k]->software, indexDeb, indexFin, checkCompatibility); |
---|
66 | diagnostic += checkCompatibility; |
---|
67 | } |
---|
68 | cout << " data manager renvoie resul = " << resul << endl; |
---|
69 | return resul; |
---|
70 | } |
---|
71 | |
---|
72 | trivaluedBool dataManager::checkElementsForExec(nomDeLogiciel logiciel, unsigned indexDeb, unsigned indexFin, string& diagnostic) |
---|
73 | { |
---|
74 | trivaluedBool resul = ok; |
---|
75 | diagnostic.clear(); |
---|
76 | trivaluedBool accepted; |
---|
77 | abstractElement* elPtr; |
---|
78 | for(unsigned j = indexDeb; j <= indexFin; j++) { |
---|
79 | elPtr = elementsGallery_.getElementPointerFromIndex(j); |
---|
80 | accepted = elPtr->is_accepted_by_software(logiciel); |
---|
81 | if(accepted == error ) { |
---|
82 | diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " is not allowed with " + logiciel.getString() + " \n"; |
---|
83 | resul = error; |
---|
84 | } else if ( accepted == warning ) { |
---|
85 | diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " will be ignored by " + logiciel.getString() + " \n"; |
---|
86 | if ( resul != error ) resul = warning; |
---|
87 | } |
---|
88 | } |
---|
89 | return resul; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | |
---|
95 | void dataManager::initializeExecution(string workingDir) |
---|
96 | { |
---|
97 | removeFile(workingDir + "parmdesz"); |
---|
98 | removeFile(workingDir + "parmin"); |
---|
99 | removeFile(workingDir + "transport.input"); |
---|
100 | removeFile(workingDir + "transport.output"); |
---|
101 | diagnosticBeam_.clear(); |
---|
102 | currentBeam_ = NULL; |
---|
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(string workingDir, string &resul) |
---|
117 | { |
---|
118 | bool success = true; |
---|
119 | resul.clear(); |
---|
120 | string resultatPartiel; |
---|
121 | unsigned k; |
---|
122 | abstractSoftware* softw =NULL; |
---|
123 | |
---|
124 | for(k = 0; k < jobList_.size(); k++) |
---|
125 | { |
---|
126 | int debut = jobList_[k]->firstElement; |
---|
127 | int fin = jobList_[k]->lastElement; |
---|
128 | resultatPartiel.clear(); |
---|
129 | cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl; |
---|
130 | |
---|
131 | if(jobList_[k]->software == nomDeLogiciel::parmela) { |
---|
132 | softw = new softwareParmela(&globParam_, &elementsGallery_); |
---|
133 | } else if (jobList_[k]->software == nomDeLogiciel::transport) { |
---|
134 | softw = new softwareTransport(&globParam_, &elementsGallery_); |
---|
135 | } else { |
---|
136 | success = false; |
---|
137 | resultatPartiel = " unknown software -- STOP \n "; |
---|
138 | break; |
---|
139 | } |
---|
140 | |
---|
141 | success = softw->createInputFile(currentBeam_, debut, fin, workingDir); |
---|
142 | if ( !success ) { |
---|
143 | resultatPartiel += " error creating input file \n"; |
---|
144 | } else { |
---|
145 | success = softw->execute(debut,fin,workingDir,resultatPartiel); |
---|
146 | if ( success ) { |
---|
147 | success = softw->buildBeamAfterElements(debut, fin, diagnosticBeam_, workingDir); |
---|
148 | if ( !success ) { |
---|
149 | resultatPartiel += " readingt results failed \n"; |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | delete softw; |
---|
154 | if ( success ) currentBeam_ = &diagnosticBeam_.back(); |
---|
155 | else currentBeam_ = NULL; |
---|
156 | resul += resultatPartiel; |
---|
157 | if ( !success ) break; |
---|
158 | } |
---|
159 | return success; |
---|
160 | } |
---|
161 | |
---|
162 | void dataManager::saveConfiguration(string workingDir, string nameOfFile) |
---|
163 | { |
---|
164 | ofstream outfile; |
---|
165 | string name = workingDir + nameOfFile + ".save"; |
---|
166 | outfile.open(name.c_str(), ios::out); |
---|
167 | if (!outfile) { |
---|
168 | cerr << " error opening output file for persistency " << name << endl; |
---|
169 | } |
---|
170 | |
---|
171 | outfile << globParam_.FileOutputFlow(); |
---|
172 | unsigned k; |
---|
173 | abstractElement* elPtr; |
---|
174 | for ( k=0 ; k < elementsGallery_.size() ; k++) |
---|
175 | { |
---|
176 | elPtr = elementsGallery_.getElementPointerFromIndex(k); |
---|
177 | outfile << elPtr->FileOutputFlow(); |
---|
178 | } |
---|
179 | outfile.close(); |
---|
180 | } |
---|
181 | |
---|
182 | bool dataManager::restoreElements(string workingDir, string inputFileName) |
---|
183 | { |
---|
184 | cout << "dataManager::restoreElements() fichier " << inputFileName << endl; |
---|
185 | ifstream infile; |
---|
186 | string name = workingDir + inputFileName + ".save"; |
---|
187 | infile.open(name.c_str(), ios::in); |
---|
188 | if (!infile) { |
---|
189 | cerr << " error opening input stream " << name << endl; |
---|
190 | return false; |
---|
191 | } |
---|
192 | else cout << " successful opening input stream " << name << endl; |
---|
193 | |
---|
194 | string globalTitle; |
---|
195 | if ( infile >> globalTitle ) { |
---|
196 | if ( globalTitle == string("globals") ) { |
---|
197 | globParam_.raz(); |
---|
198 | globParam_.FileInput(infile); |
---|
199 | } else { |
---|
200 | cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl; |
---|
201 | return false; |
---|
202 | } |
---|
203 | } else { |
---|
204 | cerr << " dataManager::restoreElements ERROR : reading data save file" << endl; |
---|
205 | return false; |
---|
206 | } |
---|
207 | |
---|
208 | elementsGallery_.raz(); |
---|
209 | // typedElement elem; |
---|
210 | typedElement elementType; |
---|
211 | string elementLabel; |
---|
212 | // while (infile >> ielem) { |
---|
213 | while (infile >> elementLabel) { |
---|
214 | // elem = (typedElement)ielem; |
---|
215 | // abstractElement* nouveau = addElement(elem); |
---|
216 | elementType = nomdElements::getTypeFromLabel(elementLabel); |
---|
217 | abstractElement* nouveau = addElement( elementType); |
---|
218 | if ( nouveau == NULL ) { |
---|
219 | cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl; |
---|
220 | return false; |
---|
221 | } |
---|
222 | nouveau->FileInput(infile); |
---|
223 | } |
---|
224 | infile.close(); |
---|
225 | |
---|
226 | // debug |
---|
227 | unsigned k; |
---|
228 | for(k = 0; k < elementsGallery_.size(); k++) { |
---|
229 | abstractElement* ptr= elementsGallery_.getElementPointerFromIndex(k); |
---|
230 | cout << "reupere " << ptr->getLabel() << endl; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | return true; |
---|
235 | } |
---|
236 | |
---|
237 | particleBeam* dataManager::getDiagnosticBeam(unsigned index) |
---|
238 | { |
---|
239 | cout << " particleBeam* dataManager je vais dessiner " << getLabelFromElementNumero(index+1) << endl; |
---|
240 | |
---|
241 | |
---|
242 | if ( index >= diagnosticBeam_.size() ) { |
---|
243 | return NULL; |
---|
244 | } |
---|
245 | else { |
---|
246 | return &diagnosticBeam_.at(index); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor) |
---|
251 | { |
---|
252 | unsigned k; |
---|
253 | if ( numeroDeb < 1 ) numeroDeb = 1; |
---|
254 | if ( numeroFin > diagnosticBeam_.size() ) numeroFin = diagnosticBeam_.size(); |
---|
255 | |
---|
256 | unsigned indexDeb = numeroDeb -1; |
---|
257 | unsigned indexFin = numeroFin -1; |
---|
258 | |
---|
259 | double longueur = 0.0; |
---|
260 | double valeur = 0.0; |
---|
261 | xcor.clear(); |
---|
262 | ycor.clear(); |
---|
263 | if ( type == "x" ) |
---|
264 | { |
---|
265 | for (k= indexDeb; k <= indexFin; k++) |
---|
266 | { |
---|
267 | if ( !diagnosticBeam_.at(k).momentRepresentationOk() ) diagnosticBeam_.at(k).buildMomentRepresentation(); |
---|
268 | longueur += elementsGallery_.getElementPointerFromIndex(k)->getLenghtOfElement(); |
---|
269 | valeur = diagnosticBeam_.at(k).getXmaxRms(); |
---|
270 | xcor.push_back(longueur); |
---|
271 | ycor.push_back(valeur); |
---|
272 | } |
---|
273 | } |
---|
274 | else |
---|
275 | { |
---|
276 | cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl; |
---|
277 | return; |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|