forked from nmayhall-vt/nbody_tucker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
executable file
·147 lines (117 loc) · 3.48 KB
/
test2.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
#! /usr/bin/env python
import numpy as np
import scipy
import scipy.linalg
import scipy.io
import copy as cp
from hdvv import *
"""
Test forming HDVV Hamiltonian and projecting onto "many-body tucker basis"
"""
n_sites = 8
lattice = np.ones(n_sites)
np.random.seed(2)
j12 = np.random.rand(n_sites,n_sites)
j12 = j12 - .5
j12 = j12 + j12.transpose()
np.fill_diagonal(j12,0)
if 0:
j12 = np.loadtxt("heis_j12.m")
if 0:
i = -5
j = -1
j12 = np.zeros([n_sites,n_sites])+j*np.random.rand(n_sites,n_sites)
j12 = np.zeros([n_sites,n_sites])+j
j12[0,1]=i
j12[0,2]=i
j12[0,3]=i
j12[1,2]=i
j12[1,3]=i
j12[2,3]=i
j12[4,5]=i
j12[4,6]=i
j12[4,7]=i
j12[5,6]=i
j12[5,7]=i
j12[6,7]=i
j12 = .5*(j12 + j12.transpose())
np.fill_diagonal(j12,0)
print j12
if 1:
i = -1
j = -.1
j12 = np.zeros([n_sites,n_sites])+j
j12 = np.zeros([n_sites,n_sites])+j*(np.random.rand(n_sites,n_sites)*2-.5)
j12[0,1]=i
j12[2,3]=i
j12[4,5]=i
j12[6,7]=i
j12 = .5*(j12 + j12.transpose())
np.fill_diagonal(j12,0)
print j12
H_tot, H_list, S2_tot = form_hdvv_H(lattice,j12)
l,v = np.linalg.eigh(H_tot)
S2_eig = np.dot(v.transpose(),np.dot(S2_tot,v))
au2ev = 27.21165;
au2cm = 219474.63;
convert = au2ev/au2cm; # convert from wavenumbers to eV
convert = 1; # 1 for wavenumbers
print " %5s %12s %12s %12s" %("State","Energy","Relative","<S2>")
for si,i in enumerate(l):
print " %5i = %12.8f %12.8f %12.8f" %(si,i*convert,(i-l[0])*convert,S2_eig[si,si])
if si>10:
break
v0 = v[:,0]
v0 = np.reshape(v0,4*np.ones(n_sites/2).astype(int))
#v0 = np.reshape(v0,16*np.ones(n_sites/4).astype(int))
#print v[:,0]
#print
#print v0
Acore, Atfac = tucker_decompose(v0,0,1)
B = tucker_recompose(Acore,Atfac)
print "\n Norm of Error tensor due to compression: %12.3e\n" %np.linalg.norm(B-v0)
#1-Body
if 0:
for si,i in enumerate(v0.shape):
n_modes = len(v0.shape)
dims2 = np.ones(n_modes)
dims2[si]=-1
Bcore, Btfac = tucker_decompose_list(v0,dims2)
B = B + tucker_recompose(Bcore,Btfac)
#2-Body
if 0:
n_modes = len(v0.shape)
dims = v0.shape
for si,i in enumerate(dims):
for sj,j in enumerate(dims):
if si>sj:
dims2 = np.ones(n_modes)
dims2[si]=-1
dims2[sj]=-1
Bcore, Btfac = tucker_decompose_list(v0,dims2)
B = B + tucker_recompose(Bcore,Btfac)
#3-Body
if 0:
dims = v0.shape
n_modes = len(dims)
for si,i in enumerate(dims):
for sj,j in enumerate(dims):
if si>sj:
for sk,k in enumerate(dims):
if sj>sk:
dims2 = np.ones(n_modes)
dims2[si]=-1
dims2[sj]=-1
dims2[sk]=-1
Bcore, Btfac = tucker_decompose_list(v0,dims2)
B = B + tucker_recompose(Bcore,Btfac)
B = np.reshape(B,[np.power(2,n_sites)])
BB = np.dot(B,B)
Bl = np.dot(B.transpose(),np.dot(H_tot,B))
Bs = np.dot(B.transpose(),np.dot(S2_tot,B))
print
print " Energy Error due to compression : %12.8f - %12.8f = %12.8f" %(Bl,l[0],Bl-l[0])
print " Energy/vv Error due to compression : %12.8f - %12.8f = %12.8f" %(Bl/BB,l[0],Bl/BB-l[0])
print " Spin Error due to compression : %12.8f %12.8f" %(S2_eig[0,0],Bs/BB)
print " Norm of compressed vector : %12.8f"%(BB)
#for si,i in enumerate(