DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
petsc.h
1// Copyright (C) 2004-2026 Johan Hoffman, Johan Jansson, Anders Logg,
2// Garth N. Wells and Jack S. Hale
3//
4// This file is part of DOLFINx (https://www.fenicsproject.org)
5//
6// SPDX-License-Identifier: LGPL-3.0-or-later
7
8#pragma once
9
10#ifdef HAS_PETSC
11
12#include <format>
13#include <petscsys.h>
14#include <source_location>
15#include <string>
16#include <string_view>
17
18namespace dolfinx::common
19{
22namespace petsc
23{
31void error(PetscErrorCode error_code, std::string_view petsc_function,
32 std::source_location loc = std::source_location::current());
33
41inline void check(PetscErrorCode ierr, std::string_view petsc_function,
42 std::source_location loc = std::source_location::current())
43{
44 if (ierr != 0)
45 error(ierr, petsc_function, loc);
46}
47
52void set_option(std::string option);
53
59template <typename T>
60 requires requires(const T& value) { std::format("{}", value); }
61void set_option(std::string option, const T& value)
62{
63 if (option[0] != '-')
64 option = '-' + option;
65
66 check(PetscOptionsSetValue(nullptr, option.c_str(),
67 std::format("{}", value).c_str()),
68 "PetscOptionsSetValue");
69}
70
73void clear_option(std::string option);
74
76void clear_options();
77} // namespace petsc
78} // namespace dolfinx::common
79
80#endif
PETSc error handling helpers shared across DOLFINx's PETSc wrappers.
Definition petsc.h:23
void check(PetscErrorCode ierr, std::string_view petsc_function, std::source_location loc=std::source_location::current())
Throw a std::runtime_error via error() if ierr indicates a PETSc call failed.
Definition petsc.h:41
void error(PetscErrorCode error_code, std::string_view petsc_function, std::source_location loc=std::source_location::current())
Print error message for a PETSc call that returned an error and throw a std::runtime_error.
Definition petsc.cpp:21
void clear_option(std::string option)
Clear a PETSc option from the PETSc options/parameter database.
Definition petsc.cpp:47
void clear_options()
Clear the PETSc global options/parameter database.
Definition petsc.cpp:56
void set_option(std::string option)
Set a PETSc option in the PETSc options/parameter database. The option must not be prefixed by '-',...
Definition petsc.cpp:42
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8