OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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
23#include "Structure/Beam.h"
24#include "Track/Track.h"
25#include "Track/TrackParser.h"
27
28namespace {
29 // The attributes of class TrackCmd
30 enum {
31 LINE, // The name of lattice to be tracked.
32 BEAM, // The name of beam to be used.
33 DT, // The integration timestep in second.
34 // In case of the adaptive integrator, time step guideline for
35 // external field integration.
36 DTSCINIT, // Only for adaptive integrator: Initial time step for space charge integration.
37 DTAU, // Only for adaptive integrator: Alternative way to set accuracy of space
38 // charge integration. Has no direct interpretation like DTSCINIT, but lower
39 // means smaller steps and more accurate. If given, DTSCINIT is not used. Useful
40 // for continuing with same step size in follow-up tracks.
41 T0, // The elapsed time (sec) of the bunch
42 MAXSTEPS, // The maximum timesteps we integrate
43 ZSTART, // Defines a z-location [m] where the reference particle starts
44 ZSTOP, // Defines a z-location [m], after which the simulation stops when the last
45 // particles passes
46 STEPSPERTURN, // Return the timsteps per revolution period. ONLY available for OPAL-cycl.
47 TIMEINTEGRATOR, // the name of time integrator
48 MAP_ORDER, // Truncation order of maps for ThickTracker (default: 1 (linear))
49 SIZE
50 };
51} // namespace
52
53const std::map<std::string, Steppers::TimeIntegrator> TrackCmd::stringTimeIntegrator_s = {
59
60TrackCmd::TrackCmd() : Action(SIZE, "TRACK", "The \"TRACK\" command initiates tracking.") {
61 itsAttr[LINE] = Attributes::makeString("LINE", "Name of lattice to be tracked.");
62
63 itsAttr[BEAM] = Attributes::makeString("BEAM", "Name of beam to be used.", "UNNAMED_BEAM");
64
65 itsAttr[DT] = Attributes::makeRealArray("DT", "The integration timestep in [s].");
66
67 itsAttr[DTSCINIT] = Attributes::makeReal(
68 "DTSCINIT", "Only for adaptive integrator: Initial time step for space charge integration.",
69 1e-12);
70
72 "DTAU",
73 "Only for adaptive integrator: Alternative way to set accuracy of space integration.",
74 -1.0);
75
76 itsAttr[T0] = Attributes::makeReal("T0", "The elapsed time of the bunch in seconds", 0.0);
77
79 "MAXSTEPS",
80 "The maximum number of integration steps dt, should be larger ZSTOP/(beta*c average).");
81
83 "ZSTART", "Defines a z-location [m] where the reference particle starts.", 0.0);
84
86 "ZSTOP",
87 "Defines a z-location [m], after which the simulation stops when the last particles "
88 "passes.");
89
90 itsAttr[STEPSPERTURN] = Attributes::makeReal(
91 "STEPSPERTURN", "The time steps per revolution period, only for opal-cycl.", 720);
92
94 "TIMEINTEGRATOR", "Name of time integrator to be used.",
95 {"RK-4", "RK4", "LF-2", "LF2", "MTS"}, "RK4");
96
97 itsAttr[MAP_ORDER] = Attributes::makeReal(
98 "MAP_ORDER", "Truncation order of maps for ThickTracker (default: 1, i.e. linear).", 1);
99
103}
104
105TrackCmd::TrackCmd(const std::string& name, TrackCmd* parent) : Action(name, parent) {
106}
107
110
111TrackCmd* TrackCmd::clone(const std::string& name) {
112 return new TrackCmd(name, this);
113}
114
115std::vector<double> TrackCmd::getDT() const {
116 std::vector<double> dTs = Attributes::getRealArray(itsAttr[DT]);
117 if (dTs.size() == 0) {
118 dTs.push_back(1e-12);
119 }
120 for (double dt : dTs) {
121 if (dt < 0.0) {
122 throw OpalException(
123 "TrackCmd::getDT", "The time steps provided with DT have to be positive");
124 }
125 }
126 return dTs;
127}
128
129double TrackCmd::getDTSCINIT() const {
130 return Attributes::getReal(itsAttr[DTSCINIT]);
131}
132
133double TrackCmd::getDTAU() const {
134 return Attributes::getReal(itsAttr[DTAU]);
135}
136
137double TrackCmd::getT0() const {
138 return Attributes::getReal(itsAttr[T0]);
139}
140
141double TrackCmd::getZStart() const {
142 return Attributes::getReal(itsAttr[ZSTART]);
143}
144
145std::vector<double> TrackCmd::getZStop() const {
146 std::vector<double> zstop = Attributes::getRealArray(itsAttr[ZSTOP]);
147 if (zstop.size() == 0) {
148 zstop.push_back(1000000.0);
149 }
150 return zstop;
151}
152
153std::vector<unsigned long long> TrackCmd::getMaxSteps() const {
154 std::vector<double> maxsteps_d = Attributes::getRealArray(itsAttr[MAXSTEPS]);
155 std::vector<unsigned long long> maxsteps_i;
156 if (maxsteps_d.size() == 0) {
157 maxsteps_i.push_back(10ul);
158 }
159 for (double numSteps : maxsteps_d) {
160 if (numSteps < 0) {
161 throw OpalException(
162 "TrackCmd::getMAXSTEPS",
163 "The number of steps provided with MAXSTEPS has to be positive");
164 } else {
165 unsigned long long value = numSteps;
166 maxsteps_i.push_back(value);
167 }
168 }
169
170 return maxsteps_i;
171}
172
174 return (int)Attributes::getReal(itsAttr[STEPSPERTURN]);
175}
176
178 std::string name = Attributes::getString(itsAttr[TIMEINTEGRATOR]);
179 return stringTimeIntegrator_s.at(name);
180}
181
183 // Find BeamSequence
185
186 // Find Beam
188
189 // std::cout << "TrackCmd::execute" << std::endl;
190 // std::cout << *theLineToTrack << std::endl;
191
192 std::vector<double> dt = getDT();
193 double t0 = getT0();
194 double dtScInit = getDTSCINIT();
195 double deltaTau = getDTAU();
196 std::vector<unsigned long long> maxsteps = getMaxSteps();
197 int stepsperturn = getStepsPerTurn();
198 double zstart = getZStart();
199 std::vector<double> zstop = getZStop();
200
202
203 size_t numTracks = dt.size();
204 numTracks = std::max(numTracks, maxsteps.size());
205 numTracks = std::max(numTracks, zstop.size());
206 for (size_t i = dt.size(); i < numTracks; ++i) {
207 dt.push_back(dt.back());
208 }
209 for (size_t i = maxsteps.size(); i < numTracks; ++i) {
210 maxsteps.push_back(maxsteps.back());
211 }
212 for (size_t i = zstop.size(); i < numTracks; ++i) {
213 zstop.push_back(zstop.back());
214 }
215
218
219 Track::block = new Track(
220 theLineToTrack, beam->getReference(), dt, maxsteps, stepsperturn, zstart, zstop,
221 timeintegrator, t0, dtScInit, deltaTau);
222
223 Track::block->truncOrder = (int)Attributes::getReal(itsAttr[MAP_ORDER]);
224
225 Track::block->parser.run();
226
227 // Clean up.
228 delete Track::block;
229 Track::block = nullptr;
230}
@ SIZE
Definition IndexMap.cpp:173
double getReal(const Attribute &attr)
Return real value.
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::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
Action(int size, const char *name, const char *help)
Constructor for exemplars.
Definition Action.cpp:54
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.
void registerOwnership(const AttributeHandler::OwnerType &itsClass) const
Definition Object.cpp:189
std::vector< Attribute > itsAttr
The object attributes.
Definition Object.h:216
Definition Beam.h:31
static Beam * find(const std::string &name)
Find named BEAM.
Definition Beam.cpp:132
const PartData & getReference() const
Return the embedded CLASSIC PartData.
Definition Beam.cpp:152
Definition Track.h:33
static Track * block
The block of track data.
Definition Track.h:55
double getDTSCINIT() const
Definition TrackCmd.cpp:129
virtual TrackCmd * clone(const std::string &name)
Return a clone.
Definition TrackCmd.cpp:111
std::vector< double > getDT() const
Return the timestep in seconds.
Definition TrackCmd.cpp:115
int getStepsPerTurn() const
Definition TrackCmd.cpp:173
std::vector< double > getZStop() const
location at which the simulation stops
Definition TrackCmd.cpp:145
virtual void execute()
Execute the command.
Definition TrackCmd.cpp:182
virtual ~TrackCmd()
Definition TrackCmd.cpp:108
double getT0() const
Return the elapsed time (sec) of the bunch.
Definition TrackCmd.cpp:137
double getDTAU() const
Definition TrackCmd.cpp:133
Steppers::TimeIntegrator getTimeIntegrator()
return the name of time integrator
Definition TrackCmd.cpp:177
double getZStart() const
location at which the simulation starts
Definition TrackCmd.cpp:141
static const std::map< std::string, Steppers::TimeIntegrator > stringTimeIntegrator_s
Definition TrackCmd.h:53
std::vector< unsigned long long > getMaxSteps() const
Return the maximum timsteps we integrate the system.
Definition TrackCmd.cpp:153
The base class for all OPAL exceptions.