Skip to content

Commit d325c99

Browse files
committed
restructure magnetic calculation to simplify memory handling
1 parent a7ca9bb commit d325c99

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

refl1d/experiment.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@ def update(self):
110110

111111
def residuals(self):
112112
if 'residuals' not in self._cache:
113+
# Trigger reflectivity calculation even if there is no data to
114+
# compare against so that we can profile simulation code, and
115+
# so that simulation smoke tests are run more thoroughly.
116+
QR = self.reflectivity()
113117
if ((self.probe.polarized
114118
and all(x is None or x.R is None for x in self.probe.xs))
115119
or (not self.probe.polarized and self.probe.R is None)):
116120
resid = np.zeros(0)
117121
else:
118-
QR = self.reflectivity()
119122
if self.probe.polarized:
120123
resid = np.hstack([(xs.R - QRi[1])/xs.dR
121124
for xs, QRi in zip(self.probe.xs, QR)

refl1d/reflectivity.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,10 @@ def magnetic_amplitude(kz,
226226

227227
sld_b, u1, u3 = calculate_u1_u3(H, rhoM, thetaM, Aguide)
228228

229-
R1, R2, R3, R4 = [np.empty(kz.shape, 'D') for pol in (1, 2, 3, 4)]
230-
# reflmodule._magnetic_amplitude(depth, sigma, rho, irho,
231-
# sld_b, u1, u3, Aguide, kz, rho_index,
232-
# R1, R2, R3, R4)
233-
234-
magnetic_amplitude_py(depth, sigma, rho, irho,
235-
sld_b, u1, u3, kz, rho_index,
236-
R1, R2, R3, R4)
237-
return R1, R2, R3, R4
229+
# Note 2021-08-01: return Rpp, Rpm, Rmp, Rmm are no longer contiguous.
230+
R = np.empty((kz.size, 4), 'D')
231+
magnetic_amplitude_py(depth, sigma, rho, irho, sld_b, u1, u3, kz, R)
232+
return R[:, 0], R[:, 1], R[:, 2], R[:, 3]
238233

239234

240235
def calculate_u1_u3(H, rhoM, thetaM, Aguide):
@@ -553,17 +548,17 @@ def Cr4xa(N, D, SIGMA, IP, RHO, IRHO, RHOM, U1, U3, KZ, Y):
553548

554549

555550
# Calculate reflectivity coefficients specified by POLSTAT
556-
Y[0] = (B24*B41 - B21*B44)/DETW # ++
557-
Y[1] = (B21*B42 - B41*B22)/DETW # +-
551+
# IP = +1 fills in ++, +-, -+, --; IP = -1 only fills in -+, --.
552+
if IP > 0:
553+
Y[0] = (B24*B41 - B21*B44)/DETW # ++
554+
Y[1] = (B21*B42 - B41*B22)/DETW # +-
558555
Y[2] = (B24*B43 - B23*B44)/DETW # -+
559556
Y[3] = (B23*B42 - B43*B22)/DETW # --
560557

561558
#@cc.export('mag_amplitude')
562-
MAGAMP_SIG = 'void(f8[:], f8[:], f8[:], f8[:], f8[:], c16[:], c16[:], f8[:], i4[:], c16[:], c16[:], c16[:], c16[:])'
559+
MAGAMP_SIG = 'void(f8[:], f8[:], f8[:], f8[:], f8[:], c16[:], c16[:], f8[:], c16[:,:])'
563560
@njit(MAGAMP_SIG, parallel=True, cache=True)
564-
def magnetic_amplitude_py(d, sigma, rho, irho,
565-
rhoM, u1, u3, KZ, rho_index,
566-
Ra, Rb, Rc, Rd):
561+
def magnetic_amplitude_py(d, sigma, rho, irho, rhoM, u1, u3, KZ, R):
567562
"""
568563
python version of calculation
569564
implicit returns: Ra, Rb, Rc, Rd
@@ -574,22 +569,17 @@ def magnetic_amplitude_py(d, sigma, rho, irho,
574569
if (np.fabs(rhoM[0]) <= MINIMAL_RHO_M and np.fabs(rhoM[layers-1]) <= MINIMAL_RHO_M):
575570
# calculations for I+ and I- are the same in the fronting and backing.
576571
for i in prange(points):
577-
Y = np.empty(4, dtype=np.complex128)
578-
Cr4xa(layers, d, sigma, 1.0, rho, irho, rhoM, u1, u3, KZ[i], Y)
579-
Ra[i], Rb[i], Rc[i], Rd[i] = Y[0], Y[1], Y[2], Y[3]
572+
Cr4xa(layers, d, sigma, 1.0, rho, irho, rhoM, u1, u3, KZ[i], R[i])
580573
else:
581-
# plus polarization
574+
# plus polarization must be before minus polarization because it
575+
# fills in all R++, R+-, R-+, R--, but minus polarization only fills
576+
# in R-+, R--.
582577
for i in prange(points):
583-
Y = np.empty(4, dtype=np.complex128)
584-
Cr4xa(layers, d, sigma, 1.0, rho, irho, rhoM, u1, u3, KZ[i], Y)
585-
Ra[i], Rb[i] = Y[0], Y[1]
578+
Cr4xa(layers, d, sigma, 1.0, rho, irho, rhoM, u1, u3, KZ[i], R[i])
586579

587580
# minus polarization
588581
for i in prange(points):
589-
Y = np.empty(4, dtype=np.complex128)
590-
Cr4xa(layers, d, sigma, -1.0, rho, irho, rhoM, u1, u3, KZ[i], Y)
591-
Rc[i], Rd[i] = Y[2], Y[3]
592-
582+
Cr4xa(layers, d, sigma, -1.0, rho, irho, rhoM, u1, u3, KZ[i], R[i])
593583

594584
#try:
595585
# from .magnetic_amplitude import mag_amplitude as magnetic_amplitude_py

0 commit comments

Comments
 (0)