Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testSourceSplits.cpp
Go to the documentation of this file.
10#include <DiophantineSolver.h>
11#include <Rational.h>
14#include <StrUtilities.h>
15#include <EnvVariable.h>
17#include <SimplexReader.h>
18#include <type_traits>
19#include <stdexcept>
20#include <iostream>
21
22namespace
23{
24 struct Unrelated {};
25 void check(bool ok, const char* message)
26 {
27 if(!ok)
28 {
29 throw std::runtime_error(message);
30 }
31 }
32 template<class Record>
33 void roundTrip(const Record& original)
34 {
35 std::stringstream first;
36 first << original;
37 Record restored(first);
38 check(!first.fail(), "Record parsing failed");
39 std::stringstream second;
40 second << restored;
41 check(first.str()==second.str(), "Record serialization changed");
42 }
43}
44
45int main()
46{
47 using namespace model;
48 static_assert(std::is_constructible_v<SphericalInclusionIO<3>,const SphericalInclusion<3>&>);
49 static_assert(std::is_constructible_v<PolyhedronInclusionIO<3>,const PolyhedronInclusion<3>&>);
50 static_assert(std::is_constructible_v<EllipsoidalInclusionIO<3>,const EllipsoidalInclusion<3>&>);
51 static_assert(!std::is_constructible_v<SphericalInclusionIO<3>,const Unrelated&>);
52 static_assert(!std::is_constructible_v<PolyhedronInclusionIO<3>,const Unrelated&>);
53 static_assert(!std::is_constructible_v<EllipsoidalInclusionIO<3>,const Unrelated&>);
54 try
55 {
56 Eigen::Matrix3d e;
57 e << 1,2,3,4,5,6,7,8,9;
58 const Eigen::Vector3d center(1,-2,3);
59 const SphericalInclusion<3> sphere(center,2.5,e,0.3,1.0,0.75,2,nullptr);
60 const SphericalInclusionIO<3> sphereIO(sphere);
61 check(sphereIO.C==center && sphereIO.a==2.5 && sphereIO.eT==e && sphereIO.inclusionID==sphere.sID,
62 "Sphere conversion changed fields");
63 roundTrip(sphereIO);
64 const std::map<size_t,PolyhedronInclusionNodeIO<3>> nodes;
65 const std::map<size_t,std::vector<size_t>> faces;
66 const PolyhedronInclusion<3> poly(nodes,faces,e,0.3,1.0,0.6,4,nullptr);
67 const PolyhedronInclusionIO<3> polyIO(poly);
68 check(polyIO.eT==e && polyIO.inclusionID==poly.sID && polyIO.phaseID==4 && polyIO.mobilityReduction==0.6,
69 "Polyhedron conversion changed fields");
70 roundTrip(polyIO);
71 roundTrip(PolyhedronInclusionNodeIO<3>(9,center));
72 roundTrip(PolyhedronInclusionEdgeIO(3,4,5,6));
73 std::stringstream sphereText("7 1 -2 3 2.5 1 2 3 4 5 6 7 8 9 0.75 2");
74 const SphericalInclusionIO<3> parsed(sphereText);
75 check(parsed.inclusionID==7 && parsed.C==center && parsed.eT==e && parsed.phaseID==2,
76 "Sphere record field order changed");
77 const BestRationalApproximation best(0.125,100);
78 check(best.num==1 && best.den==8,"Rational approximation changed");
79 const Rational sum=Rational(1,3)+Rational(1,6);
80 check(sum.n==1 && sum.d==2 && sum.asDouble()==0.5,"Rational arithmetic changed");
81 long x=0,y=0;
83 check(6*x+9*y==3,"Diophantine solution changed");
84 const Eigen::Matrix2d A=Eigen::Matrix2d::Identity()*2;
85 Eigen::Matrix<double,1,2> B;
86 B << 1,1;
87 const Eigen::Vector2d f(3,5);
88 const Eigen::VectorXd g=Eigen::VectorXd::Constant(1,1);
89 const SchurComplementSolver solve(A,B,f,g);
90 check((A*solve.X+B.transpose()*solve.L-f).norm()<1e-12 && (B*solve.X-g).norm()<1e-12,
91 "Schur solve changed");
92 const CatmullRomSplineSegment<3> segment(Eigen::Vector3d(0,0,0),Eigen::Vector3d(1,0,0),
93 Eigen::Vector3d(2,0,0),Eigen::Vector3d(3,0,0));
94 check(segment.get_r(0.25).isApprox(Eigen::Vector3d(1.25,0,0)) &&
95 segment.get_ru(0.5).isApprox(Eigen::Vector3d(1,0,0)) && segment.get_ruu(0.5).norm()<1e-12,
96 "Spline evaluation changed");
97 check(StrUtilities::lowercase("AbC")=="abc" && StrUtilities::removeSpaces(" a\t b ")=="ab",
98 "String utility changed");
99 check(!EnvVariable("PATH").empty(),"Environment variable lookup failed");
101 GaussLegendreRecursiveRelation::c(2)==0.5,"Quadrature recurrence changed");
103 reader.nodes().emplace(9,center);
104 check(reader.simplexReader().nodes().at(9)==center,"Mesh reader accessor changed");
105 reader.clear();
106 check(reader.nodes().empty() && reader.elements().empty(),"Mesh reader clear failed");
107 std::cout << "PASS extracted APIs, concrete inclusion constructors, serialization and numerics\n";
108 }
109 catch(const std::exception& e)
110 {
111 std::cerr << e.what() << '\n';
112 return 1;
113 }
114 return 0;
115}
VectorDim get_ru(const double &u) const
VectorDim get_r(const double &u) const
VectorDim get_ruu(const double &u) const
static void solveDiophantine2vars(long int a, long int b, long int c, long int &x, long int &y)
long int d
Definition Rational.h:28
long int n
Definition Rational.h:27
double asDouble() const
Solves the system of equations in block-form:
Eigen::VectorXd X
The solution vector.
Eigen::VectorXd L
The vector of Lagrange multipliers.
initial point of the source segment B
function infiniteDisplacement syms Ax Ay Az Bx By Bz qx qy qz u L x y z real A
function int_R syms a b c u real f
Definition int_R.m:4
x
Definition int_R.m:12
A simple class used to extract environment variables.
Definition EnvVariable.h:35
static double a(const int &n)
Class template defining the coefficients A, B and C of the recursive relation for orthogonal Legendre...
static double c(const int &n)
The coefficient c.
static double b(const int &n)
The coefficient b.
SimplexReaderBase< dim > & simplexReader()
const ElementContainerType & elements() const
const NodeContainerType & nodes() const
static std::string lowercase(std::string text)
static std::string removeSpaces(std::string key)
void check(bool ok, const char *message)
M sphere(const V &r, double a, const M &e, double n=nu, double m=mu)
int main()
int e
b poly
vertex first