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
npt.cpp
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#include "npt.hpp"
20
21#ifdef NPT
22
23#include "PropagationMode.hpp"
24#include "communication.hpp"
25#include "config/config.hpp"
27#include "errorhandling.hpp"
30#include "system/System.hpp"
31
32#include <utils/Vector.hpp>
34
35#include <boost/mpi/collectives.hpp>
36
37#include <algorithm>
38#include <cmath>
39#include <stdexcept>
40
42 boost::mpi::broadcast(comm_cart, nptiso->p_epsilon, 0);
43 boost::mpi::broadcast(comm_cart, nptiso->volume, 0);
44 boost::mpi::broadcast(comm_cart, npt_inst_pressure->p_inst, 0);
45}
46
48 System::System const &system) const {
49#ifdef ELECTROSTATICS
50 if (dimension < 3 and not cubic_box and system.coulomb.impl->solver) {
51 throw std::runtime_error("If electrostatics is being used you must "
52 "use the cubic box NpT.");
53 }
54#endif
55#ifdef DIPOLES
56 if (dimension < 3 and not cubic_box and system.dipoles.impl->solver) {
57 throw std::runtime_error("If magnetostatics is being used you must "
58 "use the cubic box NpT.");
59 }
60#endif
61}
62
63NptIsoParameters::NptIsoParameters(double ext_pressure, double piston,
64 Utils::Vector<bool, 3> const &rescale,
65 bool cubic_box)
66 : piston{piston}, inv_piston{piston}, p_ext{ext_pressure},
67 cubic_box{cubic_box} {
68
69 if (ext_pressure < 0.0) {
70 throw std::runtime_error("The external pressure must be positive");
71 }
72 if (piston <= 0.0) {
73 throw std::runtime_error("The piston mass must be positive");
74 }
75
76 /* set the NpT geometry */
77 for (auto const i : {0u, 1u, 2u}) {
78 if (rescale[i]) {
80 dimension += 1;
81 non_const_dim = static_cast<int>(i);
82 }
83 }
84
85 if (dimension == 0 or non_const_dim == -1) {
86 throw std::runtime_error(
87 "You must enable at least one of the x y z components "
88 "as fluctuating dimension(s) for box length motion");
89 }
90}
91
93 return {static_cast<bool>(geometry & nptgeom_dir[0]),
94 static_cast<bool>(geometry & nptgeom_dir[1]),
95 static_cast<bool>(geometry & nptgeom_dir[2])};
96}
97
98void System::System::npt_ensemble_init(bool recalc_forces) {
99 nptiso->inv_piston = 1. / nptiso->piston;
100 nptiso->volume =
101 std::pow(box_geo->length()[nptiso->non_const_dim], nptiso->dimension);
102 if (recalc_forces) {
103 npt_inst_pressure->p_inst = Utils::Vector2d{};
104 npt_inst_pressure->p_vir = Utils::Vector3d{};
105 npt_inst_pressure->p_vel = Utils::Vector3d{};
106 }
107
108 auto &mass_list = nptiso->mass_list;
109 mass_list.clear();
110 for (auto &p : cell_structure->local_particles()) {
111 mass_list.emplace_back(p.mass());
112 }
113 mass_list.erase(std::ranges::unique(mass_list).begin(), mass_list.end());
115 if (::this_node == 0) {
116 mass_list.erase(std::ranges::unique(mass_list).begin(), mass_list.end());
117 std::ranges::sort(mass_list);
118 }
119 boost::mpi::broadcast(::comm_cart, mass_list, 0);
120}
121
123 if (propagation->integ_switch == INTEG_METHOD_NPT_ISO) {
124 npt_inst_pressure->p_vir[0] += energy;
125 }
126}
127
129 Utils::Vector3d const &d) {
130 if (propagation->integ_switch == INTEG_METHOD_NPT_ISO) {
131 npt_inst_pressure->p_vir += hadamard_product(force, d);
132 }
133}
134#endif // NPT
@ INTEG_METHOD_NPT_ISO
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:98
void npt_add_virial_contribution(double energy)
Definition npt.cpp:122
void synchronize_npt_state()
Synchronize NpT state such as instantaneous and average pressure.
Definition npt.cpp:41
boost::mpi::communicator comm_cart
The communicator.
int this_node
The number of this node.
This file contains the defaults for ESPResSo.
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:47
int geometry
geometry information for the NpT integrator.
Definition npt.hpp:65
static constexpr std::array< int, 3 > nptgeom_dir
Definition npt.hpp:81
int non_const_dim
An index to one of the non-constant dimensions.
Definition npt.hpp:78
Utils::Vector< bool, 3 > get_direction() const
Definition npt.cpp:92
NptIsoParameters()=default
int dimension
The number of dimensions in which NpT boxlength motion is coupled to particles.
Definition npt.hpp:68
double piston
mass of a virtual piston representing the shaken box
Definition npt.hpp:46