Skip to content

Commit 6fdf92b

Browse files
Insert braces after control statements in C++ (deepmodeling#2629)
Signed-off-by: Jinzhe Zeng <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 473893e commit 6fdf92b

File tree

90 files changed

+1261
-466
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1261
-466
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
22
BasedOnStyle: Google
33
BinPackParameters: false
4+
InsertBraces: true
45
...

examples/infer_water/infer_water.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ int main() {
1919
DP_DeepPotCompute(dp, 3, coord, atype, cell, e, f, v, ae, av);
2020
// print results
2121
printf("energy: %f\n", *e);
22-
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, f[ii]);
23-
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, v[ii]);
22+
for (int ii = 0; ii < 9; ++ii) {
23+
printf("force[%d]: %f\n", ii, f[ii]);
24+
}
25+
for (int ii = 0; ii < 9; ++ii) {
26+
printf("force[%d]: %f\n", ii, v[ii]);
27+
}
2428
// free memory
2529
free(e);
2630
free(f);

examples/infer_water/infer_water_hpp.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ int main() {
1212
dp.compute(e, f, v, coord, atype, cell);
1313
// print results
1414
printf("energy: %f\n", e);
15-
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, f[ii]);
16-
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, v[ii]);
15+
for (int ii = 0; ii < 9; ++ii) {
16+
printf("force[%d]: %f\n", ii, f[ii]);
17+
}
18+
for (int ii = 0; ii < 9; ++ii) {
19+
printf("force[%d]: %f\n", ii, v[ii]);
20+
}
1721
}

examples/infer_water/infer_water_nlist.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ int main() {
3030
dp.compute(e, f, v, coord, atype, cell, 0, nlist, 0);
3131
// print results
3232
printf("energy: %f\n", e);
33-
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, f[ii]);
34-
for (int ii = 0; ii < 9; ++ii) printf("force[%d]: %f\n", ii, v[ii]);
33+
for (int ii = 0; ii < 9; ++ii) {
34+
printf("force[%d]: %f\n", ii, f[ii]);
35+
}
36+
for (int ii = 0; ii < 9; ++ii) {
37+
printf("force[%d]: %f\n", ii, v[ii]);
38+
}
3539
}

source/api_c/include/deepmd.hpp

+26-10
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,9 @@ class DeepPotModelDevi {
11071107
}
11081108
std::vector<const char *> cstrings;
11091109
cstrings.reserve(models.size());
1110-
for (std::string const &str : models) cstrings.push_back(str.data());
1110+
for (std::string const &str : models) {
1111+
cstrings.push_back(str.data());
1112+
}
11111113

