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69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
function [UVIndicators,out] = UsageViolationPc2D(r1,v1,C1,r2,v2,C2,HBR,params)
% UsageViolationPc2D -  Calculate usage violation indicators for 2D-Pc
%                       conjunction plane method
%
% Synatx: [UVIndicators,out] = UsageViolationPc2D(r1,v1,C1,r2,v2,C2,HBR,Params);
%
% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Inputs:
%
%    r1      - Primary object's position vector in ECI coordinates [m]
%              (1x3 or 3x1 vector)
%    v1      - Primary object's velocity vector in ECI coordinates [m/s]
%              (1x3 or 3x1 vector)
%    C1      - Primary's covariance matrix in ECI coordinate frame [m & s]
%              (6x6)
%    r2      - Secondary object's position vector in ECI coordinates [m]
%              (1x3 or 3x1 vector)
%    v2      - Secondary object's velocity vector in ECI coordinates [m/s]
%              (1x3 or 3x1 vector)
%    C2      - Secondary's cov. matrix in ECI coordinate frame [m & s]
%              (3x3 or 6x6)  
%    HBR     - Hard body radius [m]
%
% =========================================================================
%
% Outputs:
%
%    UVIndicators - Structure with conjunction plane method usage violation
%                   indicators, which indicate that the conjunction is 
%                     - potentially affected by NPD covariance matrices and
%                       the associated NPD remediation
%                     - potentially too extended in time;
%                     - potentially too offset in time from the TCA;
%                     - or, potentially have too inaccurate Pc estimates.
%
%    UVIndicators.NPDIssues - Indicates conjunctions that potentially have 
%                   issues due to detected and remediated non-positive 
%                   definite (NPD) covariance matrices at TCA due to the
%                   following:
%                   1) TCA primary   position covariance is NPD
%                   2) TCA secondary position covariance is NPD
%                   3) TCA relative primary/secondary position cov. is NPD
%      = [false false false] => No NPD position covariances detected
%      = [true  false false] => Primary   3x3 position covariance is NPD
%      = [false true  false] => Secondary 3x3 position covariance is NPD
%      = [false false true ] => TCA relative position covariance  is NPD
%      .
%      .
%      .
%      = [true  true  true ] => All three covariance matrices are NPD
%
%    UVIndicators.Extended - Indicates conjunctions that potentially have 
%                            durations that are too long
%      = NaN  => Non-convergence or algorithm failure
%      ~ 1    => Very bad violation
%      > 2e-2 => Unaccepable violation (recommended cutoff = 0.01 to 0.05)
%
%    UVIndicators.Offset - Indicates conjunctions that potentially have
%                          durations that are too offset from the TCA
%      = NaN  => Non-convergence or algorithm failure
%      ~ 1    => Very bad violation
%      > 1e-2 => Unaccepable violation (recommended cutoff = 0.01)
%
%    UVIndicators.Inaccurate - Indicates conjunctions that potentially have 
%                              inaccurate conjunction plane Pc estimates
%      = NaN  => Non-convergence or algorithm failure
%      ~ 1    => Very bad violation
%      > 2e-2 => Unaccepable violation (recommended cutoff = 0.01 to 0.10)
%
%   out - Auxiliary information
%
%      Nc2DSmallHBR - Nc2D Pc estimate using the small-HBR aproximation
%      Nc2DLoLimit  - Lower limit on actual Nc2D Pc value
%      Nc2DHiLimit  - Upper limit on actual Nc2D Pc value
%      LogPcCorrectionFactor - The log of the 2D-Nc / 2D-Pc ratio, 
%             approximated in the small HBR limit, calculated from three
%             or more time points near the curvilinear encounter peak
%             overlap point time.
%      Notes:
%      *Given a previously calculated rectlinear 2D-Pc
%       value, Pc2D, this factor can be used to roughly estimate
%       the curvilinear encounter 2D-Nc value as follows:
%        Nc2DRough = min(1, exp(log(Pc2D)+LogPcCorrectionFactor))
%      *The LogPcCorrectionFactor provide the basis to estimate the 2D-Pc
%       inaccurate usage violation metric, UVIndicators.Inaccurate
%      *Initial testing indicated that if Nc2DRough < 1e-15, then
%       the actual value will be Nc2D < 1e-10 at a very high 
%       confidence level.
%      *Further testing indicates that using Nc2DHiLimit above is a much
%       better approach than using Nc2DRough
%
% =========================================================================
%
% Initial version: Mar 2023; Latest update: Aug 2025
%
% ----------------- BEGIN CODE -----------------

%% Initializations and defaults

% warning('Using development version of function: UsageViolationPc2DNew');

% Default inputs
Nargin = nargin;
if Nargin < 8; params = []; end

% Default parameters
params = set_default_param(params,'apply_covXcorr_corrections',true);
params = set_default_param(params,'remediate_NPD_TCA_eq_covariances',false);
params = set_default_param(params,'Fclip',1e-4);
params = set_default_param(params,'ConjDurationGamma',1e-16);
params = set_default_param(params,'verbose',0);
params = set_default_param(params,'CPUVdebugging',0);

