OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TrackCmd.cpp
Go to the documentation of this file.
1//
2// Class TrackCmd
3// The class for the OPAL TRACK command.
4//
5// Copyright (c) 200x - 2022, Paul Scherrer Institut, Villigen PSI, Switzerland
6// All rights reserved
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18#include "Track/TrackCmd.h"
19
24#include "Structure/Beam.h"
25#include "Track/Track.h"
26#include "Track/TrackParser.h"
28
29namespace {
30 // The attributes of class TrackCmd
31 enum {
32 LINE, // The name of lattice to be tracked.
33 SOURCES, // The name of the emission sources list (EMISSIONSOURCELIST).
34 BEAM, // The name of beam to be used.
35 BEAMS, // The names of beams to be used (replaces BEAM for input).
36 DT, // The integration timestep in second.
37 // In case of the adaptive integrator, time step guideline for
38 // external field integration.
39 DTSCINIT, // Only for adaptive integrator: Initial time step for space charge integration.
40 DTAU, // Only for adaptive integrator: Alternative way to set accuracy of space
41 // charge integration. Has no direct interpretation like DTSCINIT, but lower
42 // means smaller steps and more accurate. If given, DTSCINIT is not used. Useful
43 // for continuing with same step size in follow-up tracks.
44 T0, // The elapsed time (sec) of the bunch
45 MAXSTEPS, // The maximum timesteps we integrate
46 ZSTART, // Defines a z-location [m] where the reference particle starts
47 ZSTOP, // Defines a z-location [m], after which the simulation stops when the last
48 // particles passes
49 STEPSPERTURN, // Return the timsteps per revolution period. ONLY available for OPAL-cycl.
50 TIMEINTEGRATOR, // the name of time integrator
51 MAP_ORDER, // Truncation order of maps for ThickTracker (default: 1 (linear))
52 SIZE
53 };
54} // namespace
55
56const std::map<std::string, Steppers::TimeIntegrator> TrackCmd::stringTimeIntegrator_s = {
62
63TrackCmd::TrackCmd() : Action(SIZE, "TRACK", "The \"TRACK\" command initiates tracking.") {
64 itsAttr[LINE] = Attributes::makeString("LINE", "Name of lattice to be tracked.");
65
67 "SOURCES", "Name of the emission sources list (EMISSIONSOURCELIST).");
68
69 itsAttr[BEAM] = Attributes::makeString("BEAM", "Name of beam to be used.", "UNNAMED_BEAM");
70
71 itsAttr[BEAMS] =
72 Attributes::makeStringArray("BEAMS", "Names of beams to be used (replaces BEAM).");
73
74 itsAttr[DT] = Attributes::makeRealArray("DT", "The integration timestep in [s].");
75
76 itsAttr[DTSCINIT] = Attributes::makeReal(
77 "DTSCINIT",
78 "Only for adaptive integrator: Initial time step for space charge integration.", 1e-12);
79
81 "DTAU",
82 "Only for adaptive integrator: Alternative way to set accuracy of space integration.",
83 -1.0);
84
85 itsAttr[T0] = Attributes::makeReal("T0", "The elapsed time of the bunch in seconds", 0.0);
86
88 "MAXSTEPS",
89 "The maximum number of integration steps dt, should be larger ZSTOP/(beta*c average).");
90
92 "ZSTART", "Defines a z-location [m] where the reference particle starts.", 0.0);
93
95 "ZSTOP",
96 "Defines a z-location [m], after which the simulation stops when the last particles "
97 "passes.");
98
99 itsAttr[STEPSPERTURN] = Attributes::makeReal(
100 "STEPSPERTURN", "The time steps per revolution period, only for opal-cycl.", 720);
101
103 "TIMEINTEGRATOR", "Name of time integrator to be used.",
104 {"RK-4", "RK4", "LF-2", "LF2", "MTS"}, "RK4");
105
106 itsAttr[MAP_ORDER] = Attributes::makeReal(
107 "MAP_ORDER", "Truncation order of maps for ThickTracker (default: 1, i.e. linear).", 1);
108
112}
113
114TrackCmd::TrackCmd(const std::string& name, TrackCmd* parent) : Action(name, parent) {}
115
117
118TrackCmd* TrackCmd::clone(const std::string& name) { return new TrackCmd(name, this); }
119
120std::vector<double> TrackCmd::getDT() const {
121 std::vector<double> dTs = Attributes::getRealArray(itsAttr[DT]);
122 if (dTs.size() == 0) {
123 dTs.push_back(1e-12);
124 }
125 for (double dt : dTs) {
126 if (dt < 0.0) {
127 throw OpalException(
128 "TrackCmd::getDT", "The time steps provided with DT have to be positive");
129 }
130 }
131 return dTs;
132}
133
134double TrackCmd::getDTSCINIT() const { return Attributes::getReal(itsAttr[DTSCINIT]); }
135
136double TrackCmd::getDTAU() const { return Attributes::getReal(itsAttr[DTAU]); }
137
138double TrackCmd::getT0() const { return Attributes::getReal(itsAttr[T0]); }
139
140double TrackCmd::getZStart() const { return Attributes::getReal(itsAttr[ZSTART]); }
141
142std::vector<double> TrackCmd::getZStop() const {
143 std::vector<double> zstop = Attributes::getRealArray(itsAttr[ZSTOP]);
144 if (zstop.size() == 0) {
145 zstop.push_back(1000000.0);
146 }
147 return zstop;
148}
149
150std::vector<unsigned long long> TrackCmd::getMaxSteps() const {
151 std::vector<double> maxsteps_d = Attributes::getRealArray(itsAttr[MAXSTEPS]);
152 std::vector<unsigned long long> maxsteps_i;
153 if (maxsteps_d.size() == 0) {
154 maxsteps_i.push_back(10ul);
155 }
156 for (double numSteps : maxsteps_d) {
157 if (numSteps < 0) {
158 throw OpalException(
159 "TrackCmd::getMAXSTEPS",
160 "The number of steps provided with MAXSTEPS has to be positive");
161 } else {
162 unsigned long long value = numSteps;
163 maxsteps_i.push_back(value);
164 }
165 }
166
167 return maxsteps_i;
168}
169
170int TrackCmd::getStepsPerTurn() const { return (int)Attributes::getReal(itsAttr[STEPSPERTURN]); }
171
173 std::string name = Attributes::getString(itsAttr[TIMEINTEGRATOR]);
174 return stringTimeIntegrator_s.at(name);
175}
176
178 // Find BeamSequence
180
181 // TRACK,SOURCES is optional (and ignored); emission sources come from the selected BEAM.
182 EmissionSourceList* emissionSourcesList = nullptr;
183
184 // Resolve beams (BEAMS has precedence over BEAM).
185 std::vector<std::string> beamNames = Attributes::getStringArray(itsAttr[BEAMS]);
186 if (beamNames.empty()) {
187 beamNames.push_back(Attributes::getString(itsAttr[BEAM]));
188 }
189 if (beamNames.empty()) {
190 throw OpalException("TrackCmd::execute", "Neither \"BEAMS\" nor \"BEAM\" was specified.");
191 }
192
193 std::vector<Beam*> beams;
194 beams.reserve(beamNames.size());
195 for (const auto& name : beamNames) {
196 if (name.empty()) {
197 throw OpalException("TrackCmd::execute", "Empty beam name in \"BEAMS\".");
198 }
199 beams.push_back(Beam::find(name)); // fail fast
200 }
201
202 // Current tracker supports only a single beam; use the first one for now.
203 Beam* beam = beams.front();
204
205 // std::cout << "TrackCmd::execute" << std::endl;
206 // std::cout << *theLineToTrack << std::endl;
207
208 std::vector<double> dt = getDT();
209 double t0 = getT0();
210 double dtScInit = getDTSCINIT();
211 double deltaTau = getDTAU();
212 std::vector<unsigned long long> maxsteps = getMaxSteps();
213 int stepsperturn = getStepsPerTurn();
214 double zstart = getZStart();
215 std::vector<double> zstop = getZStop();
216
218
219 size_t numTracks = dt.size();
220 numTracks = std::max(numTracks, maxsteps.size());
221 numTracks = std::max(numTracks, zstop.size());
222 for (size_t i = dt.size(); i < numTracks; ++i) {
223 dt.push_back(dt.back());
224 }
225 for (size_t i = maxsteps.size(); i < numTracks; ++i) {
226 maxsteps.push_back(maxsteps.back());
227 }
228 for (size_t i = zstop.size(); i < numTracks; ++i) {
229 zstop.push_back(zstop.back());
230 }
231
234
235 Track::block = new Track(
236 theLineToTrack, beam->getReference(), dt, maxsteps, stepsperturn, zstart, zstop,
237 timeintegrator, t0, dtScInit, deltaTau, emissionSourcesList, beamNames);
238
240
242
243 // Clean up.
244 delete Track::block;
245 Track::block = nullptr;
246}
@ SIZE
Definition IndexMap.cpp:179
The base class for all OPAL actions.
Definition Action.h:29
static void addAttributeOwner(const std::string &owner, const OwnerType &type, const std::string &name)
The base class for all OPAL beam lines and sequences.
static BeamSequence * find(const std::string &name)
Find a BeamSequence by name.
Definition Beam.h:32
static Beam * find(const std::string &name)
Find named BEAM.
Definition Beam.cpp:290
const PartData & getReference() const
Return the embedded OPALX PartData.
Definition Beam.cpp:310
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:169
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:210
virtual void run() const
Read current stream.
double getDTSCINIT() const
Definition TrackCmd.cpp:134
virtual TrackCmd * clone(const std::string &name)
Return a clone.
Definition TrackCmd.cpp:118
std::vector< double > getDT() const
Return the timestep in seconds.
Definition TrackCmd.cpp:120
int getStepsPerTurn() const
Definition TrackCmd.cpp:170
std::vector< double > getZStop() const
location at which the simulation stops
Definition TrackCmd.cpp:142
virtual void execute()
Execute the command.
Definition TrackCmd.cpp:177
virtual ~TrackCmd()
Definition TrackCmd.cpp:116
double getT0() const
Return the elapsed time (sec) of the bunch.
Definition TrackCmd.cpp:138
double getDTAU() const
Definition TrackCmd.cpp:136
Steppers::TimeIntegrator getTimeIntegrator()
return the name of time integrator
Definition TrackCmd.cpp:172
double getZStart() const
location at which the simulation starts
Definition TrackCmd.cpp:140
static const std::map< std::string, Steppers::TimeIntegrator > stringTimeIntegrator_s
Definition TrackCmd.h:56
std::vector< unsigned long long > getMaxSteps() const
Return the maximum timsteps we integrate the system.
Definition TrackCmd.cpp:150
Definition Track.h:35
static Track * block
The block of track data.
Definition Track.h:60
TrackParser parser
The parser used during tracking.
Definition Track.h:57
int truncOrder
Trunction order for map tracking.
Definition Track.h:88
double getReal(const Attribute &attr)
Return real value.
Attribute makeStringArray(const std::string &name, const std::string &help)
Create a string array attribute.
Attribute makePredefinedString(const std::string &name, const std::string &help, const std::initializer_list< std::string > &predefinedStrings)
Make predefined string attribute.
Attribute makeReal(const std::string &name, const std::string &help)
Make real attribute.
Attribute makeRealArray(const std::string &name, const std::string &help)
Create real array attribute.
std::vector< double > getRealArray(const Attribute &attr)
Get array value.
std::vector< std::string > getStringArray(const Attribute &attr)
Get string array value.
std::string getString(const Attribute &attr)
Get string value.
Attribute makeString(const std::string &name, const std::string &help)
Make string attribute.
TimeIntegrator
Definition Steppers.h:25