f8af84f9
David Mayerich
added MIT copyrig...
|
1
2
3
4
5
6
7
8
9
|
/*
Copyright <2017> <David Mayerich>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
|
28b882ce
Jiaming Guo
new flow header
|
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
|
#ifndef STIM_FLOW_H
#define STIM_FLOW_H
#include <vector>
#include <algorithm>
//STIM include
#include <stim/math/vec3.h>
#include <stim/parser/arguments.h>
#include <stim/biomodels/network.h>
#ifdef __CUDACC__
#include <cublas_v2.h>
#include <stim/cuda/cudatools/error.h>
#endif
namespace stim {
template <typename A, typename B, typename C>
struct triple {
A first;
B second;
C third;
};
template <typename T>
struct bridge {
std::vector<unsigned> v; // vertices' indices
std::vector<typename stim::vec3<T> > V; // vertices' coordinates
T l; // length
T r; // radii
T deltaP; // pressure drop
T Q; // volume flow rate
};
template <typename T>
class flow {
private:
// calculate the cofactor of elemen[row][col]
void get_minor(T** src, T** dest, int row, int col, int order) {
// index of element to be copied
int rowCount = 0;
int colCount = 0;
for (int i = 0; i < order; i++) {
if (i != row) {
colCount = 0;
for (int j = 0; j < order; j++) {
// when j is not the element
if (j != col) {
dest[rowCount][colCount] = src[i][j];
colCount++;
}
}
rowCount++;
|
ea2056c0
cherub
added flow.h
|
67
|
}
|
ea2056c0
cherub
added flow.h
|
68
69
|
}
}
|
ea2056c0
cherub
added flow.h
|
70
|
|
28b882ce
Jiaming Guo
new flow header
|
71
72
|
// calculate the det()
T determinant(T** mat, int order) {
|
ea2056c0
cherub
added flow.h
|
73
|
|
28b882ce
Jiaming Guo
new flow header
|
74
75
76
|
// degenate case when n = 1
if (order == 1)
return mat[0][0];
|
ea2056c0
cherub
added flow.h
|
77
|
|
28b882ce
Jiaming Guo
new flow header
|
78
|
T det = 0.0; // determinant value
|
ea2056c0
cherub
added flow.h
|
79
|
|
28b882ce
Jiaming Guo
new flow header
|
80
81
82
83
|
// allocate the cofactor matrix
T** minor = (T**)malloc((order - 1) * sizeof(T*));
for (int i = 0; i < order - 1; i++)
minor[i] = (T*)malloc((order - 1) * sizeof(T));
|
ea2056c0
cherub
added flow.h
|
84
|
|
ea2056c0
cherub
added flow.h
|
85
|
|
28b882ce
Jiaming Guo
new flow header
|
86
|
for (int i = 0; i < order; i++) {
|
ea2056c0
cherub
added flow.h
|
87
|
|
28b882ce
Jiaming Guo
new flow header
|
88
89
|
// get minor of element(0, i)
get_minor(mat, minor, 0, i, order);
|
ea2056c0
cherub
added flow.h
|
90
|
|
28b882ce
Jiaming Guo
new flow header
|
91
92
93
|
// recursion
det += (i % 2 == 1 ? -1.0 : 1.0) * mat[0][i] * determinant(minor, order - 1);
}
|
ea2056c0
cherub
added flow.h
|
94
|
|
28b882ce
Jiaming Guo
new flow header
|
95
96
97
98
|
// release memory
for (int i = 0; i < order - 1; i++)
free(minor[i]);
free(minor);
|
ea2056c0
cherub
added flow.h
|
99
|
|
28b882ce
Jiaming Guo
new flow header
|
100
|
return det;
|
ea2056c0
cherub
added flow.h
|
101
|
}
|
ea2056c0
cherub
added flow.h
|
102
|
|
28b882ce
Jiaming Guo
new flow header
|
103
|
public:
|
9db76bdb
Jiaming Guo
change flow object
|
104
105
106
107
108
|
T** C; // Conductance
std::vector<typename stim::triple<unsigned, unsigned, float> > Q; // volume flow rate
std::vector<T> QQ; // Q' vector
std::vector<T> P; // initial pressure
std::vector<T> pressure; // final pressure
|
28b882ce
Jiaming Guo
new flow header
|
109
|
|
9db76bdb
Jiaming Guo
change flow object
|
110
111
112
|
//std::vector<typename stim::triple<unsigned, unsigned, T> > V; // velocity
//std::vector<typename stim::triple<unsigned, unsigned, T> > Q; // volume flow rate
//std::vector<typename stim::triple<unsigned, unsigned, T> > deltaP; // pressure drop
|
ea2056c0
cherub
added flow.h
|
113
|
|
9db76bdb
Jiaming Guo
change flow object
|
114
|
flow() {} // default constructor
|
ea2056c0
cherub
added flow.h
|
115
|
|
9db76bdb
Jiaming Guo
change flow object
|
116
117
118
119
120
|
void init(unsigned n_e, unsigned n_v) {
C = new T*[n_v]();
for (unsigned i = 0; i < n_v; i++) {
C[i] = new T[n_v]();
|
28b882ce
Jiaming Guo
new flow header
|
121
|
}
|
ea2056c0
cherub
added flow.h
|
122
|
|
9db76bdb
Jiaming Guo
change flow object
|
123
124
125
|
QQ.resize(n_v);
P.resize(n_v);
pressure.resize(n_v);
|
ea2056c0
cherub
added flow.h
|
126
|
|
9db76bdb
Jiaming Guo
change flow object
|
127
|
Q.resize(n_e);
|
28b882ce
Jiaming Guo
new flow header
|
128
|
}
|
ea2056c0
cherub
added flow.h
|
129
|
|
b5c53037
Jiaming Guo
fixed memory leak
|
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
void reset(unsigned n_v) {
for (unsigned i = 0; i < n_v; i++) {
for (unsigned j = 0; j < n_v; j++) {
C[i][j] = 0;
}
}
}
void clear(unsigned n_v) {
for (unsigned i = 0; i < n_v; i++)
delete[] C[i];
delete[] C;
}
|
a19e3c80
David Mayerich
matrix edits for ...
|
146
147
|
/// Calculate the inverse of A and store the result in C
void inversion(T** A, int order, T* C) {
|
28b882ce
Jiaming Guo
new flow header
|
148
149
150
151
152
153
154
155
|
#ifdef __CUDACC__
// convert from double pointer to single pointer, make it flat
T* Aflat = (T*)malloc(order * order * sizeof(T));
for (unsigned i = 0; i < order; i++)
for (unsigned j = 0; j < order; j++)
Aflat[i * order + j] = A[i][j];
|
28b882ce
Jiaming Guo
new flow header
|
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// create device pointer
T* d_Aflat; // flat original matrix
T* d_Cflat; // flat inverse matrix
T** d_A; // put the flat original matrix into another array of pointer
T** d_C;
int *d_P;
int *d_INFO;
// allocate memory on device
HANDLE_ERROR(cudaMalloc((void**)&d_Aflat, order * order * sizeof(T)));
HANDLE_ERROR(cudaMalloc((void**)&d_Cflat, order * order * sizeof(T)));
HANDLE_ERROR(cudaMalloc((void**)&d_A, sizeof(T*)));
HANDLE_ERROR(cudaMalloc((void**)&d_C, sizeof(T*)));
HANDLE_ERROR(cudaMalloc((void**)&d_P, order * 1 * sizeof(int)));
HANDLE_ERROR(cudaMalloc((void**)&d_INFO, 1 * sizeof(int)));
// copy matrix from host to device
HANDLE_ERROR(cudaMemcpy(d_Aflat, Aflat, order * order * sizeof(T), cudaMemcpyHostToDevice));
// copy matrix from device to device
HANDLE_ERROR(cudaMemcpy(d_A, &d_Aflat, sizeof(T*), cudaMemcpyHostToDevice));
HANDLE_ERROR(cudaMemcpy(d_C, &d_Cflat, sizeof(T*), cudaMemcpyHostToDevice));
|
ea2056c0
cherub
added flow.h
|
179
|
|
28b882ce
Jiaming Guo
new flow header
|
180
181
182
|
// calculate the inverse of matrix based on cuBLAS
cublasHandle_t handle;
CUBLAS_HANDLE_ERROR(cublasCreate_v2(&handle)); // create cuBLAS handle object
|
ea2056c0
cherub
added flow.h
|
183
|
|
28b882ce
Jiaming Guo
new flow header
|
184
|
CUBLAS_HANDLE_ERROR(cublasSgetrfBatched(handle, order, d_A, order, d_P, d_INFO, 1));
|
ea2056c0
cherub
added flow.h
|
185
|
|
28b882ce
Jiaming Guo
new flow header
|
186
187
188
|
int INFO = 0;
HANDLE_ERROR(cudaMemcpy(&INFO, d_INFO, sizeof(int), cudaMemcpyDeviceToHost));
if (INFO == order)
|
ea2056c0
cherub
added flow.h
|
189
|
{
|
28b882ce
Jiaming Guo
new flow header
|
190
191
192
193
194
195
196
197
198
199
200
201
202
|
std::cout << "Factorization Failed : Matrix is singular." << std::endl;
cudaDeviceReset();
exit(1);
}
CUBLAS_HANDLE_ERROR(cublasSgetriBatched(handle, order, (const T **)d_A, order, d_P, d_C, order, d_INFO, 1));
CUBLAS_HANDLE_ERROR(cublasDestroy_v2(handle));
// copy inverse matrix from device to device
HANDLE_ERROR(cudaMemcpy(&d_Cflat, d_C, sizeof(T*), cudaMemcpyDeviceToHost));
// copy inverse matrix from device to host
|
a19e3c80
David Mayerich
matrix edits for ...
|
203
|
HANDLE_ERROR(cudaMemcpy(C, d_Cflat, order * order * sizeof(T), cudaMemcpyDeviceToHost));
|
28b882ce
Jiaming Guo
new flow header
|
204
|
|
28b882ce
Jiaming Guo
new flow header
|
205
206
|
// clear up
free(Aflat);
|
28b882ce
Jiaming Guo
new flow header
|
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
HANDLE_ERROR(cudaFree(d_Aflat));
HANDLE_ERROR(cudaFree(d_Cflat));
HANDLE_ERROR(cudaFree(d_A));
HANDLE_ERROR(cudaFree(d_C));
HANDLE_ERROR(cudaFree(d_P));
HANDLE_ERROR(cudaFree(d_INFO));
#else
// get the determinant of a
double det = 1.0 / determinant(A, order);
// memory allocation
T* tmp = (T*)malloc((order - 1)*(order - 1) * sizeof(T));
T** minor = (T**)malloc((order - 1) * sizeof(T*));
for (int i = 0; i < order - 1; i++)
minor[i] = tmp + (i * (order - 1));
for (int j = 0; j < order; j++) {
for (int i = 0; i < order; i++) {
// get the co-factor (matrix) of A(j,i)
get_minor(A, minor, j, i, order);
C[i][j] = det * determinant(minor, order - 1);
if ((i + j) % 2 == 1)
C[i][j] = -C[i][j];
|
ea2056c0
cherub
added flow.h
|
231
232
|
}
}
|
28b882ce
Jiaming Guo
new flow header
|
233
234
235
236
237
|
// release memory
free(tmp);
free(minor);
#endif
|
ea2056c0
cherub
added flow.h
|
238
|
}
|
28b882ce
Jiaming Guo
new flow header
|
239
|
};
|
ea2056c0
cherub
added flow.h
|
240
241
|
}
|
9db76bdb
Jiaming Guo
change flow object
|
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
|
#endif
//// calculate the flow rate of 3D model(circle cross section)
//void calculate_flow_rate(unsigned e, T r) {
// stim::triple<unsigned, unsigned, T> tmp_Q;
// tmp_Q.first = V[e].first; // copy the vertices information
// tmp_Q.second = V[e].second;
// tmp_Q.third = V[e].third * stim::PI * pow(r, 2); // UNITS: uL/s
// Q.push_back(tmp_Q); // push back the volume flow rate information for every edge
//}
//// calculate the flow rate of 2D model(rectangular cross section)
//void calculate_flow_rate(unsigned e, T r, T h) {
// stim::triple<unsigned, unsigned, T> tmp_Q;
// tmp_Q.first = V[e].first; // copy the vertices information
// tmp_Q.second = V[e].second;
// tmp_Q.third = V[e].third * h * r; // UNITS: uL/s = mm^3/s
// Q.push_back(tmp_Q); // push back the volume flow rate information for every edge
//}
//// calculate the pressure drop of 3D model(circle cross section)
//void calculate_deltaP(unsigned e, T u, T l, T r) {
// stim::triple<unsigned, unsigned, T> tmp_deltaP;
// tmp_deltaP.first = V[e].first; // copy the vertices information
// tmp_deltaP.second = V[e].second;
// tmp_deltaP.third = (8 * u * l * Q[e].third) / (stim::PI * pow(r, 4)); // UNITS: g/mm/s^2 = Pa
// deltaP.push_back(tmp_deltaP); // push back the volume flow rate information for every edge
//}
//// calculate the pressure drop of 2D model(rectangular cross section)
//void calculate_deltaP(unsigned e, T u, T l, T r, T h) {
// stim::triple<unsigned, unsigned, T> tmp_deltaP;
// tmp_deltaP.first = V[e].first; // copy the vertices information
// tmp_deltaP.second = V[e].second;
// tmp_deltaP.third = (12 * u * l * Q[e].third) / (h * pow(r, 3)); // UNITS: g/mm/s^2 = Pa
// deltaP.push_back(tmp_deltaP); // push back the volume flow rate information for every edge
//}
//// better way to do this???
//// find the maximum and minimum pressure positions
//void find_max_min_pressure(size_t n_e, size_t n_v, unsigned& max, unsigned& min) {
// std::vector<T> P(n_v, FLT_MAX);
// // set one to reference
// P[Q[0].first] = 0.0;
// unsigned first = 0;
// unsigned second = 0;
// // calculate all the relative pressure in brute force manner
// for (unsigned e = 0; e < n_e; e++) {
// // assuming the obj file stores in a straight order, in other words, like swc file
// first = Q[e].first;
// second = Q[e].second;
// if (P[first] != FLT_MAX) // if pressure at start vertex is known
// P[second] = P[first] - deltaP[e].third;
// else if (P[second] != FLT_MAX) // if pressure at end vertex is known
// P[first] = P[second] + deltaP[e].third;
// }
// // find the maximum and minimum pressure position
// auto m1 = std::max_element(P.begin(), P.end()); // temporarily max number
// auto m2 = std::min_element(P.begin(), P.end()); // temporarily min number
// max = std::distance(P.begin(), m1);
// min = std::distance(P.begin(), m2);
// T tmp_m = *m2;
// // Now set the lowest pressure port to reference pressure(0.0 Pa)
// for (unsigned i = 0; i < n_v; i++)
// P[i] -= tmp_m;
// for (unsigned i = 0; i < n_v; i++)
// pressure.push_back(P[i]);
//}
|