ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
npt.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 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#include "npt.hpp"
20
21#ifdef ESPRESSO_NPT
22
23#include "BoxGeometry.hpp"
24#include "PropagationMode.hpp"
26#include "communication.hpp"
27#include "config/config.hpp"
29#include "errorhandling.hpp"
32#include "system/System.hpp"
33
34#include <utils/Vector.hpp>
36
37#include <boost/mpi/collectives.hpp>
38
39#include <algorithm>
40#include <cmath>
41#include <functional>
42#include <stdexcept>
43
45 boost::mpi::broadcast(comm_cart, nptiso->p_epsilon, 0);
46 boost::mpi::broadcast(comm_cart, nptiso->volume, 0);
47 boost::mpi::broadcast(comm_cart, npt_inst_pressure->p_inst, 0);
48}
49
51 System::System const &system) const {
52#ifdef ESPRESSO_ELECTROSTATICS
54 throw std::runtime_error("If electrostatics is being used you must "
55 "use the cubic box NpT.");
56 }
57#endif
58#ifdef ESPRESSO_DIPOLES
60 throw std::runtime_error("If magnetostatics is being used you must "
61 "use the cubic box NpT.");
62 }
63#endif
64}
65
67 Utils::Vector<bool, 3> const &rescale,
68 bool cubic_box)
69 : piston{piston}, inv_piston{piston}, p_ext{ext_pressure},
70 cubic_box{cubic_box} {
71
72 if (ext_pressure < 0.0) {
73 throw std::runtime_error("The external pressure must be positive");
74 }
75 if (piston <= 0.0) {
76 throw std::runtime_error("The piston mass must be positive");
77 }
78
79 /* set the NpT geometry */
80 for (auto const i : {0u, 1u, 2u}) {
81 if (rescale[i]) {
83 dimension += 1;
84 non_const_dim = static_cast<int>(i);
85 }
86 }
87
88 if (dimension == 0 or non_const_dim == -1) {
89 throw std::runtime_error(
90 "You must enable at least one of the x y z components "
91 "as fluctuating dimension(s) for box length motion");
92 }
93}
94
96 return {static_cast<bool>(geometry & nptgeom_dir[0]),
97 static_cast<bool>(geometry & nptgeom_dir[1]),
98 static_cast<bool>(geometry & nptgeom_dir[2])};
99}
100
101void System::System::npt_ensemble_init(bool recalc_forces) {
102 nptiso->inv_piston = 1. / nptiso->piston;
103 nptiso->volume =
104 std::pow(box_geo->length()[nptiso->non_const_dim], nptiso->dimension);
105 if (recalc_forces) {
106 npt_inst_pressure->p_inst = Utils::Vector2d{};
107 npt_inst_pressure->p_vir = Utils::Vector3d{};
108 npt_inst_pressure->p_vel = Utils::Vector3d{};
109 }
110
111 auto const particle_number = cell_structure->local_particles().size();
112 nptiso->particle_number =
113 boost::mpi::all_reduce(::comm_cart, particle_number, std::plus<>());
114
115 auto const dt = get_time_step();
116 nptiso->half_dt_inv_piston = 0.5 * dt * nptiso->inv_piston;
117 nptiso->half_dt_inv_piston_and_Nf = -nptiso->half_dt_inv_piston;
118 if (particle_number > 1) {
119 nptiso->half_dt_inv_piston_and_Nf *=
120 (1. + 1. / static_cast<double>(particle_number - 1));
121 }
122
123 auto &mass_list = nptiso->mass_list;
124 mass_list.clear();
125 for (auto &p : cell_structure->local_particles()) {
126 mass_list.emplace_back(p.mass());
127 }
128 mass_list.erase(std::ranges::unique(mass_list).begin(), mass_list.end());
130 if (::this_node == 0) {
131 mass_list.erase(std::ranges::unique(mass_list).begin(), mass_list.end());
132 std::ranges::sort(mass_list);
133 }
134 boost::mpi::broadcast(::comm_cart, mass_list, 0);
135}
136
138 if (has_npt_enabled()) {
139 npt_inst_pressure->p_vir[0] += energy;
140 }
141}
142#endif // ESPRESSO_NPT
Vector implementation and trait types for boost qvm interoperability.
Main system class.
void npt_ensemble_init(bool recalc_forces)
Reinitialize the NpT state.
Definition npt.cpp:101
void npt_add_virial_contribution(double energy)
Definition npt.cpp:137
void synchronize_npt_state()
Synchronize NpT state such as instantaneous and average pressure.
Definition npt.cpp:44
DEVICE_QUALIFIER constexpr size_type size() const noexcept
Definition Array.hpp:166
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
This file contains the errorhandling code for severe errors, like a broken bond or illegal parameter ...
void gather_buffer(std::vector< T, Allocator > &buffer, boost::mpi::communicator const &comm, int root=0)
Gather buffer with different size on each node.
Exports for the NpT code.
void coulomb_dipole_sanity_checks(System::System const &system) const
Definition npt.cpp:50
int geometry
geometry information for the NpT integrator.
Definition npt.hpp:72
static constexpr std::array< int, 3 > nptgeom_dir
Definition npt.hpp:88
int non_const_dim
An index to one of the non-constant dimensions.
Definition npt.hpp:85
Utils::Vector< bool, 3 > get_direction() const
Definition npt.cpp:95
NptIsoParameters()=default
int dimension
The number of dimensions in which NpT boxlength motion is coupled to particles.
Definition npt.hpp:75
double piston
mass of a virtual piston representing the shaken box
Definition npt.hpp:47