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