Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testPeriodicFieldInitialization.cpp
Go to the documentation of this file.
3#include <filesystem>
4#include <fstream>
5#include <iomanip>
6#include <chrono>
7#include <iostream>
8#include <set>
9
10void require(bool condition, const char* message)
11{
12 if(!condition)
13 {
14 throw std::runtime_error(message);
15 }
16}
17
18int main(int argc, char** argv)
19{
20 try
21 {
22 require(argc == 2, "Expected repository root");
23 const std::filesystem::path root(argv[1]);
24 const auto folder = std::filesystem::temp_directory_path() /
25 ("periodic-fields-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
26 const auto input = folder / "inputFiles";
27 std::filesystem::create_directories(input);
28 std::filesystem::create_directories(folder / "F");
29 std::filesystem::copy_file(root / "Library/Materials/W.txt", input / "W.txt");
30 const auto meshFile = root / "Library/Meshes/unitCube24.msh";
31 std::filesystem::copy_file(meshFile, input / "mesh.msh");
32 model::SimplicialMesh<3> mesh(meshFile.string(), Eigen::Matrix3d::Identity(), Eigen::Vector3d::Zero(), {-1});
33 std::vector<int> seeds;
34 for(int axis = 0; axis < 3; ++axis)
35 {
36 for(const auto& region : mesh.regions())
37 {
38 for(const auto& face : region.second->faces())
39 {
40 const auto& pair = face.second->periodicFacePair;
41 if(pair.second && pair.first[axis] > 0.9 &&
42 pair.first.normalized().cross(Eigen::Vector3d::Unit(axis)).norm() < 1.e-8)
43 {
44 seeds.push_back(face.first);
45 }
46 }
47 }
48 }
49 require(seeds.size() == 3, "Expected three cube periodic directions");
50 for(int rank = 0; rank <= 3; ++rank)
51 {
52 for(int imageCount : {0, 1})
53 {
54 {
55 std::ofstream f(input / "DefectiveCrystal.txt");
56 f << "physics=DislocationDynamics;\nuseFEM=0;\nNsteps=0;\nmaxResolveSteps=0;\ndtMax=1;\n"
57 << "outputFrequency=1;\noutputBinary=0;\nstartAtTimeStep=0;\n";
58 if(rank > 0)
59 {
60 f << "periodicImageSize=";
61 for(int i = 0; i < rank; ++i)
62 {
63 f << imageCount << ' ';
64 }
65 f << ";\nEwaldLengthFactor=0.5;\n";
66 }
67 std::ofstream p(input / "polycrystal.txt");
68 const double length = 200.0 / std::sqrt(3.0);
69 p << std::setprecision(17) << "materialFile=W.txt;\nmeshFile=" << "mesh.msh"
70 << ";\nabsoluteTemperature=300;\nC2G1=1 0 0 0 1 0 0 0 1;\nF="
71 << length << " 0 0 0 " << length << " 0 0 0 " << length << ";\nX0=0 0 0;\nperiodicFaceIDs=";
72 for(int i = 0; i < rank; ++i)
73 {
74 p << seeds[i] << ' ';
75 }
76 p << ";\n";
77 }
78 model::DefectiveCrystalBase<3> base(folder.string());
79 require(base.periodicLatticeBasis.cols() == rank, "Incorrect active rank");
80 require(base.periodicImageSize.size() == size_t(rank), "Image counts ignored for active directions");
81 const size_t expected = imageCount == 0 ? 1 : size_t(std::pow(3, rank));
82 require(base.periodicShifts.size() == expected, "Incorrect image count");
83 std::set<std::vector<int>> coefficients;
84 for(const auto& shift : base.periodicShifts)
85 {
86 const Eigen::VectorXd coordinates = base.periodicLatticeReciprocalBasis.transpose() * shift;
87 std::vector<int> key;
88 for(int i = 0; i < rank; ++i)
89 {
90 key.push_back(std::lround(coordinates[i]));
91 require(std::abs(coordinates[i] - key.back()) < 1.e-8 && std::abs(key.back()) <= imageCount,
92 "Invalid periodic shift");
93 }
94 coefficients.insert(key);
95 }
96 require(coefficients.size() == expected, "Duplicate periodic shift");
97 require(rank == 0 ? base.dislocationDynamics().EwaldLength == 0.0 : base.dislocationDynamics().EwaldLength > 0.0,
98 "Ewald initialization depends on domain flag or image count");
99 }
100 }
101 std::filesystem::remove_all(folder);
102 std::cout << "PASS: image and Ewald initialization for zero, one, two and three periodic directions\n";
103 return 0;
104 }
105 catch(const std::exception& error)
106 {
107 std::cerr << error.what() << '\n';
108 return 1;
109 }
110}
function int_R syms a b c u real f
Definition int_R.m:4
int regions
Definition triangle.cpp:696
void require(bool condition, const char *message)
void argv
int axis
void b int argc
vertex p[3]
int i
newbad key