version 3.11-dev
simpleparticlecloud.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3//
4// SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_PARTICLES_SIMPLE_PARTICLE_CLOUD_HH
13#define DUMUX_PARTICLES_SIMPLE_PARTICLE_CLOUD_HH
14
15#include <vector>
16#include <ranges>
17
19
21
22namespace Dumux::Particles {
23
34template<class P>
36{
37public:
38 using Particle = P;
39
41
45 void resize(std::size_t numParticles)
46 {
47 if (numParticles > particles_.size())
48 {
49 for (std::size_t pId = particles_.size(); pId < numParticles; ++pId)
50 particles_.emplace_back(pId, false);
51 if (particles_.size() != numParticles)
52 DUNE_THROW(Dune::Exception, "");
53 }
54 }
55
59 std::size_t size(bool onlyActive = true) const
60 {
61 if (onlyActive)
62 return std::count_if(particles_.begin(), particles_.end(), [](const auto& p){ return p.isActive(); });
63 else
64 return particles_.size();
65 }
66
76 {
77 auto pIt = std::find_if(particles_.begin(), particles_.end(), [](const auto& p){ return !p.isActive(); });
78 if (pIt == particles_.end())
79 {
80 std::cout << "-- running out of particles -- adding 10% more particles." << std::endl;
81 const auto oldSize = particles_.size();
82 this->resize(static_cast<std::size_t>(1.1*oldSize));
83 pIt = particles_.begin() + oldSize;
84 }
85
86 pIt->activate();
87 return *pIt;
88 }
89
94 {
95 particles_[p.id()].deactivate();
96 }
97
103 friend inline std::ranges::forward_range auto particles(const SimpleParticleCloud& cloud)
104 { return cloud.particles_ | std::views::filter([](const auto& p){ return p.isActive(); }); }
105
110 template<class Func>
111 void forEachActiveParticle(const Func& func)
112 {
113 parallelFor(particles_.size(), [&](std::size_t i){
114 if (particles_[i].isActive())
115 func(particles_[i]);
116 });
117 }
118
123 template<class Func>
124 void forEachParticle(const Func& func)
125 {
126 parallelFor(particles_.size(), [&](std::size_t i){
127 func(particles_[i]);
128 });
129 }
130private:
131 std::vector<Particle> particles_;
132};
133
134} // end namespace Dumux::Particles
135
136#ifndef DOXYGEN // hide from doxygen
137#ifdef DUMUX_HAVE_GRIDFORMAT
138#include <gridformat/gridformat.hpp>
139
140// make the SimpleParticleCloud compatible with GridFormat
141namespace GridFormat::Traits {
142
143template<typename P>
144struct Cells<Dumux::Particles::SimpleParticleCloud<P>> {
145 static std::ranges::forward_range auto get(const Dumux::Particles::SimpleParticleCloud<P>& c) {
146 return particles(c);
147 }
148};
149
150template<typename P>
151struct Points<Dumux::Particles::SimpleParticleCloud<P>>{
152 static std::ranges::forward_range auto get(const Dumux::Particles::SimpleParticleCloud<P>& c) {
153 return particles(c);
154 }
155};
156
157template<typename P>
158struct CellPoints<Dumux::Particles::SimpleParticleCloud<P>, P> {
159 static std::ranges::range auto get(const Dumux::Particles::SimpleParticleCloud<P>&, const P& p) {
160 return std::views::single(p);
161 }
162};
163
164template<typename P>
165struct CellType<Dumux::Particles::SimpleParticleCloud<P>, P> {
166 static auto get(const Dumux::Particles::SimpleParticleCloud<P>&, const P& p) {
167 return GridFormat::CellType::vertex;
168 }
169};
170
171template<typename P>
172struct PointCoordinates<Dumux::Particles::SimpleParticleCloud<P>, P> {
173 static GridFormat::Concepts::StaticallySizedRange auto get(const Dumux::Particles::SimpleParticleCloud<P>& c, const P& p) {
174 return p.position();
175 }
176};
177
178template<typename P>
179struct PointId<Dumux::Particles::SimpleParticleCloud<P>, P> {
180 static auto get(const Dumux::Particles::SimpleParticleCloud<P>& c, const P& p) {
181 return p.id();
182 }
183};
184
185template<typename P>
186struct NumberOfPoints<Dumux::Particles::SimpleParticleCloud<P>> {
187 static std::size_t get(const Dumux::Particles::SimpleParticleCloud<P>& c) {
188 return c.size();
189 }
190};
191
192template<typename P>
193struct NumberOfCells<Dumux::Particles::SimpleParticleCloud<P>> {
194 static std::size_t get(const Dumux::Particles::SimpleParticleCloud<P>& c) {
195 return c.size();
196 }
197};
198
199template<typename P>
200struct NumberOfCellPoints<Dumux::Particles::SimpleParticleCloud<P>, P> {
201 static std::size_t get(const Dumux::Particles::SimpleParticleCloud<P>& c, const P& p) {
202 return 1;
203 }
204};
205
206static_assert(GridFormat::Concepts::UnstructuredGrid<Dumux::Particles::SimpleParticleCloud<Dumux::Particles::Particle<1>>>);
207static_assert(GridFormat::Concepts::UnstructuredGrid<Dumux::Particles::SimpleParticleCloud<Dumux::Particles::Particle<2>>>);
208static_assert(GridFormat::Concepts::UnstructuredGrid<Dumux::Particles::SimpleParticleCloud<Dumux::Particles::Particle<3>>>);
209
210} // end namespace GridFormat::Traits
211#endif // DUMUX_HAVE_GRIDFORMAT
212#endif // DOXYGEN
213
214#endif
A simple implementation of a cloud of particles.
Definition: simpleparticlecloud.hh:36
void forEachParticle(const Func &func)
For each particle apply a function.
Definition: simpleparticlecloud.hh:124
void deactivateParticle(const Particle &p)
Deactivate a given particle.
Definition: simpleparticlecloud.hh:93
friend std::ranges::forward_range auto particles(const SimpleParticleCloud &cloud)
Const range generator for iterating over all active particles.
Definition: simpleparticlecloud.hh:103
Particle & spawnParticle()
Bring one inactive particle to life Activates one particle from the pool of inactive particles.
Definition: simpleparticlecloud.hh:75
void forEachActiveParticle(const Func &func)
For each active particle apply a function.
Definition: simpleparticlecloud.hh:111
void resize(std::size_t numParticles)
Resize the cloud to contain numParticles particles.
Definition: simpleparticlecloud.hh:45
P Particle
Definition: simpleparticlecloud.hh:38
std::size_t size(bool onlyActive=true) const
the number of particles in the cloud
Definition: simpleparticlecloud.hh:59
void parallelFor(const std::size_t count, const FunctorType &functor)
A parallel for loop (multithreading)
Definition: parallel_for.hh:160
Definition: fokkerplanck.hh:31
Definition: adapt.hh:17
Parallel for loop (multithreading)
A simple particle.