preCICE
Loading...
Searching...
No Matches
Petsc.cpp
Go to the documentation of this file.
1#include "Petsc.hpp"
2
3// A logger is always required
4#include "logging/Logger.hpp"
5
6#ifndef PRECICE_NO_PETSC
7#include <memory>
8#include <mpi.h>
9#include <numeric>
10#include <sstream>
11#include <type_traits>
12#include <utility>
13#include <vector>
14
15#include "logging/LogMacros.hpp"
16#include "petsc.h"
17#include "petscdrawtypes.h"
18#include "petscis.h"
19#include "petscksp.h"
20#include "petscsystypes.h"
21#include "petscvec.h"
22#include "petscviewertypes.h"
23#include "precice/impl/versions.hpp"
24#include "utils/Parallel.hpp"
25#include "utils/assertion.hpp"
26
27#endif // not PRECICE_NO_PETSC
28
29namespace precice::utils {
30
31precice::logging::Logger precice::utils::Petsc::_log("utils::Petsc");
32
33#ifndef PRECICE_NO_PETSC
34precice::logging::Logger precice::utils::petsc::Vector::_log("utils::Petsc::Vector");
35#endif // not PRECICE_NO_PETSC
36
37bool Petsc::weInitialized = false;
38
40{
42#ifndef PRECICE_NO_PETSC
43 PetscBool petscIsInitialized;
44 PetscInitialized(&petscIsInitialized);
45 if (not petscIsInitialized) {
46 PETSC_COMM_WORLD = comm;
47 // Disable the default signal handler
48 PetscOptionsSetValue(nullptr, "-no_signal_handler", nullptr);
49 PetscErrorCode ierr;
50 int argc = 0;
51 char **argv = nullptr;
52 ierr = PetscInitialize(&argc, &argv, "", nullptr);
53 CHKERRV(ierr);
54 weInitialized = true;
55 }
56#endif // not PRECICE_NO_PETSC
57}
58
60{
61#ifndef PRECICE_NO_PETSC
62 if (!weInitialized) {
63 return;
64 }
65 PetscBool petscIsInitialized;
66 PetscInitialized(&petscIsInitialized);
67 if (petscIsInitialized) {
68 PetscOptionsSetValue(nullptr, "-options_left", "no");
69 PetscFinalize();
70 }
71#endif // not PRECICE_NO_PETSC
72}
73} // namespace precice::utils
74
75#ifndef PRECICE_NO_PETSC
76
77#include <random>
78#include <string>
79#include "petscdraw.h"
80#include "petscviewer.h"
81
83
84struct Viewer {
85 Viewer(const std::string &filename, VIEWERFORMAT format, MPI_Comm comm)
86 {
87 PetscErrorCode ierr = 0;
88 if (format == ASCII) {
89 ierr = PetscViewerASCIIOpen(comm, filename.c_str(), &viewer);
90 CHKERRV(ierr);
91 } else if (format == BINARY) {
92 ierr = PetscViewerBinaryOpen(comm, filename.c_str(), FILE_MODE_WRITE, &viewer);
93 CHKERRV(ierr);
94 pushFormat(PETSC_VIEWER_NATIVE);
95 }
96 }
97
98 Viewer(PetscViewerType type, MPI_Comm comm)
99 {
100 PetscErrorCode ierr = 0;
101 ierr = PetscViewerCreate(comm, &viewer);
102 CHKERRV(ierr);
103 ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);
104 CHKERRV(ierr);
105 }
106
108 {
109 while (popformats--) {
110 PetscViewerPopFormat(viewer);
111 }
112 PetscViewerDestroy(&viewer);
113 }
114
115 void pushFormat(PetscViewerFormat format)
116 {
117 auto ierr = PetscViewerPushFormat(viewer, format);
118 CHKERRV(ierr);
119 popformats++;
120 }
121
123 PetscViewer viewer{nullptr};
124};
125
126template <class T>
128{
129 MPI_Comm comm;
130 PetscObjectGetComm(reinterpret_cast<PetscObject>(obj), &comm);
131 return comm;
132}
133
134template <class T>
135void setName(T obj, const std::string &name)
136{
137 PetscErrorCode ierr = 0;
138 ierr = PetscObjectSetName(reinterpret_cast<PetscObject>(obj), name.c_str());
139 CHKERRV(ierr);
140}
141
142template <class T>
143std::string getName(T obj)
144{
145 const char *cstr;
146 PetscObjectGetName(reinterpret_cast<PetscObject>(obj), &cstr);
147 return cstr;
148}
149
151
153{
154 PetscErrorCode ierr = 0;
155 ierr = VecDuplicate(v.vector, &vector);
156 CHKERRV(ierr);
157 ierr = VecCopy(v.vector, vector);
158 CHKERRV(ierr);
160}
161
163{
164 Vector tmp{other};
165 swap(tmp);
166 return *this;
167}
168
169Vector::Vector(Vector &&other) noexcept
170{
171 vector = other.vector;
172 other.vector = nullptr;
173}
174
176{
177 swap(other);
178 return *this;
179}
180
181Vector::Vector(const std::string &name)
182{
183 int size;
185 PetscErrorCode ierr = 0;
186 ierr = VecCreate(utils::Parallel::current()->comm, &vector);
187 CHKERRV(ierr);
188 setName(vector, name);
189}
190
191Vector::Vector(Vec &v, const std::string &name)
192 : vector(v)
193{
194 setName(vector, name);
195}
196
198{
199 PetscErrorCode ierr = 0;
200 PetscBool petscIsInitialized;
201 PetscInitialized(&petscIsInitialized);
202 if (petscIsInitialized && vector) // If PetscFinalize is called before ~Vector
203 ierr = VecDestroy(&vector);
204 CHKERRV(ierr);
205}
206
207Vector Vector::allocate(const std::string &name)
208{
209 return Vector{name};
210}
211
212Vector Vector::allocate(Vector &other, const std::string &name)
213{
214 return allocate(other.vector, name);
215}
216
217Vector Vector::allocate(Vec &other, const std::string &name)
218{
219 Vec newvector;
220 PetscErrorCode ierr = 0;
221 ierr = VecDuplicate(other, &newvector);
222 [&] { CHKERRV(ierr); }();
223 return Vector{newvector, name};
224}
225
226Vector Vector::allocate(Matrix &m, const std::string &name, LEFTRIGHT type)
227{
228 return allocate(m.matrix, name, type);
229}
230
231Vector Vector::allocate(Mat &m, const std::string &name, LEFTRIGHT type)
232{
233 Vec newvector;
234 // MatGetVecs is deprecated, we keep it due to the old PETSc version at the SuperMUC.
235 PetscErrorCode ierr = 0;
236 if (type == LEFTRIGHT::LEFT) {
237 ierr = MatCreateVecs(m, nullptr, &newvector); // a vector with the same number of rows
238 } else {
239 ierr = MatCreateVecs(m, &newvector, nullptr); // a vector with the same number of cols
240 }
241 [&] { CHKERRV(ierr); }();
242 return Vector{newvector, name};
243}
244
245void Vector::swap(Vector &other) noexcept
246{
247 using std::swap;
248 swap(vector, other.vector);
249}
250
251Vector::operator Vec &()
252{
253 return vector;
254}
255
256void Vector::init(PetscInt rows)
257{
258 PetscErrorCode ierr = 0;
259 ierr = VecSetSizes(vector, PETSC_DECIDE, rows);
260 CHKERRV(ierr);
261 ierr = VecSetFromOptions(vector);
262 CHKERRV(ierr);
263}
264
265PetscInt Vector::getSize() const
266{
267 PetscErrorCode ierr = 0;
268 PetscInt size;
269 ierr = VecGetSize(vector, &size);
270 CHKERRQ(ierr);
271 return size;
272}
273
274PetscInt Vector::getLocalSize() const
275{
276 PetscErrorCode ierr = 0;
277 PetscInt size;
278 ierr = VecGetLocalSize(vector, &size);
279 CHKERRQ(ierr);
280 return size;
281}
282
283void Vector::setValue(PetscInt row, PetscScalar value)
284{
285 PetscErrorCode ierr = 0;
286 ierr = VecSetValue(vector, row, value, INSERT_VALUES);
287 CHKERRV(ierr);
288}
289
290void Vector::arange(double start, double stop)
291{
292 PetscErrorCode ierr = 0;
293 PetscScalar *a;
294 PetscInt range_start, range_end, size;
295 VecGetSize(vector, &size);
296 VecGetOwnershipRange(vector, &range_start, &range_end);
297 double step_size = (stop - start) / size;
298 ierr = VecGetArray(vector, &a);
299 CHKERRV(ierr);
300 for (PetscInt i = range_start; i < range_end; i++) {
301 a[i - range_start] = (i + start) * step_size;
302 }
303 VecRestoreArray(vector, &a);
304}
305
307{
308 PetscErrorCode ierr = 0;
309 PetscRandom rctx;
310
311 std::random_device rd;
312 std::uniform_real_distribution<double> dist(0, 1);
313
314 PetscRandomCreate(getCommunicator(vector), &rctx);
315 PetscRandomSetType(rctx, PETSCRAND48);
316 PetscRandomSetSeed(rctx, dist(rd));
317 PetscRandomSeed(rctx);
318 ierr = VecSetRandom(vector, rctx);
319 CHKERRV(ierr);
320 PetscRandomDestroy(&rctx);
321}
322
324{
325 // This is collective, so we can only skip if the global size is 0
326 if (getSize() == 0) {
327 return *this;
328 }
329 PRECICE_ASSERT(static_cast<PetscInt>(source.size()) == getLocalSize());
330 PetscScalar *data;
331 PetscErrorCode ierr = 0;
332 ierr = VecGetArray(vector, &data);
333 PRECICE_ASSERT(ierr == 0);
334 std::copy(source.begin(), source.end(), data);
335 ierr = VecRestoreArray(vector, &data);
336 PRECICE_ASSERT(ierr == 0);
337 return *this;
338}
339
341{
342 // This is collective, so we can only skip if the global size is 0
343 if (getSize() == 0) {
344 return *this;
345 }
346 auto localSize = getLocalSize();
347 PRECICE_ASSERT(static_cast<PetscInt>(destination.size()) == localSize);
348 PetscScalar *data;
349 PetscErrorCode ierr = 0;
350 ierr = VecGetArray(vector, &data);
351 PRECICE_ASSERT(ierr == 0);
352 auto dataEnd = std::next(data, localSize);
353 std::copy(data, dataEnd, destination.begin());
354 ierr = VecRestoreArray(vector, &data);
355 PRECICE_ASSERT(ierr == 0);
356 return *this;
357}
358
360{
361 PetscErrorCode ierr = 0;
362 PetscInt size;
363 PetscReal *a;
364 ierr = VecGetArray(vector, &a);
365 CHKERRV(ierr);
366 ierr = VecGetSize(vector, &size);
367 CHKERRV(ierr);
368 ierr = PetscSortReal(size, a);
369 CHKERRV(ierr);
370 ierr = VecRestoreArray(vector, &a);
371 CHKERRV(ierr);
372}
373
375{
376 PetscErrorCode ierr = 0;
377 ierr = VecAssemblyBegin(vector);
378 CHKERRV(ierr);
379 ierr = VecAssemblyEnd(vector);
380 CHKERRV(ierr);
381}
382
383std::pair<PetscInt, PetscInt> Vector::ownerRange() const
384{
385 PetscInt range_start, range_end;
386 VecGetOwnershipRange(vector, &range_start, &range_end);
387 return std::make_pair(range_start, range_end);
388}
389
390void Vector::write(const std::string &filename, VIEWERFORMAT format) const
391{
392 Viewer viewer{filename, format, getCommunicator(vector)};
393 VecView(vector, viewer.viewer);
394}
395
396double Vector::l2norm() const
397{
398 PetscReal val;
399 VecNorm(vector, NORM_2, &val);
400 return val;
401}
402
403void Vector::read(const std::string &filename, VIEWERFORMAT format)
404{
405 Viewer viewer{filename, format, getCommunicator(vector)};
406 VecLoad(vector, viewer.viewer);
407}
408
409void Vector::view() const
410{
411 PetscErrorCode ierr;
412 ierr = VecView(vector, PETSC_VIEWER_STDOUT_WORLD);
413 CHKERRV(ierr);
414}
415
416void swap(Vector &lhs, Vector &rhs) noexcept
417{
418 lhs.swap(rhs);
419}
420
422
423Matrix::Matrix(std::string name)
424{
425 PetscErrorCode ierr = 0;
426 ierr = MatCreate(utils::Parallel::current()->comm, &matrix);
427 CHKERRV(ierr);
428 setName(matrix, std::move(name));
429}
430
432{
433 PetscErrorCode ierr = 0;
434 PetscBool petscIsInitialized;
435 PetscInitialized(&petscIsInitialized);
436 if (petscIsInitialized && matrix) // If PetscFinalize is called before ~Matrix
437 ierr = MatDestroy(&matrix);
438 CHKERRV(ierr);
439}
440
441Matrix::operator Mat &()
442{
443 return matrix;
444}
445
446void Matrix::assemble(MatAssemblyType type)
447{
448 PetscErrorCode ierr = 0;
449 ierr = MatAssemblyBegin(matrix, type);
450 CHKERRV(ierr);
451 ierr = MatAssemblyEnd(matrix, type);
452 CHKERRV(ierr);
453}
454
455void Matrix::init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols,
456 MatType type, bool doSetup)
457{
458 PetscErrorCode ierr = 0;
459 if (type != nullptr) {
460 ierr = MatSetType(matrix, type);
461 CHKERRV(ierr);
462 }
463 ierr = MatSetSizes(matrix, localRows, localCols, globalRows, globalCols);
464 CHKERRV(ierr);
465 ierr = MatSetFromOptions(matrix);
466 CHKERRV(ierr);
467 if (doSetup)
468 ierr = MatSetUp(matrix);
469 CHKERRV(ierr);
470}
471
473{
474 PetscErrorCode ierr = 0;
475 std::string name = getName(matrix);
476 ierr = MatDestroy(&matrix);
477 CHKERRV(ierr);
478 ierr = MatCreate(utils::Parallel::current()->comm, &matrix);
479 CHKERRV(ierr);
480 setName(matrix, name);
481}
482
483MatInfo Matrix::getInfo(MatInfoType flag) const
484{
485 MatInfo info;
486 MatGetInfo(matrix, flag, &info);
487 return info;
488}
489
490void Matrix::setValue(PetscInt row, PetscInt col, PetscScalar value)
491{
492 PetscErrorCode ierr = 0;
493 ierr = MatSetValue(matrix, row, col, value, INSERT_VALUES);
494 CHKERRV(ierr);
495}
496
498{
499 PetscErrorCode ierr = 0;
500 PetscRandom rctx;
501
502 std::random_device rd;
503 std::uniform_real_distribution<double> dist(0, 1);
504
505 PetscRandomCreate(getCommunicator(matrix), &rctx);
506 PetscRandomSetType(rctx, PETSCRAND48);
507 PetscRandomSetSeed(rctx, dist(rd));
508 PetscRandomSeed(rctx);
509 ierr = MatSetRandom(matrix, rctx);
510 CHKERRV(ierr);
511 PetscRandomDestroy(&rctx);
512}
513
514void Matrix::setColumn(Vector &v, PetscInt col)
515{
516 PetscErrorCode ierr = 0;
517 const PetscScalar *vec;
518 PetscInt range_start, range_end;
519 VecGetOwnershipRange(v.vector, &range_start, &range_end);
520 std::vector<PetscInt> irow(range_end - range_start);
521 std::iota(irow.begin(), irow.end(), range_start);
522
523 ierr = VecGetArrayRead(v.vector, &vec);
524 CHKERRV(ierr);
525 ierr = MatSetValues(matrix, range_end - range_start, irow.data(), 1, &col, vec, INSERT_VALUES);
526 CHKERRV(ierr);
527 ierr = VecRestoreArrayRead(v.vector, &vec);
528 CHKERRV(ierr);
529 ierr = MatAssemblyBegin(matrix, MAT_FINAL_ASSEMBLY);
530 CHKERRV(ierr);
531 ierr = MatAssemblyEnd(matrix, MAT_FINAL_ASSEMBLY);
532 CHKERRV(ierr);
533}
534
535std::pair<PetscInt, PetscInt> Matrix::getSize() const
536{
537 PetscInt m, n;
538 MatGetSize(matrix, &m, &n);
539 return std::make_pair(m, n);
540}
541
542std::pair<PetscInt, PetscInt> Matrix::getLocalSize() const
543{
544 PetscInt m, n;
545 MatGetLocalSize(matrix, &m, &n);
546 return std::make_pair(m, n);
547}
548
549std::pair<PetscInt, PetscInt> Matrix::ownerRange() const
550{
551 PetscInt range_start, range_end;
552 MatGetOwnershipRange(matrix, &range_start, &range_end);
553 return std::make_pair(range_start, range_end);
554}
555
556std::pair<PetscInt, PetscInt> Matrix::ownerRangeColumn() const
557{
558 PetscInt range_start, range_end;
559 MatGetOwnershipRangeColumn(matrix, &range_start, &range_end);
560 return std::make_pair(range_start, range_end);
561}
562
563PetscInt Matrix::blockSize() const
564{
565 PetscErrorCode ierr = 0;
566 PetscInt bs;
567 ierr = MatGetBlockSize(matrix, &bs);
568 CHKERRQ(ierr);
569 return bs;
570}
571
572void Matrix::write(const std::string &filename, VIEWERFORMAT format) const
573{
574 PetscErrorCode ierr = 0;
575 Viewer viewer{filename, format, getCommunicator(matrix)};
576 ierr = MatView(matrix, viewer.viewer);
577 CHKERRV(ierr);
578}
579
580void Matrix::read(const std::string &filename)
581{
582 PetscErrorCode ierr = 0;
583 Viewer viewer{filename, BINARY, getCommunicator(matrix)};
584 ierr = MatLoad(matrix, viewer.viewer);
585 CHKERRV(ierr);
586}
587
588void Matrix::view() const
589{
590 Viewer viewer{PETSCVIEWERASCII, getCommunicator(matrix)};
591 viewer.pushFormat(PETSC_VIEWER_ASCII_DENSE);
592 PetscErrorCode ierr = MatView(matrix, viewer.viewer);
593 CHKERRV(ierr);
594}
595
597{
598 Viewer viewer{PETSCVIEWERDRAW, getCommunicator(matrix)};
599 viewer.pushFormat(PETSC_VIEWER_ASCII_DENSE);
600 PetscErrorCode ierr = MatView(matrix, viewer.viewer);
601 CHKERRV(ierr);
602 ierr = MatView(matrix, viewer.viewer);
603 CHKERRV(ierr);
604
605 PetscDraw draw;
606 ierr = PetscViewerDrawGetDraw(viewer.viewer, 0, &draw);
607 CHKERRV(ierr);
608 ierr = PetscDrawSetPause(draw, -1);
609 CHKERRV(ierr); // Wait for user
610}
611
613
614KSPSolver::KSPSolver(std::string name)
615{
616 PetscErrorCode ierr = 0;
617 ierr = KSPCreate(utils::Parallel::current()->comm, &ksp);
618 CHKERRV(ierr);
619 setName(ksp, std::move(name));
620}
621
623{
624 PetscErrorCode ierr = 0;
625 PetscBool petscIsInitialized;
626 PetscInitialized(&petscIsInitialized);
627 if (petscIsInitialized && ksp) // If PetscFinalize is called before ~KSPSolver
628 ierr = KSPDestroy(&ksp);
629 CHKERRV(ierr);
630}
631
632KSPSolver::operator KSP &()
633{
634 return ksp;
635}
636
638{
639 PetscErrorCode ierr = 0;
640 ierr = KSPReset(ksp);
641 CHKERRV(ierr);
642}
643
645{
646 KSPConvergedReason convReason;
647 PetscErrorCode ierr = 0;
648 ierr = KSPGetConvergedReason(ksp, &convReason);
649 if (ierr != 0) {
651 }
652 if (convReason > 0) {
654 }
655 if (convReason == KSP_DIVERGED_ITS) {
657 } else {
659 }
660}
661
663{
664 KSPSolve(ksp, b, x);
665 return getSolverResult();
666}
667
669{
670 KSPSolveTranspose(ksp, b, x);
671 return getSolverResult();
672}
673
675{
676 // See PETSc manual page for KSPGetConvergedReason to understand this function
677 // We treat divergence due to reaching max iterations as "stopped"
678 KSPConvergedReason convReason;
679 KSPGetConvergedReason(ksp, &convReason);
680
681 PetscReal rtol, atol, dtol;
682 PetscInt miter;
683 KSPGetTolerances(ksp, &rtol, &atol, &dtol, &miter);
684
685 std::ostringstream oss;
686 {
687 bool converged = (convReason >= 0);
688 bool stopped = (convReason == KSP_DIVERGED_ITS);
689 oss << "Solver " << (converged ? "converged" : (stopped ? "stopped" : "diverged"));
690 }
691 oss << " after " << getIterationNumber() << " of " << miter << " iterations due to";
692
693 switch (convReason) {
694 case (KSP_CONVERGED_RTOL):
695#if (PETSC_MAJOR == 3) && (PETSC_MINOR < 24)
696 case (KSP_CONVERGED_RTOL_NORMAL):
697#else
698 case (KSP_CONVERGED_RTOL_NORMAL_EQUATIONS):
699#endif
700 oss << " sufficient relative convergence";
701 break;
702 case (KSP_CONVERGED_ATOL):
703#if (PETSC_MAJOR == 3) && (PETSC_MINOR < 24)
704 case (KSP_CONVERGED_ATOL_NORMAL):
705#else
706 case (KSP_CONVERGED_ATOL_NORMAL_EQUATIONS):
707#endif
708 oss << " sufficient absolute convergence";
709 break;
710 case (KSP_DIVERGED_ITS):
711 oss << " reaching the maximum iterations";
712 break;
713 case (KSP_DIVERGED_DTOL):
714 oss << " sufficient divergence";
715 break;
716 case (KSP_DIVERGED_NANORINF):
717 oss << " the residual norm becoming nan or inf";
718 break;
719 case (KSP_DIVERGED_BREAKDOWN):
720 oss << " a generic breakdown of the method";
721 break;
722 default:
723 oss << " the PETSc reason " << KSPConvergedReasons[convReason];
724 break;
725 }
726
727 double bnorm = b.l2norm();
728 double dlim = bnorm * dtol;
729 double rlim = bnorm * rtol;
730
731 oss << ". Last residual norm: " << getResidualNorm() << ", limits: relative " << rlim << " (rtol " << rtol << "), absolute " << atol << ", divergence " << dlim << "(dtol " << dtol << ')';
732
733 return oss.str();
734}
735
737{
738 PetscErrorCode ierr = 0;
739 PetscInt its;
740 ierr = KSPGetIterationNumber(ksp, &its);
741 CHKERRQ(ierr);
742 return its;
743}
744
746{
747 PetscErrorCode ierr = 0;
748 PetscReal val;
749 ierr = KSPGetResidualNorm(ksp, &val);
750 CHKERRQ(ierr);
751 return val;
752}
753
755{
756 PetscErrorCode ierr = 0;
757 PetscReal rtol;
758 ierr = KSPGetTolerances(ksp, &rtol, nullptr, nullptr, nullptr);
759 CHKERRQ(ierr);
760 return rtol;
761}
762
764
765void destroy(ISLocalToGlobalMapping *IS)
766{
767 PetscErrorCode ierr = 0;
768 PetscBool petscIsInitialized;
769 PetscInitialized(&petscIsInitialized);
770
771 if (IS and petscIsInitialized) {
772 ierr = ISLocalToGlobalMappingDestroy(IS);
773 CHKERRV(ierr);
774 }
775}
776
777void destroy(AO *ao)
778{
779 PetscErrorCode ierr = 0;
780 PetscBool petscIsInitialized;
781 PetscInitialized(&petscIsInitialized);
782
783 if (ao and petscIsInitialized) {
784 ierr = AODestroy(ao);
785 CHKERRV(ierr);
786 }
787}
788
789} // namespace precice::utils::petsc
790
791#endif // PRECICE_NO_PETSC
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
std::nullptr_t MPI_Comm
Definition MPI_Mock.hpp:9
int MPI_Comm_size(MPI_Comm comm, int *size)
Definition MPI_Mock.hpp:30
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
A C++ 11 implementation of the non-owning C++20 std::span type.
Definition span.hpp:284
constexpr iterator begin() const noexcept
Definition span.hpp:503
constexpr iterator end() const noexcept
Definition span.hpp:505
constexpr size_type size() const noexcept
Definition span.hpp:469
static CommStatePtr current()
Returns an owning pointer to the current CommState.
Definition Parallel.cpp:147
static bool weInitialized
Whether we have initialized Petsc or if it was initialized by an application calling us.
Definition Petsc.hpp:29
static void finalize()
Finalizes Petsc environment.
Definition Petsc.cpp:59
static void initialize(utils::Parallel::Communicator comm)
Initializes the Petsc environment.
Definition Petsc.cpp:39
static logging::Logger _log
Definition Petsc.hpp:31
SolverResult getSolverResult()
Returns the current convergence reason as a SolverRestult.
Definition Petsc.cpp:644
PetscReal getRealtiveTolerance()
Returns the relavtive tolerance of the KSP.
Definition Petsc.cpp:754
std::string summaryFor(Vector &b)
Returns a summary the KSP solving for b.
Definition Petsc.cpp:674
SolverResult solveTranspose(Vector &b, Vector &x)
Solves the transposed linear system, returns false it not converged.
Definition Petsc.cpp:668
void reset()
Destroys and recreates the ksp on the same communicator.
Definition Petsc.cpp:637
KSPSolver(const KSPSolver &)=delete
Delete copy and assignment constructor.
PetscReal getResidualNorm()
Returns the last residual norm of the KSP.
Definition Petsc.cpp:745
PetscInt getIterationNumber()
Returns the iteration number of solver, either during or after the solve call.
Definition Petsc.cpp:736
SolverResult solve(Vector &b, Vector &x)
Solves the linear system, returns false it not converged.
Definition Petsc.cpp:662
SolverResult
The state of the KSP after returning from solve()
Definition Petsc.hpp:261
@ Stopped
The solver reached the maximum iterations.
Definition Petsc.hpp:263
void setColumn(Vector &v, PetscInt col)
Definition Petsc.cpp:514
void read(const std::string &filename)
Reads the matrix from file, stored in PETSc binary format.
Definition Petsc.cpp:580
void write(const std::string &filename, VIEWERFORMAT format=ASCII) const
Writes the matrix to file.
Definition Petsc.cpp:572
void assemble(MatAssemblyType type=MAT_FINAL_ASSEMBLY)
Definition Petsc.cpp:446
void view() const
Prints the matrix.
Definition Petsc.cpp:588
std::pair< PetscInt, PetscInt > getSize() const
Returns (rows, cols) global size.
Definition Petsc.cpp:535
void setValue(PetscInt row, PetscInt col, PetscScalar value)
Definition Petsc.cpp:490
void viewDraw() const
Graphically draws the matrix structure.
Definition Petsc.cpp:596
void init(PetscInt localRows, PetscInt localCols, PetscInt globalRows, PetscInt globalCols, MatType type=nullptr, bool doSetup=true)
Initializes matrix of given size and type.
Definition Petsc.cpp:455
std::pair< PetscInt, PetscInt > ownerRange() const
Returns a pair that mark the beginning and end of the matrix' ownership range.
Definition Petsc.cpp:549
PetscInt blockSize() const
Returns the block size of the matrix.
Definition Petsc.cpp:563
void reset()
Destroys and recreates the matrix on the same communicator.
Definition Petsc.cpp:472
Matrix(const Matrix &)=delete
Delete copy and assignment constructor.
std::pair< PetscInt, PetscInt > getLocalSize() const
Returns (rows, cols) local size.
Definition Petsc.cpp:542
MatInfo getInfo(MatInfoType flag) const
Get the MatInfo struct for the matrix.
Definition Petsc.cpp:483
std::pair< PetscInt, PetscInt > ownerRangeColumn() const
Returns a pair that mark the beginning and end of the matrix' column ownership range.
Definition Petsc.cpp:556
Vector & copyTo(precice::span< double > destination)
Definition Petsc.cpp:340
double l2norm() const
returns the l2-norm of the vector
Definition Petsc.cpp:396
PetscInt getLocalSize() const
Definition Petsc.cpp:274
void arange(double start, double stop)
Definition Petsc.cpp:290
void read(const std::string &filename, VIEWERFORMAT format=ASCII)
Reads the vector from file.
Definition Petsc.cpp:403
void setValue(PetscInt row, PetscScalar value)
Definition Petsc.cpp:283
void sort()
Sorts the LOCAL partition of the vector.
Definition Petsc.cpp:359
static Vector allocate(const std::string &name="")
Allocates a new vector on the given MPI communicator.
Definition Petsc.cpp:207
Vector & copyFrom(precice::span< const double > source)
Definition Petsc.cpp:323
void view() const
Prints the vector.
Definition Petsc.cpp:409
std::pair< PetscInt, PetscInt > ownerRange() const
Returns a pair that mark the beginning and end of the vectors ownership range. Use first and second t...
Definition Petsc.cpp:383
void write(const std::string &filename, VIEWERFORMAT format=ASCII) const
Writes the vector to file.
Definition Petsc.cpp:390
static logging::Logger _log
Definition Petsc.hpp:157
PetscInt getSize() const
Definition Petsc.cpp:265
void swap(Vector &other) noexcept
Swaps the ownership of two vectors.
Definition Petsc.cpp:245
Vector & operator=(const Vector &other)
Definition Petsc.cpp:162
Vector(const std::string &name="")
Creates a new vector on the given MPI communicator.
Definition Petsc.cpp:181
void init(PetscInt rows)
Sets the size and calls VecSetFromOptions.
Definition Petsc.cpp:256
PETSc related utilities.
Definition Petsc.cpp:82
void setName(T obj, const std::string &name)
Definition Petsc.cpp:135
std::string getName(T obj)
Definition Petsc.cpp:143
MPI_Comm getCommunicator(T obj)
Definition Petsc.cpp:127
void destroy(ISLocalToGlobalMapping *IS)
Destroys an ISLocalToGlobalMapping, if IS is not null and PetscIsInitialized.
Definition Petsc.cpp:765
void swap(Vector &lhs, Vector &rhs) noexcept
Definition Petsc.cpp:416
contains precice-related utilities.
Viewer(const std::string &filename, VIEWERFORMAT format, MPI_Comm comm)
Definition Petsc.cpp:85
void pushFormat(PetscViewerFormat format)
Definition Petsc.cpp:115
Viewer(PetscViewerType type, MPI_Comm comm)
Definition Petsc.cpp:98