| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2009 - 2025 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_mesh_worker_vector_selector_h |
| 16 |
#define dealii_mesh_worker_vector_selector_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/algorithms/any_data.h> |
| 21 |
#include <deal.II/algorithms/named_selection.h> |
| 22 |
|
| 23 |
#include <deal.II/base/enable_observer_pointer.h> |
| 24 |
#include <deal.II/base/mg_level_object.h> |
| 25 |
#include <deal.II/base/observer_pointer.h> |
| 26 |
#include <deal.II/base/tensor.h> |
| 27 |
#include <deal.II/base/types.h> |
| 28 |
|
| 29 |
DEAL_II_NAMESPACE_OPEN |
| 30 |
|
| 31 |
// Forward declaration |
| 32 |
#ifndef DOXYGEN |
| 33 |
template <int, int> |
| 34 |
class FEValuesBase; |
| 35 |
#endif |
| 36 |
|
| 37 |
namespace MeshWorker |
| 38 |
{ |
| 39 |
/** |
| 40 |
* A class that selects vectors from a list of named vectors. |
| 41 |
* |
| 42 |
* Since the number of vectors in an AnyData object may grow with every |
| 43 |
* nesting of applications or loops, it is important to be able to select |
| 44 |
* those, which are actually used in computing residuals etc. This class |
| 45 |
* organizes the selection. |
| 46 |
* |
| 47 |
* @ingroup MeshWorker |
| 48 |
*/ |
| 49 |
class VectorSelector : public EnableObserverPointer |
| 50 |
{ |
| 51 |
public: |
| 52 |
/** |
| 53 |
* Add a vector to the selection of finite element functions. The |
| 54 |
* arguments are the name of the vector and indicators, which information |
| 55 |
* is to be extracted from the vector. The name refers to an entry in a |
| 56 |
* AnyData object, which will be identified by initialize(). The three |
| 57 |
* bool parameters indicate, whether values, gradients and Hessians of the |
| 58 |
* finite element function are to be computed on each cell or face. |
| 59 |
*/ |
| 60 |
void |
| 61 |
add(const std::string &name, |
| 62 |
const bool values = true, |
| 63 |
const bool gradients = false, |
| 64 |
const bool hessians = false); |
| 65 |
|
| 66 |
/** |
| 67 |
* Does the same as the function above but it is possible to select a |
| 68 |
* block of the global vector. |
| 69 |
*/ |
| 70 |
// void add(const std::string& name, |
| 71 |
// const unsigned int selected_block, |
| 72 |
// bool values = true, |
| 73 |
// bool gradients = false, |
| 74 |
// bool hessians = false); |
| 75 |
|
| 76 |
/** |
| 77 |
* Initialize the selection field with a data vector. While add() only |
| 78 |
* enters the names of vectors, which will be used in the integration loop |
| 79 |
* over cells and faces, this function links the names to actual vectos in |
| 80 |
* a AnyData object. |
| 81 |
* |
| 82 |
* @note This function caches the index associated with a name. Therefore, |
| 83 |
* it must be called every time after the AnyData object has changed. |
| 84 |
*/ |
| 85 |
void |
| 86 |
initialize(const AnyData &); |
| 87 |
|
| 88 |
/** |
| 89 |
* Check whether any vector is selected. |
| 90 |
*/ |
| 91 |
bool |
| 92 |
empty() const; |
| 93 |
|
| 94 |
/** |
| 95 |
* Return true if values are selected for any vector. |
| 96 |
*/ |
| 97 |
bool |
| 98 |
has_values() const; |
| 99 |
|
| 100 |
/** |
| 101 |
* Return true if gradients are selected for any vector. |
| 102 |
*/ |
| 103 |
bool |
| 104 |
has_gradients() const; |
| 105 |
|
| 106 |
/** |
| 107 |
* Return true if hessians are selected for any vector. |
| 108 |
*/ |
| 109 |
bool |
| 110 |
has_hessians() const; |
| 111 |
|
| 112 |
/** |
| 113 |
* Number of vectors for values |
| 114 |
*/ |
| 115 |
unsigned int |
| 116 |
n_values() const; |
| 117 |
|
| 118 |
/** |
| 119 |
* Number of vectors for gradients |
| 120 |
*/ |
| 121 |
unsigned int |
| 122 |
n_gradients() const; |
| 123 |
|
| 124 |
/** |
| 125 |
* Number of vectors for Hessians |
| 126 |
*/ |
| 127 |
unsigned int |
| 128 |
n_hessians() const; |
| 129 |
|
| 130 |
/** |
| 131 |
* The vector index for the ith value |
| 132 |
*/ |
| 133 |
unsigned int |
| 134 |
value_index(const unsigned int i) const; |
| 135 |
|
| 136 |
/** |
| 137 |
* The vector index for the ith gradient |
| 138 |
*/ |
| 139 |
unsigned int |
| 140 |
gradient_index(const unsigned int i) const; |
| 141 |
|
| 142 |
/** |
| 143 |
* The vector index for the ith Hessian |
| 144 |
*/ |
| 145 |
unsigned int |
| 146 |
hessian_index(const unsigned int i) const; |
| 147 |
|
| 148 |
/** |
| 149 |
* Print the contents of the selection to the stream. |
| 150 |
*/ |
| 151 |
template <typename StreamType, typename DATA> |
| 152 |
void |
| 153 |
print(StreamType &s, const AnyData &v) const; |
| 154 |
|
| 155 |
/** |
| 156 |
* Print the number of selections to the stream. |
| 157 |
*/ |
| 158 |
template <typename StreamType> |
| 159 |
void |
| 160 |
print(StreamType &s) const; |
| 161 |
|
| 162 |
/** |
| 163 |
* The memory used by this object. |
| 164 |
*/ |
| 165 |
std::size_t |
| 166 |
memory_consumption() const; |
| 167 |
|
| 168 |
protected: |
| 169 |
/** |
| 170 |
* Selection of the vectors used to compute values. |
| 171 |
*/ |
| 172 |
NamedSelection value_selection; |
| 173 |
|
| 174 |
/** |
| 175 |
* Selection of the vectors used to compute gradients. |
| 176 |
*/ |
| 177 |
NamedSelection gradient_selection; |
| 178 |
|
| 179 |
/** |
| 180 |
* Selection of the vectors used to compute hessians. |
| 181 |
*/ |
| 182 |
NamedSelection hessian_selection; |
| 183 |
}; |
| 184 |
|
| 185 |
/** |
| 186 |
* Based on VectorSelector, this is the class used by IntegrationInfo to |
| 187 |
* compute values of source vectors in quadrature points. |
| 188 |
* |
| 189 |
* @ingroup MeshWorker |
| 190 |
*/ |
| 191 |
template <int dim, int spacedim = dim, typename Number = double> |
| 192 |
class VectorDataBase : public VectorSelector |
| 193 |
{ |
| 194 |
public: |
| 195 |
/** |
| 196 |
* Constructor |
| 197 |
*/ |
| 198 |
VectorDataBase() = default; |
| 199 |
|
| 200 |
/** |
| 201 |
* Constructor from a base class object |
| 202 |
*/ |
| 203 |
VectorDataBase(const VectorSelector &); |
| 204 |
|
| 205 |
/** |
| 206 |
* Initialize with a AnyData object and cache the indices in the |
| 207 |
* VectorSelector base class. |
| 208 |
* |
| 209 |
* @note Make sure the VectorSelector base class was filled with |
| 210 |
* reasonable data before calling this function. |
| 211 |
*/ |
| 212 |
void |
| 213 |
initialize(const AnyData &); |
| 214 |
|
| 215 |
/** |
| 216 |
* Virtual, but empty destructor. |
| 217 |
*/ |
| 218 |
virtual ~VectorDataBase() override = default; |
| 219 |
|
| 220 |
/** |
| 221 |
* The only function added to VectorSelector is an abstract virtual |
| 222 |
* function implemented in the derived class template and called by |
| 223 |
* IntegrationInfo. |
| 224 |
* |
| 225 |
* Depending on the selections made in our base class, this fills the |
| 226 |
* first three arguments with the local data of the finite element |
| 227 |
* functions. It is usually called either for the whole FESystem, or for |
| 228 |
* each base element separately. |
| 229 |
* |
| 230 |
* @param values is the vector filled with the values of the finite |
| 231 |
* element function in the quadrature points. |
| 232 |
* |
| 233 |
* @param gradients is the vector filled with the derivatives of the |
| 234 |
* finite element function in the quadrature points. |
| 235 |
* |
| 236 |
* @param hessians is the vector filled with the second derivatives of the |
| 237 |
* finite element function in the quadrature points. |
| 238 |
* |
| 239 |
* @param fe is the FEValuesBase object which is used to compute the |
| 240 |
* function values. Its UpdateFlags must have been set appropriately. |
| 241 |
* |
| 242 |
* @param index is the local index vector. If @p fe refers to base |
| 243 |
* elements of the system, this vector should be sorted by block and the |
| 244 |
* arguments @p start and @p size below specify the subset of @p indices |
| 245 |
* used. |
| 246 |
* |
| 247 |
* @param component is the first index in @p values, @p gradients and @p |
| 248 |
* hessians entered in this function. |
| 249 |
* |
| 250 |
* @param n_comp is the number of components to be filled. |
| 251 |
* |
| 252 |
* @param start is the first index of this block in @p indices, or zero if |
| 253 |
* no base elements are used. |
| 254 |
* |
| 255 |
* @param size is the number of dofs per cell of the current element or |
| 256 |
* base element. |
| 257 |
*/ |
| 258 |
virtual void |
| 259 |
fill(std::vector<std::vector<std::vector<Number>>> &values, |
| 260 |
std::vector<std::vector<std::vector<Tensor<1, spacedim, Number>>>> |
| 261 |
&gradients, |
| 262 |
std::vector<std::vector<std::vector<Tensor<2, spacedim, Number>>>> |
| 263 |
&hessians, |
| 264 |
const FEValuesBase<dim, spacedim> &fe, |
| 265 |
const std::vector<types::global_dof_index> &index, |
| 266 |
const unsigned int component, |
| 267 |
const unsigned int n_comp, |
| 268 |
const unsigned int start, |
| 269 |
const unsigned int size) const; |
| 270 |
|
| 271 |
/** |
| 272 |
* Fill the local data vector from level vectors. Performs exactly what |
| 273 |
* the other fill() does, but uses the cell level to access a single level |
| 274 |
* out of a hierarchy of level vectors, instead of a global data vector on |
| 275 |
* the active cells. |
| 276 |
*/ |
| 277 |
virtual void |
| 278 |
mg_fill(std::vector<std::vector<std::vector<Number>>> &values, |
| 279 |
std::vector<std::vector<std::vector<Tensor<1, spacedim, Number>>>> |
| 280 |
&gradients, |
| 281 |
std::vector<std::vector<std::vector<Tensor<2, spacedim, Number>>>> |
| 282 |
&hessians, |
| 283 |
const FEValuesBase<dim, spacedim> &fe, |
| 284 |
const unsigned int level, |
| 285 |
const std::vector<types::global_dof_index> &index, |
| 286 |
const unsigned int component, |
| 287 |
const unsigned int n_comp, |
| 288 |
const unsigned int start, |
| 289 |
const unsigned int size) const; |
| 290 |
|
| 291 |
protected: |
| 292 |
AnyData data; |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
/** |
| 297 |
* Based on VectorSelector, this is the class that implements the function |
| 298 |
* VectorDataBase::fill() for a certain type of vector, using AnyData to |
| 299 |
* identify vectors by name. |
| 300 |
* |
| 301 |
* @ingroup MeshWorker |
| 302 |
*/ |
| 303 |
template <typename VectorType, int dim, int spacedim = dim> |
| 304 |
class VectorData |
| 305 |
: public VectorDataBase<dim, spacedim, typename VectorType::value_type> |
| 306 |
{ |
| 307 |
public: |
| 308 |
/** |
| 309 |
* Constructor. |
| 310 |
*/ |
| 311 |
VectorData() = default; |
| 312 |
|
| 313 |
/** |
| 314 |
* Constructor using a prefilled VectorSelector |
| 315 |
*/ |
| 316 |
VectorData(const VectorSelector &); |
| 317 |
|
| 318 |
/** |
| 319 |
* Initialize with an object of named vectors. |
| 320 |
*/ |
| 321 |
void |
| 322 |
initialize(const AnyData &); |
| 323 |
|
| 324 |
/** |
| 325 |
* Initialize with a single vector and cache the indices in the |
| 326 |
* VectorSelector base class. |
| 327 |
* |
| 328 |
* @note Make sure the VectorSelector base class was filled with |
| 329 |
* reasonable data before calling this function. |
| 330 |
*/ |
| 331 |
void |
| 332 |
initialize(const VectorType *, const std::string &name); |
| 333 |
|
| 334 |
virtual void |
| 335 |
fill(std::vector<std::vector<std::vector<typename VectorType::value_type>>> |
| 336 |
&values, |
| 337 |
std::vector<std::vector< |
| 338 |
std::vector<Tensor<1, spacedim, typename VectorType::value_type>>>> |
| 339 |
&gradients, |
| 340 |
std::vector<std::vector< |
| 341 |
std::vector<Tensor<2, spacedim, typename VectorType::value_type>>>> |
| 342 |
&hessians, |
| 343 |
const FEValuesBase<dim, spacedim> &fe, |
| 344 |
const std::vector<types::global_dof_index> &index, |
| 345 |
const unsigned int component, |
| 346 |
const unsigned int n_comp, |
| 347 |
const unsigned int start, |
| 348 |
const unsigned int size) const override; |
| 349 |
|
| 350 |
virtual void |
| 351 |
mg_fill( |
| 352 |
std::vector<std::vector<std::vector<typename VectorType::value_type>>> |
| 353 |
&values, |
| 354 |
std::vector<std::vector< |
| 355 |
std::vector<Tensor<1, spacedim, typename VectorType::value_type>>>> |
| 356 |
&gradients, |
| 357 |
std::vector<std::vector< |
| 358 |
std::vector<Tensor<2, spacedim, typename VectorType::value_type>>>> |
| 359 |
&hessians, |
| 360 |
const FEValuesBase<dim, spacedim> &fe, |
| 361 |
const unsigned int level, |
| 362 |
const std::vector<types::global_dof_index> &index, |
| 363 |
const unsigned int component, |
| 364 |
const unsigned int n_comp, |
| 365 |
const unsigned int start, |
| 366 |
const unsigned int size) const override; |
| 367 |
|
| 368 |
/** |
| 369 |
* The memory used by this object. |
| 370 |
*/ |
| 371 |
std::size_t |
| 372 |
memory_consumption() const; |
| 373 |
}; |
| 374 |
|
| 375 |
|
| 376 |
/** |
| 377 |
* Based on VectorSelector, this is the class that implements the function |
| 378 |
* VectorDataBase::fill() for a certain type of multilevel vectors, using |
| 379 |
* AnyData to identify vectors by name. |
| 380 |
* |
| 381 |
* @ingroup MeshWorker |
| 382 |
*/ |
| 383 |
template <typename VectorType, int dim, int spacedim = dim> |
| 384 |
class MGVectorData : public VectorData<VectorType, dim, spacedim> |
| 385 |
{ |
| 386 |
public: |
| 387 |
/** |
| 388 |
* Constructor. |
| 389 |
*/ |
| 390 |
MGVectorData() = default; |
| 391 |
|
| 392 |
/** |
| 393 |
* Constructor using a prefilled VectorSelector |
| 394 |
*/ |
| 395 |
MGVectorData(const VectorSelector &); |
| 396 |
|
| 397 |
/** |
| 398 |
* Initialize with an object of named vectors |
| 399 |
*/ |
| 400 |
void |
| 401 |
initialize(const AnyData &); |
| 402 |
|
| 403 |
/** |
| 404 |
* Initialize with a single vector and cache the indices in the |
| 405 |
* VectorSelector base class. |
| 406 |
* |
| 407 |
* @note Make sure the VectorSelector base class was filled with |
| 408 |
* reasonable data before calling this function. |
| 409 |
*/ |
| 410 |
void |
| 411 |
initialize(const MGLevelObject<VectorType> *, const std::string &name); |
| 412 |
}; |
| 413 |
|
| 414 |
|
| 415 |
//----------------------------------------------------------------------// |
| 416 |
|
| 417 |
inline void |
| 418 |
VectorSelector::add(const std::string &name, |
| 419 |
const bool values, |
| 420 |
const bool gradients, |
| 421 |
const bool hessians) |
| 422 |
{ |
| 423 |
if (values) |
| 424 |
value_selection.add(name); |
| 425 |
if (gradients) |
| 426 |
gradient_selection.add(name); |
| 427 |
if (hessians) |
| 428 |
hessian_selection.add(name); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
// inline void |
| 433 |
// VectorSelector::add(const std::string& name, |
| 434 |
// const unsigned int block, |
| 435 |
// bool values, bool gradients, bool hessians) |
| 436 |
//{ |
| 437 |
// if (values) value_selection.add(name, block); |
| 438 |
// if (gradients) gradient_selection.add(name, block); |
| 439 |
// if (hessians) hessian_selection.add(name, block); |
| 440 |
//} |
| 441 |
|
| 442 |
|
| 443 |
inline void |
| 444 |
VectorSelector::initialize(const AnyData &src) |
| 445 |
{ |
| 446 |
value_selection.initialize(src); |
| 447 |
gradient_selection.initialize(src); |
| 448 |
hessian_selection.initialize(src); |
| 449 |
} |
| 450 |
|
| 451 |
inline bool |
| 452 |
VectorSelector::empty() const |
| 453 |
{ |
| 454 |
return (value_selection.size() == 0 && gradient_selection.size() == 0 && |
| 455 |
hessian_selection.size() == 0); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
inline bool |
| 460 |
VectorSelector::has_values() const |
| 461 |
{ |
| 462 |
return value_selection.size() != 0; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
inline bool |
| 467 |
VectorSelector::has_gradients() const |
| 468 |
{ |
| 469 |
return gradient_selection.size() != 0; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
inline bool |
| 474 |
VectorSelector::has_hessians() const |
| 475 |
{ |
| 476 |
return hessian_selection.size() != 0; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
inline unsigned int |
| 481 |
VectorSelector::n_values() const |
| 482 |
{ |
| 483 |
return value_selection.size(); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
inline unsigned int |
| 488 |
VectorSelector::n_gradients() const |
| 489 |
{ |
| 490 |
return gradient_selection.size(); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
inline unsigned int |
| 495 |
VectorSelector::n_hessians() const |
| 496 |
{ |
| 497 |
return hessian_selection.size(); |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
inline unsigned int |
| 502 |
VectorSelector::value_index(const unsigned int i) const |
| 503 |
{ |
| 504 |
return value_selection(i); |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
inline unsigned int |
| 509 |
VectorSelector::gradient_index(const unsigned int i) const |
| 510 |
{ |
| 511 |
return gradient_selection(i); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
inline unsigned int |
| 516 |
VectorSelector::hessian_index(const unsigned int i) const |
| 517 |
{ |
| 518 |
return hessian_selection(i); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
template <typename StreamType> |
| 523 |
inline void |
| 524 |
VectorSelector::print(StreamType &s) const |
| 525 |
{ |
| 526 |
s << "values: " << n_values() << " gradients: " << n_gradients() |
| 527 |
<< " hessians: " << n_hessians() << std::endl; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
template <typename StreamType, typename DATA> |
| 532 |
inline void |
| 533 |
VectorSelector::print(StreamType &s, const AnyData &v) const |
| 534 |
{ |
| 535 |
s << "values: "; |
| 536 |
for (unsigned int i = 0; i < n_values(); ++i) |
| 537 |
s << " '" << v.name(value_selection(i)) << '\''; |
| 538 |
s << std::endl << "gradients:"; |
| 539 |
for (unsigned int i = 0; i < n_gradients(); ++i) |
| 540 |
s << " '" << v.name(gradient_selection(i)) << '\''; |
| 541 |
s << std::endl << "hessians: "; |
| 542 |
for (unsigned int i = 0; i < n_hessians(); ++i) |
| 543 |
s << " '" << v.name(hessian_selection(i)) << '\''; |
| 544 |
s << std::endl; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
inline std::size_t |
| 549 |
VectorSelector::memory_consumption() const |
| 550 |
{ |
| 551 |
return sizeof(*this); |
| 552 |
} |
| 553 |
} // namespace MeshWorker |
| 554 |
|
| 555 |
|
| 556 |
DEAL_II_NAMESPACE_CLOSE |
| 557 |
|
| 558 |
#endif |