MFEM  v4.6.0
Finite element discretization library
petsc.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 // Author: Stefano Zampini <stefano.zampini@gmail.com>
13 
14 #ifndef MFEM_PETSC
15 #define MFEM_PETSC
16 
17 #include "../config/config.hpp"
18 
19 #ifdef MFEM_USE_PETSC
20 #ifdef MFEM_USE_MPI
21 
22 #include <limits>
23 
24 #include "handle.hpp"
25 #include "hypre.hpp"
26 #include "ode.hpp"
27 #include "../general/mem_manager.hpp"
28 
29 #include "petscconf.h"
30 #if !defined(PETSC_USE_REAL_DOUBLE)
31 #error "MFEM does not work with PETSc compiled without double precision"
32 #endif
33 #if defined(PETSC_USE_COMPLEX)
34 #error "MFEM does not work with PETSc compiled with complex numbers support"
35 #endif
36 #if defined(PETSC_USE_64BIT_INDICES) && !defined(HYPRE_BIGINT)
37 #error "Mismatch between HYPRE (32bit) and PETSc (64bit) integer types"
38 #endif
39 #if !defined(PETSC_USE_64BIT_INDICES) && defined(HYPRE_BIGINT)
40 #error "Mismatch between HYPRE (64bit) and PETSc (32bit) integer types"
41 #endif
42 
43 #include "petscversion.h"
44 #if PETSC_VERSION_GE(3,12,0)
45 #include "petscsystypes.h"
46 #else
47 typedef HYPRE_Int PetscInt;
48 typedef double PetscScalar;
49 typedef double PetscReal;
50 typedef int PetscClassId;
51 typedef struct _p_PetscObject *PetscObject;
52 #endif
53 
54 // forward declarations of PETSc internal structs
55 struct _p_Vec;
56 struct _p_Mat;
57 struct _p_KSP;
58 struct _p_PC;
59 struct _p_SNES;
60 struct _p_TS;
61 
62 
63 namespace mfem
64 {
65 
66 // Declare aliases of PETSc's types inside the namespace mfem::petsc:
67 namespace petsc
68 {
69 typedef struct ::_p_Vec *Vec;
70 typedef struct ::_p_Mat *Mat;
71 typedef struct ::_p_KSP *KSP;
72 typedef struct ::_p_PC *PC;
73 typedef struct ::_p_SNES *SNES;
74 typedef struct ::_p_TS *TS;
75 }
76 
77 /// Convenience functions to initialize/finalize PETSc
78 void MFEMInitializePetsc();
79 void MFEMInitializePetsc(int*,char***);
80 void MFEMInitializePetsc(int*,char***,const char[],const char[]);
81 void MFEMFinalizePetsc();
82 
83 /// Wrapper for syncing PETSc's vector memory
84 class PetscMemory : public Memory<double>
85 {
86 private:
87  Memory<double> *base;
88  bool read;
89  bool write;
90  bool usedev;
91 public:
92  PetscMemory() { Reset(); base = nullptr; }
93  void SetHostValid() const { flags |= VALID_HOST; }
94  void SetDeviceValid() const { flags |= VALID_DEVICE; }
95  void SetHostInvalid() const { flags &= ~VALID_HOST; }
96  void SetDeviceInvalid() const { flags &= ~VALID_DEVICE; }
97  inline bool IsAliasForSync() const { return base && (flags & ALIAS); }
98 
99  inline void MakeAliasForSync(const Memory<double> &base_, int offset_,
100  int size_, bool usedev_)
101  {
102  MFEM_VERIFY(!IsAliasForSync(),"Already alias");
103  base = (Memory<double>*)&base_;
104  read = true;
105  write = false;
106  usedev = usedev_;
107  MakeAlias(base_,offset_,size_);
108  }
109  inline void MakeAliasForSync(Memory<double> &base_, int offset_, int size_,
110  bool read_, bool write_, bool usedev_)
111  {
112  MFEM_VERIFY(!IsAliasForSync(),"Already alias");
113  base = (Memory<double>*)&base_;
114  read = read_;
115  write = write_;
116  usedev = usedev_;
117  MakeAlias(base_,offset_,size_);
118  }
119  inline void SyncBase()
120  {
121  MFEM_VERIFY(IsAliasForSync(),"MakeAliasForSynch not called");
122  base->Sync(*this);
123  }
124  inline void SyncBaseAndReset()
125  {
126  SyncBase();
127  base = nullptr;
128  Reset();
129  }
130  inline bool ReadRequested() const
131  {
132  MFEM_VERIFY(IsAliasForSync(),"MakeAliasForSynch not called");
133  return read;
134  }
135  inline bool WriteRequested() const
136  {
137  MFEM_VERIFY(IsAliasForSync(),"MakeAliasForSynch not called");
138  return write;
139  }
140  inline bool DeviceRequested() const
141  {
142  MFEM_VERIFY(IsAliasForSync(),"MakeAliasForSynch not called");
143  return usedev;
144  }
145  const double *GetHostPointer() const;
146  const double *GetDevicePointer() const;
147 };
148 
149 /// Wrapper for PETSc's vector class
150 class ParFiniteElementSpace;
151 class PetscParMatrix;
152 
153 class PetscParVector : public Vector
154 {
155 protected:
156  /// The actual PETSc object
158 
160 
161  friend class PetscParMatrix;
162  friend class PetscODESolver;
163  friend class PetscLinearSolver;
164  friend class PetscPreconditioner;
165  friend class PetscNonlinearSolver;
166  friend class PetscBDDCSolver;
167 
168  // Set Vector::data and Vector::size from x
169  void SetDataAndSize_();
170 
171  // Set Vec type from Device type
172  void SetVecType_();
173 
174  // Update Memory flags from PETSc offloadmask
175  void SetFlagsFromMask_() const;
176 
177 public:
178  /// Creates vector with given global size and partitioning of the columns.
179  /** If @a col is provided, processor P owns columns [col[P],col[P+1]).
180  Otherwise, PETSc decides the partitioning */
181  PetscParVector(MPI_Comm comm, PetscInt glob_size, PetscInt *col = NULL);
182 
183  /** @brief Creates vector with given global size, partitioning of the
184  columns, and data.
185 
186  The data must be allocated and destroyed outside. If @a data_ is NULL, a
187  dummy vector without a valid data array will be created. */
188  PetscParVector(MPI_Comm comm, PetscInt glob_size, PetscScalar *data_,
189  PetscInt *col);
190 
191  /// Creates vector compatible with @a y
192  PetscParVector(const PetscParVector &y);
193 
194  /** @brief Creates a PetscParVector from a Vector
195  @param[in] comm MPI communicator on which the new object lives
196  @param[in] x_ The mfem Vector (data is not shared)
197  @param[in] copy Whether to copy the data in x_ or not */
198  PetscParVector(MPI_Comm comm, const Vector &x_, bool copy = false);
199 
200  /** @brief Creates vector compatible with the Operator (i.e. in the domain
201  of) @a op or its adjoint. */
202  /** The argument @a allocate determines if the memory is actually allocated
203  to store the data. */
204  explicit PetscParVector(MPI_Comm comm, const Operator &op,
205  bool transpose = false, bool allocate = true);
206 
207  /// Creates vector compatible with (i.e. in the domain of) @a A or @a A^T
208  /** The argument @a allocate determines if the memory is actually allocated
209  to store the data. */
210  explicit PetscParVector(const PetscParMatrix &A, bool transpose = false,
211  bool allocate = true);
212 
213  /// Creates PetscParVector out of PETSc Vec object.
214  /** @param[in] y The PETSc Vec object.
215  @param[in] ref If true, we increase the reference count of @a y. */
216  explicit PetscParVector(petsc::Vec y, bool ref=false);
217 
218  /// Create a true dof parallel vector on a given ParFiniteElementSpace
219  explicit PetscParVector(ParFiniteElementSpace *pfes);
220 
221  /// Calls PETSc's destroy function
222  virtual ~PetscParVector();
223 
224  /// Get the associated MPI communicator
225  MPI_Comm GetComm() const;
226 
227  /// Returns the global number of rows
228  PetscInt GlobalSize() const;
229 
230  /// Typecasting to PETSc's Vec type
231  operator petsc::Vec() const { return x; }
232 
233  /// Typecasting to PETSc object
234  operator PetscObject() const { return (PetscObject)x; }
235 
236  /// Returns the global vector in each processor
237  Vector* GlobalVector() const;
238 
239  /// Set constant values
241 
242  /** @brief Set block size of a vector.
243 
244  @note This will error if the local size of the vector is not a multiple
245  of the block size @a bs.
246  @note This is a logically collective operation, so all processes need
247  to call it. */
248  void SetBlockSize(PetscInt bs);
249 
250  /** @brief Set values in a vector.
251 
252  @note Any process can insert in any location.
253  @note This is a collective operation, so all processes need to call it. */
255 
256  /** @brief Add values in a vector.
257 
258  @note Any process can add to any location.
259  @note This is a collective operation, so all processes need to call it. */
261 
262  /// Define operators for PETSc vectors.
268 
269  /** @brief Temporarily replace the data of the PETSc Vec object. To return to
270  the original data array, call ResetArray().
271 
272  @note This method calls PETSc's VecPlaceArray() function.
273  @note The inherited Vector::data pointer is not affected by this call. */
274  void PlaceArray(PetscScalar *temp_data);
275 
276  /** @brief Reset the PETSc Vec object to use its default data. Call this
277  method after the use of PlaceArray().
278 
279  @note This method calls PETSc's VecResetArray() function. */
280  void ResetArray();
281 
282  /** @brief This requests write access from where the memory is valid
283  and temporarily replaces the corresponding array used by the PETSc Vec
284  The bool parameter indicates read/write request */
285  void PlaceMemory(Memory<double>&,bool=false);
286 
287  /** @brief This requests read access from where the memory is valid
288  and temporarily replaces the corresponding array used by the PETSc Vec */
289  void PlaceMemory(const Memory<double>&);
290 
291  /** @brief Completes the operation started with PlaceMemory */
292  void ResetMemory();
293 
294  /** @brief Update PETSc's Vec after having accessed its data via GetMemory() */
295  void UpdateVecFromFlags();
296 
297  /// Set random values
298  void Randomize(PetscInt seed = 0);
299 
300  /// Prints the vector (to stdout if @a fname is NULL)
301  void Print(const char *fname = NULL, bool binary = false) const;
302 
303  const double *Read(bool=true) const override;
304  const double *HostRead() const override;
305  double *Write(bool=true) override;
306  double *HostWrite() override;
307  double *ReadWrite(bool=true) override;
308  double *HostReadWrite() override;
309  bool UseDevice() const override;
310  void UseDevice(bool) const override;
311 };
312 
313 
314 /// Wrapper for PETSc's matrix class
315 class PetscParMatrix : public Operator
316 {
317 protected:
318  /// The actual PETSc object
320 
321  /// Auxiliary vectors for typecasting
322  mutable PetscParVector *X, *Y;
323 
324  /// Initialize with defaults. Does not initialize inherited members.
325  void Init();
326 
327  /// Delete all owned data. Does not perform re-initialization with defaults.
328  void Destroy();
329 
330  /** @brief Creates a wrapper around a mfem::Operator @a op using PETSc's
331  MATSHELL object and returns the Mat in @a B.
332 
333  This does not take any reference to @a op, that should not be destroyed
334  until @a B is needed. */
335  void MakeWrapper(MPI_Comm comm, const Operator* op, petsc::Mat *B);
336 
337  /// Convert an mfem::Operator into a Mat @a B; @a op can be destroyed unless
338  /// tid == PETSC_MATSHELL or tid == PETSC_MATHYPRE
339  /// if op is a BlockOperator, the operator type is relevant to the individual
340  /// blocks
341  void ConvertOperator(MPI_Comm comm, const Operator& op, petsc::Mat *B,
342  Operator::Type tid);
343 
344  friend class PetscLinearSolver;
345  friend class PetscPreconditioner;
346 
347 private:
348  /// Constructs a block-diagonal Mat object
349  void BlockDiagonalConstructor(MPI_Comm comm, PetscInt *row_starts,
350  PetscInt *col_starts, SparseMatrix *diag,
351  bool assembled, petsc::Mat *A);
352 
353  void SetUpForDevice();
354 
355 public:
356  /// Create an empty matrix to be used as a reference to an existing matrix.
357  PetscParMatrix();
358 
359  /// Creates PetscParMatrix out of PETSc's Mat.
360  /** @param[in] a The PETSc Mat object.
361  @param[in] ref If true, we increase the reference count of @a a. */
362  PetscParMatrix(petsc::Mat a, bool ref=false);
363 
364  /** @brief Convert a PetscParMatrix @a pa with a new PETSc format @a tid.
365  Note that if @a pa is already a PetscParMatrix of the same type as
366  @a tid, the resulting PetscParMatrix will share the same Mat object */
367  explicit PetscParMatrix(const PetscParMatrix *pa, Operator::Type tid);
368 
369  /** @brief Creates a PetscParMatrix extracting the submatrix of @a A with
370  @a rows row indices and @a cols column indices */
371  PetscParMatrix(const PetscParMatrix& A, const Array<PetscInt>& rows,
372  const Array<PetscInt>& cols);
373 
374  /** @brief Convert a HypreParMatrix @a ha to a PetscParMatrix in the given
375  PETSc format @a tid. */
376  /** The supported type ids are: Operator::PETSC_MATAIJ,
377  Operator::PETSC_MATIS, Operator::PETSC_MATSHELL and
378  Operator::PETSC_MATHYPRE
379  @a ha can be destroyed unless tid == PETSC_MATSHELL or
380  tid == PETSC_MATHYPRE */
381  explicit PetscParMatrix(const HypreParMatrix *ha,
383 
384  /** @brief Convert a SparseMatrix @a ha to a PetscParMatrix in the given
385  PETSc format @a tid. */
386  explicit PetscParMatrix(const SparseMatrix *sa,
388 
389  /** @brief Convert an mfem::Operator into a PetscParMatrix in the given PETSc
390  format @a tid. */
391  /** If @a tid is Operator::PETSC_MATSHELL and @a op is not a PetscParMatrix,
392  it converts any mfem::Operator @a op implementing Operator::Mult() and
393  Operator::MultTranspose() into a PetscParMatrix. The Operator @a op
394  should not be deleted while the constructed PetscParMatrix is used.
395 
396  Otherwise, it tries to convert the operator in PETSc's classes.
397  @a op cannot be destroyed if tid == PETSC_MATHYPRE.
398 
399  In particular, if @a op is a BlockOperator, then a MATNEST Mat object is
400  created using @a tid as the type for the blocks.
401  Note that if @a op is already a PetscParMatrix of the same type as
402  @a tid, the resulting PetscParMatrix will share the same Mat object */
403  PetscParMatrix(MPI_Comm comm, const Operator *op,
405 
406  /// Creates block-diagonal square parallel matrix.
407  /** The block-diagonal is given by @a diag which must be in CSR format
408  (finalized). The new PetscParMatrix does not take ownership of any of the
409  input arrays. The type id @a tid can be either PETSC_MATAIJ (parallel
410  distributed CSR) or PETSC_MATIS. */
411  PetscParMatrix(MPI_Comm comm, PetscInt glob_size, PetscInt *row_starts,
412  SparseMatrix *diag, Operator::Type tid);
413 
414  /// Creates block-diagonal rectangular parallel matrix.
415  /** The block-diagonal is given by @a diag which must be in CSR format
416  (finalized). The new PetscParMatrix does not take ownership of any of the
417  input arrays. The type id @a tid can be either PETSC_MATAIJ (parallel
418  distributed CSR) or PETSC_MATIS. */
419  PetscParMatrix(MPI_Comm comm, PetscInt global_num_rows,
420  PetscInt global_num_cols, PetscInt *row_starts,
421  PetscInt *col_starts, SparseMatrix *diag,
422  Operator::Type tid);
423 
424  /// Calls PETSc's destroy function.
425  virtual ~PetscParMatrix() { Destroy(); }
426 
427  /// Replace the inner Mat Object. The reference count of newA is increased
428  void SetMat(petsc::Mat newA);
429 
430  /// @name Assignment operators
431  ///@{
436  ///@}
437 
438  /// Matvec: @a y = @a a A @a x + @a b @a y.
439  void Mult(double a, const Vector &x, double b, Vector &y) const;
440 
441  /// Matvec transpose: @a y = @a a A^T @a x + @a b @a y.
442  void MultTranspose(double a, const Vector &x, double b, Vector &y) const;
443 
444  void Mult(const Vector &x, Vector &y) const override
445  { Mult(1.0, x, 0.0, y); }
446 
447  void MultTranspose(const Vector &x, Vector &y) const override
448  { MultTranspose(1.0, x, 0.0, y); }
449 
450  void AddMult(const Vector &x, Vector &y,
451  const double a = 1.0) const override
452  { Mult(a, x, 1.0, y); }
453 
454  void AddMultTranspose(const Vector &x, Vector &y,
455  const double a = 1.0) const override
456  { MultTranspose(a, x, 1.0, y); }
457 
458  /// Get the associated MPI communicator
459  MPI_Comm GetComm() const;
460 
461  /// Typecasting to PETSc's Mat type
462  operator petsc::Mat() const { return A; }
463 
464  /// Typecasting to PETSc object
465  operator PetscObject() const { return (PetscObject)A; }
466 
467  /// Returns the global index of the first local row
468  PetscInt GetRowStart() const;
469 
470  /// Returns the global index of the first local column
471  PetscInt GetColStart() const;
472 
473  /// Returns the local number of rows
474  PetscInt GetNumRows() const;
475 
476  /// Returns the local number of columns
477  PetscInt GetNumCols() const;
478 
479  /// Returns the global number of rows
480  PetscInt M() const;
481 
482  /// Returns the global number of columns
483  PetscInt N() const;
484 
485  /// Returns the global number of rows
486  PetscInt GetGlobalNumRows() const { return M(); }
487 
488  /// Returns the global number of columns
489  PetscInt GetGlobalNumCols() const { return N(); }
490 
491  /// Returns the number of nonzeros.
492  /** Differently from HYPRE, this call is collective on the communicator,
493  as this number is not stored inside PETSc, but needs to be computed. */
494  PetscInt NNZ() const;
495 
496  /// Returns the inner vector in the domain of A (it creates it if needed)
497  PetscParVector* GetX() const;
498 
499  /// Returns the inner vector in the range of A (it creates it if needed)
500  PetscParVector* GetY() const;
501 
502  /// Returns the transpose of the PetscParMatrix.
503  /** If @a action is false, the new matrix is constructed with the PETSc
504  function MatTranspose().
505  If @a action is true, then the matrix is not actually transposed.
506  Instead, an object that behaves like the transpose is returned. */
507  PetscParMatrix* Transpose(bool action = false);
508 
509  /// Prints the matrix (to stdout if fname is NULL)
510  void Print(const char *fname = NULL, bool binary = false) const;
511 
512  /// Scale all entries by s: A_scaled = s*A.
513  void operator*=(double s);
514 
515  /** @brief Eliminate rows and columns from the matrix, and rows from the
516  vector @a B. Modify @a B with the BC values in @a X. Put @a diag
517  on the diagonal corresponding to eliminated entries */
518  void EliminateRowsCols(const Array<int> &rows_cols, const PetscParVector &X,
519  PetscParVector &B, double diag = 1.);
520  void EliminateRowsCols(const Array<int> &rows_cols, const HypreParVector &X,
521  HypreParVector &B, double diag = 1.);
522 
523  /** @brief Eliminate rows and columns from the matrix and store the
524  eliminated elements in a new matrix Ae (returned).
525 
526  The sum of the modified matrix and the returned matrix, Ae, is equal to
527  the original matrix. */
528  PetscParMatrix* EliminateRowsCols(const Array<int> &rows_cols);
529 
530  /// Scale the local row i by s(i).
531  void ScaleRows(const Vector & s);
532 
533  /// Scale the local col i by s(i).
534  void ScaleCols(const Vector & s);
535 
536  /// Shift diagonal by a constant
537  void Shift(double s);
538 
539  /// Shift diagonal by a vector
540  void Shift(const Vector & s);
541 
542  /** @brief Eliminate only the rows from the matrix */
543  void EliminateRows(const Array<int> &rows);
544 
545  /** @brief Set row and column block sizes of a matrix.
546 
547  @note This will error if the local sizes of the matrix are not a
548  multiple of the block sizes.
549  @note This is a logically collective operation, so all processes need
550  to call it. */
551  void SetBlockSize(PetscInt rbs,PetscInt cbs=-1);
552 
553  /// Makes this object a reference to another PetscParMatrix
554  void MakeRef(const PetscParMatrix &master);
555 
556  /** @brief Release the PETSc Mat object. If @a dereference is true, decrement
557  the refcount of the Mat object. */
558  petsc::Mat ReleaseMat(bool dereference);
559 
560  Type GetType() const;
561 };
562 
563 /// Returns the matrix A * B
564 PetscParMatrix * ParMult(const PetscParMatrix *A, const PetscParMatrix *B);
565 
566 /// Returns the matrix Rt^t * A * P
567 PetscParMatrix * RAP(PetscParMatrix *Rt, PetscParMatrix *A, PetscParMatrix *P);
568 
569 /// Returns the matrix R * A * P
570 PetscParMatrix * TripleMatrixProduct(PetscParMatrix *R, PetscParMatrix *A,
571  PetscParMatrix *P);
572 
573 /// Returns the matrix P^t * A * P
574 PetscParMatrix * RAP(PetscParMatrix *A, PetscParMatrix *P);
575 
576 /// Returns the matrix P^t * A * P
577 PetscParMatrix * RAP(HypreParMatrix *A, PetscParMatrix *P);
578 
579 /** @brief Eliminate essential BC specified by @a ess_dof_list from the solution
580  @a X to the r.h.s. @a B.
581 
582  Here, @a A is a matrix with eliminated BC, while @a Ae is such that
583  (@a A + @a Ae) is the original (Neumann) matrix before elimination. */
584 void EliminateBC(PetscParMatrix &A, PetscParMatrix &Ae,
585  const Array<int> &ess_dof_list, const Vector &X, Vector &B);
586 
587 /// Helper class for handling essential boundary conditions.
589 {
590 public:
591  enum Type
592  {
594  CONSTANT, ///< Constant in time b.c.
596  };
597 
599  bctype(type_), setup(false), eval_t(0.0),
600  eval_t_cached(std::numeric_limits<double>::min()) {}
601  PetscBCHandler(Array<int>& ess_tdof_list, Type type_ = ZERO);
602 
603  virtual ~PetscBCHandler() {}
604 
605  /// Returns the type of boundary conditions
606  Type GetType() const { return bctype; }
607 
608  /// Sets the type of boundary conditions
609  void SetType(enum Type type_) { bctype = type_; setup = false; }
610 
611  /// Boundary conditions evaluation
612  /** In the result vector, @a g, only values at the essential dofs need to be
613  set. */
614  virtual void Eval(double t, Vector &g)
615  { mfem_error("PetscBCHandler::Eval method not overloaded"); }
616 
617  /// Sets essential dofs (local, per-process numbering)
618  void SetTDofs(Array<int>& list);
619 
620  /// Gets essential dofs (local, per-process numbering)
621  Array<int>& GetTDofs() { return ess_tdof_list; }
622 
623  /// Sets the current time
624  void SetTime(double t) { eval_t = t; }
625 
626  /// SetUp the helper object, where @a n is the size of the solution vector
627  void SetUp(PetscInt n);
628 
629  /// y = x on ess_tdof_list_c and y = g (internally evaluated) on ess_tdof_list
630  void ApplyBC(const Vector &x, Vector &y);
631 
632  /// Replace boundary dofs with the current value
633  void ApplyBC(Vector &x);
634 
635  /// y = x-g on ess_tdof_list, the rest of y is unchanged
636  void FixResidualBC(const Vector& x, Vector& y);
637 
638  /// Replace boundary dofs with 0
639  void Zero(Vector &x);
640 
641  /// y = x on ess_tdof_list_c and y = 0 on ess_tdof_list
642  void ZeroBC(const Vector &x, Vector &y);
643 
644 private:
645  enum Type bctype;
646  bool setup;
647 
648  double eval_t;
649  double eval_t_cached;
650  Vector eval_g;
651 
652  Array<int> ess_tdof_list; //Essential true dofs
653 };
654 
655 // Helper class for user-defined preconditioners that needs to be setup
657 {
658 private:
659  std::string name;
660 public:
661  PetscPreconditionerFactory(const std::string &name_ = "MFEM Factory")
662  : name(name_) { }
663  const char* GetName() { return name.c_str(); }
664  virtual Solver *NewPreconditioner(const OperatorHandle& oh) = 0;
666 };
667 
668 // Forward declarations of helper classes
669 class PetscSolverMonitor;
670 
671 /// Abstract class for PETSc's solvers.
673 {
674 protected:
675  /// Boolean to handle SetFromOptions calls.
676  mutable bool clcustom;
677 
678  /// The actual PETSc object (KSP, PC, SNES or TS).
680 
681  /// The class id of the actual PETSc object
683 
684  /// Right-hand side and solution vector
685  mutable PetscParVector *B, *X;
686 
687  /// Handler for boundary conditions
689 
690  /// Private context for solver
691  void *private_ctx;
692 
693  /// Boolean to handle SetOperator calls.
694  mutable bool operatorset;
695 
696 public:
697  /// Construct an empty PetscSolver. Initialize protected objects to NULL.
698  PetscSolver();
699 
700  /// Destroy the PetscParVectors allocated (if any).
701  virtual ~PetscSolver();
702 
703  /** @name Update of PETSc options.
704  The following Set methods can be used to update the internal PETSc
705  options.
706  @note They will be overwritten by the options in the input PETSc file. */
707  ///@{
708  void SetTol(double tol);
709  void SetRelTol(double tol);
710  void SetAbsTol(double tol);
711  void SetMaxIter(int max_iter);
712  void SetPrintLevel(int plev);
713  ///@}
714 
715  /// Customize object with options set
716  /** If @a customize is false, it disables any options customization. */
717  void Customize(bool customize = true) const;
718  int GetConverged();
719  int GetNumIterations();
720  double GetFinalNorm();
721 
722  /// Sets user-defined monitoring routine.
724 
725  /// Sets the object to handle essential boundary conditions
726  void SetBCHandler(PetscBCHandler *bch);
727 
728  /// Sets the object for the creation of the preconditioner
730 
731  /// Conversion function to PetscObject.
732  operator PetscObject() const { return obj; }
733 
734  /// Get the associated MPI communicator
735  MPI_Comm GetComm() const;
736 
737 protected:
738  /// These two methods handle creation and destructions of
739  /// private data for the Solver objects
740  void CreatePrivateContext();
741  void FreePrivateContext();
742 };
743 
744 
745 /// Abstract class for PETSc's linear solvers.
746 class PetscLinearSolver : public PetscSolver, public Solver
747 {
748 private:
749  /// Internal flag to handle HypreParMatrix conversion or not.
750  bool wrap;
751  void MultKernel(const Vector &b, Vector &x, bool trans) const;
752 
753 public:
754  PetscLinearSolver(MPI_Comm comm, const std::string &prefix = std::string(),
755  bool wrap = true, bool iter_mode = false);
757  const std::string &prefix = std::string(), bool iter_mode = false);
758  /// Constructs a solver using a HypreParMatrix.
759  /** If @a wrap is true, then the MatMult ops of HypreParMatrix are wrapped.
760  No preconditioner can be automatically constructed from PETSc. If
761  @a wrap is false, the HypreParMatrix is converted into a the AIJ
762  PETSc format, which is suitable for most preconditioning methods. */
763  PetscLinearSolver(const HypreParMatrix &A, bool wrap = true,
764  const std::string &prefix = std::string(), bool iter_mode = false);
765  virtual ~PetscLinearSolver();
766 
767  /// Sets the operator to be used for mat-vec operations and
768  /// for the construction of the preconditioner
769  virtual void SetOperator(const Operator &op);
770 
771  /// Allows to prescribe a different operator (@a pop) to construct
772  /// the preconditioner
773  void SetOperator(const Operator &op, const Operator &pop);
774 
775  /// Sets the solver to perform preconditioning
776  /// preserves the linear operator for the mat-vec
777  void SetPreconditioner(Solver &precond);
778 
779  /// Application of the solver.
780  virtual void Mult(const Vector &b, Vector &x) const;
781  virtual void MultTranspose(const Vector &b, Vector &x) const;
782 
783  /// Conversion function to PETSc's KSP type.
784  operator petsc::KSP() const { return (petsc::KSP)obj; }
785 };
786 
787 
789 {
790 public:
791  PetscPCGSolver(MPI_Comm comm, const std::string &prefix = std::string(),
792  bool iter_mode = false);
793  PetscPCGSolver(PetscParMatrix &A, const std::string &prefix = std::string(),
794  bool iter_mode = false);
795  PetscPCGSolver(HypreParMatrix &A, bool wrap = true,
796  const std::string &prefix = std::string(), bool iter_mode = false);
797 };
798 
799 
800 /// Abstract class for PETSc's preconditioners.
801 class PetscPreconditioner : public PetscSolver, public Solver
802 {
803 private:
804  void MultKernel(const Vector &b, Vector &x, bool trans) const;
805 
806 public:
807  PetscPreconditioner(MPI_Comm comm,
808  const std::string &prefix = std::string());
810  const std::string &prefix = std::string());
811  PetscPreconditioner(MPI_Comm comm, Operator &op,
812  const std::string &prefix = std::string());
813  virtual ~PetscPreconditioner();
814 
815  virtual void SetOperator(const Operator &op);
816 
817  /// Application of the preconditioner.
818  virtual void Mult(const Vector &b, Vector &x) const;
819  virtual void MultTranspose(const Vector &b, Vector &x) const;
820 
821  /// Conversion function to PETSc's PC type.
822  operator petsc::PC() const { return (petsc::PC)obj; }
823 };
824 
825 
826 /// Auxiliary class for BDDC customization.
828 {
829 protected:
835  bool netflux;
836  friend class PetscBDDCSolver;
837 
838 public:
840  nat_dof(NULL), nat_dof_local(false), netflux(false)
841  {}
843 
844  /// Specify dofs on the essential boundary.
845  /** If @a loc is false, it is a list of true dofs in local ordering.
846  If @a loc is true, it is a marker for Vdofs in local ordering. */
847  void SetEssBdrDofs(const Array<int> *essdofs, bool loc = false)
848  {
849  ess_dof = essdofs;
850  ess_dof_local = loc;
851  }
852  /// Specify dofs on the natural boundary.
853  /** If @a loc is false, it is a list of true dofs in local ordering.
854  If @a loc is true, it is a marker for Vdofs in local ordering. */
855  void SetNatBdrDofs(const Array<int> *natdofs, bool loc = false)
856  {
857  nat_dof = natdofs;
858  nat_dof_local = loc;
859  }
860  /// Setup BDDC with no-net-flux local solvers. Needs a ParFiniteElementSpace attached
861  void SetComputeNetFlux(bool net = true)
862  {
863  netflux = net;
864  }
865 };
866 
867 
869 {
870 private:
871  void BDDCSolverConstructor(const PetscBDDCSolverParams &opts);
872 
873 public:
874  PetscBDDCSolver(MPI_Comm comm, Operator &op,
876  const std::string &prefix = std::string());
879  const std::string &prefix = std::string());
880 };
881 
882 
884 {
885 public:
886  PetscFieldSplitSolver(MPI_Comm comm, Operator &op,
887  const std::string &prefix = std::string());
888 };
889 
891 {
892 private:
893  void H2SolverConstructor(ParFiniteElementSpace *fes);
894 
895 public:
898  const std::string &prefix = std::string());
899 
900 };
901 
902 /// Abstract class for PETSc's nonlinear solvers.
904 {
905 public:
906  PetscNonlinearSolver(MPI_Comm comm,
907  const std::string &prefix = std::string());
908  PetscNonlinearSolver(MPI_Comm comm, Operator &op,
909  const std::string &prefix = std::string());
910  virtual ~PetscNonlinearSolver();
911 
912  /// Specification of the nonlinear operator.
913  virtual void SetOperator(const Operator &op);
914 
915  /// Specifies the desired format of the Jacobian in case a PetscParMatrix
916  /// is not returned by the GetGradient method.
917  void SetJacobianType(Operator::Type type);
918 
919  /// Application of the solver.
920  virtual void Mult(const Vector &b, Vector &x) const;
921 
922  /// Specification of an objective function to be used for line search.
923  void SetObjective(void (*obj)(Operator* op, const Vector &x, double *f));
924 
925  /// User-defined routine to be applied after a successful line search step.
926  /// The user can change the current direction Y and/or the updated solution W
927  /// (with W = X - lambda * Y) but not the previous solution X.
928  /// If Y or W have been changed, the corresponding booleans need to updated.
929  void SetPostCheck(void (*post)(Operator *op, const Vector &X, Vector &Y,
930  Vector &W, bool &changed_y, bool &changed_w));
931 
932  /// General purpose update function to be called at the beginning of each step
933  /// it is the current nonlinear iteration number
934  /// F is the current function value, X the current solution
935  /// D the previous step taken, and P the previous solution
936  void SetUpdate(void (*update)(Operator *op, int it,
937  const Vector& F, const Vector& X,
938  const Vector& D, const Vector& P));
939 
940  /// Conversion function to PETSc's SNES type.
941  operator petsc::SNES() const { return (petsc::SNES)obj; }
942 };
943 
944 
945 /// Abstract class for PETSc's ODE solvers.
946 class PetscODESolver : public PetscSolver, public ODESolver
947 {
948 public:
949  /// The type of the ODE. Use ODE_SOLVER_LINEAR if the Jacobians
950  /// are linear and independent of time.
951  enum Type
952  {
955  };
956 
957  PetscODESolver(MPI_Comm comm, const std::string &prefix = std::string());
958  virtual ~PetscODESolver();
959 
960  /// Initialize the ODE solver.
961  virtual void Init(TimeDependentOperator &f_,
962  enum PetscODESolver::Type type);
964 
967 
968  /// Specifies the desired format of the Jacobian in case a PetscParMatrix
969  /// is not returned by the GetGradient methods
970  void SetJacobianType(Operator::Type type);
971 
972  virtual void Step(Vector &x, double &t, double &dt);
973  virtual void Run(Vector &x, double &t, double &dt, double t_final);
974 
975  /// Conversion function to PETSc's TS type.
976  operator petsc::TS() const { return (petsc::TS)obj; }
977 };
978 
979 
980 /// Abstract class for monitoring PETSc's solvers.
982 {
983 public:
984  bool mon_sol;
985  bool mon_res;
986  PetscSolverMonitor(bool monitor_sol = false, bool monitor_res = true)
987  : mon_sol(monitor_sol), mon_res(monitor_res) {}
988  virtual ~PetscSolverMonitor() {}
989 
990  /// Monitor the solution vector x
991  virtual void MonitorSolution(PetscInt it, PetscReal norm, const Vector &x)
992  {
993  MFEM_ABORT("MonitorSolution() is not implemented!")
994  }
995 
996  /// Monitor the residual vector r
997  virtual void MonitorResidual(PetscInt it, PetscReal norm, const Vector &r)
998  {
999  MFEM_ABORT("MonitorResidual() is not implemented!")
1000  }
1001 
1002  /// Generic monitor to take access to the solver
1003  virtual void MonitorSolver(PetscSolver* solver) {}
1004 };
1005 
1006 
1007 } // namespace mfem
1008 
1009 #endif // MFEM_USE_MPI
1010 #endif // MFEM_USE_PETSC
1011 
1012 #endif
const double * GetDevicePointer() const
Definition: petsc.cpp:256
void SetPreconditioner(Solver &precond)
Definition: petsc.cpp:3107
void SetPrintLevel(int plev)
Definition: petsc.cpp:2450
virtual void Mult(const Vector &b, Vector &x) const
Application of the solver.
Definition: petsc.cpp:3187
PetscParVector * B
Right-hand side and solution vector.
Definition: petsc.hpp:685
void CreatePrivateContext()
Definition: petsc.cpp:2749
void trans(const Vector &u, Vector &x)
Definition: ex27.cpp:412
void EliminateBC(const HypreParMatrix &A, const HypreParMatrix &Ae, const Array< int > &ess_dof_list, const Vector &X, Vector &B)
Eliminate essential BC specified by ess_dof_list from the solution X to the r.h.s. B.
Definition: hypre.cpp:3258
void SetUpdate(void(*update)(Operator *op, int it, const Vector &F, const Vector &X, const Vector &D, const Vector &P))
Definition: petsc.cpp:4098
virtual void SetOperator(const Operator &op)
Specification of the nonlinear operator.
Definition: petsc.cpp:4005
Abstract class for PETSc&#39;s preconditioners.
Definition: petsc.hpp:801
PetscParMatrix & operator+=(const PetscParMatrix &B)
Definition: petsc.cpp:1160
double PetscScalar
Definition: petsc.hpp:48
PetscMemory pdata
Definition: petsc.hpp:159
petsc::Vec x
The actual PETSc object.
Definition: petsc.hpp:157
virtual void SetOperator(const Operator &op)
Definition: petsc.cpp:2962
bool clcustom
Boolean to handle SetFromOptions calls.
Definition: petsc.hpp:676
void SetObjective(void(*obj)(Operator *op, const Vector &x, double *f))
Specification of an objective function to be used for line search.
Definition: petsc.cpp:4073
HYPRE_Int PetscInt
Definition: petsc.hpp:47
double GetFinalNorm()
Definition: petsc.cpp:2724
PetscODESolver::Type GetType() const
Definition: petsc.cpp:4256
void MakeAliasForSync(Memory< double > &base_, int offset_, int size_, bool read_, bool write_, bool usedev_)
Definition: petsc.hpp:109
petsc::Mat A
The actual PETSc object.
Definition: petsc.hpp:319
Wrapper for PETSc&#39;s matrix class.
Definition: petsc.hpp:315
PetscParVector & operator+=(const PetscParVector &y)
Definition: petsc.cpp:766
const Array< int > * nat_dof
Definition: petsc.hpp:833
void ApplyBC(const Vector &x, Vector &y)
y = x on ess_tdof_list_c and y = g (internally evaluated) on ess_tdof_list
Definition: petsc.cpp:2832
Abstract class for PETSc&#39;s linear solvers.
Definition: petsc.hpp:746
Abstract class for PETSc&#39;s solvers.
Definition: petsc.hpp:672
virtual void Run(Vector &x, double &t, double &dt, double t_final)
Perform time integration from time t [in] to time tf [in].
Definition: petsc.cpp:4318
void SetPostCheck(void(*post)(Operator *op, const Vector &X, Vector &Y, Vector &W, bool &changed_y, bool &changed_w))
Definition: petsc.cpp:4084
PetscBCHandler(Type type_=ZERO)
Definition: petsc.hpp:598
ParFiniteElementSpace * fespace
Definition: petsc.hpp:830
Base abstract class for first order time dependent operators.
Definition: operator.hpp:316
void MakeAliasForSync(const Memory< double > &base_, int offset_, int size_, bool usedev_)
Definition: petsc.hpp:99
void SetType(PetscODESolver::Type)
Definition: petsc.cpp:4262
void ConvertOperator(MPI_Comm comm, const Operator &op, petsc::Mat *B, Operator::Type tid)
Definition: petsc.cpp:1376
virtual void MonitorSolver(PetscSolver *solver)
Generic monitor to take access to the solver.
Definition: petsc.hpp:1003
Pointer to an Operator of a specified type.
Definition: handle.hpp:33
PetscBDDCSolver(MPI_Comm comm, Operator &op, const PetscBDDCSolverParams &opts=PetscBDDCSolverParams(), const std::string &prefix=std::string())
Definition: petsc.cpp:3876
PetscParVector & AddValues(const Array< PetscInt > &, const Array< PetscScalar > &)
Add values in a vector.
Definition: petsc.cpp:745
virtual ~PetscSolver()
Destroy the PetscParVectors allocated (if any).
Definition: petsc.cpp:2361
PetscNonlinearSolver(MPI_Comm comm, const std::string &prefix=std::string())
Definition: petsc.cpp:3965
PetscParVector * GetX() const
Returns the inner vector in the domain of A (it creates it if needed)
Definition: petsc.cpp:1947
void SetBlockSize(PetscInt rbs, PetscInt cbs=-1)
Set row and column block sizes of a matrix.
Definition: petsc.cpp:1025
virtual void Step(Vector &x, double &t, double &dt)
Perform a time step from time t [in] to time t [out] based on the requested step size dt [in]...
Definition: petsc.cpp:4280
struct ::_p_Mat * Mat
Definition: petsc.hpp:70
Abstract class for solving systems of ODEs: dx/dt = f(x,t)
Definition: ode.hpp:23
void Init()
Initialize with defaults. Does not initialize inherited members.
Definition: petsc.cpp:1031
PetscPreconditioner(MPI_Comm comm, const std::string &prefix=std::string())
Definition: petsc.cpp:3239
PetscClassId cid
The class id of the actual PETSc object.
Definition: petsc.hpp:682
void MultTranspose(double a, const Vector &x, double b, Vector &y) const
Matvec transpose: y = a A^T x + b y.
Definition: petsc.cpp:2003
PetscInt GetRowStart() const
Returns the global index of the first local row.
Definition: petsc.cpp:976
virtual void Mult(const Vector &b, Vector &x) const
Application of the solver.
Definition: petsc.cpp:4111
void SetDataAndSize_()
Definition: petsc.cpp:267
Abstract parallel finite element space.
Definition: pfespace.hpp:28
Constant in time b.c.
Definition: petsc.hpp:594
void SetRelTol(double tol)
Definition: petsc.cpp:2373
STL namespace.
PetscInt M() const
Returns the global number of rows.
Definition: petsc.cpp:1004
Wrapper for syncing PETSc&#39;s vector memory.
Definition: petsc.hpp:84
void Randomize(PetscInt seed=0)
Set random values.
Definition: petsc.cpp:934
void Destroy()
Delete all owned data. Does not perform re-initialization with defaults.
Definition: petsc.cpp:1777
void SyncBaseAndReset()
Definition: petsc.hpp:124
void AddMult(const Vector &x, Vector &y, const double a=1.0) const override
Operator application: y+=A(x) (default) or y+=a*A(x).
Definition: petsc.hpp:450
void SetMonitor(PetscSolverMonitor *ctx)
Sets user-defined monitoring routine.
Definition: petsc.cpp:2535
void SetMaxIter(int max_iter)
Definition: petsc.cpp:2423
MPI_Comm GetComm() const
Get the associated MPI communicator.
Definition: petsc.cpp:1329
Abstract class for PETSc&#39;s ODE solvers.
Definition: petsc.hpp:946
PetscParVector & operator*=(PetscScalar d)
Definition: petsc.cpp:780
HypreParMatrix * ParMult(const HypreParMatrix *A, const HypreParMatrix *B, bool own_matrix)
Definition: hypre.cpp:2853
void EliminateRowsCols(const Array< int > &rows_cols, const PetscParVector &X, PetscParVector &B, double diag=1.)
Eliminate rows and columns from the matrix, and rows from the vector B. Modify B with the BC values i...
Definition: petsc.cpp:2247
double PetscReal
Definition: petsc.hpp:49
const double * HostRead() const override
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).
Definition: petsc.cpp:435
void SyncBase()
Definition: petsc.hpp:119
unsigned flags
Bit flags defined from the FlagMask enum.
int PetscClassId
Definition: petsc.hpp:50
void Print(const char *fname=NULL, bool binary=false) const
Prints the matrix (to stdout if fname is NULL)
Definition: petsc.cpp:2021
PetscParVector * Y
Definition: petsc.hpp:322
virtual ~PetscLinearSolver()
Definition: petsc.cpp:3197
std::function< double(const Vector &)> f(double mass_coeff)
Definition: lor_mms.hpp:30
struct ::_p_PC * PC
Definition: petsc.hpp:72
void ResetArray()
Reset the PETSc Vec object to use its default data. Call this method after the use of PlaceArray()...
Definition: petsc.cpp:799
PetscParVector & SetValues(const Array< PetscInt > &, const Array< PetscScalar > &)
Set values in a vector.
Definition: petsc.cpp:731
MPI_Comm GetComm() const
Get the associated MPI communicator.
Definition: petsc.cpp:2530
double * Write(bool=true) override
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).
Definition: petsc.cpp:440
Type GetType() const
Definition: petsc.cpp:2303
virtual void Init(TimeDependentOperator &f_, enum PetscODESolver::Type type)
Initialize the ODE solver.
Definition: petsc.cpp:4167
PetscParMatrix * Transpose(bool action=false)
Returns the transpose of the PetscParMatrix.
Definition: petsc.cpp:1967
void write(std::ostream &os, T value)
Write &#39;value&#39; to stream.
Definition: binaryio.hpp:30
PetscInt GetGlobalNumRows() const
Returns the global number of rows.
Definition: petsc.hpp:486
PetscObject obj
The actual PETSc object (KSP, PC, SNES or TS).
Definition: petsc.hpp:679
Abstract class for monitoring PETSc&#39;s solvers.
Definition: petsc.hpp:981
bool DeviceRequested() const
Definition: petsc.hpp:140
Data type sparse matrix.
Definition: sparsemat.hpp:50
void Sync(const Memory &other) const
Copy the host/device pointer validity flags from other to *this.
PetscLinearSolver(MPI_Comm comm, const std::string &prefix=std::string(), bool wrap=true, bool iter_mode=false)
Definition: petsc.cpp:2921
void SetJacobianType(Operator::Type type)
Definition: petsc.cpp:4067
struct ::_p_TS * TS
Definition: petsc.hpp:74
void mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
Definition: error.cpp:154
double b
Definition: lissajous.cpp:42
Auxiliary class for BDDC customization.
Definition: petsc.hpp:827
PetscInt GetNumRows() const
Returns the local number of rows.
Definition: petsc.cpp:990
virtual ~PetscPreconditioner()
Definition: petsc.cpp:3370
void FixResidualBC(const Vector &x, Vector &y)
y = x-g on ess_tdof_list, the rest of y is unchanged
Definition: petsc.cpp:2881
bool UseDevice() const override
Return the device flag of the Memory object used by the Vector.
Definition: petsc.cpp:509
void SetType(enum Type type_)
Sets the type of boundary conditions.
Definition: petsc.hpp:609
PetscParMatrix & operator=(const PetscParMatrix &B)
Definition: petsc.cpp:1144
virtual ~PetscParVector()
Calls PETSc&#39;s destroy function.
Definition: petsc.cpp:596
PetscFieldSplitSolver(MPI_Comm comm, Operator &op, const std::string &prefix=std::string())
Definition: petsc.cpp:3885
PetscBCHandler * bchandler
Handler for boundary conditions.
Definition: petsc.hpp:688
PetscParMatrix & operator-=(const PetscParMatrix &B)
Definition: petsc.cpp:1175
struct ::_p_Vec * Vec
Definition: petsc.hpp:69
virtual Solver * NewPreconditioner(const OperatorHandle &oh)=0
double * ReadWrite(bool=true) override
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).
Definition: petsc.cpp:470
void ScaleRows(const Vector &s)
Scale the local row i by s(i).
Definition: petsc.cpp:2046
void Zero(Vector &x)
Replace boundary dofs with 0.
Definition: petsc.cpp:2900
T read(std::istream &is)
Read a value from the stream and return it.
Definition: binaryio.hpp:37
virtual void MultTranspose(const Vector &b, Vector &x) const
Action of the transpose operator: y=A^t(x). The default behavior in class Operator is to generate an ...
Definition: petsc.cpp:3365
void Shift(double s)
Shift diagonal by a constant.
Definition: petsc.cpp:2068
PetscInt GlobalSize() const
Returns the global number of rows.
Definition: petsc.cpp:517
PetscParVector * GetY() const
Returns the inner vector in the range of A (it creates it if needed)
Definition: petsc.cpp:1957
double * HostReadWrite() override
Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).
Definition: petsc.cpp:495
void RAP(const DenseMatrix &A, const DenseMatrix &P, DenseMatrix &RAP)
Definition: densemat.cpp:3232
void UpdateVecFromFlags()
Update PETSc&#39;s Vec after having accessed its data via GetMemory()
Definition: petsc.cpp:357
void SetTime(double t)
Sets the current time.
Definition: petsc.hpp:624
ID for class PetscParMatrix, MATSHELL format.
Definition: operator.hpp:290
void Reset()
Reset the memory to be empty, ensuring that Delete() will be a no-op.
void SetMat(petsc::Mat newA)
Replace the inner Mat Object. The reference count of newA is increased.
Definition: petsc.cpp:1802
PetscParMatrix()
Create an empty matrix to be used as a reference to an existing matrix.
Definition: petsc.cpp:1038
void MakeWrapper(MPI_Comm comm, const Operator *op, petsc::Mat *B)
Creates a wrapper around a mfem::Operator op using PETSc&#39;s MATSHELL object and returns the Mat in B...
Definition: petsc.cpp:1342
void AddMultTranspose(const Vector &x, Vector &y, const double a=1.0) const override
Operator transpose application: y+=A^t(x) (default) or y+=a*A^t(x).
Definition: petsc.hpp:454
Abstract class for PETSc&#39;s nonlinear solvers.
Definition: petsc.hpp:903
int GetNumIterations()
Definition: petsc.cpp:2691
void Mult(double a, const Vector &x, double b, Vector &y) const
Matvec: y = a A x + b y.
Definition: petsc.cpp:1986
void SetBCHandler(PetscBCHandler *bch)
Sets the object to handle essential boundary conditions.
Definition: petsc.cpp:2565
struct ::_p_KSP * KSP
Definition: petsc.hpp:71
Wrapper for hypre&#39;s parallel vector class.
Definition: hypre.hpp:161
void SetBlockSize(PetscInt bs)
Set block size of a vector.
Definition: petsc.cpp:524
void SetDeviceInvalid() const
Definition: petsc.hpp:96
void SetHostValid() const
Definition: petsc.hpp:93
const double * Read(bool=true) const override
Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).
Definition: petsc.cpp:411
void ZeroBC(const Vector &x, Vector &y)
y = x on ess_tdof_list_c and y = 0 on ess_tdof_list
Definition: petsc.cpp:2909
virtual void Eval(double t, Vector &g)
Boundary conditions evaluation.
Definition: petsc.hpp:614
Type
Enumeration defining IDs for some classes derived from Operator.
Definition: operator.hpp:283
struct ::_p_SNES * SNES
Definition: petsc.hpp:73
PetscPreconditionerFactory(const std::string &name_="MFEM Factory")
Definition: petsc.hpp:661
PetscInt N() const
Returns the global number of columns.
Definition: petsc.cpp:1011
void SetDeviceValid() const
Definition: petsc.hpp:94
void SetFlagsFromMask_() const
Definition: petsc.cpp:323
PetscInt GetNumCols() const
Returns the local number of columns.
Definition: petsc.cpp:997
virtual void Init(TimeDependentOperator &f_)
Associate a TimeDependentOperator with the ODE solver.
Definition: petsc.hpp:963
void SetJacobianType(Operator::Type type)
Definition: petsc.cpp:4250
double * HostWrite() override
Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).
Definition: petsc.cpp:465
bool operatorset
Boolean to handle SetOperator calls.
Definition: petsc.hpp:694
PetscParVector * X
Auxiliary vectors for typecasting.
Definition: petsc.hpp:322
void MakeAlias(const Memory &base, int offset, int size)
Create a memory object that points inside the memory object base.
Pointer is an alias.
PetscInt NNZ() const
Returns the number of nonzeros.
Definition: petsc.cpp:1018
PetscParVector & operator-=(const PetscParVector &y)
Definition: petsc.cpp:773
Type GetType() const
Returns the type of boundary conditions.
Definition: petsc.hpp:606
virtual ~PetscSolverMonitor()
Definition: petsc.hpp:988
double a
Definition: lissajous.cpp:41
void MFEMFinalizePetsc()
Definition: petsc.cpp:241
virtual void Mult(const Vector &b, Vector &x) const
Application of the preconditioner.
Definition: petsc.cpp:3360
const double * GetHostPointer() const
Definition: petsc.cpp:247
virtual ~PetscNonlinearSolver()
Definition: petsc.cpp:3997
void SetComputeNetFlux(bool net=true)
Setup BDDC with no-net-flux local solvers. Needs a ParFiniteElementSpace attached.
Definition: petsc.hpp:861
virtual ~PetscODESolver()
Definition: petsc.cpp:4159
virtual ~PetscBCHandler()
Definition: petsc.hpp:603
int GetConverged()
Definition: petsc.cpp:2658
PetscSolver()
Construct an empty PetscSolver. Initialize protected objects to NULL.
Definition: petsc.cpp:2351
void SetHostInvalid() const
Definition: petsc.hpp:95
void SetPreconditionerFactory(PetscPreconditionerFactory *factory)
Sets the object for the creation of the preconditioner.
Definition: petsc.cpp:2584
void MFEMInitializePetsc()
Convenience functions to initialize/finalize PETSc.
Definition: petsc.cpp:201
petsc::Mat ReleaseMat(bool dereference)
Release the PETSc Mat object. If dereference is true, decrement the refcount of the Mat object...
Definition: petsc.cpp:2290
PetscParVector(MPI_Comm comm, PetscInt glob_size, PetscInt *col=NULL)
Creates vector with given global size and partitioning of the columns.
Definition: petsc.cpp:578
virtual void MonitorSolution(PetscInt it, PetscReal norm, const Vector &x)
Monitor the solution vector x.
Definition: petsc.hpp:991
ID for class PetscParMatrix, MATAIJ format.
Definition: operator.hpp:288
virtual void SetOperator(const Operator &op)
Set/update the solver for the given operator.
Definition: petsc.cpp:3274
void SetSpace(ParFiniteElementSpace *fe)
Definition: petsc.hpp:842
PetscParVector * X
Definition: petsc.hpp:685
const Array< int > * ess_dof
Definition: petsc.hpp:831
struct _p_PetscObject * PetscObject
Definition: petsc.hpp:51
PetscH2Solver(Operator &op, ParFiniteElementSpace *fes, const std::string &prefix=std::string())
Definition: petsc.cpp:3920
void ResetMemory()
Completes the operation started with PlaceMemory.
Definition: petsc.cpp:890
void SetAbsTol(double tol)
Definition: petsc.cpp:2398
Helper class for handling essential boundary conditions.
Definition: petsc.hpp:588
Class used by MFEM to store pointers to host and/or device memory.
Array< int > & GetTDofs()
Gets essential dofs (local, per-process numbering)
Definition: petsc.hpp:621
void SetTol(double tol)
Definition: petsc.cpp:2368
void Customize(bool customize=true) const
Customize object with options set.
Definition: petsc.cpp:2625
PetscParVector & operator=(PetscScalar d)
Set constant values.
Definition: petsc.cpp:724
PetscSolverMonitor(bool monitor_sol=false, bool monitor_res=true)
Definition: petsc.hpp:986
RefCoord t[3]
virtual void MonitorResidual(PetscInt it, PetscReal norm, const Vector &r)
Monitor the residual vector r.
Definition: petsc.hpp:997
bool WriteRequested() const
Definition: petsc.hpp:135
PetscParMatrix * TripleMatrixProduct(PetscParMatrix *R, PetscParMatrix *A, PetscParMatrix *P)
Returns the matrix R * A * P.
Definition: petsc.cpp:2087
Vector data type.
Definition: vector.hpp:58
void PlaceArray(PetscScalar *temp_data)
Temporarily replace the data of the PETSc Vec object. To return to the original data array...
Definition: petsc.cpp:794
bool IsAliasForSync() const
Definition: petsc.hpp:97
void FreePrivateContext()
Definition: petsc.cpp:2781
RefCoord s[3]
PetscODESolver(MPI_Comm comm, const std::string &prefix=std::string())
Definition: petsc.cpp:4134
bool ReadRequested() const
Definition: petsc.hpp:130
void EliminateRows(const Array< int > &rows)
Eliminate only the rows from the matrix.
Definition: petsc.cpp:2276
Base class for solvers.
Definition: operator.hpp:682
virtual void MultTranspose(const Vector &b, Vector &x) const
Action of the transpose operator: y=A^t(x). The default behavior in class Operator is to generate an ...
Definition: petsc.cpp:3192
PetscPCGSolver(MPI_Comm comm, const std::string &prefix=std::string(), bool iter_mode=false)
Definition: petsc.cpp:3207
void SetNatBdrDofs(const Array< int > *natdofs, bool loc=false)
Specify dofs on the natural boundary.
Definition: petsc.hpp:855
Abstract operator.
Definition: operator.hpp:24
Wrapper for hypre&#39;s ParCSR matrix class.
Definition: hypre.hpp:343
void Mult(const Vector &x, Vector &y) const override
Operator application: y=A(x).
Definition: petsc.hpp:444
void Print(const char *fname=NULL, bool binary=false) const
Prints the vector (to stdout if fname is NULL)
Definition: petsc.cpp:949
void operator*=(double s)
Scale all entries by s: A_scaled = s*A.
Definition: petsc.cpp:1981
void MultTranspose(const Vector &x, Vector &y) const override
Action of the transpose operator: y=A^t(x). The default behavior in class Operator is to generate an ...
Definition: petsc.hpp:447
void SetEssBdrDofs(const Array< int > *essdofs, bool loc=false)
Specify dofs on the essential boundary.
Definition: petsc.hpp:847
void SetTDofs(Array< int > &list)
Sets essential dofs (local, per-process numbering)
Definition: petsc.cpp:2809
Vector * GlobalVector() const
Returns the global vector in each processor.
Definition: petsc.cpp:699
PetscInt GetGlobalNumCols() const
Returns the global number of columns.
Definition: petsc.hpp:489
PetscInt GetColStart() const
Returns the global index of the first local column.
Definition: petsc.cpp:983
void PlaceMemory(Memory< double > &, bool=false)
This requests write access from where the memory is valid and temporarily replaces the corresponding ...
Definition: petsc.cpp:804
MPI_Comm GetComm() const
Get the associated MPI communicator.
Definition: petsc.cpp:694
void * private_ctx
Private context for solver.
Definition: petsc.hpp:691
void SetUp(PetscInt n)
SetUp the helper object, where n is the size of the solution vector.
Definition: petsc.cpp:2816
void MakeRef(const PetscParMatrix &master)
Makes this object a reference to another PetscParMatrix.
Definition: petsc.cpp:1937
void ScaleCols(const Vector &s)
Scale the local col i by s(i).
Definition: petsc.cpp:2057
virtual ~PetscParMatrix()
Calls PETSc&#39;s destroy function.
Definition: petsc.hpp:425