Skip to content

Commit 8fbb5bb

Browse files
rootroot
authored andcommitted
test: add UT and annotations for mathzone class
1 parent 89fb837 commit 8fbb5bb

File tree

3 files changed

+208
-92
lines changed

3 files changed

+208
-92
lines changed

source/module_base/mathzone.h

Lines changed: 105 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,82 @@
11
#ifndef MATHZONE_H
22
#define MATHZONE_H
33

4-
#include "realarray.h"
5-
#include "matrix3.h"
64
#include "global_function.h"
7-
#include <vector>
8-
#include <map>
5+
#include "matrix3.h"
6+
#include "realarray.h"
7+
98
#include <cassert>
109
#include <complex>
10+
#include <map>
11+
#include <vector>
1112
namespace ModuleBase
1213
{
1314

15+
/**
16+
* @brief atomic coordinates conversion functions
17+
*
18+
*/
1419
class Mathzone
1520
{
16-
public:
17-
21+
public:
1822
Mathzone();
1923
~Mathzone();
2024

21-
template<class T>
22-
static T Max3(const T &a,const T &b,const T &c)
25+
public:
26+
/**
27+
* @brief Pointwise product of two vectors with same size
28+
*
29+
* @tparam Type
30+
* @param[in] f1
31+
* @param[in] f2
32+
* @return std::vector<Type>
33+
* @author Peize Lin (2016-08-03)
34+
*/
35+
template <typename Type>
36+
static std::vector<Type> Pointwise_Product(const std::vector<Type> &f1, const std::vector<Type> &f2)
2337
{
24-
if (a>=b && a>=c) return a;
25-
else if (b>=a && b>=c) return b;
26-
else if (c>=a && c>=b) return c;
27-
else throw std::runtime_error(ModuleBase::GlobalFunc::TO_STRING(__FILE__)+" line "+ModuleBase::GlobalFunc::TO_STRING(__LINE__));
38+
assert(f1.size() == f2.size());
39+
std::vector<Type> f(f1.size());
40+
for (int ir = 0; ir != f.size(); ++ir)
41+
f[ir] = f1[ir] * f2[ir];
42+
return f;
2843
}
2944

30-
// be careful, this can only be used for plane wave
31-
// during parallel calculation
32-
33-
public:
34-
35-
36-
37-
// Peize Lin add 2016-08-03
38-
template< typename Type >
39-
static std::vector<Type> Pointwise_Product( const std::vector<Type> &f1, const std::vector<Type> &f2 )
40-
{
41-
assert(f1.size()==f2.size());
42-
std::vector<Type> f(f1.size());
43-
for( int ir=0; ir!=f.size(); ++ir )
44-
f[ir] = f1[ir] * f2[ir];
45-
return f;
46-
}
47-
48-
//==========================================================
49-
// MEMBER FUNCTION :
50-
// NAME : Direct_to_Cartesian
51-
// use lattice vector matrix R
52-
// change the direct std::vector (dx,dy,dz) to cartesuab vectir
53-
// (cx,cy,cz)
54-
// (dx,dy,dz) = (cx,cy,cz) * R
55-
//
56-
// NAME : Cartesian_to_Direct
57-
// the same as above
58-
// (cx,cy,cz) = (dx,dy,dz) * R^(-1)
59-
//==========================================================
60-
static inline void Direct_to_Cartesian
61-
(
62-
const double &dx,const double &dy,const double &dz,
63-
const double &R11,const double &R12,const double &R13,
64-
const double &R21,const double &R22,const double &R23,
65-
const double &R31,const double &R32,const double &R33,
66-
double &cx,double &cy,double &cz)
45+
/**
46+
* @brief change direct coordinate (dx,dy,dz) to
47+
* Cartesian coordinate (cx,cy,cz), (dx,dy,dz) = (cx,cy,cz) * R
48+
*
49+
* @param[in] dx Direct coordinats
50+
* @param[in] dy
51+
* @param[in] dz
52+
* @param[in] R11 Lattice vector matrix R_ij: i_row, j_column
53+
* @param[in] R12
54+
* @param[in] R13
55+
* @param[in] R21
56+
* @param[in] R22
57+
* @param[in] R23
58+
* @param[in] R31
59+
* @param[in] R32
60+
* @param[in] R33
61+
* @param[out] cx Cartesian coordinats
62+
* @param[out] cy
63+
* @param[out] cz
64+
*/
65+
static inline void Direct_to_Cartesian(const double &dx,
66+
const double &dy,
67+
const double &dz,
68+
const double &R11,
69+
const double &R12,
70+
const double &R13,
71+
const double &R21,
72+
const double &R22,
73+
const double &R23,
74+
const double &R31,
75+
const double &R32,
76+
const double &R33,
77+
double &cx,
78+
double &cy,
79+
double &cz)
6780
{
6881
static ModuleBase::Matrix3 lattice_vector;
6982
static ModuleBase::Vector3<double> direct_vec, cartesian_vec;
@@ -88,13 +101,41 @@ class Mathzone
88101
return;
89102
}
90103

91-
static inline void Cartesian_to_Direct
92-
(
93-
const double &cx,const double &cy,const double &cz,
94-
const double &R11,const double &R12,const double &R13,
95-
const double &R21,const double &R22,const double &R23,
96-
const double &R31,const double &R32,const double &R33,
97-
double &dx,double &dy,double &dz)
104+
/**
105+
* @brief Change Cartesian coordinate (cx,cy,cz) to
106+
* direct coordinate (dx,dy,dz), (cx,cy,cz) = (dx,dy,dz) * R^(-1)
107+
*
108+
* @param[in] cx Cartesian coordinats
109+
* @param[in] cy
110+
* @param[in] cz
111+
* @param[in] R11 Lattice vector matrix R_ij: i_row, j_column
112+
* @param[in] R12
113+
* @param[in] R13
114+
* @param[in] R21
115+
* @param[in] R22
116+
* @param[in] R23
117+
* @param[in] R31
118+
* @param[in] R32
119+
* @param[in] R33
120+
* @param[out] dx Direct coordinats
121+
* @param[out] dy
122+
* @param[out] dz
123+
*/
124+
static inline void Cartesian_to_Direct(const double &cx,
125+
const double &cy,
126+
const double &cz,
127+
const double &R11,
128+
const double &R12,
129+
const double &R13,
130+
const double &R21,
131+
const double &R22,
132+
const double &R23,
133+
const double &R31,
134+
const double &R32,
135+
const double &R33,
136+
double &dx,
137+
double &dy,
138+
double &dz)
98139
{
99140
static ModuleBase::Matrix3 lattice_vector, inv_lat;
100141
lattice_vector.e11 = R11;
@@ -120,45 +161,17 @@ class Mathzone
120161
return;
121162
}
122163

123-
124-
static void To_Polar_Coordinate
164+
void To_Polar_Coordinate
125165
(
126-
const double &x_cartesian,
127-
const double &y_cartesian,
128-
const double &z_cartesian,
129-
double &r,
130-
double &theta,
131-
double &phi
132-
);
133-
134-
135-
// coeff1 * x1 + (1-coeff1) * x2
136-
// Peize Lin add 2017-08-09
137-
template< typename T, typename T_coeff >
138-
static T Linear_Mixing( const T & x1, const T & x2, const T_coeff & coeff1 )
139-
{
140-
return coeff1 * x1 + (1-coeff1) * x2;
141-
}
142-
template< typename T, typename T_coeff >
143-
static std::vector<T> Linear_Mixing( const std::vector<T> & x1, const std::vector<T> & x2, const T_coeff & coeff1 )
144-
{
145-
assert(x1.size()==x2.size());
146-
std::vector<T> x;
147-
for( size_t i=0; i!=x1.size(); ++i )
148-
x.push_back( Linear_Mixing( x1[i], x2[i], coeff1 ) );
149-
return x;
150-
}
151-
template< typename T1, typename T2, typename T_coeff >
152-
static std::map<T1,T2> Linear_Mixing( const std::map<T1,T2> & x1, const std::map<T1,T2> & x2, const T_coeff & coeff1 )
153-
{
154-
std::map<T1,T2> x;
155-
for( const auto & x1i : x1 )
156-
x.insert( make_pair( x1i.first, Linear_Mixing( x1i.second, x2.at(x1i.first), coeff1 ) ) );
157-
return x;
158-
}
166+
const double &x_cartesian,
167+
const double &y_cartesian,
168+
const double &z_cartesian,
169+
double &r,
170+
double &theta,
171+
double &phi);
159172

160173
};
161174

