Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testDDconfigEllipsoidalIO.cpp
Go to the documentation of this file.
2#include <array>
3#include <filesystem>
4#include <sstream>
5#include <stdexcept>
6
8void check(bool ok, const char *message)
9{
10 if(!ok)
11 {
12 throw std::runtime_error(message);
13 }
14}
15std::string text(Config &config)
16{
17 std::ostringstream out;
18 config.writeTxtStream(out);
19 return out.str();
20}
21int main()
22{
23 try
24 {
25 Config defaultConfig;
26 check(defaultConfig.getTxtFilename(7) == "config/evl_7.txt", "Default configuration folder is wrong");
27 const auto directory =
28 std::filesystem::temp_directory_path() /
29 ("model-ddconfig-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
30 std::filesystem::create_directory(directory);
31 Config config(directory.string()), restored(directory.string());
32 Eigen::Matrix3d axes;
33 axes << 0, -2, 0, 3, 0, 0, 0, 0, 1;
34 Eigen::Matrix3d strain;
35 strain << .01, .003, -.004, .003, -.002, .005, -.004, .005, .007;
36 const Eigen::Vector3d center(1, -2, 3);
37 config.sphericalInclusions().emplace_back(10, center, 2.0, strain, .5, 1);
38 config.ellipsoidalInclusions().emplace_back(20, center, axes, strain, .625, 2);
39 config.ellipsoidalInclusions().emplace_back(21, -center, 2 * axes, -strain, .75, 3);
40 for(size_t id = 30; id < 33; ++id)
41 {
42 config.polyhedronInclusions().emplace_back(id, strain, .875, 4);
43 }
44 config.polyhedronInclusionNodes().emplace_back(100, center);
45 config.polyhedronInclusionEdges().emplace_back(30, 0, 100, 100);
46 const std::array<size_t, 11> counts{{0, 0, 0, 0, 1, 2, 3, 1, 1, 0, 0}};
47 const auto serialized = text(config);
48 std::istringstream header(serialized);
49 std::string line;
50 for(auto count : counts)
51 {
52 std::getline(header, line);
53 check(std::stoull(line) == count, "Text count order is wrong");
54 }
55 for(auto id : {10, 20, 21, 30, 31, 32, 100, 30})
56 {
57 std::getline(header, line);
58 check(std::stoull(line) == static_cast<size_t>(id), "Text block order is wrong");
59 }
60 std::istringstream input(serialized);
61 restored.readTxtStream(input);
62 check(text(restored) == serialized, "Text round trip failed");
63 const Config &constConfig = restored;
64 check(constConfig.ellipsoidalInclusions().size() == 2, "Const accessor failed");
65
66 config.writeBin(0);
67 std::ifstream binary(config.getBinFilename(0), std::ios::binary);
68 for(auto count : counts)
69 {
70 size_t actual = 0;
71 binary.read(reinterpret_cast<char *>(&actual), sizeof(actual));
72 check(binary.good() && actual == count, "Binary count order is wrong");
73 }
75 binary.read(reinterpret_cast<char *>(&sphere), sizeof(sphere));
76 check(sphere.inclusionID == 10, "Binary spherical block is misplaced");
77 for(size_t id : {20, 21})
78 {
80 binary.read(reinterpret_cast<char *>(&ellipsoid), sizeof(ellipsoid));
81 check(ellipsoid.inclusionID == id, "Binary ellipsoidal block is misplaced");
82 }
84 binary.read(reinterpret_cast<char *>(&polyhedron), sizeof(polyhedron));
85 check(polyhedron.inclusionID == 30, "Binary polyhedron block is misplaced");
86 binary.close();
87 restored.readBin(0);
88 check(text(restored) == serialized, "Binary round trip failed");
89 restored.bin2txt(0, false);
90 restored.readTxt(0);
91 check(text(restored) == serialized, "Binary-to-text conversion failed");
92
93 // Trailing text matrices must remain aligned after the new inclusion block.
94 config.displacementMatrix().setConstant(2, 6, 1.25);
95 config.cdMatrix().setConstant(1, config.cdMatrix().cols(), 2.5);
96 std::istringstream withMatrices(text(config));
97 restored.readTxtStream(withMatrices);
98 check(text(restored) == text(config), "Trailing matrices were shifted");
99
100 config.clear();
101 check(config.ellipsoidalInclusions().empty(), "clear() retained ellipsoids");
102 const auto empty = text(config);
103 std::istringstream emptyInput(empty);
104 restored.readTxtStream(emptyInput);
105 check(text(restored) == empty, "Empty text read retained old data");
106 config.writeBin(1);
107 restored.readBin(1);
108 check(text(restored) == empty, "Empty binary round trip failed");
109 std::filesystem::remove_all(directory);
110 std::cout << "PASS DefectiveCrystal configuration defaults, inclusion ordering, text/binary round trips, conversion, matrices, and clear\n";
111 return 0;
112 }
113 catch(const std::exception &error)
114 {
115 std::cerr << error.what() << '\n';
116 return 1;
117 }
118}
const std::vector< EllipsoidalInclusionIO< dim > > & ellipsoidalInclusions() const
void bin2txt(const size_t &runID, const bool &writeSegments)
std::string getTxtFilename(const size_t &runID)
model::DefectiveCrystalConfigIO< 3 > Config
void check(bool ok, const char *message)
std::string text(Config &config)
M sphere(const V &r, double a, const M &e, double n=nu, double m=mu)
void out