/
opan_utils_vector.py
404 lines (337 loc) · 16.5 KB
/
opan_utils_vector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#-------------------------------------------------------------------------------
# Name: opan_utils_vector
# Purpose: Test objects for opan.utils.vector
#
# Author: Brian Skinn
# bskinn@alum.mit.edu
#
# Created: 19 Apr 2016
# Copyright: (c) Brian Skinn 2016
# License: The MIT License; see "license.txt" for full license terms
# and contributor agreement.
#
# This file is part of opan (Open Anharmonic), a system for automated
# computation of anharmonic properties of molecular systems via wrapper
# calls to computational/quantum chemical software packages.
#
# http://www.github.com/bskinn/opan
#
#-------------------------------------------------------------------------------
import unittest
class TestOpanUtilsVectorParallelCheck(unittest.TestCase):
def test_Utils_Vector_ParallelCheck_Good(self):
from opan.utils.vector import parallel_check as pc
import numpy as np
# Parallel vectors
self.assertTrue(pc(np.array([1, 2, 3]),
np.array([1.5, 3, 4.5])))
# Anti-parallel vectors
self.assertTrue(pc(np.array([-1, 5.3, 3.1]),
np.array([3, -15.9, -9.3])))
# Non-(anti-)parallel vectors
self.assertFalse(pc(np.array([4.8, 0.35, -1.822]),
np.array([-1.3, 3.77, 19.14])))
def test_Utils_Vector_ParallelCheck_BadShape(self):
from opan.utils.vector import parallel_check as pc
import numpy as np
self.assertRaises(ValueError, pc,
np.array([[1, 2, 3], [3, 2, 1]]),
np.array([2, 4, 9]))
def test_Utils_Vector_ParallelCheck_LenMismatch(self):
from opan.utils.vector import parallel_check as pc
import numpy as np
self.assertRaises(ValueError, pc,
np.array(range(3)),
np.array(range(4)))
class TestOpanUtilsVectorProjRejAngle(unittest.TestCase):
import numpy as np
from opan.const import OpanEnum
class DType(OpanEnum):
V1 = 'V1'
V2 = 'V2'
PROJ = 'PROJ'
REJ = 'REJ'
ANG = 'ANG'
class VecType(OpanEnum): # Types of vectors
O1 = 'O1' # Both order-one
LOL = 'LOL' # Both large (large on large)
SOS = 'SOS' # Both small (small on small)
LOS = 'LOS' # Large onto small
SOL = 'SOL' # Small onto large
BS = 'BS' # Badly-scaled
class RelType(OpanEnum): # Type of vector relationship
NS = 'NS' # Nonspecific
PAR = 'PAR' # Nearly parallel
NORM = 'NORM' # Nearly normal
AP = 'AP' # Nearly anti-parallel
namestr = "{0}_{1}"
# Dict of dicts of data
data = {
# Unremarkable vectors with ~order-one components
namestr.format(RelType.NS, VecType.O1) :
{DType.V1: np.array([1, 2, 3]),
DType.V2: np.array([-1, 3, 8]),
DType.PROJ: np.array([-0.391892, 1.175676, 3.135135]),
DType.REJ: np.array([1.391892, 0.824324, -0.135135]),
DType.ANG: np.float_(25.712002)},
# Nearly-normal vectors with ~order-one components
namestr.format(RelType.NORM, VecType.O1) :
{DType.V1: np.array([2, 8, -4, 2.5, -1.4]),
DType.V2: np.array([-1, 3, 6.2, 5, 7]),
DType.PROJ: np.array([0.000817, -0.002450, -0.005064,
-0.004084, -0.005717]),
DType.REJ: np.array([1.999183, 8.002450, -3.994936,
2.504084, -1.394283]),
DType.ANG: np.float_(90.053923)},
# Nearly-parallel vectors with ~order-one components
namestr.format(RelType.PAR, VecType.O1) :
{DType.V1: np.array([1, 2, 2.9999, 4]),
DType.V2: np.array([1.0001, 2, 3, 4]),
DType.PROJ: np.array([1.000087, 1.999973, 2.999960, 3.999947]),
DType.REJ: np.array([-0.000087, 0.000027,
-0.000060, 0.000053]),
DType.ANG: np.float_(0.001267)},
# Nearly-antiparallel vectors with ~order-one components
namestr.format(RelType.AP, VecType.O1) :
{DType.V1: np.array([-0.5, 2.3, 1.4, -3.1]),
DType.V2: np.array([0.50001, -2.29999, -1.4, 3.1]),
DType.PROJ: np.array([-0.500011, 2.299992,
1.400001, -3.100003]),
DType.REJ: np.array([0.000011, 0.000008,
-0.000001, 0.000003]),
DType.ANG: np.float_(179.999814)},
# Two large vectors far from parallel/normal
namestr.format(RelType.NS, VecType.LOL) :
{DType.V1: np.array([376328., 384874.,
992834., 182873.]),
DType.V2: np.array([538344., 283747.,
1838447., 929292.]),
DType.PROJ: np.array([269185.658799, 141880.699195,
919270.144855, 464669.577884]),
DType.REJ: np.array([107142.341201, 242993.300805,
73563.855145, -281796.577884]),
DType.ANG: np.float_(20.151554)},
# Two small vectors far from parallel/normal
namestr.format(RelType.NS, VecType.SOS) :
{DType.V1: np.array([0.000045, -0.00031,
-0.000915, 0.000002]),
DType.V2: np.array([0.0002874, -0.0003987,
0.0000034, 0.000719]),
DType.PROJ: np.array([0.000051, -0.000071,
0.000001, 0.000128]),
DType.REJ: np.array([-0.000006, -0.000239,
-0.000916, -0.000126]),
DType.ANG: np.float_(80.787151)},
# Large onto small, far from parallel/normal
namestr.format(RelType.NS, VecType.LOS) :
{DType.V1: np.array([238973., 239884.,
-1092938., -893983.]),
DType.V2: np.array([0.0002874, -0.0003987,
0.0000034, 0.000719]),
DType.PROJ: np.array([-255163.218951, 353979.037564,
-3018.632375, -638351.963904]),
DType.REJ: np.array([494136.218951, -114095.037564,
-1089919.367625, -255631.036096]),
DType.ANG: np.float_(122.176632)},
# Small onto large, far from parallel/normal
namestr.format(RelType.NS, VecType.SOL) :
{DType.V1: np.array([0.00000374, -0.0000233,
0.0002837, 0.0000026]),
DType.V2: np.array([538344., 283747., 1838447., 929292.]),
DType.PROJ: np.array([0.000061, 0.000032,
0.000207, 0.000105]),
DType.REJ: np.array([-0.000057, -0.000055,
0.000077, -0.000102]),
DType.ANG: np.float_(31.859107)},
# Two badly scaled vectors far from parallel/normal
namestr.format(RelType.NS, VecType.BS) :
{DType.V1: np.array([0.000015, 6214., -0.000235, 12374.]),
DType.V2: np.array([-0.00005, 38184., 0.000045, 21669.]),
DType.PROJ: np.array([-0.000013, 10011.853795,
0.000012, 5681.616904]),
DType.REJ: np.array([0.000028, -3797.853795,
-0.000247, 6692.383096]),
DType.ANG: np.float_(33.760587)},
# Two large vectors nearly parallel
namestr.format(RelType.PAR, VecType.LOL) :
{DType.V1: np.array([554387., 38185., -532247., 12374.]),
DType.V2: np.array([554389., 38184., -532248., 12375.]),
DType.PROJ: np.array([554387.488030, 38183.895862,
-532246.548415, 12374.966250]),
DType.REJ: np.array([-0.488030, 1.104138,
-0.451585, -0.966250]),
DType.ANG: np.float_(0.000120)},
# Two small vectors nearly parallel
namestr.format(RelType.PAR, VecType.SOS) :
{DType.V1: np.array([0.000015, 0.000016, -0.000042,
-0.000034, 0.000065, -0.000033]),
DType.V2: np.array([0.000014, 0.000017, -0.000041,
-0.000033, 0.000066, -0.000032]),
DType.PROJ: np.array([0.000014, 0.000017, -0.000041,
-0.000033, 0.000066, -0.000032]),
DType.REJ: np.array([0.000001, -0.000001, -0.000001,
-0.000001, -0.000001, -0.000001]),
DType.ANG: np.float_(1.483536)},
# Large onto small, nearly parallel
namestr.format(RelType.PAR, VecType.LOS) :
{DType.V1: np.array([14001., 17002., -41003., -33001.,
66004., -32005., 75008.]),
DType.V2: np.array([0.000014, 0.000017, -0.000041,
-0.000033, 0.000066, -0.000032, 0.000075]),
DType.PROJ: np.array([14001.205610, 17001.463955,
-41003.530715, -33002.841795,
66005.683590, -32002.755680,
75006.458626]),
DType.REJ: np.array([-0.205610, 0.536045, 0.530715,
1.841795, -1.683590, -2.244320,
1.541374]),
DType.ANG: np.float_(0.001811)},
# Small onto large, nearly parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Two badly scaled vectors nearly parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Two large vectors nearly normal
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Two small vectors nearly normal
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Large onto small, nearly normal
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Small onto large, nearly normal
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Two badly scaled vectors nearly normal
namestr.format(RelType.NORM, VecType.BS) :
{DType.V1: np.array([0.0015, 6214., 2319., 145.]),
DType.V2: np.array([8285., -0.0004, 0.0034, 2166.]),
DType.PROJ: np.array([35.485053, -0.000002,
0.000015, 9.277082]),
DType.REJ: np.array([-35.483553, 6214.000002,
2318.999985, 135.722918]),
DType.ANG: np.float_(89.683234)}
# Two large vectors nearly anti-parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Two small vectors nearly anti-parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Large onto small, nearly anti-parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Small onto large, nearly anti-parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()},
# Two badly scaled vectors nearly anti-parallel
#~ {DType.V1: np.array([]),
#~ DType.V2: np.array([]),
#~ DType.PROJ: np.array([]),
#~ DType.REJ: np.array([]),
#~ DType.ANG: np.float_()}
}
# Template functions
# Vector projection template
def template_proj(self, name, data):
from opan.utils.vector import proj
v1 = data[self.DType.V1]
v2 = data[self.DType.V2]
p = proj(v1, v2)
for i, t in enumerate(zip(p, data[self.DType.PROJ])):
self.assertAlmostEqual(*t, delta=1e-6,
msg="Test {0}: Index {1}; V1 = {2}; V2 = {3}"
.format(name, i, v1, v2))
# Vector rejection template
def template_rej(self, name, data):
from opan.utils.vector import rej
v1 = data[self.DType.V1]
v2 = data[self.DType.V2]
r = rej(v1, v2)
for i, t in enumerate(zip(r, data[self.DType.REJ])):
self.assertAlmostEqual(*t, delta=1e-6,
msg="Test {0}: Index {1}; V1 = {2}; V2 = {3}"
.format(name, i, v1, v2))
# Vector angle template
def template_angle(self, name, data):
from opan.utils.vector import vec_angle
v1 = data[self.DType.V1]
v2 = data[self.DType.V2]
a = vec_angle(v1, v2)
self.assertAlmostEqual(a, data[self.DType.ANG], delta=1e-6,
msg="Test {0}: V1 = {1}; V2 = {2}"
.format(name, v1, v2))
# Populate the local namespace with the auto-generated
# test methods
for k, d in data.items():
# Vector projection
fxnname = "test_Vector_Proj_Good_{0}".format(k)
fxn = lambda self, k=k, d=d: self.template_proj(k, d)
locals().update({fxnname: fxn})
# Vector rejection
fxnname = "test_Vector_Rej_Good_{0}".format(k)
fxn = lambda self, k=k, d=d: self.template_rej(k, d)
locals().update({fxnname: fxn})
# Vector angle
fxnname = "test_Vector_Angle_Good_{0}".format(k)
fxn = lambda self, k=k, d=d: self.template_angle(k, d)
locals().update({fxnname: fxn})
def setUp(self):
self.longMessage = True
def test_Utils_Vector_Proj_BadVec_NotVector(self):
import numpy as np
from opan.utils.vector import proj
self.assertRaises(ValueError, proj,
np.array(range(16)).reshape((4, 4)),
np.array(range(16)))
def test_Utils_Vector_Proj_BadVecOnto_NotVector(self):
import numpy as np
from opan.utils.vector import proj
self.assertRaises(ValueError, proj,
np.array(range(16)),
np.array(range(16)).reshape((4, 4)))
def test_Utils_Vector_Proj_BadVecsShapeMismatch(self):
import numpy as np
from opan.utils.vector import proj
self.assertRaises(ValueError, proj,
np.array(range(5)), np.array(range(6)))
def suite():
s = unittest.TestSuite()
tl = unittest.TestLoader()
s.addTests([tl.loadTestsFromTestCase(TestOpanUtilsVectorParallelCheck),
tl.loadTestsFromTestCase(TestOpanUtilsVectorProjRejAngle)
])
return s
if __name__ == '__main__': # pragma: no cover
print("Module not executable.")