Loading...
Searching...
No Matches
gpr::io::FileManager Class Reference

Responsible for generic IO operations. More...

#include <managers/io/FileManager.h>

Public Member Functions

 FileManager ()
virtual ~FileManager ()
void readInputFile (const std::string file_name, gpr::InputParameters &parameters)
 Read data from the input file and populates structure of parameters. The input file may have empty lines and in-line comments that should start with "%" character. The values should be assigned to variable using the assignment operator "=".
template<typename T>
void readDataFile (const std::string file_name, std::map< std::string, T * > &data)
 Read data file. The method reads data file written in ASCII format. The values of data should point to fields that supposed to be populated. The keys in data should have names of fields from the data file.
template<typename T>
bool readFromPlainFile (const std::string file_name, T &field)
 Read plain ASCII data file. This function reads data line-by-line from a plain text file. No memory allocation and no range chacks are invoked during the execution of the function.
template<typename T>
bool writePlainFile (const std::string file_name, const T &field, std::ios::openmode mode)
bool writeXYZFile (const std::string file_name, const gpr::Coord &field, std::ios::openmode mode)

Detailed Description

Responsible for generic IO operations.

Definition at line 34 of file FileManager.h.

Constructor & Destructor Documentation

◆ FileManager()

gpr::io::FileManager::FileManager ( )

Definition at line 23 of file FileManager.cpp.

23{ }

◆ ~FileManager()

gpr::io::FileManager::~FileManager ( )
virtual

Definition at line 25 of file FileManager.cpp.

25{ }

Member Function Documentation

◆ readDataFile()

template<typename T>
void gpr::io::FileManager::readDataFile ( const std::string file_name,
std::map< std::string, T * > & data )

Read data file. The method reads data file written in ASCII format. The values of data should point to fields that supposed to be populated. The keys in data should have names of fields from the data file.

Note
Template T should be of type Coord or Field.
Parameters
file_nameName of the data file.
dataMap of fields names and pointers to the fields objects.

Definition at line 98 of file FileManager.inl.

