-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.cpp
322 lines (264 loc) · 7.98 KB
/
helper.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//max difference
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
#include "helper.h"
#include "global.h"
#include "interface.h"
#include "numerics.h"
using namespace std;
//maximum error
double field_max_diff(double ***f1, double ***f2, GridData &grid){
double max = 0.0;
double error = max;
for (int i=0;i<=grid.nx[0];i++){
for (int j=0;j<=grid.nx[1];j++){
for (int k=0;k<=grid.nx[2];k++){
error = abs(f1[i][j][k]-f2[i][j][k]);
max = (error>max)?error:max;
}
}
}
return max;
}
void write_field(string fname, double ***matrix, GridData &grid){
ofstream outputfile(fname);
for (int i=0;i<=grid.nx[0];i++){
for (int j=0;j<=grid.nx[1];j++){
for (int k=0;k<=grid.nx[2];k++){
outputfile << matrix[i][j][k] << " ";
}
}
}
outputfile.close();
}
// print nbr of m around index, m = 1, 3x3x3 nbr, m = 2, 5x5x5 nbr, outside 1, inside 0
// in right hand coordinant system, view at direction (1,0,0)
void print_surf(double ***S, int* index, int m){
for (int i = -m; i <= m; i ++){
printf("i = %d\n", i);
for (int k = m; k >= -m; k --){
for (int j = m; j >= -m; j --){
printf("%c ", (S[index[0]+i][index[1]+j][index[2]+k] < 0)?'-':'+' );
}
printf("\n");
}
}
}
void print_surf(double ***S, array<int,3> ind, int m){
print_surf(S, ind.data(), m);
}
// use cartesian coordinate, x go right, y go in, z go up
// for char surface
// print nbr of m around index, m = 1, 3x3x3 nbr, m = 2, 5x5x5 nbr, outside 1, inside 0
void print_surf(char ***S, int* index, int m){
for (int i = -m; i <= m; i ++){
printf("i = %d\n", i);
for (int k = m; k >= -m; k --){
for (int j = m; j >= -m; j --){
printf("%c ", (S[index[0]+i][index[1]+j][index[2]+k])==0?'-':'+');
}
printf("\n");
}
}
}
void print_surf(char ***S, array<int,3> ind, int m){
print_surf(S, ind.data(), m);
}
void print_interface_info(double ***S, GridData &grid){
// find the minimum distance to grid points
double min_alpha = 1e9;
double min_dist = 1e9;
double tangent[3];
double normal[3];
int num = 0;
for(int i = 0; i <= grid.nx[0]; i ++){
for(int j = 0; j <= grid.nx[1]; j ++){
for(int k = 0; k <= grid.nx[2]; k ++){
Index index = {i, j, k};
if(nearinterface(index, S, grid)){
// For interface points
for(int rstar = 0; rstar < 3; rstar ++){
for(int sstar = -1; sstar <=1; sstar+=2){
// get direction of interface
Index rindex = index;
rindex[rstar] += sstar;
if(!SameSide(S, index, rindex )){
double alpha;
double tangent[3], normal[3];
Vector3d exact_Du = {0,0,0};
getinterfaceinfo(alpha,tangent,normal,S,index.data(),rindex.data(),grid);
double dist = alpha * grid.dx[rstar];
if (alpha < min_alpha){
min_alpha = alpha;
min_dist = dist;
}
}
}
}
}
}
}
}
cout<<"min_alpha "<<min_alpha<<endl;
cout<<"min_dist "<<min_dist<<endl;
}
// void init_surf_perturb(double ***S, double radius, GridData &grid, int opt, PBData &pb, double tol){
// init_surf(S, radius, grid, opt);
// if (globperturb == 0)
// perturb(S,grid.tol,pb,grid);
// else if (globperturb > 0)
// perturbstatus(S,grid.tol,globperturb,pb,grid);
// }
// return BFS, at most n element
vector<Index> BFS(Index idx, double ***S, GridData& grid, int n){
vector<Index> result;
vector<Index> queue;
queue.push_back(idx);
result.push_back(idx);
while(!queue.empty() && result.size() < n){
Index temp = queue.back();
queue.pop_back();
for(int r: {0,1,2}){
for(int s: {-1,1}){
if(result.size() >= n){
break;
}
Index nbr = temp;
nbr[r] += s;
if (SameSide(S, temp, nbr) && find(result.begin(), result.end(), nbr) == result.end()){
// if same side, not already in result
queue.push_back(nbr);
result.push_back(nbr);
}
}
}
}
return result;
}
// fillin small void in protein
void FillVoid(double ***S, GridData& grid){
const int search_max = 7;
for(int i = 0; i <= grid.nx[0]; i++){
for(int j = 0; j <= grid.nx[1]; j++){
for(int k = 0; k <= grid.nx[2]; k++){
Index idx = {i, j, k};
if(GetSign(S, idx) > 0.0 && nearinterface(idx, S, grid)){
// if near interface and negative
vector<Index> connect = BFS(idx, S, grid, search_max);
if(connect.size() < search_max){
cout<<"filling void at"<<endl;
print_surf(S, idx, 3);
for(Index temp : connect){
setvalarray(S, temp, -1.0);
}
}
}
}
}
}
}
// void init_surf_protein_dist(double ***S, GridData &grid, int n){
// vector< vector<double> > p;
// read_protein(S,grid,p);
// #pragma omp parallel for collapse(3)
// for(int i = 0; i <= grid.nx[0]; i++){
// for(int j = 0; j <= grid.nx[1]; j++){
// for(int k = 0; k <= grid.nx[2]; k++){
// array<double,3> x = sub2coord(array<int,3> {i,j,k}, grid);
// S[i][j][k] = sqrt(3.0) * 2 * fabs(grid.a[0]);
// for(int n = 0; n < p.size(); n++){
// array<double,3> px = {p[n][0],p[n][1],p[n][2]};//px is coordinate
// double d = dist(x,px) - p[n][3];
// S[i][j][k] = min(S[i][j][k],d);
// }
// }
// }
// }
// TempStruct tmp;
// init_TempStruct(tmp, grid);
// for(int i = 0; i<n; i++){
// advanceheat(S, tmp, grid);
// }
// }
//create random matrix
vector<vector<double>> randmx(int m, int n){
vector<vector<double>> A(m, vector<double>(n,0.0));
static default_random_engine e;
static uniform_real_distribution<double> u(-1,1);
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
A[i][j] = u(e);
}
}
return A;
}
vector<double> randmx(int n){
vector<double> x(n, 0.0);
static default_random_engine e;
static uniform_real_distribution<double> u(-1,1);
for(int i = 0; i<n; i++){
x[i] = u(e);
}
return x;
}
//calculate 1 norm, of matrix A dim n+1 x n+1, max column sum
double norm1(const vector<vector<double>> &A){
double max = 0.0;
int m = A.size();
int n = A[0].size();
for(int j = 0; j < n; j++){
double temp = 0.0;
for(int i = 0; i < m; i++){
temp += abs(A[i][j]);
}
max = (temp>max)?temp:max;
}
return max;
}
//
vector<vector<double>> Pointer2dToVector(double **A, int m, int n){
vector<vector<double>> B(m, vector<double>(n,0.0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
B[i][j] = A[i][j];
}
}
return B;
}
double** VectorToPointer2d(const vector<vector<double>> &A){
int m = A.size();
int n = A[0].size();
double **B = matrix(m-1,n-1);
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
B[i][j] = A[i][j];
}
}
return B;
}
//solve Ax = b , dim n, idx 0 to n-1
vector<double> gesolve(const vector<vector<double>> &A, vector<double> b){
int n = b.size();
int PLR[n], PLC[n];
double **LU = matrix(n-1,n-1);
double **B = VectorToPointer2d(A);
vector<double> x(n,0.0);
gecp0(LU,PLR,PLC,B,n-1,n-1);
forwardbacksub0(x.data(),b.data(),LU,PLR,PLC,n-1);
free_matrix(LU,n-1,n-1);
free_matrix(B,n-1,n-1);
return x;
}
void OutputVector(string filename, double *x, int n)
{
int i, r;
ofstream outfile(filename);
outfile.precision(16);
for (i = 0; i < n; i++){
outfile << i << " " << scientific << x[i] << endl;
}
}