PythonicDISORT 1.2__py3-none-any.whl → 1.3__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,51 +429,57 @@ 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[None, l_uniq]
456
+ neg_scale_tau_l_div_mu0 = -scale_tau_l / mu0
457
+ scale_tau_l_div_mu_arr_pos = scale_tau_l / mu_arr_pos[:, None]
458
+
451
459
  TMS_correction_pos = (
452
- np.exp(-scaled_tau / mu0)[None, :] / (-scale_tau / mu0)[None, l]
460
+ np.exp(neg_scaled_tau_div_mu0)[None, :] / neg_scale_tau_l_div_mu0[l_inv]
453
461
  - np.exp(
454
462
  (scaled_tau - scaled_tau_arr_l)[None, :] / mu_arr_pos[:, None]
455
463
  - scaled_tau_arr_l[None, :] / mu0
456
- ) / (scale_tau[None, l] / mu_arr_pos[:, None])
464
+ ) / scale_tau_l_div_mu_arr_pos[l_inv]
457
465
  )
458
466
  TMS_correction_neg = (
459
- np.exp(-scaled_tau / mu0)[None, :] / (-scale_tau / mu0)[None, l]
460
- - np.exp(
467
+ np.exp(neg_scaled_tau_div_mu0)[None, :] / neg_scale_tau_l_div_mu0[l_inv]
468
+ + np.exp(
461
469
  (scaled_tau_arr_lm1 - scaled_tau)[None, :] / mu_arr_pos[:, None]
462
470
  - scaled_tau_arr_lm1[None, :] / mu0
463
- ) / (-scale_tau[None, l] / mu_arr_pos[:, None])
471
+ ) / scale_tau_l_div_mu_arr_pos[l_inv]
464
472
  )
465
473
  else:
466
474
  TMS_correction_pos = (
467
- np.exp(-scaled_tau / mu0)[None, :]
475
+ np.exp(neg_scaled_tau_div_mu0)[None, :]
468
476
  - np.exp(
469
477
  (scaled_tau - scaled_tau_arr_l)[None, :] / mu_arr_pos[:, None]
470
478
  - scaled_tau_arr_l[None, :] / mu0
471
479
  )
472
480
  )
473
481
  TMS_correction_neg = (
474
- np.exp(-scaled_tau / mu0)[None, :]
482
+ np.exp(neg_scaled_tau_div_mu0)[None, :]
475
483
  - np.exp(
476
484
  (scaled_tau_arr_lm1 - scaled_tau)[None, :] / mu_arr_pos[:, None]
477
485
  - scaled_tau_arr_lm1[None, :] / mu0
@@ -479,150 +487,116 @@ def pydisort(
479
487
  )
480
488
 
481
489
  ## 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
490
  # --------------------------------------------------------------------------------------------------------------------------
485
491
  solution = (
486
- mathscr_B[:, l, :] * np.vstack((TMS_correction_pos, TMS_correction_neg))[:, :, None]
492
+ mathscr_B[:, l_inv, :]
493
+ * np.concatenate((TMS_correction_pos, TMS_correction_neg), axis=0)[:, :, None]
487
494
  )
488
495
 
489
496
  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
497
+ any_pos = (l.min() < NLayers - 1)
498
+ any_neg = (l.max() > 0)
498
499
 
499
- layers_arr_repeat = np.repeat(layers_arr, Ntau)
500
- scaled_tau_tile = np.tile(scaled_tau, NLayers)
500
+ mu0_inv = 1 / mu0
501
501
 
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
-
502
+ scaled_tauwith0_front = scaled_tau_arr_with_0[:-1] # (NLayers,)
503
+ scaled_tauwith0_back = scaled_tau_arr_with_0[1:] # (NLayers,); i.e. `scaled_tau_arr`
504
+
505
+ exp_taufront_mu0_inv = np.concatenate(
506
+ ([1], np.exp(-scaled_tauwith0_front[1:] * mu0_inv))
507
+ ) # (Nlayers, )
508
+
509
+
510
+ Bpos = mathscr_B[:N, :, :] # (N, NLayers_uniq, Nphi)
511
+ Bneg = mathscr_B[N:, :, :] # (N, NLayers_uniq, Nphi)
512
+
513
+ # decay[n,r] = exp(-scaled_thickness_arr[r]/mu[n]); always <= 1
514
+ log_decay = -scaled_thickness_arr[None, :] * M_inv[:, None] # (N, NLayers)
515
+
516
+ # decay_prod[n,k] = prod_{m=0}^{k-1} decay[n,m], with decay_prod[:,0]=1
517
+ log_decay_prod = np.cumsum(log_decay, axis=1) # The cumsum requires ALL layers; `l_uniq` is inapplicable
518
+ decay_prod = np.concatenate((np.ones((N, 1)), np.exp(log_decay_prod)), axis=1) # (N, NLayers + 1)
519
+
520
+ if is_antiderivative_wrt_tau:
521
+ # (N, NLayers)
522
+ integration_factor = mu_arr_pos[:, None] / scale_tau
523
+
524
+ # ====================================================================
525
+ # POS: pre-accumulate "contribution from below" sums Rpos[N, l, Nphi]
526
+ # ====================================================================
527
+ if any_pos:
528
+ # fac_pos = 1 - exp(-scaled_thickness_arr*(1/mu + 1/mu0)) = -expm1(-thickness_pos)
529
+ thickness_pos = scaled_thickness_arr[None, :] * (M_inv + mu0_inv)[:, None] # (N, NLayers); always > 0
530
+ fac_pos = -np.expm1(-thickness_pos) # (N, NLayers)
511
531
  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
- )
532
+ fac_pos = fac_pos * integration_factor
533
+
534
+ layer_term_pos = fac_pos * exp_taufront_mu0_inv[None, :] # (N, NLayers)
535
+
536
+ # Reverse-scan via cumsum:
537
+ # Rpos[l] = (1/decay_prod[l+1]) * sum_{r=l+1..} (layer_term_pos[r] * decay_prod[r])
538
+ weighted_layer_term_pos = layer_term_pos * decay_prod[:, :NLayers] # (N, NLayers)
539
+ cumsum_pos = np.cumsum(weighted_layer_term_pos[:, ::-1], axis=1)[:, ::-1] # (N, NLayers)
540
+
541
+ Rpos_rest = cumsum_pos[:, 1:] / decay_prod[:, 1 : NLayers] # (N, NLayers - 1)
542
+ Rpos = np.concatenate([Rpos_rest, np.zeros((N, 1))], axis=1) # (N, NLayers)
543
+
544
+ # Apply to all `tau`
545
+ # expfac_pos = exp(M_inv*tau) * exp(-M_inv*scaled_tauwith0_back[l])
546
+ expfac_pos = np.exp(M_inv[:, None] * (scaled_tau - scaled_tauwith0_back[l])[None, :]) # (N, Ntau)
547
+ tmp = np.take(Rpos, l, axis=1) # (N, Ntau)
548
+ tmp = (tmp * expfac_pos)[:, :, None] * Bpos[:, l_inv, :]
549
+
550
+ if autograd_compatible:
551
+ solution = np.concatenate((solution[:N, :, :] + tmp, solution[N:, :, :]), axis=0)
535
552
  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]
