-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiCISCF.py
591 lines (488 loc) · 19.5 KB
/
iCISCF.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# coding=UTF-8
import pyscf
import os
import sys
import numpy
import struct
from pyscf import tools
import copy
from pyscf.lib import logger
from pyscf import lib
from pyscf import tools
from pyscf import ao2mo
from pyscf import mcscf, fci
import iCI_InputFile_Generator
# Constant/Configuration
iCI_ProgramName = "ICI_CPP"
FILE_RDM1_NAME = "rdm1.csv"
FILE_RDM2_NAME = "rdm2.csv"
# default configuration
CONFIG = {
"inputfile": "iCI.inp",
"rotatemo": 0,
"state": [[0, 0, 1, [1]]], # spintwo,irrep,nstates,weight
"cmin": 1e-4,
"perturbation": 0,
"dumprdm": 0,
"relative": 0,
"inputocfg": 0,
"etol": 1e-6,
}
# Other Util
def writeIntegralFile(iciobj, h1eff, eri_cas, ncas, nelec, ecore=0):
if isinstance(nelec, (int, numpy.integer)):
nelecb = nelec // 2
neleca = nelec - nelecb
else:
neleca, nelecb = nelec
if iciobj.groupname is not None and iciobj.orbsym is not []:
# First removing the symmetry forbidden integrals. This has been done using
# the pyscf internal irrep-IDs (stored in iciobj.orbsym)
orbsym = numpy.asarray(iciobj.orbsym) % 10
pair_irrep = (orbsym.reshape(-1, 1) ^ orbsym)[numpy.tril_indices(ncas)]
sym_forbid = pair_irrep.reshape(-1, 1) != pair_irrep.ravel()
eri_cas = ao2mo.restore(4, eri_cas, ncas)
eri_cas[sym_forbid] = 0
eri_cas = ao2mo.restore(8, eri_cas, ncas)
# Convert the pyscf internal irrep-ID to molpro irrep-ID, why ?
# orbsym = numpy.asarray(
# symmetry.convert_orbsym(iciobj.groupname, orbsym))
else:
orbsym = []
eri_cas = ao2mo.restore(8, eri_cas, ncas)
if not os.path.exists(iciobj.runtimedir):
os.makedirs(iciobj.runtimedir)
# The name of the FCIDUMP file, default is "FCIDUMP".
integralFile = os.path.join(iciobj.runtimedir, iciobj.integralfile)
tools.fcidump.from_integrals(integralFile, h1eff, eri_cas, ncas,
neleca+nelecb, ecore, ms=abs(neleca-nelecb),
orbsym=orbsym,
)
return integralFile
def execute_iCI(iciobj):
iciobj.inputfile = iciobj.config["taskname"] + \
"_" + str(iciobj.runtime) + ".inp"
iciobj.outputfile = iciobj.config["taskname"] + \
"_" + str(iciobj.runtime) + ".out"
iciobj.energydat = iciobj.inputfile + ".enedat"
iciobj.runtime += 1
# Write input file
# consider aimed selection
iCI_InputFile_Generator._Generate_InputFile_iCI(inputfilename=iciobj.inputfile,
Segment=iciobj.config["segment"],
nelec_val=iciobj.config["nvalelec"],
rotatemo=iciobj.config["rotatemo"],
cmin=iciobj.config["cmin"],
perturbation=iciobj.config["perturbation"],
dumprdm=iciobj.config["dumprdm"],
relative=iciobj.config["relative"],
Task=iciobj.config["task"],
inputocfg=iciobj.config["inputocfg"],
etol=iciobj.config["etol"],
selection=iciobj.config["selection"],
direct=iciobj.config["direct"]
)
# execute
os.system("%s %s > %s" %
(iciobj.executable, iciobj.inputfile, iciobj.outputfile))
return iciobj
def read_energy(iciobj):
calc_e = []
# print("open file %s" % (iciobj.energydat))
binfile = open(iciobj.energydat, "rb")
# size = os.path.getsize(binfile)
# if size != iciobj.nroots * 8:
# print("Fatal error in read_energy()")
# exit(1)
# 读取能量
for i in range(iciobj.nroots):
data = binfile.read(8)
calc_e.append(float(struct.unpack('d', data)[0]))
# print(struct.unpack('d', data))
return calc_e
class iCI(lib.StreamObject):
r'''iCI program interface and object to hold iCI program input
parameters.
See also the reference:
(1) https://pubs.acs.org/doi/10.1021/acs.jctc.9b01200
(2) https://pubs.acs.org/doi/10.1021/acs.jctc.0c01187
Attributes:
(1) all the subroutine do in a state-averaged way, never do in a state-specific way!
(2)
Examples:
'''
def __init__(self,
mo_coeff=None,
cmin=1e-4,
tol=1e-8,
inputocfg=0,
mol=None, state=None):
self.mol = mol
if mol is None:
self.stdout = sys.stdout
self.verbose = logger.NOTE
else:
self.stdout = mol.stdout # useless for iCI
self.verbose = mol.verbose
self.executable = os.getenv(iCI_ProgramName)
self.runtimedir = '.'
self.integralfile = "FCIDUMP"
self.config = copy.deepcopy(CONFIG)
# set config
self.config["segment"] = None
# self.config["nvalelec"] = nvalelec
# if segment == None or nvalelec == None:
# print("Fatal Error in initializing objects of iCI")
# exit(1)
self.config["cmin"] = str(cmin)
self.config["etol"] = tol
self.config["rotatemo"] = 0 # 永远都不旋转轨道
# self.config["perturbation"] = perturbation # 基本不会做 perturbation
self.config["perturbation"] = 0 # 基本不会做 perturbation
# self.config["dumprdm"] = dumprdm
self.config["dumprdm"] = 2 # 无论如何都计算密度矩阵
self.config["relative"] = 0 # 无论如何都不考虑相对论
self.config["inputocfg"] = inputocfg
self.config["hasfzc"] = False
self.config["nfzc"] = 0
self.config["readinocfg"] = None
if state is not None:
self.config["state"] = state
self.config["task"], self.spin, self.weight = iCI_InputFile_Generator._generate_task_spinarray_weight(
state)
# print(self.weight)
# self.weights = self.weight
self.config["selection"] = 1
self.config["aimedtarget"] = 0.0
self.config["taskname"] = "iCI"
self.config["direct"] = 0
if mol is not None and mol.symmetry:
self.groupname = mol.groupname
else:
self.groupname = None
if mol is not None and mol.nao == mo_coeff.shape[0] and mol.symmetry:
OrbSym = pyscf.symm.label_orb_symm(mol, mol.irrep_name, mol.symm_orb,
mo_coeff)
IrrepOrb = []
for i in range(len(OrbSym)):
IrrepOrb.append(pyscf.symm.irrep_name2id(
mol.groupname, OrbSym[i]))
self.orbsymtot = IrrepOrb
else:
self.orbsymtot = []
self.orbsym = []
self.conv_tol = tol
self.nroots = len(self.weight)
self.restart = False
# self.spin = None
self.runtime = 0
self.fixocfg_iter = -1
self.inputfile = ""
self.outputfile = ""
self.energydat = ""
self.converged = True # 默认全部的根都收敛了
##################################################
# DO NOT CHANGE these parameters, unless you know the code in details
self.orbsym = []
self._keys = set(self.__dict__.keys()) # For what ?
def dump_flags(self, verbose=None):
log = logger.new_logger(self, verbose)
log.info('')
log.info('******** iCI flags ********')
log.info('executable = %s', self.executable)
log.info('runtimedir = %s', self.runtimedir)
# log.debug1('config = %s', self.config)
log.info('')
return self
def make_rdm1(self, state, norb, nelec, **kwargs): # always return state averaged one
dm_file = os.path.join(self.runtimedir, FILE_RDM1_NAME)
if not os.path.isfile(dm_file):
print("Fatal error in reading rdm1")
exit(1)
i, j, val = numpy.loadtxt(dm_file, dtype=numpy.dtype('i,i,d'),
delimiter=',', skiprows=1, unpack=True)
rdm1 = numpy.zeros((norb, norb))
rdm1[i, j] = rdm1[j, i] = val
nfzc = self.config["nfzc"]
for j in range(nfzc):
rdm1[j, j] = 2.0
return rdm1
def make_rdm12(self, state, norb, nelec, **kwargs): # always return state averaged one
rdm1 = self.make_rdm1(state, norb, nelec, kwargs=kwargs)
dm_file = os.path.join(self.runtimedir, FILE_RDM2_NAME)
nfzc = self.config["nfzc"]
if not os.path.isfile(dm_file):
print("Fatal error in reading rdm2")
exit(1)
i, j, k, l, val = numpy.loadtxt(dm_file, dtype=numpy.dtype('i,i,i,i,d'),
delimiter=',', skiprows=1, unpack=True)
rdm2 = numpy.zeros((norb, norb, norb, norb))
rdm2[i, j, k, l] = rdm2[j, i, l, k] = val
rdm2 = rdm2.transpose(0, 3, 1, 2) # p^+ q r^+ s
# 补全 nfzc
for j in range(nfzc):
rdm2[j, j, j, j] = 2.0
for j in range(nfzc):
for i in range(nfzc):
if i == j:
continue
rdm2[i, i, j, j] = 4.0
rdm2[i, j, j, i] = -2.0
norb = rdm2.shape[0]
for i in range(nfzc):
for p in range(nfzc, norb):
for q in range(nfzc, norb):
rdm2[i, i, p, q] = rdm1[p, q]*2.0
rdm2[p, q, i, i] = rdm1[p, q]*2.0
rdm2[p, q, i, i] = rdm1[p, q]*2.0
rdm2[i, p, q, i] = rdm1[p, q]*-1.0
rdm2[p, i, i, q] = rdm1[p, q]*-1.0
return rdm1, rdm2
def set_config(self, _key, _value):
self.config[_key] = _value
def kernel(self, h1e, eri, norb, nelec, ci0=None, ecore=0, restart=None,
**kwargs): # Driver
# judge whether to restart
if (self.runtime > 0):
self.restart = True
# print(self.restart, restart, self.runtime)
if restart is None:
restart = self.restart
if restart == True and self.runtime == 0:
print("Haven't run iCI fatal error since restart = true!")
exit(1)
if self.fixocfg_iter > 0:
if self.runtime > self.fixocfg_iter:
self.config["selection"] = 0
print(self.runtime, self.fixocfg_iter)
if restart == True:
if 'approx' in kwargs and kwargs['approx'] is True:
self.config["inputocfg"] = 3 # iCI is changed!
# self.config["inputocfg"] = 2
else:
# self.config["inputocfg"] = 1
if self.config["readinocfg"] != True:
self.config["inputocfg"] = 0 # iCI is changed
else:
self.config["inputocfg"] = 2
else:
if self.config["readinocfg"] != True:
self.config["inputocfg"] = 0 # iCI is changed
else:
self.config["inputocfg"] = 2
if self.config["selection"] == 0:
if self.config["readinocfg"] == True:
self.config["inputocfg"] = 2
else:
self.config["inputocfg"] = 3
if 'orbsym' in kwargs:
self.orbsym = kwargs['orbsym']
# wright integral files
# generate nsegment and nvalelec
nelectrons = 0
if isinstance(nelec, (int, numpy.integer)):
nelectrons = nelec
else:
nelectrons = nelec[0] + nelec[1]
if "nvalelec" not in self.config.keys():
if (nelectrons <= 10):
self.config["nvalelec"] = nelectrons
else:
if nelectrons % 2 == 0:
self.config["nvalelec"] = 10
else:
self.config["nvalelec"] = 9
nval = min(self.config["nvalelec"], norb)
if norb <= 8:
nval = norb
nval_hole = nval//2
nval_part = nval - nval_hole
ncore = (nelectrons - self.config["nvalelec"])//2
nvir = norb - nval - ncore
if nvir < 0:
nval = norb - ncore
nvir = 0
nval_hole = nval//2
nval_part = nval - nval_hole
if self.config["segment"] == None:
self.config["segment"] = "0 " + \
str(ncore) + " "+str(nval_hole)+" " + \
str(nval_part)+" "+str(nvir) + " 0"
if self.orbsymtot is not []:
nelectron_fzc = self.mol.nelectron - nelectrons
nfzc = nelectron_fzc//2
self.orbsym = self.orbsymtot[nfzc:nfzc+norb]
# print(self.orbsym)
writeIntegralFile(self, h1e, eri, norb, nelec, ecore)
if 'tol' in kwargs:
self.config['tol'] = kwargs['tol']
# execute iCI
self = execute_iCI(self)
# read energy
calc_e = read_energy(self)
res_calc_e = 0.0
for i in range(self.nroots):
res_calc_e += calc_e[i]*self.weight[i]
print("State %3d energy %20.12f" % (i, calc_e[i]))
roots = list(range((self.nroots)))
# return calc_e, roots
return res_calc_e, 0
def kernel_second_orbopt(self, h1e, eri, norb, nelec, ci0=None, ecore=0, restart=None,
**kwargs): # Driver
# judge whether to restart
# if (self.runtime > 0):
# self.restart = True
# print(self.restart, restart, self.runtime)
# if restart is None:
# restart = self.restart
if restart == True and self.runtime == 0:
print("Haven't run iCI fatal error since restart = true!")
exit(1)
# if restart == True:
# if 'approx' in kwargs and kwargs['approx'] is True:
# self.config["inputocfg"] = 2
# else:
# self.config["inputocfg"] = 1
# else:
self.config["inputocfg"] = 0
self.config["rotatemo"] = 2
if 'orbsym' in kwargs:
self.orbsym = kwargs['orbsym']
# wright integral files
# generate nsegment and nvalelec
nelectrons = 0
if isinstance(nelec, (int, numpy.integer)):
nelectrons = nelec
else:
nelectrons = nelec[0] + nelec[1]
if "nvalelec" not in self.config.keys():
if (nelectrons <= 10):
self.config["nvalelec"] = nelectrons
else:
if nelectrons % 2 == 0:
self.config["nvalelec"] = 10
else:
self.config["nvalelec"] = 9
nval = min(self.config["nvalelec"], norb)
nval_hole = nval//2
nval_part = nval - nval_hole
ncore = (nelectrons - self.config["nvalelec"])//2
nvir = norb - nval - ncore
nelectron_fzc = self.mol.nelectron - nelectrons
nfzc = nelectron_fzc//2
if self.config["segment"] == None:
self.config["segment"] = str(nfzc) + " " + \
str(ncore) + " "+str(nval_hole)+" " + \
str(nval_part)+" "+str(nvir) + " 0"
if self.orbsymtot is not []:
# nelectron_fzc = self.mol.nelectron - nelectrons
# nfzc = nelectron_fzc//2
# self.orbsym = self.orbsymtot[nfzc:nfzc+norb]
self.orbsym = self.orbsymtot[0:nfzc+norb]
# print(self.orbsym)
writeIntegralFile(self, h1e, eri, nfzc+norb, self.mol.nelectron, ecore)
if 'tol' in kwargs:
self.config['tol'] = kwargs['tol']
# execute iCI
self = execute_iCI(self)
# read energy
calc_e = read_energy(self)
res_calc_e = 0.0
for i in range(self.nroots):
res_calc_e += calc_e[i]*self.weight[i]
print("State %3d energy %20.12f" % (i, calc_e[i]))
roots = list(range((self.nroots)))
# return calc_e, roots
return res_calc_e, 0
def approx_kernel(self, h1e, eri, norb, nelec, ci0=None, ecore=0,
restart=None, **kwargs): # 近似求解 iCI
# self.config["etol"] = self.conv_tol * 1e3
# the same as kernel
return self.kernel(h1e, eri, norb, nelec, ci0, ecore, restart, kwargs=kwargs, approx=True)
def spin_square(self, civec, norb, nelec):
state_id = civec
spintwo = self.spin[state_id]
s = float(spintwo)/2
ss = s*s
return ss, s*2+1
def contract_2e(self, eri, civec, norb, nelec, client=None, **kwargs): # Calculate Hc
return None
def iCISCF(mf, norb, nelec, tol=1.e-8, cmin=1e-4, state=[[0, 0, 1]], *args, **kwargs):
'''Shortcut function to setup CASSCF using the iCI solver. The iCI
solver is properly initialized in this function so that the 1-step
algorithm can be applied with iCI-CASSCF.
NOTE: it is not the iCISCF in BDF, it is just a MCSCF with iCI solver.
Examples:
'''
mc = mcscf.CASSCF(mf, norb, nelec, *args, **kwargs)
mc.fcisolver = iCI(mol=mf.mol, tol=tol,
mo_coeff=mf.mo_coeff, state=state, cmin=cmin)
# mc.fcisolver.config['get_1rdm_csv'] = True
# mc.fcisolver.config['get_2rdm_csv'] = True
# mc.fcisolver.config['var_only'] = True
# mc.fcisolver.config['s2'] = True
return mc
if __name__ == "__main__":
from pyscf import gto, scf
# Initialize C2 molecule
b = 1.24253
mol = gto.Mole()
mol.build(
verbose=4,
output=None,
atom=[
['C', (0.000000, 0.000000, -b/2)],
['C', (0.000000, 0.000000, b/2)], ],
basis={'C': 'ccpvdz', },
symmetry='d2h',
)
# Create HF molecule
mf = scf.RHF(mol).run()
# print(mf.mo_coeff)
# Number of orbital and electrons
norb = 8
nelec = 8
dimer_atom = 'C'
mciCI = mcscf.CASCI(mf, norb, nelec)
mciCI.fcisolver = iCI(mol=mol, cmin=0.0, state=[
[0, 0, 1]], tol=1e-12, mo_coeff=mf.mo_coeff)
mciCI.kernel()
dm1, dm2 = mciCI.fcisolver.make_rdm12(0, norb, nelec)
mc1 = mcscf.CASCI(mf, norb, nelec)
mc1.kernel(mciCI.mo_coeff)
dm1ref, dm2ref = mc1.fcisolver.make_rdm12(mc1.ci, norb, nelec)
print(abs(dm1ref-dm1).max())
print(abs(dm2ref-dm2).max())
# print(dm1ref)
# print(dm2ref)
# exit()
# mch = shci.SHCISCF(mf, norb, nelec)
# mch.internal_rotation = True
# mch.kernel()
mc2step = mcscf.CASSCF(mf, norb, nelec)
solver1 = fci.direct_spin1_symm.FCI(mol)
solver1.wfnsym = 'ag'
solver1.nroots = 3
solver1.spin = 0
# mc.fcisolver = solver1
# mc2step.fcisolver =
mc2step = mcscf.state_average_mix_(mc2step, [solver1], (0.5, 0.25, 0.25))
# mc2step.mc2step()
mc2step.mc1step()
# mc2step.kernel()
mymc2step = mcscf.CASSCF(mf, norb, nelec)
mymc2step.fcisolver = iCI(mol=mol, cmin=0.0, state=[
[0, 0, 3, [2, 1, 1]]], tol=1e-12, mo_coeff=mf.mo_coeff)
# mymc2step.mc2step()
mymc2step.mc1step()
norb = 10
nelec = 12
mymc2step = mcscf.CASSCF(mf, norb, nelec)
mymc2step.fcisolver = iCI(mol=mol, cmin=0.0, state=[
[0, 0, 3, [2, 1, 1]]], tol=1e-12, mo_coeff=mf.mo_coeff)
mymc2step.fcisolver.config["segment"] = "2 0 4 4 0 0"
mymc2step.fcisolver.config["selection"] = 1
mymc2step.fcisolver.config["nvalelec"] = 8
mymc2step.fcisolver.config["nfzc"] = 2
# mymc2step.mc2step()
mymc2step.mc1step()