Skip to content

Commit 5ec9acf

Browse files
author
Adrian Roman
committed
Code quality improvements
1 parent 74c45ec commit 5ec9acf

File tree

4 files changed

+181
-196
lines changed

4 files changed

+181
-196
lines changed

Car-Parrinello.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -649,5 +649,4 @@ def TwoElectronTwoCenterDeriv(alpha, beta, gamma, delta, Ra, Rb, Rc, Rd, X):
649649
# In[28]:
650650

651651

652-
print(X) # the experimental value given in the book is 1.401, the equilibrium with Hartree-Fock using only s-type orbitals is 1.3881
653-
652+
print(X) # the experimental value given in the book is 1.401, the equilibrium with Hartree-Fock using only s-type orbitals is 1.3881

dft.ipynb

+111-117
Large diffs are not rendered by default.

dft.py

+68-69
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def CreateIndices(S):
9090

9191
def Gaussian(r, sigma = 0.5):
9292
twosigma2 = 2. * sigma * sigma
93-
return np.exp(-r*r/twosigma2) / np.power(np.sqrt(m.pi * twosigma2), 3);
93+
return np.exp(-r*r/twosigma2) / np.power(np.sqrt(m.pi * twosigma2), 3)
9494

9595

9696
# In[9]:
@@ -137,42 +137,42 @@ def fft3(dat, N, s):
137137
# In[14]:
138138

139139

140-
def cI(input):
141-
return fft3(input, S, 1)
140+
def cI(inp):
141+
return fft3(inp, S, 1)
142142

143143

144144
# In[15]:
145145

146146

147-
def cJ(input):
148-
return 1. / np.prod(S) * fft3(input, S, -1)
147+
def cJ(inp):
148+
return 1. / np.prod(S) * fft3(inp, S, -1)
149149

150150

151151
# In[16]:
152152

153153

154-
def O(input):
155-
return splalg.det(R) * input
154+
def O(inp):
155+
return splalg.det(R) * inp
156156

157157

158158
# In[17]:
159159

160160

161-
def L(input):
162-
return -splalg.det(R) * G2 * input
161+
def L(inp):
162+
return -splalg.det(R) * G2 * inp
163163

164164

165165
# In[18]:
166166

167167

168168
def Linv(inp):
169169
if inp.ndim == 1:
170-
input = np.reshape(inp, (inp.size, 1))
170+
vals = np.reshape(inp, (inp.size, 1))
171171
else:
172-
input = inp
172+
vals = inp
173173

174174
old_settings = np.seterr(divide='ignore', invalid='ignore')
175-
result = -1. / splalg.det(R) * input / np.reshape(G2, input.shape)
175+
result = -1. / splalg.det(R) * vals / np.reshape(G2, vals.shape)
176176
result[0] = 0
177177
np.seterr(**old_settings)
178178
return result
@@ -238,7 +238,7 @@ def Plot(dat):
238238
ax3 = fig.add_subplot(1, 3, 3, projection='3d')
239239
ax3.plot_surface(xs, ys, toplot3, cmap='viridis', edgecolor='none')
240240

241-
plt.tight_layout
241+
plt.tight_layout()
242242
plt.show()
243243

244244

@@ -334,13 +334,13 @@ def Plot(dat):
334334

335335
def cI(inp):
336336
if inp.ndim == 1:
337-
input = np.reshape(inp, (inp.size, 1))
337+
vals = np.reshape(inp, (inp.size, 1))
338338
else:
339-
input = inp
339+
vals = inp
340340

341-
out = np.zeros(input.shape, dtype = "complex_")
342-
for col in range(np.size(input, 1)):
343-
out[:,col] = fft3(input[:,col], S, 1)
341+
out = np.zeros(vals.shape, dtype = "complex_")
342+
for col in range(np.size(vals, 1)):
343+
out[:,col] = fft3(vals[:,col], S, 1)
344344

345345
return out
346346

@@ -350,15 +350,15 @@ def cI(inp):
350350

351351
def cJ(inp):
352352
if inp.ndim == 1:
353-
input = np.reshape(inp, (inp.size, 1))
353+
vals = np.reshape(inp, (inp.size, 1))
354354
else:
355-
input = inp
355+
vals = inp
356356

357357
norm = 1. / np.prod(S)
358-
out = np.zeros(input.shape, dtype = "complex_")
358+
out = np.zeros(vals.shape, dtype = "complex_")
359359

360-
for col in range(np.size(input, 1)):
361-
out[:,col] = norm * fft3(input[:,col], S, -1)
360+
for col in range(np.size(vals, 1)):
361+
out[:,col] = norm * fft3(vals[:,col], S, -1)
362362

363363
return out
364364

