Skip to content

Commit 9525582

Browse files
Beam read_from_file also reads spin (#1172)
Co-authored-by: Alexander Sinn <[email protected]>
1 parent df2fb20 commit 9525582

File tree

2 files changed

+88
-20
lines changed

2 files changed

+88
-20
lines changed

src/particles/beam/BeamParticleContainer.cpp

+21-14
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,29 @@ BeamParticleContainer::ReadParameters ()
9292
"Tilted beams and correlated energy spreads are only implemented for fixed weight beams");
9393
}
9494
queryWithParserAlt(pp, "initialize_on_cpu", m_initialize_on_cpu, pp_alt);
95+
queryWithParserAlt(pp, "do_spin_tracking", m_do_spin_tracking, pp_alt);
96+
if (m_do_spin_tracking) {
97+
if (m_injection_type != "from_file") {
98+
getWithParserAlt(pp, "initial_spin", m_initial_spin, pp_alt);
99+
}
100+
queryWithParserAlt(pp, "spin_anom", m_spin_anom, pp_alt);
101+
for (auto& beam_tile : m_slices) {
102+
// Use 3 real and 0 int runtime components
103+
beam_tile.define(3, 0);
104+
}
105+
getBeamInitSlice().define(3, 0);
106+
}
95107
auto& soa = getBeamInitSlice().GetStructOfArrays();
96108
soa.GetIdCPUData().setArena(
97109
m_initialize_on_cpu ? amrex::The_Pinned_Arena() : amrex::The_Arena());
98110
for (int rcomp = 0; rcomp < soa.NumRealComps(); ++rcomp) {
99-
soa.GetRealData()[rcomp].setArena(
111+
soa.GetRealData(rcomp).setArena(
100112
m_initialize_on_cpu ? amrex::The_Pinned_Arena() : amrex::The_Arena());
101113
}
102114
for (int icomp = 0; icomp < soa.NumIntComps(); ++icomp) {
103-
soa.GetIntData()[icomp].setArena(
115+
soa.GetIntData(icomp).setArena(
104116
m_initialize_on_cpu ? amrex::The_Pinned_Arena() : amrex::The_Arena());
105117
}
106-
queryWithParserAlt(pp, "do_spin_tracking", m_do_spin_tracking, pp_alt);
107-
if (m_do_spin_tracking) {
108-
getWithParserAlt(pp, "initial_spin", m_initial_spin, pp_alt);
109-
queryWithParserAlt(pp, "spin_anom", m_spin_anom, pp_alt);
110-
for (auto& beam_tile : m_slices) {
111-
// Use 3 real and 0 int runtime components
112-
beam_tile.define(3, 0);
113-
}
114-
}
115118
}
116119

117120
amrex::Real
@@ -369,6 +372,7 @@ BeamParticleContainer::initializeSlice (int slice, int which_slice) {
369372

370373
const int slice_offset = m_init_sorter.m_box_offsets_cpu[slice];
371374
const auto permutations = m_init_sorter.m_box_permutations.dataPtr();
375+
const bool do_spin_tracking = m_do_spin_tracking;
372376

373377
amrex::ParallelFor(num_particles,
374378
[=] AMREX_GPU_DEVICE (const int ip) {
@@ -380,20 +384,23 @@ BeamParticleContainer::initializeSlice (int slice, int which_slice) {
380384
ptd.rdata(BeamIdx::ux)[ip] = ptd_init.rdata(BeamIdx::ux)[idx_src];
381385
ptd.rdata(BeamIdx::uy)[ip] = ptd_init.rdata(BeamIdx::uy)[idx_src];
382386
ptd.rdata(BeamIdx::uz)[ip] = ptd_init.rdata(BeamIdx::uz)[idx_src];
383-
387+
if (do_spin_tracking) {
388+
ptd.m_runtime_rdata[0][ip] = ptd_init.m_runtime_rdata[0][idx_src];
389+
ptd.m_runtime_rdata[1][ip] = ptd_init.m_runtime_rdata[1][idx_src];
390+
ptd.m_runtime_rdata[2][ip] = ptd_init.m_runtime_rdata[2][idx_src];
391+
}
384392
ptd.idcpu(ip) = ptd_init.idcpu(idx_src);
385393
ptd.idata(BeamIdx::nsubcycles)[ip] = 0;
386394
ptd.idata(BeamIdx::mr_level)[ip] = 0;
387395
}
388396
);
389397
}
390398

391-
if (m_do_spin_tracking) {
399+
if (m_do_spin_tracking && m_injection_type != "from_file") {
392400
HIPACE_PROFILE("BeamParticleContainer::initializeSpin()");
393401
auto ptd = getBeamSlice(which_slice).getParticleTileData();
394402

395403
const amrex::RealVect initial_spin_norm = m_initial_spin / m_initial_spin.vectorLength();
396-
397404
amrex::ParallelFor(getNumParticles(which_slice),
398405
[=] AMREX_GPU_DEVICE (const int ip) {
399406
ptd.m_runtime_rdata[0][ip] = initial_spin_norm[0];

src/particles/beam/BeamParticleContainerInit.cpp

+67-6
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ namespace
4242
*/
4343
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
4444
void AddOneBeamParticle (
45-
const BeamTileInit::ParticleTileDataType& ptd, const amrex::Real& x,
46-
const amrex::Real& y, const amrex::Real& z, const amrex::Real& ux, const amrex::Real& uy,
47-
const amrex::Real& uz, const amrex::Real& weight, const amrex::Long pid,
48-
const amrex::Long ip, const amrex::Real& speed_of_light, const EnforceBC& enforceBC) noexcept
45+
const BeamTileInit::ParticleTileDataType& ptd,
46+
const amrex::Real& x, const amrex::Real& y, const amrex::Real& z,
47+
const amrex::Real& ux, const amrex::Real& uy, const amrex::Real& uz,
48+
const amrex::Real& sx, const amrex::Real& sy, const amrex::Real& sz,
49+
const amrex::Real& weight, const amrex::Long pid, const amrex::Long ip,
50+
const amrex::Real& speed_of_light, const EnforceBC& enforceBC, bool do_spin) noexcept
4951
{
5052
amrex::Real xp = x;
5153
amrex::Real yp = y;
@@ -59,6 +61,11 @@ namespace
5961
ptd.rdata(BeamIdx::ux )[ip] = uxp;
6062
ptd.rdata(BeamIdx::uy )[ip] = uyp;
6163
ptd.rdata(BeamIdx::uz )[ip] = uz * speed_of_light;
64+
if (do_spin) {
65+
ptd.m_runtime_rdata[0][ip] = sx;
66+
ptd.m_runtime_rdata[1][ip] = sy;
67+
ptd.m_runtime_rdata[2][ip] = sz;
68+
}
6269
ptd.rdata(BeamIdx::w )[ip] = std::abs(weight);
6370

6471
ptd.idcpu(ip) = pid + ip;
@@ -790,6 +797,7 @@ InitBeamFromFile (const std::string input_file,
790797
std::string name_particle ="";
791798
std::string name_r ="", name_rx ="", name_ry ="", name_rz ="";
792799
std::string name_u ="", name_ux ="", name_uy ="", name_uz ="";
800+
std::string name_s ="", name_sx ="", name_sy ="", name_sz ="";
793801
std::string name_m ="", name_mm ="";
794802
std::string name_q ="", name_qq ="";
795803
std::string name_g ="", name_gg ="";
@@ -873,6 +881,24 @@ InitBeamFromFile (const std::string input_file,
873881
}
874882
}
875883
}
884+
else if(units == std::array<double,7> {2., 1., -1., 0., 0., 0., 0.} &&
885+
m_do_spin_tracking) {
886+
// spin
887+
if(physical_quantity.first == "spin") {
888+
name_s = physical_quantity.first;
889+
for( auto const& axes_direction : physical_quantity.second ) {
890+
if(axes_direction.first == "x" || axes_direction.first == "X") {
891+
name_sx = axes_direction.first;
892+
}
893+
if(axes_direction.first == "y" || axes_direction.first == "Y") {
894+
name_sy = axes_direction.first;
895+
}
896+
if(axes_direction.first == "z" || axes_direction.first == "Z") {
897+
name_sz = axes_direction.first;
898+
}
899+
}
900+
}
901+
}
876902
}
877903
}
878904
}
@@ -953,15 +979,28 @@ InitBeamFromFile (const std::string input_file,
953979
}
954980
}
955981

982+
if (m_do_spin_tracking) {
983+
for(std::string name_s_c : {name_sx, name_sy, name_sz}) {
984+
if(!series.iterations[num_iteration].particles[name_particle][name_s].contains(name_s_c)) {
985+
amrex::Abort("Beam input file does not contain " + name_s_c + " coordinate in " +
986+
name_s + " (spin). An attempt to read these was done because " +
987+
"do_spin_tracking is on for at least one beam.\n");
988+
}
989+
}
990+
}
956991
// print the names of the arrays in the file that are used for the simulation
957992
if(Hipace::m_verbose >= 3){
958993
amrex::Print() << "Beam Input File '" << input_file << "' in Iteration '" << num_iteration
959994
<< "' and Paricle '" << name_particle
960-
<< "' imported with:\nPositon '" << name_r << "' (coordinates '" << name_rx << "', '"
995+
<< "' imported with:\nPosition '" << name_r << "' (coordinates '" << name_rx << "', '"
961996
<< name_ry << "', '" << name_rz << "')\n"
962997
<< momentum_type << " '" << name_u << "' (coordinates '" << name_ux
963998
<< "', '" << name_uy << "', '" << name_uz << "')\n"
964999
<< weighting_type << " '" << name_w << "' (in '" << name_ww << "')\n";
1000+
if (m_do_spin_tracking) {
1001+
amrex::Print() << name_u << "' (coordinates '" << name_ux
1002+
<< "', '" << name_uy << "', '" << name_uz << "')\n";
1003+
}
9651004
}
9661005

9671006
auto electrons = series.iterations[num_iteration].particles[name_particle];
@@ -987,6 +1026,15 @@ InitBeamFromFile (const std::string input_file,
9871026
amrex::The_Pinned_Arena()->alloc(sizeof(input_type)*num_to_add) ), del};
9881027
std::shared_ptr<input_type> u_z_data{ reinterpret_cast<input_type*>(
9891028
amrex::The_Pinned_Arena()->alloc(sizeof(input_type)*num_to_add) ), del};
1029+
std::shared_ptr<input_type> s_x_data{ reinterpret_cast<input_type*>(
1030+
amrex::The_Pinned_Arena()->alloc(m_do_spin_tracking ?
1031+
sizeof(input_type)*num_to_add : 0) ), del};
1032+
std::shared_ptr<input_type> s_y_data{ reinterpret_cast<input_type*>(
1033+
amrex::The_Pinned_Arena()->alloc(m_do_spin_tracking ?
1034+
sizeof(input_type)*num_to_add : 0) ), del};
1035+
std::shared_ptr<input_type> s_z_data{ reinterpret_cast<input_type*>(
1036+
amrex::The_Pinned_Arena()->alloc(m_do_spin_tracking ?
1037+
sizeof(input_type)*num_to_add: 0 ) ), del};
9901038
std::shared_ptr<input_type> w_w_data{ reinterpret_cast<input_type*>(
9911039
amrex::The_Pinned_Arena()->alloc(sizeof(input_type)*num_to_add) ), del};
9921040

