phystota

eduPIC_DRR_v0.1

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