Loading...
Searching...
No Matches
MetatomicPotential.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
14#include "eon/Parameters.h"
15#include "eon/Potential.h"
16#include <mutex>
17
18// Metatomic and torch headers
19// These pragmas are included to suppress warnings from third-party libraries
20#pragma GCC diagnostic push
21#pragma GCC diagnostic ignored "-Wpedantic"
22#pragma GCC diagnostic ignored "-Wunused-parameter"
23#pragma GCC diagnostic ignored "-Wfloat-equal"
24#pragma GCC diagnostic ignored "-Wfloat-conversion"
25#pragma GCC diagnostic ignored "-Wsign-conversion"
26#pragma GCC diagnostic ignored "-Wold-style-cast"
27
28#include <torch/script.h>
29#include <torch/version.h>
30#if __has_include(<torch/cuda.h>)
31#include <torch/cuda.h>
32#endif
33#if __has_include(<torch/mps.h>)
34#include <torch/mps.h>
35#endif
36
37#include "metatensor/torch.hpp"
38#include "metatensor/torch/module.hpp"
39#include "metatomic/torch.hpp"
40
41#pragma GCC diagnostic pop
42
43// A Windows DLL exports nothing unless asked. The units that define this
44// constructor set EON_MTA_POT_BUILD so the shared pot exports it. Callers
45// stay undecorated: the Windows metatomic CI links a static archive, and
46// dllimport would look for an __imp_ symbol that archive does not have.
47#if defined(_WIN32) && defined(EON_MTA_POT_BUILD)
48#define EON_MTA_POT_API __declspec(dllexport)
49#else
50#define EON_MTA_POT_API
51#endif
52
65private:
68 // --- Metatomic and Torch members ---
69 metatensor_torch::Module model_;
70 metatomic_torch::ModelCapabilities capabilities_;
71 std::vector<metatomic_torch::NeighborListOptions> nl_requests_;
72 metatomic_torch::ModelEvaluationOptions evaluations_options_;
73
74 torch::ScalarType dtype_;
75 c10::DeviceType device_type_;
76 torch::Device device_;
78 // -- Variants / output keys
79 std::string energy_key_;
81 std::string nc_forces_key_; // empty => conservative (autograd) forces
82 bool non_conservative_{false};
83 bool random_rotation_{false};
85 // --- Uncertainty handling ---
86 // If non-positive, uncertainty checks are effectively disabled.
88
103 metatensor_torch::TensorBlock
104 computeNeighbors(metatomic_torch::NeighborListOptions request, long nAtoms,
105 const double *positions, const double *box,
106 const bool periodic[3]);
107
108public:
116 // The dlopen'd plugin cannot call Parameters::Parameters(): that
117 // constructor lives in the client library, which is linked later.
118 // random_seed is main.randomSeed; 0 and negative leave the torch RNG alone.
120 long random_seed = 0);
121
125 ~MetatomicPotential() override = default;
126
127 [[nodiscard]] std::shared_ptr<eonc::Potential>
128 clonePotential() const override;
129
148 void force(long nAtoms, const double *positions, const int *atomicNrs,
149 double *forces, double *energy, double *variance,
150 const double *box) override;
151
156 [[nodiscard]] bool isThreadSafe() const noexcept override { return false; }
157 [[nodiscard]] bool needsPerImageInstance() const noexcept override {
158 return false;
159 }
160
165 [[nodiscard]] bool supportsBatchEvaluation() const noexcept override {
166 return true;
167 }
168 void forceBatch(long nSystems, long nAtoms, const double *const *positions,
169 const int *const *atomicNrs, double *const *forces,
170 double *energies, double *variances,
171 const double *const *boxes) override;
172
173private:
174 struct CloneTag {};
176 void forceBatchNative(long nSystems, long nAtoms,
177 const double *const *positions,
178 const int *const *atomicNrs, double *const *forces,
179 double *energies, double *variances,
180 const double *const *boxes);
181 mutable std::mutex inference_mutex_;
182};
#define EON_MTA_POT_API
void force(long nAtoms, const double *positions, const int *atomicNrs, double *forces, double *energy, double *variance, const double *box) override
Calculates the energy and forces for a given atomic configuration.
void forceBatchNative(long nSystems, long nAtoms, const double *const *positions, const int *const *atomicNrs, double *const *forces, double *energies, double *variances, const double *const *boxes)
std::vector< metatomic_torch::NeighborListOptions > nl_requests_
bool needsPerImageInstance() const noexcept override
Whether NEB should create separate Potential instances per image for true parallel force evaluation.
bool supportsBatchEvaluation() const noexcept override
Batched evaluation via single shared instance.
metatomic_torch::ModelEvaluationOptions evaluations_options_
eonc::log::Scoped m_log
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
metatomic_torch::ModelCapabilities capabilities_
bool isThreadSafe() const noexcept override
Single shared instance, serialized via mutex.
EON_MTA_POT_API MetatomicPotential(const eonc::Parameters &params)
Constructor for the MetatomicPotential.
std::string energy_uncertainty_key_
eonc::Parameters::metatomic_options_t m_metatomic_opts
c10::DeviceType device_type_
metatensor_torch::TensorBlock computeNeighbors(metatomic_torch::NeighborListOptions request, long nAtoms, const double *positions, const double *box, const bool periodic[3])
Computes neighbor list using the vesin library.
~MetatomicPotential() override=default
Destructor.
std::shared_ptr< eonc::Potential > clonePotential() const override
Independent instance that does not reload from disk.
metatensor_torch::Module model_
torch::ScalarType dtype_
eonc::metatomic_options_t metatomic_options_t
Definition Parameters.h:77
RAII helper for class-scoped logging.
Definition EonLogger.h:238