@@ -997,6 +1045,11 @@ InitBeamFromFile (const std::string input_file,
9971045
electrons[name_u][name_uy].loadChunk<input_type>(u_y_data, {0u}, {num_to_add});
9981046
electrons[name_u][name_uz].loadChunk<input_type>(u_z_data, {0u}, {num_to_add});
9991047
electrons[name_w][name_ww].loadChunk<input_type>(w_w_data, {0u}, {num_to_add});
1048+
if (m_do_spin_tracking) {
1049+
electrons[name_s][name_sx].loadChunk<input_type>(s_x_data, {0u}, {num_to_add});
1050+
electrons[name_s][name_sy].loadChunk<input_type>(s_y_data, {0u}, {num_to_add});
1051+
electrons[name_s][name_sz].loadChunk<input_type>(s_z_data, {0u}, {num_to_add});
1052+
}
10001053

10011054
series.flush();
10021055

@@ -1052,6 +1105,7 @@ InitBeamFromFile (const std::string input_file,
10521105
auto old_size = particle_tile.size();
10531106
auto new_size = old_size + num_to_add;
10541107
particle_tile.resize(new_size);
1108+
10551109
const auto ptd = particle_tile.getParticleTileData();
10561110
const auto enforceBC = EnforceBC();
10571111

@@ -1064,7 +1118,11 @@ InitBeamFromFile (const std::string input_file,
10641118
const input_type * const u_x_ptr = u_x_data.get();
10651119
const input_type * const u_y_ptr = u_y_data.get();
10661120
const input_type * const u_z_ptr = u_z_data.get();
1121+
const input_type * const s_x_ptr = m_do_spin_tracking ? s_x_data.get() : nullptr;
1122+
const input_type * const s_y_ptr = m_do_spin_tracking ? s_y_data.get() : nullptr;
1123+
const input_type * const s_z_ptr = m_do_spin_tracking ? s_z_data.get() : nullptr;
10671124
const input_type * const w_w_ptr = w_w_data.get();
1125+
const bool do_spin_tracking = m_do_spin_tracking;
10681126

10691127
amrex::ParallelFor(amrex::Long(num_to_add),
10701128
[=] AMREX_GPU_DEVICE (const amrex::Long i) {
@@ -1075,8 +1133,11 @@ InitBeamFromFile (const std::string input_file,
10751133
static_cast<amrex::Real>(u_x_ptr[i] * unit_ux), // = gamma * beta
10761134
static_cast<amrex::Real>(u_y_ptr[i] * unit_uy),
10771135
static_cast<amrex::Real>(u_z_ptr[i] * unit_uz),
1136+
do_spin_tracking ? static_cast<amrex::Real>(s_x_ptr[i]) : 0.,
1137+
do_spin_tracking ? static_cast<amrex::Real>(s_y_ptr[i]) : 0.,
1138+
do_spin_tracking ? static_cast<amrex::Real>(s_z_ptr[i]) : 0.,
10781139
static_cast<amrex::Real>(w_w_ptr[i] * unit_ww),
1079-
pid, i, phys_const.c, enforceBC);
1140+
pid, i, phys_const.c, enforceBC, do_spin_tracking);
10801141
});
10811142

10821143
amrex::Gpu::streamSynchronize();

0 commit comments

Comments
 (0)