OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
FromFile.h
Go to the documentation of this file.
1//
2// Struct FromFile
3// Simple functor that reads vector data from a file. If the file contains
4// more than one value the sum is returned.
5// \f[
6// result = \sum_{i=0}^n value_i
7// \f]
8//
9// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
10// All rights reserved
11//
12// Implemented as part of the PhD thesis
13// "Toward massively parallel multi-objective optimization with application to
14// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
15//
16// This file is part of OPAL.
17//
18// OPAL is free software: you can redistribute it and/or modify
19// it under the terms of the GNU General Public License as published by
20// the Free Software Foundation, either version 3 of the License, or
21// (at your option) any later version.
22//
23// You should have received a copy of the GNU General Public License
24// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
25//
26#ifndef __FROMFILE_H__
27#define __FROMFILE_H__
28
29#include <fstream>
30#include <iterator>
31#include <map>
32#include <set>
33#include <string>
34#include <tuple>
35#include <variant>
36
37#include "Util/Types.h"
40
41struct FromFile {
42
43 static const std::string name;
44
46 if (args.size() != 1) {
47 throw OptPilotException("FromFile::operator()",
48 "fromFile expects 1 arguments, " + std::to_string(args.size()) + " given");
49 }
50
51 filename_ = std::get<std::string>(args[0]);
52
53 double sum = 0;
54 bool is_valid = true;
55
56 try {
57 readValues();
58 } catch(const OptPilotException& e) {
59 return std::make_tuple(0.0, false);
60 }
61
62 for(double obj_value : values_)
63 sum += obj_value;
64
65 return std::make_tuple(sum, is_valid);
66 }
67
68
69private:
70
71 std::vector<double> values_;
72
73 std::string filename_;
74
75 void readValues();
76
77};
78
79#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
Sampling method that reads design variable values from a text file.
Definition FromFile.h:50
static const std::string name
Definition FromFile.h:43
std::string filename_
Definition FromFile.h:73
Expressions::Result_t operator()(client::function::arguments_t args)
Definition FromFile.h:45
void readValues()
reads a simple list of double values
Definition FromFile.cpp:29
std::vector< double > values_
Definition FromFile.h:71