@@ -422,7 +422,7 @@ def full_Fourier(mp, wvl, Ein, normFac, flagScaleFPM=False):
422422 return Eout
423423
424424
425- def compact_reverse_gradient (command_vec , mp , EestAll , EFendPrev , log10reg ):
425+ def compact_reverse_gradient (command_vec , log10reg , mp , EestAll , EFend_list , Edm1post_list , Edm2pre_list , DM2surf_list ):
426426 """
427427 Simplified (aka compact) model used by estimator and controller.
428428
@@ -488,11 +488,10 @@ def compact_reverse_gradient(command_vec, mp, EestAll, EFendPrev, log10reg):
488488 dDM2Vvec [mp .dm2 .act_ele ] = command_vec [mp .ctrl .uLegend == 2 ]
489489 dv_dm1 = dDM1Vvec .reshape ((mp .dm1 .Nact , mp .dm1 .Nact ))
490490 dv_dm2 = dDM2Vvec .reshape ((mp .dm2 .Nact , mp .dm2 .Nact ))
491- # mp.dm1.V += command_vec[0:mp.dm1.NactTotal].reshape([mp.dm1.Nact, mp.dm1.Nact])
492- # mp.dm2.V += command_vec[mp.dm2.NactTotal::].reshape([mp.dm2.Nact, mp.dm2.Nact])
493491
494- total_cost = 0 # initialize
495- normFacADweightedSum = 0
492+ # initialize
493+ total_cost = 0
494+ # normFacADweightedSum = 0
496495
497496 for imode in range (mp .jac .Nmode ):
498497 modvar = falco .config .ModelVariables ()
@@ -505,17 +504,18 @@ def compact_reverse_gradient(command_vec, mp, EestAll, EFendPrev, log10reg):
505504 kk = mirrorFac * 2 * np .pi / wvl
506505 I00 = mp .Fend .compact .I00 [modvar .sbpIndex ]
507506 # normFac = mp.Fend.compact.I00[modvar.sbpIndex]
507+ # print(f'normFacAD = {normFacAD}')
508508 # normFacFull = np.mean(mp.Fend.full.I00[modvar.sbpIndex, :])
509509 EestVec = EestAll [:, imode ]
510510 Eest2D = np .zeros_like (mp .Fend .corr .maskBool , dtype = complex )
511- Eest2D [mp .Fend .corr .maskBool ] = EestVec # * np.sqrt(normFacFull) # Remove normalization
512- normFacAD = np .sum (np .abs (EestVec )** 2 )
513- # print(f'normFacAD = {normFacAD}')
511+ Eest2D [mp .Fend .corr .maskBool ] = EestVec
512+ # normFacAD = np.sum(np.abs(EestVec)**2)
514513
515- # Get model-based E-field before deltas
516- EFendA , Edm1post , Edm2pre , DM1surf , DM2surf = compact (
517- mp , modvar , isNorm = True , isEvalMode = isEvalMode , useFPM = useFPM ,
518- forRevGradModel = True )
514+ # Get model-based E-field before deltas. Should be pre-computed for speed.
515+ EFendA = EFend_list [imode ]
516+ Edm1post = Edm1post_list [imode ]
517+ Edm2pre = Edm2pre_list [imode ]
518+ DM2surf = DM2surf_list [imode ]
519519
520520 # Get model-based E-field With delta DM commands applied.
521521 mp .dm1 .V = mp .dm1 .V0 + dv_dm1
@@ -528,17 +528,14 @@ def compact_reverse_gradient(command_vec, mp, EestAll, EFendPrev, log10reg):
528528 mp .dm2 .V = mp .dm2 .V0 .copy ()
529529
530530 # Compute the delta E-field from the latest commands (model new - model old).
531- # EFendA = EFendPrev[imode]
532531 dEend = EFendB - EFendA
533532
534533 # DH = EFend[mp.Fend.corr.maskBool]
535- # Eest2D = EFendA # DEBUGGING
536534 EdhNew = Eest2D + dEend
537535 DH = EdhNew [mp .Fend .corr .maskBool ]
538536 int_in_dh = np .sum (np .abs (DH )** 2 )
539- # print(f'int_in_dh = {int_in_dh}')
540537 total_cost += mp .jac .weights [imode ] * int_in_dh
541- normFacADweightedSum += mp .jac .weights [imode ] / normFacAD
538+ # normFacADweightedSum += mp.jac.weights[imode] / normFacAD
542539
543540 # Gradient
544541 Fend_masked = mp .jac .weights [imode ]* 2 / np .sqrt (I00 )* EdhNew * mp .Fend .corr .maskBool
@@ -612,14 +609,14 @@ def compact_reverse_gradient(command_vec, mp, EestAll, EFendPrev, log10reg):
612609 Edm1_grad = falco .prop .ptp (Edm2_grad , mp .P2 .compact .dx * Edm2_grad .shape [0 ], wvl , - mp .d_dm1_dm2 )
613610 surf_dm1_bar = - kk * np .imag (Edm1_grad * np .conj (Edm1post ))
614611
615- surf_dm2_bar_total += mp . jac . weights [ imode ] * surf_dm2_bar
616- surf_dm1_bar_total += mp . jac . weights [ imode ] * surf_dm1_bar
612+ surf_dm2_bar_total += surf_dm2_bar
613+ surf_dm1_bar_total += surf_dm1_bar
617614
618- # # Calculate DM penalty term component of cost function
619- # utu_coefs = normFacADweightedSum * mp.ctrl.ad.utu_scale_fac * 10.0**(log10reg)
620- # total_cost += utu_coefs * np.sum(command_vec**2)
621- # # print('normFacADweightedSum = %.4g' % normFacADweightedSum)
622- # # print('utu_coefs = %.4g' % utu_coefs )
615+ # Calculate DM penalty term component of cost function
616+ utu_coef = mp .ctrl .ad .utu_scale_fac * 10.0 ** (log10reg )
617+ total_cost += utu_coef * np .sum (command_vec ** 2 )
618+ # print('normFacADweightedSum = %.4g' % normFacADweightedSum)
619+ # print('utu_coef = %.4g' % utu_coef )
623620
624621 if mp .dm1 .useDifferentiableModel and mp .dm2 .useDifferentiableModel :
625622 Vout1 = mp .dm1 .differentiableModel .render_backprop (
@@ -630,11 +627,12 @@ def compact_reverse_gradient(command_vec, mp, EestAll, EFendPrev, log10reg):
630627 else :
631628 raise ValueError ('mp.dm1.useDifferentiableModel and mp.dm2.useDifferentiableModel must be True for AD-EFC.' )
632629
633- Vout1 *= mp .dm1 .VtoH
634- Vout2 *= mp .dm2 .VtoH
630+ Vout1 *= mp .dm1 .VtoH * np . conj ( ~ mp . dm1 . dead_map )
631+ Vout2 *= mp .dm2 .VtoH * np . conj ( ~ mp . dm2 . dead_map )
635632 gradient = np .concatenate ((Vout1 .reshape ([mp .dm1 .NactTotal ])[mp .dm1 .act_ele ],
636633 Vout2 .reshape ([mp .dm2 .NactTotal ])[mp .dm2 .act_ele ]),
637634 axis = None )
635+ gradient += 2 * utu_coef * gradient
638636
639637 return total_cost , gradient
640638
0 commit comments