-
Notifications
You must be signed in to change notification settings - Fork 3
/
pymodule.py
187 lines (144 loc) · 6.43 KB
/
pymodule.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
#
# @BEGIN LICENSE
#
# psixas by Psi4 Developer, a plugin to:
#
# Psi4: an open-source quantum chemistry software package
#
# Copyright (c) 2007-2017 The Psi4 Developers.
#
# The copyrights for code used from other parties are included in
# the corresponding files.
#
# This file is part of Psi4.
#
# Psi4 is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, version 3.
#
# Psi4 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with Psi4; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# @END LICENSE
#
import sys,os
import numpy as np
import psi4
import psi4.driver.p4util as p4util
from psixas.src.helper.kshelper import MoldenWriter
from psi4.driver.procrouting import proc_util
from psixas.src.dft.ksgs import DFTGroundState
from psixas.src.dft.ksex import DFTExcitedState
from psixas.src.helper.spec import CalcSpec
from psixas.src.helper.kshelper import printHeader
import logging
logging.basicConfig(filename='additional.log',level=logging.CRITICAL,filemode='w', format='%(name)s -%(levelname)s - %(message)s')
def run_psixas(name, **kwargs):
r"""Function encoding sequence of PSI module and plugin calls so that
psixas can be called via :py:func:`~driver.energy`. For post-scf plugins.
>>> energy('psixas')
"""
lowername = name.lower()
kwargs = p4util.kwargs_lower(kwargs)
#print the banner
printBanner()
mol = kwargs["molecule"]
func = kwargs["functional"]
tmp = psi4.core.get_local_option("PSIXAS","MODE")
if (psi4.core.has_local_option_changed("SCF","SCF_TYPE")==False):
psi4.core.set_local_option("SCF","SCF_TYPE","MEM_DF")
mode = tmp.split("+")
if not(all([x in ["GS","LOC","EX","SPEC"] for x in mode])):
raise Exception("Wrong mode, possible values are GS, LOC, EX, SPEC.")
if "GS" in mode:
DFTGroundState(mol,func,PREFIX=psi4.core.get_local_option("PSIXAS","PREFIX"))
if "LOC" in mode:
loc_sub = np.array(psi4.core.get_local_option("PSIXAS","LOC_SUB"),dtype=np.int32)
printHeader("Entering Localization")
psi4.core.be_quiet()
wfn = psi4.core.Wavefunction.build(mol,psi4.core.get_global_option('BASIS'))
nbf = wfn.nso()
sup = psi4.driver.dft.build_superfunctional(func, False)[0]
sup.set_deriv(2)
sup.allocate()
uhf = psi4.core.UHF(wfn,sup)
orbitalFileName = None
prefix = psi4.core.get_local_option("PSIXAS","PREFIX")
psi4.core.reopen_outfile()
if os.path.exists(prefix+"_exorbs.npz"):
psi4.core.print_out("\nUsing excited orbital file! \n\n")
Ca = np.load(prefix+"_exorbs.npz")["Ca"]
Cb = np.load(prefix+"_exorbs.npz")["Cb"]
occa = np.load(prefix+"_exorbs.npz")["occa"]
occb = np.load(prefix+"_exorbs.npz")["occb"]
epsa = np.load(prefix+"_exorbs.npz")["epsa"]
epsb = np.load(prefix+"_exorbs.npz")["epsb"]
orbitalFileName = prefix+"_exorbs.npz"
else:
if os.path.exists(prefix+"_gsorbs.npz"):
psi4.core.print_out("\nUsing ground state orbitals!\n\n")
Ca = np.load(prefix+"_gsorbs.npz")["Ca"]
Cb = np.load(prefix+"_gsorbs.npz")["Cb"]
occa = np.load(prefix+"_gsorbs.npz")["occa"]
occb = np.load(prefix+"_gsorbs.npz")["occb"]
epsa = np.load(prefix+"_gsorbs.npz")["epsa"]
epsb = np.load(prefix+"_gsorbs.npz")["epsb"]
orbitalFileName = prefix+"_gsorbs.npz"
else:
Exception("Cannot Localize, there are no orbitals!")
psi4.core.be_quiet()
locCa = psi4.core.Matrix(wfn.nso(),len(loc_sub))
locCb = psi4.core.Matrix(wfn.nso(),len(loc_sub))
locCa.np[:] = np.copy(Ca[:,loc_sub])
locCb.np[:] = np.copy(Cb[:,loc_sub])
LocalA = psi4.core.Localizer.build("PIPEK_MEZEY", wfn.basisset(), locCa )
LocalB = psi4.core.Localizer.build("PIPEK_MEZEY", wfn.basisset(), locCb )
psi4.core.reopen_outfile()
LocalA.localize()
LocalB.localize()
Ca[:,loc_sub] = LocalA.L
Cb[:,loc_sub] = LocalB.L
np.savez(orbitalFileName,Ca=Ca,Cb=Cb,occa=occa,occb=occb)
psi4.core.print_out(f"Localized Orbitals written as {orbitalFileName}\n")
OCCA = psi4.core.Vector(nbf)
OCCB = psi4.core.Vector(nbf)
OCCA.np[:] = occa
OCCB.np[:] = occb
MoldenWriter(prefix+'_loc.molden',wfn,Ca,Cb,epsa,epsb,occa,occb)
psi4.core.print_out("Moldenfile written\n")
orbitals = []
if ("EX" in mode):
orbs = psi4.core.get_local_option("PSIXAS","ORBS")
occs = psi4.core.get_local_option("PSIXAS","OCCS")
freeze = psi4.core.get_local_option("PSIXAS","FREEZE")
spin = psi4.core.get_local_option("PSIXAS","SPIN")
ovl = psi4.core.get_local_option("PSIXAS","OVL")
lens = [len(x) for x in [orbs,occs,freeze,spin,ovl]]
if len(list((set(lens))))>1:
raise Exception("Input arrays have inconsistent length"+" ".join(str(lens)))
for i in range(len(orbs)):
orbitals.append({"orb" : orbs[i],"spin": spin[i].lower(),"occ" : occs[i], "frz" : freeze[i]=="T","DoOvl":ovl[i] == "T" })
DFTExcitedState(mol,func,orbitals)
if ("SPEC" in mode):
CalcSpec(mol,func)
#psixas_wfn = psi4.core.plugin('psixas.so', wfn)
return 0 #psixas_wfn
# Integration with driver routines
psi4.driver.procedures['energy']['psixas'] = run_psixas
def printBanner():
Banner = '''
___ ___ ___ __ __ ___ ___
| _ \ / __| |_ _| \ \/ / / \ / __|
| _/ \__ \ | | > < | - | \__ \
_|_|_ |___/ |___| /_/\_\ |_|_| |___/
_| """ | _|"""""| _|"""""| _|"""""| _|"""""| _|"""""|
"`-0-0-' "`-0-0-' "`-0-0-' "`-0-0-' "`-0-0-' "`-0-0-'
An X-Ray Absorption Plugin for PSI4
'''
psi4.core.print_out(Banner)