[342] | 1 | |
---|
[304] | 2 | #include "softwareGenerator.h" |
---|
| 3 | #include "abstractElement.h" |
---|
[312] | 4 | #include "generatorParticle.h" |
---|
| 5 | #include "mathematicalConstants.h" |
---|
| 6 | #include "PhysicalConstants.h" |
---|
[359] | 7 | #include "dataManager.h" |
---|
[304] | 8 | |
---|
| 9 | softwareGenerator::softwareGenerator() : abstractSoftware() |
---|
[386] | 10 | { |
---|
[485] | 11 | nameOfSoftware_ = new nomDeLogiciel("generator"); |
---|
[386] | 12 | } |
---|
[304] | 13 | |
---|
[472] | 14 | softwareGenerator::softwareGenerator(string inputFileName, sectionToExecute* sect,dataManager* data) : abstractSoftware(inputFileName, sect,data) |
---|
[386] | 15 | { |
---|
[442] | 16 | cout << " softwareUsersprogram::softwareGenerator ENREGISTREMENT " << endl; |
---|
[485] | 17 | nameOfSoftware_ = new nomDeLogiciel("generator"); |
---|
| 18 | // registerElement(nomdElements::RFgun,TBoolOk); |
---|
[386] | 19 | } |
---|
[304] | 20 | |
---|
[455] | 21 | bool softwareGenerator::createInputFile(particleBeam* beamBefore, string workingDir) |
---|
[304] | 22 | { |
---|
[455] | 23 | abstractElement* firstElement = getSectionToExecute()->getElements().front(); |
---|
| 24 | |
---|
| 25 | if ( firstElement->getNomdElement().getElementType() != nomdElements::RFgun ) { |
---|
[313] | 26 | dataManager_->consoleMessage(" softwareGenerator::createInputFile : the element must be rfgun " ); |
---|
[304] | 27 | cerr << " softwareGenerator::createInputFile : the element must be rfgun" << endl; |
---|
| 28 | return false; |
---|
| 29 | } |
---|
[342] | 30 | |
---|
[304] | 31 | ofstream outfile; |
---|
[316] | 32 | string name = workingDir + inputFileName_; |
---|
[304] | 33 | outfile.open(name.c_str(), ios::out); |
---|
| 34 | if (!outfile) { |
---|
[313] | 35 | dataManager_->consoleMessage(" softwareGenerator::createInputFile : error opening output stream " ); |
---|
[304] | 36 | cerr << " error opening output stream " << name << endl; |
---|
| 37 | return false; |
---|
| 38 | } |
---|
| 39 | outfile << "&INPUT" << endl; |
---|
| 40 | string fichier = "'" + workingDir + "faisceau.ini" + "'"; |
---|
| 41 | outfile << " FNAME = " << fichier << endl; |
---|
| 42 | outfile << " Add=FALSE, N_add=0" << endl; |
---|
| 43 | outfile << " Species='electrons'" << endl; |
---|
[455] | 44 | // outfile << firstElement->generatorOutputFlow() << endl; |
---|
[418] | 45 | |
---|
[455] | 46 | outfile << elementsData(firstElement->parametersToSoftware()) << endl; |
---|
[418] | 47 | |
---|
[304] | 48 | outfile << "/" << endl; |
---|
| 49 | outfile.close(); |
---|
[313] | 50 | dataManager_->consoleMessage("fichier input termine pour GENERATOR"); |
---|
[304] | 51 | return true; |
---|
| 52 | } |
---|
[312] | 53 | |
---|
[386] | 54 | bool softwareGenerator::execute(string workingDir) { |
---|
[342] | 55 | |
---|
| 56 | bool ExecuteStatus = true; |
---|
| 57 | |
---|
[312] | 58 | ostringstream sortie; |
---|
[472] | 59 | sortie << " EXECUTION DE GENERATOR " << endl; |
---|
[312] | 60 | |
---|
| 61 | string generatorJob = workingDir + "generator"; |
---|
| 62 | generatorJob += string(" "); |
---|
[316] | 63 | generatorJob += workingDir + inputFileName_; |
---|
[312] | 64 | |
---|
| 65 | string resultOfRun; |
---|
[342] | 66 | bool success = launchJob(generatorJob,resultOfRun); |
---|
[312] | 67 | sortie << resultOfRun << endl; |
---|
[342] | 68 | if ( !success ) { |
---|
[312] | 69 | sortie << " launching of generator failed " << endl; |
---|
| 70 | ExecuteStatus = false; |
---|
[342] | 71 | } else { |
---|
[312] | 72 | cout << " execution generator MARCHE " << endl; |
---|
| 73 | sortie << resultOfRun; |
---|
[342] | 74 | string nameOut = workingDir + "generator.output"; |
---|
| 75 | ofstream outfile; |
---|
[312] | 76 | outfile.open(nameOut.c_str(), ios::out); |
---|
[342] | 77 | if (!outfile) { |
---|
| 78 | sortie << " error first opening transport output stream " << nameOut << endl; |
---|
| 79 | ExecuteStatus = false; |
---|
| 80 | } else { |
---|
[312] | 81 | // on copie la sortie dans un fichier 'generator.out' |
---|
| 82 | outfile << resultOfRun << endl; |
---|
| 83 | outfile.close(); |
---|
| 84 | } |
---|
| 85 | } |
---|
[342] | 86 | |
---|
| 87 | dataManager_->consoleMessage(sortie.str()); |
---|
[312] | 88 | return ExecuteStatus; |
---|
| 89 | } |
---|
| 90 | |
---|
[386] | 91 | bool softwareGenerator::buildBeamAfterElements( string workingDir) { |
---|
[312] | 92 | |
---|
| 93 | bool result = true; |
---|
[386] | 94 | |
---|
[472] | 95 | if ( getSectionToExecute()->getElements().size() != 1 ) { |
---|
[313] | 96 | dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : only one element (rfgun) must be calculated " ); |
---|
| 97 | return false; |
---|
| 98 | } |
---|
[386] | 99 | // on initialise une nouvelle sortie diagnostic |
---|
| 100 | particleBeam* newDiag = dataManager_->updateCurrentDiagnostic(true); |
---|
[312] | 101 | |
---|
[386] | 102 | // beams.push_back(particleBeam()); |
---|
[312] | 103 | vector<double> centroid; |
---|
| 104 | bareParticle refPart; |
---|
| 105 | vector<bareParticle> particles; |
---|
| 106 | vector<bareParticle> particlesPassives; // on ne fait rien de ces particules pour l'instant |
---|
| 107 | if (beamFromGenerator(string("faisceau.ini"),workingDir, centroid, refPart,particles, particlesPassives )) { |
---|
[386] | 108 | newDiag->setWithParticles(centroid, refPart,particles); |
---|
[312] | 109 | } else { |
---|
[313] | 110 | dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : error " ); |
---|
[312] | 111 | result = false; |
---|
| 112 | } |
---|
| 113 | return result; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | bool softwareGenerator::beamFromGenerator(string beamFileName, string workingDir, vector<double>& centroid, bareParticle& refPart,vector<bareParticle>& particles, vector<bareParticle>& passiveParticles ) { |
---|
| 117 | unsigned k; |
---|
| 118 | FILE* filefais; |
---|
| 119 | string nomfilefais = workingDir + beamFileName; |
---|
| 120 | cout << " nom fichier sortie generator : " << nomfilefais << endl; |
---|
| 121 | filefais = fopen(nomfilefais.c_str(), "r"); |
---|
| 122 | if ( filefais == (FILE*)0 ) { |
---|
[313] | 123 | dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error openig the file " + nomfilefais ); |
---|
[312] | 124 | cerr << " beamFromGenerator() erreur a l'ouverture du fichier" << nomfilefais << endl;; |
---|
| 125 | return false; |
---|
| 126 | } |
---|
| 127 | else cout << " beamFromGenerator() : ouverture du fichier " << nomfilefais << endl; |
---|
| 128 | |
---|
| 129 | generatorParticle partic; |
---|
| 130 | std::vector<generatorParticle> faisceau; |
---|
| 131 | int nbProbPart =0; |
---|
| 132 | double timeRef = 0.0; |
---|
| 133 | double betagammaZRef = 0.0; |
---|
| 134 | // lecture part. de reference |
---|
| 135 | if ( partic.readFromGeneratorFile(filefais) > 0 ) |
---|
| 136 | { |
---|
| 137 | if ( partic.index != 1 ) { |
---|
[313] | 138 | dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method beamFromGenerator" ); |
---|
| 139 | cout << " ERROR softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method " << endl; |
---|
[312] | 140 | return false; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | if ( partic.flag != -1 ) { |
---|
| 144 | cout << " ATTENTION softwareGenerator::beamFromGenerator : flag different de -1 " << endl; |
---|
| 145 | } |
---|
| 146 | if (fabs(partic.xx) > EPSILON || fabs(partic.yy) > EPSILON || fabs(partic.px) > EPSILON || fabs(partic.py) > EPSILON) { |
---|
| 147 | printf(" ATTENTION softwareGenerator::beamFromGenerator : part. reference douteuse \n"); |
---|
| 148 | partic.imprim(); |
---|
| 149 | } |
---|
| 150 | timeRef = partic.clock; |
---|
| 151 | TRIDVECTOR posRef(100.*partic.xx,100.*partic.yy,100.*partic.zz); // en cm |
---|
[356] | 152 | betagammaZRef = partic.pz/EREST_eV; |
---|
[312] | 153 | // l'impulsion donnee par generator est en eV/c |
---|
[356] | 154 | TRIDVECTOR betagammaRef(partic.px/EREST_eV , partic.py/EREST_eV, betagammaZRef); |
---|
[312] | 155 | refPart = bareParticle(posRef, betagammaRef); |
---|
| 156 | |
---|
| 157 | // seule la part. de reference a un pz absolu (les autres pat. on un pz relatif a la ref) |
---|
| 158 | // je mets ici a zero, pour homogeneiser |
---|
| 159 | partic.pz = 0.0; |
---|
| 160 | faisceau.push_back(partic); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | while( partic.readFromGeneratorFile(filefais) > 0 ) { |
---|
| 164 | faisceau.push_back(partic); |
---|
| 165 | if ( partic.flag != -1 ) nbProbPart++; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if ( faisceau.size() == 0) |
---|
| 169 | { |
---|
[313] | 170 | dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error no particle found" ); |
---|
[312] | 171 | cerr << " softwareGenerator::beamFromGenerator echec lecture " << endl; |
---|
| 172 | return false; |
---|
| 173 | } |
---|
| 174 | // pour l'instant on choisit un centroid nul; |
---|
| 175 | centroid.clear(); |
---|
| 176 | centroid = vector<double>(6,0.0); |
---|
| 177 | |
---|
| 178 | particles.clear(); |
---|
| 179 | passiveParticles.clear(); |
---|
| 180 | // particles.resize(faisceau.size() - nbProbPart, bareParticle()); |
---|
| 181 | // passiveParticles.resize(nbProbPart, bareParticle()); |
---|
| 182 | double x,y; |
---|
[417] | 183 | // double deltaz; |
---|
| 184 | double cdeltat; |
---|
[312] | 185 | TRIDVECTOR pos; |
---|
| 186 | TRIDVECTOR betagamma; |
---|
| 187 | double pxPart; |
---|
| 188 | double pyPart; |
---|
| 189 | double pzPartRel; |
---|
| 190 | double deltaPzPart; |
---|
| 191 | |
---|
| 192 | for ( k=0; k < faisceau.size(); k++) { |
---|
| 193 | |
---|
| 194 | pxPart = faisceau.at(k).px; |
---|
| 195 | pyPart = faisceau.at(k).py; |
---|
| 196 | pzPartRel = faisceau.at(k).pz; |
---|
| 197 | deltaPzPart = faisceau.at(k).pz; |
---|
| 198 | x=faisceau.at(k).xx; |
---|
| 199 | y=faisceau.at(k).yy; |
---|
| 200 | |
---|
| 201 | // tout ce qui suit sera a clarifier |
---|
[356] | 202 | double betaGammax = pxPart/EREST_eV; |
---|
| 203 | double betaGammay = pyPart/EREST_eV; |
---|
| 204 | double betaGammaz = betagammaZRef + pzPartRel/EREST_eV; |
---|
[312] | 205 | betagamma.setComponents(betaGammax, betaGammay, betaGammaz); |
---|
| 206 | double gamma = sqrt(1.0 + betagamma.norm2()); |
---|
| 207 | |
---|
[417] | 208 | // decalage temporel par rapport a la reference ? |
---|
| 209 | // on prend le faisceau sous la forme "a un instant donne" |
---|
[312] | 210 | |
---|
[417] | 211 | // deltat = faisceau.at(k).clock - timeRef; // nanoseondes |
---|
| 212 | cdeltat = CLIGHT_m_per_ns * (faisceau.at(k).clock - timeRef); // metres |
---|
| 213 | // double ds = CLIGHT_m_per_ns * deltat /gamma; |
---|
| 214 | // x += betaGammax * ds; // en metres |
---|
| 215 | // y += betaGammay * ds; |
---|
| 216 | |
---|
[312] | 217 | // ici on neglige la difference entre gamma de la part. et gamma de la ref. |
---|
[417] | 218 | // deltaz = (pzPartRel/EREST_eV) * CLIGHT_m_per_ns * deltat; // en metres |
---|
[312] | 219 | |
---|
[417] | 220 | // pos.setComponents(100.*x,100.*y,100.*deltaz); // en cm |
---|
| 221 | pos.setComponents(100.*x,100.*y,100.*cdeltat); // en cm |
---|
[312] | 222 | if ( faisceau.at(k).flag == -1 ) { |
---|
| 223 | particles.push_back(bareParticle(pos,betagamma)); |
---|
[434] | 224 | // cout << " generator enregistre " << particles.back().FileOutputFlow() << endl; |
---|
[312] | 225 | } else { |
---|
| 226 | passiveParticles.push_back(bareParticle(pos,betagamma)); |
---|
| 227 | } |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | return true; |
---|
| 232 | } |
---|
[418] | 233 | |
---|
| 234 | |
---|
| 235 | string softwareGenerator::elementsData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 236 | unsigned k; |
---|
| 237 | cout << " PASSAGE softwareGenerator::elementsData " << endl; |
---|
| 238 | if ( donnees.at(0).first != "labelsGenericSpecific" ) { |
---|
| 239 | cout << " softwareGenerator::elementsData ERROR : element badly defined " << endl; |
---|
| 240 | return string(); |
---|
| 241 | } |
---|
| 242 | string genericName = donnees.at(0).second.at(0); |
---|
| 243 | if ( genericName == "rfgun" ) return rfgunData(donnees); |
---|
| 244 | return string(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | string softwareGenerator::rfgunData(const vector< pair<string, vector<string> > >& donnees) const { |
---|
| 248 | |
---|
| 249 | cout << " PASSAGE softwareGenerator::rfgunData " << endl; |
---|
| 250 | string nmacrop = "0"; |
---|
| 251 | string sigma_t = "0.0"; |
---|
| 252 | string sigma_r = "0.0"; |
---|
| 253 | string emit_x = "0.0"; |
---|
| 254 | string emit_y = "0.0"; |
---|
| 255 | string E_cin = "0.0"; |
---|
| 256 | string sigma_E = "0.0"; |
---|
| 257 | string phaseStep = "0.0"; |
---|
| 258 | string totalCharge = "0.0"; |
---|
| 259 | |
---|
| 260 | unsigned k; |
---|
| 261 | for ( k=1; k < donnees.size(); k++) { |
---|
| 262 | if ( donnees.at(k).first == "nbMacroparticles" ) { |
---|
| 263 | nmacrop = donnees.at(k).second.at(0); |
---|
| 264 | } else if ( donnees.at(k).first == "sigmasTR" ) { |
---|
| 265 | sigma_t = donnees.at(k).second.at(0); |
---|
| 266 | sigma_r = donnees.at(k).second.at(1); |
---|
| 267 | } else if ( donnees.at(k).first == "emittancesXY" ) { |
---|
| 268 | emit_x = donnees.at(k).second.at(0); |
---|
| 269 | emit_y = donnees.at(k).second.at(1); |
---|
| 270 | } else if ( donnees.at(k).first == "kineticE" ) { |
---|
| 271 | E_cin = donnees.at(k).second.at(0); |
---|
| 272 | sigma_E = donnees.at(k).second.at(1); |
---|
| 273 | } else if ( donnees.at(k).first == "phaseStep" ) { |
---|
| 274 | phaseStep = donnees.at(k).second.at(0); |
---|
| 275 | } else if ( donnees.at(k).first == "totalCharge" ) { |
---|
| 276 | totalCharge = donnees.at(k).second.at(0); |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | ostringstream sortie; |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | sortie << "Ipart=" << nmacrop << endl; |
---|
| 283 | |
---|
| 284 | sortie << "Probe=.True." << endl; |
---|
| 285 | sortie << "Noise_reduc=.T" << endl; |
---|
| 286 | sortie << "Cathode=.T." << endl; |
---|
| 287 | sortie << "Q_total=" << totalCharge << endl; |
---|
| 288 | sortie << "Ref_zpos=0.0" << endl; |
---|
| 289 | sortie << "Ref_clock=0.0" << endl; |
---|
| 290 | sortie << "Ref_Ekin=" << E_cin << endl; // tjs en MeV |
---|
| 291 | sortie << "Dist_z='g'" << endl; |
---|
| 292 | sortie << "sig_clock=" << 1.0e-3*atof(sigma_t.c_str()) << endl; // passage de ps en ns |
---|
| 293 | sortie << "Dist_pz='g', sig_Ekin=" << 1000.*atof(sigma_E.c_str()) << ", emit_z=0.0 , cor_Ekin=0.0 " << endl; // passage en keV |
---|
| 294 | sortie << "Dist_x='gauss', sig_x=" << 10.*atof(sigma_r.c_str()) << endl; // passage en mm |
---|
| 295 | sortie << "Dist_px='g', Nemit_x=" << emit_x << ", cor_px=0.0" << endl; |
---|
| 296 | sortie << "Dist_y='gauss', sig_y=" << 10.*atof(sigma_r.c_str()) << endl; // passage en mm |
---|
| 297 | sortie << "Dist_py='g', Nemit_y=" << emit_y << ", cor_py=0.0" << endl; |
---|
| 298 | |
---|
| 299 | return sortie.str(); |
---|
| 300 | } |
---|