// ------------------------------------------------------------------------ // // SPDX-License-Identifier: LGPL-2.1-or-later // Copyright (C) 2007 - 2024 by the deal.II authors // // This file is part of the deal.II library. // // Part of the source code is dual licensed under Apache-2.0 WITH // LLVM-exception OR LGPL-2.1-or-later. Detailed license information // governing the source code and code contributions can be found in // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. // // ------------------------------------------------------------------------ #ifndef dealii_fe_field_function_templates_h #define dealii_fe_field_function_templates_h #include #include #include #include #include #include #include #include #include #include #include DEAL_II_NAMESPACE_OPEN namespace Functions { template FEFieldFunction::FEFieldFunction( const DoFHandler &mydh, const VectorType &myv, const Mapping &mymapping) : Function( mydh.get_fe(0).n_components()) , dh(&mydh, "FEFieldFunction") , data_vector(myv) , mapping(mymapping) , cache(dh->get_triangulation(), mymapping) , cell_hint(dh->end()) {} template void FEFieldFunction::set_active_cell( const typename DoFHandler::active_cell_iterator &newcell) { cell_hint.get() = newcell; } template void FEFieldFunction::vector_value( const Point &p, Vector &values) const { Assert(values.size() == this->n_components, ExcDimensionMismatch(values.size(), this->n_components)); typename DoFHandler::active_cell_iterator cell = cell_hint.get(); if (cell == dh->end()) cell = dh->begin_active(); std::optional> qp = get_reference_coordinates(cell, p); if (!qp) { const std::pair< typename dealii::internal:: ActiveCellIterator>::type, Point> my_pair = GridTools::find_active_cell_around_point(mapping, *dh, p); AssertThrow(my_pair.first.state() == IteratorState::valid && !my_pair.first->is_artificial(), VectorTools::ExcPointNotAvailableHere()); cell = my_pair.first; qp = my_pair.second; } cell_hint.get() = cell; // check that the current cell is available: AssertThrow(!cell->is_artificial(), VectorTools::ExcPointNotAvailableHere()); // Now we can find out about the point Quadrature quad = *qp; FEValues fe_v(mapping, cell->get_fe(), quad, update_values); fe_v.reinit(cell); std::vector> vector_values( 1, Vector(values.size())); fe_v.get_function_values(data_vector, vector_values); values = vector_values[0]; } template typename VectorType::value_type FEFieldFunction::value( const Point &p, const unsigned int comp) const { Vector values(this->n_components); vector_value(p, values); return values(comp); } template void FEFieldFunction::vector_gradient( const Point &p, std::vector> &gradients) const { using number = typename VectorType::value_type; Assert(gradients.size() == this->n_components, ExcDimensionMismatch(gradients.size(), this->n_components)); typename DoFHandler::active_cell_iterator cell = cell_hint.get(); if (cell == dh->end()) cell = dh->begin_active(); std::optional> qp = get_reference_coordinates(cell, p); if (!qp) { const std::pair< typename dealii::internal:: ActiveCellIterator>::type, Point> my_pair = GridTools::find_active_cell_around_point(mapping, *dh, p); AssertThrow(my_pair.first.state() == IteratorState::valid && !my_pair.first->is_artificial(), VectorTools::ExcPointNotAvailableHere()); cell = my_pair.first; qp = my_pair.second; } // check that the current cell is available: AssertThrow(!cell->is_artificial(), VectorTools::ExcPointNotAvailableHere()); cell_hint.get() = cell; // Now we can find out about the point Quadrature quad = *qp; FEValues fe_v(mapping, cell->get_fe(), quad, update_gradients); fe_v.reinit(cell); if (this->n_components == 1) { // the size of the @p gradients coincidentally coincides // with the number of quadrature points we evaluate the function at. fe_v.get_function_gradients(data_vector, gradients); } else { // Unfortunately we still need a temporary argument as we want to // evaluate a gradient of a (generally) multicomponent function at // a single quadrature point. Note that the first std::vector<> is // related to the number of quadrature points (always one here), whereas // the second to the number of components. std::vector>> vgrads( 1, std::vector>(this->n_components)); fe_v.get_function_gradients(data_vector, vgrads); gradients = vgrads[0]; } } template Tensor<1, dim, typename VectorType::value_type> FEFieldFunction::gradient( const Point &p, const unsigned int comp) const { std::vector> grads( this->n_components); vector_gradient(p, grads); return grads[comp]; } template void FEFieldFunction::vector_laplacian( const Point &p, Vector &values) const { Assert(values.size() == this->n_components, ExcDimensionMismatch(values.size(), this->n_components)); typename DoFHandler::active_cell_iterator cell = cell_hint.get(); if (cell == dh->end()) cell = dh->begin_active(); std::optional> qp = get_reference_coordinates(cell, p); if (!qp) { const std::pair< typename dealii::internal:: ActiveCellIterator>::type, Point> my_pair = GridTools::find_active_cell_around_point(mapping, *dh, p); AssertThrow(my_pair.first.state() == IteratorState::valid && !my_pair.first->is_artificial(), VectorTools::ExcPointNotAvailableHere()); cell = my_pair.first; qp = my_pair.second; } // check that the current cell is available: AssertThrow(!cell->is_artificial(), VectorTools::ExcPointNotAvailableHere()); cell_hint.get() = cell; // Now we can find out about the point Quadrature quad = *qp; FEValues fe_v(mapping, cell->get_fe(), quad, update_hessians); fe_v.reinit(cell); std::vector> vector_values( 1, Vector(values.size())); fe_v.get_function_laplacians(data_vector, vector_values); values = vector_values[0]; } template typename VectorType::value_type FEFieldFunction::laplacian( const Point &p, const unsigned int comp) const { Vector lap(this->n_components); vector_laplacian(p, lap); return lap[comp]; } // Now the list versions // ============================== template void FEFieldFunction::vector_value_list( const std::vector> &points, std::vector> &values) const { Assert(points.size() == values.size(), ExcDimensionMismatch(points.size(), values.size())); std::vector::active_cell_iterator> cells; std::vector>> qpoints; std::vector> maps; const unsigned int n_cells = compute_point_locations(points, cells, qpoints, maps); // Create quadrature collection hp::QCollection quadrature_collection; for (unsigned int i = 0; i < n_cells; ++i) { // Number of quadrature points on this cell unsigned int nq = qpoints[i].size(); // Construct a quadrature formula std::vector ww(nq, 1. / nq); quadrature_collection.push_back(Quadrature(qpoints[i], ww)); } // Now gather all the information we need hp::MappingCollection mapping_collection(mapping); hp::FEValues fe_v(mapping_collection, dh->get_fe_collection(), quadrature_collection, update_values); for (unsigned int i = 0; i < n_cells; ++i) { AssertThrow(!cells[i]->is_artificial(), VectorTools::ExcPointNotAvailableHere()); fe_v.reinit(cells[i], i, 0); const unsigned int nq = qpoints[i].size(); std::vector> vector_values( nq, Vector(this->n_components)); fe_v.get_present_fe_values().get_function_values(data_vector, vector_values); for (unsigned int q = 0; q < nq; ++q) values[maps[i][q]] = vector_values[q]; } } template void FEFieldFunction::value_list( const std::vector> &points, std::vector &values, const unsigned int component) const { Assert(points.size() == values.size(), ExcDimensionMismatch(points.size(), values.size())); // Simply forward everything to the vector_value_list() // function. This requires a temporary object, but everything we // do here is so expensive that that really doesn't make any // difference any more. std::vector> vector_values( points.size(), Vector(this->n_components)); vector_value_list(points, vector_values); for (unsigned int q = 0; q < points.size(); ++q) values[q] = vector_values[q](component); } template void FEFieldFunction::vector_gradient_list( const std::vector> &points, std::vector>> &values) const { Assert(points.size() == values.size(), ExcDimensionMismatch(points.size(), values.size())); std::vector::active_cell_iterator> cells; std::vector>> qpoints; std::vector> maps; const unsigned int n_cells = compute_point_locations(points, cells, qpoints, maps); // Create quadrature collection hp::QCollection quadrature_collection; for (unsigned int i = 0; i < n_cells; ++i) { // Number of quadrature points on this cell unsigned int nq = qpoints[i].size(); // Construct a quadrature formula std::vector ww(nq, 1. / nq); quadrature_collection.push_back(Quadrature(qpoints[i], ww)); } // Now gather all the information we need hp::MappingCollection mapping_collection(mapping); hp::FEValues fe_v(mapping_collection, dh->get_fe_collection(), quadrature_collection, update_gradients); for (unsigned int i = 0; i < n_cells; ++i) { AssertThrow(!cells[i]->is_artificial(), VectorTools::ExcPointNotAvailableHere()); fe_v.reinit(cells[i], i, 0); const unsigned int nq = qpoints[i].size(); std::vector< std::vector>> vgrads(nq, std::vector>( this->n_components)); fe_v.get_present_fe_values().get_function_gradients(data_vector, vgrads); for (unsigned int q = 0; q < nq; ++q) { const unsigned int s = vgrads[q].size(); values[maps[i][q]].resize(s); for (unsigned int l = 0; l < s; ++l) values[maps[i][q]][l] = vgrads[q][l]; } } } template void FEFieldFunction::gradient_list( const std::vector> &points, std::vector> &values, const unsigned int component) const { Assert(points.size() == values.size(), ExcDimensionMismatch(points.size(), values.size())); // Simply forward everything to the vector_gradient_list() // function. This requires a temporary object, but everything we // do here is so expensive that that really doesn't make any // difference any more. std::vector>> vector_values( points.size(), std::vector>( this->n_components)); vector_gradient_list(points, vector_values); for (unsigned int q = 0; q < points.size(); ++q) values[q] = vector_values[q][component]; } template void FEFieldFunction::vector_laplacian_list( const std::vector> &points, std::vector> &values) const { Assert(points.size() == values.size(), ExcDimensionMismatch(points.size(), values.size())); std::vector::active_cell_iterator> cells; std::vector>> qpoints; std::vector> maps; const unsigned int n_cells = compute_point_locations(points, cells, qpoints, maps); // Create quadrature collection hp::QCollection quadrature_collection; for (unsigned int i = 0; i < n_cells; ++i) { // Number of quadrature points on this cell unsigned int nq = qpoints[i].size(); // Construct a quadrature formula std::vector ww(nq, 1. / nq); quadrature_collection.push_back(Quadrature(qpoints[i], ww)); } // Now gather all the information we need hp::MappingCollection mapping_collection(mapping); hp::FEValues fe_v(mapping_collection, dh->get_fe_collection(), quadrature_collection, update_hessians); // Now gather all the information we need for (unsigned int i = 0; i < n_cells; ++i) { AssertThrow(!cells[i]->is_artificial(), VectorTools::ExcPointNotAvailableHere()); fe_v.reinit(cells[i], i, 0); const unsigned int nq = qpoints[i].size(); std::vector> vector_values( nq, Vector(this->n_components)); fe_v.get_present_fe_values().get_function_laplacians(data_vector, vector_values); for (unsigned int q = 0; q < nq; ++q) values[maps[i][q]] = vector_values[q]; } } template void FEFieldFunction::laplacian_list( const std::vector> &points, std::vector &values, const unsigned int component) const { Assert(points.size() == values.size(), ExcDimensionMismatch(points.size(), values.size())); // Simply forward everything to the vector_gradient_list() // function. This requires a temporary object, but everything we // do here is so expensive that that really doesn't make any // difference any more. std::vector> vector_values( points.size(), Vector(this->n_components)); vector_laplacian_list(points, vector_values); for (unsigned int q = 0; q < points.size(); ++q) values[q] = vector_values[q](component); } template unsigned int FEFieldFunction::compute_point_locations( const std::vector> &points, std::vector::active_cell_iterator> &cells, std::vector>> &qpoints, std::vector> &maps) const { // Calling the GridTools routine and preparing output auto cell_qpoint_map = GridTools::compute_point_locations_try_all(cache, points, cell_hint.get()); const auto &tria_cells = std::get<0>(cell_qpoint_map); cells.resize(tria_cells.size()); unsigned int i = 0; for (const auto &c : tria_cells) cells[i++] = typename DoFHandler::cell_iterator(*c, dh); qpoints = std::get<1>(cell_qpoint_map); maps = std::get<2>(cell_qpoint_map); // Ensure that we found all points AssertThrow(std::get<3>(cell_qpoint_map).empty(), VectorTools::ExcPointNotAvailableHere()); return cells.size(); } template std::optional> FEFieldFunction::get_reference_coordinates( const typename DoFHandler::active_cell_iterator &cell, const Point &point) const { try { Point qp = mapping.transform_real_to_unit_cell(cell, point); if (GeometryInfo::is_inside_unit_cell(qp)) return qp; else return std::optional>(); } catch (const typename Mapping::ExcTransformationFailed &) { // transformation failed, so // assume the point is // outside return std::optional>(); } } } // namespace Functions DEAL_II_NAMESPACE_CLOSE #endif