@@ -368,26 +368,26 @@ def cJ(inp):
368368

369369
def L(inp):
370370
if inp.ndim == 1:
371-
input = np.reshape(inp, (inp.size, 1))
371+
vals = np.reshape(inp, (inp.size, 1))
372372
else:
373-
input = inp
373+
vals = inp
374374

375-
return -splalg.det(R) * (G2 @ np.ones((1, np.size(input, 1)))) * input
375+
return -splalg.det(R) * (G2 @ np.ones((1, np.size(vals, 1)))) * vals
376376

377377

378378
# In[37]:
379379

380380

381381
def cIdag(inp):
382382
if inp.ndim == 1:
383-
input = np.reshape(inp, (inp.size, 1))
383+
vals = np.reshape(inp, (inp.size, 1))
384384
else:
385-
input = inp
385+
vals = inp
386386

387-
out = np.zeros(input.shape, dtype = "complex_")
387+
out = np.zeros(vals.shape, dtype = "complex_")
388388

389-
for col in range(np.size(input, 1)):
390-
out[:,col] = fft3(input[:,col], S, -1)
389+
for col in range(np.size(vals, 1)):
390+
out[:,col] = fft3(vals[:,col], S, -1)
391391

392392
return out
393393

@@ -397,15 +397,15 @@ def cIdag(inp):
397397

398398
def cJdag(inp):
399399
if inp.ndim == 1:
400-
input = np.reshape(inp, (inp.size, 1))
400+
vals = np.reshape(inp, (inp.size, 1))
401401
else:
402-
input = inp
402+
vals = inp
403403

404404
norm = 1. / np.prod(S)
405-
out = np.zeros(input.shape, dtype = "complex_")
405+
out = np.zeros(vals.shape, dtype = "complex_")
406406

407-
for col in range(np.size(input, 1)):
408-
out[:,col] = norm * fft3(input[:,col], S, 1)
407+
for col in range(np.size(vals, 1)):
408+
out[:,col] = norm * fft3(vals[:,col], S, 1)
409409

410410
return out
411411

@@ -572,7 +572,7 @@ def getPsi(W):
572572
# In[54]:
573573

574574

575-
epsilon
575+
print(epsilon)
576576

577577

578578
# In[55]:
@@ -725,7 +725,7 @@ def H(W):
725725
# In[66]:
726726

727727

728-
epsilon
728+
print(epsilon)
729729

730730

731731
# In[67]:
@@ -873,8 +873,8 @@ def lm(Win, Nit, fillE = True):
873873
# In[76]:
874874

875875

876-
def K(input):
877-
return input / (1. + G2)
876+
def K(inp):
877+
return inp / (1. + G2)
878878

879879

880880
# The preconditioned line minimization is the same as the line minimization above, but with the direction changed:
@@ -1229,45 +1229,45 @@ def pccg(Win, Nit, cgform, fillE = True, alphat = 0.00003):
12291229

12301230
def L(inp):
12311231
if inp.ndim == 1:
1232-
input = np.reshape(inp, (inp.size, 1))
1232+
out = np.reshape(inp, (inp.size, 1))
12331233
else:
1234-
input = inp
1234+
out = inp
12351235

1236-
if np.size(input, 0) == G2c.size:
1237-
return -splalg.det(R) * (G2c @ np.ones((1, np.size(input, 1)))) * input
1236+
if np.size(out, 0) == G2c.size:
1237+
return -splalg.det(R) * (G2c @ np.ones((1, np.size(out, 1)))) * out
12381238

1239-
return -splalg.det(R) * (G2 @ np.ones((1, np.size(input, 1)))) * input
1239+
return -splalg.det(R) * (G2 @ np.ones((1, np.size(out, 1)))) * out
12401240

12411241

12421242
# In[92]:
12431243

12441244

1245-
def K(input):
1246-
if np.size(input, 0) == G2c.size:
1247-
return input / (1. + G2c)
1245+
def K(inp):
1246+
if np.size(inp, 0) == G2c.size:
1247+
return inp / (1. + G2c)
12481248

1249-
return input / (1. + G2)
1249+
return inp / (1. + G2)
12501250

12511251

12521252
# In[93]:
12531253

12541254

12551255
def cI(inp):
12561256
if inp.ndim == 1:
1257-
input = np.reshape(inp, (inp.size, 1))
1257+
vals = np.reshape(inp, (inp.size, 1))
12581258
else:
1259-
input = inp
1259+
vals = inp
12601260

12611261
pS = np.prod(S)
1262-
out = np.zeros((pS, np.size(input, axis = 1)), dtype = "complex_")
1262+
out = np.zeros((pS, np.size(vals, axis = 1)), dtype = "complex_")
12631263

