MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
nonlinearform.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 #ifndef MFEM_NONLINEARFORM
13 #define MFEM_NONLINEARFORM
14 
15 #include "../config/config.hpp"
16 #include "nonlininteg.hpp"
17 #include "nonlinearform_ext.hpp"
18 #include "bilinearform.hpp"
19 #include "gridfunc.hpp"
20 
21 namespace mfem
22 {
23 
24 class NonlinearForm : public Operator
25 {
26 protected:
27  /// The assembly level.
29 
30  /** Extension for supporting Partial Assembly (PA) or
31  Matrix Free assembly (MF). */
33 
34  /// FE space on which the form lives.
35  FiniteElementSpace *fes; // not owned
36 
37  /// Set of Domain Integrators to be assembled (added).
39 
40  /// Set of interior face Integrators to be assembled (added).
42 
43  /// Set of boundary face Integrators to be assembled (added).
46 
47  mutable SparseMatrix *Grad, *cGrad; // owned
48 
49  /// A list of all essential true dofs
51 
52  /// Counter for updates propagated from the FiniteElementSpace.
53  long sequence;
54 
55  /// Auxiliary Vector%s
56  mutable Vector aux1, aux2;
57 
58  /// Pointer to the prolongation matrix of fes, may be NULL.
59  const Operator *P; // not owned
60  /// The result of dynamic-casting P to SparseMatrix pointer.
61  const SparseMatrix *cP; // not owned
62 
63  bool Serial() const { return (!P || cP); }
64  const Vector &Prolongate(const Vector &x) const;
65 
66 public:
67  /// Construct a NonlinearForm on the given FiniteElementSpace, @a f.
68  /** As an Operator, the NonlinearForm has input and output size equal to the
69  number of true degrees of freedom, i.e. f->GetTrueVSize(). */
71  : Operator(f->GetTrueVSize()), assembly(AssemblyLevel::NONE),
72  ext(NULL), fes(f), Grad(NULL), cGrad(NULL),
73  sequence(f->GetSequence()), P(f->GetProlongationMatrix()),
74  cP(dynamic_cast<const SparseMatrix*>(P))
75  { }
76 
77  /// Set the desired assembly level. The default is AssemblyLevel::NONE.
78  /** This method must be called before assembly. */
79  void SetAssemblyLevel(AssemblyLevel assembly_level);
80 
82  const FiniteElementSpace *FESpace() const { return fes; }
83 
84  /// Adds new Domain Integrator.
86  { dnfi.Append(nlfi); }
87 
88  /// Access all integrators added with AddDomainIntegrator().
90 
91  /// Adds new Interior Face Integrator.
93  { fnfi.Append(nlfi); }
94 
95  /// Adds new Boundary Face Integrator.
97  { bfnfi.Append(nlfi); bfnfi_marker.Append(NULL); }
98 
99  /** @brief Adds new Boundary Face Integrator, restricted to specific boundary
100  attributes. */
102  Array<int> &bdr_marker)
103  { bfnfi.Append(nfi); bfnfi_marker.Append(&bdr_marker); }
104 
105  /// Specify essential boundary conditions.
106  /** This method calls FiniteElementSpace::GetEssentialTrueDofs() and stores
107  the result internally for use by other methods. If the @a rhs pointer is
108  not NULL, its essential true dofs will be set to zero. This makes it
109  "compatible" with the output vectors from the Mult() method which also
110  have zero entries at the essential true dofs. */
111  void SetEssentialBC(const Array<int> &bdr_attr_is_ess, Vector *rhs = NULL);
112 
113  /// (DEPRECATED) Specify essential boundary conditions.
114  /** @deprecated Use either SetEssentialBC() or SetEssentialTrueDofs(). */
115  void SetEssentialVDofs(const Array<int> &ess_vdofs_list);
116 
117  /// Specify essential boundary conditions.
119  { ess_tdof_list.Copy(this->ess_tdof_list); }
120 
121  /// Return a (read-only) list of all essential true dofs.
122  const Array<int> &GetEssentialTrueDofs() const { return ess_tdof_list; }
123 
124  /// Compute the enery corresponding to the state @a x.
125  /** In general, @a x may have non-homogeneous essential boundary values.
126 
127  The state @a x must be a "GridFunction size" vector, i.e. its size must
128  be fes->GetVSize(). */
129  double GetGridFunctionEnergy(const Vector &x) const;
130 
131  /// Compute the enery corresponding to the state @a x.
132  /** In general, @a x may have non-homogeneous essential boundary values.
133 
134  The state @a x must be a true-dof vector. */
135  virtual double GetEnergy(const Vector &x) const
136  { return GetGridFunctionEnergy(Prolongate(x)); }
137 
138  /// Evaluate the action of the NonlinearForm.
139  /** The input essential dofs in @a x will, generally, be non-zero. However,
140  the output essential dofs in @a y will always be set to zero.
141 
142  Both the input and the output vectors, @a x and @a y, must be true-dof
143  vectors, i.e. their size must be fes->GetTrueVSize(). */
144  virtual void Mult(const Vector &x, Vector &y) const;
145 
146  /** @brief Compute the gradient Operator of the NonlinearForm corresponding
147  to the state @a x. */
148  /** Any previously specified essential boundary conditions will be
149  automatically imposed on the gradient operator.
150 
151  The returned object is valid until the next call to this method or the
152  destruction of this object.
153 
154  In general, @a x may have non-homogeneous essential boundary values.
155 
156  The state @a x must be a true-dof vector. */
157  virtual Operator &GetGradient(const Vector &x) const;
158 
159  /// Update the NonlinearForm to propagate updates of the associated FE space.
160  /** After calling this method, the essential boundary conditions need to be
161  set again. */
162  virtual void Update();
163 
164  /// Setup the NonlinearForm
165  virtual void Setup();
166 
167  /// Get the finite element space prolongation matrix
168  virtual const Operator *GetProlongation() const { return P; }
169  /// Get the finite element space restriction matrix
170  virtual const Operator *GetRestriction() const
171  { return fes->GetRestrictionMatrix(); }
172 
173  /** @brief Destroy the NoninearForm including the owned
174  NonlinearFormIntegrator%s and gradient Operator. */
175  virtual ~NonlinearForm();
176 };
177 
178 
179 /** @brief A class representing a general block nonlinear operator defined on
180  the Cartesian product of multiple FiniteElementSpace%s. */
182 {
183 protected:
184  /// FE spaces on which the form lives.
186 
187  /// Set of Domain Integrators to be assembled (added).
189 
190  /// Set of interior face Integrators to be assembled (added).
192 
193  /// Set of Boundary Face Integrators to be assembled (added).
196 
197  /** Auxiliary block-vectors for wrapping input and output vectors or holding
198  GridFunction-like block-vector data (e.g. in parallel). */
199  mutable BlockVector xs, ys;
200 
203 
204  // A list of the offsets
207 
208  // Essential vdofs: one list of vdofs for each space in 'fes'
210 
211  /// Specialized version of GetEnergy() for BlockVectors
212  double GetEnergyBlocked(const BlockVector &bx) const;
213 
214  /// Specialized version of Mult() for BlockVector%s
215  void MultBlocked(const BlockVector &bx, BlockVector &by) const;
216 
217  /// Specialized version of GetGradient() for BlockVector
218  Operator &GetGradientBlocked(const BlockVector &bx) const;
219 
220 public:
221  /// Construct an empty BlockNonlinearForm. Initialize with SetSpaces().
223 
224  /// Construct a BlockNonlinearForm on the given set of FiniteElementSpace%s.
226 
227  /// Return the @a k-th FE space of the BlockNonlinearForm.
228  FiniteElementSpace *FESpace(int k) { return fes[k]; }
229  /// Return the @a k-th FE space of the BlockNonlinearForm (const version).
230  const FiniteElementSpace *FESpace(int k) const { return fes[k]; }
231 
232  /// (Re)initialize the BlockNonlinearForm.
233  /** After a call to SetSpaces(), the essential b.c. must be set again. */
235 
236  /// Return the regular dof offsets.
237  const Array<int> &GetBlockOffsets() const { return block_offsets; }
238  /// Return the true-dof offsets.
240 
241  /// Adds new Domain Integrator.
243  { dnfi.Append(nlfi); }
244 
245  /// Adds new Interior Face Integrator.
247  { fnfi.Append(nlfi); }
248 
249  /// Adds new Boundary Face Integrator.
251  { bfnfi.Append(nlfi); bfnfi_marker.Append(NULL); }
252 
253  /** @brief Adds new Boundary Face Integrator, restricted to specific boundary
254  attributes. */
256  Array<int> &bdr_marker);
257 
258  virtual void SetEssentialBC(const Array<Array<int> *>&bdr_attr_is_ess,
259  Array<Vector *> &rhs);
260 
261  virtual double GetEnergy(const Vector &x) const;
262 
263  virtual void Mult(const Vector &x, Vector &y) const;
264 
265  virtual Operator &GetGradient(const Vector &x) const;
266 
267  /// Destructor.
268  virtual ~BlockNonlinearForm();
269 };
270 
271 
272 }
273 
274 #endif
virtual const Operator * GetProlongation() const
Get the finite element space prolongation matrix.
BlockNonlinearForm()
Construct an empty BlockNonlinearForm. Initialize with SetSpaces().
Array< NonlinearFormIntegrator * > dnfi
Set of Domain Integrators to be assembled (added).
const FiniteElementSpace * FESpace(int k) const
Return the k-th FE space of the BlockNonlinearForm (const version).
Array< BlockNonlinearFormIntegrator * > dnfi
Set of Domain Integrators to be assembled (added).
A class to handle Vectors in a block fashion.
Definition: blockvector.hpp:30
const Array< int > & GetBlockOffsets() const
Return the regular dof offsets.
void AddDomainIntegrator(BlockNonlinearFormIntegrator *nlfi)
Adds new Domain Integrator.
SparseMatrix * cGrad
virtual void SetEssentialBC(const Array< Array< int > * > &bdr_attr_is_ess, Array< Vector * > &rhs)
virtual void Setup()
Setup the NonlinearForm.
const FiniteElementSpace * FESpace() const
void AddDomainIntegrator(NonlinearFormIntegrator *nlfi)
Adds new Domain Integrator.
void Copy(Array &copy) const
Create a copy of the current array.
Definition: array.hpp:812
void AddBdrFaceIntegrator(NonlinearFormIntegrator *nfi, Array< int > &bdr_marker)
Adds new Boundary Face Integrator, restricted to specific boundary attributes.
A class representing a general block nonlinear operator defined on the Cartesian product of multiple ...
Array< NonlinearFormIntegrator * > bfnfi
Set of boundary face Integrators to be assembled (added).
Array< BlockNonlinearFormIntegrator * > bfnfi
Set of Boundary Face Integrators to be assembled (added).
Array< int > ess_tdof_list
A list of all essential true dofs.
FiniteElementSpace * fes
FE space on which the form lives.
void AddInteriorFaceIntegrator(NonlinearFormIntegrator *nlfi)
Adds new Interior Face Integrator.
void AddBdrFaceIntegrator(NonlinearFormIntegrator *nlfi)
Adds new Boundary Face Integrator.
virtual const Operator * GetRestriction() const
Get the finite element space restriction matrix.
Array2D< SparseMatrix * > Grads
void SetSpaces(Array< FiniteElementSpace * > &f)
(Re)initialize the BlockNonlinearForm.
virtual double GetEnergy(const Vector &x) const
Compute the enery corresponding to the state x.
virtual double GetEnergy(const Vector &x) const
Array< Array< int > * > ess_vdofs
void AddBdrFaceIntegrator(BlockNonlinearFormIntegrator *nlfi)
Adds new Boundary Face Integrator.
const Array< int > & GetBlockTrueOffsets() const
Return the true-dof offsets.
Array< FiniteElementSpace * > fes
FE spaces on which the form lives.
Data type sparse matrix.
Definition: sparsemat.hpp:40
const Array< int > & GetEssentialTrueDofs() const
Return a (read-only) list of all essential true dofs.
void AddInteriorFaceIntegrator(BlockNonlinearFormIntegrator *nlfi)
Adds new Interior Face Integrator.
Array< NonlinearFormIntegrator * > * GetDNFI()
Access all integrators added with AddDomainIntegrator().
Array< NonlinearFormIntegrator * > fnfi
Set of interior face Integrators to be assembled (added).
NonlinearForm(FiniteElementSpace *f)
Construct a NonlinearForm on the given FiniteElementSpace, f.
const SparseMatrix * cP
The result of dynamic-casting P to SparseMatrix pointer.
Vector aux1
Auxiliary Vectors.
Array< BlockNonlinearFormIntegrator * > fnfi
Set of interior face Integrators to be assembled (added).
AssemblyLevel assembly
The assembly level.
const Operator * P
Pointer to the prolongation matrix of fes, may be NULL.
Array< Array< int > * > bfnfi_marker
Array< Array< int > * > bfnfi_marker
void SetAssemblyLevel(AssemblyLevel assembly_level)
Set the desired assembly level. The default is AssemblyLevel::NONE.
Dynamic 2D array using row-major layout.
Definition: array.hpp:316
FiniteElementSpace * FESpace()
bool Serial() const
virtual void Mult(const Vector &x, Vector &y) const
Evaluate the action of the NonlinearForm.
virtual void Update()
Update the NonlinearForm to propagate updates of the associated FE space.
void SetEssentialTrueDofs(const Array< int > &ess_tdof_list)
Specify essential boundary conditions.
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:87
NonlinearFormExtension * ext
virtual ~BlockNonlinearForm()
Destructor.
virtual ~NonlinearForm()
Destroy the NoninearForm including the owned NonlinearFormIntegrators and gradient Operator...
double GetGridFunctionEnergy(const Vector &x) const
Compute the enery corresponding to the state x.
virtual void Mult(const Vector &x, Vector &y) const
Operator application: y=A(x).
Operator & GetGradientBlocked(const BlockVector &bx) const
Specialized version of GetGradient() for BlockVector.
virtual Operator & GetGradient(const Vector &x) const
Compute the gradient Operator of the NonlinearForm corresponding to the state x.
double GetEnergyBlocked(const BlockVector &bx) const
Specialized version of GetEnergy() for BlockVectors.
long sequence
Counter for updates propagated from the FiniteElementSpace.
void SetEssentialBC(const Array< int > &bdr_attr_is_ess, Vector *rhs=NULL)
Specify essential boundary conditions.
Vector data type.
Definition: vector.hpp:48
SparseMatrix * Grad
virtual Operator & GetGradient(const Vector &x) const
Evaluate the gradient operator at the point x. The default behavior in class Operator is to generate ...
void SetEssentialVDofs(const Array< int > &ess_vdofs_list)
(DEPRECATED) Specify essential boundary conditions.
Abstract operator.
Definition: operator.hpp:24
virtual const SparseMatrix * GetRestrictionMatrix() const
The returned SparseMatrix is owned by the FiniteElementSpace.
Definition: fespace.hpp:315
A class to handle Block systems in a matrix-free implementation.
FiniteElementSpace * FESpace(int k)
Return the k-th FE space of the BlockNonlinearForm.
const Vector & Prolongate(const Vector &x) const
void MultBlocked(const BlockVector &bx, BlockVector &by) const
Specialized version of Mult() for BlockVectors.