553
+ solution[:N, :, :] += tmp
554
+
555
+ # ====================================================================
556
+ # NEG: pre-accumulate "contribution from above" sums Rneg[N, l, Nphi]
557
+ # ====================================================================
558
+ if any_neg:
559
+ decay = np.exp(log_decay) # (N, NLayers)
559
560
 
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]
561
+ # Ensure no positive exponents
562
+ thickness_neg = scaled_thickness_arr[None, :] * (M_inv - mu0_inv)[:, None] # (N, NLayers)
563
+ mask = (thickness_neg >= 0)
564
+
565
+ exp_x1 = np.exp(-scaled_tauwith0_back * mu0_inv)[None, :] # (1, NLayers)
566
+ exp_x0 = decay * exp_taufront_mu0_inv[None, :] # (N, NLayers)
567
+
568
+ # Let a = |thickness_neg| >= 0. Then expm1(-a) uses only non-positive inputs.
569
+ em1 = np.expm1(-np.abs(thickness_neg)) # (N, NLayers)
570
+
571
+ # thickness_neg>=0: exp_x1 * (1 - exp(-a)) = exp_x1 * (-expm1(-a)) = exp_x1 * (-em1)
572
+ # thickness_neg<0 : exp_x0 * (exp(-a) - 1) = exp_x0 * expm1(-a) = exp_x0 * ( em1)
573
+ layer_term_negthick = em1 * exp_x0 # (N, NLayers)
574
+ layer_term_posthick = -em1 * exp_x1 # (N, NLayers)
575
+ layer_term_neg = layer_term_posthick * mask + layer_term_negthick * (~mask) # (N, NLayers)
564
576
 
565
577
  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
578
+ layer_term_neg = -integration_factor * layer_term_neg
579
+
580
+ # Forward-scan via cumsum:
581
+ # Rneg[l] = decay_prod[l] * sum_{r<l} layer_term_neg[r] / decay_prod[r+1]
582
+ weighted_layer_term_neg = layer_term_neg / decay_prod[:, 1:] # (N, NLayers)
583
+ cumsum_neg = np.cumsum(weighted_layer_term_neg, axis=1) # (N, NLayers)
584
+
585
+ Rneg_rest = decay_prod[:, 1 : NLayers] * cumsum_neg[:, :-1] # (N, NLayers - 1)
586
+ Rneg = np.concatenate([np.zeros((N, 1)), Rneg_rest], axis=1) # (N, NLayers)
587
+
588
+ # Apply to all `tau`
589
+ expfac_neg = np.exp(M_inv[:, None] * (scaled_tauwith0_front[l] - scaled_tau)[None, :]) # (N, Ntau)
590
+ tmp = np.take(Rneg, l, axis=1) # (N, Ntau)
591
+ tmp = (tmp * expfac_neg)[:, :, None] * Bneg[:, l_inv, :]
625
592
 