% Parameters related to curvilinear trajectory analysis
params = set_default_param(params,'FullPc2DViolationAnalysis',true);
params = set_default_param(params,'AcceptableInitOffsetFactor',10);
params = set_default_param(params,'TimeSigmaSpacing',1);
params = set_default_param(params,'TimeSigmaSpacing',1);
params = set_default_param(params,'TimeSigmaDiffLimit',2.5);
params = set_default_param(params,'QfunctionDiffLimit',1.0);
params = set_default_param(params,'MaxRefinements',20);

% Set the default retrograde orbit reorientation mode
%  0 => No retrograde orbit adjustment (causes 2D-Nc and 3D-Nc to fail for
%       retrograde orbits, such as the Alfano 2009 test cases)
%  1 => If either orbits is retrograde, try reorienting the reference 
%       frame axes (recommended)
%  2 => Always try reorienting the ref. frame axes (testing mode)
%  3 => Reorient axes to force primary to be retrograde (testing mode)
params = set_default_param(params,'RetrogradeReorientation',1);

% Initialize outputs to indicate unconverged results
UVIndicators.Inaccurate = NaN;
UVIndicators.NPDIssues  = NaN(1,3);
UVIndicators.Extended   = NaN;
UVIndicators.Offset     = NaN;


% Initialize 2D-Nc estimates available from the usage violation analysis 
out.Nc2DSmallHBR          = NaN;
out.Nc2DLoLimit           = NaN;
out.Nc2DHiLimit           = NaN;
out.LogPcCorrectionFactor = NaN;

% Save parameters in output structure
out.params = params;

% Ensure primary/secondary state vectors are column vectors
r1 = reshape(r1,3,1); v1 = reshape(v1,3,1);
r2 = reshape(r2,3,1); v2 = reshape(v2,3,1);

% Ensure primary/secondary covariances are 6x6
if size(C1,1) ~= 6 || size(C1,2) ~=6
    error('C1 covariance must be a 6x6 matrix');
end
if size(C2,1) ~= 6 || size(C2,2) ~=6
    error('C2 covariance must be a 6x6 matrix');
end

% Process the input HBR
N_HBR = numel(HBR);
if (N_HBR == 2)
    % Process individual primary and secondary HBR values
    %  HBR(1) = Primary   HBR
    %  HBR(2) = Secondary HBR
    if (min([HBR(1) HBR(2)]) < 0)
       error('Both HBR values must be nonnegative.');
    end  
    HBR = HBR(1)+HBR(2);
elseif (N_HBR ~= 1)
    error('Input HBR must have one or two elements.');
end
if (HBR <= 0)
    error('Combined HBR value must be positive.');
end
out.HBR = HBR;
HBRkm = HBR/1e3; % HBR in km units

% Initialize parameters for PeakOverlapPos function
POPPAR.verbose = params.verbose > 0;
POPPAR.Fclip   = params.Fclip;
POPPAR.maxiter = 100;

% Constants
% twopi = 2*pi; I6x6 = eye(6,6);

%% Retrograde orbit processing

% Reorient to handle conjunctions involving retrograde orbits, which occur
% for the Alfano (2009) test cases, but have never been encountered with
% actual CARA conjunctions. This eliminates the occurence of retrograde
% orbits by reorienting the (x,y,z) axes of the reference frame.
if params.RetrogradeReorientation > 0
    [r1,v1,C1,r2,v2,C2,RRout] = ...
        RetrogradeReorientation(r1,v1,C1,r2,v2,C2,params);
    out.RetrogradeReorientation = RRout.Reoriented;
else
    out.RetrogradeReorientation = false;
end

%% Mean equinoctial matrices at nominal TCA for primary and secondary

[out.Xmean10,out.Pmean10,out.Emean10,out.Jmean10,out.Kmean10, ...
    out.Qmean10,out.Qmean10RemStat,out.Qmean10Raw,            ...
    out.Qmean10Rem,out.C1Rem] = EquinoctialMatrices(r1,v1,C1, ...
    params.remediate_NPD_TCA_eq_covariances);

[out.Xmean20,out.Pmean20,out.Emean20,out.Jmean20,out.Kmean20, ...
    out.Qmean20,out.Qmean20RemStat,out.Qmean20Raw,            ...
    out.Qmean20Rem,out.C2Rem] = EquinoctialMatrices(r2,v2,C2, ...
    params.remediate_NPD_TCA_eq_covariances);

if any(isnan(out.Emean10)) || any(isnan(out.Emean20))
    out.RefinementLevel = NaN;
    return;
end

%% Get covariance cross correction (CCC or XC) parameters

