PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ATF2/FlightSim/testApps/bumpgui/bumpgui.m

http://atf2flightsim.googlecode.com/
MATLAB | 807 lines | 677 code | 90 blank | 40 comment | 147 complexity | f99cb9bdeb5c76d7060180530d54a3e8 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. function knob = bumpgui(plane,type,targ,cors)
  2. % knob = bumpgui(plane,type,targ,cors)
  3. % plane = # (1:4) (x,x',y,y')
  4. % type = 3 | 4 (# corrector bump)
  5. % targ = Lucretia BEAMLINE element number of bump target
  6. % cors = corrector or quad with mover list (BEAMLINE element number)
  7. %====================
  8. % 11/10/2009 GW
  9. % Add ability to choose quads on movers in addition to correctors
  10. %
  11. global BEAMLINE GIRDER
  12. % Check the number of input arguments -- should be 0 or 4
  13. if nargin==0 % GUI mode, auto=true
  14. auto = true;
  15. elseif nargin==4 % CLI mode, auto=false
  16. auto = false;
  17. else
  18. error('Incorrect inputs to bumpgui')
  19. end
  20. % If in CLI mode, then check the inputs
  21. if ~auto
  22. if ~(plane==1 || plane==2 || plane==3 || plane==4)
  23. error('First input to bumpgui must be 1, 2, 3, or 4')
  24. end
  25. if ~(type==3 || type==4)
  26. error('Second input to bumpgui must be 3 or 4')
  27. end
  28. if ~isnumeric(targ) || ~(targ<=length(BEAMLINE))
  29. error('Third input to bumpgui must be a number less than length(BEAMLINE)')
  30. end
  31. if ~isnumeric(cors) || ~(max(cors)<=length(BEAMLINE))
  32. error(['Fourth input to bumpgui must be an array of numbers, ' ...
  33. 'each less than length(BEAMLINE)'])
  34. end
  35. if length(cors)~=type
  36. error([num2str(type) ' correctors should be specified for a ' ...
  37. num2str(type) '-corrector bump!'])
  38. end
  39. if plane==1 || plane==2
  40. for cornum=1:type
  41. if ~strcmpi('xcor',BEAMLINE{cors(cornum)}.Class) && ~strcmpi('mark',BEAMLINE{cors(cornum)}.Class) && ...
  42. ~(strcmp(BEAMLINE{cors(cornum)}.Class,'QUAD') && isfield(BEAMLINE{cors(cornum)},'Girder') &&...
  43. ~isempty(BEAMLINE{cors(cornum)}.Girder) && BEAMLINE{cors(cornum)}.Girder)
  44. error(['BEAMLINE element #' num2str(cors(cornum)) ...
  45. ' is not an xcor or quad with mover'])
  46. end
  47. end
  48. elseif plane==3 || plane==4
  49. for cornum=1:type
  50. if ~strcmpi('ycor',BEAMLINE{cors(cornum)}.Class) && ~strcmpi('ycor',BEAMLINE{cors(cornum)}.Class) &&...
  51. ~(strcmp(BEAMLINE{cors(cornum)}.Class,'QUAD') && isfield(BEAMLINE{cors(cornum)},'Girder') &&...
  52. ~isempty(BEAMLINE{cors(cornum)}.Girder) && BEAMLINE{cors(cornum)}.Girder)
  53. error(['BEAMLINE element #' num2str(cors(cornum)) ...
  54. ' is not an ycor or quad with mover'])
  55. end
  56. end
  57. end
  58. if type==3
  59. if cors(2)<=cors(1) || cors(3)<=cors(2)
  60. error('4th input should be an ordered list of 3 BEAMLINE elements')
  61. end
  62. if cors(1)>targ || cors(3)<targ
  63. error('The target must be between the correctors!')
  64. end
  65. end
  66. if type==4
  67. if cors(2)<=cors(1) || cors(3)<=cors(2) || cors(4)<=cors(3)
  68. error('4th input should be an ordered list of 4 BEAMLINE elements')
  69. end
  70. if cors(2)>targ || cors(3)<targ
  71. error('The target must be between the 2nd and 3rd elements')
  72. end
  73. end
  74. end
  75. % In GUI mode, we need the location of the start/end of the ext and ff
  76. if auto
  77. ringext = findcells(BEAMLINE,'Name','IEX');
  78. extff = findcells(BEAMLINE,'Name','BEGFF');
  79. if isempty(ringext)
  80. error('Element Name ''IEX'' cannot be found')
  81. elseif isempty(extff)
  82. error('Element Name ''BEGFF'' cannot be found')
  83. end
  84. end
  85. % Set up strings for GUI dialog boxes
  86. if auto
  87. btypeq = ...
  88. {'3 Corrector (Position OR Angle bump)',...
  89. '4 Corrector (Position AND Angle bump)'};
  90. region = {'EXT','FF'};
  91. end
  92. % 3 or 4 cor bump?
  93. if auto
  94. [bumptype ok] = listdlg('ListString',btypeq,'SelectionMode','Single',...
  95. 'Name','Bump type','PromptString','Please choose a bump type',...
  96. 'ListSize',[250 100]);
  97. if ~ok; return; end
  98. else
  99. if type==3
  100. bumptype=1;
  101. elseif type==4
  102. bumptype=2;
  103. end
  104. end
  105. % Which region? ext or ff?
  106. if auto
  107. [roi ok] = listdlg('ListString',region,'SelectionMode','Single',...
  108. 'Name','Region','PromptString','Choose the region for the bump',...
  109. 'ListSize',[200 100]);
  110. if ~ok; return; end
  111. end
  112. % Define start and end of region of interest
  113. if auto
  114. switch roi
  115. case 1 % EXT
  116. start = ringext;
  117. stop = extff;
  118. case 2 % FF
  119. start = extff;
  120. stop = length(BEAMLINE);
  121. end
  122. elementlist = {BEAMLINE{start:stop}};
  123. elenamelist = cell(1,length(elementlist));
  124. for count = 1:length(elementlist)
  125. elenamelist{count} = elementlist{count}.Name;
  126. end
  127. end
  128. % x or y bump? Position or angle?
  129. if auto
  130. xory = questdlg('Bump in x or y?','x or y','x','y','x');
  131. switch xory
  132. case 'x'
  133. plane = 1;
  134. corinds = findcells(BEAMLINE,'Class','XCOR');
  135. case 'y'
  136. plane = 3;
  137. corinds = findcells(BEAMLINE,'Class','YCOR');
  138. end
  139. if bumptype==1
  140. posorang = questdlg('Bump in position or angle?',...
  141. [xory ' or ' xory ''''],xory,[xory ''''],xory);
  142. switch posorang
  143. case [xory '''']
  144. plane = plane+1;
  145. end
  146. elseif bumptype==2
  147. posorang = questdlg('Bump in position or angle?',...
  148. [xory ' or ' xory ''''],xory,[xory ''''],xory);
  149. switch posorang
  150. case [xory '''']
  151. plane = plane+1;
  152. end
  153. end
  154. end
  155. % Choose bump position
  156. % Check availability of cors to open/close the bump
  157. if auto
  158. closecors = false;
  159. while closecors==false
  160. [bumppos ok] = listdlg('ListString',elenamelist,'SelectionMode','Single',...
  161. 'Name','Element','PromptString','Choose the bump position',...
  162. 'ListSize',[160 600]);
  163. if ~ok; return; end
  164. bumppos = bumppos + start - 1;
  165. numclosecors = length(find(corinds>bumppos));
  166. if numclosecors==0
  167. closecors=false;
  168. uiwait(errordlg(['There are no correctors after this point, therefore this bump' ...
  169. ' cannot be closed. Please select another point or press ''cancel'' at the' ...
  170. ' next prompt to cancel bump generation.'],'No closing correctors'));
  171. else
  172. closecors=true;
  173. end
  174. corinds1 = corinds(corinds>ringext & corinds<bumppos);
  175. if isempty(corinds1)
  176. closecors=false;
  177. uiwait(errordlg(['There are no correctors available to open this bump.' ...
  178. ' Please select another point or press ''cancel'' at the' ...
  179. ' next prompt to cancel bump generation.'],'No opening correctors'));
  180. else
  181. corlist1 = cell(1,length(corinds1));
  182. for count = 1:length(corinds1)
  183. corlist1{count} = BEAMLINE{corinds1(count)}.Name;
  184. end
  185. end
  186. end
  187. else
  188. bumppos=targ;
  189. end
  190. % Choose opening correctors
  191. if auto
  192. causality = false;
  193. while ~causality
  194. [cor1 ok] = listdlg('ListString',corlist1,'SelectionMode','Single',...
  195. 'Name','First corrector','PromptString','Choose the opening corrector',...
  196. 'ListSize',[200 600]);
  197. if ~ok; return; end
  198. if BEAMLINE{corinds1(cor1)}.S >= BEAMLINE{bumppos}.S
  199. causality = false;
  200. warnh = warndlg(['First corrector must be upstream of the bump position ' ...
  201. BEAMLINE{bumppos}.Name],'Corrector location error');
  202. uiwait(warnh);
  203. else
  204. causality = true;
  205. end
  206. if bumptype==2
  207. corinds2 = corinds(corinds>corinds1(cor1) & corinds<bumppos);
  208. if isempty(corinds2)
  209. causality=false;
  210. uiwait(errordlg(['Choosing this corrector results in no available corrector to '...
  211. 'correctly open the bump. Please select another corrector or press ''cancel'' ' ...
  212. 'at the next prompt to cancel bump generation.'],'No opening correctors'));
  213. else
  214. corlist2 = cell(1,length(corinds2));
  215. for count = 1:length(corinds2)
  216. corlist2{count} = BEAMLINE{corinds2(count)}.Name;
  217. end
  218. end
  219. end
  220. end
  221. end
  222. % Choose closing correctors
  223. switch bumptype
  224. case 1 % 3 corr bump
  225. if auto
  226. corinds2 = corinds(corinds>corinds1(cor1));
  227. corlist2 = cell(1,length(corinds2));
  228. for count = 1:length(corinds2)
  229. corlist2{count} = BEAMLINE{corinds2(count)}.Name;
  230. end
  231. end
  232. if auto
  233. causality = false;
  234. while ~causality
  235. [cor2 ok] = listdlg('ListString',corlist2,'SelectionMode','Single',...
  236. 'Name','Second corrector','PromptString','Choose the 2nd corrector',...
  237. 'ListSize',[200 600]);
  238. if ~ok; return; end
  239. if BEAMLINE{corinds2(cor2)}.S <= BEAMLINE{corinds1(cor1)}.S
  240. causality = false;
  241. warnh = warndlg(...
  242. ['Second corrector must be upstream of the first corrector ' ...
  243. BEAMLINE{cor1}.Name],'Corrector location error');
  244. uiwait(warnh);
  245. else
  246. causality = true;
  247. end
  248. corinds3 = corinds(corinds>bumppos & corinds>corinds2(cor2));
  249. if isempty(corinds3)
  250. causality=false;
  251. uiwait(errordlg(['Choosing this corrector results in no available corrector to '...
  252. 'close the bump. Please select another corrector or press ''cancel'' at the' ...
  253. ' next prompt to cancel bump generation.'],'No closing correctors'));
  254. else
  255. corlist3 = cell(1,length(corinds3));
  256. for count = 1:length(corinds3)
  257. corlist3{count} = BEAMLINE{corinds3(count)}.Name;
  258. end
  259. end
  260. end
  261. end
  262. if auto
  263. causality = false;
  264. while ~causality
  265. [cor3 ok] = listdlg('ListString',corlist3,'SelectionMode','Single',...
  266. 'Name','Third corrector','PromptString','Choose the 3rd corrector',...
  267. 'ListSize',[200 600]);
  268. if ~ok; return; end
  269. if (BEAMLINE{corinds3(cor3)}.S <= BEAMLINE{corinds2(cor2)}.S) && ...
  270. (BEAMLINE{corinds3(cor3)}.S <= BEAMLINE{bumppos}.S)
  271. causality = false;
  272. warnh = warndlg(...
  273. ['Third corrector must be upstream of the second corrector ' ...
  274. BEAMLINE{cor1}.Name ' and the bump position ' ...
  275. BEAMLINE{bumppos}.Name],...
  276. 'Corrector location error');
  277. uiwait(warnh);
  278. else
  279. causality = true;
  280. end
  281. end
  282. end
  283. case 2 % 4 corr bump
  284. % if auto
  285. % corinds2 = corinds(corinds>corinds1(cor1) & corinds<bumppos);
  286. % corlist2 = cell(1,length(corinds2));
  287. % for count = 1:length(corinds2)
  288. % corlist2{count} = BEAMLINE{corinds2(count)}.Name;
  289. % end
  290. % end
  291. if auto
  292. causality = false;
  293. while ~causality
  294. [cor2 ok] = listdlg('ListString',corlist2,'SelectionMode','Single',...
  295. 'Name','Second corrector','PromptString','Choose the 2nd corrector',...
  296. 'ListSize',[200 600]);
  297. if ~ok; return; end
  298. if BEAMLINE{corinds2(cor2)}.S <= BEAMLINE{corinds1(cor1)}.S
  299. causality = false;
  300. warnh = warndlg(...
  301. ['Second corrector must be downstream of the first corrector ' ...
  302. BEAMLINE{cor1}.Name],'Corrector location error');
  303. uiwait(warnh);
  304. else
  305. causality = true;
  306. end
  307. end
  308. end
  309. if auto
  310. corinds3 = corinds(corinds>bumppos);
  311. corlist3 = cell(1,length(corinds3));
  312. for count = 1:length(corinds3)
  313. corlist3{count} = BEAMLINE{corinds3(count)}.Name;
  314. end
  315. end
  316. if auto
  317. causality = false;
  318. while ~causality
  319. [cor3 ok] = listdlg('ListString',corlist3,'SelectionMode','Single',...
  320. 'Name','Third corrector','PromptString','Choose the 3rd corrector',...
  321. 'ListSize',[200 600]);
  322. if ~ok; return; end
  323. if (BEAMLINE{corinds3(cor3)}.S <= BEAMLINE{corinds2(cor2)}.S) && ...
  324. (BEAMLINE{corinds3(cor3)}.S <= BEAMLINE{bumppos}.S)
  325. causality = false;
  326. warnh = warndlg(...
  327. ['Third corrector must be downstream of the second corrector ' ...
  328. BEAMLINE{cor1}.Name ' and the bump position ' ...
  329. BEAMLINE{bumppos}.Name],...
  330. 'Corrector location error');
  331. uiwait(warnh);
  332. else
  333. causality = true;
  334. end
  335. corinds4 = corinds(corinds>corinds3(cor3));
  336. if isempty(corinds4)
  337. causality=false;
  338. uiwait(errordlg(['Choosing this corrector results in no available corrector to '...
  339. 'close the bump. Please select another corrector or press ''cancel'' at the' ...
  340. ' next prompt to cancel bump generation.'],'No closing correctors'));
  341. else
  342. corlist4 = cell(1,length(corinds4));
  343. for count = 1:length(corinds4)
  344. corlist4{count} = BEAMLINE{corinds4(count)}.Name;
  345. end
  346. end
  347. end
  348. end
  349. if auto
  350. causality = false;
  351. while ~causality
  352. [cor4 ok] = listdlg('ListString',corlist4,'SelectionMode','Single',...
  353. 'Name','Fourth corrector','PromptString','Choose the 4th corrector',...
  354. 'ListSize',[200 600]);
  355. if ~ok; return; end
  356. if (BEAMLINE{corinds4(cor4)}.S <= BEAMLINE{corinds3(cor3)}.S) && ...
  357. (BEAMLINE{corinds4(cor4)}.S <= BEAMLINE{bumppos}.S)
  358. causality = false;
  359. warnh = warndlg(...
  360. ['Fourth corrector must be downstream of the third corrector ' ...
  361. BEAMLINE{cor1}.Name ' and the bump position ' ...
  362. BEAMLINE{bumppos}.Name],...
  363. 'Corrector location error');
  364. uiwait(warnh);
  365. else
  366. causality = true;
  367. end
  368. end
  369. end
  370. end
  371. % Find out if any cors are shared with the ring
  372. switch bumptype
  373. case 1 % 3 cor bump
  374. if exist('corinds1','var')
  375. cors = [corinds1(cor1) corinds2(cor2) corinds3(cor3)];
  376. end
  377. if ( strcmpi(BEAMLINE{cors(1)}.Name,'ZH100RX') || ...
  378. strcmpi(BEAMLINE{cors(1)}.Name,'ZH101RX') || ...
  379. strcmpi(BEAMLINE{cors(1)}.Name,'ZV100RX') || ...
  380. strcmpi(BEAMLINE{cors(2)}.Name,'ZH100RX') || ...
  381. strcmpi(BEAMLINE{cors(2)}.Name,'ZH101RX') || ...
  382. strcmpi(BEAMLINE{cors(2)}.Name,'ZV100RX') || ...
  383. strcmpi(BEAMLINE{cors(3)}.Name,'ZH100RX') || ...
  384. strcmpi(BEAMLINE{cors(3)}.Name,'ZH101RX') || ...
  385. strcmpi(BEAMLINE{cors(3)}.Name,'ZV100RX') )
  386. bumptype = 3;
  387. end
  388. case 2 % 4 cor bump
  389. if exist('corinds1','var')
  390. cors = [corinds1(cor1) corinds2(cor2) corinds3(cor3) corinds4(cor4)];
  391. end
  392. if ( strcmpi(BEAMLINE{cors(1)}.Name,'ZH100RX') || ...
  393. strcmpi(BEAMLINE{cors(1)}.Name,'ZH101RX') || ...
  394. strcmpi(BEAMLINE{cors(1)}.Name,'ZV100RX') || ...
  395. strcmpi(BEAMLINE{cors(2)}.Name,'ZH100RX') || ...
  396. strcmpi(BEAMLINE{cors(2)}.Name,'ZH101RX') || ...
  397. strcmpi(BEAMLINE{cors(2)}.Name,'ZV100RX') || ...
  398. strcmpi(BEAMLINE{cors(3)}.Name,'ZH100RX') || ...
  399. strcmpi(BEAMLINE{cors(3)}.Name,'ZH101RX') || ...
  400. strcmpi(BEAMLINE{cors(3)}.Name,'ZV100RX') || ...
  401. strcmpi(BEAMLINE{cors(4)}.Name,'ZH100RX') || ...
  402. strcmpi(BEAMLINE{cors(4)}.Name,'ZH101RX') || ...
  403. strcmpi(BEAMLINE{cors(4)}.Name,'ZV100RX') )
  404. bumptype = 4;
  405. end
  406. end
  407. % NOW DO THE CALCULATIONS!!
  408. switch bumptype
  409. case 1 % 3 cor bump
  410. % Generate matrices for drifts. Half the corrector length.
  411. if auto
  412. bumpcorinds = [corinds1(cor1) corinds2(cor2) corinds3(cor3)];
  413. else
  414. bumpcorinds = cors;
  415. end
  416. bumpcoefs = threecorbump(bumpcorinds, bumppos, plane);
  417. if auto
  418. comment = inputdlg('Please enter a comment describing this knob','Comment');
  419. if isempty(comment)
  420. return
  421. end
  422. else
  423. comment{1} = ['auto-generated knob. Target = element #' num2str(targ) ...
  424. ' ' num2str(type) '-corrector bump'];
  425. end
  426. % Adjust bumps for quads on movers
  427. for ibump=1:3
  428. if ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'COR')) ||...
  429. ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'MARK'))
  430. name{ibump}=['PS(' num2str(BEAMLINE{bumpcorinds(ibump)}.PS) ').SetPt'];
  431. else
  432. [stat R]=RmatAtoB(GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(1),...
  433. GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(end));
  434. if plane<3
  435. moverdim=1;
  436. bumpcoefs(ibump)=bumpcoefs(ibump)/R(2,1);
  437. else
  438. moverdim=2;
  439. bumpcoefs(ibump)=bumpcoefs(ibump)/R(4,3);
  440. end
  441. name{ibump}=['GIRDER{' num2str(BEAMLINE{bumpcorinds(ibump)}.Girder) '}.MoverSetPt(',num2str(moverdim),')'];
  442. end
  443. end
  444. [stat knob] = MakeMultiKnob(comment{1},...
  445. name{1},bumpcoefs(1),name{2},bumpcoefs(2),name{3},bumpcoefs(3));
  446. if stat{1}~=1; error(stat{2}); end
  447. if auto
  448. filename = uiputfile(...
  449. '~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/*.mat',...
  450. 'Save knob file');
  451. save(['~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/' filename],'knob')
  452. end
  453. return
  454. case 2 % 4 cor bump
  455. if auto
  456. bumpcorinds = [corinds1(cor1) corinds2(cor2) corinds3(cor3) corinds4(cor4)];
  457. else
  458. bumpcorinds = cors;
  459. end
  460. bumpcoefs = fourcorbump(bumpcorinds, bumppos, plane);
  461. if auto
  462. comment = inputdlg('Please enter a comment describing this knob','Comment');
  463. else
  464. comment{1} = ['auto-generated knob. Target = element #' num2str(targ) ...
  465. ' ' num2str(type) '-corrector bump'];
  466. end
  467. % Adjust bumps for quads on movers
  468. for ibump=1:4
  469. if ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'COR')) ||...
  470. ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'MARK'))
  471. name{ibump}=['PS(' num2str(BEAMLINE{bumpcorinds(ibump)}.PS) ').SetPt'];
  472. else
  473. [stat R]=RmatAtoB(GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(1),...
  474. GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(end));
  475. if plane<3
  476. moverdim=1;
  477. bumpcoefs(ibump)=bumpcoefs(ibump)/R(2,1);
  478. else
  479. moverdim=2;
  480. bumpcoefs(ibump)=bumpcoefs(ibump)/R(4,3);
  481. end
  482. name{ibump}=['GIRDER{' num2str(BEAMLINE{bumpcorinds(ibump)}.Girder) '}.MoverSetPt(',num2str(moverdim),')'];
  483. end
  484. end
  485. [stat knob] = MakeMultiKnob(comment{1},name{1},bumpcoefs(1),name{2},bumpcoefs(2),...
  486. name{3},bumpcoefs(3),name{4},bumpcoefs(4));
  487. if stat{1}~=1; error(stat{2}); end
  488. if auto
  489. filename = uiputfile(...
  490. '~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/*.mat',...
  491. 'Save knob file');
  492. save(['~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/' filename],'knob')
  493. end
  494. return
  495. case 3 % 3 cor bump shared with the ring
  496. % Generate matrices for drifts. Half the corrector length.
  497. if auto
  498. bumpcorinds = [corinds1(cor1) corinds2(cor2) corinds3(cor3)];
  499. else
  500. bumpcorinds = cors;
  501. end
  502. bumpcoefs = threecorbump(bumpcorinds, bumppos, plane);
  503. % We need to figure out how many cors are shared with the ring and
  504. % the amount they will kick the beam
  505. if plane==1 || plane==2
  506. ZH100RXind = findcells(BEAMLINE,'Name','ZH100RX');
  507. ZH101RXind = findcells(BEAMLINE,'Name','ZH101RX');
  508. if bumpcorinds(1)==ZH100RXind
  509. xp_1 = bumpcoefs(1);
  510. elseif bumpcorinds(2)==ZH100RXind
  511. xp_1 = bumpcoefs(1);
  512. elseif bumpcorinds(3)==ZH100RXind
  513. xp_1 = bumpcoefs(1);
  514. else
  515. xp_1 = 0;
  516. end
  517. if bumpcorinds(1)==ZH101RXind
  518. xp_2 = bumpcoefs(1);
  519. elseif bumpcorinds(2)==ZH101RXind
  520. xp_2 = bumpcoefs(2);
  521. elseif bumpcorinds(3)==ZH101RXind
  522. xp_2 = bumpcoefs(3);
  523. else
  524. xp_2 = 0;
  525. end
  526. ZH100Rind = findcells(BEAMLINE,'Name','ZH100R');
  527. ZH101Rind = findcells(BEAMLINE,'Name','ZH101R');
  528. xcorinds = findcells(BEAMLINE,'Class','XCOR');
  529. ringcor(1) = xcorinds( find(xcorinds==ZH101Rind) + 1 );
  530. ringcor(2) = xcorinds( find(xcorinds==ZH101Rind) + 2 );
  531. [stat R1toE] = RmatAtoB(ZH100Rind,ringcor(2));
  532. [stat R2toE] = RmatAtoB(ZH101Rind,ringcor(2));
  533. [stat R3toE] = RmatAtoB(ringcor(1),ringcor(2));
  534. x_Eorig = R1toE(1,2)*xp_1 + R2toE(1,2)*xp_2;
  535. xp_3 = -(x_Eorig / R3toE(1,2));
  536. xp_4 = -( R1toE(2,2)*xp_1 + R2toE(2,2)*xp_2 + R3toE(2,2)*xp_3 );
  537. rp_3 = xp_3;
  538. rp_4 = xp_4;
  539. elseif plane==3 || plane==4
  540. ZV100RXind = findcells(BEAMLINE,'Name','ZV100RX');
  541. if bumpcorinds(1)==ZV100RXind
  542. yp_1 = bumpcoefs(1);
  543. elseif bumpcorinds(2)==ZV100RXind
  544. yp_1 = bumpcoefs(1);
  545. elseif bumpcorinds(3)==ZV100RXind
  546. yp_1 = bumpcoefs(1);
  547. else
  548. yp_1 = 0;
  549. end
  550. yp_2 = 0; % There is only 1 shared ycor
  551. ZV100Rind = findcells(BEAMLINE,'Name','ZV100R');
  552. ycorinds = findcells(BEAMLINE,'Class','YCOR');
  553. ringcor(1) = ycorinds( find(ycorinds==ZV100Rind) + 1 );
  554. ringcor(2) = ycorinds( find(ycorinds==ZV100Rind) + 2 );
  555. [stat R1toE] = RmatAtoB(ZV100Rind,ringcor(2));
  556. R2toE = zeros(6); % There is only 1 shared ycor
  557. [stat R3toE] = RmatAtoB(ringcor(1),ringcor(2));
  558. y_Eorig = R1toE(3,4)*yp_1 + R2toE(3,4)*yp_2;
  559. yp_3 = -(y_Eorig / R3toE(3,4));
  560. yp_4 = -( R1toE(4,4)*yp_1 + R2toE(4,4)*yp_2 + R3toE(4,4)*yp_3 );
  561. rp_3 = yp_3;
  562. rp_4 = yp_4;
  563. else
  564. error('How the hell did you get here?')
  565. end
  566. if auto
  567. comment = inputdlg('Please enter a comment describing this knob','Comment');
  568. if isempty(comment)
  569. return
  570. end
  571. else
  572. comment{1} = ['auto-generated knob. Target = element #' num2str(targ) ...
  573. ' ' num2str(type) '-corrector bump'];
  574. end
  575. % Adjust bumps for quads on movers
  576. for ibump=1:3
  577. if ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'COR')) ||...
  578. ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'MARK'))
  579. name{ibump}=['PS(' num2str(BEAMLINE{bumpcorinds(ibump)}.PS) ').SetPt'];
  580. else
  581. [stat R]=RmatAtoB(GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(1),...
  582. GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(end));
  583. if plane<3
  584. moverdim=1;
  585. bumpcoefs(ibump)=bumpcoefs(ibump)/R(2,1);
  586. else
  587. moverdim=2;
  588. bumpcoefs(ibump)=bumpcoefs(ibump)/R(4,3);
  589. end
  590. name{ibump}=['GIRDER{' num2str(BEAMLINE{bumpcorinds(ibump)}.Girder) '}.MoverSetPt(',num2str(moverdim),')'];
  591. end
  592. end
  593. [stat knob] = MakeMultiKnob(comment{1},...
  594. name{1},bumpcoefs(1),name{2},bumpcoefs(2),name{3},bumpcoefs(3),...
  595. ['PS(' num2str(BEAMLINE{ringcor(1)}.PS) ').SetPt'],rp_3,...
  596. ['PS(' num2str(BEAMLINE{ringcor(2)}.PS) ').SetPt'],rp_4);
  597. if stat{1}~=1; error(stat{2}); end
  598. if auto
  599. filename = uiputfile(...
  600. '~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/*.mat',...
  601. 'Save knob file');
  602. save(['~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/' filename],'knob')
  603. end
  604. return
  605. case 4 % 4 cor bump shared with the ring
  606. if auto
  607. bumpcorinds = [corinds1(cor1) corinds2(cor2) corinds3(cor3) corinds4(cor4)];
  608. else
  609. bumpcorinds = cors;
  610. end
  611. bumpcoefs = fourcorbump(bumpcorinds, bumppos, plane);
  612. % We need to figure out how many cors are shared with the ring and
  613. % the amount they will kick the beam
  614. if plane==1 || plane==2
  615. ZH100RXind = findcells(BEAMLINE,'Name','ZH100RX');
  616. ZH101RXind = findcells(BEAMLINE,'Name','ZH101RX');
  617. if bumpcorinds(1)==ZH100RXind
  618. xp_1 = bumpcoefs(1);
  619. elseif bumpcorinds(2)==ZH100RXind
  620. xp_1 = bumpcoefs(1);
  621. elseif bumpcorinds(3)==ZH100RXind
  622. xp_1 = bumpcoefs(1);
  623. else
  624. xp_1 = 0;
  625. end
  626. if bumpcorinds(1)==ZH101RXind
  627. xp_2 = bumpcoefs(1);
  628. elseif bumpcorinds(2)==ZH101RXind
  629. xp_2 = bumpcoefs(2);
  630. elseif bumpcorinds(3)==ZH101RXind
  631. xp_2 = bumpcoefs(3);
  632. else
  633. xp_2 = 0;
  634. end
  635. ZH100Rind = findcells(BEAMLINE,'Name','ZH100R');
  636. ZH101Rind = findcells(BEAMLINE,'Name','ZH101R');
  637. xcorinds = findcells(BEAMLINE,'Class','XCOR');
  638. ringcor(1) = xcorinds( find(xcorinds==ZH101Rind) + 1 );
  639. ringcor(2) = xcorinds( find(xcorinds==ZH101Rind) + 2 );
  640. [stat R1toE] = RmatAtoB(ZH100Rind,ringcor(2));
  641. [stat R2toE] = RmatAtoB(ZH101Rind,ringcor(2));
  642. [stat R3toE] = RmatAtoB(ringcor(1),ringcor(2));
  643. x_Eorig = R1toE(1,2)*xp_1 + R2toE(1,2)*xp_2;
  644. xp_3 = -(x_Eorig / R3toE(1,2));
  645. xp_4 = -( R1toE(2,2)*xp_1 + R2toE(2,2)*xp_2 + R3toE(2,2)*xp_3 );
  646. rp_3 = xp_3;
  647. rp_4 = xp_4;
  648. elseif plane==3 || plane==4
  649. ZV100RXind = findcells(BEAMLINE,'Name','ZV100RX');
  650. if bumpcorinds(1)==ZV100RXind
  651. yp_1 = bumpcoefs(1);
  652. elseif bumpcorinds(2)==ZV100RXind
  653. yp_1 = bumpcoefs(1);
  654. elseif bumpcorinds(3)==ZV100RXind
  655. yp_1 = bumpcoefs(1);
  656. else
  657. yp_1 = 0;
  658. end
  659. yp_2 = 0; % There is only 1 shared ycor
  660. ZV100Rind = findcells(BEAMLINE,'Name','ZV100R');
  661. ycorinds = findcells(BEAMLINE,'Class','YCOR');
  662. ringcor(1) = ycorinds( find(ycorinds==ZV100Rind) + 1 );
  663. ringcor(2) = ycorinds( find(ycorinds==ZV100Rind) + 2 );
  664. [stat R1toE] = RmatAtoB(ZV100Rind,ringcor(2));
  665. R2toE = zeros(6); % There is only 1 shared ycor
  666. [stat R3toE] = RmatAtoB(ringcor(1),ringcor(2));
  667. y_Eorig = R1toE(3,4)*yp_1 + R2toE(3,4)*yp_2;
  668. yp_3 = -(y_Eorig / R3toE(3,4));
  669. yp_4 = -( R1toE(4,4)*yp_1 + R2toE(4,4)*yp_2 + R3toE(4,4)*yp_3 );
  670. rp_3 = yp_3;
  671. rp_4 = yp_4;
  672. else
  673. error('How the hell did you get here?')
  674. end
  675. if auto
  676. comment = inputdlg('Please enter a comment describing this knob','Comment');
  677. if isempty(comment)
  678. return
  679. end
  680. else
  681. comment{1} = ['auto-generated knob. Target = element #' num2str(targ) ...
  682. ' ' num2str(type) '-corrector bump'];
  683. end
  684. % Adjust bumps for quads on movers
  685. for ibump=1:4
  686. if ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'COR')) || ...
  687. ~isempty(findstr(BEAMLINE{bumpcorinds(ibump)}.Class,'MARK'))
  688. name{ibump}=['PS(' num2str(BEAMLINE{bumpcorinds(ibump)}.PS) ').SetPt'];
  689. else
  690. [stat R]=RmatAtoB(GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(1),...
  691. GIRDER{BEAMLINE{bumpcorinds(ibump)}.Girder}.Element(end));
  692. if plane<3
  693. moverdim=1;
  694. bumpcoefs(ibump)=bumpcoefs(ibump)/R(2,1);
  695. else
  696. moverdim=2;
  697. bumpcoefs(ibump)=bumpcoefs(ibump)/R(4,3);
  698. end
  699. name{ibump}=['GIRDER{' num2str(BEAMLINE{bumpcorinds(ibump)}.Girder) '}.MoverSetPt(',num2str(moverdim),')'];
  700. end
  701. end
  702. [stat knob] = MakeMultiKnob(comment{1},...
  703. name{1},bumpcoefs(1),...
  704. name{2},bumpcoefs(2),...
  705. name{3},bumpcoefs(3),...
  706. name{4},bumpcoefs(4),...
  707. ['PS(' num2str(BEAMLINE{ringcor(1)}.PS) ').SetPt'],rp_3,...
  708. ['PS(' num2str(BEAMLINE{ringcor(2)}.PS) ').SetPt'],rp_4);
  709. if stat{1}~=1; error(stat{2}); end
  710. if auto
  711. filename = uiputfile(...
  712. '~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/*.mat',...
  713. 'Save knob file');
  714. save(['~/Lucretia/src/Floodland/testApps/bumpgui/savedbumps/' filename],'knob')
  715. end
  716. return
  717. end