Skip to content

Commit 6768fae

Browse files
LDAmorimax3lMaxThevenet
authored
Read species distribution from OPMD - part 2 (#859)
* Added <species>.profile=external_file and .profile_file * Added description of input parameters to Docs * Changed from profile to injection option for external file * Fix typo in amrex abort message (due to copy paste) * Added the OpenPMD use amrex abort message * Minor fix - not sure how to remove EOL issue * Tried to add AddExternalFileBeam functon to PhysicalParticleContainer * Trued to fix EOL white space issue * Added read/print species name from OPMD file * Fixed OpenPMD charge and mass read * Added number of particles in species * Added nparts and converted charge/mass units to SI * Fix to nr of particles * Added q_tot parameter to determine part weight * Added macroparticle's weight * Fixed std::int typo - use only int * Update Source/Initialization/PlasmaInjector.cpp Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.H Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-Authored-By: Axel Huebl <[email protected]> * No need to include openPMD header yet * Minor Fixes * Fix EOL according to @ax3l's recommendation in #845 * Remove commented out AbortMessage Co-Authored-By: MaxThevenet <[email protected]> * Removed commented out part initialization (used only in branch for next PR) Co-Authored-By: MaxThevenet <[email protected]> * Added warning that this is WIP Co-Authored-By: MaxThevenet <[email protected]> * Changed function name to AddPlasmaFromFile * Removed AMReX warning from loop Co-Authored-By: MaxThevenet <[email protected]> * Added fix suggested in PR# 847 * Changed to mevpc2_kg * Fix EOL again * Style fix adding spaces around ` = ` Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-Authored-By: Axel Huebl <[email protected]> * Style fix added spaces in definitions and between comma Co-Authored-By: Axel Huebl <[email protected]> * Style fix Co-Authored-By: Axel Huebl <[email protected]> * Added Assert Message for #iterations and TODO const mass/charge * Fix EOL again * Update Docs/source/running_cpp/parameters.rst Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-Authored-By: Axel Huebl <[email protected]> * Corrected division by int Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-Authored-By: Axel Huebl <[email protected]> * Update Source/Particles/PhysicalParticleContainer.cpp Co-authored-by: Axel Huebl <[email protected]> Co-authored-by: MaxThevenet <[email protected]>
1 parent 8a0b7db commit 6768fae

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

Docs/source/running_cpp/parameters.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ Particle initialization
280280
symmetrize the beam in the x and y directions).
281281

282282
* ``external_file``: inject macroparticles with properties (charge, mass, position, and momentum) according to data in external file.
283-
It requires the additional argument ``<species_name>.injection_file``, which is the string corresponding to the OpenPMD file name.
283+
It requires the additional arguments ``<species_name>.injection_file`` and ``<species_name>.q_tot``, which are the string corresponding to the openPMD file name and the beam charge.
284284
When using this style, it is not necessary to add other ``<species_name>.(...)`` paramters, because they will be read directly from the file.
285285

286286
* ``<species_name>.num_particles_per_cell_each_dim`` (`3 integers in 3D and RZ, 2 integers in 2D`)

Source/Initialization/PlasmaInjector.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ PlasmaInjector::PlasmaInjector (int ispecies, const std::string& name)
229229
#ifdef WARPX_USE_OPENPMD
230230
external_file = true;
231231
pp.get("injection_file",str_injection_file);
232+
pp.get("q_tot",q_tot);
232233
#else
233-
amrex::Abort("WarpX has to be compiled with USE_OPENPMD=TRUE to be able to"
234-
" read the external openPMD file with species data");
234+
amrex::Abort("WarpX has to be compiled with USE_OPENPMD=TRUE to be able"
235+
" to read the external openPMD file with species data");
235236
#endif
236237
} else {
237238
StringParseAbortMessage("Injection style", part_pos_s);

Source/Particles/PhysicalParticleContainer.H

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ public:
206206
/** Load a particle beam from an external file
207207
*
208208
* @param[in] s_f path to an external file in openPMD markup
209+
* @param[in] q_tot total charge of the particle species to be initialized
209210
*/
210-
void AddPlasmaFromFile(const std::string s_f);
211+
void AddPlasmaFromFile(const std::string s_f, amrex::Real q_tot);
211212

212213
void CheckAndAddParticle(amrex::Real x, amrex::Real y, amrex::Real z,
213214
std::array<amrex::Real, 3> u,

Source/Particles/PhysicalParticleContainer.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,34 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m,
307307
}
308308

309309
void
310-
PhysicalParticleContainer::AddPlasmaFromFile(const std::string s_f)
310+
PhysicalParticleContainer::AddPlasmaFromFile(const std::string s_f, amrex::Real q_tot)
311311
{
312312
#ifdef WARPX_USE_OPENPMD
313313
openPMD::Series series = openPMD::Series(s_f, openPMD::AccessType::READ_ONLY);
314314
amrex::Print() << "openPMD standard version " << series.openPMD() << "\n";
315315

316+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(series.iterations.size() == 1u, "External "
317+
"file should contain only one iteration\n");
316318
openPMD::Iteration& i = series.iterations[1];
317-
amrex::Print() << "File contains " << i.particles.size() << " specie(s):" << "\n";
318-
for( auto const& ps : i.particles ) {
319-
amrex::Print() << "\t" << ps.first << "\n";
320-
}
319+
320+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(i.particles.size() == 1u, "External file "
321+
"should contain only one species\n");
322+
std::pair<std::string,openPMD::ParticleSpecies> ps = *i.particles.begin();
323+
324+
//TODO: In future PRs will add AMREX_ALWAYS_ASSERT_WITH_MESSAGE to test if mass and charge are both const
325+
amrex::Real p_m = ps.second["mass"][openPMD::RecordComponent::SCALAR].loadChunk<amrex::Real>().get()[0];
326+
amrex::Real p_q = ps.second["charge"][openPMD::RecordComponent::SCALAR].loadChunk<amrex::Real>().get()[0];
327+
int npart = ps.second["position"]["x"].getExtent()[0];
328+
series.flush();
329+
330+
mass = p_m*PhysConst::mevpc2_kg;
331+
charge = p_q*PhysConst::q_e;
332+
Real const weight = q_tot/(charge*amrex::Real(npart));
333+
334+
amrex::Print() << npart << " parts of species " << ps.first << "\nWith"
335+
<< " mass = " << mass << " and charge = " << charge << "\nTo initialize "
336+
<< npart << " macroparticles with weight " << weight << "\n";
337+
321338
amrex::Print()<<"WARNING: this is WIP, no particle has been injected!!";
322339
#endif
323340
return;
@@ -380,7 +397,8 @@ PhysicalParticleContainer::AddParticles (int lev)
380397
}
381398

382399
if (plasma_injector->external_file) {
383-
AddPlasmaFromFile(plasma_injector->str_injection_file);
400+
AddPlasmaFromFile(plasma_injector->str_injection_file,
401+
plasma_injector->q_tot);
384402
return;
385403
}
386404

Source/Utils/WarpXConst.H

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace PhysConst
3434
(45.*m_e*m_e*m_e*m_e*c*c*c*c*c); // SI value is 1.3050122.e-52
3535
static constexpr amrex::Real xi_c2 = xi * c * c; // This should be usable for single precision, though
3636
// very close to smallest number possible: smallest number = 1.2e-38, xi_c2 = 1.1e-35
37+
static constexpr amrex::Real mevpc2_kg = 1.7826619216279e-30; // to convert MeV in kg
3738
}
3839

3940
#endif

0 commit comments

Comments
 (0)