41#include <boost/mpi/collectives/gather.hpp>
53#include <unordered_map>
58static int coord(std::string
const &s) {
65 throw std::invalid_argument(
"Invalid Cartesian coordinate: '" + s +
"'");
90 auto const error_msg = std::string(
"Parameter 'node_grid'");
94 assert(new_node_grid == old_node_grid);
99 if (n_nodes_new != n_nodes_old) {
100 std::stringstream reason;
101 reason <<
": MPI world size " << n_nodes_old <<
" incompatible "
102 <<
"with new node grid [" << new_node_grid <<
"]";
103 throw std::invalid_argument(error_msg + reason.str());
115 []() { return ::communicator.node_grid; }},
120 if (
context()->is_head_node()) {
121 throw std::domain_error(
"Parameter 'skin' must be >= 0");
138 auto const hd = get_hybrid_decomposition();
139 auto const ns_types = hd.get_n_square_types();
140 return Variant{std::vector<int>(ns_types.begin(), ns_types.end())};
148 auto const rd = get_regular_decomposition();
149 auto const fcb = rd.fully_connected_boundary();
152 return Variant{std::unordered_map<std::string, Variant>{
162 auto const hd = get_hybrid_decomposition();
163 return Variant{hd.get_cutoff_regular()};
166 [
this]() {
return get_system().nonbonded_ias->maximal_cutoff(); }},
168 [
this]() {
return get_system().bonded_ias->maximal_cutoff(); }},
170 [
this]() {
return get_system().get_interaction_range(); }},
176 if (
name ==
"initialize") {
178 auto const cs_type = cs_name_to_type.at(cs_name);
179 initialize(cs_type, params);
182 if (
name ==
"resort") {
184 return mpi_resort_particles(global_flag);
186 if (
name ==
"get_state") {
190 auto const rd = get_regular_decomposition();
191 state[
"cell_grid"] =
Variant{rd.cell_grid};
192 state[
"cell_size"] =
Variant{rd.cell_size};
194 auto const hd = get_hybrid_decomposition();
195 state[
"cell_grid"] =
Variant{hd.get_cell_grid()};
196 state[
"cell_size"] =
Variant{hd.get_cell_size()};
197 mpi_resort_particles(
true);
198 state[
"parts_per_decomposition"] =
199 Variant{std::unordered_map<std::string, Variant>{
200 {
"regular", hd.count_particles_in_regular()},
201 {
"n_square", hd.count_particles_in_n_square()}}};
205 state[
"omp_num_threads"] = omp_get_max_threads();
208 if (
name ==
"get_pairs") {
209 std::vector<Variant> out;
212 system.on_observable_calc();
213 std::vector<std::pair<int, int>> pair_list;
215 if (std::get_if<std::string>(¶ms.at(
"types"))) {
218 throw std::invalid_argument(
"Unknown argument types='" + key +
"'");
226 std::ranges::transform(pair_list, std::back_inserter(out),
227 [](
auto const &pair) {
228 return std::vector<int>{pair.first, pair.second};
233 if (
name ==
"get_neighbors") {
234 std::vector<std::vector<int>> neighbors_global;
237 system.on_observable_calc();
241 std::vector<int> neighbors_local;
243 neighbors_local = *ret;
245 boost::mpi::gather(
context()->get_comm(), neighbors_local,
246 neighbors_global, 0);
248 std::vector<int> neighbors;
249 for (
auto const &neighbors_local : neighbors_global) {
250 if (not neighbors_local.empty()) {
251 neighbors = neighbors_local;
257 if (
name ==
"non_bonded_loop_trace") {
259 system.on_observable_calc();
260 std::vector<Variant> out;
264 std::ranges::transform(
265 pair_list, std::back_inserter(out), [](
auto const &pair) {
266 return std::vector<Variant>{pair.id1, pair.id2, pair.pos1,
267 pair.pos2, pair.vec21, pair.node};
271 if (
name ==
"tune_skin") {
280 throw std::domain_error(
"Parameter 'tol' must be > 0");
282 if (int_steps <= 0) {
284 throw std::domain_error(
"Parameter 'int_steps' must be > 0");
287 throw std::domain_error(
"Parameter 'min_skin' must be >= 0");
290 throw std::domain_error(
"Parameter 'max_skin' must be >= 0");
292 if (max_skin < min_skin) {
293 throw std::domain_error(
"Parameter 'max_skin' must be >= 'min_skin'");
297 system.tune_verlet_skin(
298 min_skin, max_skin, tol, int_steps,
302 if (
name ==
"get_max_range") {
308std::vector<int> CellSystem::mpi_resort_particles(
bool global_flag)
const {
310 cell_structure.resort_particles(global_flag);
313 auto const size =
static_cast<int>(cell_structure.local_particles().size());
314 std::vector<int> n_part_per_node;
315 boost::mpi::gather(
context()->get_comm(), size, n_part_per_node, 0);
316 return n_part_per_node;
323 m_cell_structure->use_verlet_list = verlet;
326 auto const ns_types =
328 auto n_square_types = std::set<int>{ns_types.begin(), ns_types.end()};
329 m_cell_structure->set_hybrid_decomposition(cutoff_regular, n_square_types);
331 std::optional<std::pair<int, int>> fcb_pair = std::nullopt;
332 if (params.contains(
"fully_connected_boundary") and
333 not
is_none(params.at(
"fully_connected_boundary"))) {
337 fcb_pair = {{
coord(std::get<std::string>(variant.at(
"boundary"))),
338 coord(std::get<std::string>(variant.at(
"direction")))}};
342 m_cell_structure->set_regular_decomposition(
343 get_system().get_interaction_range(), fcb_pair);
346 system.set_cell_structure_topology(cs_type);
364 if (
name ==
"get_node_grid") {
365 return ::communicator.node_grid;
367 if (
name ==
"acquire_lock") {
370 }
else if (
name ==
"release_lock") {
CellStructureType
Cell structure topology.
@ HYBRID
Hybrid decomposition.
@ REGULAR
Regular decomposition.
static int coord(std::string const &s)
static std::string coord_letter(int c)
Vector implementation and trait types for boost qvm interoperability.
Data structures for bonded interactions.
std::optional< std::vector< int > > get_short_range_neighbors(System::System const &system, int const pid, double const distance)
Get ids of particles that are within a certain distance of another particle.
std::vector< PairInfo > non_bonded_loop_trace(System::System const &system, int const rank)
Returns pairs of particle ids, positions and distance as seen by the non-bonded loop.
std::vector< std::pair< int, int > > get_pairs_of_types(System::System const &system, double const distance, std::vector< int > const &types)
Get pairs closer than distance if both their types are in types.
std::vector< std::pair< int, int > > get_pairs(System::System const &system, double const distance)
Get pairs closer than distance from the cells.
This file contains everything related to the global cell structure / cell system.
void add_parameters(std::vector< AutoParameter > &¶ms)
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
auto & get_cell_structure() const
void configure(Particles::ParticleHandle &)
void do_construct(VariantMap const ¶ms) override
Variant do_call_method(std::string const &name, VariantMap const ¶ms) override
virtual void parallel_try_catch(std::function< void()> const &cb) const =0
virtual boost::mpi::communicator const & get_comm() const =0
VariantMap get_parameters() const
Get current parameters.
Context * context() const
Responsible context.
std::string_view name() const
void attach(std::weak_ptr<::System::System > system)
void attach(std::weak_ptr<::System::System > system)
auto const & get_system() const
std::weak_ptr<::System::System > m_system
Communicator communicator
This file contains the asynchronous MPI communication.
void initialize(Utils::Factory< ObjectHandle > *om)
constexpr bool is_none(Variant const &v)
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.
T get_value_or(VariantMap const &vals, std::string const &name, T const &default_)
Get a value from a VariantMap by name, or return a default value if it does not exist.
std::unordered_map< std::string, Variant > VariantMap
constexpr const None none
None-"literal".
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
T product(Vector< T, N > const &v)
Various procedures concerning interactions between particles.
void clear_particle_node()
Invalidate particle_node.
Particles creation and deletion.
static constexpr const ReadOnly read_only