MFEM  v4.5.2
Finite element discretization library
nonlinearform_ext.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, 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 NONLINEARFORM_EXT_HPP
13 #define NONLINEARFORM_EXT_HPP
14 
15 #include "../config/config.hpp"
16 #include "fespace.hpp"
17 
18 namespace mfem
19 {
20 
21 class NonlinearForm;
22 class NonlinearFormIntegrator;
23 
24 /** @brief Class extending the NonlinearForm class to support the different
25  AssemblyLevel%s. */
26 /** This class represents the action of the NonlinearForm as an L-to-L operator,
27  i.e. both the input and output Vectors are L-vectors (GridFunction-size
28  vectors). Essential boundary conditions are NOT applied to the action of the
29  operator. */
31 {
32 protected:
33  const NonlinearForm *nlf; ///< Not owned
34 
35 public:
37 
38  /// Assemble at the AssemblyLevel of the subclass.
39  virtual void Assemble() = 0;
40 
41  /** @brief Return the gradient as an L-to-L Operator. The input @a x must be
42  an L-vector (i.e. GridFunction-size vector). */
43  /** Essential boundary conditions are NOT applied to the returned operator.
44 
45  The returned gradient Operator defines the virtual method GetProlongation
46  which enables support for the method FormSystemOperator to define the
47  matrix-free global true-dof gradient with imposed boundary conditions. */
48  virtual Operator &GetGradient(const Vector &x) const = 0;
49 
50  /// Compute the local (to the MPI rank) energy of the L-vector state @a x.
51  virtual double GetGridFunctionEnergy(const Vector &x) const = 0;
52 
53  /// Called by NonlinearForm::Update() to reflect changes in the FE space.
54  virtual void Update() = 0;
55 };
56 
57 /// Data and methods for partially-assembled nonlinear forms
59 {
60 private:
61  class Gradient : public Operator
62  {
63  protected:
64  const PANonlinearFormExtension &ext;
65 
66  public:
67  /// Assumes that @a g is a ldof Vector.
68  Gradient(const PANonlinearFormExtension &ext);
69 
70  /// Assumes that @a x and @a y are ldof Vector%s.
71  virtual void Mult(const Vector &x, Vector &y) const;
72 
73  /// Assumes that @a g is an ldof Vector.
74  void AssembleGrad(const Vector &g);
75 
76  /// Assemble the diagonal of the gradient into the ldof Vector @a diag.
77  virtual void AssembleDiagonal(Vector &diag) const;
78 
79  /** @brief Define the prolongation Operator for use with methods like
80  FormSystemOperator. */
81  virtual const Operator *GetProlongation() const
82  {
83  return ext.fes.GetProlongationMatrix();
84  }
85 
86  /** @brief Called by PANonlinearFormExtension::Update to reflect changes
87  in the FiniteElementSpace. */
88  void Update();
89  };
90 
91 protected:
92  mutable Vector xe, ye;
95  const Operator *elemR; // not owned
96  mutable Gradient Grad;
97 
98 public:
100 
101  /// Prepare the PANonlinearFormExtension for evaluation with Mult().
102  /** This method must be called before the first call to Mult(), when the mesh
103  coordinates are changed, or some coefficients in the integrators need to
104  be re-evaluated (this is NonlinearFormIntegrator-dependent). */
105  void Assemble() override;
106 
107  /// Perform the action of the PANonlinearFormExtension.
108  /** Both the input, @a x, and output, @a y, vectors are L-vectors, i.e.
109  GridFunction-size vectors. */
110  void Mult(const Vector &x, Vector &y) const override;
111 
112  /** @brief Return the gradient as an L-to-L Operator. The input @a x must be
113  an L-vector (i.e. GridFunction-size vector). */
114  /** Essential boundary conditions are NOT applied to the returned operator.
115 
116  The returned gradient Operator defines the virtual method GetProlongation
117  which enables support for the method FormSystemOperator to define the
118  matrix-free global true-dof gradient with imposed boundary conditions. */
119  Operator &GetGradient(const Vector &x) const override;
120 
121  /// Compute the local (to the MPI rank) energy of the L-vector state @a x.
122  double GetGridFunctionEnergy(const Vector &x) const override;
123 
124  /// Called by NonlinearForm::Update() to reflect changes in the FE space.
125  void Update() override;
126 };
127 
128 /// Data and methods for unassembled nonlinear forms
130 {
131 protected:
132  const FiniteElementSpace &fes; // Not owned
133  mutable Vector localX, localY;
134  const Operator *elem_restrict_lex; // Not owned
135 
136 public:
138 
139  /// Prepare the MFNonlinearFormExtension for evaluation with Mult().
140  void Assemble() override;
141 
142  /// Perform the action of the MFNonlinearFormExtension.
143  /** Both the input, @a x, and output, @a y, vectors are L-vectors, i.e.
144  GridFunction-size vectors. */
145  void Mult(const Vector &x, Vector &y) const override;
146 
147  Operator &GetGradient(const Vector &x) const override
148  {
149  MFEM_ABORT("TODO");
150  return *const_cast<MFNonlinearFormExtension*>(this);
151  }
152 
153  double GetGridFunctionEnergy(const Vector &x) const override
154  {
155  MFEM_ABORT("TODO");
156  return 0.0;
157  }
158 
159  /// Called by NonlinearForm::Update() to reflect changes in the FE space.
160  void Update() override;
161 };
162 
163 }
164 #endif // NONLINEARFORM_EXT_HPP
Data and methods for unassembled nonlinear forms.
const Array< NonlinearFormIntegrator * > & dnfi
NonlinearFormExtension(const NonlinearForm *)
void Mult(const Vector &x, Vector &y) const override
Perform the action of the MFNonlinearFormExtension.
void Assemble() override
Prepare the PANonlinearFormExtension for evaluation with Mult().
const FiniteElementSpace & fes
Data and methods for partially-assembled nonlinear forms.
void Mult(const Vector &x, Vector &y) const override
Perform the action of the PANonlinearFormExtension.
const NonlinearForm * nlf
Not owned.
Operator & GetGradient(const Vector &x) const override
Return the gradient as an L-to-L Operator. The input x must be an L-vector (i.e. GridFunction-size ve...
void Update() override
Called by NonlinearForm::Update() to reflect changes in the FE space.
virtual void Assemble()=0
Assemble at the AssemblyLevel of the subclass.
virtual double GetGridFunctionEnergy(const Vector &x) const =0
Compute the local (to the MPI rank) energy of the L-vector state x.
void Update() override
Called by NonlinearForm::Update() to reflect changes in the FE space.
Class extending the NonlinearForm class to support the different AssemblyLevels.
virtual Operator & GetGradient(const Vector &x) const =0
Return the gradient as an L-to-L Operator. The input x must be an L-vector (i.e. GridFunction-size ve...
double GetGridFunctionEnergy(const Vector &x) const override
Compute the local (to the MPI rank) energy of the L-vector state x.
virtual const Operator * GetProlongation() const
Prolongation operator from linear algebra (linear system) vectors, to input vectors for the operator...
Definition: operator.hpp:139
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
Definition: fespace.hpp:96
void Assemble() override
Prepare the MFNonlinearFormExtension for evaluation with Mult().
const FiniteElementSpace & fes
MFNonlinearFormExtension(const NonlinearForm *)
virtual const Operator * GetProlongationMatrix() const
The returned Operator is owned by the FiniteElementSpace.
Definition: fespace.hpp:479
virtual void AssembleDiagonal(Vector &diag) const
Computes the diagonal entries into diag. Typically, this operation only makes sense for linear Operat...
Definition: operator.hpp:131
PANonlinearFormExtension(const NonlinearForm *nlf)
virtual void Update()=0
Called by NonlinearForm::Update() to reflect changes in the FE space.
double GetGridFunctionEnergy(const Vector &x) const override
Compute the local (to the MPI rank) energy of the L-vector state x.
Vector data type.
Definition: vector.hpp:60
Operator & GetGradient(const Vector &x) const override
Return the gradient as an L-to-L Operator. The input x must be an L-vector (i.e. GridFunction-size ve...
Abstract operator.
Definition: operator.hpp:24