1264-
if np.size(input, 0) == pS:
1265-
for col in range(np.size(input, 1)):
1266-
out[:,col] = fft3(input[:,col], S, 1)
1264+
if np.size(vals, 0) == pS:
1265+
for col in range(np.size(vals, 1)):
1266+
out[:,col] = fft3(out[:,col], S, 1)
12671267
else:
1268-
for col in range(np.size(input, 1)):
1268+
for col in range(np.size(vals, 1)):
12691269
full = np.zeros((pS, 1), dtype = "complex_")
1270-
full[active] = input[:,col]
1270+
full[active] = vals[:,col]
12711271
out[:,col] = fft3(full[:,col], S, 1)
12721272

12731273
return out
@@ -1278,16 +1278,16 @@ def cI(inp):
12781278

12791279
def cIdag(inp):
12801280
if inp.ndim == 1:
1281-
input = np.reshape(inp, (inp.size, 1))
1281+
vals = np.reshape(inp, (inp.size, 1))
12821282
cols = 1
12831283
else:
1284-
input = inp
1285-
cols = np.size(input, 1)
1284+
vals = inp
1285+
cols = np.size(vals, 1)
12861286

12871287
out = np.zeros((active[0].size, cols), dtype = "complex_")
12881288

12891289
for col in range(cols):
1290-
full = fft3(input[:,col], S, -1)
1290+
full = fft3(vals[:,col], S, -1)
12911291
out[:,col] = np.reshape(full,(full.size, 1))[active]
12921292

12931293
return out
@@ -1365,7 +1365,7 @@ def H(W):
13651365

13661366
Veff = Vdual + cJdag(O(PoissonSolve(n) + cJ(exc))) + np.reshape(excp, (excp.size, 1)) * cJdag(O(cJ(n)))
13671367

1368-
out = -0.5 * L(W);
1368+
out = -0.5 * L(W)
13691369

13701370
for col in range(np.size(W, axis = 1)):
13711371
out[:, col] += np.reshape(cIdag(Veff * cI(W[:,col])), np.size(out, axis = 0))
@@ -1463,7 +1463,7 @@ def PseudoGe(pos):
14631463
if pos < 1E-10:
14641464
P = 0
14651465
else:
1466-
P = - Z/pos * (1. - m.exp(-lam * pos)) / (1 + m.exp(-lam * (pos - rc)));
1466+
P = - Z/pos * (1. - m.exp(-lam * pos)) / (1 + m.exp(-lam * (pos - rc)))
14671467

14681468
return P
14691469

@@ -1521,7 +1521,7 @@ def PseudoGe(pos):
15211521
ax = fig.add_subplot(1, 1, 1, projection='3d')
15221522
ax.plot_surface(xs, ys, toplot, cmap='viridis', edgecolor='none')
15231523
ax.title.set_text('Real Space')
1524-
plt.tight_layout
1524+
plt.tight_layout()
15251525
plt.show()
15261526

15271527

@@ -1538,7 +1538,7 @@ def PseudoGe(pos):
15381538
W, Elist = sd(W, 150, False)
15391539
W = orthogonalize(W)
15401540

1541-
W, Elist = pccg(W,100,1, False)
1541+
W, Elist = pccg(W, 100, 1, False)
15421542

15431543

15441544
# In[106]:
@@ -1726,7 +1726,7 @@ def getgrad(W):
17261726
Vdual = cJ(Vps * Sf)
17271727

17281728

1729-
# In[125]:
1729+
# In[117]:
17301730

17311731

17321732
np.random.seed(100)
@@ -1746,7 +1746,7 @@ def getgrad(W):
17461746
#W, Elist = pccg(W, 10, 1, True)
17471747

17481748

1749-
# In[126]:
1749+
# In[118]:
17501750

17511751

17521752
Psi, epsilon = getPsi(W)
@@ -1758,5 +1758,4 @@ def getgrad(W):
17581758
Etot = E + Ewald
17591759
print('\nTotal energy:', Etot)
17601760
print('Electronic energy:', E)
1761-
print('Energy dif beteen s and p orbitals:', epsilon[1] - epsilon[0], 'Expected (from NIST data): 0.276641')
1762-
1761+
print('Energy dif beteen s and p orbitals:', epsilon[1] - epsilon[0], 'Expected (from NIST data): 0.276641')

hartree-fock.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -526,11 +526,4 @@ def TwoElectronTwoCenter(alpha, beta, gamma, delta, Ra, Rb, Rc, Rd):
526526
# In[41]:
527527

528528

529-
print(C)
530-
531-
532-
# In[ ]:
533-
534-
535-
536-
529+
print(C)

0 commit comments

Comments
 (0)