TBTK
TBTK::Exporter Class Reference

Exports data on human readable format. More...

#include <Exporter.h>

Public Types

enum  Format { RowMajor, ColumnMajor }
 

Public Member Functions

 Exporter ()
 
template<typename DataType >
void save (const Property::AbstractProperty< DataType > &abstractProperty, const std::string &filename) const
 
template<typename DataType >
void save (const Property::AbstractProperty< DataType > &abstractProperty, const Index &pattern, const std::string &filename) const
 
template<typename DataType >
void save (const Array< DataType > &array, const std::string &filename) const
 
void setFormat (Format format)
 

Detailed Description

Exports data on human readable format.

The Exporter can write Arrays and Properties to file.

Output format

Row major order

By default, the data is written to file on row major order. This means that the right most Subindex increments the fastest, while the left most subindex changes the slowest. For Properties with a block structure, the intra block index should be understood as standing to the right of any Index. Similarly, any data type with indexable internal structure should be understood as having its indices to the right of any Index and intra block index.

For example, assume the property has index structure {x, y}, a block index n, and data type std::complexa<double>. The total index structure can then be thought of as being {x, y, n, c}, where c=0 and c=1 corresponds to real and imaginary part, respectively. If the dimensions are given by {SIZE_X, SIZE_Y, BLOCK_SIZE, 2}, the element order is given by

\(2*(BLOCK\_SIZE*(SIZE\_Y*x + y) + n) + c\).

Column major order

Languages such as Fortran and MATLAB use column major order. To simplify import to such languages, it is also possible to export the data on column major order. To do so, set the format to column major before exporting the data.

exporter.setFormat(Exporter::Format::ColumnMajor);

Arrays

Arrays are exported as follows

Exporter exporter;
exporter.save(array, "Filename");

Properties on the None and Ranges format

Properties that are on the formats IndexDescriptor::Format::None and IndexDescriptor::Format::Ranges can be exported as follows.

Exporter exporter;
exporter.save(property, "Filename");

Properties on the Custom format

Properties that are on the format IndexDescriptor::Format::Custom can be exported as follows.

Exporter exporter;
exporter.save({_a_, 5, _a_}, property, "Filename");

Here it is assumed that property has the index structure {x, y, z} and that the data for all indices satisfying the pattern {x, 5, z}. are to be exported.

Export to external languages

Below we demonstrate how to export data to other languages. The example is for a density with the index structure {x, y, z} and with the number of elements for the corresponding dimensions being {SIZE_X, SIZE_Y, SIZE_Z}.

MATLAB

Export from C++

Exporter exporter;
exporter.setFormat(Exporter::Format::ColumnMajor);
exporter.save({_a_, _a_, _a_}, density, "Filename");

Import to MATLAB

data = dlmread('Filename')
density = reshape(data, [SIZE_X, SIZE_Y, SIZE_Z])

Python

Export from C++

Exporter exporter;
exporter.save({_a_, _a_, _a_}, density, "Filename");

Import to Python

import numpy as np
density = np.loadtxt("Filename").reshape(SIZE_X, SIZE_Y, SIZE_Z)

Member Enumeration Documentation

◆ Format

Enum class for

Constructor & Destructor Documentation

◆ Exporter()

TBTK::Exporter::Exporter ( )
inline

Default constructor.

Member Function Documentation

◆ save() [1/3]

template<typename DataType >
void TBTK::Exporter::save ( const Property::AbstractProperty< DataType > &  abstractProperty,
const std::string &  filename 
) const

Export a Property on the format IndexDescriptor::Format::None or IndexDescriptor::Format::Ranges.

Parameters
abstractPropertyThe Property to export.
filenameThe name of the file to write the data to.

◆ save() [2/3]

template<typename DataType >
void TBTK::Exporter::save ( const Property::AbstractProperty< DataType > &  abstractProperty,
const Index pattern,
const std::string &  filename 
) const

Export a Property on the format IndexDescriptor::Format::Custom.

Parameters
abstractPropertyThe Property to export.
patternPattern that determines which points to export the data for.
filenameThe name of the file to write the data to.

◆ save() [3/3]

template<typename DataType >
void TBTK::Exporter::save ( const Array< DataType > &  array,
const std::string &  filename 
) const

Export an Array to file.

Parameters
arrayThe Array to export.
filenameThe name of file to write the data to.

◆ setFormat()

void TBTK::Exporter::setFormat ( Format  format)
inline

Set the output format. The default value is Format::RowMajor

Parameters
formatThe output format to use.

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