ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
Constraints.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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 "Constraints.hpp"
21#include "BoxGeometry.hpp"
22#include "Constraint.hpp"
23#include "Observable_stat.hpp"
24#include "ParticleRange.hpp"
25#include "system/System.hpp"
26
27#include <cassert>
28#include <memory>
29#include <stdexcept>
30#include <vector>
31
32namespace Constraints {
33
34void Constraints::add(std::shared_ptr<Constraint> const &constraint) {
35 auto &system = get_system();
36 if (not constraint->fits_in_box(system.box_geo->length())) {
37 throw std::runtime_error("Constraint not compatible with box size.");
38 }
39 assert(not contains(constraint));
40 m_constraints.emplace_back(constraint);
41 system.on_constraint_change();
42}
43void Constraints::remove(std::shared_ptr<Constraint> const &constraint) {
44 auto &system = get_system();
45 assert(contains(constraint));
46 std::erase(m_constraints, constraint);
47 system.on_constraint_change();
48}
49void Constraints::add_forces(ParticleRange &particles, double time) const {
50 if (m_constraints.empty())
51 return;
52
53 reset_forces();
54 auto const &box_geo = *get_system().box_geo;
55
56 for (auto &p : particles) {
57 auto const pos = box_geo.folded_position(p.pos());
58 ParticleForce force{};
59 for (auto const &constraint : *this) {
60 force += constraint->force(p, pos, time);
61 }
62
63 p.force_and_torque() += force;
64 }
65}
66
67void Constraints::add_energy(ParticleRange const &particles, double time,
68 Observable_stat &obs_energy) const {
69 if (m_constraints.empty())
70 return;
71
72 auto const &box_geo = *get_system().box_geo;
73
74 for (auto const &p : particles) {
75 auto const pos = box_geo.folded_position(p.pos());
76
77 for (auto const &constraint : *this) {
78 constraint->add_energy(p, pos, time, obs_energy);
79 }
80 }
81}
82
83} // namespace Constraints
void remove(std::shared_ptr< Constraint > const &constraint)
void add_energy(ParticleRange const &particles, double time, Observable_stat &obs_energy) const
void add_forces(ParticleRange &particles, double time) const
void add(std::shared_ptr< Constraint > const &constraint)
Observable for the pressure and energy.
A range of particles.
std::shared_ptr< BoxGeometry > box_geo
Force information on a particle.
Definition Particle.hpp:345