-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
utils.py
44 lines (35 loc) · 1.16 KB
/
utils.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
import numpy as np
# integral
def integral(x,y,axis=0):
dx=x[1]-x[0]
return np.sum(y*dx, axis=axis)
def get_nx(num_electron, psi, x):
# normalization
I=integral(x,psi**2,axis=0)
normed_psi=psi/np.sqrt(I)[None, :]
# occupation num
fn=[2 for _ in range(num_electron//2)]
if num_electron % 2:
fn.append(1)
# density
res=np.zeros_like(normed_psi[:,0])
for ne, psi in zip(fn,normed_psi.T):
res += ne*(psi**2)
return res
def get_exchange(nx,x):
energy=-3./4.*(3./np.pi)**(1./3.)*integral(x,nx**(4./3.))
potential=-(3./np.pi)**(1./3.)*nx**(1./3.)
return energy, potential
def get_hatree(nx,x, eps=1e-1):
h=x[1]-x[0]
energy=np.sum(nx[None,:]*nx[:,None]*h**2/np.sqrt((x[None,:]-x[:,None])**2+eps)/2)
potential=np.sum(nx[None,:]*h/np.sqrt((x[None,:]-x[:,None])**2+eps),axis=-1)
return energy, potential
def print_log(i,log):
print(f"step: {i:<5} energy: {log['energy'][-1]:<10.4f} energy_diff: {log['energy_diff'][-1]:.10f}")
def get_d_d2(h, n_grid):
D=-np.eye(n_grid)+np.diagflat(np.ones(n_grid-1),1)
D = D / h
D2=D.dot(-D.T)
D2[-1,-1]=D2[0,0]
return D, D2