Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
tensor.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Francesco Daniele <francesco.daniele@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_LA_TENSOR_HPP_
25#define VULPES_LA_TENSOR_HPP_
26
27#include <petscsys.h>
28#include <petscvec.h>
29#include <petscviewerhdf5.h>
30
31#include <map>
32#include <string>
33#include <utility>
34
35#include <Eigen/Dense>
36
39
40namespace vulpes {
41namespace la {
42
52class Tensor {
53 static_assert(
54 std::is_same_v<double, PetscScalar>,
55 "Tensor requires PetscScalar to be double");
56
57 public:
58 template <int Dim>
59 using LocalMap =
60 Eigen::Map<const Eigen::Matrix<double, Dim, Dim, Eigen::RowMajor>>;
61
72 explicit Tensor(const std::string& filename, const std::string& dataset_name)
73 : m_vec_data(nullptr) {
74 PetscCallAbortWorld(VecCreate(PETSC_COMM_WORLD, &m_vec_data));
76 PetscObjectSetName((PetscObject)m_vec_data, dataset_name.c_str()));
77
78 PetscViewer viewer;
79 PetscCallAbortWorld(PetscViewerHDF5Open(
80 PETSC_COMM_WORLD, filename.c_str(), FILE_MODE_READ, &viewer));
81 PetscCallAbortWorld(VecLoad(m_vec_data, viewer));
82 PetscCallAbortWorld(PetscViewerDestroy(&viewer));
83
84 PetscCallAbortWorld(VecSetFromOptions(m_vec_data));
85 }
86
87 // Delete copy operations
88 Tensor(const Tensor&) = delete;
89 Tensor& operator=(const Tensor&) = delete;
90
98 template <int Dim>
99 LocalMap<Dim> getEigenMap(PetscInt local_index) const {
100 const PetscInt components_per_cell = Dim * Dim;
101 if (!m_raw_ptr) {
102 PetscCallAbortWorld(VecGetArrayRead(m_vec_data, &m_raw_ptr));
103 }
104 // Calculate the memory offset for this specific local cell
105 return LocalMap<Dim>(m_raw_ptr + (local_index * components_per_cell));
106 }
107
115 template <int Dim, typename IndexT>
116 auto getEvaluation(IndexT local_index) const {
117 return getEigenMap<Dim>(static_cast<PetscInt>(local_index)).eval();
118 }
119
121 if (m_vec_data) {
122 if (m_raw_ptr) {
123 PetscCallAbortWorld(VecRestoreArrayRead(m_vec_data, &m_raw_ptr));
124 }
125 PetscCallAbortWorld(VecDestroy(&m_vec_data));
126 }
127 }
128
129 private:
133 mutable const PetscScalar* m_raw_ptr = nullptr;
134};
135
136} // namespace la
137} // namespace vulpes
138
139#endif // VULPES_LA_TENSOR_HPP_
auto getEvaluation(IndexT local_index) const
Get the evaluated tensor for a specific local cell id.
Definition tensor.hpp:116
Vec m_vec_data
Store the complete flat tensor in a PETsc vector.
Definition tensor.hpp:131
Tensor(const Tensor &)=delete
const PetscScalar * m_raw_ptr
Pointer to the PETSc data. Used to avoid multiple redefinition.
Definition tensor.hpp:133
~Tensor()
Definition tensor.hpp:120
Tensor(const std::string &filename, const std::string &dataset_name)
Construct a Tensor by loading data from HDF5. The class can manage only cell constant tensors.
Definition tensor.hpp:72
Eigen::Map< const Eigen::Matrix< double, Dim, Dim, Eigen::RowMajor > > LocalMap
Definition tensor.hpp:59
Tensor & operator=(const Tensor &)=delete
LocalMap< Dim > getEigenMap(PetscInt local_index) const
Get the tensor for a specific local cell id throw an Eigen Map.
Definition tensor.hpp:99
Definition boomer_amg.hpp:27
Definition cell_manager.hpp:38
#define PetscCallAbortWorld(...)
Definition petsc_utils.hpp:32