forked from topopt/TopOpt_in_PETSc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filter.cc
519 lines (461 loc) · 18 KB
/
Filter.cc
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#include "Filter.h"
/* -----------------------------------------------------------------------------
Authors: Niels Aage, Erik Andreassen, Boyan Lazarov, August 2013
Copyright (C) 2013-2014,
This Filter implementation is licensed under Version 2.1 of the GNU
Lesser General Public License.
This MMA implementation is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This Module is distributed in the hope that it will be useful,implementation
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this Module; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------- */
Filter::Filter(DM da_nodes, Vec x, PetscInt filterT, PetscScalar Rin) {
// Set all pointers to NULL
H = NULL;
Hs = NULL;
da_elem = NULL;
pdef = NULL;
// Get parameters
R = Rin;
filterType = filterT;
// Call the setup method
SetUp(da_nodes, x);
}
Filter::~Filter() {
// Deallocate data
if (Hs != NULL) {
VecDestroy(&Hs);
}
if (H != NULL) {
MatDestroy(&H);
}
if (da_elem != NULL) {
DMDestroy(&da_elem);
}
if (pdef != NULL) {
delete pdef;
}
if (dx != NULL) {
VecDestroy(&dx);
}
}
// Filter design variables
PetscErrorCode Filter::FilterProject(Vec x, Vec xTilde, Vec xPhys, PetscBool projectionFilter, PetscScalar beta,
PetscScalar eta) {
PetscErrorCode ierr;
// Filter the design variables or copy to xPhys
// STANDARD FILTER
if (filterType == 1) {
// Filter the densitities
ierr = MatMult(H, x, xTilde);
CHKERRQ(ierr);
VecPointwiseDivide(xTilde, xTilde, Hs);
}
// PDE FILTER
else if (filterType == 2) {
ierr = pdef->FilterProject(x, xTilde);
CHKERRQ(ierr);
// Check for bound violation: simple, but cheap check!
PetscScalar* xp;
PetscInt locsiz;
VecGetArray(xTilde, &xp);
VecGetLocalSize(xTilde, &locsiz);
for (PetscInt i = 0; i < locsiz; i++) {
if (xp[i] < 0.0) {
if (PetscAbsReal(xp[i]) > 1.0e-4) {
PetscPrintf(PETSC_COMM_WORLD,
"BOUND VIOLATION IN PDEFILTER - INCREASE RMIN OR MESH "
"RESOLUTION: xPhys = %f\n",
xp[i]);
}
xp[i] = 0.0;
}
if (xp[i] > 1.0) {
if (PetscAbsReal(xp[i] - 1.0) > 1.0e-4) {
PetscPrintf(PETSC_COMM_WORLD,
"BOUND VIOLATION IN PDEFILTER - INCREASE RMIN OR MESH "
"RESOLUTION: xPhys = %f\n",
xp[i]);
}
xp[i] = 1.0;
}
}
VecRestoreArray(xTilde, &xp);
}
// COPY IN CASE OF SENSITIVITY FILTER
else {
ierr = VecCopy(x, xTilde);
CHKERRQ(ierr);
}
// Check for projection
if (projectionFilter) {
HeavisideFilter(xPhys, xTilde, beta, eta);
} else {
VecCopy(xTilde, xPhys);
}
return ierr;
}
// Filter the sensitivities
PetscErrorCode Filter::Gradients(Vec x, Vec xTilde, Vec dfdx, PetscInt m, Vec* dgdx, PetscBool projectionFilter,
PetscScalar beta, PetscScalar eta) {
PetscErrorCode ierr;
// Cheinrule for projection filtering
if (projectionFilter) {
// Get correction
ChainruleHeavisideFilter(dx, xTilde, beta, eta);
PetscScalar *xt, *dg, *df, *dxp;
PetscInt locsiz;
ierr = VecGetLocalSize(xTilde, &locsiz);
CHKERRQ(ierr);
ierr = VecGetArray(xTilde, &xt);
CHKERRQ(ierr);
ierr = VecGetArray(dx, &dxp);
CHKERRQ(ierr);
// Objective function
ierr = VecGetArray(dfdx, &df);
CHKERRQ(ierr);
for (PetscInt j = 0; j < locsiz; j++) {
df[j] = df[j] * dxp[j];
}
ierr = VecRestoreArray(dfdx, &df);
CHKERRQ(ierr);
// Run through all constraints
for (PetscInt i = 0; i < m; i++) {
ierr = VecGetArray(dgdx[i], &dg);
CHKERRQ(ierr);
// The eta item corresponding to the correct realization
for (PetscInt j = 0; j < locsiz; j++) {
dg[j] = dg[j] * dxp[j];
}
ierr = VecRestoreArray(dgdx[i], &dg);
CHKERRQ(ierr);
}
ierr = VecRestoreArray(dx, &dxp);
CHKERRQ(ierr);
ierr = VecRestoreArray(dgdx[0], &dg);
CHKERRQ(ierr);
ierr = VecRestoreArray(xTilde, &xt);
CHKERRQ(ierr);
}
// Chainrule/Filter for the sensitivities
if (filterType == 0)
// Filter the sensitivities, df,dg
{
Vec xtmp;
ierr = VecDuplicate(xTilde, &xtmp);
CHKERRQ(ierr);
VecPointwiseMult(xtmp, dfdx, x);
MatMult(H, xtmp, dfdx);
VecPointwiseDivide(xtmp, dfdx, Hs);
VecPointwiseDivide(dfdx, xtmp, x);
VecDestroy(&xtmp);
} else if (filterType == 1) {
// Filter the densities, df,dg: STANDARD FILTER
Vec xtmp;
ierr = VecDuplicate(x, &xtmp);
CHKERRQ(ierr);
// dfdx
VecPointwiseDivide(xtmp, dfdx, Hs);
MatMult(H, xtmp, dfdx);
// dgdx
for (PetscInt i = 0; i < m; i++) {
VecPointwiseDivide(xtmp, dgdx[i], Hs);
MatMult(H, xtmp, dgdx[i]);
}
// tidy up
VecDestroy(&xtmp);
} else if (filterType == 2) {
// Filter the densities, df,dg: PDE FILTER
ierr = pdef->Gradients(dfdx, dfdx);
CHKERRQ(ierr);
for (PetscInt i = 0; i < m; i++) {
ierr = pdef->Gradients(dgdx[i], dgdx[i]);
CHKERRQ(ierr);
}
}
return ierr;
}
PetscScalar Filter::GetMND(Vec x) {
PetscScalar mnd, mndloc = 0.0;
PetscScalar* xv;
PetscInt nelloc, nelglob;
VecGetLocalSize(x, &nelloc);
VecGetSize(x, &nelglob);
// Compute power sum
VecGetArray(x, &xv);
for (PetscInt i = 0; i < nelloc; i++) {
mndloc += 4 * xv[i] * (1.0 - xv[i]);
}
// Collect from procs
MPI_Allreduce(&mndloc, &mnd, 1, MPIU_SCALAR, MPI_SUM, PETSC_COMM_WORLD);
mnd = mnd / ((PetscScalar)nelglob);
return mnd;
}
PetscErrorCode Filter::HeavisideFilter(Vec y, Vec x, PetscReal beta, PetscReal eta) {
PetscErrorCode ierr;
PetscScalar *yp, *xp;
PetscInt nelloc;
VecGetLocalSize(x, &nelloc);
ierr = VecGetArray(x, &xp);
CHKERRQ(ierr);
ierr = VecGetArray(y, &yp);
CHKERRQ(ierr);
for (PetscInt i = 0; i < nelloc; i++) {
yp[i] = SmoothProjection(xp[i], beta, eta);
}
ierr = VecRestoreArray(x, &xp);
CHKERRQ(ierr);
ierr = VecRestoreArray(y, &yp);
CHKERRQ(ierr);
}
PetscErrorCode Filter::ChainruleHeavisideFilter(Vec y, Vec x, PetscReal beta, PetscReal eta) {
PetscErrorCode ierr;
PetscScalar *yp, *xp;
PetscInt nelloc;
VecGetLocalSize(x, &nelloc);
ierr = VecGetArray(x, &xp);
CHKERRQ(ierr);
ierr = VecGetArray(y, &yp);
CHKERRQ(ierr);
for (PetscInt i = 0; i < nelloc; i++) {
yp[i] = ChainruleSmoothProjection(xp[i], beta, eta);
}
ierr = VecRestoreArray(x, &xp);
CHKERRQ(ierr);
ierr = VecRestoreArray(y, &yp);
CHKERRQ(ierr);
}
// Continuation function
PetscBool Filter::IncreaseBeta(PetscReal* beta, PetscReal betaFinal, PetscScalar gx, PetscInt itr, PetscReal ch) {
PetscBool changeBeta = PETSC_FALSE;
// Increase beta when fitting
if ((ch < 0.01 || itr % 10 == 0) && beta[0] < betaFinal && gx < 0.000001) {
changeBeta = PETSC_TRUE;
if (beta[0] < 7) {
beta[0] = beta[0] + 1;
} else {
beta[0] = beta[0] * 1.2;
}
if (beta[0] > betaFinal) {
beta[0] = betaFinal;
changeBeta = PETSC_FALSE;
}
PetscPrintf(PETSC_COMM_WORLD, "Beta has been increased to: %f\n", beta[0]);
}
return changeBeta;
}
PetscErrorCode Filter::SetUp(DM da_nodes, Vec x) {
PetscErrorCode ierr;
VecDuplicate(x, &dx);
VecSet(dx, 1.0);
if (filterType == 0 || filterType == 1) {
// Extract information from the nodal mesh
PetscInt M, N, P, md, nd, pd;
DMBoundaryType bx, by, bz;
DMDAStencilType stype;
ierr = DMDAGetInfo(da_nodes, NULL, &M, &N, &P, &md, &nd, &pd, NULL, NULL, &bx, &by, &bz, &stype);
CHKERRQ(ierr);
// Find the element size
Vec lcoor;
DMGetCoordinatesLocal(da_nodes, &lcoor);
PetscScalar* lcoorp;
VecGetArray(lcoor, &lcoorp);
PetscInt nel, nen;
const PetscInt* necon;
DMDAGetElements_3D(da_nodes, &nel, &nen, &necon);
PetscScalar dx, dy, dz;
// Use the first element to compute the dx, dy, dz
dx = lcoorp[3 * necon[0 * nen + 1] + 0] - lcoorp[3 * necon[0 * nen + 0] + 0];
dy = lcoorp[3 * necon[0 * nen + 2] + 1] - lcoorp[3 * necon[0 * nen + 1] + 1];
dz = lcoorp[3 * necon[0 * nen + 4] + 2] - lcoorp[3 * necon[0 * nen + 0] + 2];
VecRestoreArray(lcoor, &lcoorp);
// Create the minimum element connectivity shit
PetscInt ElemConn;
// Check dx,dy,dz and find max conn for a given rmin
ElemConn = (PetscInt)PetscMax(ceil(R / dx) - 1, PetscMax(ceil(R / dy) - 1, ceil(R / dz) - 1));
ElemConn = PetscMin(ElemConn, PetscMin((M - 1) / 2, PetscMin((N - 1) / 2, (P - 1) / 2)));
// The following is needed due to roundoff errors
PetscInt tmp;
MPI_Allreduce(&ElemConn, &tmp, 1, MPIU_INT, MPI_MAX, PETSC_COMM_WORLD);
ElemConn = tmp;
// Print to screen: mesh overlap!
PetscPrintf(PETSC_COMM_WORLD, "# Filter radius rmin = %f results in a stencil of %i elements \n", R, ElemConn);
// Find the geometric partitioning of the nodal mesh, so the element mesh
// will coincide
PetscInt* Lx = new PetscInt[md];
PetscInt* Ly = new PetscInt[nd];
PetscInt* Lz = new PetscInt[pd];
// get number of nodes for each partition
const PetscInt *LxCorrect, *LyCorrect, *LzCorrect;
DMDAGetOwnershipRanges(da_nodes, &LxCorrect, &LyCorrect, &LzCorrect);
// subtract one from the lower left corner.
for (int i = 0; i < md; i++) {
Lx[i] = LxCorrect[i];
if (i == 0) {
Lx[i] = Lx[i] - 1;
}
}
for (int i = 0; i < nd; i++) {
Ly[i] = LyCorrect[i];
if (i == 0) {
Ly[i] = Ly[i] - 1;
}
}
for (int i = 0; i < pd; i++) {
Lz[i] = LzCorrect[i];
if (i == 0) {
Lz[i] = Lz[i] - 1;
}
}
// Create the element grid:
DMDACreate3d(PETSC_COMM_WORLD, bx, by, bz, stype, M - 1, N - 1, P - 1, md, nd, pd, 1, ElemConn, Lx, Ly, Lz,
&da_elem);
// Initialize
DMSetFromOptions(da_elem);
DMSetUp(da_elem);
// Set the coordinates: from 0+dx/2 to xmax-dx/2 and so on
PetscScalar xmax = (M - 1) * dx;
PetscScalar ymax = (N - 1) * dy;
PetscScalar zmax = (P - 1) * dz;
DMDASetUniformCoordinates(da_elem, dx / 2.0, xmax - dx / 2.0, dy / 2.0, ymax - dy / 2.0, dz / 2.0,
zmax - dz / 2.0);
// Allocate and assemble
DMCreateMatrix(da_elem, &H);
DMCreateGlobalVector(da_elem, &Hs);
// Set the filter matrix and vector
DMGetCoordinatesLocal(da_elem, &lcoor);
VecGetArray(lcoor, &lcoorp);
DMDALocalInfo info;
DMDAGetLocalInfo(da_elem, &info);
// The variables from info that are used are described below:
// -------------------------------------------------------------------------
// sw = Stencil width
// mx, my, mz = Global number of "elements" in each direction
// xs, ys, zs = Starting point of this processor, excluding ghosts
// xm, ym, zm = Number of grid points on this processor, excluding ghosts
// gxs, gys, gzs = Starting point of this processor, including ghosts
// gxm, gym, gzm = Number of grid points on this processor, including ghosts
// -------------------------------------------------------------------------
// Outer loop is local part = find row
// What is done here, is:
//
// 1. Run through all elements in the mesh - should not include ghosts
for (PetscInt k = info.zs; k < info.zs + info.zm; k++) {
for (PetscInt j = info.ys; j < info.ys + info.ym; j++) {
for (PetscInt i = info.xs; i < info.xs + info.xm; i++) {
// The row number of the element we are considering:
PetscInt row =
(i - info.gxs) + (j - info.gys) * (info.gxm) + (k - info.gzs) * (info.gxm) * (info.gym);
//
// 2. Loop over nodes (including ghosts) within a cubic domain with
// center at (i,j,k)
// For each element, run through all elements in a box of size
// stencilWidth * stencilWidth * stencilWidth Remark, we want to
// make sure we are not running "out of the domain", therefore k2
// etc. are limited to the max global index (info.mz-1 etc.)
for (PetscInt k2 = PetscMax(k - info.sw, 0); k2 <= PetscMin(k + info.sw, info.mz - 1); k2++) {
for (PetscInt j2 = PetscMax(j - info.sw, 0); j2 <= PetscMin(j + info.sw, info.my - 1); j2++) {
for (PetscInt i2 = PetscMax(i - info.sw, 0); i2 <= PetscMin(i + info.sw, info.mx - 1);
i2++) {
PetscInt col = (i2 - info.gxs) + (j2 - info.gys) * (info.gxm) +
(k2 - info.gzs) * (info.gxm) * (info.gym);
PetscScalar dist = 0.0;
// Compute the distance from the "col"-element to the
// "row"-element
for (PetscInt kk = 0; kk < 3; kk++) {
dist = dist + PetscPowScalar(lcoorp[3 * row + kk] - lcoorp[3 * col + kk], 2.0);
}
dist = PetscSqrtScalar(dist);
if (dist < R) {
// Longer distances should have less weight
dist = R - dist;
MatSetValuesLocal(H, 1, &row, 1, &col, &dist, INSERT_VALUES);
}
}
}
}
}
}
}
// Assemble H:
MatAssemblyBegin(H, MAT_FINAL_ASSEMBLY);
MatAssemblyEnd(H, MAT_FINAL_ASSEMBLY);
// Compute the Hs, i.e. sum the rows
Vec dummy;
VecDuplicate(Hs, &dummy);
VecSet(dummy, 1.0);
MatMult(H, dummy, Hs);
// Clean up
VecRestoreArray(lcoor, &lcoorp);
VecDestroy(&dummy);
delete[] Lx;
delete[] Ly;
delete[] Lz;
} else if (filterType == 2) {
// ALLOCATE AND SETUP THE PDE FILTER CLASS
pdef = new PDEFilt(da_nodes, R);
}
return ierr;
}
PetscErrorCode Filter::DMDAGetElements_3D(DM dm, PetscInt* nel, PetscInt* nen, const PetscInt* e[]) {
PetscErrorCode ierr;
DM_DA* da = (DM_DA*)dm->data;
PetscInt i, xs, xe, Xs, Xe;
PetscInt j, ys, ye, Ys, Ye;
PetscInt k, zs, ze, Zs, Ze;
PetscInt cnt = 0, cell[8], ns = 1, nn = 8;
PetscInt c;
if (!da->e) {
if (da->elementtype == DMDA_ELEMENT_Q1) {
ns = 1;
nn = 8;
}
ierr = DMDAGetCorners(dm, &xs, &ys, &zs, &xe, &ye, &ze);
CHKERRQ(ierr);
ierr = DMDAGetGhostCorners(dm, &Xs, &Ys, &Zs, &Xe, &Ye, &Ze);
CHKERRQ(ierr);
xe += xs;
Xe += Xs;
if (xs != Xs)
xs -= 1;
ye += ys;
Ye += Ys;
if (ys != Ys)
ys -= 1;
ze += zs;
Ze += Zs;
if (zs != Zs)
zs -= 1;
da->ne = ns * (xe - xs - 1) * (ye - ys - 1) * (ze - zs - 1);
PetscMalloc((1 + nn * da->ne) * sizeof(PetscInt), &da->e);
for (k = zs; k < ze - 1; k++) {
for (j = ys; j < ye - 1; j++) {
for (i = xs; i < xe - 1; i++) {
cell[0] = (i - Xs) + (j - Ys) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
cell[1] = (i - Xs + 1) + (j - Ys) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
cell[2] = (i - Xs + 1) + (j - Ys + 1) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
cell[3] = (i - Xs) + (j - Ys + 1) * (Xe - Xs) + (k - Zs) * (Xe - Xs) * (Ye - Ys);
cell[4] = (i - Xs) + (j - Ys) * (Xe - Xs) + (k - Zs + 1) * (Xe - Xs) * (Ye - Ys);
cell[5] = (i - Xs + 1) + (j - Ys) * (Xe - Xs) + (k - Zs + 1) * (Xe - Xs) * (Ye - Ys);
cell[6] = (i - Xs + 1) + (j - Ys + 1) * (Xe - Xs) + (k - Zs + 1) * (Xe - Xs) * (Ye - Ys);
cell[7] = (i - Xs) + (j - Ys + 1) * (Xe - Xs) + (k - Zs + 1) * (Xe - Xs) * (Ye - Ys);
if (da->elementtype == DMDA_ELEMENT_Q1) {
for (c = 0; c < ns * nn; c++)
da->e[cnt++] = cell[c];
}
}
}
}
}
*nel = da->ne;
*nen = nn;
*e = da->e;
return (0);
}