-
Notifications
You must be signed in to change notification settings - Fork 2
/
Project.m
1796 lines (1497 loc) · 69 KB
/
Project.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%# Project is a class for Optimizing Interplanetary Trajectory Missions as specifed by user
classdef Project
%#
%# PROPERTIES:
%#
%#
%# name; : Name of Project
%# BSP; : Filename for Binary SPK file from JPL
%# Body_List; : List of Bodies Available to Choose from
%# NBody_List; : Number of Bodies Available to Choose from
%# Max_NBody = 20; : Maximum Number of Bodies allowed for Optimizer
%# Min_Spice_Time; : Lower Limit of Spice Kernel Range
%# Max_Spice_Time; : Upper Limit of Spice Kernel Range
%# Min_Spice_Select; : Selected Values of Min_Spice_Time
%# Max_Spice_Select; : Selected Values of Max_Spice_Time
%# Body_Select; : List of Selected Bodies
%# Body_Chosen; : List of Chosen Bodies for Optimization
%# Body_Number; : Number of Selected Body
%# Current_Mission; : Current Mission Selected bu User
%# Global_Solution; : Result of Running Global Optimizer
%# Local_Solution; : Result of Running Local Optimzer
%# Solution; : Result of Last run of Optimizer
%# Run_Time = 60; : Maximum Optimizer Run Time (Default 60secs)
%# Nconstraints; : Number of Periapsis Constraints
%# NPerihelia; : Number of Perihelia Constraints
%# Per_Pointer; : Pointer to Array of Perihelia Values
%# Constr_Tol; : Array of Constraint Tolerances
%# MAX_DURATION=1e50; : Upper Limit for Max_Duration
%# Max_Duration=1e50; : Maximum Duration Of Entire Mission (optional)
%# factor = 0.5; : Factor used for Local Optimizer
%# AU = 149597870700; : Astronomical Unit in Metres
%# Min_time; : Array of Minimum Times for Optimization
%# Max_time; : Array of Maximum Times for Optimization
%# Min_Per; : Array of Minimum Periapsis for each Body
%# Max_dV; : Array of Maximum Allowable DeltaV's for each Body
%# Perihelia; : Array of Minimum Perihelia for each Transfer
%# Perihelia_flag=0; : Flag to indicate if Perihelia Constraints are Orbital Parameters or achieved
%# AngleConstraint; : For Intermediate Points. 2D Array of Minimum and Maximum Longitudes and Latitudes
%#
%# METHODS:
%#
%# Initialize_SPICE : Initializes SPICE Toolkit and Opens Leap Second File naif0012.tls
%# Get_SPICE_List : Opens and extracts BINARY SPICE KERNEL Files .BSP
%# Merge_Body_Data : Merges Data on the Planets with Data extracted from Get_SPICE_List
%# Add_Intermediate_Point : Adds an INTERMEDIATE POINT to the Possible Bodies to Select From
%# Add_Fixed_Point : Adds a FIXED POINT to the Possible Bodies to Select From
%# Optimize_Mission : Optimizes Mission provided by Current_Mission -> Solution goes to Solution
%# Compute_DeltaV_NLopt : Calculates DeltaV as determined by times input by Optimize_Mission
%# Update_Traj : Updates current Trajectory and calculates all the constraints based on the times and thetas and thi's
%# Per_NLopt : Calculates Periapsis Constraints for Optimize_Mission
%# dV_NLopt : Calculates DeltaV Constraints for Optimize_Mission
%# Perhel : Calculates Perihelion Constraints for Optimize_Mission
%# Overall_Duration : Calculates Overall Mission Duration Constraints for Optimize_Mission
%# View_Results : Displays Interplanetary Trajectory in 2D & 3D Plot form as well as orbits
%# : of SSO's. Also Displays Speed Against Radial Distance Plot of Trajectory
%# View_Info : Display Basic Info in numeric form about the encounter with each SSO in turn
%# View_Planetary_Encounters: Displays Plots of Distance Against Time for Each SSO Encounter as well
%# : As 3D Trajectory for each SSO Encounter
%# View_DeltaV_Vs_Time : Displays Plots of DeltaV Against Time
%# View_Orbit_Info : Displays Orbital Information in numeric form
%# Animate_Results : Animates Interplanetary Trajectory
%#
properties
name; % Name of Project
BSP; % Filename for Binary SPK file from JPL
Body_List; % List of Bodies Available to Choose from
NBody_List; % Number of Bodies Available to Choose from
Max_NBody = 20; % Maximum Number of Bodies allowed for Optimizer
Min_Spice_Time; % Lower Limit of Spice Kernel Range
Max_Spice_Time; % Upper Limit of Spice Kernel Range
Min_Spice_Select; % Selected Values of Min_Spice_Time
Max_Spice_Select; % Selected Values of Max_Spice_Time
Body_Select; % List of Selected Bodies
Body_Chosen; % List of Chosen Bodies for Optimization
Body_Number; % Number of Selected Body
Current_Mission; % Current Mission Selected bu User
Global_Solution; % Result of Running Global Optimizer
Local_Solution; % Result of Running Local Optimzer
Solution; % Result of Last run of Optimizer
Run_Time = 60; % Maximum Optimizer Run Time (Default 60secs)
Nconstraints; % Number of Periapsis Constraints
NPerihelia; % Number of Perihelia Constraints
Per_Pointer; % Pointer to Array of Perihelia Values
Constr_Tol; % Array of Constraint Tolerances
MAX_DURATION=1e50; % Upper Limit for Max_Duration
Max_Duration=1e50; % Maximum Duration Of Entire Mission (optional)
factor = 0.5; % Factor used for Local Optimizer
AU = 149597870700; % Astronomical Unit in Metres
Min_time; % Array of Minimum Times for Optimization
Max_time; % Array of Maximum Times for Optimization
Min_Per; % Array of Minimum Periapsis for each Body
Max_dV; % Array of Maximum Allowable DeltaV for each Body
Con_TI; % Array of Minimum/Max Intercept Times for each Body
Perihelia; % Array of Minimum Perihelia for each Transfer
Perihelia_flag=0; % Flag to indicate if Perihelia Constraints are Orbital Parameters or achieved
Orbit_flag=0; % Flag to Indicate if Target is an orbit rather than a particular body.
Min_TI_flag=0; % Flag to Indicated that Con_TI (Constraint on Intercept Time at nth body) is a Minimum (2^n=1)
AngleConstraint; % For Intermediate Points. (Array of Minimum and Maximum Longitudes and Latitudes.)
end
methods
function obj = Initialize_SPICE(obj)
%# Initialize_SPICE : Initializes SPICE Toolkit and Opens Leap Second File naif0012.tls
% Initialise Various SPICE files
addpath('thirdparty/SPICE/');
addpath('thirdparty/SPICE/mice/mice/src/mice');
addpath('thirdparty/SPICE/mice/mice/lib');
cspice_tkvrsn('toolkit');
cspice_furnsh('thirdparty/SPICE/naif0012.tls');
end
function obj = Get_SPICE_List(obj, SPK)
%# Get_SPICE_List : Opens and extracts BINARY SPICE KERNEL Files .BSP
MAXIV = 1000;
WINSIZ = 2 * MAXIV;
% Initialise Kernel
cspice_furnsh(SPK);
%
% Find the set of objects in the SPK file.
%
ids = cspice_spkobj( SPK, MAXIV );
BODYLISTN = Body(numel(ids));
%
% We want to display the coverage for each object. Loop over
% the contents of the ID code set, find the coverage for
% each item in the set, and display the coverage.
%
for i=1:numel(ids)
%
% Extract the coverage data for object 'ids(i)'.
%
cover = cspice_spkcov( SPK, ids(i), WINSIZ );
%
% Display a simple banner.
%
fprintf( '========================================\n')
fprintf( 'Coverage for object %d\n', ids(i) )
%
% Convert the endpoints to TDB calendar format time strings
% and display them. Pass the endpoints in an array,
% so cspice_timout returns an array of time strings.
%
% Recall a vectorized input has dimension 1xM so transpose
% the 'cover' slice.
%
timstr = cspice_timout( cover(1:2)', ...
'YYYY MON DD HR:MN:SC.### (TDB) ::TDB' );
fprintf(' Start: %s\n' , timstr(1,:) )
fprintf(' Stop: %s\n\n', timstr(2,:) )
BODYLISTN(i).ID=sprintf('%d', ids(i));
BODYLISTN(i).name = cspice_bodc2n(ids(i));
if(BODYLISTN(i).name=="")
BODYLISTN(i).name=BODYLISTN(i).ID;
end
BODYLISTN(i).radius=0;
BODYLISTN(i).mu=0.0;
obj.Min_Spice_Time(i+obj.NBody_List) = cspice_str2et(timstr(1,:));
obj.Max_Spice_Time(i+obj.NBody_List) = cspice_str2et(timstr(2,:));
end
if (obj.NBody_List==0)
obj.Body_List = BODYLISTN;
obj.NBody_List = numel(BODYLISTN);
else
obj.Body_List = cat (2, obj.Body_List, BODYLISTN);
obj.NBody_List = numel(obj.Body_List);
end
end
function obj = Merge_Body_Data( obj )
%# Merge_Body_Data : Merges Data on the Planets with Data extracted from Get_SPICE_List
PLANET_NAMES = {"MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE", "PLUTO"};
PLANET_MU = 6.67259e-11*[ 0.3302e24 4.869e24 5.9736e24 0.6419e24 1898.6e24 568.46e24 86.83e24 102.43e24 0.0125e24];
PLANET_RADIUS= [2440e3 6052e3 6378135 3397e3 71492e3 60268e3 25559e3 24766e3 1137e3];
for i=1:obj.NBody_List
for j=1:9
if(~isempty(strfind(obj.Body_List(i).name, PLANET_NAMES{j})))
obj.Body_List(i).radius=PLANET_RADIUS(j);
obj.Body_List(i).mu=PLANET_MU(j);
break;
end
end
if (strcmp(obj.Body_List(i).ID,'3788040'))
obj.Body_List(i).name="OUMUAMUA"; % Make sure OUMUAMUA has a name!
end
end
end
function obj= Add_Intermediate_Point( obj)
%# Add_Intermediate_Point : Adds an INTERMEDIATE POINT to be Optimized to the Possible Bodies to Select From
Joker = Body;
Joker.ID = 'INTERMEDIATE POINT';
Joker.name = "INTERMEDIATE POINT";
Joker.radius = 0;
Joker.Fixed_Point = 1;
Joker.ephem0.r = [ obj.AU 0 0 ];
Joker.ephem0.v = [ 0 0 0 ];
Joker.ephem0.t = 0;
Joker.ephemt=Joker.ephem0;
spice_min= -1e50;
spice_max= 1e50;
obj.Body_List = cat(2, obj.Body_List, Joker);
obj.NBody_List = numel(obj.Body_List);
obj.Min_Spice_Time(obj.NBody_List) = spice_min;
obj.Max_Spice_Time(obj.NBody_List) = spice_max;
end
function obj= Add_Fixed_Point( obj)
%# Add_Fixed_Point : Adds a FIXED POINT to the Possible Bodies to Select From
Joker2 = Body;
Joker2.ID = 'FIXED POINT';
Joker2.name = "FIXED POINT";
Joker2.radius = 0;
Joker2.Fixed_Point = 2;
Joker2.ephem0.r = [ obj.AU 0 0 ];
Joker2.ephem0.v = [ 0 0 0 ];
Joker2.ephem0.t = 0;
spice_min= -1e50;
spice_max= 1e50;
obj.Body_List = cat(2, obj.Body_List, Joker2);
obj.NBody_List = numel(obj.Body_List);
obj.Min_Spice_Time(obj.NBody_List) = spice_min;
obj.Max_Spice_Time(obj.NBody_List) = spice_max;
end
function obj = Add_Custom_Body( obj)
Custom = Body;
Custom.ID = 'CUSTOM BODY';
Custom.name = "CUSTOM BODY";
Custom.radius=0;
spice_min= -1e50;
spice_max= 1e50;
obj.Body_List = cat(2, obj.Body_List, Custom);
obj.NBody_List = numel(obj.Body_List);
obj.Min_Spice_Time(obj.NBody_List) = spice_min;
obj.Max_Spice_Time(obj.NBody_List) = spice_max;
end
function obj = Optimize_Mission(obj, DVF)
%# Optimize_Mission : Optimizes Mission provided by Current_Mission -> Solution goes to Solution
% Set up Inputs To Optimizer
obj.Solution=obj.Current_Mission;
% Calculate number of Intermediate Points in Body_Set
NIP =0;
for i1=1:obj.Solution.Trajectory.Nbody
if (obj.Solution.Trajectory.Body_Set(i1).Fixed_Point==1)
NIP=NIP+1;
end
end
% Determine whether there are any target Orbits
NOR=0;
obj.Orbit_flag=0;
for i1=1:obj.Solution.Trajectory.Nbody
if contains(obj.Solution.Trajectory.Body_Set(i1).name,"ORBIT",'IgnoreCase',true)
NOR=NOR+1;
obj.Orbit_flag=obj.Orbit_flag+2^i1;
if obj.Solution.Trajectory.Body_Set(i1).Fixed_Point>=0
obj.Solution.Trajectory.Body_Set(i1)=obj.Solution.Trajectory.Body_Set(i1).compute_ephem_at_t(obj.Solution.Absolute_Times(i1),2,1e-4);
obj.Solution.Trajectory.Body_Set(i1)=obj.Solution.Trajectory.Body_Set(i1).calculate_orbit_from_ephem(obj.Solution.Absolute_Times(i1));
obj.Solution.Trajectory.Body_Set(i1).Fixed_Point=-1;
obj.Solution.Trajectory.Body_Set(i1).true_anomaly=obj.Solution.Trajectory.Body_Set(i1).orbit.ta;
end
elseif (obj.Solution.Trajectory.Body_Set(i1).Fixed_Point==-1)
obj.Solution.Trajectory.Body_Set(i1).Fixed_Point=0;
end
end
tin(1:(obj.Solution.Trajectory.Nbody+2*NIP+NOR)) =0.0 ;
lb(1:(obj.Solution.Trajectory.Nbody+2*NIP+NOR)) =0.0 ;
ub(1:(obj.Solution.Trajectory.Nbody+2*NIP+NOR)) =0.0 ;
% tin=zeros(1,obj.Solution.Trajectory.Nbody+2*NIP+NOR);
% lb=zeros(1,obj.Solution.Trajectory.Nbody+2*NIP+NOR);
% ub=zeros(1,obj.Solution.Trajectory.Nbody+2*NIP+NOR);
for i1=1:obj.Solution.Trajectory.Nbody
lb(i1)=obj.Min_time(i1);
ub(i1)=obj.Max_time(i1);
end
% Set up Inequality Constraint Functions
funcstring= '@(x)[';
% Firstly The Minimum allowable Periapsis
if (obj.Nconstraints>0)
for i1=1:obj.Nconstraints
bb_output_type{i1}='PB';
funcstring= strcat(funcstring, sprintf(' Per_NLopt(x,%d)',i1+1));
funcstring = strcat( funcstring ,' ;' );
nle(i1)=-1;
nlrhs(i1)=0;
end
end
% Secondly The Maximum allowable Periapsis (This adds a scale to the
% Minimum Perapsis and reduces the chance of overshoot of the minimum
% periapsis.)
if (obj.Nconstraints>0)
for i1=(obj.Nconstraints+1):2*obj.Nconstraints
bb_output_type{i1}='PB';
funcstring= strcat(funcstring, sprintf(' Per_NLopt(x,%d)',i1+1));
if i1<2*obj.Nconstraints
funcstring = strcat( funcstring ,' ;' );
end
nle(i1)=-1;
nlrhs(i1)=0;
end
end
% Thirdly The Minimum allowable Perihelia
if(obj.NPerihelia>0)
funcstring = strcat( funcstring ,' ;' );
for i1=1:obj.NPerihelia
funcstring = strcat(funcstring,sprintf(' Perhel(x,%d)',i1));
if i1<obj.NPerihelia
funcstring = strcat( funcstring ,' ;' );
end
nle(i1+2*obj.Nconstraints)=-1;
nlrhs(i1+2*obj.Nconstraints)=0;
bb_output_type{i1+2*obj.Nconstraints}='EB';
% bb_output_type{i+2*obj.Nconstraints}='PB';
end
end
Max_Dur_Flag = 0;
% Check to see if limit on the Maximum Duration
if (obj.Max_Duration < obj.MAX_DURATION )
Max_Dur_Flag = 1;
funcstring = strcat( funcstring ,' ; Overall_Duration(x)');
nle(2*obj.Nconstraints+obj.NPerihelia+1)=-1;
nlrhs(2*obj.Nconstraints+obj.NPerihelia+1)=0;
bb_output_type{2*obj.Nconstraints+obj.NPerihelia+1}='EB';
end
% Finally check for Maximum DeltaV Constraint
Number_DeltaV_Constraints=0;
for i1=1:obj.Solution.Trajectory.Nbody
if (obj.Max_dV(i1)<1e50)
Number_DeltaV_Constraints = Number_DeltaV_Constraints + 1;
funcstring = strcat(funcstring,sprintf(' ; dV_NLopt(x,%d)',i1));
nle(2*obj.Nconstraints+obj.NPerihelia+Max_Dur_Flag+Number_DeltaV_Constraints)=-1;
nlrhs(2*obj.Nconstraints+obj.NPerihelia+Max_Dur_Flag+Number_DeltaV_Constraints)=0;
bb_output_type{2*obj.Nconstraints+obj.NPerihelia+Max_Dur_Flag+Number_DeltaV_Constraints}='PB';
end
end
% Check for minimum Intercept time constraint
Number_Min_Time_Constraints=0;
for i1=1:obj.Solution.Trajectory.Nbody
if(obj.Con_TI(i1)>-1e50)
Number_Min_Time_Constraints = Number_Min_Time_Constraints + 1;
funcstring = strcat(funcstring,sprintf(' ; TI_NLopt(x,%d)',i1));
nle(2*obj.Nconstraints+obj.NPerihelia+Max_Dur_Flag+Number_DeltaV_Constraints+Number_Min_Time_Constraints)=-1;
nlrhs(2*obj.Nconstraints+obj.NPerihelia+Max_Dur_Flag+Number_DeltaV_Constraints+Number_Min_Time_Constraints)=0;
bb_output_type{2*obj.Nconstraints+obj.NPerihelia+Max_Dur_Flag+Number_DeltaV_Constraints+Number_Min_Time_Constraints}='PB';
end
end
% Construct Function Handle From String.
funcstring=strcat(funcstring, ' ]');
nlcon = eval(funcstring);
% Specify Problem type (Non-linear)
optiSolver('NLP');
% Initial Guess
% Check for Presence of Intermediate Point
count2=obj.Solution.Trajectory.Nbody;
NIP=-1;
for i1=1:obj.Solution.Trajectory.Nbody
if obj.Solution.Trajectory.Body_Set(i1).Fixed_Point==1 % INTERMEDIATE POINT ?
NIP=NIP+2;
count2=count2+2;
% Set-up initial values for the Ecliptic polar co-ordinates,
% theta and phi for this INTERMEDIATE POINT
R=norm(obj.Solution.Trajectory.Body_Set(i1).ephemt.r);
theta= atan2(obj.Solution.Trajectory.Body_Set(i1).ephemt.r(2),obj.Solution.Trajectory.Body_Set(i1).ephemt.r(1));
phi=asin(obj.Solution.Trajectory.Body_Set(i1).ephemt.r(3)/R);
theta=obj.AngleConstraint(i1,1);
phi=obj.AngleConstraint(i1,2);
% Set-up initial value of tin
if NIP==1
tin = cat ( 2, obj.Solution.Mission_Times ,[ theta phi ] );
else
tin = cat ( 2, tin , [ theta phi ] );
end
% The lower and upper bounds on the theta and phi's
lb(obj.Solution.Trajectory.Nbody+NIP)=obj.AngleConstraint(i1,3);
lb(obj.Solution.Trajectory.Nbody+NIP+1)=obj.AngleConstraint(i1,5);
% lb(obj.Solution.Trajectory.Nbody+NIP+1)=0.0;
ub(obj.Solution.Trajectory.Nbody+NIP)=obj.AngleConstraint(i1,4);
ub(obj.Solution.Trajectory.Nbody+NIP+1)=obj.AngleConstraint(i1,6);
end
end
% Check for presence of Orbit Flags
if (obj.Orbit_flag>0)
for i1=1:obj.Solution.Trajectory.Nbody
if(bitand(obj.Orbit_flag,2^i1))
count2=count2+1;
% obj.Solution.bodies(i1)=obj.Solution.bodies(i1).calculate_orbit_from_ephem(obj.Solution.bodies(i1).ephemt.t);
theta2=obj.Solution.Trajectory.Body_Set(i1).true_anomaly; % Initial guess at true anomaly
if count2 == obj.Solution.Trajectory.Nbody + 1
tin = cat ( 2, obj.Solution.Mission_Times , theta2 );
else
tin = cat ( 2, tin , theta2 );
end
% The lower and upper bounds on the theta2's
if (obj.Solution.Trajectory.Body_Set(i1).orbit.e>1)
lb(count2)=-acos(-1/obj.Solution.Trajectory.Body_Set(i1).orbit.e);
ub(count2)=acos(-1/obj.Solution.Trajectory.Body_Set(i1).orbit.e);
else
lb(count2)=-pi;
ub(count2)=+pi;
end
end
end
end
% Make sure tin is initialised if no intermediate points!
for i1=1:obj.Solution.Trajectory.Nbody
tin(1,i1) = obj.Solution.Mission_Times(i1);
end
% Initialise the values of the last updated variables used by the
% Objective Function and the Constraint Functions
tlast1 = tin - tin;
DeltaVold=0;
ceq=zeros(1,min(1,2*obj.Nconstraints));
ceqold=zeros(1,min(1,2*obj.Nconstraints));
dVeq=zeros(1,min(1,2*obj.Nconstraints));
dVeqold=zeros(1,min(1,2*obj.Nconstraints));
TIeq=zeros(1,obj.Solution.Trajectory.Nbody);
TIeqold=zeros(1,obj.Solution.Trajectory.Nbody);
req=zeros(1,obj.Solution.Trajectory.Nbody-1);
reqold=zeros(1,obj.Solution.Trajectory.Nbody-1);
% Initialise Trajectory
DeltaV = Compute_DeltaV_NLopt(tin);
% Initialise NOMAD Optimising Settings
if (obj.Nconstraints>0||obj.NPerihelia>0)
nopts = nomadset('bb_output_type',bb_output_type ,'vns_search',0.95,'max_eval',500000);
elseif obj.Max_Duration < obj.MAX_DURATION
nopts = nomadset('bb_output_type',bb_output_type,'vns_search',0.75);
else
nopts=nomadset('vns_search',0.75);
nlrhs=[];
nle=[];
end
if DVF==1
func=@Compute_DeltaV_NLopt;
else
func=@Overall_Duration2;
end
opts=optiset('solver','nomad','display','iter','maxfeval',500000,'maxtime',obj.Run_Time,'solverOpts',nopts);
Opt = opti('fun',func,'nlmix',nlcon,nlrhs,nle,'bounds',lb,ub,'x0',tin,'options',opts);
% Run Optimization
tinstart=tin;
[Optimum,DeltaV,~,~] = solve(Opt,tin)
% tin
% Optimum
tlast1 = 2*tin;
DeltaV
tin = Optimum;
%Set Up the Trajectory for storing
for i1=1:obj.Solution.Trajectory.Nbody
obj.Solution.Mission_Times(1,i1) = Optimum(i1);
end
tlast1(1)=tlast1(1)+1;
[DeltaV,~] = Compute_DeltaV_NLopt(tin)
if (obj.Orbit_flag>0)
for i1=1:obj.Solution.Trajectory.Nbody
if(bitand(obj.Orbit_flag,2^i1))
if contains(obj.Solution.Trajectory.Body_Set(i1).ID,"INTERMEDIATE POINT")
obj.Solution.Trajectory.Body_Set(i1).Fixed_Point=1;
else
obj.Solution.Trajectory.Body_Set(i1).Fixed_Point=-1;
end
end
end
end
% Set Solution
obj.Current_Mission = obj.Solution;
obj.Global_Solution = obj.Solution;
return;
function [DeltaV, gradient ] = Compute_DeltaV_NLopt(tin)
%# Compute_DeltaV_NLopt : Calculates DeltaV as determined by times input by Optimize_Mission
if ~isequal(tin,tlast1)
[DeltaV,gradient] = Update_Traj(tin(:));
DeltaVold=DeltaV;
end
DeltaV=DeltaVold;
return;
end
function [DeltaV,gradient] = Update_Traj(tin)
%# Update_Traj : Updates current Trajectory and calculates all the constraints based on the times and thetas and thi's i.e. tin
% Firstly calculate the Intermediate Points
NIP=-1;
for j=1:obj.Solution.Trajectory.Nbody
if obj.Solution.Trajectory.Body_Set(j).Fixed_Point==1
NIP=NIP+2;
coslong=cos(tin(obj.Solution.Trajectory.Nbody+NIP));
sinlong=sin(tin(obj.Solution.Trajectory.Nbody+NIP));
coslat=cos(tin(obj.Solution.Trajectory.Nbody+NIP+1));
sinlat=sin(tin(obj.Solution.Trajectory.Nbody+NIP+1));
obj.Solution.Trajectory.Body_Set(j).ephem0.r(1)=obj.Min_Per(j)*coslat*coslong;
obj.Solution.Trajectory.Body_Set(j).ephem0.r(2)=obj.Min_Per(j)*coslat*sinlong;
obj.Solution.Trajectory.Body_Set(j).ephem0.r(3)=obj.Min_Per(j)*sinlat;
end
end
% Secondly update all true_anomalies for orbits where specified
if obj.Orbit_flag>0
count2=0;
for j=1:obj.Solution.Trajectory.Nbody
if obj.Solution.Trajectory.Body_Set(j).Fixed_Point==-1
count2=count2+1;
obj.Solution.Trajectory.Body_Set(j).true_anomaly=tin(obj.Solution.Trajectory.Nbody+NIP+1+count2);
end
end
end
% Now Update the Trajectory
[obj.Solution, DeltaV ]= obj.Solution.Compute_DeltaV(tin(1:obj.Solution.Trajectory.Nbody));
gradient=[];
% Remember to update the value of tlast1!!!
tlast1=tin;
% The best permutation of Transfers is 'Best'
Best = obj.Solution.Trajectory.Best;
% IF the value of C3 is constrained at the home planet:
for i=1:obj.Solution.Trajectory.Nbody
if (obj.Max_dV(i)<1e50)
dVeqold(i)= obj.Solution.Trajectory.dV(Best,i) - abs(obj.Max_dV(i));
if (obj.Max_dV(i)<0.0)
dVeqold(i)= -dVeqold(i);
end
end
end
% IF the Intercept Time is Constrained at any body:
for i=1:obj.Solution.Trajectory.Nbody
if (obj.Con_TI(i)>-1e50)
TIeqold(i)= obj.Solution.Absolute_Times(i) - obj.Con_TI(i);
if (bitand(obj.Min_TI_flag,2^(i-1)))
TIeqold(i)=-TIeqold(i);
end
end
end
% Now calculate minimum periapsis constraints for each body not
% at beginning or end of Traejctory: Remember to ignore if
% There is no Encounter for whatever reason.
for j = 2:obj.Solution.Trajectory.Ntrans
if(bitand(obj.Solution.Trajectory.NO_ENCOUNTER,2^j))
ceqold(j)=0;
else
ceqold(j) = obj.Solution.Trajectory.Hyperbola(Best,j).Planet.radius +obj.Min_Per(j)- obj.Solution.Trajectory.Hyperbola(Best,j).Per;
end
end
% Now calculate Maximum Periapsis Constraints for each body not
% at beginning or end of Traejctory: Remember to ignore if
% There is no Encounter for whatever reason.
for j=(obj.Solution.Trajectory.Ntrans+1):(2*obj.Solution.Trajectory.Ntrans-1)
pointer = j-obj.Solution.Trajectory.Ntrans+1;
obj.Solution.Trajectory.Body_Set(pointer)=obj.Solution.Trajectory.Body_Set(pointer).Sphere_Of_Influence();
SphereI = obj.Solution.Trajectory.Body_Set(pointer).SpoI;
if(bitand(obj.Solution.Trajectory.NO_ENCOUNTER,2^pointer))
ceqold(j)=0;
else
ceqold(j) =obj.Solution.Trajectory.Hyperbola(Best,pointer).Per- SphereI;
end
end
% Finally calculate Minimum Perihelia Constraints where present
for j = 1:obj.Solution.Trajectory.Ntrans
if (bitand(obj.Perihelia_flag,2^j))
PER= obj.Solution.Trajectory.Trans_Set(j).transfer_body(obj.Solution.Trajectory.perm(Best,j)).orbit.a*(1-obj.Solution.Trajectory.Trans_Set(j).transfer_body(obj.Solution.Trajectory.perm(Best,j)).orbit.e);
reqold(j) = obj.Perihelia(j+1)-PER;
else
obj.Solution.Trajectory.Trans_Set(j)=obj.Solution.Trajectory.Trans_Set(j).Calculate_Perihelion();
reqold(j) = obj.Perihelia(j+1)-obj.Solution.Trajectory.Trans_Set(j).perihelion(obj.Solution.Trajectory.perm(Best,j));
end
end
return;
end
function [cond,g] = Overall_Duration2(tin)
%# Overall_Duration : Calculates Overall Mission Duration Constraints for Optimize_Mission
cond = 0;
if ~isequal(tin,tlast1)
[DeltaV,g] = Update_Traj(tin);
DeltaVold=DeltaV;
end
for j = 2:obj.Solution.Trajectory.Nbody
cond=cond+tin(j);
end
return;
end
% Periapsis Constraints
function [ con, gradient ] = Per_NLopt(tin,run_mode)
%# Per_NLopt : Calculates Periapsis Constraints for Optimize_Mission
if ~isequal(tin,tlast1)
[DeltaV,gradient] = Update_Traj(tin);
end
% Periapsis Constraints
ceq=ceqold;
con=ceq(run_mode);
gradient = [];
return;
end
% DeltaV Constraints
function [ con, gradient ] = dV_NLopt(tin,run_mode)
%# dV_NLopt : Calculates DeltaV Constraints for Optimize_Mission
if ~isequal(tin,tlast1)
[DeltaV,gradient] = Update_Traj(tin);
end
% Periapsis Constraints
dVeq=dVeqold;
con=dVeq(run_mode);
gradient = [];
return;
end
function [ con, gradient ] = TI_NLopt(tin,run_mode)
%# TI_NLopt : Calculates Intercept Time Constraints for Optimize_Mission
if ~isequal(tin,tlast1)
[DeltaV,gradient] = Update_Traj(tin);
end
% Intercept Time Constraints
TIeq=TIeqold;
con=TIeq(run_mode);
gradient = [];
return;
end
% Perihelion Constraints
function [ con, gradient ] = Perhel(tin,run_mode)
%# Perhel : Calculates Perihelion Constraints for Optimize_Mission
if ~isequal(tin,tlast1)
[DeltaV,gradient] = Update_Traj(tin);
end
req=reqold;
% Perihelion Constraints
con=req(obj.Per_Pointer(run_mode)-1);
% con/obj.AU
gradient = [];
return;
end
function [cond,gradient] = Overall_Duration(tin)
%# Overall_Duration : Calculates Overall Mission Duration Constraints for Optimize_Mission
cond = 0;
for j = 2:obj.Solution.Trajectory.Nbody
cond=cond+tin(j);
end
cond = cond-obj.Max_Duration;
gradient = [];
return;
end
end
% Output Results
function obj = View_Results(obj, numdata, Runmode)
%# View_Results : Displays Interplanetary Trajectory in 2D & 3D Plot form as well as orbits
%# : of SSO's. Also Displays Speed Against Radial Distance Plot of Trajectory
if (Runmode==1)
PlotMiss = obj.Global_Solution.Trajectory;
else
PlotMiss = obj.Current_Mission.Trajectory;
end
% Plot Transfers
nplanets =PlotMiss.Nbody;
ntrans =PlotMiss.Ntrans;
% Specify Time Range
X=zeros(nplanets,numdata,3);
% Firstly Planets
for i=1:nplanets
if PlotMiss.Body_Set(i).Fixed_Point>0
Time_Range = 0;
elseif PlotMiss.Body_Set(i).orbit.e>=1
Time_Range = PlotMiss.Trans_Set(i-1).tar-PlotMiss.Trans_Set(i-1).td ;
else
Time_Range = PlotMiss.Body_Set(i).orbit.TP;
end
if i==1
Time_Range=min(Time_Range,-PlotMiss.Trans_Set(i).td+obj.Max_Spice_Select(i));
tplot=linspace(PlotMiss.Trans_Set(i).td,PlotMiss.Trans_Set(i).td+Time_Range,numdata);
else
Time_Range=min(Time_Range,-PlotMiss.Trans_Set(i-1).td+obj.Max_Spice_Select(i));
tplot=linspace(PlotMiss.Trans_Set(i-1).td,PlotMiss.Trans_Set(i-1).td+Time_Range,numdata);
end
for j=1:numdata
if (bitand(2^i,obj.Current_Mission.Out_Of_Spice_Bounds))
mode=1;
elseif (bitand(obj.Orbit_flag,2^i))
mode=1;
PlotMiss.Body_Set(i).Fixed_Point = 0;
else
mode=2;
end
PlotMiss.Body_Set(i)=PlotMiss.Body_Set(i).compute_ephem_at_t(tplot(j),mode,1e-4);
X(i,j,1)=PlotMiss.Body_Set(i).ephemt.r(1)/obj.AU;
X(i,j,2)=PlotMiss.Body_Set(i).ephemt.r(2)/obj.AU;
X(i,j,3)=PlotMiss.Body_Set(i).ephemt.r(3)/obj.AU;
% if i==2
% tplot(j)
% 180/pi*acos(dot(PlotMiss.Body_Set(i).ephemt.r,PlotMiss.Trans_Set(3).ephema(1).r)/PlotMiss.Body_Set(i).ephemt.R/PlotMiss.Trans_Set(3).ephema(1).R)
% end
end
end
Y1=zeros(ntrans,numdata);
Y2=zeros(ntrans,numdata);
Y3=zeros(ntrans,numdata);
HT=zeros(1,numdata*ntrans);
HR=zeros(1,numdata*ntrans);
HV=zeros(1,numdata*ntrans);
% Secondly Transfers
for i=1:ntrans
Best_Perm =PlotMiss.perm(PlotMiss.Best,i);
tt=linspace(PlotMiss.Trans_Set(i).td,PlotMiss.Trans_Set(i).tar,numdata);
for j=1:numdata
PlotMiss.Trans_Set(i).transfer_body(Best_Perm)=PlotMiss.Trans_Set(i).transfer_body(Best_Perm).compute_ephem_at_t(tt(j),1,1);
HT((i-1)*numdata+j)=tt(j);
HR((i-1)*numdata+j)=PlotMiss.Trans_Set(i).transfer_body(Best_Perm).ephemt.R/obj.AU;
HV((i-1)*numdata+j)=PlotMiss.Trans_Set(i).transfer_body(Best_Perm).ephemt.V;
if((j==1)&&(i>1))
HV((i-1)*numdata+j)=HV((i-1)*numdata+j)+PlotMiss.dV(PlotMiss.Best,i);
end
Y1(i,j)=PlotMiss.Trans_Set(i).transfer_body(Best_Perm).ephemt.r(1)/obj.AU;
Y2(i,j)=PlotMiss.Trans_Set(i).transfer_body(Best_Perm).ephemt.r(2)/obj.AU;
Y3(i,j)=PlotMiss.Trans_Set(i).transfer_body(Best_Perm).ephemt.r(3)/obj.AU;
% Time=datetime(tt(j)/24/60/60,'ConvertFrom','juliandate');
% Disp = sprintf('%d Time= %s Y1=%f Y2=%f\n' ,j,Time,Y1(i,j),Y2(i,j));
% disp(Disp);
end
end
%PlotMiss.Trans_Set(i).transfer_body.ephemt
%PlotMiss.Body_Set(nplanets).ephemt
figure(1);
axis equal;
for i=1:nplanets
plot(-X(i,:,2),X(i,:,1),'--');
hold on;
end
for i=1:ntrans
plot(-Y2(i,1:numdata),Y1(i,1:numdata));
hold on;
end
hold off;
figure(2);
plot(HR,HV);
figure(3);
axis equal;
for i=1:nplanets
plot3(-X(i,:,2),X(i,:,1),X(i,:,3),'--');
hold on;
end
for i=1:ntrans
plot3(-Y2(i,1:numdata),Y1(i,1:numdata),Y3(i,1:numdata));
hold on;
end
hold off;
return;
end
function obj = View_Info(obj,Runmode)
%# View_Info : Display Basic Info in numeric form about the encounter with each SSO in turn
if (Runmode==1)
PrinMiss = obj.Global_Solution;
else
PrinMiss = obj.Current_Mission;
end
f=figure(10);
f.Position = [ 50 50 1650 700 ];
f.Name = 'Velocity and DeltaV Information Together with Periapsis for each Solar System Object Visited';
D{1} = "";
D{2} = "";
D{3} = " Number Planet Time Arrival speed Departure speed DeltaV Cumulative DeltaV Periapsis";
D{4} = " m/s m/s m/s m/s km ";
D{5} = "";
cumdV =0;
for i = 1:PrinMiss.Trajectory.Nbody
Time = cspice_et2utc(PrinMiss.Absolute_Times(i),'C',0);
if i==1
index=1;
Periapsis = "N/A";
elseif (bitand(PrinMiss.Trajectory.NO_ENCOUNTER,2^i))
index=PrinMiss.Trajectory.perm(PrinMiss.Trajectory.Best,i-1);
Periapsis = "N/A";
else
index=PrinMiss.Trajectory.perm(PrinMiss.Trajectory.Best,i-1);
end
if i==PrinMiss.Trajectory.Nbody
index2=PrinMiss.Trajectory.perm(PrinMiss.Trajectory.Best,i-1);
if (PrinMiss.FlybyRendez==1)
DELTAV = PrinMiss.Trajectory.dV(PrinMiss.Trajectory.Best,i);
else
DELTAV = 0;
end
cumdV = cumdV + DELTAV;
Periapsis = "N/A";
else
index2=PrinMiss.Trajectory.perm(PrinMiss.Trajectory.Best,i);
DELTAV = PrinMiss.Trajectory.dV(PrinMiss.Trajectory.Best,i);
cumdV = cumdV + DELTAV;
if(i>1&&~bitand(PrinMiss.Trajectory.NO_ENCOUNTER,2^i))
Periapsis = sprintf("%8.1f",(PrinMiss.Trajectory.Hyperbola(PrinMiss.Trajectory.Best,i).Per- PrinMiss.Trajectory.Body_Set(i).radius)/1000.0);
end
end
D{i+5} = sprintf(" %d %20s %22s %8.1f %8.1f %8.1f %8.1f %10s",i,PrinMiss.Trajectory.Body_Set(i).name,Time,norm(PrinMiss.Trajectory.VA(:,i,index)),norm(PrinMiss.Trajectory.VD(:,i,index2)),DELTAV,cumdV,Periapsis);
% disp(D);
% disp(E)
end
for j=PrinMiss.Trajectory.Nbody+6:obj.Max_NBody+6
D{j}="";
end
u=uicontrol('Style','edit','Min',1,'Max',3);
u.FontName = 'Courier';
u.FontSize = 14;
u.HorizontalAlignment = 'left';
[outstring, ~] = textwrap( u, D , 200);
set(u,'String',outstring, 'Position', [10 10 1600 675]);
end
function obj = View_Encounter_Details(obj)
RS = 696342e3;
Nbody=obj.Current_Mission.Trajectory.Nbody;
Ntrans=obj.Current_Mission.Trajectory.Ntrans;
Best=obj.Current_Mission.Trajectory.Best;
perm(:)=obj.Current_Mission.Trajectory.perm(Best,:);
for i=1:Nbody
BODY(i)=obj.Current_Mission.Trajectory.Body_Set(i);
long(i)=atan2(BODY(i).ephemt.r(2),BODY(i).ephemt.r(1));
lat(i)=asin(BODY(i).ephemt.r(3)/BODY(i).ephemt.R);
R(i)=BODY(i).ephemt.R;
Name{i}=BODY(i).name;
x(:,i)=BODY(i).ephemt.r(:);
v(:,i)=BODY(i).ephemt.v(:);
ta(i)=BODY(i).orbit.ta;
xform = cspice_pxform('ECLIPJ2000','J2000',BODY(i).ephemt.t);
xJ2000(:,i)=xform*x(:,i);
vJ2000(:,i)=xform*v(:,i);
end
for i=1:Nbody
if (i==1)
VA(1:3,1)=0.0;
VD(:,1)=obj.Current_Mission.Trajectory.VD(:,1,perm(1));
Per(i)=0.0;
alpha(i)=0.0;
beta(i)=0.0;
thetain(i)=0.0;
thetaout(i)=0.0;
Miss_Dist_Inf(i)=0.0;
Impact_Param(i)=0.0;
elseif (i==Nbody)
VA(:,Nbody)=obj.Current_Mission.Trajectory.VA(:,Nbody,perm(Nbody-1));
VD(1:3,Nbody)=0.0;
Per(i)=0.0;
alpha(i)=0.0;
beta(i)=0.0;
thetain(i)=0.0;
thetaout(i)=0.0;
Miss_Dist_Inf(i)=0.0;
Impact_Param(i)=0.0;
if (BODY(i).radius>0.0)
Impact_Param(i)=BODY(i).radius/norm(VA(:,i))*sqrt(norm(VA(:,i))^2+2*BODY(i).mu/BODY(i).radius);
if (obj.Current_Mission.FlybyRendez>0) && (obj.Current_Mission.target_periapsis>0)
Per(i)=obj.Current_Mission.target_periapsis-BODY(i).radius;
EN=norm(VA(:,i))^2/2
SMA=1/2*(-BODY(i).mu/EN);
VPER=sqrt(2*(EN+BODY(i).mu/obj.Current_Mission.target_periapsis));
H=VPER*obj.Current_Mission.target_periapsis;
SLR=H^2/BODY(i).mu;