#include "softwareMadx.h" #include "dataManager.h" softwareMadx::softwareMadx() : abstractSoftware() { nameOfSoftware_ = nomDeLogiciel("madx"); } softwareMadx::softwareMadx(string inputFileName,sectionToExecute* sect, dataManager* data) : abstractSoftware(inputFileName,sect,data) { nameOfSoftware_ = nomDeLogiciel("madx"); registerElement(nomdElements::drift,TBoolOk); registerElement(nomdElements::mpole,TBoolOk); } bool softwareMadx::createInputFile(particleBeam* beamBefore, string workingDir) { cout << "***********************************" << endl; cout << " softwareMadx::createInputFile(...) " << endl << endl; dataManager_->consoleMessage(" softwareMadx::createInputFile"); 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; } cout << " softwareMadx::createInputFile(...) lecture des elts " << endl; // element label ////////////////////////// outfile << endl; // saut de ligne abstractElement* elPtr; std::vector elements = getSectionToExecute()->getElements(); for(unsigned k = 0; k < elements.size(); k++) { elPtr = elements[k]; //cout << " debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl; vector v= elPtr->parametersToSoftware(); outfile << inputFormat(v); } // sublines /////////////////////////////// outfile << endl; // saut de ligne //_______________________________________________________ // FIXME Francois !!!!! //_______________________________________________________ /* vector u = sectParamPtr_->getSectors(); for(unsigned int i = 0; i < u.size(); i++) { ostringstream os; os << u.at(i).first << ": line=("; unsigned ai = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(0)); unsigned bi = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(1)); std::vector elements = getSectionToExecute()->getElements(); for(unsigned k = 0; k < elements.size(); k++) { elPtr = elements[k]; if(k >= ai && k <= bi) { if(k == bi) os << elPtr->getLabel() <<");"; else os << elPtr->getLabel() <<","; } }// k outfile << os.str() << endl; }// i // relection and repetition /////////////// outfile << endl; // saut de ligne string *str= NULL; if(u.size() > 0) { str = new string[u.size()]; for(unsigned int i = 0; i < u.size(); i++) str[i]= "false"; } ostringstream os; os << "all: " << "line=("; for(unsigned k = 0; k < elements.size(); k++) { elPtr = elements[k]; bool isMember = false; for(unsigned int i = 0; i < u.size(); i++) { unsigned ai = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(0)); unsigned bi = dataManager_->getNumeroFromElementLabel(u.at(i).second.at(1)); if(k >= ai && k <= bi) { isMember = true; if(str[i] == "false") { //int reflection = atoi(u.at(i).second.at(2).c_str()); int reflection = ( (u.at(i).second.at(2) == "Unchecked") ? 0:1 ); int repeat = atoi(u.at(i).second.at(3).c_str()); if(reflection > 0) os << "-" << repeat << "*" << u.at(i).first; else os << repeat << "*" << u.at(i).first; if(i < u.size()-1) os << ", "; } str[i] = "true"; break; } }// i if(isMember && k == (elements.size()-1)) os << ");"; if(!isMember) { if(k == (elements.size()-1)) os << elPtr->getLabel() << ");"; else os << elPtr->getLabel() << ","; } }// k outfile << os.str() << endl; /////////////////////////////////////////// outfile << endl; // saut de ligne 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 = "+workingDir+"twiss.out;" << endl; //p51 outfile << "stop;" << endl; */ //_______________________________________________________ // FIXME Francois !!!!! //_______________________________________________________ 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 " << 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; }