100{
101 std::ifstream is;
102 std::string line;
103 char split_char = ' ';
104
105 is.open(file_name.c_str(), std::ios::in);
106
107 if (!is.is_open()) {
108 gpr::io::ErrorManager err_man;
109 err_man << "Input file '" << file_name << "' cannot be opened.\n";
110 return;
111 }
112
113 // Read the file by data sets:
114 // first - field name and dimensions
115 // second - values
116 while (std::getline(is, line)) {
117 if (!line.empty()) {
118 std::vector<std::string> split_line;
119 std::string key = "";
120
121 split_line = splitString(line, std::string(1, split_char));
122
123 // A header is "<name> <rows> <cols>". Anything shorter is not one:
124 // skip it rather than indexing the split out of range.
125 if (split_line.size() < 3) continue;
126
127 key = split_line[0];
128
129 if (data.find(key) != data.end()) {
130 T *field = data[key];
131 // Dimensions are attacker-controlled text. Reject anything
132 // negative or unparseable before it reaches resize(), where
133 // Index_t's unsignedness would turn -1 into 4294967295.
134 const long rows_in = std::atol(split_line[1].c_str());
135 const long cols_in = std::atol(split_line[2].c_str());
136 if (rows_in < 0 || cols_in < 0) {
137 gpr::io::ErrorManager err_man;
138 err_man << "Field '" << key << "' declares negative "
139 << "dimensions (" << rows_in << " x " << cols_in
140 << "); skipped.\n";
141 continue;
142 }
143 const gpr::Index_t num_elts_i =
144 static_cast<gpr::Index_t>(rows_in);
145 const gpr::Index_t num_elts_j =
146 static_cast<gpr::Index_t>(cols_in);
147
148 field->resize(num_elts_i, num_elts_j);
149
150 // Get values
151 if (!std::getline(is, line) || line.empty()) {
152 gpr::io::ErrorManager err_man;
153 err_man << "Field '" << key
154 << "' has no value line; left zero-filled.\n";
155 continue;
156 }
157
158 split_line.clear();
159 split_line = splitString(line, std::string(1, split_char));
160
161 // Stop at the declared extent: a value line longer than
162 // rows*cols must not run off the end of the field, and a
163 // zero column count would leave counter_j pinned below.
164 const size_t capacity = static_cast<size_t>(num_elts_i) *
165 static_cast<size_t>(num_elts_j);
166 const size_t count = std::min(capacity, split_line.size());
167 gpr::Index_t counter_i = 0;
168 gpr::Index_t counter_j = 0;
169 for (size_t n = 0; n < count; ++n) {
170 field->operator()(counter_i, counter_j) =
171 std::atof(split_line[n].c_str());
172 ++counter_j;
173 if (counter_j == num_elts_j) {
174 ++counter_i;
175 counter_j = 0;
176 }
177 }
178 if (split_line.size() > capacity) {
179 gpr::io::ErrorManager err_man;
180 err_man << "Field '" << key << "' declares " << num_elts_i
181 << " x " << num_elts_j << " but supplies "
182 << split_line.size()
183 << " values; extra values ignored.\n";
184 }
185 }
186 }
187 }
188
189 is.close();
190
191 // Print if needed
192 // for(std::pair<std::string, gpr::Field<T>*> elt : data) {
193 // std::cout << elt.first << "\n";
194 // for(Index_t n = 0; n < elt.second->getDimensions(); ++n)
195 // for(Index_t m = 0; m < elt.second->getSize(n); ++m)
196 // std::cout << elt.second->operator()(n, m) << " ";
197 // std::cout << "\n";
198 // }
199}
std::string line(const std::string &topic, const std::vector< Field > &fields)
uint32_t Index_t
Definition Macros.h:17

◆ readFromPlainFile()

template<typename T>
bool gpr::io::FileManager::readFromPlainFile ( const std::string file_name,
T & field )

Read plain ASCII data file. This function reads data line-by-line from a plain text file. No memory allocation and no range chacks are invoked during the execution of the function.

Warning
field should be pre-allocated!
Parameters
file_nameFile name
field2D field of data
Returns
EXIT_SUCCESS or EXIT_FAILURE codes

Definition at line 202 of file FileManager.inl.

203{
204 std::ifstream in_stream;
205 in_stream.open(file_name, std::ios::in);
206
207 if (!in_stream.is_open()) {
208 io::ErrorManager err;
209 err << "Error! Reference data file '" << file_name << "' not found!\n";
210 return false;
211 }
212
213 // A short or malformed file leaves the tail of `field` holding whatever it
214 // held before, which reads downstream as data. Report the truncation.
215 decltype(field.size()) n = 0;
216 for (; n < field.size(); ++n) {
217 if (!(in_stream >> *(field.data() + n))) break;
218 }
219 in_stream.close();
220 if (n != field.size()) {
221 io::ErrorManager err;
222 err << "Error! File '" << file_name << "' supplied " << n << " of "
223 << field.size() << " expected values.\n";
224 return false;
225 }
226 return true;
227}

◆ readInputFile()

void gpr::io::FileManager::readInputFile ( const std::string file_name,
gpr::InputParameters & parameters )

Read data from the input file and populates structure of parameters. The input file may have empty lines and in-line comments that should start with "%" character. The values should be assigned to variable using the assignment operator "=".

Parameters
file_nameName of the input file.
parametersStructure of parameters

Definition at line 27 of file FileManager.cpp.

