MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
lor_solvers.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2021, Lawrence Livermore National Security, LLC. Produced
2 // at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details.
11 //
12 // ---------------------------------
13 // Low-Order Refined Solvers Miniapp
14 // ---------------------------------
15 //
16 // This miniapp illustrates the use of low-order refined preconditioners for
17 // finite element problems defined using H1, H(curl), H(div), or L2 finite
18 // element spaces. The following problems are solved, depending on the chosen
19 // finite element space:
20 //
21 // H1 and L2: definite Helmholtz problem, u - Delta u = f
22 // (in L2 discretized using the symmetric interior penalty DG method)
23 //
24 // H(curl): definite Maxwell problem, u + curl curl u = f
25 //
26 // H(div): grad-div problem, u - grad(div u) = f
27 //
28 // In each case, the high-order finite element problem is preconditioned using a
29 // low-order finite element discretization defined on a Gauss-Lobatto refined
30 // mesh. The low-order problem is solved using a direct solver if MFEM is
31 // compiled with SuiteSparse enabled, or with one iteration of symmetric
32 // Gauss-Seidel smoothing otherwise.
33 //
34 // For vector finite element spaces, the special "Integrated" basis type is used
35 // to obtain spectral equivalence between the high-order and low-order refined
36 // discretizations. This basis is defined in reference [1] and spectral
37 // equivalence is shown in [2]:
38 //
39 // [1]. M. Gerritsma. Edge functions for spectral element methods. Spectral and
40 // High Order Methods for Partial Differential Equations. (2010)
41 // [2]. C. Dohrmann. Spectral equivalence properties of higher-order tensor
42 // product finite elements and applications to preconditioning. (2021)
43 //
44 // The action of the high-order operator is computed using MFEM's partial
45 // assembly/matrix-free algorithms (except in the case of L2, which remains
46 // future work).
47 //
48 // Compile with: make lor_solvers
49 //
50 // Sample runs:
51 //
52 // lor_solvers -fe h
53 // lor_solvers -fe n
54 // lor_solvers -fe r
55 // lor_solvers -fe l
56 // lor_solvers -m ../../data/amr-quad.mesh -fe h
57 // lor_solvers -m ../../data/amr-quad.mesh -fe n
58 // lor_solvers -m ../../data/amr-quad.mesh -fe r
59 // lor_solvers -m ../../data/amr-quad.mesh -fe l
60 
61 #include "mfem.hpp"
62 #include <fstream>
63 #include <iostream>
64 #include <memory>
65 
66 #include "lor_mms.hpp"
67 
68 using namespace std;
69 using namespace mfem;
70 
71 bool grad_div_problem = false;
72 
73 int main(int argc, char *argv[])
74 {
75  const char *mesh_file = "../../data/star.mesh";
76  int ref_levels = 1;
77  int order = 3;
78  const char *fe = "h";
79  bool visualization = true;
80 
81  OptionsParser args(argc, argv);
82  args.AddOption(&mesh_file, "-m", "--mesh", "Mesh file to use.");
83  args.AddOption(&ref_levels, "-r", "--refine",
84  "Number of times to refine the mesh uniformly.");
85  args.AddOption(&order, "-o", "--order", "Polynomial degree.");
86  args.AddOption(&fe, "-fe", "--fe-type",
87  "FE type. h for H1, n for Hcurl, r for Hdiv, l for L2");
88  args.AddOption(&visualization, "-vis", "--visualization", "-no-vis",
89  "--no-visualization",
90  "Enable or disable GLVis visualization.");
91  args.ParseCheck();
92 
93  bool H1 = false, ND = false, RT = false, L2 = false;
94  if (string(fe) == "h") { H1 = true; }
95  else if (string(fe) == "n") { ND = true; }
96  else if (string(fe) == "r") { RT = true; }
97  else if (string(fe) == "l") { L2 = true; }
98  else { MFEM_ABORT("Bad FE type. Must be 'h', 'n', 'r', or 'l'."); }
99 
100  if (RT) { grad_div_problem = true; }
101  double kappa = (order+1)*(order+1); // Penalty used for DG discretizations
102 
103  Mesh mesh(mesh_file, 1, 1);
104  int dim = mesh.Dimension();
105  MFEM_VERIFY(dim == 2 || dim == 3, "Spatial dimension must be 2 or 3.");
106  for (int l = 0; l < ref_levels; l++) { mesh.UniformRefinement(); }
107 
108  FunctionCoefficient f_coeff(f), u_coeff(u);
109  VectorFunctionCoefficient f_vec_coeff(dim, f_vec), u_vec_coeff(dim, u_vec);
110 
111  int b1 = BasisType::GaussLobatto, b2 = BasisType::IntegratedGLL;
112  unique_ptr<FiniteElementCollection> fec;
113  if (H1) { fec.reset(new H1_FECollection(order, dim, b1)); }
114  else if (ND) { fec.reset(new ND_FECollection(order, dim, b1, b2)); }
115  else if (RT) { fec.reset(new RT_FECollection(order-1, dim, b1, b2)); }
116  else { fec.reset(new L2_FECollection(order, dim, b1)); }
117 
118  FiniteElementSpace fes(&mesh, fec.get());
119  cout << "Number of DOFs: " << fes.GetTrueVSize() << endl;
120 
121  Array<int> ess_dofs;
122  // In DG, boundary conditions are enforced weakly, so no essential DOFs.
123  if (!L2) { fes.GetBoundaryTrueDofs(ess_dofs); }
124 
125  BilinearForm a(&fes);
126  if (H1 || L2)
127  {
130  }
131  else
132  {
134  }
135 
136  if (ND) { a.AddDomainIntegrator(new CurlCurlIntegrator); }
137  else if (RT) { a.AddDomainIntegrator(new DivDivIntegrator); }
138  else if (L2)
139  {
141  a.AddBdrFaceIntegrator(new DGDiffusionIntegrator(-1.0, kappa));
142  }
143  // TODO: L2 diffusion not implemented with partial assembly
144  if (!L2) { a.SetAssemblyLevel(AssemblyLevel::PARTIAL); }
145  a.Assemble();
146 
147  LinearForm b(&fes);
148  if (H1 || L2) { b.AddDomainIntegrator(new DomainLFIntegrator(f_coeff)); }
149  else { b.AddDomainIntegrator(new VectorFEDomainLFIntegrator(f_vec_coeff)); }
150  if (L2)
151  {
152  // DG boundary conditions are enforced weakly with this integrator.
153  b.AddBdrFaceIntegrator(new DGDirichletLFIntegrator(u_coeff, -1.0, kappa));
154  }
155  b.Assemble();
156 
157  GridFunction x(&fes);
158  if (H1 || L2) { x.ProjectCoefficient(u_coeff);}
159  else { x.ProjectCoefficient(u_vec_coeff); }
160 
161  Vector X, B;
162  OperatorHandle A;
163  a.FormLinearSystem(ess_dofs, x, b, A, X, B);
164 
165 #ifdef MFEM_USE_SUITESPARSE
166  LORSolver<UMFPackSolver> solv_lor(a, ess_dofs);
167 #else
168  LORSolver<GSSmoother> solv_lor(a, ess_dofs);
169 #endif
170 
171  CGSolver cg;
172  cg.SetAbsTol(0.0);
173  cg.SetRelTol(1e-12);
174  cg.SetMaxIter(500);
175  cg.SetPrintLevel(1);
176  cg.SetOperator(*A);
177  cg.SetPreconditioner(solv_lor);
178  cg.Mult(B, X);
179 
180  a.RecoverFEMSolution(X, b, x);
181 
182  double er =
183  (H1 || L2) ? x.ComputeL2Error(u_coeff) : x.ComputeL2Error(u_vec_coeff);
184  cout << "L2 error: " << er << endl;
185 
186  if (visualization)
187  {
188  // Save the solution and mesh to disk. The output can be viewed using
189  // GLVis as follows: "glvis -m mesh.mesh -g sol.gf"
190  x.Save("sol.gf");
191  mesh.Save("mesh.mesh");
192 
193  // Also save the solution for visualization using ParaView
194  ParaViewDataCollection dc("LOR", &mesh);
195  dc.SetPrefixPath("ParaView");
196  dc.SetHighOrderOutput(true);
197  dc.SetLevelsOfDetail(order);
198  dc.RegisterField("u", &x);
199  dc.SetCycle(0);
200  dc.SetTime(0.0);
201  dc.Save();
202  }
203 
204  return 0;
205 }
Class for domain integration L(v) := (f, v)
Definition: lininteg.hpp:97
virtual void Save(const char *fname, int precision=16) const
Definition: mesh.cpp:9615
Conjugate gradient method.
Definition: solvers.hpp:316
Class for grid function - Vector with associated FE space.
Definition: gridfunc.hpp:30
void SetCycle(int c)
Set time cycle (for time-dependent simulations)
void Assemble(int skip_zeros=1)
Assembles the form i.e. sums over all domain/bdr integrators.
virtual double ComputeL2Error(Coefficient &exsol, const IntegrationRule *irs[]=NULL) const
Definition: gridfunc.hpp:456
virtual void Mult(const Vector &b, Vector &x) const
Operator application: y=A(x).
Definition: solvers.cpp:616
Helper class for ParaView visualization data.
Integrator for (curl u, curl v) for Nedelec elements.
void Assemble()
Assembles the linear form i.e. sums over all domain/bdr integrators.
Definition: linearform.cpp:102
Pointer to an Operator of a specified type.
Definition: handle.hpp:33
virtual void FormLinearSystem(const Array< int > &ess_tdof_list, Vector &x, Vector &b, OperatorHandle &A, Vector &X, Vector &B, int copy_interior=0)
Form the linear system A X = B, corresponding to this bilinear form and the linear form b(...
virtual void RecoverFEMSolution(const Vector &X, const Vector &b, Vector &x)
Recover the solution of a linear system formed with FormLinearSystem().
void SetAssemblyLevel(AssemblyLevel assembly_level)
Set the desired assembly level.
double kappa
Definition: ex24.cpp:54
(Q div u, div v) for RT elements
bool grad_div_problem
Definition: lor_solvers.cpp:71
virtual void Save(std::ostream &out) const
Save the GridFunction to an output stream.
Definition: gridfunc.cpp:3484
virtual void RegisterField(const std::string &field_name, GridFunction *gf)
Add a grid function to the collection.
void SetPrintLevel(int print_lvl)
Definition: solvers.cpp:71
double b
Definition: lissajous.cpp:42
void UniformRefinement(int i, const DSTable &, int *, int *, int *)
Definition: mesh.cpp:9143
void SetMaxIter(int max_it)
Definition: solvers.hpp:100
void u_vec(const Vector &xvec, Vector &u)
Definition: lor_mms.hpp:50
void SetHighOrderOutput(bool high_order_output_)
void AddBdrFaceIntegrator(LinearFormIntegrator *lfi)
Adds new Boundary Face Integrator. Assumes ownership of lfi.
Definition: linearform.cpp:83
virtual int GetTrueVSize() const
Return the number of vector true (conforming) dofs.
Definition: fespace.hpp:543
int Dimension() const
Definition: mesh.hpp:911
void SetTime(double t)
Set physical time (for time-dependent simulations)
Arbitrary order H(div)-conforming Raviart-Thomas finite elements.
Definition: fe_coll.hpp:341
A general vector function coefficient.
void AddDomainIntegrator(LinearFormIntegrator *lfi)
Adds new Domain Integrator. Assumes ownership of lfi.
Definition: linearform.cpp:39
void SetAbsTol(double atol)
Definition: solvers.hpp:99
void SetRelTol(double rtol)
Definition: solvers.hpp:98
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:87
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set &#39;var&#39; to receive the value. Enable/disable tags are used to set the bool...
Definition: optparser.hpp:82
double a
Definition: lissajous.cpp:41
A &quot;square matrix&quot; operator for the associated FE space and BLFIntegrators The sum of all the BLFInteg...
int dim
Definition: ex24.cpp:53
void AddInteriorFaceIntegrator(BilinearFormIntegrator *bfi)
Adds new interior Face Integrator. Assumes ownership of bfi.
void AddDomainIntegrator(BilinearFormIntegrator *bfi)
Adds new Domain Integrator. Assumes ownership of bfi.
virtual void ProjectCoefficient(Coefficient &coeff)
Project coeff Coefficient to this GridFunction. The projection computation depends on the choice of t...
Definition: gridfunc.cpp:2278
void SetLevelsOfDetail(int levels_of_detail_)
for VectorFiniteElements (Nedelec, Raviart-Thomas)
Definition: lininteg.hpp:266
virtual void SetOperator(const Operator &op)
Also calls SetOperator for the preconditioner if there is one.
Definition: solvers.hpp:330
A general function coefficient.
Arbitrary order H(curl)-conforming Nedelec finite elements.
Definition: fe_coll.hpp:416
Vector data type.
Definition: vector.hpp:60
void f_vec(const Vector &xvec, Vector &f)
Definition: lor_mms.hpp:68
virtual void SetPreconditioner(Solver &pr)
This should be called before SetOperator.
Definition: solvers.cpp:92
Arbitrary order H1-conforming (continuous) finite elements.
Definition: fe_coll.hpp:216
Vector with associated FE space and LinearFormIntegrators.
Definition: linearform.hpp:23
virtual void Save() override
double u(const Vector &xvec)
Definition: lor_mms.hpp:24
int main()
void ParseCheck(std::ostream &out=mfem::out)
Definition: optparser.cpp:251
void SetPrefixPath(const std::string &prefix)
Set the path where the DataCollection will be saved.
Represents a solver of type SolverType created using the low-order refined version of the given Bilin...
Definition: lor.hpp:190
void AddBdrFaceIntegrator(BilinearFormIntegrator *bfi)
Adds new boundary Face Integrator. Assumes ownership of bfi.
Arbitrary order &quot;L2-conforming&quot; discontinuous finite elements.
Definition: fe_coll.hpp:285
double f(const Vector &p)