|
| 1 | +// --------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright (C) 2023 by the deal.II authors |
| 4 | +// |
| 5 | +// This file is part of the deal.II library. |
| 6 | +// |
| 7 | +// The deal.II library is free software; you can use it, redistribute |
| 8 | +// it, and/or modify it under the terms of the GNU Lesser General |
| 9 | +// Public License as published by the Free Software Foundation; either |
| 10 | +// version 2.1 of the License, or (at your option) any later version. |
| 11 | +// The full text of the license can be found in the file LICENSE.md at |
| 12 | +// the top level directory of deal.II. |
| 13 | +// |
| 14 | +// Authors: Peter Munch, Martin Kronbichler |
| 15 | +// |
| 16 | +// --------------------------------------------------------------------- |
| 17 | + |
| 18 | +// deal.II includes |
| 19 | +#include <deal.II/dofs/dof_tools.h> |
| 20 | + |
| 21 | +#include <deal.II/fe/mapping.h> |
| 22 | + |
| 23 | +#include <deal.II/lac/la_parallel_vector.h> |
| 24 | + |
| 25 | +#include <deal.II/matrix_free/fe_evaluation.h> |
| 26 | +#include <deal.II/matrix_free/matrix_free.h> |
| 27 | +#include <deal.II/matrix_free/shape_info.h> |
| 28 | +#include <deal.II/matrix_free/tools.h> |
| 29 | + |
| 30 | +// libCEED includes |
| 31 | +#include <ceed.h> |
| 32 | +#include <ceed/backend.h> |
| 33 | + |
| 34 | +// QFunction source |
| 35 | +#include "bps-qfunctions.h" |
| 36 | + |
| 37 | +using namespace dealii; |
| 38 | + |
| 39 | + |
| 40 | +template <int dim, typename Number> |
| 41 | +class OperatorDealii : public OperatorBase<Number> |
| 42 | +{ |
| 43 | +public: |
| 44 | + using VectorType = typename OperatorBase<Number>::VectorType; |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructor. |
| 48 | + */ |
| 49 | + OperatorDealii(const Mapping<dim> &mapping, |
| 50 | + const DoFHandler<dim> &dof_handler, |
| 51 | + const AffineConstraints<Number> &constraints, |
| 52 | + const Quadrature<dim> &quadrature, |
| 53 | + const BPType &bp) |
| 54 | + : mapping(mapping) |
| 55 | + , dof_handler(dof_handler) |
| 56 | + , constraints(constraints) |
| 57 | + , quadrature(quadrature) |
| 58 | + , bp(bp) |
| 59 | + { |
| 60 | + reinit(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Destructor. |
| 65 | + */ |
| 66 | + ~OperatorDealii() = default; |
| 67 | + |
| 68 | + /** |
| 69 | + * Initialized internal data structures, particularly, MatrixFree. |
| 70 | + */ |
| 71 | + void |
| 72 | + reinit() override |
| 73 | + { |
| 74 | + // configure MatrixFree |
| 75 | + typename MatrixFree<dim, Number>::AdditionalData additional_data; |
| 76 | + additional_data.tasks_parallel_scheme = |
| 77 | + MatrixFree<dim, Number>::AdditionalData::TasksParallelScheme::none; |
| 78 | + |
| 79 | + // create MatrixFree |
| 80 | + matrix_free.reinit(mapping, dof_handler, constraints, quadrature, additional_data); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Matrix-vector product. |
| 85 | + */ |
| 86 | + void |
| 87 | + vmult(VectorType &dst, const VectorType &src) const override |
| 88 | + { |
| 89 | + if (dof_handler.get_fe().n_components() == 1) |
| 90 | + { |
| 91 | + matrix_free.cell_loop(&OperatorDealii::do_cell_integral_range<1>, this, dst, src, true); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + AssertThrow(dof_handler.get_fe().n_components() == dim, ExcInternalError()); |
| 96 | + |
| 97 | + matrix_free.cell_loop(&OperatorDealii::do_cell_integral_range<dim>, this, dst, src, true); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Initialize vector. |
| 103 | + */ |
| 104 | + void |
| 105 | + initialize_dof_vector(VectorType &vec) const override |
| 106 | + { |
| 107 | + matrix_free.initialize_dof_vector(vec); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Compute inverse of diagonal. |
| 112 | + */ |
| 113 | + void |
| 114 | + compute_inverse_diagonal(VectorType &diagonal) const override |
| 115 | + { |
| 116 | + this->initialize_dof_vector(diagonal); |
| 117 | + |
| 118 | + if (dof_handler.get_fe().n_components() == 1) |
| 119 | + { |
| 120 | + MatrixFreeTools::compute_diagonal(matrix_free, |
| 121 | + diagonal, |
| 122 | + &OperatorDealii::do_cell_integral_local<1>, |
| 123 | + this); |
| 124 | + } |
| 125 | + else |
| 126 | + { |
| 127 | + AssertThrow(dof_handler.get_fe().n_components() == dim, ExcInternalError()); |
| 128 | + |
| 129 | + MatrixFreeTools::compute_diagonal(matrix_free, |
| 130 | + diagonal, |
| 131 | + &OperatorDealii::do_cell_integral_local<dim>, |
| 132 | + this); |
| 133 | + } |
| 134 | + |
| 135 | + for (auto &i : diagonal) |
| 136 | + i = (std::abs(i) > 1.0e-10) ? (1.0 / i) : 1.0; |
| 137 | + } |
| 138 | + |
| 139 | +private: |
| 140 | + /** |
| 141 | + * Cell integral without vector access. |
| 142 | + */ |
| 143 | + template <int n_components> |
| 144 | + void |
| 145 | + do_cell_integral_local(FEEvaluation<dim, -1, 0, n_components, Number> &phi) const |
| 146 | + { |
| 147 | + if (bp <= BPType::BP2) // mass matrix |
| 148 | + { |
| 149 | + phi.evaluate(EvaluationFlags::values); |
| 150 | + for (const auto q : phi.quadrature_point_indices()) |
| 151 | + phi.submit_value(phi.get_value(q), q); |
| 152 | + phi.integrate(EvaluationFlags::values); |
| 153 | + } |
| 154 | + else // Poisson operator |
| 155 | + { |
| 156 | + phi.evaluate(EvaluationFlags::gradients); |
| 157 | + for (const auto q : phi.quadrature_point_indices()) |
| 158 | + phi.submit_gradient(phi.get_gradient(q), q); |
| 159 | + phi.integrate(EvaluationFlags::gradients); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Cell integral on a range of cells. |
| 165 | + */ |
| 166 | + template <int n_components> |
| 167 | + void |
| 168 | + do_cell_integral_range(const MatrixFree<dim, Number> &matrix_free, |
| 169 | + VectorType &dst, |
| 170 | + const VectorType &src, |
| 171 | + const std::pair<unsigned int, unsigned int> &range) const |
| 172 | + { |
| 173 | + FEEvaluation<dim, -1, 0, n_components, Number> phi(matrix_free, range); |
| 174 | + |
| 175 | + for (unsigned cell = range.first; cell < range.second; ++cell) |
| 176 | + { |
| 177 | + phi.reinit(cell); |
| 178 | + phi.read_dof_values(src); // read source vector |
| 179 | + do_cell_integral_local(phi); // cell integral |
| 180 | + phi.distribute_local_to_global(dst); // write to destination vector |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Mapping object passed to the constructor. |
| 186 | + */ |
| 187 | + const Mapping<dim> &mapping; |
| 188 | + |
| 189 | + /** |
| 190 | + * DoFHandler object passed to the constructor. |
| 191 | + */ |
| 192 | + const DoFHandler<dim> &dof_handler; |
| 193 | + |
| 194 | + /** |
| 195 | + * Constraints object passed to the constructor. |
| 196 | + */ |
| 197 | + const AffineConstraints<Number> &constraints; |
| 198 | + |
| 199 | + /** |
| 200 | + * Quadrature rule object passed to the constructor. |
| 201 | + */ |
| 202 | + const Quadrature<dim> &quadrature; |
| 203 | + |
| 204 | + /** |
| 205 | + * Selected BP. |
| 206 | + */ |
| 207 | + const BPType bp; |
| 208 | + |
| 209 | + /** |
| 210 | + * MatrixFree object. |
| 211 | + */ |
| 212 | + MatrixFree<dim, Number> matrix_free; |
| 213 | +}; |
0 commit comments