phystota

v3.5 - Clean, full (MPI parallel)

Apr 26th, 2025
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 50.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <fstream>
  4. #include <assert.h>
  5.  
  6.  
  7. #include <omp.h>
  8. #include <chrono>
  9.  
  10. #include <math.h>
  11. #include <time.h>
  12. #include <iomanip>  // For std::fixed and std::setprecision
  13.  
  14. #include <algorithm>  // For std::shuffle
  15. #include <numeric>    // For std::iota
  16.  
  17. #include <random>
  18. #include <limits>
  19.  
  20. inline double fast_uniform(std::mt19937 &g) {
  21.     // g() returns uint32_t in [0, 2^32−1].
  22.     // Divide by max to get a double in [0,1].
  23.     return double(g()) * (1.0 / double(std::numeric_limits<uint32_t>::max()));
  24. }
  25.  
  26. //physical constants
  27.  
  28. #define m_e 9.1093837E-31 // electron mass in kg
  29. #define M_n 6.6464731E-27 // Helium atom mass
  30. #define k_b 1.380649E-23 // Boltzmann constant
  31. #define q 1.602176634E-19 // elementary charge    - eV -> J transfer param
  32. #define Coulomb_log 15.87 // Coulomb logarithm
  33. #define epsilon_0 8.854188E-12 // Vacuum permittivity
  34. #define Coulomb_const pow(q,4)/(pow(4.0*M_PI*epsilon_0,2)) // e^4/(4*pi*eps0)^2
  35. #define thresh0 0.0 //elastic threshold
  36. #define thresh1 19.82 // threshold energy excitation tripet state
  37. #define thresh2 20.61 // threshold energy excitation singlet state
  38. #define thresh3 24.587 // threshold energy ionization HeI
  39. #define tau_singlet 0.0195
  40.  
  41. // simulation parameters
  42.  
  43.  
  44. // #define N_He 1000000 // Helium neutrals number
  45. #define T_n 2.0 // Helium neutral temperature in eV
  46. #define T_e 10.0    // electron Maxwell initial distribution
  47. #define Emin 0.0
  48. #define Emax 3000.0
  49. #define Volume 1.0E-12 // Volume to calculate netral density and collision frequency
  50. #define total_time 8.0E-4 // 500 microsec time to equalibrate the system
  51. #define dopant 1.0E-5 // addition to avoid zeros
  52. #define E_reduced 0.6 // constant electrin field along z-axis
  53.  
  54. #define bin_width 0.05 // keep energy step ~ this to maintain cross-section clarity (Ramsauer minimum etc)
  55. #define N ( (int)((Emax-Emin)/bin_width) + 1) // add 1 to include E_max if needed?
  56.  
  57. // handling final energy bin
  58.  
  59. #define bin_width_smooth 0.5 // energy bin for smooth final distribution
  60. #define N_smooth ( (int)((Emax-Emin)/bin_width_smooth) )
  61.  
  62.  
  63.  
  64. double solve_A(double s) { // Netwon method solver
  65.  
  66.     if (s > 3) {
  67.         return 3*exp(-s);
  68.     }
  69.     if (s < 0.01) {
  70.         return 1.0/s;
  71.     }
  72.    
  73.     double A0 = 0.01; // initial guess
  74.     double A = A0;  //starting with initial guess
  75.     double tol = 1.0E-7; // accuracy
  76.  
  77.              
  78.     for (int i = 0; i < 1000; i++){
  79.  
  80.         double tanhA = tanh(A);
  81.         // Avoid division by an extremely small tanh(A)
  82.         if (fabs(tanhA) < 1e-12) {
  83.             std::cerr << "tanh(A) too small, returning fallback at iteration " << i << "\n";
  84.             return 1.0E-7;
  85.         }        
  86.  
  87.         double f = 1.0 / tanhA - 1.0 / A - exp(-s);
  88.         if (fabs(f) < tol)
  89.             break;
  90.  
  91.         double sinhA = sinh(A);
  92.         if (fabs(sinhA) < 1e-12) {
  93.             std::cerr << "sinh(A) too small, returning fallback at iteration " << i << "\n";
  94.             return 1.0E-5;
  95.         }
  96.  
  97.         double dfdA = -1.0/(sinh(A)*sinh(A)) + 1.0/(A*A);
  98.  
  99.         // Check if derivative is too close to zero to avoid huge updates
  100.         if (fabs(dfdA) < 1e-12) {
  101.             std::cerr << "dfdA is too small at iteration " << i << ", returning fallback\n";
  102.             if (s < 0.01) {
  103. //                std::cout << "Small s! Huge A!" << "\n";
  104.                 return 1.0/s;
  105.             }
  106.             if (s > 3) {
  107.                 return 3.0*exp(-s);
  108.             }
  109.         }        
  110.  
  111.         A -= f/dfdA;
  112.  
  113.         // Early check for numerical issues
  114.         if (std::isnan(A) || std::isinf(A)) {
  115.             std::cerr << "Numerical error detected, invalid A at iteration " << i << "\n";
  116.             return (A > 0) ? 1.0E-5 : -1.0E-5;  // Fallback value based on sign
  117.         }        
  118.  
  119.  
  120.     }
  121.  
  122.     return A;
  123. }
  124.  
  125. struct Electron {
  126.  
  127.     //velocity components
  128.     double vx = 0.0;
  129.     double vy = 0.0;
  130.     double vz = 0.0;
  131.     //energy in eV
  132.     double energy = 0.0;
  133.     //Collision flag
  134.     bool collided_en = false;
  135.     bool collided_ee = false;
  136.  
  137.     // initializing Maxwell-Boltzmann distribution with T_e
  138.     void initialize(std::mt19937& gen, std::uniform_real_distribution<double>& dis, std::gamma_distribution<double>& maxwell) {
  139.  
  140.         double R = dis(gen);
  141.  
  142.         // velocity angles in spherical coordinates
  143.         double phi = 2*M_PI*dis(gen);
  144.         double cosTheta = 2.0*dis(gen) - 1.0;
  145.         double sinTheta = sqrt(1.0 - cosTheta*cosTheta);
  146.  
  147.            
  148.         energy = maxwell(gen); // neutrals energies sampled as Maxwell distribution in eV
  149.            
  150.         double speed = sqrt(2*energy*q/m_e);
  151.  
  152.         //velocity components of neutrals in m/s
  153.         vx = speed * sinTheta * cos(phi);
  154.         vy = speed * sinTheta * sin(phi);
  155.         vz = speed * cosTheta;
  156.     }
  157.  
  158.  
  159. };
  160.  
  161. struct CrossSection {
  162.     double energy;
  163.     double sigma;
  164. };
  165.  
  166. double interpolate (double energy, const std::vector<CrossSection>& CS) {
  167.  
  168.  
  169.     if (energy < CS.front().energy) {
  170. //        std::cout << " required energy value lower than range of cross-section data at energy: " << energy << "\n";
  171.         return 0.0;
  172.     }
  173.     if (energy > CS.back().energy) {
  174. //        std::cout << " required energy value higher than range of cross-section data" << "\n";
  175.         return 0.0;        
  176.     }
  177.  
  178.     int step = 0;  
  179.         while (step < CS.size() && energy > CS[step].energy) {
  180.             step++;
  181.         }
  182.  
  183.     double k = (CS[step].sigma - CS[step-1].sigma)/(CS[step].energy - CS[step-1].energy);
  184.     double m = CS[step].sigma - k*CS[step].energy;
  185.    
  186.     return k*energy + m;
  187. }
  188.  
  189.  
  190. struct NeutralParticle {
  191.  
  192.     double energy = 0.0;
  193.     double vx = 0.0;
  194.     double vy = 0.0;
  195.     double vz = 0.0;
  196.  
  197.     void initialize(std::mt19937& gen, std::uniform_real_distribution<double>& dis, std::gamma_distribution<double>& maxwell) {
  198.  
  199.         double R = dis(gen);
  200.  
  201.         // velocity angles in spherical coordinates
  202.         double phi = 2*M_PI*dis(gen);
  203.         double cosTheta = 2.0*dis(gen) - 1.0;
  204.         double sinTheta = sqrt(1.0 - cosTheta*cosTheta);
  205.  
  206.            
  207.         energy = maxwell(gen); // neutrals energies sampled as Maxwell distribution in eV
  208.            
  209.         double speed = sqrt(2*energy*q/M_n);
  210.  
  211.         //velocity components of neutrals in m/s
  212.         vx = speed * sinTheta * cos(phi);
  213.         vy = speed * sinTheta * sin(phi);
  214.         vz = speed * cosTheta;
  215.     }
  216.    
  217. };
  218.  
  219. struct Excited_neutral {
  220.  
  221.     double energy;
  222.     double vx;
  223.     double vy;
  224.     double vz;
  225.    
  226. };
  227.  
  228.  
  229.  
  230. int main() {
  231.  
  232.     int nThreads = omp_get_max_threads();
  233.     omp_set_num_threads(nThreads);
  234.  
  235.     clock_t start = clock();
  236.    
  237.     int N_He = 10000000;
  238.     int n_e = 500000;
  239.  
  240.     std::vector<Electron> electrons(n_e); // better to use vector instead of simple array as it's dynamically allocated (beneficial for ionization)
  241.     electrons.reserve(n_e * 10);
  242. //    std::vector<NeutralParticle> neutrals(N_He); // I don't need a vector of neutrals bcs it's like a backhround in MCC-simulation
  243.  
  244.     std::vector<int> histo_random(N, 0); // initialize N size zero-vector for random (initial) histogram
  245.     std::vector<int> histo_maxwell(N, 0); // initialize N size zero-vector for maxwellian histogram
  246.     std::vector<int> histo_neutral(N, 0); // initialize N size zero-vector for neutral distribution histogram
  247.     std::vector<int> histo_excited(N, 0); // initialize N size zero-vector for excited He distribution histogram
  248.  
  249.     std::vector<double> elastic_vec(N, 0); // precompiled elastic cross-section-energy vector
  250.     std::vector<double> inelastic1_vec(N, 0); // precompiled inelastic(triplet excitation) cross-section-energy vector
  251.     std::vector<double> inelastic2_vec(N, 0); // precompiled inelastic(singlet excitation) cross-section-energy vector    
  252.     std::vector<double> superelastic1_vec(N, 0); // precompiled superelastic(triplet de-excitation) cross-section-energy vector
  253.     std::vector<double> superelastic2_vec(N, 0); // precompiled superelastic(triplet de-excitation) cross-section-energy vector
  254.     std::vector<double> ionization_vec(N, 0); // precompiled ionization cross-section-energy vector
  255.  
  256.     std::random_device rd;
  257.     std::mt19937 gen(rd());
  258.     std::uniform_real_distribution<double> dis(0.0, 1.0);
  259.     std::gamma_distribution<double> maxwell_neutral(1.5, T_n);
  260.     std::gamma_distribution<double> maxwell_electron(1.5, T_e);
  261.  
  262.     std::ifstream elastic_cs_dat("cross_sections/elastic.dat");
  263.     if (!elastic_cs_dat.is_open()) {
  264.         std::cerr << "Error opening elastic cross-sections file!" << std::endl;
  265.         return 1;
  266.     }    
  267.  
  268.     std::ifstream excitation1_cs_dat("cross_sections/inelastic_triplet.dat");
  269.     if (!excitation1_cs_dat.is_open()) {
  270.         std::cerr << "Error opening inelastic triplet cross-sections file!" << std::endl;
  271.         return 1;
  272.     }
  273.  
  274.     std::ifstream excitation2_cs_dat("cross_sections/inelastic_singlet.dat");
  275.     if (!excitation2_cs_dat.is_open()) {
  276.         std::cerr << "Error opening inelastic singlet cross-sections file!" << std::endl;
  277.         return 1;
  278.     }
  279.  
  280.     std::ifstream ionization_cs_dat("cross_sections/ionization.dat");
  281.     if (!ionization_cs_dat.is_open()) {
  282.         std::cerr << "Error opening inelastic singlet cross-sections file!" << std::endl;
  283.         return 1;
  284.     }
  285.  
  286.     // --- starts reading cross section datafiles
  287.  
  288. //-----------------elastic---------------------------//
  289.     std::vector<CrossSection> elastic_CS_temp;
  290.  
  291.     double energy, sigma;
  292.  
  293.     while (elastic_cs_dat >> energy >> sigma) {
  294.         elastic_CS_temp.push_back({energy, sigma});
  295.     }    
  296.     elastic_cs_dat.close();
  297.  
  298.     energy = 0.0;
  299.     sigma = 0.0;
  300. //-----------------triplet excitation---------------------------//
  301.     std::vector<CrossSection> inelastic1_CS_temp;
  302.  
  303.     while (excitation1_cs_dat >> energy >> sigma) {
  304.         inelastic1_CS_temp.push_back({energy, sigma});
  305.     }    
  306.     excitation1_cs_dat.close();    
  307. //-----------------singlet excitation---------------------------//
  308.     energy = 0.0;
  309.     sigma = 0.0;
  310.  
  311.     std::vector<CrossSection> inelastic2_CS_temp;
  312.  
  313.     while (excitation2_cs_dat >> energy >> sigma) {
  314.         inelastic2_CS_temp.push_back({energy, sigma});
  315.     }    
  316.     excitation2_cs_dat.close();    
  317. //-----------------Ionization---------------------------//
  318.     energy = 0.0;
  319.     sigma = 0.0;
  320.  
  321.     std::vector<CrossSection> ionization_CS_temp;
  322.  
  323.     while (ionization_cs_dat >> energy >> sigma) {
  324.         ionization_CS_temp.push_back({energy, sigma});
  325.     }    
  326.     ionization_cs_dat.close();    
  327.     // --- finish reading cross-section datafiles  
  328.  
  329.     std::ofstream file0("output_files/velocities.dat");    
  330.     std::ofstream file1("output_files/energies.dat");        
  331.     std::ofstream file2("output_files/energies_final.dat");    
  332.     std::ofstream file3("output_files/histo_random.dat");    
  333.     file3 << std::fixed << std::setprecision(10);
  334.    
  335.     std::ofstream file4("output_files/histo_maxwell.dat");
  336.     file4 << std::fixed << std::setprecision(10);          
  337.    
  338.     std::ofstream file5("output_files/neutral_distribution.dat");    
  339.     std::ofstream file6("output_files/E*f(E).dat");    
  340.     std::ofstream file7("output_files/nu_max.dat");
  341.     std::ofstream file8("output_files/electron_mean_energy.dat");
  342.     std::ofstream file9("output_files/nu_elastic_average_initial.dat");
  343.     std::ofstream file10("output_files/nu_inelastic1_average_initial.dat");
  344.     std::ofstream file11("output_files/nu_elastic_average_final.dat");
  345.     std::ofstream file12("output_files/nu_inelastic1_average_final.dat");
  346.     std::ofstream file13("output_files/log_output.dat");  
  347.     std::ofstream file14("output_files/excited_energies.dat");      
  348.     std::ofstream file15("output_files/excited_histo.dat");            
  349.     std::ofstream file_temp("output_files/collision_rates.dat");
  350.     std::ofstream file16("output_files/energy_gain.dat");  
  351.     std::ofstream file17("output_files/ionization_check.dat");
  352.  
  353.     // Initialize all electrons
  354.     for (auto& e : electrons) {
  355.         e.initialize(gen, dis, maxwell_electron);
  356.     }
  357.  
  358.     // precalculate cross-sections for each energy bin
  359.     for (int i = 0; i < N; i++){
  360.         elastic_vec[i] = interpolate(bin_width*(i+0.5), elastic_CS_temp); //elastiuc
  361.         inelastic1_vec[i] = interpolate(bin_width*(i+0.5), inelastic1_CS_temp); //triplet excitation
  362.         inelastic2_vec[i] = interpolate(bin_width*(i+0.5), inelastic2_CS_temp); //singlet excitation
  363.         ionization_vec[i] = interpolate(bin_width*(i+0.5), ionization_CS_temp); //ionization
  364.     }
  365.  
  366.     // precalculate superelastic cross-section (triplet -> ground) for each energy bin
  367.     // detailed balance gives: sigma_j_i(E) = (g_i/g_j)*((E+theshold)/E)*sigma_i_j(E+theshold)
  368.     for (int i = 0; i < N; i++){
  369.         double energy = Emin + (i + 0.5) * bin_width;
  370.         int thresh_bin = (int)( (thresh1 - Emin)/bin_width ); // calculating bin for threshold energy
  371.         superelastic1_vec[i] = (1.0/3.0)*((energy + thresh1)/energy)*interpolate(energy + thresh1, inelastic1_CS_temp); // using detailed balance, calculating backward deexcitation cross-section
  372.         superelastic2_vec[i] = (1.0/1.0)*((energy + thresh2)/energy)*interpolate(energy + thresh2, inelastic2_CS_temp);
  373.     }
  374.  
  375.     for (int i = 0; i < n_e; i++){
  376.         file1 << i << " " << electrons.at(i).energy << "\n";
  377.         file0 << i << " " << electrons[i].vx << " " << electrons[i].vy << " " << electrons[i].vz << "\n";
  378.     }
  379.  
  380.     // -----initial electrons energy distribution starts------------////
  381.     for (int i = 0; i < n_e; i++){
  382.         int bin = (int)( (electrons[i].energy - Emin)/bin_width );
  383.         if (bin >=0 && bin < histo_random.size())
  384.             histo_random[bin]++;
  385.     }
  386.  
  387.     for (int i = 0; i < histo_random.size(); i++){
  388.         double bin_center = Emin + (i + 0.5) * bin_width;
  389.         file3 << bin_center << " " <<  static_cast<double>(histo_random[i])/(electrons.size()*bin_width) << "\n"; // this is electron normalized distribution function
  390.     }
  391.  
  392.     //calculating excited specied population
  393.  
  394.     /*From Boltzman distribution y_k = g_k*exp(-E_k/kT)/norm, where g_k - stat weight of k-state,
  395.     E_k - threshold energy for k-state, norm is a total partition function or normaliztion factor     */
  396.  
  397.     double part_ground = 1.0*exp(-0.0/T_n); // partition function for ground state
  398.     double part_triplet = 3.0*exp(-thresh1/T_n); // partition function for triplet excited state
  399.     double part_singlet = 1.0*exp(-thresh2/T_n); // partition function for singlet excited state
  400.     double part_func_total = part_ground + part_triplet + part_singlet; // total partition function
  401.     double N_trpilet = (part_triplet/part_func_total)*N_He; // population of tripet state
  402.     double N_singlet = (part_singlet/part_func_total)*N_He; // population of singlet state
  403.  
  404.     std::vector<Excited_neutral> exc_1(static_cast<int>(N_trpilet));  // vector to track triplet excited atoms of Helium
  405.     std::vector<Excited_neutral> exc_2(static_cast<int>(N_singlet));  // vector to track singlet excited atoms of Helium    
  406.  
  407.     // adjusting neutrals number:
  408.  
  409.     N_He -= (N_trpilet + N_singlet);
  410.  
  411.     std::cout << N_He << "\n";
  412.  
  413.     // initializing excited species with Maxwellian distribution
  414.  
  415.     for (auto& exc : exc_1) {
  416.     NeutralParticle tmp_neutral;
  417.     tmp_neutral.initialize(gen, dis, maxwell_neutral);
  418.     exc.energy = tmp_neutral.energy;
  419.     exc.vx = tmp_neutral.vx;
  420.     exc.vy = tmp_neutral.vy;
  421.     exc.vz = tmp_neutral.vz;
  422.     }
  423.  
  424.     for (auto& exc : exc_2) {
  425.     NeutralParticle tmp_neutral;
  426.     tmp_neutral.initialize(gen, dis, maxwell_neutral);
  427.     exc.energy = tmp_neutral.energy;
  428.     exc.vx = tmp_neutral.vx;
  429.     exc.vy = tmp_neutral.vy;
  430.     exc.vz = tmp_neutral.vz;
  431.     }
  432.  
  433.     std::cout << "Triplet population initialized: " << exc_1.size() << "\n";
  434.     std::cout << "Singlet population initialized: " << exc_2.size() << "\n";    
  435.  
  436.     // calculating excited specied population finished //
  437.  
  438.  
  439.     int print_interval = 10;
  440.     int el_coll_counter = 0; // track all elastic collisions
  441.     int exc1_coll_counter = 0; // track all triplet excitation collisions
  442.     int exc2_coll_counter = 0; // track all singlet excitation collisions
  443.     int null_coll_counter = 0; // track null-collisions
  444.     int ee_coll_counter = 0; //track e-e Coulomb collisions
  445.     int super1_coll_counter = 0; // track superelastic triplet collisions
  446.     int super2_coll_counter = 0; // track superelastic triplet collisions    
  447.     int ionization_counter = 0; //track ionization collisions
  448.  
  449.  
  450.     double a_z = ((-1.0)*q * E_reduced) / m_e;
  451.     double mass_ratio = 2.0*(m_e/M_n);
  452.     double charge_mass_ratio = 0.5*m_e/q;
  453.     double sqrt_charge_mass = sqrt(2*q/m_e);
  454.     double C1 = -1.0*q*E_reduced;
  455.     double C2 = 0.5*C1*C1/m_e;
  456.  
  457.     double t_elapsed = 0.0;
  458.  
  459.     std::cout << C1 << "    " << C2 << "\n";
  460.  
  461.  
  462.     // -----calculating nu-max for null-collision method starts ------------////
  463.     double nu_max = 0.0;
  464.     double nu_max_temp = 0.0;
  465.     double sigma_total = 0.0;
  466.    
  467.     for (int i = 0; i < N; i++){
  468.         // Get initial densities
  469.         double n_ground = N_He / Volume;
  470.         double n_excited1 = exc_1.size() / Volume;
  471.         double n_excited2 = exc_2.size() / Volume;
  472.  
  473.         double energy = Emin + (i + 0.5) * bin_width;
  474.  
  475.         // Total collision frequency for this energy bin
  476.         double sigma_total =
  477.             elastic_vec[i] * n_ground +
  478.             inelastic1_vec[i] * n_ground +
  479.             inelastic2_vec[i] * n_ground +
  480.             superelastic1_vec[i] * n_excited1 +
  481.             superelastic2_vec[i] * n_excited2 +
  482.             ionization_vec[i] * n_ground;
  483.  
  484.         double v = sqrt(2 * energy * q / m_e);
  485.         double nu_temp = sigma_total * v;
  486.        
  487.         if (nu_temp > nu_max) nu_max = nu_temp;
  488.     }
  489.  
  490.     std::cout << "initial nu_max: " <<nu_max << "\n";
  491.     // -----calculating nu-max for null-collision method ends ------------////    
  492.  
  493.     double dt = 0.1/nu_max;   // minimum should be 0.1/nu_max to get acceptable numerical error range see Vahedi Surrendra 1995
  494.  
  495.  
  496.     std::vector<std::vector<Electron>> local_newborn(nThreads);
  497.     for(auto &v : local_newborn)
  498.         v.reserve(n_e/nThreads/2);
  499.  
  500.     std::vector<std::mt19937> engines(nThreads);
  501.     for(int i=0; i<nThreads; ++i) engines[i].seed(rd()+i);    
  502.  
  503. // ---------------------------------------------------------------- main loop begin----------------------------------------------------------///
  504.  
  505.     while (t_elapsed < total_time) {
  506.         // Handle edge case for final step
  507.         if (t_elapsed + dt > total_time) {
  508.             dt = total_time - t_elapsed;
  509.         }    
  510.  
  511.  
  512.         //using  null-collision technique, getting the number of particles colliding each step: P_collision = 1 - exp(-nu_max*dt)
  513.         int Ne_collided = (1.0-exp(-1.0*dt*nu_max))*n_e;  
  514.  
  515.  
  516.         for(auto &v:local_newborn) v.clear();
  517.  
  518.         // Generate shuffled list of electron indices
  519.         std::vector<int> electron_indices(n_e);
  520.         std::iota(electron_indices.begin(), electron_indices.end(), 0); // fill with index
  521.         int reshuffle_interval = 1;
  522.  
  523.  
  524.  
  525.         for (int i = 0; i < Ne_collided; ++i) {
  526.             int j = i + std::uniform_int_distribution<int>(0, n_e - i - 1)(gen);
  527.             std::swap(electron_indices[i], electron_indices[j]);
  528.         }
  529.  
  530.         int exc1_coll_counter_temp = 0;
  531.         int super1_coll_counter_temp = 0;
  532.         int exc2_coll_counter_temp = 0;
  533.         int super2_coll_counter_temp = 0;
  534.         int null_coll_counter_temp = 0;
  535.         int ionization_counter_temp = 0;
  536.  
  537.         double energy_exc = 0.0; // calculating excitation losses each timestep
  538.         double energy_sup = 0.0; // calculating superelastic gains each timestep
  539.         double energy_Efield = 0.0; // calculating field gains/losses each timestep
  540.  
  541.  
  542.         // std::cout << "Progress: " << (t_elapsed/total_time)*100 << "\n";
  543.  
  544.         // setting flags to false each timestep
  545.         for (auto& e : electrons) e.collided_en = false;
  546.         for (auto& e : electrons) e.collided_ee = false;        
  547.  
  548.         int collision_counter_en = 0; // electron-neutral collision counter
  549.         int collision_counter_ee = 0; // e-e collisoin counter
  550.  
  551.         /// -- electrin field heating along E-Z axis begin--- /// -- first half!!!
  552.         for (int idx : electron_indices) {        
  553.             energy_Efield += ( C1*electrons[idx].vz*dt + C2*dt*dt )/q; // dividing by q to get eV            
  554.             // Update velocity component due to electric field
  555.             // double a_z = ((-1.0)*q * E_reduced) / m_e; // acceleration in z-direction, m/s^2
  556.             electrons[idx].vz += a_z * (dt); // only half timestep
  557.  
  558.             // Recalculate energy from updated velocity
  559.             double vx = electrons[idx].vx;
  560.             double vy = electrons[idx].vy;
  561.             double vz = electrons[idx].vz;
  562.             electrons[idx].energy = (vx*vx + vy*vy + vz*vz)*charge_mass_ratio;
  563.         }
  564.         // -------------------------------------------- filed heating ends ------------------------//  
  565.  
  566.         #pragma omp parallel
  567.         {
  568.             int tid = omp_get_thread_num();
  569.             auto& my_newborn = local_newborn[tid];
  570.  
  571.         #pragma omp for schedule(static) nowait
  572.         for (int ii = 0; ii < Ne_collided; ++ii) {
  573.            
  574.             int tid = omp_get_thread_num();
  575.             auto& gen = engines[tid];
  576.             auto& my_dis = dis;            
  577.             auto& my_maxwell = maxwell_neutral;
  578.  
  579.             int idx = electron_indices[ii];
  580.             // if (collision_counter_en >= Ne_collided) break; // quit if reached all collisions
  581.  
  582.  
  583.             Electron& e = electrons[idx];
  584.             if (e.collided_en) continue;  // Skip already collided electrons
  585.  
  586.             double dens_neutrals = (N_He/Volume);
  587.             double dens_exc_1 = (exc_1.size()/Volume);
  588.             double dens_exc_2 = (exc_2.size()/Volume);
  589.             double speed = sqrt_charge_mass*sqrt(e.energy);
  590.  
  591.             int bin_energy = static_cast<int>(e.energy / bin_width);
  592.             double nu_elastic = dens_neutrals * elastic_vec[bin_energy] * speed;
  593.             double nu_inelastic1 = dens_neutrals * inelastic1_vec[bin_energy] * speed;
  594.             double nu_superelastic1 = dens_exc_1 * superelastic1_vec[bin_energy] * speed;
  595.             double nu_inelastic2 = dens_neutrals * inelastic2_vec[bin_energy] * speed;
  596.             double nu_superelastic2 = dens_exc_2 * superelastic2_vec[bin_energy] * speed;
  597.             double nu_ionization = dens_neutrals * ionization_vec[bin_energy] * speed;
  598.  
  599.             double r = dis(gen);
  600.  
  601.             double P0 = nu_elastic/nu_max;
  602.             double P1 = (nu_elastic + nu_inelastic1)/nu_max;
  603.             double P2 = (nu_elastic + nu_inelastic1 + nu_superelastic1)/nu_max;
  604.             double P3 = (nu_elastic + nu_inelastic1 + nu_superelastic1 + nu_inelastic2)/nu_max;
  605.             double P4 = (nu_elastic + nu_inelastic1 + nu_superelastic1 + nu_inelastic2 + nu_superelastic2)/nu_max;
  606.             double P5 = (nu_elastic + nu_inelastic1 + nu_superelastic1 + nu_inelastic2 + nu_superelastic2 + nu_ionization)/nu_max;
  607.            
  608.             if (r < P0) {
  609.  
  610.                 // elastic collision happens
  611.  
  612.                 // ----   Collision energy redistribution module
  613.  
  614.                 // electron particle X Y Z initial velocities and energy
  615.                 double V0_x = e.vx;
  616.                 double V0_y = e.vy;
  617.                 double V0_z = e.vz;
  618.                 double E_0 = e.energy;
  619.                
  620.                 // neutral that collides with electron
  621.  
  622.                 // randomize particles each collision
  623.  
  624.                 NeutralParticle tmp_neutral;
  625.                 tmp_neutral.initialize(gen, dis, maxwell_neutral);
  626.                 double V_x_n = tmp_neutral.vx;
  627.                 double V_y_n = tmp_neutral.vy;
  628.                 double V_z_n = tmp_neutral.vz;
  629.                 double E_n = tmp_neutral.energy;
  630.  
  631.  
  632.                 double V0 = sqrt(V0_x*V0_x + V0_y*V0_y + V0_z*V0_z);
  633.  
  634.                 // initial relative velocity
  635.                 double V_rel_x = V0_x - V_x_n;
  636.                 double V_rel_y = V0_y - V_y_n;
  637.                 double V_rel_z = V0_z - V_z_n;
  638.  
  639.                 double V_rel = sqrt(V_rel_x*V_rel_x + V_rel_y*V_rel_y + V_rel_z*V_rel_z);
  640.                 if (std::isnan(V_rel) || std::isinf(V_rel) || fabs(V_rel) < 1e-12) {
  641.                     std::cerr << "Invalid V_rel copmuted: " << V_rel << " at timestep " << t_elapsed << std::endl;
  642.                 }
  643.                 //initial relative velocity unit vectors components
  644.                 double e1 = V_rel_x/V_rel;
  645.                 double e2 = V_rel_y/V_rel;
  646.                 double e3 = V_rel_z/V_rel;
  647.  
  648.                 double C = -V_rel/(1.0+(m_e/M_n));
  649.                 double beta = sqrt(1.0 - thresh0/e.energy);
  650.                 // generating random variables to calculate random direction of center-of-mass after the collision
  651.  
  652.                 double R1 = fast_uniform(gen);
  653.                 double R2 = fast_uniform(gen);
  654.  
  655.                 //// calculating spherical angles for center-of-mass random direction
  656.                 // double theta = acos(1.0- 2.0*R1);
  657.                 // double phi = 2*M_PI*R2;
  658.                 double cos_khi = 1.0 - 2.0*R1;
  659.                 double sin_khi = sqrt(1.0 - cos_khi*cos_khi);
  660.                 if (std::isnan(sin_khi) || std::isinf(sin_khi)) {
  661.                     std::cerr << "Invalid sin_khi copmuted: " << sin_khi << " at timestep " << t_elapsed << std::endl;
  662.                 }                
  663.  
  664.                 double phi = 2.0*M_PI*R2;
  665.  
  666.                 //calculating velocity change of electron
  667.  
  668.                 double dv_x = C*( (1.0-beta*cos_khi)*e1 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(-e2) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e1*e3)  );
  669.                 double dv_y = C*( (1.0-beta*cos_khi)*e2 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(e1) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e2*e3)  );
  670.                 double dv_z = C*( (1.0-beta*cos_khi)*e3 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(0.0) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(e1*e1 + e2*e2)  );
  671.  
  672.                 //updating electron energy and velocities  
  673.  
  674.                 //updating electron energy and velocities
  675.                
  676.                 e.vx += dv_x;
  677.                 e.vy += dv_y;
  678.                 e.vz += dv_z;
  679.  
  680.                 e.energy = (e.vx*e.vx + e.vy*e.vy + e.vz*e.vz) * charge_mass_ratio;
  681.  
  682.                 collision_counter_en++;
  683.                 el_coll_counter++;
  684.  
  685.                 e.collided_en = true;
  686.             }      
  687.  
  688.             else if (r < P1) {
  689.                 if (e.energy < thresh1) {
  690.                     null_coll_counter++;
  691.                     collision_counter_en++;
  692.                     e.collided_en = true;
  693.                     continue;
  694.                 }
  695.                 else {                
  696.                     //inelastic 1(triplet) collision happens
  697.  
  698.                     // ----   Collision energy redistribution module
  699.    
  700.                     // electron particle X Y Z initial velocities and energy
  701.                     double V0_x = e.vx;
  702.                     double V0_y = e.vy;
  703.                     double V0_z = e.vz;
  704.                     double E_0 = e.energy;
  705.                    
  706.                     // neutral that collides with electron
  707.  
  708.                     // randomize particles each collision
  709.  
  710.                     NeutralParticle tmp_neutral;
  711.                     tmp_neutral.initialize(gen, dis, maxwell_neutral);
  712.                     double V_x_n = tmp_neutral.vx;
  713.                     double V_y_n = tmp_neutral.vy;
  714.                     double V_z_n = tmp_neutral.vz;
  715.                     double E_n = tmp_neutral.energy;
  716.  
  717.    
  718.                     double V0 = sqrt(V0_x*V0_x + V0_y*V0_y + V0_z*V0_z);
  719.  
  720.                     // initial relative velocity
  721.                     double V_rel_x = V0_x - V_x_n;
  722.                     double V_rel_y = V0_y - V_y_n;
  723.                     double V_rel_z = V0_z - V_z_n;
  724.  
  725.                     double V_rel = sqrt(V_rel_x*V_rel_x + V_rel_y*V_rel_y + V_rel_z*V_rel_z);
  726.                    
  727.                     if (std::isnan(V_rel) || std::isinf(V_rel) || fabs(V_rel) < 1e-12) {
  728.                         std::cerr << "Invalid V_rel copmuted: " << V_rel << " at timestep " << t_elapsed << std::endl;
  729.                     }
  730.  
  731.                     //initial relative velocity unit vectors components
  732.                     double e1 = V_rel_x/V_rel;
  733.                     double e2 = V_rel_y/V_rel;
  734.                     double e3 = V_rel_z/V_rel;
  735.  
  736.                     double C = -V_rel/(1.0+(m_e/M_n));
  737.                     double beta = sqrt(abs(1.0 - thresh1/e.energy));
  738.                     // generating random variables to calculate random direction of center-of-mass after the collision
  739.    
  740.                     double R1 = fast_uniform(gen);
  741.                     double R2 = fast_uniform(gen);
  742.    
  743.                     //// calculating spherical angles for center-of-mass random direction
  744.                     // double theta = acos(1.0- 2.0*R1);
  745.                     // double phi = 2*M_PI*R2;
  746.                     double cos_khi = 1.0 - 2.0*R1;
  747.                     double sin_khi = sqrt(1.0 - cos_khi*cos_khi);
  748.                     if (std::isnan(sin_khi) || std::isinf(sin_khi)) {
  749.                         std::cerr << "Invalid sin_khi copmuted: " << sin_khi << " at timestep " << t_elapsed << std::endl;
  750.                     }      
  751.                     double phi = 2.0*M_PI*R2;
  752.  
  753.                     //calculating velocity change of electron
  754.  
  755.                     double dv_x = C*( (1.0-beta*cos_khi)*e1 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(-e2) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e1*e3)  );
  756.                     double dv_y = C*( (1.0-beta*cos_khi)*e2 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(e1) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e2*e3)  );
  757.                     double dv_z = C*( (1.0-beta*cos_khi)*e3 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(0.0) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(e1*e1 + e2*e2)  );
  758.  
  759.                     //updating electron energy and velocities        
  760.  
  761.                     e.vx += dv_x;
  762.                     e.vy += dv_y;
  763.                     e.vz += dv_z;
  764.  
  765.                     e.energy = (e.vx*e.vx + e.vy*e.vy + e.vz*e.vz) * charge_mass_ratio;
  766.  
  767.                     collision_counter_en++;  
  768.                     exc1_coll_counter++;
  769.                     exc1_coll_counter_temp++;
  770.  
  771.                     e.collided_en = true;
  772.  
  773.                 }
  774.             }    
  775.  
  776.             else if (r < P2) {
  777.  
  778.                 //superelastic 1(triplet -> ground state) collision happens
  779.  
  780.                 if (exc_1.empty()) {
  781.                     null_coll_counter++;
  782.                     collision_counter_en++;
  783.                     e.collided_en = true;
  784.                     continue;            
  785.                 }
  786.  
  787.                 // ----   Collision energy redistribution module
  788.  
  789.                 // electron particle X Y Z initial velocities and energy
  790.                 double V0_x = e.vx;
  791.                 double V0_y = e.vy;
  792.                 double V0_z = e.vz;
  793.                 double E_0 = e.energy;
  794.  
  795.                 // neutral that collides with electron
  796.                 // taking particles from dynamic array of excited neutrals
  797.  
  798.                 int index = std::uniform_int_distribution<int>(0, exc_1.size()-1)(gen);
  799.                 Excited_neutral& exc = exc_1[index];
  800.                 double V_x_n = exc.vx;
  801.                 double V_y_n = exc.vy;
  802.                 double V_z_n = exc.vz;
  803.                 double E_n = exc.energy;
  804.                
  805.                 double V0 = sqrt(V0_x*V0_x + V0_y*V0_y + V0_z*V0_z);
  806.  
  807.                 // initial relative velocity
  808.                 double V_rel_x = V0_x - V_x_n;
  809.                 double V_rel_y = V0_y - V_y_n;
  810.                 double V_rel_z = V0_z - V_z_n;
  811.  
  812.                 double V_rel = sqrt(V_rel_x*V_rel_x + V_rel_y*V_rel_y + V_rel_z*V_rel_z);
  813.  
  814.                 if (std::isnan(V_rel) || std::isinf(V_rel) || fabs(V_rel) < 1e-12) {
  815.                     std::cerr << "Invalid V_rel copmuted: " << V_rel << " at timestep " << t_elapsed << std::endl;
  816.                 }
  817.  
  818.                 //initial relative velocity unit vectors components
  819.                 double e1 = V_rel_x/V_rel;
  820.                 double e2 = V_rel_y/V_rel;
  821.                 double e3 = V_rel_z/V_rel;
  822.  
  823.                 double C = -V_rel/(1.0+(m_e/M_n));
  824.                 double beta = sqrt(1.0 + thresh1/e.energy);
  825.                 // generating random variables to calculate random direction of center-of-mass after the collision
  826.  
  827.                 double R1 = fast_uniform(gen);
  828.                 double R2 = fast_uniform(gen);
  829.  
  830.                 //// calculating spherical angles for center-of-mass random direction
  831.                 // double theta = acos(1.0- 2.0*R1);
  832.                 // double phi = 2*M_PI*R2;
  833.                 double cos_khi = 1.0 - 2.0*R1;
  834.                 double sin_khi = sqrt(1.0 - cos_khi*cos_khi);
  835.                 if (std::isnan(sin_khi) || std::isinf(sin_khi)) {
  836.                     std::cerr << "Invalid sin_khi copmuted: " << sin_khi << " at timestep " << t_elapsed << std::endl;
  837.                 }      
  838.                 double phi = 2.0*M_PI*R2;
  839.  
  840.                 //calculating velocity change of electron
  841.  
  842.                 double dv_x = C*( (1.0-beta*cos_khi)*e1 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(-e2) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e1*e3)  );
  843.                 double dv_y = C*( (1.0-beta*cos_khi)*e2 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(e1) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e2*e3)  );
  844.                 double dv_z = C*( (1.0-beta*cos_khi)*e3 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(0.0) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(e1*e1 + e2*e2)  );
  845.  
  846.                 //updating electron energy and velocities        
  847.                
  848.                 e.vx += dv_x;
  849.                 e.vy += dv_y;
  850.                 e.vz += dv_z;
  851.  
  852.                 e.energy = (e.vx*e.vx + e.vy*e.vy + e.vz*e.vz) * charge_mass_ratio;
  853.  
  854.                 // //counting collisions, working with flags, popping atom out of the vector
  855.                 // if (!exc_1.empty()) {
  856.                 //     std::swap(exc_1[index], exc_1.back());
  857.                 //     exc_1.pop_back();
  858.                 //     N_He++;
  859.                 // }
  860.                 collision_counter_en++;  
  861.                 super1_coll_counter++;
  862.                 super1_coll_counter_temp++;
  863.                 energy_sup += thresh1;
  864.  
  865.                 e.collided_en = true;
  866.             }  
  867.  
  868.             else if (r < P3) {
  869.  
  870.                 if (e.energy < thresh1) {
  871.                     null_coll_counter++;
  872.                     collision_counter_en++;
  873.                     e.collided_en = true;
  874.                     continue;
  875.                 }
  876.                 else {
  877.  
  878.                     //inelastic 1(singlet) excitation collision happens
  879.  
  880.                     // ----   Collision energy redistribution module
  881.    
  882.                     // electron particle X Y Z initial velocities and energy
  883.                     double V0_x = e.vx;
  884.                     double V0_y = e.vy;
  885.                     double V0_z = e.vz;
  886.                     double E_0 = e.energy;
  887.                    
  888.                     // neutral that collides with electron
  889.  
  890.                     // randomize particles each collision
  891.  
  892.                     NeutralParticle tmp_neutral;
  893.                     tmp_neutral.initialize(gen, dis, maxwell_neutral);
  894.                     double V_x_n = tmp_neutral.vx;
  895.                     double V_y_n = tmp_neutral.vy;
  896.                     double V_z_n = tmp_neutral.vz;
  897.                     double E_n = tmp_neutral.energy;
  898.  
  899.    
  900.                     double V0 = sqrt(V0_x*V0_x + V0_y*V0_y + V0_z*V0_z);
  901.  
  902.                     // initial relative velocity
  903.                     double V_rel_x = V0_x - V_x_n;
  904.                     double V_rel_y = V0_y - V_y_n;
  905.                     double V_rel_z = V0_z - V_z_n;
  906.  
  907.                     double V_rel = sqrt(V_rel_x*V_rel_x + V_rel_y*V_rel_y + V_rel_z*V_rel_z);
  908.  
  909.                     if (std::isnan(V_rel) || std::isinf(V_rel) || fabs(V_rel) < 1e-12) {
  910.                         std::cerr << "Invalid V_rel copmuted: " << V_rel << " at timestep " << t_elapsed << std::endl;
  911.                     }
  912.  
  913.                     //initial relative velocity unit vectors components
  914.                     double e1 = V_rel_x/V_rel;
  915.                     double e2 = V_rel_y/V_rel;
  916.                     double e3 = V_rel_z/V_rel;
  917.  
  918.                     double C = -V_rel/(1.0+(m_e/M_n));
  919.                     double beta = sqrt(abs(1.0 - thresh2/e.energy));
  920.                     // generating random variables to calculate random direction of center-of-mass after the collision
  921.    
  922.                     double R1 = fast_uniform(gen);
  923.                     double R2 = fast_uniform(gen);
  924.    
  925.                     //// calculating spherical angles for center-of-mass random direction
  926.                     // double theta = acos(1.0- 2.0*R1);
  927.                     // double phi = 2*M_PI*R2;
  928.                     double cos_khi = 1.0 - 2.0*R1;
  929.                     double sin_khi = sqrt(1.0 - cos_khi*cos_khi);
  930.                     if (std::isnan(sin_khi) || std::isinf(sin_khi)) {
  931.                         std::cerr << "Invalid sin_khi copmuted: " << sin_khi << " at timestep " << t_elapsed << std::endl;
  932.                     }      
  933.                     double phi = 2.0*M_PI*R2;
  934.  
  935.                     //calculating velocity change of electron
  936.  
  937.                     double dv_x = C*( (1.0-beta*cos_khi)*e1 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(-e2) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e1*e3)  );
  938.                     double dv_y = C*( (1.0-beta*cos_khi)*e2 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(e1) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e2*e3)  );
  939.                     double dv_z = C*( (1.0-beta*cos_khi)*e3 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(0.0) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(e1*e1 + e2*e2)  );
  940.  
  941.                     //updating electron energy and velocities        
  942.  
  943.                     e.vx += dv_x;
  944.                     e.vy += dv_y;
  945.                     e.vz += dv_z;
  946.  
  947.                     e.energy = (e.vx*e.vx + e.vy*e.vy + e.vz*e.vz) * charge_mass_ratio;
  948.  
  949.                     collision_counter_en++;  
  950.                     exc2_coll_counter++;
  951.                     exc2_coll_counter_temp++;
  952.  
  953.                     e.collided_en = true;
  954.  
  955.                 }
  956.             }
  957.  
  958.             else if (r < P4) {
  959.  
  960.                 //supernelastic 1(singlet -> ground state) collision happens
  961.  
  962.                 if (exc_2.empty()) {
  963.                     null_coll_counter++;
  964.                     collision_counter_en++;
  965.                     e.collided_en = true;
  966.                     continue;            
  967.                 }
  968.  
  969.                 // ----   Collision energy redistribution module
  970.  
  971.                 // electron particle X Y Z initial velocities and energy
  972.                 double V0_x = e.vx;
  973.                 double V0_y = e.vy;
  974.                 double V0_z = e.vz;
  975.                 double E_0 = e.energy;
  976.  
  977.                 // neutral that collides with electron
  978.                 // taking particles from dynamic array of excited neutrals
  979.  
  980.                 int index = std::uniform_int_distribution<int>(0, exc_2.size()-1)(gen);
  981.                 Excited_neutral& exc = exc_2[index];
  982.                 double V_x_n = exc.vx;
  983.                 double V_y_n = exc.vy;
  984.                 double V_z_n = exc.vz;
  985.                 double E_n = exc.energy;
  986.                
  987.                 double V0 = sqrt(V0_x*V0_x + V0_y*V0_y + V0_z*V0_z);
  988.  
  989.                 // initial relative velocity
  990.                 double V_rel_x = V0_x - V_x_n;
  991.                 double V_rel_y = V0_y - V_y_n;
  992.                 double V_rel_z = V0_z - V_z_n;
  993.  
  994.                 double V_rel = sqrt(V_rel_x*V_rel_x + V_rel_y*V_rel_y + V_rel_z*V_rel_z);
  995.  
  996.                 if (std::isnan(V_rel) || std::isinf(V_rel) || fabs(V_rel) < 1e-12) {
  997.                     std::cerr << "Invalid V_rel copmuted: " << V_rel << " at timestep " << t_elapsed << std::endl;
  998.                 }
  999.  
  1000.                 //initial relative velocity unit vectors components
  1001.                 double e1 = V_rel_x/V_rel;
  1002.                 double e2 = V_rel_y/V_rel;
  1003.                 double e3 = V_rel_z/V_rel;
  1004.  
  1005.                 double C = -V_rel/(1.0+(m_e/M_n));
  1006.                 double beta = sqrt(1.0 + thresh2/e.energy);
  1007.                 // generating random variables to calculate random direction of center-of-mass after the collision
  1008.  
  1009.                 double R1 = fast_uniform(gen);
  1010.                 double R2 = fast_uniform(gen);
  1011.  
  1012.                 //// calculating spherical angles for center-of-mass random direction
  1013.                 // double theta = acos(1.0- 2.0*R1);
  1014.                 // double phi = 2*M_PI*R2;
  1015.                 double cos_khi = 1.0 - 2.0*R1;
  1016.                 double sin_khi = sqrt(1.0 - cos_khi*cos_khi);
  1017.                 if (std::isnan(sin_khi) || std::isinf(sin_khi)) {
  1018.                     std::cerr << "Invalid sin_khi copmuted: " << sin_khi << " at timestep " << t_elapsed << std::endl;
  1019.                 }      
  1020.                 double phi = 2.0*M_PI*R2;
  1021.  
  1022.                 //calculating velocity change of electron
  1023.  
  1024.                 double dv_x = C*( (1.0-beta*cos_khi)*e1 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(-e2) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e1*e3)  );
  1025.                 double dv_y = C*( (1.0-beta*cos_khi)*e2 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(e1) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e2*e3)  );
  1026.                 double dv_z = C*( (1.0-beta*cos_khi)*e3 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(0.0) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(e1*e1 + e2*e2)  );
  1027.  
  1028.                 //updating electron energy and velocities        
  1029.                
  1030.                 e.vx += dv_x;
  1031.                 e.vy += dv_y;
  1032.                 e.vz += dv_z;
  1033.  
  1034.                 e.energy = (e.vx*e.vx + e.vy*e.vy + e.vz*e.vz) * charge_mass_ratio;
  1035.                 //counting collisions, working with flags, popping atom out of the vector
  1036.  
  1037.                 // if (!exc_2.empty()) {
  1038.                 //     std::swap(exc_2[index], exc_2.back());
  1039.                 //     exc_2.pop_back();
  1040.                 //     N_He++;
  1041.                 // }
  1042.  
  1043.                 collision_counter_en++;  
  1044.                 super2_coll_counter++;
  1045.                 super2_coll_counter_temp++;
  1046.                 energy_sup += thresh2;
  1047.  
  1048.                 e.collided_en = true;
  1049.             }              
  1050.  
  1051.             else if (r < P5) {
  1052.  
  1053.                 //ionization e-n collision happens
  1054.  
  1055.                 if (e.energy < thresh3) {
  1056.                     null_coll_counter++;
  1057.                     collision_counter_en++;
  1058.                     e.collided_en = true;
  1059.                     continue;
  1060.                 }              
  1061.                 else {
  1062.                     // ----   Collision energy redistribution module
  1063.    
  1064.                     // electron particle X Y Z initial velocities and energy
  1065.                     double V0_x = e.vx;
  1066.                     double V0_y = e.vy;
  1067.                     double V0_z = e.vz;
  1068.                     double E_0 = e.energy;
  1069.                    
  1070.                     // neutral that collides with electron
  1071.  
  1072.                     // randomize particles each collision
  1073.  
  1074.                     NeutralParticle tmp_neutral;
  1075.                     tmp_neutral.initialize(gen, dis, maxwell_neutral);
  1076.                     double V_x_n = tmp_neutral.vx;
  1077.                     double V_y_n = tmp_neutral.vy;
  1078.                     double V_z_n = tmp_neutral.vz;
  1079.                     double E_n = tmp_neutral.energy;
  1080.  
  1081.    
  1082.                     double V0 = sqrt(V0_x*V0_x + V0_y*V0_y + V0_z*V0_z);
  1083.  
  1084.                     // initial relative velocity
  1085.                     double V_rel_x = V0_x - V_x_n;
  1086.                     double V_rel_y = V0_y - V_y_n;
  1087.                     double V_rel_z = V0_z - V_z_n;
  1088.  
  1089.                     double V_rel = sqrt(V_rel_x*V_rel_x + V_rel_y*V_rel_y + V_rel_z*V_rel_z);
  1090.                    
  1091.                     if (std::isnan(V_rel) || std::isinf(V_rel) || fabs(V_rel) < 1e-12) {
  1092.                         std::cerr << "Invalid V_rel copmuted: " << V_rel << " at timestep " << t_elapsed << std::endl;
  1093.                     }
  1094.  
  1095.                     //initial relative velocity unit vectors components
  1096.                     double e1 = V_rel_x/V_rel;
  1097.                     double e2 = V_rel_y/V_rel;
  1098.                     double e3 = V_rel_z/V_rel;
  1099.  
  1100.                     double C = -V_rel/(1.0+(m_e/M_n));
  1101.                     double beta = sqrt(abs(1.0 - thresh3/e.energy));
  1102.                     // generating random variables to calculate random direction of center-of-mass after the collision
  1103.    
  1104.                     double R1 = fast_uniform(gen);
  1105.                     double R2 = fast_uniform(gen);
  1106.    
  1107.                     //// calculating spherical angles for center-of-mass random direction
  1108.                     // double theta = acos(1.0- 2.0*R1);
  1109.                     // double phi = 2*M_PI*R2;
  1110.                     double cos_khi = 1.0 - 2.0*R1;
  1111.                     double sin_khi = sqrt(1.0 - cos_khi*cos_khi);
  1112.                     if (std::isnan(sin_khi) || std::isinf(sin_khi)) {
  1113.                         std::cerr << "Invalid sin_khi copmuted: " << sin_khi << " at timestep " << t_elapsed << std::endl;
  1114.                     }      
  1115.                     double phi = 2.0*M_PI*R2;
  1116.  
  1117.                     //calculating velocity change of electron
  1118.  
  1119.                     double dv_x = C*( (1.0-beta*cos_khi)*e1 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(-e2) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e1*e3)  );
  1120.                     double dv_y = C*( (1.0-beta*cos_khi)*e2 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(e1) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(-e2*e3)  );
  1121.                     double dv_z = C*( (1.0-beta*cos_khi)*e3 + (beta*sin_khi*sin(phi)/sqrt(e1*e1+e2*e2))*(0.0) + (beta*sin_khi*cos(phi)/sqrt(e1*e1+e2*e2))*(e1*e1 + e2*e2)  );
  1122.  
  1123.                     //updating electron energy and velocities        
  1124.  
  1125.                     //new electron emerges, and two electrons shares the energy and obtain an equal directions (no physical background for this assumption,
  1126.                     // should think what to do with it - maybe regenerate velocities)
  1127.                    
  1128.                     //scatterd electron velocity rescaled by a factor of 2
  1129.                     e.vx = (e.vx + dv_x)/sqrt(2.0);
  1130.                     e.vy = (e.vy + dv_y)/sqrt(2.0);
  1131.                     e.vz = (e.vz + dv_z)/sqrt(2.0);
  1132.                     e.energy = (e.vx*e.vx + e.vy*e.vy + e.vz*e.vz) * charge_mass_ratio;
  1133.                     e.collided_en = true;
  1134.  
  1135.                     //ejected electron handling
  1136.  
  1137.                     // velocity angles in spherical coordinates
  1138.                     double phi_ej = 2*M_PI*fast_uniform(gen);
  1139.                     double cosTheta_ej = 2.0*fast_uniform(gen) - 1.0;
  1140.                     double sinTheta_ej = sqrt(1.0 - cosTheta_ej*cosTheta_ej);
  1141.  
  1142.                        
  1143.                     double energy_ej = e.energy; // ejected electron energy is the same
  1144.                        
  1145.                     double speed = sqrt(2*energy_ej*q/m_e);
  1146.                     double vx_ej = speed * sinTheta_ej * cos(phi_ej);
  1147.                     double vy_ej = speed * sinTheta_ej * sin(phi_ej);
  1148.                     double vz_ej = speed * cosTheta_ej;
  1149.                    
  1150.                     Electron ejected_e{vx_ej, vy_ej, vz_ej, energy_ej, false, false};
  1151.                     // electrons.push_back(ejected_e);
  1152.                    
  1153.                     my_newborn.push_back(ejected_e);
  1154.  
  1155.                     collision_counter_en++;  
  1156.                     ionization_counter++;
  1157.                     ionization_counter_temp++;
  1158.  
  1159.                 }
  1160.             }
  1161.  
  1162.             else {
  1163.                 // null-collision
  1164.                 collision_counter_en++;
  1165.                 null_coll_counter++;
  1166.                 e.collided_en = true;
  1167.             }
  1168.         }
  1169.         }
  1170.  
  1171.         for (auto& vec : local_newborn) {
  1172.         if (!vec.empty()) {
  1173.             electrons.insert(electrons.end(),
  1174.                             std::make_move_iterator(vec.begin()),
  1175.                             std::make_move_iterator(vec.end()));
  1176.             n_e += vec.size();
  1177.         }
  1178.         }
  1179.  
  1180.         t_elapsed += dt; // Advance time
  1181.  
  1182.  
  1183.         // calculating mean energy
  1184.         if (static_cast<int>(t_elapsed/dt)%print_interval == 0) {
  1185.             double total_energy = 0.0;
  1186.             for (const auto& e : electrons) total_energy += e.energy;
  1187.             double mean_energy = total_energy / n_e;
  1188.             file8 << t_elapsed << " " << mean_energy << "\n";            
  1189.             // file_temp << t_elapsed << " " << exc_1.size() << " " << exc_2.size() << "\n";
  1190.             std::cout << "Progress: " << (t_elapsed/total_time)*100 << "%" << "\n";
  1191.             // file16 << t_elapsed << " " << energy_Efield/n_e << " " << energy_sup/n_e << "\n";
  1192.             file17 << t_elapsed << " " << n_e << "\n";
  1193.         }        
  1194.  
  1195.     }
  1196.  
  1197.     // ----- final electron energies distribution begins
  1198.    
  1199.     for (int i = 0; i < n_e; i++){
  1200.  
  1201.         file2 << i << " " << electrons[i].energy << "\n";
  1202.  
  1203.         int bin = static_cast<int>( (electrons[i].energy - Emin)/bin_width_smooth);
  1204.         if (bin >=0 && bin < histo_maxwell.size())
  1205.             histo_maxwell[bin]++;
  1206.     }
  1207.  
  1208.     int check = 0;
  1209.     for (int i = 0; i < N_smooth; i++){
  1210.         check += histo_maxwell[i];
  1211.         double bin_center = Emin + (i + 0.5) * bin_width_smooth;
  1212.         file4 << bin_center << " " <<  static_cast<double>(histo_maxwell[i])/(sqrt(bin_center)*electrons.size()*bin_width_smooth) << "\n"; // getting f(E)
  1213.     }
  1214.  
  1215.         std::cout << "Total # of electrons in a final histogram: " << check << "\n";
  1216.         std::cout << "Final nu max: " << nu_max << "\n";
  1217.  
  1218.     // ----- final electron energies distribution ends
  1219.  
  1220.  
  1221.     file0.close(); file1.close(); file2.close(); file3.close(); file4.close(); file5.close();
  1222.     file6.close(); file7.close(); file8.close(); file9.close(); file10.close(); file11.close();
  1223.     file12.close(); file13.close(); file14.close(); file15.close(); file_temp.close(); file16.close(); file17.close();
  1224.  
  1225.     clock_t end = clock();
  1226.  
  1227.     double elapsed = (double)(end - start) / CLOCKS_PER_SEC;
  1228.  
  1229.     std::cout << "Elapsed time: " << elapsed << " seconds" << "\n";
  1230.  
  1231.     return 0;
  1232.  
  1233. }
Add Comment
Please, Sign In to add comment