phystota

v3.3 - MPI pragma unroll attempt

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