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) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef PARSER_H
19#define PARSER_H
20
21#include <list>
22#include <map>
23#include <set>
24#include <string>
25#include <vector>
26
27#include "aperture.h"
28#include "atom.h"
29#include "beam.h"
30#include "blmplacement.h"
31#include "cavitymodel.h"
32#include "newcolour.h"
33#include "coolingchannel.h"
34#include "crystal.h"
35#include "element.h"
36#include "elementtype.h"
37#include "field.h"
38#include "fastlist.h"
39#include "laser.h"
40#include "material.h"
41#include "modulator.h"
42#include "options.h"
43#include "parameters.h"
44#include "physicsbiasing.h"
45#include "placement.h"
46#include "query.h"
47#include "region.h"
48#include "scorer.h"
49#include "scorermesh.h"
50#include "samplerplacement.h"
51#include "symbolmap.h"
52#include "tunnel.h"
53
55int yyerror(const char *);
56int yyerror2(const char *s);
58extern int yylex();
59
66namespace GMAD
67{
68 class Array;
69 class Symtab;
80 class Parser
81 {
82 public:
83
85 static Parser* Instance(const std::string& filename);
87 static Parser* Instance();
89 virtual ~Parser();
91 Parser();
92
93 protected:
95 explicit Parser(std::string filename);
96
97 private:
101 void Initialise();
103 void ParseFile(FILE *f);
104
105 public:
107 void quit();
109 void write_table(std::string* name, ElementType type, bool isLine=false);
110
112 void expand_sequences();
115 void expand_line(FastList<Element>& target,
116 const std::string& name,
117 const std::string& start = "",
118 const std::string& end = "");
119
121 void expand_line(const std::string& name,
122 const std::string& start,
123 const std::string& end);
124
126 std::vector<std::string>& get_sequences();
127
130 const FastList<Element>& get_sequence(const std::string& name, bool bExit = true);
131
134 int add_sampler_partIDSet(std::list<int>* samplerPartIDListIn);
136 void add_sampler(const std::string& name, int count, ElementType type, std::string samplerType, std::list<int>* samplerPartIDListIn = nullptr);
138 template <class C, class Container=FastList<C>>
139 void Add();
140 template <class C, class Container=FastList<C>>
141 void Add(bool unique, const std::string& className);
143 template <class C>
145 template <class C>
146 C* GetGlobalPtr();
148 template <class C, class Container=FastList<C>>
149 Container& GetList();
150
151 const std::set<std::set<int>>& GetSamplerFilters() const {return samplerFilters;}
152 const std::map<int, std::set<int>>& GetSamplerFilterIDToSet() const {return samplerFilterIDToSet;}
153
155 Element& find_element(const std::string& element_name);
157 const Element& find_element(const std::string& element_name) const;
159 const Element* find_element_safe(const std::string& element_name) const;
161 const Element* find_placement_element_safe(const std::string& element_name) const;
163 double property_lookup(const std::string& element_name, const std::string& property_name) const;
165 void add_element_temp(const std::string& name, int number, bool pushfront, ElementType linetype);
167 int copy_element_to_params(const std::string& elementName);
168
169 bool InvalidSymbolName(const std::string& s, std::string& errorReason);
171 Symtab * symcreate(const std::string& s);
173 Symtab * symlook(const std::string& s);
174
176 void Store(double value);
177 void Store(const std::string& name);
180 void FillArray(Array*);
181 void FillString(Array*);
184 void ClearParams();
186 template <class C, typename T>
187 void SetValue(std::string property, T value);
189 template <class C>
190 double GetValue(std::string property);
191
192 template<typename T>
193 std::list<T>* ArrayToList(Array*);
194
196 template <typename T>
197 void ExtendValue(const std::string& property, T value);
198
200 void Overwrite(const std::string& objectName);
202 void AddVariable(std::string* name);
204 void PrintBeamline()const;
205 void PrintElements()const;
206 void PrintOptions()const;
210 bool TryPrintingObject(const std::string& objectName) const;
211
213 std::string current_line;
214 std::string current_start;
215 std::string current_end;
218 const FastList<Element>& GetBeamline() const;
219
220 std::string GetCallSequenceLog();
221 private:
223 void set_sampler(const std::string& name,
224 int count, ElementType type,
225 const std::string& samplerType,
226 double samplerRadius=0,
227 int particleSetID = -1);
229 void add_func(std::string name, double (*func)(double));
230 void add_var(std::string name, double value, int is_reserved = 0);
231
232 // protected implementation (for inheritance to BDSParser - hackish)
233 protected:
261
262 private:
263 // *****************
264 // Private members *
265 // *****************
267 const int MAX_EXPAND_ITERATIONS = 50;
268
270 std::list<double> tmparray;
271 std::list<std::string> tmpstring;
274 std::vector<std::list<Element>*> allocated_lines;
275
299
301 template <class C>
302 bool FindAndExtend(const std::string& objectName);
304 template <class C>
305 void ExtendObject(C& object);
306
308 std::map<std::string, double> extendedNumbers;
310 std::map<std::string, std::string> extendedStrings;
312 std::map<std::string, Array*> extendedVectors;
313
316
321
323 std::list<Element> tmp_list;
324
326 std::vector<std::string> sequences;
327
329 std::map<std::string, FastList<Element>*> expandedSequences;
330
334 std::vector<std::string*> var_list;
335
338 std::set<std::set<int>> samplerFilters;
339 std::map<int, std::set<int>> samplerFilterIDToSet;
340 std::map<std::set<int>, int> setToSamplerFilterID;
341
343 std::stringstream *call_sequence_log = new std::stringstream();
344 };
345
346 template <class C, typename T>
347 void Parser::SetValue(std::string property, T value)
348 {
349 GetGlobal<C>().set_value(property, value);
350 }
351
352 template <class C>
353 double Parser::GetValue(std::string property)
354 {
355 return GetGlobal<C>().get_value(property);
356 }
357
358 template<typename T>
359 std::list<T>* Parser::ArrayToList(Array* arrayIn)
360 {
361 if (!arrayIn)
362 {return nullptr;}
363 else
364 {
365 std::list<T>* result = new std::list<T>();
366 const auto& doubleData = arrayIn->GetDataList();
367 for (auto& value : doubleData)
368 {result->push_back((T)value);}
369 return result;
370 }
371 }
372}
373
374#endif
Aperture class.
Definition aperture.h:37
Representation of arrays used in tokens.
Definition array.h:39
blm for parser.
Beam class.
Definition beam.h:43
RF CavityModel class for parser.
Definition cavitymodel.h:34
Cooling channel parameters.
Crystal class for parser.
Definition crystal.h:36
List with Efficient Lookup.
Definition fastlist.h:41
Field class for parser.
Definition field.h:36
Placement class for laser.
Definition laser.h:35
Modulator class for parser.
Definition modulator.h:36
Colour definition for parser.
Definition newcolour.h:39
Options class.
Definition options.h:43
Parser class.
Definition parser.h:81
void Overwrite(const std::string &objectName)
Overwrite object with current values.
Definition parser.cc:892
FastList< Atom > atom_list
List of parser defined instances of that object.
Definition parser.h:241
void Add()
Insert global object of parser class C in Container class.
Definition parser.cc:1182
Parser()
Default contructor.
Definition parser.cc:115
std::vector< std::string > & get_sequences()
Get names of the available sequences.
Definition parser.cc:524
std::vector< std::list< Element > * > allocated_lines
Definition parser.h:274
double property_lookup(const std::string &element_name, const std::string &property_name) const
access property of Element with element_name
Definition parser.cc:751
FastList< BLMPlacement > blm_list
List of parser defined instances of that object.
Definition parser.h:258
std::vector< std::string > sequences
Names of all defined sequences in the parser with 'line'.
Definition parser.h:326
void FillString(Array *)
Definition parser.cc:875
const int MAX_EXPAND_ITERATIONS
maximum number of nested lines
Definition parser.h:267
void quit()
Exit method.
Definition parser.cc:314
Laser laser
The one instance we fill before appending to a list.
Definition parser.h:283
Scorer scorer
The one instance we fill before appending to a list.
Definition parser.h:292
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:213
void ExtendObject(C &object)
Extend object with maps.
Definition parser.cc:968
FastList< Crystal > crystal_list
List of parser defined instances of that object.
Definition parser.h:243
FastList< Placement > placement_list
List of parser defined instances of that object.
Definition parser.h:251
Material material
The one instance we fill before appending to a list.
Definition parser.h:284
virtual ~Parser()
Destructor.
Definition parser.cc:142
Atom atom
The one instance we fill before appending to a list.
Definition parser.h:278
Symtab * symlook(const std::string &s)
look up parser symbol
Definition parser.cc:846
static Parser * Instance()
Access method.
Definition parser.cc:121
std::map< std::string, FastList< Element > * > expandedSequences
Cached copy of expanded sequences.
Definition parser.h:329
FastList< Region > region_list
List of parser defined instances of that object.
Definition parser.h:248
FastList< Modulator > modulator_list
List of parser defined instances of that object.
Definition parser.h:259
void SetValue(std::string property, T value)
Set value for parser class.
Definition parser.h:347
FastList< NewColour > colour_list
List of parser defined instances of that object.
Definition parser.h:242
Parameters params
The one instance we fill before appending to a list.
Definition parser.h:277
int add_sampler_partIDSet(std::list< int > *samplerPartIDListIn)
Definition parser.cc:641
FastList< Scorer > scorer_list
List of parser defined instances of that object.
Definition parser.h:255
FastList< Tunnel > tunnel_list
List of parser defined instances of that object.
Definition parser.h:249
FastList< Element > element_list
List of all encountered elements.
Definition parser.h:315
Container & GetList()
Get list for parser class C.
CoolingChannel coolingchannel
The one instance we fill before appending to a list.
Definition parser.h:281
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:668
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:791
Crystal crystal
The one instance we fill before appending to a list.
Definition parser.h:280
bool TryPrintingObject(const std::string &objectName) const
Definition parser.cc:1002
Options options
General options.
Definition parser.h:237
Element & find_element(const std::string &element_name)
find element
Definition parser.cc:694
FastList< Aperture > aperture_list
List of parser defined instances of that object.
Definition parser.h:257
Modulator modulator
The one instance we fill before appending to a list.
Definition parser.h:296
FastList< CoolingChannel > coolingchannel_list
List of parser defined instances of that object.
Definition parser.h:244
std::stringstream * call_sequence_log
execution log to understand call order (not an error or debug log)
Definition parser.h:343
Tunnel tunnel
The one instance we fill before appending to a list.
Definition parser.h:289
void expand_line(FastList< Element > &target, const std::string &name, const std::string &start="", const std::string &end="")
Definition parser.cc:368
FastList< PhysicsBiasing > xsecbias_list
List of parser defined instances of that object.
Definition parser.h:250
void write_table(std::string *name, ElementType type, bool isLine=false)
Method that transfers parameters to element properties.
Definition parser.cc:322
FastList< ScorerMesh > scorermesh_list
List of parser defined instances of that object.
Definition parser.h:256
const Element * find_placement_element_safe(const std::string &element_name) const
search placement_element
Definition parser.cc:723
void PrintElements() const
Print methods.
Definition parser.cc:992
FastList< Element > placement_elements
Definition parser.h:320
std::vector< std::string * > var_list
Variable vector for memory storage.
Definition parser.h:334
const FastList< Element > & get_sequence(const std::string &name, bool bExit=true)
Definition parser.cc:530
FastList< Element > beamline_list
Beamline.
Definition parser.h:239
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:760
std::set< std::set< int > > samplerFilters
Definition parser.h:338
FastList< SamplerPlacement > samplerplacement_list
List of parser defined instances of that object.
Definition parser.h:254
Field field
The one instance we fill before appending to a list.
Definition parser.h:282
std::list< Element > tmp_list
Temporary list.
Definition parser.h:323
Query query
The one instance we fill before appending to a list.
Definition parser.h:287
Aperture aperture
The one instance we fill before appending to a list.
Definition parser.h:294
void AddVariable(std::string *name)
Add variable memory to variable list for memory management.
Definition parser.cc:980
BLMPlacement blm
The one instance we fill before appending to a list.
Definition parser.h:295
std::map< std::string, std::string > extendedStrings
Map for options of type string for extending objects.
Definition parser.h:310
Region region
The one instance we fill before appending to a list.
Definition parser.h:288
std::string current_end
Name of beamline.
Definition parser.h:215
void ParseFile(FILE *f)
Parse the input file and construct beamline_list and options.
Definition parser.cc:187
bool FindAndExtend(const std::string &objectName)
Laser instance.
Definition parser.cc:953
SymbolMap symtab_map
Parser symbol map.
Definition parser.h:332
void PrintBeamline() const
Print methods.
Definition parser.cc:987
std::map< std::string, Array * > extendedVectors
Map for options of type vector for extending objects.
Definition parser.h:312
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:549
double GetValue(std::string property)
Get value for parser class (only for doubles)
Definition parser.h:353
Symtab * symcreate(const std::string &s)
create new parser symbol
Definition parser.cc:839
NewColour colour
The one instance we fill before appending to a list.
Definition parser.h:279
ScorerMesh scorermesh
The one instance we fill before appending to a list.
Definition parser.h:293
const FastList< Element > & GetBeamline() const
Definition parser.cc:1073
void PrintOptions() const
Print methods.
Definition parser.cc:997
void ClearParams()
Definition parser.cc:884
void FillArray(Array *)
Definition parser.cc:867
FastList< Field > field_list
List of parser defined instances of that object.
Definition parser.h:245
FastList< CavityModel > cavitymodel_list
List of parser defined instances of that object.
Definition parser.h:253
std::map< std::string, double > extendedNumbers
Map for options of type double for extending objects.
Definition parser.h:308
Placement placement
The one instance we fill before appending to a list.
Definition parser.h:286
std::list< double > tmparray
temporary list for reading of arrays in parser
Definition parser.h:270
CavityModel cavitymodel
The one instance we fill before appending to a list.
Definition parser.h:290
FastList< Query > query_list
List of parser defined instances of that object.
Definition parser.h:247
void Store(double value)
Add value to front of temporary list.
Definition parser.cc:853
static Parser * instance
Instance.
Definition parser.h:99
PhysicsBiasing xsecbias
The one instance we fill before appending to a list.
Definition parser.h:285
FastList< Laser > laser_list
List of parser defined instances of that object.
Definition parser.h:252
Beam beam
Beam instance;.
Definition parser.h:235
void add_func(std::string name, double(*func)(double))
Add function to parser.
Definition parser.cc:809
void Initialise()
Initialisation of parser functions and constants.
Definition parser.cc:223
FastList< Material > material_list
List of parser defined instances of that object.
Definition parser.h:246
std::string current_start
Name of beamline.
Definition parser.h:214
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:737
void expand_sequences()
Expand all sequences define with 'line' into FastLists.
Definition parser.cc:347
std::list< std::string > tmpstring
temporary list for reading of arrays in parser
Definition parser.h:271
SamplerPlacement samplerplacement
The one instance we fill before appending to a list.
Definition parser.h:291
Physics biasing class for parser.
Placement class for parser.
Definition placement.h:40
Query structure class for parser.
Definition query.h:36
Region class for parser.
Definition region.h:35
Sampler placement class for parser.
ScorerMesh class for parser.
Definition scorermesh.h:39
Scorer class for parser.
Definition scorer.h:36
Class that hold the symbol table.
Definition symbolmap.h:32
Tunnel class for parser.
Definition tunnel.h:34
Parser namespace for GMAD language. Combination of Geant4 and MAD.
ElementType
types of elements
Definition elementtype.h:27
Atom class.
Definition atom.h:35
Material class.
Definition material.h:40
Parameters - Element class with booleans.
Definition parameters.h:43