BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
parser.cc
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#include "parser.h"
19
20#include <algorithm>
21#include <cmath>
22#include <iostream>
23#include <list>
24#include <set>
25#include <string>
26#include <vector>
27
28// for getpwuid: http://linux.die.net/man/3/getpwuid
29#include <unistd.h>
30#include <sys/types.h>
31#include <pwd.h>
32
33#include "array.h"
34#include "sym_table.h"
35
36namespace {
37 // helper method
38 // replace algorithm of all substring instances
39 // from http://stackoverflow.com/questions/2896600/how-to-replace-all-occurrences-of-a-character-in-string
40 void replaceAll(std::string& source, const std::string& from, const std::string& to)
41 {
42 std::string newString;
43 newString.reserve( source.length() ); // avoids a few memory allocations
44
45 std::string::size_type lastPos = 0;
46 std::string::size_type findPos;
47
48 while( std::string::npos != ( findPos = source.find( from, lastPos )))
49 {
50 newString.append( source, lastPos, findPos - lastPos );
51 newString += to;
52 lastPos = findPos + from.length();
53 }
54
55 // Care for the rest after last occurrence
56 newString += source.substr( lastPos );
57
58 source.swap( newString );
59 }
60}
61
62namespace GMAD {
63 // Explicitly make the templates we need here
64 template void Parser::Add<Atom, FastList<Atom> >();
65 template void Parser::Add<Aperture, FastList<Aperture> >();
66 template void Parser::Add<BLMPlacement, FastList<BLMPlacement> >();
67 template void Parser::Add<CavityModel, FastList<CavityModel> >();
68 template void Parser::Add<Crystal, FastList<Crystal> >();
69 template void Parser::Add<Field, FastList<Field> >();
70 template void Parser::Add<Laser, FastList<Laser> >();
71 template void Parser::Add<Material, FastList<Material> >();
72 template void Parser::Add<Modulator, FastList<Modulator> >();
73 template void Parser::Add<NewColour, FastList<NewColour> >();
74 template void Parser::Add<PhysicsBiasing, FastList<PhysicsBiasing> >();
75 template void Parser::Add<Placement, FastList<Placement> >();
76 template void Parser::Add<Query, FastList<Query> >();
77 template void Parser::Add<Region, FastList<Region> >();
78 template void Parser::Add<SamplerPlacement, FastList<SamplerPlacement> >();
79 template void Parser::Add<Scorer, FastList<Scorer> >();
80 template void Parser::Add<ScorerMesh, FastList<ScorerMesh> >();
81 template void Parser::Add<Tunnel, FastList<Tunnel> >();
82
83 template void Parser::Add<Atom, FastList<Atom> >(bool unique, const std::string& className);
84 template void Parser::Add<Aperture, FastList<Aperture> >(bool unique, const std::string& className);
85 template void Parser::Add<BLMPlacement, FastList<BLMPlacement> >(bool unique, const std::string& className);
86 template void Parser::Add<CavityModel, FastList<CavityModel> >(bool unique, const std::string& className);
87 template void Parser::Add<CoolingChannel, FastList<CoolingChannel> >(bool unique, const std::string& className);
88 template void Parser::Add<Crystal, FastList<Crystal> >(bool unique, const std::string& className);
89 template void Parser::Add<Field, FastList<Field> >(bool unique, const std::string& className);
90 template void Parser::Add<ScorerMesh, FastList<ScorerMesh> >(bool unique, const std::string& className);
91 template void Parser::Add<Material, FastList<Material> >(bool unique, const std::string& className);
92 template void Parser::Add<Modulator, FastList<Modulator> >(bool unique, const std::string& className);
93 template void Parser::Add<NewColour, FastList<NewColour> >(bool unique, const std::string& className);
94 template void Parser::Add<PhysicsBiasing, FastList<PhysicsBiasing> >(bool unique, const std::string& className);
95 //template void Parser::Add<Placement, FastList<Placement> >(bool unique, const std::string& className);
96 template void Parser::Add<Query, FastList<Query> >(bool unique, const std::string& className);
97 template void Parser::Add<Region, FastList<Region> >(bool unique, const std::string& className);
98 template void Parser::Add<SamplerPlacement, FastList<SamplerPlacement> >(bool unique, const std::string& className);
99 template void Parser::Add<Scorer, FastList<Scorer> >(bool unique, const std::string& className);
100 template void Parser::Add<Tunnel, FastList<Tunnel> >(bool unique, const std::string& className);
101 template void Parser::Add<Laser, FastList<Laser> >(bool unique, const std::string& className);
102}
103
104using namespace GMAD;
105
106namespace GMAD {
107 extern std::string yyfilename;
108}
109
110extern int yyparse();
111extern FILE *yyin;
112
113Parser* Parser::instance = nullptr;
114
116 (*call_sequence_log) << "Parser::Parser()" << std::endl;
117
118 Initialise();
119}
120
122{
123 if (!instance)
124 {
125 std::cerr << "Parser has not been initialized!" << std::endl;
126 exit(1);
127 }
128 return instance;
129}
130
131Parser* Parser::Instance(const std::string& name)
132{
133 if(instance)
134 {
135 std::cerr << "Warning parser was already initialized!" << std::endl;
136 delete instance;
137 }
138 instance = new Parser(name);
139 return instance;
140}
141
143{
144 (*call_sequence_log) << "Parser::~Parser()" << std::endl;
145
146 beamline_list.erase();
147 // delete allocated lines
148 for (auto element : allocated_lines)
149 {delete element;}
150
151 instance = nullptr;
152}
153
154Parser::Parser(std::string name)
155{
156 (*call_sequence_log) << "Parser::Parser(" << name << ")" << std::endl;
157
158 instance = this;
159#ifdef BDSDEBUG
160 std::cout << "gmad_parser> opening file" << std::endl;
161#endif
162 // replace all ~ symbols with home directory to allow for that
163 // note $HOME is not necessarily equivalent to ~
164 // see http://linux.die.net/man/3/getpwuid
165 std::string tilde("~");
166 std::string home(getpwuid(getuid())->pw_dir);
167
168 replaceAll(name,tilde,home);
169
170 FILE *f = fopen(name.c_str(),"r");
171
172 if(f==nullptr)
173 {
174 std::cerr << "gmad_parser> Can't open input file " << name << std::endl;
175 exit(1);
176 }
177 // set global string for parser
178 yyfilename = std::string(name);
179
180 Initialise();
181
182 std::cout.precision(10); // set output precision to 10 decimals
183
184 ParseFile(f);
185}
186
187void Parser::ParseFile(FILE *f)
188{
189 (*call_sequence_log) << "Parser::ParseFile(f=" << f << ")" << std::endl;
190
191 yyin=f;
192
193#ifdef BDSDEBUG
194 std::cout << "gmad_parser> beginning to parse file" << std::endl;
195#endif
196
197 while(!feof(yyin))
198 {yyparse();}
199
201#ifdef BDSDEBUG
202 std::cout << "gmad_parser> finished to parsing file" << std::endl;
203#endif
204 // clear temporary stuff
205#ifdef BDSDEBUG
206 std::cout << "gmad_parser> clearing temporary lists" << std::endl;
207#endif
208 element_list.clear();
209 tmp_list.clear();
211 for(auto it : var_list)
212 {delete it;}
213
214#ifdef BDSDEBUG
215 std::cout << "gmad_parser> finished" << std::endl;
216#endif
217
218 fclose(f);
219
220 // std::cout << call_sequence_log->str() << std::endl;
221}
222
224{
225 (*call_sequence_log) << "Parser::Initialise()" << std::endl;
226
227 const int reserved = 1;
228 // embedded arithmetical functions
229 add_func("sqrt",std::sqrt);
230 add_func("cos",std::cos);
231 add_func("sin",std::sin);
232 add_func("exp",std::exp);
233 add_func("log",std::log);
234 add_func("tan",std::tan);
235 add_func("asin",std::asin);
236 add_func("acos",std::acos);
237 add_func("atan",std::atan);
238 add_func("abs",std::abs);
239
240 add_var("pi", 4.0*std::atan(1),reserved);
241 add_var("twopi", 8.0*std::atan(1),reserved);
242 add_var("halfpi", 2.0*std::atan(1),reserved);
243
244 add_var("PeV",1e6, reserved);
245 add_var("TeV",1e3, reserved);
246 add_var("GeV",1.0 ,reserved);
247 add_var("MeV",1e-3,reserved);
248 add_var("keV",1e-6,reserved);
249 add_var("KeV",1e-6,reserved); // for compatibility
250 add_var("eV" ,1e-9,reserved);
251
252 add_var("PJ", 1e12, reserved);
253 add_var("GJ", 1e9, reserved);
254 add_var("MJ", 1e6, reserved);
255 add_var("kJ", 1e3, reserved);
256 add_var("J", 1, reserved);
257 add_var("mJ", 1e-3, reserved);
258 add_var("uJ", 1e-6, reserved);
259 add_var("nJ", 1e-9, reserved);
260 add_var("pJ", 1e-12, reserved);
261
262 add_var("mV",1e-3,reserved);
263 add_var("V" ,1.0, reserved);
264 add_var("kV",1e+3,reserved);
265 add_var("MV",1e+6,reserved);
266 add_var("GV",1e+9,reserved);
267
268 add_var("Tesla",1.0,reserved);
269 add_var("T", 1.0,reserved);
270
271 add_var("km" ,1e3 ,reserved);
272 add_var("m" ,1.0 ,reserved);
273 add_var("cm" ,1e-2,reserved);
274 add_var("mm" ,1e-3,reserved);
275 add_var("um" ,1e-6,reserved);
276 add_var("mum",1e-6,reserved);
277 add_var("nm" ,1e-9,reserved);
278 add_var("ang",1e-10,reserved);
279 add_var("pm" ,1e-12,reserved);
280
281 add_var("s" ,1.0 ,reserved);
282 add_var("ms" ,1.e-3,reserved);
283 add_var("us" ,1.e-6,reserved);
284 add_var("ns" ,1.e-9,reserved);
285 add_var("ps" ,1.e-12,reserved);
286
287 add_var("Hz" ,1.0, reserved);
288 add_var("kHz",1e+3, reserved);
289 add_var("MHz",1e+6, reserved);
290 add_var("GHz",1e+9, reserved);
291 add_var("THz",1e+12,reserved);
292
293 add_var("rad" ,1.0, reserved);
294 add_var("mrad",1e-3,reserved);
295 add_var("urad",1e-6,reserved);
296 add_var("nrad",1e-9,reserved);
297
298 add_var("degrees",std::atan(1)/45,reserved);
299
300 add_var("clight",2.99792458e+8,reserved);
301
302 add_var("kg_per_cubic_m", 1e-3, reserved);
303 add_var("g_per_cubic_m", 1e-6, reserved);
304 add_var("g_per_cubic_cm", 1, reserved);
305
306 add_var("pascal", 1/101324.99987983676, reserved);
307 add_var("bar", 1/1.0132499987983676, reserved);
308 add_var("torr", 0.00131579, reserved);
309 add_var("atmosphere", 1, reserved);
310
311 params.flush();
312}
313
315{
316 (*call_sequence_log) << "Parser::quit()" << std::endl;
317
318 std::cout << "parsing complete..." << std::endl;
319 exit(0);
320}
321
322void Parser::write_table(std::string* name, ElementType type, bool isLine)
323{
324 (*call_sequence_log) << "Parser::write_table(name=" << *name
325 << ",type=" << type
326 << ",isLine=" << isLine << ")" << std::endl;
327 Element e;
328 e.set(params,*name,type);
329
330 (*call_sequence_log) << "Parser::write_table> name=" << e.name << std::endl;
331 (*call_sequence_log) << "Parser::write_table> type=" << e.type << std::endl;
332 (*call_sequence_log) << "Parser::write_table> l=" << e.l << std::endl;
333
334 if (isLine)
335 {
336 e.lst = new std::list<Element>(tmp_list);
337 allocated_lines.push_back(e.lst);
338 // clean list
339 tmp_list.clear();
340 sequences.push_back(*name); // append to all sequence definitions
341 }
342
343 // insert element with uniqueness requirement
344 element_list.push_back(e,true);
345}
346
348{
349 (*call_sequence_log) << "Parser::expand_sequences" << std::endl;
350
351 for (const auto& name : sequences)
352 {
353 FastList<Element>* newLine = new FastList<Element>();
354 expand_line(*newLine, name);
355 expandedSequences[name] = newLine;
356 }
357}
358
359void Parser::expand_line(const std::string& name,
360 const std::string& start,
361 const std::string& end)
362{
363 (*call_sequence_log) << "Parser::expand_line(name=" << name << ",start=" << start << ",end=" << end << ")" << std::endl;
364
365 expand_line(beamline_list, name, start, end);
366}
367
369 const std::string& name,
370 const std::string& start,
371 const std::string& end)
372{
373 (*call_sequence_log) << "Parser::expand_line(target=" << target.size() << ",name=" << name << ",start="
374 << start << " end=" << end << ")" << std::endl;
375
376 const Element& line = find_element(name);
377 if(line.type != ElementType::_LINE && line.type != ElementType::_REV_LINE )
378 {
379 std::cerr << "Error with use command: \"" << name << "\" is not a line" << std::endl;
380 exit(1);
381 }
382
383 // delete the previous beamline
384 target.clear();
385
386 // expand the desired beamline
387 Element e;
388 e.type = line.type;
389 e.name = name;
390 e.l = 0;
391 e.lst = nullptr;
392
393 target.push_back(e);
394
395#ifdef BDSDEBUG
396 std::cout << "expanding line " << name << ", range = " << start << end << std::endl;
397#endif
398 if (!line.lst)
399 {return;} //list empty
400
401 // first expand the whole range
402 std::list<Element>::iterator sit = line.lst->begin();
403 std::list<Element>::iterator eit = line.lst->end();
404
405 // copy the list into the resulting list
406 switch(line.type)
407 {
408 case ElementType::_LINE:
409 {target.insert(target.end(),sit,eit); break;}
410 case ElementType::_REV_LINE:
411 {target.insert(target.end(),line.lst->rbegin(),line.lst->rend()); break;}
412 default:
413 {target.insert(target.end(),sit,eit); break;}
414 }
415 // bool to check if beamline is fully expanded
416 bool is_expanded = false;
417
418 // parse starting from the second element until the list is expanded
419 int iteration = 0;
420 while (!is_expanded)
421 {
422 is_expanded = true;
423 // start at second element
424 std::list<Element>::iterator it = ++target.begin();
425 for (; it!=target.end(); ++it)
426 {
427 Element& element = *it; // alias
428 const ElementType& type = element.type;
429#ifdef BDSDEBUG
430 std::cout << element.name << " , " << type << std::endl;
431#endif
432 // if list - expand further
433 if (type != ElementType::_LINE && type != ElementType::_REV_LINE)
434 {continue;}
435 is_expanded = false;
436 // lookup the line in main list
437 std::list<Element>::const_iterator tmpit = element_list.find(element.name);
438 std::list<Element>::const_iterator iterEnd = element_list.end();
439 if ( (tmpit != iterEnd) && ( (*tmpit).lst != nullptr) )
440 { // sublist found and not empty
441 const Element& list = *tmpit; // alias
442#ifdef BDSDEBUG
443 std::cout << "inserting sequence for " << element.name << " - " << list.name << " ...";
444#endif
445 if (type == ElementType::_LINE)
446 {target.insert(it,list.lst->begin(),list.lst->end());}
447 else if (type == ElementType::_REV_LINE)
448 {
449 //iterate over list and invert any sublines contained within. SPM
450 std::list<Element> tmpList;
451 tmpList.insert(tmpList.end(),list.lst->begin(),list.lst->end());
452 for (std::list<Element>::iterator itLineInverter = tmpList.begin();
453 itLineInverter != tmpList.end(); ++itLineInverter)
454 {
455 if ( (*itLineInverter).type == ElementType::_LINE)
456 {(*itLineInverter).type = ElementType::_REV_LINE;}
457 else if ((*itLineInverter).type == ElementType::_REV_LINE)
458 {(*itLineInverter).type = ElementType::_LINE;}
459 }
460 target.insert(it,tmpList.rbegin(),tmpList.rend());
461 }
462#ifdef BDSDEBUG
463 std::cout << "inserted" << std::endl;
464#endif
465 // delete the list pointer
466 target.erase(it--);
467 }
468 else if ( tmpit != iterEnd )
469 { // entry points to a scalar element type -
470 //transfer properties from the main list
471#ifdef BDSDEBUG
472 std::cout << "keeping element..." << element.name << std::endl;
473#endif
474 // copy properties
475 element = (*tmpit);
476
477#ifdef BDSDEBUG
478 std::cout << "done" << std::endl;
479#endif
480 }
481 else
482 { // element of undefined type
483 std::cerr << "Error : Expanding line \"" << name << "\" : element \"" << element.name
484 << "\" has not been defined! " << std::endl;
485 exit(1);
486 }
487 }
488 iteration++;
489 if ( iteration > MAX_EXPAND_ITERATIONS )
490 {
491 std::cerr << "Error : Line expansion of '" << name << "' seems to loop, " << std::endl
492 << "possible recursive line definition, quitting" << std::endl;
493 exit(1);
494 }
495 }// while
496
497 // leave only the desired range
498 //
499 // rule - from first occurrence of 'start' till first 'end' coming after 'start'
500
501 if ( !start.empty()) // determine the start element
502 {
503 std::list<Element>::const_iterator startIt = target.find(std::string(start));
504
505 if(startIt!=target.end())
506 {target.erase(target.begin(),startIt);}
507 }
508
509 if ( !end.empty()) // determine the end element
510 {
511 std::list<Element>::const_iterator endIt = target.find(std::string(end));
512
513 if(endIt!=target.end())
514 {target.erase(++endIt,target.end());}
515 }
516
517 // insert the tunnel if present
518
519 std::list<Element>::iterator itTunnel = element_list.find("tunnel");
520 if (itTunnel!=element_list.end())
521 {target.push_back(*itTunnel);}
522}
523
524std::vector<std::string>& Parser::get_sequences() {
525 (*call_sequence_log) << "Parser::get_sequences()" << std::endl;
526
527 return sequences;
528}
529
530const FastList<Element>& Parser::get_sequence(const std::string& name, bool bExit)
531{
532 (*call_sequence_log) << "Parser::get_sequence(name=" << name << ")" << std::endl;
533
534 // search for previously queried beamlines
535 const auto search = expandedSequences.find(name);
536 if (search != expandedSequences.end())
537 {return *(search->second);}
538 else
539 {
540 std::cerr << "parser> no such sequence \"" << name << "\"" << std::endl;
541 if(bExit)
542 {exit(1);}
543 else {
544 throw std::runtime_error("parser> no such sequence "+name);
545 }
546 }
547}
548
549void Parser::set_sampler(const std::string& name,
550 int count,
551 ElementType type,
552 const std::string& samplerType,
553 double samplerRadius,
554 int particleSetID)
555{
556 (*call_sequence_log) << "Parser::set_sampler(name=" << name
557 << ", count=" << count
558 << ", ElementType=" << type
559 << ", samplerType=" << samplerType
560 << ", samplerRadius=" << samplerRadius
561 << ", particleSetID=" << particleSetID << ")" << std::endl;
562
563 // if count equal to -2 add to all elements regardless of name
564 // typically used for output elements like samplers
565 // skip first element and add one at the end
566 if (count == -2)
567 {
568 for (auto it = beamline_list.begin(); it != beamline_list.end(); ++it)
569 {// skip LINEs
570 if((*it).type == ElementType::_LINE || (*it).type == ElementType::_REV_LINE)
571 {continue;}
572 // if type not equal to NONE and elements have to match type
573 if (type != ElementType::_NONE && type != (*it).type)
574 {continue;}
575
576 (*it).setSamplerInfo(samplerType,(*it).name,samplerRadius,particleSetID);
577 }
578 }
579 else if (count == -1) // if count equal to -1 add sampler to all element instances
580 {
581 auto itPair = beamline_list.equal_range(name);
582 if (itPair.first == itPair.second)
583 {
584 std::string msg = "parser> SetSampler> current beamline doesn't contain element \"" + name + "\"";
585 yyerror2(msg.c_str());
586 }
587 for (auto it = itPair.first; it != itPair.second; ++it)
588 {
589 // if sampler is attached to a marker, really attach it to the previous element with the name of marker
590 auto elementIt = (it->second);
591 std::string samplerName = elementIt->name;
592 if ((*elementIt).type == ElementType::_MARKER)
593 {
594 // need to find real element before
595 // but careful not to go beyond first element also!
596 while ((*elementIt).isSpecial())
597 {
598 elementIt--;
599 // have to break first before continue since in while loop
600 if (elementIt == beamline_list.begin())
601 {break;}
602 }
603
604 if (elementIt==beamline_list.begin())
605 {
606 std::cout << "parser> SetSampler> WARNING: no element before marker " << name << ", no sampler added" << std::endl;
607 continue;
608 }
609 }
610 (*elementIt).setSamplerInfo(samplerType,samplerName,samplerRadius,particleSetID);
611 }
612 }
613 else
614 {
615 auto it = beamline_list.find(name,count);
616 if (it==beamline_list.end())
617 {
618 std::string msg = "parser> SetSampler> current beamline doesn't contain element \"" + name + "\" with number " + std::to_string(count);
619 yyerror2(msg.c_str());
620 }
621 // if sampler is attached to a marker, really attach it to the previous element with the name of marker
622 std::string samplerName = (*it).name;
623 if ((*it).type == ElementType::_MARKER)
624 {
625 // need to find real element before
626 // but careful not to go beyond first element also!
627 while ((*it).isSpecial())
628 {
629 it--;
630 if (it == beamline_list.begin())
631 {
632 std::cout << "parser> SetSampler> WARNING: no element before marker " << name << ", no sampler added" << std::endl;
633 return;
634 }
635 }
636 }
637 (*it).setSamplerInfo(samplerType,samplerName,samplerRadius,particleSetID);
638 }
639}
640
641int Parser::add_sampler_partIDSet(std::list<int>* samplerPartIDListIn)
642{
643 (*call_sequence_log) << "Parser::add_sampler_partIDSet(samplerPartIDListIn=" << samplerPartIDListIn << ")" << std::endl;
644 if(samplerPartIDListIn != nullptr)
645 {
646 (*call_sequence_log) << " samplerPartIDListIn.size=" << samplerPartIDListIn->size() << std::endl;
647 }
648 else {
649 (*call_sequence_log) << " samplerPartIDListIn=nullptr" << std::endl;
650 }
651
652 if (!samplerPartIDListIn)
653 {return -1;}
654 std::set<int> partIDs = std::set<int>(std::begin(*samplerPartIDListIn), std::end(*samplerPartIDListIn));
655 auto alreadyExists = samplerFilters.count(partIDs);
656 if (alreadyExists > 0)
657 {return setToSamplerFilterID[partIDs];}
658 else
659 {
660 int particleSetID = (int) samplerFilterIDToSet.size();
661 samplerFilterIDToSet[particleSetID] = partIDs;
662 setToSamplerFilterID[partIDs] = particleSetID;
663 samplerFilters.insert(partIDs);
664 return particleSetID;
665 }
666}
667
668void Parser::add_sampler(const std::string& name, int count, ElementType type,
669 std::string samplerType, std::list<int>* samplerPartIDListIn)
670{
671#ifdef BDSDEBUG
672 std::cout << "inserting sampler " << name;
673 if (count>=0)
674 {std::cout << "[" << count << "]";}
675 std::cout << std::endl;
676#endif
677
678 (*call_sequence_log) << "Parser::add_sampler(name=" << name
679 << ", count=" << count
680 << ", type=" << type
681 << ", samplerType=" << samplerType;
682 if(samplerPartIDListIn != nullptr)
683 {
684 (*call_sequence_log) << " samplerPartIDListIn.size=" << samplerPartIDListIn->size() << ")" << std::endl;
685 }
686 else {
687 (*call_sequence_log) << " samplerPartIDListIn.size=nullptr" << ")" << std::endl;
688 }
689
690 int particleSetID = add_sampler_partIDSet(samplerPartIDListIn);
691 set_sampler(name,count,type,samplerType,0,particleSetID);
692}
693
694Element& Parser::find_element(const std::string& element_name)
695{
696 (*call_sequence_log) << "Parser::find_element(element_name=" << element_name << ")" << std::endl;
697
698 std::list<Element>::iterator it = element_list.find(element_name);
699 std::list<Element>::const_iterator iterEnd = element_list.end();
700
701 if(it == iterEnd)
702 {
703 std::cerr << "parser.h> Error: element (type) \"" << element_name
704 << "\" has not been defined." << std::endl;
705 exit(1);
706 }
707 return (*it);
708}
709
710const Element& Parser::find_element(const std::string& element_name)const
711{
712 (*call_sequence_log) << "Parser::find_element(element_name=" << element_name << ")" << std::endl;
713
714 auto search = element_list.find(element_name);
715 if (search == element_list.end())
716 {
717 std::cerr << "parser.h> Error: unknown element \"" << element_name << "\"." << std::endl;
718 exit(1);
719 }
720 return (*search);
721}
722
723const Element* Parser::find_placement_element_safe(const std::string& element_name) const
724{
725 (*call_sequence_log) << "Parser::find_placement_element_safe(element_name=" << element_name << ")" << std::endl;
726
727 const Element* result = nullptr;
728 auto search = placement_elements.find(element_name);
729 if (search != placement_elements.end())
730 {
731 const GMAD::Element& ele = *search;
732 result = &ele;
733 }
734 return result;
735}
736
737const Element* Parser::find_element_safe(const std::string& element_name) const
738{
739 (*call_sequence_log) << "Parser::find_element_safe(element_name=" << element_name << ")" << std::endl;
740
741 const Element* result = nullptr;
742 auto search = element_list.find(element_name);
743 if (search != element_list.end())
744 {
745 const GMAD::Element& ele = *search;
746 result = &ele;
747 }
748 return result;
749}
750
751double Parser::property_lookup(const std::string& element_name, const std::string& property_name)const
752{
753 (*call_sequence_log) << "Parser::property_lookup(element_name=" << element_name
754 << ", property_name=" << property_name << ")" << std::endl;
755
756 const Element& element = find_element(element_name);
757 return element.property_lookup(property_name);
758}
759
760void Parser::add_element_temp(const std::string& name, int number, bool pushfront, ElementType linetype)
761{
762#ifdef BDSDEBUG
763 std::cout << "matched sequence element, " << name;
764 if (number > 1)
765 {std::cout << " * " << number;}
766 std::cout << std::endl;
767#endif
768
769 (*call_sequence_log) << "Parser::add_element_temp(name=" << name
770 << ", number=" << number
771 << ", pushfront=" << pushfront
772 << ", linetype=" << linetype << ")" << std::endl;
773
774 // add to temporary element sequence
775 Element e;
776 e.name = name;
777 e.type = linetype;
778 e.lst = nullptr;
779 if (pushfront)
780 {
781 for (int i = 0; i < number; i++)
782 {tmp_list.push_front(e);}
783 }
784 else
785 {
786 for (int i = 0; i < number; i++)
787 {tmp_list.push_back(e);}
788 }
789}
790
791int Parser::copy_element_to_params(const std::string& elementName)
792{
793 int type;
794#ifdef BDSDEBUG
795 std::cout << "newinstance : VARIABLE -- " << elementName << std::endl;
796#endif
797
798 (*call_sequence_log) << "Parser::copy_element_to_params(elementName=" << elementName << ")" << std::endl;
799
800 const Element& element = find_element(elementName);
801
802 // inherit properties from the base type
803 type = static_cast<int>(element.type);
804 params.inherit_properties(element);
805
806 return type;
807}
808
809void Parser::add_func(std::string name, double (*func)(double))
810{
811 (*call_sequence_log) << "Parser::add_func(name=" << name
812 << ", func=" << func << ")" << std::endl;
813
814 Symtab *sp=symtab_map.symcreate(name);
815 sp->Set(func);
816}
817
818void Parser::add_var(std::string name, double value, int is_reserved)
819{
820 (*call_sequence_log) << "Parser::add_var(name=" << name
821 << ", value=" << value
822 << ", is_reserved=" << is_reserved << ")" << std::endl;
823
824 Symtab* sp = symtab_map.symcreate(name);
825 sp->Set(value,is_reserved);
826}
827
828bool Parser::InvalidSymbolName(const std::string& s, std::string& errorReason)
829{
830 (*call_sequence_log) << "Parser::InvalidSymbolName(s=" << s
831 << ", errorReason=" << errorReason << ")" << std::endl;
832
833 bool result = false;
834 if (options.NameExists(s))
835 {result = true; errorReason = "The variable name \"" + s + "\" is an option name and cannot be used as a variable name";}
836 return result;
837}
838
839Symtab * Parser::symcreate(const std::string& s)
840{
841 (*call_sequence_log) << "Parser::symcreate(s=" << s << ")" << std::endl;
842
843 return symtab_map.symcreate(s);
844}
845
846Symtab * Parser::symlook(const std::string& s)
847{
848 (*call_sequence_log) << "Parser::symlook(s=" << s << ")" << std::endl;
849
850 return symtab_map.symlook(s);
851}
852
853void Parser::Store(double value)
854{
855 (*call_sequence_log) << "Parser::Store(s=" << value << ")" << std::endl;
856
857 tmparray.push_front(value);
858}
859
860void Parser::Store(const std::string& name)
861{
862 (*call_sequence_log) << "Parser::Store(name=" << name << ")" << std::endl;
863
864 tmpstring.push_front(name);
865}
866
868{
869 (*call_sequence_log) << "Parser::FillArray(array.size=" << array->GetData().size() << ")" << std::endl;
870
871 array->Copy(tmparray);
872 tmparray.clear();
873}
874
876{
877
878 (*call_sequence_log) << "Parser::FillString(array.size=" << array->GetData().size() << ")" << std::endl;
879
880 array->Copy(tmpstring);
881 tmpstring.clear();
882}
883
885{
886 (*call_sequence_log) << "Parser::ClearParams()" << std::endl;
887
888 params.flush();
889 samplerFilters.clear();
890}
891
892void Parser::Overwrite(const std::string& objectName)
893{
894 (*call_sequence_log) << "Parser::Overwrite(objectName=" << objectName << ")" << std::endl;
895
896 // find object and set values
897
898 // possible object types are:
899 // element, atom, colour, crystal, coolingchannel, field, laser, material, physicsbiasing, placement,
900 // query, region, tunnel, cavitymodel, samplerplacement, aperture, scorer, scorermesh, blm
901 bool extended = false;
902 auto element_it = element_list.find(objectName);
903 if (element_it != element_list.end())
904 {
905 ExtendObject(*element_it);
906 extended = true;
907 }
908 else
909 {
910 auto it = xsecbias_list.find(objectName);
911 if (it != xsecbias_list.end() )
912 {
913 ExtendObject(*it);
914 extended = true;
915 }
916 }
917 // vectors
918 if (!extended) {
919 if ( (extended = FindAndExtend<Atom> (objectName)) ) {}
920 else if ( (extended = FindAndExtend<NewColour> (objectName)) ) {}
921 else if ( (extended = FindAndExtend<Crystal> (objectName)) ) {}
922 else if ( (extended = FindAndExtend<CoolingChannel> (objectName)) ) {}
923 else if ( (extended = FindAndExtend<Field> (objectName)) ) {}
924 else if ( (extended = FindAndExtend<Material> (objectName)) ) {}
925 else if ( (extended = FindAndExtend<Placement> (objectName)) ) {}
926 else if ( (extended = FindAndExtend<Query> (objectName)) ) {}
927 else if ( (extended = FindAndExtend<Region> (objectName)) ) {}
928 else if ( (extended = FindAndExtend<Tunnel> (objectName)) ) {}
929 else if ( (extended = FindAndExtend<CavityModel>(objectName)) ) {}
930 else if ( (extended = FindAndExtend<SamplerPlacement>(objectName)) ) {}
931 else if ( (extended = FindAndExtend<Scorer> (objectName)) ) {}
932 else if ( (extended = FindAndExtend<ScorerMesh> (objectName)) ) {}
933 else if ( (extended = FindAndExtend<Aperture> (objectName)) ) {}
934 else if ( (extended = FindAndExtend<BLMPlacement> (objectName)) ) {}
935 else if ( (extended = FindAndExtend<Modulator> (objectName)) ) {}
936 else if ( (extended = FindAndExtend<Laser> (objectName)) ) {}
937 }
938
939 if (!extended)
940 {
941 std::cerr << "parser.h> Error: object \"" << objectName
942 << "\" has not been defined and can't be extended." << std::endl;
943 exit(1);
944 }
945
946 // clear maps
947 extendedNumbers.clear();
948 extendedStrings.clear();
949 extendedVectors.clear();
950}
951
952template <class C>
953bool Parser::FindAndExtend(const std::string& objectName)
954{
955 (*call_sequence_log) << "Parser::FindAndExtend(objectName=" << objectName << ")" << std::endl;
956
957 GMAD::FastList<C>& fl = GetList<C>();
958 auto search = fl.find(objectName);
959 if (search != fl.end())
960 {
961 ExtendObject(*search);
962 return true;
963 }
964 return false;
965}
966
967template<class C>
968void Parser::ExtendObject(C& object)
969{
970 (*call_sequence_log) << "Parser::Extend(<C>object=" << &object << ")" << std::endl;
971
972 for (auto& option : extendedNumbers)
973 {object.set_value(option.first, option.second);}
974 for (auto& option : extendedStrings)
975 {object.set_value(option.first, option.second);}
976 for (auto& option : extendedVectors)
977 {object.set_value(option.first, option.second);}
978}
979
980void Parser::AddVariable(std::string* name)
981{
982 (*call_sequence_log) << "Parser::AddVariable(name=" << *name << ")" << std::endl;
983
984 var_list.push_back(name);
985}
986
988{
989 beamline_list.print();
990}
991
993{
994 element_list.print();
995}
996
998{
999 options.print();
1000}
1001
1002bool Parser::TryPrintingObject(const std::string& objectName) const
1003{
1004 // We just don't know the type of the object, only the name, so we must
1005 // search each member vector. Try to optimise by returning once done.
1006 // This is a cpu-heavy solution vs a memory-heavy one that would have to
1007 // keep a duplicate copy of all objects for printing.
1008
1009 const std::string& on = objectName; // shortcut
1010
1011 // we use a lambda to compare against obj.name instead of obj itself
1012 auto searchAtom = std::find_if(atom_list.begin(), atom_list.end(), [&on](const Atom& obj) {return obj.name == on;});
1013 if (searchAtom != atom_list.end())
1014 {searchAtom->print(); return true;}
1015 auto searchNewColour = std::find_if(colour_list.begin(), colour_list.end(), [&on](const NewColour& obj) {return obj.name == on;});
1016 if (searchNewColour != colour_list.end())
1017 {searchNewColour->print(); return true;}
1018 auto searchCrystal = std::find_if(crystal_list.begin(), crystal_list.end(), [&on](const Crystal& obj) {return obj.name == on;});
1019 if (searchCrystal != crystal_list.end())
1020 {searchCrystal->print(); return true;}
1021 auto searchCoolingChannel = std::find_if(coolingchannel_list.begin(), coolingchannel_list.end(), [&on](const CoolingChannel& obj) {return obj.name == on;});
1022 if (searchCoolingChannel != coolingchannel_list.end())
1023 {searchCoolingChannel->print(); return true;}
1024 auto searchField = std::find_if(field_list.begin(), field_list.end(), [&on](const Field& obj) {return obj.name == on;});
1025 if (searchField != field_list.end())
1026 {searchField->print(); return true;}
1027 auto searchLaser = std::find_if(laser_list.begin(), laser_list.end(), [&on](const Laser& obj) {return obj.name == on;});
1028 if (searchLaser != laser_list.end())
1029 {searchLaser->print(); return true;}
1030 auto searchMaterial = std::find_if(material_list.begin(), material_list.end(), [&on](const Material& obj) {return obj.name == on;});
1031 if (searchMaterial != material_list.end())
1032 {searchMaterial->print(); return true;}
1033 auto searchQuery = std::find_if(query_list.begin(), query_list.end(), [&on](const Query& obj) {return obj.name == on;});
1034 if (searchQuery != query_list.end())
1035 {searchQuery->print(); return true;}
1036 auto searchRegion = std::find_if(region_list.begin(), region_list.end(), [&on](const Region& obj) {return obj.name == on;});
1037 if (searchRegion != region_list.end())
1038 {searchRegion->print(); return true;}
1039 auto searchTunnel = std::find_if(tunnel_list.begin(), tunnel_list.end(), [&on](const Tunnel& obj) {return obj.name == on;});
1040 if (searchTunnel != tunnel_list.end())
1041 {searchTunnel->print(); return true;}
1042 auto searchXsecbias = std::find_if(xsecbias_list.begin(), xsecbias_list.end(), [&on](const PhysicsBiasing& obj) {return obj.name == on;});
1043 if (searchXsecbias != xsecbias_list.end())
1044 {searchXsecbias->print(); return true;}
1045 auto searchPlacement = std::find_if(placement_list.begin(), placement_list.end(), [&on](const Placement& obj) {return obj.name == on;});
1046 if (searchPlacement != placement_list.end())
1047 {searchPlacement->print(); return true;}
1048 auto searchCavityModel = std::find_if(cavitymodel_list.begin(), cavitymodel_list.end(), [&on](const CavityModel& obj) {return obj.name == on;});
1049 if (searchCavityModel != cavitymodel_list.end())
1050 {searchCavityModel->print(); return true;}
1051 auto searchSamplerPlacement = std::find_if(samplerplacement_list.begin(), samplerplacement_list.end(), [&on](const SamplerPlacement& obj) {return obj.name == on;});
1052 if (searchSamplerPlacement != samplerplacement_list.end())
1053 {searchSamplerPlacement->print(); return true;}
1054 auto searchScorer = std::find_if(scorer_list.begin(), scorer_list.end(), [&on](const Scorer& obj) {return obj.name == on;});
1055 if (searchScorer != scorer_list.end())
1056 {searchScorer->print(); return true;}
1057 auto searchScorerMesh = std::find_if(scorermesh_list.begin(), scorermesh_list.end(), [&on](const ScorerMesh& obj) {return obj.name == on;});
1058 if (searchScorerMesh != scorermesh_list.end())
1059 {searchScorerMesh->print(); return true;}
1060 auto searchAperture = std::find_if(aperture_list.begin(), aperture_list.end(), [&on](const Aperture& obj) {return obj.name == on;});
1061 if (searchAperture != aperture_list.end())
1062 {searchAperture->print(); return true;}
1063 auto searchBLMPlacement = std::find_if(blm_list.begin(), blm_list.end(), [&on](const BLMPlacement& obj) {return obj.name == on;});
1064 if (searchBLMPlacement != blm_list.end())
1065 {searchBLMPlacement->print(); return true;}
1066 auto searchModulator = std::find_if(modulator_list.begin(), modulator_list.end(), [&on](const Modulator& obj) {return obj.name == on;});
1067 if (searchModulator != modulator_list.end())
1068 {searchModulator->print(); return true;}
1069
1070 return false;
1071}
1072
1074{
1075 (*call_sequence_log) << "Parser::GetBeamline()" << std::endl;
1076
1077 return beamline_list;
1078}
1079
1080// template specialisation
1081// put explicitly in namespace since g++ complains
1082namespace GMAD {
1083
1084 template<> Aperture& Parser::GetGlobal() {return aperture;}
1085 template<> Aperture* Parser::GetGlobalPtr(){return &aperture;}
1086 template<> FastList<Aperture>& Parser::GetList<Aperture>() {return aperture_list;}
1087
1088 template<> Atom& Parser::GetGlobal(){return atom;}
1089 template<> Atom* Parser::GetGlobalPtr(){return &atom;}
1090 template<> FastList<Atom>& Parser::GetList<Atom>(){return atom_list;}
1091
1092 template<> Beam& Parser::GetGlobal(){return beam;}
1093 template<> Beam* Parser::GetGlobalPtr(){return &beam;}
1094
1095 template<> BLMPlacement& Parser::GetGlobal() {return blm;}
1096 template<> BLMPlacement* Parser::GetGlobalPtr() {return &blm;}
1097 template<> FastList<BLMPlacement>& Parser::GetList<BLMPlacement>() {return blm_list;}
1098
1099 template<> CavityModel& Parser::GetGlobal(){return cavitymodel;}
1100 template<> CavityModel* Parser::GetGlobalPtr(){return &cavitymodel;}
1101 template<> FastList<CavityModel>& Parser::GetList<CavityModel>(){return cavitymodel_list;}
1102
1103 template<> CoolingChannel& Parser::GetGlobal(){return coolingchannel;}
1104 template<> CoolingChannel* Parser::GetGlobalPtr(){return &coolingchannel;}
1105 template<> FastList<CoolingChannel>& Parser::GetList<CoolingChannel>(){return coolingchannel_list;}
1106
1107 template<> NewColour& Parser::GetGlobal(){return colour;}
1108 template<> NewColour* Parser::GetGlobalPtr(){return &colour;}
1109 template<> FastList<NewColour>& Parser::GetList<NewColour>(){return colour_list;}
1110
1111 template<> Crystal& Parser::GetGlobal(){return crystal;}
1112 template<> Crystal* Parser::GetGlobalPtr(){return &crystal;}
1113 template<> FastList<Crystal>& Parser::GetList<Crystal>(){return crystal_list;}
1114
1115 template<> Field& Parser::GetGlobal(){return field;}
1116 template<> Field* Parser::GetGlobalPtr(){return &field;}
1117 template<> FastList<Field>& Parser::GetList<Field>(){return field_list;}
1118
1119 template<> Laser& Parser::GetGlobal(){return laser;}
1120 template<> Laser* Parser::GetGlobalPtr(){return &laser;}
1121 template<> FastList<Laser>& Parser::GetList<Laser>(){return laser_list;}
1122
1123 template<> Material& Parser::GetGlobal(){return material;}
1124 template<> Material* Parser::GetGlobalPtr(){return &material;}
1125 template<> FastList<Material>& Parser::GetList<Material>(){return material_list;}
1126
1127 template<> Modulator& Parser::GetGlobal() {return modulator;}
1128 template<> Modulator* Parser::GetGlobalPtr() {return &modulator;}
1129 template<> FastList<Modulator>& Parser::GetList<Modulator>() {return modulator_list;}
1130
1131 template<> Options& Parser::GetGlobal(){return options;}
1132 template<> Options* Parser::GetGlobalPtr(){return &options;}
1133
1134 template<> Parameters& Parser::GetGlobal(){return params;}
1135 template<> Parameters* Parser::GetGlobalPtr(){return &params;}
1136
1137 template<> Placement& Parser::GetGlobal(){return placement;}
1138 template<> Placement* Parser::GetGlobalPtr(){return &placement;}
1139 template<> FastList<Placement>& Parser::GetList<Placement>(){return placement_list;}
1140
1141 template<> PhysicsBiasing& Parser::GetGlobal(){return xsecbias;}
1142 template<> PhysicsBiasing* Parser::GetGlobalPtr(){return &xsecbias;}
1143 template<> FastList<PhysicsBiasing>& Parser::GetList<PhysicsBiasing, FastList<PhysicsBiasing>>(){return xsecbias_list;}
1144
1145 template<> Query& Parser::GetGlobal(){return query;}
1146 template<> Query* Parser::GetGlobalPtr(){return &query;}
1147 template<> FastList<Query>& Parser::GetList<Query>(){return query_list;}
1148
1149 template<> Region& Parser::GetGlobal(){return region;}
1150 template<> Region* Parser::GetGlobalPtr(){return &region;}
1151 template<> FastList<Region>& Parser::GetList<Region>(){return region_list;}
1152
1154 template<> SamplerPlacement* Parser::GetGlobalPtr(){return &samplerplacement;}
1155 template<> FastList<SamplerPlacement>& Parser::GetList<SamplerPlacement>() {return samplerplacement_list;}
1156
1157 template<> Scorer& Parser::GetGlobal(){return scorer;}
1158 template<> Scorer* Parser::GetGlobalPtr(){return &scorer;}
1159 template<> FastList<Scorer>& Parser::GetList<Scorer>() {return scorer_list;}
1160
1161 template<> ScorerMesh& Parser::GetGlobal(){return scorermesh;}
1162 template<> ScorerMesh* Parser::GetGlobalPtr(){return &scorermesh;}
1163 template<> FastList<ScorerMesh>& Parser::GetList<ScorerMesh>() {return scorermesh_list;}
1164
1165 template<> Tunnel& Parser::GetGlobal(){return tunnel;}
1166 template<> Tunnel* Parser::GetGlobalPtr(){return &tunnel;}
1167 template<> FastList<Tunnel>& Parser::GetList<Tunnel>(){return tunnel_list;}
1168
1169 template<>
1170 void Parser::ExtendValue(const std::string& property, double value)
1171 {extendedNumbers[property]=value;}
1172
1173 template<>
1174 void Parser::ExtendValue(const std::string& property, std::string value)
1175 {extendedStrings[property]=value;}
1176
1177 template<>
1178 void Parser::ExtendValue(const std::string& property, Array* value)
1179 {extendedVectors[property]=value;}
1180
1181 template <class C, class Container>
1183 {
1184 // copy from global
1185 C& global = GetGlobal<C>();
1186 C inst(global);
1187 // reset global
1188 global.clear();
1189#ifdef BDSDEBUG
1190 inst.print();
1191#endif
1192 GetList<C, Container>().push_back(inst);
1193 }
1194
1195 template <class C, class Container>
1196 void Parser::Add(bool unique, const std::string& className)
1197 {
1198 // copy from global
1199 C& global = GetGlobal<C>();
1200 C inst(global);
1201 // reset global
1202 global.clear();
1203#ifdef BDSDEBUG
1204 inst.print();
1205#endif
1206 GetList<C, Container>().push_back(inst, unique, className);
1207 }
1208
1215 template <>
1216 void Parser::Add<Placement, FastList<Placement>>(bool unique, const std::string& className)
1217 {
1218 // copy from global
1219 Placement& global = GetGlobal<Placement>();
1220 Placement inst(global);
1221 // reset global
1222 global.clear();
1223#ifdef BDSDEBUG
1224 inst.print();
1225#endif
1226 GetList<Placement, FastList<Placement>>().push_back(inst, unique, className);
1227 // if an element definition is used for a placement, keep a separate copy of it
1228 if (!inst.bdsimElement.empty())
1229 {
1230 const Element* elDef = find_element_safe(inst.bdsimElement);
1231 if (!elDef)
1232 {
1233 std::cerr << "The bdsimElement referred to in \"" << inst.name << "\" (\""
1234 << inst.bdsimElement << "\") cannot be found and should be defined"
1235 << " before this placement" << std::endl;
1236 exit(1);
1237 }
1238 placement_elements.push_back(Element(*elDef));
1239 }
1240 }
1241}
1242
1243std::string Parser::GetCallSequenceLog() {
1244 return call_sequence_log->str();
1245}
Aperture class.
Definition aperture.h:37
Representation of arrays used in tokens.
Definition array.h:39
void Copy(Container< std::string, std::allocator< std::string > > &cpy)
Copy STL string containers into symbols.
Definition array.h:85
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
FastListIterator begin()
Definition fastlist.h:217
FastListIterator insert(FastListInputIterator position, const T &val)
template definitions need to be in header
Definition fastlist.h:117
FastListIterator end()
Definition fastlist.h:222
void clear()
empty lists
Definition fastlist.h:171
void erase()
erase elements
Definition fastlist.h:177
void push_back(const T &el, bool unique=false, const std::string &objectName="element")
Definition fastlist.h:154
FastListConstIterator find(std::string name, unsigned int count=1) const
Definition fastlist.h:256
int size() const
size of list
Definition fastlist.h:166
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
void print() const
print some properties
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.
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
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
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
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
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
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
std::string name
Name of this placement.
Definition placement.h:42
void clear()
reset
Definition placement.cc:32
void print() const
print some properties
Definition placement.cc:90
std::string bdsimElement
Name of bdsim-built component to place instead of piece of geometry.
Definition placement.h:44
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
void clear()
Destructor that clears memory.
Definition symbolmap.cc:47
Symtab * symlook(std::string s)
Look up parser symbol.
Definition symbolmap.cc:41
Symtab * symcreate(std::string s)
Create new parser symbol.
Definition symbolmap.cc:27
Common header for the lexer and the parser to share Symbol table for numeric variables,...
Definition sym_table.h:32
void Set(Array *)
Set to Array value.
Definition sym_table.cc:36
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
Element class.
Definition element.h:47
double property_lookup(std::string property_name) const
Definition element.cc:730
void set(const Parameters &params)
set method from Parameters structure
Definition element.cc:758
std::list< Element > * lst
in case the element is a list itself (line)
Definition element.h:288
double l
length in metres
Definition element.h:53
ElementType type
element enum
Definition element.h:48
Material class.
Definition material.h:40
Parameters - Element class with booleans.
Definition parameters.h:43
void inherit_properties(const Element &e)
void flush()
Reset the parameters to defaults and setMap.