if params.apply_covXcorr_corrections
    [XCprocessing,sigp,Gp,sigs,Gs] = get_covXcorr_parameters(params);
    if XCprocessing
        % Product of DCP sigma values
        sigpXsigs = sigp*sigs;
        % DCP 6x1 sensitivity vectors for TCA pos/vel states (km units)
        Gp = Gp' / 1e3; % Primary

        Gs = Gs' / 1e3; % Secondary
        % Reorient the 6x1 sensitivity vectors, if required
        if out.RetrogradeReorientation
            Gp = RRout.M6 * Gp;
            Gs = RRout.M6 * Gs;
        end
        % DCP 6x1 sensitivity vectors for TCA equinoctial states
        GEp = out.Kmean10*Gp;
        GEs = out.Kmean20*Gs;
        % Add to parameters to calculate peak-overlap points effective
        % Mahalanobis distances
        POPPAR.XCprocessing = true;
        POPPAR.sigpXsigs = sigpXsigs; POPPAR.GEp = GEp; POPPAR.GEs = GEs;
    else
        POPPAR.XCprocessing = false;
    end
else
    XCprocessing = false; POPPAR.XCprocessing = false;
end
out.covXcorr_corrections_applied = XCprocessing;

%% Construct the nominal TCA relative state and covariance

% Relative pos and vel
r = r2-r1;
v = v2-v1;

% Apply covariance cross correlation correction to rel. PV state covariance
CRem = out.C1Rem + out.C2Rem;
if XCprocessing
    % Use Casali (2018) eq 11 & convert sensitivity vectors from km to m
    CRem = CRem - sigpXsigs * (Gs*Gp'+Gp*Gs') * 1e6;
end

%% Rectilinear (RL) encounter Mahalanobis distances and Q functions

% Calculate the inverse of A, remediating NPDs with eigenvalue clipping
A = CRem(1:3,1:3);
Lclip = (params.Fclip*HBR)^2;
[Veig,Leig] = eig(A); Leig = diag(Leig); 
RelPosCovNPD = any(Leig <= 0);
RelPosCovRemediation = Leig < Lclip;
Leig(RelPosCovRemediation) = Lclip;
Adet = Leig(1) * Leig(2) * Leig(3);
Ainv = Veig * diag(1./Leig) * Veig';



% Min MD2 and Q function time and value for RL trajectories

rT_Ai = r' * Ainv;
rT_Ai_r = rT_Ai * r;
rT_Ai_v = rT_Ai * v;
vT_Ai_v = v' * Ainv * v;

if rT_Ai_v == 0

    T = 0;

else

    T = -rT_Ai_v / vT_Ai_v;

end

out.TRectilinear = T;



% 1-sigma Q function width in time for rectilinear encounter

SigmaSquaredRL = 1 / vT_Ai_v;

out.STRectilinear = sqrt(SigmaSquaredRL);



% Set up for 3-point UV evaluation

if params.TimeSigmaSpacing <= 0

    error('Invalid TimeSigmaSpacing parameter; recommendation = 1');

else

    if params.TimeSigmaSpacing < 0.5 || params.TimeSigmaSpacing > 2

        warning('Recommend TimeSigmaSpacing parameter of 0.5 to 2');

    end

    % Three point estimation mode (default and recommended)

    dt = params.TimeSigmaSpacing*out.STRectilinear;

    t = [T-dt T T+dt]; MidPoint = 2;

end



% MD2 values for the time points for rectilinear (RL) trajectories

MD2Rectilinear = rT_Ai_r + t * (2*rT_Ai_v) +(t.^2) * vT_Ai_v;



% Log of RL determinant for the time points, accounting for km vs m units

logAdet = log(Adet/1e18);



% Value of RL Q function for the time points

out.tRectilinear = t;

out.QtRectilinear = MD2Rectilinear + logAdet;

out.MDtRectilinear = sqrt(MD2Rectilinear);

out.QTRectilinear = out.QtRectilinear(MidPoint);

out.MDTRectilinear = out.MDtRectilinear(MidPoint);

out.VTRectilinear = norm(v/1e3);



%% Define the indicators for NPD covariance remediation



% Calculate primary and secondary position covariance NPD remediation

[~,Leig] = eig(C1(1:3,1:3)); Leig = diag(Leig);

PriPosCovNPD = any(Leig <= 0);



[~,Leig] = eig(C2(1:3,1:3)); Leig = diag(Leig);

SecPosCovNPD = any(Leig <= 0);



% Define conj. plane method NPDIssues usage violation indicators for

% primary, secondary, and relative position covariances

UVIndicators.NPDIssues = [PriPosCovNPD SecPosCovNPD RelPosCovNPD];



%% Define initial usage violation indicators



% Use rectilinear trajectory results to define initial usage violations.

% Later, if a full curvilinear analysis is performed and converges, these

% will be superceded with final, more accurate values



% Rectilinear-only processing does not represent a converged Q-function

% calculation (Q = Q(t) = modified Mahalanobis distance for curvilinear

% trajectory)

out.Qconverged = false;



% Initialize the min time, sigma, velocity and magnitude associated with

% the minimum modified Maha distance, Q(T) = argmin[Q(t)]

out.TCurvilinear  = NaN;

out.QTCurvilinear = NaN;

out.STCurvilinear = NaN;

out.VTCurvilinear = NaN;



% Calculate the conjunction time bounds in the small HBR limit, using

