OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
SDDSVariable.h
Go to the documentation of this file.
1//
2// Struct SDDSVariable
3// A simple expression to get SDDS (filename) value near a
4// specific position (ref_val, default: spos) of a reference
5// variable (ref_name) for a variable (var_name). Possible
6// argument orders:
7// args = [var_name, ref_val, filename]
8// args = [var_name, ref_name, ref_val, filename]
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 __SDDSVARIABLE_H__
28#define __SDDSVARIABLE_H__
29
30#include <string>
31#include <tuple>
32#include <variant>
33
34#include "Util/Types.h"
35#include "Util/SDDSReader.h"
38
39
41
42 static const std::string name;
43
45 switch ( args.size() ) {
46 case 3: {
47 var_name_ = std::get<std::string>(args[0]);
48 ref_name_ = "s";
49 ref_val_ = std::get<double>(args[1]);
50 stat_filename_ = std::get<std::string>(args[2]);
51 break;
52 }
53 case 4: {
54 var_name_ = std::get<std::string>(args[0]);
55 ref_name_ = std::get<std::string>(args[1]);
56 ref_val_ = std::get<double>(args[2]);
57 stat_filename_ = std::get<std::string>(args[3]);
58 break;
59 }
60 default: {
61 throw OptPilotException("SDDSVariable::operator()",
62 "sddsVariableAt expects 3 or 4 arguments, " +
63 std::to_string(args.size()) + " given");
64 }
65 }
66
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 (SDDSParserException &ex) {
73 std::cout << "Caught exception: " << ex.what() << std::endl;
74 is_valid = false;
75 }
76
77 double sim_value = 0.0;
78 try {
79 sim_stats->getInterpolatedValue(ref_name_, ref_val_, var_name_, sim_value);
80 } catch(SDDSParserException &e) {
81 std::cout << "Exception while getting value "
82 << "from SDDS file: " << e.what()
83 << std::endl;
84 is_valid = false;
85 } catch(...) {
86 std::cout << "Exception while getting '" + var_name_ + "' "
87 << "from SDDS file. "
88 << std::endl;
89 is_valid = false;
90 }
91
92 return std::make_tuple(sim_value, is_valid);
93 }
94
95private:
96
97 std::string var_name_;
98 std::string stat_filename_;
99 std::string ref_name_;
100 double ref_val_;
101};
102
113 sameSDDSVariable(const std::string & base_filename) {
114 size_t pos = base_filename.find_last_of("/");
115 std::string tmplfile = base_filename;
116 if(pos != std::string::npos)
117 tmplfile = base_filename.substr(pos+1);
118 pos = tmplfile.find_last_of(".");
119 // std::string simName =
120 stat_filename_ = tmplfile.substr(0,pos) + ".stat";
121 }
122
124 if (args.size() < 2 || args.size() > 3) {
125 throw OptPilotException("sameSDDSVariable::operator()",
126 "statVariableAt expects 2 or 3 arguments, " +
127 std::to_string(args.size()) + " given");
128 }
129
130 args.push_back(stat_filename_);
131
132 return var_(args);
133 }
134
135private:
138};
139
140#endif
std::tuple< double, bool > Result_t
Definition Expression.h:65
std::vector< argument_t > arguments_t
Definition function.hpp:14
std::variant< double, bool, std::string > argument_t
Definition function.hpp:12
Expressions::Result_t operator()(client::function::arguments_t args)
std::string ref_name_
std::string stat_filename_
std::string var_name_
static const std::string name
client::function::argument_t stat_filename_
sameSDDSVariable(const std::string &base_filename)
Expressions::Result_t operator()(client::function::arguments_t args)
SDDSVariable var_
virtual const char * what() const