Loading [MathJax]/extensions/TeX/AMSmath.js
ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
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 "system/System.hpp"
25
26#include <cassert>
27#include <memory>
28#include <stdexcept>
29#include <vector>
30
31namespace Constraints {
32
33void Constraints::add(std::shared_ptr<Constraint> const &constraint) {
34 auto &system = get_system();
35 if (not constraint->fits_in_box(system.box_geo->length())) {
36 throw std::runtime_error("Constraint not compatible with box size.");
37 }
38 assert(not contains(constraint));
39 m_constraints.emplace_back(constraint);
40 system.on_constraint_change();
41}
42void Constraints::remove(std::shared_ptr<Constraint> const &constraint) {
43 auto &system = get_system();
44 assert(contains(constraint));
45 std::erase(m_constraints, constraint);
46 system.on_constraint_change();
47}
48void Constraints::add_forces(ParticleRange &particles, double time) const {
49 if (m_constraints.empty())
50 return;
51
52 reset_forces();
53 auto const &box_geo = *get_system().box_geo;
54
55 for (auto &p : particles) {
56 auto const pos = box_geo.folded_position(p.pos());
57 ParticleForce force{};
58 for (auto const &constraint : *this) {
59 force += constraint->force(p, pos, time);
60 }
61
62 p.force_and_torque() += force;
63 }
64}
65
66void Constraints::add_energy(ParticleRange const &particles, double time,
67 Observable_stat &obs_energy) const {
68 if (m_constraints.empty())
69 return;
70
71 auto const &box_geo = *get_system().box_geo;
72
73 for (auto const &p : particles) {
74 auto const pos = box_geo.folded_position(p.pos());
75
76 for (auto const &constraint : *this) {
77 constraint->add_energy(p, pos, time, obs_energy);
78 }
79 }
80}
81
82} // 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:290