162-
}
175+
} // namespace ModuleBase
163176

164177
#endif

source/module_base/test/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,20 @@ AddTest(
3838
LIBS ${math_libs}
3939
SOURCES complexmatrix_test.cpp ../complexmatrix.cpp ../matrix.cpp
4040
)
41+
AddTest(
42+
TARGET base_realarray
43+
SOURCES realarray_test.cpp ../realarray.cpp
44+
)
45+
AddTest(
46+
TARGET base_intarray
47+
SOURCES intarray_test.cpp ../intarray.cpp
48+
)
49+
AddTest(
50+
TARGET base_vector3
51+
SOURCES vector3_test.cpp
52+
)
53+
AddTest(
54+
TARGET base_mathzone
55+
LIBS ${math_libs}
56+
SOURCES mathzone_test.cpp ../mathzone.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../memory.cpp ../timer.cpp
57+
)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include "../mathzone.h"
2+
#include "../matrix3.h"
3+
#include "../vector3.h"
4+
#include "gtest/gtest.h"
5+
#include "gmock/gmock.h"
6+
7+
/************************************************
8+
* unit test of class Mathzone
9+
***********************************************/
10+
11+
/**
12+
* - Tested Functions:
13+
* - PointwiseProduct
14+
* - return a vector, which is the pointwise
15+
* - product of another two vectors of the same
16+
* - length
17+
* - Direct2Cartesian
18+
* - change atomic coordinates from direct
19+
* - to Cartesian
20+
* - Cartesian2Direct
21+
* - change atomic coordinates from Cartesian
22+
* - to Direct
23+
*/
24+
25+
class MathzoneTest : public testing::Test
26+
{
27+
protected:
28+
double R11 = 3.68; double R12 = 0.00; double R13 = 0.00;
29+
double R21 = 0.00; double R22 = 10.1; double R23 = 0.00;
30+
double R31 = 0.00; double R32 = 0.00; double R33 = 26.7;
31+
ModuleBase::Matrix3 lattice;
32+
ModuleBase::Vector3<double> direct, cartesian;
33+
};
34+
35+
TEST_F(MathzoneTest, PointwiseProduct)
36+
{
37+
std::vector<double> aa, bb, cc;
38+
for(int i=0;i<10;i++)
39+
{
40+
aa.push_back(i*i);
41+
bb.push_back(i*2);
42+
}
43+
cc = ModuleBase::Mathzone::Pointwise_Product(aa,bb);
44+
for(int i=0;i<10;i++)
45+
{
46+
EXPECT_EQ(cc[i],i*i*i*2);
47+
}
48+
}
49+
50+
TEST_F(MathzoneTest, Direct2Cartesian)
51+
{
52+
direct.set(0.1,0.2,0.4);
53+
cartesian.set(0.368,2.02,10.68);
54+
ModuleBase::Vector3<double> cartnew;
55+
ModuleBase::Mathzone::Direct_to_Cartesian(direct.x,
56+
direct.y,
57+
direct.z,
58+
R11, R12, R13,
59+
R21, R22, R23,
60+
R31, R32, R33,
61+
cartnew.x,
62+
cartnew.y,
63+
cartnew.z);
64+
EXPECT_NEAR(cartnew.x,cartesian.x, 1e-15);
65+
EXPECT_NEAR(cartnew.y,cartesian.y, 1e-15);
66+
EXPECT_NEAR(cartnew.z,cartesian.z, 1e-15);
67+
}
68+
69+
TEST_F(MathzoneTest, Cartesian2Direct)
70+
{
71+
direct.set(0.1,0.2,0.4);
72+
cartesian.set(0.368,2.02,10.68);
73+
ModuleBase::Vector3<double> directnew;
74+
ModuleBase::Mathzone::Cartesian_to_Direct(cartesian.x,
75+
cartesian.y,
76+
cartesian.z,
77+
R11, R12, R13,
78+
R21, R22, R23,
79+
R31, R32, R33,
80+
directnew.x,
81+
directnew.y,
82+
directnew.z);
83+
EXPECT_NEAR(directnew.x,direct.x, 1e-15);
84+
EXPECT_NEAR(directnew.y,direct.y, 1e-15);
85+
EXPECT_NEAR(directnew.z,direct.z, 1e-15);
86+
}

0 commit comments

Comments
 (0)