-
Notifications
You must be signed in to change notification settings - Fork 1
/
dvr.py
172 lines (137 loc) · 6.48 KB
/
dvr.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
"""Discrete variable representation functions
More info: TBW"""
import numpy as np
from scipy.sparse import kronsum, csr_matrix
from itertools import product
def calc_grid(dimension, basis_specs):
"""Diagonalizes the matrix of the position operator
:dimension: dimension of the variational basis = number of grid points
:basis_specs: array with a string which specifies the variational basis
as first entry and an array with basis-characteristic parameters
as the second
entry; e.g.
basis_specs = ['SINE', [spatial_dim, box_size, box_origin, mass]]
basis_specs = ['HO', [spatial_dim, x_equilibrium, mass, omega]]
:return: the eigenvalues of the position-operator matrix and
the transformation matrix such that diag_x = U^T * x * U
"""
pos_op_mat = np.zeros((dimension, dimension))
if basis_specs[0] == 'SINE':
pos_op_mat[0, 1] = 0.5
pos_op_mat[dimension - 1, dimension - 2] = 0.5
for j in range(1, dimension - 1):
pos_op_mat[j, j + 1] = 0.5
pos_op_mat[j, j - 1] = 0.5
evsys = np.linalg.eigh(pos_op_mat)
evs = basis_specs[1][2] + basis_specs[1][1] / np.pi * np.arccos(
evsys[0])
return [np.sort(evs), evsys[1]]
if basis_specs[0] == 'HO':
for j in range(dimension):
for k in range(dimension):
if j == k:
pos_op_mat[j, k] = basis_specs[1][1]
if j == k - 1:
pos_op_mat[j, k] = np.sqrt(
(j + 1) / (2 * basis_specs[1][2] * basis_specs[1][3]))
if j == k + 1:
pos_op_mat[j, k] = np.sqrt(
j / (2 * basis_specs[1][2] * basis_specs[1][3]))
return np.linalg.eigh(pos_op_mat)
def calc_Ekin(dim_basis, n_part, basis_specs, eigensys_coord):
"""Calculates the kinetic energy matrix.
1) fill 1-particle matrix for 1 Cartesian dimension
2) fill the N-particle, D-cart-dim matrix via a double Kronecker sum
:dim_basis: dvr basis dimension '=' nbr of grid points (for ONE dimension)
:n_part: nbr of particles
:basis_specs: type and parameters of the variation basis (includes spatial dimensionality)
:eigensys_coord: eigenvectors and transformation matrix from variational to discrete basis
:return: Kinetic energy matrix
"""
tmp = np.zeros((dim_basis, dim_basis))
tmp2 = np.zeros((dim_basis, dim_basis))
# analytic DVR of the kinetic-energy operator from
# The Journal of Chemical Physics 96, 1982 (1992), Eq. A6a/b
if basis_specs[0] == 'SINE':
# infer the constant grid spacing (dx) and the box length (L) from
# the eigenvalues; as the eigenvalues do not include the edges, 2*dx
# has to be added;
if sum(
np.isclose(
np.diff(eigensys_coord[0]),
np.roll(np.diff(eigensys_coord[0]),
1))) != len(eigensys_coord[0]) - 1:
print(
'Grid points of the SINE basis are not equally spaced! Exiting...'
)
exit()
dx = abs(eigensys_coord[0][1] - eigensys_coord[0][0])
L = abs(eigensys_coord[0][-1] - eigensys_coord[0][0]) + 2 * dx
for i in range(dim_basis):
for ip in range(dim_basis):
i_idx = i + 1
ip_idx = ip + 1 # to array indices (i,ip) add 1
if i == ip:
tmp[i, i] = (((2. * (dim_basis + 1)**2 + 1) / 3.) -
np.sin(i_idx * (np.pi /
(dim_basis + 1)))**(-2))
else:
tmp[i, ip] = ((-1)**(i_idx - ip_idx)) * (
np.sin(np.pi / (2. * (dim_basis + 1)) *
(i_idx - ip_idx))**
(-2) - np.sin(np.pi / (2. * (dim_basis + 1)) *
(i_idx + ip_idx))**(-2))
tmp[i, ip] *= np.pi**2 / (2. * L**2)
tmp = tmp / (2. * basis_specs[1][3])
# this factor has to be verified!
if basis_specs[0] == 'HO':
for alpha in range(dim_basis):
for beta in range(dim_basis):
for k in range(dim_basis):
tmp[alpha, beta] += basis_specs[1][3] * eigensys_coord[1][
k, alpha] * (k + 0.5) * eigensys_coord[1][k, beta]
if alpha == beta:
tmp[alpha, beta] -= 0.5 * basis_specs[1][2] * basis_specs[
1][3]**2 * (
eigensys_coord[0][alpha] - basis_specs[1][1])**2
mEkin = csr_matrix(0)
# the two loops represent the Kronecker sums which generate the full matrix
# for N particles, each in D Cartesian dimensions
for particle in range(n_part):
for cart_dim in range(basis_specs[1][0]):
mEkin = kronsum(mEkin, tmp)
return mEkin
def calc_potential(n_part, space_dims, pot_specs, eigensys_coord):
"""Calculates the potential energy matrix:
2-body Gauss: V(r1,r2) = lec * exp(-beta * (r1-r2)**2)
N-body HO: V(r1,...,rN) = 1/2 m omega^2 (r1^2 + ... + rN^2)
:space_dims: Spatial dimensionality
::
:return: Potential energy at (row_idx, col_idx)
"""
# calculate dimension of the Hilbert space as a direct product of
# spaces for each particle and spatial dimension
dim_hilber = (len(eigensys_coord[0]))**(n_part * space_dims)
# we populate only diagonal elements (to be changed, later)
mpotential = np.zeros((dim_hilber))
idx = 0
grd1D = product(eigensys_coord[0], repeat=(space_dims * n_part))
if pot_specs[0] == 'GAUSS':
for coord in grd1D:
mpotential[idx] = pot_specs[2] * np.exp(
-pot_specs[1] * sum([(coord[n] - coord[n + space_dims])**2
for n in range(space_dims)]))
idx += 1
if pot_specs[0] == 'HO':
for coord in grd1D:
mpotential[idx] = 0.5 * pot_specs[2] * pot_specs[3]**2 * sum([(
i - pot_specs[1])**2 for i in coord])
idx += 1
if pot_specs[0] == 'HOINT':
for coord in grd1D:
mpotential[idx] = (0.5 * pot_specs[1]**2 * sum(
np.array([[[(coord[i + x] - coord[j + x])**2
for i in range(n_part)] for j in range(n_part)]
for x in range(space_dims)]).flatten()))
idx += 1
return mpotential