PythonicDISORT 1.2__py3-none-any.whl → 1.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  from PythonicDISORT import subroutines
2
+ from PythonicDISORT.subroutines import prepend
2
3
  from PythonicDISORT._assemble_intensity_and_fluxes import _assemble_intensity_and_fluxes
3
4
  import warnings
4
5
 
@@ -208,7 +209,7 @@ def pydisort(
208
209
  b_neg_is_scalar = False
209
210
  b_pos_is_vector = False
210
211
  b_neg_is_vector = False
211
- thickness_arr = np.insert(np.diff(tau_arr), 0, tau_arr[0])
212
+ thickness_arr = prepend(np.diff(tau_arr), NLayers - 1, tau_arr[0])
212
213
  NLeg_all = np.shape(Leg_coeffs_all)[1]
213
214
  N = NQuad // 2
214
215
  there_is_beam_source = I0 > 0
@@ -245,8 +246,8 @@ def pydisort(
245
246
  if not np.all(omega_arr * Leg_coeffs_all[:, 0] == omega_arr):
246
247
  warnings.warn("The zeroth index phase function Legendre coefficient must be, and has been corrected to, 1.")
247
248
  Leg_coeffs_all[:, 0] = 1
248
- if not (np.all(-1 <= Leg_coeffs_all) and np.all(Leg_coeffs_all <= 1)):
249
- raise ValueError("The phase function Legendre coefficients must all be between -1 and 1.")
249
+ if not (np.all(-1 < Leg_coeffs_all[:, 1:]) and np.all(Leg_coeffs_all[:, 1:] < 1)):
250
+ raise ValueError("The phase function Legendre coefficients must all be between -1 and 1 exclusive (only the zeroth coefficient can equal 1).")
250
251
  # Conditions on the number of quadrature angles (NQuad), Legendre coefficients (NLeg) and loops (NFourier)
251
252
  if not NQuad >= 2:
252
253
  raise ValueError("There must be at least two streams.")
@@ -316,31 +317,31 @@ def pydisort(
316
317
 
317
318
  scale_tau = 1 - omega_arr * f_arr
318
319
  scaled_thickness_arr = scale_tau * thickness_arr
319
- scaled_tau_arr_with_0 = np.insert(np.cumsum(scaled_thickness_arr), 0, 0)
320
- scaled_Leg_coeffs = (Leg_coeffs - f_arr[:, None]) / (1 - f_arr[:, None])
320
+ scaled_tau_arr_with_0 = prepend(np.cumsum(scaled_thickness_arr), NLayers, 0)
321
+ scaled_Leg_coeffs = (Leg_coeffs - f_arr[:, None]) / (1 - f_arr)[:, None]
321
322
  weighted_scaled_Leg_coeffs = scaled_Leg_coeffs * (2 * np.arange(NLeg) + 1)[None, :]
322
323
  scaled_omega_arr = (1 - f_arr) / scale_tau * omega_arr
323
324
 
324
- translations = scaled_tau_arr_with_0[:-1] - scale_tau * np.insert(tau_arr[:-1], 0, 0)
325
+ translations = scaled_tau_arr_with_0[:-1] - scale_tau * prepend(tau_arr[:-1], NLayers - 1, 0)
325
326
  scaled_s_poly_coeffs = (
326
327
  subroutines.affine_transform_poly_coeffs(s_poly_coeffs, scale_tau, translations)
327
328
  / scale_tau[:, None] # Divide by d\tau^* / d\tau
328
- ) * (1 - omega_arr[:, None]) # Enforce Kirchoff's Law of Thermal Radiation: absorptivity equals emissivity
329
+ ) * (1 - omega_arr)[:, None] # Enforce Kirchoff's Law of Thermal Radiation: absorptivity equals emissivity
329
330
 
330
331
  else:
331
332
  # This is a shortcut to the same results
332
333
  scale_tau = np.ones(NLayers)
333
- scaled_tau_arr_with_0 = np.insert(tau_arr, 0, 0)
334
+ scaled_tau_arr_with_0 = prepend(tau_arr, NLayers, 0)
334
335
  scaled_Leg_coeffs = Leg_coeffs
335
336
  weighted_scaled_Leg_coeffs = scaled_Leg_coeffs * (2 * np.arange(NLeg) + 1)[None, :]
336
337
  scaled_omega_arr = omega_arr
337
- scaled_s_poly_coeffs = s_poly_coeffs * (1 - omega_arr[:, None]) # Enforce Kirchoff's Law of Thermal Radiation: absorptivity equals emissivity
338
+ scaled_s_poly_coeffs = s_poly_coeffs * (1 - omega_arr)[:, None] # Enforce Kirchoff's Law of Thermal Radiation: absorptivity equals emissivity
338
339
 
339
340
  if np.any(scaled_omega_arr > 1 - 1e-6):
340
341
  warnings.warn("Some delta-scaled single-scattering albedos are very close to 1 which may cause numerical instability.")
341
- if (np.any(-0.95 > scaled_Leg_coeffs[:, 1:]) and np.any(scaled_Leg_coeffs[:, 1:] > 0.95)):
342
+ if (np.any(-0.95 > scaled_Leg_coeffs[:, 1:]) or np.any(scaled_Leg_coeffs[:, 1:] > 0.95)):
342
343
  warnings.warn("Some delta-scaled phase function Legendre coefficients have a magnitude that is very close to 1" +
343
- " (this excludes the zeroth index coefficient which must be 1) and this may cause numerical instability.")
344
+ " (this excludes the zeroth index coefficient which must be 1) which may cause numerical instability.")
344
345
 
345
346
  # --------------------------------------------------------------------------------------------------------------------------
346
347
 
@@ -367,6 +368,8 @@ def pydisort(
367
368
  I0 = (I0 / rescale_factor).copy()
368
369
  b_pos = (b_pos / rescale_factor).copy()
369
370
  b_neg = (b_neg / rescale_factor).copy()
371
+
372
+ I0_div_4pi = I0 / (4 * pi)
370
373
  # --------------------------------------------------------------------------------------------------------------------------
371
374
 
372
375
  if NT_cor and not only_flux and there_is_beam_source and np.any(f_arr > 0) and NLeg < NLeg_all and np.any(omega_arr > 0):
@@ -379,14 +382,15 @@ def pydisort(
379
382
  scaled_omega_arr,
380
383
  tau_arr,
381
384
  scaled_tau_arr_with_0,
382
- mu_arr_pos, mu_arr,
385
+ mu_arr_pos,
383
386
  M_inv, W,
384
387
  N, NQuad, NLeg,
385
388
  NFourier, NLayers, NBDRF,
386
389
  is_atmos_multilayered,
387
390
  weighted_scaled_Leg_coeffs,
388
391
  BDRF_Fourier_modes,
389
- mu0, I0, rescale_factor, phi0,
392
+ mu0, I0, I0_div_4pi,
393
+ rescale_factor, phi0,
390
394
  there_is_beam_source,
391
395
  b_pos, b_neg,
392
396
  b_pos_is_scalar, b_neg_is_scalar,
@@ -402,21 +406,19 @@ def pydisort(
402
406
 
403
407
  # TMS correction for the intensity (see section 3.7.2)
404
408
  # --------------------------------------------------------------------------------------------------------------------------
405
- def TMS_correction(tau, phi, is_antiderivative_wrt_tau):
409
+ def TMS_correction(tau, phi, is_antiderivative_wrt_tau, l, l_uniq, l_inv):
406
410
  Ntau = len(tau)
407
411
  Nphi = len(phi)
408
412
  # Atmospheric layer indices
409
- l = np.argmax(tau[:, None] <= tau_arr[None, :], axis=1)
410
- scaled_tau_arr_l = scaled_tau_arr_with_0[l + 1]
413
+ if not there_is_iso_source:
414
+ l_uniq, l_inv = np.unique(l, return_inverse=True)
415
+ scaled_tau_arr_l = scaled_tau_arr_with_0[1:][l]
411
416
  scaled_tau_arr_lm1 = scaled_tau_arr_with_0[l]
412
417
 
413
418
  # Delta-M scaling
414
- if np.any(scale_tau != np.ones(NLayers)):
415
- tau_dist_from_top = tau_arr[l] - tau
416
- scaled_tau_dist_from_top = tau_dist_from_top * scale_tau[l]
417
- scaled_tau = scaled_tau_arr_l - scaled_tau_dist_from_top
418
- else:
419
- scaled_tau = tau
419
+ tau_dist_from_top = tau_arr[l] - tau
420
+ scaled_tau_dist_from_top = tau_dist_from_top * scale_tau[l]
421
+ scaled_tau = scaled_tau_arr_l - scaled_tau_dist_from_top
420
422
 
421
423
  # mathscr_B
422
424
  # --------------------------------------------------------------------------------------------------------------------------
@@ -427,202 +429,167 @@ def pydisort(
427
429
  [
428
430
  f(nu)[:, None, :]
429
431
  # Iterates over the 0th axis
430
- for f in map(Legendre, weighted_Leg_coeffs_all)
432
+ for f in map(Legendre, weighted_Leg_coeffs_all[l_uniq])
431
433
  ],
432
434
  axis=1,
433
- )
435
+ ) # (NQuad, NLayers_uniq, Nphi)
434
436
  p_trun = np.concatenate(
435
437
  [
436
438
  f(nu)[:, None, :]
437
439
  # Iterates over the 0th axis
438
- for f in map(Legendre, weighted_scaled_Leg_coeffs)
440
+ for f in map(Legendre, weighted_scaled_Leg_coeffs[l_uniq])
439
441
  ],
440
442
  axis=1,
441
- )
443
+ ) # (NQuad, NLayers_uniq, Nphi)
444
+
442
445
  mathscr_B = (
443
- (scaled_omega_arr * I0)[None, :, None]
444
- / (4 * np.pi)
446
+ (scaled_omega_arr[l_uniq] * I0_div_4pi)[None, :, None]
445
447
  * (mu0 / (mu0 + mu_arr))[:, None, None]
446
- * (p_true / (1 - f_arr[None, :, None]) - p_trun)
447
- )
448
+ * (p_true / (1 - f_arr)[None, l_uniq, None] - p_trun)
449
+ ) # (NQuad, NLayers_uniq, Nphi)
448
450
  # --------------------------------------------------------------------------------------------------------------------------
449
451
 
452
+ neg_scaled_tau_div_mu0 = -scaled_tau / mu0
450
453
  if is_antiderivative_wrt_tau:
454
+
455
+ scale_tau_l = scale_tau[l_uniq]
456
+ neg_scale_tau_l_div_mu0 = -scale_tau_l / mu0
457
+ scale_tau_l_div_mu_arr_pos = scale_tau_l[None, :] / mu_arr_pos[:, None]
458
+
451
459
  TMS_correction_pos = (
452
- np.exp(-scaled_tau / mu0)[None, :] / (-scale_tau / mu0)[None, l]
453
- - np.exp(
454
- (scaled_tau - scaled_tau_arr_l)[None, :] / mu_arr_pos[:, None]
455
- - scaled_tau_arr_l[None, :] / mu0
456
- ) / (scale_tau[None, l] / mu_arr_pos[:, None])
457
- )
460
+ np.exp(neg_scaled_tau_div_mu0) / neg_scale_tau_l_div_mu0[l_inv]
461
+ )[None, :] - np.exp(
462
+ (scaled_tau - scaled_tau_arr_l)[None, :] / mu_arr_pos[:, None]
463
+ - scaled_tau_arr_l[None, :] / mu0
464
+ ) / scale_tau_l_div_mu_arr_pos[:, l_inv]
458
465
  TMS_correction_neg = (
459
- np.exp(-scaled_tau / mu0)[None, :] / (-scale_tau / mu0)[None, l]
460
- - np.exp(
461
- (scaled_tau_arr_lm1 - scaled_tau)[None, :] / mu_arr_pos[:, None]
462
- - scaled_tau_arr_lm1[None, :] / mu0
463
- ) / (-scale_tau[None, l] / mu_arr_pos[:, None])
464
- )
466
+ np.exp(neg_scaled_tau_div_mu0) / neg_scale_tau_l_div_mu0[l_inv]
467
+ )[None, :] + np.exp(
468
+ (scaled_tau_arr_lm1 - scaled_tau)[None, :] / mu_arr_pos[:, None]
469
+ - scaled_tau_arr_lm1[None, :] / mu0
470
+ ) / scale_tau_l_div_mu_arr_pos[:, l_inv]
465
471
  else:
