Commit 191cf5fc authored by TOUZE Francois's avatar TOUZE Francois
Browse files

check for twiss & match actions with updated softwares

parent d054d610
...@@ -66,6 +66,9 @@ bool softwareElegant::createInputFile(particleBeam *beamBefore) ...@@ -66,6 +66,9 @@ bool softwareElegant::createInputFile(particleBeam *beamBefore)
ofstream outfile; ofstream outfile;
string sfile= userDir_ + lattice; string sfile= userDir_ + lattice;
outfile.open(sfile.c_str(),ios::out); outfile.open(sfile.c_str(),ios::out);
// the list of the parameters to be varied (filled in beamLine)
if( !vary_.empty() ) vary_.clear();
outfile << beamLine( firstIndex ) << endl; outfile << beamLine( firstIndex ) << endl;
outfile.close(); outfile.close();
...@@ -80,7 +83,11 @@ bool softwareElegant::createInputFile(particleBeam *beamBefore) ...@@ -80,7 +83,11 @@ bool softwareElegant::createInputFile(particleBeam *beamBefore)
if ( action == "twiss" ) { if ( action == "twiss" ) {
outfile << run_setup( lattice ); outfile << run_setup( lattice );
outfile << twiss_output(amap,true,false,false); outfile << "&twiss_output\n";
outfile << twiss_options( amap ) << endl;
// the Twiss parameters file => xxx-twiss.sdds
outfile << " filename = " << simulationId_+"-twiss.sdds" << endl;
outfile << "&end\n" << endl;
outfile << "&run_control &end\n" << endl; outfile << "&run_control &end\n" << endl;
} }
...@@ -88,28 +95,33 @@ bool softwareElegant::createInputFile(particleBeam *beamBefore) ...@@ -88,28 +95,33 @@ bool softwareElegant::createInputFile(particleBeam *beamBefore)
if ( action == "match" ) { if ( action == "match" ) {
outfile << run_setup( lattice ); outfile << run_setup( lattice );
outfile << twiss_output(amap,false,false,false); outfile << "&twiss_output\n";
outfile << " matched = 0\n";
outfile << twiss_initialValues( amap );
outfile << "&end\n" << endl;
outfile << "&run_control &end\n" << endl; outfile << "&run_control &end\n" << endl;
outfile << optimization( amap ); outfile << optimization( amap );
outfile << "&bunched_beam &end\n" << endl; outfile << "&bunched_beam &end\n" << endl;
outfile << "&optimize &end\n" << endl; outfile << "&optimize &end\n" << endl;
// save the current beamLine => xxx-save_current.new // save the current beamLine => xxx-save_current.new
string save_current= simulationId_ + "-save_current.new"; string save_current= simulationId_ + "-save_current.new";
outfile << "&save_lattice" << endl; outfile << "&save_lattice" << endl;
outfile << " filename = " << save_current << endl; outfile << " filename = " << save_current << endl;
outfile << "&end\n" << endl; outfile << "&end\n" << endl;
outfile << run_setup( save_current ); outfile << run_setup( save_current );
outfile << twiss_output(amap,true,false,true ); outfile << "&twiss_output\n";
outfile << " matched = 0\n";
outfile << twiss_initialValues( amap );
outfile << " statistics = 1\n";
outfile << " filename = " << simulationId_+"-twiss.sdds" << endl;
outfile << "&end\n" << endl;
outfile << "&run_control &end" << endl; outfile << "&run_control &end" << endl;
} }
outfile << "&bunched_beam &end\n" << endl; outfile << "&bunched_beam &end\n" << endl;
outfile << "&track &end\n" << endl; outfile << "&track &end\n" << endl;
outfile << "&stop &end" << endl; outfile << "&stop &end" << endl;
outfile.close(); outfile.close();
dataManager_->consoleMessage("inputFile done for elegant\n"); dataManager_->consoleMessage("inputFile done for elegant\n");
return true; return true;
...@@ -236,6 +248,7 @@ string softwareElegant::run_control() ...@@ -236,6 +248,7 @@ string softwareElegant::run_control()
return os.str(); return os.str();
} }
/**
string softwareElegant::twiss_output(const std::vector<smap>& v,bool outfile,bool output_at_each_step,bool statistics) string softwareElegant::twiss_output(const std::vector<smap>& v,bool outfile,bool output_at_each_step,bool statistics)
{ {
if(debug > 0) cout << "softwareElegant::twiss_output()\n"; if(debug > 0) cout << "softwareElegant::twiss_output()\n";
...@@ -289,6 +302,54 @@ string softwareElegant::twiss_output(const std::vector<smap>& v,bool outfile,boo ...@@ -289,6 +302,54 @@ string softwareElegant::twiss_output(const std::vector<smap>& v,bool outfile,boo
os << "&end\n" << endl; os << "&end\n" << endl;
return os.str(); return os.str();
} }
*/
string softwareElegant::twiss_options(const std::vector<smap>& v)
{
ostringstream os;
string str= v[0].find("twiss_option")->second.at(0);
if ( str.find("option 1") != string::npos ) {
os << " matched = 0" << endl;
os << " beta_x = " << bunch_.find("beta_x")->second.at(0) << endl;
os << " beta_y = " << bunch_.find("beta_y")->second.at(0) << endl;
os << " alpha_x = " << bunch_.find("alfa_x")->second.at(0) << endl;
os << " alpha_y = " << bunch_.find("alfa_y")->second.at(0) << endl;
} else if ( str.find("option 2") != string::npos ) {
// calculations are performed in transport line mode starting from the given initial values of betax, alphax, etc..
os << " matched = 0" << endl;
os << twiss_initialValues( v ) << endl;
} else {
os << " matched = 1" << endl;
}
return os.str();
}
string softwareElegant::twiss_initialValues(const std::vector<smap>& v)
{
ostringstream os;
smap::const_iterator kt= v[1].find("beta_x");
if (kt != v[1].end()) os << " beta_x = " << kt->second.at(0) << endl;
kt= v[1].find("alpha_x");
if (kt != v[1].end()) os << " alpha_x = " << kt->second.at(0) << endl;
kt= v[1].find("dx");
if (kt != v[1].end()) os << " eta_x = " << kt->second.at(0) << endl;
kt= v[1].find("beta_y");
if (kt != v[1].end()) os << " beta_y = " << kt->second.at(0) << endl;
kt= v[1].find("alpha_y");
if (kt != v[1].end()) os << " alpha_y = " << kt->second.at(0) << endl;
kt= v[1].find("dpx");
if (kt != v[1].end()) os << " etap_x = " << kt->second.at(0) << endl;
return os.str();
}
string softwareElegant::nameOpticalFunction(string name) string softwareElegant::nameOpticalFunction(string name)
{ {
...@@ -373,7 +434,7 @@ string softwareElegant::optimization(const std::vector<smap>& v) ...@@ -373,7 +434,7 @@ string softwareElegant::optimization(const std::vector<smap>& v)
os << "&optimization_setup" << endl; os << "&optimization_setup" << endl;
if ( !os1.str().empty() ) os << " equation = " << os1.str() << endl; if ( !os1.str().empty() ) os << " equation = " << os1.str() << endl;
os << " mode = " << v[ 6 ].find("mode")->second.at(0) << endl; os << " mode = " << v[ 6 ].find("mode")->second.at(0) << endl;
//recommended methods are “simplex” and “randomwalk”. // recommended methods are “simplex” and “randomwalk”.
string s= v[ 6 ].find("method")->second.at(0); string s= v[ 6 ].find("method")->second.at(0);
if ( s.find("none") == string::npos ) if ( s.find("none") == string::npos )
os << " method = " << s << endl; os << " method = " << s << endl;
...@@ -409,10 +470,22 @@ string softwareElegant::optimization(const std::vector<smap>& v) ...@@ -409,10 +470,22 @@ string softwareElegant::optimization(const std::vector<smap>& v)
smap::const_iterator it; smap::const_iterator it;
for(it = v[ 4 ].begin(); it != v[ 4 ].end(); ++it) for(it = v[ 4 ].begin(); it != v[ 4 ].end(); ++it)
{ {
string s( it->second.at(0) ); string attribute;
string::size_type nn= s.find(":"); smap::iterator where= vary_.find( it->first );
string attribute(s.substr(0,nn)); if( where == vary_.end() ) continue;
os << "&optimization_variable" << endl;
string key= where->second.at(0);
if (key == "mpole") {
attribute= "KNL";
} else if (key == "qpole") {
attribute= "K1";
} else if (key == "spole") {
attribute= "K2";
} else {
continue;
}
os << "&optimization_variable\n";
os << " name = " + it->first + ", item = " << attribute; os << " name = " + it->first + ", item = " << attribute;
string low= it->second.at(2); string low= it->second.at(2);
if( low.find("none") == string::npos ) if( low.find("none") == string::npos )
...@@ -446,7 +519,7 @@ string softwareElegant::beamLine(unsigned firstIndex) ...@@ -446,7 +519,7 @@ string softwareElegant::beamLine(unsigned firstIndex)
elPtr= getComputingBlock()->getElement( k ); elPtr= getComputingBlock()->getElement( k );
string label= elPtr->getLabel(); string label= elPtr->getLabel();
if(find(lElts.begin(),lElts.end(),label) == lElts.end()) { if (find(lElts.begin(),lElts.end(),label) == lElts.end()) {
vector<psvs> v= elPtr->parametersToSoftware(); vector<psvs> v= elPtr->parametersToSoftware();
os1 << elementsData( v ); os1 << elementsData( v );
...@@ -463,14 +536,15 @@ string softwareElegant::beamLine(unsigned firstIndex) ...@@ -463,14 +536,15 @@ string softwareElegant::beamLine(unsigned firstIndex)
return os.str(); return os.str();
} }
string softwareElegant::elementsData(const vector<psvs>& v) const string softwareElegant::elementsData(const vector<psvs>& v)
{ {
if(debug > 0) cout << "softwareElegant::inputFormat()\n"; if(debug > 0) cout << "softwareElegant::inputFormat()\n";
string keyword= v.at(0).second.at(0); string keyword= v.at(0).second.at(0);
string label= v.at(0).second.at(1); string label= v.at(0).second.at(1);
vary_[ label ].push_back( keyword );
ostringstream os; ostringstream os;
if(keyword == "marker") { if(keyword == "marker") {
os << label << ": marker\n"; os << label << ": marker\n";
...@@ -481,13 +555,13 @@ string softwareElegant::elementsData(const vector<psvs>& v) const ...@@ -481,13 +555,13 @@ string softwareElegant::elementsData(const vector<psvs>& v) const
os << label << ":" << " drift, l=" << length << endl; os << label << ":" << " drift, l=" << length << endl;
} else if(keyword == "qpole") { } else if(keyword == "qpole") {
double ln= atof(v.at(1).second.at(0).c_str()); double ln= atof(v.at(1).second.at(0).c_str());
double k1= atof(v.at(1).second.at(1).c_str()); double k1= atof(v.at(1).second.at(1).c_str());
os << label << ":" << " quadrupole, l=" << ln << ", k1= " << k1 << "\n"; os << label << ":" << " quadrupole, l=" << ln << ", k1= " << k1 << "\n";
} else if(keyword == "spole") { } else if(keyword == "spole") {
double ln= atof(v.at(1).second.at(0).c_str()); double ln= atof(v.at(1).second.at(0).c_str());
double k2= atof(v.at(1).second.at(1).c_str()); double k2= atof(v.at(1).second.at(1).c_str());
os << label << ":" << " sextupole, l=" << ln << ", k2= " << k2 << endl; os << label << ":" << " sextupole, l=" << ln << ", k2= " << k2 << endl;
...@@ -496,7 +570,7 @@ string softwareElegant::elementsData(const vector<psvs>& v) const ...@@ -496,7 +570,7 @@ string softwareElegant::elementsData(const vector<psvs>& v) const
int order= atoi(v.at(1).second.at(0).c_str()); int order= atoi(v.at(1).second.at(0).c_str());
double knL= atof(v.at(1).second.at(1).c_str()); double knL= atof(v.at(1).second.at(1).c_str());
os << label << ":" << " mult, knl=" << knL << ", order= " << order << endl; os << label << ":" << " mult, knl=" << knL << ", order= " << order << "\n";
} else if(keyword == "bend") { } else if(keyword == "bend") {
......
...@@ -8,17 +8,22 @@ ...@@ -8,17 +8,22 @@
class softwareElegant : public abstractSoftware class softwareElegant : public abstractSoftware
{ {
smap bunch_; smap bunch_;
smap vary_;
void beamData(const vector<psvs>&); void beamData(const vector<psvs>&);
void fromParameters(const vector<psvs>&); void fromParameters(const vector<psvs>&);
void fromDistribution(const vector<psvs>&); void fromDistribution(const vector<psvs>&);
void beamData(particleBeam*); void beamData(particleBeam*);
string beamLine(unsigned); string beamLine(unsigned);
string elementsData(const vector<psvs>&) const; string elementsData(const vector<psvs>&);
string run_setup(string); string run_setup(string);
string run_control(); string run_control();
string twiss_output(const std::vector<smap>&,bool,bool,bool);
//string twiss_output(const std::vector<smap>&,bool,bool,bool);
string twiss_initialValues(const std::vector<smap>&);
string twiss_options(const std::vector<smap>&);
string optimization(const vector<smap>&); string optimization(const vector<smap>&);
string nameOpticalFunction(string); string nameOpticalFunction(string);
......
...@@ -264,8 +264,6 @@ string softwareMadx::twissStatement(const std::vector<smap>& v) ...@@ -264,8 +264,6 @@ string softwareMadx::twissStatement(const std::vector<smap>& v)
string opticalFunctions= "betx,alfx,mux,dx,dpx,bety,alfy,muy;"; string opticalFunctions= "betx,alfx,mux,dx,dpx,bety,alfy,muy;";
os << "select, flag= twiss, column= name,s," + opticalFunctions << "\n"; os << "select, flag= twiss, column= name,s," + opticalFunctions << "\n";
// => os << "select, flag= twiss, column= name,s,betx,alfx,dx,bety,alfy" << ";\n";
ostringstream os3; ostringstream os3;
string str = v[0].find("twiss_option")->second.at(0); string str = v[0].find("twiss_option")->second.at(0);
if (str.find("option 1") != string::npos) { if (str.find("option 1") != string::npos) {
...@@ -298,11 +296,7 @@ string softwareMadx::twissStatement(const std::vector<smap>& v) ...@@ -298,11 +296,7 @@ string softwareMadx::twissStatement(const std::vector<smap>& v)
} }
// creates the TWISS table => xxx-twiss.txt // creates the TWISS table => xxx-twiss.txt
os << "twiss, save, file= " + getFileName("twiss",true) + os3.str() << ";\n"; os << "twiss, file= " + getFileName("twiss",true) + os3.str() << ";\n";
// Plotting data
// => os << "plot, haxis=s, vaxis1=betx,bety, vaxis2=dx, colour=100, file= " + setPlotsFile() << ";\n";
os << "plot, haxis=s, vaxis1=betx,bety, colour=100, file= " + setPlotsFile() << ";\n";
if (!v[ 2 ].empty()) { if (!v[ 2 ].empty()) {
...@@ -327,15 +321,10 @@ string softwareMadx::twissStatement(const std::vector<smap>& v) ...@@ -327,15 +321,10 @@ string softwareMadx::twissStatement(const std::vector<smap>& v)
os << endl; os << endl;
os << os1.str() << endl; os << os1.str() << endl;
os << "select, flag= twiss, clear;\n"; os << "select, flag= twiss, clear;\n";
string derivedQuantities= "s,sigx,Hinvx,sigy;"; string derivedQuantities= "sigx,Hinvx,sigy;";
os << "select, flag= twiss, column= s," + derivedQuantities << "\n"; os << "select, flag= twiss, column= " + derivedQuantities << "\n";
// creates the SIGMA table => xxx-sigma.txt // creates the SIGMA table => xxx-sigma.txt
os << "twiss, save, file= " + getFileName("sigma",true) << ";\n"; os << "twiss, file= " + getFileName("sigma",true) + os3.str() << ";\n";
// Plotting data
os << "plot, haxis=s, vaxis=sigx,sigy, colour=100;\n";
os << "plot, haxis=s, vaxis=Hinvx, colour=100;\n";
} }
// chromatic functions // chromatic functions
...@@ -343,13 +332,9 @@ string softwareMadx::twissStatement(const std::vector<smap>& v) ...@@ -343,13 +332,9 @@ string softwareMadx::twissStatement(const std::vector<smap>& v)
if (found != it->second.end()) { if (found != it->second.end()) {
os << "select, flag= twiss, clear;\n"; os << "select, flag= twiss, clear;\n";
os << "select, flag= twiss, column= s,wx,wy,ddx;\n"; os << "select, flag= twiss, column= wx,wy,ddx;\n";
// creates the CHROM table => xxx-chrom.txt // creates the CHROM table => xxx-chrom.txt
os << "twiss, chrom, file= " + getFileName("chrom",true) << ";\n"; os << "twiss, chrom, file= " + getFileName("chrom",true) + os3.str() << ";\n";
// Plotting data
os << "plot, table=twiss, haxis=s, vaxis1=wx,wy, vaxis2= ddx, colour=100;\n";
} }
} }
...@@ -739,16 +724,16 @@ string softwareMadx::beamLine(unsigned firstIndex) ...@@ -739,16 +724,16 @@ string softwareMadx::beamLine(unsigned firstIndex)
{ {
elPtr= getComputingBlock()->getElement(k); elPtr= getComputingBlock()->getElement(k);
string eType= elPtr->getGenericName(); //string eType= elPtr->getGenericName();
string label= elPtr->getLabel(); string label= elPtr->getLabel();
if(find(lElts.begin(),lElts.end(),label) == lElts.end()) { if (find(lElts.begin(),lElts.end(),label) == lElts.end()) {
vector<psvs> v= elPtr->parametersToSoftware(); vector<psvs> v= elPtr->parametersToSoftware();
os1 << elementsData(v); os1 << elementsData( v );
lElts.push_back(label); lElts.push_back( label );
} }
if(k < nElts-1) os2 << label << ","; if (k < nElts-1) os2 << label << ",";
else os2 << label << ");" << endl; else os2 << label << ");" << endl;
} }
...@@ -1230,10 +1215,10 @@ void softwareMadx::TTwiss(json& j) ...@@ -1230,10 +1215,10 @@ void softwareMadx::TTwiss(json& j)
dataManager_->consoleMessage("softwareMadx::readOpticalParameters: ERROR when opening " + name + "\n"); dataManager_->consoleMessage("softwareMadx::readOpticalParameters: ERROR when opening " + name + "\n");
return; return;
} }
const smap& vmap= getTableContents( ifs ); const smap& vmap= getTableContents( ifs );
ifs.close(); ifs.close();
if( vmap.empty() ) { if( vmap.empty() ) {
dataManager_->consoleMessage("softwareMadx::readOpticalParameters: ERROR when reading" + name + "\n"); dataManager_->consoleMessage("softwareMadx::readOpticalParameters: ERROR when reading" + name + "\n");
return; return;
...@@ -1314,9 +1299,8 @@ void softwareMadx::TSigma(json& j) ...@@ -1314,9 +1299,8 @@ void softwareMadx::TSigma(json& j)
if ( buf.find("*") != string::npos ) break; if ( buf.find("*") != string::npos ) break;
} }
getline(ifs,buf); // skip formats getline(ifs,buf); // skip formats
while (ifs >> buf) // s while (ifs >> buf)
{ {
ifs >> buf;
vmap[ "sigx" ].push_back( buf ); vmap[ "sigx" ].push_back( buf );
ifs >> buf; ifs >> buf;
vmap[ "hinvx" ].push_back( buf ); vmap[ "hinvx" ].push_back( buf );
...@@ -1362,9 +1346,8 @@ void softwareMadx::TChrom(json& j) ...@@ -1362,9 +1346,8 @@ void softwareMadx::TChrom(json& j)
if ( buf.find("*") != string::npos ) break; if ( buf.find("*") != string::npos ) break;
} }
getline(ifs,buf); // skip formats getline(ifs,buf); // skip formats
while (ifs >> buf) // s while (ifs >> buf)
{ {
ifs >> buf;
vmap[ "wx" ].push_back( buf ); vmap[ "wx" ].push_back( buf );
ifs >> buf; ifs >> buf;
vmap[ "wy" ].push_back( buf ); vmap[ "wy" ].push_back( buf );
......
...@@ -24,8 +24,10 @@ void pspaplot::settings(string str) ...@@ -24,8 +24,10 @@ void pspaplot::settings(string str)
//cout << "item choisi => " << item << endl; //cout << "item choisi => " << item << endl;
json obj; json obj;
if (item == "phase-space") { if (item == "ellipse$START") {
obj= phaseSpace( j ); obj= phaseSpace( j,"start" );
} else if (item == "ellipse$END") {
obj= phaseSpace( j,"end" );
} else { } else {
obj= beamParameters( j ); obj= beamParameters( j );
} }
...@@ -37,7 +39,7 @@ void pspaplot::settings(string str) ...@@ -37,7 +39,7 @@ void pspaplot::settings(string str)
outfile.close(); outfile.close();
} }
json pspaplot::phaseSpace(json j) json pspaplot::phaseSpace(json j, string opt)
{ {
auto ids= j.find( "plotSimulation" ); auto ids= j.find( "plotSimulation" );
//std::cout << std::boolalpha; //std::cout << std::boolalpha;
...@@ -57,21 +59,25 @@ json pspaplot::phaseSpace(json j) ...@@ -57,21 +59,25 @@ json pspaplot::phaseSpace(json j)
ifs >> obj; ifs >> obj;
ifs.close(); ifs.close();
// displays the horizontal (xx') ellipse at the end of the beam line // displays the horizontal (xx') ellipse at the beginning/end of the beam line
vector<double> Xaxis; vector<double> Xaxis, Yaxis;
vector<double> Yaxis; double zcoor, xrms, alfa, beta;
if (opt == "start") {
double zcoor= obj.find( "zcoor" )->front(); zcoor= obj.find( "zcoor" )->front();
double xrms= obj.find("xsigma")->front(); xrms= obj.find("xsigma")->front();
double alfa= obj.find("xalfa")->front(); alfa= obj.find("xalfa")->front();
double beta= obj.find("xbeta")->front(); beta= obj.find("xbeta")->front();
} else if (opt == "end") {
//double zcoor= obj.find( "zcoor" )->back(); zcoor= obj.find( "zcoor" )->back();
//double xrms= obj.find("xsigma")->back(); xrms= obj.find("xsigma")->back();
//double alfa= obj.find("xalfa")->back(); alfa= obj.find("xalfa")->back();
//double beta= obj.find("xbeta")->back(); beta= obj.find("xbeta")->back();
} else {
cout << "option error in the ellipse plot => " << opt << endl;
return json({});
}
int n= 50; int n= 50;
double h= 2.0*xrms/n; double h= 2.0*xrms/n;
for (int k = 0; k < n; ++k) for (int k = 0; k < n; ++k)
...@@ -260,6 +266,11 @@ void pspaplot::submenu2(string key,json obj,map<string,vector<double> >& Ymaps) ...@@ -260,6 +266,11 @@ void pspaplot::submenu2(string key,json obj,map<string,vector<double> >& Ymaps)
if (key == "beta-functions") { if (key == "beta-functions") {
menu("xbeta",obj,Ymaps); menu("xbeta",obj,Ymaps);
menu("ybeta",obj,Ymaps); menu("ybeta",obj,Ymaps);
}
if (key == "beta&xeta") {
menu("xbeta",obj,Ymaps);
menu("ybeta",obj,Ymaps);
menu("xeta",obj,Ymaps); menu("xeta",obj,Ymaps);
} }
...@@ -405,10 +416,12 @@ json pspaplot::lineplots(vector<double> Xaxis,map<string,vector<double> > Ymaps) ...@@ -405,10 +416,12 @@ json pspaplot::lineplots(vector<double> Xaxis,map<string,vector<double> > Ymaps)
leftLabel += ",σy"; leftLabel += ",σy";
} }
if (Ymaps.find("ybeta") != Ymaps.end()) { if (Ymaps.find("ybeta") != Ymaps.end()) {
x["label"]= "βy [m]"; if (Ymaps.find("xeta") != Ymaps.end()) {
it= Ymaps.find("ybeta"); x["label"]= "βy [m]";
addDataset= true; it= Ymaps.find("ybeta");
leftLabel += ",βy"; addDataset= true;
leftLabel += ",βy";
}
} }
if (Ymaps.find("yalfa") != Ymaps.end()) { if (Ymaps.find("yalfa") != Ymaps.end()) {
x["label"]= "αy [m]"; x["label"]= "αy [m]";
...@@ -447,11 +460,19 @@ json pspaplot::lineplots(vector<double> Xaxis,map<string,vector<double> > Ymaps) ...@@ -447,11 +460,19 @@ json pspaplot::lineplots(vector<double> Xaxis,map<string,vector<double> > Ymaps)