-
Notifications
You must be signed in to change notification settings - Fork 3
/
indices.C
424 lines (320 loc) · 12.2 KB
/
indices.C
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
#include <cassert>
#include "indices.h"
#include "utils.h"
namespace libresponse {
type::indices make_indices_ao(const arma::uvec &nbasis_frgm)
{
const size_t nfrgm = nbasis_frgm.n_elem;
type::indices v;
size_t start, stop;
for (size_t i = 0; i < nfrgm; i++) {
if (i == 0)
start = 0;
else
start = arma::accu(nbasis_frgm.subvec(0, i - 1));
stop = start + nbasis_frgm(i);
v.push_back(range(start, stop));
}
return v;
}
type::pair_indices make_indices_mo_separate(const arma::uvec &nocc_frgm, const arma::uvec &nvirt_frgm)
{
if (nocc_frgm.n_elem != nvirt_frgm.n_elem)
throw std::runtime_error("nocc_frgm.n_elem != nvirt_frgm.n_elem");
const size_t nocc = arma::accu(nocc_frgm);
const size_t nfrgm = nocc_frgm.n_elem;
type::indices v_occ, v_virt;
size_t start_occ, stop_occ, start_virt, stop_virt;
for (size_t i = 0; i < nfrgm; i++) {
if (i == 0) {
start_occ = 0;
start_virt = nocc;
} else {
start_occ = arma::accu(nocc_frgm.subvec(0, i - 1));
start_virt = nocc + arma::accu(nvirt_frgm.subvec(0, i - 1));
}
stop_occ = start_occ + nocc_frgm(i);
stop_virt = start_virt + nvirt_frgm(i);
v_occ.push_back(range(start_occ, stop_occ));
v_virt.push_back(range(start_virt, stop_virt));
}
type::pair_indices p = std::make_pair(v_occ, v_virt);
return p;
}
type::indices make_indices_mo_combined(const arma::uvec &nocc_frgm, const arma::uvec &nvirt_frgm)
{
type::indices v;
const type::pair_indices p = make_indices_mo_separate(nocc_frgm, nvirt_frgm);
const size_t nfrgm = nocc_frgm.n_elem;
for (size_t i = 0; i < nfrgm; i++) {
// force qualified name lookup
// https://stackoverflow.com/a/7376212
v.push_back(::join(p.first[i], p.second[i]));
}
return v;
}
arma::uvec make_indices_mo_restricted(const arma::uvec &nocc_frgm, const arma::uvec &nvirt_frgm)
{
const size_t nocc_tot = arma::accu(nocc_frgm);
const size_t nvirt_tot = arma::accu(nvirt_frgm);
const size_t norb_tot = nocc_tot + nvirt_tot;
const size_t nfrgm = nocc_frgm.n_elem;
const type::pair_indices p = make_indices_mo_separate(nocc_frgm, nvirt_frgm);
type::pairs pairs_all, pairs_good, pairs_bad;
// Collect all possible occ-virt excitation pairs within each
// fragment.
for (size_t f = 0; f < nfrgm; f++) {
for (size_t i = 0; i < p.first[f].n_elem; i++) {
for (size_t a = 0; a < p.second[f].n_elem; a++) {
pairs_good.insert(std::make_pair(p.first[f](i), p.second[f](a)));
}
}
}
// Collect all possible occ-virt excitation pairs for the
// supersystem.
for (size_t i = 0; i < nocc_tot; i++) {
for (size_t a = nocc_tot; a < norb_tot; a++) {
pairs_all.insert(std::make_pair(i, a));
}
}
// All the "bad" (disallowed) pairs are the difference between the
// supersystem and the fragment-localized pairs.
// We do this by iterating over the set of all possible pairs, and
// ones that are not members of the restricted set are added to
// the disallowed set.
type::pairs_iterator it_all;
for (it_all = pairs_all.begin(); it_all != pairs_all.end(); ++it_all) {
if (pairs_good.count(*it_all) == 0)
pairs_bad.insert(*it_all);
}
if ((pairs_good.size() + pairs_bad.size()) != pairs_all.size())
throw std::runtime_error("inconsistent sizes between good + bad and all pairs");
// Now, convert all of the pairs to compound indices.
std::vector<size_t> v_all, v_good, v_bad;
size_t i = 0;
for (it_all = pairs_all.begin(); it_all != pairs_all.end(); ++it_all) {
v_all.push_back(i);
if (pairs_good.count(*it_all) > 0)
v_good.push_back(i);
if (pairs_bad.count(*it_all) > 0)
v_bad.push_back(i);
i++;
}
// std::cout << "pairs_all" << std::endl;
// std::cout << pairs_all << std::endl;
// std::cout << "pairs_good" << std::endl;
// std::cout << pairs_good << std::endl;
// std::cout << "pairs_bad" << std::endl;
// std::cout << pairs_bad << std::endl;
// std::cout << "v_all" << std::endl;
// std::cout << v_all << std::endl;
// std::cout << "v_good" << std::endl;
// std::cout << v_good << std::endl;
// std::cout << "v_bad" << std::endl;
// std::cout << v_bad << std::endl;
return arma::conv_to<arma::uvec>::from(v_good);
}
type::indices make_indices_mo_restricted_local_occ_all_virt(const arma::uvec &nocc_frgm, const arma::uvec &nvirt_frgm)
{
const size_t nocc_tot = arma::accu(nocc_frgm);
const size_t nvirt_tot = arma::accu(nvirt_frgm);
const size_t norb_tot = nocc_tot + nvirt_tot;
const size_t nfrgm = nocc_frgm.n_elem;
const type::pair_indices p = make_indices_mo_separate(nocc_frgm, nvirt_frgm);
// For each fragment, collect pairs corresonding to its occupied
// indices to all virtual indices (spanning all fragments).
std::vector<type::pairs> pairs_per_frgm;
for (size_t f = 0; f < nfrgm; f++) {
type::pairs pairs_frgm;
for (size_t i = 0; i < p.first[f].n_elem; i++) {
for (size_t a = nocc_tot; a < norb_tot; a++) {
pairs_frgm.insert(std::make_pair(p.first[f](i), a));
}
}
pairs_per_frgm.push_back(pairs_frgm);
}
// Collect all possible occ-virt excitation pairs for the
// supersystem.
type::pairs pairs_all;
for (size_t i = 0; i < nocc_tot; i++) {
for (size_t a = nocc_tot; a < norb_tot; a++) {
pairs_all.insert(std::make_pair(i, a));
}
}
// Convert the allowed pairs per fragment into compound indices.
type::indices v_all;
type::pairs_iterator it_all;
for (size_t f = 0; f < nfrgm; f++) {
const type::pairs pairs_frgm = pairs_per_frgm.at(f);
std::vector<size_t> v_frgm;
size_t i = 0;
for (it_all = pairs_all.begin(); it_all != pairs_all.end(); ++it_all) {
if (pairs_frgm.count(*it_all) > 0) {
// std::cout << i << " " << *it_all << std::endl;
v_frgm.push_back(i);
}
i++;
}
v_all.push_back(arma::conv_to<arma::uvec>::from(v_frgm));
}
return v_all;
}
arma::uvec join(const type::indices &idxs)
{
const size_t size = idxs.size();
arma::uvec v;
if (size == 0) {
} else if (size == 1) {
v = idxs[0];
} else {
// for (size_t i = 0; i < size; i++)
// v = ::join(v, idxs[i]);
v = idxs[0];
arma::uvec x;
for (size_t i = 1; i < size; i++) {
const arma::uvec w = idxs[i];
const size_t lw = w.n_elem;
const size_t lv = v.n_elem;
x.set_size(lv + lw);
x.subvec(0, lv - 1) = v;
x.subvec(lv, lv + lw - 1) = w;
v = x;
}
}
return v;
}
void make_masked_mat(arma::mat &mm, const arma::mat &m, const arma::uvec &idxs, double fill_value, bool reduce)
{
if (reduce) {
const size_t dim = idxs.n_elem;
mm.set_size(dim, dim);
mm.fill(fill_value);
mm = m.submat(idxs, idxs);
} else {
mm.set_size(m.n_rows, m.n_cols);
mm.fill(fill_value);
mm.submat(idxs, idxs) = m.submat(idxs, idxs);
}
return;
}
void make_masked_cube(arma::cube &mc, const arma::cube &c, const arma::uvec &idxs, double fill_value, bool reduce)
{
if (reduce) {
const size_t dim = idxs.n_elem;
mc.set_size(dim, dim, c.n_slices);
} else
mc.set_size(c.n_rows, c.n_cols, c.n_slices);
for (size_t ns = 0; ns < c.n_slices; ns++) {
make_masked_mat(mc.slice(ns), c.slice(ns), idxs, fill_value, reduce);
}
return;
}
// fill then copy, rather than copy then fill
void make_masked_mat(arma::mat &mm, const arma::mat &m, const type::indices &idxs, double fill_value, bool reduce)
{
if (idxs.empty())
throw std::runtime_error("idxs.empty()");
const size_t nblocks = idxs.size();
if (reduce) {
const arma::uvec idxs_joined = join(idxs);
const size_t dim = idxs_joined.n_elem;
mm.set_size(dim, dim);
mm.fill(fill_value);
mm = m.submat(idxs_joined, idxs_joined);
} else {
mm.set_size(m.n_rows, m.n_cols);
mm.fill(fill_value);
for (size_t i = 0; i < nblocks; i++) {
// submat takes 2 uvecs and automatically forms the correct
// outer product between them for indexing
mm.submat(idxs[i], idxs[i]) = m.submat(idxs[i], idxs[i]);
}
}
return;
}
// TODO template over index type?
void make_masked_cube(arma::cube &mc, const arma::cube &c, const type::indices &idxs, double fill_value, bool reduce)
{
if (reduce) {
// urgh, my kingdom for a list comprehension
const size_t dim = join(idxs).n_elem;
mc.set_size(dim, dim, c.n_slices);
} else
mc.set_size(c.n_rows, c.n_cols, c.n_slices);
for (size_t ns = 0; ns < c.n_slices; ns++) {
make_masked_mat(mc.slice(ns), c.slice(ns), idxs, fill_value, reduce);
}
return;
}
void make_masked_mat(arma::mat &mm, const arma::mat &m, const type::indices &idxs_rows, const type::indices &idxs_cols, double fill_value, bool reduce)
{
if (idxs_rows.empty() || idxs_cols.empty())
throw std::runtime_error("idxs_rows.empty() || idxs_cols.empty()");
if (reduce) {
const arma::uvec idxs_rows_joined = join(idxs_rows);
const size_t dim_rows = idxs_rows_joined.n_elem;
const arma::uvec idxs_cols_joined = join(idxs_cols);
const size_t dim_cols = idxs_cols_joined.n_elem;
mm.set_size(dim_rows, dim_cols);
mm.fill(fill_value);
mm = m.submat(idxs_rows_joined, idxs_cols_joined);
} else {
mm.set_size(m.n_rows, m.n_cols);
mm.fill(fill_value);
// this is an artificial constraint, there's probably a better way
// of doing this
if (idxs_rows.size() != idxs_cols.size())
throw std::runtime_error("idxs_rows.size() != idxs_cols.size()");
const size_t nblocks = idxs_rows.size();
for (size_t i = 0; i < nblocks; i++) {
// submat takes 2 uvecs and automatically forms the correct
// outer product between them for indexing
mm.submat(idxs_rows[i], idxs_cols[i]) = m.submat(idxs_rows[i], idxs_cols[i]);
}
}
return;
}
void make_masked_cube(arma::cube &mc, const arma::cube &c, const type::indices &idxs_rows, const type::indices &idxs_cols, double fill_value, bool reduce)
{
if (reduce) {
const size_t dim_rows = join(idxs_rows).n_elem;
const size_t dim_cols = join(idxs_cols).n_elem;
mc.set_size(dim_rows, dim_cols, c.n_slices);
} else
mc.set_size(c.n_rows, c.n_cols, c.n_slices);
for (size_t ns = 0; ns < c.n_slices; ns++) {
make_masked_mat(mc.slice(ns), c.slice(ns), idxs_rows, idxs_cols, fill_value, reduce);
}
return;
}
template <typename T>
std::vector<T> set_to_ordered_vector(const std::set<T> &s)
{
std::vector<T> v(s.begin(), s.end());
std::sort(v.begin(), v.end());
return v;
}
template std::vector<size_t> set_to_ordered_vector(const std::set<size_t> &s);
type::pair_arma make_indices_from_mask(const arma::umat &mask, int mask_val_for_return)
{
// blow up to avoid weird casting tricks
if (mask_val_for_return < 0 || mask_val_for_return > 1)
throw std::runtime_error("mask_val_for_return < 0 || mask_val_for_return > 1");
std::set<size_t> sr, sc;
for (size_t i = 0; i < mask.n_rows; i++) {
for (size_t j = 0; j < mask.n_cols; j++) {
if (mask(i, j) == mask_val_for_return) {
sr.insert(i);
sc.insert(j);
}
}
}
// transfer the set contents to vectors, which are then sorted in
// place
const std::vector<size_t> vr = set_to_ordered_vector(sr);
const std::vector<size_t> vc = set_to_ordered_vector(sc);
const arma::uvec ar = arma::conv_to<arma::uvec>::from(vr);
const arma::uvec ac = arma::conv_to<arma::uvec>::from(vc);
return std::make_pair(ar, ac);
}
} // namespace libresponse