[295] | 1 | #include "softwareTransport.h" |
---|
| 2 | #include "abstractElement.h" |
---|
| 3 | #include "beam2Moments.h" |
---|
[305] | 4 | #include "dataManager.h" |
---|
[295] | 5 | |
---|
[297] | 6 | #include <algorithm> |
---|
[295] | 7 | |
---|
| 8 | softwareTransport::softwareTransport() : abstractSoftware() |
---|
[386] | 9 | { |
---|
| 10 | nameOfSoftware_ = nomDeLogiciel("transport"); |
---|
| 11 | } |
---|
[295] | 12 | |
---|
[455] | 13 | softwareTransport::softwareTransport(string inputFileName,sectionToExecute* sect) : abstractSoftware(inputFileName, sect) |
---|
[386] | 14 | { |
---|
| 15 | nameOfSoftware_ = nomDeLogiciel("transport"); |
---|
[418] | 16 | registerElement(nomdElements::beam,TBoolOk); |
---|
| 17 | registerElement(nomdElements::drift,TBoolOk); |
---|
| 18 | registerElement(nomdElements::bend,TBoolOk); |
---|
| 19 | registerElement(nomdElements::soleno,TBoolOk); |
---|
| 20 | registerElement(nomdElements::fit,TBoolOk); |
---|
| 21 | registerElement(nomdElements::snapshot,TBoolIgnore); |
---|
[386] | 22 | } |
---|
[295] | 23 | |
---|
[455] | 24 | bool softwareTransport::createInputFile(particleBeam* beamBefore, string workingDir) |
---|
[353] | 25 | { |
---|
| 26 | |
---|
[413] | 27 | |
---|
[455] | 28 | abstractElement* premierEl = getSectionToExecute()->getElements().front(); |
---|
[413] | 29 | |
---|
[295] | 30 | // ceinture et bretelles |
---|
| 31 | if ( premierEl == NULL ) { |
---|
| 32 | cerr << " premier element inexistant ?? " << endl; |
---|
| 33 | return false; |
---|
| 34 | } |
---|
| 35 | |
---|
[353] | 36 | string name = workingDir + inputFileName_; |
---|
[295] | 37 | ofstream outfile; |
---|
| 38 | outfile.open(name.c_str(), ios::out); |
---|
[353] | 39 | if (!outfile) { |
---|
[313] | 40 | dataManager_->consoleMessage(" softwareTransport::createInputFile : error opening output stream " ); |
---|
[353] | 41 | cerr << " error opening output stream " << name << endl; |
---|
| 42 | return false; |
---|
| 43 | } |
---|
[295] | 44 | |
---|
| 45 | outfile << string("'titre provisoire'") << endl; |
---|
| 46 | outfile << " 0 " << endl; |
---|
| 47 | outfile << " UTRANS " << endl; |
---|
| 48 | |
---|
| 49 | |
---|
[413] | 50 | // cout << "debug:: c'est quoi le premier element? " << premierEl->getNomdElement().getElementName() << endl; |
---|
[353] | 51 | |
---|
[295] | 52 | // si le premier element a traiter est un 'beam', on ne fait rien pour l'instant |
---|
| 53 | // il sera traite plus bas de maniere standard |
---|
| 54 | |
---|
| 55 | // Dans le cas où le premier element n'est pas beam |
---|
[418] | 56 | if ( premierEl->getNomdElement().getElementType() != nomdElements::beam ) { |
---|
[295] | 57 | // s'il n'y a pas faisceau courant, erreur |
---|
| 58 | if ( beamBefore == NULL ) { |
---|
[413] | 59 | dataManager_->consoleMessage(" softwareTransport::createInputFile : no input beam, input file not created"); |
---|
[295] | 60 | cout << " pas de faisceau " << endl; |
---|
| 61 | return false; |
---|
[353] | 62 | } else { |
---|
[295] | 63 | // s'il y a un faisceau courant |
---|
| 64 | if ( !beamBefore->momentRepresentationOk() ) { |
---|
| 65 | // s'il n'est pas au format transport (issu de parmela ?) |
---|
| 66 | // on le met au format transport, s'il est deja au format 'particules' (par ex/ parmela) |
---|
| 67 | if ( !beamBefore->particleRepresentationOk() ) { |
---|
[353] | 68 | dataManager_->consoleMessage(" softwareParmela::createInputFile : ca cafouille dans les representations de faisceau : ni particules, ni moments ? " ); |
---|
[295] | 69 | cout << " ca cafouille dans les representations de faisceau : ni particules, ni moments ? " << endl; |
---|
| 70 | return false; |
---|
[353] | 71 | } else { |
---|
[295] | 72 | beamBefore->buildMomentRepresentation(); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | // ici, en principe, on a un faisceau correct |
---|
| 76 | // on ecrit sur le fichier input |
---|
| 77 | const beam2Moments& moments = beamBefore->getTransportMoments(); |
---|
| 78 | double P0 = beamBefore->getP0Transport(); |
---|
| 79 | outfile << moments.writeToTransportInput(P0); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | outfile << " PRINT, BEAM, ON; " << endl; |
---|
| 84 | |
---|
| 85 | abstractElement* elPtr; |
---|
[455] | 86 | for (unsigned k = 0; k < getSectionToExecute()->getElements().size(); k++) |
---|
[295] | 87 | { |
---|
[455] | 88 | elPtr = getSectionToExecute()->getElements()[k]; |
---|
[418] | 89 | cout << "debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl; |
---|
| 90 | // outfile << elPtr->transportOutputFlow(); |
---|
| 91 | outfile << elementsData(elPtr->parametersToSoftware()); |
---|
[295] | 92 | } |
---|
| 93 | |
---|
| 94 | outfile << " SENTINEL " << endl; |
---|
| 95 | outfile << " SENTINEL " << endl; |
---|
| 96 | |
---|
| 97 | outfile.close(); |
---|
[313] | 98 | dataManager_->consoleMessage("fichier input termine pour TRANSPORT"); |
---|
[295] | 99 | return true; |
---|
| 100 | } |
---|
| 101 | |
---|
[386] | 102 | bool softwareTransport::execute(string workingDir) |
---|
[295] | 103 | { |
---|
[353] | 104 | bool ExecuteStatus = true; |
---|
| 105 | |
---|
[295] | 106 | ostringstream sortie; |
---|
[455] | 107 | sortie << " EXECUTION DE TRANSPORT DE " << numeroDeb_deprecated_ << " a " << numeroFin_deprecated_ << endl; |
---|
[353] | 108 | |
---|
[295] | 109 | string transportJob = workingDir + "transport"; |
---|
| 110 | transportJob += string(" < "); |
---|
[316] | 111 | transportJob += workingDir + inputFileName_; |
---|
[295] | 112 | cout << " job transport= " << transportJob << endl; |
---|
[353] | 113 | |
---|
[295] | 114 | string resultOfRun; |
---|
[353] | 115 | bool success = launchJob(transportJob,resultOfRun); |
---|
[295] | 116 | sortie << resultOfRun << endl; |
---|
[353] | 117 | if ( !success ) { |
---|
[295] | 118 | sortie << " launching of transport failed " << endl; |
---|
| 119 | ExecuteStatus = false; |
---|
[353] | 120 | } else { |
---|
[295] | 121 | cout << " execution transport MARCHE " << endl; |
---|
[353] | 122 | string nameOut = workingDir + "transport.output"; |
---|
| 123 | ofstream outfile; |
---|
[295] | 124 | outfile.open(nameOut.c_str(), ios::out); |
---|
[353] | 125 | if (!outfile) { |
---|
| 126 | sortie << " error first opening transport output stream " << nameOut << endl; |
---|
| 127 | ExecuteStatus = false; |
---|
| 128 | } else { |
---|
[295] | 129 | // on copie la sortie dans un fichier 'transport.out' |
---|
| 130 | outfile << resultOfRun << endl; |
---|
| 131 | outfile.close(); |
---|
| 132 | } |
---|
| 133 | } |
---|
[353] | 134 | |
---|
| 135 | dataManager_->consoleMessage(sortie.str()); |
---|
[295] | 136 | return ExecuteStatus; |
---|
| 137 | } |
---|
| 138 | |
---|
[386] | 139 | bool softwareTransport::buildBeamAfterElements(string workingDir) |
---|
[353] | 140 | { |
---|
[295] | 141 | |
---|
| 142 | bool result = true; |
---|
[455] | 143 | if ( !ComputationLimitsOk_deprecated() ) return false; |
---|
[386] | 144 | |
---|
[455] | 145 | for (unsigned k = 0; k < getSectionToExecute()->getElements().size(); k++) |
---|
[353] | 146 | { |
---|
[455] | 147 | abstractElement* ptr = getSectionToExecute()->getElements()[k]; |
---|
[353] | 148 | if ( ptr == NULL ) { |
---|
| 149 | dataManager_->consoleMessage(" softwareTransport::buildBeamAfterElements : null pointer on element " ); |
---|
| 150 | cout << " softwareTransport::buildBeamAfterElements : null pointer on element " << endl; |
---|
[386] | 151 | return false; |
---|
[353] | 152 | } |
---|
[295] | 153 | |
---|
[386] | 154 | |
---|
[418] | 155 | // if ( ptr->is_accepted_by_software(nameOfSoftware_) != TBoolOk ) { |
---|
| 156 | if ( doAcceptElement(ptr->getNomdElement().getElementType()) != TBoolOk ) { |
---|
| 157 | |
---|
[386] | 158 | // si l'element doit etre ignore, on renvoie sur le diag precedent |
---|
| 159 | dataManager_->updateCurrentDiagnostic(false); |
---|
[387] | 160 | // // si l'element est un snapshot, recuperer la sortie precedente |
---|
| 161 | // if(ptr->getNomdElement().getElementType() == snapshot) { |
---|
| 162 | // string* param = ptr->getParametersString(); |
---|
| 163 | // string cliche = workingDir + param[2].c_str(); |
---|
| 164 | // cout<<"["<<k<<"] : ecrit sur fichier " << cliche << " le contenu de beam[ ]"<<endl; |
---|
| 165 | // } |
---|
[386] | 166 | } else { |
---|
| 167 | // si le numero est reconnu de transport |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | string elemLabel = ptr->getLabel(); |
---|
| 172 | beam2Moments transpMoments; |
---|
| 173 | // lecture sortie transport |
---|
| 174 | if (!moments2FromTransport(workingDir,elemLabel,ptr->getNomdElement(),transpMoments) ) |
---|
| 175 | { |
---|
| 176 | // si echec, on renvoie sur le diag precedent |
---|
| 177 | dataManager_->updateCurrentDiagnostic(false); |
---|
| 178 | |
---|
| 179 | dataManager_->consoleMessage(" softwareTransport::buildBeamAfterElements : element skipped in reading moments from transport output : " + elemLabel); |
---|
| 180 | // cout << " softwareTransport::buildBeamAfterElements plantage dans lecture moments " << endl; |
---|
| 181 | return true; |
---|
| 182 | } else { |
---|
| 183 | // si succes, |
---|
| 184 | // on initialise une nouvelle sortie diagnostic |
---|
| 185 | particleBeam* newDiag = dataManager_->updateCurrentDiagnostic(true); |
---|
| 186 | // et on la complete |
---|
| 187 | newDiag->set2Moments(transpMoments); |
---|
| 188 | } |
---|
[353] | 189 | } |
---|
| 190 | } |
---|
| 191 | return result; |
---|
[295] | 192 | } |
---|
[353] | 193 | bool softwareTransport::moments2FromTransport(string workingDir,string elLab,const nomdElements elem,beam2Moments& moments) const |
---|
| 194 | { |
---|
[295] | 195 | string elementLabel = elLab; |
---|
| 196 | // transformer le label en majuscules ; je ne suis pas sur que ca |
---|
| 197 | // marchera a tous les coups (glm) |
---|
[353] | 198 | std::transform(elementLabel.begin(),elementLabel.end(),elementLabel.begin(),(int (*)(int))std::toupper); |
---|
[295] | 199 | |
---|
[353] | 200 | cout << "softwareTransport::moments2FromTransport on cherche element " << elementLabel << endl; |
---|
| 201 | |
---|
| 202 | string nameIn = workingDir + "transport.output"; |
---|
[295] | 203 | ifstream infile; |
---|
| 204 | infile.open(nameIn.c_str(), ios::in); |
---|
| 205 | if (!infile) { |
---|
[313] | 206 | dataManager_->consoleMessage(" softwareTransport::moments2FromTransport : error re-opening transport output stream "); |
---|
[295] | 207 | cerr << " dataManager::moments2FromTransport : error re-opening transport output stream " << nameIn << endl; |
---|
| 208 | return false; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | string::size_type nn = string::npos; |
---|
| 212 | string fichier; |
---|
| 213 | string buf; |
---|
| 214 | unsigned compteur = 0; |
---|
| 215 | while ( getline(infile, buf) ) { |
---|
[353] | 216 | fichier += buf+"\n"; |
---|
[295] | 217 | nn = buf.find(elementLabel); |
---|
| 218 | if ( nn != string::npos ) { |
---|
| 219 | compteur++; |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | infile.close(); |
---|
[353] | 223 | |
---|
[295] | 224 | cout << " compteur= " << compteur << endl; |
---|
| 225 | if ( compteur == 0 ) { |
---|
[313] | 226 | dataManager_->consoleMessage(" softwareTransport::moments2FromTransport : element"+elementLabel+"not found in the file "+nameIn); |
---|
[295] | 227 | cerr << " dataManager::moments2FromTransport : element " << elementLabel << " non trouve dans le fichier " << nameIn << endl; |
---|
| 228 | return false; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | stringstream fichierStream(fichier); |
---|
| 232 | buf.clear(); |
---|
| 233 | unsigned relu = 0; |
---|
| 234 | while ( std::getline(fichierStream, buf) ) { |
---|
| 235 | // cout << " relecture buffer : " << buf << endl; |
---|
| 236 | nn = buf.find(elementLabel); |
---|
| 237 | if ( nn != string::npos ) { |
---|
| 238 | relu++; |
---|
| 239 | if ( relu == compteur ) { |
---|
| 240 | cout << " TROUVE !" << endl; |
---|
| 241 | break; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
[353] | 245 | |
---|
[386] | 246 | return moments.readFromTransportOutput(fichierStream); |
---|
[295] | 247 | } |
---|
[418] | 248 | |
---|
| 249 | string softwareTransport::elementsData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 250 | unsigned k; |
---|
| 251 | cout << " PASSAGE softwareTransport::elementsData " << endl; |
---|
| 252 | if ( donnees.at(0).first != "labelsGenericSpecific" ) { |
---|
| 253 | cout << " softwareTransport::elementsData ERROR : element badly defined " << endl; |
---|
| 254 | return string(); |
---|
| 255 | } |
---|
| 256 | string genericName = donnees.at(0).second.at(0); |
---|
| 257 | if ( genericName == "beam" ) return beamData(donnees); |
---|
| 258 | if ( genericName == "drift" ) return driftData(donnees); |
---|
| 259 | if ( genericName == "solnd" ) return solenoData(donnees); |
---|
| 260 | if ( genericName == "bend" ) return bendData(donnees); |
---|
| 261 | if ( genericName == "fit" ) return fitData(donnees); |
---|
| 262 | return string(); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | string softwareTransport::beamData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 266 | |
---|
| 267 | cout << " PASSAGE softwareTransport::beamData " << endl; |
---|
| 268 | double x = 0.0; |
---|
| 269 | double xp = 0.0; |
---|
| 270 | double y = 0.0; |
---|
| 271 | double yp = 0.0; |
---|
| 272 | double dl = 0.0; |
---|
| 273 | double del = 0.0; |
---|
| 274 | double p0 = 0.0; |
---|
| 275 | string specificName = ""; |
---|
| 276 | |
---|
| 277 | unsigned k; |
---|
| 278 | for ( k=0; k < donnees.size(); k++) { |
---|
| 279 | if ( donnees.at(k).first == "transverseCoordinates" ) { |
---|
| 280 | x = atof( donnees.at(k).second.at(0).c_str() ); |
---|
| 281 | xp = atof( donnees.at(k).second.at(1).c_str() ); |
---|
| 282 | y = atof( donnees.at(k).second.at(2).c_str() ); |
---|
| 283 | yp = atof( donnees.at(k).second.at(3).c_str() ); |
---|
| 284 | } else if ( donnees.at(k).first == "longitudinalCoordinates" ) { |
---|
| 285 | dl = atof( donnees.at(k).second.at(0).c_str() ); |
---|
| 286 | del = atof( donnees.at(k).second.at(1).c_str() ); |
---|
| 287 | } else if ( donnees.at(k).first == "momentum" ) { |
---|
| 288 | p0 = atof( donnees.at(k).second.at(0).c_str() ); |
---|
| 289 | } else if ( donnees.at(k).first == "labelsGenericSpecific" ) { |
---|
| 290 | specificName = donnees.at(k).second.at(1); |
---|
| 291 | } |
---|
| 292 | } |
---|
| 293 | ostringstream sortie; |
---|
| 294 | beam2Moments moments(x, xp, y, yp, dl, del); |
---|
| 295 | sortie << specificName << ":" << moments.writeToTransportInput(p0) << endl; |
---|
| 296 | return sortie.str(); |
---|
| 297 | return sortie.str(); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | string softwareTransport::driftData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 301 | cout << " PASSAGE softwareTransport::driftData " << endl; |
---|
| 302 | |
---|
| 303 | string lenght = "0.0"; |
---|
| 304 | string specificName = ""; |
---|
| 305 | |
---|
| 306 | unsigned k; |
---|
| 307 | for ( k=0; k < donnees.size(); k++) { |
---|
| 308 | if ( donnees.at(k).first == "lenghtAperture" ) { |
---|
| 309 | lenght = donnees.at(k).second.at(0); |
---|
| 310 | } else if ( donnees.at(k).first == "labelsGenericSpecific" ) { |
---|
| 311 | specificName = donnees.at(k).second.at(1); |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | ostringstream sortie; |
---|
| 315 | // longueur en m |
---|
| 316 | sortie << specificName << ":" << " DRIFT, L=" << 0.01*atof(lenght.c_str()) << ";" << endl; |
---|
| 317 | return sortie.str(); |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | |
---|
| 321 | string softwareTransport::solenoData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 322 | cout << " softwareTransport::solenoData " << endl; |
---|
| 323 | string lenght = "0.0"; |
---|
| 324 | string B0 = "0.0"; |
---|
| 325 | string varyB0 = "0"; |
---|
| 326 | string specificName = ""; |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | unsigned k; |
---|
| 330 | for ( k=0; k < donnees.size(); k++) { |
---|
| 331 | if ( donnees.at(k).first == "lenghtAperture" ) { |
---|
| 332 | lenght = donnees.at(k).second.at(0); |
---|
| 333 | } else if ( donnees.at(k).first == "field" ) { |
---|
| 334 | B0 = donnees.at(k).second.at(0); |
---|
| 335 | } else if ( donnees.at(k).first == "isBvariable" ) { |
---|
| 336 | varyB0 = donnees.at(k).second.at(0); |
---|
| 337 | } else if ( donnees.at(k).first == "labelsGenericSpecific" ) { |
---|
| 338 | specificName = donnees.at(k).second.at(1); |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | ostringstream sortie; |
---|
| 342 | // on passe la longueur en metres |
---|
| 343 | sortie << specificName << ":" << " SOLENOID, L=" << 0.01*atof (lenght.c_str()) << ", B=" << B0 << ";" << endl; |
---|
| 344 | if ( atoi(varyB0.c_str()) ) { |
---|
| 345 | sortie << " VARY, " << specificName+"[B]" << ";" << endl; |
---|
| 346 | } |
---|
| 347 | return sortie.str(); |
---|
| 348 | |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | |
---|
| 352 | string softwareTransport::bendData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 353 | |
---|
| 354 | cout << " softwareTransport::bendData " << endl; |
---|
| 355 | |
---|
| 356 | string lenght = "0.0"; |
---|
| 357 | string radius = "0.0"; |
---|
| 358 | string angleDeg = "0.0"; |
---|
| 359 | string beta1 = "0.0"; |
---|
| 360 | string beta2 = "0.0"; |
---|
| 361 | |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | string specificName = ""; |
---|
| 365 | |
---|
| 366 | unsigned k; |
---|
| 367 | for ( k=0; k < donnees.size(); k++) { |
---|
| 368 | if ( donnees.at(k).first == "lenghtAperture" ) { |
---|
| 369 | lenght = donnees.at(k).second.at(0); |
---|
| 370 | } else if ( donnees.at(k).first == "angleDegre" ) { |
---|
| 371 | angleDeg = donnees.at(k).second.at(0); |
---|
| 372 | } else if ( donnees.at(k).first == "radius" ) { |
---|
| 373 | radius = donnees.at(k).second.at(0); |
---|
| 374 | } else if ( donnees.at(k).first == "rotatedFaces" ) { |
---|
| 375 | beta1 = donnees.at(k).second.at(0); |
---|
| 376 | beta2 = donnees.at(k).second.at(1); |
---|
| 377 | } else if ( donnees.at(k).first == "labelsGenericSpecific" ) { |
---|
| 378 | specificName = donnees.at(k).second.at(1); |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | ostringstream sortie; |
---|
| 383 | sortie << specificName << ":" << " SBEND, RADIUS=" << radius << ", ANGLE=" << angleDeg << ", E1=" << beta1 << ", E2=" << beta2 << ";" << endl; |
---|
| 384 | return sortie.str(); |
---|
| 385 | |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | |
---|
| 389 | string softwareTransport::fitData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 390 | cout << " softwareTransport::fitData " << endl; |
---|
| 391 | string variableName = ""; |
---|
| 392 | string variableValue = "0.0"; |
---|
| 393 | string tolerance = "0.0"; |
---|
| 394 | string specificName = ""; |
---|
| 395 | |
---|
| 396 | unsigned k; |
---|
| 397 | for ( k=0; k < donnees.size(); k++) { |
---|
| 398 | if ( donnees.at(k).first == "variable") { |
---|
| 399 | variableName = donnees.at(k).second.at(0); |
---|
| 400 | variableValue = donnees.at(k).second.at(1); |
---|
| 401 | tolerance = donnees.at(k).second.at(2); |
---|
| 402 | } else if ( donnees.at(k).first == "labelsGenericSpecific" ) { |
---|
| 403 | specificName = donnees.at(k).second.at(1); |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | ostringstream sortie; |
---|
| 407 | sortie << specificName << ":" << " FIT," << variableName << " = " << variableValue << ", TOLER = " << tolerance << " ;" << endl; |
---|
| 408 | if ( variableName == "R11" || variableName == "R22" || variableName == "R12" || variableName == "R21" ) { |
---|
| 409 | sortie << " PRINT, TRANS;" << endl; |
---|
| 410 | } |
---|
| 411 | return sortie.str(); |
---|
| 412 | } |
---|