MFEM  v4.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, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_NONLINEARFORM
13 #define MFEM_NONLINEARFORM
14 
15 #include "../config/config.hpp"
16 #include "nonlininteg.hpp"
17 #include "gridfunc.hpp"
18 
19 namespace mfem
20 {
21 
22 class NonlinearForm : public Operator
23 {
24 protected:
25  /// FE space on which the form lives.
26  FiniteElementSpace *fes; // not owned
27 
28  /// Set of Domain Integrators to be assembled (added).
30 
31  /// Set of interior face Integrators to be assembled (added).
33 
34  /// Set of boundary face Integrators to be assembled (added).
37 
38  mutable SparseMatrix *Grad, *cGrad; // owned
39 
40  /// A list of all essential true dofs
42 
43  /// Counter for updates propagated from the FiniteElementSpace.
44  long sequence;
45 
46  /// Auxiliary Vector%s
47  mutable Vector aux1, aux2;
48 
49  /// Pointer to the prolongation matrix of fes, may be NULL.
50  const Operator *P; // not owned
51  /// The result of dynamic-casting P to SparseMatrix pointer.
52  const SparseMatrix *cP; // not owned
53 
54  bool Serial() const { return (!P || cP); }
55  const Vector &Prolongate(const Vector &x) const;
56 
57 public:
58  /// Construct a NonlinearForm on the given FiniteElementSpace, @a f.
59  /** As an Operator, the NonlinearForm has input and output size equal to the
60  number of true degrees of freedom, i.e. f->GetTrueVSize(). */
62  : Operator(f->GetTrueVSize()), fes(f), Grad(NULL), cGrad(NULL),
63  sequence(f->GetSequence()), P(f->GetProlongationMatrix()),
64  cP(dynamic_cast<const SparseMatrix*>(P))
65  { }
66 
68  const FiniteElementSpace *FESpace() const { return fes; }
69 
70  /// Adds new Domain Integrator.
72  { dnfi.Append(nlfi); }
73 
74  /// Adds new Interior Face Integrator.
76  { fnfi.Append(nlfi); }
77 
78  /// Adds new Boundary Face Integrator.
80  { bfnfi.Append(nlfi); bfnfi_marker.Append(NULL); }
81 
82  /** @brief Adds new Boundary Face Integrator, restricted to specific boundary
83  attributes. */
85  Array<int> &bdr_marker)
86  { bfnfi.Append(nfi); bfnfi_marker.Append(&bdr_marker); }
87 
88  /// Specify essential boundary conditions.
89  /** This method calls FiniteElementSpace::GetEssentialTrueDofs() and stores
90  the result internally for use by other methods. If the @a rhs pointer is
91  not NULL, its essential true dofs will be set to zero. This makes it
92  "compatible" with the output vectors from the Mult() method which also
93  have zero entries at the essential true dofs. */
94  void SetEssentialBC(const Array<int> &bdr_attr_is_ess, Vector *rhs = NULL);
95 
96  /// (DEPRECATED) Specify essential boundary conditions.
97  /** @deprecated Use either SetEssentialBC() or SetEssentialTrueDofs(). */
98  void SetEssentialVDofs(const Array<int> &ess_vdofs_list);
99 
100  /// Specify essential boundary conditions.
102  { ess_tdof_list.Copy(this->ess_tdof_list); }
103 
104  /// Return a (read-only) list of all essential true dofs.
105  const Array<int> &GetEssentialTrueDofs() const { return ess_tdof_list; }
106 
107  /// Compute the enery corresponding to the state @a x.
108  /** In general, @a x may have non-homogeneous essential boundary values.
109 
110  The state @a x must be a "GridFunction size" vector, i.e. its size must
111  be fes->GetVSize(). */
112  double GetGridFunctionEnergy(const Vector &x) const;
113 
114  /// Compute the enery corresponding to the state @a x.
115  /** In general, @a x may have non-homogeneous essential boundary values.
116 
117  The state @a x must be a true-dof vector. */
118  virtual double GetEnergy(const Vector &x) const
119  { return GetGridFunctionEnergy(Prolongate(x)); }
120 
121  /// Evaluate the action of the NonlinearForm.
122  /** The input essential dofs in @a x will, generally, be non-zero. However,
123  the output essential dofs in @a y will always be set to zero.
124 
125  Both the input and the output vectors, @a x and @a y, must be true-dof
126  vectors, i.e. their size must be fes->GetTrueVSize(). */
127  virtual void Mult(const Vector &x, Vector &y) const;
128 
129  /** @brief Compute the gradient Operator of the NonlinearForm corresponding
130  to the state @a x. */
131  /** Any previously specified essential boundary conditions will be
132  automatically imposed on the gradient operator.
133 
134  The returned object is valid until the next call to this method or the
135  destruction of this object.
136 
137  In general, @a x may have non-homogeneous essential boundary values.
138 
139  The state @a x must be a true-dof vector. */
140  virtual Operator &GetGradient(const Vector &x) const;
141 
142  /// Update the NonlinearForm to propagate updates of the associated FE space.
143  /** After calling this method, the essential boundary conditions need to be
144  set again. */
145  virtual void Update();
146 
147  /// Get the finite element space prolongation matrix
148  virtual const Operator *GetProlongation() const { return P; }
149  /// Get the finite element space restriction matrix
150  virtual const Operator *GetRestriction() const
151  { return fes->GetRestrictionMatrix(); }
152 
153  /** @brief Destroy the NoninearForm including the owned
154  NonlinearFormIntegrator%s and gradient Operator. */
155  virtual ~NonlinearForm();
156 };
157 
158 
159 /** @brief A class representing a general block nonlinear operator defined on
160  the Cartesian product of multiple FiniteElementSpace%s. */
162 {
163 protected:
164  /// FE spaces on which the form lives.
166 
167  /// Set of Domain Integrators to be assembled (added).
169 
170  /// Set of interior face Integrators to be assembled (added).
172 
173  /// Set of Boundary Face Integrators to be assembled (added).
176 
177  /** Auxiliary block-vectors for wrapping input and output vectors or holding
178  GridFunction-like block-vector data (e.g. in parallel). */
179  mutable BlockVector xs, ys;
180 
183 
184  // A list of the offsets
187 
188  // Essential vdofs: one list of vdofs for each space in 'fes'
190 
191  /// Specialized version of GetEnergy() for BlockVectors
192  double GetEnergyBlocked(const BlockVector &bx) const;
193 
194  /// Specialized version of Mult() for BlockVector%s
195  void MultBlocked(const BlockVector &bx, BlockVector &by) const;
196 
197  /// Specialized version of GetGradient() for BlockVector
198  Operator &GetGradientBlocked(const BlockVector &bx) const;
199 
200 public:
201  /// Construct an empty BlockNonlinearForm. Initialize with SetSpaces().
203 
204  /// Construct a BlockNonlinearForm on the given set of FiniteElementSpace%s.
206 
207  /// Return the @a k-th FE space of the BlockNonlinearForm.
208  FiniteElementSpace *FESpace(int k) { return fes[k]; }
209  /// Return the @a k-th FE space of the BlockNonlinearForm (const version).
210  const FiniteElementSpace *FESpace(int k) const { return fes[k]; }
211 
212  /// (Re)initialize the BlockNonlinearForm.
213  /** After a call to SetSpaces(), the essential b.c. must be set again. */
215 
216  /// Return the regular dof offsets.
217  const Array<int> &GetBlockOffsets() const { return block_offsets; }
218  /// Return the true-dof offsets.
220 
221  /// Adds new Domain Integrator.
223  { dnfi.Append(nlfi); }
224 
225  /// Adds new Interior Face Integrator.
227  { fnfi.Append(nlfi); }
228 
229  /// Adds new Boundary Face Integrator.
231  { bfnfi.Append(nlfi); bfnfi_marker.Append(NULL); }
232 
233  /** @brief Adds new Boundary Face Integrator, restricted to specific boundary
234  attributes. */
236  Array<int> &bdr_marker);
237 
238  virtual void SetEssentialBC(const Array<Array<int> *>&bdr_attr_is_ess,
239  Array<Vector *> &rhs);
240 
241  virtual double GetEnergy(const Vector &x) const;
242 
243  virtual void Mult(const Vector &x, Vector &y) const;
244 
245  virtual Operator &GetGradient(const Vector &x) const;
246 
247  /// Destructor.
248  virtual ~BlockNonlinearForm();
249 };
250 
251 
252 }
253 
254 #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)
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:795
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 * > 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).
const Operator * P
Pointer to the prolongation matrix of fes, may be NULL.
Array< Array< int > * > bfnfi_marker
Array< Array< int > * > bfnfi_marker
Dynamic 2D array using row-major layout.
Definition: array.hpp:304
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:85
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:21
virtual const SparseMatrix * GetRestrictionMatrix() const
The returned SparseMatrix is owned by the FiniteElementSpace.
Definition: fespace.hpp:292
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.