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