preCICE v3.1.2
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 const std::string &name,
40 const std::string &location,
41 const mesh::Mesh & mesh)
42{
43 PRECICE_TRACE(name, location, mesh.getName());
45
46 // Ignore empty meshes
47 if (mesh.empty()) {
48 return;
49 }
50
51 // Construct full filename
52 std::string filename{name};
53 int rank{0};
56 filename.append("_").append(std::to_string(rank));
57 }
58 filename.append(".csv");
59
60 namespace fs = std::filesystem;
61 fs::path outfile(location);
62 if (not location.empty()) {
64 }
65 outfile /= filename;
66
67 // Prepare filestream
68 std::ofstream outFile(outfile.string(), std::ios::trunc);
69 const bool is3d = (mesh.getDimensions() == 3);
70
71 // write header
72 outFile << "PosX;PosY";
73 if (is3d) {
74 outFile << ";PosZ";
75 }
76 outFile << ";Rank";
77 for (const auto &data : mesh.data()) {
78 auto dataName = data->getName();
79 auto dim = data->getDimensions();
80 PRECICE_ASSERT(static_cast<std::size_t>(data->values().size()) == mesh.nVertices() * dim);
81 outFile << ';' << dataName;
82 if (dim == 2) {
83 outFile << "X;" << dataName << 'Y';
84 } else if (dim == 3) {
85 outFile << "X;" << dataName << "Y;" << dataName << 'Z';
86 }
87 }
88 outFile << '\n';
89
90 // Prepare writing data
92 for (const auto &data : mesh.data()) {
93 auto dim = data->getDimensions();
94 double *values = data->values().data();
95 for (int i = 0; i < dim; ++i) {
96 dataColumns.push_back({std::next(values, i), dim});
97 }
98 }
99
100 // write vertex data
101 const std::string rankCol = ";" + std::to_string(rank);
102 const auto size = mesh.nVertices();
103 for (std::size_t vid = 0; vid < size; ++vid) {
104 const auto &vertex = mesh.vertex(vid);
105 outFile << vertex.coord(0) << ';';
106 outFile << vertex.coord(1);
107 if (is3d) {
108 outFile << ";" << vertex.coord(2);
109 }
110 outFile << rankCol;
111 for (auto &dc : dataColumns) {
112 outFile << ';' << *dc;
113 dc.next();
114 }
115 outFile << '\n';
116 }
117}
118
119} // namespace precice::io
int stride
Definition ExportCSV.cpp:24
double const * ptr
Definition ExportCSV.cpp:23
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
std::string name
T advance(T... args)
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
virtual void doExport(const std::string &name, const std::string &location, const mesh::Mesh &mesh)
Does export. Has to be implemented in subclass.
Definition ExportCSV.cpp:38
Container and creator for meshes.
Definition Mesh.hpp:39
int getDimensions() const
Definition Mesh.cpp:98
const std::string & getName() const
Returns the name of the mesh, as set in the config file.
Definition Mesh.cpp:218
std::size_t nVertices() const
Returns the number of vertices.
Definition Mesh.cpp:63
Vertex & vertex(VertexID id)
Mutable access to a vertex by VertexID.
Definition Mesh.cpp:41
bool empty() const
Does the mesh contain any vertices?
Definition Mesh.hpp:88
const DataContainer & data() const
Allows access to all data.
Definition Mesh.cpp:170
static Rank getRank()
Current rank.
Definition IntraComm.cpp:42
static bool isParallel()
True if this process is running in parallel.
Definition IntraComm.cpp:62
T create_directories(T... args)
T empty(T... args)
provides Import and Export of the coupling mesh and data.
T next(T... args)
T push_back(T... args)
T to_string(T... args)