RAY pseudoparticles - How to speed-up simulations

Dear FLUKA experts,

I thank you in advance for your kind support and help.

I am trying to use FLUKA (v. 4-3.1) to perform quick simulations of standard X-ray radiographies. I am only interested in the evaluation of the 2D attenuation pattern of the beam through the object.
Therefore, I want to take advantage of the FLUKA capability to perform ray-tracing through a geometry and save the μ * length product, when RAY pseudoparticles are set as primaries.

I made a simple test, all the files are attached to the present post. I defined a source subroutine in which a series of monenergetic RAYs are sent one-by-one from a point source to each pixel of a detector (which is simply a GADOX screen), scanning all the rows and columns. Then, following this example, I made a Fortran program that saves EXP(-ALGTOT) for each RAY hitting the detector, hence for every pixel. I get the correct attenuation image in about 8 seconds and everything works fine.

The problem is that my real source is an X-ray tube, so the monoenergetic image is not accurate enough. The usual solution to this problem is to obtain monoenergetic images for each energy bin of the spectrum and then calculate the total image as a weighted sum of the various images, were the weights are based on the spectral shape.

However, this way it takes at least 20 min (for 130 energy bins) to get the overall image, a giant fort.10 file is generated, and also it is kinda useless to trace the RAYs through the same geometry. I could as well trace the RAYs only once, get the path lengths in each region and multiply them by the μ(E) vector of each region in a separate program, and then weight them.

I wonder if you could help me to find a better solution. Also, I ask you the following questions:

  1. Is it possible to save only ONE variable in the unformatted Fortran file with RAYs pseudoparticles? For example, to save only the ALGTOT?
  2. Is there any other way to speed up the simulation? Maybe the calculation of the trajectory in the source routine slows down the simulation?
  3. Do you have any tips on opening an unformatted Fortran file (as the one produced by the RAY simulation) with Python or Matlab?

Sorry for the lengthy post,

Thank you very much for your help.


simple_example.flair (3.1 KB)
simple_example.inp (3.0 KB)
source_RAY.f (7.7 KB)
save_ALGTOT.f (7.1 KB)

Dear Francesco,

I wonder if the most convenient way to achieve your goal is then to forget about the RAY unformatted file and make use of the src/user/mgdraw_empty.f routine (to be activated by USERDUMP). There, in the ENTRY BXDRAW, you can write on a formatted file the RAY coordinates XSCO, YSCO, ZSCO at the boundary between the region MREG and the region NEWREG (for info, ICODE should be 69). The NCASE variable, giving the event number, allows you to distinguish different rays. This looks like fulfilling your plan. Let me know if I missed something. (If required, a solution not to produce the fort.10 file can be provided).

Dear Francesco,

Thank you for your prompt response!

I can rely for sure on the BXDRAW routine, and I will try to implement it to get what I need. I did not immediately went into BXDRAW because, as I saw from the RAY example, when using RAYs as primaries FLUKA saves all the data anyway. Moreover, it saves exactly the data I need to do the calculation, without a lot of post-processing on my side.

Still, I would like to understand if there are ways to speed up RAY simulations, maybe also allowing to save only a few variables in the fort.10 file or, as suggested by you, even without the generation a fort.10 file.

I would really appreciate any further information on this issue.

Thank you very much for your help,


In fact, your geometry is pretty simple and the average simulation time per ray is below 0.1 ms, not offering a big margin for improvement. So, the time cost depends on the required resolution, i.e. the number of primaries.
I attach a modified binary version of the concerned core routine, to be renamed kasray.o and then linked into your executable (let me know if you meet compilation compatibility issues), which generates a formatted fort.10 file containing only EXP(-ALGTOT) for each ray entering your region #3.
Hope it helps

1 Like

Dear Francesco,

Thank you very much for the file!

I have no problem in the compilation and everything works perfectly. Also, the simulation time is almost half of the previous one (i.e., without the kasray.o file). This solves the issue for sure, so thanks a lot.

In the meanwhile, I wrote an mgdraw.f routine to save the RAY path length in every region (it works with the previous input file if the detector is region is put in #2). I would like to add a small part to this routine, based on the example presented in this post.
As you can see in the attached file, I tried to write a part of the SODRAW in which I scan the MEDFLK array to get the FLUKA codes of the used materials, and then I try to get the arrays of the attenuation coefficients through the SGTTEP function (as in the mentioned example). This would be useful, as I could calculate the attenuation for a polyenergetic beam.

The mgdraw.f is compiled, but when I try to run the simulation i get the following error:
“At line 99 of file emf/sgttep.f
Fortran runtime error: Index ‘0’ of dimension 1 of array ‘ge1’ below lower bound of 1”

I hope you can help me understand the error.

Thanks a lot again, I truly appreciate your help and support :pray:

Best regards,

mgdraw.f (10.7 KB)

This is because you did not exclude the BLCKHOLE region (for which MEDFLK(I,1)=1 and MEDEMF(I,1)=0).

I do not think I share the #2 constraint on the detector region. Note that in BXDRAW you use an unknown NREG variable.


1 Like

Dear Francesco,

That clarifies everything!

I corrected the typo “NREG” into “NEWREG”.

Thanks again, have a nice day!


Dear Francesco,

Sorry if I insist, but is there any way to have no fort.10 file output at all?

It would be nice to have to option to only produce the mgdraw.f output, without extra data being generated, to save space on the disk in case of a large dataset of polyenergetic images.

Thank you very much for your patience :pray:

Best regards,

Dear Francesco,

it is possible to suppress the fort.10 file by creating a symbolic link to /dev/null in the rfluka script.
You need to add the line:

ln -s /dev/null ${FORT}${FORTD}10


ln -s "${ERRF}" ${FORT}${FORTD}15

This way the fort.10 file still will be created, but it will be empty after the run.


1 Like

Dear David,

Thanks a lot for your answer, now the fort.10 file is 0 kB.

Thanks again to you and @ceruttif for your help and support,

All the best,