Skip to content

Commit a491def

Browse files
MaxThevenetlucafedeli88Yin-YinjianZhao
authored
Option to specify the type of physical species (#746)
* first implementation, doesnt link * fix species type and use it in some examples * eol * typo in input file * Apply suggestions from code review Co-Authored-By: Luca Fedeli <[email protected]> Co-Authored-By: Yinjian Zhao <[email protected]> * changes suggested by review * put species functions into a namespace Co-authored-by: Luca Fedeli <[email protected]> Co-authored-by: Yinjian Zhao <[email protected]>
1 parent c8b1d21 commit a491def

File tree

9 files changed

+142
-30
lines changed

9 files changed

+142
-30
lines changed

Docs/source/running_cpp/parameters.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,17 @@ Particle initialization
210210
particles are pushed in a standard way, using the specified pusher.
211211
(see the parameter ``<species_name>.zinject_plane`` below)
212212

213-
* ``<species_name>.charge`` (`float`)
213+
* ``<species_name>.species_type`` (`string`) optional (default `unspecified`)
214+
Type of physical species, ``"electron"``, ``"positron"``, ``"photon"``, ``"hydrogen"``.
215+
Either this or both ``mass`` and ``charge`` have to be specified.
216+
217+
* ``<species_name>.charge`` (`float`) optional (default `NaN`)
214218
The charge of one `physical` particle of this species.
219+
If ``species_type`` is specified, the charge will be set to the physical value and ``charge`` is optional.
215220

216-
* ``<species_name>.mass`` (`float`)
221+
* ``<species_name>.mass`` (`float`) optional (default `NaN`)
217222
The mass of one `physical` particle of this species.
223+
If ``species_type`` is specified, the mass will be set to the physical value and ``mass`` is optional.
218224

219225
* ``<species_name>.injection_style`` (`string`)
220226
Determines how the particles will be injected in the simulation.

Examples/Physics_applications/laser_acceleration/inputs_2d

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ warpx.moving_window_v = 1.0 # units of speed of light
3939
particles.nspecies = 2 # number of species
4040
particles.species_names = electrons beam
4141

42-
electrons.charge = -q_e
43-
electrons.mass = m_e
42+
electrons.species_type = electron
4443
electrons.injection_style = "NUniformPerCell"
4544
electrons.num_particles_per_cell_each_dim = 1 1 1
4645
electrons.xmin = -20.e-6
@@ -51,8 +50,7 @@ electrons.density = 2.e23 # number of electrons per m^3
5150
electrons.momentum_distribution_type = "constant"
5251
electrons.do_continuous_injection = 1
5352

54-
beam.charge = -q_e
55-
beam.mass = m_e
53+
beam.species_type = electron
5654
beam.injection_style = "gaussian_beam"
5755
beam.x_rms = .5e-6
5856
beam.y_rms = .5e-6

Examples/Physics_applications/plasma_acceleration/inputs_2d_boost

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ particles.species_names = driver plasma_e plasma_p beam
5151
particles.use_fdtd_nci_corr = 1
5252
particles.rigid_injected_species = driver beam
5353

54-
driver.charge = -q_e
55-
driver.mass = m_e
54+
driver.species_type = electron
5655
driver.injection_style = "gaussian_beam"
5756
driver.x_rms = 2.e-6
5857
driver.y_rms = 2.e-6
@@ -74,8 +73,7 @@ driver.rigid_advance = true
7473
driver.projected = true
7574
driver.focused = false
7675

77-
plasma_e.charge = -q_e
78-
plasma_e.mass = m_e
76+
plasma_e.species_type = electron
7977
plasma_e.injection_style = "NUniformPerCell"
8078
plasma_e.zmin = 0.e-6
8179
plasma_e.zmax = 0.2
@@ -90,8 +88,7 @@ plasma_e.uy = 0.0
9088
plasma_e.uz = 0.0
9189
plasma_e.do_continuous_injection = 1
9290

93-
plasma_p.charge = q_e
94-
plasma_p.mass = m_p
91+
plasma_p.species_type = hydrogen
9592
plasma_p.injection_style = "NUniformPerCell"
9693
plasma_p.zmin = 0.e-6
9794
plasma_p.zmax = 0.2
@@ -106,8 +103,7 @@ plasma_p.uy = 0.0
106103
plasma_p.uz = 0.0
107104
plasma_p.do_continuous_injection = 1
108105

109-
beam.charge = -q_e
110-
beam.mass = m_e
106+
beam.species_type = electron
111107
beam.injection_style = "gaussian_beam"
112108
beam.x_rms = .5e-6
113109
beam.y_rms = .5e-6

Examples/Physics_applications/uniform_plasma/inputs_3d

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ amr.plot_int = 10
2525
particles.nspecies = 1
2626
particles.species_names = electrons
2727

28-
electrons.charge = -q_e
29-
electrons.mass = m_e
28+
electrons.species_type = electron
3029
electrons.injection_style = "NUniformPerCell"
3130
electrons.num_particles_per_cell_each_dim = 2 2 2
3231
electrons.profile = constant

Source/Initialization/PlasmaInjector.H

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef PLASMA_INJECTOR_H_
1010
#define PLASMA_INJECTOR_H_
1111

12+
#include "SpeciesPhysicalProperties.H"
1213
#include "InjectorPosition.H"
1314
#include "InjectorDensity.H"
1415
#include "InjectorMomentum.H"
@@ -47,6 +48,7 @@ public:
4748

4849
amrex::Real getCharge () {return charge;}
4950
amrex::Real getMass () {return mass;}
51+
PhysicalSpecies getPhysicalSpecies() const {return physical_species;}
5052

5153
bool doInjection () const noexcept { return inj_pos != NULL;}
5254

@@ -87,6 +89,8 @@ protected:
8789

8890
amrex::Real mass, charge;
8991

92+
PhysicalSpecies physical_species;
93+
9094
amrex::Real density;
9195

9296
int species_id;

Source/Initialization/PlasmaInjector.cpp

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* License: BSD-3-Clause-LBNL
99
*/
1010
#include "PlasmaInjector.H"
11-
11+
#include "SpeciesPhysicalProperties.H"
1212
#include "Utils/WarpXConst.H"
1313
#include "Utils/WarpXUtil.H"
1414
#include "WarpX.H"
@@ -95,22 +95,50 @@ PlasmaInjector::PlasmaInjector (int ispecies, const std::string& name)
9595
pp.query("density_min", density_min);
9696
pp.query("density_max", density_max);
9797

98+
std::string physical_species_s;
99+
bool species_is_specified = pp.query("species_type", physical_species_s);
100+
if (species_is_specified){
101+
physical_species = species::from_string( physical_species_s );
102+
// charge = SpeciesCharge[physical_species];
103+
charge = species::get_charge( physical_species );
104+
// mass = SpeciesMass[physical_species];
105+
mass = species::get_mass( physical_species );
106+
}
107+
98108
// parse charge and mass
99109
std::string charge_s;
100-
pp.get("charge", charge_s);
101-
std::transform(charge_s.begin(),
102-
charge_s.end(),
103-
charge_s.begin(),
104-
::tolower);
105-
charge = parseChargeString(pp, charge_s);
110+
bool charge_is_specified = pp.query("charge", charge_s);
111+
if (charge_is_specified){
112+
std::transform(charge_s.begin(),
113+
charge_s.end(),
114+
charge_s.begin(),
115+
::tolower);
116+
charge = parseChargeString(pp, charge_s);
117+
}
118+
if ( charge_is_specified && species_is_specified ){
119+
Print()<<"WARNING: Both <species>.charge and <species>species_type specified\n";
120+
Print()<<" The charge in <species>.mass overwrite the one from <species>.species_type\n";
121+
}
122+
if (!charge_is_specified && !species_is_specified){
123+
amrex::Abort("Need to specify at least one of species_type or charge");
124+
}
106125

107126
std::string mass_s;
108-
pp.get("mass", mass_s);
109-
std::transform(mass_s.begin(),
110-
mass_s.end(),
111-
mass_s.begin(),
112-
::tolower);
113-
mass = parseMassString(pp, mass_s);
127+
bool mass_is_specified = pp.query("mass", mass_s);
128+
if (mass_is_specified){
129+
std::transform(mass_s.begin(),
130+
mass_s.end(),
131+
mass_s.begin(),
132+
::tolower);
133+
mass = parseMassString(pp, mass_s);
134+
}
135+
if ( mass_is_specified && species_is_specified ){
136+
Print()<<"WARNING: Both <species>.mass and <species>species_type specified\n";
137+
Print()<<" The mass in <species>.mass overwrite the one from <species>.species_type\n";
138+
}
139+
if (!mass_is_specified && !species_is_specified){
140+
amrex::Abort("Need to specify at least one of species_type or mass");
141+
}
114142

115143
// parse injection style
116144
std::string part_pos_s;
@@ -362,4 +390,3 @@ PlasmaInjector::getInjectorMomentum ()
362390
{
363391
return inj_mom.get();
364392
}
365-

Source/Particles/PhysicalParticleContainer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ PhysicalParticleContainer::PhysicalParticleContainer (AmrCore* amr_core, int isp
4040
species_name(name)
4141
{
4242
plasma_injector.reset(new PlasmaInjector(species_id, species_name));
43+
physical_species = plasma_injector->getPhysicalSpecies();
4344
charge = plasma_injector->getCharge();
4445
mass = plasma_injector->getMass();
4546

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/* Copyright 2020 Maxence Thevenet
2+
*
3+
* This file is part of WarpX.
4+
*
5+
* License: BSD-3-Clause-LBNL
6+
*/
7+
8+
#ifndef WARPX_SPECIESPHYSICALPROPERTIES_H_
9+
#define WARPX_SPECIESPHYSICALPROPERTIES_H_
10+
11+
#include "Utils/WarpXConst.H"
12+
13+
#include <AMReX_REAL.H>
14+
#include <AMReX_AmrCore.H>
15+
16+
#include <map>
17+
#include <limits>
18+
19+
enum struct PhysicalSpecies{unspecified=0, electron, positron, photon, hydrogen};
20+
21+
namespace species
22+
{
23+
static PhysicalSpecies from_string(std::string species)
24+
{
25+
if( species=="unspecified" )
26+
return PhysicalSpecies::unspecified;
27+
if( species=="electron" )
28+
return PhysicalSpecies::electron;
29+
if( species=="positron" )
30+
return PhysicalSpecies::positron;
31+
if( species=="photon" )
32+
return PhysicalSpecies::photon;
33+
if( species=="hydrogen" )
34+
return PhysicalSpecies::hydrogen;
35+
amrex::Abort("unknown PhysicalSpecies");
36+
return PhysicalSpecies::unspecified;
37+
}
38+
39+
static amrex::Real get_charge (PhysicalSpecies ps)
40+
{
41+
switch(ps) {
42+
case PhysicalSpecies::unspecified:
43+
return std::numeric_limits<amrex::Real>::quiet_NaN();
44+
case PhysicalSpecies::electron:
45+
return -PhysConst::q_e;
46+
case PhysicalSpecies::positron:
47+
return PhysConst::q_e;
48+
case PhysicalSpecies::photon:
49+
return 0.;
50+
case PhysicalSpecies::hydrogen:
51+
return PhysConst::q_e;
52+
default:
53+
amrex::Abort("unknown PhysicalSpecies");
54+
return 0.;
55+
}
56+
}
57+
58+
static amrex::Real get_mass (PhysicalSpecies ps)
59+
{
60+
switch(ps) {
61+
case PhysicalSpecies::unspecified:
62+
return std::numeric_limits<amrex::Real>::quiet_NaN();
63+
case PhysicalSpecies::electron:
64+
return PhysConst::m_e;
65+
case PhysicalSpecies::positron:
66+
return PhysConst::m_e;
67+
case PhysicalSpecies::photon:
68+
return 0.;
69+
case PhysicalSpecies::hydrogen:
70+
return PhysConst::m_p;
71+
default:
72+
amrex::Abort("unknown PhysicalSpecies");
73+
return 0.;
74+
}
75+
}
76+
}
77+
78+
#endif // WARPX_SPECIESPHYSICALPROPERTIES_H_

Source/Particles/WarpXParticleContainer.H

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef WARPX_WarpXParticleContainer_H_
1111
#define WARPX_WarpXParticleContainer_H_
1212

13+
#include "Utils/WarpXConst.H"
14+
#include "SpeciesPhysicalProperties.H"
1315
#include "Evolve/WarpXDtType.H"
1416

1517
#include <AMReX_Particles.H>
@@ -319,6 +321,7 @@ protected:
319321

320322
amrex::Real charge;
321323
amrex::Real mass;
324+
PhysicalSpecies physical_species;
322325

323326
//! instead of depositing (current, charge) on the finest patch level, deposit to the coarsest grid
324327
bool m_deposit_on_main_grid = false;

0 commit comments

Comments
 (0)