|
| 1 | +/* |
| 2 | + Copyright (C) 2024 by the authors of the ASPECT code. |
| 3 | +
|
| 4 | + This file is part of ASPECT. |
| 5 | +
|
| 6 | + ASPECT 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 2, or (at your option) |
| 9 | + any later version. |
| 10 | +
|
| 11 | + ASPECT 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 |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with ASPECT; see the file LICENSE. If not see |
| 18 | + <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <aspect/particle/interpolator/distance_weighted_average.h> |
| 22 | + |
| 23 | +#include <deal.II/base/signaling_nan.h> |
| 24 | +#include <deal.II/grid/grid_tools.h> |
| 25 | + |
| 26 | +namespace aspect |
| 27 | +{ |
| 28 | + namespace Particle |
| 29 | + { |
| 30 | + namespace Interpolator |
| 31 | + { |
| 32 | + namespace internal |
| 33 | + { |
| 34 | + double weight (const double distance, const double cell_width) |
| 35 | + { |
| 36 | + // linear weight function (hat function) |
| 37 | + return std::max(1.0 - (distance/cell_width),0.0); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + template <int dim> |
| 44 | + void |
| 45 | + DistanceWeightedAverage<dim>::update() |
| 46 | + { |
| 47 | + vertex_to_cell_map = GridTools::vertex_to_cell_map(this->get_triangulation()); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + template <int dim> |
| 53 | + std::vector<std::vector<double>> |
| 54 | + DistanceWeightedAverage<dim>::properties_at_points(const ParticleHandler<dim> &particle_handler, |
| 55 | + const std::vector<Point<dim>> &positions, |
| 56 | + const ComponentMask &selected_properties, |
| 57 | + const typename parallel::distributed::Triangulation<dim>::active_cell_iterator &cell) const |
| 58 | + { |
| 59 | + const unsigned int n_interpolate_positions = positions.size(); |
| 60 | + const unsigned int n_particle_properties = particle_handler.n_properties_per_particle(); |
| 61 | + |
| 62 | + // Create with signaling NaNs |
| 63 | + std::vector<std::vector<double>> cell_properties(n_interpolate_positions, |
| 64 | + std::vector<double>(n_particle_properties, |
| 65 | + numbers::signaling_nan<double>())); |
| 66 | + |
| 67 | + // Set requested properties to 0.0 |
| 68 | + for (unsigned int index_positions = 0; index_positions < n_interpolate_positions; ++index_positions) |
| 69 | + for (unsigned int index_properties = 0; index_properties < n_particle_properties; ++index_properties) |
| 70 | + if (selected_properties[index_properties]) |
| 71 | + cell_properties[index_positions][index_properties] = 0.0; |
| 72 | + |
| 73 | + std::set<typename Triangulation<dim>::active_cell_iterator> cell_and_neighbors; |
| 74 | + |
| 75 | + for (const auto v : cell->vertex_indices()) |
| 76 | + { |
| 77 | + const unsigned int vertex_index = cell->vertex_index(v); |
| 78 | + cell_and_neighbors.insert(vertex_to_cell_map[vertex_index].begin(), |
| 79 | + vertex_to_cell_map[vertex_index].end()); |
| 80 | + } |
| 81 | + |
| 82 | + const double interpolation_support = 0.5 * cell->diameter(); |
| 83 | + std::vector<double> integrated_weight(n_interpolate_positions,0.0); |
| 84 | + |
| 85 | + for (const auto ¤t_cell: cell_and_neighbors) |
| 86 | + { |
| 87 | + const typename ParticleHandler<dim>::particle_iterator_range particle_range = |
| 88 | + particle_handler.particles_in_cell(current_cell); |
| 89 | + |
| 90 | + for (const auto &particle: particle_range) |
| 91 | + { |
| 92 | + const ArrayView<const double> particle_properties = particle.get_properties(); |
| 93 | + unsigned int index_positions = 0; |
| 94 | + |
| 95 | + for (const auto &interpolation_point: positions) |
| 96 | + { |
| 97 | + const double distance = particle.get_location().distance(interpolation_point); |
| 98 | + const double weight = internal::weight(distance,interpolation_support); |
| 99 | + |
| 100 | + for (unsigned int index_properties = 0; index_properties < particle_properties.size(); ++index_properties) |
| 101 | + if (selected_properties[index_properties]) |
| 102 | + cell_properties[index_positions][index_properties] += weight * particle_properties[index_properties]; |
| 103 | + |
| 104 | + integrated_weight[index_positions] += weight; |
| 105 | + ++index_positions; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for (unsigned int index_positions = 0; index_positions < n_interpolate_positions; ++index_positions) |
| 111 | + { |
| 112 | + AssertThrow(integrated_weight[index_positions] > 0.0, ExcInternalError()); |
| 113 | + |
| 114 | + for (unsigned int index_properties = 0; index_properties < n_particle_properties; ++index_properties) |
| 115 | + if (selected_properties[index_properties]) |
| 116 | + cell_properties[index_positions][index_properties] /= integrated_weight[index_positions]; |
| 117 | + } |
| 118 | + |
| 119 | + return cell_properties; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +// explicit instantiations |
| 127 | +namespace aspect |
| 128 | +{ |
| 129 | + namespace Particle |
| 130 | + { |
| 131 | + namespace Interpolator |
| 132 | + { |
| 133 | + ASPECT_REGISTER_PARTICLE_INTERPOLATOR(DistanceWeightedAverage, |
| 134 | + "distance weighted average", |
| 135 | + "Interpolates particle properties onto a vector of points using a " |
| 136 | + "distance weighed averaging method.") |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments