Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert braces after control statements in C++ #2629

Merged
merged 2 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
BasedOnStyle: Google
BinPackParameters: false
InsertBraces: true
...
8 changes: 6 additions & 2 deletions examples/infer_water/infer_water.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ int main() {
DP_DeepPotCompute(dp, 3, coord, atype, cell, e, f, v, ae, av);
// print results
printf("energy: %f\n", *e);
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, f[ii]);
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, v[ii]);
for (int ii = 0; ii < 9; ++ii) {
printf("force[%d]: %f\n", ii, f[ii]);
}
for (int ii = 0; ii < 9; ++ii) {
printf("force[%d]: %f\n", ii, v[ii]);
}
// free memory
free(e);
free(f);
Expand Down
8 changes: 6 additions & 2 deletions examples/infer_water/infer_water_hpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ int main() {
dp.compute(e, f, v, coord, atype, cell);
// print results
printf("energy: %f\n", e);
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, f[ii]);
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, v[ii]);
for (int ii = 0; ii < 9; ++ii) {
printf("force[%d]: %f\n", ii, f[ii]);
}
for (int ii = 0; ii < 9; ++ii) {
printf("force[%d]: %f\n", ii, v[ii]);
}
}
8 changes: 6 additions & 2 deletions examples/infer_water/infer_water_nlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ int main() {
dp.compute(e, f, v, coord, atype, cell, 0, nlist, 0);
// print results
printf("energy: %f\n", e);
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, f[ii]);
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, v[ii]);
for (int ii = 0; ii < 9; ++ii) {
printf("force[%d]: %f\n", ii, f[ii]);
}
for (int ii = 0; ii < 9; ++ii) {
printf("force[%d]: %f\n", ii, v[ii]);
}
}
36 changes: 26 additions & 10 deletions source/api_c/include/deepmd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,9 @@ class DeepPotModelDevi {
}
std::vector<const char *> cstrings;
cstrings.reserve(models.size());
for (std::string const &str : models) cstrings.push_back(str.data());
for (std::string const &str : models) {
cstrings.push_back(str.data());
}

