phystota

eduPIC_DRR_v1.2 - tracking nu avg

May 26th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 69.37 KB | None | 0 0
  1. //-------------------------------------------------------------------//
  2. //         eduPIC : educational 1d3v PIC/MCC simulation code         //
  3. //           version 1.0, release date: March 16, 2021               //
  4. //                       :) Share & enjoy :)                         //
  5. //-------------------------------------------------------------------//
  6. // When you use this code, you are required to acknowledge the       //
  7. // authors by citing the paper:                                      //
  8. // Z. Donko, A. Derzsi, M. Vass, B. Horvath, S. Wilczek              //
  9. // B. Hartmann, P. Hartmann:                                         //
  10. // "eduPIC: an introductory particle based  code for radio-frequency //
  11. // plasma simulation"                                                //
  12. // Plasma Sources Science and Technology, vol 30, pp. 095017 (2021)  //
  13. //-------------------------------------------------------------------//
  14. // Disclaimer: The eduPIC (educational Particle-in-Cell/Monte Carlo  //
  15. // Collisions simulation code), Copyright (C) 2021                   //
  16. // Zoltan Donko et al. is free software: you can redistribute it     //
  17. // and/or modify it under the terms of the GNU General Public License//
  18. // as published by the Free Software Foundation, version 3.          //
  19. // This program is distributed in the hope that it will be useful,   //
  20. // but WITHOUT ANY WARRANTY; without even the implied warranty of    //
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  //
  22. // General Public License for more details at                        //
  23. // https://www.gnu.org/licenses/gpl-3.0.html.                        //
  24. //-------------------------------------------------------------------//
  25.  
  26. #include <cstdio>
  27. #include <cstdlib>
  28. #include <cstring>
  29. #include <cstdbool>
  30. #include <cmath>
  31. #include <ctime>
  32. #include <random>
  33. #include <vector>
  34. #include <string>
  35. #include <fstream>
  36. #include <sstream>
  37. #include <algorithm>    
  38. #include <stdexcept>
  39. #include <iostream>
  40.  
  41. using namespace::std;
  42.  
  43. // constants
  44.  
  45. const double     PI             = 3.141592653589793;          // mathematical constant Pi
  46. const double     TWO_PI         = 2.0 * PI;                   // two times Pi
  47. const double     E_CHARGE       = 1.60217662e-19;             // electron charge [C]
  48. const double     EV_TO_J        = E_CHARGE;                   // eV <-> Joule conversion factor
  49. const double     E_MASS         = 9.109e-31;                  // mass of electron [kg]
  50. const double     HE_MASS        = 6.67e-27;                   // mass of He atom [kg]
  51. const double     MU_HEHE        = HE_MASS / 2.0;              // reduced mass of two He atoms [kg]
  52. const double     K_BOLTZMANN    = 1.38064852e-23;             // Boltzmann's constant [J/K]
  53. const double     EPSILON0       = 8.85418781e-12;             // permittivity of free space [F/m]
  54.  
  55. // simulation parameters
  56.  
  57. const int        N_G            = 129;                        // number of grid points
  58. const int        N_T            = 400;                        // time steps within an RF period
  59.  
  60. const double     FREQUENCY      = 13.56e6;                    // driving frequency [Hz]
  61. const double     VOLTAGE        = 450.0;                      // voltage amplitude [V]
  62. const double     L              = 0.067;                      // electrode gap [m]
  63. const double     PRESSURE       = 3.99283551984;              // gas pressure [Pa] // n*k*T to match Turner's case
  64. const double     T_neutral      = 300.0;                      // background gas temperature [K] (also ion temperature)
  65. const double     T_electron     = 30000.0;                    // initial electron temperatutre [K]
  66. const double     WEIGHT         = 41875.0;                  // weight of superparticles
  67. const double     ELECTRODE_AREA = 1.6e-4;                     // (fictive) electrode area [m^2]
  68. const int        N_INIT         = 65536;                      // number of initial electrons and ions
  69.  
  70. // additional (derived) constants
  71.  
  72. const double     PERIOD         = 1.0 / FREQUENCY;                           // RF period length [s]
  73. const double     DT_E           = PERIOD / (double)(N_T);                    // electron time step [s]
  74. const int        N_SUB          = 5;                                         // ions move only in these cycles (subcycling)
  75. const int        N_avg          = 1;                          // number of cycles to average rates --- Artem
  76.       int        counter        = 0;                          // cycles since update of rates --- Artem
  77. const double     DT_I           = N_SUB * DT_E;                              // ion time step [s]
  78. const double     DX             = L / (double)(N_G - 1);                     // spatial grid division [m]
  79. const double     INV_DX         = 1.0 / DX;                                  // inverse of spatial grid size [1/m]
  80. const double     GAS_DENSITY    = PRESSURE / (K_BOLTZMANN * T_neutral);      // background gas density [1/m^3]
  81. const double     OMEGA          = TWO_PI * FREQUENCY;                        // angular frequency [rad/s]
  82.  
  83. // electron and ion cross sections
  84.  
  85. const int        N_CS           = 8;                          // total number of processes / cross sections
  86. const int        E_ELA          = 0;                          // process identifier: electron/elastic
  87. const int        E_EXC_1        = 1;                          // process identifier: electron/excitation1
  88. const int        E_EXC_2        = 2;                          // process identifier: electron/excitation1
  89. const int        E_ION          = 3;                          // process identifier: electron/ionization
  90. const int        I_ISO          = 4;                          // process identifier: ion/elastic/isotropic
  91. const int        I_BACK         = 5;                          // process identifier: ion/elastic/backscattering
  92.  
  93. const int        E_SUPER_1      = 6;                          // triplet excitation - Artem
  94. const int        E_SUPER_2      = 7;                          // singlet excitation - Artem
  95.  
  96. const double     E_EXC_TH_1     = 19.82;                      // electron impact excitation threshold [eV]
  97. const double     E_EXC_TH_2     = 20.61;                      // electron impact excitation threshold [eV]
  98. const double     E_ION_TH       = 24.587;                     // electron impact ionization threshold [eV]
  99. const int        CS_RANGES      = 1000000;                    // number of entries in cross section arrays
  100. const double     DE_CS          = 0.001;                      // energy division in cross section arrays [eV]
  101. typedef float    cross_section[CS_RANGES];                    // cross section array
  102. cross_section    sigma[N_CS];                                 // set of cross section arrays
  103. cross_section    sigma_tot_e;                                 // total macroscopic cross section of electrons
  104. cross_section    sigma_tot_i;                                 // total macroscopic cross section of ions
  105.  
  106. double nu_avg;                                                // average nu for electrons thorugh 1 cycle
  107.  
  108. // particle coordinates
  109.  
  110. const int        MAX_N_P = 10000000;                           // maximum number of particles (electrons / ions)
  111. typedef double   particle_vector[MAX_N_P];                    // array for particle properties
  112. int              N_e = 0;                                     // number of electrons
  113. int              N_i = 0;                                     // number of ions
  114. int              N_e1 = 0;                                    // number of singlet excited states
  115. int              N_e2 = 0;                                    // number of triplet excited states
  116. particle_vector  x_e, vx_e, vy_e, vz_e;                       // coordinates of electrons (one spatial, three velocity components)
  117. particle_vector  x_i, vx_i, vy_i, vz_i;                       // coordinates of ions (one spatial, three velocity components)
  118.  
  119.  
  120.  
  121.  
  122. typedef double   xvector[N_G];                                // array for quantities defined at gird points
  123. xvector          efield,pot;                                  // electric field and potential
  124. xvector          e_density,i_density;                         // electron and ion densities
  125. xvector          cumul_e_density,cumul_i_density;             // cumulative densities
  126.  
  127. //excited states handling
  128. xvector e1_density;
  129. xvector e2_density;
  130. xvector sum_electron_density; xvector avg_electron_density;
  131.  
  132. xvector sum_rate1f = {0.0}; xvector sum_rate1b = {0.0}; xvector sum_rate2f = {0.0}; xvector sum_rate2b = {0.0};
  133.  
  134. xvector avg_rate1f = {0.0}; xvector avg_rate1b = {0.0}; xvector avg_rate2f = {0.0}; xvector avg_rate2b = {0.0};
  135.  
  136.                                        // array for Triplet excitation rates!!! Artem
  137. xvector S1 = {0.0};
  138. xvector S2 = {0.0};                                           // source terms for DRR module -- Artem
  139.  
  140. typedef unsigned long long int Ullong;                        // compact name for 64 bit unsigned integer
  141. Ullong       N_e_abs_pow  = 0;                                // counter for electrons absorbed at the powered electrode
  142. Ullong       N_e_abs_gnd  = 0;                                // counter for electrons absorbed at the grounded electrode
  143. Ullong       N_i_abs_pow  = 0;                                // counter for ions absorbed at the powered electrode
  144. Ullong       N_i_abs_gnd  = 0;                                // counter for ions absorbed at the grounded electrode
  145.  
  146. // electron energy probability function
  147.  
  148. const int    N_EEPF  = 2000;                                 // number of energy bins in Electron Energy Probability Function (EEPF)
  149. const double DE_EEPF = 0.05;                                 // resolution of EEPF [eV]
  150. typedef double eepf_vector[N_EEPF];                          // array for EEPF
  151. eepf_vector eepf     = {0.0};                                // time integrated EEPF in the center of the plasma
  152.  
  153. // ion flux-energy distributions
  154.  
  155. const int    N_IFED   = 200;                                 // number of energy bins in Ion Flux-Energy Distributions (IFEDs)
  156. const double DE_IFED  = 1.0;                                 // resolution of IFEDs [eV]
  157. typedef int  ifed_vector[N_IFED];                            // array for IFEDs
  158. ifed_vector  ifed_pow = {0};                                 // IFED at the powered electrode
  159. ifed_vector  ifed_gnd = {0};                                 // IFED at the grounded electrode
  160. double       mean_i_energy_pow;                              // mean ion energy at the powered electrode
  161. double       mean_i_energy_gnd;                              // mean ion energy at the grounded electrode
  162.  
  163. // spatio-temporal (XT) distributions
  164.  
  165. const int N_BIN                     = 20;                    // number of time steps binned for the XT distributions
  166. const int N_XT                      = N_T / N_BIN;           // number of spatial bins for the XT distributions
  167. typedef double xt_distr[N_G][N_XT];                          // array for XT distributions (decimal numbers)
  168. xt_distr pot_xt                     = {0.0};                 // XT distribution of the potential
  169. xt_distr efield_xt                  = {0.0};                 // XT distribution of the electric field
  170. xt_distr ne_xt                      = {0.0};                 // XT distribution of the electron density
  171. xt_distr ni_xt                      = {0.0};                 // XT distribution of the ion density
  172. xt_distr ue_xt                      = {0.0};                 // XT distribution of the mean electron velocity
  173. xt_distr ui_xt                      = {0.0};                 // XT distribution of the mean ion velocity
  174. xt_distr je_xt                      = {0.0};                 // XT distribution of the electron current density
  175. xt_distr ji_xt                      = {0.0};                 // XT distribution of the ion current density
  176. xt_distr powere_xt                  = {0.0};                 // XT distribution of the electron powering (power absorption) rate
  177. xt_distr poweri_xt                  = {0.0};                 // XT distribution of the ion powering (power absorption) rate
  178. xt_distr meanee_xt                  = {0.0};                 // XT distribution of the mean electron energy
  179. xt_distr meanei_xt                  = {0.0};                 // XT distribution of the mean ion energy
  180. xt_distr counter_e_xt               = {0.0};                 // XT counter for electron properties
  181. xt_distr counter_i_xt               = {0.0};                 // XT counter for ion properties
  182. xt_distr ioniz_rate_xt              = {0.0};                 // XT distribution of the ionisation rate
  183.  
  184. xt_distr e1_xt                      = {0.0};                 // XT distribution of triplet excited states density - Artem
  185. xt_distr e2_xt                      = {0.0};                 // XT distribution of singlet excited states density - Artem
  186.  
  187. double   mean_energy_accu_center    = 0;                     // mean electron energy accumulator in the center of the gap
  188. Ullong   mean_energy_counter_center = 0;                     // mean electron energy counter in the center of the gap
  189. Ullong   N_e_coll                   = 0;                     // counter for electron collisions
  190. Ullong   N_i_coll                   = 0;                     // counter for ion collisions
  191. double   Time;                                               // total simulated time (from the beginning of the simulation)
  192. int      cycle,no_of_cycles,cycles_done;                     // current cycle and total cycles in the run, cycles completed
  193. int      arg1;                                               // used for reading command line arguments
  194. char     st0[80];                                            // used for reading command line arguments
  195. FILE     *datafile;                                          // used for saving data
  196. bool     measurement_mode;                                   // flag that controls measurements and data saving
  197.  
  198. //---------------------------------------------------------------------------//
  199. // C++ Mersenne Twister 19937 generator                                      //
  200. // R01(MTgen) will genarate uniform distribution over [0,1) interval         //
  201. // RMB(MTgen) will generate Maxwell-Boltzmann distribution (of gas atoms)    //
  202. //---------------------------------------------------------------------------//
  203.  
  204. std::random_device rd{};
  205. std::mt19937 MTgen(rd());
  206. std::uniform_real_distribution<> R01(0.0, 1.0);
  207. std::normal_distribution<> RMB_n(0.0,sqrt(K_BOLTZMANN * T_neutral / HE_MASS));
  208. std::normal_distribution<> RMB_e(0.0,sqrt(K_BOLTZMANN * T_electron / E_MASS));
  209.  
  210. //----------------------------------------------------------------------------//
  211. //  electron cross sections: A V Phelps & Z Lj Petrovic, PSST 8 R21 (1999)    //
  212. //----------------------------------------------------------------------------//
  213.  
  214. class CSInterpolator {
  215. public:
  216.   // load "filename" which must have two whitespace‐separated columns:
  217.   //    energy (eV)    cross_section (in m^2 or cm^2 as you prefer)
  218.   CSInterpolator(const std::string &filename) {
  219.     std::ifstream in(filename);
  220.     if (!in) throw std::runtime_error("CSInterpolator: cannot open " + filename);
  221.     double E, sigma;
  222.     std::string line;
  223.     while (std::getline(in, line)) {
  224.       std::istringstream iss(line);
  225.       if (iss >> E >> sigma) {
  226.         E_pts_.push_back(E);
  227.         sigma_pts_.push_back(sigma);
  228.       }
  229.     }
  230.     if (E_pts_.size()<2)
  231.       throw std::runtime_error("CSInterpolator: need at least two data points in " + filename);
  232.   }
  233.  
  234.   // return σ(E) by simple linear interpolation (clamped to end‐points)
  235.   double operator()(double E) const {
  236.     auto it = std::lower_bound(E_pts_.begin(), E_pts_.end(), E);
  237.     if (it == E_pts_.begin()) {
  238.         std::cerr << "Warning: E="<<E<<" below data range, clamping to "<< 0.0 <<"\n";
  239.         return 0.0;
  240.     }  
  241.     if (it == E_pts_.end()){
  242.         std::cerr << "Warning: E="<<E<<" above data range, clamping to "<< sigma_pts_.back() <<"\n";
  243.         return sigma_pts_.back();
  244.     }    
  245.     size_t idx = (it - E_pts_.begin());
  246.     double E1 = E_pts_[idx-1], E2 = E_pts_[idx];
  247.     double s1 = sigma_pts_[idx-1], s2 = sigma_pts_[idx];
  248.     // linear interp
  249.     return s1 + (s2 - s1) * (E - E1)/(E2 - E1);
  250.   }
  251.  
  252. private:
  253.   std::vector<double> E_pts_, sigma_pts_;
  254. };
  255.  
  256. void set_electron_cross_sections_ar(void){
  257.     int    i;
  258.     double en,qmel,qexc_1,qexc_2,qion;
  259.    
  260.     printf(">> eduPIC: Setting e- / He cross sections\n");
  261.  
  262.     // load your four datafiles (make sure these names match your files!)
  263.     CSInterpolator cs_ela  ("CS/He_electron_elastic.dat");   // two‐col: E σ_ela
  264.     CSInterpolator cs_exc1 ("CS/He_electron_exc1.dat");      // two‐col: E σ_exc1
  265.     CSInterpolator cs_exc2 ("CS/He_electron_exc2.dat");      // two‐col: E σ_exc2
  266.     CSInterpolator cs_ion  ("CS/He_electron_ionization.dat");// two‐col: E σ_ion
  267.  
  268.     for(int i=0; i<CS_RANGES; i++){
  269.         // your energy grid
  270.         double en = (i==0 ? DE_CS : DE_CS * i);
  271.  
  272.         // interpolate
  273.         sigma[E_ELA][i]   = cs_ela(en);
  274.         sigma[E_EXC_1][i] = cs_exc1(en);
  275.         sigma[E_EXC_2][i] = cs_exc2(en);
  276.         sigma[E_ION][i]   = cs_ion(en);
  277.  
  278.         // Superelastic for triplet (E_SUPER_1)
  279.         double en_plus_1 = en + E_EXC_TH_1;
  280.         int idx1 = en_plus_1 / DE_CS;
  281.         if (idx1 < CS_RANGES && en > 0) {
  282.             sigma[E_SUPER_1][i] = (1.0 / 3.0) * (en_plus_1 / en) * sigma[E_EXC_1][idx1];
  283.         } else {
  284.             sigma[E_SUPER_1][i] = 0.0;
  285.         }        
  286.  
  287.         // Superelastic for singlet (E_SUPER_2)
  288.         double en_plus_2 = en + E_EXC_TH_2;
  289.         int idx2 = en_plus_2 / DE_CS;
  290.         if (idx2 < CS_RANGES && en > 0) {
  291.             sigma[E_SUPER_2][i] = (1.0 / 1.0) * (en_plus_2 / en) * sigma[E_EXC_2][idx2];
  292.         } else {
  293.             sigma[E_SUPER_2][i] = 0.0;
  294.         }        
  295.     }
  296.  
  297. }
  298.  
  299. //------------------------------------------------------------------------------//
  300. //  ion cross sections: A. V. Phelps, J. Appl. Phys. 76, 747 (1994)             //
  301. //------------------------------------------------------------------------------//
  302.  
  303. void set_ion_cross_sections_ar(void){
  304.     int    i;
  305.     double e_com,e_lab,qmom,qback,qiso;
  306.    
  307.     printf(">> eduPIC: Setting He+ / He cross sections\n");
  308.     for(i=0; i<CS_RANGES; i++){
  309.         if (i == 0) {e_com = DE_CS;} else {e_com = DE_CS * i;}             // ion energy in the center of mass frame of reference
  310.         e_lab = 2.0 * e_com;                                               // ion energy in the laboratory frame of reference
  311.         qiso  = 7.63 *pow(10,-20) * pow(e_com, -0.5);
  312.         qback = 1.0 * pow(10,-19) * pow( (e_com/1000.0), -0.15 ) * pow( (1.0 + e_com/1000.0), -0.25 ) * pow( (1.0 + 5.0/e_com), -0.15 );
  313.         sigma[I_ISO][i]  = qiso;             // cross section for He+ / He isotropic part of elastic scattering
  314.         sigma[I_BACK][i] = qback;            // cross section for He+ / He backward elastic scattering
  315.     }
  316. }
  317.  
  318. //----------------------------------------------------------------------//
  319. //  calculation of total cross sections for electrons and ions          //
  320. //----------------------------------------------------------------------//
  321.  
  322. void calc_total_cross_sections(void){
  323.     int i;
  324.    
  325.     for(i=0; i<CS_RANGES; i++){
  326.         sigma_tot_e[i] = (sigma[E_ELA][i] + sigma[E_EXC_1][i] + sigma[E_EXC_2][i] + sigma[E_ION][i]) * GAS_DENSITY;   // total macroscopic cross section of electrons
  327.         sigma_tot_i[i] = (sigma[I_ISO][i] + sigma[I_BACK][i]) * GAS_DENSITY;                    // total macroscopic cross section of ions
  328.     }
  329. }
  330.  
  331. //----------------------------------------------------------------------//
  332. //  test of cross sections for electrons and ions                       //
  333. //----------------------------------------------------------------------//
  334.  
  335. void test_cross_sections(void){
  336.     FILE  * f;
  337.     int   i,j;
  338.    
  339.     f = fopen("cross_sections.dat","w");        // cross sections saved in data file: cross_sections.dat
  340.     for(i=0; i<CS_RANGES; i++){
  341.         fprintf(f,"%12.4f ",i*DE_CS);
  342.         for(j=0; j<N_CS; j++) fprintf(f,"%14e ",sigma[j][i]);
  343.         fprintf(f,"\n");
  344.     }
  345.     fclose(f);
  346. }
  347.  
  348. //---------------------------------------------------------------------//
  349. // find upper limit of collision frequencies                           //
  350. //---------------------------------------------------------------------//
  351.  
  352. double max_electron_coll_freq (void){
  353.     int i;
  354.     double e,v,nu,nu_max;
  355.     nu_max = 0;
  356.     for(i=0; i<CS_RANGES; i++){
  357.         e  = i * DE_CS;
  358.         v  = sqrt(2.0 * e * EV_TO_J / E_MASS);
  359.         nu = v * sigma_tot_e[i];
  360.         if (nu > nu_max) {nu_max = nu;}
  361.     }
  362.     return nu_max;
  363. }
  364.  
  365. double max_ion_coll_freq (void){
  366.     int i;
  367.     double e,g,nu,nu_max;
  368.     nu_max = 0;
  369.     for(i=0; i<CS_RANGES; i++){
  370.         e  = i * DE_CS;
  371.         g  = sqrt(2.0 * e * EV_TO_J / MU_HEHE);
  372.         nu = g * sigma_tot_i[i];
  373.         if (nu > nu_max) nu_max = nu;
  374.     }
  375.     return nu_max;
  376. }
  377.  
  378. //----------------------------------------------------------------------//
  379. // initialization of the simulation by placing a given number of        //
  380. // electrons and ions at random positions between the electrodes        //
  381. //----------------------------------------------------------------------//
  382.  
  383. // initialization of excited states distribtuion, assuming Maxwellian-Boltzmann balance -- Artem
  384. std::pair<int, int> init_excited_distr() {
  385.     double part_ground = 1.0*exp(-0.0/T_neutral); // partition function for ground state
  386.     double part_triplet = 3.0*exp(-E_EXC_TH_1*EV_TO_J/(K_BOLTZMANN*T_neutral)); // partition function for triplet excited state
  387.     double part_singlet = 1.0*exp(-E_EXC_TH_2*EV_TO_J/(K_BOLTZMANN*T_neutral)); // partition function for singlet excited state
  388.     double part_func_total = part_ground + part_triplet + part_singlet; // total partition function
  389.     double n_triplet = ((part_triplet/part_func_total)*GAS_DENSITY); // denisty population of tripet state
  390.     double n_singlet = ((part_singlet/part_func_total)*GAS_DENSITY); // density population of singlet state
  391.  
  392.     return {n_triplet, n_singlet};
  393. }
  394.  
  395. void print_excitation_densities(void) {
  396.     double total_e1 = 0.0, total_e2 = 0.0;
  397.     const double cell_volume = ELECTRODE_AREA * DX;
  398.    
  399.     // Sum densities across all grid cells
  400.     for (int p = 0; p < N_G; p++) {
  401.         total_e1 += e1_density[p];  // triplet state density
  402.         total_e2 += e2_density[p];  // singlet state density
  403.     }
  404.     printf("Triplet SP = %8.2e | Singlet SP = %8.2e\n", total_e1, total_e2);
  405. }
  406.  
  407. void init(int nseed){
  408.     int i;
  409.    
  410.     for (i=0; i<nseed; i++){
  411.         x_e[i]  = L * R01(MTgen);                                                   // initial random position of the electron
  412.         vx_e[i] = RMB_e(MTgen); vy_e[i] = RMB_e(MTgen); vz_e[i] = RMB_e(MTgen);     // initial velocity components of the electron
  413.         x_i[i]  = L * R01(MTgen);                                                   // initial random position of the ion
  414.         vx_i[i] = RMB_n(MTgen); vy_i[i] = RMB_n(MTgen); vz_i[i] = RMB_n(MTgen);     // initial velocity components of the ion
  415.     }
  416.     N_e = nseed;    // initial number of electrons
  417.     N_i = nseed;    // initial number of ions
  418.  
  419.     auto exc = init_excited_distr();
  420.     for (int p = 0; p < N_G; p++) {
  421.         e1_density[p] = exc.first;
  422.         e2_density[p] = exc.second;
  423.     }
  424. }
  425.  
  426. //----------------------------------------------------------------------//
  427. // e / He collision  (cold gas approximation)                           //
  428. //----------------------------------------------------------------------//
  429.  
  430. void collision_electron (double xe, double *vxe, double *vye, double *vze, int eindex){
  431.     const double F1 = E_MASS  / (E_MASS + HE_MASS);
  432.     const double F2 = HE_MASS / (E_MASS + HE_MASS);
  433.     double t0,t1,t2,t3,t4,t5,rnd;
  434.     double g,g2,gx,gy,gz,wx,wy,wz,theta,phi;
  435.     double chi,eta,chi2,eta2,sc,cc,se,ce,st,ct,sp,cp,energy,e_sc,e_ej;
  436.  
  437.     // - Artem
  438.     // Determine cell p where the electron is
  439.     double c0 = xe * INV_DX;
  440.     int p = std::max(0, std::min(N_G - 1, static_cast<int>(c0)));
  441.  
  442.     // Local densities -- Artem
  443.     double e1_dens = e1_density[p];
  444.     double e2_dens = e2_density[p];
  445.     double ground_dens = GAS_DENSITY - e1_dens - e2_dens;    
  446.  
  447.    
  448.     // calculate relative velocity before collision & velocity of the centre of mass
  449.    
  450.     gx = (*vxe);
  451.     gy = (*vye);
  452.     gz = (*vze);
  453.     g  = sqrt(gx * gx + gy * gy + gz * gz);
  454.     wx = F1 * (*vxe);
  455.     wy = F1 * (*vye);
  456.     wz = F1 * (*vze);
  457.    
  458.     // find Euler angles
  459.    
  460.     if (gx == 0) {theta = 0.5 * PI;}
  461.     else {theta = atan2(sqrt(gy * gy + gz * gz),gx);}
  462.     if (gy == 0) {
  463.         if (gz > 0){phi = 0.5 * PI;} else {phi = - 0.5 * PI;}
  464.     } else {phi = atan2(gz, gy);}
  465.     st  = sin(theta);
  466.     ct  = cos(theta);
  467.     sp  = sin(phi);
  468.     cp  = cos(phi);
  469.    
  470.     // choose the type of collision based on the cross sections
  471.     // take into account energy loss in inelastic collisions
  472.     // generate scattering and azimuth angles
  473.     // in case of ionization handle the 'new' electron
  474.    
  475.     t0   =      sigma[E_ELA][eindex] * ground_dens;
  476.     t1   = t0 + sigma[E_EXC_1][eindex] * ground_dens;
  477.     t2   = t1 + sigma[E_EXC_2][eindex] * ground_dens;
  478.     t3   = t2 + sigma[E_ION][eindex] * ground_dens;
  479.     t4   = t3 + sigma[E_SUPER_1][eindex]  * e1_dens;   // account for superelastic triplet - Artem
  480.     t5   = t4 + sigma[E_SUPER_2][eindex] * e2_dens;   // account for superelastic singlet- Artem
  481.  
  482.     rnd  = R01(MTgen);
  483.     if (rnd < (t0/t5)){                              // elastic scattering
  484.         chi = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering
  485.         eta = TWO_PI * R01(MTgen);                   // azimuthal angle
  486.  
  487.     } else if (rnd < (t1/t5)){                       // excitation 1 (triplet)
  488.         energy = 0.5 * E_MASS * g * g;               // electron energy
  489.         energy = fabs(energy - E_EXC_TH_1 * EV_TO_J);  // subtract energy loss for excitation
  490.         g   = sqrt(2.0 * energy / E_MASS);           // relative velocity after energy loss
  491.         chi = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering
  492.         eta = TWO_PI * R01(MTgen);                   // azimuthal angle
  493.  
  494.         //add new triplet excited He atom density to this grid point, sample velocities from Maxwellian distribution - Artem
  495.         //no need if we calculate rates and densities outside
  496. //        e1_density[p] += WEIGHT / (ELECTRODE_AREA * DX);
  497.  
  498.     } else if (rnd < (t2/t5)){                       // excitation 2 (singlet)
  499.         energy = 0.5 * E_MASS * g * g;               // electron energy
  500.         energy = fabs(energy - E_EXC_TH_2 * EV_TO_J);  // subtract energy loss for excitation
  501.         g   = sqrt(2.0 * energy / E_MASS);           // relative velocity after energy loss
  502.         chi = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering
  503.         eta = TWO_PI * R01(MTgen);                   // azimuthal angle    
  504.  
  505.         //add new singet excited He atom, sample velocities from Maxwellian distribution - Artem
  506.         //no need if we calculate rates and densities outside
  507. //        e2_density[p] += WEIGHT / (ELECTRODE_AREA * DX);
  508.  
  509.     } else if (rnd < (t3/t5)) {                                         // ionization
  510.         energy = 0.5 * E_MASS * g * g;               // electron energy
  511.         energy = fabs(energy - E_ION_TH * EV_TO_J);  // subtract energy loss of ionization
  512.         // e_ej  = 10.0 * tan(R01(MTgen) * atan(energy/EV_TO_J / 20.0)) * EV_TO_J; // energy of the ejected electron
  513.         // e_sc = fabs(energy - e_ej);                  // energy of scattered electron after the collision
  514.         e_ej = 0.5*energy;                          // energy of the ejected electron
  515.         e_sc = fabs(energy - e_ej);                  // energy of scattered electron after the collision        
  516.         g    = sqrt(2.0 * e_sc / E_MASS);            // relative velocity of scattered electron
  517.         g2   = sqrt(2.0 * e_ej / E_MASS);            // relative velocity of ejected electron
  518.         // chi  = acos(sqrt(e_sc / energy));            // scattering angle for scattered electron
  519.         // chi2 = acos(sqrt(e_ej / energy));            // scattering angle for ejected electrons
  520.         chi = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering for scattered electron (as in Turner's case)
  521.         chi2 = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering for ejected electrons (as in Turner's case)
  522.         eta  = TWO_PI * R01(MTgen);                  // azimuthal angle for scattered electron
  523.         eta2 = eta + PI;                             // azimuthal angle for ejected electron
  524.         sc  = sin(chi2);
  525.         cc  = cos(chi2);
  526.         se  = sin(eta2);
  527.         ce  = cos(eta2);
  528.         gx  = g2 * (ct * cc - st * sc * ce);
  529.         gy  = g2 * (st * cp * cc + ct * cp * sc * ce - sp * sc * se);
  530.         gz  = g2 * (st * sp * cc + ct * sp * sc * ce + cp * sc * se);
  531.         x_e[N_e]  = xe;                              // add new electron
  532.         vx_e[N_e] = wx + F2 * gx;
  533.         vy_e[N_e] = wy + F2 * gy;
  534.         vz_e[N_e] = wz + F2 * gz;
  535.         N_e++;
  536.         x_i[N_i]  = xe;                              // add new ion
  537.         vx_i[N_i] = RMB_n(MTgen);                      // velocity is sampled from background thermal distribution
  538.         vy_i[N_i] = RMB_n(MTgen);
  539.         vz_i[N_i] = RMB_n(MTgen);
  540.         N_i++;
  541.     }
  542.      else if (rnd < (t4/t5)) {                      // accounting for superelastic collisions - triplet - Artem
  543.         energy = 0.5 * E_MASS * g * g;               // electron energy
  544.         energy = fabs(energy + E_EXC_TH_1 * EV_TO_J);  // add energy for deexcitation
  545.         g   = sqrt(2.0 * energy / E_MASS);           // relative velocity after energy loss
  546.         chi = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering
  547.         eta = TWO_PI * R01(MTgen);                   // azimuthal angle    
  548.  
  549.         //substract  excited He atom density from the grid point - Artem
  550.         //no need if we calculate rates and densities outside
  551. //        e1_density[p] = std::max(e1_density[p] - WEIGHT / (ELECTRODE_AREA * DX), 0.0);
  552.    
  553.     } else {                                         // account for supeelastic collision - singlet - Artem
  554.         energy = 0.5 * E_MASS * g * g;               // electron energy
  555.         energy = fabs(energy + E_EXC_TH_2 * EV_TO_J);  // add energy for deexcitation
  556.         g   = sqrt(2.0 * energy / E_MASS);           // relative velocity after energy loss
  557.         chi = acos(1.0 - 2.0 * R01(MTgen));          // isotropic scattering
  558.         eta = TWO_PI * R01(MTgen);                   // azimuthal angle    
  559.  
  560.         //substract  excited He atom density from the grid point - Artem
  561.         //no need if we calculate rates and densities outside
  562.  //       e2_density[p] = std::max(e2_density[p] - WEIGHT / (ELECTRODE_AREA * DX), 0.0);
  563.     }
  564.  
  565.    
  566.     // scatter the primary electron
  567.    
  568.     sc = sin(chi);
  569.     cc = cos(chi);
  570.     se = sin(eta);
  571.     ce = cos(eta);
  572.    
  573.     // compute new relative velocity:
  574.    
  575.     gx = g * (ct * cc - st * sc * ce);
  576.     gy = g * (st * cp * cc + ct * cp * sc * ce - sp * sc * se);
  577.     gz = g * (st * sp * cc + ct * sp * sc * ce + cp * sc * se);
  578.    
  579.     // post-collision velocity of the colliding electron
  580.    
  581.     (*vxe) = wx + F2 * gx;
  582.     (*vye) = wy + F2 * gy;
  583.     (*vze) = wz + F2 * gz;
  584. }
  585.  
  586. //----------------------------------------------------------------------//
  587. // He+ / He collision                                                   //
  588. //----------------------------------------------------------------------//
  589.  
  590. void collision_ion (double *vx_1, double *vy_1, double *vz_1,
  591.                     double *vx_2, double *vy_2, double *vz_2, int e_index){
  592.     double   g,gx,gy,gz,wx,wy,wz,rnd;
  593.     double   theta,phi,chi,eta,st,ct,sp,cp,sc,cc,se,ce,t1,t2;
  594.    
  595.     // calculate relative velocity before collision
  596.     // random Maxwellian target atom already selected (vx_2,vy_2,vz_2 velocity components of target atom come with the call)
  597.    
  598.     gx = (*vx_1)-(*vx_2);
  599.     gy = (*vy_1)-(*vy_2);
  600.     gz = (*vz_1)-(*vz_2);
  601.     g  = sqrt(gx * gx + gy * gy + gz * gz);
  602.     wx = 0.5 * ((*vx_1) + (*vx_2));
  603.     wy = 0.5 * ((*vy_1) + (*vy_2));
  604.     wz = 0.5 * ((*vz_1) + (*vz_2));
  605.    
  606.     // find Euler angles
  607.    
  608.     if (gx == 0) {theta = 0.5 * PI;} else {theta = atan2(sqrt(gy * gy + gz * gz),gx);}
  609.     if (gy == 0) {
  610.         if (gz > 0){phi = 0.5 * PI;} else {phi = - 0.5 * PI;}
  611.     } else {phi = atan2(gz, gy);}
  612.    
  613.     // determine the type of collision based on cross sections and generate scattering angle
  614.    
  615.     t1  =      sigma[I_ISO][e_index];
  616.     t2  = t1 + sigma[I_BACK][e_index];
  617.     rnd = R01(MTgen);
  618.     if  (rnd < (t1 /t2)){                        // isotropic scattering
  619.         chi = acos(1.0 - 2.0 * R01(MTgen));      // scattering angle
  620.     } else {                                     // backward scattering
  621.         chi = PI;                                // scattering angle
  622.     }
  623.     eta = TWO_PI * R01(MTgen);                   // azimuthal angle
  624.     sc  = sin(chi);
  625.     cc  = cos(chi);
  626.     se  = sin(eta);
  627.     ce  = cos(eta);
  628.     st  = sin(theta);
  629.     ct  = cos(theta);
  630.     sp  = sin(phi);
  631.     cp  = cos(phi);
  632.    
  633.     // compute new relative velocity
  634.    
  635.     gx = g * (ct * cc - st * sc * ce);
  636.     gy = g * (st * cp * cc + ct * cp * sc * ce - sp * sc * se);
  637.     gz = g * (st * sp * cc + ct * sp * sc * ce + cp * sc * se);
  638.    
  639.     // post-collision velocity of the ion
  640.    
  641.     (*vx_1) = wx + 0.5 * gx;
  642.     (*vy_1) = wy + 0.5 * gy;
  643.     (*vz_1) = wz + 0.5 * gz;
  644. }
  645.  
  646. //-----------------------------------------------------------------//
  647. // solve Poisson equation (Thomas algorithm)                       //
  648. //-----------------------------------------------------------------//
  649.  
  650. void solve_Poisson (xvector rho1, double tt){
  651.     const double A =  1.0;
  652.     const double B = -2.0;
  653.     const double C =  1.0;
  654.     const double S = 1.0 / (2.0 * DX);
  655.     const double ALPHA = -DX * DX / EPSILON0;
  656.     xvector      g, w, f;
  657.     int          i;
  658.    
  659.     // apply potential to the electrodes - boundary conditions
  660.    
  661.     pot[0]     = VOLTAGE * cos(OMEGA * tt);         // potential at the powered electrode
  662.     pot[N_G-1] = 0.0;                               // potential at the grounded electrode
  663.    
  664.     // solve Poisson equation
  665.    
  666.     for(i=1; i<=N_G-2; i++) f[i] = ALPHA * rho1[i];
  667.     f[1] -= pot[0];
  668.     f[N_G-2] -= pot[N_G-1];
  669.     w[1] = C/B;
  670.     g[1] = f[1]/B;
  671.     for(i=2; i<=N_G-2; i++){
  672.         w[i] = C / (B - A * w[i-1]);
  673.         g[i] = (f[i] - A * g[i-1]) / (B - A * w[i-1]);
  674.     }
  675.     pot[N_G-2] = g[N_G-2];
  676.     for (i=N_G-3; i>0; i--) pot[i] = g[i] - w[i] * pot[i+1];            // potential at the grid points between the electrodes
  677.    
  678.     // compute electric field
  679.    
  680.     for(i=1; i<=N_G-2; i++) efield[i] = (pot[i-1] - pot[i+1]) * S;      // electric field at the grid points between the electrodes
  681.     efield[0]     = (pot[0]     - pot[1])     * INV_DX - rho1[0]     * DX / (2.0 * EPSILON0);   // powered electrode
  682.     efield[N_G-1] = (pot[N_G-2] - pot[N_G-1]) * INV_DX + rho1[N_G-1] * DX / (2.0 * EPSILON0);   // grounded electrode
  683. }
  684.  
  685. //---------------------------------------------------------------------//
  686. // simulation of one radiofrequency cycle                              //
  687. //---------------------------------------------------------------------//
  688.  
  689. void accumulate_rates() {
  690.     double v_sqr, velocity, energy, c0_temp;
  691.     int energy_index, p_temp;
  692.     const double Volume = (ELECTRODE_AREA * DX);
  693.  
  694.     for (int k=0; k<N_e; k++){                              
  695.         v_sqr = vx_e[k] * vx_e[k] + vy_e[k] * vy_e[k] + vz_e[k] * vz_e[k];
  696.         velocity = sqrt(v_sqr);
  697.         energy   = 0.5 * E_MASS * v_sqr / EV_TO_J;
  698.         energy_index = min( int(energy / DE_CS + 0.5), CS_RANGES-1);
  699.  
  700.         c0_temp = x_e[k] * INV_DX;
  701.         p_temp = std::max(0, std::min(N_G - 1, static_cast<int>(c0_temp)));
  702.    
  703.         sum_electron_density[p_temp] += WEIGHT / Volume;
  704.  
  705.         sum_rate1f[p_temp] += sigma[E_EXC_1][energy_index] * velocity * WEIGHT;
  706.         sum_rate2f[p_temp] += sigma[E_EXC_2][energy_index] * velocity * WEIGHT;
  707.  
  708.         sum_rate1b[p_temp] += sigma[E_SUPER_1][energy_index] * velocity * WEIGHT;
  709.         sum_rate2b[p_temp] += sigma[E_SUPER_2][energy_index] * velocity * WEIGHT;
  710.     }    
  711. }
  712.  
  713. // averaging the rates each RF cycle
  714. void average_rates() {
  715.     const double inv_NT = 1.0 / (N_avg * N_T); // averaging through N_avg RF periods each contains N_T cycles
  716.     const double inv_Volume = 1.0/(ELECTRODE_AREA * DX);
  717.     for (int p = 0; p < N_G; p++) {
  718.         avg_rate1f[p] = sum_rate1f[p] * inv_NT * inv_Volume;  
  719.         avg_rate1b[p] = sum_rate1b[p] * inv_NT * inv_Volume;
  720.         avg_rate2f[p] = sum_rate2f[p] * inv_NT * inv_Volume;
  721.         avg_rate2b[p] = sum_rate2b[p] * inv_NT * inv_Volume;
  722.         avg_electron_density[p] = sum_electron_density[p] * inv_NT;
  723.     }    
  724. }
  725.  
  726. void solve_steady_state(int p) {
  727.     double dn0, dn1, dn2;
  728.     double gas_dens_local;
  729.     double tol = 1.0E-5;
  730.  
  731.     bool converged;
  732.  
  733.     const int max_iter = static_cast<int>(N_avg * PERIOD / DT_E);
  734.  
  735.     gas_dens_local = GAS_DENSITY - e1_density[p] - e2_density[p];
  736.  
  737.     // Store original values for rollback
  738.     const double original_e1 = e1_density[p];
  739.     const double original_e2 = e2_density[p];
  740.     double original_gas = GAS_DENSITY - original_e1 - original_e2;    
  741.  
  742.     for (int j = 0; j < max_iter; j++) {
  743.    
  744.         dn0 = DT_E*(-(avg_rate1f[p]+avg_rate2f[p])*gas_dens_local + avg_rate1b[p]*e1_density[p] + avg_rate2b[p]*e2_density[p]);
  745.         dn1 = DT_E*(avg_rate1f[p]*gas_dens_local - avg_rate1b[p]*e1_density[p]);
  746.         dn2 = DT_E*(avg_rate2f[p]*gas_dens_local - avg_rate2b[p]*e2_density[p]);
  747.  
  748.         gas_dens_local += dn0;
  749.         gas_dens_local = std::max(gas_dens_local, 0.0);  // Prevent negative ground density
  750.  
  751.         e1_density[p] += dn1;
  752.         e1_density[p] = std::max(e1_density[p], 0.0);
  753.        
  754.         e2_density[p] += dn2;
  755.         e2_density[p] = std::max(e2_density[p], 0.0);
  756.  
  757.         if (fabs(dn0/gas_dens_local) < tol && fabs(dn1/e1_density[p]) < tol &&  fabs(dn2/e2_density[p]) < tol) {
  758.             converged = true;
  759.             break;
  760.         }
  761.     }  
  762.  
  763.     if (!converged) {
  764.         std::cerr << "Steady-state not reached for cell " << p
  765.                 << " after " << max_iter << " iterations. Using previous values.\n";
  766.             gas_dens_local = original_gas;
  767.             e1_density[p] = original_e1;
  768.             e2_density[p] = original_e2;
  769.     }    
  770. }
  771.  
  772. void update_excited_dens() {
  773.     for (int p = 0; p < N_G; p++){
  774.         solve_steady_state(p);
  775.     }
  776. }
  777.  
  778.  
  779. void do_one_cycle (void){
  780.     const double DV       = ELECTRODE_AREA * DX;
  781.     const double FACTOR_W = WEIGHT / DV;
  782.     const double FACTOR_E = DT_E / E_MASS * E_CHARGE;
  783.     const double FACTOR_I = DT_I / HE_MASS * E_CHARGE;
  784.     const double MIN_X    = 0.45 * L;                       // min. position for EEPF collection
  785.     const double MAX_X    = 0.55 * L;                       // max. position for EEPF collection
  786.     int      k, t, p, energy_index;
  787.     double   g, g_sqr, gx, gy, gz, vx_a, vy_a, vz_a, e_x, energy, nu, p_coll, v_sqr, velocity;
  788.     double   mean_v, c0, c1, c2, rate;
  789.     bool     out;
  790.     xvector  rho;
  791.     int      t_index;
  792.  
  793.     nu_avg = 0.0;
  794.    
  795.     for (t=0; t<N_T; t++){          // the RF period is divided into N_T equal time intervals (time step DT_E)
  796.         Time += DT_E;               // update of the total simulated time
  797.         t_index = t / N_BIN;        // index for XT distributions
  798.        
  799.         // step 1: compute densities at grid points
  800.        
  801.         for(p=0; p<N_G; p++) e_density[p] = 0;                             // electron density - computed in every time step
  802.         for(k=0; k<N_e; k++){
  803.  
  804.             if      (p < 0)        p = 0;
  805.             else if (p > N_G - 2)  p = N_G - 2;
  806.             c0 = x_e[k] * INV_DX;
  807.             p  = int(c0);
  808.             e_density[p]   += (p + 1 - c0) * FACTOR_W;
  809.             e_density[p+1] += (c0 - p) * FACTOR_W;
  810.         }
  811.         e_density[0]     *= 2.0; // double at the edge bcs working with half-domain (no left/right neighbour)
  812.         e_density[N_G-1] *= 2.0;
  813.         for(p=0; p<N_G; p++) cumul_e_density[p] += e_density[p];
  814.        
  815.         if ((t % N_SUB) == 0) {                                            // ion density - computed in every N_SUB-th time steps (subcycling)
  816.             for(p=0; p<N_G; p++) i_density[p] = 0;
  817.             for(k=0; k<N_i; k++){
  818.                 c0 = x_i[k] * INV_DX;
  819.                 p  = int(c0);
  820.                 i_density[p]   += (p + 1 - c0) * FACTOR_W;  
  821.                 i_density[p+1] += (c0 - p) * FACTOR_W;
  822.             }
  823.             i_density[0]     *= 2.0; // double at the edge bcs working with half-domain (no left/right neighbour)
  824.             i_density[N_G-1] *= 2.0;
  825.         }
  826.         for(p=0; p<N_G; p++) cumul_i_density[p] += i_density[p];
  827.        
  828.         // step 2: solve Poisson equation
  829.        
  830.         for(p=0; p<N_G; p++) rho[p] = E_CHARGE * (i_density[p] - e_density[p]);  // get charge density
  831.         solve_Poisson(rho,Time);                                                 // compute potential and electric field
  832.        
  833.         // steps 3 & 4: move particles according to electric field interpolated to particle positions
  834.        
  835.         for(k=0; k<N_e; k++){                       // move all electrons in every time step
  836.             c0  = x_e[k] * INV_DX;
  837.             p   = int(c0);
  838.             c1  = p + 1.0 - c0;
  839.             c2  = c0 - p;
  840.             e_x = c1 * efield[p] + c2 * efield[p+1];
  841.            
  842.             if (measurement_mode) {
  843.                
  844.                 // measurements: 'x' and 'v' are needed at the same time, i.e. old 'x' and mean 'v'
  845.                
  846.                 mean_v = vx_e[k] - 0.5 * e_x * FACTOR_E;
  847.                 counter_e_xt[p][t_index]   += c1;
  848.                 counter_e_xt[p+1][t_index] += c2;
  849.                 ue_xt[p][t_index]   += c1 * mean_v;
  850.                 ue_xt[p+1][t_index] += c2 * mean_v;
  851.                 v_sqr  = mean_v * mean_v + vy_e[k] * vy_e[k] + vz_e[k] * vz_e[k];
  852.                 energy = 0.5 * E_MASS * v_sqr / EV_TO_J;
  853.                 meanee_xt[p][t_index]   += c1 * energy;
  854.                 meanee_xt[p+1][t_index] += c2 * energy;
  855.                 energy_index = min( int(energy / DE_CS + 0.5), CS_RANGES-1);
  856.                 velocity = sqrt(v_sqr);
  857.                 double local_neut_dens = GAS_DENSITY - e1_density[p] - e2_density[p];
  858.                 rate = sigma[E_ION][energy_index] * velocity * DT_E * local_neut_dens;
  859.                 ioniz_rate_xt[p][t_index]   += c1 * rate;
  860.                 ioniz_rate_xt[p+1][t_index] += c2 * rate;
  861.  
  862.                 // measure EEPF in the center
  863.                
  864.                 if ((MIN_X < x_e[k]) && (x_e[k] < MAX_X)){
  865.                     energy_index = (int)(energy / DE_EEPF);
  866.                     if (energy_index < N_EEPF) {eepf[energy_index] += 1.0;}
  867.                     mean_energy_accu_center += energy;
  868.                     mean_energy_counter_center++;
  869.                 }
  870.             }
  871.            
  872.             // update velocity and position
  873.            
  874.             vx_e[k] -= e_x * FACTOR_E;
  875.             x_e[k]  += vx_e[k] * DT_E;
  876.         }
  877.        
  878.         if ((t % N_SUB) == 0) {                       // move all ions in every N_SUB-th time steps (subcycling)
  879.             for(k=0; k<N_i; k++){
  880.                 c0  = x_i[k] * INV_DX;
  881.                 p   = int(c0);
  882.                 c1  = p + 1 - c0;
  883.                 c2  = c0 - p;
  884.                 e_x = c1 * efield[p] + c2 * efield[p+1];
  885.                
  886.                 if (measurement_mode) {
  887.                    
  888.                     // measurements: 'x' and 'v' are needed at the same time, i.e. old 'x' and mean 'v'
  889.  
  890.                     mean_v = vx_i[k] + 0.5 * e_x * FACTOR_I;
  891.                     counter_i_xt[p][t_index]   += c1;
  892.                     counter_i_xt[p+1][t_index] += c2;
  893.                     ui_xt[p][t_index]   += c1 * mean_v;
  894.                     ui_xt[p+1][t_index] += c2 * mean_v;
  895.                     v_sqr  = mean_v * mean_v + vy_i[k] * vy_i[k] + vz_i[k] * vz_i[k];
  896.                     energy = 0.5 * HE_MASS * v_sqr / EV_TO_J;
  897.                     meanei_xt[p][t_index]   += c1 * energy;
  898.                     meanei_xt[p+1][t_index] += c2 * energy;
  899.                 }
  900.                
  901.                 // update velocity and position and accumulate absorbed energy
  902.                
  903.                 vx_i[k] += e_x * FACTOR_I;
  904.                 x_i[k]  += vx_i[k] * DT_I;
  905.             }
  906.         }
  907.        
  908.         // step 5: check boundaries
  909.        
  910.         k = 0;
  911.         while(k < N_e) {    // check boundaries for all electrons in every time step
  912.             out = false;
  913.             if (x_e[k] < 0) {N_e_abs_pow++; out = true;}    // the electron is out at the powered electrode
  914.             if (x_e[k] > L) {N_e_abs_gnd++; out = true;}    // the electron is out at the grounded electrode
  915.             if (out) {                                      // remove the electron, if out
  916.                 x_e [k] = x_e [N_e-1]; // pushing last element on a vacant place
  917.                 vx_e[k] = vx_e[N_e-1];
  918.                 vy_e[k] = vy_e[N_e-1];
  919.                 vz_e[k] = vz_e[N_e-1];
  920.                 N_e--;
  921.             } else k++;
  922.         }
  923.        
  924.         if ((t % N_SUB) == 0) {   // check boundaries for all ions in every N_SUB-th time steps (subcycling)
  925.             k = 0;
  926.             while(k < N_i) {
  927.                 out = false;
  928.                 if (x_i[k] < 0) {       // the ion is out at the powered electrode
  929.                     N_i_abs_pow++;
  930.                     out    = true;
  931.                     v_sqr  = vx_i[k] * vx_i[k] + vy_i[k] * vy_i[k] + vz_i[k] * vz_i[k];
  932.                     energy = 0.5 * HE_MASS *  v_sqr/ EV_TO_J;
  933.                     energy_index = (int)(energy / DE_IFED);
  934.                     if (energy_index < N_IFED) {ifed_pow[energy_index]++;}       // save IFED at the powered electrode
  935.                 }
  936.                 if (x_i[k] > L) {       // the ion is out at the grounded electrode
  937.                     N_i_abs_gnd++;
  938.                     out    = true;
  939.                     v_sqr  = vx_i[k] * vx_i[k] + vy_i[k] * vy_i[k] + vz_i[k] * vz_i[k];
  940.                     energy = 0.5 * HE_MASS * v_sqr / EV_TO_J;
  941.                     energy_index = (int)(energy / DE_IFED);
  942.                     if (energy_index < N_IFED) {ifed_gnd[energy_index]++;}        // save IFED at the grounded electrode
  943.                 }
  944.                 if (out) {  // delete the ion, if out
  945.                     x_i [k] = x_i [N_i-1];
  946.                     vx_i[k] = vx_i[N_i-1];
  947.                     vy_i[k] = vy_i[N_i-1];
  948.                     vz_i[k] = vz_i[N_i-1];
  949.                     N_i--;
  950.                 } else k++;
  951.             }
  952.         }
  953.        
  954.         // step 6: collisions
  955.  
  956.        
  957.        
  958.         for (k=0; k<N_e; k++){                              // checking for occurrence of a collision for all electrons in every time step
  959.             v_sqr = vx_e[k] * vx_e[k] + vy_e[k] * vy_e[k] + vz_e[k] * vz_e[k];
  960.             velocity = sqrt(v_sqr);
  961.             energy   = 0.5 * E_MASS * v_sqr / EV_TO_J;
  962.             energy_index = min( int(energy / DE_CS + 0.5), CS_RANGES-1);
  963.  
  964.             // Artem  - adding superelastic impact on total collisoinal probability//
  965.  
  966.            
  967.             int e_crdnt = std::max(0, std::min(N_G - 1, static_cast<int>(x_e[k] * INV_DX)));
  968.             double sigma_super_e = sigma[E_SUPER_1][energy_index] * e1_density[e_crdnt] + sigma[E_SUPER_2][energy_index] * e2_density[e_crdnt];
  969.             double ground_dens_local = GAS_DENSITY - e1_density[e_crdnt] - e2_density[e_crdnt];
  970.             double sigma_ground = (sigma[E_ELA][energy_index] + sigma[E_EXC_1][energy_index] +
  971.                                 sigma[E_EXC_2][energy_index] + sigma[E_ION][energy_index]) * ground_dens_local;
  972.             nu = (sigma_ground + sigma_super_e) * velocity;
  973.  
  974.             nu_avg += nu;
  975.            
  976.             p_coll = 1 - exp(- nu * DT_E);                  // collision probability for electrons
  977.             if (R01(MTgen) < p_coll) {                      // electron collision takes place
  978.                 collision_electron(x_e[k], &vx_e[k], &vy_e[k], &vz_e[k], energy_index);
  979.                 N_e_coll++;
  980.             }
  981.         }
  982.        
  983.         if ((t % N_SUB) == 0) {                             // checking for occurrence of a collision for all ions in every N_SUB-th time steps (subcycling)
  984.             for (k=0; k<N_i; k++){
  985.                 vx_a = RMB_n(MTgen);                          // pick velocity components of a random target gas atom
  986.                 vy_a = RMB_n(MTgen);
  987.                 vz_a = RMB_n(MTgen);
  988.                 gx   = vx_i[k] - vx_a;                       // compute the relative velocity of the collision partners
  989.                 gy   = vy_i[k] - vy_a;
  990.                 gz   = vz_i[k] - vz_a;
  991.                 g_sqr = gx * gx + gy * gy + gz * gz;
  992.                 g = sqrt(g_sqr);
  993.                 energy = 0.5 * MU_HEHE * g_sqr / EV_TO_J;
  994.                 energy_index = min( int(energy / DE_CS + 0.5), CS_RANGES-1);
  995.                 nu = sigma_tot_i[energy_index] * g;
  996.                 p_coll = 1 - exp(- nu * DT_I);              // collision probability for ions
  997.                 if (R01(MTgen)< p_coll) {                   // ion collision takes place
  998.                     collision_ion (&vx_i[k], &vy_i[k], &vz_i[k], &vx_a, &vy_a, &vz_a, energy_index);
  999.                     N_i_coll++;
  1000.                 }
  1001.             }
  1002.         }
  1003.  
  1004.         //step 7: accumulate rates
  1005.         accumulate_rates();
  1006.  
  1007.         if (measurement_mode) {
  1008.            
  1009.             // collect 'xt' data from the grid
  1010.            
  1011.             for (p=0; p<N_G; p++) {
  1012.                 pot_xt   [p][t_index] += pot[p];
  1013.                 efield_xt[p][t_index] += efield[p];
  1014.                 ne_xt    [p][t_index] += e_density[p];
  1015.                 ni_xt    [p][t_index] += i_density[p];
  1016.                 e1_xt    [p][t_index] += e1_density[p];  // Artem
  1017.                 e2_xt    [p][t_index] += e2_density[p];  // Artem
  1018.             }
  1019.         }
  1020.        
  1021.         if ((t % 1000) == 0) printf(" c = %8d  t = %8d  #e = %8d  #i = %8d\n", cycle,t,N_e,N_i);
  1022.     }
  1023.  
  1024.     counter++;
  1025.  
  1026.     // updating denisites each N_avg cycles: --- Artem
  1027.     if (counter%N_avg == 0) {
  1028.         // compute average rates over a cycle
  1029.         average_rates();
  1030.         // updating densities
  1031.         update_excited_dens();
  1032.         // reset accumulators
  1033.         memset(sum_rate1f, 0, sizeof(sum_rate1f));
  1034.         memset(sum_rate1b, 0, sizeof(sum_rate1b));
  1035.         memset(sum_rate2f, 0, sizeof(sum_rate2f));
  1036.         memset(sum_rate2b, 0, sizeof(sum_rate2b));
  1037.         memset(sum_electron_density, 0, sizeof(sum_electron_density));  
  1038.     }    
  1039.  
  1040.  
  1041.     //calculate nu electron average:
  1042.  
  1043.     nu_avg /= (N_T*N_e);
  1044.  
  1045.     fprintf(datafile,"%8d  %8d  %8d\n",cycle,N_e,N_i);
  1046.     print_excitation_densities();
  1047. }
  1048.  
  1049. //---------------------------------------------------------------------//
  1050. // save particle coordinates                                           //
  1051. //---------------------------------------------------------------------//
  1052.  
  1053. void save_particle_data(){
  1054.     double   d;
  1055.     FILE   * f;
  1056.     char fname[80];
  1057.    
  1058.     strcpy(fname,"picdata.bin");
  1059.     f = fopen(fname,"wb");
  1060.     fwrite(&Time,sizeof(double),1,f);
  1061.     d = (double)(cycles_done);
  1062.     fwrite(&d,sizeof(double),1,f);
  1063.     d = (double)(N_e);
  1064.     fwrite(&d,sizeof(double),1,f);
  1065.     fwrite(x_e, sizeof(double),N_e,f);
  1066.     fwrite(vx_e,sizeof(double),N_e,f);
  1067.     fwrite(vy_e,sizeof(double),N_e,f);
  1068.     fwrite(vz_e,sizeof(double),N_e,f);
  1069.     d = (double)(N_i);
  1070.     fwrite(&d,sizeof(double),1,f);
  1071.     fwrite(x_i, sizeof(double),N_i,f);
  1072.     fwrite(vx_i,sizeof(double),N_i,f);
  1073.     fwrite(vy_i,sizeof(double),N_i,f);
  1074.     fwrite(vz_i,sizeof(double),N_i,f);
  1075.  
  1076.     // saving excited state densities - Artem
  1077.  
  1078.     fwrite(e1_density, sizeof(double), N_G, f);
  1079.     fwrite(e2_density, sizeof(double), N_G, f);    
  1080.  
  1081.     fclose(f);
  1082.     printf(">> eduPIC: data saved : %d electrons %d ions, excited states densities , %d cycles completed, time is %e [s]\n",N_e,N_i,cycles_done,Time);
  1083. }
  1084.  
  1085. //---------------------------------------------------------------------//
  1086. // load particle coordinates                                           //
  1087. //---------------------------------------------------------------------//
  1088.  
  1089. void load_particle_data(){
  1090.     double   d;
  1091.     FILE   * f;
  1092.     char fname[80];
  1093.    
  1094.     strcpy(fname,"picdata.bin");    
  1095.     f = fopen(fname,"rb");
  1096.     if (f==NULL) {printf(">> eduPIC: ERROR: No particle data file found, try running initial cycle using argument '0'\n"); exit(0); }
  1097.     fread(&Time,sizeof(double),1,f);
  1098.     fread(&d,sizeof(double),1,f);
  1099.     cycles_done = int(d);
  1100.     fread(&d,sizeof(double),1,f);
  1101.     N_e = int(d);
  1102.     fread(x_e, sizeof(double),N_e,f);
  1103.     fread(vx_e,sizeof(double),N_e,f);
  1104.     fread(vy_e,sizeof(double),N_e,f);
  1105.     fread(vz_e,sizeof(double),N_e,f);
  1106.     fread(&d,sizeof(double),1,f);
  1107.     N_i = int(d);
  1108.     fread(x_i, sizeof(double),N_i,f);
  1109.     fread(vx_i,sizeof(double),N_i,f);
  1110.     fread(vy_i,sizeof(double),N_i,f);
  1111.     fread(vz_i,sizeof(double),N_i,f);
  1112.  
  1113.     // reading excited states densities -- Artem
  1114.  
  1115.     fread(e1_density, sizeof(double), N_G, f);
  1116.     fread(e2_density, sizeof(double), N_G, f);    
  1117.  
  1118.     fclose(f);
  1119.     printf(">> eduPIC: data loaded : %d electrons %d ions, excited states densities, %d cycles completed before, time is %e [s]\n",N_e,N_i,cycles_done,Time);
  1120. }
  1121.  
  1122. //---------------------------------------------------------------------//
  1123. // save density data                                                   //
  1124. //---------------------------------------------------------------------//
  1125.  
  1126. void save_density(void){
  1127.     FILE *f;
  1128.     double c;
  1129.     int m;
  1130.    
  1131.     f = fopen("density.dat","w");
  1132.     c = 1.0 / (double)(no_of_cycles) / (double)(N_T);
  1133.     for(m=0; m<N_G; m++){
  1134.         fprintf(f,"%8.5f  %12e  %12e\n",m * DX, cumul_e_density[m] * c, cumul_i_density[m] * c);
  1135.     }
  1136.     fclose(f);
  1137. }
  1138.  
  1139. // save Final excited states densities - Artem
  1140.  
  1141. void save_final_excited_densities(void) {
  1142.     FILE *f = fopen("excited_densities.dat", "w");
  1143.     for(int p=0; p<N_G; p++) {
  1144.         fprintf(f, "%8.5f %12e %12e\n", p*DX, e1_density[p], e2_density[p]);
  1145.     }
  1146.     fclose(f);
  1147. }
  1148.  
  1149. //---------------------------------------------------------------------//
  1150. // save EEPF data                                                      //
  1151. //---------------------------------------------------------------------//
  1152.  
  1153. void save_eepf(void) {
  1154.     FILE   *f;
  1155.     int    i;
  1156.     double h,energy;
  1157.    
  1158.     h = 0.0;
  1159.     for (i=0; i<N_EEPF; i++) {h += eepf[i];}
  1160.     h *= DE_EEPF;
  1161.     f = fopen("eepf.dat","w");
  1162.     for (i=0; i<N_EEPF; i++) {
  1163.         energy = (i + 0.5) * DE_EEPF;
  1164.         fprintf(f,"%e  %e\n", energy, eepf[i] / h / sqrt(energy));
  1165.     }
  1166.     fclose(f);
  1167. }
  1168.  
  1169. //---------------------------------------------------------------------//
  1170. // save IFED data                                                      //
  1171. //---------------------------------------------------------------------//
  1172.  
  1173. void save_ifed(void) {
  1174.     FILE   *f;
  1175.     int    i;
  1176.     double h_pow,h_gnd,energy;
  1177.    
  1178.     h_pow = 0.0;
  1179.     h_gnd = 0.0;
  1180.     for (i=0; i<N_IFED; i++) {h_pow += ifed_pow[i]; h_gnd += ifed_gnd[i];}
  1181.     h_pow *= DE_IFED;
  1182.     h_gnd *= DE_IFED;
  1183.     mean_i_energy_pow = 0.0;
  1184.     mean_i_energy_gnd = 0.0;
  1185.     f = fopen("ifed.dat","w");
  1186.     for (i=0; i<N_IFED; i++) {
  1187.         energy = (i + 0.5) * DE_IFED;
  1188.         fprintf(f,"%6.2f %10.6f %10.6f\n", energy, (double)(ifed_pow[i])/h_pow, (double)(ifed_gnd[i])/h_gnd);
  1189.         mean_i_energy_pow += energy * (double)(ifed_pow[i]) / h_pow;
  1190.         mean_i_energy_gnd += energy * (double)(ifed_gnd[i]) / h_gnd;
  1191.     }
  1192.     fclose(f);
  1193. }
  1194.  
  1195. //--------------------------------------------------------------------//
  1196. // save XT data                                                       //
  1197. //--------------------------------------------------------------------//
  1198.  
  1199. void save_xt_1(xt_distr distr, char *fname) {
  1200.     FILE   *f;
  1201.     int    i, j;
  1202.    
  1203.     f = fopen(fname,"w");
  1204.     for (i=0; i<N_G; i++){
  1205.         for (j=0; j<N_XT; j++){
  1206.             fprintf(f,"%e  ", distr[i][j]);
  1207.         }
  1208.         fprintf(f,"\n");
  1209.     }
  1210.     fclose(f);
  1211. }
  1212.  
  1213. void norm_all_xt(void){
  1214.     double f1, f2;
  1215.     int    i, j;
  1216.    
  1217.     // normalize all XT data
  1218.    
  1219.     f1 = (double)(N_XT) / (double)(no_of_cycles * N_T);
  1220.     f2 = WEIGHT / (ELECTRODE_AREA * DX) / (no_of_cycles * (PERIOD / (double)(N_XT)));
  1221.    
  1222.     for (i=0; i<N_G; i++){
  1223.         for (j=0; j<N_XT; j++){
  1224.             pot_xt[i][j]    *= f1;
  1225.             efield_xt[i][j] *= f1;
  1226.             ne_xt[i][j]     *= f1;
  1227.             ni_xt[i][j]     *= f1;
  1228.             e1_xt[i][j]     *= f1;   // Artem
  1229.             e2_xt[i][j]     *= f1;   // Artem
  1230.             if (counter_e_xt[i][j] > 0) {
  1231.                 ue_xt[i][j]     =  ue_xt[i][j] / counter_e_xt[i][j];
  1232.                 je_xt[i][j]     = -ue_xt[i][j] * ne_xt[i][j] * E_CHARGE;
  1233.                 meanee_xt[i][j] =  meanee_xt[i][j] / counter_e_xt[i][j];
  1234.                 ioniz_rate_xt[i][j] *= f2;
  1235.              } else {
  1236.                 ue_xt[i][j]         = 0.0;
  1237.                 je_xt[i][j]         = 0.0;
  1238.                 meanee_xt[i][j]     = 0.0;
  1239.                 ioniz_rate_xt[i][j] = 0.0;
  1240.             }
  1241.             if (counter_i_xt[i][j] > 0) {
  1242.                 ui_xt[i][j]     = ui_xt[i][j] / counter_i_xt[i][j];
  1243.                 ji_xt[i][j]     = ui_xt[i][j] * ni_xt[i][j] * E_CHARGE;
  1244.                 meanei_xt[i][j] = meanei_xt[i][j] / counter_i_xt[i][j];
  1245.             } else {
  1246.                 ui_xt[i][j]     = 0.0;
  1247.                 ji_xt[i][j]     = 0.0;
  1248.                 meanei_xt[i][j] = 0.0;
  1249.             }
  1250.             powere_xt[i][j] = je_xt[i][j] * efield_xt[i][j];
  1251.             poweri_xt[i][j] = ji_xt[i][j] * efield_xt[i][j];
  1252.         }
  1253.     }
  1254. }
  1255.  
  1256. void save_all_xt(void){
  1257.     char fname[80];
  1258.    
  1259.     strcpy(fname,"pot_xt.dat");     save_xt_1(pot_xt, fname);
  1260.     strcpy(fname,"efield_xt.dat");  save_xt_1(efield_xt, fname);
  1261.     strcpy(fname,"ne_xt.dat");      save_xt_1(ne_xt, fname);
  1262.     strcpy(fname,"ni_xt.dat");      save_xt_1(ni_xt, fname);
  1263.     strcpy(fname,"je_xt.dat");      save_xt_1(je_xt, fname);
  1264.     strcpy(fname,"ji_xt.dat");      save_xt_1(ji_xt, fname);
  1265.     strcpy(fname,"powere_xt.dat");  save_xt_1(powere_xt, fname);
  1266.     strcpy(fname,"poweri_xt.dat");  save_xt_1(poweri_xt, fname);
  1267.     strcpy(fname,"meanee_xt.dat");  save_xt_1(meanee_xt, fname);
  1268.     strcpy(fname,"meanei_xt.dat");  save_xt_1(meanei_xt, fname);
  1269.     strcpy(fname,"ioniz_xt.dat");   save_xt_1(ioniz_rate_xt, fname);
  1270.     strcpy(fname,"e1_xt.dat");      save_xt_1(e1_xt, fname);
  1271.     strcpy(fname,"e2_xt.dat");      save_xt_1(e2_xt, fname);
  1272. }
  1273.  
  1274. //---------------------------------------------------------------------//
  1275. // simulation report including stability and accuracy conditions       //
  1276. //---------------------------------------------------------------------//
  1277.  
  1278. void check_and_save_info(void){
  1279.     FILE     *f;
  1280.     double   plas_freq, meane, kT, debye_length, density, ecoll_freq, icoll_freq, sim_time, e_max, v_max, power_e, power_i, c;
  1281.     int      i,j;
  1282.     bool     conditions_OK;
  1283.    
  1284.     density    = cumul_e_density[N_G / 2] / (double)(no_of_cycles) / (double)(N_T);  // e density @ center
  1285.     plas_freq  = E_CHARGE * sqrt(density / EPSILON0 / E_MASS);                       // e plasma frequency @ center
  1286.     meane      = mean_energy_accu_center / (double)(mean_energy_counter_center);     // e mean energy @ center
  1287.     kT         = 2.0 * meane * EV_TO_J / 3.0;                                        // k T_e @ center (approximate)
  1288.     sim_time   = (double)(no_of_cycles) / FREQUENCY;                                 // simulated time
  1289.     ecoll_freq = (double)(N_e_coll) / sim_time / (double)(N_e);                      // e collision frequency
  1290.     icoll_freq = (double)(N_i_coll) / sim_time / (double)(N_i);                      // ion collision frequency
  1291.     debye_length = sqrt(EPSILON0 * kT / density) / E_CHARGE;                         // e Debye length @ center
  1292.    
  1293.     f = fopen("info.txt","w");
  1294.     fprintf(f,"########################## eduPIC simulation report ############################\n");
  1295.     fprintf(f,"Simulation parameters:\n");
  1296.     fprintf(f,"Gap distance                          = %12.3e [m]\n",  L);
  1297.     fprintf(f,"# of grid divisions                   = %12d\n",      N_G);
  1298.     fprintf(f,"Frequency                             = %12.3e [Hz]\n", FREQUENCY);
  1299.     fprintf(f,"# of time steps / period              = %12d\n",      N_T);
  1300.     fprintf(f,"# of electron / ion time steps        = %12d\n",      N_SUB);
  1301.     fprintf(f,"Voltage amplitude                     = %12.3e [V]\n",  VOLTAGE);
  1302.     fprintf(f,"Pressure (Ar)                         = %12.3e [Pa]\n", PRESSURE);
  1303.     fprintf(f,"Temperature                           = %12.3e [K]\n",  T_neutral);
  1304.     fprintf(f,"Superparticle weight                  = %12.3e\n",      WEIGHT);
  1305.     fprintf(f,"# of simulation cycles in this run    = %12d\n",      no_of_cycles);
  1306.     fprintf(f,"--------------------------------------------------------------------------------\n");
  1307.     fprintf(f,"Plasma characteristics:\n");
  1308.     fprintf(f,"Electron density @ center             = %12.3e [m^{-3}]\n", density);
  1309.     fprintf(f,"Plasma frequency @ center             = %12.3e [rad/s]\n",  plas_freq);
  1310.     fprintf(f,"Debye length @ center                 = %12.3e [m]\n",      debye_length);
  1311.     fprintf(f,"Electron collision frequency          = %12.3e [1/s]\n",    ecoll_freq);
  1312.     fprintf(f,"Ion collision frequency               = %12.3e [1/s]\n",    icoll_freq);
  1313.     fprintf(f,"--------------------------------------------------------------------------------\n");
  1314.     fprintf(f,"Stability and accuracy conditions:\n");
  1315.     conditions_OK = true;
  1316.     c = plas_freq * DT_E;
  1317.     fprintf(f,"Plasma frequency @ center * DT_E      = %12.3f (OK if less than 0.20)\n", c);
  1318.     if (c > 0.2) {conditions_OK = false;}
  1319.     c = DX / debye_length;
  1320.     fprintf(f,"DX / Debye length @ center            = %12.3f (OK if less than 1.00)\n", c);
  1321.     if (c > 1.0) {conditions_OK = false;}
  1322.     c = max_electron_coll_freq() * DT_E;
  1323.     fprintf(f,"Max. electron coll. frequency * DT_E  = %12.3f (OK if less than 0.05)\n", c);
  1324.     if (c > 0.05) {conditions_OK = false;}
  1325.     c = max_ion_coll_freq() * DT_I;
  1326.     fprintf(f,"Max. ion coll. frequency * DT_I       = %12.3f (OK if less than 0.05)\n", c);
  1327.     if (c > 0.05) {conditions_OK = false;}
  1328.     if (conditions_OK == false){
  1329.         fprintf(f,"--------------------------------------------------------------------------------\n");
  1330.         fprintf(f,"** STABILITY AND ACCURACY CONDITION(S) VIOLATED - REFINE SIMULATION SETTINGS! **\n");
  1331.         fprintf(f,"--------------------------------------------------------------------------------\n");
  1332.         fclose(f);
  1333.         printf(">> eduPIC: ERROR: STABILITY AND ACCURACY CONDITION(S) VIOLATED!\n");
  1334.         printf(">> eduPIC: for details see 'info.txt' and refine simulation settings!\n");
  1335.     }
  1336.     else
  1337.     {
  1338.         // calculate maximum energy for which the Courant-Friedrichs-Levy condition holds:
  1339.        
  1340.         v_max = DX / DT_E;
  1341.         e_max = 0.5 * E_MASS * v_max * v_max / EV_TO_J;
  1342.         fprintf(f,"Max e- energy for CFL condition       = %12.3f [eV]\n", e_max);
  1343.         fprintf(f,"Check EEPF to ensure that CFL is fulfilled for the majority of the electrons!\n");
  1344.         fprintf(f,"--------------------------------------------------------------------------------\n");
  1345.        
  1346.         // saving of the following data is done here as some of the further lines need data
  1347.         // that are computed / normalized in these functions
  1348.        
  1349.         printf(">> eduPIC: saving diagnostics data\n");
  1350.         save_density();
  1351.         save_final_excited_densities();   // Artem
  1352.         save_eepf();
  1353.         save_ifed();
  1354.         norm_all_xt();
  1355.         save_all_xt();
  1356.         fprintf(f,"Particle characteristics at the electrodes:\n");
  1357.         fprintf(f,"Ion flux at powered electrode         = %12.3e [m^{-2} s^{-1}]\n", N_i_abs_pow * WEIGHT / ELECTRODE_AREA / (no_of_cycles * PERIOD));
  1358.         fprintf(f,"Ion flux at grounded electrode        = %12.3e [m^{-2} s^{-1}]\n", N_i_abs_gnd * WEIGHT / ELECTRODE_AREA / (no_of_cycles * PERIOD));
  1359.         fprintf(f,"Mean ion energy at powered electrode  = %12.3e [eV]\n", mean_i_energy_pow);
  1360.         fprintf(f,"Mean ion energy at grounded electrode = %12.3e [eV]\n", mean_i_energy_gnd);
  1361.         fprintf(f,"Electron flux at powered electrode    = %12.3e [m^{-2} s^{-1}]\n", N_e_abs_pow * WEIGHT / ELECTRODE_AREA / (no_of_cycles * PERIOD));
  1362.         fprintf(f,"Electron flux at grounded electrode   = %12.3e [m^{-2} s^{-1}]\n", N_e_abs_gnd * WEIGHT / ELECTRODE_AREA / (no_of_cycles * PERIOD));
  1363.         fprintf(f,"--------------------------------------------------------------------------------\n");
  1364.        
  1365.         // calculate spatially and temporally averaged power absorption by the electrons and ions
  1366.        
  1367.         power_e = 0.0;
  1368.         power_i = 0.0;
  1369.         for (i=0; i<N_G; i++){
  1370.             for (j=0; j<N_XT; j++){
  1371.                 power_e += powere_xt[i][j];
  1372.                 power_i += poweri_xt[i][j];
  1373.             }
  1374.         }
  1375.         power_e /= (double)(N_XT * N_G);
  1376.         power_i /= (double)(N_XT * N_G);
  1377.         fprintf(f,"Absorbed power calculated as <j*E>:\n");
  1378.         fprintf(f,"Electron power density (average)      = %12.3e [W m^{-3}]\n", power_e);
  1379.         fprintf(f,"Ion power density (average)           = %12.3e [W m^{-3}]\n", power_i);
  1380.         fprintf(f,"Total power density(average)          = %12.3e [W m^{-3}]\n", power_e + power_i);
  1381.         fprintf(f,"--------------------------------------------------------------------------------\n");
  1382.         fclose(f);
  1383.     }
  1384. }
  1385.  
  1386. //------------------------------------------------------------------------------------------//
  1387. // main                                                                                     //
  1388. // command line arguments:                                                                  //
  1389. // [1]: number of cycles (0 for init)                                                       //
  1390. // [2]: "m" turns on data collection and saving                                             //
  1391. //------------------------------------------------------------------------------------------//
  1392.  
  1393. int main (int argc, char *argv[]){
  1394.     printf(">> eduPIC: starting...\n");
  1395.     printf(">> eduPIC: **************************************************************************\n");
  1396.     printf(">> eduPIC: Copyright (C) 2021 Z. Donko et al.\n");
  1397.     printf(">> eduPIC: This program comes with ABSOLUTELY NO WARRANTY\n");
  1398.     printf(">> eduPIC: This is free software, you are welcome to use, modify and redistribute it\n");
  1399.     printf(">> eduPIC: according to the GNU General Public License, https://www.gnu.org/licenses/\n");
  1400.     printf(">> eduPIC: **************************************************************************\n");
  1401.  
  1402.     if (argc == 1) {
  1403.         printf(">> eduPIC: error = need starting_cycle argument\n");
  1404.         return 1;
  1405.     } else {
  1406.         strcpy(st0,argv[1]);
  1407.         arg1 = atol(st0);
  1408.         if (argc > 2) {
  1409.             if (strcmp (argv[2],"m") == 0){
  1410.                 measurement_mode = true;                  // measurements will be done
  1411.             } else {
  1412.                 measurement_mode = false;
  1413.             }
  1414.         }
  1415.     }
  1416.     if (measurement_mode) {
  1417.         printf(">> eduPIC: measurement mode: on\n");
  1418.     } else {
  1419.         printf(">> eduPIC: measurement mode: off\n");
  1420.     }
  1421.     set_electron_cross_sections_ar();
  1422.     set_ion_cross_sections_ar();
  1423.     calc_total_cross_sections();
  1424.  
  1425.     auto exc = init_excited_distr();
  1426.     printf("Excited state population - superparticles!!!\n");
  1427.     printf("%d %d\n", exc.first, exc.second);
  1428.  
  1429.     printf("Sanity check \n");
  1430.     fflush(stdout);
  1431.  
  1432.     std::ofstream file0("nu_avg.dat");
  1433.  
  1434.     //test_cross_sections(); return 1;
  1435.     datafile = fopen("conv.dat","a");
  1436.     if (arg1 == 0) {
  1437.         if (FILE *file = fopen("picdata.bin", "r")) { fclose(file);
  1438.             printf(">> eduPIC: Warning: Data from previous calculation are detected.\n");
  1439.             printf("           To start a new simulation from the beginning, please delete all output files before running ./eduPIC 0\n");
  1440.             printf("           To continue the existing calculation, please specify the number of cycles to run, e.g. ./eduPIC 100\n");
  1441.             exit(0);
  1442.         }
  1443.         no_of_cycles = 1;
  1444.         cycle = 1;                                        // init cycle
  1445.         init(N_INIT);                                     // seed initial electrons & ions
  1446.         printf(">> eduPIC: running initializing cycle\n");
  1447.         Time = 0;
  1448.         do_one_cycle();
  1449.         print_excitation_densities();
  1450.         file0 << cycle << " " << nu_avg << "\n";
  1451.         cycles_done = 1;
  1452.     } else {
  1453.         no_of_cycles = arg1;                              // run number of cycles specified in command line
  1454.         load_particle_data();                            // read previous configuration from file
  1455.         printf(">> eduPIC: running %d cycle(s)\n",no_of_cycles);
  1456.         for (cycle=cycles_done+1;cycle<=cycles_done+no_of_cycles;cycle++) {
  1457.             do_one_cycle();
  1458.             file0 << cycle << " " << nu_avg << "\n";
  1459.         }
  1460.         cycles_done += no_of_cycles;
  1461.     }
  1462.     fclose(datafile);
  1463.     save_particle_data();
  1464.     if (measurement_mode) {
  1465.         check_and_save_info();
  1466.     }
  1467.     printf(">> eduPIC: simulation of %d cycle(s) is completed.\n",no_of_cycles);
  1468.     file0.close();
  1469. }
  1470.  
Add Comment
Please, Sign In to add comment