Repository navigation
Expand file tree
/
Copy pathex1.cpp
More file actions
451 lines (408 loc) · 16.1 KB
/
Copy pathex1.cpp
File metadata and controls
451 lines (408 loc) · 16.1 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
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
// MFEM Example 1 - High-Performance Version
//
// Compile with: make ex1
//
// Sample runs: ex1 -m ../../data/fichera.mesh -perf -mf -pc lor
// ex1 -m ../../data/fichera.mesh -perf -asm -pc ho
// ex1 -m ../../data/fichera.mesh -perf -asm -pc ho -sc
// ex1 -m ../../data/fichera.mesh -std -asm -pc ho
// ex1 -m ../../data/fichera.mesh -std -asm -pc ho -sc
// ex1 -m ../../data/amr-hex.mesh -perf -asm -pc ho -sc
// ex1 -m ../../data/amr-hex.mesh -std -asm -pc ho -sc
// ex1 -m ../../data/ball-nurbs.mesh -perf -asm -pc ho -sc
// ex1 -m ../../data/ball-nurbs.mesh -std -asm -pc ho -sc
// ex1 -m ../../data/pipe-nurbs.mesh -perf -mf -pc lor
// ex1 -m ../../data/pipe-nurbs.mesh -std -asm -pc ho -sc
// ex1 -m ../../data/star.mesh -perf -mf -pc lor
// ex1 -m ../../data/star.mesh -perf -asm -pc ho
// ex1 -m ../../data/star.mesh -perf -asm -pc ho -sc
// ex1 -m ../../data/star.mesh -std -asm -pc ho
// ex1 -m ../../data/star.mesh -std -asm -pc ho -sc
// ex1 -m ../../data/amr-quad.mesh -perf -asm -pc ho -sc
// ex1 -m ../../data/amr-quad.mesh -std -asm -pc ho -sc
// ex1 -m ../../data/disc-nurbs.mesh -perf -asm -pc ho -sc
// ex1 -m ../../data/disc-nurbs.mesh -std -asm -pc ho -sc
//
// Description: This example code demonstrates the use of MFEM to define a
// simple finite element discretization of the Poisson problem
// -Delta u = 1 with homogeneous Dirichlet boundary conditions.
// Specifically, we discretize using a FE space of the specified
// order, or if order < 1 using an isoparametric/isogeometric
// space (i.e. quadratic for quadratic curvilinear mesh, NURBS for
// NURBS mesh, etc.)
//
// The example highlights the use of mesh refinement, finite
// element grid functions, as well as linear and bilinear forms
// corresponding to the left-hand side and right-hand side of the
// discrete linear system. We also cover the explicit elimination
// of essential boundary conditions, static condensation, and the
// optional connection to the GLVis tool for visualization.
#include "mfem-performance.hpp"
#include <fstream>
#include <iostream>
using namespace std;
using namespace mfem;
enum class PCType { NONE, LOR, HO };
// Define template parameters for optimized build.
template <int dim> struct geom_t { };
template <>
struct geom_t<2> { static const Geometry::Type value = Geometry::SQUARE; };
template <>
struct geom_t<3> { static const Geometry::Type value = Geometry::CUBE; };
const int mesh_p = 3; // mesh curvature (default: 3)
const int sol_p = 3; // solution order (default: 3)
template <int dim>
struct ex1_t
{
static const Geometry::Type geom = geom_t<dim>::value;
static const int rdim = Geometry::Constants<geom>::Dimension;
static const int ir_order = 2*sol_p+rdim-1;
// Static mesh type
using mesh_fe_t = H1_FiniteElement<geom,mesh_p>;
using mesh_fes_t = H1_FiniteElementSpace<mesh_fe_t>;
using mesh_t = TMesh<mesh_fes_t>;
// Static solution finite element space type
using sol_fe_t = H1_FiniteElement<geom,sol_p>;
using sol_fes_t = H1_FiniteElementSpace<sol_fe_t>;
// Static quadrature, coefficient and integrator types
using int_rule_t = TIntegrationRule<geom,ir_order>;
using coeff_t = TConstantCoefficient<>;
using integ_t = TIntegrator<coeff_t,TDiffusionKernel>;
using HPCBilinearForm = TBilinearForm<mesh_t,sol_fes_t,int_rule_t,integ_t>;
static int run(Mesh *mesh, int ref_levels, int order, int basis,
bool static_cond, PCType pc_choice, bool perf,
bool matrix_free, bool visualization, int visport = 19916);
};
int main(int argc, char *argv[])
{
// 1. Parse command-line options.
const char *mesh_file = "../../data/fichera.mesh";
int ref_levels = -1;
int order = sol_p;
const char *basis_type = "G"; // Gauss-Lobatto
bool static_cond = false;
const char *pc = "none";
bool perf = true;
bool matrix_free = true;
int visport = 19916;
bool visualization = 1;
OptionsParser args(argc, argv);
args.AddOption(&mesh_file, "-m", "--mesh",
"Mesh file to use.");
args.AddOption(&ref_levels, "-r", "--refine",
"Number of times to refine the mesh uniformly;"
" -1 = auto: <= 50,000 elements.");
args.AddOption(&order, "-o", "--order",
"Finite element order (polynomial degree) or -1 for"
" isoparametric space.");
args.AddOption(&basis_type, "-b", "--basis-type",
"Basis: G - Gauss-Lobatto, P - Positive, U - Uniform");
args.AddOption(&perf, "-perf", "--hpc-version", "-std", "--standard-version",
"Enable high-performance, tensor-based, assembly/evaluation.");
args.AddOption(&matrix_free, "-mf", "--matrix-free", "-asm", "--assembly",
"Use matrix-free evaluation or efficient matrix assembly in "
"the high-performance version.");
args.AddOption(&pc, "-pc", "--preconditioner",
"Preconditioner: lor - low-order-refined (matrix-free) GS, "
"ho - high-order (assembled) GS, none.");
args.AddOption(&static_cond, "-sc", "--static-condensation", "-no-sc",
"--no-static-condensation", "Enable static condensation.");
args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
"--no-visualization",
"Enable or disable GLVis visualization.");
args.AddOption(&visport, "-p", "--send-port", "Socket for GLVis.");
args.Parse();
if (!args.Good())
{
args.PrintUsage(cout);
return 1;
}
if (static_cond && perf && matrix_free)
{
cout << "\nStatic condensation can not be used with matrix-free"
" evaluation!\n" << endl;
return 2;
}
MFEM_VERIFY(perf || !matrix_free,
"--standard-version is not compatible with --matrix-free");
args.PrintOptions(cout);
PCType pc_choice;
if (!strcmp(pc, "ho")) { pc_choice = PCType::HO; }
else if (!strcmp(pc, "lor")) { pc_choice = PCType::LOR; }
else if (!strcmp(pc, "none")) { pc_choice = PCType::NONE; }
else
{
mfem_error("Invalid Preconditioner specified");
return 3;
}
cout << "\nMFEM SIMD width: " << MFEM_SIMD_BYTES/sizeof(double)
<< " doubles\n" << endl;
// See class BasisType in fem/fe_coll.hpp for available basis types
int basis = BasisType::GetType(basis_type[0]);
cout << "Using " << BasisType::Name(basis) << " basis ..." << endl;
// 2. Read the mesh from the given mesh file. We can handle triangular,
// quadrilateral, tetrahedral, hexahedral, surface and volume meshes with
// the same code.
Mesh *mesh = new Mesh(mesh_file, 1, 1);
int dim = mesh->Dimension();
if (dim == 2)
{
return ex1_t<2>::run(mesh, ref_levels, order, basis, static_cond,
pc_choice, perf, matrix_free, visualization, visport);
}
else if (dim == 3)
{
return ex1_t<3>::run(mesh, ref_levels, order, basis, static_cond,
pc_choice, perf, matrix_free, visualization, visport);
}
else
{
MFEM_ABORT("Dimension must be 2 or 3.")
}
return 0;
}
template <int dim>
int ex1_t<dim>::run(Mesh *mesh, int ref_levels, int order, int basis,
bool static_cond, PCType pc_choice, bool perf,
bool matrix_free, bool visualization, int visport)
{
// 3. Check if the optimized version matches the given mesh
if (perf)
{
cout << "High-performance version using integration rule with "
<< int_rule_t::qpts << " points ..." << endl;
if (!mesh_t::MatchesGeometry(*mesh))
{
cout << "The given mesh does not match the optimized 'geom' parameter.\n"
<< "Recompile with suitable 'geom' value." << endl;
delete mesh;
return 4;
}
else if (!mesh_t::MatchesNodes(*mesh))
{
cout << "Switching the mesh curvature to match the "
<< "optimized value (order " << mesh_p << ") ..." << endl;
mesh->SetCurvature(mesh_p, false, -1, Ordering::byNODES);
}
}
// 4. Refine the mesh to increase the resolution. In this example we do
// 'ref_levels' of uniform refinement. We choose 'ref_levels' to be the
// largest number that gives a final mesh with no more than 50,000
// elements, or as specified on the command line with the option
// '--refine'.
{
ref_levels = (ref_levels != -1) ? ref_levels :
(int)floor(log(50000./mesh->GetNE())/log(2.)/dim);
for (int l = 0; l < ref_levels; l++)
{
mesh->UniformRefinement();
}
}
if (mesh->MeshGenerator() & 1) // simplex mesh
{
MFEM_VERIFY(pc_choice != PCType::LOR, "triangle and tet meshes do not "
" support the LOR preconditioner yet");
}
// 5. Define a finite element space on the mesh. Here we use continuous
// Lagrange finite elements of the specified order. If order < 1, we
// instead use an isoparametric/isogeometric space.
FiniteElementCollection *fec;
if (order > 0)
{
fec = new H1_FECollection(order, dim, basis);
}
else if (mesh->GetNodes())
{
fec = mesh->GetNodes()->OwnFEC();
cout << "Using isoparametric FEs: " << fec->Name() << endl;
}
else
{
fec = new H1_FECollection(order = 1, dim, basis);
}
FiniteElementSpace *fespace = new FiniteElementSpace(mesh, fec);
cout << "Number of finite element unknowns: "
<< fespace->GetTrueVSize() << endl;
// Create the LOR mesh and finite element space. In the settings of this
// example, we can transfer between HO and LOR with the identity operator.
Mesh mesh_lor;
FiniteElementCollection *fec_lor = NULL;
FiniteElementSpace *fespace_lor = NULL;
if (pc_choice == PCType::LOR)
{
int basis_lor = basis;
if (basis == BasisType::Positive) { basis_lor=BasisType::ClosedUniform; }
mesh_lor = Mesh::MakeRefined(*mesh, order, basis_lor);
fec_lor = new H1_FECollection(1, dim);
fespace_lor = new FiniteElementSpace(&mesh_lor, fec_lor);
}
// 6. Check if the optimized version matches the given space
if (perf && !sol_fes_t::Matches(*fespace))
{
cout << "The given order does not match the optimized parameter.\n"
<< "Recompile with suitable 'sol_p' value." << endl;
delete fespace;
delete fec;
delete mesh;
return 5;
}
// 7. Determine the list of true (i.e. conforming) essential boundary dofs.
// In this example, the boundary conditions are defined by marking all
// the boundary attributes from the mesh as essential (Dirichlet) and
// converting them to a list of true dofs.
Array<int> ess_tdof_list;
if (mesh->bdr_attributes.Size())
{
Array<int> ess_bdr(mesh->bdr_attributes.Max());
ess_bdr = 1;
fespace->GetEssentialTrueDofs(ess_bdr, ess_tdof_list);
}
// 8. Set up the linear form b(.) which corresponds to the right-hand side of
// the FEM linear system, which in this case is (1,phi_i) where phi_i are
// the basis functions in the finite element fespace.
LinearForm *b = new LinearForm(fespace);
ConstantCoefficient one(1.0);
b->AddDomainIntegrator(new DomainLFIntegrator(one));
b->Assemble();
// 9. Define the solution vector x as a finite element grid function
// corresponding to fespace. Initialize x with initial guess of zero,
// which satisfies the boundary conditions.
GridFunction x(fespace);
x = 0.0;
// 10. Set up the bilinear form a(.,.) on the finite element space that will
// hold the matrix corresponding to the Laplacian operator -Delta.
// Optionally setup a form to be assembled for preconditioning (a_pc).
BilinearForm *a = new BilinearForm(fespace);
BilinearForm *a_pc = NULL;
if (pc_choice == PCType::LOR) { a_pc = new BilinearForm(fespace_lor); }
if (pc_choice == PCType::HO) { a_pc = new BilinearForm(fespace); }
// 11. Assemble the bilinear form and the corresponding linear system,
// applying any necessary transformations such as: eliminating boundary
// conditions, applying conforming constraints for non-conforming AMR,
// static condensation, etc.
if (static_cond)
{
a->EnableStaticCondensation();
MFEM_VERIFY(pc_choice != PCType::LOR,
"cannot use LOR preconditioner with static condensation");
}
cout << "Assembling the bilinear form ..." << flush;
tic_toc.Clear();
tic_toc.Start();
// Pre-allocate sparsity assuming dense element matrices
a->UsePrecomputedSparsity();
HPCBilinearForm *a_hpc = NULL;
Operator *a_oper = NULL;
if (!perf)
{
// Standard assembly using a diffusion domain integrator
a->AddDomainIntegrator(new DiffusionIntegrator(one));
a->Assemble();
}
else
{
// High-performance assembly/evaluation using the templated operator type
a_hpc = new HPCBilinearForm(integ_t(coeff_t(1.0)), *fespace);
if (matrix_free)
{
a_hpc->Assemble(); // partial assembly
}
else
{
a_hpc->AssembleBilinearForm(*a); // full matrix assembly
}
}
tic_toc.Stop();
cout << " done, " << tic_toc.RealTime() << "s." << endl;
// 12. Solve the system A X = B with CG. In the standard case, use a simple
// symmetric Gauss-Seidel preconditioner.
// Setup the operator matrix (if applicable)
SparseMatrix A;
Vector B, X;
if (perf && matrix_free)
{
a_hpc->FormLinearSystem(ess_tdof_list, x, *b, a_oper, X, B);
cout << "Size of linear system: " << a_hpc->Height() << endl;
}
else
{
a->FormLinearSystem(ess_tdof_list, x, *b, A, X, B);
cout << "Size of linear system: " << A.Height() << endl;
a_oper = &A;
}
// Setup the matrix used for preconditioning
cout << "Assembling the preconditioning matrix ..." << flush;
tic_toc.Clear();
tic_toc.Start();
SparseMatrix A_pc;
if (pc_choice == PCType::LOR)
{
// TODO: assemble the LOR matrix using the performance code
a_pc->AddDomainIntegrator(new DiffusionIntegrator(one));
a_pc->UsePrecomputedSparsity();
a_pc->Assemble();
a_pc->FormSystemMatrix(ess_tdof_list, A_pc);
}
else if (pc_choice == PCType::HO)
{
if (!matrix_free)
{
A_pc.MakeRef(A); // matrix already assembled, reuse it
}
else
{
a_pc->UsePrecomputedSparsity();
a_hpc->AssembleBilinearForm(*a_pc);
a_pc->FormSystemMatrix(ess_tdof_list, A_pc);
}
}
tic_toc.Stop();
cout << " done, " << tic_toc.RealTime() << "s." << endl;
// Solve with CG or PCG, depending if the matrix A_pc is available
if (pc_choice != PCType::NONE)
{
GSSmoother M(A_pc);
PCG(*a_oper, M, B, X, 1, 500, 1e-12, 0.0);
}
else
{
CG(*a_oper, B, X, 1, 500, 1e-12, 0.0);
}
// 13. Recover the solution as a finite element grid function.
if (perf && matrix_free)
{
a_hpc->RecoverFEMSolution(X, *b, x);
}
else
{
a->RecoverFEMSolution(X, *b, x);
}
// 14. Save the refined mesh and the solution. This output can be viewed later
// using GLVis: "glvis -m refined.mesh -g sol.gf".
ofstream mesh_ofs("refined.mesh");
mesh_ofs.precision(8);
mesh->Print(mesh_ofs);
ofstream sol_ofs("sol.gf");
sol_ofs.precision(8);
x.Save(sol_ofs);
// 15. Send the solution by socket to a GLVis server.
if (visualization)
{
char vishost[] = "localhost";
socketstream sol_sock(vishost, visport);
sol_sock.precision(8);
sol_sock << "solution\n" << *mesh << x << flush;
}
// 16. Free the used memory.
delete a;
delete a_hpc;
if (a_oper != &A) { delete a_oper; }
delete a_pc;
delete b;
delete fespace;
delete fespace_lor;
delete fec_lor;
if (order > 0) { delete fec; }
delete mesh;
return 0;
}