Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testEllipsoidalInclusionIO.cpp
Go to the documentation of this file.
3#include <iostream>
4#include <stdexcept>
5#include <string>
6
7namespace
8{
9 void check(const bool condition, const char *message)
10 {
11 if(!condition)
12 {
13 throw std::runtime_error(message);
14 }
15 }
16} // namespace
17
18int main()
19{
20 try
21 {
23 using Inclusion = model::EllipsoidalInclusion<3>;
24 const IO empty;
25 check(empty.inclusionID == 0 && empty.C.isZero() && empty.axes.isZero() && empty.eT.isZero() &&
26 empty.mobilityReduction == 1.0 && empty.phaseID == 0,
27 "Default record is not initialized");
28
29 // Read an independently specified record to catch row/column transpositions.
30 std::stringstream input("42 1 -2 3 0 -2 0 3 0 0 0 0 1 "
31 "0.01 0.003 -0.004 0.003 -0.002 0.005 "
32 "-0.004 0.005 0.007 0.625 7");
33 const IO record(input);
34 Eigen::Matrix3d axes;
35 axes << 0, -2, 0, 3, 0, 0, 0, 0, 1;
36 Eigen::Matrix3d strain;
37 strain << 0.01, 0.003, -0.004, 0.003, -0.002, 0.005, -0.004, 0.005, 0.007;
38 check(!input.fail() && record.inclusionID == 42 && record.phaseID == 7 && record.mobilityReduction == 0.625 &&
39 record.C.isApprox(Eigen::Vector3d(1, -2, 3)) && record.axes.isApprox(axes) &&
40 record.eT.isApprox(strain),
41 "Record field order is incorrect");
42
43 const Inclusion original(record.C, record.axes, record.eT, 0.31, 43.0, record.mobilityReduction, record.phaseID,
44 nullptr);
45 const IO fromInclusion(original);
46 check(fromInclusion.inclusionID == original.sID, "Inclusion ID was not copied");
47 const IO fromFields(record.inclusionID, record.C, record.axes, record.eT, record.mobilityReduction,
48 record.phaseID);
49 for(const IO *source : {&fromInclusion, &fromFields})
50 {
51 std::stringstream text;
52 text << *source;
53 check(text.str().find('\n') == std::string::npos, "Output spans multiple lines");
54 const IO restored(text);
55 check(!text.fail() && restored.inclusionID == source->inclusionID && restored.C.isApprox(source->C) &&
56 restored.axes.isApprox(source->axes) && restored.eT.isApprox(source->eT) &&
57 restored.mobilityReduction == source->mobilityReduction && restored.phaseID == source->phaseID,
58 "Text round trip failed");
59 std::string extra;
60 check(!(text >> extra), "Output contains unexpected extra fields");
61
62 const Inclusion reconstructed(restored.C, restored.axes, restored.eT, 0.31, 43.0,
63 restored.mobilityReduction, restored.phaseID, nullptr);
64 for(const Eigen::Vector3d &point : {record.C, Eigen::Vector3d(5, 6, -7)})
65 {
66 check(reconstructed.contains(point) == original.contains(point), "Round trip changed containment");
67 check(reconstructed.stress(point).isApprox(original.stress(point), 1.e-13),
68 "Round trip changed stress");
69 }
70 }
71
72 for(const char *invalid : {"", "42 1 2", "42 invalid"})
73 {
74 std::stringstream text(invalid);
75 const IO rejected(text);
76 check(text.fail(), "Invalid record did not set stream failure");
77 }
78 std::cout << "PASS EllipsoidalInclusionIO field order, round trips, reconstruction, and invalid input\n";
79 return 0;
80 }
81 catch(const std::exception &error)
82 {
83 std::cerr << error.what() << '\n';
84 return 1;
85 }
86}
void check(bool ok, const char *message)
std::string text(Config &config)