# Proper nuclear fission simulation in C++

4

I'm trying to simulate atomic fission in C++.

Fissile nuclides have known probabilities to produce certain atomic numbers and mass numbers as fission products, such distributions are called fission products yield.

I know mass product and atomic product fission yields of uranium-235 (available here).

A fission product yield distribution adds up to 2, since a nuclear fission produces 2 new nuclides and 2 or 3 free neutrons.

While I may have misunderstood some physics concept, from a computer science point of view my problem is to generate 5 integers with known probability distributions, in pseudocode:

``````massYield = [...]
possibleMassProducts = [...]
atomicYield = [...]
possibleAtomicProducts = [...]
firstProductMassNumber = generateRandom(massYield, possibleMassProducts)
firstProductAtomicNumber = generateRandom(atomicYield, possibleAtomicProducts)
secondProductMassNumber = generateRandom(massYield, possibleMassProducts)
secondProductAtomicNumber = generateRandom(atomicYield, possibleAtomicProducts)
freeNeutrons = generateRandom([0.5, 0.5], [2, 3])
``````

I've made a class called IntegerRandomGenerator which generates random numbers following a specific distribution:

``````class IntegerRandomGenerator
{
public:
IntegerRandomGenerator(std::vector<double> distribution, std::vector<int> values)
{
size_t size_dist = distribution.size();
size_t size_val  = values.size();
if( size_dist != size_val )
{
throw std::invalid_argument("distribution and values vectors shall have the same length");
}
else
{
for( double p_i : distribution )
{
mDist.push_back(p_i);
}
for( int x_i : values )
{
mValues.push_back(x_i);
}
}
}

int generate();

private:
std::vector<double> mDist;
std::vector<int>   mValues;
std::mt19937_64 mRng;
};

int IntegerRandomGenerator::generate()
{
uint64_t timeSeed = std::chrono::high_resolution_clock::now().time_since_epoch().count();

std::seed_seq ss{uint32_t(timeSeed & 0xffffffff), uint32_t(timeSeed>>32)};
mRng.seed(ss);

std::uniform_real_distribution<double> unif(0,1);
double random = unif(mRng);

uint32_t distSize = static_cast<uint32_t>(mDist.size());
for(uint32_t i = 0; i < distSize; i++)
{
if(random < mDist.at(i))
{
return mValues.at(i);
}
else
{
random -= mDist.at(i);
}
}

return -1;
}
``````

Hence my strategy to generate fission products is the following:

``````std::vector<int> generateFissionValues(std::vector<double> aYields, std::vector<double> zYields, std::vector<int> aNumbers, std::vector<int> zNumbers)
{
std::vector<double> massNormalized, atomicNormalized;
std::vector<double> unif;
std::vector<int> nNumbers;

for(double p_i : aYields)
{
massNormalized.push_back(p_i/2);
}

for(double q_i : zYields)
{
atomicNormalized.push_back(q_i/2);
}

unif.push_back(0.5);
unif.push_back(0.5);
nNumbers.push_back(2);
nNumbers.push_back(3);

IntegerRandomGenerator massGenerator(massNormalized, aNumbers);
IntegerRandomGenerator atomGenerator(atomicNormalized, zNumbers);
IntegerRandomGenerator freeGenerator(unif, nNumbers);

int a_Product1 = massGenerator.generate();
int z_Product1 = atomGenerator.generate();

int a_Product2 = massGenerator.generate();
int z_Product2 = atomGenerator.generate();

int freeNeutr = freeGenerator.generate();

std::vector<int> fissionProducts;

fissionProducts.push_back(a_Product1);
fissionProducts.push_back(z_Product1);

fissionProducts.push_back(a_Product2);
fissionProducts.push_back(z_Product2);

fissionProducts.push_back(freeNeutr);

return fissionProducts;
}
``````

What I expect to get, repeating this routine a fixed number of times and counting occurrencies of each atomic and mass number, is to get an empirical distribution which is similar to the theoretically known. This is my main:

``````#define MIN_MASS_NUMBER 66
#define MIN_ATOMIC_NUMBER 23
#define MASS_YIELD_LENGTH 107
#define ATOMIC_YIELD_LENGTH 49

double MASS_YIELD[]=
{
7.2405671e-10,  3.6155178e-09,  8.3798843e-09,  1.4793428e-08,  3.7126096e-08,  8.4074658e-08,  2.6566021e-07,
1.0675927e-06,  3.3943611e-06,  1.0705677e-05,  3.0947722e-05,  7.5980282e-05,  2.0984921e-04,  4.4786922e-04,
1.2829461e-03,  1.9049358e-03,  3.2662529e-03,  5.3619387e-03,  8.9643421e-03,  1.2891416e-02,  1.4066064e-02,
2.5282028e-02,  3.4205412e-02,  4.7612426e-02,  5.8969791e-02,  5.8718196e-02,  5.9912701e-02,  6.2513918e-02,
6.5931230e-02,  6.5585160e-02,  6.3729133e-02,  6.1194661e-02,  5.7681295e-02,  6.1623490e-02,  6.5885448e-02,
5.1864512e-02,  4.2059644e-02,  3.0389326e-02,  1.8792336e-02,  9.7353401e-03,  4.0277579e-03,  1.4658471e-03,
5.4222280e-04,  3.1255579e-04,  2.5608411e-04,  1.7477930e-04,  1.3934770e-04,  1.3765549e-04,  1.1957731e-04,
1.3077006e-04,  1.3324824e-04,  1.1755897e-04,  1.1374826e-04,  1.2290949e-04,  1.2642393e-04,  1.3080367e-04,
1.5523846e-04,  1.1021012e-04,  2.6858588e-04,  2.9061019e-04,  5.8370361e-04,  1.5268885e-03,  3.4896815e-03,
5.3614445e-03,  1.8159164e-02,  2.8895004e-02,  4.3250233e-02,  6.7141993e-02,  7.8703816e-02,  6.5640320e-02,
6.1137911e-02,  6.3605380e-02,  6.7934012e-02,  6.4934315e-02,  6.2395971e-02,  5.8687079e-02,  5.8447475e-02,
5.9831620e-02,  5.5176036e-02,  3.9532454e-02,  3.0059773e-02,  2.2527624e-02,  1.6771722e-02,  1.0853545e-02,
6.5508342e-03,  4.1317995e-03,  2.6763110e-03,  1.5870986e-03,  7.4634286e-04,  3.2221285e-04,  1.4892861e-04,
6.1673994e-05,  3.2940204e-05,  1.0121245e-05,  3.1986347e-06,  8.5523451e-07,  1.5949051e-07,  6.1183839e-08,
1.8867389e-08,  9.5405554e-09,  3.6338899e-09,  2.4780248e-09,  5.7277553e-10,  2.3914282e-10,  5.0200588e-11,
2.3540988e-11,  7.7074464e-12
};

