-
Notifications
You must be signed in to change notification settings - Fork 3
/
alphaInitializerFoam.C
executable file
·233 lines (190 loc) · 6.85 KB
/
alphaInitializerFoam.C
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
/*---------------------------------------------------------------------------*\
| alphaInitializerFoam | Copyright (C) 2019 Dezhi Dai |
-------------------------------------------------------------------------------
Cork | Copyright (C) 2016 Gilbert Bernstein
libigl | Copyright (C) 2018 Alec Jacobson, Daniele Panozzo and others.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
alphaInitializerFoam
Description
Initialize the alpha (VOF) field with an input shape surface mesh
in .stl format. The previous fluid shape will be retained.
Reference:
\verbatim
Dai, Dezhi (2019).
alphaInitializerFoam
Mendeley Data, V2
doi 10.17632/wg4sx7sc57.2
url http://dx.doi.org/10.17632/wg4sx7sc57.2
\endverbatim
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "dynamicFvMesh.H"
#include "igl/readSTL.h"
#include "igl/remove_duplicate_vertices.h"
#include "igl/copyleft/cork/mesh_boolean.h"
#include "igl/readSTL.h"
#include "igl/volume.h"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addNote
(
"Initialize the alpha (VOF) field with an input shape surface mesh "
"in .stl format. The previous fluid shape will be retained.\n"
);
// Disable and add some options
argList::noParallel(); // Disable parallel function
argList::noFunctionObjects(); // Don't use function objects
argList::addArgument("stl-file", "The input surface mesh in .stl format");
Foam::argList::addBoolOption
(
"gas",
"Initialize the shape for gas phase"
);
#include "setRootCase.H"
#include "createTime.H"
#include "createDynamicFvMesh.H"
Info << "\nReading field alpha.water\n" << endl;
volScalarField alpha1
(
IOobject
(
"alpha.water",
"0",
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
),
mesh
);
Info << "Reading shape file " << args[1] << "\n" << endl;
Eigen::MatrixXd temp_V, V, N;
Eigen::MatrixXi temp_F, VI, VJ, F;
bool success = igl::readSTL(args[1], temp_V, temp_F, N);
if (success)
{
igl::remove_duplicate_vertices(temp_V, temp_F, 1e-6, V, VI, VJ, F);
}
Info << "Initializing field alpha.water\n" << endl;
const scalarField& cellVolumes = mesh.V();
const cellList& cellFaces = mesh.cells();
const faceList& faces = mesh.faces();
const pointField& points = mesh.points();
const labelList& own = mesh.faceOwner();
forAll(alpha1, cellI)
{
scalar fluidVol(0.0);
// Localize cell point labels
const cell& currentCell(cellFaces[cellI]);
const labelList& globalCellPtLabels(currentCell.labels(faces));
pointField localPts;
forAll(globalCellPtLabels, pointI)
{
localPts.append(points[globalCellPtLabels[pointI]]);
}
// Localize cell face labels
labelListList localFaces;
forAll(currentCell, faceI)
{
const face& fa = faces[currentCell[faceI]];
localPts.append(fa.centre(points));
edgeList edges = fa.edges();
forAll(edges, edgeI)
{
labelList localTriface;
label c0 = localPts.size() - 1;
localTriface.append(c0);
label c1 = edges[edgeI].start();
label c2 = edges[edgeI].end();
forAll(globalCellPtLabels, pointI)
{
if(c1 == globalCellPtLabels[pointI])
{
c1 = pointI;
break;
}
}
forAll(globalCellPtLabels, pointI)
{
if(c2 == globalCellPtLabels[pointI])
{
c2 = pointI;
break;
}
}
if(cellI == own[currentCell[faceI]])
{
localTriface.append(c1);
localTriface.append(c2);
}
else
{
localTriface.append(c2);
localTriface.append(c1);
}
localFaces.append(localTriface);
}
}
Eigen::MatrixXd VC(localPts.size(), 3);
forAll(localPts, pointI)
{
VC(pointI, 0) = localPts[pointI].x();
VC(pointI, 1) = localPts[pointI].y();
VC(pointI, 2) = localPts[pointI].z();
}
Eigen::MatrixXi FC(localFaces.size(), 3);
forAll(localFaces, faceI)
{
const labelList& fa(localFaces[faceI]);
FC(faceI, 0) = fa[0];
FC(faceI, 1) = fa[1];
FC(faceI, 2) = fa[2];
}
// Calculate intersection of the shape surface mesh and cellI
Eigen::MatrixXd V_bool;
Eigen::MatrixXi F_bool;
igl::copyleft::cork::mesh_boolean
(
VC, FC, V, F, igl::MESH_BOOLEAN_TYPE_INTERSECT, V_bool, F_bool
);
// Calculate intersected volume
if (F_bool.rows() > 0)
{
Eigen::MatrixXd V2(V_bool.rows() + 1, V_bool.cols());
V2.topRows(V_bool.rows()) = V_bool;
V2.bottomRows(1).setZero();
Eigen::MatrixXi T(F_bool.rows(), 4);
T.leftCols(3) = F_bool;
T.rightCols(1).setConstant(V_bool.rows());
Eigen::VectorXd vol;
igl::volume(V2, T, vol);
fluidVol = mag(vol.sum());
scalar alphaFluid(fluidVol / cellVolumes[cellI]);
alphaFluid = min(scalar(1.0), max(scalar(0.0), alphaFluid));
if (args.found("gas"))
{
alpha1.ref()[cellI] = 1.0 - alphaFluid;
}
else
{
alpha1.ref()[cellI] = alphaFluid;
}
}
}
Info << "Writing field alpha.water\n" << endl;
alpha1.write();
return 0;
}