BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
parser.h
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef PARSER_H
20#define PARSER_H
21
22#include <list>
23#include <map>
24#include <set>
25#include <string>
26#include <vector>
27
28#include "aperture.h"
29#include "atom.h"
30#include "beam.h"
31#include "blmplacement.h"
32#include "cavitymodel.h"
33#include "newcolour.h"
34#include "crystal.h"
35#include "element.h"
36#include "elementtype.h"
37#include "field.h"
38#include "fastlist.h"
39#include "material.h"
40#include "modulator.h"
41#include "options.h"
42#include "parameters.h"
43#include "physicsbiasing.h"
44#include "placement.h"
45#include "query.h"
46#include "region.h"
47#include "scorer.h"
48#include "scorermesh.h"
49#include "samplerplacement.h"
50#include "symbolmap.h"
51#include "tunnel.h"
52
54int yyerror(const char *);
55int yyerror2(const char *s);
57extern int yylex();
58
65namespace GMAD
66{
67 class Array;
68 class Symtab;
79 class Parser
80 {
81 public:
83 Parser() = delete;
85 static Parser* Instance(const std::string& filename);
87 static Parser* Instance();
89 virtual ~Parser();
90
91 protected:
93 explicit Parser(std::string filename);
94 private:
98 void Initialise();
100 void ParseFile(FILE *f);
101
102 public:
104 void quit();
106 void write_table(std::string* name, ElementType type, bool isLine=false);
107
110 void expand_line(FastList<Element>& target,
111 const std::string& name,
112 const std::string& start = "",
113 const std::string& end = "");
114
116 void expand_line(const std::string& name,
117 const std::string& start,
118 const std::string& end);
119
122 const FastList<Element>& get_sequence(const std::string& name);
123
126 int add_sampler_partIDSet(std::list<int>* samplerPartIDListIn);
128 void add_sampler(const std::string& name, int count, ElementType type, std::string samplerType, std::list<int>* samplerPartIDListIn = nullptr);
130 template <class C, class Container=FastList<C>>
131 void Add();
132 template <class C, class Container=FastList<C>>
133 void Add(bool unique, const std::string& className);
135 template <class C>
138 template <class C, class Container=FastList<C>>
139 Container& GetList();
140
141 const std::set<std::set<int>>& GetSamplerFilters() const {return samplerFilters;}
142 const std::map<int, std::set<int>>& GetSamplerFilterIDToSet() const {return samplerFilterIDToSet;}
143
145 Element& find_element(const std::string& element_name);
147 const Element& find_element(const std::string& element_name) const;
149 const Element* find_element_safe(const std::string& element_name) const;
151 const Element* find_placement_element_safe(const std::string& element_name) const;
153 double property_lookup(const std::string& element_name, const std::string& property_name) const;
155 void add_element_temp(const std::string& name, int number, bool pushfront, ElementType linetype);
157 int copy_element_to_params(const std::string& elementName);
158
159 bool InvalidSymbolName(const std::string& s, std::string& errorReason);
161 Symtab * symcreate(const std::string& s);
163 Symtab * symlook(const std::string& s);
164
166 void Store(double value);
167 void Store(const std::string& name);
170 void FillArray(Array*);
171 void FillString(Array*);
174 void ClearParams();
176 template <class C, typename T>
177 void SetValue(std::string property, T value);
179 template <class C>
180 double GetValue(std::string property);
181
182 template<typename T>
183 std::list<T>* ArrayToList(Array*);
184
186 template <typename T>
187 void ExtendValue(const std::string& property, T value);
188
190 void Overwrite(const std::string& objectName);
192 void AddVariable(std::string* name);
194 void PrintBeamline()const;
195 void PrintElements()const;
196 void PrintOptions()const;
200 bool TryPrintingObject(const std::string& objectName) const;
201
203 std::string current_line;
204 std::string current_start;
205 std::string current_end;
208 const FastList<Element>& GetBeamline() const;
209
210 private:
212 void set_sampler(const std::string& name,
213 int count, ElementType type,
214 const std::string& samplerType,
215 double samplerRadius=0,
216 int particleSetID = -1);
218 void add_func(std::string name, double (*func)(double));
219 void add_var(std::string name, double value, int is_reserved = 0);
220
222 void expand_sequences();
223
224 // protected implementation (for inheritance to BDSParser - hackish)
225 protected:
251
252 private:
253 // *****************
254 // Private members *
255 // *****************
257 const int MAX_EXPAND_ITERATIONS = 50;
258
260 std::list<double> tmparray;
261 std::list<std::string> tmpstring;
264 std::vector<std::list<Element>*> allocated_lines;
265
286
288 template <class C>
289 bool FindAndExtend(const std::string& objectName);
291 template <class C>
292 void ExtendObject(C& object);
293
295 std::map<std::string, double> extendedNumbers;
297 std::map<std::string, std::string> extendedStrings;
299 std::map<std::string, Array*> extendedVectors;
300
303
308
310 std::list<Element> tmp_list;
311
313 std::vector<std::string> sequences;
314
316 std::map<std::string, FastList<Element>*> expandedSequences;
317
321 std::vector<std::string*> var_list;
322
325 std::set<std::set<int>> samplerFilters;
326 std::map<int, std::set<int>> samplerFilterIDToSet;
327 std::map<std::set<int>, int> setToSamplerFilterID;
328 };
329
330 template <class C, typename T>
331 void Parser::SetValue(std::string property, T value)
332 {
333 GetGlobal<C>().set_value(property, value);
334 }
335
336 template <class C>
337 double Parser::GetValue(std::string property)
338 {
339 return GetGlobal<C>().get_value(property);
340 }
341
342 template<typename T>
343 std::list<T>* Parser::ArrayToList(Array* arrayIn)
344 {
345 if (!arrayIn)
346 {return nullptr;}
347 else
348 {
349 std::list<T>* result = new std::list<T>();
350 const auto& doubleData = arrayIn->GetDataList();
351 for (auto& value : doubleData)
352 {result->push_back((T)value);}
353 return result;
354 }
355 }
356}
357
358#endif
Aperture class.
Definition: aperture.h:38
Representation of arrays used in tokens.
Definition: array.h:40
blm for parser.
Definition: blmplacement.h:39
Beam class.
Definition: beam.h:44
RF CavityModel class for parser.
Definition: cavitymodel.h:35
Crystal class for parser.
Definition: crystal.h:37
List with Efficient Lookup.
Definition: fastlist.h:42
Field class for parser.
Definition: field.h:37
Modulator class for parser.
Definition: modulator.h:37
Colour definition for parser.
Definition: newcolour.h:40
Options class.
Definition: options.h:44
Parser class.
Definition: parser.h:80
void Overwrite(const std::string &objectName)
Overwrite object with current values.
Definition: parser.cc:738
FastList< Atom > atom_list
List of parser defined instances of that object.
Definition: parser.h:233
void Add()
Insert global object of parser class C in Container class.
Definition: parser.cc:1035
std::vector< std::list< Element > * > allocated_lines
Definition: parser.h:264
double property_lookup(const std::string &element_name, const std::string &property_name) const
access property of Element with element_name
Definition: parser.cc:635
FastList< BLMPlacement > blm_list
List of parser defined instances of that object.
Definition: parser.h:248
std::vector< std::string > sequences
Names of all defined sequences in the parser with 'line'.
Definition: parser.h:313
void FillString(Array *)
Definition: parser.cc:726
const int MAX_EXPAND_ITERATIONS
maximum number of nested lines
Definition: parser.h:257
void quit()
Exit method.
Definition: parser.cc:268
Scorer scorer
The one instance we fill before appending to a list.
Definition: parser.h:280
void ExtendValue(const std::string &property, T value)
Add value to be extended to object.
std::string current_line
Name of beamline.
Definition: parser.h:203
void ExtendObject(C &object)
Extend object with maps.
Definition: parser.cc:808
FastList< Crystal > crystal_list
List of parser defined instances of that object.
Definition: parser.h:235
FastList< Placement > placement_list
List of parser defined instances of that object.
Definition: parser.h:242
Material material
The one instance we fill before appending to a list.
Definition: parser.h:272
virtual ~Parser()
Destructor.
Definition: parser.cc:115
Atom atom
The one instance we fill before appending to a list.
Definition: parser.h:268
Symtab * symlook(const std::string &s)
look up parser symbol
Definition: parser.cc:706
static Parser * Instance()
Access method.
Definition: parser.cc:94
std::map< std::string, FastList< Element > * > expandedSequences
Cached copy of expanded sequences.
Definition: parser.h:316
FastList< Region > region_list
List of parser defined instances of that object.
Definition: parser.h:239
FastList< Modulator > modulator_list
List of parser defined instances of that object.
Definition: parser.h:249
void SetValue(std::string property, T value)
Set value for parser class.
Definition: parser.h:331
FastList< NewColour > colour_list
List of parser defined instances of that object.
Definition: parser.h:234
Parameters params
The one instance we fill before appending to a list.
Definition: parser.h:267
int add_sampler_partIDSet(std::list< int > *samplerPartIDListIn)
Definition: parser.cc:556
FastList< Scorer > scorer_list
List of parser defined instances of that object.
Definition: parser.h:245
FastList< Tunnel > tunnel_list
List of parser defined instances of that object.
Definition: parser.h:240
FastList< Element > element_list
List of all encountered elements.
Definition: parser.h:302
Container & GetList()
Get list for parser class C.
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
Definition: parser.cc:574
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
Definition: parser.cc:666
Crystal crystal
The one instance we fill before appending to a list.
Definition: parser.h:270
bool TryPrintingObject(const std::string &objectName) const
Definition: parser.cc:838
Options options
General options.
Definition: parser.h:229
Element & find_element(const std::string &element_name)
find element
Definition: parser.cc:586
FastList< Aperture > aperture_list
List of parser defined instances of that object.
Definition: parser.h:247
Modulator modulator
The one instance we fill before appending to a list.
Definition: parser.h:284
Tunnel tunnel
The one instance we fill before appending to a list.
Definition: parser.h:277
void expand_line(FastList< Element > &target, const std::string &name, const std::string &start="", const std::string &end="")
Definition: parser.cc:308
FastList< PhysicsBiasing > xsecbias_list
List of parser defined instances of that object.
Definition: parser.h:241
void write_table(std::string *name, ElementType type, bool isLine=false)
Method that transfers parameters to element properties.
Definition: parser.cc:274
FastList< ScorerMesh > scorermesh_list
List of parser defined instances of that object.
Definition: parser.h:246
const Element * find_placement_element_safe(const std::string &element_name) const
search placement_element
Definition: parser.cc:611
void PrintElements() const
Print methods.
Definition: parser.cc:828
FastList< Element > placement_elements
Definition: parser.h:307
std::vector< std::string * > var_list
Variable vector for memory storage.
Definition: parser.h:321
FastList< Element > beamline_list
Beamline.
Definition: parser.h:231
void add_element_temp(const std::string &name, int number, bool pushfront, ElementType linetype)
add element to temporary element sequence tmp_list
Definition: parser.cc:641
std::set< std::set< int > > samplerFilters
Definition: parser.h:325
FastList< SamplerPlacement > samplerplacement_list
List of parser defined instances of that object.
Definition: parser.h:244
Field field
The one instance we fill before appending to a list.
Definition: parser.h:271
std::list< Element > tmp_list
Temporary list.
Definition: parser.h:310
Query query
The one instance we fill before appending to a list.
Definition: parser.h:275
Aperture aperture
The one instance we fill before appending to a list.
Definition: parser.h:282
void AddVariable(std::string *name)
Add variable memory to variable list for memory management.
Definition: parser.cc:818
BLMPlacement blm
The one instance we fill before appending to a list.
Definition: parser.h:283
std::map< std::string, std::string > extendedStrings
Map for options of type string for extending objects.
Definition: parser.h:297
Region region
The one instance we fill before appending to a list.
Definition: parser.h:276
std::string current_end
Name of beamline.
Definition: parser.h:205
void ParseFile(FILE *f)
Parse the input file and construct beamline_list and options.
Definition: parser.cc:156
bool FindAndExtend(const std::string &objectName)
Find object by name in list.
Definition: parser.cc:795
SymbolMap symtab_map
Parser symbol map.
Definition: parser.h:319
void PrintBeamline() const
Print methods.
Definition: parser.cc:823
const FastList< Element > & get_sequence(const std::string &name)
Definition: parser.cc:461
std::map< std::string, Array * > extendedVectors
Map for options of type vector for extending objects.
Definition: parser.h:299
void set_sampler(const std::string &name, int count, ElementType type, const std::string &samplerType, double samplerRadius=0, int particleSetID=-1)
Set sampler.
Definition: parser.cc:471
double GetValue(std::string property)
Get value for parser class (only for doubles)
Definition: parser.h:337
Symtab * symcreate(const std::string &s)
create new parser symbol
Definition: parser.cc:701
NewColour colour
The one instance we fill before appending to a list.
Definition: parser.h:269
ScorerMesh scorermesh
The one instance we fill before appending to a list.
Definition: parser.h:281
const FastList< Element > & GetBeamline() const
Definition: parser.cc:903
void PrintOptions() const
Print methods.
Definition: parser.cc:833
void ClearParams()
Definition: parser.cc:732
void FillArray(Array *)
Definition: parser.cc:720
FastList< Field > field_list
List of parser defined instances of that object.
Definition: parser.h:236
FastList< CavityModel > cavitymodel_list
List of parser defined instances of that object.
Definition: parser.h:243
std::map< std::string, double > extendedNumbers
Map for options of type double for extending objects.
Definition: parser.h:295
Placement placement
The one instance we fill before appending to a list.
Definition: parser.h:274
std::list< double > tmparray
temporary list for reading of arrays in parser
Definition: parser.h:260
CavityModel cavitymodel
The one instance we fill before appending to a list.
Definition: parser.h:278
FastList< Query > query_list
List of parser defined instances of that object.
Definition: parser.h:238
Parser()=delete
No default constructor.
void Store(double value)
Add value to front of temporary list.
Definition: parser.cc:710
static Parser * instance
Instance.
Definition: parser.h:96
PhysicsBiasing xsecbias
The one instance we fill before appending to a list.
Definition: parser.h:273
Beam beam
Beam instance;.
Definition: parser.h:227
void add_func(std::string name, double(*func)(double))
Add function to parser.
Definition: parser.cc:681
void Initialise()
Initialisation of parser functions and constants.
Definition: parser.cc:188
FastList< Material > material_list
List of parser defined instances of that object.
Definition: parser.h:237
std::string current_start
Name of beamline.
Definition: parser.h:204
const Element * find_element_safe(const std::string &element_name) const
find element by pointer - nullptr if not found - searches element_list
Definition: parser.cc:623
void expand_sequences()
Expand all sequences define with 'line' into FastLists.
Definition: parser.cc:291
std::list< std::string > tmpstring
temporary list for reading of arrays in parser
Definition: parser.h:261
SamplerPlacement samplerplacement
The one instance we fill before appending to a list.
Definition: parser.h:279
Physics biasing class for parser.
Placement class for parser.
Definition: placement.h:41
Query structure class for parser.
Definition: query.h:37
Region class for parser.
Definition: region.h:36
Sampler placement class for parser.
ScorerMesh class for parser.
Definition: scorermesh.h:38
Scorer class for parser.
Definition: scorer.h:37
Class that hold the symbol table.
Definition: symbolmap.h:33
Tunnel class for parser.
Definition: tunnel.h:35
Parser namespace for GMAD language. Combination of Geant4 and MAD.
ElementType
types of elements
Definition: elementtype.h:28
Atom class.
Definition: atom.h:36
Material class.
Definition: material.h:39
Parameters - Element class with booleans.
Definition: parameters.h:44