BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSMessenger.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 "BDSAcceleratorModel.hh"
19#include "BDSBeamline.hh"
20#include "BDSBeamlineElement.hh"
21#include "BDSMessenger.hh"
22#include "BDSParser.hh"
23#include "BDSSamplerRegistry.hh"
24#include "BDSUIcmdStringInt.hh"
25#include "BDSUtilities.hh"
26
27#include "parser/query.h"
28
29#include "globals.hh"
30#include "G4UImanager.hh"
31#include "G4UIdirectory.hh"
32#include "G4UIcmdWithoutParameter.hh"
33#include "G4UIcmdWithAString.hh"
34
35#include <iomanip>
36#include <iostream>
37#include <ostream>
38#include <string>
39
40
41BDSMessenger::BDSMessenger()
42{
43 bdsDirectory = new G4UIdirectory("/bds/");
44 bdsDirectory->SetGuidance("BDSIM commands");
45
46 bdsBeamlineDirectory = new G4UIdirectory("/bds/beamline/");
47
48 beamlineListCmd = new G4UIcmdWithoutParameter("/bds/beamline/list",this);
49 beamlineListCmd->SetGuidance("List beamline components");
50
51 elementNameSearchCmd = new G4UIcmdWithAString("/bds/beamline/namesearch",this);
52 elementNameSearchCmd->SetGuidance("Search beamline components for element");
53
54 elementGoToCmd = new BDSUIcmdStringInt("/bds/beamline/goto", this);
55 G4String elementGoToCmdGuidance = "Move the viewpoint to a particular element's location. ";
56 elementGoToCmdGuidance += "Arguments are the name of an element in the beam line and optionally,";
57 elementGoToCmdGuidance += " the instance number (zero-counting).";
58 elementGoToCmd->SetGuidance(elementGoToCmdGuidance);
59
60 bdsSamplersDirectory = new G4UIdirectory("/bds/samplers/");
61 samplerListCmd = new G4UIcmdWithoutParameter("/bds/samplers/list",this);
62 samplerListCmd->SetGuidance("List samplers");
63
64 samplerViewCmd = new G4UIcmdWithoutParameter("/bds/samplers/view",this);
65 samplerViewCmd->SetGuidance("View sampler paralle world");
66
67 queryListCmd = new G4UIcmdWithoutParameter("/bds/field/listQueries", this);
68 queryListCmd->SetGuidance("List all queries defined in input");
69
70 // G4UImanager* UIManager = G4UImanager::GetUIpointer();
71 // UIManager->ApplyCommand("/control/execute " + visMacroFilename);
72}
73
74BDSMessenger::~BDSMessenger()
75{
76 delete bdsDirectory;
77 delete bdsBeamlineDirectory;
78 delete beamlineListCmd;
79 delete elementNameSearchCmd;
80 delete elementGoToCmd;
81 delete bdsSamplersDirectory;
82 delete samplerListCmd;
83 delete samplerViewCmd;
84}
85
86void BDSMessenger::SetNewValue(G4UIcommand* command, G4String newValue)
87{
88 if(command == beamlineListCmd)
89 {BeamLineList();}
90 else if(command == elementNameSearchCmd)
91 {ElementNameSearch(newValue);}
92 else if(command == elementGoToCmd)
93 {GoToElement(newValue);}
94 else if(command == samplerListCmd)
95 {SamplerList();}
96 else if (command == samplerViewCmd)
97 {ViewSamplers();}
98 else if (command == queryListCmd)
99 {ListQueries();}
100}
101
102void BDSMessenger::BeamLineList()
103{
104 const BDSBeamline *beamline = BDSAcceleratorModel::Instance()->BeamlineMain();
105
106 int j = 0;
107 auto flagsCache(G4cout.flags());
108 G4cout << std::right
109 << std::setw(4) << "index" << std::setfill(' ')
110 << std::setw(25) << "name"
111 << std::setw(25) << "placement name"
112 << std::setw(20) << "type"
113 << std::setw(20) << "S-middle[m]" << G4endl;
114 for (auto i = beamline->begin(); i != beamline->end(); ++i, ++j)
115 {G4cout << BDSBeamlineElementToString(j) << G4endl;}
116 G4cout.flags(flagsCache);
117}
118
119std::string BDSMessenger::BDSBeamlineElementToString(G4int iElement)
120{
121 std::stringstream ss;
122
123 const BDSBeamline* beamline = BDSAcceleratorModel::Instance()->BeamlineMain();
124 const BDSBeamlineElement* e = beamline->at(iElement);
125
126 ss << std::setfill('0') << std::right << std::setw(4) << iElement << " " << std::setfill(' ')
127 << std::setw(25) << e->GetName()
128 << std::setw(25) << e->GetPlacementName()
129 << std::setw(20) << e->GetType()
130 << std::setw(20) << std::setprecision(4) << std::fixed << e->GetSPositionMiddle()/CLHEP::m;
131
132 return ss.str();
133}
134
135void BDSMessenger::ElementNameSearch(std::string name)
136{
137 const BDSBeamline* beamline = BDSAcceleratorModel::Instance()->BeamlineMain();
138 for (auto i = beamline->begin(); i != beamline->end(); ++i)
139 {
140 if(BDS::StrContains((*i)->GetName(), name))
141 {G4cout << (*i)->GetName() << G4endl;}
142 }
143}
144
145void BDSMessenger::GoToElement(const std::string& value)
146{
147 if (value.empty())
148 {G4cerr << "empty string given - can't search" << G4endl; return;}
149 std::vector<G4String> words = BDS::SplitOnWhiteSpace(value);
150 G4String name = words[0];
151 G4int instance = 0;
152 if (words.size() > 1)
153 {instance = std::stoi(words[1]);}
154
155 const BDSBeamline* beamline = BDSAcceleratorModel::Instance()->BeamlineMain();
156
157 if (!beamline)
158 {G4cout << "No beam line in this model so not possible to search it."; return;}
159
160 // search for the name exactly
161 const BDSBeamlineElement* e = beamline->GetElement(name, instance);
162
163 G4int count = -1;
164 if (!e)
165 {// search the beam line for any element containing the name at all
166 for (const auto& el : *beamline)
167 {
168 if (BDS::StrContains(el->GetName(), name))
169 {
170 count++;
171 if (count == instance)
172 {
173 e = el;
174 break;
175 }
176 }
177 }
178 if (!e)
179 {
180 G4cout << "No component found by that name" << G4endl;
181 if (count > -1)
182 {G4cout << "only " << count << " instances found." << G4endl;}
183 return;
184 }
185 }
186
187 G4ThreeVector pos = e->GetReferencePositionMiddle();
188 G4cout << "goto> " << name << " at global position> " << pos/CLHEP::m << " m" << G4endl;
189 G4UImanager* UIManager = G4UImanager::GetUIpointer();
190 G4String posStr = std::to_string(pos.x()) + " " + std::to_string(pos.y()) + " " + std::to_string(pos.z());
191 UIManager->ApplyCommand("/vis/viewer/set/targetPoint " + posStr + " mm");
192}
193
194void BDSMessenger::ElementTypeSearch(std::string /*type*/)
195{;}
196
197void BDSMessenger::SamplerList()
198{
199 if (BDSSamplerRegistry::Instance()->size() == 0)
200 {
201 G4cout << "No samplers defined" << G4endl;
202 return;
203 }
204 for (const auto& name : BDSSamplerRegistry::Instance()->GetUniqueNames())
205 {G4cout << name << G4endl;}
206}
207
208void BDSMessenger::ViewSamplers()
209{
210 // we can't use drawVolume because that clears the scene and only draws one volume
211 // inside it really applied add/volume as we do here
212 // somehow viewing worlds undoes these trajectory actions in our default vis.mac
213 G4UImanager* UIManager = G4UImanager::GetUIpointer();
214 UIManager->ApplyCommand("/vis/scene/add/volume SamplerWorld_main");
215 UIManager->ApplyCommand("/vis/sceneHandler/attach");
216 UIManager->ApplyCommand("/vis/scene/endOfEventAction accumulate");
217 UIManager->ApplyCommand("/vis/scene/add/trajectories");
218 UIManager->ApplyCommand("/vis/ogl/flushAt endOfEvent");
219}
220
221std::string BDSMessenger::BDSSamplerToString(std::string /*samplerName*/)
222{
223 return std::string();
224}
225
226std::string BDSMessenger::BDSSamplerToString(int iSampler)
227{
228 std::stringstream ss;
229
231
232 ss << std::setfill('0') << std::right << std::setw(4) << iSampler << " " << std::setfill(' ')
233 << std::right << std::setw(20) << sInfo.Name() << " "
234 << std::right << std::setw(20) << sInfo.SPosition();
235 return ss.str();
236}
237
238void BDSMessenger::ListQueries()
239{
240 auto queries = BDSParser::Instance()->GetQueries();
241 for (const auto& qu : queries)
242 {G4cout << qu.name << G4endl;}
243}
const BDSBeamline * BeamlineMain() const
Accessor.
A class that holds a fully constructed BDSAcceleratorComponent as well as any information relevant to...
G4String GetPlacementName() const
Accessor.
G4String GetName() const
Accessor.
G4ThreeVector GetReferencePositionMiddle() const
Accessor.
G4double GetSPositionMiddle() const
Accessor.
G4String GetType() const
Accessor.
A vector of BDSBeamlineElement instances - a beamline.
const BDSBeamlineElement * GetElement(G4String acceleratorComponentName, G4int i=0) const
Get the ith placement of an element in the beam line. Returns null pointer if not found.
const BDSBeamlineElement * at(int iElement) const
Return a reference to the element at i.
iterator begin()
Iterator mechanics.
iterator end()
Iterator mechanics.
static BDSParser * Instance()
Access method.
Definition BDSParser.cc:29
std::vector< GMAD::Query > GetQueries() const
Return the parser list of that object.
Definition BDSParser.hh:107
Information about a registered sampler.
G4double SPosition() const
Accessor.
Associated information for the placement of a sampler.
static BDSSamplerRegistry * Instance()
Accessor for registry.
const BDSSamplerPlacementRecord & GetInfo(G4int index) const
Accessor.
UI command with string, then optional integer argument.
G4bool StrContains(const G4String &str, const G4String &test)
Utility function to simplify lots of syntax changes for pedantic g4 changes.
std::vector< G4String > SplitOnWhiteSpace(const G4String &input)
Split a string on whitespace and return a vector of these 'words'.