OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FromFile.h
Go to the documentation of this file.
1//
2// Class FromFile
3// This class parses a file that contains design variable values.
4// Each column belongs to a design variable.
5// The first line is considered as header and consists of the
6// design variable name. The name has to agree with the string
7// in the input file.
8//
9// Copyright (c) 2018, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
10// All rights reserved
11//
12// Implemented as part of the PhD thesis
13// "Precise Simulations of Multibunches in High Intensity Cyclotrons"
14//
15// This file is part of OPAL.
16//
17// OPAL is free software: you can redistribute it and/or modify
18// it under the terms of the GNU General Public License as published by
19// the Free Software Foundation, either version 3 of the License, or
20// (at your option) any later version.
21//
22// You should have received a copy of the GNU General Public License
23// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
24//
25#ifndef OPAL_SEQUENCE_H
26#define OPAL_SEQUENCE_H
27
30
31#include <string>
32#include <fstream>
33#include <sstream>
34#include <iterator>
35
36#include <vector>
37
39{
40
41public:
42
43 FromFile(const std::string &filename, const std::string &dvarName, size_t modulo)
44 : mod_m(modulo)
45 , filename_m(filename)
46 , dvarName_m(dvarName)
47 {
48 // we need to count the number of lines
49 std::ifstream in(filename_m);
50
51 if ( !in.is_open() ) {
52 throw OpalException("FromFile()",
53 "Couldn't open file \"" + filename_m + "\".");
54 }
55
56 int nLines = std::count(std::istreambuf_iterator<char>(in),
57 std::istreambuf_iterator<char>(), '\n');
58
59 // make sure we do not count empty lines at end
60 in.seekg(-1, std::ios_base::end);
61 std::size_t pos = in.tellg();
62
63 std::string line;
64 std::getline(in, line);
65
66 while ( line.empty() ) {
67 --nLines;
68 --pos;
69 in.seekg(pos, std::ios_base::beg);
70 std::getline(in, line);
71 }
72
73 if ( nLines < 0 )
74 throw OpalException("FromFile()", "Empty file \"" + filename_m + "\".");
75
76 globalSize_m = nLines;
77
78 in.close();
79 }
80
81 void create(boost::shared_ptr<SampleIndividual>& ind, size_t i) {
82 ind->genes[i] = getNext(ind->id);
83 }
84
85 void allocate(const CmdArguments_t& /*args*/, const Comm::Bundle_t& /*comm*/) {
86 std::ifstream in(filename_m);
87
88 if ( !in.is_open() ) {
89 throw OpalException("FromFile()",
90 "Couldn't open file \"" + filename_m + "\".");
91 }
92
93 std::string header;
94 std::getline(in, header);
95 std::istringstream iss(header);
96 std::vector<std::string> dvars({std::istream_iterator<std::string>{iss},
97 std::istream_iterator<std::string>{}});
98 size_t j = 0;
99 for (const std::string& str: dvars) {
100 if (str == dvarName_m) break;
101 ++ j;
102 }
103
104 if (j == dvars.size()) {
105 throw OpalException("FromFile()",
106 "Couldn't find the dvar '" + dvarName_m + "' in the file '" + filename_m + "'");
107 }
108
109 std::string line;
110 std::getline(in, line);
111
112 for (unsigned int i = 0; i < globalSize_m; ++i) {
113 std::istringstream iss(line);
114 std::vector<std::string> numbers({std::istream_iterator<std::string>{iss},
115 std::istream_iterator<std::string>{}});
116
117 chain_m.push_back(std::stod(numbers[j]));
118
119 std::getline(in, line);
120 }
121 in.close();
122 }
123
124 double getNext(unsigned int id) {
125 int idx = int(id / mod_m) % globalSize_m;
126 double sample = chain_m[idx];
127 return sample;
128 }
129
130 unsigned int getSize() const {
131 return globalSize_m;
132 }
133
135
136private:
137 std::vector<double> chain_m;
138 size_t mod_m;
139 std::string filename_m;
140 std::string dvarName_m;
141
142 unsigned int globalSize_m;
143};
144
145#endif
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
boost::shared_ptr< CmdArguments > CmdArguments_t
Definition: CmdArguments.h:176
size_t mod_m
Definition: FromFile.h:138
~FromFile()
Definition: FromFile.h:134
unsigned int getSize() const
Definition: FromFile.h:130
std::vector< double > chain_m
Definition: FromFile.h:137
std::string dvarName_m
Definition: FromFile.h:140
std::string filename_m
Definition: FromFile.h:139
FromFile(const std::string &filename, const std::string &dvarName, size_t modulo)
Definition: FromFile.h:43
void create(boost::shared_ptr< SampleIndividual > &ind, size_t i)
Definition: FromFile.h:81
unsigned int globalSize_m
Definition: FromFile.h:142
void allocate(const CmdArguments_t &, const Comm::Bundle_t &)
Definition: FromFile.h:85
double getNext(unsigned int id)
Definition: FromFile.h:124
The base class for all OPAL exceptions.
Definition: OpalException.h:28
bundles all communicators for a specific role/pid
Definition: types.h:32