11121114
std::vector<const char *> c_file_contents;
11131115
std::vector<int> size_file_contents;
@@ -1189,9 +1191,12 @@ class DeepPotModelDevi {
11891191
ener[i] = energy_flat[i];
11901192
force[i].resize(natoms * 3);
11911193
virial[i].resize(9);
1192-
for (int j = 0; j < natoms * 3; j++)
1194+
for (int j = 0; j < natoms * 3; j++) {
11931195
force[i][j] = force_flat[i * natoms * 3 + j];
1194-
for (int j = 0; j < 9; j++) virial[i][j] = virial_flat[i * 9 + j];
1196+
}
1197+
for (int j = 0; j < 9; j++) {
1198+
virial[i][j] = virial_flat[i * 9 + j];
1199+
}
11951200
}
11961201
};
11971202
/**
@@ -1267,13 +1272,18 @@ class DeepPotModelDevi {
12671272
virial[i].resize(9);
12681273
atom_energy[i].resize(natoms);
12691274
atom_virial[i].resize(natoms * 9);
1270-
for (int j = 0; j < natoms * 3; j++)
1275+
for (int j = 0; j < natoms * 3; j++) {
12711276
force[i][j] = force_flat[i * natoms * 3 + j];
1272-
for (int j = 0; j < 9; j++) virial[i][j] = virial_flat[i * 9 + j];
1273-
for (int j = 0; j < natoms; j++)
1277+
}
1278+
for (int j = 0; j < 9; j++) {
1279+
virial[i][j] = virial_flat[i * 9 + j];
1280+
}
1281+
for (int j = 0; j < natoms; j++) {
12741282
atom_energy[i][j] = atom_energy_flat[i * natoms + j];
1275-
for (int j = 0; j < natoms * 9; j++)
1283+
}
1284+
for (int j = 0; j < natoms * 9; j++) {
12761285
atom_virial[i][j] = atom_virial_flat[i * natoms * 9 + j];
1286+
}
12771287
}
12781288
};
12791289
/**
@@ -1325,7 +1335,9 @@ class DeepPotModelDevi {
13251335
void compute_avg(std::vector<VALUETYPE> &avg,
13261336
const std::vector<std::vector<VALUETYPE>> &xx) {
13271337
assert(xx.size() == numb_models);
1328-
if (numb_models == 0) return;
1338+
if (numb_models == 0) {
1339+
return;
1340+
}
13291341

13301342
avg.resize(xx[0].size());
13311343
fill(avg.begin(), avg.end(), VALUETYPE(0.));
@@ -1353,7 +1365,9 @@ class DeepPotModelDevi {
13531365
const std::vector<std::vector<VALUETYPE>> &xx,
13541366
const int &stride) {
13551367
assert(xx.size() == numb_models);
1356-
if (numb_models == 0) return;
1368+
if (numb_models == 0) {
1369+
return;
1370+
}
13571371

13581372
unsigned ndof = avg.size();
13591373
unsigned nloc = ndof / stride;
@@ -2017,7 +2031,9 @@ void select_map(std::vector<VT> &out,
20172031
const int nall1 = in.size() / stride;
20182032
int nall2 = 0;
20192033
for (int ii = 0; ii < nall1; ++ii) {
2020-
if (fwd_map[ii] >= 0) nall2++;
2034+
if (fwd_map[ii] >= 0) {
2035+
nall2++;
2036+
}
20212037
}
20222038
out.resize(nall2 * stride);
20232039
DP_SelectMapInt(&in[0], &fwd_map[0], stride, nall1, nall2, &out[0]);

source/api_c/src/c_api.cc

+99-33
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,21 @@ inline void DP_DeepPotCompute_variant(DP_DeepPot* dp,
172172
DP_REQUIRES_OK(dp, dp->dp.compute(e, f, v, ae, av, coord_, atype_, cell_,
173173
fparam_, aparam_));
174174
// copy from C++ vectors to C arrays, if not NULL pointer
175-
if (energy) std::copy(e.begin(), e.end(), energy);
176-
if (force) std::copy(f.begin(), f.end(), force);
177-
if (virial) std::copy(v.begin(), v.end(), virial);
178-
if (atomic_energy) std::copy(ae.begin(), ae.end(), atomic_energy);
179-
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
175+
if (energy) {
176+
std::copy(e.begin(), e.end(), energy);
177+
}
178+
if (force) {
179+
std::copy(f.begin(), f.end(), force);
180+
}
181+
if (virial) {
182+
std::copy(v.begin(), v.end(), virial);
183+
}
184+
if (atomic_energy) {
185+
std::copy(ae.begin(), ae.end(), atomic_energy);
186+
}
187+
if (atomic_virial) {
188+
std::copy(av.begin(), av.end(), atomic_virial);
189+
}
180190
}
181191

182192
template void DP_DeepPotCompute_variant<double>(DP_DeepPot* dp,
@@ -246,11 +256,21 @@ inline void DP_DeepPotComputeNList_variant(DP_DeepPot* dp,
246256
DP_REQUIRES_OK(dp, dp->dp.compute(e, f, v, ae, av, coord_, atype_, cell_,
247257
nghost, nlist->nl, ago, fparam_, aparam_));
248258
// copy from C++ vectors to C arrays, if not NULL pointer
249-
if (energy) std::copy(e.begin(), e.end(), energy);
250-
if (force) std::copy(f.begin(), f.end(), force);
251-
if (virial) std::copy(v.begin(), v.end(), virial);
252-
if (atomic_energy) std::copy(ae.begin(), ae.end(), atomic_energy);
253-
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
259+
if (energy) {
260+
std::copy(e.begin(), e.end(), energy);
261+
}
262+
if (force) {
263+
std::copy(f.begin(), f.end(), force);
264+
}
265+
if (virial) {
266+
std::copy(v.begin(), v.end(), virial);
267+
}
268+
if (atomic_energy) {
269+
std::copy(ae.begin(), ae.end(), atomic_energy);
270+
}
271+
if (atomic_virial) {
272+
std::copy(av.begin(), av.end(), atomic_virial);
273+
}
254274
}
255275

256276
template void DP_DeepPotComputeNList_variant<double>(DP_DeepPot* dp,
@@ -324,11 +344,21 @@ inline void DP_DeepPotComputeMixedType_variant(DP_DeepPot* dp,
324344
dp, dp->dp.compute_mixed_type(e, f, v, ae, av, nframes, coord_, atype_,
325345
cell_, fparam_, aparam_));
326346
// copy from C++ vectors to C arrays, if not NULL pointer
327-
if (energy) std::copy(e.begin(), e.end(), energy);
328-
if (force) std::copy(f.begin(), f.end(), force);
329-
if (virial) std::copy(v.begin(), v.end(), virial);
330-
if (atomic_energy) std::copy(ae.begin(), ae.end(), atomic_energy);
331-
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
347+
if (energy) {
348+
std::copy(e.begin(), e.end(), energy);
349+
}
350+
if (force) {
351+
std::copy(f.begin(), f.end(), force);
352+
}
353+
if (virial) {
354+
std::copy(v.begin(), v.end(), virial);
355+
}
356+
if (atomic_energy) {
357+
std::copy(ae.begin(), ae.end(), atomic_energy);
358+
}
359+
if (atomic_virial) {
360+
std::copy(av.begin(), av.end(), atomic_virial);
361+
}
332362
}
333363

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

586618
DP_REQUIRES_OK(dt, dt->dt.compute(t, f, v, at, av, coord_, atype_, cell_));
587619
// copy from C++ vectors to C arrays, if not NULL pointer
588-
if (global_tensor) std::copy(t.begin(), t.end(), global_tensor);
589-
if (force) std::copy(f.begin(), f.end(), force);
590-
if (virial) std::copy(v.begin(), v.end(), virial);
591-
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
620+
if (global_tensor) {
621+
std::copy(t.begin(), t.end(), global_tensor);
622+
}
623+
if (force) {
624+
std::copy(f.begin(), f.end(), force);
625+
}
626+
if (virial) {
627+
std::copy(v.begin(), v.end(), virial);
628+
}
629+
if (atomic_virial) {
630+
std::copy(av.begin(), av.end(), atomic_virial);
631+
}
592632
// do not know the size of atomic tensor in advance...
593633
if (atomic_tensor) {
594634
*atomic_tensor = new VALUETYPE[at.size()];
595635
std::copy(at.begin(), at.end(), *atomic_tensor);
596636
}
597-
if (size_at) *size_at = at.size();
637+
if (size_at) {
638+
*size_at = at.size();
639+
}
598640
}
599641

600642
template void DP_DeepTensorCompute_variant<double>(DP_DeepTensor* dt,
@@ -648,16 +690,26 @@ inline void DP_DeepTensorComputeNList_variant(DP_DeepTensor* dt,
648690
DP_REQUIRES_OK(dt, dt->dt.compute(t, f, v, at, av, coord_, atype_, cell_,
649691
nghost, nlist->nl));
650692
// copy from C++ vectors to C arrays, if not NULL pointer
651-
if (global_tensor) std::copy(t.begin(), t.end(), global_tensor);
652-
if (force) std::copy(f.begin(), f.end(), force);
653-
if (virial) std::copy(v.begin(), v.end(), virial);
654-
if (atomic_virial) std::copy(av.begin(), av.end(), atomic_virial);
693+
if (global_tensor) {
694+
std::copy(t.begin(), t.end(), global_tensor);
695+
}
696+
if (force) {
697+
std::copy(f.begin(), f.end(), force);
698+
}
699+
if (virial) {
700+
std::copy(v.begin(), v.end(), virial);
701+
}
702+
if (atomic_virial) {
703+
std::copy(av.begin(), av.end(), atomic_virial);
704+
}
655705
// do not know the size of atomic tensor in advance...
656706
if (atomic_tensor) {
657707
*atomic_tensor = new VALUETYPE[at.size()];
658708
std::copy(at.begin(), at.end(), *atomic_tensor);
659709
}
660-
if (size_at) *size_at = at.size();
710+
if (size_at) {
711+
*size_at = at.size();
712+
}
661713
}
662714

663715
template void DP_DeepTensorComputeNList_variant<double>(DP_DeepTensor* dt,
@@ -721,8 +773,12 @@ inline void DP_DipoleChargeModifierComputeNList_variant(
721773
DP_REQUIRES_OK(dcm, dcm->dcm.compute(df, dv, coord_, atype_, cell_, pairs_,
722774
delef_, nghost, nlist->nl));
723775
// copy from C++ vectors to C arrays, if not NULL pointer
724-
if (dfcorr_) std::copy(df.begin(), df.end(), dfcorr_);
725-
if (dvcorr_) std::copy(dv.begin(), dv.end(), dvcorr_);
776+
if (dfcorr_) {
777+
std::copy(df.begin(), df.end(), dfcorr_);
778+
}
779+
if (dvcorr_) {
780+
std::copy(dv.begin(), dv.end(), dvcorr_);
781+
}
726782
}
727783

728784
template void DP_DipoleChargeModifierComputeNList_variant<double>(
@@ -1316,10 +1372,18 @@ void DP_SelectByType(const int natoms,
13161372
int nghost_real_;
13171373
deepmd::select_by_type(fwd_map_, bkw_map_, nghost_real_,
13181374
std::vector<double>(), atype_, nghost, sel_type_);
1319-
if (fwd_map) std::copy(fwd_map_.begin(), fwd_map_.end(), fwd_map);
1320-
if (bkw_map) std::copy(bkw_map_.begin(), bkw_map_.end(), bkw_map);
1321-
if (nreal) *nreal = bkw_map_.size();
1322-
if (nghost_real) *nghost_real = nghost_real_;
1375+
if (fwd_map) {
1376+
std::copy(fwd_map_.begin(), fwd_map_.end(), fwd_map);
1377+
}
1378+
if (bkw_map) {
1379+
std::copy(bkw_map_.begin(), bkw_map_.end(), bkw_map);
1380+
}
1381+
if (nreal) {
1382+
*nreal = bkw_map_.size();
1383+
}
1384+
if (nghost_real) {
1385+
*nghost_real = nghost_real_;
1386+
}
13231387
}
13241388

13251389
void DP_SelectMapInt(const int* in,
@@ -1332,7 +1396,9 @@ void DP_SelectMapInt(const int* in,
13321396
std::vector<int> fwd_map_(fwd_map, fwd_map + nall1);
13331397
std::vector<int> out_(stride * nall2);
13341398
deepmd::select_map(out_, in_, fwd_map_, stride);
1335-
if (out) std::copy(out_.begin(), out_.end(), out);
1399+
if (out) {
1400+
std::copy(out_.begin(), out_.end(), out);
1401+
}
13361402
}
13371403

13381404
} // extern "C"

source/api_c/tests/test_dipolecharge.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ class TestDipoleCharge : public ::testing::Test {
7575
static bool _in_vec(const int& value, const std::vector<int>& vec) {
7676
// naive impl.
7777
for (int ii = 0; ii < vec.size(); ++ii) {
78-
if (value == vec[ii]) return true;
78+
if (value == vec[ii]) {
79+
return true;
80+
}
7981
}
8082
return false;
8183
}

source/api_c/tests/test_select_map.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ class TestSelectMap : public ::testing::Test {
3131
EXPECT_EQ(natoms, fwd_map_1.size());
3232
EXPECT_EQ(4, expected_atype_out_1.size());
3333

34-
for (int ii = 0; ii < 2; ii++) atype_out_0.push_back(0);
35-
for (int ii = 0; ii < 4; ii++) atype_out_1.push_back(0);
34+
for (int ii = 0; ii < 2; ii++) {
35+
atype_out_0.push_back(0);
36+
}
37+
for (int ii = 0; ii < 4; ii++) {
38+
atype_out_1.push_back(0);
39+
}
3640
}
3741
};
3842

0 commit comments

Comments
 (0)