MFEM  v3.3
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
handle.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_HANDLE_HPP
13 #define MFEM_HANDLE_HPP
14 
15 #include "../config/config.hpp"
16 #include "operator.hpp"
17 #ifdef MFEM_USE_MPI
18 #include "hypre.hpp"
19 #include "petsc.hpp"
20 #endif
21 
22 namespace mfem
23 {
24 
25 /// Pointer to an Operator of a specified type
26 /** This class provides a common interface for global, matrix-type operators to
27  be used in bilinear forms, gradients of nonlinear forms, static condensation,
28  hybridization, etc. The following backends are currently supported:
29  - HYPRE parallel sparse matrix (HYPRE_PARCSR)
30  - PETSC globally assembled parallel sparse matrix (PETSC_MATAIJ)
31  - PETSC parallel matrix assembled on each processor (PETSC_MATIS)
32  See also Operator::Type.
33 */
35 {
36 protected:
37  static const char not_supported_msg[];
38 
41  bool own_oper;
42 
44 
45  template <typename OpType>
46  void pSet(OpType *A, bool own_A = true)
47  {
48  oper = A;
49  type_id = A->GetType();
50  own_oper = own_A;
51  }
52 
53 public:
54  /** @brief Create an OperatorHandle with type id = Operator::MFEM_SPARSEMAT
55  without allocating the actual matrix. */
57  : oper(NULL), type_id(Operator::MFEM_SPARSEMAT), own_oper(false) { }
58 
59  /** @brief Create a OperatorHandle with a specified type id, @a tid, without
60  allocating the actual matrix. */
62  : oper(NULL), type_id(CheckType(tid)), own_oper(false) { }
63 
64  /// Create an OperatorHandle for the given OpType pointer, @a A.
65  /** Presently, OpType can be SparseMatrix, HypreParMatrix, or PetscParMatrix.
66 
67  The operator ownership flag is set to the value of @a own_A.
68 
69  It is expected that @a A points to a valid object. */
70  template <typename OpType>
71  explicit OperatorHandle(OpType *A, bool own_A = true) { pSet(A, own_A); }
72 
73  ~OperatorHandle() { if (own_oper) { delete oper; } }
74 
75  /// Shallow copy. The ownership flag of the target is set to false.
77  {
78  Clear(); oper = master.oper; type_id = master.type_id; own_oper = false;
79  return *this;
80  }
81 
82  /// Access the underlying Operator pointer.
83  Operator *Ptr() const { return oper; }
84 
85  /// Get the currently set operator type id.
86  Operator::Type Type() const { return type_id; }
87 
88  /** @brief Return the Operator pointer statically cast to a specified OpType.
89  Similar to the method Get(). */
90  template <typename OpType>
91  OpType *As() const { return static_cast<OpType*>(oper); }
92 
93  /// Return the Operator pointer dynamically cast to a specified OpType.
94  template <typename OpType>
95  OpType *Is() const { return dynamic_cast<OpType*>(oper); }
96 
97  /// Return the Operator pointer statically cast to a given OpType.
98  /** Similar to the method As(), however the template type OpType can be
99  derived automatically from the argument @a A. */
100  template <typename OpType>
101  void Get(OpType *&A) const { A = static_cast<OpType*>(oper); }
102 
103  /// Return true if the OperatorHandle owns the held Operator.
104  bool OwnsOperator() const { return own_oper; }
105 
106  /// Set the ownership flag for the held Operator.
107  void SetOperatorOwner(bool own = true) { own_oper = own; }
108 
109  /** @brief Clear the OperatorHandle, deleting the held Operator (if owned),
110  while leaving the type id unchanged. */
111  void Clear()
112  {
113  if (own_oper) { delete oper; }
114  oper = NULL;
115  own_oper = false;
116  }
117 
118  /// Invoke Clear() and set a new type id.
120  {
121  Clear();
122  type_id = CheckType(tid);
123  }
124 
125  /// Reset the OperatorHandle to the given OpType pointer, @a A.
126  /** Presently, OpType can be SparseMatrix, HypreParMatrix, or PetscParMatrix.
127 
128  The operator ownership flag is set to the value of @a own_A.
129 
130  It is expected that @a A points to a valid object. */
131  template <typename OpType>
132  void Reset(OpType *A, bool own_A = true)
133  {
134  if (own_oper) { delete oper; }
135  pSet(A, own_A);
136  }
137 
138 #ifdef MFEM_USE_MPI
139  /** @brief Reset the OperatorHandle to hold a parallel square block-diagonal
140  matrix using the currently set type id. */
141  /** The operator ownership flag is set to true. */
142  void MakeSquareBlockDiag(MPI_Comm comm, HYPRE_Int glob_size,
143  HYPRE_Int *row_starts, SparseMatrix *diag);
144 
145  /** @brief Reset the OperatorHandle to hold a parallel rectangular
146  block-diagonal matrix using the currently set type id. */
147  /** The operator ownership flag is set to true. */
148  void MakeRectangularBlockDiag(MPI_Comm comm, HYPRE_Int glob_num_rows,
149  HYPRE_Int glob_num_cols, HYPRE_Int *row_starts,
150  HYPRE_Int *col_starts, SparseMatrix *diag);
151 #endif // MFEM_USE_MPI
152 
153  /// Reset the OperatorHandle to hold the product @a P^t @a A @a P.
154  /** The type id of the result is determined by that of @a A and @a P. The
155  operator ownership flag is set to true. */
157 
158  /** @brief Reset the OperatorHandle to hold the product @a R @a A @a P, where
159  R = @a Rt^t. */
160  /** The type id of the result is determined by that of @a Rt, @a A, and @a P.
161  The operator ownership flag is set to true. */
163 
164  /// Convert the given OperatorHandle @a A to the currently set type id.
165  /** The operator ownership flag is set to false if the target type id is the
166  same as the input; otherwise it is set to true. */
167  void ConvertFrom(OperatorHandle &A);
168 
169  /// Convert the given OpType pointer, @a A, to the currently set type id.
170  /** This method creates a temporary OperatorHandle for @a A and invokes
171  ConvertFrom(OperatorHandle &) with it. */
172  template <typename OpType>
173  void ConvertFrom(OpType *A)
174  {
175  OperatorHandle Ah(A, false);
176  ConvertFrom(Ah);
177  }
178 
179  /** @brief Reset the OperatorHandle to be the eliminated part of @a A after
180  elimination of the essential dofs @a ess_dof_list. */
181  void EliminateRowsCols(OperatorHandle &A, const Array<int> &ess_dof_list);
182 
183  /// Eliminate essential dofs from the solution @a X into the r.h.s. @a B.
184  /** The argument @a A_e is expected to be the result of the method
185  EliminateRowsCols(). */
186  void EliminateBC(const OperatorHandle &A_e, const Array<int> &ess_dof_list,
187  const Vector &X, Vector &B) const;
188 };
189 
190 } // namespace mfem
191 
192 #endif
OperatorHandle()
Create an OperatorHandle with type id = Operator::MFEM_SPARSEMAT without allocating the actual matrix...
Definition: handle.hpp:56
void ConvertFrom(OperatorHandle &A)
Convert the given OperatorHandle A to the currently set type id.
Definition: handle.cpp:163
OpType * As() const
Return the Operator pointer statically cast to a specified OpType. Similar to the method Get()...
Definition: handle.hpp:91
Pointer to an Operator of a specified type.
Definition: handle.hpp:34
Operator::Type Type() const
Get the currently set operator type id.
Definition: handle.hpp:86
void EliminateBC(const OperatorHandle &A_e, const Array< int > &ess_dof_list, const Vector &X, Vector &B) const
Eliminate essential dofs from the solution X into the r.h.s. B.
Definition: handle.cpp:236
Operator * oper
Definition: handle.hpp:39
static const char not_supported_msg[]
Definition: handle.hpp:37
OpType * Is() const
Return the Operator pointer dynamically cast to a specified OpType.
Definition: handle.hpp:95
Data type sparse matrix.
Definition: sparsemat.hpp:38
void EliminateRowsCols(OperatorHandle &A, const Array< int > &ess_dof_list)
Reset the OperatorHandle to be the eliminated part of A after elimination of the essential dofs ess_d...
Definition: handle.cpp:194
Operator::Type CheckType(Operator::Type tid)
Definition: handle.cpp:21
Operator * Ptr() const
Access the underlying Operator pointer.
Definition: handle.hpp:83
Type
Enumeration defining IDs for some classes derived from Operator.
Definition: operator.hpp:123
bool OwnsOperator() const
Return true if the OperatorHandle owns the held Operator.
Definition: handle.hpp:104
OperatorHandle(Operator::Type tid)
Create a OperatorHandle with a specified type id, tid, without allocating the actual matrix...
Definition: handle.hpp:61
void ConvertFrom(OpType *A)
Convert the given OpType pointer, A, to the currently set type id.
Definition: handle.hpp:173
void Clear()
Clear the OperatorHandle, deleting the held Operator (if owned), while leaving the type id unchanged...
Definition: handle.hpp:111
void SetOperatorOwner(bool own=true)
Set the ownership flag for the held Operator.
Definition: handle.hpp:107
void MakeSquareBlockDiag(MPI_Comm comm, HYPRE_Int glob_size, HYPRE_Int *row_starts, SparseMatrix *diag)
Reset the OperatorHandle to hold a parallel square block-diagonal matrix using the currently set type...
Definition: handle.cpp:48
void pSet(OpType *A, bool own_A=true)
Definition: handle.hpp:46
Operator::Type type_id
Definition: handle.hpp:40
void MakeRectangularBlockDiag(MPI_Comm comm, HYPRE_Int glob_num_rows, HYPRE_Int glob_num_cols, HYPRE_Int *row_starts, HYPRE_Int *col_starts, SparseMatrix *diag)
Reset the OperatorHandle to hold a parallel rectangular block-diagonal matrix using the currently set...
Definition: handle.cpp:72
void Get(OpType *&A) const
Return the Operator pointer statically cast to a given OpType.
Definition: handle.hpp:101
Vector data type.
Definition: vector.hpp:36
OperatorHandle & operator=(const OperatorHandle &master)
Shallow copy. The ownership flag of the target is set to false.
Definition: handle.hpp:76
Abstract operator.
Definition: operator.hpp:21
void MakePtAP(OperatorHandle &A, OperatorHandle &P)
Reset the OperatorHandle to hold the product P^t A P.
Definition: handle.cpp:98
void SetType(Operator::Type tid)
Invoke Clear() and set a new type id.
Definition: handle.hpp:119
void MakeRAP(OperatorHandle &Rt, OperatorHandle &A, OperatorHandle &P)
Reset the OperatorHandle to hold the product R A P, where R = Rt^t.
Definition: handle.cpp:130
OperatorHandle(OpType *A, bool own_A=true)
Create an OperatorHandle for the given OpType pointer, A.
Definition: handle.hpp:71
void Reset(OpType *A, bool own_A=true)
Reset the OperatorHandle to the given OpType pointer, A.
Definition: handle.hpp:132