ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
ContactTimes.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2025 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 "ContactTimes.hpp"
21
22#include <boost/archive/binary_iarchive.hpp>
23#include <boost/archive/binary_oarchive.hpp>
24#include <boost/iostreams/device/array.hpp>
25#include <boost/iostreams/stream.hpp>
26#include <boost/serialization/vector.hpp>
27
28#include <sstream>
29#include <string>
30
31namespace Accumulators {
32void ContactTimes::update(boost::mpi::communicator const &comm) {
33 if (comm.rank() != 0) {
34 return;
35 }
36 auto const &system = System::get_system();
37 auto time = system.get_sim_time();
38 auto dt = system.get_time_step();
39 auto dists = m_obs->operator()(comm);
40
41 // Initialize the bookkeeping vectors
42 if (contacts.empty()) {
43 contacts.resize(dists.size(), false);
44 }
45
46 if (first_contact_times.empty()) {
47 first_contact_times.resize(dists.size(), time);
48 }
49
50 for (std::size_t i = 0u; i < dists.size(); ++i) {
51 if (dists[i] < m_contact_threshold) { // distance below the threshold
52 if (not contacts[i]) { // but it was not before!
53 contacts[i] = true;
54 first_contact_times[i] = time;
55 }
56 } else { // distance above the threshold
57 if (contacts[i]) { // it was below before
58 // calculate the total contact time
59 auto contact_time = time - first_contact_times[i] - dt;
60 if (contact_time < dt) {
61 contact_time = 0.;
62 }
63 contacts[i] = false;
64 m_data.emplace_back(contact_time);
65 }
66 }
67 }
68}
69
71 std::stringstream ss;
72 boost::archive::binary_oarchive oa(ss);
73
74 oa << m_data;
75
76 return ss.str();
77}
78
79void ContactTimes::set_internal_state(std::string const &state) {
80 namespace iostreams = boost::iostreams;
81 iostreams::array_source src(state.data(), state.size());
82 iostreams::stream<iostreams::array_source> ss(src);
83 boost::archive::binary_iarchive ia(ss);
84
85 ia >> m_data;
86 m_system = nullptr;
87}
88} // namespace Accumulators
void const * m_system
for bookkeeping purposes
std::string get_internal_state() const final
void update(boost::mpi::communicator const &comm) override
void set_internal_state(std::string const &) final
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
System & get_system()