% the rectilinear analysis information

sqrt2_erfcinv_gamma = sqrt(2)*erfcinv(params.ConjDurationGamma);

dtau = out.STRectilinear * sqrt2_erfcinv_gamma;

out.Ta = out.TRectilinear - dtau;

out.Tb = out.TRectilinear + dtau;



% Calculate the orbital periods of the primary and secondary

out.Period1 = orbit_period(r1,v1);

out.Period2 = orbit_period(r2,v2);

out.PeriodMin = min(out.Period1,out.Period2);



% Long duration indicator

UVIndicators.Extended = min(1,2*dtau/out.PeriodMin);



% Offset duration indicator

Tab = max(abs([out.Ta out.Tb]));

UVIndicators.Offset = min(1,Tab/out.PeriodMin);



%% Handle special request not to perform full usage violation analysis



% If full analysis is not requested, then base 2D-Pc method usage

% violations only on rectilinear trajectory effects. This approach is not

% recommended, because it is not as relaible as a full analysis, and it

% does not provide information on 2D-Pc method inaccuracies, or an

% estimated correction factor to account for 2D-Pc inaccuracies



if ~params.FullPc2DViolationAnalysis

    % Return now, because remainder of function is devoted

    % analysis of curvilinear trajectory effects

    out.RefinementLevel = -2;

    return;

end



%% Handle special case of effectively zero relative velocity



if isinf(out.STRectilinear)

    % Return now, because remainder of function is devoted

    % analysis of curvilinear trajectory effects

    out.RefinementLevel = -1;

    return;

end



%% Curvilinear encounter Mahalanobis distances and Q functions



% if params.CPUVdebugging

%     POPPAR.verbose = 1;

% end



% Initialize curvilinear refinement level

out.RefinementLevel = 0;



% Calculate POP Q-function (i.e., modified MD2) value at the three times

out.tCurvilinear  = t;

out.QtCurvilinear = NaN(size(t));

for nt=1:3

    [QnewCL,XXu,PPs,~,~,~] = PeakOverlapMD2(t(nt), ...

        0,out.Emean10,out.Qmean10, ...

        0,out.Emean20,out.Qmean20, ...

        HBRkm,1,POPPAR);

    out.QtCurvilinear(nt) = QnewCL;

    if nt == MidPoint

        Xu = XXu; Ps = PPs;

    end

end



% Check for curvilinear analysis convergence

Qconverged = ~any(isnan(out.QtCurvilinear));



