41 void replaceAll(std::string& source,
const std::string& from,
const std::string& to)
43 std::string newString;
44 newString.reserve( source.length() );
46 std::string::size_type lastPos = 0;
47 std::string::size_type findPos;
49 while( std::string::npos != ( findPos = source.find( from, lastPos )))
51 newString.append( source, lastPos, findPos - lastPos );
53 lastPos = findPos + from.length();
57 newString += source.substr( lastPos );
59 source.swap( newString );
65 template void Parser::Add<ScorerMesh, FastList<ScorerMesh> >(
bool unique,
const std::string& className);
66 template void Parser::Add<CavityModel, FastList<CavityModel> >(
bool unique,
const std::string& className);
67 template void Parser::Add<BLMPlacement, FastList<BLMPlacement> >(
bool unique,
const std::string& className);
68 template void Parser::Add<Modulator, FastList<Modulator> >(
bool unique,
const std::string& className);
69 template void Parser::Add<SamplerPlacement, FastList<SamplerPlacement> >(
bool unique,
const std::string& className);
70 template void Parser::Add<Atom, FastList<Atom> >(
bool unique,
const std::string& className);
71 template void Parser::Add<Field, FastList<Field> >(
bool unique,
const std::string& className);
72 template void Parser::Add<Query, FastList<Query> >(
bool unique,
const std::string& className);
73 template void Parser::Add<Region, FastList<Region> >(
bool unique,
const std::string& className);
74 template void Parser::Add<Scorer, FastList<Scorer> >(
bool unique,
const std::string& className);
75 template void Parser::Add<Tunnel, FastList<Tunnel> >(
bool unique,
const std::string& className);
76 template void Parser::Add<Crystal, FastList<Crystal> >(
bool unique,
const std::string& className);
77 template void Parser::Add<Aperture, FastList<Aperture> >(
bool unique,
const std::string& className);
78 template void Parser::Add<Material, FastList<Material> >(
bool unique,
const std::string& className);
79 template void Parser::Add<NewColour, FastList<NewColour> >(
bool unique,
const std::string& className);
80 template void Parser::Add<PhysicsBiasing, FastList<PhysicsBiasing> >(
bool unique,
const std::string& className);
86 extern std::string yyfilename;
98 std::cerr <<
"Parser has not been initialized!" << std::endl;
108 std::cerr <<
"Warning parser was already initialized!" << std::endl;
129 std::cout <<
"gmad_parser> opening file" << std::endl;
134 std::string tilde(
"~");
135 std::string home(getpwuid(getuid())->pw_dir);
137 replaceAll(name,tilde,home);
139 FILE *f = fopen(name.c_str(),
"r");
143 std::cerr <<
"gmad_parser> Can't open input file " << name << std::endl;
147 yyfilename = std::string(name);
151 std::cout.precision(10);
161 std::cout <<
"gmad_parser> beginning to parse file" << std::endl;
169 std::cout <<
"gmad_parser> finished to parsing file" << std::endl;
173 std::cout <<
"gmad_parser> clearing temporary lists" << std::endl;
182 std::cout <<
"gmad_parser> finished" << std::endl;
190 const int reserved = 1;
203 add_var(
"pi", 4.0*std::atan(1),reserved);
204 add_var(
"twopi", 8.0*std::atan(1),reserved);
205 add_var(
"halfpi", 2.0*std::atan(1),reserved);
207 add_var(
"PeV",1e6, reserved);
208 add_var(
"TeV",1e3, reserved);
209 add_var(
"GeV",1.0 ,reserved);
210 add_var(
"MeV",1e-3,reserved);
211 add_var(
"keV",1e-6,reserved);
212 add_var(
"KeV",1e-6,reserved);
213 add_var(
"eV" ,1e-9,reserved);
215 add_var(
"PJ", 1e12, reserved);
216 add_var(
"GJ", 1e9, reserved);
217 add_var(
"MJ", 1e6, reserved);
218 add_var(
"kJ", 1e3, reserved);
219 add_var(
"J", 1, reserved);
220 add_var(
"mJ", 1e-3, reserved);
221 add_var(
"uJ", 1e-6, reserved);
222 add_var(
"nJ", 1e-9, reserved);
223 add_var(
"pJ", 1e-12, reserved);
225 add_var(
"mV",1e-3,reserved);
226 add_var(
"V" ,1.0, reserved);
227 add_var(
"kV",1e+3,reserved);
228 add_var(
"MV",1e+6,reserved);
229 add_var(
"GV",1e+9,reserved);
231 add_var(
"Tesla",1.0,reserved);
232 add_var(
"T", 1.0,reserved);
234 add_var(
"km" ,1e3 ,reserved);
235 add_var(
"m" ,1.0 ,reserved);
236 add_var(
"cm" ,1e-2,reserved);
237 add_var(
"mm" ,1e-3,reserved);
238 add_var(
"um" ,1e-6,reserved);
239 add_var(
"mum",1e-6,reserved);
240 add_var(
"nm" ,1e-9,reserved);
241 add_var(
"ang",1e-10,reserved);
242 add_var(
"pm" ,1e-12,reserved);
244 add_var(
"s" ,1.0 ,reserved);
245 add_var(
"ms" ,1.e-3,reserved);
246 add_var(
"us" ,1.e-6,reserved);
247 add_var(
"ns" ,1.e-9,reserved);
248 add_var(
"ps" ,1.e-12,reserved);
250 add_var(
"Hz" ,1.0, reserved);
251 add_var(
"kHz",1e+3, reserved);
252 add_var(
"MHz",1e+6, reserved);
253 add_var(
"GHz",1e+9, reserved);
254 add_var(
"THz",1e+12,reserved);
256 add_var(
"rad" ,1.0, reserved);
257 add_var(
"mrad",1e-3,reserved);
258 add_var(
"urad",1e-6,reserved);
259 add_var(
"nrad",1e-9,reserved);
261 add_var(
"degrees",std::atan(1)/45,reserved);
263 add_var(
"clight",2.99792458e+8,reserved);
270 std::cout <<
"parsing complete..." << std::endl;
302 const std::string& start,
303 const std::string& end)
309 const std::string& name,
310 const std::string& start,
311 const std::string& end)
314 if(line.
type != ElementType::_LINE && line.
type != ElementType::_REV_LINE )
316 std::cerr <<
"Error with use command: \"" << name <<
"\" is not a line" << std::endl;
333 std::cout <<
"expanding line " << name <<
", range = " << start << end << std::endl;
339 std::list<Element>::iterator sit = line.
lst->begin();
340 std::list<Element>::iterator eit = line.
lst->end();
345 case ElementType::_LINE:
346 {target.
insert(target.
end(),sit,eit);
break;}
347 case ElementType::_REV_LINE:
348 {target.
insert(target.
end(),line.
lst->rbegin(),line.
lst->rend());
break;}
350 {target.
insert(target.
end(),sit,eit);
break;}
353 bool is_expanded =
false;
361 std::list<Element>::iterator it = ++target.
begin();
362 for (; it!=target.
end(); ++it)
367 std::cout << element.name <<
" , " << type << std::endl;
370 if (type != ElementType::_LINE && type != ElementType::_REV_LINE)
374 std::list<Element>::const_iterator tmpit =
element_list.find(element.name);
375 std::list<Element>::const_iterator iterEnd =
element_list.end();
376 if ( (tmpit != iterEnd) && ( (*tmpit).lst !=
nullptr) )
380 std::cout <<
"inserting sequence for " << element.name <<
" - " << list.name <<
" ...";
382 if (type == ElementType::_LINE)
384 else if (type == ElementType::_REV_LINE)
387 std::list<Element> tmpList;
388 tmpList.insert(tmpList.end(),list.
lst->begin(),list.
lst->end());
389 for (std::list<Element>::iterator itLineInverter = tmpList.begin();
390 itLineInverter != tmpList.end(); ++itLineInverter)
392 if ( (*itLineInverter).type == ElementType::_LINE)
393 {(*itLineInverter).type = ElementType::_REV_LINE;}
394 else if ((*itLineInverter).type == ElementType::_REV_LINE)
395 {(*itLineInverter).type = ElementType::_LINE;}
397 target.
insert(it,tmpList.rbegin(),tmpList.rend());
400 std::cout <<
"inserted" << std::endl;
405 else if ( tmpit != iterEnd )
409 std::cout <<
"keeping element..." << element.name << std::endl;
415 std::cout <<
"done" << std::endl;
420 std::cerr <<
"Error : Expanding line \"" << name <<
"\" : element \"" << element.name
421 <<
"\" has not been defined! " << std::endl;
428 std::cerr <<
"Error : Line expansion of '" << name <<
"' seems to loop, " << std::endl
429 <<
"possible recursive line definition, quitting" << std::endl;
440 std::list<Element>::const_iterator startIt = target.
find(std::string(start));
442 if(startIt!=target.
end())
448 std::list<Element>::const_iterator endIt = target.
find(std::string(end));
450 if(endIt!=target.
end())
451 {target.
erase(++endIt,target.
end());}
456 std::list<Element>::iterator itTunnel =
element_list.find(
"tunnel");
466 {
return *(search->second);}
468 {std::cerr <<
"parser> no such sequence \"" << name <<
"\"" << std::endl; exit(1);}
474 const std::string& samplerType,
475 double samplerRadius,
485 if((*it).type == ElementType::_LINE || (*it).type == ElementType::_REV_LINE)
488 if (type != ElementType::_NONE && type != (*it).type)
491 (*it).setSamplerInfo(samplerType,(*it).name,samplerRadius,particleSetID);
494 else if (count == -1)
497 if (itPair.first == itPair.second)
499 std::string msg =
"parser> SetSampler> current beamline doesn't contain element \"" + name +
"\"";
500 yyerror2(msg.c_str());
502 for (
auto it = itPair.first; it != itPair.second; ++it)
505 auto elementIt = (it->second);
506 std::string samplerName = elementIt->name;
507 if ((*elementIt).type == ElementType::_MARKER)
511 while ((*elementIt).isSpecial())
521 std::cout <<
"parser> SetSampler> WARNING: no element before marker " << name <<
", no sampler added" << std::endl;
525 (*elementIt).setSamplerInfo(samplerType,samplerName,samplerRadius,particleSetID);
533 std::string msg =
"parser> SetSampler> current beamline doesn't contain element \"" + name +
"\" with number " + std::to_string(count);
534 yyerror2(msg.c_str());
537 std::string samplerName = (*it).name;
538 if ((*it).type == ElementType::_MARKER)
542 while ((*it).isSpecial())
547 std::cout <<
"parser> SetSampler> WARNING: no element before marker " << name <<
", no sampler added" << std::endl;
552 (*it).setSamplerInfo(samplerType,samplerName,samplerRadius,particleSetID);
558 if (!samplerPartIDListIn)
560 std::set<int> partIDs = std::set<int>(std::begin(*samplerPartIDListIn), std::end(*samplerPartIDListIn));
562 if (alreadyExists > 0)
563 {
return setToSamplerFilterID[partIDs];}
566 int particleSetID = (int) samplerFilterIDToSet.size();
567 samplerFilterIDToSet[particleSetID] = partIDs;
568 setToSamplerFilterID[partIDs] = particleSetID;
570 return particleSetID;
577 std::cout <<
"inserting sampler " << name;
579 {std::cout <<
"[" << count <<
"]";}
580 std::cout << std::endl;
583 set_sampler(name,count,type,samplerType,0,particleSetID);
588 std::list<Element>::iterator it =
element_list.find(element_name);
589 std::list<Element>::const_iterator iterEnd =
element_list.end();
593 std::cerr <<
"parser.h> Error: element (type) \"" << element_name
594 <<
"\" has not been defined." << std::endl;
605 std::cerr <<
"parser.h> Error: unknown element \"" << element_name <<
"\"." << std::endl;
613 const Element* result =
nullptr;
625 const Element* result =
nullptr;
644 std::cout <<
"matched sequence element, " << name;
646 {std::cout <<
" * " << number;}
647 std::cout << std::endl;
656 for (
int i = 0; i < number; i++)
661 for (
int i = 0; i < number; i++)
670 std::cout <<
"newinstance : VARIABLE -- " << elementName << std::endl;
675 type =
static_cast<int>(element.
type);
687void Parser::add_var(std::string name,
double value,
int is_reserved)
690 sp->
Set(value,is_reserved);
693bool Parser::InvalidSymbolName(
const std::string& s, std::string& errorReason)
697 {result =
true; errorReason =
"The variable name \"" + s +
"\" is an option name and cannot be used as a variable name";}
745 bool extended =
false;
763 if ( (extended = FindAndExtend<Atom> (objectName)) ) {}
764 else if ( (extended = FindAndExtend<NewColour> (objectName)) ) {}
765 else if ( (extended = FindAndExtend<Crystal> (objectName)) ) {}
766 else if ( (extended = FindAndExtend<Field> (objectName)) ) {}
767 else if ( (extended = FindAndExtend<Material> (objectName)) ) {}
768 else if ( (extended = FindAndExtend<Placement> (objectName)) ) {}
769 else if ( (extended = FindAndExtend<Query> (objectName)) ) {}
770 else if ( (extended = FindAndExtend<Region> (objectName)) ) {}
771 else if ( (extended = FindAndExtend<Tunnel> (objectName)) ) {}
772 else if ( (extended = FindAndExtend<CavityModel>(objectName)) ) {}
773 else if ( (extended = FindAndExtend<SamplerPlacement>(objectName)) ) {}
774 else if ( (extended = FindAndExtend<Scorer> (objectName)) ) {}
775 else if ( (extended = FindAndExtend<ScorerMesh> (objectName)) ) {}
776 else if ( (extended = FindAndExtend<Aperture> (objectName)) ) {}
777 else if ( (extended = FindAndExtend<BLMPlacement> (objectName)) ) {}
778 else if ( (extended = FindAndExtend<Modulator> (objectName)) ) {}
783 std::cerr <<
"parser.h> Error: object \"" << objectName
784 <<
"\" has not been defined and can't be extended." << std::endl;
798 auto search = fl.
find(objectName);
799 if (search != fl.
end())
811 {
object.set_value(option.first, option.second);}
813 {
object.set_value(option.first, option.second);}
815 {
object.set_value(option.first, option.second);}
845 const std::string& on = objectName;
848 auto searchAtom = std::find_if(
atom_list.begin(),
atom_list.end(), [&on](
const Atom& obj) {return obj.name == on;});
850 {searchAtom->print();
return true;}
853 {searchNewColour->print();
return true;}
856 {searchCrystal->print();
return true;}
859 {searchField->print();
return true;}
862 {searchMaterial->print();
return true;}
865 {searchQuery->print();
return true;}
868 {searchRegion->print();
return true;}
871 {searchTunnel->print();
return true;}
874 {searchXsecbias->print();
return true;}
877 {searchPlacement->print();
return true;}
880 {searchCavityModel->print();
return true;}
883 {searchSamplerPlacement->print();
return true;}
886 {searchScorer->print();
return true;}
889 {searchScorerMesh->print();
return true;}
892 {searchAperture->print();
return true;}
894 if (searchBLMPlacement !=
blm_list.end())
895 {searchBLMPlacement->print();
return true;}
898 {searchModulator->print();
return true;}
1034 template <
class C,
class Container>
1038 C& global = GetGlobal<C>();
1045 GetList<C, Container>().push_back(inst);
1048 template <
class C,
class Container>
1049 void Parser::Add(
bool unique,
const std::string& className)
1052 C& global = GetGlobal<C>();
1059 GetList<C, Container>().push_back(inst, unique, className);
1069 void Parser::Add<Placement, FastList<Placement>>(
bool unique,
const std::string& className)
1072 Placement& global = GetGlobal<Placement>();
1079 GetList<Placement, FastList<Placement>>().push_back(inst, unique, className);
1086 std::cerr <<
"The bdsimElement referred to in \"" << inst.
name <<
"\" (\""
1087 << inst.
bdsimElement <<
"\") cannot be found and should be defined"
1088 <<
" before this placement" << std::endl;
1091 placement_elements.push_back(
Element(*elDef));
Representation of arrays used in tokens.
void Copy(Container< std::string, std::allocator< std::string > > &cpy)
Copy STL string containers into symbols.
RF CavityModel class for parser.
Crystal class for parser.
List with Efficient Lookup.
FastListIterator insert(FastListInputIterator position, const T &val)
template definitions need to be in header
void erase()
erase elements
void push_back(const T &el, bool unique=false, const std::string &objectName="element")
FastListConstIterator find(std::string name, unsigned int count=1) const
Modulator class for parser.
Colour definition for parser.
void print() const
print some properties
void Overwrite(const std::string &objectName)
Overwrite object with current values.
FastList< Atom > atom_list
List of parser defined instances of that object.
void Add()
Insert global object of parser class C in Container class.
std::vector< std::list< Element > * > allocated_lines
double property_lookup(const std::string &element_name, const std::string &property_name) const
access property of Element with element_name
FastList< BLMPlacement > blm_list
List of parser defined instances of that object.
std::vector< std::string > sequences
Names of all defined sequences in the parser with 'line'.
const int MAX_EXPAND_ITERATIONS
maximum number of nested lines
Scorer scorer
The one instance we fill before appending to a list.
void ExtendValue(const std::string &property, T value)
Add value to be extended to object.
void ExtendObject(C &object)
Extend object with maps.
FastList< Crystal > crystal_list
List of parser defined instances of that object.
FastList< Placement > placement_list
List of parser defined instances of that object.
Material material
The one instance we fill before appending to a list.
virtual ~Parser()
Destructor.
Atom atom
The one instance we fill before appending to a list.
Symtab * symlook(const std::string &s)
look up parser symbol
static Parser * Instance()
Access method.
std::map< std::string, FastList< Element > * > expandedSequences
Cached copy of expanded sequences.
FastList< Region > region_list
List of parser defined instances of that object.
FastList< Modulator > modulator_list
List of parser defined instances of that object.
FastList< NewColour > colour_list
List of parser defined instances of that object.
Parameters params
The one instance we fill before appending to a list.
int add_sampler_partIDSet(std::list< int > *samplerPartIDListIn)
FastList< Scorer > scorer_list
List of parser defined instances of that object.
FastList< Tunnel > tunnel_list
List of parser defined instances of that object.
FastList< Element > element_list
List of all encountered elements.
void add_sampler(const std::string &name, int count, ElementType type, std::string samplerType, std::list< int > *samplerPartIDListIn=nullptr)
insert a sampler into beamline_list
C & GetGlobal()
Get global object of parser class C.
int copy_element_to_params(const std::string &elementName)
copy properties from Element into params, returns element type as integer, returs _NONE if not found
Crystal crystal
The one instance we fill before appending to a list.
bool TryPrintingObject(const std::string &objectName) const
Options options
General options.
Element & find_element(const std::string &element_name)
find element
FastList< Aperture > aperture_list
List of parser defined instances of that object.
Modulator modulator
The one instance we fill before appending to a list.
Tunnel tunnel
The one instance we fill before appending to a list.
void expand_line(FastList< Element > &target, const std::string &name, const std::string &start="", const std::string &end="")
FastList< PhysicsBiasing > xsecbias_list
List of parser defined instances of that object.
void write_table(std::string *name, ElementType type, bool isLine=false)
Method that transfers parameters to element properties.
FastList< ScorerMesh > scorermesh_list
List of parser defined instances of that object.
const Element * find_placement_element_safe(const std::string &element_name) const
search placement_element
void PrintElements() const
Print methods.
FastList< Element > placement_elements
std::vector< std::string * > var_list
Variable vector for memory storage.
FastList< Element > beamline_list
Beamline.
void add_element_temp(const std::string &name, int number, bool pushfront, ElementType linetype)
add element to temporary element sequence tmp_list
std::set< std::set< int > > samplerFilters
FastList< SamplerPlacement > samplerplacement_list
List of parser defined instances of that object.
Field field
The one instance we fill before appending to a list.
std::list< Element > tmp_list
Temporary list.
Query query
The one instance we fill before appending to a list.
Aperture aperture
The one instance we fill before appending to a list.
void AddVariable(std::string *name)
Add variable memory to variable list for memory management.
BLMPlacement blm
The one instance we fill before appending to a list.
std::map< std::string, std::string > extendedStrings
Map for options of type string for extending objects.
Region region
The one instance we fill before appending to a list.
void ParseFile(FILE *f)
Parse the input file and construct beamline_list and options.
bool FindAndExtend(const std::string &objectName)
Find object by name in list.
SymbolMap symtab_map
Parser symbol map.
void PrintBeamline() const
Print methods.
const FastList< Element > & get_sequence(const std::string &name)
std::map< std::string, Array * > extendedVectors
Map for options of type vector for extending objects.
void set_sampler(const std::string &name, int count, ElementType type, const std::string &samplerType, double samplerRadius=0, int particleSetID=-1)
Set sampler.
Symtab * symcreate(const std::string &s)
create new parser symbol
NewColour colour
The one instance we fill before appending to a list.
ScorerMesh scorermesh
The one instance we fill before appending to a list.
const FastList< Element > & GetBeamline() const
void PrintOptions() const
Print methods.
FastList< Field > field_list
List of parser defined instances of that object.
FastList< CavityModel > cavitymodel_list
List of parser defined instances of that object.
std::map< std::string, double > extendedNumbers
Map for options of type double for extending objects.
Placement placement
The one instance we fill before appending to a list.
std::list< double > tmparray
temporary list for reading of arrays in parser
CavityModel cavitymodel
The one instance we fill before appending to a list.
FastList< Query > query_list
List of parser defined instances of that object.
Parser()=delete
No default constructor.
void Store(double value)
Add value to front of temporary list.
static Parser * instance
Instance.
PhysicsBiasing xsecbias
The one instance we fill before appending to a list.
void add_func(std::string name, double(*func)(double))
Add function to parser.
void Initialise()
Initialisation of parser functions and constants.
FastList< Material > material_list
List of parser defined instances of that object.
const Element * find_element_safe(const std::string &element_name) const
find element by pointer - nullptr if not found - searches element_list
void expand_sequences()
Expand all sequences define with 'line' into FastLists.
std::list< std::string > tmpstring
temporary list for reading of arrays in parser
SamplerPlacement samplerplacement
The one instance we fill before appending to a list.
Physics biasing class for parser.
Placement class for parser.
std::string name
Name of this placement.
void print() const
print some properties
std::string bdsimElement
Name of bdsim-built component to place instead of piece of geometry.
Query structure class for parser.
Sampler placement class for parser.
ScorerMesh class for parser.
void clear()
Destructor that clears memory.
Symtab * symlook(std::string s)
Look up parser symbol.
Symtab * symcreate(std::string s)
Create new parser symbol.
Common header for the lexer and the parser to share Symbol table for numeric variables,...
void Set(Array *)
Set to Array value.
Parser namespace for GMAD language. Combination of Geant4 and MAD.
ElementType
types of elements
double property_lookup(std::string property_name) const
void set(const Parameters ¶ms)
set method from Parameters structure
std::list< Element > * lst
in case the element is a list itself (line)
ElementType type
element enum
Parameters - Element class with booleans.
void inherit_properties(const Element &e)
void flush()
Reset the parameters to defaults and setMap.