Home > lattice > atreduce.m

atreduce

PURPOSE ^

ATREDUCE Remove useless elements from an AT structure

SYNOPSIS ^

function [newring,indx,newrefs] = atreduce(oldring,oldrefs)

DESCRIPTION ^

ATREDUCE Remove useless elements from an AT structure
NEWRING=ATREDUCE(RING)

 Remove elements with PassMethod='IdentityPass' and merges adjacent
 similar elements

NEWRING=ATREDUCE(RING,REFPTS)
    When merging similar elements, keep REFPTS intact.

[NEWRING,KEPT]=ATREDUCE(...)
    Returns the index of kept elements so that NEWRING=OLDRING(KEPT)

[NEWRING,KEPT,NEWREFPTS]=ATREDUCE(RING,REFPTS)
    Returns in addition the updated list of reference points.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [newring,indx,newrefs] = atreduce(oldring,oldrefs)
0002 %ATREDUCE Remove useless elements from an AT structure
0003 %NEWRING=ATREDUCE(RING)
0004 %
0005 % Remove elements with PassMethod='IdentityPass' and merges adjacent
0006 % similar elements
0007 %
0008 %NEWRING=ATREDUCE(RING,REFPTS)
0009 %    When merging similar elements, keep REFPTS intact.
0010 %
0011 %[NEWRING,KEPT]=ATREDUCE(...)
0012 %    Returns the index of kept elements so that NEWRING=OLDRING(KEPT)
0013 %
0014 %[NEWRING,KEPT,NEWREFPTS]=ATREDUCE(RING,REFPTS)
0015 %    Returns in addition the updated list of reference points.
0016 %
0017 
0018 lg=numel(oldring);
0019 refs=false(lg+1,1);
0020 if nargin < 2
0021 elseif islogical(oldrefs)
0022     refs(1:length(oldrefs))=oldrefs(:);
0023 else
0024     refs(oldrefs)=true;
0025 end
0026 refend=refs(lg+1);
0027 refs=refs(1:lg);
0028 %                    Remove useless elements
0029 keep=~atgetcells(oldring(:),'PassMethod','IdentityPass') | refs;
0030 newring=oldring(keep);
0031 indx=keep;
0032 %                    Merge adjacent elements
0033 bring=cellfun(@(el) elstrip(el,{'PassMethod','PolynomA','PolynomB',...
0034     'MaxOrder','RApertures','EApertures'}),newring,'UniformOutput',false);
0035 bring(refs(indx))={NaN};
0036 bends=atgetcells(newring,'BendingAngle');
0037 keep=true(size(newring));
0038 ba=zeros(size(newring));
0039 invrad=zeros(size(newring));
0040 ll=atgetfieldvalues(newring,'Length');
0041 ba(bends)=atgetfieldvalues(newring(bends),'BendingAngle');
0042 invrad(bends)=ba(bends)./ll(bends);
0043 islikenext=cellfun(@isequal,[bring(2:end);{NaN}],bring) & abs(invrad([2:end 1])-invrad)<5*eps(invrad);
0044 islikeprev=islikenext([end 1:end-1]);
0045 arrayfun(@group,find(islikenext & ~islikeprev),find(~islikenext & islikeprev));
0046 newring=newring(keep);
0047 indx(indx)=keep;
0048 
0049 if nargout >= 3
0050     if nargin >= 2 && islogical(oldrefs)
0051         if length(oldrefs)==lg
0052             newrefs=refs(indx);
0053         else
0054             newrefs=[refs(indx);refend];
0055         end
0056     else
0057         newrefs=find([refs(indx);refend]);
0058     end
0059 end
0060 
0061     function el2=elstrip(el,fname)
0062         if isfield(el,'Length') && el.Length>0
0063             fnms=fieldnames(el);
0064             dd=~cellfun(@(ff) any(strcmp(ff,fname)),fnms);
0065             el2=rmfield(el,fnms(dd));
0066         else
0067             el2=NaN;
0068         end
0069     end
0070 
0071     function group(i1,i2)   % group consecutive elements
0072         newelem=newring{i1};
0073         newelem.Length=sum(ll(i1:i2));
0074         if bends(i1)
0075             newelem.BendingAngle=sum(ba(i1:i2));
0076             newelem.ExitAngle=newring{i2}.ExitAngle;
0077         end
0078         if isfield(newring{i2},'T2'), newelem.T2=newring{i2}.T2; end
0079         if isfield(newring{i2},'R2'), newelem.R2=newring{i2}.R2; end
0080         newelem.FamName=gname(atgetfieldvalues(newring(i1:i2),'FamName'));
0081         newring{i1}=newelem;
0082         keep(i1+1:i2)=false;
0083     end
0084 
0085     function nm=gname(names)    % generate a group name as the longest common part
0086         lmin=min(cellfun(@length,names));
0087         nm=names{1};
0088         cellfun(@cmps,names);
0089         if lmin > 0, nm=nm(1:lmin); end
0090         
0091         function cmps(name)
0092             lmin=find([name(1:lmin) 'a'] ~= [nm(1:lmin) 'b'],1)-1;
0093             if (lmin>0) && (nm(lmin)=='_' || nm(lmin)=='-'), lmin=lmin-1; end
0094         end
0095     end
0096 end

Generated on Thu 24-Aug-2017 18:47:33 by m2html © 2005