if params.CPUVdebugging

    disp(['Before refinement Qconverged = ' num2str(Qconverged) ]);

    disp([' tCL = ' num2str(out.tCurvilinear,'%0.16e ')]);
    disp(['QtCL = ' num2str(out.QtCurvilinear,'%0.16e ')]);
    disp(['X10 = ' num2str([r1' v1'],'%0.16e ')]);
    disp(['X20 = ' num2str([r2' v2'],'%0.16e ')]);
    disp(['E10 = ' num2str(out.Emean10','%0.16e ')]);
    disp(['E20 = ' num2str(out.Emean20','%0.16e ')]);
    keyboard;
end

%% Process converged curvilinear estimates, and refine if necessary

% Calculate derivatives of curvilinear Q curve, to estimate the min time
% and value
if Qconverged
    
    % Set refinement level to one, which will be the case for most
    % conjunctions, as most do not need any further refinement
    out.RefinementLevel = 1;
    
    % Numerical second derivative of Q(t) curve
    Qdotdot = (out.QtCurvilinear(3)-2*out.QtCurvilinear(2) ...
              +out.QtCurvilinear(1))/(dt^2);
          
    % Register a usage usage violation if Qdotdot is not positive
    if Qdotdot <= 0
        
        % If Qdotdot is not positive, make the sigma value infinite
        out.STCurvilinear = Inf;
        
    else

        % Calculate best-fit parabola
        [abc,~,~,rankTPF] = ...
            TimeParabolaFit(out.tCurvilinear, ...
                            out.QtCurvilinear);
    
        % Min parabola time and value, calculated using the relationships:
        %  Qdotdot(T) = abc(1)/2;
        %  Qdot(T) = abc(2);
        %  Q(T) = abc(3)-abc(2)^2/abc(1)/4;
        t0 = -abc(2)/Qdotdot;
        Qt0 = abc(3)-abc(2)^2/abc(1)/4;

        if params.CPUVdebugging > 2
            tmod = linspace(min([out.tCurvilinear t0]),max([out.tCurvilinear t0]),100);
            Qmod = abc(1)*tmod.^2 + abc(2)*tmod + abc(3);
            plot(tmod,Qmod,'.-k');
            hold on;
            plot(out.tCurvilinear,out.QtCurvilinear,'m*');
            plot(t0,Qt0,'vr');
            hold off;
        end
        
        % Check if the initial time offset is acceptable, or too large
        AcceptableInitOffset = true;
        delt0max = params.AcceptableInitOffsetFactor * ...
            (out.tCurvilinear(3)-out.tCurvilinear(1));
        if t0 < out.tCurvilinear(1)-delt0max
            AcceptableInitOffset = false;
            t0 = out.tCurvilinear(1)-delt0max;
            Qt0 = abc(1)*t0^2 + abc(2)*t0 + abc(3);
        elseif t0 > out.tCurvilinear(3)+delt0max
            AcceptableInitOffset = false;
            t0 = out.tCurvilinear(3)+delt0max;
            Qt0 = abc(1)*t0^2 + abc(2)*t0 + abc(3);
        end
        
        if params.CPUVdebugging > 2
            if ~AcceptableInitOffset
                hold on; plot(t0,Qt0,'sb'); hold off;
            end
            keyboard;
        end
        
        % Store the current estimate Q function value and associated sigma
        out.TCurvilinear = t0;
        out.QTCurvilinear = Qt0;
        SigmaSquaredCL = 2 / Qdotdot;
        out.STCurvilinear = sqrt(SigmaSquaredCL);

        % Relative velocity estimate
        out.VTCurvilinear = norm(Xu(4:6));
        
        % Estimate the log of the 2D-Nc / 2D-Pc ratio,
        % in the small HBR limit
        out.LogPcCorrectionFactor = ...
            log(out.STCurvilinear/out.STRectilinear) ...
            + log(out.VTCurvilinear/out.VTRectilinear) ...
            + (out.QTRectilinear-out.QTCurvilinear)/2;
                
        % Check if refinement is required, based on the difference between
        % the min-Q times and values for the rectilinear and approximated
        % curvilinear cases
        if AcceptableInitOffset
            TimeDifference = out.TCurvilinear - out.TRectilinear;
            MaxTimeDifference = params.TimeSigmaDiffLimit ...
                              * min(out.STRectilinear,out.STCurvilinear);
            QDifference = out.QTCurvilinear - out.QTRectilinear;
            NeedsRefinement = abs(TimeDifference) >= MaxTimeDifference ...
                            | abs(QDifference)    >= params.QfunctionDiffLimit;
            if params.CPUVdebugging
                disp(['Tdf = ' num2str(TimeDifference/MaxTimeDifference) ...
                     ' Qdf = ' num2str(QDifference/params.QfunctionDiffLimit)]);
            end
        else
            NeedsRefinement = true;
        end
                    
        if params.CPUVdebugging
            disp(['TCL = ' num2str(out.TCurvilinear) ...
                 ' QCL = ' num2str(out.QTCurvilinear)]);
        end
        
        % Refine until not required
        while NeedsRefinement

            % Increase refinement level
            out.RefinementLevel = out.RefinementLevel + 1;
            
            % Current min Q time point
            [~,ItMinimum] = min(out.QtCurvilinear);
            
            % Refine by calculating actual Q value at the current estimate
            % of the min-Q time point
            tnewCurvilinear = out.TCurvilinear;
            [QnewCurvilinear,Xu,Ps,~,~,~] = PeakOverlapMD2( ...
                tnewCurvilinear, ...
                0,out.Emean10,out.Qmean10, ...
                0,out.Emean20,out.Qmean20, ...
                HBRkm,1,POPPAR);
            
            % Evaluate if refinement needs to be continued
            if isnan(QnewCurvilinear)
                
                % Peak Overlap Point (POP) algorithm did not converge for
                % new time point
                Qconverged = false;
                
                if params.CPUVdebugging
                    disp(['POP did not converge for refinement # ' ...
                        num2str(out.RefinementLevel)]);
                end

                % Bisect unbounded interval on either side of current time
                % points, seeking a time point when the POP algorithm
                % converges
                min_tCurvilinear = min(out.tCurvilinear);
                max_tCurvilinear = max(out.tCurvilinear);
                if out.TCurvilinear < min_tCurvilinear
                    % Bisect low time interval
                    tnew = (out.TCurvilinear + min_tCurvilinear) / 2;
                    if params.CPUVdebugging
                        disp(' Bisecting low-side time interval');
                    end
                elseif out.TCurvilinear > max_tCurvilinear
                    % Bisect high time interval
                    tnew = (out.TCurvilinear + max_tCurvilinear) / 2;
                    if params.CPUVdebugging
                        disp(' Bisecting high-side time interval');
                    end
                else
                    % Time point already bounded
                    tnew = NaN;
                end
                
                % Check if more refinement is required
                if isnan(tnew) || numel(out.tCurvilinear) > 3
                    % Unconverged result
                    NeedsRefinement = false;
                else
                    if out.RefinementLevel < params.MaxRefinements
                        % Another refinement is needed
                        NeedsRefinement = true;
                        % New estimate for min-Q time and value for the
                        % next refinement
                        out.TCurvilinear  = tnew;
                        out.QTCurvilinear = NaN;
                    else
                        % Too many refinements have been tried
                        NeedsRefinement = false;
                    end
                end
                
                if params.CPUVdebugging > 1

                    tcomb = [out.TCurvilinear out.tCurvilinear];
                    tdelt = 3*dt;
                    tt = linspace(min(tcomb)-tdelt,max(tcomb)+tdelt,1000);
                    QRL = rT_Ai_r + tt*(2*rT_Ai_v) + (tt.^2)*(vT_Ai_v) + logAdet;
                    QCL = NaN(size(tt));
                    for n=1:numel(tt)
                        [QnewCL,~,~,~,~,~] =  PeakOverlapMD2(tt(n), ...
                            0,out.Emean10,out.Qmean10, ...
                            0,out.Emean20,out.Qmean20, ...
                            HBRkm,1,POPPAR);
                        QCL(n) = QnewCL;
                    end
                    figure(1); clf;
                    plot(tt,QRL,'-c','LineWidth',2);
                    hold on;
                    plot(tt,QCL,'--m','LineWidth',1);
                    [~,imin] = min(QCL);
                    plot(tt(imin),QCL(imin),'*g');
                    plot(out.tRectilinear,out.QtRectilinear,'sg');
                    plot(out.tCurvilinear,out.QtCurvilinear,'*r');
                    yrng = ylim;
                    plot([tnewCurvilinear tnewCurvilinear],yrng,'--r');
                    plot([out.TCurvilinear out.TCurvilinear ],yrng,':g');
                    hold off;
                    keyboard; 

                    % plot(out.tCurvilinear,out.QtCurvilinear,'xk');
                    % hold on;
                    % yrng = ylim;
                    % plot([tnewCurvilinear tnewCurvilinear],yrng,'--r');
                    % plot([out.TCurvilinear out.TCurvilinear ],yrng,':g');
                    % hold off;
                    % keyboard;
                end

            else
                
                % Peak Overlap Point (POP) algorithm did converge for new
                % time point
                Qconverged = true;

                % Check to see if latest Q point is part of a decreasing
                % trend
                Qsrt = sort(out.QtCurvilinear);
                Qcut = Qsrt(3);
                Qdecreasing = QnewCurvilinear < Qcut;
                
                % Add the latest converged Q point to the buffer
                out.tCurvilinear  = [out.tCurvilinear  tnewCurvilinear];
                out.QtCurvilinear = [out.QtCurvilinear QnewCurvilinear];

                % Only accept latest refinement if min-Q decreases
                if Qdecreasing
                    
                    % Refined relative velocity estimate
                    out.VTCurvilinear = norm(Xu(4:6));
                    
                    % Calculate best-fit parabola using only (t,Q) points
                    % nearest the min Q point
                    [yy,tinc,Qinc,rankAmat] = ...
                        TimeParabolaFit(out.tCurvilinear, ...
                                        out.QtCurvilinear);
                    
                    % Calculate refined Q-min parameters using the
                    % following relationships:
                    %  Qdotdot(T) = yy(1)/2; Qdot(T) = yy(2); Q(T) = yy(3);
                    if yy(1) <= 0 % i.e., Qdotdot < 0
                        % If Qdotdot is not positive, make sigma infinite
                        out.STCurvilinear = Inf;
                    else
                        % Calculate refined quantities
                        out.TCurvilinear = -yy(2)/yy(1)/2;
                        out.QTCurvilinear = yy(3)-yy(2)^2/yy(1)/4;
                        SigmaSquaredCL = 1/yy(1);
                        out.STCurvilinear = sqrt(SigmaSquaredCL);
                    end
                    
                else

                    if params.CPUVdebugging
                        disp(['Q did not decrease: ' ...
                            num2str(QnewCurvilinear) ' ' ...
                            num2str(out.QtCurvilinear(ItMinimum)) ' ' ...
                            num2str(QnewCurvilinear-out.QtCurvilinear(ItMinimum))]);
                    end
                    
                    % Bisect interval for points outside of original range.
                    % Otherwise, adopt current min-Q point
                    
                    NtC = numel(out.tCurvilinear)-1;
                    min_tCurvilinear = min(out.tCurvilinear(1:NtC));
                    max_tCurvilinear = max(out.tCurvilinear(1:NtC));
                    
                    if out.TCurvilinear < min_tCurvilinear
                        % Bisect low time interval
                        tnew = ...
                            (out.TCurvilinear + min_tCurvilinear) / 2;
                        [yy,tinc,Qinc] = TimeParabolaFit( ...
                            out.tCurvilinear,out.QtCurvilinear);
                        Qnew = yy(1) + tnew*(yy(2)+tnew*yy(3));
                        if params.CPUVdebugging
                            disp(' Bisecting low-side time interval');
                        end
                    elseif out.TCurvilinear > max_tCurvilinear
                        % Bisect high time interval
                        tnew = ...
                            (out.TCurvilinear + max_tCurvilinear) / 2;
                        [yy,tinc,Qinc] = TimeParabolaFit( ...
                            out.tCurvilinear,out.QtCurvilinear);
                        Qnew = yy(1) + tnew*(yy(2)+tnew*yy(3));
                        if params.CPUVdebugging
                            disp(' Bisecting high-side time interval');
                        end
                    else
                        % Use current min point, which ends the iterations
                        tnew = out.tCurvilinear( ItMinimum);
                        Qnew = out.QtCurvilinear(ItMinimum);
                    end

                    % New estimate for min-Q time and value
                    out.TCurvilinear  = tnew;
                    out.QTCurvilinear = Qnew;
                    
                end
                
                % Calculate the log of the 2D-Nc / 2D-Pc ratio in the small
                % HBR limit, using the refined curvilinear quantities
                if ~isinf(out.STCurvilinear)
                    out.LogPcCorrectionFactor = ...
                          log(out.STCurvilinear/out.STRectilinear) ...
                        + log(out.VTCurvilinear/out.VTRectilinear) ...
                        + (out.QTRectilinear-out.QTCurvilinear)/2;
                end

                % Check if more refinement is required
                NeedsRefinement = false;
                if ~isinf(out.STCurvilinear)
                    if out.RefinementLevel < params.MaxRefinements
                        TimeDifference = out.TCurvilinear - out.tCurvilinear(ItMinimum);
                        MaxTimeDifference = params.TimeSigmaDiffLimit ...
                                          * out.STCurvilinear;
                        QDifference = out.QTCurvilinear - out.QtCurvilinear(ItMinimum);
                        NeedsRefinement = ...
                            abs(TimeDifference) >= MaxTimeDifference ...
                          | abs(QDifference)    >= params.QfunctionDiffLimit;
                        if params.CPUVdebugging
                            disp(['NRe = ' num2str(out.RefinementLevel)]);
                            disp(['TCL = ' num2str(out.TCurvilinear) ...
                                 ' QCL = ' num2str(out.QTCurvilinear) ...
                                 ' SCL = ' num2str(out.STCurvilinear)]);
                            disp(['Tdf = ' num2str(TimeDifference/MaxTimeDifference) ...
                                 ' Qdf = ' num2str(QDifference/params.QfunctionDiffLimit)]);
                            disp(['NeedsRefinement = ' num2str(NeedsRefinement)]);
                            format longe;
                            disp([out.tCurvilinear' out.QtCurvilinear']);
                            if params.CPUVdebugging > 1
                                if Qconverged
                                    tcomb = [out.TCurvilinear out.tCurvilinear];
                                    tdelt = 3*dt;
                                    tt = linspace(min(tcomb)-tdelt,max(tcomb)+tdelt,1000);
                                    QRL = rT_Ai_r + tt*(2*rT_Ai_v) + (tt.^2)*(vT_Ai_v) + logAdet;
                                    QCL = NaN(size(tt)); MD2CL = QCL;
                                    for n=1:numel(tt)
                                        [QnewCL,~,~,Asdet,~,~] ...
                                            =  PeakOverlapMD2(tt(n), ...
                                            0,out.Emean10,out.Qmean10, ...
                                            0,out.Emean20,out.Qmean20, ...
                                            HBRkm,1,POPPAR);
                                        QCL(n) = QnewCL;
                                        MD2CL(n) = QnewCL - log(Asdet);
                                    end
                                    figure(1); clf;
                                    plot(tt,QRL,'--c','LineWidth',2);
                                    hold on;
                                    plot(tt,QCL,'-m','LineWidth',1);
                                    [~,imin] = min(QCL);
                                    plot(tt(imin),QCL(imin),'*g');
                                    plot(out.tRectilinear,out.QtRectilinear,'sg');
                                    plot(out.tCurvilinear,out.QtCurvilinear,'*r');
                                    if exist('tinc','var')
                                        plot(tinc,Qinc,'or');
                                    end
                                    plot(out.TCurvilinear,out.QTCurvilinear,'ob');
                                    hold off;
                                end
                                keyboard;
                            end
                        end
                    end
                end
                
            end
            
        end
    end
end

% Mark as unconverged for undefined final valuea of curvilinear time sigma
if isinf(out.STCurvilinear) || isnan(out.STCurvilinear)
    Qconverged = false;
end

% Calculate exact value for the Q function at time = T, i.e.,
% the time of minimimum modified Maha. distance
if Qconverged
    % Calculate the new min Q value, Q(T)
    tnewCurvilinear = out.TCurvilinear;
    [QnewCurvilinear,Xu,~,Asdet,Asinv,POPconv,AsAux] = PeakOverlapMD2( ...
        tnewCurvilinear, ...
        0,out.Emean10,out.Qmean10, ...
        0,out.Emean20,out.Qmean20, ...
        HBRkm,1,POPPAR);
    if ~POPconv
        Qconverged = false;
    else
        % Update value of Q(T), the minimimum modified Maha. distance
        out.QTCurvilinear = QnewCurvilinear;
        % Update the log of the 2D-Nc / 2D-Pc correction ratio in the small
        % HBR limit, using the refined curvilinear quantities. This
        % estimate neglects velocity uncertainties and position-velocity
        % correlations.
        out.LogPcCorrectionFactor = ...
              log(out.STCurvilinear/out.STRectilinear) ...
            + log(out.VTCurvilinear/out.VTRectilinear) ...
            + (out.QTRectilinear-out.QTCurvilinear)/2;
        % Also calculate small-HBR limit of Nc2D value, and Nc2D high and
        % low limiting values, using the refined curvilinear quantities.
        % These estimates also neglect vel. uncertainties and pos-vel
        % correlations.
        HBR2 = HBRkm^2;
        Nc2DCoef = HBR2 * ...
                   out.VTCurvilinear *  out.STCurvilinear / 2;
        out.Nc2DSmallHBR = min(1, Nc2DCoef * exp(-out.QTCurvilinear/2));
        % Min and max Q values on surface of collision sphere
        Asiru = Asinv*Xu(1:3);
        Aterm = 2*HBRkm*sqrt(Asiru'*Asiru);

        QTmax = HBR2/min(AsAux.AsLeig) + Aterm + out.QTCurvilinear;

        out.Nc2DLoLimit = min(1, Nc2DCoef * exp(-QTmax/2));

        logAsdet = log(Asdet);

        MD2Curvilinear = out.QTCurvilinear - logAsdet;

        MD2min = HBR2/max(AsAux.AsLeig) - Aterm + MD2Curvilinear;

        MD2min = max(0,MD2min);

        QTmin = MD2min + logAsdet;

        out.Nc2DHiLimit = min(1, Nc2DCoef * exp(-QTmin/2));

    end

end



% Make a plot of the results (used for debugging)

if params.CPUVdebugging && Qconverged

    tcomb = [out.TCurvilinear out.tCurvilinear];

    tdelt = 3*dt;

    tt = linspace(min(tcomb)-tdelt,max(tcomb)+tdelt,1000);

    QRL = rT_Ai_r + tt*(2*rT_Ai_v) + (tt.^2)*(vT_Ai_v) + logAdet;

    QCL = NaN(size(tt));

    for n=1:numel(tt)

        [QnewCL,~,~,~,~,~] =  PeakOverlapMD2(tt(n), ...

            0,out.Emean10,out.Qmean10, ...

            0,out.Emean20,out.Qmean20, ...

            HBRkm,1,POPPAR);

        QCL(n) = QnewCL;

    end

    figure(1); clf;

    plot(tt,QRL,'--c','LineWidth',2);

    hold on;

    plot(tt,QCL,'-m','LineWidth',1);

    [~,imin] = min(QCL);

    plot(tt(imin),QCL(imin),'*g');

    plot(out.tRectilinear,out.QtRectilinear,'sg');

    plot(out.tCurvilinear,out.QtCurvilinear,'*r');

    if exist('tinc','var')

        plot(tinc,Qinc,'or');

    end

    plot(out.TCurvilinear,out.QTCurvilinear,'ob');

    hold off;

    keyboard;

end



%% Calculate the Pc2D usage violation indicator, if converged



out.Qconverged = Qconverged;



if Qconverged



    % Curvilinear analysis converged sufficiently to comment on conj. plane

    % method usage violations (UVs)



    % Check for very long duration conjunction, which represents an

    % extended conjunction UV

    if isinf(out.STCurvilinear)

        % Qdotdot non-positive indicates downward curvature in Q curve, 

        % which represents an extended conjunction UV

        UVIndicators.Extended = 1;

    else

        % Calculate the conjunction time bounds in the small HBR limit,

        % using the curvilinear analysis information

        dtau = out.STCurvilinear * sqrt2_erfcinv_gamma;

        out.Ta = out.TCurvilinear - dtau;

        out.Tb = out.TCurvilinear + dtau;

        % Calculate the orbital periods of the primary and secondary

        out.Period1 = orbit_period(r1,v1);

        out.Period2 = orbit_period(r2,v2);

        out.PeriodMin = min(out.Period1,out.Period2);

        % Long duration indicator

        UVIndicators.Extended = min(1,(out.Tb-out.Ta)/out.PeriodMin);

        % Offset duration indicator

        Tab = max(abs([out.Ta out.Tb]));

        UVIndicators.Offset = min(1,Tab/out.PeriodMin);        

        % Potentical inaccuracy indicator based on LogPcCorrectionFactor

        UVIndicators.Inaccurate = 1-exp(-abs(out.LogPcCorrectionFactor));    

    end

    

end



return

end



% ----------------- END OF CODE ------------------

%

% Please record any changes to the software in the change history 

% shown below:

%

% ----------------- CHANGE HISTORY ------------------

% Developer      |    Date     |     Description

% ---------------------------------------------------

% D. Hall        | 2023-Mar-19 | Initial Development

% D. Hall        | 2023-Aug-07 | Added support for retrograde orbits.

% D. Hall        | 2024-Feb-23 | Initialized several outputs to default

%                                values.

% D. Hall        | 2025-Jan-05 | Applied fixes for various usage violation

%                                special cases.

% L. Baars       | 2025-Aug-06 | Updated documentation for public release.



% =========================================================================

%

% Copyright (c) 2023-2025 United States Government as represented by the

% Administrator of the National Aeronautics and Space Administration.

% All Rights Reserved.

%

% =========================================================================