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