Dear Dr. Fang,
I am currently working on a research project that requires running a large number of mcxlab simulations. Given the broad range of optical properties and source/detector configurations we need to test, the estimated runtime is several months. We have already minimized the number of required simulations, and simply reducing the number of launched photons is not an option, as we have determined a necessary photon count to ensure negligible statistical error. Furthermore, we intend to apply weighting afterward for different absorption values and therefore run the initial simulation with μ_a = 0, so setting cfg.minenergy also does not help. Thus, we are hoping there might be further optimizations to reduce runtime.
In our simulations, we are only interested in storing detected photon information. Flux and trajectory data are unnecessary for our analysis. Although we have already minimized the simulation volume resolution, the resulting speed improvement was marginal. This has led us to wonder if there might be additional calculations—either in settings or within the source code—that we could suppress to enhance simulation speed, given that our sole interest is the det.data output.
To provide a clearer picture of our approach, I’ve included some matlab code below that outlines the core of our simulation setup. Any guidance on potential optimizations to reduce runtime would be greatly appreciated.
Kind regards,
Robin
% Basic pencil beam simulation to calculate the reflectance as a function of radial distance later on for different absorption and detector sizes (SFR)
% General params
cfg.nphoton = 1e8;
cfg.vol = uint8(ones(60,60,60)); % Not reduced here
cfg.unitinmm = 1;
cfg.gpuid = 1;
cfg.autopilot = 1;
cfg.tstart = 0;
cfg.tend = 5e-9;
cfg.tstep = 5e-9;
cfg.isreflect = 0;
% Sim specific settings
% Different phasefunctions not included here
mu_sp = 5; % per mm
NA = 0.22;
% General source and detector params
cfg.issrcfrom0 = 1;
cfg.respin = 1;
cfg.srctype = 'pencil';
cfg.srcpos = [30 30 0];
cfg.srcdir = [0 0 1];
cfg.bc = 'aa_aaa001000';
cfg.maxdetphoton = cfg.nphoton;
cfg.maxjumpdebug = 1e5;
cfg.savedetflag = 'dspxv';
max_NA = NA;
theta_acc_max = asin(max_NA / 1.35);
cfg.angleinvcdf = linspace(0, asin(max_NA / 1.35) / pi, 5);
cfg.prop = [0 0 1 1; 0 mu_sp / (1 - 0.9) 0.9 1.35];
cfg.seed = -1;
[~, det, ~, ~, ~] = mcxlab(cfg);
r_max = fiber_diameter; % Not defined here
r_tmp = sqrt(sum((tmp_det.p(:, 1:2) - cfg.srcpos(1:2)).^2, 2));
det.data(:, r_tmp > r_max) = []; % Drop outside radius
% Remove photons outside acceptance angle
tmp_angles = acos(-(tmp_det.data(9, :)));
det.data(:, tmp_angles > theta_acc_max) = [];
save(‘detected photons’, ‘det.data’)
If you type [~, det, ~, ~, ~] = mcxlab(cfg) as in your example, mcxlab will compute all 5 outputs because nargout = 5. If you want to skip the last 3, you just use [flux, det]=mcxlab(). If you do not want to allocate flux, just clear it afterwards. [~, det] won't save any computation


