XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
plasma.hpp
Go to the documentation of this file.
1 #ifndef PLASMA_HPP
2 #define PLASMA_HPP
3 #include "species.hpp"
4 
5 // Fortran routine:
6 extern "C" int get_spall_num_g(int isp);
7 
8 class Plasma{
9 
10  // Variables for managing the device particle memory allocation
14 
15  public:
16 
17  std::vector<Species<DeviceType>> all_species;
18 
19  // At some point remove nspecies from class Simulation to only have in class Plasma
20  int nspecies;
22 
23  // Constructors
25  : particles_d_has_owner(false),
27  nspecies(0), // Initialize with 0 species
28  n_nonadiabatic_species(0) // Initialize with 0 species
29  {}
30 
32  : particles_d_has_owner(false),
34  {
35  nlr.use_namelist("ptl_param");
36  nspecies = nlr.get<int>("ptl_nsp",1);
37  nspecies += 1; // Add electrons
39 
40  // Add each species
41  for(int isp = 0; isp<nspecies; isp++){
42  all_species.push_back(Species<DeviceType>(nlr, isp));
43  if(!all_species[isp].is_adiabatic){
45  }
46  }
47  }
48 
49  // Loop over all species
50  template<typename F>
51  inline void for_all_species(F func){
52  for(int isp = 0; isp<all_species.size(); isp++){
54  func(all_species[isp]);
55  }
56  }
57 
58  // Loop over all non-adiabatic species
59  template<typename F>
60  inline void for_all_nonadiabatic_species(F func){
61  for(int isp = 0; isp<all_species.size(); isp++){
62  if(isp>10) { // ISP ERROR
63  printf("ISP ERROR in for_all_nonadiabatic_sepcies: isp=%d",isp);
64  fflush(stdout);
65  }
66  if(!all_species[isp].is_adiabatic){
68  func(all_species[isp]);
69  }
70  }
71  }
72 
73  // Loop over electrons
74  template<typename F>
75  inline void for_electrons(F func){
76  for(int isp = 0; isp<all_species.size(); isp++){
77  if(all_species[isp].is_electron){
79  func(all_species[isp]);
80  }
81  }
82  }
83 
84  // Loop over ions
85  template<typename F>
86  inline void for_all_ions(F func){
87  for(int isp = 0; isp<all_species.size(); isp++){
88  if(!all_species[isp].is_electron){
90  func(all_species[isp]);
91  }
92  }
93  }
94 
95  // Operate on one species
96  template<typename F>
97  inline void for_one_species(int isp, F func){
99  func(all_species[isp]);
100  }
101 
102  int largest_n_ptl(bool check_backup){
103  int max_n_ptl = 0;
104 
105  // Loop over all species
107  if(species.is_electron && check_backup){
108  // Check on backup particles instead, if that's what's getting used
109  max_n_ptl = std::max(max_n_ptl,species.n_backup_particles);
110  } else {
111  // For now, need to access fortran object for n_ptl
112  max_n_ptl = std::max(max_n_ptl,get_spall_num_g(species.idx));
113  }
114  });
115 
116  return max_n_ptl;
117  }
118 
119  /* Deallocates the device particles and resets ownership tracking variables
120  * */
122  all_species[particles_d_owner].particles_d = Cabana::AoSoA<ParticleDataTypes,DeviceType,VEC_LEN>();
123  all_species[particles_d_owner].owns_particles_d = false;
124  particles_d_has_owner = false;
125  }
126 
127  private:
128 
130  if(!particles_d_has_owner){
131  // Set new owner and initialize with 0 particles
132  particles_d_owner = isp;
133 
134  all_species[particles_d_owner].particles_d = Cabana::AoSoA<ParticleDataTypes,DeviceType,VEC_LEN>("particles_d", 0);
135  all_species[particles_d_owner].owns_particles_d = true;
136  particles_d_has_owner = true;
137  }else{
138  // No-op if species is handing off to itself
139  if(particles_d_owner == isp) return;
140 
141  // Shallow copy to new owner
142  all_species[isp].particles_d = all_species[particles_d_owner].particles_d;
143 
144  // Delete original
145  all_species[particles_d_owner].particles_d = Cabana::AoSoA<ParticleDataTypes,DeviceType,VEC_LEN>();
146  all_species[particles_d_owner].owns_particles_d = false;
147 
148  // Set new owner
149  particles_d_owner = isp;
150  all_species[particles_d_owner].owns_particles_d = true;
151  }
152  }
153 
154 };
155 
156 #endif
bool is_electron
Whether this species is the electrons.
Definition: species.hpp:25
T get(const string &param, const T default_val, int val_ind=0)
Definition: NamelistReader.hpp:353
bool particles_d_has_owner
Whether a species owns the device particles allocation.
Definition: plasma.hpp:12
void deallocate_device_ptl()
Definition: plasma.hpp:121
Definition: NamelistReader.hpp:163
int idx
Index in all_species.
Definition: species.hpp:24
std::vector< Species< DeviceType > > all_species
Every particle species in the simulation.
Definition: plasma.hpp:17
void for_all_species(F func)
Definition: plasma.hpp:51
int particles_d_owner
Which species, if any, owns the device particles allocation.
Definition: plasma.hpp:13
void use_namelist(const string &namelist)
Definition: NamelistReader.hpp:322
Plasma(NLReader::NamelistReader &nlr)
Definition: plasma.hpp:31
int get_spall_num_g(int isp)
int largest_n_ptl(bool check_backup)
Definition: plasma.hpp:102
int nspecies
Number of species including electrons.
Definition: plasma.hpp:20
int n_nonadiabatic_species
Number of nonadiabatic species.
Definition: plasma.hpp:21
int n_backup_particles
Definition: species.hpp:54
void for_all_ions(F func)
Definition: plasma.hpp:86
Definition: plasma.hpp:8
bool species_share_particles_d_ownership
Whether to use the device particles sharing scheme.
Definition: plasma.hpp:11
void for_one_species(int isp, F func)
Definition: plasma.hpp:97
Plasma()
Definition: plasma.hpp:24
Definition: species.hpp:21
void transfer_particles_d_ownership(int isp)
Definition: plasma.hpp:129
void for_all_nonadiabatic_species(F func)
Definition: plasma.hpp:60
void for_electrons(F func)
Definition: plasma.hpp:75