OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
TestDirichletPlaneWriter.cpp
Go to the documentation of this file.
1//
2// Unit tests for DirichletPlaneWriter.
3//
4
5#include <gtest/gtest.h>
6
7#include "Ippl.h"
10
11#include <filesystem>
12#include <fstream>
13#include <sstream>
14#include <string>
15#include <vector>
16
17class DirichletPlaneWriterTest : public ::testing::Test {
18protected:
19 static void SetUpTestSuite() {
20 int argc = 0;
21 char** argv = nullptr;
22 ippl::initialize(argc, argv);
23 }
24
25 static void TearDownTestSuite() { ippl::finalize(); }
26
27 static std::string readFile(const std::string& path) {
28 std::ifstream f(path);
29 std::stringstream ss;
30 ss << f.rdbuf();
31 return ss.str();
32 }
33};
34
35TEST_F(DirichletPlaneWriterTest, ConstructorCreatesOutputDirectory) {
36 const std::string dir = "dirichlet_writer_test_dir";
37 std::filesystem::remove_all(dir);
38
39 DirichletPlaneWriter writer(dir);
40
41 EXPECT_TRUE(std::filesystem::exists(dir));
42 EXPECT_TRUE(std::filesystem::is_directory(dir));
43}
44
45TEST_F(DirichletPlaneWriterTest, WritePlaneCreatesAsciiDumpWithMetadata) {
46 const std::string dir = "dirichlet_writer_dump_dir";
47 std::filesystem::remove_all(dir);
48
49 DirichletPlaneWriter writer(dir);
50
51 const std::size_t nx = 2;
52 const std::size_t ny = 3;
53 const std::vector<double> x = {0.0, 0.1};
54 const std::vector<double> y = {-0.2, 0.0, 0.2};
55 const std::vector<double> phi = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};
56
57 writer.writePlane(
58 /*step=*/7,
59 /*time=*/1.5e-9,
60 /*zPlane=*/0.25, x, y, phi, nx, ny, "legacy");
61
62 const std::string expectedFile = dir + "/phi_dirichlet_legacy_step-00000007.dat";
63 ASSERT_TRUE(std::filesystem::exists(expectedFile));
64
65 const std::string content = readFile(expectedFile);
66 EXPECT_NE(content.find("# Dirichlet-plane potential dump"), std::string::npos);
67 EXPECT_NE(content.find("step=7"), std::string::npos);
68 EXPECT_NE(content.find("zPlane=0.25"), std::string::npos);
69 EXPECT_NE(content.find("columns: i j x[m] y[m] phi[V]"), std::string::npos);
70}
71
72TEST_F(DirichletPlaneWriterTest, InvalidDimensionsThrow) {
73 const std::string dir = "dirichlet_writer_invalid_dims";
74 std::filesystem::remove_all(dir);
75
76 DirichletPlaneWriter writer(dir);
77
78 const std::vector<double> x = {0.0};
79 const std::vector<double> y = {0.0};
80 const std::vector<double> phi = {1.0};
81
82 EXPECT_THROW(
83 writer.writePlane(
84 /*step=*/0,
85 /*time=*/0.0,
86 /*zPlane=*/0.0, x, y, phi,
87 /*nx=*/2,
88 /*ny=*/1, "legacy"),
90}
TEST_F(DirichletPlaneWriterTest, ConstructorCreatesOutputDirectory)
static std::string readFile(const std::string &path)
Writes dirichlet-plane potential snapshots to ASCII files.
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.