Commit 27ba7232 authored by TOUZE Francois's avatar TOUZE Francois
Browse files

development of a parser for BETA

parent 8d30412b
......@@ -10,6 +10,7 @@
#include "AML/AMLReader.hpp"
#include "Translate/AMLParser.hpp"
#include "Translate/MADXParser.hpp"
#include "BETAParser.h"
#include "INIReader.h"
namespace BU = BasicUtilities;
......@@ -225,77 +226,98 @@ bool dataManager::parseXFile( string fileName )
UAPNode *nd3= nd2->getChildByName( "machine" );
UAPAttribute *a1= nd3->getChildByName( "sector" )->getAttribute( "ref" );
// recovers the working beam line ( lower case )
string nameOfMachine= a1->getValue();
if(debug > 0) cout << "machine = " << nameOfMachine << endl;
clearSectors(); // clear the BeamLine
// from the <beam> node creates the sector::BUNCH
UAPNode *nd4= nd2->getChildByName( "beam" );
if( nd4 ) {
sectors_.push_back( new sector(this,"beam") );
UAPNode *nd5;
// GeV => in MeV
nd5= nd4->getChildByName( "total_energy" );
if (nd5) {
UAPAttribute *a= nd5->getAttribute( "design" );
double EGev= BasicUtilities::string_to_double( a->getValue() );
string sMev= BasicUtilities::double_to_string( 1000.*EGev );
nd5->addAttribute("design",sMev);
}
// GeV => in MeV
nd5= nd4->getChildByName( "pc" );
if (nd5) {
UAPAttribute *a= nd5->getAttribute( "design" );
double EGev= BasicUtilities::string_to_double( a->getValue() );
string sMev= BasicUtilities::double_to_string( 1000.*EGev );
nd5->addAttribute("design",sMev);
}
sectors_.push_back( new sector(this,"BUNCH") );
sectors_.back()->readAMLTree( nd4 );
} else {
cout << "dataManager::parseXFile:: no beam in the machine?...\n";
return false;
}
// parmi les <sector> recupere celui qui decrit la <machine>
// among <sector> nodes recovers the one which describes the beam line
NodeVec lst1= nd2->getChildrenByName( "sector" );
NodeVecIter where= AMLtools::find(lst1.begin(),lst1.end(),nameOfMachine);
// creates the sector::nameOfMachine with its elements
NodeVec lst2= nd2->getChildrenByName( "element" );
machineElements(*where,lst1,lst2);
if(debug > 0) cout << (*where)->toStringTree() << endl;
// la balise <machine>
// creates the machine made up sector::BUNCH and sector::nameOfMachine
if ( !machineCourante_ ) machineCourante_ = new machine(this);
// sector::BUNCH is the first of the list
machineCourante_->addSector( sectors_.at(0) );
// convert to upper case
mixedTools::toupper( nameOfMachine );
// among the others sectors find the one whose name is nameOfMachine
for (unsigned k = 0; k < sectors_.size(); k++)
{
// convert to lower case
string s= mixedTools::str_to_lower( sectors_.at(k)->getLabel() );
if( s.find( "beam" ) != std::string::npos)
machineCourante_->addSector( sectors_.at(k) );
if( s.find( nameOfMachine ) != std::string::npos)
string s= sectors_.at( k )->getLabel();
if (s.find( nameOfMachine ) != std::string::npos) {
machineCourante_->addSector( sectors_.at(k) );
}
}
return true;
/**
UAPNode* ma= new UAPNode("");
ma->addChild("machine")->addAttribute("name","dummy");
ma->addChild("machineSectors");
UAPNode* exma= ma->addChild( "expandedMachine" );
exma->addAttribute("name","dummy");
UAPNode* blk= exma->addChild( "mcomputingBlocks" );
UAPNode* nc= blk->addChild( "computingBlock" );
nc->addChild( "software" )->addAttribute("name","unknown");
UAPNode* nex= nc->addChild( "blocExtension" );
nex->addAttribute("rankOfFirstElement","0");
nex->addAttribute("numberOfelements","0");
nc->addChild( "action" )->addAttribute("name","");
machineCourante_->FileAMLInput(ma);
*/
}
void dataManager::machineElements(UAPNode* node,NodeVec& sList,NodeVec& eList,bool reflx)
void dataManager::machineElements(UAPNode *nde,NodeVec& sList,NodeVec& eList,bool reflx)
{
if(debug > 0) cout << "dataManager::machineElements()\n";
NodeVec v;
NodeVec& children= nde->getChildren();
NodeVecIter it;
for (it = node->getChildren().begin(); it != node->getChildren().end(); ++it)
for (it = children.begin(); it != children.end(); ++it)
{
v.push_back(*it);
if( (*it)->getName() == "sector" ) {
UAPAttribute* a2= (*it)->getAttribute("ref");
UAPAttribute* a3= (*it)->getAttribute("reflection");
if ((*it)->getName() == "sector") {
UAPAttribute *a2= (*it)->getAttribute("ref");
UAPAttribute *a3= (*it)->getAttribute("reflection");
NodeVecIter kt;
kt= AMLtools::find(sList.begin(),sList.end(),a2->getValue());
if( a3 ) machineElements(*kt,sList,eList,true);
if ( a3 ) machineElements(*kt,sList,eList,true);
else machineElements(*kt,sList,eList,false);
}
}
string s= node->getAttributeString("name");
string s= nde->getAttributeString("name");
string upper= BasicUtilities::str_to_upper( s );
if( reflx ) {
upper.insert(0,"-");
......@@ -305,6 +327,7 @@ void dataManager::machineElements(UAPNode* node,NodeVec& sList,NodeVec& eList,bo
if(debug > 0) {
cout<<"dataManager::machineElements(): new sector "<< upper << endl;
}
sector *sor= new sector(this,upper);
sectors_.push_back( sor );
sor->sectorTree(v,eList);
......@@ -505,6 +528,8 @@ json dataManager::toJSONFormat()
void dataManager::toJSON(json& j)
{
if(debug > 0) cout << "\n\ndataManager::toJSON()\n";
if ( !machineCourante_ ) {
cout << "dataManager::toJSON:: no machine ?...\n";
return;
......@@ -515,6 +540,7 @@ void dataManager::toJSON(json& j)
{
string s1= "sector-" + mixedTools::alea();
string s2= sors.at(k)->getLabel();
mixedTools::toupper( s2 );
json x;
x["id"] = s1;
......@@ -524,6 +550,8 @@ void dataManager::toJSON(json& j)
j["machine"].push_back( x );
}
// cout << j.dump(4) << endl;
cout << "dataManager::toJSON nameOfCase => " << nameOfCase_ << endl;
j["name"]= nameOfCase_;
......@@ -665,3 +693,57 @@ void dataManager::settingPlot()
json obj= softw->readOpticalParameters();
}
}
bool dataManager::parseBETAFile( string fileName )
{
if(debug > 0) cout << "dataManager::parseBETAFile()\n";
BETAParser reader;
UAPNode *upRoot= NULL;
fileName= userDir_ + fileName;
upRoot= reader.FileToAMLRep( fileName );
if ( !upRoot ) {
cout << "dataManager::parseBETAFile:: failure to parse " << fileName << "\n";
return false;
}
if(debug > 0) cout << upRoot->toStringTree() << endl;
// From ΒΕΤA sections there are two PSPA::sectors defined
clearSectors();
// The first one contains the element BEAM
UAPNode *nd1= upRoot->getChildByName( "beam" );
if( nd1 ) {
sectors_.push_back( new sector(this,"BUNCH") );
sectors_.back()->readAMLTree( nd1 );
} else {
cout << "dataManager::parseBETAFile:: no beam in the machine\n";
return false;
}
// The second one contains the elements in the STRUCTURE
NodeVec lst1= upRoot->getChildrenByName( "sector" );
NodeVec lst2= upRoot->getChildrenByName( "element" );
machineElements(lst1[0],lst1,lst2);
if (!machineCourante_) machineCourante_= new machine( this );
machineCourante_->addSector( sectors_.at(0) );
machineCourante_->addSector( sectors_.at(1) );
if(debug > 0) {
for (unsigned k = 0; k < sectors_.size(); k++)
{
cout << sectors_.at(k)->getLabel() << " =>\n";
vector<abstractElement*>& elts= sectors_.at(k)->getElements();
for (unsigned i = 0; i < elts.size(); i++)
{
cout << elts.at(i)->getLabel() << endl;
}
}
}
return true;
}
......@@ -34,6 +34,7 @@ class dataManager
void writeToAMLFile( string );
bool parseAMLFile( string );
bool parseXFile( string );
bool parseBETAFile( string );
void setSecteurCourant(string);
sector* getSecteurCourant() const;
......
......@@ -10,7 +10,7 @@
#include "softwareUsersprogram.h"
#include "softwareTest.h"
#include "softwareMadx.h" //ok
#include "softwareBeta.h"
#include "softwareBeta.h" //ok
#include "softwareElegant.h" //ok
#include "softwareGenerator.h" //ok
#include "softwareAstra.h" //ok
......@@ -89,7 +89,7 @@ abstractSoftware* expandedMachine::getSoftwarePointer(std::string logiciel,compu
if (logiciel == "astra") {
prog= new softwareAstra(cpblk,dataManager_ );
} else if (logiciel == "beta") {
//prog= new softwareBeta(cpblk,dataManager_);
prog= new softwareBeta(cpblk,dataManager_);
} else if (logiciel == "elegant") {
prog= new softwareElegant(cpblk,dataManager_);
} else if (logiciel == "generator") {
......@@ -692,6 +692,9 @@ void expandedMachine::toJSON(json& j)
{
if(debug > 0) std::cout << "expandedMachine::toJSON()\n";
std::cout << "expandedMachine::toJSON()\n";
vector<string> color {"blue-table","green-table","purple-table","fuschia-table","red-table","salmon-table","yellow-table","deepblue-table","ocean-table","menthol-table","pink-table"};
json z;
......
......@@ -115,7 +115,7 @@ bool machine::FileAMLInput(UAPNode *nde)
if (!expMachine_->FileAMLInput( nd1 )) return false;
} else {
expMachine_= NULL;
return false;
return true;
}
vector<computingBlock*>& blks= expMachine_->getComputingBlocks();
......
......@@ -5,6 +5,8 @@
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
......@@ -177,6 +179,20 @@ class mixedTools
return status;
}
static void toupper(std::string& str)
{
std::for_each(str.begin(), str.end(), [](char & c) {
c = ::toupper(c);
});
}
static void tolower(std::string& str)
{
std::for_each(str.begin(), str.end(), [](char & c) {
c = ::tolower(c);
});
}
static std::string str_to_upper(const std::string& str)
{
return BasicUtilities::str_to_upper( str );
......
......@@ -434,7 +434,7 @@ void particleBeam::buildMomentRepresentation()
rij_.impression();
}
P0Transport_ = 1.0e-3*EREST_MeV*PREFMeV_sur_c; // Gev
P0Transport_= 1.0e-3*EREST_MeV*PREFMeV_sur_c; // Gev
momentRepresentationOk_ = true;
}
......
......@@ -414,8 +414,8 @@ void sector::readAMLTree(UAPNode *bm)
if(debug > 0) cout << "sector::readAMLTree()\n";
expandedElements_.clear();
sectorComponent* nc = NULL;
nc = createElementAfterComponent("beam",nc);
sectorComponent *nc = NULL;
nc= createElementAfterComponent("beam",nc);
if(nc != NULL) nc->AMLRepToPSPA("beam",bm);
}
......@@ -446,12 +446,12 @@ void sector::AMLRepToPSPA(string key,UAPNode *jnode)
return;
}
void sector::sectorTree(NodeVec& myvector,NodeVec& eList)
void sector::sectorTree(NodeVec& vnodes,NodeVec& eList)
{
if(debug > 0) cout << "sector::sectorTree()\n";
expandedElements_.clear();
sectorComponent* nc = NULL;
sectorComponent *nc = NULL;
StrMap map_key_to_pspa;
map_key_to_pspa["bend"]= "bend";
......@@ -467,64 +467,61 @@ void sector::sectorTree(NodeVec& myvector,NodeVec& eList)
map_key_to_pspa["sextupole"]= "spole";
if(debug > 0) cout << "open sector [" << getLabel() << "]\n";
for(NodeVecIter jt = myvector.begin(); jt != myvector.end(); jt++)
NodeVecIter jt;
for (jt = vnodes.begin(); jt != vnodes.end(); jt++)
{
if( (*jt)->getName() == "sector" ) {
if ((*jt)->getName() == "sector") {
nc = createElementAfterComponent("composanteSecteur",nc);
if(nc != NULL) nc->AMLRepToPSPA("composanteSecteur",*jt);
}
nc= createElementAfterComponent("composanteSecteur",nc);
if (nc != NULL) nc->AMLRepToPSPA("composanteSecteur",*jt);
if( (*jt)->getName() == "element" ) {
} else if ((*jt)->getName() == "element") {
UAPAttribute* a2= (*jt)->getAttribute("ref");
UAPAttribute *a2= (*jt)->getAttribute("ref");
NodeVecIter w;
w= AMLtools::find(eList.begin(),eList.end(),a2->getValue());
int nfois = 1;
UAPAttribute* a3= (*jt)->getAttribute("repeat");
if(a3) a3->getInt(nfois);
if( (*w)->getName() == "lattice" ) {
nc = createElementAfterComponent("beam",nc);
if(nc != NULL) nc->AMLRepToPSPA("beam",*w);
} else { // (*w)->getName() == "element"
NodeVec& v3 = (*w)->getChildren();
// kind of element
NodeVecIter kt = v3.begin();
string key = (*kt)->getName();
if( key == "length" ) {
for(int k = 0; k < nfois; k++)
NodeVec& v3= (*w)->getChildren();
switch( v3.size() )
{
// drift= element with no key from map_key_to_pspa
nc = createElementAfterComponent("drift",nc);
if(nc != NULL) nc->AMLRepToPSPA("drift",*kt);
}
} else {
for (NodeVecIter it = kt; it!= v3.end(); it++)
case 0:
// <marker> does not have any children
nc= createElementAfterComponent("marker",nc);
if(nc != NULL) nc->AMLRepToPSPA("marker",*w);
break;
case 1:
// <drift> does not have any key from map_key_to_pspa
nc= createElementAfterComponent("drift",nc);
if(nc != NULL) nc->AMLRepToPSPA("drift",v3[0]);
break;
default:
for (NodeVecIter it = v3.begin(); it!= v3.end(); it++)
{
key = (*it)->getName();
if(map_key_to_pspa.find( key ) != map_key_to_pspa.end()) {
nc=createElementAfterComponent(map_key_to_pspa[key],nc);
if(nc != NULL)
nc->AMLRepToPSPA(map_key_to_pspa[key],*it);
StrMapIter kt= map_key_to_pspa.find( (*it)->getName() );
if (kt != map_key_to_pspa.end()) {
nc= createElementAfterComponent(kt->second,nc);
if (nc != NULL) nc->AMLRepToPSPA(kt->second,*it);
}
}
}
} //it
}
}
} //end element
} //jt
}
void sector::toJSON(json& j)
{
if(debug > 0) cout << "sector::toJSON()\n";
vector<abstractElement*>& elts= this->getElements();
for (unsigned k = 0; k < elts.size(); k++)
{
if(debug > 0) cout << " elt:: " << elts.at(k)->getComponentName() << " => " << elts.at(k)->getGenericName() << endl;
json x;
elts.at(k)->toJSON( x );
j["elements"].push_back( x );
}
}
......
......@@ -3,45 +3,46 @@
#include <map>
#include <string>
#include "abstractSoftware.h"
class softwareBeta : public abstractSoftware
{
std::map<string,string> bunch_;
void beamData(const vector<psvs>&);
void fromParameters(const vector<psvs>&);
void fromDistribution(const vector<psvs>&);
void beamData(particleBeam*);
void bunch_setup(const std::vector<smap>&);
string elementsData(unsigned);
string inputFormat(const vector<psvs>&,int&) const;
string execCommands();
string beamLine(unsigned);
string elementsData(const vector<psvs>&,int&);
string twissStatement(const std::vector<smap>&);
void initialValues(const smap&);
string executeCommand();
string beam_matrix();
string matchStatement(const std::vector<smap>&);
string checkTypeOfConstraint(string);
string parametersCalculation();
string graphicOption();
string nameOpticalFunction(string);
void readFileContents(ifstream&,vector<double>&,vector<double>&);
//void beamMatrix(double ws[5]);
void parametersUpdate(string);
string batchCommands();
string graphicStep();
void FinalValues();
smap getTableContents(ifstream&);
void setBeamParameters(double ws[5]);
map< string,string > bunch_;
virtual string plotting( int ) {return string();}
public :
softwareBeta();
softwareBeta(string,computingBlock*,dataManager*);
virtual ~softwareBeta();
softwareBeta(computingBlock*,dataManager*);
virtual ~softwareBeta() {;}
virtual bool createInputFile(particleBeam*,string);
virtual bool execute(string);
virtual bool buildBeamAfterElements(string);
virtual bool readOpticalParameters(int,int,vector<double>&,map<string,vector<double> >&);
virtual bool createInputFile(particleBeam*);
virtual bool execute();
virtual bool buildBeamAfterElements();
virtual json readOpticalParameters();
virtual string plotting( int ) {return string();}
inline string getColor() {return "#33FFOO";}
};
#endif
......@@ -587,11 +587,11 @@ string softwareElegant::elementsData(const vector<psvs>& v)
double ln= atof(v.at(1).second.at(0).c_str()); // m
double volt= atof(v.at(1).second.at(1).c_str()); // MV
volt *= 1000.; // V
volt *= 1.e+06; // V
double phase= atof(v.at(1).second.at(2).c_str()); // degrees
string s= v.at(1).second.at(3); // frequency (MHz)
double freq= 500000000; // RF frequency
double freq= 500.e+06; // RF frequency
if( s.find("none") == std::string::npos ) {
freq= atof( s.c_str() )*1.e+06; // Hz
} else {
......
......@@ -58,7 +58,7 @@ bool softwareMadx::createInputFile(particleBeam *beamBefore)
dataManager_->consoleMessage("WARNING softwareMadx::createInputFile : beam data set to the defaults\n");
os << "beam, particle= ELECTRON";
Energy_= "1.0";
Energy_= "1.0"; // 1GeV
} else {
......@@ -225,8 +225,6 @@ string softwareMadx::tunesStatement(const std::vector<smap>& v)
return os.str();
}
string softwareMadx::twissStatement(const std::vector<smap>& v)
{
// cout << "softwareMadx::twissStatement()\n";
......@@ -564,6 +562,7 @@ string softwareMadx::beamData(const vector<psvs>& v)
ostringstream os;
os << "beam, particle= " << v.at(1).second.at(0);
string str= v.at(1).first;
if( str.find("particleDistribution") == std::string::npos ) {
os << fromParameters( v );
......@@ -578,33 +577,36 @@ string softwareMadx::fromParameters(const vector<psvs>& v)
if(debug > 0) cout << "softwareMadx::fromParameters()\n";
ostringstream os;
//string Energy;
string str= v.at(1).second.at(1);
if( str.find("none") == std::string::npos ) {
if (str.find("none") == std::string::npos) {
double EGev= atof(str.c_str())*1.e-03; // GeV
str= mixedTools::doubleToString( EGev );
os << ", energy= " << str;
Energy_= str;
} else {
str= v.at(1).second.at(2);
if( str.find("none") == std::string::npos ) {
if (str.find("none") == std::string::npos) {
double EGev= atof(str.c_str())*1.e-03; // GeV
str= mixedTools::doubleToString( EGev );
os << ", pc= " << str;
Energy_= str;
}
}
str= v.at(1).second.at(3);
if( str.find("none") == std::string::npos )
os << ", ex= " << str;
if (str.find("none") == std::string::npos) os << ", ex= " << str;
str= v.at(1).second.at(4);
if( str.find("none") == std::string::npos )
os << ", ey= " << str;
if (str.find("none") == std::string::npos) os << ", ey= " << str;
str= v.at(1).second.at(5);
if( str.find("none") == std::string::npos )
os << ", et= " << str;
if (str.find("none") == std::string::npos) os << ", et= " << str;
str= v.at(1).second.at(6);
if( str.find("none") == std::string::npos )
os << ", sigt= " << str;
if (str.find("none") == std::string::npos) os << ", sigt= " << str;
<