MDStressLab++
Loading...
Searching...
No Matches
helper.hpp
Go to the documentation of this file.
1/*
2 * helper.hpp
3 *
4 * Created on: Nov 9, 2019
5 * Author: Nikhil
6 */
7
8#ifndef HELPER_HPP_
9#define HELPER_HPP_
10
11#include <cmath>
12#include <iostream>
13#include <stdlib.h>
14#include <vector>
15
16
17#define SMALL 1.0e-10
18
19// norm of a 3-element vector
20inline double norm(double const * a)
21{
22 return std::sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
23}
24
25
26// dot product of two 3-element vectors
27inline double dot(double const * a, double const * b)
28{
29 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
30}
31
32
33// cross product of two 3-element vectors
34inline void cross(double const * a, double const * b, double * const axb)
35{
36 axb[0] = a[1] * b[2] - a[2] * b[1];
37 axb[1] = a[2] * b[0] - a[0] * b[2];
38 axb[2] = a[0] * b[1] - a[1] * b[0];
39}
40
41
42// determinant of a 3 by 3 matrix
43inline double det(double const * mat)
44{
45 return mat[0] * mat[4] * mat[8] - mat[0] * mat[5] * mat[7]
46 - mat[1] * mat[3] * mat[8] + mat[1] * mat[5] * mat[6]
47 + mat[2] * mat[3] * mat[7] - mat[2] * mat[4] * mat[6];
48}
49
50
51// determinant of a 2 by 2 matrix
52inline double det2(double a11, double a12, double a21, double a22)
53{
54 return (a11 * a22) - (a12 * a21);
55}
56
57
58// transpose of a 3 by 3 matrix
59inline void transpose(double const * mat, double * const trans)
60{
61 for (int i = 0; i < 3; i++)
62 {
63 for (int j = 0; j < 3; j++) { trans[3 * i + j] = mat[3 * j + i]; }
64 }
65}
66
67
68// inverse of a 3 by 3 matrix
69inline int inverse(double const * mat, double * const inv)
70{
71 inv[0] = det2(mat[4], mat[5], mat[7], mat[8]);
72 inv[1] = det2(mat[2], mat[1], mat[8], mat[7]);
73 inv[2] = det2(mat[1], mat[2], mat[4], mat[5]);
74 inv[3] = det2(mat[5], mat[3], mat[8], mat[6]);
75 inv[4] = det2(mat[0], mat[3], mat[6], mat[8]);
76 inv[5] = det2(mat[2], mat[0], mat[5], mat[3]);
77 inv[6] = det2(mat[3], mat[4], mat[6], mat[7]);
78 inv[7] = det2(mat[1], mat[0], mat[7], mat[6]);
79 inv[8] = det2(mat[0], mat[1], mat[3], mat[4]);
80
81 double dd = det(mat);
82 if (std::abs(dd) < SMALL)
83 {
84 MY_WARNING("Cannot invert cell matrix. Determinant is 0.");
85 return 1;
86 }
87 for (int i = 0; i < 9; i++) { inv[i] /= dd; }
88 return 0;
89}
90
91
92inline void coords_to_index(double const * x,
93 int const * size,
94 double const * max,
95 double const * min,
96 int * const index)
97{
98 for (int i = 0; i < 3; i++)
99 {
100 index[i]
101 = static_cast<int>(((x[i] - min[i]) / (max[i] - min[i])) * size[i]);
102 index[i] = std::min(index[i],
103 size[i] - 1); // handle edge case when x[i] = max[i]
104 }
105}
106
107template< typename T >
109{
110 void operator ()( T const * p)
111 {
112 delete[] p;
113 }
114};
115
116inline void progressBar(const double& progress)
117{
118 if (progress < 1.0)
119 {
120 int barWidth = 70;
121 std::cout << "[";
122 int pos = barWidth * progress;
123 for (int i = 0; i < barWidth; ++i)
124 {
125 if (i < pos) std::cout << "=";
126 else if (i == pos) std::cout << ">";
127 else std::cout << " ";
128 }
129 std::cout << "] " << int(progress * 100.0) << "\n";
130 std::cout.flush();
131 }
132 else
133 std::cout << std::endl;
134}
135#endif /* HELPER_HPP_ */
void transpose(double const *mat, double *const trans)
Definition helper.hpp:59
#define SMALL
Definition helper.hpp:17
void coords_to_index(double const *x, int const *size, double const *max, double const *min, int *const index)
Definition helper.hpp:92
void cross(double const *a, double const *b, double *const axb)
Definition helper.hpp:34
double dot(double const *a, double const *b)
Definition helper.hpp:27
double norm(double const *a)
Definition helper.hpp:20
double det2(double a11, double a12, double a21, double a22)
Definition helper.hpp:52
double det(double const *mat)
Definition helper.hpp:43
int inverse(double const *mat, double *const inv)
Definition helper.hpp:69
void progressBar(const double &progress)
Definition helper.hpp:116
void operator()(T const *p)
Definition helper.hpp:110
#define MY_WARNING(message)
Definition typedef.h:24