29{
30 std::ifstream is;
31 std::string line;
32 std::map<std::string, std::vector<std::string> > input_data;
33
34 char comment_char = '%'; // Change these two characters to modify the
35 char assignment_char = '='; // comment symbol and the assignment operator
36
37 char open_array_char = '['; // Change these characters to modify the
38 char close_array_char = ']'; // opening and closing brackets for arrays
39 char array_separator_char = ','; // and the separator character
40
41 std::string empy_value =
42 "none"; // Used to keep some strings empty in the file
43
44 is.open(file_name.c_str(), std::ios::in);
45
46 if (!is.is_open()) {
47 io::ErrorManager err;
48 err << "Input file '" << file_name << "' cannot be opened.\n";
49 return;
50 }
51
52 // Read the file line-by-line
53 while (std::getline(is, line)) {
54 // Process the line only if it is not empty
55 if (!line.empty()) {
56 std::vector<std::string> split_line;
57 size_t comment_pos = 0;
58 bool array_as_value = false;
59
60 // Remove all tabs and white spaces if there are any
61 line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
62 line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
63
64 // A line that was only whitespace is now empty; skip it before the
65 // line[0] access below (otherwise an out-of-range read aborts).
66 if (line.empty()) continue;
67
68 // Skip the following steps if the line is a comment
69 if (line[0] == comment_char) continue;
70
71 // Check and remove any characters after (and including) the comment
72 // indicator
73 comment_pos = line.find_first_of(comment_char);
74 if (comment_pos != std::string::npos)
75 line.erase(line.begin() + comment_pos, line.end());
76
77 // Split the line using the assignment character
78 split_line = splitString(line, std::string(1, assignment_char));
79
80 // A well-formed entry is exactly "key = value" (two fields). A line
81 // with no assignment char -- a stray token, or binary bytes from a
82 // file handed to the text reader by mistake -- splits into fewer
83 // than two fields; skip it instead of indexing split_line[1] out of
84 // range. The size()==2 check below handles the too-many-fields
85 // case.
86 if (split_line.size() < 2) continue;
87
88 // "key=" leaves an empty value. size() - 1 below would wrap to
89 // SIZE_MAX, so drop the line before indexing into it.
90 if (split_line[1].empty()) continue;
91
92 // Check if the value is a valid array. It should have both opening
93 // and closing brackets
94 if (split_line[1][0] == open_array_char ||
95 split_line[1][split_line[1].size() - 1] == close_array_char) {
96 if (split_line[1][0] != open_array_char ||
97 split_line[1][split_line[1].size() - 1] !=
98 close_array_char) {
99 io::ErrorManager err;
100 err << "Malformed array value for key '" << split_line[0]
101 << "' (unbalanced brackets); line skipped.\n";
102 continue;
103 } else {
104 split_line[1].erase(
105 std::remove(split_line[1].begin(), split_line[1].end(),
106 open_array_char),
107 split_line[1].end());
108 split_line[1].erase(
109 std::remove(split_line[1].begin(), split_line[1].end(),
110 close_array_char),
111 split_line[1].end());
112 array_as_value = true;
113 }
114 }
115
116 // A well-formed entry is exactly "key = value". More fields means
117 // a second assignment character on the line, and silently keeping
118 // split_line[1] would drop the rest.
119 if (split_line.size() != 2) {
120 io::ErrorManager err;
121 err << "Key '" << split_line[0] << "' has " << split_line.size()
122 << " assignment-separated fields, expected 2; line "
123 "skipped.\n";
124 continue;
125 }
126
127 // Add the result to the map
128 if (!array_as_value) {
129 input_data[split_line[0]].resize(1);
130 if (split_line[1] == empy_value) split_line[1] = "";
131 input_data[split_line[0]][0] = split_line[1];
132 } else {
133 std::vector<std::string> split_array;
134 split_array = splitString(split_line[1],
135 std::string(1, array_separator_char));
136
137 input_data[split_line[0]].resize(split_array.size());
138
139 for (size_t n = 0; n < split_array.size(); ++n) {
140 if (split_array[n] == empy_value) split_array[n] = "";
141 input_data[split_line[0]][n] = split_array[n];
142 }
143 }
144 }
145 }
146
147 // Print the data if needed
148 // for(std::map<std::string, std::vector<std::string> >::iterator
149 // it_beg = input_data.begin(),
150 // it_end = input_data.end();
151 // it_beg != it_end; ++it_beg) {
152 // std::cout << it_beg->first << " : [";
153 // for(size_t n = 0, end = it_beg->second.size(); n < end; ++n) {
154 // std::cout << it_beg->second[n];
155 // if (n < end - 1)
156 // std::cout << " ";
157 // }
158 // std::cout << "]\n";
159 // }
160
161 convertStringToValue(input_data, parameters.i_dist);
162 convertStringToValue(input_data, parameters.i_run);
163 convertStringToValue(input_data, parameters.actdist_fro);
164 convertStringToValue(input_data, parameters.eval_image1);
165 convertStringToValue(input_data, parameters.initrot_nogp);
166 convertStringToValue(input_data, parameters.num_iter_initrot);
167 convertStringToValue(input_data, parameters.num_iter_rot_gp);
168 convertStringToValue(input_data, parameters.divisor_T_dimer_gp);
169 convertStringToValue(input_data, parameters.T_dimer);
170 convertStringToValue(input_data, parameters.T_anglerot_init);
171 convertStringToValue(input_data, parameters.T_anglerot_gp);
172 convertStringToValue(input_data, parameters.disp_max);
173 convertStringToValue(input_data, parameters.ratio_at_limit);
174 convertStringToValue(input_data, parameters.num_bigiter);
175 convertStringToValue(input_data, parameters.num_iter);
176 convertStringToValue(input_data, parameters.islarge_num_iter);
177 convertStringToValue(input_data, parameters.dimer_sep);
178 convertStringToValue(input_data, parameters.gp_sigma2);
179 convertStringToValue(input_data, parameters.jitter_sigma2);
180 convertStringToValue(input_data, parameters.sigma2);
181 convertStringToValue(input_data, parameters.magnSigma2);
182 convertStringToValue(input_data, parameters.constSigma2);
183 convertStringToValue(input_data, parameters.prior_mu);
184 convertStringToValue(input_data, parameters.prior_nu);
185 convertStringToValue(input_data, parameters.prior_s2);
186 convertStringToValue(input_data, parameters.report_level);
187 convertStringToValue(input_data, parameters.max_iter);
188 convertStringToValue(input_data, parameters.tolerance_func);
189 convertStringToValue(input_data, parameters.tolerance_sol);
190 convertStringToValue(input_data, parameters.lambda_limit);
191 convertStringToValue(input_data, parameters.lambda);
192 convertStringToString(input_data, parameters.curvature_mode);
193
194 convertStringToValue(input_data, parameters.use_prune);
195 convertStringToValue(input_data, parameters.start_prune_at);
196 convertStringToValue(input_data, parameters.nprune_vals);
197 convertStringToValue(input_data, parameters.prune_threshold);
198 convertStringToValue(input_data, parameters.es_threshold);
199 convertStringToValue(input_data, parameters.es_use_adaptive_threshold);
200 convertStringToValue(input_data, parameters.es_adaptive_A);
201 convertStringToValue(input_data, parameters.es_adaptive_floor);
202 convertStringToValue(input_data, parameters.es_t_min);
203 convertStringToValue(input_data, parameters.es_delta_explore);
204 convertStringToValue(input_data, parameters.es_half_life);
205
206 convertStringToString(input_data, parameters.method_rot);
207 convertStringToString(input_data, parameters.method_trans);
208 convertStringToString(input_data, parameters.load_file);
209 convertStringToString(input_data, parameters.save_file);
210
211 convertStringToString(input_data, parameters.debug_output_dir);
212 convertStringToString(input_data, parameters.debug_output_file_R);
213 convertStringToString(input_data, parameters.debug_output_file_E);
214 convertStringToString(input_data, parameters.debug_output_file_G);
215 convertStringToString(input_data, parameters.debug_output_file_extension);
216 convertStringToValue(input_data, parameters.debug_offset_from_mid_point);
217 convertStringToValue(input_data, parameters.debug_dy);
218 convertStringToValue(input_data, parameters.debug_dz);
219 convertStringToValue(input_data, parameters.debug_level);
220
221 convertStringToString(input_data, parameters.optimization_alg);
222 convertStringToString(input_data, parameters.check_derivative);
223
224 convertStringToValueArray(input_data, parameters.dist_sp);
225 convertStringToValueArray(input_data, parameters.param_trans);
226 convertStringToValueArray(input_data, parameters.cell_dimensions);
227
228 is.close();
229}
KeyValuePair< double[2]> param_trans
Definition Structures.h:440
KeyValuePair< double > T_anglerot_gp
Definition Structures.h:484
KeyValuePair< double > prior_s2
Definition Structures.h:650
KeyValuePair< double > es_t_min
Definition Structures.h:659
KeyValuePair< std::string > debug_output_file_G
Definition Structures.h:589
KeyValuePair< int > max_iter
Definition Structures.h:674
KeyValuePair< int > i_dist
Definition Structures.h:371
KeyValuePair< int > initrot_nogp
Definition Structures.h:467
KeyValuePair< std::string > optimization_alg
Definition Structures.h:614
KeyValuePair< bool > es_use_adaptive_threshold
Definition Structures.h:655
KeyValuePair< double[9]> cell_dimensions
Definition Structures.h:605
KeyValuePair< double > dimer_sep
Definition Structures.h:425
KeyValuePair< int > start_prune_at
Definition Structures.h:571
KeyValuePair< int > islarge_num_iter
Definition Structures.h:544
KeyValuePair< double > lambda
Definition Structures.h:678
KeyValuePair< double > prior_nu
Definition Structures.h:649
KeyValuePair< int > report_level
Definition Structures.h:673
KeyValuePair< double > T_anglerot_init
Definition Structures.h:474
KeyValuePair< double > jitter_sigma2
Definition Structures.h:613
KeyValuePair< std::string > debug_output_dir
Definition Structures.h:584
KeyValuePair< double > ratio_at_limit
Definition Structures.h:512
KeyValuePair< int > debug_level
Definition Structures.h:597
KeyValuePair< std::string > method_trans
Definition Structures.h:435
KeyValuePair< double > debug_dz
Definition Structures.h:596
KeyValuePair< double > prune_threshold
Definition Structures.h:581
KeyValuePair< double > es_delta_explore
Definition Structures.h:660
KeyValuePair< int > nprune_vals
Definition Structures.h:576
KeyValuePair< double > actdist_fro
Definition Structures.h:379
KeyValuePair< std::string > method_rot
Definition Structures.h:430
KeyValuePair< double > constSigma2
Definition Structures.h:618
KeyValuePair< bool > use_prune
Definition Structures.h:562
KeyValuePair< int > divisor_T_dimer_gp
Definition Structures.h:498
KeyValuePair< int > num_iter
Definition Structures.h:537
KeyValuePair< int > num_iter_initrot
Definition Structures.h:479
KeyValuePair< int > eval_image1
Definition Structures.h:446
KeyValuePair< double > lambda_limit
Definition Structures.h:677
KeyValuePair< double > debug_offset_from_mid_point
Definition Structures.h:593
KeyValuePair< double > disp_max
Definition Structures.h:505
KeyValuePair< double > tolerance_func
Definition Structures.h:675
KeyValuePair< std::string > check_derivative
Definition Structures.h:672
KeyValuePair< std::string > debug_output_file_E
Definition Structures.h:587
KeyValuePair< double[4]> dist_sp
Definition Structures.h:374
KeyValuePair< double > es_adaptive_A
Definition Structures.h:657
KeyValuePair< int > es_half_life
Definition Structures.h:661
KeyValuePair< std::string > load_file
Definition Structures.h:550
KeyValuePair< std::string > debug_output_file_extension
Definition Structures.h:591
KeyValuePair< double > tolerance_sol
Definition Structures.h:676
KeyValuePair< double > prior_mu
Definition Structures.h:648
KeyValuePair< int > num_bigiter
Definition Structures.h:532
KeyValuePair< double > sigma2
Definition Structures.h:616
KeyValuePair< std::string > curvature_mode
Definition Structures.h:679
KeyValuePair< int > i_run
Definition Structures.h:372
KeyValuePair< double > magnSigma2
Definition Structures.h:617
KeyValuePair< double > es_threshold
Definition Structures.h:654
KeyValuePair< double > debug_dy
Definition Structures.h:595
KeyValuePair< std::string > debug_output_file_R
Definition Structures.h:585
KeyValuePair< double > gp_sigma2
Definition Structures.h:612
KeyValuePair< double > es_adaptive_floor
Definition Structures.h:658
KeyValuePair< double > T_dimer
Definition Structures.h:453
KeyValuePair< int > num_iter_rot_gp
Definition Structures.h:490
KeyValuePair< std::string > save_file
Definition Structures.h:556

