I'm working with widefield source and detector in a reflectance configuration and have encountered several inconsistencies when running the experiments.
Depending on the order of the calls to mccaddsrc and mmcadddet the mesh completely changes, even sometimes removing the -1 elem of the light source. This causes a variety of bugs from random hangs, to incorrect detected photosn.
Below I present four simple experiments on a 2 layer cube using a widefield source and detector to explain the issues I've encountered.
Common matlab code to generate the cubes:
voxelCube = ones(20, 20, 20);
voxelCube = uint8(voxelCube);
voxelCube(:,:,1:10) = 2;
[node, elem, face] = v2m(voxelCube, [], 1, 1, 'cgalmesh');
- CASE 1: Source further than detector.
- CASE 1.1: First call adddet and then addsrc:
srcPos = [5, 5, 35]; % FURTHER
detPos = [10,10,25]; % CLOSER
srcStruct = struct( ...
'srctype', 'planar', ...
'srcpos', srcPos, ...
'srcdir', [ 0,0,-1], ...
'srcparam1', [10 0 0 0], ...
'srcparam2', [0 10 0 0]);
detStruct = struct( ...
'srctype', 'disk', ...
'srcpos', detPos, ...
'srcdir', [ 0,0,-1], ...
'srcparam1', [20 0 0 0], ...
'srcparam2', [0 0 0 0]);
[nodes, elem] = mmcadddet(nodes, elem, detStruct);
[nodes, elem] = mmcaddsrc(nodes, elem, srcStruct);
elem(:, 1:4) = meshreorient(nodes, elem(:, 1:4));
eid = tsearchn(nodes, elem(:,1:4), srcPos);
savemmcmesh(fileNameBase, nodes, elem);
plotmesh(nodes, elem, 'y>8');
un = unique(elem(:,5));
We can correctly see cube (yellows), remesh scaffolding (light blue), source (blue) and detector (dark blue).
- CASE 1.2: First call addsrc and then adddet: (Just changed the order in the following lines)
[nodes, elem] = mmcaddsrc(nodes, elem, srcStruct);
[nodes, elem] = mmcadddet(nodes, elem, detStruct);
Detector is moved to the side.
- CASE 2: Detector further than source
- CASE 2.1: First call adddet and then addsrc:
srcPos = [5, 5, 25]; % CLOSER
detPos = [10,10,35]; % FURTHER
In this case, the unique elements show [-2;0;1;2], light source (-1) is gone, all the other show [-2;-1;0;1;2],
- CASE 2.2: First call addsrc and then adddet:
[nodes, elem] = mmcaddsrc(nodes, elem, srcStruct);
[nodes, elem] = mmcadddet(nodes, elem, detStruct);
This causes a range of inconsistencies also when running the simulations. As I detail below:- CASE 1.1: No detected photons. Fluence looks ok.
retrieving flux ... saved 4232477 trajectory positions, total: 4232477 detected 0 photons, total: 0 transfer complete: 1909 ms
saving data to file ... saving data complete : 1914 ms
simulated 100000 photons (100000) with 1 devices (ray-tet 18890768)
MCX simulation speed: 187.97 photon/ms
total simulated energy: 100000.00 absorbed: 70.26094%
- CASE 1.2: Simulation just hangs forever, when activating -M P and cancelling progress stays at 99%, had to reduce number
of photons to 10000. Still hang is not deterministic, sometimes it hangs forever and sometimes
it finishes successfully, seems like the number of photons increases probability of hang.(maybe
as there are more photons they reach some bad element or something that causes the hang, just
speculation). This is the output from one of the successful runs.
retrieving flux ... saved 417725 trajectory positions, total: 417725 detected 522 photons, total: 522 transfer complete: 1729 ms
saving data to file ... saving data complete : 1733 ms
simulated 10000 photons (10000) with 1 devices (ray-tet 1556636)
MCX simulation speed: 23.87 photon/ms
total simulated energy: 10000.00 absorbed: 69.90376%
Even though it seems correct, when plotting the photons detected, they lie in that triangle faces that we saw
in the figure of the case 1.2, not in the detector:

- CASE 2.1: No detected photons, no absorbed energy:
retrieving flux ... saved 200000 trajectory positions, total: 200000 detected 0 photons, total: 0 transfer complete: 1770 ms
saving data to file ... saving data complete : 1772 ms
simulated 100000 photons (100000) with 1 devices (ray-tet 400000)
MCX simulation speed: 235.85 photon/ms
total simulated energy: 100000.00 absorbed: 0.00000%
- CASE 2.2: Hangs again, same as case 1.2, when reducing photons to 10000 the simulation
randomly hangs or finishes.
retrieving flux ... saved 422819 trajectory positions, total: 422819 detected 4017 photons, total: 4017 transfer complete: 1804 ms
saving data to file ... saving data complete : 1809 ms
simulated 10000 photons (10000) with 1 devices (ray-tet 1618513)
MCX simulation speed: 22.83 photon/ms
total simulated energy: 10000.00 absorbed: 70.44712%
However, in this case, when it finishes, the results look correct:
Simulations are run from cli with:
mmc -f config.json -V 1 -k 0 -b 1 -M P -D SEM -d 1
And the following config.json is used (changing position of source and detector accordingly with the cases):{
"Mesh": {
"MeshID": "sample_cube",
"InitElem": -1
},
"Session": {
"Photons": 100000,
"Seed": 42,
"ID": "sample_cube",
"DoPartialPath": true,
"DoSaveExit": true,
"DoDCS": false
},
"Forward": {
"T0": 0.0,
"T1": 5e-09,
"Dt": 5e-09
},
"Optode": {
"Source": {
"Type": "planar",
"Pos": [5, 5, 35],
"Dir": [0, 0, -1],
"Param1": [10.0, 0.0, 0.0, 0.0],
"Param2": [0.0, 10.0, 0.0, 0.0]
},
"Detector": [
{
"Pos": [10, 10, 25],
"R": 20
}
]
}
}
Specifications:
iso2mesh toolbox version: 1.9.5-Rev
2 computers tested:
- ubuntu 24 x64 RTX4060 GPU (8GB VRAM)
- ubuntu 24 x64 QuadroM1200 (4GB VRAM)
MMC compiled from commit 31d6a06.
I would appreciate if you could take a look at this, since I can't seem to be able to design widefield
experiments at the moment.
Thank you very much,
please let me know if I can help with more information.
Alejandro