Commit 8d30412b authored by TOUZE Francois's avatar TOUZE Francois
Browse files

development of a parser for BETA

parent 7c3a0374
#include <iostream>
#include "BETAParser.h"
using namespace std;
namespace BU = BasicUtilities;
BETAParser::BETAParser() :
nodeList_(),
HEdges_()
{}
BETAParser::~BETAParser()
{}
UAPNode* BETAParser::FileToAMLRep (const string& fileName)
{
std::ifstream ifs;
ifs.open(fileName.c_str(),ios::in);
if (!ifs) {
cout << "BETAParser::FileToAMLRep:: ERROR opening " << fileName << endl;
return NULL;
}
string buf;
// find the TITLE
do {
getline(ifs,buf);
} while ( buf.find("* TITRE") == string::npos && !ifs.eof() );
getline(ifs,buf);
cout << "found TITRE => " << buf << endl;
string nameOfMachine= buf;
// find the ELEMENTS
do {
getline(ifs,buf);
} while ( buf.find("* LIST OF ELEMENTS") == string::npos && !ifs.eof() );
ListOfElements(ifs);
// find the STRUCTURE
do {
getline(ifs,buf);
} while ( buf.find("* STRUCTURE") == string::npos && !ifs.eof() );
getline(ifs,buf);
int num= BasicUtilities::string_to_int( buf );
UAPNode *n3= new UAPNode("sector");
n3->addAttribute("name","STRUCTURE");
for (int k = 0; k < num; ++k)
{
ifs >> buf;
string Label= BasicUtilities::str_to_lower(buf);
if (find(HEdges_.begin(),HEdges_.end(),Label) == HEdges_.end()) {
n3->addChild("element")->addAttribute("ref",Label);
}
}
// set the MACHINE
UAPNode *n4= new UAPNode("machine");
n4->addAttribute("name",nameOfMachine);
n4->addChild( "sector" )->addAttribute("ref","STRUCTURE");
// set the BEAM
UAPNode *n5= new UAPNode("beam");
n5->addAttribute("name","beam");
// find the PARTICLE TYPE
do {
getline(ifs,buf);
} while ( buf.find("* PARTICLE") == string::npos && !ifs.eof() );
getline(ifs,buf);
string ptype;
if (buf.find("E") != string::npos) {
ptype= "ELECTRON";
} else if (buf.find("P") != string::npos) {
ptype= "PROTON";
} else if (buf.find("D") != string::npos) {
ptype= "DEUTRON";
} else if (buf.find("M") != string::npos) {
ptype= "MUON";
} else {
cout << "OTHER => the next line contains the mass number ans the charge number of the particle\n";
ptype= "OTHER";
}
n5->addChild("particle")->addAttribute("type",ptype);
// find ENERGIE CINETIQUE (MeV)
do {
getline(ifs,buf);
} while ( buf.find("* ENERGIE") == string::npos && !ifs.eof() );
ifs >> buf;
n5->addChild("total_energy")->addAttribute("design",buf);
ifs >> buf;
n5->addChild("mass")->addAttribute("design",buf);
// find EMITTANCE
do {
getline(ifs,buf);
} while ( buf.find("* EMITTANCE") == string::npos && !ifs.eof() );
ifs >> buf;
n5->addChild("emittance_a")->addAttribute("design",buf);
ifs >> buf;
n5->addChild("emittance_b")->addAttribute("design",buf);
ifs >> buf;
n5->addChild("emittance_z")->addAttribute("design",buf);
// Creates a UAPNode named "machine"
UAPNode *nde= NULL;
nde= new UAPNode("laboratory");
nde->addAttribute("version","BETA");
nde->addChild( n5 );
NodeVecIter it;
for (it = nodeList_.begin(); it != nodeList_.end(); it++)
{
nde->addChild( *it );
}
nde->addChild( n3 );
nde->addChild( n4 );
cout << "BETAPARSER:: " << nde->toStringTree() << endl;
return nde;
}
vector<string> BETAParser::split(std::ifstream &ifs)
{
string buf;
getline(ifs,buf);
//cout << "split:: buf => " << buf << endl;
std::istringstream iss(buf);
std::vector<std::string> tokens(std::istream_iterator<std::string>{iss},std::istream_iterator<std::string>());
return tokens;
}
void BETAParser::ListOfElements(std::ifstream &ifs)
{
string buf;
// collect the elements
if (!nodeList_.empty()) nodeList_.clear();
// collect the dipole edge elements
if (!HEdges_.empty()) HEdges_.clear();
// Number of defined elements
getline(ifs,buf);
int num= BasicUtilities::string_to_int( buf );
// The full list of the elements
int k = 0;
do {
std::vector<std::string> tokens= split( ifs );
// converts TYPE to upper case
string eType= BasicUtilities::str_to_upper(tokens[1]);
// converts LABEL to lower case
string Label= BasicUtilities::str_to_lower(tokens[0]);
UAPNode *n1= new UAPNode( "element" );
nodeList_.push_back( n1 );
if (eType == "SD") {
n1->addAttribute("name",Label);
n1->addChild("length")->addAttribute("design",tokens[2]);
}
if (eType == "QP") {
n1->addAttribute("name",Label);
n1->addChild("length")->addAttribute("design",tokens[2]);
UAPNode *n2= n1->addChild("quadrupole");
n2->addChild("k")->addAttribute("design",tokens[3]);
}
if (eType == "OB") {
n1->addAttribute("name",Label);
}
if (eType == "SX") {
n1->addAttribute("name",Label);
n1->addChild("length")->addAttribute("design",tokens[2]);
UAPNode *n2= n1->addChild("sextupole");
double s2= BasicUtilities::string_to_double( tokens[3] );
// in BETA the sextupole strength S = 0.5*k2 m^-3
string k2= BasicUtilities::double_to_string( 2.*s2 );
n2->addChild("k")->addAttribute("design",k2);
}
if (eType == "CO") { // tokens#5
k++;
// DIPOLE
std::vector<std::string> di= split( ifs ); // di#4
Label= BasicUtilities::str_to_lower(di[0]);
n1->addAttribute("name",Label); // its LABEL
// ELEMENT LENGTH
double alfa= BasicUtilities::string_to_double( di[2] );
double rho = BasicUtilities::string_to_double( di[3] );
string length= BasicUtilities::double_to_string( alfa*rho );
n1->addChild("length")->addAttribute("design",length);
UAPNode *n2= n1->addChild("bend");
// BENDING RADIUS 1/radius= alfa/length
//string bending= BasicUtilities::double_to_string(1./rho);
string bending= di[2] + "/" + length;
n2->addChild("g")->addAttribute("design",bending);
// ENTRANCE POLE EDGE
n2->addChild("e1")->addAttribute("design",tokens[2]);
n2->addChild("f_int1")->addAttribute("design",tokens[4]);
n2->addChild("h_gap1")->addAttribute("design","HGAP");
Label= BasicUtilities::str_to_lower(tokens[0]);
HEdges_.push_back(Label);
// EXIT POLE EDGE
k++;
std::vector<std::string> co= split( ifs ); // co#5
n2->addChild("e2")->addAttribute("design",co[2]);
n2->addChild("f_int2")->addAttribute("design",co[4]);
n2->addChild("h_gap2")->addAttribute("design","HGAP");
Label= BasicUtilities::str_to_lower(co[0]);
HEdges_.push_back(Label);
}
if (eType == "DI") {
n1->addAttribute("name",Label);
// ELEMENT LENGTH
double alfa= BasicUtilities::string_to_double( tokens[2] );
double rho = BasicUtilities::string_to_double( tokens[3] );
string length= BasicUtilities::double_to_string( alfa*rho );
//double length= atof(tokens[2].c_str())*radius;
n1->addChild("length")->addAttribute("design",length);
UAPNode *n2= n1->addChild("bend");
// BENDING RADIUS 1/radius
//string bending= BasicUtilities::double_to_string(1./rho);
string bending= tokens[2] + "/" + length;
n2->addChild("g")->addAttribute("design",bending);
}
if (eType == "SO") {
n1->addAttribute("name",Label);
n1->addChild("length")->addAttribute("design",tokens[2]);
UAPNode *n2= n1->addChild("solenoid");
double so= BasicUtilities::string_to_double( tokens[3] );
// in BETA the solenoid strength SO = 0.5*ks m^-1
string ks= BasicUtilities::double_to_string( 2.*so );
n2->addChild("k")->addAttribute("design",ks);
}
if (eType == "CA") {
}
if (eType == "KI") {
n1->addAttribute("name",Label);
n1->addChild("length")->addAttribute("design","0.0000000E+00");
UAPNode *n2= n1->addChild("kicker");
n2->addChild("x_kick")->addAttribute("design",tokens[2]);
n2->addChild("y_kick")->addAttribute("design",tokens[3]);
}
k++;
} while ( k < num);
}
#ifndef BETAPARSER_SEEN
#define BETAPARSER_SEEN
#include <iostream>
#include <string>
#include <vector>
#include "UAP/UAPNode.hpp"
class BETAParser
{
public:
BETAParser();
~BETAParser();
UAPNode* FileToAMLRep (const Str& fileName);
NodeVec& nodeList();
private:
void ListOfElements(std::ifstream&);
std::vector<std::string> split(std::ifstream&);
NodeVec nodeList_;
std::vector<std::string> HEdges_;
};
#endif
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment