Program Listing for File common.h#
↰ Return to documentation for file (source/api_cc/include/common.h)
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include <cstdint>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "AtomMap.h"
#include "errors.h"
#include "neighbor_list.h"
#include "version.h"
namespace deepmd {
typedef double ENERGYTYPE;
enum DPBackend { TensorFlow, PyTorch, PyTorchExportable, Paddle, JAX, Unknown };
DPBackend get_backend(const std::string& model);
struct NeighborListData {
std::vector<int> ilist;
std::vector<std::vector<int>> jlist;
std::vector<int> numneigh;
std::vector<int*> firstneigh;
public:
void copy_from_nlist(const InputNlist& inlist, const int natoms = -1);
void shuffle(const std::vector<int>& fwd_map);
void shuffle(const deepmd::AtomMap& map);
void shuffle_exclude_empty(const std::vector<int>& fwd_map);
void make_inlist(InputNlist& inlist);
void padding();
};
bool model_compatable(std::string& model_version);
template <typename VALUETYPE>
void select_by_type(std::vector<int>& fwd_map,
std::vector<int>& bkw_map,
int& nghost_real,
const std::vector<VALUETYPE>& dcoord_,
const std::vector<int>& datype_,
const int& nghost,
const std::vector<int>& sel_type_);
template <typename VALUETYPE>
void select_real_atoms(std::vector<int>& fwd_map,
std::vector<int>& bkw_map,
int& nghost_real,
const std::vector<VALUETYPE>& dcoord_,
const std::vector<int>& datype_,
const int& nghost,
const int& ntypes);
template <typename VALUETYPE>
void select_real_atoms_coord(std::vector<VALUETYPE>& dcoord,
std::vector<int>& datype,
std::vector<VALUETYPE>& aparam,
int& nghost_real,
std::vector<int>& fwd_map,
std::vector<int>& bkw_map,
int& nall_real,
int& nloc_real,
const std::vector<VALUETYPE>& dcoord_,
const std::vector<int>& datype_,
const std::vector<VALUETYPE>& aparam_,
const int& nghost,
const int& ntypes,
const int& nframes,
const int& daparam,
const int& nall,
const bool aparam_nall = false);
template <typename VT>
void select_map(std::vector<VT>& out,
const std::vector<VT>& in,
const std::vector<int>& fwd_map,
const int& stride,
const int& nframes = 1,
// nall will not take effect if nframes is 1
const int& nall1 = 0,
const int& nall2 = 0);
template <typename VT>
void select_map(typename std::vector<VT>::iterator out,
const typename std::vector<VT>::const_iterator in,
const std::vector<int>& fwd_map,
const int& stride,
const int& nframes = 1,
const int& nall1 = 0,
const int& nall2 = 0);
template <typename VT>
void select_map_inv(std::vector<VT>& out,
const std::vector<VT>& in,
const std::vector<int>& fwd_map,
const int& stride);
template <typename VT>
void select_map_inv(typename std::vector<VT>::iterator out,
const typename std::vector<VT>::const_iterator in,
const std::vector<int>& fwd_map,
const int& stride);
void remap_comm_sendlist(std::vector<std::vector<int>>& new_sendlist,
std::vector<int>& new_sendnum,
std::vector<int>& new_recvnum,
const InputNlist& lmp_list,
const std::vector<int>& fwd_map);
std::vector<std::intptr_t> pack_comm_sendlist_pointers(int* const* sendlist,
int nswap);
void get_env_nthreads(int& num_intra_nthreads, int& num_inter_nthreads);
void load_op_library();
void load_op_library(DPBackend backend);
struct tf_exception : public deepmd::deepmd_exception {
public:
tf_exception() : deepmd::deepmd_exception("TensorFlow Error!") {};
tf_exception(const std::string& msg)
: deepmd::deepmd_exception(std::string("TensorFlow Error: ") + msg) {};
};
std::string name_prefix(const std::string& name_scope);
void read_file_to_string(std::string model, std::string& file_content);
void convert_pbtxt_to_pb(std::string fn_pb_txt, std::string fn_pb);
void print_summary(const std::string& pre);
template <typename VT>
void fold_back(std::vector<VT>& out,
const std::vector<VT>& in,
const std::vector<int>& mapping,
const int nloc,
const int nall,
const int ndim,
const int nframes = 1) {
const ptrdiff_t nloc_ = nloc;
const ptrdiff_t nall_ = nall;
const ptrdiff_t ndim_ = ndim;
out.resize(static_cast<size_t>(nframes) * nloc_ * ndim_);
for (ptrdiff_t kk = 0; kk < nframes; ++kk) {
std::copy(in.begin() + kk * nall_ * ndim_,
in.begin() + kk * nall_ * ndim_ + nloc_ * ndim_,
out.begin() + kk * nloc_ * ndim_);
for (ptrdiff_t ii = nloc_; ii < nall_; ++ii) {
ptrdiff_t out_idx = mapping[ii];
for (ptrdiff_t dd = 0; dd < ndim_; ++dd) {
out[kk * nloc_ * ndim_ + out_idx * ndim_ + dd] +=
in[kk * nall_ * ndim_ + ii * ndim_ + dd];
}
}
}
}
inline std::vector<int> buildPairExcludeTable(
const int ntypes, const std::vector<std::pair<int, int>>& exclude_types) {
if (exclude_types.empty()) {
return {};
}
const int n1 = ntypes + 1;
std::set<std::pair<int, int>> excl;
for (const auto& tt : exclude_types) {
excl.insert({tt.first, tt.second});
excl.insert({tt.second, tt.first});
}
// type_mask[tj][ti] == 0 iff (ti, tj) is excluded (mirrors the Python
// list comprehension in PairExcludeMask.__init__, reshape(-1)).
std::vector<int> type_mask(static_cast<size_t>(n1) * n1, 1);
for (int tj = 0; tj < n1; ++tj) {
for (int ti = 0; ti < n1; ++ti) {
if (excl.count({ti, tj})) {
type_mask[static_cast<size_t>(tj) * n1 + ti] = 0;
}
}
}
return type_mask;
}
inline void applyPairExcludeNlistVec(std::vector<std::int64_t>& nlist,
const std::vector<int>& atype,
const std::vector<int>& type_mask_table,
const int ntypes,
const int nloc,
const int max_size) {
if (type_mask_table.empty()) {
return;
}
const int n1 = ntypes + 1;
for (int ii = 0; ii < nloc; ++ii) {
const int ti = atype[ii]; // centre type
for (int jj = 0; jj < max_size; ++jj) {
const std::int64_t nb = nlist[static_cast<size_t>(ii) * max_size + jj];
if (nb < 0) {
continue; // empty slot
}
const int tj = atype[static_cast<size_t>(nb)]; // neighbour type
if (tj < 0) {
continue; // padding atom; nothing to exclude
}
if (type_mask_table[static_cast<size_t>(ti) * n1 + tj] == 0) {
nlist[static_cast<size_t>(ii) * max_size + jj] = -1;
}
}
}
}
} // namespace deepmd