593
+ if autograd_compatible:
594
+ solution = np.concatenate((solution[:N, :, :], solution[N:, :, :] + tmp), axis=0)
595
+ else:
596
+ solution[N:, :, :] += tmp
597
+
598
+ return solution
599
+
626
600
  # --------------------------------------------------------------------------------------------------------------------------
627
601
 
628
602
  else:
@@ -661,8 +635,7 @@ def pydisort(
661
635
  ) / (mu_arr_pos * scaled_mu0 * x)[:, None]
662
636
 
663
637
  return (
664
- I0
665
- / (4 * pi)
638
+ I0_div_4pi
666
639
  * (omega_avg * f_avg) ** 2
667
640
  / (1 - omega_avg * f_avg)
668
641
  * Legendre(
@@ -674,25 +647,59 @@ def pydisort(
674
647
 
675
648
  # The corrected intensity
676
649
  # --------------------------------------------------------------------------------------------------------------------------
677
- def u_corrected(tau, phi, is_antiderivative_wrt_tau=False, return_Fourier_error=False, return_tau_arr=False):
650
+ def u_corrected(
651
+ tau,
652
+ phi,
653
+ is_antiderivative_wrt_tau=False,
654
+ return_Fourier_error=False,
655
+ return_tau_arr=False,
656
+ ):
678
657
  tau = np.atleast_1d(tau)
679
658
  phi = np.atleast_1d(phi)
680
- NT_corrections = TMS_correction(tau, phi, is_antiderivative_wrt_tau)
681
-
659
+
660
+ u_star_outputs = u_star(
661
+ tau,
662
+ phi,
663
+ is_antiderivative_wrt_tau,
664
+ return_Fourier_error,
665
+ return_tau_arr,
666
+ _return_l=True,
667
+ )
668
+ if there_is_iso_source:
669
+ NT_corrections = TMS_correction(
670
+ tau,
671
+ phi,
672
+ is_antiderivative_wrt_tau,
673
+ u_star_outputs[-3],
674
+ u_star_outputs[-2],
675
+ u_star_outputs[-1],
676
+ )
677
+ u_star_outputs = u_star_outputs[:-3]
678
+ else:
679
+ NT_corrections = TMS_correction(
680
+ tau, phi, is_antiderivative_wrt_tau, u_star_outputs[-1], None, None
681
+ )
682
+ u_star_outputs = u_star_outputs[:-1]
683
+
682
684
  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
685
+ NT_corrections = np.concatenate(
686
+ (
687
+ NT_corrections[:N, :, :],
688
+ NT_corrections[N:, :, :]
689
+ + IMS_correction(tau, phi, is_antiderivative_wrt_tau),
690
+ ),
691
+ axis=0,
685
692
  )
686
693
  else:
687
694
  NT_corrections[N:, :, :] += IMS_correction(tau, phi, is_antiderivative_wrt_tau)
688
-
695
+
689
696
  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
697
  return (
692
698
  u_star_outputs[0] + rescale_factor * np.squeeze(NT_corrections),
693
699
  ) + u_star_outputs[1:]
694
700
  else:
695
- return u_star(tau, phi, is_antiderivative_wrt_tau) + rescale_factor * np.squeeze(NT_corrections)
701
+ return u_star_outputs[0] + rescale_factor * np.squeeze(NT_corrections)
702
+
696
703
  # --------------------------------------------------------------------------------------------------------------------------
697
704
 
698
705
  return mu_arr, flux_up, flux_down, u0, u_corrected
@@ -702,14 +709,15 @@ def pydisort(
702
709
  scaled_omega_arr,
703
710
  tau_arr,
704
711
  scaled_tau_arr_with_0,
705
- mu_arr_pos, mu_arr,
712
+ mu_arr_pos,
706
713
  M_inv, W,
707
714
  N, NQuad, NLeg,
708
715
  NFourier, NLayers, NBDRF,
709
716
  is_atmos_multilayered,
710
717
  weighted_scaled_Leg_coeffs,
711
718
  BDRF_Fourier_modes,
712
- mu0, I0, rescale_factor, phi0,
719
+ mu0, I0, I0_div_4pi,
720
+ rescale_factor, phi0,
713
721
  there_is_beam_source,
714
722
  b_pos, b_neg,
715
723
  b_pos_is_scalar, b_neg_is_scalar,