OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
MaxNormRadialPeak.h
Go to the documentation of this file.
1//
2// Struct MaxNormRadialPeak
3// A simple expression computing the maximum norm of all peak errors (given as
4// first and second argument) for a range of peaks (third argument and fourth argument)
5// according to
6//
7// \f[
8// result = ||measurement_i - value_i||_\infty
9// \f]
10//
11// Copyright (c) 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
12// All rights reserved
13//
14// Implemented as part of the PhD thesis
15// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
16// and the paper
17// "Matching of turn pattern measurements for cyclotrons using multiobjective optimization"
18// (https://doi.org/10.1103/PhysRevAccelBeams.22.064602)
19//
20// This file is part of OPAL.
21//
22// OPAL is free software: you can redistribute it and/or modify
23// it under the terms of the GNU General Public License as published by
24// the Free Software Foundation, either version 3 of the License, or
25// (at your option) any later version.
26//
27// You should have received a copy of the GNU General Public License
28// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
29//
30#ifndef __MAX_NORM_RADIALPEAK_H__
31#define __MAX_NORM_RADIALPEAK_H__
32
33#include <cmath>
34#include <string>
35#include <tuple>
36#include <variant>
37
38#include "Util/Types.h"
39#include "Util/PeakReader.h"
41
43
44 static const std::string name;
45
47 if (args.size() != 4) {
48 throw OptPilotException("MaxNormRadialPeak::operator()",
49 "maxNormRadialPeak expects 4 arguments, " + std::to_string(args.size()) + " given");
50 }
51
52 meas_filename_ = std::get<std::string>(args[0]);
53 sim_filename_ = std::get<std::string>(args[1]);
54 begin_ = std::get<double>(args[2]);
55 end_ = std::get<double>(args[3]);
56
57 bool is_valid = true;
58
59 const std::unique_ptr<PeakReader> meas_peaks(new PeakReader(meas_filename_));
60 const std::unique_ptr<PeakReader> sim_peaks(new PeakReader(sim_filename_));
61 try {
62 sim_peaks->parseFile();
63 meas_peaks->parseFile();
64
65 if ( end_ < begin_ || end_ < 0 || begin_ < 0 )
66 throw OptPilotException("MaxNormRadialPeak::operator()",
67 "Error check turn number range");
68
69 } catch (OptPilotException &ex) {
70 std::cout << "Caught exception: " << ex.what() << std::endl;
71 is_valid = false;
72 }
73
74 double maximum = -1.0;
75
76 for (int turn = begin_; turn < end_ + 1; ++turn) {
77 double sim_value = 0.0, meas_value = 0.0;
78 try {
79 sim_peaks->getPeak(turn, sim_value);
80 meas_peaks->getPeak(turn, meas_value);
81 } catch(OptPilotException &e) {
82 std::cout << "Exception while getting value "
83 << "from peak file: " << e.what()
84 << std::endl;
85 is_valid = false;
86 }
87 double val = std::abs(meas_value - sim_value);
88 maximum = std::max(maximum, val);
89 }
90
91 return std::make_tuple(maximum, is_valid);
92 }
93
94private:
95 std::string meas_filename_;
96 std::string sim_filename_;
97 int begin_;
98 int end_;
99
100 // define a mapping to arguments in argument vector
101 std::tuple<std::string, std::string, int, int> argument_types;
102 // :FIXME: remove unused enum
103#if 0
104 enum {
105 meas_filename
106 , sim_filename
107 , begin
108 , end
109 } argument_type_id;
110#endif
111};
112
113#endif
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
std::tuple< double, bool > Result_t
Definition Expression.h:65
std::vector< argument_t > arguments_t
Definition function.hpp:14
std::string meas_filename_
std::tuple< std::string, std::string, int, int > argument_types
static const std::string name
std::string sim_filename_
Expressions::Result_t operator()(client::function::arguments_t args)
virtual const char * what() const