| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2020 - 2023 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_vector_tools_point_gradient_templates_h |
| 16 |
#define dealii_vector_tools_point_gradient_templates_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/fe/fe_values.h> |
| 20 |
|
| 21 |
#include <deal.II/grid/grid_tools.h> |
| 22 |
|
| 23 |
#include <deal.II/hp/fe_values.h> |
| 24 |
|
| 25 |
#include <deal.II/lac/block_vector.h> |
| 26 |
#include <deal.II/lac/la_parallel_block_vector.h> |
| 27 |
#include <deal.II/lac/la_parallel_vector.h> |
| 28 |
#include <deal.II/lac/petsc_block_vector.h> |
| 29 |
#include <deal.II/lac/petsc_vector.h> |
| 30 |
#include <deal.II/lac/trilinos_parallel_block_vector.h> |
| 31 |
#include <deal.II/lac/trilinos_vector.h> |
| 32 |
|
| 33 |
#include <deal.II/numerics/vector_tools_common.h> |
| 34 |
#include <deal.II/numerics/vector_tools_point_gradient.h> |
| 35 |
|
| 36 |
DEAL_II_NAMESPACE_OPEN |
| 37 |
|
| 38 |
namespace VectorTools |
| 39 |
{ |
| 40 |
template <int dim, typename VectorType, int spacedim> |
| 41 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>) |
| 42 |
void point_gradient( |
| 43 |
const DoFHandler<dim, spacedim> &dof, |
| 44 |
const VectorType &fe_function, |
| 45 |
const Point<spacedim> &point, |
| 46 |
std::vector<Tensor<1, spacedim, typename VectorType::value_type>> |
| 47 |
&gradients) |
| 48 |
{ |
| 49 |
if (dof.has_hp_capabilities() == false) |
| 50 |
point_gradient(get_default_linear_mapping(dof.get_triangulation()), |
| 51 |
dof, |
| 52 |
fe_function, |
| 53 |
point, |
| 54 |
gradients); |
| 55 |
else |
| 56 |
point_gradient(hp::StaticMappingQ1<dim, spacedim>::mapping_collection, |
| 57 |
dof, |
| 58 |
fe_function, |
| 59 |
point, |
| 60 |
gradients); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
template <int dim, typename VectorType, int spacedim> |
| 65 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>) |
| 66 |
Tensor<1, spacedim, typename VectorType::value_type> point_gradient( |
| 67 |
const DoFHandler<dim, spacedim> &dof, |
| 68 |
const VectorType &fe_function, |
| 69 |
const Point<spacedim> &point) |
| 70 |
{ |
| 71 |
if (dof.has_hp_capabilities() == false) |
| 72 |
return point_gradient(get_default_linear_mapping(dof.get_triangulation()), |
| 73 |
dof, |
| 74 |
fe_function, |
| 75 |
point); |
| 76 |
else |
| 77 |
return point_gradient( |
| 78 |
hp::StaticMappingQ1<dim, spacedim>::mapping_collection, |
| 79 |
dof, |
| 80 |
fe_function, |
| 81 |
point); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
template <int dim, typename VectorType, int spacedim> |
| 86 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>) |
| 87 |
void point_gradient( |
| 88 |
const Mapping<dim, spacedim> &mapping, |
| 89 |
const DoFHandler<dim, spacedim> &dof, |
| 90 |
const VectorType &fe_function, |
| 91 |
const Point<spacedim> &point, |
| 92 |
std::vector<Tensor<1, spacedim, typename VectorType::value_type>> &gradient) |
| 93 |
{ |
| 94 |
const FiniteElement<dim> &fe = dof.get_fe(); |
| 95 |
|
| 96 |
Assert(gradient.size() == fe.n_components(), |
| 97 |
ExcDimensionMismatch(gradient.size(), fe.n_components())); |
| 98 |
|
| 99 |
// first find the cell in which this point |
| 100 |
// is, initialize a quadrature rule with |
| 101 |
// it, and then a FEValues object |
| 102 |
const std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator, |
| 103 |
Point<spacedim>> |
| 104 |
cell_point = |
| 105 |
GridTools::find_active_cell_around_point(mapping, dof, point); |
| 106 |
|
| 107 |
AssertThrow(cell_point.first.state() == IteratorState::valid && |
| 108 |
cell_point.first->is_locally_owned(), |
| 109 |
ExcPointNotAvailableHere()); |
| 110 |
Assert(GeometryInfo<dim>::distance_to_unit_cell(cell_point.second) < 1e-10, |
| 111 |
ExcInternalError()); |
| 112 |
|
| 113 |
const Quadrature<dim> quadrature( |
| 114 |
cell_point.first->reference_cell().closest_point(cell_point.second)); |
| 115 |
|
| 116 |
FEValues<dim> fe_values(mapping, fe, quadrature, update_gradients); |
| 117 |
fe_values.reinit(cell_point.first); |
| 118 |
|
| 119 |
// then use this to get the gradients of |
| 120 |
// the given fe_function at this point |
| 121 |
using Number = typename VectorType::value_type; |
| 122 |
std::vector<std::vector<Tensor<1, dim, Number>>> u_gradient( |
| 123 |
1, std::vector<Tensor<1, dim, Number>>(fe.n_components())); |
| 124 |
fe_values.get_function_gradients(fe_function, u_gradient); |
| 125 |
|
| 126 |
gradient = u_gradient[0]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
template <int dim, typename VectorType, int spacedim> |
| 131 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>) |
| 132 |
void point_gradient( |
| 133 |
const hp::MappingCollection<dim, spacedim> &mapping, |
| 134 |
const DoFHandler<dim, spacedim> &dof, |
| 135 |
const VectorType &fe_function, |
| 136 |
const Point<spacedim> &point, |
| 137 |
std::vector<Tensor<1, spacedim, typename VectorType::value_type>> &gradient) |
| 138 |
{ |
| 139 |
using Number = typename VectorType::value_type; |
| 140 |
const hp::FECollection<dim, spacedim> &fe = dof.get_fe_collection(); |
| 141 |
|
| 142 |
Assert(gradient.size() == fe.n_components(), |
| 143 |
ExcDimensionMismatch(gradient.size(), fe.n_components())); |
| 144 |
|
| 145 |
// first find the cell in which this point |
| 146 |
// is, initialize a quadrature rule with |
| 147 |
// it, and then a FEValues object |
| 148 |
const std::pair<typename DoFHandler<dim, spacedim>::active_cell_iterator, |
| 149 |
Point<spacedim>> |
| 150 |
cell_point = |
| 151 |
GridTools::find_active_cell_around_point(mapping, dof, point); |
| 152 |
|
| 153 |
AssertThrow(cell_point.first.state() == IteratorState::valid && |
| 154 |
cell_point.first->is_locally_owned(), |
| 155 |
ExcPointNotAvailableHere()); |
| 156 |
Assert(GeometryInfo<dim>::distance_to_unit_cell(cell_point.second) < 1e-10, |
| 157 |
ExcInternalError()); |
| 158 |
|
| 159 |
const Quadrature<dim> quadrature( |
| 160 |
cell_point.first->reference_cell().closest_point(cell_point.second)); |
| 161 |
hp::FEValues<dim, spacedim> hp_fe_values(mapping, |
| 162 |
fe, |
| 163 |
hp::QCollection<dim>(quadrature), |
| 164 |
update_gradients); |
| 165 |
hp_fe_values.reinit(cell_point.first); |
| 166 |
const FEValues<dim, spacedim> &fe_values = |
| 167 |
hp_fe_values.get_present_fe_values(); |
| 168 |
|
| 169 |
std::vector<std::vector<Tensor<1, dim, Number>>> u_gradient( |
| 170 |
1, std::vector<Tensor<1, dim, Number>>(fe.n_components())); |
| 171 |
fe_values.get_function_gradients(fe_function, u_gradient); |
| 172 |
|
| 173 |
gradient = u_gradient[0]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
template <int dim, typename VectorType, int spacedim> |
| 178 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>) |
| 179 |
Tensor<1, spacedim, typename VectorType::value_type> point_gradient( |
| 180 |
const Mapping<dim, spacedim> &mapping, |
| 181 |
const DoFHandler<dim, spacedim> &dof, |
| 182 |
const VectorType &fe_function, |
| 183 |
const Point<spacedim> &point) |
| 184 |
{ |
| 185 |
Assert(dof.get_fe(0).n_components() == 1, |
| 186 |
ExcMessage( |
| 187 |
"Finite element is not scalar as is necessary for this function")); |
| 188 |
|
| 189 |
std::vector<Tensor<1, dim, typename VectorType::value_type>> gradient(1); |
| 190 |
point_gradient(mapping, dof, fe_function, point, gradient); |
| 191 |
|
| 192 |
return gradient[0]; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
template <int dim, typename VectorType, int spacedim> |
| 198 |
DEAL_II_CXX20_REQUIRES(concepts::is_dealii_vector_type<VectorType>) |
| 199 |
Tensor<1, spacedim, typename VectorType::value_type> point_gradient( |
| 200 |
const hp::MappingCollection<dim, spacedim> &mapping, |
| 201 |
const DoFHandler<dim, spacedim> &dof, |
| 202 |
const VectorType &fe_function, |
| 203 |
const Point<spacedim> &point) |
| 204 |
{ |
| 205 |
Assert(dof.get_fe(0).n_components() == 1, |
| 206 |
ExcMessage( |
| 207 |
"Finite element is not scalar as is necessary for this function")); |
| 208 |
|
| 209 |
std::vector<Tensor<1, dim, typename VectorType::value_type>> gradient(1); |
| 210 |
point_gradient(mapping, dof, fe_function, point, gradient); |
| 211 |
|
| 212 |
return gradient[0]; |
| 213 |
} |
| 214 |
} // namespace VectorTools |
| 215 |
|
| 216 |
DEAL_II_NAMESPACE_CLOSE |
| 217 |
|
| 218 |
#endif // dealii_vector_tools_point_gradient_templates_h |