71 lines
1.6 KiB
C++
71 lines
1.6 KiB
C++
//
|
|
// random_operations.h
|
|
// mc_electron
|
|
//
|
|
// Created by Christian Reiling on 30.04.19.
|
|
// Copyright © 2019 Christian Reiling. All rights reserved.
|
|
//
|
|
|
|
#ifndef random_operations_h
|
|
#define random_operations_h
|
|
|
|
#include <random/uniform.h>
|
|
#include <random/normal.h>
|
|
|
|
#include "kinematics.h"
|
|
|
|
|
|
typedef ranlib::Uniform<double> UniRNG;
|
|
typedef ranlib::NormalUnit<double> NormalRNG;
|
|
|
|
|
|
class RNG_Base {
|
|
public:
|
|
RNG_Base(UniRNG *rngUni, NormalRNG *rngNormal) {
|
|
this->rngUni = rngUni;
|
|
this->rngNormal = rngNormal;
|
|
};
|
|
protected:
|
|
UniRNG *rngUni;
|
|
NormalRNG *rngNormal;
|
|
};
|
|
|
|
inline vec3 roll_uniform_direction(ranlib::Uniform<double> *rngUni) {
|
|
double phi = 2*pi*rngUni->random();
|
|
double theta = pi*rngUni->random();
|
|
return vec3(sin(theta)*cos(phi) , sin(theta)*sin(phi) , cos(theta));
|
|
}
|
|
|
|
inline double roll_MBEnergy(const double &Temp_K, const double &m_eV,ranlib::NormalUnit<double> *rngNormal) {
|
|
double C = kb_eVK*Temp_K/m_eV;
|
|
double beta = 0;
|
|
double sum = 0;
|
|
for (int i=0;i<3;i++) {
|
|
sum += pow(rngNormal->random(),2);
|
|
}
|
|
beta = sqrt(C*(sum));
|
|
/*
|
|
double gamma = calc_gamma_from_beta(beta);
|
|
if (gamma == 1) {
|
|
return 0.5*m_eV*beta*beta;
|
|
} else {
|
|
return ((gamma-1)*m_eV);
|
|
}
|
|
*/
|
|
return 0.5*m_eV*beta*beta;
|
|
}
|
|
|
|
inline double roll_Rejectionmethod(double (*pdf)(double), double xmin, double xmax, double pmax, ranlib::Uniform<double> *rngUni) {
|
|
// get Energy distribution using Rejection Method
|
|
double xcan,eta1,eta2;
|
|
do {
|
|
eta1 = rngUni->random();
|
|
eta2 = rngUni->random();
|
|
xcan = xmin + eta1*(xmax - xmin);
|
|
} while( eta2*pmax > pdf(xcan) );
|
|
return xcan;
|
|
}
|
|
|
|
|
|
#endif /* random_operations_h */
|