clustering  0.12
Clustering suite for molecular dynamics trajectories.
 All Classes Namespaces Files Functions Variables Typedefs
tools.hxx
1 /*
2 Copyright (c) 2015, Florian Sittel (www.lettis.net)
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice,
9  this list of conditions and the following disclaimer.
10 
11 2. Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18 SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 
26 #include "tools.hpp"
27 
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <iterator>
32 #include <map>
33 
34 namespace Clustering {
35 namespace Tools {
36 
37 template <typename NUM>
38 std::tuple<NUM*, std::size_t, std::size_t>
39 read_coords(std::string filename, std::vector<std::size_t> usecols) {
40  std::size_t n_rows=0;
41  std::size_t n_cols=0;
42  std::size_t n_cols_used=0;
43 
44  std::ifstream ifs(filename);
45  {
46  // determine n_cols
47  std::string linebuf;
48  while (linebuf.empty() && ifs.good()) {
49  std::getline(ifs, linebuf);
50  }
51  std::stringstream ss(linebuf);
52  n_cols = std::distance(std::istream_iterator<std::string>(ss),
53  std::istream_iterator<std::string>());
54  // go back to beginning to read complete file
55  ifs.seekg(0);
56  // determine n_rows
57  while (ifs.good()) {
58  std::getline(ifs, linebuf);
59  if ( ! linebuf.empty()) {
60  ++n_rows;
61  }
62  }
63  // go back again
64  ifs.clear();
65  ifs.seekg(0, std::ios::beg);
66  }
67  std::map<std::size_t, bool> col_used;
68  if (usecols.size() == 0) {
69  // use all columns
70  n_cols_used = n_cols;
71  for (std::size_t i=0; i < n_cols; ++i) {
72  col_used[i] = true;
73  }
74  } else {
75  // use only defined columns
76  n_cols_used = usecols.size();
77  for (std::size_t i=0; i < n_cols; ++i) {
78  col_used[i] = false;
79  }
80  for (std::size_t i: usecols) {
81  col_used[i] = true;
82  }
83  }
84  // allocate memory
85  // DC_MEM_ALIGNMENT is defined during cmake and
86  // set depending on usage of SSE2, SSE4_1, AVX or Xeon Phi
87  NUM* coords = (NUM*) _mm_malloc(sizeof(NUM)*n_rows*n_cols_used, DC_MEM_ALIGNMENT);
88  ASSUME_ALIGNED(coords);
89  // read data
90  for (std::size_t cur_row = 0; cur_row < n_rows; ++cur_row) {
91  std::size_t cur_col = 0;
92  for (std::size_t i=0; i < n_cols; ++i) {
93  NUM buf;
94  ifs >> buf;
95  if (col_used[i]) {
96  coords[cur_row*n_cols_used + cur_col] = buf;
97  ++cur_col;
98  }
99  }
100  }
101  return std::make_tuple(coords, n_rows, n_cols_used);
102 }
103 
104 
105 template <typename NUM>
106 void
107 free_coords(NUM* coords) {
108  _mm_free(coords);
109 }
110 
111 template <typename KEY, typename VAL>
112 void
113 write_map(std::string filename, std::map<KEY, VAL> mapping) {
114  std::ofstream ofs(filename);
115  if (ofs.fail()) {
116  std::cerr << "error: cannot open file '" << filename << "' for writing." << std::endl;
117  exit(EXIT_FAILURE);
118  }
119  for (auto key_val: mapping) {
120  ofs << key_val.first << " " << key_val.second << "\n";
121  }
122 }
123 
124 template <typename NUM>
125 std::vector<NUM>
126 read_single_column(std::string filename) {
127  std::vector<NUM> dat;
128  std::ifstream ifs(filename);
129  if (ifs.fail()) {
130  std::cerr << "error: cannot open file '" << filename << "'" << std::endl;
131  exit(EXIT_FAILURE);
132  } else {
133  while (ifs.good()) {
134  NUM buf;
135  ifs >> buf;
136  if ( ! ifs.fail()) {
137  dat.push_back(buf);
138  }
139  }
140  }
141  return dat;
142 }
143 
144 
145 template <typename NUM>
146 void
147 write_single_column(std::string filename, std::vector<NUM> dat, bool with_scientific_format) {
148  std::ofstream ofs(filename);
149  if (ofs.fail()) {
150  std::cerr << "error: cannot open file '" << filename << "' for writing." << std::endl;
151  exit(EXIT_FAILURE);
152  }
153  if (with_scientific_format) {
154  ofs << std::scientific;
155  }
156  for (NUM i: dat) {
157  ofs << i << "\n";
158  }
159 }
160 
161 template <typename NUM>
162 NUM
163 string_to_num(const std::string &s) {
164  std::stringstream ss(s);
165  NUM buf;
166  ss >> buf;
167  return buf;
168 }
169 
170 } // end namespace Tools
171 } // end namespace Clustering
172 
std::tuple< NUM *, std::size_t, std::size_t > read_coords(std::string filename, std::vector< std::size_t > usecols=std::vector< std::size_t >())
Definition: tools.hxx:39
void free_coords(NUM *coords)
free memory pointing to coordinates
Definition: tools.hxx:107
NUM string_to_num(const std::string &s)
convert std::string to number of given template format
Definition: tools.hxx:163
void write_map(std::string filename, std::map< KEY, VAL > mapping)
write key-value map to plain text file with key as first and value as second column ...
Definition: tools.hxx:113
void write_single_column(std::string filename, std::vector< NUM > dat, bool with_scientific_format=false)
write single column of numbers to given file. number type (int, float, ...) given as template paramet...
Definition: tools.hxx:147
std::vector< NUM > read_single_column(std::string filename)
read single column of numbers from given file. number type (int, float, ...) given as template parame...
Definition: tools.hxx:126