OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
SumErrSq.h
Go to the documentation of this file.
1//
2// Struct SumErrSq
3// A simple expression computing the sum of all measurement errors (given as
4// first and third argument) for a variable (second argument) according to
5//
6// \f[
7// result = \frac{1}{n} * \sqrt{\sum_{i=0}^n (measurement_i - value_i)^2}
8// \f]
9//
10// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
11// All rights reserved
12//
13// Implemented as part of the PhD thesis
14// "Toward massively parallel multi-objective optimization with application to
15// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
16//
17// This file is part of OPAL.
18//
19// OPAL is free software: you can redistribute it and/or modify
20// it under the terms of the GNU General Public License as published by
21// the Free Software Foundation, either version 3 of the License, or
22// (at your option) any later version.
23//
24// You should have received a copy of the GNU General Public License
25// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
26//
27#ifndef __SUMERRSQ_H__
28#define __SUMERRSQ_H__
29
30#include <fstream>
31#include <iterator>
32#include <map>
33#include <string>
34#include <tuple>
35#include <variant>
36
37#include "Util/Types.h"
38#include "Util/SDDSReader.h"
40
42public:
43 double spos;
45
46 friend std::istream & operator>>(std::istream & stream, Measurement & measurement);
47};
48
49
50struct SumErrSq {
51
52 static const std::string name;
53
55
56 if (args.size() != 3) {
57 throw OptPilotException("SumErrSq::operator()",
58 "sumErrSq expects 3 arguments, " + std::to_string(args.size()) + " given");
59 }
60
61 std::string measurement_filename = std::get<std::string>(args[0]);
62 var_name_ = std::get<std::string>(args[1]);
63 stat_filename_ = std::get<std::string>(args[2]);
64
65 //FIXME: we could assume measurements don't change
66 parseMeasurements(measurement_filename);
67 bool is_valid = true;
68
69 const std::unique_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
70 try {
71 sim_stats->parseFile();
72 } catch (OptPilotException &ex) {
73 std::cout << "Caught exception: " << ex.what() << std::endl;
74 is_valid = false;
75 }
76
77 double sum = 0;
78
79 for(Measurement measurement : measurements_) {
80 double sim_value = 0.0;
81 try {
82 sim_stats->getInterpolatedValue(
83 measurement.spos, var_name_, sim_value);
84 } catch(OptPilotException &e) {
85 std::cout << "Exception while getting value "
86 << "from SDDS file: " << e.what()
87 << std::endl;
88 is_valid = false;
89 }
90 double val = measurement.measurement - sim_value;
91 sum += val * val;
92 }
93
94 return std::make_tuple(std::sqrt(sum/measurements_.size()), is_valid);
95 }
96
97private:
98
99 std::vector<Measurement> measurements_;
100
101 std::string var_name_;
102 std::string stat_filename_;
103
104 void parseMeasurements(std::string measurement_filename);
105
106 // define a mapping to arguments in argument vector
107 std::tuple<std::string, std::string, std::string> argument_types;
108};
109
110#endif
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition PETE.h:1111
std::tuple< double, bool > Result_t
Definition Expression.h:65
std::vector< argument_t > arguments_t
Definition function.hpp:14
double spos
Definition SumErrSq.h:43
double measurement
Definition SumErrSq.h:44
friend std::istream & operator>>(std::istream &stream, Measurement &measurement)
Definition SumErrSq.cpp:5
Expressions::Result_t operator()(client::function::arguments_t args)
Definition SumErrSq.h:54
static const std::string name
Definition SumErrSq.h:52
std::vector< Measurement > measurements_
Definition SumErrSq.h:99
void parseMeasurements(std::string measurement_filename)
Definition SumErrSq.cpp:13
std::string var_name_
Definition SumErrSq.h:101
std::tuple< std::string, std::string, std::string > argument_types
Definition SumErrSq.h:107
std::string stat_filename_
Definition SumErrSq.h:102
virtual const char * what() const