Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testEllipsoidalSpecification.cpp
Go to the documentation of this file.
3#include <filesystem>
4#include <chrono>
5#include <fstream>
6#include <stdexcept>
7
8void check(bool ok, const char *message)
9{
10 if(!ok)
11 {
12 throw std::runtime_error(message);
13 }
14}
15int main(int argc, char **argv)
16{
17 try
18 {
20 check(argc == 2, "Expected example filename");
21 const Spec defaults;
22 check(defaults.type == "EllipsoidalInclusion" && defaults.style == "Individual" && !defaults.parser &&
23 defaults.center.isZero() && defaults.crystalAxes_SI.isZero() && defaults.crystalEigenDistortion.isZero() &&
24 defaults.velocityReductionFactor == 1.0 && defaults.phaseID == 0 && !defaults.allowOverlap &&
25 !defaults.allowOutside,
26 "Incorrect specification defaults");
27 const Spec parsed(argv[1]);
28 Eigen::Matrix3d expectedAxes;
29 expectedAxes << 0, -2e-8, 0, 3e-8, 0, 0, 0, 0, 1e-8;
30 Eigen::Matrix3d expectedStrain;
31 expectedStrain << 1e-3, 2e-4, 0, 2e-4, -2e-3, 0, 0, 0, 3e-3;
32 check(parsed.center.isApprox(Eigen::Vector3d(200, 0, 0)) && parsed.crystalAxes_SI.isApprox(expectedAxes) &&
33 parsed.crystalEigenDistortion(1) == 2e-4 && parsed.crystalEigenDistortion(8) == 3e-3 &&
34 parsed.velocityReductionFactor == 1.0 && parsed.phaseID == 0 && !parsed.allowOverlap &&
35 !parsed.allowOutside,
36 "Example parsing failed");
37 const Eigen::Matrix3d strain = Eigen::Map<const Eigen::Matrix3d>(parsed.crystalEigenDistortion.data()).transpose();
38 check(strain.isApprox(expectedStrain), "Eigen-distortion ordering failed");
39 const double b_SI = 2.5e-10;
40 const model::EllipsoidalInclusion<3> inclusion(parsed.center, parsed.crystalAxes_SI / b_SI, strain, .31, 43.0,
41 parsed.velocityReductionFactor, parsed.phaseID, nullptr);
42 check(inclusion.contains(parsed.center) && inclusion.stress(parsed.center).allFinite(),
43 "Specification cannot construct an inclusion");
44
45 const auto file = std::filesystem::temp_directory_path() /
46 ("model-ellipsoid-spec-" +
47 std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()) + ".txt");
48 {
49 std::ofstream out(file);
50 out << "center=1 2 3;\ncrystalAxes_SI=1 2;\n";
51 }
52 bool rejected = false;
53 try
54 {
55 const Spec invalid(file.string());
56 }
57 catch(const std::runtime_error &)
58 {
59 rejected = true;
60 }
61 check(rejected, "Wrong axes dimensions accepted");
62 {
63 std::ofstream out(file);
64 out << "center=1 2 3;\ncrystalAxes_SI=1e-8 0 0 0 2e-8 0 0 0 3e-8;\n"
65 << "crystalEigenDistortion=0 0 0 0 0 0 0 0 0;\n"
66 << "velocityReductionFactor=.75;\nphaseID=4;\nallowOverlap=1;\nallowOutside=1;\n";
67 }
68 const Spec custom(file.string());
69 check(custom.center.isApprox(Eigen::Vector3d(1, 2, 3)) && custom.velocityReductionFactor == .75 &&
70 custom.phaseID == 4 && custom.allowOverlap && custom.allowOutside,
71 "Custom metadata parsing failed");
72 std::filesystem::remove(file);
73 std::cout
74 << "PASS specification defaults, file parsing, field order, unit conversion, and malformed dimensions\n";
75 return 0;
76 }
77 catch(const std::exception &error)
78 {
79 std::cerr << error.what() << '\n';
80 return 1;
81 }
82}
void check(bool ok, const char *message)
void out
void argv
void b int argc
int e