Advertisement
phystota

v3.0 - Hagelaar forms

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