How to implement weighted source

Dear FLUKA experts,
I need to define a source that has the following distribution

To do it, I defined a flat distribution in source_newgen.f with an energy between 0 and 25 MeV. As a second step, I want to define a particle weight function (for the y axis) and I have to normalize each weight with respect to the total weight.
To compute the total weight I need to sum each weight factor, so I need a vector of weights to sum them all. This vector would require that I have the size of the vector containing the energy values.
Consequently, my question is does

momentum_energy = sample_flat_momentum_energy()

give a vector containing energy values or it gives only a scalar?

To make the problem clear, I am attaching the source file which may not be written in a correct way, but it expresses the idea about generating a beam of electrons with a such distribution.
DEMO2.f (11.7 KB)

Best regards,
Lovepreet

Dear Lovepreet,

the variable momentum_energy can only store one double precision scalar value, thus all sampling functions / subroutines return only one number.

You need to write your sampling routine a way, that is returns one energy and one weight values at each call.

The normalization of the weights to the total is not necessary, since the final FLUKA result will be automatically normalized to the total primary weight.

Cheers,
David

Thanks for your answer, SIr.
Since particle_weight line comes before the momentum_energy and the particle_weight depends, in my case, on the energy, does the order of the different lines in the file has to be respected? In other words, can I write the particle_weight after the momentum_energy line? or the order of the lines in the source_newgen file cannot be changed?

Kind regards,
Lovepreet

Dear Lovepreet,

The order of the lines is completely arbitrary, you can reorder them as you need.

Cheers,
David

Thank you, Sir.
Is there a way to plot the user-defined distribution in FLUKA, so I can compare the generated distribution with the original one?

Best regards,
Lovepreet

Dear Lovepreet,

you can place a small sphere around your source location, and use a USRBDX scoring from the sphere to the outside region, to get the source spectrum.

A bit more complicated way would be to use the USERDUMP card.

Cheers,
David

1 Like

A post was split to a new topic: Problem modeling weighted source