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