OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
GlobalFunctions.h
Go to the documentation of this file.
1//
2// Namespace GlobalFunctions
3// Defines multiple expressions to be evaluated in
4// objectives.
5//
6// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
7// All rights reserved
8//
9// Implemented as part of the PhD thesis
10// "Toward massively parallel multi-objective optimization with application to
11// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
12//
13// This file is part of OPAL.
14//
15// OPAL is free software: you can redistribute it and/or modify
16// it under the terms of the GNU General Public License as published by
17// the Free Software Foundation, either version 3 of the License, or
18// (at your option) any later version.
19//
20// You should have received a copy of the GNU General Public License
21// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
22//
23#ifndef __GLOBAL_FUNCTIONS_H__
24#define __GLOBAL_FUNCTIONS_H__
25
26#include <cmath>
27#include <string>
28#include <tuple>
29#include <variant>
30
31#include "Util/Types.h"
32#include "Util/SDDSReader.h"
36
37namespace GlobalFunctions {
38
39//FIXME
40/*
41struct _stat_sum {
42
43 double operator()(client::function::arguments_t args) const {
44
45 // get all arguments
46 double start = std::get<double>(args[0]);
47 double end = std::get<double>(args[1]);
48 double step = std::get<double>(args[2]);
49 std::string name = std::get<std::string>(args[3]);
50
51 const std::unique_ptr<SDDSReader> sim_stats(new SDDSReader(stat_filename_));
52 try {
53 sim_stats->parseFile();
54 } catch (OptPilotException &ex) {
55 std::cout << "Caught exception: " << ex.what() << std::endl;
56 }
57
58 double sum = 0.0;
59 bool is_valid = true;
60 for(size_t i = static_cast<size_t>(start);
61 i < static_cast<size_t>(end); i++) {
62
63 double sim_value;
64 try {
65 sim_stats->getInterpolatedValue(i, name, sim_value);
66 } catch(OptPilotException &e) {
67 std::cout << "Exception while getting value "
68 << "from SDDS file: " << e.what()
69 << std::endl;
70 is_valid = false;
71 }
72
73 sum += sim_value;
74 }
75
76 return std::make_tuple(value, is_valid);
77 }
78};
79*/
80
81struct _sqrt {
83 if (args.size() != 1) {
84 throw OptPilotException("_sqrt::operator()",
85 "sqrt expects 1 arguments, " + std::to_string(args.size()) + " given");
86 }
87
88 double value = std::get<double>(args[0]);
89 return std::make_tuple(sqrt(value), true);
90 }
91};
92
93struct _sq {
95 if (args.size() != 1) {
96 throw OptPilotException("_sq::operator()",
97 "sq expects 1 arguments, " + std::to_string(args.size()) + " given");
98 }
99
100 double value = std::get<double>(args[0]);
101 return std::make_tuple(value * value, true);
102 }
103};
104
105struct _pow {
107 if (args.size() != 2) {
108 throw OptPilotException("_pow::operator()",
109 "pow expects 2 arguments, " + std::to_string(args.size()) + " given");
110 }
111
112 double value = std::get<double>(args[0]);
113 double exp = std::get<double>(args[1]);
114 return std::make_tuple(pow(value, exp), true);
115 }
116};
117
118struct _exp {
120 if (args.size() != 1) {
121 throw OptPilotException("_exp::operator()",
122 "exp expects 1 arguments, " + std::to_string(args.size()) + " given");
123 }
124
125 double value = std::get<double>(args[0]);
126 return std::make_tuple(exp(value), true);
127 }
128};
129
130struct _log {
132 if (args.size() != 1) {
133 throw OptPilotException("_log::operator()",
134 "log expects 1 arguments, " + std::to_string(args.size()) + " given");
135 }
136
137 double value = std::get<double>(args[0]);
138 return std::make_tuple(log(value), true);
139 }
140};
141
142struct _ceil {
144 if (args.size() != 1) {
145 throw OptPilotException("_ceil::operator()",
146 "ceil expects 1 arguments, " + std::to_string(args.size()) + " given");
147 }
148
149 double value = std::get<double>(args[0]);
150 return std::make_tuple(ceil(value), true);
151 }
152};
153
154struct _fabs {
156 if (args.size() != 1) {
157 throw OptPilotException("_fabs::operator()",
158 "fabs expects 1 arguments, " + std::to_string(args.size()) + " given");
159 }
160
161 double value = std::get<double>(args[0]);
162 return std::make_tuple(fabs(value), true);
163 }
164};
165
166struct _floor {
168 if (args.size() != 1) {
169 throw OptPilotException("_floor::operator()",
170 "floor expects 1 arguments, " + std::to_string(args.size()) + " given");
171 }
172
173 double value = std::get<double>(args[0]);
174 return std::make_tuple(floor(value), true);
175 }
176};
177
178struct _fmod {
180 if (args.size() != 2) {
181 throw OptPilotException("_fmod::operator()",
182 "fmod expects 2 arguments, " + std::to_string(args.size()) + " given");
183 }
184
185 double value = std::get<double>(args[0]);
186 double val2 = std::get<double>(args[1]);
187 return std::make_tuple(fmod(value, val2), true);
188 }
189};
190
191struct _sin {
193 if (args.size() != 1) {
194 throw OptPilotException("_sin::operator()",
195 "sin expects 1 arguments, " + std::to_string(args.size()) + " given");
196 }
197
198 double value = std::get<double>(args[0]);
199 return std::make_tuple(sin(value), true);
200 }
201};
202
203struct _asin {
205 if (args.size() != 1) {
206 throw OptPilotException("_asin::operator()",
207 "asin expects 1 arguments, " + std::to_string(args.size()) + " given");
208 }
209
210 double value = std::get<double>(args[0]);
211 return std::make_tuple(asin(value), true);
212 }
213};
214
215struct _cos {
217 if (args.size() != 1) {
218 throw OptPilotException("_cos::operator()",
219 "cos expects 1 arguments, " + std::to_string(args.size()) + " given");
220 }
221
222 double value = std::get<double>(args[0]);
223 return std::make_tuple(cos(value), true);
224 }
225};
226
227struct _acos {
229 if (args.size() != 1) {
230 throw OptPilotException("_acos::operator()",
231 "acos expects 1 arguments, " + std::to_string(args.size()) + " given");
232 }
233
234 double value = std::get<double>(args[0]);
235 return std::make_tuple(acos(value), true);
236 }
237};
238
239struct _tan {
241 if (args.size() != 1) {
242 throw OptPilotException("_tan::operator()",
243 "tan expects 1 arguments, " + std::to_string(args.size()) + " given");
244 }
245
246 double value = std::get<double>(args[0]);
247 return std::make_tuple(tan(value), true);
248 }
249};
250
251struct _atan {
253 if (args.size() != 1) {
254 throw OptPilotException("_atan::operator()",
255 "atan expects 1 arguments, " + std::to_string(args.size()) + " given");
256 }
257
258 double value = std::get<double>(args[0]);
259 return std::make_tuple(atan(value), true);
260 }
261};
262
263static functionDictionary_t get() {
264
265 typedef std::pair<std::string, client::function::type> funcEntry_t;
267
268 client::function::type sqrt_; sqrt_ = _sqrt();
269 funcs.insert(funcEntry_t("sqrt", sqrt_));
270 client::function::type sq_; sq_ = _sq();
271 funcs.insert(funcEntry_t("sq", sq_));
272 client::function::type pow_; pow_ = _pow();
273 funcs.insert(funcEntry_t("pow", pow_));
274 client::function::type exp_; exp_ = _exp();
275 funcs.insert(funcEntry_t("exp", exp_));
276 client::function::type log_; log_ = _log();
277 funcs.insert(funcEntry_t("log", log_));
278
279 client::function::type ceil_; ceil_ = _ceil();
280 funcs.insert(funcEntry_t("ceil", ceil_));
281 client::function::type fabs_; fabs_ = _fabs();
282 funcs.insert(funcEntry_t("fabs", fabs_));
283 client::function::type floor_; floor_ = _floor();
284 funcs.insert(funcEntry_t("floor", floor_));
285 client::function::type fmod_; fmod_ = _fmod();
286 funcs.insert(funcEntry_t("fmod", fmod_));
287
288 client::function::type sin_; sin_ = _sin();
289 funcs.insert(funcEntry_t("sin", sin_));
290 client::function::type asin_; asin_ = _asin();
291 funcs.insert(funcEntry_t("asin", asin_));
292 client::function::type cos_; cos_ = _cos();
293 funcs.insert(funcEntry_t("cos", cos_));
294 client::function::type acos_; acos_ = _acos();
295 funcs.insert(funcEntry_t("acos", acos_));
296 client::function::type tan_; tan_ = _tan();
297 funcs.insert(funcEntry_t("tan", tan_));
298 client::function::type atan_; atan_ = _atan();
299 funcs.insert(funcEntry_t("atan", atan_));
300
301 return funcs;
302}
303
304}
305
306#endif
Tps< T > log(const Tps< T > &x)
Natural logarithm.
Definition TpsMath.h:182
Tps< T > cos(const Tps< T > &x)
Cosine.
Definition TpsMath.h:129
Tps< T > pow(const Tps< T > &x, int y)
Integer power.
Definition TpsMath.h:76
Tps< T > tan(const Tps< T > &x)
Tangent.
Definition TpsMath.h:147
Tps< T > exp(const Tps< T > &x)
Exponential.
Definition TpsMath.h:165
Tps< T > sin(const Tps< T > &x)
Sine.
Definition TpsMath.h:111
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition TpsMath.h:91
PETE_TBTree< FnFmod, PETE_Scalar< Vektor< T1, Dim > >, typename T2::PETE_Expr_t > fmod(const Vektor< T1, Dim > &l, const PETE_Expr< T2 > &r)
PETE_TUTree< FnCeil, typename T::PETE_Expr_t > ceil(const PETE_Expr< T > &l)
Definition PETE.h:728
PETE_TUTree< FnArcCos, typename T::PETE_Expr_t > acos(const PETE_Expr< T > &l)
Definition PETE.h:725
PETE_TUTree< FnFabs, typename T::PETE_Expr_t > fabs(const PETE_Expr< T > &l)
Definition PETE.h:732
PETE_TUTree< FnArcSin, typename T::PETE_Expr_t > asin(const PETE_Expr< T > &l)
Definition PETE.h:726
PETE_TUTree< FnArcTan, typename T::PETE_Expr_t > atan(const PETE_Expr< T > &l)
Definition PETE.h:727
PETE_TUTree< FnFloor, typename T::PETE_Expr_t > floor(const PETE_Expr< T > &l)
Definition PETE.h:733
std::map< std::string, client::function::type > functionDictionary_t
Definition Expression.h:55
std::tuple< double, bool > Result_t
Definition Expression.h:65
std::function< std::tuple< double, bool >(arguments_t)> type
Definition function.hpp:16
std::vector< argument_t > arguments_t
Definition function.hpp:14
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)
Expressions::Result_t operator()(client::function::arguments_t args)