TBTK
Need a break? Support the development by playing Polarity Puzzles
Exporter.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_EXPORTER
24 #define COM_DAFER45_TBTK_EXPORTER
25 
26 #include "TBTK/AnnotatedArray.h"
27 #include "TBTK/MultiCounter.h"
28 #include "TBTK/Property/AbstractProperty.h"
29 #include "TBTK/PropertyConverter.h"
30 #include "TBTK/SpinMatrix.h"
31 
32 #include <complex>
33 #include <fstream>
34 #include <string>
35 #include <vector>
36 
37 namespace TBTK{
38 
133 class Exporter{
134 public:
136  enum class Format {RowMajor, ColumnMajor};
137 
139  Exporter();
140 
149  template<typename DataType>
150  void save(
151  const Property::AbstractProperty<DataType> &abstractProperty,
152  const std::string &filename
153  ) const;
154 
165  template<typename DataType>
166  void save(
167  const Property::AbstractProperty<DataType> &abstractProperty,
168  const Index &pattern,
169  const std::string &filename
170  ) const;
171 
176  template<typename DataType>
177  void save(
178  const Array<DataType> &array,
179  const std::string &filename
180  ) const;
181 
185  void setFormat(Format format);
186 private:
188  Format format;
189 
191  template<typename DataType>
192  void write(std::ofstream &stream, const DataType &value) const;
193 };
194 
196  format = Format::RowMajor;
197 }
198 
199 template<typename DataType>
201  const Property::AbstractProperty<DataType> &abstractProperty,
202  const std::string &filename
203 ) const{
205  = PropertyConverter::convert(abstractProperty);
206 
207  save(annotatedArray, filename);
208 }
209 
210 template<typename DataType>
212  const Property::AbstractProperty<DataType> &abstractProperty,
213  const Index &pattern,
214  const std::string &filename
215 ) const{
217  = PropertyConverter::convert(abstractProperty, pattern);
218 
219  save(annotatedArray, filename);
220 }
221 
222 template<typename DataType>
224  const Array<DataType> &array,
225  const std::string &filename
226 ) const{
227  Array<DataType> outputArray;
228  switch(format){
229  case Format::RowMajor:
230  outputArray = array;
231  break;
232  case Format::ColumnMajor:
233  outputArray = array.getArrayWithReversedIndices();
234  break;
235  default:
236  TBTKExit(
237  "Exporter::save()",
238  "Unknown format '" << static_cast<int>(format) << "'.",
239  ""
240  );
241  }
242 
243  std::ofstream fout(filename);
244  if(!fout){
245  TBTKExit(
246  "Exporter::save()",
247  "Unable to open file '" << filename << "'.",
248  ""
249  );
250  }
251  const std::vector<unsigned int> &ranges = outputArray.getRanges();
252  std::vector<unsigned int> begin = ranges;
253  std::vector<unsigned int> end = ranges;
254  std::vector<unsigned int> increment = ranges;
255  for(unsigned int n = 0; n < begin.size(); n++){
256  begin[n] = 0;
257  increment[n] = 1;
258  }
259  MultiCounter<unsigned int> counter(begin, end, increment);
260  for(counter.reset(); !counter.done(); ++counter)
261  write(fout, outputArray[counter]);
262  fout.close();
263 }
264 
265 inline void Exporter::setFormat(Format format){
266  this->format = format;
267 }
268 
269 template<typename DataType>
270 void Exporter::write(std::ofstream &stream, const DataType &value) const{
271  stream << value << "\n";
272 }
273 
274 template<>
275 inline void Exporter::write<std::complex<double>>(
276  std::ofstream &stream,
277  const std::complex<double> &value
278 ) const{
279  stream << real(value) << "\n";
280  stream << imag(value) << "\n";
281 }
282 
283 template<>
284 inline void Exporter::write<SpinMatrix>(
285  std::ofstream &stream,
286  const SpinMatrix &value
287 ) const{
288  switch(format){
289  case Format::RowMajor:
290  write(stream, value.at(0, 0));
291  write(stream, value.at(0, 1));
292  write(stream, value.at(1, 0));
293  write(stream, value.at(1, 1));
294  break;
295  case Format::ColumnMajor:
296  write(stream, value.at(0, 0));
297  write(stream, value.at(1, 0));
298  write(stream, value.at(0, 1));
299  write(stream, value.at(1, 1));
300  break;
301  default:
302  TBTKExit(
303  "Exporter::write()",
304  "This should never happen, contact the developer.",
305  ""
306  );
307  }
308 }
309 
310 }; //End of namesapce TBTK
311 
312 #endif
TBTK::Array
Multi-dimensional array.
Definition: Array.h:98
TBTK::AnnotatedArray
Array with additional information about its axes.
Definition: AnnotatedArray.h:41
TBTK::Exporter::Format
Format
Definition: Exporter.h:136
TBTK::MultiCounter
Helper class for flattening nested loops.
Definition: MultiCounter.h:42
TBTK::Exporter::save
void save(const Property::AbstractProperty< DataType > &abstractProperty, const std::string &filename) const
Definition: Exporter.h:200
TBTK::Exporter::setFormat
void setFormat(Format format)
Definition: Exporter.h:265
TBTK::MultiCounter::reset
void reset()
Definition: MultiCounter.h:174
AnnotatedArray.h
Array with additional information about its axes.
PropertyConverter.h
Converts Properties to AnnotatedArrays.
TBTK::Exporter
Exports data on human readable format.
Definition: Exporter.h:133
TBTK::Exporter::Exporter
Exporter()
Definition: Exporter.h:195
TBTK::Array::getRanges
const std::vector< unsigned int > & getRanges() const
Definition: Array.h:1228
TBTK::Array::getArrayWithReversedIndices
Array< DataType > getArrayWithReversedIndices() const
Definition: Array.h:1169
TBTK::Property::AbstractProperty
Abstract Property class.
Definition: AbstractProperty.h:101
TBTK::MultiCounter::done
bool done() const
Definition: MultiCounter.h:179
TBTK::PropertyConverter::convert
static AnnotatedArray< DataType, Subindex > convert(const Property::AbstractProperty< DataType > &abstractProperty)
Definition: PropertyConverter.h:114
TBTK::Index
Physical index.
Definition: Index.h:44