preCICE v3.1.2
Loading...
Searching...
No Matches
TXTTableWriter.cpp
Go to the documentation of this file.
1#include "TXTTableWriter.hpp"
2#include <algorithm>
3#include <iomanip>
5#include "utils/Helpers.hpp"
6#include "utils/assertion.hpp"
7
8namespace precice::io {
9
11 const std::string &filename)
12 : _data(),
13 _writeIterator(_data.end()),
14 _outputStream()
15{
16 _outputStream.open(filename);
17 PRECICE_CHECK(_outputStream, "TXT table writer failed to open file \"{}\"", filename);
18
19 _outputStream.setf(std::ios::showpoint);
20 _outputStream.setf(std::ios::fixed);
22}
23
25 const std::string &name,
26 DataType type)
27{
29 Data data;
30 data.name = name;
31 data.type = type;
32 _data.push_back(data);
33
34 std::string delimiter = _data.empty() ? "" : " ";
35 if ((type == INT) || (type == DOUBLE)) {
36 _outputStream << delimiter << name;
37 } else {
38 _outputStream << delimiter << name << 0;
39 if (type == VECTOR2D) {
40 for (int i = 1; i < 2; i++) {
41 _outputStream << " " << name << i;
42 }
43 } else {
44 PRECICE_ASSERT(type == VECTOR3D);
45 for (int i = 1; i < 3; i++) {
46 _outputStream << " " << name << i;
47 }
48 }
49 }
50 // Print out everything apart from INT consistently in scientific
51 // notation using a fixed precision
52 if (type == DOUBLE || type == VECTOR2D || type == VECTOR3D) {
54 }
55 _writeIterator = _data.end();
56}
57
59 const std::string &name,
60 int value)
61{
63 PRECICE_ASSERT(not _data.empty());
64 if (_writeIterator == _data.end()) {
65 _writeIterator = _data.begin();
66 _outputStream << "\n";
67 }
70
71 std::string delimiter = _writeIterator == _data.begin() ? "" : " ";
72 _outputStream << delimiter << std::setw(6) << value;
74 if (_writeIterator == _data.end()) {
76 }
77}
78
80 const std::string &name,
81 double value)
82{
84 PRECICE_ASSERT(not _data.empty());
85 if (_writeIterator == _data.end()) {
86 _writeIterator = _data.begin();
87 _outputStream << "\n";
88 }
91
92 std::string delimiter = _writeIterator == _data.begin() ? "" : " ";
93 _outputStream << delimiter << std::setw(15) << value;
95 if (_writeIterator == _data.end()) {
97 }
98}
99
101 const std::string & name,
102 const Eigen::Vector2d &value)
103{
105 PRECICE_ASSERT(not _data.empty());
106 if (_writeIterator == _data.end()) {
107 _writeIterator = _data.begin();
108 _outputStream << "\n";
109 }
112
113 std::string delimiter = _writeIterator == _data.begin() ? "" : " ";
114 _outputStream << delimiter << std::setw(15) << value[0];
115
116 for (int i = 1; i < value.size(); i++) {
117 _outputStream << " " << std::setw(15) << value[i];
118 }
120 if (_writeIterator == _data.end()) {
122 }
123}
124
126 const std::string & name,
127 const Eigen::Vector3d &value)
128{
130 PRECICE_ASSERT(not _data.empty());
131 if (_writeIterator == _data.end()) {
132 _writeIterator = _data.begin();
133 _outputStream << "\n";
134 }
137
138 std::string delimiter = _writeIterator == _data.begin() ? "" : " ";
139 _outputStream << delimiter << std::setw(15) << value[0];
140 for (int i = 1; i < value.size(); i++) {
141 _outputStream << " " << std::setw(15) << value[i];
142 }
144 if (_writeIterator == _data.end()) {
146 }
147}
148
154
157{
158 _data.clear();
159 _writeIterator = _data.end();
160}
161
162} // namespace precice::io
163
164auto fmt::formatter<precice::io::TXTTableWriter::DataType>::format(precice::io::TXTTableWriter::DataType c, format_context &ctx) const
165{
166 std::string_view name = "unknown";
167 switch (c) {
169 name = "int";
170 break;
172 name = "double";
173 break;
175 name = "vector2D";
176 break;
178 name = "vector3D";
179 break;
180 }
181 return formatter<string_view>::format(name, ctx);
182}
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:35
std::string name
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
T begin(T... args)
void close()
Closes the file, is automatically called on destruction.
DataType
Constants defining possible data types to be written.
std::vector< Data >::const_iterator _writeIterator
void addData(const std::string &name, DataType type)
Adds a data entry to the table.
TXTTableWriter(const std::string &filename)
Constructor, opens file.
void reset()
Resets the table information.
void writeData(const std::string &name, int value)
Writes a integral scalar data value associated to the entry name.
T close(T... args)
T empty(T... args)
T scientific(T... args)
T flush(T... args)
T is_open(T... args)
provides Import and Export of the coupling mesh and data.
T open(T... args)
T setf(T... args)
T setprecision(T... args)
T setw(T... args)
Represents one data entry to be written.