std::vector<const char *> c_file_contents;
std::vector<int> size_file_contents;
Expand Down Expand Up @@ -1189,9 +1191,12 @@ class DeepPotModelDevi {
ener[i] = energy_flat[i];
force[i].resize(natoms * 3);
virial[i].resize(9);
for (int j = 0; j < natoms * 3; j++)
for (int j = 0; j < natoms * 3; j++) {
force[i][j] = force_flat[i * natoms * 3 + j];
for (int j = 0; j < 9; j++) virial[i][j] = virial_flat[i * 9 + j];
}
for (int j = 0; j < 9; j++) {
virial[i][j] = virial_flat[i * 9 + j];
}
}
};
/**
Expand Down Expand Up @@ -1267,13 +1272,18 @@ class DeepPotModelDevi {
virial[i].resize(9);
atom_energy[i].resize(natoms);
atom_virial[i].resize(natoms * 9);
for (int j = 0; j < natoms * 3; j++)
for (int j = 0; j < natoms * 3; j++) {
force[i][j] = force_flat[i * natoms * 3 + j];
for (int j = 0; j < 9; j++) virial[i][j] = virial_flat[i * 9 + j];
for (int j = 0; j < natoms; j++)
}
for (int j = 0; j < 9; j++) {
virial[i][j] = virial_flat[i * 9 + j];
}
for (int j = 0; j < natoms; j++) {
atom_energy[i][j] = atom_energy_flat[i * natoms + j];
for (int j = 0; j < natoms * 9; j++)
}
for (int j = 0; j < natoms * 9; j++) {
atom_virial[i][j] = atom_virial_flat[i * natoms * 9 + j];
}
}
};
/**
Expand Down Expand Up @@ -1325,7 +1335,9 @@ class DeepPotModelDevi {
void compute_avg(std::vector<VALUETYPE> &avg,
const std::vector<std::vector<VALUETYPE>> &xx) {
assert(xx.size() == numb_models);
if (numb_models == 0) return;
if (numb_models == 0) {
return;
}

avg.resize(xx[0].size());
fill(avg.begin(), avg.end(), VALUETYPE(0.));
Expand Down Expand Up @@ -1353,7 +1365,9 @@ class DeepPotModelDevi {
const std::vector<std::vector<VALUETYPE>> &xx,
const int &stride) {
assert(xx.size() == numb_models);
if (numb_models == 0) return;
if (numb_models == 0) {
return;
}

unsigned ndof = avg.size();
unsigned nloc = ndof / stride;
Expand Down Expand Up @@ -2017,7 +2031,9 @@ void select_map(std::vector<VT> &out,
const int nall1 = in.size() / stride;
int nall2 = 0;
for (int ii = 0; ii < nall1; ++ii) {
if (fwd_map[ii] >= 0) nall2++;
if (fwd_map[ii] >= 0) {
nall2++;
}
}
out.resize(nall2 * stride);
DP_SelectMapInt(&in[0], &fwd_map[0], stride, nall1, nall2, &out[0]);
Expand Down
132 changes: 99 additions & 33 deletions source/api_c/src/c_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,21 @@ inline void DP_DeepPotCompute_variant(DP_DeepPot* dp,
DP_REQUIRES_OK(dp, dp->dp.compute(e, f, v, ae, av, coord_, atype_, cell_,
fparam_, aparam_));
// copy from C++ vectors to C arrays, if not NULL pointer
if (energy) std::copy(e.begin(), e.end(), energy);
if (force) std::copy(f.begin(), f.end(), force);
if (virial) std::copy(v.begin(), v.end(), virial);
if (atomic_energy) std::copy(ae.begin(), ae.end(), atomic_energy);
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
if (energy) {
std::copy(e.begin(), e.end(), energy);
}
if (force) {
std::copy(f.begin(), f.end(), force);
}
if (virial) {
std::copy(v.begin(), v.end(), virial);
}
if (atomic_energy) {
std::copy(ae.begin(), ae.end(), atomic_energy);
}
if (atomic_virial) {
std::copy(av.begin(), av.end(), atomic_virial);
}
}

template void DP_DeepPotCompute_variant<double>(DP_DeepPot* dp,
Expand Down Expand Up @@ -246,11 +256,21 @@ inline void DP_DeepPotComputeNList_variant(DP_DeepPot* dp,
DP_REQUIRES_OK(dp, dp->dp.compute(e, f, v, ae, av, coord_, atype_, cell_,
nghost, nlist->nl, ago, fparam_, aparam_));
// copy from C++ vectors to C arrays, if not NULL pointer
if (energy) std::copy(e.begin(), e.end(), energy);
if (force) std::copy(f.begin(), f.end(), force);
if (virial) std::copy(v.begin(), v.end(), virial);
if (atomic_energy) std::copy(ae.begin(), ae.end(), atomic_energy);
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
if (energy) {
std::copy(e.begin(), e.end(), energy);
}
if (force) {
std::copy(f.begin(), f.end(), force);
}
if (virial) {
std::copy(v.begin(), v.end(), virial);
}
if (atomic_energy) {
std::copy(ae.begin(), ae.end(), atomic_energy);
}
if (atomic_virial) {
std::copy(av.begin(), av.end(), atomic_virial);
}
}

template void DP_DeepPotComputeNList_variant<double>(DP_DeepPot* dp,
Expand Down Expand Up @@ -324,11 +344,21 @@ inline void DP_DeepPotComputeMixedType_variant(DP_DeepPot* dp,
dp, dp->dp.compute_mixed_type(e, f, v, ae, av, nframes, coord_, atype_,
cell_, fparam_, aparam_));
// copy from C++ vectors to C arrays, if not NULL pointer
if (energy) std::copy(e.begin(), e.end(), energy);
if (force) std::copy(f.begin(), f.end(), force);
if (virial) std::copy(v.begin(), v.end(), virial);
if (atomic_energy) std::copy(ae.begin(), ae.end(), atomic_energy);
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
if (energy) {
std::copy(e.begin(), e.end(), energy);
}
if (force) {
std::copy(f.begin(), f.end(), force);
}
if (virial) {
std::copy(v.begin(), v.end(), virial);
}
if (atomic_energy) {
std::copy(ae.begin(), ae.end(), atomic_energy);
}
if (atomic_virial) {
std::copy(av.begin(), av.end(), atomic_virial);
}
}

template void DP_DeepPotComputeMixedType_variant<double>(DP_DeepPot* dp,
Expand Down Expand Up @@ -385,7 +415,9 @@ void DP_DeepPotModelDeviComputeNList_variant(DP_DeepPotModelDevi* dp,
VALUETYPE* virial,
VALUETYPE* atomic_energy,
VALUETYPE* atomic_virial) {
if (nframes > 1) throw std::runtime_error("nframes > 1 not supported yet");
if (nframes > 1) {
throw std::runtime_error("nframes > 1 not supported yet");
}
// init C++ vectors from C arrays
std::vector<VALUETYPE> coord_(coord, coord + natoms * 3);
std::vector<int> atype_(atype, atype + natoms);
Expand Down Expand Up @@ -585,16 +617,26 @@ inline void DP_DeepTensorCompute_variant(DP_DeepTensor* dt,

DP_REQUIRES_OK(dt, dt->dt.compute(t, f, v, at, av, coord_, atype_, cell_));
// copy from C++ vectors to C arrays, if not NULL pointer
if (global_tensor) std::copy(t.begin(), t.end(), global_tensor);
if (force) std::copy(f.begin(), f.end(), force);
if (virial) std::copy(v.begin(), v.end(), virial);
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
if (global_tensor) {
std::copy(t.begin(), t.end(), global_tensor);
}
if (force) {
std::copy(f.begin(), f.end(), force);
}
if (virial) {
std::copy(v.begin(), v.end(), virial);
}
if (atomic_virial) {
std::copy(av.begin(), av.end(), atomic_virial);
}
// do not know the size of atomic tensor in advance...
if (atomic_tensor) {
*atomic_tensor = new VALUETYPE[at.size()];
std::copy(at.begin(), at.end(), *atomic_tensor);
}
if (size_at) *size_at = at.size();
if (size_at) {
*size_at = at.size();
}
}

template void DP_DeepTensorCompute_variant<double>(DP_DeepTensor* dt,
Expand Down Expand Up @@ -648,16 +690,26 @@ inline void DP_DeepTensorComputeNList_variant(DP_DeepTensor* dt,
DP_REQUIRES_OK(dt, dt->dt.compute(t, f, v, at, av, coord_, atype_, cell_,
nghost, nlist->nl));
// copy from C++ vectors to C arrays, if not NULL pointer
if (global_tensor) std::copy(t.begin(), t.end(), global_tensor);
if (force) std::copy(f.begin(), f.end(), force);
if (virial) std::copy(v.begin(), v.end(), virial);
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
if (global_tensor) {
std::copy(t.begin(), t.end(), global_tensor);
}
if (force) {
std::copy(f.begin(), f.end(), force);
}
if (virial) {
std::copy(v.begin(), v.end(), virial);
}
if (atomic_virial) {
std::copy(av.begin(), av.end(), atomic_virial);
}
// do not know the size of atomic tensor in advance...
if (atomic_tensor) {
*atomic_tensor = new VALUETYPE[at.size()];
std::copy(at.begin(), at.end(), *atomic_tensor);
}
if (size_at) *size_at = at.size();
if (size_at) {
*size_at = at.size();
}
}

template void DP_DeepTensorComputeNList_variant<double>(DP_DeepTensor* dt,
Expand Down Expand Up @@ -721,8 +773,12 @@ inline void DP_DipoleChargeModifierComputeNList_variant(
DP_REQUIRES_OK(dcm, dcm->dcm.compute(df, dv, coord_, atype_, cell_, pairs_,
delef_, nghost, nlist->nl));
// copy from C++ vectors to C arrays, if not NULL pointer
if (dfcorr_) std::copy(df.begin(), df.end(), dfcorr_);
if (dvcorr_) std::copy(dv.begin(), dv.end(), dvcorr_);
if (dfcorr_) {
std::copy(df.begin(), df.end(), dfcorr_);
}
if (dvcorr_) {
std::copy(dv.begin(), dv.end(), dvcorr_);
}
}

template void DP_DipoleChargeModifierComputeNList_variant<double>(
Expand Down Expand Up @@ -1316,10 +1372,18 @@ void DP_SelectByType(const int natoms,
int nghost_real_;
deepmd::select_by_type(fwd_map_, bkw_map_, nghost_real_,
std::vector<double>(), atype_, nghost, sel_type_);
if (fwd_map) std::copy(fwd_map_.begin(), fwd_map_.end(), fwd_map);
if (bkw_map) std::copy(bkw_map_.begin(), bkw_map_.end(), bkw_map);
if (nreal) *nreal = bkw_map_.size();
if (nghost_real) *nghost_real = nghost_real_;
if (fwd_map) {
std::copy(fwd_map_.begin(), fwd_map_.end(), fwd_map);
}
if (bkw_map) {
std::copy(bkw_map_.begin(), bkw_map_.end(), bkw_map);
}
if (nreal) {
*nreal = bkw_map_.size();
}
if (nghost_real) {
*nghost_real = nghost_real_;
}
}

void DP_SelectMapInt(const int* in,
Expand All @@ -1332,7 +1396,9 @@ void DP_SelectMapInt(const int* in,
std::vector<int> fwd_map_(fwd_map, fwd_map + nall1);
std::vector<int> out_(stride * nall2);
deepmd::select_map(out_, in_, fwd_map_, stride);
if (out) std::copy(out_.begin(), out_.end(), out);
if (out) {
std::copy(out_.begin(), out_.end(), out);
}
}

} // extern "C"
4 changes: 3 additions & 1 deletion source/api_c/tests/test_dipolecharge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ class TestDipoleCharge : public ::testing::Test {
static bool _in_vec(const int& value, const std::vector<int>& vec) {
// naive impl.
for (int ii = 0; ii < vec.size(); ++ii) {
if (value == vec[ii]) return true;
if (value == vec[ii]) {
return true;
}
}
return false;
}
Expand Down
8 changes: 6 additions & 2 deletions source/api_c/tests/test_select_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ class TestSelectMap : public ::testing::Test {
EXPECT_EQ(natoms, fwd_map_1.size());
EXPECT_EQ(4, expected_atype_out_1.size());

for (int ii = 0; ii < 2; ii++) atype_out_0.push_back(0);
for (int ii = 0; ii < 4; ii++) atype_out_1.push_back(0);
for (int ii = 0; ii < 2; ii++) {
atype_out_0.push_back(0);
}
for (int ii = 0; ii < 4; ii++) {
atype_out_1.push_back(0);
}
}
};

Expand Down
Loading