-
Notifications
You must be signed in to change notification settings - Fork 19
/
FormatIndex.py
265 lines (228 loc) · 9.99 KB
/
FormatIndex.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
#!/usr/bin/env python3
## vi: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
## ---------------------------------------------------------------------
##
## Copyright (C) 2019 by the adcc authors
##
## This file is part of adcc.
##
## adcc is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## adcc 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 General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with adcc. If not, see <http://www.gnu.org/licenses/>.
##
## ---------------------------------------------------------------------
import numpy as np
from libadcc import MoSpaces, ReferenceState
class FormatIndexBase:
def __init__(self, *args, **kwargs):
pass
def optimise_formatting(self, space_index_pairs):
pass
def format(self, space, idx, concat_spin=True):
raise NotImplementedError("Implement the format function.")
class FormatIndexHfProvider(FormatIndexBase):
def __init__(self, refstate, max_digits=1):
"""
Format the given index for the given orbital subspace
looking up it's equivalent index in the HF Provider's
indexing convention. Returns a tuple, namely the formatted
index and the corresponding spin block.
Parameters
----------
refstate : adcc.ReferenceState
ReferenceState to use to get host program information
max_digits : int, optional
Number of digits to reserve for the host index
"""
if not isinstance(refstate, ReferenceState):
raise TypeError(f"Unsupported type: {str(refstate)}")
# TODO some backends such as pyscf have a 1-based indexing
# ... this should be worked in here!
self.base_index = 0
self.mospaces = refstate.mospaces
self.max_digits = max_digits
self.noa = self.mospaces.n_orbs_alpha("f")
def _translate_index(self, space, idx):
ihf = self.mospaces.map_index_hf_provider[space][idx]
if ihf < self.noa:
return self.base_index + ihf, "a"
else:
return self.base_index + ihf - self.noa, "b"
def optimise_formatting(self, space_index_pairs):
"""
Optimise the formatting parameters of this class in order to be able to
nicely produce equivalently formatted tensor format strings for all the
passed spaces-index pairs.
This function can be called multiple times.
"""
log_max_idx = int(np.log(max(self._translate_index(space, idx)[0]
for space, idx in space_index_pairs)))
self.max_digits = max(log_max_idx, self.max_digits, 1)
def format(self, space, idx, concat_spin=True):
"""Format the provided space-index pair and return resulting string"""
fstr = "{:" + str(self.max_digits) + "d}"
tidx, spin = self._translate_index(space, idx)
if concat_spin:
return fstr.format(tidx) + spin
else:
return fstr.format(tidx), spin
@property
def max_n_characters(self):
"""
The maximum number of characters needed for a formatted index (excluding
the "a" or "b" spin string) according to the current optimised formatting.
"""
return self.max_digits
class FormatIndexAdcc(FormatIndexBase):
def __init__(self, refstate_or_mospaces, max_digits=1):
"""
Format the given index for the given orbital subspace
by simply pretty-printing the passed space and index data.
Parameters
----------
refstate_or_mospaces : adcc.ReferenceState or adcc.MoSpaces
ReferenceState to use to get host program information
max_digits : int, optional
Number of digits to reserve for the host index
"""
if isinstance(refstate_or_mospaces, ReferenceState):
self.mospaces = refstate_or_mospaces.mospaces
elif isinstance(refstate_or_mospaces, MoSpaces):
self.mospaces = refstate_or_mospaces
else:
raise TypeError(f"Unsupported type: {str(refstate_or_mospaces)}")
self.max_digits = max_digits
def _translate_index(self, space, idx):
noa = self.mospaces.n_orbs_alpha(space)
if idx < noa:
return space, idx, "a"
else:
return space, idx - noa, "b"
def optimise_formatting(self, space_index_pairs):
"""
Optimise the formatting parameters of this class in order to be able to
nicely produce equivalently formatted tensor format strings for all the
passed spaces-index pairs.
This function can be called multiple times.
"""
maxlen = max(self._translate_index(space, idx)[1]
for space, idx in space_index_pairs)
log_max_idx = int(np.log(max(1, maxlen)))
self.max_digits = max(log_max_idx, self.max_digits, 1)
def format(self, space, idx, concat_spin=True):
"""Format the provided space-index pair and return resulting string"""
fstr = "({:2s} {:" + str(self.max_digits) + "d})"
space, tidx, spin = self._translate_index(space, idx)
if concat_spin:
return fstr.format(space, tidx) + spin
else:
return fstr.format(space, tidx), spin
@property
def max_n_characters(self):
"""
The maximum number of characters needed for a formatted index (excluding
the "a" or "b" spin string) according to the current optimised formatting.
"""
return 5 + self.max_digits
class FormatIndexHomoLumo(FormatIndexBase):
def __init__(self, refstate, max_digits=1, use_hoco=True):
"""
Format the given index for the given orbital subspace
by translating it into a form HOMO-1, LUMO+4 and so on.
The special label HOCO can be optionally used to refer
to the highest occupied orbital of the core space
of CVS calculations.
Parameters
----------
refstate : adcc.ReferenceState
ReferenceState to use to get host program information
max_digits : int, optional
Number of digits to reserve for the HOMO/LUMO/HOCO
offset.
use_hoco : bool, optional
Use the special HOCO indicator to point to the highest-energy
orbital of the CVS core space (True) or treat it relative
to the HOMO as well (False).
"""
if not isinstance(refstate, ReferenceState):
raise TypeError(f"Unsupported type: {str(refstate)}")
self.mospaces = refstate.mospaces
self.maxlen_offset = max_digits + 1 # + 1 for "+"/"-" string
self.n_alpha = refstate.n_alpha
self.n_beta = refstate.n_beta
self.use_hoco = use_hoco
if not self.mospaces.has_core_occupied_space:
self.use_hoco = False
# I do not know the convention or whether this even makes
# sense in the cases where there is no closed-shell or no Aufbau
# TODO Expand this class there is something sensible
closed_shell = refstate.n_alpha == refstate.n_beta
if not refstate.is_aufbau_occupation or not closed_shell:
raise ValueError("format_homolumo only produces the right results "
"for closed-shell references with an Aufbau "
"occupation")
def _translate_index(self, space, idx):
# Deal with core-occupied orbitals first:
if self.use_hoco and space == "o2":
noa = self.mospaces.n_orbs_alpha("o2")
no = self.mospaces.n_orbs("o2")
if idx == noa - 1:
return "HOCO", "", "a"
elif idx == no - 1:
return "HOCO", "", "b"
elif idx < noa:
return "HOCO", "-" + str(noa - 1 - idx), "a"
else:
return "HOCO", "-" + str(no - 1 - idx), "b"
else:
ihf = self.mospaces.map_index_hf_provider[space][idx]
ifull = self.mospaces.map_index_hf_provider["f"].index(ihf)
spin = "a"
noa = self.mospaces.n_orbs_alpha("f")
assert noa == self.mospaces.n_orbs_beta("f")
if ifull >= noa:
spin = "b"
ifull = ifull - noa
ihomo = self.n_alpha - 1
ilumo = ihomo + 1
if ifull < ihomo:
return "HOMO", "-" + str(ihomo - ifull), spin
elif ifull == ihomo:
return "HOMO", "", spin
elif ifull == ilumo:
return "LUMO", "", spin
elif ifull > ilumo:
return "LUMO", "+" + str(ifull - ilumo), spin
def optimise_formatting(self, space_index_pairs):
"""
Optimise the formatting parameters of this class in order to be able to
nicely produce equivalently formatted tensor format strings for all the
passed spaces-index pairs.
This function can be called multiple times.
"""
maxlen = max(len(self._translate_index(space, idx)[1])
for space, idx in space_index_pairs)
self.maxlen_offset = max(maxlen, self.maxlen_offset, 2)
def format(self, space, idx, concat_spin=True):
word, offset, spin = self._translate_index(space, idx)
fmt = "{:s}{:>" + str(self.maxlen_offset) + "s}"
if concat_spin:
return fmt.format(word, offset) + spin
else:
return fmt.format(word, offset), spin
@property
def max_n_characters(self):
"""
The maximum number of characters needed for a formatted index (excluding
the "a" or "b" spin string) according to the current optimised formatting.
"""
return 4 + self.maxlen_offset