forked from UBCHREST/ablate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurfaceForce.cpp
More file actions
326 lines (221 loc) · 12.8 KB
/
surfaceForce.cpp
File metadata and controls
326 lines (221 loc) · 12.8 KB
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
322
323
324
325
326
#include "domain/RBF/phs.hpp"
#include "surfaceForce.hpp"
#include "finiteVolume/compressibleFlowFields.hpp"
#include "levelSet/levelSetUtilities.hpp"
#include "registrar.hpp"
#include "utilities/constants.hpp"
#include "utilities/mathUtilities.hpp"
#include "utilities/petscSupport.hpp"
#include "mathFunctions/functionFactory.hpp"
#define xexit(S, ...) {PetscFPrintf(MPI_COMM_WORLD, stderr, \
"\x1b[1m(%s:%d, %s)\x1b[0m\n \x1b[1m\x1b[90mexiting:\x1b[0m " S "\n", \
__FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); exit(0);}
ablate::finiteVolume::processes::SurfaceForce::SurfaceForce(PetscReal sigma) : sigma(sigma) {
printf("Sigma is equal to %e\n", sigma);
}
// Done once at the beginning of every run
void ablate::finiteVolume::processes::SurfaceForce::Setup(ablate::finiteVolume::FiniteVolumeSolver &flow) {
flow.RegisterRHSFunction(ComputeSource, this);
}
// Called every time the mesh changes
void ablate::finiteVolume::processes::SurfaceForce::Initialize(ablate::finiteVolume::FiniteVolumeSolver &flow) {
SurfaceForce::subDomain = flow.GetSubDomainPtr();
if (SurfaceForce::reconstruction) {
xexit("");
SurfaceForce::reconstruction.reset();
}
SurfaceForce::reconstruction = std::make_shared<ablate::levelSet::Reconstruction>(SurfaceForce::subDomain, flow.GetRegionWithoutGhost());
//Vec auxVector = subDomain->GetAuxVector(); // Getting the Auxiliary Vector (contains data for all aux fields)
//DM aux_dm = subDomain->GetAuxDM(); // Getting the Auxiliary DM which has Auxiliary fields
//const ablate::domain::Field *levelSetField = &(subDomain->GetField("levelSet")); // Getting the level set field for vertices (FEM)
//process->reconstruction->arbit_interface(aux_dm, *levelSetField, auxVector);
}
void SurfaceForce_SaveCellData(DM dm, const Vec vec, const char fname[255], const PetscInt id, PetscInt Nc, std::shared_ptr<ablate::domain::SubDomain> subDomain) {
ablate::domain::Range range;
const PetscScalar *array;
PetscInt dim = subDomain->GetDimensions();
MPI_Comm comm = PetscObjectComm((PetscObject)dm);
int rank, size;
MPI_Comm_size(comm, &size);
MPI_Comm_rank(comm, &rank);
subDomain->GetCellRange(nullptr, range);
VecGetArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
PetscInt boundaryCellStart;
DMPlexGetCellTypeStratum(dm, DM_POLYTOPE_FV_GHOST, &boundaryCellStart, nullptr) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt r = 0; r < size; ++r) {
if ( rank==r ) {
FILE *f1;
if ( rank==0 ) f1 = fopen(fname, "w");
else f1 = fopen(fname, "a");
// char rankName[255];
// sprintf(rankName, "%s.%d", fname, rank);
// f1 = fopen(rankName, "w");
for (PetscInt c = range.start; c < range.end; ++c) {
PetscInt cell = range.points ? range.points[c] : c;
DMPolytopeType ct;
DMPlexGetCellType(dm, cell, &ct) >> ablate::utilities::PetscUtilities::checkError;
if (ct < 12) {
PetscReal x0[3];
DMPlexComputeCellGeometryFVM(dm, cell, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt d = 0; d < dim; ++d) {
fprintf(f1, "%+e\t", x0[d]);
}
const PetscScalar *val;
DMPlexPointLocalFieldRead(dm, cell, id, array, &val) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt i = 0; i < Nc; ++i) {
fprintf(f1, "%+e\t", val[i]);
}
fprintf(f1, "\n");
}
}
fclose(f1);
}
MPI_Barrier(PETSC_COMM_WORLD);
}
VecRestoreArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
ablate::domain::RestoreRange(range);
}
inline PetscReal SmoothDirac(PetscReal c, PetscReal c0, PetscReal t) {
return (PetscAbsReal(c-c0) < t ? 0.5*(1.0 + cos(M_PI*(c - c0)/t))/t : 0);
}
PetscErrorCode ablate::finiteVolume::processes::SurfaceForce::ComputeSource(const ablate::finiteVolume::FiniteVolumeSolver &flow, DM dm, PetscReal time, Vec locX, Vec locF, void *ctx) {
PetscFunctionBegin;
ablate::finiteVolume::processes::SurfaceForce *process = (ablate::finiteVolume::processes::SurfaceForce *)ctx;
std::shared_ptr<ablate::domain::SubDomain> subDomain = process->subDomain;
const PetscInt dim = subDomain->GetDimensions();
ablate::domain::Range cellRange;
//const ablate::domain::Field *vofField = &(subDomain->GetField(TwoPhaseEulerAdvection::VOLUME_FRACTION_FIELD));
//process->reconstruction->ToLevelSet(dm, locX, *vofField);
xexit("");
Vec auxVector = subDomain->GetAuxVector(); // Getting the Auxiliary Vector (contains data for all aux fields)
DM aux_dm = subDomain->GetAuxDM(); // Getting the Auxiliary DM which has Auxiliary fields
const ablate::domain::Field *levelSetField = &(subDomain->GetField("levelSet")); // Getting the level set field for vertices (FEM)
process->reconstruction->arbit_interface(aux_dm, *levelSetField, auxVector);
const ablate::domain::Field *eulerField = &(subDomain->GetField(ablate::finiteVolume::CompressibleFlowFields::EULER_FIELD));
DM eulerDM = subDomain->GetFieldDM(*eulerField); // Get an euler-specific DM in case it's not in the same solution vector as the VOF field
const PetscReal sigma = process->sigma; // Surface tension coefficient
PetscScalar *fArray = nullptr;
PetscScalar *auxArray = nullptr;
const PetscScalar *xArray = nullptr;
DM auxDM = subDomain->GetAuxDM();
Vec auxVec = subDomain->GetAuxVector();
VecGetArray(locF, &fArray) >> utilities::PetscUtilities::checkError;
VecGetArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError;
VecGetArray(auxVec, &auxArray) >> ablate::utilities::PetscUtilities::checkError;
flow.GetCellRangeWithoutGhost(cellRange);
for (PetscInt c = cellRange.start; c < cellRange.end; ++c) {
PetscInt cell = cellRange.GetPoint(c);
if (ablate::levelSet::Utilities::ValidCell(auxDM, cell)) {
const PetscScalar *euler = nullptr;
xDMPlexPointLocalRead(eulerDM, cell, eulerField->id, xArray, &euler) >> utilities::PetscUtilities::checkError;
const PetscScalar density = euler[ablate::finiteVolume::CompressibleFlowFields::RHO];
PetscScalar *eulerSource = nullptr;
xDMPlexPointLocalRef(eulerDM, cell, eulerField->id, fArray, &eulerSource) >> utilities::PetscUtilities::checkError;
PetscReal testForce[3] = {sigma, 0.0, 0.0};
PetscReal x[3];
DMPlexComputeCellGeometryFVM(eulerDM, cell, NULL, x, NULL) >> ablate::utilities::PetscUtilities::checkError;
if (x[0] < -1.0 || x[0] > 1.0 || x[1] < -1.0 || x[1] > 1.0) {
testForce[0] = 0.0;
}
for (PetscInt d = 0; d < dim; ++d) {
// calculate surface force and energy
PetscReal surfaceForce = testForce[d];
PetscReal vel = euler[ablate::finiteVolume::CompressibleFlowFields::RHOU + d] / density;
PetscReal surfaceEnergy = surfaceForce * vel;
// add in the contributions
eulerSource[ablate::finiteVolume::CompressibleFlowFields::RHOU + d] += surfaceForce;
eulerSource[ablate::finiteVolume::CompressibleFlowFields::RHOE] += surfaceEnergy;
}
}
}
flow.RestoreRange(cellRange);
// Cleanup
VecRestoreArray(locF, &fArray) >> utilities::PetscUtilities::checkError;
VecRestoreArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError;
VecRestoreArray(auxVec, &auxArray) >> ablate::utilities::PetscUtilities::checkError;
PetscFunctionReturn(PETSC_SUCCESS);
}
//PetscErrorCode ablate::finiteVolume::processes::SurfaceForce::ComputeSource(const ablate::finiteVolume::FiniteVolumeSolver &flow, DM dm, PetscReal time, Vec locX, Vec locF, void *ctx) {
// PetscFunctionBegin;
// ablate::finiteVolume::processes::SurfaceForce *process = (ablate::finiteVolume::processes::SurfaceForce *)ctx;
// std::shared_ptr<ablate::domain::SubDomain> subDomain = process->subDomain;
// const PetscInt dim = subDomain->GetDimensions();
// ablate::domain::Range cellRange;
// const ablate::domain::Field *vofField = &(subDomain->GetField(TwoPhaseEulerAdvection::VOLUME_FRACTION_FIELD));
//process->reconstruction->ToLevelSet(dm, locX, *vofField);
// const ablate::domain::Field *lsField = &(subDomain->GetField("levelSet"));
// const ablate::domain::Field *vertexNormalField = &(subDomain->GetField("vertexNormal"));
// const ablate::domain::Field *curvField = &(subDomain->GetField("curvature"));
// const ablate::domain::Field *cellNormalField = &(subDomain->GetField("cellNormal"));
// const ablate::domain::Field *eulerField = &(subDomain->GetField(ablate::finiteVolume::CompressibleFlowFields::EULER_FIELD));
// DM eulerDM = subDomain->GetFieldDM(*eulerField); // Get an euler-specific DM in case it's not in the same solution vector as the VOF field
// const PetscReal sigma = process->sigma; // Surface tension coefficient
// PetscScalar *fArray = nullptr;
// PetscScalar *auxArray = nullptr;
// const PetscScalar *xArray = nullptr;
// // The grid spacing
// PetscReal h;
// DMPlexGetMinRadius(dm, &h) >> ablate::utilities::PetscUtilities::checkError;
// h *= 2.0; // Min radius returns the distance between a cell-center and a face. Double it to get the average cell size
// ablate::levelSet::Utilities::Reinitialize(flow, subDomain, locX, vofField, 8, lsField, vertexNormalField, cellNormalField, curvField);
//xexit("");
// DM auxDM = subDomain->GetAuxDM();
// Vec auxVec = subDomain->GetAuxVector();
// VecGetArray(locF, &fArray) >> utilities::PetscUtilities::checkError;
// VecGetArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError;
// VecGetArray(auxVec, &auxArray) >> ablate::utilities::PetscUtilities::checkError;
// const ablate::domain::Field *tensionForceField = &(subDomain->GetField("surfaceTensionForce"));
// flow.GetCellRangeWithoutGhost(cellRange);
// for (PetscInt c = cellRange.start; c < cellRange.end; ++c) {
// PetscInt cell = cellRange.GetPoint(c);
// PetscScalar *force = nullptr;
// xDMPlexPointLocalRef(auxDM, cell, tensionForceField->id, auxArray, &force) >> utilities::PetscUtilities::checkError;
// for (PetscInt d = 0; d < dim; ++d) force[d] = 0.0;
// if (ablate::levelSet::Utilities::ValidCell(auxDM, cell)) {
// PetscReal cellPhi = 0.0, dirac = -1.0;
// PetscInt nv, *verts;
// DMPlexCellGetVertices(auxDM, cell, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
// for (PetscInt v = 0; v < nv; ++v) {
// const PetscReal *phi = nullptr;
// xDMPlexPointLocalRead(auxDM, verts[v], lsField->id, auxArray, &phi);
// cellPhi += *phi;
// }
// cellPhi /= nv;
// DMPlexCellRestoreVertices(auxDM, cell, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
// dirac = SmoothDirac(cellPhi, 0.0, 2.0*h);
// if (dirac > 1e-10){
// // Normal at the cell-center
// PetscReal *n = nullptr;
// xDMPlexPointLocalRead(auxDM, cell, cellNormalField->id, auxArray, &n);
// // Curvature at the cell-center
// PetscReal *H = nullptr;
// xDMPlexPointLocalRead(auxDM, cell, curvField->id, auxArray, &H);
// const PetscScalar *euler = nullptr;
// xDMPlexPointLocalRead(eulerDM, cell, eulerField->id, xArray, &euler) >> utilities::PetscUtilities::checkError;
// const PetscScalar density = euler[ablate::finiteVolume::CompressibleFlowFields::RHO];
// PetscScalar *eulerSource = nullptr;
// xDMPlexPointLocalRef(eulerDM, cell, eulerField->id, fArray, &eulerSource) >> utilities::PetscUtilities::checkError;
// for (PetscInt d = 0; d < dim; ++d) {
// // calculate surface force and energy
// PetscReal surfaceForce = - dirac* density * sigma * H[0] * n[d];
// PetscReal vel = euler[ablate::finiteVolume::CompressibleFlowFields::RHOU + d] / density;
// PetscReal surfaceEnergy = surfaceForce * vel;
// // add in the contributions
// eulerSource[ablate::finiteVolume::CompressibleFlowFields::RHOU + d] += surfaceForce;
// eulerSource[ablate::finiteVolume::CompressibleFlowFields::RHOE] += surfaceEnergy;
// force[d] = surfaceForce;
// }
// }
// }
// }
// flow.RestoreRange(cellRange);
// // Cleanup
// VecRestoreArray(locF, &fArray) >> utilities::PetscUtilities::checkError;
// VecRestoreArrayRead(locX, &xArray) >> utilities::PetscUtilities::checkError;
// VecRestoreArray(auxVec, &auxArray) >> ablate::utilities::PetscUtilities::checkError;
// PetscFunctionReturn(PETSC_SUCCESS);
//}
ablate::finiteVolume::processes::SurfaceForce::~SurfaceForce() {
}
REGISTER(ablate::finiteVolume::processes::Process, ablate::finiteVolume::processes::SurfaceForce, "calculates surface tension force and adds source terms",
ARG(PetscReal, "sigma", "sigma, surface tension coefficient"));