#include "softwareMadx.h" #include "dataManager.h" softwareMadx::softwareMadx() : abstractSoftware() { nameOfSoftware_ = nomDeLogiciel("madx"); } softwareMadx::softwareMadx(string inputFileName,sectorParameters* lattice,dataManager* dt) : abstractSoftware(inputFileName,lattice,dt) { nameOfSoftware_ = nomDeLogiciel("madx"); registerElement(nomdElements::drift,TBoolOk); registerElement(nomdElements::mpole,TBoolOk); } bool softwareMadx::createInputFile(particleBeam* beamBefore,unsigned int numeroDeb,unsigned int numeroFin,string workingDir) { cout << "***********************************" << endl; cout << " softwareMadx::createInputFile(...) " << endl << endl; dataManager_->consoleMessage(" softwareMadx::createInputFile"); if (!initComputationLimits(numeroDeb,numeroFin)) return false; string name = workingDir + inputFileName_; ofstream outfile; outfile.open(name.c_str(),ios::out); if(!outfile) { dataManager_->consoleMessage(" softwareMadx::createInputFile : error opening output stream "); cerr << " error opening output stream " << name << endl; return false; } outfile << endl; // saut de ligne ostringstream os; os << "L: " << "line=("; abstractElement* elPtr; for(unsigned k = numeroDeb_; k <= numeroFin_; k++) { elPtr = dataManager_->getElementPointerFromNumero(k); cout << "debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl; vector v= elPtr->parametersToSoftware(); string label= v.at(0).second.at(1); outfile << inputFormat(v); //<< endl; os << label <<","; }//k; os << ");" << endl; int nCells= sectParamPtr_->getNumberOfCells(); os << "all: line=(" << nCells << "*L);" << endl; outfile << endl; // saut de ligne outfile << os.str() << endl; // beam line definition p34 outfile << "beam;" << endl; // beam data p46 outfile << "use, period = all;" << endl << endl; // action commands p45 outfile << "select,flag = twiss,column = name,s,betx,bety;" << endl; //p48 outfile << "twiss,save,centre,file = twiss.out;" << endl; //p51 outfile << "stop;" << endl; outfile.close(); dataManager_->consoleMessage("input file done for madx"); return true; } string softwareMadx::inputFormat(const vector& v) const { // v.at(0).first = "labelsGenericSpecific" // v.at(0).second = vector de 2 elements (type de l'element,label) string keyword= v.at(0).second.at(0); string label= v.at(0).second.at(1); ostringstream os; if(keyword == "drift") { double length = atof(v.at(1).second.at(0).c_str()); os << label << ":" << " drift, l=" << 0.01*length<< ";" << endl; } else if(keyword == "mpole") { double x[9]= {0.0}; // 0 <= n <= 9 in user's manual of MAD program int n= atoi(v.at(1).second.at(0).c_str()); x[n]= atof(v.at(1).second.at(1).c_str()); os << label << ":" << " multipole, knl={" << x[0]; for(int i = 1; i <= n; i++) { os << "," << x[i]; } os <<"};" << endl; } else { cout << "softwareMadx::inputFormat ERROR : element type= " << keyword << " not defined" << endl; } return os.str(); } bool softwareMadx::execute(string workingDir) { cout << "***********************************" << endl; cout << " softwareMadx::execute(...) " << endl << endl; dataManager_->consoleMessage(" softwareMadx::execute"); bool status= true; ostringstream sortie; sortie << " run madx from " << numeroDeb_ << " to " << numeroFin_ << endl; string mjob = workingDir + "madx64"; mjob += string(" < "); mjob += workingDir + inputFileName_; cout << " job madx = " << mjob << endl; string resultOfRun; bool success = launchJob(mjob,resultOfRun); // xx sortie << resultOfRun << endl; if ( !success ) { //sortie << " launching of madx failed " << endl; status = false; } else { sortie << " madx finished normally" << endl; string nameOut = workingDir + "madx.output"; ofstream outfile; outfile.open(nameOut.c_str(),ios::out); if (!outfile) { sortie << " error in opening madx output stream " << nameOut << endl; status = false; } else { // on copie la sortie dans un fichier 'madx.out' outfile << resultOfRun << endl; outfile.close(); } } dataManager_->consoleMessage(sortie.str()); return status; } bool softwareMadx::buildBeamAfterElements(string workingDir) { cout << "***********************************" << endl; cout << " softwareMadx::buildBeamAfterElements(...) " << endl << endl; dataManager_->consoleMessage(" softwareMadx::buildBeamAfterElements"); return false; }