phystota

eduPIC_DRR_v1.1 - Betnz' parameters

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