Commit 1ed035fb authored by Simone Liuzzo's avatar Simone Liuzzo

Merge branch 'atmodel_copyable' into 'master'

atmodel inherits from matlab.mixin.Copyable

See merge request !53
parents ec5d30ce 50ed512b
This diff is collapsed.
classdef atmodel < handle
classdef atmodel < matlab.mixin.Copyable
%Class providing access to optical values
%Used as a base class for tl2.model, sy.mode, sr.model
%
......@@ -51,6 +51,7 @@ classdef atmodel < handle
emitv_
espread_
blength_
dpp_
end
properties (SetAccess=private)
......@@ -69,6 +70,7 @@ classdef atmodel < handle
end
properties (Dependent)
tunes % Tunes
dpp % Off-momentum
end
properties
......@@ -91,6 +93,10 @@ classdef atmodel < handle
end
events
LatticeModified
end
methods (Static)
function pth=getdirectory(varargin)
pth='';
......@@ -260,17 +266,13 @@ classdef atmodel < handle
end
methods (Access=protected)
function recompute(this,atstruct)
function recompute(this,varargin)
%Compute the stored parameters
if nargin >= 2
this.ring=atstruct;
this.modified=true;
end
if this.modified
ndata=length(this.ring)+1;
this.ll_=findspos(this.ring,ndata);
this.alpha_=mcf(this.ring);
[lindata,avebeta,avemu,avedisp,~,~]=atavedata(this.ring,0.0,1:ndata);
[lindata,avebeta,avemu,avedisp,~,~]=atavedata(this.ring,this.dpp_,1:ndata);
this.inidata=lindata;
tune=lindata(end).mu/2/pi;
this.tunes_=tune;
......@@ -296,7 +298,6 @@ classdef atmodel < handle
idz=this.getid('oc');
this.vlave(idz,12)=setintegfield(this.ring(idz),'PolynomB',{4});
this.modified=false;
this.emith_=[]; % Triger computation of emittances
end
function v=setintegfield(at,varargin)
......@@ -308,7 +309,7 @@ classdef atmodel < handle
function recompute_emittance(this)
if isempty(this.emith_)
[~,params]=atx(this.ring,0.0,1);
[~,params]=atx(this.ring,this.dpp_,1);
this.emith_=params.modemittance(1);
% this.emitv_=max(params.modemittance(2),5.e-12);
this.emitv_=params.modemittance(2);
......@@ -329,8 +330,8 @@ classdef atmodel < handle
if ischar(code) % string code
lcode=lower(code);
if ~isfield(this.idx,lcode)
id=this.elselect(code);
try
id=this.elselect(lcode);
this.idx.(lcode)=id;
catch
warning('atmodel:getid','Cannot store %s in the cache',code);
......@@ -345,7 +346,7 @@ classdef atmodel < handle
function idx=elselect(this,code)
%Return indices of selected elements
switch code
switch lower(code)
case 'bpm'
idx=atgetcells(this.ring,'Class','Monitor');
case 'qp'
......@@ -371,12 +372,13 @@ classdef atmodel < handle
end
methods
function this=atmodel(atstruct)
function this=atmodel(atstruct,varargin)
%Object giving access to the optical parameters
%
%ATMODEL(ATring)
%
%ATring: AT structure
[dpp,varargs]=getargs(varargin,0); %#ok<ASGLU>
ringpar=atgetcells(atstruct,'Class','RingParam');
if any(ringpar)
ringparam=atstruct{find(ringpar,1)};
......@@ -386,9 +388,23 @@ classdef atmodel < handle
this.strname='?';
this.periods=1;
end
this.recompute(atstruct);
this.ring=atstruct;
this.dpp_=dpp;
end
function set.modified(this,val)
this.modified=val;
if val
this.emith_=[]; %#ok<MCSUP>
notify(this,'LatticeModified');
end
end
function set.ring(this,atstruct)
this.ring=atstruct;
this.idx=[]; %#ok<MCSUP> % Clear cache
this.modified=true; %#ok<MCSUP> % Force recomputation
end
% dependent properties computed by atlinopt
function ll=get.ll(this)
this.recompute();
......@@ -405,6 +421,13 @@ classdef atmodel < handle
function set.tunes(this,tunes)
this.settune(tunes/this.periods);
end
function dpp=get.dpp(this)
dpp=this.dpp_;
end
function set.dpp(this,dpp)
this.dpp_=dpp;
this.modified=true;
end
function nuv=get.nuv(this)
this.recompute();
nuv=this.periods*this.tunes_(2);
......@@ -486,6 +509,17 @@ classdef atmodel < handle
'UniformOutput',false);
end
function varargout=avedata(this,varargin)
%Return average optical parameters at selected locations
%
%[lindata1,lindata2,...]=LINDATA(CODE_1,CODE_2...)
%CODEn: One of the available locations, or AT index
%lindatan: average data at the entrance of selected elements
this.recompute();
varargout=cellfun(@(arg) this.vlave(this.getid(arg),:), varargin,...
'UniformOutput',false);
end
function varargout=plot(this,varargin)
%Plot the optical functions
%
......@@ -501,10 +535,12 @@ classdef atmodel < handle
end
end
function settune(this,tunes,varargin) %#ok<INUSD>
%Change tunes
function settune(this,varargin) %#ok<INUSD>
%SETTUNE Change thetunes of the model
%
%SETTUNE(TUNES)
%SETTUNE(DPP,TUNES)
% DPP: deltaP/P, default 0
% TUNES: 1x2 vector of desired tunes
error('atmodel:NotImplemented','No generic imlementation of retuning');
end
......@@ -530,8 +566,6 @@ classdef atmodel < handle
% the strength of all quadrupoles
idz=this.getid(code);
this.ring(idz)=atsetfieldvalues(this.ring(idz),varargin{:});
this.modified=true;
this.emith_=[];
end
function varargout=getfieldvalue(this,code,varargin)
......@@ -549,6 +583,25 @@ classdef atmodel < handle
varargout{1}=atgetfieldvalues(this.ring(idz),varargin{:});
end
function substitute(this,code,modfunc,varargin)
%Substitute selected element with new ones
%
%SUBSTITUTE(CODE,MOD_FUNC,VALUE1,VALUE2,...)
% CODE: One of the available locations, or AT index
% MOD_FUNC: Function handle. Selected elements are replaced by
% NEWELEM=MODIFIER(ELEM,VALUE1,VALUE2,...)
% VALUEn: Column cell array of argument n to the MODIFIER function.
% Size should be N_SELECTED x 1
%
%Example:
%>> SUBSTITUTE('qp',@set_strength,STRENGTHS) apply
% set_strength(strength) to each quadrupole:
%
idz=this.getid(code);
this.ring(idz)=cellfun(modfunc,this.ring(idz),varargin{:},...
'UniformOutput',false);
end
function data=bpm(this,varargin)
data=this.getave('bpm',varargin{:});
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment