-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinclusion_solver.cpp
469 lines (383 loc) · 12.2 KB
/
inclusion_solver.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
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
#include "inclusion_solver.hpp"
using namespace std;
using namespace mfem;
InclusionSolver::InclusionSolver(ParMesh & pmesh, int order,
Array<int> & dbcs,
Coefficient & epsCoef,
double (*phi_bc )(const Vector&))
: myid_(0),
num_procs_(1),
order_(order),
pmesh_(&pmesh),
dbcs_(&dbcs),
visit_dc_(NULL),
H1FESpace_(NULL),
HCurlFESpace_(NULL),
HDivFESpace_(NULL),
L2FESpace_(NULL),
divEpsGrad_(NULL),
h1Mass_(NULL),
h1SurfMass_(NULL),
hDivMass_(NULL),
hCurlHDivEps_(NULL),
hCurlHDiv_(NULL),
weakDiv_(NULL),
rhod_(NULL),
l2_vol_int_(NULL),
rt_surf_int_(NULL),
grad_(NULL),
phi_(NULL),
rho_(NULL),
e_(NULL),
d_(NULL),
oneCoef_(1.0),
epsCoef_(&epsCoef),
phiBCCoef_(NULL),
pCoef_(NULL),
phi_bc_func_(phi_bc)
{
// Initialize MPI variables
MPI_Comm_size(pmesh_->GetComm(), &num_procs_);
MPI_Comm_rank(pmesh_->GetComm(), &myid_);
// Define compatible parallel finite element spaces on the parallel
// mesh. Here we use arbitrary order H1, Nedelec, and Raviart-Thomas finite
// elements.
H1FESpace_ = new H1_ParFESpace(pmesh_,order,pmesh_->Dimension());
HCurlFESpace_ = new ND_ParFESpace(pmesh_,order,pmesh_->Dimension());
HDivFESpace_ = new RT_ParFESpace(pmesh_,order,pmesh_->Dimension());
L2FESpace_ = new L2_ParFESpace(pmesh_,order-1,pmesh_->Dimension());
// Select surface attributes for Dirichlet BCs
/* AttrToMarker(pmesh.bdr_attributes.Max(), *dbcs_, ess_bdr_); */
ess_bdr_.SetSize(pmesh.bdr_attributes.Max());
ess_bdr_ = 0;
for (int i=0; i<dbcs.Size(); i++)
{
int attr = dbcs[i];
MFEM_VERIFY(attr > 0, "Attribute number less than one!");
ess_bdr_[attr-1] = 1;
}
// Setup various coefficients
// Potential on outer surface
if ( phi_bc_func_ != NULL )
{
phiBCCoef_ = new FunctionCoefficient(*phi_bc_func_);
}
// Bilinear Forms
divEpsGrad_ = new ParBilinearForm(H1FESpace_);
divEpsGrad_->AddDomainIntegrator(new DiffusionIntegrator(*epsCoef_));
hDivMass_ = new ParBilinearForm(HDivFESpace_);
hDivMass_->AddDomainIntegrator(new VectorFEMassIntegrator);
hCurlHDivEps_ = new ParMixedBilinearForm(HCurlFESpace_,HDivFESpace_);
hCurlHDivEps_->AddDomainIntegrator(new VectorFEMassIntegrator(*epsCoef_));
rhod_ = new ParLinearForm(H1FESpace_);
l2_vol_int_ = new ParLinearForm(L2FESpace_);
l2_vol_int_->AddDomainIntegrator(new DomainLFIntegrator(oneCoef_));
rt_surf_int_ = new ParLinearForm(HDivFESpace_);
rt_surf_int_->AddBoundaryIntegrator(new VectorFEBoundaryFluxLFIntegrator);
// Discrete derivative operator
grad_ = new ParDiscreteGradOperator(H1FESpace_, HCurlFESpace_);
div_ = new ParDiscreteDivOperator(HDivFESpace_, L2FESpace_);
// Build grid functions
phi_ = new ParGridFunction(H1FESpace_);
d_ = new ParGridFunction(HDivFESpace_);
e_ = new ParGridFunction(HCurlFESpace_);
rho_ = new ParGridFunction(L2FESpace_);
}
InclusionSolver::~InclusionSolver()
{
delete phiBCCoef_;
/* delete rhoCoef_; */
delete pCoef_;
delete phi_;
delete rho_;
delete rhod_;
delete l2_vol_int_;
delete rt_surf_int_;
delete d_;
delete e_;
delete grad_;
delete div_;
delete divEpsGrad_;
delete h1Mass_;
delete h1SurfMass_;
delete hDivMass_;
delete hCurlHDivEps_;
delete hCurlHDiv_;
delete weakDiv_;
delete H1FESpace_;
delete HCurlFESpace_;
delete HDivFESpace_;
delete L2FESpace_;
map<string,socketstream*>::iterator mit;
for (mit=socks_.begin(); mit!=socks_.end(); mit++)
{
delete mit->second;
}
}
HYPRE_BigInt
InclusionSolver::GetProblemSize()
{
return H1FESpace_->GlobalTrueVSize();
}
void
InclusionSolver::PrintSizes()
{
HYPRE_BigInt size_h1 = H1FESpace_->GlobalTrueVSize();
HYPRE_BigInt size_nd = HCurlFESpace_->GlobalTrueVSize();
HYPRE_BigInt size_rt = HDivFESpace_->GlobalTrueVSize();
HYPRE_BigInt size_l2 = L2FESpace_->GlobalTrueVSize();
if (myid_ == 0)
{
cout << "Number of H1 unknowns: " << size_h1 << endl;
cout << "Number of H(Curl) unknowns: " << size_nd << endl;
cout << "Number of H(Div) unknowns: " << size_rt << endl;
cout << "Number of L2 unknowns: " << size_l2 << endl;
}
}
void InclusionSolver::Assemble()
{
if (myid_ == 0) { cout << "Assembling ... " << flush; }
divEpsGrad_->Assemble();
divEpsGrad_->Finalize();
hDivMass_->Assemble();
hDivMass_->Finalize();
hCurlHDivEps_->Assemble();
hCurlHDivEps_->Finalize();
*rhod_ = 0.0;
rhod_->Assemble();
l2_vol_int_->Assemble();
rt_surf_int_->Assemble();
grad_->Assemble();
grad_->Finalize();
div_->Assemble();
div_->Finalize();
if ( h1Mass_ )
{
h1Mass_->Assemble();
h1Mass_->Finalize();
}
if ( h1SurfMass_ )
{
h1SurfMass_->Assemble();
h1SurfMass_->Finalize();
}
if ( hCurlHDiv_ )
{
hCurlHDiv_->Assemble();
hCurlHDiv_->Finalize();
}
if ( weakDiv_ )
{
weakDiv_->Assemble();
weakDiv_->Finalize();
}
if (myid_ == 0) { cout << "done." << endl << flush; }
}
void
InclusionSolver::Update()
{
if (myid_ == 0) { cout << "Updating ..." << endl; }
// Inform the spaces that the mesh has changed
// Note: we don't need to interpolate any GridFunctions on the new mesh
// so we pass 'false' to skip creation of any transformation matrices.
H1FESpace_->Update(false);
HCurlFESpace_->Update(false);
HDivFESpace_->Update(false);
L2FESpace_->Update(false);
// Inform the grid functions that the space has changed.
phi_->Update();
rhod_->Update();
l2_vol_int_->Update();
rt_surf_int_->Update();
d_->Update();
e_->Update();
rho_->Update();
// Inform the bilinear forms that the space has changed.
divEpsGrad_->Update();
hDivMass_->Update();
hCurlHDivEps_->Update();
if ( h1Mass_ ) { h1Mass_->Update(); }
if ( h1SurfMass_ ) { h1SurfMass_->Update(); }
if ( hCurlHDiv_ ) { hCurlHDiv_->Update(); }
if ( weakDiv_ ) { weakDiv_->Update(); }
// Inform the other objects that the space has changed.
grad_->Update();
div_->Update();
}
void
InclusionSolver::Solve()
{
if (myid_ == 0) { cout << "Running solver ... " << endl; }
// Initialize the electric potential with its boundary conditions
*phi_ = 0.0;
if ( dbcs_->Size() > 0 )
{
if ( phiBCCoef_ )
{
// Apply gradient boundary condition
phi_->ProjectBdrCoefficient(*phiBCCoef_, ess_bdr_);
}
else
{
MFEM_VERIFY(0, "The code should not reach here!");
}
}
// Determine the essential BC degrees of freedom
if ( dbcs_->Size() > 0 )
{
// From user supplied boundary attributes
H1FESpace_->GetEssentialTrueDofs(ess_bdr_, ess_bdr_tdofs_);
}
else
{
// Use the first DoF on processor zero by default
if ( myid_ == 0 )
{
ess_bdr_tdofs_.SetSize(1);
ess_bdr_tdofs_[0] = 0;
}
}
// Apply essential BC and form linear system
HypreParMatrix DivEpsGrad;
HypreParVector Phi(H1FESpace_);
HypreParVector RHS(H1FESpace_);
divEpsGrad_->FormLinearSystem(ess_bdr_tdofs_, *phi_, *rhod_, DivEpsGrad,
Phi, RHS);
// Define and apply a parallel PCG solver for AX=B with the AMG
// preconditioner from hypre.
HypreBoomerAMG amg(DivEpsGrad);
HyprePCG pcg(DivEpsGrad);
pcg.SetTol(1e-12);
pcg.SetMaxIter(500);
pcg.SetPrintLevel(2);
pcg.SetPreconditioner(amg);
pcg.Mult(RHS, Phi);
// Extract the parallel grid function corresponding to the finite
// element approximation Phi. This is the local solution on each
// processor.
divEpsGrad_->RecoverFEMSolution(Phi, *rhod_, *phi_);
// Compute the negative Gradient of the solution vector. This is
// the magnetic field corresponding to the scalar potential
// represented by phi.
grad_->Mult(*phi_, *e_); *e_ *= -1.0;
// Compute electric displacement (D) from E and P (if present)
if (myid_ == 0) { cout << "Computing D ..." << flush; }
ParGridFunction ed(HDivFESpace_);
hCurlHDivEps_->Mult(*e_, ed);
HypreParMatrix MassHDiv;
Vector ED, D;
Array<int> dbc_dofs_d;
hDivMass_->FormLinearSystem(dbc_dofs_d, *d_, ed, MassHDiv, D, ED);
HyprePCG pcgM(MassHDiv);
pcgM.SetTol(1e-12);
pcgM.SetMaxIter(500);
pcgM.SetPrintLevel(0);
HypreDiagScale diagM;
pcgM.SetPreconditioner(diagM);
pcgM.Mult(ED, D);
hDivMass_->RecoverFEMSolution(D, ed, *d_);
// Compute charge density from rho = Div(D)
div_->Mult(*d_, *rho_);
if (myid_ == 0) { cout << "done." << flush; }
{
// Compute total charge as volume integral of rho
double charge_rho = (*l2_vol_int_)(*rho_);
// Compute total charge as surface integral of D
double charge_D = (*rt_surf_int_)(*d_);
if (myid_ == 0)
{
cout << endl << "Total charge: \n"
<< " Volume integral of charge density: " << charge_rho
<< "\n Surface integral of dielectric flux: " << charge_D
<< endl << flush;
}
}
if (myid_ == 0) { cout << "Solver done. " << endl; }
}
void
InclusionSolver::GetErrorEstimates(Vector & errors)
{
if (myid_ == 0) { cout << "Estimating Error ... " << flush; }
// Space for the discontinuous (original) flux
DiffusionIntegrator flux_integrator(*epsCoef_);
L2_FECollection flux_fec(order_, pmesh_->Dimension());
// ND_FECollection flux_fec(order_, pmesh_->Dimension());
ParFiniteElementSpace flux_fes(pmesh_, &flux_fec, pmesh_->SpaceDimension());
// Space for the smoothed (conforming) flux
double norm_p = 1;
RT_FECollection smooth_flux_fec(order_-1, pmesh_->Dimension());
ParFiniteElementSpace smooth_flux_fes(pmesh_, &smooth_flux_fec);
L2ZZErrorEstimator(flux_integrator, *phi_,
smooth_flux_fes, flux_fes, errors, norm_p);
if (myid_ == 0) { cout << "done." << endl; }
}
void
InclusionSolver::RegisterVisItFields(VisItDataCollection & visit_dc)
{
visit_dc_ = &visit_dc;
visit_dc.RegisterField("Phi", phi_);
visit_dc.RegisterField("D", d_);
visit_dc.RegisterField("E", e_);
visit_dc.RegisterField("Rho", rho_);
}
void
InclusionSolver::WriteVisItFields(int it)
{
if ( visit_dc_ )
{
if (myid_ == 0) { cout << "Writing VisIt files ..." << flush; }
HYPRE_BigInt prob_size = this->GetProblemSize();
visit_dc_->SetCycle(it);
visit_dc_->SetTime(prob_size);
/* visit_dc_->Save(); */
if (myid_ == 0) { cout << " done." << endl; }
}
}
void
InclusionSolver::InitializeGLVis()
{
if ( myid_ == 0 ) { cout << "Opening GLVis sockets." << endl; }
socks_["Phi"] = new socketstream;
socks_["Phi"]->precision(8);
socks_["D"] = new socketstream;
socks_["D"]->precision(8);
socks_["E"] = new socketstream;
socks_["E"]->precision(8);
socks_["Rho"] = new socketstream;
socks_["Rho"]->precision(8);
}
void
InclusionSolver::DisplayToGLVis()
{
if (myid_ == 0) { cout << "Sending data to GLVis ..." << flush; }
char vishost[] = "localhost";
int visport = 19916;
int Wx = 0, Wy = 0; // window position
int Ww = 350, Wh = 350; // window size
int offx = Ww+10, offy = Wh+45; // window offsets
VisualizeField(*socks_["Phi"], vishost, visport,
*phi_, "Electric Potential (Phi)", Wx, Wy, Ww, Wh);
Wx += offx;
VisualizeField(*socks_["E"], vishost, visport,
*e_, "Electric Field (E)", Wx, Wy, Ww, Wh);
Wx += offx;
VisualizeField(*socks_["D"], vishost, visport,
*d_, "Electric Displacement (D)", Wx, Wy, Ww, Wh);
Wx += offx;
VisualizeField(*socks_["Rho"], vishost, visport,
*rho_, "Charge Density", Wx, Wy, Ww, Wh);
if (myid_ == 0) { cout << " done." << endl; }
}
void
InclusionSolver::WriteToVtk(const char* name, int res)
{
char filename[128];
sprintf(filename, "%s_proc%05d.vtk", name, myid_);
ofstream ofs;
ofs.open(filename, ofstream::out);
pmesh_->PrintVTK(ofs, res);
phi_->SaveVTK(ofs, "potential", res);
e_->SaveVTK(ofs, "ElectricField", res);
d_->SaveVTK(ofs, "ElectricDisplacementField", res);
ofs.close();
}