ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
energy.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2026 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <config/config.hpp>
23
24#include "BoxGeometry.hpp"
25#include "Observable_stat.hpp"
26#include "Particle.hpp"
29#include "constraints/Constraints.hpp"
30#include "energy_cabana.hpp"
31#include "energy_inline.hpp"
36#include "short_range_loop.hpp"
38#include "system/System.hpp"
39
42
43#include <cmath>
44#include <cstddef>
45#include <memory>
46#include <optional>
47#include <span>
48#include <vector>
49
50namespace System {
51
52std::shared_ptr<Observable_stat> System::calculate_energy() {
53
54 auto obs_energy_ptr = std::make_shared<Observable_stat>(
55 1ul, static_cast<std::size_t>(bonded_ias->get_next_key()),
56 nonbonded_ias->get_max_seen_particle_type());
57
58 if (long_range_interactions_sanity_checks()) {
59 return obs_energy_ptr;
60 }
61
62 auto &obs_energy = *obs_energy_ptr;
63#if defined(ESPRESSO_CUDA) and \
64 (defined(ESPRESSO_ELECTROSTATICS) or defined(ESPRESSO_DIPOLES))
65 gpu->clear_energy_on_device();
66 gpu->update();
67#endif
68 on_observable_calc();
69
70 auto const local_parts = cell_structure->local_particles();
71
72 for (auto const &p : local_parts) {
73 obs_energy.kinetic_lin[0] += translational_kinetic_energy(p);
74 obs_energy.kinetic_rot[0] += rotational_kinetic_energy(p);
75 }
76
77 auto const coulomb_kernel = coulomb.pair_energy_kernel();
78 auto const dipoles_kernel = dipoles.pair_energy_kernel();
79
80#ifdef ESPRESSO_CALIPER
81 CALI_MARK_BEGIN("cabana_short_range");
82#endif
83 VerletCriterion<> const verlet_criterion{*this,
84 cell_structure->get_verlet_skin(),
85 get_interaction_range(),
86 coulomb.cutoff(),
87 dipoles.cutoff(),
89 update_cabana_state(*cell_structure, verlet_criterion,
90 get_interaction_range(), propagation->integ_switch);
91
92 EnergyBinLayout layout{
93 static_cast<std::size_t>(bonded_ias->get_next_key()),
94 std::size_t(nonbonded_ias->get_max_seen_particle_type() + 1)};
95
96 using exec = Kokkos::DefaultExecutionSpace;
98 "local_energy", exec().concurrency(), layout.total);
99 auto const &unique_particles = cell_structure->get_unique_particles();
100 auto const n_particles = static_cast<int>(unique_particles.size());
101 Kokkos::View<int *> mol_id_view("mol_id", n_particles);
102 auto mol_id_host = Kokkos::create_mirror_view(mol_id_view);
103 for (int i = 0; i < n_particles; ++i) {
104 mol_id_host(i) = unique_particles[i]->mol_id();
105 }
106 Kokkos::deep_copy(mol_id_view, mol_id_host);
107
108 // Non Bonded energies
109 EnergyKernel pair_e_kernel{*bonded_ias,
110 *nonbonded_ias,
111 coulomb,
112 get_ptr(coulomb_kernel),
113 get_ptr(dipoles_kernel),
114 *box_geo,
115 cell_structure->get_unique_particles(),
116 local_energy,
117 layout,
118 cell_structure->get_aosoa(),
119 mol_id_view,
120 maximal_cutoff()};
121
122 // Bonded energies: write a BondsEnergyKernelData + *BondsEnergyKernel
123 auto &bs = cell_structure->bond_state();
124 BondsEnergyKernelData bonds_e_data{*bonded_ias, *box_geo, local_energy,
125 layout, cell_structure->get_aosoa()};
126 PairBondsEnergyKernel pair_be_kernel{bonds_e_data, bs.pair_list, bs.pair_ids,
127 get_ptr(coulomb_kernel)};
128 AngleBondsEnergyKernel angle_be_kernel{bonds_e_data, bs.angle_list,
129 bs.angle_ids};
130 DihedralBondsEnergyKernel dih_be_kernel{bonds_e_data, bs.dihedral_list,
131 bs.dihedral_ids};
132
133 cabana_short_range(pair_be_kernel, angle_be_kernel, dih_be_kernel,
134 pair_e_kernel, *cell_structure, get_interaction_range(),
135 bonded_ias->maximal_cutoff(), verlet_criterion,
136 propagation->integ_switch);
137
138 reduce_cabana_energy(local_energy, layout, obs_energy, *bonded_ias,
139 nonbonded_ias->get_max_seen_particle_type() + 1);
140#ifdef ESPRESSO_CALIPER
141 CALI_MARK_END("cabana_short_range");
142#endif
143
144#ifdef ESPRESSO_ELECTROSTATICS
145 /* calculate k-space part of electrostatic interaction. */
146 obs_energy.coulomb[1] = coulomb.calc_energy_long_range();
147#endif
148
149#ifdef ESPRESSO_DIPOLES
150 /* calculate k-space part of magnetostatic interaction. */
151 obs_energy.dipolar[1] = dipoles.calc_energy_long_range();
152#endif
153
154 constraints->add_energy(local_parts, get_sim_time(), obs_energy);
155
156#if defined(ESPRESSO_CUDA) and \
157 (defined(ESPRESSO_ELECTROSTATICS) or defined(ESPRESSO_DIPOLES))
158 auto const energy_host = gpu->copy_energy_to_host();
159 if (!obs_energy.coulomb.empty())
160 obs_energy.coulomb[1] += static_cast<double>(energy_host.coulomb);
161 if (!obs_energy.dipolar.empty())
162 obs_energy.dipolar[1] += static_cast<double>(energy_host.dipolar);
163#endif
164
165 obs_energy.mpi_reduce();
166 return obs_energy_ptr;
167 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
168}
169
171 if (cell_structure->get_resort_particles()) {
172 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
173 }
174
175 auto ret = 0.0;
176 if (auto const p = cell_structure->get_local_particle(pid)) {
177 auto const coulomb_kernel = coulomb.pair_energy_kernel();
178 auto kernel = [&ret, this](Particle const &p, Particle const &p1,
179 Utils::Vector3d const &vec) {
180#ifdef ESPRESSO_EXCLUSIONS
181 if (not do_nonbonded(p, p1))
182 return;
183#endif
184 auto const &ia_params = nonbonded_ias->get_ia_param(p.type(), p1.type());
185 // Add energy for current particle pair to result
186 ret += calc_non_bonded_pair_energy(p, p1, ia_params, vec, vec.norm(),
187 *bonded_ias, coulomb, nullptr);
188 };
189 cell_structure->run_on_particle_short_range_neighbors(*p, kernel);
190 }
191 return ret;
192}
193
194std::optional<double> System::particle_bond_energy(int pid, int bond_id,
195 std::vector<int> partners) {
196 if (cell_structure->get_resort_particles()) {
197 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
198 }
199 Particle const *p = cell_structure->get_local_particle(pid);
200 if (not p or p->is_ghost())
201 return {}; // not available on this MPI rank or ghost
202 auto const &iaparams = *bonded_ias->at(bond_id);
203 try {
204 auto resolved_partners = cell_structure->resolve_bond_partners(partners);
205 auto const coulomb_kernel = coulomb.pair_energy_kernel();
206 return calc_bonded_energy(
207 iaparams, *p,
208 std::span(resolved_partners.data(), resolved_partners.size()), *box_geo,
209 get_ptr(coulomb_kernel));
210 } catch (const BondResolutionError &) {
211 bond_broken_error(p->id(), partners);
212 return {};
213 }
214}
215
216} // namespace System
void bond_broken_error(int id, std::span< const int > partner_ids)
double particle_short_range_energy_contribution(int pid)
Compute the short-range energy of a particle.
Definition energy.cpp:170
std::optional< double > particle_bond_energy(int pid, int bond_id, std::vector< int > partners)
Compute the energy of a given bond which has to exist on the given particle.
Definition energy.cpp:194
std::shared_ptr< Observable_stat > calculate_energy()
Calculate the total energy.
Definition energy.cpp:52
Returns true if the particles are to be considered for short range interactions.
constexpr double inactive_cutoff
Special cutoff value for an inactive interaction.
Definition config.hpp:53
const T * get_ptr(std::optional< T > const &opt)
static void reduce_cabana_energy(Kokkos::View< double **, Kokkos::LayoutRight > const &local_energy, EnergyBinLayout const &layout, Observable_stat &obs, BondedInteractionsMap const &bonded_ias, int n_types)
Energy calculation.
std::optional< double > calc_bonded_energy(Bonded_IA_Parameters const &iaparams, Particle const &p1, std::span< Particle * > partners, BoxGeometry const &box_geo, Coulomb::ShortRangeEnergyKernel::kernel_type const *kernel)
double translational_kinetic_energy(Particle const &p)
Calculate kinetic energies from translation for one particle.
double rotational_kinetic_energy(Particle const &p)
Calculate kinetic energies from rotation for one particle.
double calc_non_bonded_pair_energy(Particle const &p1, Particle const &p2, IA_parameters const &ia_params, Utils::Vector3d const &d, double const dist, BondedInteractionsMap const &bonded_ias, Coulomb::Solver const &coulomb, Coulomb::ShortRangeEnergyKernel::kernel_type const *coulomb_kernel)
Calculate non-bonded energies between a pair of particles.
bool do_nonbonded(Particle const &p1, Particle const &p2)
Determine if the non-bonded interactions between p1 and p2 should be calculated.
Various procedures concerning interactions between particles.
ESPRESSO_ATTR_ALWAYS_INLINE void update_cabana_state(CellStructure &cell_structure, auto const &verlet_criterion, double const pair_cutoff, auto const integ_switch)
void cabana_short_range(auto const &pair_bonds_kernel, auto const &angle_bonds_kernel, auto const &dihedral_bonds_kernel, auto const &nonbonded_kernel, CellStructure &cell_structure, double pair_cutoff, double bond_cutoff, auto const &verlet_criterion, auto const integ_switch)
Exception indicating that a particle id could not be resolved.
Struct holding all information for one particle.
Definition Particle.hpp:435
bool is_ghost() const
Definition Particle.hpp:480
auto const & id() const
Definition Particle.hpp:454