OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
DirichletPlaneWriter.cpp
Go to the documentation of this file.
1//
2// Class DirichletPlaneWriter
3// Writes interpolated potential values on a 2D dirichlet plane to ASCII files.
4//
5
7
8#include "Ippl.h"
10
11#include <cctype>
12#include <fstream>
13#include <iomanip>
14#include <sstream>
15
16namespace {
17 std::string sanitizeTag(const std::string& tag) {
18 std::string result = tag;
19 for (char& c : result) {
20 const unsigned char uc = static_cast<unsigned char>(c);
21 if (!std::isalnum(uc) && c != '-' && c != '_') {
22 c = '_';
23 }
24 }
25 return result.empty() ? std::string("solve") : result;
26 }
27} // namespace
28
29DirichletPlaneWriter::DirichletPlaneWriter(const std::string& outputDirectory)
30 : outputDirectory_m(outputDirectory) {
31 Inform m("DirichletPlaneWriter::DirichletPlaneWriter");
32 std::error_code ec;
33 std::filesystem::create_directories(outputDirectory_m, ec);
34 if (ec) {
35 throw OpalException(
36 "DirichletPlaneWriter::DirichletPlaneWriter", "Failed to create output directory \""
37 + outputDirectory_m.string()
38 + "\": " + ec.message());
39 }
40
41 m << level4 << "Dirichlet-plane diagnostics output directory: \"" << outputDirectory_m.string()
42 << "\"." << endl;
43}
44
46 long long step, double time, double zPlane, const std::vector<double>& xCoords,
47 const std::vector<double>& yCoords, const std::vector<double>& phiValues, std::size_t nx,
48 std::size_t ny, const std::string& solveTag) {
49 if (xCoords.size() != nx || yCoords.size() != ny || phiValues.size() != nx * ny) {
50 throw OpalException(
51 "DirichletPlaneWriter::writePlane",
52 "Invalid plane dimensions for dirichlet-plane dump.");
53 }
54
55 const std::string safeTag = sanitizeTag(solveTag);
56
57 std::ostringstream fileName;
58 fileName << "phi_dirichlet_" << safeTag << "_step-" << std::setfill('0') << std::setw(8) << step
59 << ".dat";
60
61 const std::filesystem::path filePath = outputDirectory_m / fileName.str();
62 std::ofstream out(filePath.string(), std::ios::out | std::ios::trunc);
63 if (!out) {
64 throw OpalException(
65 "DirichletPlaneWriter::writePlane",
66 "Failed to open output file \"" + filePath.string() + "\".");
67 }
68
69 out << std::setprecision(17);
70 out << "# Dirichlet-plane potential dump\n";
71 out << "# step=" << step << " time=" << time << " [s] zPlane=" << zPlane
72 << " [m] tag=" << safeTag << "\n";
73 out << "# nx=" << nx << " ny=" << ny << "\n";
74 out << "# columns: i j x[m] y[m] phi[V]\n";
75
76 for (std::size_t i = 0; i < nx; ++i) {
77 for (std::size_t j = 0; j < ny; ++j) {
78 const std::size_t idx = i * ny + j;
79 out << std::setw(6) << i << std::setw(6) << j << " " << std::scientific << xCoords[i]
80 << " " << yCoords[j] << " " << phiValues[idx] << "\n";
81 }
82 }
83}
std::filesystem::path outputDirectory_m
DirichletPlaneWriter(const std::string &outputDirectory)
Create a writer that emits files into outputDirectory.
void writePlane(long long step, double time, double zPlane, const std::vector< double > &xCoords, const std::vector< double > &yCoords, const std::vector< double > &phiValues, std::size_t nx, std::size_t ny, const std::string &solveTag)
Write one plane snapshot.
constexpr double c
The velocity of light in m/s.
Definition Physics.h:60