Loading...
Searching...
No Matches
Potential.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** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12#pragma once
13#include "EonLogger.h"
14
15#include "Eigen.h"
16#include "Parameters.h"
17#include "PotRegistry.h"
18#include <atomic>
19#include <memory>
20
21namespace eonc {
22
23class Potential {
24protected:
26
27private:
28 uint64_t m_registry_id;
30
31public:
32 std::atomic<size_t> forceCallCounter;
33
34 // Main Constructor (no Parameters dependency)
35 explicit Potential(PotType a_ptype)
36 : ptype{a_ptype},
37 m_registry_id{PotRegistry::get().on_created(a_ptype)},
38 m_created_at{PotRegistry::Clock::now()},
40
41 // Convenience constructor from Parameters (for backward compat)
42 Potential(PotType a_ptype, const Parameters &)
43 : Potential(a_ptype) {}
44
45 Potential(const Parameters &a_params)
46 : Potential(a_params.potential_options.potential) {}
47
52
53 // Does not take into account the fixed / free atoms
54 // Variance here is null when not needed and that's OK
55 void virtual force(long nAtoms, const double *positions, const int *atomicNrs,
56 double *forces, double *energy, double *variance,
57 const double *box) = 0;
58
59 std::tuple<double, AtomMatrix>
60 get_ef(const AtomMatrix &pos, const VectorXi &atmnrs, const Matrix3d &box);
61
62 [[nodiscard]] PotType getType() const { return this->ptype; }
63
66 [[nodiscard]] virtual bool isSurrogate() const noexcept { return false; }
67
70 [[nodiscard]] virtual bool requiresIsolatedMoleculeLayout() const noexcept {
71 return false;
72 }
73
79 [[nodiscard]] virtual bool isThreadSafe() const noexcept { return true; }
80
85 [[nodiscard]] virtual bool isSharedInstanceThreadSafe() const noexcept {
86 return isThreadSafe();
87 }
88
94 [[nodiscard]] virtual bool needsPerImageInstance() const noexcept {
95 return false;
96 }
97
101 [[nodiscard]] virtual bool supportsBatchEvaluation() const noexcept {
102 return false;
103 }
104
108 virtual void forceBatch(long nSystems, long nAtoms,
109 const double *const *positions,
110 const int *const *atomicNrs, double *const *forces,
111 double *energies, double *variances,
112 const double *const *boxes) {
113 for (long i = 0; i < nSystems; i++) {
114 double var = 0;
115 force(nAtoms, positions[i], atomicNrs[i], forces[i], &energies[i], &var,
116 boxes[i]);
117 if (variances)
118 variances[i] = var;
121 }
122 }
123};
124
125namespace helpers {
126std::shared_ptr<Potential> makePotential(const Parameters &params);
127std::shared_ptr<Potential> makePotential(PotType ptype,
128 const Parameters &params);
129} // namespace helpers
130
131} // namespace eonc
132
133using eonc::Potential;
Eigen::Matrix< double, 3, 3, eOnStorageOrder > Matrix3d
Definition Eigen.h:35
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
void on_destroyed(uint64_t id, PotType t, size_t force_calls, TimePoint created_at)
static PotRegistry & get() noexcept
Process-lifetime singleton.
void on_force_call(PotType t) noexcept
Clock::time_point TimePoint
Definition PotRegistry.h:27
Potential(PotType a_ptype, const Parameters &)
Definition Potential.h:42
std::atomic< size_t > forceCallCounter
Definition Potential.h:32
virtual bool supportsBatchEvaluation() const noexcept
Whether this potential supports batched evaluation of N systems in a single call.
Definition Potential.h:101
PotType getType() const
Definition Potential.h:62
virtual void force(long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box)=0
virtual ~Potential()
Definition Potential.h:48
virtual bool isSharedInstanceThreadSafe() const noexcept
Conservative gate for sharing one Potential instance across threads.
Definition Potential.h:85
virtual bool isSurrogate() const noexcept
Whether this is a surrogate (GP) potential.
Definition Potential.h:66
uint64_t m_registry_id
Definition Potential.h:28
virtual bool needsPerImageInstance() const noexcept
Whether NEB should create separate Potential instances per image for true parallel force evaluation.
Definition Potential.h:94
PotRegistry::TimePoint m_created_at
Definition Potential.h:29
virtual 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)
Evaluate forces for N systems in a single call.
Definition Potential.h:108
std::tuple< double, AtomMatrix > get_ef(const AtomMatrix &pos, const VectorXi &atmnrs, const Matrix3d &box)
PotType ptype
Definition Potential.h:25
virtual bool requiresIsolatedMoleculeLayout() const noexcept
True for molecular QM / non-PBC backends (NWChem socket, ASE ORCA/NWChem, …).
Definition Potential.h:70
Potential(const Parameters &a_params)
Definition Potential.h:45
virtual bool isThreadSafe() const noexcept
Whether this potential's force() can be called from multiple threads on the SAME instance.
Definition Potential.h:79
Potential(PotType a_ptype)
Definition Potential.h:35
std::shared_ptr< Potential > makePotential(const Parameters &params)
RAII resource manager for the ARTn C library with global synchronization.