73 std::ofstream stream(path);
75 throw std::runtime_error(
"Cannot write cube file: " + path);
79 stream <<
"units: bohr, " << field.
unit <<
"; grid " << dimensions[0] <<
"x"
80 << dimensions[1] <<
"x" << dimensions[2] <<
", third axis fastest\n";
82 stream << std::fixed << std::setprecision(header_decimals);
83 stream << std::setw(5) << field.
number_of_atoms() << std::setw(header_width) << 0.0
84 << std::setw(header_width) << 0.0 << std::setw(header_width) << 0.0 <<
"\n";
85 for (
int axis = 0; axis < 3; axis++) {
86 stream << std::setw(5) << dimensions[axis];
87 for (
int component = 0; component < 3; component++) {
88 stream << std::setw(header_width)
93 for (
int atom_index = 0; atom_index < field.
number_of_atoms(); atom_index++) {
96 stream << std::setw(5) << field.
atomic_number(atom_index) << std::setw(header_width)
98 << position.
x << std::setw(header_width) << position.
y << std::setw(header_width)
99 << position.
z <<
"\n";
102 stream << std::scientific << std::setprecision(10);
103 int values_on_line = 0;
104 for (
int index_1 = 0; index_1 < dimensions[0]; index_1++) {
105 for (
int index_2 = 0; index_2 < dimensions[1]; index_2++) {
106 for (
int index_3 = 0; index_3 < dimensions[2]; index_3++) {
107 stream << std::setw(18) << field.
value(index_1, index_2, index_3);
108 if (++values_on_line == values_per_line) {
118 if (values_on_line != 0) {
127 throw std::runtime_error(
"Failed while writing cube file: " + path);
131 return stem +
".cube";
135 static constexpr int values_per_line = 6;
137 static constexpr int header_decimals = 12;
138 static constexpr int header_width = 20;
void write(const std::string &path, const VolumetricField &field) const override
Write field to path, throwing std::runtime_error if it cannot be written.
std::string default_file_name(const std::string &stem) const override
Vector3D cartesian_position_bohr(int atom_index) const
Cartesian position of one atom, in bohr.
int number_of_atoms() const
std::string unit
The unit values is in, e.g. "e/bohr^3".
Matrix3x3 lattice_vectors_bohr
Rows are a1, a2, a3, Cartesian, in bohr.
std::vector< int > atom_species_indices
Species of each atom, an index into species_symbols.
std::vector< Real > species_valence_charges
Valence charge of each species, parallel to species_symbols.
std::string description
Free text for a format's comment line, e.g. "charge density".
Real value(int index_1, int index_2, int index_3) const
The sample at grid point (i1, i2, i3), third index fastest.
int atomic_number(int atom_index) const
Atomic number of one atom, from its element symbol.
std::array< int, 3 > grid_dimensions
Grid point counts along a1, a2, a3.