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 - 2024.
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 "coolingchannel.h"
35#include "crystal.h"
36#include "element.h"
37#include "elementtype.h"
38#include "field.h"
39#include "fastlist.h"
40#include "laser.h"
41#include "material.h"
42#include "modulator.h"
43#include "options.h"
44#include "parameters.h"
45#include "physicsbiasing.h"
46#include "placement.h"
47#include "query.h"
48#include "region.h"
49#include "scorer.h"
50#include "scorermesh.h"
51#include "samplerplacement.h"
52#include "symbolmap.h"
53#include "tunnel.h"
54
56int yyerror(const char *);
57int yyerror2(const char *s);
59extern int yylex();
60
67namespace GMAD
68{
69 class Array;
70 class Symtab;
81 class Parser
82 {
83 public:
84
86 static Parser* Instance(const std::string& filename);
88 static Parser* Instance();
90 virtual ~Parser();
92 Parser();
93
94 protected:
96 explicit Parser(std::string filename);
97
98 private:
102 void Initialise();
104 void ParseFile(FILE *f);
105
106 public:
108 void quit();
110 void write_table(std::string* name, ElementType type, bool isLine=false);
111
114 void expand_line(FastList<Element>& target,
115 const std::string& name,
116 const std::string& start = "",
117 const std::string& end = "");
118
120 void expand_line(const std::string& name,
121 const std::string& start,
122 const std::string& end);
123
126 const FastList<Element>& get_sequence(const std::string& name);
127
130 int add_sampler_partIDSet(std::list<int>* samplerPartIDListIn);
132 void add_sampler(const std::string& name, int count, ElementType type, std::string samplerType, std::list<int>* samplerPartIDListIn = nullptr);
134 template <class C, class Container=FastList<C>>
135 void Add();
136 template <class C, class Container=FastList<C>>
137 void Add(bool unique, const std::string& className);
139 template <class C>
141 template <class C>
142 C* GetGlobalPtr();
144 template <class C, class Container=FastList<C>>
145 Container& GetList();
146
147 const std::set<std::set<int>>& GetSamplerFilters() const {return samplerFilters;}
148 const std::map<int, std::set<int>>& GetSamplerFilterIDToSet() const {return samplerFilterIDToSet;}
149
151 Element& find_element(const std::string& element_name);
153 const Element& find_element(const std::string& element_name) const;
155 const Element* find_element_safe(const std::string& element_name) const;
157 const Element* find_placement_element_safe(const std::string& element_name) const;
159 double property_lookup(const std::string& element_name, const std::string& property_name) const;
161 void add_element_temp(const std::string& name, int number, bool pushfront, ElementType linetype);
163 int copy_element_to_params(const std::string& elementName);
164
165 bool InvalidSymbolName(const std::string& s, std::string& errorReason);
167 Symtab * symcreate(const std::string& s);
169 Symtab * symlook(const std::string& s);
170
172 void Store(double value);
173 void Store(const std::string& name);
176 void FillArray(Array*);
177 void FillString(Array*);
180 void ClearParams();
182 template <class C, typename T>
183 void SetValue(std::string property, T value);
185 template <class C>
186 double GetValue(std::string property);
187
188 template<typename T>
189 std::list<T>* ArrayToList(Array*);
190
192 template <typename T>
193 void ExtendValue(const std::string& property, T value);
194
196 void Overwrite(const std::string& objectName);
198 void AddVariable(std::string* name);
200 void PrintBeamline()const;
201 void PrintElements()const;
202 void PrintOptions()const;
206 bool TryPrintingObject(const std::string& objectName) const;
207
209 std::string current_line;
210 std::string current_start;
211 std::string current_end;
214 const FastList<Element>& GetBeamline() const;
215
216 private:
218 void set_sampler(const std::string& name,
219 int count, ElementType type,
220 const std::string& samplerType,
221 double samplerRadius=0,
222 int particleSetID = -1);
224 void add_func(std::string name, double (*func)(double));
225 void add_var(std::string name, double value, int is_reserved = 0);
226
228 void expand_sequences();
229
230 // protected implementation (for inheritance to BDSParser - hackish)
231 protected:
259
260 private:
261 // *****************
262 // Private members *
263 // *****************
265 const int MAX_EXPAND_ITERATIONS = 50;
266
268 std::list<double> tmparray;
269 std::list<std::string> tmpstring;
272 std::vector<std::list<Element>*> allocated_lines;
273
297
299 template <class C>
300 bool FindAndExtend(const std::string& objectName);
302 template <class C>
303 void ExtendObject(C& object);
304
306 std::map<std::string, double> extendedNumbers;
308 std::map<std::string, std::string> extendedStrings;
310 std::map<std::string, Array*> extendedVectors;
311
314
319
321 std::list<Element> tmp_list;
322
324 std::vector<std::string> sequences;
325
327 std::map<std::string, FastList<Element>*> expandedSequences;
328
332 std::vector<std::string*> var_list;
333
336 std::set<std::set<int>> samplerFilters;
337 std::map<int, std::set<int>> samplerFilterIDToSet;
338 std::map<std::set<int>, int> setToSamplerFilterID;
339 };
340
341 template <class C, typename T>
342 void Parser::SetValue(std::string property, T value)
343 {
344 GetGlobal<C>().set_value(property, value);
345 }
346
347 template <class C>
348 double Parser::GetValue(std::string property)
349 {
350 return GetGlobal<C>().get_value(property);
351 }
352
353 template<typename T>
354 std::list<T>* Parser::ArrayToList(Array* arrayIn)
355 {
356 if (!arrayIn)
357 {return nullptr;}
358 else
359 {
360 std::list<T>* result = new std::list<T>();
361 const auto& doubleData = arrayIn->GetDataList();
362 for (auto& value : doubleData)
363 {result->push_back((T)value);}
364 return result;
365 }
366 }
367}
368
369#endif
Aperture class.
Definition aperture.h:38
Representation of arrays used in tokens.
Definition array.h:40
blm for parser.
Beam class.
Definition beam.h:44
RF CavityModel class for parser.
Definition cavitymodel.h:35
Cooling channel parameters.
Crystal class for parser.
Definition crystal.h:37
List with Efficient Lookup.
Definition fastlist.h:42
Field class for parser.
Definition field.h:37
Placement class for laser.
Definition laser.h:36
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:82
void Overwrite(const std::string &objectName)
Overwrite object with current values.
Definition parser.cc:772
FastList< Atom > atom_list
List of parser defined instances of that object.
Definition parser.h:239
void Add()
Insert global object of parser class C in Container class.
Definition parser.cc:1054
Parser()
Default contructor.
Definition parser.cc:115
std::vector< std::list< Element > * > allocated_lines
Definition parser.h:272
double property_lookup(const std::string &element_name, const std::string &property_name) const
access property of Element with element_name
Definition parser.cc:669
FastList< BLMPlacement > blm_list
List of parser defined instances of that object.
Definition parser.h:256
std::vector< std::string > sequences
Names of all defined sequences in the parser with 'line'.
Definition parser.h:324
void FillString(Array *)
Definition parser.cc:760
const int MAX_EXPAND_ITERATIONS
maximum number of nested lines
Definition parser.h:265
void quit()
Exit method.
Definition parser.cc:302
Laser laser
The one instance we fill before appending to a list.
Definition parser.h:281
Scorer scorer
The one instance we fill before appending to a list.
Definition parser.h:290
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:209
void ExtendObject(C &object)
Extend object with maps.
Definition parser.cc:844
FastList< Crystal > crystal_list
List of parser defined instances of that object.
Definition parser.h:241
FastList< Placement > placement_list
List of parser defined instances of that object.
Definition parser.h:249
Material material
The one instance we fill before appending to a list.
Definition parser.h:282
virtual ~Parser()
Destructor.
Definition parser.cc:140
Atom atom
The one instance we fill before appending to a list.
Definition parser.h:276
Symtab * symlook(const std::string &s)
look up parser symbol
Definition parser.cc:740
static Parser * Instance()
Access method.
Definition parser.cc:119
std::map< std::string, FastList< Element > * > expandedSequences
Cached copy of expanded sequences.
Definition parser.h:327
FastList< Region > region_list
List of parser defined instances of that object.
Definition parser.h:246
FastList< Modulator > modulator_list
List of parser defined instances of that object.
Definition parser.h:257
void SetValue(std::string property, T value)
Set value for parser class.
Definition parser.h:342
FastList< NewColour > colour_list
List of parser defined instances of that object.
Definition parser.h:240
Parameters params
The one instance we fill before appending to a list.
Definition parser.h:275
int add_sampler_partIDSet(std::list< int > *samplerPartIDListIn)
Definition parser.cc:590
FastList< Scorer > scorer_list
List of parser defined instances of that object.
Definition parser.h:253
FastList< Tunnel > tunnel_list
List of parser defined instances of that object.
Definition parser.h:247
FastList< Element > element_list
List of all encountered elements.
Definition parser.h:313
Container & GetList()
Get list for parser class C.
CoolingChannel coolingchannel
The one instance we fill before appending to a list.
Definition parser.h:279
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:608
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:700
Crystal crystal
The one instance we fill before appending to a list.
Definition parser.h:278
bool TryPrintingObject(const std::string &objectName) const
Definition parser.cc:874
Options options
General options.
Definition parser.h:235
Element & find_element(const std::string &element_name)
find element
Definition parser.cc:620
FastList< Aperture > aperture_list
List of parser defined instances of that object.
Definition parser.h:255
Modulator modulator
The one instance we fill before appending to a list.
Definition parser.h:294
FastList< CoolingChannel > coolingchannel_list
List of parser defined instances of that object.
Definition parser.h:242
Tunnel tunnel
The one instance we fill before appending to a list.
Definition parser.h:287
void expand_line(FastList< Element > &target, const std::string &name, const std::string &start="", const std::string &end="")
Definition parser.cc:342
FastList< PhysicsBiasing > xsecbias_list
List of parser defined instances of that object.
Definition parser.h:248
void write_table(std::string *name, ElementType type, bool isLine=false)
Method that transfers parameters to element properties.
Definition parser.cc:308
FastList< ScorerMesh > scorermesh_list
List of parser defined instances of that object.
Definition parser.h:254
const Element * find_placement_element_safe(const std::string &element_name) const
search placement_element
Definition parser.cc:645
void PrintElements() const
Print methods.
Definition parser.cc:864
FastList< Element > placement_elements
Definition parser.h:318
std::vector< std::string * > var_list
Variable vector for memory storage.
Definition parser.h:332
FastList< Element > beamline_list
Beamline.
Definition parser.h:237
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:675
std::set< std::set< int > > samplerFilters
Definition parser.h:336
FastList< SamplerPlacement > samplerplacement_list
List of parser defined instances of that object.
Definition parser.h:252
Field field
The one instance we fill before appending to a list.
Definition parser.h:280
std::list< Element > tmp_list
Temporary list.
Definition parser.h:321
Query query
The one instance we fill before appending to a list.
Definition parser.h:285
Aperture aperture
The one instance we fill before appending to a list.
Definition parser.h:292
void AddVariable(std::string *name)
Add variable memory to variable list for memory management.
Definition parser.cc:854
BLMPlacement blm
The one instance we fill before appending to a list.
Definition parser.h:293
std::map< std::string, std::string > extendedStrings
Map for options of type string for extending objects.
Definition parser.h:308
Region region
The one instance we fill before appending to a list.
Definition parser.h:286
std::string current_end
Name of beamline.
Definition parser.h:211
void ParseFile(FILE *f)
Parse the input file and construct beamline_list and options.
Definition parser.cc:181
bool FindAndExtend(const std::string &objectName)
Laser instance.
Definition parser.cc:831
SymbolMap symtab_map
Parser symbol map.
Definition parser.h:330
void PrintBeamline() const
Print methods.
Definition parser.cc:859
const FastList< Element > & get_sequence(const std::string &name)
Definition parser.cc:495
std::map< std::string, Array * > extendedVectors
Map for options of type vector for extending objects.
Definition parser.h:310
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:505
double GetValue(std::string property)
Get value for parser class (only for doubles)
Definition parser.h:348
Symtab * symcreate(const std::string &s)
create new parser symbol
Definition parser.cc:735
NewColour colour
The one instance we fill before appending to a list.
Definition parser.h:277
ScorerMesh scorermesh
The one instance we fill before appending to a list.
Definition parser.h:291
const FastList< Element > & GetBeamline() const
Definition parser.cc:945
void PrintOptions() const
Print methods.
Definition parser.cc:869
void ClearParams()
Definition parser.cc:766
void FillArray(Array *)
Definition parser.cc:754
FastList< Field > field_list
List of parser defined instances of that object.
Definition parser.h:243
FastList< CavityModel > cavitymodel_list
List of parser defined instances of that object.
Definition parser.h:251
std::map< std::string, double > extendedNumbers
Map for options of type double for extending objects.
Definition parser.h:306
Placement placement
The one instance we fill before appending to a list.
Definition parser.h:284
std::list< double > tmparray
temporary list for reading of arrays in parser
Definition parser.h:268
CavityModel cavitymodel
The one instance we fill before appending to a list.
Definition parser.h:288
FastList< Query > query_list
List of parser defined instances of that object.
Definition parser.h:245
void Store(double value)
Add value to front of temporary list.
Definition parser.cc:744
static Parser * instance
Instance.
Definition parser.h:100
PhysicsBiasing xsecbias
The one instance we fill before appending to a list.
Definition parser.h:283
FastList< Laser > laser_list
List of parser defined instances of that object.
Definition parser.h:250
Beam beam
Beam instance;.
Definition parser.h:233
void add_func(std::string name, double(*func)(double))
Add function to parser.
Definition parser.cc:715
void Initialise()
Initialisation of parser functions and constants.
Definition parser.cc:213
FastList< Material > material_list
List of parser defined instances of that object.
Definition parser.h:244
std::string current_start
Name of beamline.
Definition parser.h:210
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:657
void expand_sequences()
Expand all sequences define with 'line' into FastLists.
Definition parser.cc:325
std::list< std::string > tmpstring
temporary list for reading of arrays in parser
Definition parser.h:269
SamplerPlacement samplerplacement
The one instance we fill before appending to a list.
Definition parser.h:289
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:40
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