ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
AutoUpdateAccumulators.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2016-2024 The ESPResSo project
3
*
4
* This file is part of ESPResSo.
5
*
6
* ESPResSo 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 3 of the License, or
9
* (at your option) any later version.
10
*
11
* ESPResSo 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 this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
#include "
AutoUpdateAccumulators.hpp
"
21
22
#include <boost/mpi/communicator.hpp>
23
24
#include <algorithm>
25
#include <cassert>
26
#include <limits>
27
#include <numeric>
28
#include <vector>
29
30
namespace
Accumulators
{
31
32
void
AutoUpdateAccumulators::operator()
(boost::mpi::communicator
const
&comm,
33
int
steps) {
34
for
(
auto
&acc : m_accumulators) {
35
assert(steps <= acc.frequency);
36
acc.counter -= steps;
37
if
(acc.counter <= 0) {
38
acc.acc->update(comm);
39
acc.counter = acc.frequency;
40
}
41
42
assert(acc.counter > 0);
43
}
44
}
45
46
int
AutoUpdateAccumulators::next_update
()
const
{
47
return
std::accumulate(m_accumulators.begin(), m_accumulators.end(),
48
std::numeric_limits<int>::max(),
49
[](
int
a, AutoUpdateAccumulator
const
&acc) {
50
return std::min(a, acc.counter);
51
});
52
}
53
54
namespace
detail {
55
struct
MatchPredicate {
56
AccumulatorBase
const
*m_acc;
57
template
<
typename
T>
bool
operator()(T
const
&a)
const
{
58
return
a.acc == m_acc;
59
}
60
};
61
}
// namespace detail
62
63
void
AutoUpdateAccumulators::add
(
AccumulatorBase
*acc) {
64
assert(not
contains
(acc));
65
auto
const
*this_system = &
get_system
();
66
if
(acc->
has_same_system_handle
(
nullptr
)) {
67
acc->
override_system_handle
(this_system);
68
}
else
if
(not acc->
has_same_system_handle
(this_system)) {
69
throw
std::runtime_error(
"This accumulator is bound to another system"
);
70
}
71
m_accumulators.emplace_back(acc);
72
}
73
74
void
AutoUpdateAccumulators::remove
(
AccumulatorBase
*acc) {
75
assert(
contains
(acc));
76
std::erase_if(m_accumulators, detail::MatchPredicate{acc});
77
}
78
79
bool
AutoUpdateAccumulators::contains
(
AccumulatorBase
const
*acc)
const
{
80
assert(acc);
81
return
std::ranges::any_of(m_accumulators, detail::MatchPredicate{acc});
82
}
83
84
}
// namespace Accumulators
Accumulators::AccumulatorBase
Definition
core/accumulators/AccumulatorBase.hpp:37
Accumulators::AccumulatorBase::override_system_handle
void override_system_handle(::System::System const *system)
Definition
core/accumulators/AccumulatorBase.hpp:47
Accumulators::AccumulatorBase::has_same_system_handle
bool has_same_system_handle(::System::System const *system) const
Definition
core/accumulators/AccumulatorBase.hpp:44
Accumulators::AutoUpdateAccumulators::remove
void remove(AccumulatorBase *)
Definition
AutoUpdateAccumulators.cpp:74
Accumulators::AutoUpdateAccumulators::next_update
int next_update() const
Definition
AutoUpdateAccumulators.cpp:46
Accumulators::AutoUpdateAccumulators::contains
bool contains(AccumulatorBase const *) const
Definition
AutoUpdateAccumulators.cpp:79
Accumulators::AutoUpdateAccumulators::operator()
void operator()(boost::mpi::communicator const &comm, int steps)
Update accumulators.
Definition
AutoUpdateAccumulators.cpp:32
Accumulators::AutoUpdateAccumulators::add
void add(AccumulatorBase *)
Definition
AutoUpdateAccumulators.cpp:63
System::Leaf< AutoUpdateAccumulators >::get_system
auto & get_system()
Definition
core/system/Leaf.hpp:38
Accumulators
Definition
core/accumulators/AccumulatorBase.hpp:35
AutoUpdateAccumulators.hpp
src
core
accumulators
AutoUpdateAccumulators.cpp
Generated on Thu Dec 19 2024 02:13:15 for ESPResSo by
1.9.8