OpenMPCD
test_Autocorrelation.py
1 import pytest
2 
3 from MPCDAnalysis.Autocorrelation import Autocorrelation
4 from MPCDAnalysis.OnTheFlyStatisticsDDDA import OnTheFlyStatisticsDDDA
5 
6 def test_constructor():
7  with pytest.raises(TypeError):
8  Autocorrelation(1.0)
9 
10  with pytest.raises(ValueError):
11  Autocorrelation(-1)
12 
13  Autocorrelation(0)
14  Autocorrelation(1)
15  Autocorrelation(2)
16 
17 
18 def test_getMaxCorrelationTime():
19  for N_max in range(0, 5):
20  ac = Autocorrelation(N_max)
21  assert ac.getMaxCorrelationTime() == N_max
22 
23 
24 def test_correlationTimeIsAvailable():
25  N_max = 10
26 
27  def testExceptions(ac):
28  for N in range(0, ac.getMaxCorrelationTime() + 1):
29  with pytest.raises(TypeError):
30  ac.correlationTimeIsAvailable(float(N))
31 
32  with pytest.raises(ValueError):
33  ac.correlationTimeIsAvailable(-1)
34 
35  with pytest.raises(ValueError):
36  ac.correlationTimeIsAvailable(ac.getMaxCorrelationTime() + 1)
37 
38 
39 
40  ac = Autocorrelation(N_max)
41 
42  testExceptions(ac)
43 
44  for datum in range(0, N_max * 2):
45  dataPointsSuppliedSoFar = datum
46  for N in range(0, N_max + 1):
47  if dataPointsSuppliedSoFar >= N + 1:
48  expected = True
49  else:
50  expected = False
51  assert ac.correlationTimeIsAvailable(N) == expected
52 
53  ac.addDatum(datum)
54 
55  testExceptions(ac)
56 
57 
58 def test_getAutocorrelation():
59  import random
60  n_max = 100
61  N_max = 10
62 
63  data = [random.uniform(-10, 10) for _ in range(0, n_max)]
64 
65  ac = Autocorrelation(N_max)
66 
67  for datumIndex, datum in enumerate(data):
68  ac.addDatum(datum)
69 
70  dataSupplied = data[0 : datumIndex + 1]
71  for N in range(0, N_max + 1):
72  if not ac.correlationTimeIsAvailable(N):
73  continue
74 
75  ddda = OnTheFlyStatisticsDDDA()
76  for i in range(0, len(dataSupplied)):
77  if i + N >= len(dataSupplied):
78  break
79  ddda.addDatum(dataSupplied[i] * dataSupplied[i + N])
80 
81  assert ac.getAutocorrelation(N) == ddda
82 
83 
84 def test_getAutocorrelation_Vector3DReal():
85  import random
86  n_max = 100
87  N_max = 10
88 
89  from MPCDAnalysis.Vector3DReal import Vector3DReal
90  data = [
91  Vector3DReal(
92  random.uniform(-10, 10),
93  random.uniform(-10, 10),
94  random.uniform(-10, 10))
95  for _ in range(0, n_max)]
96 
97  ac = Autocorrelation(N_max)
98 
99  def mul(v1, v2):
100  return v1.dot(v2)
101 
102  for datumIndex, datum in enumerate(data):
103  ac.addDatum(datum, mul)
104 
105  dataSupplied = data[0 : datumIndex + 1]
106  for N in range(0, N_max + 1):
107  if not ac.correlationTimeIsAvailable(N):
108  continue
109 
110  ddda = OnTheFlyStatisticsDDDA()
111  for i in range(0, len(dataSupplied)):
112  if i + N >= len(dataSupplied):
113  break
114  ddda.addDatum(dataSupplied[i].dot(dataSupplied[i + N]))
115 
116  assert ac.getAutocorrelation(N) == ddda
MPCDAnalysis.Vector3DReal
Definition: Vector3DReal.py:1
MPCDAnalysis.Autocorrelation
Definition: Autocorrelation.py:1
MPCDAnalysis.OnTheFlyStatisticsDDDA
Definition: OnTheFlyStatisticsDDDA.py:1