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