AutoPas  3.0.0
Loading...
Searching...
No Matches
ContainerSelector.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <array>
10#include <vector>
11
15#include "autopas/containers/linkedCells/LinkedCellsReferences.h"
27
28namespace autopas {
29
39template <class Particle_T>
41 public:
48 ContainerSelector(const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax, double cutoff)
49 : _boxMin(boxMin), _boxMax(boxMax), _cutoff(cutoff), _currentContainer(nullptr), _currentInfo() {}
50
56 void selectContainer(ContainerOption containerOption, ContainerSelectorInfo containerInfo);
57
63 void resizeBox(const std::array<double, 3> &boxMin, const std::array<double, 3> &boxMax) {
64 _boxMin = boxMin;
65 _boxMax = boxMax;
66
67 _currentContainer = std::move(generateContainer(_currentContainer->getContainerType(), _currentInfo));
68 }
69
75
81
82 private:
89 std::unique_ptr<autopas::ParticleContainerInterface<Particle_T>> generateContainer(
90 ContainerOption containerChoice, ContainerSelectorInfo containerInfo);
91
92 std::array<double, 3> _boxMin, _boxMax;
93 const double _cutoff;
94 std::unique_ptr<autopas::ParticleContainerInterface<Particle_T>> _currentContainer;
95 ContainerSelectorInfo _currentInfo;
96};
97
98template <class Particle_T>
99std::unique_ptr<autopas::ParticleContainerInterface<Particle_T>> ContainerSelector<Particle_T>::generateContainer(
100 ContainerOption containerChoice, ContainerSelectorInfo containerInfo) {
101 std::unique_ptr<autopas::ParticleContainerInterface<Particle_T>> container;
102 switch (containerChoice) {
103 case ContainerOption::directSum: {
104 container = std::make_unique<DirectSum<Particle_T>>(_boxMin, _boxMax, _cutoff, containerInfo.verletSkin,
105 containerInfo.verletRebuildFrequency);
106 break;
107 }
108
109 case ContainerOption::linkedCells: {
110 container = std::make_unique<LinkedCells<Particle_T>>(_boxMin, _boxMax, _cutoff, containerInfo.verletSkin,
111 containerInfo.verletRebuildFrequency,
112 containerInfo.cellSizeFactor, containerInfo.loadEstimator);
113 break;
114 }
115 case ContainerOption::linkedCellsReferences: {
116 container = std::make_unique<LinkedCellsReferences<Particle_T>>(
117 _boxMin, _boxMax, _cutoff, containerInfo.verletSkin, containerInfo.verletRebuildFrequency,
118 containerInfo.cellSizeFactor);
119 break;
120 }
121 case ContainerOption::verletLists: {
122 container = std::make_unique<VerletLists<Particle_T>>(
123 _boxMin, _boxMax, _cutoff, containerInfo.verletSkin, containerInfo.verletRebuildFrequency,
124 VerletLists<Particle_T>::BuildVerletListType::VerletSoA, containerInfo.cellSizeFactor);
125 break;
126 }
127 case ContainerOption::verletListsCells: {
128 container = std::make_unique<VerletListsCells<Particle_T, VLCAllCellsNeighborList<Particle_T>>>(
129 _boxMin, _boxMax, _cutoff, containerInfo.verletSkin, containerInfo.verletRebuildFrequency,
130 containerInfo.cellSizeFactor, containerInfo.loadEstimator, VerletListsCellsHelpers::VLCBuildType::soaBuild);
131 break;
132 }
133 case ContainerOption::verletClusterLists: {
134 container = std::make_unique<VerletClusterLists<Particle_T>>(
135 _boxMin, _boxMax, _cutoff, containerInfo.verletSkin, containerInfo.verletRebuildFrequency,
136 containerInfo.verletClusterSize, containerInfo.loadEstimator);
137 break;
138 }
139 case ContainerOption::varVerletListsAsBuild: {
140 container = std::make_unique<VarVerletLists<Particle_T, VerletNeighborListAsBuild<Particle_T>>>(
141 _boxMin, _boxMax, _cutoff, containerInfo.verletSkin, containerInfo.verletRebuildFrequency,
142 containerInfo.cellSizeFactor);
143 break;
144 }
145
146 case ContainerOption::pairwiseVerletLists: {
147 container = std::make_unique<VerletListsCells<Particle_T, VLCCellPairNeighborList<Particle_T>>>(
148 _boxMin, _boxMax, _cutoff, containerInfo.verletSkin, containerInfo.verletRebuildFrequency,
149 containerInfo.cellSizeFactor, containerInfo.loadEstimator, VerletListsCellsHelpers::VLCBuildType::soaBuild);
150 break;
151 }
152 case ContainerOption::octree: {
153 container =
154 std::make_unique<Octree<Particle_T>>(_boxMin, _boxMax, _cutoff, containerInfo.verletSkin,
155 containerInfo.verletRebuildFrequency, containerInfo.cellSizeFactor);
156 break;
157 }
158 default: {
159 utils::ExceptionHandler::exception("ContainerSelector: Container type {} is not a known type!",
160 containerChoice.to_string());
161 }
162 }
163
164 // copy particles so they do not get lost when container is switched
165 if (_currentContainer != nullptr) {
166 // with these assumptions slightly more space is reserved as numParticlesTotal already includes halos
167 const auto numParticlesTotal = _currentContainer->size();
169 numParticlesTotal, _currentContainer->getBoxMin(), _currentContainer->getBoxMax(),
170 _currentContainer->getInteractionLength());
171
172 container->reserve(numParticlesTotal, numParticlesHalo);
173 for (auto particleIter = _currentContainer->begin(IteratorBehavior::ownedOrHalo); particleIter.isValid();
174 ++particleIter) {
175 // add particle as inner if it is owned
176 if (particleIter->isOwned()) {
177 container->addParticle(*particleIter);
178 } else {
179 container->addHaloParticle(*particleIter);
180 }
181 }
182 }
183
184 return container;
185}
186
187template <class Particle_T>
189 if (_currentContainer == nullptr) {
191 "ContainerSelector: getCurrentContainer() called before any container was selected!");
192 }
193 return *_currentContainer;
194}
195
196template <class Particle_T>
198 if (_currentContainer == nullptr) {
200 "ContainerSelector: getCurrentContainer() called before any container was selected!");
201 }
202 return *_currentContainer;
203}
204
205template <class Particle_T>
206void ContainerSelector<Particle_T>::selectContainer(ContainerOption containerOption,
207 ContainerSelectorInfo containerInfo) {
208 // Only do something if we have no container, a new type is required, or the info changed
209 if (_currentContainer == nullptr or _currentContainer->getContainerType() != containerOption or
210 _currentInfo != containerInfo) {
211 _currentContainer = std::move(generateContainer(containerOption, containerInfo));
212 _currentInfo = containerInfo;
213 }
214}
215} // namespace autopas
Info to generate a container.
Definition: ContainerSelectorInfo.h:17
Selector for a particle container.
Definition: ContainerSelector.h:40
void selectContainer(ContainerOption containerOption, ContainerSelectorInfo containerInfo)
Sets the container to the given option.
Definition: ContainerSelector.h:206
ContainerSelector(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, double cutoff)
Constructor for the ContainerSelecor class.
Definition: ContainerSelector.h:48
void resizeBox(const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax)
Set new boundaries, convert the container and reevaluate particle ownership.
Definition: ContainerSelector.h:63
autopas::ParticleContainerInterface< Particle_T > & getCurrentContainer()
Getter for the optimal container.
Definition: ContainerSelector.h:188
The ParticleContainerInterface class provides a basic interface for all Containers within AutoPas.
Definition: ParticleContainerInterface.h:37
@ VerletSoA
Build it using AoS.
Definition: VerletLists.h:47
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
size_t estimateNumHalosUniform(size_t numParticles, const std::array< double, 3 > &boxMin, const std::array< double, 3 > &boxMax, double haloWidth)
Given a number of particles and the dimensions of a box, estimate the number of halo particles.
Definition: NumParticlesEstimator.cpp:9
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32