forked from tan2/DynEarthSol-old
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.hpp
205 lines (174 loc) · 4.85 KB
/
utils.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef DYNEARTHSOL3D_UTILS_HPP
#define DYNEARTHSOL3D_UTILS_HPP
#include "parameters.hpp"
#include <cmath>
#include <iostream>
#include <vector>
#include <cfloat>
#include <math.h>
#include <iomanip>
#if defined(_WIN32)
#include <windows.h>
#else
#include <time.h>
#endif
static void print(std::ostream& os, const double& x)
{
os << x;
}
static void print(std::ostream& os, const int& x)
{
os << x;
}
static void print(std::ostream& os, const std::size_t& x)
{
os << x;
}
template <typename T1, typename T2>
void print(std::ostream& os, const std::pair<T1,T2>& x)
{
os << x.first << ':' << x.second;
}
template <typename T>
void print(std::ostream& os, const T& A, std::size_t size)
{
os << "[";
for (std::size_t i = 0; i != size; ++i) {
print(os, A[i]);
if (i+1 != size)
os << ", ";
}
os << "]";
}
template <typename Array>
void print(std::ostream& os, const Array& A)
{
typename Array::const_iterator i;
os << "[";
for (i = A.begin(); i != A.end(); ++i) {
print(os, *i);
os << ", ";
}
os << "]";
}
#pragma acc routine seq
static double trace(const double* s)
{
#ifdef THREED
return s[0] + s[1] + s[2];
#else
return s[0] + s[1];
#endif
}
static double second_invariant2(const double* t)
{
#ifdef THREED
double a = (t[0] + t[1] + t[2]) / 3;
return ( 0.5 * ((t[0]-a)*(t[0]-a) + (t[1]-a)*(t[1]-a) + (t[2]-a)*(t[2]-a))
+ t[3]*t[3] + t[4]*t[4] + t[5]*t[5] );
#else
return 0.25*(t[0]-t[1])*(t[0]-t[1]) + t[2]*t[2];
#endif
}
static double second_invariant(const double* t)
{
/* second invariant of the deviatoric part of tensor t
* defined as: td = deviatoric(t); sqrt( td(i,j) * td(i,j) / 2)
*/
return std::sqrt(second_invariant2(t));
}
static int findNearestNeighbourIndex( double x_new, const double_vec& x )
{
/* find nearest neighbour index for interpolation
* x vector only can be ascending
*/
double dist = DBL_MAX;
int idx = -1;
for (size_t i = 0; i < x.size(); ++i ) {
double newDist = x_new - x[i];
if ( newDist >= 0 && newDist <= dist ) {
dist = newDist;
idx = i;
}
}
return idx;
}
static double interp1(const double_vec& x, const double_vec& y, double x_new)
{
int idx = findNearestNeighbourIndex( x_new, x);
double slope = 0;
if (idx < 0)
idx = 0;
else if ( idx < static_cast<int>(x.size()-1) )
slope = (y[idx+1] - y[idx]) / (x[idx+1] - x[idx]);
return slope * (x_new-x[idx]) + y[idx];
}
static int64_t get_nanoseconds() {
#if defined(_WIN32)
LARGE_INTEGER frequency, counter;
QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&counter);
return (int64_t)((double)counter.QuadPart / frequency.QuadPart * 1e9);
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1e9 + ts.tv_nsec;
#endif
}
static void print_time_ns(const int64_t duration) {
int hours = duration / (int64_t)3600000000000;
int minutes = (duration % (int64_t)3600000000000) / (int64_t)60000000000;
double seconds = (duration % (int64_t)60000000000) / 1e9;
std::cout << std::setw(3) << std::setfill('0') << hours << ":"
<< std::setw(2) << std::setfill('0') << minutes << ":"
<< std::setw(9) << std::fixed << std::setprecision(6) << std::setfill('0') << seconds;
}
#endif
static void check_nan(const Variables& var) {
for (int e=0; e<var.nelem;e++) {
if (std::isnan((*var.volume)[e])) {
std::cerr << "Error: volume becomes NaN\n";
std::exit(11);
}
if (std::isnan((*var.dpressure)[e])) {
std::cerr << "Error: dpressure becomes NaN\n";
std::exit(11);
}
if (std::isnan((*var.viscosity)[e])) {
std::cerr << "Error: viscosity becomes NaN\n";
std::exit(11);
}
for (int i=0; i<3;i++) {
if(std::isnan((*var.connectivity)[e][i])) {
std::cerr << "Error: connectivity becomes NaN\n";
std::exit(11);
}
}
}
for (int i=0; i<var.nnode; i++) {
if (std::isnan((*var.temperature)[i])) {
std::cerr << "Error: temperature becomes NaN\n";
std::exit(11);
}
for (int j=0; j<NDIMS; j++) {
if (std::isnan((*var.force)[i][j])) {
std::cerr << "Error: force becomes NaN\n";
std::exit(11);
}
if (std::isnan((*var.vel)[i][j])) {
std::cerr << "Error: vel becomes NaN\n";
std::exit(11);
}
if (std::isnan((*var.coord)[i][j])) {
std::cerr << "Error: coordinate becomes NaN\n";
std::exit(11);
}
}
for (int j=0; j<3; j++) {
if (std::isnan((*var.stress)[i][j])) {
std::cerr << "Error: stress becomes NaN\n";
std::exit(11);
}
}
}
}