Skip to content

Commit d522b81

Browse files
authored
PlasmaInjector.H: Hide openPMD.hpp (#6016)
Avoid the `openPMD/openPMD.hpp` include in `PlasmaInjector.H`, because the latter file is included in tons of translation units, and the former is an "expensive" (~1.5sec) include, in terms of compile-time #5984. Use a PIMPL-like idom with `std::any` to still store the series as a member variable, but only pull in the type in the two specific translation units, `AddParticles.cpp` and `PlasmaInjector.cpp` that access the member. All other openPMD includes look good and TU local.
1 parent 68a8e1e commit d522b81

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

Source/Initialization/PlasmaInjector.H

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525

2626
#include <AMReX_BaseFwd.H>
2727

28-
#ifdef WARPX_USE_OPENPMD
29-
# include <openPMD/openPMD.hpp>
30-
#endif
31-
28+
#include <any>
3229
#include <limits>
3330
#include <memory>
3431
#include <string>
@@ -119,7 +116,7 @@ public:
119116
amrex::Real z_shift = 0.0; //! additional z offset for particle positions
120117
#ifdef WARPX_USE_OPENPMD
121118
//! openPMD::Series to load from in external_file injection
122-
std::unique_ptr<openPMD::Series> m_openpmd_input_series;
119+
std::any m_openpmd_input_series;
123120
#endif
124121

125122
amrex::Real surface_flux_pos; // surface location

Source/Initialization/PlasmaInjector.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <AMReX_RandomEngine.H>
3636
#include <AMReX_REAL.H>
3737

38+
#ifdef WARPX_USE_OPENPMD
39+
# include <openPMD/openPMD.hpp>
40+
#endif
41+
3842
#include <algorithm>
3943
#include <cctype>
4044
#include <map>
@@ -469,13 +473,13 @@ void PlasmaInjector::setupExternalFile (amrex::ParmParse const& pp_species)
469473
const bool species_is_specified = pp_species.contains("species_type");
470474

471475
if (amrex::ParallelDescriptor::IOProcessor()) {
472-
m_openpmd_input_series = std::make_unique<openPMD::Series>(
476+
auto series = openPMD::Series(
473477
str_injection_file, openPMD::Access::READ_ONLY);
474478

475479
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
476-
m_openpmd_input_series->iterations.size() == 1u,
480+
series.iterations.size() == 1u,
477481
"External file should contain only 1 iteration\n");
478-
openPMD::Iteration it = m_openpmd_input_series->iterations.begin()->second;
482+
openPMD::Iteration it = series.iterations.begin()->second;
479483
WARPX_ALWAYS_ASSERT_WITH_MESSAGE(
480484
it.particles.size() == 1u,
481485
"External file should contain only 1 species\n");
@@ -502,7 +506,7 @@ void PlasmaInjector::setupExternalFile (amrex::ParmParse const& pp_species)
502506
// TODO: Add ASSERT_WITH_MESSAGE to test if charge is a constant record
503507
auto p_q_ptr =
504508
ps["charge"][openPMD::RecordComponent::SCALAR].loadChunk<amrex::ParticleReal>();
505-
m_openpmd_input_series->flush();
509+
series.flush();
506510
amrex::ParticleReal const p_q = p_q_ptr.get()[0];
507511
auto const charge_unit = static_cast<amrex::Real>(ps["charge"][openPMD::RecordComponent::SCALAR].unitSI());
508512
charge = p_q * charge_unit;
@@ -525,12 +529,13 @@ void PlasmaInjector::setupExternalFile (amrex::ParmParse const& pp_species)
525529
// TODO: Add ASSERT_WITH_MESSAGE to test if mass is a constant record
526530
auto p_m_ptr =
527531
ps["mass"][openPMD::RecordComponent::SCALAR].loadChunk<amrex::ParticleReal>();
528-
m_openpmd_input_series->flush();
532+
series.flush();
529533
amrex::ParticleReal const p_m = p_m_ptr.get()[0];
530534
auto const mass_unit = static_cast<amrex::Real>(ps["mass"][openPMD::RecordComponent::SCALAR].unitSI());
531535
mass = p_m * mass_unit;
532536
}
533537
}
538+
m_openpmd_input_series = series;
534539
} // IOProcessor
535540

