preCICE v3.2.0
Loading...
Searching...
No Matches
ExportCSV.cpp
Go to the documentation of this file.
1#include "io/ExportCSV.hpp"
2
3#include <Eigen/Core>
4#include <filesystem>
5#include <fstream>
6#include <iomanip>
7#include <memory>
8#include "io/Export.hpp"
10#include "mesh/Data.hpp"
11#include "mesh/Edge.hpp"
12#include "mesh/Mesh.hpp"
14#include "mesh/Triangle.hpp"
15#include "mesh/Vertex.hpp"
16#include "utils/IntraComm.hpp"
17#include "utils/assertion.hpp"
18
19namespace precice::io {
20
21namespace {
22struct StridedAccess {
23 double const *ptr;
24 int stride;
25
26 double operator*() const
27 {
28 return *ptr;
29 }
30
31 void next()
32 {
33 std::advance(ptr, stride);
34 }
35};
36} // namespace
37
39 std::string_view participantName,
40 std::string_view location,
41 const mesh::Mesh &mesh,
42 ExportKind kind,
43 int frequency,
44 int rank,
45 int size)
46 : Export(participantName, location, mesh, kind, frequency, rank, size) {};
47
48void ExportCSV::doExport(int index, double time)
49{
50 PRECICE_TRACE(index, time, _mesh->getName());
51 PRECICE_ASSERT(index >= 0);
52 PRECICE_ASSERT(time >= 0.0);
53
54 if (!keepExport(index))
55 return;
56
57 // Construct filename
58 std::string filename;
59 if (isParallel()) {
60 // Mesh-Participant.r2.it2
61 filename = fmt::format("{}-{}.{}_{}.csv", _mesh->getName(), _participantName, _rank, formatIndex(index));
62 } else {
63 // Mesh-Participant.it2
64 filename = fmt::format("{}-{}.{}.csv", _mesh->getName(), _participantName, formatIndex(index));
65 }
66
67 namespace fs = std::filesystem;
68 fs::path outfile(_location);
69 if (not _location.empty()) {
71 }
72 outfile /= filename;
73
74 // Prepare filestream
75 std::ofstream outFile(outfile.string(), std::ios::trunc);
76 const bool is3d = (_mesh->getDimensions() == 3);
77
78 // write header
79 outFile << "PosX;PosY";
80 if (is3d) {
81 outFile << ";PosZ";
82 }
83 outFile << ";Rank";
84 for (const auto &data : _mesh->data()) {
85 if (data->timeStepsStorage().empty()) {
86 continue;
87 }
88 auto dataName = data->getName();
89 auto dim = data->getDimensions();
90 outFile << ';' << dataName;
91 if (dim == 2) {
92 outFile << "X;" << dataName << 'Y';
93 } else if (dim == 3) {
94 outFile << "X;" << dataName << "Y;" << dataName << 'Z';
95 }
96 }
97 outFile << '\n';
98
99 // Prepare writing data
100 std::vector<StridedAccess> dataColumns;
101 for (const auto &data : _mesh->data()) {
102 if (data->timeStepsStorage().empty()) {
103 continue;
104 }
105 auto dim = data->getDimensions();
106 double const *values = data->timeStepsStorage().last().sample.values.data();
107 for (int i = 0; i < dim; ++i) {
108 dataColumns.push_back({std::next(values, i), dim});
109 }
110 }
111
112 // write vertex data
113 const std::string rankCol = ";" + std::to_string(_rank);
114 const auto size = _mesh->nVertices();
115 for (std::size_t vid = 0; vid < size; ++vid) {
116 const auto &vertex = _mesh->vertex(vid);
117 outFile << vertex.coord(0) << ';';
118 outFile << vertex.coord(1);
119 if (is3d) {
120 outFile << ";" << vertex.coord(2);
121 }
122 outFile << rankCol;
123 for (auto &dc : dataColumns) {
124 outFile << ';' << *dc;
125 dc.next();
126 }
127 outFile << '\n';
128 }
129}
130
132{
133 // not supported by paraview
134}
135
136} // namespace precice::io
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
T advance(T... args)
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
ExportCSV(std::string_view participantName, std::string_view location, const mesh::Mesh &mesh, ExportKind kind, int frequency, int rank, int size)
Definition ExportCSV.cpp:38
void doExport(int index, double time) final override
Export the mesh and writes files.
Definition ExportCSV.cpp:48
void exportSeries() const final override
Export(std::string_view participantName, std::string_view location, const mesh::Mesh &mesh, ExportKind kind, int frequency, int rank, int size)
Definition Export.hpp:23
std::string _location
Definition Export.hpp:65
bool isParallel() const
Definition Export.cpp:7
std::string _participantName
Definition Export.hpp:64
const mesh::Mesh *const _mesh
Definition Export.hpp:66
bool keepExport(int index) const
Definition Export.hpp:59
std::string formatIndex(int index) const
Definition Export.cpp:12
Container and creator for meshes.
Definition Mesh.hpp:38
T create_directories(T... args)
provides Import and Export of the coupling mesh and data.
provides Mesh, Data and primitives.
contains the time interpolation logic.
Definition Sample.hpp:8
T next(T... args)
T push_back(T... args)
T to_string(T... args)