466
- TMS_correction_pos = (
467
- np.exp(-scaled_tau / mu0)[None, :]
468
- - np.exp(
469
- (scaled_tau - scaled_tau_arr_l)[None, :] / mu_arr_pos[:, None]
470
- - scaled_tau_arr_l[None, :] / mu0
471
- )
472
+ TMS_correction_pos = np.exp(neg_scaled_tau_div_mu0)[None, :] - np.exp(
473
+ (scaled_tau - scaled_tau_arr_l)[None, :] / mu_arr_pos[:, None]
474
+ - scaled_tau_arr_l[None, :] / mu0
472
475
  )
473
- TMS_correction_neg = (
474
- np.exp(-scaled_tau / mu0)[None, :]
475
- - np.exp(
476
- (scaled_tau_arr_lm1 - scaled_tau)[None, :] / mu_arr_pos[:, None]
477
- - scaled_tau_arr_lm1[None, :] / mu0
478
- )
476
+ TMS_correction_neg = np.exp(neg_scaled_tau_div_mu0)[None, :] - np.exp(
477
+ (scaled_tau_arr_lm1 - scaled_tau)[None, :] / mu_arr_pos[:, None]
478
+ - scaled_tau_arr_lm1[None, :] / mu0
479
479
  )
480
+
480
481
 
481
482
  ## Contribution from other layers
482
- # TODO: Despite being vectorized and despite many attempts at further optimization,
483
- # this block of code is slower than desired and is the bottleneck for the TMS correction.
484
483
  # --------------------------------------------------------------------------------------------------------------------------
485
484
  solution = (
486
- mathscr_B[:, l, :] * np.vstack((TMS_correction_pos, TMS_correction_neg))[:, :, None]
485
+ mathscr_B[:, l_inv, :]
486
+ * np.concatenate((TMS_correction_pos, TMS_correction_neg), axis=0)[:, :, None]
487
487
  )
488
488
 
489
489
  if is_atmos_multilayered:
490
- layers_arr = np.arange(NLayers)[:, None]
491
- pos_contribution_mask = (l[None, :] < layers_arr).ravel()
492
- neg_contribution_mask = (l[None, :] > layers_arr).ravel()
493
- any_pos_contribution = np.any(pos_contribution_mask)
494
- any_neg_contribution = np.any(neg_contribution_mask)
495
-
496
- if not (any_pos_contribution or any_neg_contribution):
497
- return solution
490
+ any_pos = (l.min() < NLayers - 1)
491
+ any_neg = (l.max() > 0)
498
492
 
499
- layers_arr_repeat = np.repeat(layers_arr, Ntau)
500
- scaled_tau_tile = np.tile(scaled_tau, NLayers)
493
+ mu0_inv = 1 / mu0
501
494
 
502
- if any_pos_contribution:
503
- contribution_from_each_other_layer_pos = np.zeros((N, NLayers * Ntau, Nphi))
504
- scaled_tau_tile_pos = scaled_tau_tile[pos_contribution_mask]
505
-
506
- layers_arr_repeat_pos = layers_arr_repeat[pos_contribution_mask]
507
- mathscr_B_repeat_pos = mathscr_B[:N, layers_arr_repeat_pos, :]
508
- scaled_tau_arr_with_0_l_repeat_pos = scaled_tau_arr_with_0[:-1][layers_arr_repeat_pos]
509
- scaled_tau_arr_with_0_lp1_repeat_pos = scaled_tau_arr_with_0[1:][layers_arr_repeat_pos]
510
-
495
+ scaled_tauwith0_front = scaled_tau_arr_with_0[:-1] # (NLayers,)
496
+ scaled_tauwith0_back = scaled_tau_arr_with_0[1:] # (NLayers,); i.e. `scaled_tau_arr`
497
+
498
+ exp_taufront_mu0_inv = np.concatenate(
499
+ ([1], np.exp(-scaled_tauwith0_front[1:] * mu0_inv))
500
+ ) # (Nlayers, )
501
+
502
+
503
+ Bpos = mathscr_B[:N, :, :] # (N, NLayers_uniq, Nphi)
504
+ Bneg = mathscr_B[N:, :, :] # (N, NLayers_uniq, Nphi)
505
+
506
+ # decay[n,r] = exp(-scaled_thickness_arr[r]/mu[n]); always <= 1
507
+ log_decay = -scaled_thickness_arr[None, :] * M_inv[:, None] # (N, NLayers)
508
+
509
+ # decay_prod[n,k] = prod_{m=0}^{k-1} decay[n,m], with decay_prod[:,0]=1
510
+ log_decay_prod = np.cumsum(log_decay, axis=1) # The cumsum requires ALL layers; `l_uniq` is inapplicable
511
+ decay_prod = np.concatenate((np.ones((N, 1)), np.exp(log_decay_prod)), axis=1) # (N, NLayers + 1)
512
+
513
+ if is_antiderivative_wrt_tau:
514
+ # (N, NLayers)
515
+ integration_factor = mu_arr_pos[:, None] / scale_tau
516
+
517
+ # ====================================================================
518
+ # POS: pre-accumulate "contribution from below" sums Rpos[N, l, Nphi]
519
+ # ====================================================================
520
+ if any_pos:
521
+ thickness_pos = scaled_thickness_arr[None, :] * (M_inv + mu0_inv)[:, None] # (N, NLayers); always > 0
522
+ # em1_pos = 1 - exp(-scaled_thickness_arr*(1/mu + 1/mu0)) = -expm1(-thickness_pos)
523
+ em1_pos = -np.expm1(-thickness_pos) # (N, NLayers)
511
524
  if is_antiderivative_wrt_tau:
512
- scale_tau_repeat_pos = scale_tau[layers_arr_repeat_pos]
513
- contribution_from_each_other_layer_pos[:, pos_contribution_mask, :] = (
514
- mathscr_B_repeat_pos
515
- * (
516
- (
517
- np.exp(
518
- (scaled_tau_tile_pos - scaled_tau_arr_with_0_l_repeat_pos)
519
- / mu_arr_pos[:, None]
520
- - scaled_tau_arr_with_0_l_repeat_pos / mu0
521
- )
522
- - np.exp(
523
- (scaled_tau_tile_pos - scaled_tau_arr_with_0_lp1_repeat_pos)
524
- / mu_arr_pos[:, None]
525
- - scaled_tau_arr_with_0_lp1_repeat_pos / mu0
526
- )
527
- )
528
- / (scale_tau_repeat_pos / mu_arr_pos[:, None])
529
- )[:, :, None]
530
- )
531
- contribution_from_other_layers_pos = np.sum(
532
- contribution_from_each_other_layer_pos.reshape((N, NLayers, Ntau, Nphi)),
533
- axis=1,
534
- )
525
+ em1_pos = integration_factor * em1_pos
526
+
527
+ layer_term_pos = em1_pos * exp_taufront_mu0_inv[None, :] # (N, NLayers)
528
+
529
+ # Reverse-scan via cumsum:
530
+ # Rpos[l] = (1/decay_prod[l+1]) * sum_{r=l+1..} (layer_term_pos[r] * decay_prod[r])
531
+ weighted_layer_term_pos = layer_term_pos * decay_prod[:, :NLayers] # (N, NLayers)
532
+ cumsum_pos = np.cumsum(weighted_layer_term_pos[:, ::-1], axis=1)[:, ::-1] # (N, NLayers)
533
+
534
+ Rpos_rest = cumsum_pos[:, 1:] / decay_prod[:, 1 : NLayers] # (N, NLayers - 1)
535
+ Rpos = np.concatenate([Rpos_rest, np.zeros((N, 1))], axis=1) # (N, NLayers)
536
+
537
+ # Apply to all `tau`
538
+ # expfac_pos = exp(M_inv*tau) * exp(-M_inv*scaled_tauwith0_back[l])
539
+ expfac_pos = np.exp(M_inv[:, None] * (scaled_tau - scaled_tauwith0_back[l])[None, :]) # (N, Ntau)
540
+ tmp = np.take(Rpos, l, axis=1) # (N, Ntau)
541
+ tmp = (tmp * expfac_pos)[:, :, None] * Bpos[:, l_inv, :]
542
+
543
+ if autograd_compatible:
544
+ solution = np.concatenate((solution[:N, :, :] + tmp, solution[N:, :, :]), axis=0)
535
545
  else:
536
- contribution_from_each_other_layer_pos[:, pos_contribution_mask, :] = (
537
- mathscr_B_repeat_pos
538
- * (
539
- np.exp(
540
- (scaled_tau_tile_pos - scaled_tau_arr_with_0_l_repeat_pos)
541
- / mu_arr_pos[:, None]
542
- - scaled_tau_arr_with_0_l_repeat_pos / mu0
543
- )
544
- - np.exp(
545
- (scaled_tau_tile_pos - scaled_tau_arr_with_0_lp1_repeat_pos)
546
- / mu_arr_pos[:, None]
547
- - scaled_tau_arr_with_0_lp1_repeat_pos / mu0
548
- )
549
- )[:, :, None]
550
- )
551
- contribution_from_other_layers_pos = np.sum(
552
- contribution_from_each_other_layer_pos.reshape((N, NLayers, Ntau, Nphi)),
553
- axis=1,
554
- )
555
-
556
- if any_neg_contribution:
557
- contribution_from_each_other_layer_neg = np.zeros((N, NLayers * Ntau, Nphi))
558
- scaled_tau_tile_neg = scaled_tau_tile[neg_contribution_mask]
546
+ solution[:N, :, :] += tmp
547
+
548
+ # ====================================================================
549
+ # NEG: pre-accumulate "contribution from above" sums Rneg[N, l, Nphi]
550
+ # ====================================================================
551
+ if any_neg:
552
+ decay = np.exp(log_decay) # (N, NLayers)
559
553
 
560
- layers_arr_repeat_neg = layers_arr_repeat[neg_contribution_mask]
561
- mathscr_B_repeat_neg = mathscr_B[N:, layers_arr_repeat_neg, :]
562
- scaled_tau_arr_with_0_l_repeat_neg = scaled_tau_arr_with_0[:-1][layers_arr_repeat_neg]
563
- scaled_tau_arr_with_0_lp1_repeat_neg = scaled_tau_arr_with_0[1:][layers_arr_repeat_neg]
554
+ # Ensure no positive exponents
555
+ thickness_neg = scaled_thickness_arr[None, :] * (M_inv - mu0_inv)[:, None] # (N, NLayers)
556
+ mask = (thickness_neg >= 0)
557
+
558
+ exp_x1 = np.exp(-scaled_tauwith0_back * mu0_inv)[None, :] # (1, NLayers)
559
+ exp_x0 = decay * exp_taufront_mu0_inv[None, :] # (N, NLayers)
560
+
561
+ # Let a = |thickness_neg| >= 0. Then expm1(-a) uses only non-positive inputs.
562
+ em1_neg = np.expm1(-np.abs(thickness_neg)) # (N, NLayers)
563
+
564
+ # thickness_neg>=0: exp_x1 * (1 - exp(-a)) = exp_x1 * (-expm1(-a)) = exp_x1 * (-em1_neg)
565
+ # thickness_neg<0 : exp_x0 * (exp(-a) - 1) = exp_x0 * expm1(-a) = exp_x0 * ( em1_neg)
566
+ layer_term_negthick = em1_neg * exp_x0 # (N, NLayers)
567
+ layer_term_posthick = -em1_neg * exp_x1 # (N, NLayers)
568
+ layer_term_neg = layer_term_posthick * mask + layer_term_negthick * (~mask) # (N, NLayers)
564
569
 
565
570
  if is_antiderivative_wrt_tau:
566
- scale_tau_repeat_neg = scale_tau[layers_arr_repeat_neg]
567
- contribution_from_each_other_layer_neg[:, neg_contribution_mask, :] = (
568
- mathscr_B_repeat_neg
569
- * (
570
- (
571
- np.exp(
572
- (scaled_tau_arr_with_0_lp1_repeat_neg - scaled_tau_tile_neg)
573
- / mu_arr_pos[:, None]
574
- - scaled_tau_arr_with_0_lp1_repeat_neg / mu0
575
- )
576
- - np.exp(
577
- (scaled_tau_arr_with_0_l_repeat_neg - scaled_tau_tile_neg)
578
- / mu_arr_pos[:, None]
579
- - scaled_tau_arr_with_0_l_repeat_neg / mu0
580
- )
581
- )
582
- / (-scale_tau_repeat_neg / mu_arr_pos[:, None])
583
- )[:, :, None]
584
- )
585
- contribution_from_other_layers_neg = np.sum(
586
- contribution_from_each_other_layer_neg.reshape((N, NLayers, Ntau, Nphi)),
587
- axis=1,
588
- )
589
- else:
590
- contribution_from_each_other_layer_neg[:, neg_contribution_mask, :] = (
591
- mathscr_B_repeat_neg
592
- * (
593
- np.exp(
594
- (scaled_tau_arr_with_0_lp1_repeat_neg - scaled_tau_tile_neg)
595
- / mu_arr_pos[:, None]
596
- - scaled_tau_arr_with_0_lp1_repeat_neg / mu0
597
- )
598
- - np.exp(
599
- (scaled_tau_arr_with_0_l_repeat_neg - scaled_tau_tile_neg)
600
- / mu_arr_pos[:, None]
601
- - scaled_tau_arr_with_0_l_repeat_neg / mu0
602
- )
603
- )[:, :, None]
604
- )
605
- contribution_from_other_layers_neg = np.sum(
606
- contribution_from_each_other_layer_neg.reshape((N, NLayers, Ntau, Nphi)),
607
- axis=1,
608
- )
609
-
610
- if autograd_compatible:
611
- if not any_pos_contribution:
612
- TMS_correction_pos = np.zeros((N, Ntau, Nphi))
613
- if not any_neg_contribution:
614
- TMS_correction_neg = np.zeros((N, Ntau, Nphi))
615
- return solution + np.concatenate(
616
- (contribution_from_other_layers_pos, contribution_from_other_layers_neg),
617
- axis=0,
618
- )
619
- else:
620
- if any_pos_contribution:
621
- solution[:N, :, :] += contribution_from_other_layers_pos
622
- if any_neg_contribution:
623
- solution[N:, :, :] += contribution_from_other_layers_neg
624
- return solution
571
+ layer_term_neg = -integration_factor * layer_term_neg
572
+
573
+ # Forward-scan via cumsum:
574
+ # Rneg[l] = decay_prod[l] * sum_{r<l} layer_term_neg[r] / decay_prod[r+1]
575
+ weighted_layer_term_neg = layer_term_neg / decay_prod[:, 1:] # (N, NLayers)
576
+ cumsum_neg = np.cumsum(weighted_layer_term_neg, axis=1) # (N, NLayers)
577
+
578
+ Rneg_rest = decay_prod[:, 1 : NLayers] * cumsum_neg[:, :-1] # (N, NLayers - 1)
579
+ Rneg = np.concatenate([np.zeros((N, 1)), Rneg_rest], axis=1) # (N, NLayers)
580
+
581
+ # Apply to all `tau`
582
+ expfac_neg = np.exp(M_inv[:, None] * (scaled_tauwith0_front[l] - scaled_tau)[None, :]) # (N, Ntau)
583
+ tmp = np.take(Rneg, l, axis=1) # (N, Ntau)
584
+ tmp = (tmp * expfac_neg)[:, :, None] * Bneg[:, l_inv, :]
625
585
 
586
+ if autograd_compatible:
587
+ solution = np.concatenate((solution[:N, :, :], solution[N:, :, :] + tmp), axis=0)
588
+ else:
589
+ solution[N:, :, :] += tmp
590
+
591
+ return solution
592
+
626
593
  # --------------------------------------------------------------------------------------------------------------------------
627
594
 
628
595
  else:
@@ -661,8 +628,7 @@ def pydisort(
661
628
  ) / (mu_arr_pos * scaled_mu0 * x)[:, None]
662
629
 
663
630
  return (
664
- I0
665
- / (4 * pi)
631
+ I0_div_4pi
666
632
  * (omega_avg * f_avg) ** 2
667
633
  / (1 - omega_avg * f_avg)
668
634
  * Legendre(
@@ -674,25 +640,59 @@ def pydisort(
674
640
 
675
641
  # The corrected intensity
676
642
  # --------------------------------------------------------------------------------------------------------------------------
677
- def u_corrected(tau, phi, is_antiderivative_wrt_tau=False, return_Fourier_error=False, return_tau_arr=False):
643
+ def u_corrected(
644
+ tau,
645
+ phi,
646
+ is_antiderivative_wrt_tau=False,
647
+ return_Fourier_error=False,
648
+ return_tau_arr=False,
649
+ ):
678
650
  tau = np.atleast_1d(tau)
679
651
  phi = np.atleast_1d(phi)
680
- NT_corrections = TMS_correction(tau, phi, is_antiderivative_wrt_tau)
681
-
652
+
653
+ u_star_outputs = u_star(
654
+ tau,
655
+ phi,
656
+ is_antiderivative_wrt_tau,
657
+ return_Fourier_error,
658
+ return_tau_arr,
659
+ _return_l=True,
660
+ )
661
+ if there_is_iso_source:
662
+ NT_corrections = TMS_correction(
663
+ tau,
664
+ phi,
665
+ is_antiderivative_wrt_tau,
666
+ u_star_outputs[-3],
667
+ u_star_outputs[-2],
668
+ u_star_outputs[-1],
669
+ )
670
+ u_star_outputs = u_star_outputs[:-3]
671
+ else:
672
+ NT_corrections = TMS_correction(
673
+ tau, phi, is_antiderivative_wrt_tau, u_star_outputs[-1], None, None
674
+ )
675
+ u_star_outputs = u_star_outputs[:-1]
676
+
682
677
  if autograd_compatible:
683
- NT_corrections = NT_corrections + np.concatenate(
684
- [np.zeros((N, len(tau), len(phi))), IMS_correction(tau, phi, is_antiderivative_wrt_tau)], axis=0
678
+ NT_corrections = np.concatenate(
679
+ (
680
+ NT_corrections[:N, :, :],
681
+ NT_corrections[N:, :, :]
682
+ + IMS_correction(tau, phi, is_antiderivative_wrt_tau),
683
+ ),
684
+ axis=0,
685
685
  )
686
686
  else:
687
687
  NT_corrections[N:, :, :] += IMS_correction(tau, phi, is_antiderivative_wrt_tau)
688
-
688
+
689
689
  if return_Fourier_error or return_tau_arr:
690
- u_star_outputs = u_star(tau, phi, is_antiderivative_wrt_tau, return_Fourier_error, return_tau_arr)
691
690
  return (
692
691
  u_star_outputs[0] + rescale_factor * np.squeeze(NT_corrections),
693
692
  ) + u_star_outputs[1:]
694
693
  else:
695
- return u_star(tau, phi, is_antiderivative_wrt_tau) + rescale_factor * np.squeeze(NT_corrections)
694
+ return u_star_outputs[0] + rescale_factor * np.squeeze(NT_corrections)
695
+
696
696
  # --------------------------------------------------------------------------------------------------------------------------
697
697
 
698
698
  return mu_arr, flux_up, flux_down, u0, u_corrected
@@ -702,14 +702,15 @@ def pydisort(
702
702
  scaled_omega_arr,
703
703
  tau_arr,
704
704
  scaled_tau_arr_with_0,
705
- mu_arr_pos, mu_arr,
705
+ mu_arr_pos,
706
706
  M_inv, W,
707
707
  N, NQuad, NLeg,
708
708
  NFourier, NLayers, NBDRF,
709
709
  is_atmos_multilayered,
710
710
  weighted_scaled_Leg_coeffs,
711
711
  BDRF_Fourier_modes,
712
- mu0, I0, rescale_factor, phi0,
712
+ mu0, I0, I0_div_4pi,
713
+ rescale_factor, phi0,
713
714
  there_is_beam_source,
714
715
  b_pos, b_neg,
715
716
  b_pos_is_scalar, b_neg_is_scalar,