PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
testUtilLib.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "testUtilLib.h"
12#include "util/geom.h"
13#include "util/transformation.h"
14#include <fstream>
15#include "fmt/format.h"
16#include <string>
17
18namespace {
19
20void errExit(std::string msg){
21 std::cerr << msg;
22 exit(EXIT_FAILURE);
23}
24}
25
27
28 const double tol = 1.e-10;
29
30 //
31 {
32 std::pair<util::Point, util::Point> box = {util::Point(),
33 util::Point(1., 1., 1.)};
34 auto corner_pts = util::getCornerPoints(3, box);
35 auto edges = util::getEdges(3, box);
36 auto xc = util::getCenter(3, box);
37
38 for (size_t i=0; i<2; i++)
39 for (size_t j=0; j<2; j++)
40 for (size_t k=0; k<2; k++) {
41
42 auto p = util::Point(double(i), double(j), double(k));
43 bool found_p = false;
44 for (auto q : corner_pts) {
45 if (q.dist(p) < tol)
46 found_p = true;
47 }
48 if (!found_p)
49 errExit(fmt::format("Error: Can not find corner point {}\n", p.printStr()));
50 }
51
52 if (xc.dist(util::Point(0.5, 0.5, 0.5)) > tol)
53 errExit("Error: getCenter()\n");
54 }
55
56 //
57 {
58 if (std::abs(util::triangleArea(util::Point(0., 0., 0.), util::Point(2., 0., 0.), util::Point(1., 1., 0.)) - 1.) > tol)
59 errExit("Error: triangleArea()\n");
60 }
61
62 //
63 {
64 std::vector<double> x = {1., 0., 0.};
65 std::vector<double> y_check = {1./std::sqrt(2.), -1./std::sqrt(2.), 0.};
66 auto y = util::rotateCW2D(x, M_PI * 0.25);
67 if (util::l2Dist(y_check, y) > tol)
68 errExit("Error: rotateCW2D()\n");
69
70 if (util::Point(y_check).dist(util::rotateCW2D(util::Point(x), M_PI * 0.25)) > tol)
71 errExit("Error: rotateCW2D()\n");
72
73 y_check = {1./std::sqrt(2.), 1./std::sqrt(2.), 0.};
74 y = util::rotateACW2D(x, M_PI * 0.25);
75 if (util::l2Dist(y_check, y) > tol)
76 errExit("Error: rotateACW2D()\n");
77 }
78
79 //
80 {
81 auto x = util::Point(1., 0., 0.);
82 auto a = util::Point(0., 0., 1.);
83 auto y_check = util::Point(0., 1., 0.);
84 auto y = util::rotate(x, M_PI * 0.5, a);
85 if (y_check.dist(y) > tol)
86 errExit(fmt::format("Error: rotate(). y_check = {}, y = {}\n", y_check.printStr(), y.printStr()));
87
88 x = util::Point(1., 1., 1.);
89 y_check = util::Point(-1., 1., 1.);
90 y = util::rotate(x, M_PI * 0.5, a);
91 if (y_check.dist(y) > tol)
92 errExit(fmt::format("Error: rotate(). y_check = {}, y = {}\n", y_check.printStr(), y.printStr()));
93 }
94
95 //
96 {
97 auto x1 = util::Point(1., 1., 0.);
98 auto x2 = util::Point(1., 0., 0.);
99 if (std::abs(M_PI*0.25 - util::angle(x1, x2)) > tol)
100 errExit("Error: angle()\n");
101
102 x2 = util::Point(0., 0., 1.);
103 if (std::abs(M_PI*0.5 - util::angle(x1, x2)) > tol)
104 errExit("Error: angle()\n");
105
106 x2 = util::Point(0., 1., 1.);
107 if (std::abs(M_PI/3. - util::angle(x1, x2)) > tol)
108 errExit("Error: angle()\n");
109 }
110}
void testUtilMethods()
Test methods
T l2Dist(const std::vector< T > &x1, const std::vector< T > &x2)
Computes l2 distance between two vectors.
Definition geom.h:357
std::vector< std::pair< util::Point, util::Point > > getEdges(size_t dim, const std::pair< util::Point, util::Point > &box)
Returns all corner points in the box.
Definition geom.cpp:43
double angle(util::Point a, util::Point b)
Computes angle between two vectors.
std::vector< util::Point > getCornerPoints(size_t dim, const std::pair< util::Point, util::Point > &box)
Returns all corner points in the box.
Definition geom.cpp:14
util::Point getCenter(size_t dim, const std::pair< util::Point, util::Point > &box)
Returns center point.
Definition geom.cpp:91
std::vector< double > rotateACW2D(const std::vector< double > &x, const double &theta)
Rotates a vector in xy-plane in anti-clockwise direction.
std::vector< double > rotateCW2D(const std::vector< double > &x, const double &theta)
Rotates a vector in xy-plane in clockwise direction.
double triangleArea(const util::Point &x1, const util::Point &x2, const util::Point &x3)
Compute area of triangle.
Definition geom.cpp:642
util::Point rotate(const util::Point &p, const double &theta, const util::Point &axis)
Returns the vector after rotating by desired angle.
A structure to represent 3d vectors.
Definition point.h:30