double ATOMIC_YIELD[]=
{
2.0558300e-19,  2.8862345e-14,  1.3368750e-11,  1.6193162e-09,  1.3709663e-08,  3.0150265e-07,  3.6301782e-06,
1.1200954e-04,  7.0351909e-04,  4.4498299e-03,  9.1719425e-03,  3.6528367e-02,  5.2682609e-02,  1.5625724e-01,
1.2071735e-01,  1.9331319e-01,  1.2675553e-01,  1.8037105e-01,  6.8939851e-02,  4.3268378e-02,  3.4683702e-03,
4.1462569e-04,  2.2779437e-04,  3.4012286e-04,  2.7543142e-04,  1.6063619e-03,  1.7210332e-03,  3.6208508e-02,
7.7033688e-02,  1.7331044e-01,  1.1582092e-01,  2.0090419e-01,  1.1037331e-01,  1.6401242e-01,  6.0615264e-02,
4.1834320e-02,  1.3435892e-02,  4.5405310e-03,  4.6638638e-04,  1.0987593e-04,  5.1296865e-06,  3.9564637e-07,
1.1679425e-08,  1.9805406e-09,  7.0990343e-11,  4.8416648e-12,  2.4283831e-14,  2.4747259e-17,  0.0000000e+00
};

int main()
{
const int nSim = 10000;

std::vector<double> distMass(std::begin(MASS_YIELD), std::end(MASS_YIELD));
std::vector<double> distAtom(std::begin(ATOMIC_YIELD), std::end(ATOMIC_YIELD));
std::vector<int>   massNumbers;
std::vector<int>   atomNumbers;

for(int i = 0; i < MASS_YIELD_LENGTH; i++)
{
massNumbers.push_back(i + MIN_MASS_NUMBER);
}
for(int i = 0; i < ATOMIC_YIELD_LENGTH; i++)
{
atomNumbers.push_back(i + MIN_ATOMIC_NUMBER);
}

int countsPerZ[ATOMIC_YIELD_LENGTH];
std::vector<double> relCountsPerZ;

int countsPerA[MASS_YIELD_LENGTH];
std::vector<double> relCountsPerA;

memset(countsPerZ, 0x00, ATOMIC_YIELD_LENGTH * sizeof(int));
memset(countsPerA, 0x00, MASS_YIELD_LENGTH * sizeof(int));

std::cout << "Start " << nSim << " atomic fission simulations . . ." << std::endl;
for(int i = 0; i < nSim;)
{
std::vector<int> fissionProducts = generateFissionValues(distMass, distAtom, massNumbers, atomNumbers);

int a1 = fissionProducts.at(0);
countsPerA[a1 - MIN_MASS_NUMBER] += 1;

int z1 = fissionProducts.at(1);
countsPerZ[z1 - MIN_ATOMIC_NUMBER] += 1;

int a2 = fissionProducts.at(2);
countsPerA[a2 - MIN_MASS_NUMBER] += 1;

int z2 = fissionProducts.at(3);
countsPerA[z2 - MIN_ATOMIC_NUMBER] += 1;

i++;

double percentage = (100.0 * i)/nSim;
printf("Progress: %d %%\r", static_cast<int>(percentage));
fflush(stdout);
}

//here I pass countsPerA and countsPerZ to another function which plots results
}
``````

Here are my results, out of 10000 repetitions of the experiment.

For Mass Numbers: For Atomic Numbers:

It is strange because with the same strategy, atomic numbers seems to be correctly simulated, while mass numbers definitely not. What am I doing wrong with mass numbers?

c++
statistics
simulation
physics
asked on Stack Overflow Jan 22, 2020 by Alex Foglia • edited Jan 22, 2020 by Alex Foglia

2

When you're populating the `countsPerA` and `countsPerZ` arrays:

``````    int a1 = fissionProducts.at(0);
countsPerA[a1 - MIN_MASS_NUMBER] += 1;

int z1 = fissionProducts.at(1);
countsPerZ[z1 - MIN_ATOMIC_NUMBER] += 1;

int a2 = fissionProducts.at(2);
countsPerA[a2 - MIN_MASS_NUMBER] += 1;

int z2 = fissionProducts.at(3);
countsPerA[z2 - MIN_ATOMIC_NUMBER] += 1; //typo here
``````

You're adding data from the atomic numbers results into the Mass Numbers count due to a typo.

answered on Stack Overflow Jan 22, 2020 by Klaycon

User contributions licensed under CC BY-SA 3.0