Skip to content

Commit 22d5cb0

Browse files
committed
gh-486: run individual iternorms on ell blocks
1 parent ade5e60 commit 22d5cb0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

glass/fields.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -400,27 +400,42 @@ def _generate_grf(
400400
msg = "all gls are empty"
401401
raise ValueError(msg)
402402

403-
# generates the covariance matrix for the iterative sampler
404-
cov = cls2cov(gls, n, ngrf, ncorr)
405-
406403
# working arrays for the iterative sampling
407404
z = np.zeros(n * (n + 1) // 2, dtype=np.complex128)
408405
y = np.zeros((n * (n + 1) // 2, ncorr), dtype=np.complex128)
409406

407+
blocks = []
408+
block_ns = []
409+
for j in range(len(gls)):
410+
block = [gls[i][j : j + 1] for i in range(j, len(gls))]
411+
blocks.append(block)
412+
block_ns.append(len(gls) - j)
413+
410414
# generate the conditional normal distribution for iterative sampling
411-
conditional_dist = iternorm(ncorr, cov, size=n)
415+
conditional_dists = []
416+
for block, block_n in zip(blocks, block_ns, strict=False):
417+
# generate the covariance matrix of this block for the iterative sampler
418+
block_cov = cls2cov(block, block_n, ngrf, ncorr)
419+
# generate the conditional normal distribution for iterative sampling
420+
conditional_dist = iternorm(ncorr, block_cov, size=block_n)
421+
# store for parallel processing of all blocks
422+
conditional_dists.append(conditional_dist)
412423

413424
# sample the fields from the conditional distribution
414-
for j, a, s in conditional_dist:
425+
for results in zip(*conditional_dists, strict=False):
415426
# standard normal random variates for alm
416427
# sample real and imaginary parts, then view as complex number
417428
rng.standard_normal(n * (n + 1), np.float64, z.view(np.float64))
418429

430+
# concatenate individual updates into one update
431+
s = np.concatenate([block_s for _, _, block_s in results])
432+
419433
# scale by standard deviation of the conditional distribution
420434
# variance is distributed over real and imaginary part
421435
alm = _multalm(z, s)
422436

423437
# add the mean of the conditional distribution
438+
a = np.concatenate([block_a for _, block_a, _ in results])
424439
for i in range(ncorr):
425440
alm += _multalm(y[:, i], a[:, i])
426441

0 commit comments

Comments
 (0)