DOLFINx 0.12.0.0
DOLFINx C++
Loading...
Searching...
No Matches
utils.h
1// Copyright (C) 2018-2022 Garth N. Wells
2//
3// This file is part of DOLFINx (https://www.fenicsproject.org)
4//
5// SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include <concepts>
10#include <cstddef>
11#include <cstdint>
12#include <dolfinx/common/types.h>
13#include <format>
14#include <ranges>
15#include <span>
16#include <stdexcept>
17
18namespace dolfinx::la
19{
21enum class Norm : std::int8_t
22{
23 l1,
24 l2,
25 linf,
26 frobenius
27};
28
31template <class U, class T>
32concept MatSet
33 = std::invocable<U, std::span<const std::int32_t>,
34 std::span<const std::int32_t>, std::span<const T>>;
50template <dolfinx::scalar T>
51void set_diagonal(MatSet<T> auto&& set_fn,
52 const common::LocalIndexRange auto& rows, T diagonal = T(1))
53{
54 std::span<const T, 1> diag_span(&diagonal, 1);
55 for (std::size_t i = 0; i < std::ranges::size(rows); ++i)
56 {
57 std::span<const std::int32_t, 1> row(std::ranges::data(rows) + i, 1);
58 set_fn(row, row, diag_span);
59 }
60}
61
72template <dolfinx::scalar T>
73void set_diagonal(MatSet<T> auto&& set_fn,
74 const common::LocalIndexRange auto& rows,
75 std::span<const T> diagonals)
76{
77 if (diagonals.size() != std::ranges::size(rows))
78 {
79 throw std::invalid_argument(
80 std::format("Number of diagonal values ({}) does not match number "
81 "of rows ({}).",
82 diagonals.size(), std::ranges::size(rows)));
83 }
84
85 for (std::size_t i = 0; i < diagonals.size(); ++i)
86 {
88 set_fn,
89 std::span<const std::int32_t, 1>(std::ranges::data(rows) + i, 1),
90 diagonals[i]);
91 }
92}
93} // namespace dolfinx::la
Concept for a contiguous list of process-local indices, e.g. the row or column indices passed to a ma...
Definition types.h:75
Matrix accumulate/set concept for functions that can be used in assemblers to accumulate or set value...
Definition utils.h:33
Linear algebra interface.
Definition dolfinx_la.h:7
void set_diagonal(MatSet< T > auto &&set_fn, const common::LocalIndexRange auto &rows, T diagonal=T(1))
Set a value on the diagonal of a matrix for specified rows.
Definition utils.h:51
Norm
Norm types.
Definition utils.h:22