summaryrefslogtreecommitdiff
path: root/fitter_check.m
blob: 3e0a1df8d0fc353522fe12e5a82c41593bba3b11 (plain)
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
% ########################################## 
% Sample Solution
clear;
lambda= 1.064E-6 ;
Ltot= 1.010675025828971 ;
r0= 1.0E+100 ;
w0= 2.563E-5 ;
x0= 0 ;
focal_length1 = .075;
focal_length2 = .075;
focal_length3 = .203;

lns1.abcd=abcd_lens( focal_length1 ) ;
lns1.x= 0.21358727296049 ;
lns2.abcd=abcd_lens( focal_length2 ) ;
lns2.x= 0.40361319425309 ;
lns3.abcd=abcd_lens( focal_length3 ) ;
lns3.x= 0.80361319425309 ;
wf= 3.709E-5 ;
rf= 1.0E+100 ;
xf= Ltot;

q0=wr2q(w0,r0,lambda);
x0=0;
qf=wr2q(wf,rf,lambda);
xf=Ltot;

optics={lns1,lns2,lns3};
figure(1)
w_final_handmade = solution_visualization(q0,x0, qf, xf, optics, lambda);
title('Hand made');
% ########################################## 


%Initialize sample arrays
sample_energy = [];
sample_x = [];
possible_soln = [];
possible_lens_pos = [];
possible_sample_energy = [];
lens_size = .03;

%Lens permutations
lens_permutations = perms( [ focal_length1, focal_length2, focal_length3 ]);
n_shuffles=10;

%Check if permutation has duplicates
lens_permutations = unique(lens_permutations,'rows');
n_perms = size(lens_permutations,1);

for i = 1:n_perms
    
    
    lenses_choice=lens_permutations(i,:)
      
    for iteration = 1:n_shuffles
        f3=lenses_choice(3);
        x3=xf-f3; % last lense transfer collimated region to focused spot
        optics_x_rand = sort(lens_size+(x3-2*lens_size)*rand(1,2));
        optics_x_rand = [optics_x_rand, x3];
        
        fitness_simplified=@(x) fitness(q0, qf, Ltot, x, lenses_choice, lambda );
        [x_sol, energy]=fminsearch(fitness_simplified, optics_x_rand, optimset('TolX',1e-8,'TolFun',1e-8,'MaxFunEvals',1e8,'MaxIter',200));
        
        sample_energy = [sample_energy; energy];
        sample_x = [sample_x; x_sol];
        
        %Return final Waist of trial
        q_f_trial = gbeam_propagation(Ltot,q0,x0,optics_placer(x_sol, lenses_choice));
        [waist, Radius] = q2wr(q_f_trial, lambda);
        
        %If it is a good solution, add to list of possible solutions
        waist_desired = wf;
        compare_waist = abs(waist - waist_desired);
        tolerance = 1E-6;
                
        if compare_waist < tolerance
            possible_soln = [possible_soln; x_sol];
            possible_lens_pos = [possible_lens_pos; lenses_choice];
            possible_sample_energy = [possible_sample_energy; energy];
        end
        
        %Visualize solution
        figure(2)
        solution_visualization(q0,x0, qf, xf, optics_placer(x_sol, lenses_choice), lambda);
        title('Testing Points');
        drawnow;       
      
    end
    
end



%Sorting possible solution according to energy
[possible_sample_energy, index] = sort(possible_sample_energy);
possible_soln = possible_soln(index,:);
possible_lens_pos = possible_lens_pos(index,:);

%Truncate other possible solutions to an accuracy of n decimal places
n=4; 
possible_soln_trunc = round(possible_soln*10^n)./10^n;
[possible_soln_uniq, index] = unique(possible_soln_trunc,'rows','stable'); %Unique solutions only

%Visualize five best solutions
n_possible_soln = min(5,size(possible_soln_uniq,1));
for n_graph = 1:n_possible_soln
    figure(n_graph+1)
    w_final_trial = solution_visualization(q0,x0, qf, xf, optics_placer(possible_soln(index(n_graph),:), possible_lens_pos(index(n_graph),:)), lambda);
    title('Other Solutions');
end

w_final_handmade;
x_sol