Loading...
Searching...
No Matches
RgpotAdapter.h
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Adapter over rgpot potential kernels. Migrated pair potentials live in
7** librgpot (github.com/OmniPotentRPC/rgpot); eOn's config surface,
8** PotRegistry, force counters, and caching stay on this side.
9*/
10#pragma once
11
12#include <memory>
13#include <stdexcept>
14#include <type_traits>
15#include <vector>
16
17#include "eon/Parameters.h"
18#include "eon/Potential.h"
21
22#include "rgpot/ForceStructs.hpp"
23#include "rgpot/pot_caps.hpp"
24
32template <class RPot> class RgpotAdapter final : public eonc::Potential {
33public:
37 template <class Cfg>
38 requires(!std::is_base_of_v<eonc::IPluginLoader, std::remove_cvref_t<Cfg>>)
40 const Cfg &cfg)
42
43 template <class Cfg>
44 requires(!std::is_base_of_v<eonc::IPluginLoader, std::remove_cvref_t<Cfg>>)
46 const Cfg &cfg, eonc::IPluginLoader &loader)
47 : eonc::Potential(ptype, params), pot_(cfg), loader_(loader) {
48 finishConstruct(params);
49 }
50
53 : eonc::Potential(ptype, params), pot_(),
54 loader_(eonc::PluginLoader::instance()) {
55 finishConstruct(params);
56 }
57
59 eonc::IPluginLoader &loader)
60 : eonc::Potential(ptype, params), pot_(), loader_(loader) {
61 finishConstruct(params);
62 }
63
66 RPot &&kernel)
67 : RgpotAdapter(ptype, params, std::move(kernel),
68 eonc::PluginLoader::instance()) {}
69
71 RPot &&kernel, eonc::IPluginLoader &loader)
72 : eonc::Potential(ptype, params), pot_(std::move(kernel)),
73 loader_(loader) {
74 finishConstruct(params);
75 }
76
77 void force(long N, const double *R, const int *atomicNrs, double *F,
78 double *U, double *variance, const double *box) override {
79 rgpot::ForceInput in{static_cast<size_t>(N), R, atomicNrs, box};
80 rgpot::ForceOut out{F, 0.0, 0.0};
81 pot_.forceImpl(in, &out);
82 *U = out.energy;
83 if (variance != nullptr) {
84 *variance = out.variance;
85 }
86 storeStress(out);
87 }
88
89 [[nodiscard]] bool computesStress() const noexcept override {
90 return haveStress_;
91 }
92
95 [[nodiscard]] Matrix3d cauchyStress() const override {
96 if (!haveStress_) {
97 throw std::logic_error("rgpot kernel did not return a stress tensor");
98 }
99 return stress_;
100 }
101
106 [[nodiscard]] bool supportsBatchEvaluation() const noexcept override {
107 return pot_.caps().batched;
108 }
109
118 void forceBatch(long nSystems, long nAtoms, const double *const *positions,
119 const int *const *atomicNrs, double *const *forces,
120 double *energies, double *variances,
121 const double *const *boxes) override {
122 const auto n = static_cast<size_t>(nSystems);
123 std::vector<rgpot::ForceInput> in;
124 std::vector<rgpot::ForceOut> out;
125 in.reserve(n);
126 out.reserve(n);
127 for (size_t i = 0; i < n; ++i) {
128 in.push_back(rgpot::ForceInput{static_cast<size_t>(nAtoms), positions[i],
129 atomicNrs[i], boxes[i]});
130 out.push_back(rgpot::ForceOut{forces[i], 0.0, 0.0});
131 }
132
133 pot_.forceBatchImpl(
134 rgpot::ForceBatch{.nSystems = n, .in = in.data(), .out = out.data()});
135
136 haveStress_ = false;
137 for (size_t i = 0; i < n; ++i) {
138 energies[i] = out[i].energy;
139 if (variances != nullptr) {
140 variances[i] = out[i].variance;
141 }
142 if (out[i].has_stress) {
143 storeStress(out[i]);
144 }
146 }
147 }
148
149 [[nodiscard]] bool isThreadSafe() const noexcept override {
150 return pot_.caps().reentrancy == rgpot::Reentrancy::SharedInstance;
151 }
152
156 [[nodiscard]] bool requiresIsolatedMoleculeLayout() const noexcept override {
157 return !pot_.caps().periodic;
158 }
159
160 [[nodiscard]] bool needsPerImageInstance() const noexcept override {
161 const auto caps = pot_.caps();
162 return caps.perImageInstances ||
163 caps.reentrancy == rgpot::Reentrancy::PerInstance;
164 }
165
166 [[nodiscard]] bool isSharedInstanceThreadSafe() const noexcept override {
167 if (pot_.caps().reentrancy != rgpot::Reentrancy::SharedInstance) {
168 return false;
169 }
171 }
172
173 [[nodiscard]] const RPot &kernel() const noexcept { return pot_; }
174
177 [[nodiscard]] double hessianRange() const noexcept override {
179 }
180
181private:
182 void finishConstruct(const eonc::Parameters &params) {
183 loader_.add_config_paths(params.potential_options().potentialsPath);
184 validateCaps();
185 }
186
187 void validateCaps() const {
188 const auto caps = pot_.caps();
189 using R = rgpot::Reentrancy;
190 if (caps.reentrancy != R::SharedInstance &&
191 caps.reentrancy != R::PerInstance &&
192 caps.reentrancy != R::ProcessSerial) {
193 throw std::runtime_error("rgpot kernel advertised an unknown reentrancy");
194 }
195 }
196
197 void storeStress(const rgpot::ForceOut &out) {
198 haveStress_ = out.has_stress != 0;
199 if (!haveStress_) {
200 return;
201 }
202 for (int row = 0; row < 3; ++row) {
203 for (int col = 0; col < 3; ++col) {
204 stress_(row, col) = out.stress[row * 3 + col];
205 }
206 }
207 }
208
209 RPot pot_;
211 Matrix3d stress_{Matrix3d::Zero()};
212 bool haveStress_{false};
213};
214
217template <class RPot>
218std::unique_ptr<eonc::Potential>
220 return std::make_unique<RgpotAdapter<RPot>>(ptype, params);
221}
222
224template <class RPot, class Cfg>
225std::unique_ptr<eonc::Potential>
226makeRgpot(eonc::PotType ptype, const eonc::Parameters &params, const Cfg &cfg) {
227 return std::make_unique<RgpotAdapter<RPot>>(ptype, params, cfg);
228}
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition Eigen.h:35
std::unique_ptr< eonc::Potential > makeRgpotDefault(eonc::PotType ptype, const eonc::Parameters &params)
Factory arm helper for kernels whose parameters are fixed tabulated data with no eOn-side configurati...
std::unique_ptr< eonc::Potential > makeRgpot(eonc::PotType ptype, const eonc::Parameters &params, const Cfg &cfg)
Factory arm helper: construct the kernel from its config and wrap it.
RgpotAdapter(eonc::PotType ptype, const eonc::Parameters &params, RPot &&kernel)
Take a pre-built kernel (ExprPot and other move-only constructors).
bool isThreadSafe() const noexcept override
Whether this potential's force() can be called from multiple threads on the SAME instance.
bool supportsBatchEvaluation() const noexcept override
Reports whether the kernel serves a batch natively.
bool requiresIsolatedMoleculeLayout() const noexcept override
Kernels that do not implement periodic boundaries must not have their coordinates wrapped.
double hessianRange() const noexcept override
Hessian block range from HessianRangeOf<RPot>, not from whether the kernel happens to have a cutoff f...
eonc::IPluginLoader & loader_
RgpotAdapter(eonc::PotType ptype, const eonc::Parameters &params)
Kernels with no configuration surface default-construct in place.
bool computesStress() const noexcept override
True when force() leaves a Cauchy stress that cauchyStress() can read until the next force() on this ...
void force(long N, const double *R, const int *atomicNrs, double *F, double *U, double *variance, const double *box) override
bool isSharedInstanceThreadSafe() const noexcept override
Conservative gate for sharing one Potential instance across threads.
RgpotAdapter(eonc::PotType ptype, const eonc::Parameters &params, eonc::IPluginLoader &loader)
Test seam: injected loader, no process-default search-path mutation.
const RPot & kernel() const noexcept
void validateCaps() const
void storeStress(const rgpot::ForceOut &out)
void finishConstruct(const eonc::Parameters &params)
Matrix3d stress_
RgpotAdapter(eonc::PotType ptype, const eonc::Parameters &params, const Cfg &cfg)
Kernels construct in place from their config: several hold mutexes or other immovable state,...
bool needsPerImageInstance() const noexcept override
Whether NEB should create separate Potential instances per image for true parallel force evaluation.
Matrix3d cauchyStress() const override
Cauchy stress from the last force() that reported one.
RgpotAdapter(eonc::PotType ptype, const eonc::Parameters &params, const Cfg &cfg, eonc::IPluginLoader &loader)
Test seam: injected loader, no process-default search-path mutation.
void forceBatch(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes) override
Hands eOn's batch straight to the kernel.
RgpotAdapter(eonc::PotType ptype, const eonc::Parameters &params, RPot &&kernel, eonc::IPluginLoader &loader)
Test seam: injected loader, no process-default search-path mutation.
const potential_options_t & potential_options() const
static PluginLoader & instance()
Thread-safe singleton accessor (Meyer's pattern).
virtual bool isSharedInstanceThreadSafe() const noexcept
Conservative gate for sharing one Potential instance across threads.
Definition Potential.h:167
PotType ptype
Definition Potential.h:47
void recordForceCall() noexcept
Charge one evaluated geometry to this potential and its owning registry.
Potential(PotType a_ptype)
Production default: construction-scope registry, else PotRegistry::get().
RAII resource manager for the ARTn C library with global synchronization.
double acceptedHessianRange(double range) noexcept
Drop a non-positive or non-finite range so coloring stays off.
static constexpr double range(const RPot &) noexcept