ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/constraints/Constraints.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 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#ifndef CORE_CONSTRAINTS_CONSTRAINTS_HPP
20#define CORE_CONSTRAINTS_CONSTRAINTS_HPP
21
22#include "BoxGeometry.hpp"
23#include "Observable_stat.hpp"
24#include "system/System.hpp"
25
26#include <algorithm>
27#include <cassert>
28#include <memory>
29#include <stdexcept>
30#include <vector>
31
32namespace Constraints {
33template <class ParticleRange, class Constraint> class Constraints {
34 using container_type = std::vector<std::shared_ptr<Constraint>>;
35
36public:
37 using value_type = typename container_type::value_type;
38 using iterator = typename container_type::iterator;
39 using const_iterator = typename container_type::const_iterator;
40
41private:
42 void reset_forces() const {
43 for (auto const &constraint : *this) {
44 constraint->reset_force();
45 }
46 }
47
48 container_type m_constraints;
49
50public:
51 bool contains(std::shared_ptr<Constraint> const &constraint) const noexcept {
52 return std::find(begin(), end(), constraint) != end();
53 }
54 void add(std::shared_ptr<Constraint> const &constraint) {
55 auto &system = System::get_system();
56 auto const &box_geo = *system.box_geo;
57 if (not constraint->fits_in_box(box_geo.length())) {
58 throw std::runtime_error("Constraint not compatible with box size.");
59 }
60 assert(not contains(constraint));
61 m_constraints.emplace_back(constraint);
62 system.on_constraint_change();
63 }
64 void remove(std::shared_ptr<Constraint> const &constraint) {
65 auto &system = System::get_system();
66 assert(contains(constraint));
67 m_constraints.erase(std::remove(begin(), end(), constraint), end());
68 system.on_constraint_change();
69 }
70
71 iterator begin() { return m_constraints.begin(); }
72 iterator end() { return m_constraints.end(); }
73 const_iterator begin() const { return m_constraints.begin(); }
74 const_iterator end() const { return m_constraints.end(); }
75
76 void add_forces(BoxGeometry const &box_geo, ParticleRange &particles,
77 double time) const {
78 if (m_constraints.empty())
79 return;
80
81 reset_forces();
82
83 for (auto &p : particles) {
84 auto const pos = box_geo.folded_position(p.pos());
86 for (auto const &constraint : *this) {
87 force += constraint->force(p, pos, time);
88 }
89
90 p.force_and_torque() += force;
91 }
92 }
93
94 void add_energy(BoxGeometry const &box_geo, ParticleRange const &particles,
95 double time, Observable_stat &obs_energy) const {
96 if (m_constraints.empty())
97 return;
98
99 for (auto const &p : particles) {
100 auto const pos = box_geo.folded_position(p.pos());
101
102 for (auto const &constraint : *this) {
103 constraint->add_energy(p, pos, time, obs_energy);
104 }
105 }
106 }
107
108 void veto_boxl_change() const {
109 if (not m_constraints.empty()) {
110 throw std::runtime_error("The box size can not be changed because there "
111 "are active constraints.");
112 }
113 }
114
115 void on_boxl_change() const { veto_boxl_change(); }
116};
117} // namespace Constraints
118
119#endif
__global__ float * force
__shared__ int pos[MAXDEPTH *THREADS5/WARPSIZE]
auto folded_position(Utils::Vector3d const &p) const
Calculate coordinates folded to primary simulation box.
typename container_type::const_iterator const_iterator
void add_energy(BoxGeometry const &box_geo, ParticleRange const &particles, double time, Observable_stat &obs_energy) const
typename container_type::value_type value_type
bool contains(std::shared_ptr< Constraint > const &constraint) const noexcept
void add(std::shared_ptr< Constraint > const &constraint)
void add_forces(BoxGeometry const &box_geo, ParticleRange &particles, double time) const
typename container_type::iterator iterator
void remove(std::shared_ptr< Constraint > const &constraint)
Observable for the pressure and energy.
A range of particles.
System & get_system()
Force information on a particle.
Definition Particle.hpp:290