phystota

eduPIC_DRR_v1.0 - works!

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