helicalDosecalcSetup7_fullRO.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. %%
  2. % This version was modified by Peter Ferjancic to accept .nrrd file format
  3. % for target. It was also rollbacked on some parameters back to the
  4. % clinical system settings.
  5. % This file has been modified by Surendra Prajapati especially to run
  6. % WiscPlan for KV beams. Works for running locally as well as in Server.
  7. %
  8. % RTF 11/4/05
  9. % Sets up the run files needed for running a Condor convolution
  10. % superposition dose calculation with photon beams.
  11. % Versions of the convolution code that run in both Windows XP, Condor, and
  12. % directly through Matlab can all be created here. The only real
  13. % differences between the files are in the access conventions, which just%
  14. % This has to do with switching forward slashes with backslashes.
  15. % Surendra edits: num_batches, SAD, pitch, xpmin/max, ypmin/max, Mxp, Nphi
  16. % Also edit:
  17. % Kernel should always be named Kernels.mat
  18. function num_batches = helicalDosecalcSetup7_fullRO(patient_dir, OptGoals, beamlet_dir)
  19. % -- INPUT:
  20. % patient_dir: specify where kernel and [geometry] files are located
  21. iso = [0 0 0]; % Point about which gantry rotations begin
  22. SAD = 85; % source-axis distance for the x-ray source ##
  23. pitch = 0.86; % fraction of beam with couch translates per rotation
  24. %% --- make the figure prompt for number of angles and beamlets
  25. str = inputdlg({'Enter number of calc cores', 'Enter number of angles (51 default)', ...
  26. 'Enter number of beamlets (64 default)'}, 'input', [1,35], {'1', '51', '64'});
  27. num_batches = str2double(str{1}); % number of cores you want to run the beam calculation
  28. % -- (3 for a 4-core comp to prevent lockdown)
  29. N_angles = str2double(str{2}); % 51 for full resolution
  30. Mxp = str2double(str{3}); % Mxp = 64; number of MLC leaves;
  31. Nyp = 1; % always 1 for Tomo due to binary mlc
  32. % define the overall beam field size for each beam angle
  33. % beam is 40 cm wide in transverse direction and 1-5 cm (usually 2) in y
  34. % direction.
  35. % isocenter is 85 cm from source, ends of jaws are 23 cm from source
  36. xpmin = -20.0; % -field width / 2
  37. xpmax = 20.0; % +field width / 2
  38. % ypmin = -0.3125; % total jaw width is 0.625 cm
  39. % ypmax = 0.3125;
  40. % ypmin = -0.5; % total jaw width is 1 cm
  41. % ypmax = 0.5;
  42. ypmin = -1.25; % total jaw width is 2.5 cm
  43. ypmax = 1.25;
  44. % y-prime points in the z-direction in the CT coordinate system
  45. % ============================================= End of user-supplied inputs
  46. % executable_path = 'C:\010-work\003_localGit\WiscPlan_v2\WiscPlanPhotonkV125\WiscPlanEXE\RyanCsphoton.x86.exe';
  47. executable_path = mfilename('fullpath');
  48. executable_path = [executable_path(1:end-37), 'WiscPlanEXE\RyanCsphoton.x86.exe']
  49. kernel_file = 'Kernels.mat';
  50. geometry_file = fullfile(patient_dir, 'matlab_files\Geometry.mat');
  51. load(geometry_file);
  52. ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  53. [target_idx, okay] = listdlg('ListString', ROI_names, ...
  54. 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  55. 'PromptString', 'Please select the target ROI for beamlet calc. ');
  56. if okay ~= 1
  57. msgbox('Plan creation aborted');
  58. return;
  59. end
  60. targetMask = zeros(size(Geometry.data));
  61. targetMask(Geometry.ROIS{target_idx}.ind) = 1;
  62. % Grozomah - targetMask needs to get a 'double' matrix with the location of
  63. % the target
  64. targetMaskZ = sum(sum(targetMask,1),2);
  65. zBow = (find(targetMaskZ>0, 1, 'first')-1)*Geometry.voxel_size(3) + Geometry.start(3) + ypmin;
  66. zStern = (find(targetMaskZ>0, 1, 'last')+1)*Geometry.voxel_size(3) + Geometry.start(3) + ypmax;
  67. [subi, subj, subk] = ind2sub(size(Geometry.data), Geometry.ROIS{target_idx}.ind);
  68. iso = [Geometry.start(1)+Geometry.voxel_size(1)*mean(subi) ...
  69. Geometry.start(2)+Geometry.voxel_size(2)*mean(subj) 0];
  70. % flags used to select which calculations will be set up
  71. Condor_flag = 1;
  72. ptvInd = target_idx; % PTV index in Geometry.ROIS
  73. fieldWidth = ypmax - ypmin;
  74. % total number of rotations required for treatment
  75. Nrot = ceil(abs(zBow - zStern)/(pitch*fieldWidth));
  76. % Nphi = Nrot*51; % number of angles used in the calculation
  77. Nphi = Nrot * N_angles; % Grozomah
  78. % define the limits of the angles that will be used for the calculation
  79. % ##phimin = 0; % starting angle in radians
  80. % ##phimax = 2*pi*Nphi;
  81. phi = [0:Nphi-1]/Nphi *2*pi*Nrot;
  82. condor_folder = patient_dir;
  83. winxp_folder = 'winxp';
  84. % create names for condor input and output folders
  85. input_folder = '.';
  86. output_folder = '.';
  87. % name of the convolution/superposition executable, which will be in the
  88. % 'code' folder of each respective run type folder
  89. condor_exectuable_name = 'convolutionCondor'; % relative path on the cluster where code will be
  90. winxp_executable_name = 'convolution.exe';
  91. matlab_executable_name = 'convolution_mex'; % name of the Matlab version of the dose calculation code
  92. % set the beam parameters, assuming a helical beam trajectory
  93. % folders that will be inside the 'input' folder
  94. beamspec_folder = 'beamspecfiles'; % directory where beam files will be stored
  95. beamspec_batches_folder = 'beamspecbatches';
  96. beamspec_batch_base_name = 'beamspecbatch'; % base name for a beamlet batch file
  97. kernel_folder = 'kernelfiles'; % folder where kernel information will be saved
  98. kernel_filenames_condor = 'kernelFilenamesCondor.txt';
  99. kernel_filenames_winxp = 'kernelFilenamesWinXP.txt';
  100. % output folders
  101. beamlet_batch_base_name = 'beamletbatch'; % base name for a dose batch file
  102. geometry_header_filename = 'geometryHeader.txt';
  103. geometry_density_filename = 'density.bin'; % save the density, not the Hounsfield units!
  104. % end of user-defined parameters
  105. % check the validity of the user-defined variables
  106. if xpmin >= xpmax
  107. error('xpmin must be less than xpmax.');
  108. end
  109. if ypmin >= ypmax
  110. error('ypmin must be less than ypmax.');b
  111. end
  112. % if phimin > phimax
  113. % error('phimin must be less than or equal to phimax.');
  114. % end
  115. if Mxp <= 0 || Nyp <= 0 || Nphi <= 0
  116. error('Mxp, Nyp, and Nphi must be greater than zero.');
  117. end
  118. if SAD < 50
  119. error('It is recommended that the SAD be greater than 50 cm.');
  120. end
  121. % the xy plane is perpendicular to the isocenter axis of the linac gantry
  122. % size of each beam aperture, making them vectors so extension to
  123. % non-uniform aperture sizes becomes obvious
  124. del_xp = (xpmax - xpmin)/Mxp;
  125. del_yp = (ypmax - ypmin)/Nyp;
  126. % Calculate the xp and yp offsets, which lie at the centers of the
  127. % apertures.
  128. xp = [xpmin:del_xp:xpmax-del_xp] + del_xp/2;
  129. yp = [ypmin:del_yp:ypmax-del_yp] + del_yp/2;
  130. [M,N,Q] = size(Geometry.rhomw);
  131. START = single(Geometry.start - iso);
  132. INC = single(Geometry.voxel_size);
  133. % Grozomah ##
  134. % START(1) = START(1)/10;
  135. % START(2) = START(2)/10;
  136. % INC(1) = INC(1)/10;
  137. % INC(2) = INC(2)/10;
  138. % END= START+[32,32,40].*INC
  139. % define the tumor mask
  140. tumorMask = zeros(size(Geometry.rhomw),'single');
  141. tumorMask(Geometry.ROIS{ptvInd}.ind) = 1;
  142. BW = bwdist(tumorMask);
  143. tumorMaskExp = tumorMask;
  144. tumorMaskExp(BW <= 4) = 1;
  145. P = zeros(Mxp,Nphi);
  146. fprintf('Checking beam''s eye view ...\n');
  147. for p=1:Nphi
  148. % ir and jr form the beam's eye view (BEV)
  149. ir = [-sin(phi(p)); cos(phi(p)); 0];
  150. jr = [0 0 1]';
  151. % kr denotes the beam direction
  152. kr = [cos(phi(p)); sin(phi(p)); 0];
  153. for m=1:Mxp
  154. point1 = single(-kr*SAD + [0 0 zBow + pitch*fieldWidth*phi(p)/(2*pi)]'); % source point
  155. point2 = single(point1 + (SAD*kr + ir*xp(m))*10);
  156. [indVisited,deffVisited] = singleRaytraceClean(tumorMaskExp,START,INC,point1,point2);
  157. if ~isempty(indVisited)
  158. P(m,p) = max(deffVisited);
  159. end
  160. end
  161. end
  162. fprintf('Finished checking BEV\n');
  163. % load data required for the dose calculator
  164. load(kernel_file);
  165. Geometry.rhomw(Geometry.rhomw < 0) = 0;
  166. Geometry.rhomw(Geometry.rhomw < 0.0013) = 0.0013; % fill blank voxels with air
  167. % convert Geometry and kernels to single
  168. f = fieldnames(Kernels);
  169. for k=1:length(f)
  170. if isnumeric(getfield(Kernels,f{k}))
  171. Kernels = setfield(Kernels,f{k},single(getfield(Kernels,f{k})));
  172. end
  173. end
  174. f = fieldnames(Geometry);
  175. for k=1:length(f)
  176. if isnumeric(getfield(Geometry,f{k}))
  177. Geometry = setfield(Geometry,f{k},single(getfield(Geometry,f{k})));
  178. end
  179. end
  180. % account for isocenter
  181. Geometry.start_nominal = single(Geometry.start - iso);
  182. %% account for beamlet shift
  183. for scenario_i = 1 % :numel(OptGoals.sss_scene_list)
  184. disp(OptGoals.sss_scene_list{scenario_i});
  185. % change Condor folder names as appropriate
  186. condor_folder_scenario = [beamlet_dir '\scenario' num2str(scenario_i)];
  187. mkdir(condor_folder_scenario)
  188. patient_dir_scenario = condor_folder_scenario
  189. % do the isocenter shift
  190. shift = OptGoals.sss_scene_list{scenario_i}; % Y X Z
  191. iso = [iso(1)+Geometry.voxel_size(1)*shift(1) ...
  192. iso(2)+Geometry.voxel_size(2)*shift(2) ...
  193. iso(3)+Geometry.voxel_size(3)*shift(3)];
  194. Geometry.start = Geometry.start_nominal- iso;
  195. % find the total number of beams
  196. Nbeam = Nphi*Mxp*Nyp;
  197. batch_num = 0; % start the count for the number of total batches
  198. % fill up a cell array of beam structures, grouped by batch
  199. clear batches;
  200. batch_num = 0;
  201. batches = cell(1,Nrot); % start the batches cell array (cell array of beam batches)
  202. rotNum = 0;
  203. % calculate beams for all source directions and apertures
  204. for k=1:Nphi % loop through all gantry angles
  205. % calculate the source location for a helical trajectory
  206. beam.SAD = single(SAD);
  207. % the kp vector is the beam direction, ip and jp span the beam's eye view
  208. beam.ip = single([-sin(phi(k)) cos(phi(k)) 0]);
  209. beam.jp = single([0 0 1]);
  210. beam.kp = single([cos(phi(k)) sin(phi(k)) 0]);
  211. beam.y_vec = single(-beam.kp*SAD + [0 0 zBow + pitch*fieldWidth*phi(k)/(2*pi)]);
  212. rotNumOld = rotNum;
  213. rotNum = floor(k/51) + 1; % current rotation number
  214. if rotNum - rotNumOld > 0
  215. beam_num = 0; % if the rotation number has changed, start the beam count over
  216. end
  217. for m=1:Mxp % loop through all apertures in the xp-direction
  218. % calculate the beam if the tomotherapy fluence value is non-zero
  219. if P(m,k) > 0
  220. num = m + (k-1)*Mxp - 1; % beamlet number (overall)
  221. beam_num = beam_num + 1;
  222. % set the beam aperture parameters
  223. beam.del_xp = single(del_xp);
  224. beam.del_yp = single(del_yp);
  225. beam.xp = single(xp(m));
  226. beam.yp = single(0);
  227. beam.num = single(num); % record the beam number to avoid any later ambiguity
  228. batches{rotNum}{beam_num} = beam;
  229. end
  230. end
  231. end
  232. % merge/split batches
  233. all_beams = horzcat(batches{:});
  234. num_beams_per_batch = ceil(numel(all_beams)/num_batches);
  235. batches = cell(num_batches,1);
  236. for k = 1:(num_batches)
  237. beams_idx = 1+num_beams_per_batch*(k-1):num_beams_per_batch*k;
  238. beams_idx (beams_idx>numel(all_beams)) = [];
  239. batches{k} = all_beams(beams_idx);
  240. end
  241. % batches{num_batches} = all_beams(1+num_beams_per_batch*(k):end);
  242. % Everything else in this file is related to saving the batches in a
  243. % useable form.
  244. if Condor_flag == 1
  245. % delete the old submission file
  246. err = rmdir(fullfile(condor_folder_scenario,beamspec_batches_folder),'s');
  247. err = rmdir(fullfile(condor_folder_scenario,kernel_folder),'s');
  248. % create folders where batch information will be sent
  249. mkdir([condor_folder_scenario '/' input_folder '/' beamspec_batches_folder]);
  250. % save the kernels
  251. save_kernels(Kernels,[condor_folder_scenario '/' input_folder '/' kernel_folder]);
  252. fprintf(['Successfully saved Condor kernels to ' input_folder '/' kernel_folder '\n']);
  253. % create kernel filenames files
  254. kernel_filenames_CHTC = 'kernelFilenamesCHTC.txt';
  255. kernel_filenames_condor = 'kernelFilenamesCondor.txt';
  256. fid = fopen([condor_folder_scenario '/' input_folder '/' kernel_filenames_condor],'w');
  257. fid2 = fopen([condor_folder_scenario '/' input_folder '/' kernel_filenames_CHTC],'w');
  258. fprintf(fid,'kernel_header\n');
  259. % fprintf(fid,['./' input_folder '/' kernel_folder '/kernel_header.txt\n']);
  260. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'kernel_header.txt'));
  261. fprintf(fid2,'kernel_header\n');
  262. fprintf(fid2, '%s/%s\n', kernel_folder,'kernel_header.txt');
  263. fprintf(fid,'kernel_radii\n');
  264. % fprintf(fid,['./' input_folder '/' kernel_folder '/radii.bin\n']);
  265. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'radii.bin'));
  266. fprintf(fid2,'kernel_radii\n');
  267. fprintf(fid2, '%s/%s\n', kernel_folder,'radii.bin');
  268. fprintf(fid,'kernel_angles\n');
  269. % fprintf(fid,['./' input_folder '/' kernel_folder '/angles.bin\n']);
  270. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'angles.bin'));
  271. fprintf(fid2,'kernel_angles\n');
  272. fprintf(fid2, '%s/%s\n', kernel_folder,'angles.bin');
  273. fprintf(fid,'kernel_energies\n');
  274. % fprintf(fid,['./' input_folder '/' kernel_folder '/energies.bin\n']);
  275. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'energies.bin'));
  276. fprintf(fid2,'kernel_energies\n');
  277. fprintf(fid2, '%s/%s\n', kernel_folder,'energies.bin');
  278. fprintf(fid,'kernel_primary\n');
  279. % fprintf(fid,['./' input_folder '/' kernel_folder '/primary.bin\n']);
  280. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'primary.bin'));
  281. fprintf(fid2,'kernel_primary\n');
  282. fprintf(fid2, '%s/%s\n', kernel_folder,'primary.bin');
  283. fprintf(fid,'kernel_first_scatter\n');
  284. % fprintf(fid,['./' input_folder '/' kernel_folder '/first_scatter.bin\n']);
  285. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'first_scatter.bin'));
  286. fprintf(fid2,'kernel_first_scatter\n');
  287. fprintf(fid2, '%s/%s\n', kernel_folder,'first_scatter.bin');
  288. fprintf(fid,'kernel_second_scatter\n');
  289. % fprintf(fid,['./' input_folder '/' kernel_folder '/second_scatter.bin\n']);
  290. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'second_scatter.bin'));
  291. fprintf(fid2,'kernel_second_scatter\n');
  292. fprintf(fid2, '%s/%s\n', kernel_folder,'second_scatter.bin');
  293. fprintf(fid,'kernel_multiple_scatter\n');
  294. % fprintf(fid,['./' input_folder '/' kernel_folder '/multiple_scatter.bin\n']);
  295. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'multiple_scatter.bin'));
  296. fprintf(fid2,'kernel_multiple_scatter\n');
  297. fprintf(fid2, '%s/%s\n', kernel_folder,'multiple_scatter.bin');
  298. fprintf(fid,'kernel_brem_annih\n');
  299. % fprintf(fid,['./' input_folder '/' kernel_folder '/brem_annih.bin\n']);
  300. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'brem_annih.bin'));
  301. fprintf(fid2,'kernel_brem_annih\n');
  302. fprintf(fid2, '%s/%s\n', kernel_folder,'brem_annih.bin');
  303. fprintf(fid,'kernel_total\n');
  304. % fprintf(fid,['./' input_folder '/' kernel_folder '/total.bin\n']);
  305. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'total.bin'));
  306. fprintf(fid2,'kernel_total\n');
  307. fprintf(fid2, '%s/%s\n', kernel_folder,'total.bin');
  308. fprintf(fid,'kernel_fluence\n');
  309. % fprintf(fid,['./' input_folder '/' kernel_folder '/fluence.bin\n']);
  310. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'fluence.bin'));
  311. fprintf(fid2,'kernel_fluence\n');
  312. fprintf(fid2, '%s/%s\n', kernel_folder,'fluence.bin');
  313. fprintf(fid,'kernel_mu\n');
  314. % fprintf(fid,['./' input_folder '/' kernel_folder '/mu.bin\n']);
  315. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'mu.bin'));
  316. fprintf(fid2,'kernel_mu\n');
  317. fprintf(fid2, '%s/%s\n', kernel_folder,'mu.bin');
  318. fprintf(fid,'kernel_mu_en\n');
  319. % fprintf(fid,['./' input_folder '/' kernel_folder '/mu_en.bin\n']);
  320. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,kernel_folder,'mu_en.bin'));
  321. fprintf(fid2,'kernel_mu_en\n');
  322. fprintf(fid2, '%s/%s\n', kernel_folder,'mu_en.bin');
  323. fclose(fid);
  324. end
  325. % name for the condor submit file that will be used
  326. condor_submit_file = 'convolutionSubmit.txt';
  327. geometry_filenames_condor = 'geometryFilenamesCondor.txt';
  328. geometry_filenames_CHTC = 'geometryFilenamesCHTC.txt';
  329. % check the geometry file to ensure that it's not in Hounsfield units
  330. if length(find(Geometry.rhomw > 20)) || length(find(Geometry.rhomw < 0))
  331. error('Double check the Geometry structure, it may still be in Hounsfield units!');
  332. end
  333. geometry_folder = 'geometryfiles';
  334. batch_output_folder = 'batchoutput'; % folder to which stdout will be printed
  335. beamlet_batches_folder = 'beamletbatches'; % folder where resulting beamlet batches will be stored
  336. if Condor_flag == 1
  337. mkdir([condor_folder_scenario '/' output_folder '/' beamlet_batches_folder]);
  338. mkdir([condor_folder_scenario '/' output_folder '/' batch_output_folder]);
  339. save_geometry(Geometry,[condor_folder_scenario '/' input_folder '/' geometry_folder],geometry_header_filename,geometry_density_filename);
  340. fprintf(['Successfully saved Condor geometry to ' input_folder '/' geometry_folder '\n']);
  341. % create geometry filenames files
  342. fid = fopen([condor_folder_scenario '/' input_folder '/' geometry_filenames_condor],'w');
  343. fid2 = fopen([condor_folder_scenario '/' input_folder '/' geometry_filenames_CHTC],'w');
  344. fprintf(fid,'geometry_header\n');
  345. % fprintf(fid,['./' input_folder '/' geometry_folder '/' geometry_header_filename '\n']);
  346. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,geometry_folder,geometry_header_filename));
  347. fprintf(fid2,'geometry_header\n');
  348. fprintf(fid2, '%s/%s\n', geometry_folder,'geometryHeader.txt');
  349. fprintf(fid,'geometry_density\n');
  350. % fprintf(fid,['./' input_folder '/' geometry_folder '/' geometry_density_filename '\n']);
  351. fprintf(fid,'%s\n',fullfile(patient_dir_scenario,input_folder,geometry_folder,geometry_density_filename));
  352. fprintf(fid2,'geometry_density\n');
  353. fprintf(fid2, '%s/%s\n', geometry_folder,'density.bin');
  354. fclose(fid);
  355. % write command file
  356. % TODO consistent naming throughout script
  357. for k = 1:numel(batches)
  358. fid = fopen(fullfile(condor_folder_scenario,sprintf('run%d.cmd',k-1)), 'w');
  359. fprintf(fid, '"%s" "%s" "%s" "%s" "%s"', executable_path,...
  360. fullfile(patient_dir_scenario, kernel_filenames_condor),...
  361. fullfile(patient_dir_scenario, geometry_filenames_condor),...
  362. fullfile(patient_dir_scenario, 'beamspecbatches', sprintf('beamspecbatch%d.txt',k-1)),...
  363. fullfile(patient_dir_scenario, sprintf('batch_dose%d_S%d.bin',k-1, scenario_i)));
  364. fclose(fid);
  365. end
  366. % write the condor submit file
  367. % beamspec_batch_filename = ['./' input_folder '/' beamspec_batches_folder '/' beamspec_batch_base_name '$(Process).txt'];
  368. % beamlet_batch_filename = ['./' output_folder '/' beamlet_batches_folder '/' beamlet_batch_base_name '$(Process).bin'];
  369. % fid = fopen([condor_folder '/' condor_submit_file],'w');
  370. % fprintf(fid,'###############################################################\n');
  371. % fprintf(fid,'# Condor submission script for convolution/superposition code\n');
  372. % fprintf(fid,'###############################################################\n\n');
  373. % fprintf(fid,'copy_to_spool = false\n');
  374. % fprintf(fid,['Executable = ' code_folder '/' condor_exectuable_name '\n']);
  375. % fprintf(fid,['arguments = ' input_folder '/' kernel_filenames_condor ' ' input_folder '/' geometry_filenames_condor ' ' beamspec_batch_filename ' ' beamlet_batch_filename '\n']);
  376. % fprintf(fid,['Output = ./' output_folder '/' batch_output_folder '/batchout$(Process).txt\n']);
  377. % fprintf(fid,['Log = ./' output_folder '/' batch_output_folder '/log.txt\n']);
  378. % fprintf(fid,['Queue ' num2str(Nrot)]);
  379. % fclose(fid);
  380. % % write the condor submit file
  381. % beamspec_batch_filename = ['./' input_folder '/' beamspec_batches_folder '/' beamspec_batch_base_name '$(Process).txt'];
  382. % beamlet_batch_filename = ['./' output_folder '/' beamlet_batches_folder '/' beamlet_batch_base_name '$(Process).bin'];
  383. fid = fopen([condor_folder_scenario '/' condor_submit_file],'w');
  384. fprintf(fid,'###############################################################\n');
  385. fprintf(fid,'# Condor submission script for convolution/superposition code\n');
  386. fprintf(fid,'###############################################################\n\n');
  387. fprintf(fid,'copy_to_spool = false\n');
  388. fprintf(fid,['Executable = ' condor_exectuable_name '\n']);
  389. fprintf(fid,['Arguments = ' kernel_filenames_CHTC ' ' geometry_filenames_CHTC ' ' beamspec_batch_base_name '$(Process).txt ' 'batch_dose$(Process).bin\n']);
  390. fprintf(fid,['Transfer_input_files = ' kernel_folder ',' geometry_folder ',' beamspec_batches_folder '/' beamspec_batch_base_name '$(Process).txt' ',' kernel_filenames_CHTC ',' geometry_filenames_CHTC '\n']);
  391. fprintf(fid,['Request_memory = 1000' '\n']);
  392. fprintf(fid,['Request_disk = 500000' '\n']);
  393. fprintf(fid,['Output = $(Cluster).out' '\n']);
  394. fprintf(fid,['Log = $(Cluster).log' '\n']);
  395. fprintf(fid,['Error = $(Cluster).err' '\n']);
  396. fprintf(fid,['Queue ' num2str(num_batches) '\n']);
  397. % fclose(fid);
  398. end
  399. % write the batches to files
  400. for n=1:numel(batches)
  401. batch = batches{n}; % current batch
  402. if Condor_flag == 1
  403. save_beamspec_batch(batch,[condor_folder_scenario '/' input_folder '/' beamspec_batches_folder],[beamspec_batch_base_name num2str(n-1) '.txt']);
  404. end
  405. end
  406. all_beams{1}.Mxp = Mxp;
  407. all_beams{1}.N_angles = N_angles;
  408. all_beams{1}.num_batches = num_batches;
  409. save([condor_folder_scenario '\all_beams.mat'], 'all_beams');
  410. % for k = 1:numel(batches)
  411. % system([fullfile(patient_dir,sprintf('run%d.cmd',k-1)) ' &']);
  412. % end
  413. % Ask for User option to run the dose calculation locally on the computer
  414. % or just to get necessary files for CHTC server
  415. % 'y' means run locally, 'n' means not to run locally on the computer
  416. strBeamlet = '';
  417. while(1)
  418. if strcmpi('y',strBeamlet)
  419. break;
  420. elseif strcmpi('n',strBeamlet)
  421. break;
  422. end
  423. % strBeamlet = input('Run beamlet batches dose calculation locally? y/n \n','s');
  424. strBeamlet = 'y'; %bypass question
  425. end
  426. t = datetime('now');
  427. disp(['Calculating ' num2str(size(all_beams, 2)) ' beamlets in ' num2str(size(batches, 1))...
  428. ' batches. Start: ' datestr(t)])
  429. if(strcmpi('y',strBeamlet))
  430. for k = 1:numel(batches)
  431. system([fullfile(patient_dir_scenario,sprintf('run%d.cmd',k-1)) ' &']);
  432. end
  433. end
  434. end % end of scenario loop
  435. end