From cd5d87e1bf3b6e1b33747614a4105f43ea5abb4f Mon Sep 17 00:00:00 2001 From: Luca Heltai Date: Mon, 15 Jun 2020 18:55:45 +0200 Subject: [PATCH] Particles as indexable. --- doc/news/changes/minor/20200615LucaHeltai | 3 + include/deal.II/particles/particle.h | 35 +++++++++++ include/deal.II/particles/particle_accessor.h | 34 ++++++++++ tests/particles/particle_09.cc | 54 ++++++++++++++++ tests/particles/particle_09.output | 4 ++ tests/particles/particle_handler_serial_08.cc | 62 +++++++++++++++++++ .../particle_handler_serial_08.output | 4 ++ 7 files changed, 196 insertions(+) create mode 100644 doc/news/changes/minor/20200615LucaHeltai create mode 100644 tests/particles/particle_09.cc create mode 100644 tests/particles/particle_09.output create mode 100644 tests/particles/particle_handler_serial_08.cc create mode 100644 tests/particles/particle_handler_serial_08.output diff --git a/doc/news/changes/minor/20200615LucaHeltai b/doc/news/changes/minor/20200615LucaHeltai new file mode 100644 index 0000000000..2bb30313f6 --- /dev/null +++ b/doc/news/changes/minor/20200615LucaHeltai @@ -0,0 +1,3 @@ +New: Particles::Particle and Particles::ParticleAccessor can now be used as +indexable in boost::rtree objects. +
(Luca Heltai, 2020/06/15) diff --git a/include/deal.II/particles/particle.h b/include/deal.II/particles/particle.h index dc239e1cae..9208c9ef94 100644 --- a/include/deal.II/particles/particle.h +++ b/include/deal.II/particles/particle.h @@ -432,4 +432,39 @@ namespace Particles DEAL_II_NAMESPACE_CLOSE + +namespace boost +{ + namespace geometry + { + namespace index + { + // Forward declaration of bgi::indexable + template + struct indexable; + + /** + * Make sure we can construct an RTree of Particles::Particle objects. + */ + template + struct indexable> + { + /** + * boost::rtree expects a const reference to an indexable object. For + * a Particles::Particle object, this is its reference location. + */ + using result_type = const dealii::Point &; + + result_type + operator()( + const dealii::Particles::Particle &particle) const + { + return particle.get_location(); + } + }; + + } // namespace index + } // namespace geometry +} // namespace boost + #endif diff --git a/include/deal.II/particles/particle_accessor.h b/include/deal.II/particles/particle_accessor.h index 415a69cddc..50a38480fc 100644 --- a/include/deal.II/particles/particle_accessor.h +++ b/include/deal.II/particles/particle_accessor.h @@ -252,4 +252,38 @@ namespace Particles DEAL_II_NAMESPACE_CLOSE +namespace boost +{ + namespace geometry + { + namespace index + { + // Forward declaration of bgi::indexable + template + struct indexable; + + /** + * Make sure we can construct an RTree from Particles::ParticleAccessor + * objects. + */ + template + struct indexable> + { + /** + * boost::rtree expects a const reference to an indexable object. For + * a Particles::Particle object, this is its reference location. + */ + using result_type = const dealii::Point &; + + result_type + operator()(const dealii::Particles::ParticleAccessor + &accessor) const + { + return accessor.get_location(); + } + }; + } // namespace index + } // namespace geometry +} // namespace boost + #endif diff --git a/tests/particles/particle_09.cc b/tests/particles/particle_09.cc new file mode 100644 index 0000000000..e7ebf02a4c --- /dev/null +++ b/tests/particles/particle_09.cc @@ -0,0 +1,54 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + +// Test constructing an rtree of particles. + +#include + +#include + +#include "../tests.h" + +namespace bgi = boost::geometry::index; + +template +void +test() +{ + const int n_particles = 10; + std::vector> particles(n_particles); + + unsigned int id = 0; + for (auto &p : particles) + { + p.set_location(random_point()); + p.set_id(id++); + } + + auto tree = pack_rtree(particles); + + auto p = random_point(); + for (const auto part : tree | bgi::adaptors::queried(bgi::nearest(p, 3))) + deallog << "Particle " << part.get_id() << " is close to " << p + << " (location = " << part.get_location() << ")" << std::endl; +} + +int +main() +{ + initlog(); + test<2, 2>(); +} diff --git a/tests/particles/particle_09.output b/tests/particles/particle_09.output new file mode 100644 index 0000000000..b7fe35021c --- /dev/null +++ b/tests/particles/particle_09.output @@ -0,0 +1,4 @@ + +DEAL::Particle 6 is close to 0.0163006 0.242887 (location = 0.364784 0.513401) +DEAL::Particle 4 is close to 0.0163006 0.242887 (location = 0.277775 0.553970) +DEAL::Particle 9 is close to 0.0163006 0.242887 (location = 0.141603 0.606969) diff --git a/tests/particles/particle_handler_serial_08.cc b/tests/particles/particle_handler_serial_08.cc new file mode 100644 index 0000000000..7b12e1cb6f --- /dev/null +++ b/tests/particles/particle_handler_serial_08.cc @@ -0,0 +1,62 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + +// Test constructing an rtree of particles from a ParticleHandler object. + +#include +#include + +#include + +#include +#include + +#include "../tests.h" + +namespace bgi = boost::geometry::index; + +template +void +test() +{ + Triangulation tria; + GridGenerator::hyper_cube(tria); + tria.refine_global(2); + + Particles::ParticleHandler particle_handler( + tria, StaticMappingQ1::mapping); + + const int n_particles = 10; + std::vector> particles(n_particles); + + for (auto &p : particles) + p = random_point(); + + particle_handler.insert_particles(particles); + + auto tree = pack_rtree(particle_handler.begin(), particle_handler.end()); + + auto p = random_point(); + for (const auto &part : tree | bgi::adaptors::queried(bgi::nearest(p, 3))) + deallog << "Particle " << part.get_id() << " is close to " << p + << " (location = " << part.get_location() << ")" << std::endl; +} + +int +main() +{ + initlog(); + test<2, 2>(); +} diff --git a/tests/particles/particle_handler_serial_08.output b/tests/particles/particle_handler_serial_08.output new file mode 100644 index 0000000000..3f1d4998b8 --- /dev/null +++ b/tests/particles/particle_handler_serial_08.output @@ -0,0 +1,4 @@ + +DEAL::Particle 6 is close to 0.0163006 0.242887 (location = 0.364784 0.513401) +DEAL::Particle 9 is close to 0.0163006 0.242887 (location = 0.141603 0.606969) +DEAL::Particle 4 is close to 0.0163006 0.242887 (location = 0.277775 0.553970) -- 2.39.5