536541
// Broadcast charge and mass to non-IO processors if read in from the file

Source/Particles/MultiParticleContainer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ void MultiParticleContainer::InitQuantumSync ()
11551155
// qs_minimum_chi_part is the minimum chi parameter to be
11561156
// considered for Synchrotron emission. If a lepton has chi < chi_min,
11571157
// the optical depth is not evolved and photon generation is ignored
1158-
amrex::Real qs_minimum_chi_part;
1158+
amrex::Real qs_minimum_chi_part = 0;
11591159
utils::parser::getWithParser(pp_qed_qs, "chi_min", qs_minimum_chi_part);
11601160

11611161

@@ -1277,7 +1277,7 @@ MultiParticleContainer::QuantumSyncGenerateTable ()
12771277
// qs_minimum_chi_part is the minimum chi parameter to be
12781278
// considered for Synchrotron emission. If a lepton has chi < chi_min,
12791279
// the optical depth is not evolved and photon generation is ignored
1280-
amrex::Real qs_minimum_chi_part;
1280+
amrex::Real qs_minimum_chi_part = 0;
12811281
utils::parser::getWithParser(pp_qed_qs, "chi_min", qs_minimum_chi_part);
12821282

12831283
if(ParallelDescriptor::IOProcessor()){
@@ -1367,7 +1367,7 @@ MultiParticleContainer::BreitWheelerGenerateTable ()
13671367
// bw_minimum_chi_phot is the minimum chi parameter to be
13681368
// considered for pair production. If a photon has chi < chi_min,
13691369
// the optical depth is not evolved and photon generation is ignored
1370-
amrex::Real bw_minimum_chi_part;
1370+
amrex::Real bw_minimum_chi_part = 0;
13711371
utils::parser::getWithParser(pp_qed_bw, "chi_min", bw_minimum_chi_part);
13721372

13731373
if(ParallelDescriptor::IOProcessor()){

Source/Particles/ParticleCreation/AddParticles.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
# include <openPMD/openPMD.hpp>
7777
#endif
7878

79+
#include <any>
7980
#include <algorithm>
8081
#include <array>
8182
#include <cmath>
@@ -564,10 +565,10 @@ PhysicalParticleContainer::AddPlasmaFromFile(PlasmaInjector & plasma_injector,
564565
//TODO: Make changes for read/write in multiple MPI ranks
565566
if (ParallelDescriptor::IOProcessor()) {
566567
// take ownership of the series and close it when done
567-
auto series = std::move(plasma_injector.m_openpmd_input_series);
568+
auto series = std::any_cast<openPMD::Series>(std::move(plasma_injector.m_openpmd_input_series));
568569

569570
// assumption asserts: see PlasmaInjector
570-
openPMD::Iteration it = series->iterations.begin()->second;
571+
openPMD::Iteration it = series.iterations.begin()->second;
571572
const ParmParse pp_species_name(species_name);
572573
pp_species_name.query("impose_t_lab_from_file", impose_t_lab_from_file);
573574
double t_lab = 0._prt;
@@ -610,7 +611,7 @@ PhysicalParticleContainer::AddPlasmaFromFile(PlasmaInjector & plasma_injector,
610611
ptr_uy = ps["momentum"]["y"].loadChunk<ParticleReal>();
611612
momentum_unit_y = static_cast<ParticleReal>(ps["momentum"]["y"].unitSI());
612613
}
613-
series->flush(); // shared_ptr data can be read now
614+
series.flush(); // shared_ptr data can be read now
614615

615616
if (q_tot != 0.0) {
616617
std::stringstream warnMsg;

0 commit comments

Comments
 (0)