1 | #include "dataManager.h" |
---|
2 | #include "environmentVariables.h" |
---|
3 | |
---|
4 | #include <stdio.h> |
---|
5 | #include <fstream> |
---|
6 | |
---|
7 | //using namespace std; |
---|
8 | |
---|
9 | abstractElement* dataManager::addElement(nomdElement elemType) |
---|
10 | { |
---|
11 | return elementsGallery_.addElement(elemType); |
---|
12 | } |
---|
13 | |
---|
14 | void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog) |
---|
15 | { |
---|
16 | jobList_.push_back(new sectionToExecute); |
---|
17 | jobList_.back()->firstElement = debut; |
---|
18 | jobList_.back()->lastElement = fin; |
---|
19 | jobList_.back()->software = prog; |
---|
20 | } |
---|
21 | |
---|
22 | void dataManager::clearSectionToExecute() |
---|
23 | { |
---|
24 | unsigned k; |
---|
25 | for (k=0; k < jobList_.size();k++) |
---|
26 | { |
---|
27 | if ( jobList_[k] != NULL ) delete jobList_[k]; |
---|
28 | } |
---|
29 | jobList_.clear(); |
---|
30 | } |
---|
31 | |
---|
32 | string dataManager::checkExecute() |
---|
33 | { |
---|
34 | int k,j; |
---|
35 | string diagnostic; |
---|
36 | int indexDeb, indexFin; |
---|
37 | cout << " controle execution : " << endl; |
---|
38 | for ( k=0; k< jobList_.size(); k++) |
---|
39 | { |
---|
40 | indexDeb = jobList_[k]->firstElement; |
---|
41 | indexFin = jobList_[k]->lastElement; |
---|
42 | indexDeb--; |
---|
43 | indexFin--; |
---|
44 | abstractElement* elPtr; |
---|
45 | if ( jobList_[k]->software == nomDeLogiciel::parmela ) |
---|
46 | { |
---|
47 | for ( j=indexDeb; j <= indexFin; j++) |
---|
48 | { |
---|
49 | elPtr = elementsGallery_.getElementPointer(j); |
---|
50 | if ( !elPtr->is_parmela_element() ) |
---|
51 | { |
---|
52 | diagnostic += " the element " + elPtr->getNameString() + " is not allowed for PARMELA \n"; |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | else if ( jobList_[k]->software == nomDeLogiciel::transport ) |
---|
57 | { |
---|
58 | for ( j=indexDeb; j <= indexFin; j++) |
---|
59 | { |
---|
60 | elPtr = elementsGallery_.getElementPointer(j); |
---|
61 | if ( !elPtr->is_transport_element() ) |
---|
62 | { |
---|
63 | diagnostic += " the element " + elPtr->getNameString() + " is not allowed for TRANSPORT \n"; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | else |
---|
68 | { |
---|
69 | diagnostic += (jobList_[k]->software).getString() + " : unknown software \n "; |
---|
70 | } |
---|
71 | } |
---|
72 | return diagnostic; |
---|
73 | } |
---|
74 | |
---|
75 | bool dataManager::executeTransport( unsigned int indexDeb, unsigned int indexFin, string& resul) |
---|
76 | { |
---|
77 | ostringstream sortie; |
---|
78 | bool success = true; |
---|
79 | sortie << " EXECUTION DE TRANSPORT DE " << indexDeb << " a " << indexFin << endl; |
---|
80 | createInputFileTransport( indexDeb, indexFin); |
---|
81 | |
---|
82 | string bufString; |
---|
83 | char buf[132]; |
---|
84 | string transportJob = WORKINGAREA + "transport"; |
---|
85 | transportJob += string(" < "); |
---|
86 | transportJob += WORKINGAREA + "transport.input"; |
---|
87 | // transportJob += string(" > "); |
---|
88 | // transportJob += WORKINGAREA + "transport.output"; |
---|
89 | cout << " job transport= " << transportJob << endl; |
---|
90 | |
---|
91 | ofstream outfile; |
---|
92 | string nameOut = WORKINGAREA + "transport.output"; |
---|
93 | |
---|
94 | |
---|
95 | FILE* pp = popen(transportJob.c_str(), "r"); |
---|
96 | if (pp != NULL) |
---|
97 | { |
---|
98 | cout << " execution transport MARCHE " << endl; |
---|
99 | |
---|
100 | //////////////////////// |
---|
101 | outfile.open(nameOut.c_str(), ios::out); |
---|
102 | if (!outfile) |
---|
103 | { |
---|
104 | sortie << " error first opening transport output stream " << nameOut << endl; |
---|
105 | success = false; |
---|
106 | return success; |
---|
107 | } |
---|
108 | |
---|
109 | unsigned nbl = 0; |
---|
110 | while (fgets(buf, sizeof buf, pp)) |
---|
111 | { |
---|
112 | // cout << " buf= " << buf << endl; |
---|
113 | sortie << buf; |
---|
114 | outfile << buf; |
---|
115 | nbl++; |
---|
116 | } |
---|
117 | outfile.close(); |
---|
118 | ///////////////////////////////// |
---|
119 | |
---|
120 | // cout << " datamanager ecriture output terminee nbl = " << nbl << endl; |
---|
121 | // on re-ouvre le fichier de sortie |
---|
122 | |
---|
123 | ifstream infile; |
---|
124 | |
---|
125 | infile.open(nameOut.c_str(), ios::in); |
---|
126 | if (!infile) |
---|
127 | { |
---|
128 | sortie << " error re-opening transport output stream " << nameOut << endl; |
---|
129 | success = false; |
---|
130 | return success; |
---|
131 | } |
---|
132 | |
---|
133 | currentBeam_.setFromTransport(infile, nbl); |
---|
134 | |
---|
135 | infile.close(); |
---|
136 | pclose(pp); |
---|
137 | resul += sortie.str(); |
---|
138 | } |
---|
139 | else |
---|
140 | { |
---|
141 | sortie << " launching of transport failed \n"; |
---|
142 | success = false; |
---|
143 | } |
---|
144 | return success; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | bool dataManager::executeParmela( unsigned int indexDeb, unsigned int indexFin, string& resul) |
---|
149 | { |
---|
150 | bool success = true; |
---|
151 | createInputFileParmela(indexDeb, indexFin); |
---|
152 | |
---|
153 | ostringstream sortie; |
---|
154 | sortie << " EXECUTION DE PARMELA DE l'ELEMENT " << indexDeb << " A L'ELEMENT " << indexFin << endl; |
---|
155 | |
---|
156 | char buf[132]; |
---|
157 | string parmelaJob = WORKINGAREA + "parmela"; |
---|
158 | parmelaJob += string(" "); |
---|
159 | parmelaJob += WORKINGAREA; |
---|
160 | cout << " job parmela= " << parmelaJob << endl; |
---|
161 | FILE* pp = popen(parmelaJob.c_str(), "r"); |
---|
162 | if (pp != NULL) |
---|
163 | { |
---|
164 | cout << " execution parmela MARCHE " << endl; |
---|
165 | while (fgets(buf, sizeof buf, pp)) |
---|
166 | { |
---|
167 | sortie << buf; |
---|
168 | } |
---|
169 | |
---|
170 | // cout << " sortie parmela " << sortie.str() << endl; |
---|
171 | |
---|
172 | currentBeam_.setFromParmela(globParam_.getFrequency()); |
---|
173 | pclose(pp); |
---|
174 | resul += sortie.str(); |
---|
175 | } |
---|
176 | else |
---|
177 | { |
---|
178 | cerr << " launching of parmela failed " << endl; |
---|
179 | success = false; |
---|
180 | } |
---|
181 | return success; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | void dataManager::createInputFileTransport( unsigned int indexDeb, unsigned int indexFin) |
---|
186 | { |
---|
187 | unsigned int k; |
---|
188 | if ( !currentBeam_.momentRepresentationOk() ) |
---|
189 | { |
---|
190 | |
---|
191 | cerr << " dataManager::createInputFileTransport : the beam is not OK " << endl; |
---|
192 | } |
---|
193 | indexDeb--; |
---|
194 | indexFin--; |
---|
195 | |
---|
196 | const vector< vector<double> >& moments = currentBeam_.getTransportMoments(); |
---|
197 | double P0 = currentBeam_.getP0Transport(); |
---|
198 | ofstream outfile; |
---|
199 | string name = WORKINGAREA + "transport.input"; |
---|
200 | outfile.open(name.c_str(), ios::out); |
---|
201 | if (!outfile) |
---|
202 | { |
---|
203 | cerr << " error opening output stream " << name << endl; |
---|
204 | } |
---|
205 | |
---|
206 | outfile << string("'titre provisoire'") << endl; |
---|
207 | outfile << " 0 " << endl; |
---|
208 | outfile << " UTRANS " << endl; |
---|
209 | |
---|
210 | outfile << " BEAM, X=" << (moments.at(0)).at(0) << ", XP="<< (moments.at(1)).at(1) << ", Y=" << (moments.at(2)).at(2)<< ", YP=" << (moments.at(3)).at(3) << ", & " << endl; |
---|
211 | |
---|
212 | outfile << " DL=" << (moments.at(4)).at(4) << ", DEL="<< (moments.at(5)).at(5) << ", P0=" << P0 << " ; " << endl; |
---|
213 | |
---|
214 | outfile << " CORR, C21=" << (moments.at(1)).at(0) << ", C31="<< (moments.at(2)).at(0) << ", C32=" << (moments.at(2)).at(1) << ", & " << endl; |
---|
215 | |
---|
216 | outfile << " C41=" << (moments.at(3)).at(0) << ", C42="<< (moments.at(3)).at(1) << ", C43=" << (moments.at(3)).at(2) << ", C51=" << (moments.at(4)).at(0) << ", & " << endl; |
---|
217 | |
---|
218 | outfile << " C52=" << (moments.at(4)).at(1) << ", C53="<< (moments.at(4)).at(2) << ", C54=" << (moments.at(4)).at(3) << ", C61=" << (moments.at(5)).at(0) << ", & " << endl; |
---|
219 | |
---|
220 | outfile << " C62=" << (moments.at(5)).at(1) << ", C63="<< (moments.at(5)).at(2) << ", C64=" << (moments.at(5)).at(3) << ", C65=" << (moments.at(5)).at(4) << " ; " << endl; |
---|
221 | outfile << " PRINT, BEAM, ON; " << endl; |
---|
222 | |
---|
223 | |
---|
224 | abstractElement* elPtr; |
---|
225 | for ( k=indexDeb; k <= indexFin; k++) |
---|
226 | { |
---|
227 | elPtr = elementsGallery_.getElementPointer(k); |
---|
228 | outfile << elPtr->transportOutputFlow() << endl; |
---|
229 | } |
---|
230 | |
---|
231 | outfile << " SENTINEL " << endl; |
---|
232 | outfile << " SENTINEL " << endl; |
---|
233 | |
---|
234 | outfile.close(); |
---|
235 | |
---|
236 | } |
---|
237 | |
---|
238 | void dataManager::createInputFileParmela( unsigned int indexDeb, unsigned int indexFin) |
---|
239 | { |
---|
240 | indexDeb--; |
---|
241 | indexFin--; |
---|
242 | |
---|
243 | |
---|
244 | |
---|
245 | |
---|
246 | ofstream outfile; |
---|
247 | string name = WORKINGAREA + "parmin"; |
---|
248 | outfile.open(name.c_str(), ios::out); |
---|
249 | if (!outfile) |
---|
250 | { |
---|
251 | cerr << " error opening output stream " << name << endl; |
---|
252 | } |
---|
253 | unsigned int k; |
---|
254 | abstractElement* elPtr; |
---|
255 | |
---|
256 | elPtr = elementsGallery_.getElementPointer(indexDeb); |
---|
257 | if ( elPtr->getName() != initialBeam ) |
---|
258 | { |
---|
259 | cerr << " dataManager::createInputFileParmela : the first element should be initialBeam" << endl; |
---|
260 | } |
---|
261 | |
---|
262 | |
---|
263 | double initalKineticEnergy = 0.0; |
---|
264 | for ( k=indexDeb; k <= indexFin; k++) |
---|
265 | { |
---|
266 | if ( elPtr->getName() == initialBeam ) |
---|
267 | { |
---|
268 | elPtr->setPhaseStep( globParam_.getIntegrationStep() ); |
---|
269 | elPtr = elementsGallery_.getElementPointer(k); |
---|
270 | initalKineticEnergy = elPtr->getInitialKineticEnergy(); |
---|
271 | break; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | outfile << "TITLE" << endl; |
---|
276 | outfile << " titre provisoire " << endl; |
---|
277 | outfile << "RUN /n0=1 /ip=999 /freq=" << globParam_.getFrequency() << " /z0=0.0 /W0=" << initalKineticEnergy << " /itype=1" << endl; |
---|
278 | outfile << "OUTPUT 0" << endl; |
---|
279 | |
---|
280 | for ( k=indexDeb; k <= indexFin; k++) |
---|
281 | { |
---|
282 | elPtr = elementsGallery_.getElementPointer(k); |
---|
283 | outfile << elPtr->parmelaOutputFlow() << endl; |
---|
284 | } |
---|
285 | outfile << "ZOUT" << endl; |
---|
286 | outfile << "START /wt=0.0 /dwt=" << globParam_.getIntegrationStep() << " /nsteps=" << globParam_.getNbSteps() << " nsc=" << globParam_.getScPeriod() << " /nout=10" << endl; |
---|
287 | outfile << "END" << endl; |
---|
288 | outfile.close(); |
---|
289 | } |
---|
290 | |
---|
291 | void dataManager::initializeExecution() |
---|
292 | { |
---|
293 | removeFile("parmdesz"); |
---|
294 | removeFile("parmin"); |
---|
295 | removeFile("transport.input"); |
---|
296 | removeFile("transport.output"); |
---|
297 | currentBeam_.clear(); |
---|
298 | clearSectionToExecute(); |
---|
299 | } |
---|
300 | |
---|
301 | void dataManager::removeFile(string nameOfFile) |
---|
302 | { |
---|
303 | ifstream fileExists; |
---|
304 | string completeName = WORKINGAREA + nameOfFile; |
---|
305 | fileExists.open(completeName.c_str(), ios::in); |
---|
306 | if (fileExists) |
---|
307 | { |
---|
308 | fileExists.close(); |
---|
309 | remove(completeName.c_str()); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | bool dataManager::executeAll(string &resul) |
---|
314 | { |
---|
315 | unsigned k; |
---|
316 | int debut, fin; |
---|
317 | bool success = true; |
---|
318 | for ( k=0; k< jobList_.size(); k++) |
---|
319 | { |
---|
320 | debut = jobList_[k]->firstElement; |
---|
321 | fin = jobList_[k]->lastElement; |
---|
322 | if ( jobList_[k]->software == nomDeLogiciel::parmela ) success = executeParmela( debut, fin, resul); |
---|
323 | else if ( jobList_[k]->software == nomDeLogiciel::transport ) success = executeTransport( debut, fin, resul); |
---|
324 | else |
---|
325 | { |
---|
326 | success = false; |
---|
327 | resul += " unknown software -- STOP \n "; |
---|
328 | break; |
---|
329 | } |
---|
330 | } |
---|
331 | return success; |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | void dataManager::saveAll() |
---|
336 | { |
---|
337 | ofstream outfile; |
---|
338 | string name = WORKINGAREA + "pspa.save"; |
---|
339 | outfile.open(name.c_str(), ios::out); |
---|
340 | if (!outfile) |
---|
341 | { |
---|
342 | cerr << " error opening output file for persistency " << name << endl; |
---|
343 | } |
---|
344 | outfile << globParam_.FileOutputFlow(); |
---|
345 | int k; |
---|
346 | abstractElement* elPtr; |
---|
347 | for ( k=0 ; k < elementsGallery_.size() ; k++) |
---|
348 | { |
---|
349 | elPtr = elementsGallery_.getElementPointer(k); |
---|
350 | outfile << elPtr->FileOutputFlow(); |
---|
351 | } |
---|
352 | outfile.close(); |
---|
353 | } |
---|
354 | |
---|
355 | void dataManager::restoreAll() |
---|
356 | { |
---|
357 | ifstream infile; |
---|
358 | string name = WORKINGAREA + "pspa.save"; |
---|
359 | infile.open(name.c_str(), ios::in); |
---|
360 | if (!infile) |
---|
361 | { |
---|
362 | cerr << " error opening input stream " << name << endl; |
---|
363 | } |
---|
364 | int ielem; |
---|
365 | |
---|
366 | string globalTitle; |
---|
367 | if ( infile >> globalTitle ) |
---|
368 | { |
---|
369 | if ( globalTitle == string("globals") ) |
---|
370 | { |
---|
371 | globParam_.raz(); |
---|
372 | globParam_.FileInput(infile); |
---|
373 | } |
---|
374 | else |
---|
375 | { |
---|
376 | cerr << " dataManager::restoreAll ERROR : global parameters seems to be missing" << endl; |
---|
377 | } |
---|
378 | } |
---|
379 | else |
---|
380 | { |
---|
381 | cerr << " dataManager::restoreAll ERROR : reading data save file" << endl; |
---|
382 | } |
---|
383 | elementsGallery_.raz(); |
---|
384 | nomdElement elem; |
---|
385 | while (infile >> ielem) |
---|
386 | { |
---|
387 | elem = (nomdElement)ielem; |
---|
388 | abstractElement* nouveau = addElement(elem); |
---|
389 | nouveau->FileInput(infile); |
---|
390 | } |
---|
391 | |
---|
392 | |
---|
393 | infile.close(); |
---|
394 | } |
---|