◆ writePlainFile()

template<typename T>
bool gpr::io::FileManager::writePlainFile ( const std::string file_name,
const T & field,
std::ios::openmode mode )

Definition at line 230 of file FileManager.inl.

232{
233 std::ofstream out_stream;
234
235 out_stream.open(file_name, mode);
236
237 if (!out_stream.is_open()) {
238 io::ErrorManager err;
239 err << "Error! Can't create file '" << file_name << "'!\n";
240 return false;
241 }
242
243 for (Eigen::Index n = 0; n < field.size(); ++n) {
244 out_stream << *(field.data() + n) << "\n";
245 }
246 out_stream.close();
247 return true;
248}

◆ writeXYZFile()

bool gpr::io::FileManager::writeXYZFile ( const std::string file_name,
const gpr::Coord & field,
std::ios::openmode mode )

Definition at line 231 of file FileManager.cpp.

233{
234 const gpr::Index_t n = gpr::coord::numPoints(field);
235 const double cell[3] = {25.0, 25.0, 25.0};
236 const double angles[3] = {90.0, 90.0, 90.0};
237 readcon::RKRConFrameBuilder* builder = readcon::rkr_frame_new(
238 cell, angles, "Generated by gpridge", nullptr, "", "");
239 if (!builder) return EXIT_FAILURE;
240 for (gpr::Index_t i = 0; i < n; ++i) {
241 const Eigen::Vector3d v = gpr::coord::at(field, 0, i);
242 if (readcon::rkr_frame_add_atom(builder, "X", v(0), v(1), v(2), false,
243 static_cast<std::uint64_t>(i),
244 1.0) != 0) {
245 readcon::free_rkr_frame_builder(builder);
246 return EXIT_FAILURE;
247 }
248 }
249 readcon::RKRConFrame* frame = readcon::rkr_frame_builder_build(builder);
250 if (!frame) return EXIT_FAILURE;
251 const bool append = (mode & std::ios::app) != 0;
252 readcon::RKRConFrameWriter* writer =
253 append ? readcon::create_writer_from_path_append_c(file_name.c_str())
254 : readcon::create_writer_from_path_c(file_name.c_str());
255 if (!writer) {
256 readcon::free_rkr_frame(frame);
257 return EXIT_FAILURE;
258 }
259 const readcon::RKRConFrame* frames[] = {frame};
260 const int status = readcon::rkr_writer_extend(writer, frames, 1);
261 readcon::free_rkr_writer(writer);
262 readcon::free_rkr_frame(frame);
263 return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
264}
int status
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
Index_t numPoints(const FieldMatrixd &mat)

The documentation for this class was generated from the following files: