|
| 1 | +function [pnotlost, plost] = atfloodfill(ring, varargin) |
| 2 | +% [pnotlost, plost] = floodfill(ring) |
| 3 | +% |
| 4 | +% Finds the 2D acceptance of the ring using Flood Fill. |
| 5 | +% |
| 6 | +% Flood fill tracks particles from the exterior to the border of the |
| 7 | +% acceptance. |
| 8 | +% The lost particles are returned in plost. |
| 9 | +% The not lost particles are returned in pnotlost. |
| 10 | +% |
| 11 | +% Parameters: |
| 12 | +% ring: AT lattice. |
| 13 | +% |
| 14 | +% Keyword Arguments: |
| 15 | +% nturns: Number of turns for the tracking. Default: 1000 |
| 16 | +% window: Min and max coordinate range |
| 17 | +% [Axis1min,Axis1max,Axis2min,Axis2max]. |
| 18 | +% Default [-10e-3,10e-3,-10e-3,10e-3]. |
| 19 | +% Axis1 and Axis2 are defined by 'axes'. |
| 20 | +% gridsize: Number of steps per axis. Default [10,10]. |
| 21 | +% axes: Indexes of axes to be scanned. Default is [1,3], i.e. x-y. |
| 22 | +% sixdoffset: Offset to be added. Default zeros(6,1). |
| 23 | +% This is useful to study off-axis acceptance on any plane, |
| 24 | +% or off-momentum acceptance by adding dp to the 5th coord., |
| 25 | +% to track particles on the closed orbit, or to add |
| 26 | +% a small deviation to the tracked coordinates, |
| 27 | +% e.g. [10e-5 10e-5] in the transverse planes. |
| 28 | +% verbose: Print extra info. Default 0. |
| 29 | +% |
| 30 | +% Returns: |
| 31 | +% pnotlost: array of size (2,n_not_lost) containing the 2D offsets of |
| 32 | +% n_not_lost tracked particles that survived. |
| 33 | +% plost: array of size (3,n_lost) containing the 2D offsets of |
| 34 | +% n_lost trackedp articles that did not survive; and |
| 35 | +% in the third row the turn on which each particle is lost. |
| 36 | +% |
| 37 | +% Example: |
| 38 | +% [pnl, pl] = atfloodfill(THERING, nturns=500) |
| 39 | +% |
| 40 | +% |
| 41 | +% Notes: |
| 42 | +% This method is recomended for single or low number of CPUs, and, |
| 43 | +% it does not scale well for parallel computing. |
| 44 | +% |
| 45 | +% Based on the article, |
| 46 | +% B. Riemann, M. Aiba, J. Kallestrup, and A. Streun, "Efficient |
| 47 | +% algorithms for dynamic aperture and momentum acceptance |
| 48 | +% calculation in synchrotron light sources", Phys. Rev. Accel. |
| 49 | +% Beams, vol. 27, no. 9, p. 094 002, 2024. |
| 50 | +% doi:10.1103/PhysRevAccelBeams.27.094002 |
| 51 | + |
| 52 | +% Author : E. Serra, UAB and ALBA, 2025 original version in python |
| 53 | +% See. IPAC2025, MOPB065. |
| 54 | +% APPLICATION OF FAST ALGORITHMS TO |
| 55 | +% CALCULATE DYNAMIC AND |
| 56 | +% MOMENTUM APERTURE TO THE DESIGN OF |
| 57 | +% ALBA II. |
| 58 | +% doi: 10.18429/JACoW-IPAC25-MOPB065 |
| 59 | +% Edited : O. Blanco, ALBA, 2025 matlab version |
| 60 | + |
| 61 | +% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 62 | +% Parse optional arguments |
| 63 | + p = inputParser; |
| 64 | + addOptional(p,'nturns',1000); |
| 65 | + addOptional(p,'window',[-10e-3,10e-3,-10e-3,10e-3]); |
| 66 | + addOptional(p,'gridsize',[10,10]); |
| 67 | + addOptional(p,'axes',[1,3]); |
| 68 | + addOptional(p,'sixdoffset',zeros(6,1)); |
| 69 | + addOptional(p,'verbose',0); |
| 70 | + parse(p,varargin{:}); |
| 71 | + par = p.Results; |
| 72 | + |
| 73 | + % Initialize variables |
| 74 | + nturns = par.nturns; |
| 75 | + window = par.window; |
| 76 | + gridsize = par.gridsize; |
| 77 | + sixdoffset = par.sixdoffset; |
| 78 | + verbose = par.verbose; |
| 79 | + axes = par.axes; |
| 80 | + |
| 81 | + % Initialize output in case we return earlier |
| 82 | + pnotlost = zeros(2,0); |
| 83 | + plost = zeros(3,0); |
| 84 | + |
| 85 | + if verbose, fprintf('Flood fill starts.\n'); end |
| 86 | + |
| 87 | + if verbose, fprintf('Max. number of turns: %d\n',nturns); end |
| 88 | + |
| 89 | + % Create the grid |
| 90 | + nx = gridsize(1); |
| 91 | + ny = gridsize(2); |
| 92 | + npart = nx*ny; |
| 93 | + |
| 94 | + % Check the window dimensions |
| 95 | + if (window(1) == window(2) || window(3) == window(4)) && verbose |
| 96 | + fprintf('Window is quite narrow.\n'); |
| 97 | + return; |
| 98 | + end |
| 99 | + % Check the grid |
| 100 | + if any(gridsize < 2) |
| 101 | + fprintf('Horizontal and vertical gridsize is too small.\n'); |
| 102 | + return; |
| 103 | + end |
| 104 | + xvals = linspace(min(window(1:2)),max(window(1:2)),nx); |
| 105 | + yvals = linspace(min(window(3:4)),max(window(3:4)),ny); |
| 106 | + points = zeros(2,npart); |
| 107 | + |
| 108 | + if verbose, fprintf('Number of grid points: %d.\n',npart); end |
| 109 | + |
| 110 | + % Set the order |
| 111 | + ii = 1; |
| 112 | + for ix = 1:nx |
| 113 | + for iy = 1:ny |
| 114 | + points(:,ii) = [xvals(ix),yvals(iy)]'; |
| 115 | + ii = ii + 1; |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + % Particle coordinates |
| 120 | + particles = zeros(6,npart); |
| 121 | + particles = particles + sixdoffset; |
| 122 | + particles(axes(1),:) = particles(axes(1),:) + points(1,:); |
| 123 | + particles(axes(2),:) = particles(axes(2),:) + points(2,:); |
| 124 | + |
| 125 | + % Track the particles for nturns using the flood-fill algorithm. |
| 126 | + % the_queue is replaced by an array with a queue_counter in matlab. |
| 127 | + % The order is fixed to explore the top, left, bottom and right sides. |
| 128 | + the_queue = zeros(npart,1); |
| 129 | + the_queue(1 :(ny-2) ) = ((nx-1)*ny+2):(nx*ny-1); %r |
| 130 | + the_queue((ny-1) :(nx+ny-2) ) = ((nx-1):-1:0)*ny+1; %b |
| 131 | + the_queue((nx+ny-1) :(nx+ny+ny-4) ) = 2:(ny-1); %l |
| 132 | + the_queue((nx+ny+ny-3):(2*nx + 2*(ny-2))) = (nx:-1:1)*ny; %t |
| 133 | + queue_counter = 2*nx + 2*(ny-2); |
| 134 | + |
| 135 | + % particles that have been tracked |
| 136 | + idxpartdone = false(npart,1); |
| 137 | + |
| 138 | + % output |
| 139 | + plost = cell(npart,1); |
| 140 | + pnotlost = cell(npart,1); |
| 141 | + |
| 142 | + if verbose, fprintf('Tracking... '); end |
| 143 | + while queue_counter > 0 |
| 144 | + % take one index from the_queue and track that particle |
| 145 | + i = the_queue(queue_counter); |
| 146 | + queue_counter = queue_counter - 1; |
| 147 | + % check if valid and not done |
| 148 | + if (1 <= i && i <= npart) && ~idxpartdone(i) |
| 149 | + idxpartdone(i) = true; |
| 150 | + [~, ~, ~, lossinfo] = ringpass(ring, particles(:,i), nturns); |
| 151 | + |
| 152 | + if lossinfo.lost |
| 153 | + % if lost, add new points |
| 154 | + newidx = [i+1 i-1 i+ny i-ny]; |
| 155 | + maskidx = newidx > 0 & newidx <= npart; |
| 156 | + newidx = newidx(maskidx); |
| 157 | + nextpoints = newidx(~idxpartdone(newidx)); |
| 158 | + queue_counter_aux = queue_counter+length(nextpoints); |
| 159 | + the_queue(queue_counter+1:queue_counter_aux) = nextpoints; |
| 160 | + queue_counter = queue_counter_aux; |
| 161 | + |
| 162 | + % save point |
| 163 | + plost{i} = [ ... |
| 164 | + particles(axes(1),i); ... |
| 165 | + particles(axes(2),i); ... |
| 166 | + lossinfo.turn ... |
| 167 | + ]; |
| 168 | + else |
| 169 | + % if not lost, save point |
| 170 | + pnotlost{i} = [ ... |
| 171 | + particles(axes(1),i); ... |
| 172 | + particles(axes(2),i); ... |
| 173 | + ]; |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + if verbose, fprintf(' done.\n'); end |
| 179 | + |
| 180 | + % remove empty cells |
| 181 | + plost = plost(~cellfun('isempty',plost)); |
| 182 | + pnotlost = pnotlost(~cellfun('isempty',pnotlost)); |
| 183 | + |
| 184 | + % reshape output |
| 185 | + plost = reshape(cell2mat(plost),3,[]); |
| 186 | + pnotlost = reshape(cell2mat(pnotlost),2,[]); |
| 187 | + |
| 188 | + if isempty(pnotlost) && verbose |
| 189 | + fprintf('No surviving particles.\n'); |
| 190 | + end |
| 191 | +end |
0 commit comments