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-2022 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"
28#include "constraints/Constraints.hpp"
29#include "energy_inline.hpp"
31#include "short_range_loop.hpp"
32#include "system/System.hpp"
33
36
37#include <cstddef>
38#include <memory>
39#include <optional>
40#include <span>
41#include <vector>
42
43namespace System {
44
45std::shared_ptr<Observable_stat> System::calculate_energy() {
46
47 auto obs_energy_ptr = std::make_shared<Observable_stat>(
48 1ul, static_cast<std::size_t>(bonded_ias->get_next_key()),
49 nonbonded_ias->get_max_seen_particle_type());
50
51 if (long_range_interactions_sanity_checks()) {
52 return obs_energy_ptr;
53 }
54
55 auto &obs_energy = *obs_energy_ptr;
56#if defined(ESPRESSO_CUDA) and \
57 (defined(ESPRESSO_ELECTROSTATICS) or defined(ESPRESSO_DIPOLES))
58 gpu.clear_energy_on_device();
59 gpu.update();
60#endif
61 on_observable_calc();
62
63 auto const local_parts = cell_structure->local_particles();
64
65 for (auto const &p : local_parts) {
66 obs_energy.kinetic_lin[0] += translational_kinetic_energy(p);
67 obs_energy.kinetic_rot[0] += rotational_kinetic_energy(p);
68 }
69
70 auto const coulomb_kernel = coulomb.pair_energy_kernel();
71 auto const dipoles_kernel = dipoles.pair_energy_kernel();
72
74 [this, coulomb_kernel_ptr = get_ptr(coulomb_kernel), &obs_energy](
75 Particle const &p1, int bond_id, std::span<Particle *> partners) {
76 auto const &iaparams = *bonded_ias->at(bond_id);
77 auto const result = calc_bonded_energy(iaparams, p1, partners, *box_geo,
78 coulomb_kernel_ptr);
79 if (result) {
80 obs_energy.bonded_contribution(bond_id)[0] += result.value();
81 return false;
82 }
83 return true;
84 },
85 [coulomb_kernel_ptr = get_ptr(coulomb_kernel),
86 dipoles_kernel_ptr = get_ptr(dipoles_kernel), this,
87 &obs_energy](Particle const &p1, Particle const &p2, Distance const &d) {
88 auto const &ia_params =
89 nonbonded_ias->get_ia_param(p1.type(), p2.type());
91 p1, p2, d.vec21, sqrt(d.dist2), d.dist2, ia_params, *bonded_ias,
92 coulomb, coulomb_kernel_ptr, dipoles_kernel_ptr, obs_energy);
93 },
94 *cell_structure, maximal_cutoff(), bonded_ias->maximal_cutoff());
95
96#ifdef ESPRESSO_ELECTROSTATICS
97 /* calculate k-space part of electrostatic interaction. */
98 obs_energy.coulomb[1] = coulomb.calc_energy_long_range();
99#endif
100
101#ifdef ESPRESSO_DIPOLES
102 /* calculate k-space part of magnetostatic interaction. */
103 obs_energy.dipolar[1] = dipoles.calc_energy_long_range();
104#endif
105
106 constraints->add_energy(local_parts, get_sim_time(), obs_energy);
107
108#if defined(ESPRESSO_CUDA) and \
109 (defined(ESPRESSO_ELECTROSTATICS) or defined(ESPRESSO_DIPOLES))
110 auto const energy_host = gpu.copy_energy_to_host();
111 if (!obs_energy.coulomb.empty())
112 obs_energy.coulomb[1] += static_cast<double>(energy_host.coulomb);
113 if (!obs_energy.dipolar.empty())
114 obs_energy.dipolar[1] += static_cast<double>(energy_host.dipolar);
115#endif
116
117 obs_energy.mpi_reduce();
118 return obs_energy_ptr;
119 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
120}
121
123 if (cell_structure->get_resort_particles()) {
124 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
125 }
126
127 auto ret = 0.0;
128 if (auto const p = cell_structure->get_local_particle(pid)) {
129 auto const coulomb_kernel = coulomb.pair_energy_kernel();
130 auto kernel = [coulomb_kernel_ptr = get_ptr(coulomb_kernel), &ret,
131 this](Particle const &p, Particle const &p1,
132 Utils::Vector3d const &vec) {
133#ifdef ESPRESSO_EXCLUSIONS
134 if (not do_nonbonded(p, p1))
135 return;
136#endif
137 auto const &ia_params = nonbonded_ias->get_ia_param(p.type(), p1.type());
138 // Add energy for current particle pair to result
139 ret +=
140 calc_non_bonded_pair_energy(p, p1, ia_params, vec, vec.norm(),
141 *bonded_ias, coulomb, coulomb_kernel_ptr);
142 };
143 cell_structure->run_on_particle_short_range_neighbors(*p, kernel);
144 }
145 return ret;
146}
147
148std::optional<double> System::particle_bond_energy(int pid, int bond_id,
149 std::vector<int> partners) {
150 if (cell_structure->get_resort_particles()) {
151 cell_structure->update_ghosts_and_resort_particle(get_global_ghost_flags());
152 }
153 Particle const *p = cell_structure->get_local_particle(pid);
154 if (not p or p->is_ghost())
155 return {}; // not available on this MPI rank or ghost
156 auto const &iaparams = *bonded_ias->at(bond_id);
157 try {
158 auto resolved_partners = cell_structure->resolve_bond_partners(partners);
159 auto const coulomb_kernel = coulomb.pair_energy_kernel();
160 return calc_bonded_energy(
161 iaparams, *p,
162 std::span(resolved_partners.data(), resolved_partners.size()), *box_geo,
163 get_ptr(coulomb_kernel));
164 } catch (const BondResolutionError &) {
165 bond_broken_error(p->id(), partners);
166 return {};
167 }
168}
169
170} // 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:122
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:148
std::shared_ptr< Observable_stat > calculate_energy()
Calculate the total energy.
Definition energy.cpp:45
const T * get_ptr(std::optional< T > const &opt)
Energy calculation.
void add_non_bonded_pair_energy(Particle const &p1, Particle const &p2, Utils::Vector3d const &d, double const dist, double const dist2, IA_parameters const &ia_params, BondedInteractionsMap const &bonded_ias, Coulomb::Solver const &coulomb, Coulomb::ShortRangeEnergyKernel::kernel_type const *coulomb_kernel, Dipoles::ShortRangeEnergyKernel::kernel_type const *dipoles_kernel, Observable_stat &obs_energy)
Add non-bonded and short-range Coulomb energies between a pair of particles to the energy observable.
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.
void short_range_loop(BondKernel bond_kernel, PairKernel pair_kernel, CellStructure &cell_structure, double pair_cutoff, double bond_cutoff, VerletCriterion const &verlet_criterion={})
Exception indicating that a particle id could not be resolved.
Distance vector and length handed to pair kernels.
Struct holding all information for one particle.
Definition Particle.hpp:450
auto const & type() const
Definition Particle.hpp:473
bool is_ghost() const
Definition Particle.hpp:495
auto const & id() const
Definition Particle.hpp:469