@@ -8,12 +8,35 @@ This header-only library provides a C++ 11 interface to the DeePMD-kit C API.
88
99#include < algorithm>
1010#include < cassert>
11+ #include < exception>
1112#include < iostream>
1213#include < string>
1314#include < vector>
1415
1516#include " c_api.h"
1617
18+ namespace deepmd {
19+ namespace hpp {
20+ /* *
21+ * @brief General DeePMD-kit exception. Throw if anything doesn't work.
22+ **/
23+ struct deepmd_exception : public std ::runtime_error {
24+ public:
25+ deepmd_exception () : runtime_error(" DeePMD-kit C API Error!" ){};
26+ deepmd_exception (const std::string &msg)
27+ : runtime_error(std::string(" DeePMD-kit C API Error: " ) + msg){};
28+ };
29+ } // namespace hpp
30+ } // namespace deepmd
31+
32+ /* *
33+ * @brief Check if any exceptions throw in the C++ API. Throw if possible.
34+ */
35+ #define DP_CHECK_OK (check_func, dp ) \
36+ const char *err_msg = check_func(dp); \
37+ if (strlen(err_msg)) \
38+ throw deepmd::hpp::deepmd_exception (std::string(err_msg));
39+
1740template <typename FPTYPE>
1841inline void _DP_DeepPotCompute (DP_DeepPot *dp,
1942 const int nframes,
@@ -422,13 +445,17 @@ struct InputNlist {
422445 ilist (nullptr ),
423446 numneigh(nullptr ),
424447 firstneigh(nullptr ),
425- nl(DP_NewNlist(0 , nullptr , nullptr , nullptr )){};
448+ nl(DP_NewNlist(0 , nullptr , nullptr , nullptr )) {
449+ DP_CHECK_OK (DP_NlistCheckOK, nl);
450+ };
426451 InputNlist (int inum_, int *ilist_, int *numneigh_, int **firstneigh_)
427452 : inum(inum_),
428453 ilist(ilist_),
429454 numneigh(numneigh_),
430455 firstneigh(firstneigh_),
431- nl(DP_NewNlist(inum_, ilist_, numneigh_, firstneigh_)){};
456+ nl(DP_NewNlist(inum_, ilist_, numneigh_, firstneigh_)) {
457+ DP_CHECK_OK (DP_NlistCheckOK, nl);
458+ };
432459 // / @brief C API neighbor list.
433460 DP_Nlist *nl;
434461 // / @brief Number of core region atoms
@@ -504,6 +531,7 @@ class DeepPot {
504531 return ;
505532 }
506533 dp = DP_NewDeepPotWithParam (model.c_str (), gpu_rank, file_content.c_str ());
534+ DP_CHECK_OK (DP_DeepPotCheckOK, dp);
507535 };
508536
509537 /* *
@@ -542,6 +570,7 @@ class DeepPot {
542570 _DP_DeepPotCompute<VALUETYPE>(dp, nframes, natoms, coord_, atype_, box_,
543571 nullptr , nullptr , ener_, force_, virial_,
544572 nullptr , nullptr );
573+ DP_CHECK_OK (DP_DeepPotCheckOK, dp);
545574 };
546575 /* *
547576 * @brief Evaluate the energy, force, virial, atomic energy, and atomic virial
@@ -589,6 +618,7 @@ class DeepPot {
589618 _DP_DeepPotCompute<VALUETYPE>(dp, nframes, natoms, coord_, atype_, box_,
590619 nullptr , nullptr , ener_, force_, virial_,
591620 atomic_ener_, atomic_virial_);
621+ DP_CHECK_OK (DP_DeepPotCheckOK, dp);
592622 };
593623
594624 /* *
@@ -634,6 +664,7 @@ class DeepPot {
634664 _DP_DeepPotComputeNList<VALUETYPE>(
635665 dp, nframes, natoms, coord_, atype_, box_, nghost, lmp_list.nl , ago,
636666 nullptr , nullptr , ener_, force_, virial_, nullptr , nullptr );
667+ DP_CHECK_OK (DP_DeepPotCheckOK, dp);
637668 };
638669 /* *
639670 * @brief Evaluate the energy, force, virial, atomic energy, and atomic virial
@@ -687,6 +718,7 @@ class DeepPot {
687718 _DP_DeepPotComputeNList<VALUETYPE>(
688719 dp, nframes, natoms, coord_, atype_, box_, nghost, lmp_list.nl , ago,
689720 nullptr , nullptr , ener_, force_, virial_, atomic_ener_, atomic_virial_);
721+ DP_CHECK_OK (DP_DeepPotCheckOK, dp);
690722 };
691723 /* *
692724 * @brief Get the cutoff radius.
@@ -759,6 +791,7 @@ class DeepPotModelDevi {
759791 for (std::string const &str : models) cstrings.push_back (str.data ());
760792
761793 dp = DP_NewDeepPotModelDevi (cstrings.data (), cstrings.size ());
794+ DP_CHECK_OK (DP_DeepPotModelDeviCheckOK, dp);
762795 numb_models = models.size ();
763796 };
764797
@@ -804,6 +837,7 @@ class DeepPotModelDevi {
804837 _DP_DeepPotModelDeviComputeNList<VALUETYPE>(
805838 dp, natoms, coord_, atype_, box_, nghost, lmp_list.nl , ago, ener_,
806839 force_, virial_, nullptr , nullptr );
840+ DP_CHECK_OK (DP_DeepPotModelDeviCheckOK, dp);
807841
808842 // reshape
809843 ener.resize (numb_models);
@@ -867,6 +901,7 @@ class DeepPotModelDevi {
867901 _DP_DeepPotModelDeviComputeNList<VALUETYPE>(
868902 dp, natoms, coord_, atype_, box_, nghost, lmp_list.nl , ago, ener_,
869903 force_, virial_, atomic_ener_, atomic_virial_);
904+ DP_CHECK_OK (DP_DeepPotModelDeviCheckOK, dp);
870905
871906 // reshape
872907 ener.resize (numb_models);
@@ -945,6 +980,7 @@ class DeepTensor {
945980 return ;
946981 }
947982 dt = DP_NewDeepTensorWithParam (model.c_str (), gpu_rank, name_scope.c_str ());
983+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
948984 odim = output_dim ();
949985 nsel_types = DP_DeepTensorGetNumbSelTypes (dt);
950986 };
@@ -979,6 +1015,7 @@ class DeepTensor {
9791015
9801016 _DP_DeepTensorComputeTensor<VALUETYPE>(dt, natoms, coord_, atype_, box_,
9811017 p_tensor, p_size);
1018+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
9821019
9831020 tensor.resize (size);
9841021 std::copy (tensor_, tensor_ + size, tensor.begin ());
@@ -1021,6 +1058,7 @@ class DeepTensor {
10211058 _DP_DeepTensorComputeTensorNList<VALUETYPE>(dt, natoms, coord_, atype_,
10221059 box_, nghost, lmp_list.nl ,
10231060 p_tensor, p_size);
1061+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
10241062
10251063 tensor.resize (size);
10261064 std::copy (tensor_, tensor_ + size, tensor.begin ());
@@ -1064,6 +1102,7 @@ class DeepTensor {
10641102 _DP_DeepTensorCompute<VALUETYPE>(dt, natoms, coord_, atype_, box_,
10651103 global_tensor_, force_, virial_, nullptr ,
10661104 nullptr , nullptr );
1105+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
10671106 };
10681107 /* *
10691108 * @brief Evaluate the global tensor, force, virial, atomic tensor, and atomic
@@ -1114,6 +1153,7 @@ class DeepTensor {
11141153 _DP_DeepTensorCompute<VALUETYPE>(
11151154 dt, natoms, coord_, atype_, box_, global_tensor_, force_, virial_,
11161155 p_atomic_tensor, atomic_virial_, p_size_at);
1156+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
11171157
11181158 atom_tensor.resize (size_at);
11191159 std::copy (atomic_tensor_, atomic_tensor_ + size_at, atom_tensor.begin ());
@@ -1161,6 +1201,7 @@ class DeepTensor {
11611201 _DP_DeepTensorComputeNList<VALUETYPE>(
11621202 dt, natoms, coord_, atype_, box_, nghost, lmp_list.nl , global_tensor_,
11631203 force_, virial_, nullptr , nullptr , nullptr );
1204+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
11641205 };
11651206 /* *
11661207 * @brief Evaluate the global tensor, force, virial, atomic tensor, and atomic
@@ -1215,6 +1256,7 @@ class DeepTensor {
12151256 _DP_DeepTensorComputeNList<VALUETYPE>(
12161257 dt, natoms, coord_, atype_, box_, nghost, lmp_list.nl , global_tensor_,
12171258 force_, virial_, p_atomic_tensor, atomic_virial_, p_size_at);
1259+ DP_CHECK_OK (DP_DeepTensorCheckOK, dt);
12181260
12191261 atom_tensor.resize (size_at);
12201262 std::copy (atomic_tensor_, atomic_tensor_ + size_at, atom_tensor.begin ());
@@ -1302,6 +1344,7 @@ class DipoleChargeModifier {
13021344 }
13031345 dcm = DP_NewDipoleChargeModifierWithParam (model.c_str (), gpu_rank,
13041346 name_scope.c_str ());
1347+ DP_CHECK_OK (DP_DipoleChargeModifierCheckOK, dcm);
13051348 nsel_types = DP_DipoleChargeModifierGetNumbSelTypes (dcm);
13061349 };
13071350 /* *
@@ -1350,6 +1393,7 @@ class DipoleChargeModifier {
13501393 _DP_DipoleChargeModifierComputeNList<VALUETYPE>(
13511394 dcm, natoms, dcoord, datype, dbox_, dpairs, npairs, delef, nghost,
13521395 lmp_list.nl , dfcorr, dvcorr);
1396+ DP_CHECK_OK (DP_DipoleChargeModifierCheckOK, dcm);
13531397 };
13541398 /* *
13551399 * @brief Get the cutoff radius.
0 commit comments