forked from gzavo/hemoflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopening.cpp
265 lines (203 loc) · 9.44 KB
/
opening.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
#include "opening.h"
OpeningHandler::OpeningHandler(unsigned short* flagAray, int flag_, T radius_lb, vec3d dirVec)
{
flag = flag_;
hasScaleFunction = false;
cTimePos = 0;
cTimeVal = 0;
vector<int> xCoord; vector<int> yCoord; vector<int> zCoord;
pcout << "-> Looping through flag array... " << std::endl;
for(int i=0; i<Nx; i++)
for(int j=0; j<Ny; j++)
for(int k=0; k<Nz; k++)
if(flagAray[gT(i,j,k)]==flag) {
nodes.push_back({i,j,k});
xCoord.push_back(i); yCoord.push_back(j); zCoord.push_back(k);
}
if(nodes.size() == 0) {
pcout << "WARNING! Non-existing opening (zero size)!" << std::endl;
return;
}
pcout << "-> Calculating bounding box and center point... " << std::endl;
boundingBox = new Box3D(*min_element(xCoord.begin(), xCoord.end()),
*max_element(xCoord.begin(), xCoord.end()),
*min_element(yCoord.begin(), yCoord.end()),
*max_element(yCoord.begin(), yCoord.end()),
*min_element(zCoord.begin(), zCoord.end()),
*max_element(zCoord.begin(), zCoord.end()) );
R = radius_lb; // Radius data from the voxelizer code (comes from the centerline caluclation).
// Geometric centerpoint of the opening
center.x = accumulate( xCoord.begin(), xCoord.end(), 0.0) / xCoord.size();
center.y = accumulate( yCoord.begin(), yCoord.end(), 0.0) / yCoord.size();
center.z = accumulate( zCoord.begin(), zCoord.end(), 0.0) / zCoord.size();
// Direction in which the opening faces
direction.x = dirVec.x; direction.y = dirVec.y; direction.z = dirVec.z;
}
void OpeningHandler::printOpeningDetails()
{
pcout << "Opening parameters:" << std::endl;
if(flag == INLET)
pcout << "=> This is the inlet." << std::endl;
if(flag == FIRST_OUTLET)
pcout << "=> This is the smallest pressure outlet." << std::endl;
pcout << "-> flag: " << flag << std::endl;
pcout << "-> radius [lb]: " << R << std::endl;
//pcout << "-> Q rate: " << Qr << std::endl;
pcout << "-> Center [lb]: " << center.x << " " << center.y << " " << center.z << std::endl;
pcout << "-> Normal: " << direction.x << " " << direction.y << " " << direction.z << std::endl;
pcout << "-> Area [lb]: " << nodes.size() << std::endl;
pcout << "-> Scale function: " << hasScaleFunction << " length: " << scaleSignal.size() << std::endl;
}
void OpeningHandler::createPoiseauilleProfile(T u_avg)
{
pcout << "-> Creating direction-corrected Pouseuille velocity profile on flag: " << flag << std::endl;
// Sanity check
if ( !(boundingBox->x0 == boundingBox->x1 || boundingBox->y0 == boundingBox->y1 || boundingBox->z0 == boundingBox->z1) ) {
pcout << "!!! ERROR: The opening is not parallel to any major plane! This functionality is not implemented, the opening will not funxtion!" << std::endl;
}
// Paraboloid height from average
T u_max = 2. * u_avg;
// Search for the farthest point from the centerpoint of the opening.
T l_max = 0.0;
vec3d v_max(0,0,0);
for(auto const& v: nodes) {
vec3d dist(v.x-center.x, v.y-center.y, v.z-center.z);
T length = dist.norm();
if(length > l_max) {
l_max = length;
v_max.set(dist.x, dist.y, dist.z);
}
}
// The normal radial direction should be perpendicular to it
vec3d v_min = v_max.cross(direction);
v_min.normalize();
v_min = v_min * this->R;
pcout << "The scaling coordinate system (R=" << this->R << "): " << v_min.norm() << ", " << l_max << std::endl;
// Now use the closes and farthest points as coordinate system to scale the paraboloid.
// Note: these two direction vectors are supposed to be perpendicular!
for(auto const& v: nodes) {
vec3d cR(v.x-center.x, v.y-center.y, v.z-center.z);
T l_min = v_min.norm();
T proj_r_min = cR.dot(v_min) / l_min;
T proj_r_max = cR.dot(v_max) / l_max * (l_min /l_max); // Scale it to r_min
// At this point both projections are in (0,l_min == R), so the projected vector length will also be in (0, R)
T proj_r_len2 = proj_r_max * proj_r_max + proj_r_min * proj_r_min;
T r_min2 = l_min*l_min;
vec3d vel = direction * (u_max / r_min2 * (r_min2 - proj_r_len2));
velArr[v.x][v.y][v.z].set(vel.x, vel.y, vel.z);
}
}
void OpeningHandler::createBluntVelocityProfile(T u_avg)
{
pcout << "-> Creating blunt velocity profile on flag: " << flag << std::endl;
// Sanity check
if ( !(boundingBox->x0 == boundingBox->x1 || boundingBox->y0 == boundingBox->y1 || boundingBox->z0 == boundingBox->z1) ) {
pcout << "!!! ERROR: The opening is not parallel to any major plane! This functionality is not implemented, the opening will not funxtion!" << std::endl;
}
vec3d vel = direction * u_avg;
for(auto const& v: nodes) {
velArr[v.x][v.y][v.z].set(vel.x, vel.y, vel.z);
}
}
void OpeningHandler::createConstantPressureProfile(T density)
{
pcout << "-> Creating constant pressure on flag: " << flag << std::endl;
if(flag < FIRST_OUTLET) {
pcout << "WARNING! Calculating pressure profile for a non-pressure opening!" << std::endl;
}
for(auto const& v: nodes) {
presArr[v.x][v.y][v.z] = density;
}
}
void OpeningHandler::loadScaleFunction(string fileName)
{
pcout << "-> Loading scale function: " << fileName << std::endl;
plb_ifstream finSign(fileName.c_str());
//istream &finSign = pfinSign.getOriginalStream();
if(!finSign.good()) {
pcout << "WARNING!!! Flow rate scale file " << fileName << " is not readable!" << std::endl;
// hasScaleFunction = false;
}
T time, value;
int Ns;
finSign >> Ns;
global::mpi().bCast(&Ns, 1);
global::mpi().barrier();
for(int i=0; i<Ns; i++) {
finSign >> time; finSign >> value;
global::mpi().bCast(&time, 1);
global::mpi().bCast(&value, 1);
global::mpi().barrier();
scaleTime.push_back(time); scaleSignal.push_back(value);
}
finSign.close();
hasScaleFunction = true;
pcout << fileName << " loaded with " << scaleTime.size() << " data points." << std::endl;
}
void OpeningHandler::setBC(MultiBlockLattice3D<T, DESCRIPTOR> *lattice)
{
// The first outlet is the smallest, set as a constant pressure outlet
if (flag == INLET) {
OnLatticeBoundaryCondition3D<T, DESCRIPTOR> *bc = createLocalBoundaryCondition3D<T,DESCRIPTOR>();
// OnLatticeBoundaryCondition3D<T, DESCRIPTOR> *bc = createZouHeBoundaryCondition3D<T,DESCRIPTOR>();
bc->setVelocityConditionOnBlockBoundaries(*lattice, *boundingBox, boundary::dirichlet);
}
else {
// OnLatticeBoundaryCondition3D<T, DESCRIPTOR> *bc = createLocalBoundaryCondition3D<T,DESCRIPTOR>();
// OnLatticeBoundaryCondition3D<T, DESCRIPTOR> *bc = createZouHeBoundaryCondition3D<T,DESCRIPTOR>();
OnLatticeBoundaryCondition3D<T, DESCRIPTOR> *bc = createInterpBoundaryCondition3D<T,DESCRIPTOR>();
bc->setPressureConditionOnBlockBoundaries(*lattice, *boundingBox, boundary::dirichlet);
}
}
void OpeningHandler::setVelocityProfile(MultiBlockLattice3D<T, DESCRIPTOR> *lattice, field3D &velocityArr)
{
// For flowrate, additional measurements required after lattice initialization
if(flag==INLET || flag > FIRST_OUTLET) {
// Set given profile
setBoundaryVelocity(*lattice, *boundingBox, VelocityProfile3D<T,DESCRIPTOR>(&velocityArr, 1.0));
}
else {
pcout << "WARNING, opening " << flag << " incorrectly addressed as velocity opening (instead of pressure)!" << std::endl;
}
}
void OpeningHandler::setPressureProfile(MultiBlockLattice3D<T, DESCRIPTOR> *lattice, scalar3D &pressureArr)
{
// For flowrate, additional measurements required after lattice initialization
if(flag==FIRST_OUTLET) {
// Set given profile
setBoundaryDensity(*lattice, *boundingBox, PressureProfile3D<T,DESCRIPTOR>(&pressureArr, 1.0));
}
else {
pcout << "WARNING, opening " << flag << " incorrectly addressed as pressure opening (instead of velocity)!" << std::endl;
}
}
void OpeningHandler::imposeBC(MultiBlockLattice3D<T, DESCRIPTOR> *lattice, T dt)
{
T scale = 1.0;
if(hasScaleFunction)
{
int len = scaleTime.size();
cTimeVal += dt;
if(scaleTime[cTimePos] < cTimeVal) // TODO : We might need to skip some positions if simulation dt is too large. (With LBM, heck no....)
cTimePos++;
if (cTimePos > len-1) {
cTimePos = 0;
cTimeVal -= scaleTime[len-1];
}
if(cTimePos < len-1)
scale = interpolate(scaleTime[cTimePos], scaleTime[cTimePos+1], cTimeVal, scaleSignal[cTimePos], scaleSignal[cTimePos+1]);
else
scale = interpolate(scaleTime[cTimePos], scaleTime[0], cTimeVal, scaleSignal[cTimePos], scaleSignal[0]);
//scale /= scaleDivider; // WTF is this?
}
if (flag == FIRST_OUTLET) {
setBoundaryDensity(*lattice, *boundingBox, 1.0);
// setBoundaryDensity(*lattice, *boundingBox, PressureProfile3D<T,DESCRIPTOR>(&presArr, scale)); // For time dependent pressure boundary
}
else {
setBoundaryVelocity(*lattice, *boundingBox, VelocityProfile3D<T,DESCRIPTOR>(&velArr, scale));
}
}
OpeningHandler::~OpeningHandler()
{
}