Skip to content

Commit de98353

Browse files
committed
small fix
1 parent 4c8876e commit de98353

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

source/module_base/grid/batch.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace {
3535
* indices placed together.
3636
* @param[in] m Number of selected points (length of idx).
3737
*
38-
* @return The number of points in the first subset (according to idx).
38+
* @return The number of points in the first subset within idx.
3939
*
4040
*/
4141
int _maxmin_divide(const double* grid, int* idx, int m) {

source/module_base/grid/batch.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ namespace Batch {
3535
* 8 9 20 10 11
3636
* 12 13 19 14 15
3737
*
38-
* a possible outcome with m_thr = 4 and idx(in) = {0, 1, 2, ..., 15} is:
38+
* a possible outcome with m_thr = 4 and idx(in) = {0, 1, 2, ..., 15}
39+
* (idx may correspond to a subset of grid and does not have to be sorted,
40+
* but it must not contain duplicates) is:
3941
*
4042
* idx(out): 0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15
4143
* return : {0, 4, 8, 12}

source/module_base/grid/test/test_batch.cpp

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "gtest/gtest.h"
44
#include <algorithm>
55
#include <random>
6+
//#include <cstdio>
67

78
#ifdef __MPI
89
#include <mpi.h>
@@ -18,7 +19,7 @@ class BatchTest: public ::testing::Test
1819
std::vector<double> grid_;
1920
std::vector<int> idx_;
2021

21-
// parameters for octant clusters
22+
// parameters for 8-octant clusters
2223
const int n_batch_oct_ = 10;
2324
const double width_oct_ = 1.0;
2425
const double offset_x_ = 7.0;
@@ -32,13 +33,14 @@ class BatchTest: public ::testing::Test
3233
// plane are equivalent in terms of the maxmin optimization problem.
3334
// This means eigenvectors are arbitrary in this case.
3435

35-
// parameters for random cluster
36-
const int n_grid_rand_ = 10000;
37-
const int n_batch_rand_ = 100;
38-
const double width_rand_ = 20.0;
39-
const double xc_ = 5.0;
40-
const double yc_ = 5.0;
41-
const double zc_ = 7.0;
36+
37+
// parameters for a random cluster
38+
const int n_grid_rand_ = 1000;
39+
const int n_batch_rand_ = 200;
40+
const double width_rand_ = 10.0;
41+
const double xc_ = 1.0;
42+
const double yc_ = 1.0;
43+
const double zc_ = 2.0;
4244
};
4345

4446

@@ -128,7 +130,27 @@ bool is_same_octant(int ngrid, const double* grid) {
128130
}
129131

130132

131-
TEST_F(BatchTest, MaxMinRandomCluster)
133+
bool good_batch_size(
134+
const std::vector<int>& idx,
135+
const std::vector<int>& delim,
136+
int n_batch_thr
137+
) {
138+
// checks if the sizes of batches are within the specified limit
139+
140+
bool flag = (delim[0] == 0);
141+
142+
size_t i = 1;
143+
while (flag && i < delim.size()) {
144+
int sz_batch = delim[i] - delim[i-1];
145+
flag = flag && (sz_batch > 0) && (sz_batch <= n_batch_thr);
146+
++i;
147+
}
148+
149+
return flag && ( ((int)idx.size() - delim.back()) < n_batch_thr );
150+
}
151+
152+
153+
TEST_F(BatchTest, MaxMinRandom)
132154
{
133155
// This test verifies that the sizes of batches produced by maxmin
134156
// do not exceed the specified limit.
@@ -138,20 +160,31 @@ TEST_F(BatchTest, MaxMinRandomCluster)
138160
std::vector<int> delim =
139161
maxmin(grid_.data(), idx_.data(), idx_.size(), n_batch_rand_);
140162

141-
for (size_t i = 0; i < delim.size(); ++i) {
142-
if (i == 0) {
143-
EXPECT_EQ(delim[i], 0);
144-
} else {
145-
int sz_batch = delim[i] - delim[i-1];
146-
EXPECT_GT(sz_batch, 0);
147-
EXPECT_LE(sz_batch, n_batch_rand_);
148-
}
149-
}
150-
EXPECT_LE(idx_.size() - delim.back(), n_batch_rand_);
163+
EXPECT_TRUE(good_batch_size(idx_, delim, n_batch_rand_));
164+
165+
// write grid, idx & delim to file
166+
//FILE* fp = fopen("grid.dat", "w");
167+
//for (size_t i = 0; i < grid_.size() / 3; ++i) {
168+
// std::fprintf(fp, "% 12.6f % 12.6f % 12.6f\n",
169+
// grid_[3*i], grid_[3*i + 1], grid_[3*i + 2]);
170+
//}
171+
//fclose(fp);
172+
173+
//fp = fopen("idx.dat", "w");
174+
//for (size_t i = 0; i < idx_.size(); ++i) {
175+
// std::fprintf(fp, "%d\n", idx_[i]);
176+
//}
177+
//fclose(fp);
178+
179+
//fp = fopen("delim.dat", "w");
180+
//for (size_t i = 0; i < delim.size(); ++i) {
181+
// std::fprintf(fp, "%d\n", delim[i]);
182+
//}
183+
//fclose(fp);
151184
}
152185

153186

154-
TEST_F(BatchTest, MaxMinOctantCluster)
187+
TEST_F(BatchTest, MaxMinOctant)
155188
{
156189
// This test applies maxmin to a set of points consisting of 8
157190
// well-separated, equal-sized clusters located in individual octants.

0 commit comments

Comments
 (0)