From 46b9fde64b935703e9bda18f6150a165c14d4032 Mon Sep 17 00:00:00 2001 From: MaxThevenet Date: Fri, 24 May 2019 16:19:14 -0700 Subject: option do symmetrize gaussian particle beam --- Source/Particles/PhysicalParticleContainer.cpp | 91 +++++++++++++++++--------- 1 file changed, 60 insertions(+), 31 deletions(-) (limited to 'Source/Particles/PhysicalParticleContainer.cpp') diff --git a/Source/Particles/PhysicalParticleContainer.cpp b/Source/Particles/PhysicalParticleContainer.cpp index 212084e64..38ea55d6e 100644 --- a/Source/Particles/PhysicalParticleContainer.cpp +++ b/Source/Particles/PhysicalParticleContainer.cpp @@ -181,7 +181,8 @@ void PhysicalParticleContainer::MapParticletoBoostedFrame(Real& x, Real& y, Real void PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, Real x_rms, Real y_rms, Real z_rms, - Real q_tot, long npart) { + Real q_tot, long npart, + int do_symmetrize) { const Geometry& geom = m_gdb->Geom(0); RealBox containing_bx = geom.ProbDomain(); @@ -191,13 +192,15 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, std::normal_distribution disty(y_m, y_rms); std::normal_distribution distz(z_m, z_rms); - std::array attribs; - attribs.fill(0.0); - if (ParallelDescriptor::IOProcessor()) { - std::array u; - Real weight; - for (long i = 0; i < npart; ++i) { + std::array u; + Real weight; + // If do_symmetrize, create 4x fewer particles, and + // Replicate each particle 4 times (x,y) (-x,y) (x,-y) (-x,-y) + if (do_symmetrize){ + npart /= 4; + } + for (long i = 0; i < npart; ++i) { #if ( AMREX_SPACEDIM == 3 | WARPX_RZ) weight = q_tot/npart/charge; Real x = distx(mt); @@ -209,35 +212,60 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, Real y = 0.; Real z = distz(mt); #endif - if (plasma_injector->insideBounds(x, y, z)) { - plasma_injector->getMomentum(u, x, y, z); - if (WarpX::gamma_boost > 1.) { - MapParticletoBoostedFrame(x, y, z, u); - } - attribs[PIdx::ux] = u[0]; - attribs[PIdx::uy] = u[1]; - attribs[PIdx::uz] = u[2]; - attribs[PIdx::w ] = weight; - - if (WarpX::do_boosted_frame_diagnostic && do_boosted_frame_diags) - { - auto& particle_tile = DefineAndReturnParticleTile(0, 0, 0); - particle_tile.push_back_real(particle_comps["xold"], x); - particle_tile.push_back_real(particle_comps["yold"], y); - particle_tile.push_back_real(particle_comps["zold"], z); - - particle_tile.push_back_real(particle_comps["uxold"], u[0]); - particle_tile.push_back_real(particle_comps["uyold"], u[1]); - particle_tile.push_back_real(particle_comps["uzold"], u[2]); - } - - AddOneParticle(0, 0, 0, x, y, z, attribs); + if (plasma_injector->insideBounds(x, y, z)) { + plasma_injector->getMomentum(u, x, y, z); + if (do_symmetrize){ + // Add four particles to the beam: + // (x,ux,y,uy) (-x,-ux,y,uy) (x,ux,-y,-uy) (-x,-ux,-y,-uy) + std::array u_tmp = u; + for (int ix=0; ix<2; ix++){ + for (int iy=0; iy<2; iy++){ + x *= std::pow(-1,ix); + u_tmp[0] *= std::pow(-1,ix); + y *= std::pow(-1,iy); + u_tmp[1] *= std::pow(-1,iy); + CheckAndAddParticle(x, y, z, u_tmp, weight); + } + } + } else { + CheckAndAddParticle(x, y, z, u, weight); + } } } } Redistribute(); } +void +PhysicalParticleContainer::CheckAndAddParticle(Real x, Real y, Real z, + std::array u, + Real weight) +{ + std::array attribs; + attribs.fill(0.0); + + if (WarpX::gamma_boost > 1.) { + MapParticletoBoostedFrame(x, y, z, u); + } + attribs[PIdx::ux] = u[0]; + attribs[PIdx::uy] = u[1]; + attribs[PIdx::uz] = u[2]; + attribs[PIdx::w ] = weight; + + if (WarpX::do_boosted_frame_diagnostic && do_boosted_frame_diags) + { + auto& particle_tile = DefineAndReturnParticleTile(0, 0, 0); + particle_tile.push_back_real(particle_comps["xold"], x); + particle_tile.push_back_real(particle_comps["yold"], y); + particle_tile.push_back_real(particle_comps["zold"], z); + + particle_tile.push_back_real(particle_comps["uxold"], u[0]); + particle_tile.push_back_real(particle_comps["uyold"], u[1]); + particle_tile.push_back_real(particle_comps["uzold"], u[2]); + } + AddOneParticle(0, 0, 0, x, y, z, attribs); +} + void PhysicalParticleContainer::AddParticles (int lev) { @@ -263,7 +291,8 @@ PhysicalParticleContainer::AddParticles (int lev) plasma_injector->y_rms, plasma_injector->z_rms, plasma_injector->q_tot, - plasma_injector->npart); + plasma_injector->npart, + plasma_injector->do_symmetrize); return; -- cgit v1.2.3 From 5473027f14805cae5f00f21a8eef2ca988d9f8ab Mon Sep 17 00:00:00 2001 From: MaxThevenet Date: Fri, 24 May 2019 16:27:09 -0700 Subject: add doc --- Docs/source/running_cpp/parameters.rst | 9 +++++++++ Source/Particles/PhysicalParticleContainer.cpp | 3 +++ 2 files changed, 12 insertions(+) (limited to 'Source/Particles/PhysicalParticleContainer.cpp') diff --git a/Docs/source/running_cpp/parameters.rst b/Docs/source/running_cpp/parameters.rst index ac378dc03..2ab071673 100644 --- a/Docs/source/running_cpp/parameters.rst +++ b/Docs/source/running_cpp/parameters.rst @@ -187,6 +187,15 @@ Particle initialization * ``NRandomPerCell``: injection with a fixed number of randomly-distributed particles per cell. This requires the additional parameter ``.num_particles_per_cell``. + * ``gaussian_beam``: Inject particle beam with gaussian distribution in + space in all directions. This requires additional parameters: + ``.q_tot`` (beam charge), + ``.npart`` (number of particles in the beam), + ``.x/y/z_m`` (average position in `x/y/z`), + ``.x/y/z_rms`` (standard deviation in `x/y/z`), + and optional argument ``.do_symmetrize`` (whether to + symmetrize the beam in the x and y directions). + * ``.do_continuous_injection`` (`0` or `1`) Whether to inject particles during the simulation, and not only at initialization. This can be required whith a moving window and/or when diff --git a/Source/Particles/PhysicalParticleContainer.cpp b/Source/Particles/PhysicalParticleContainer.cpp index 38ea55d6e..bf93375fe 100644 --- a/Source/Particles/PhysicalParticleContainer.cpp +++ b/Source/Particles/PhysicalParticleContainer.cpp @@ -244,6 +244,7 @@ PhysicalParticleContainer::CheckAndAddParticle(Real x, Real y, Real z, std::array attribs; attribs.fill(0.0); + // update attribs with input arguments if (WarpX::gamma_boost > 1.) { MapParticletoBoostedFrame(x, y, z, u); } @@ -254,6 +255,7 @@ PhysicalParticleContainer::CheckAndAddParticle(Real x, Real y, Real z, if (WarpX::do_boosted_frame_diagnostic && do_boosted_frame_diags) { + // need to create old values auto& particle_tile = DefineAndReturnParticleTile(0, 0, 0); particle_tile.push_back_real(particle_comps["xold"], x); particle_tile.push_back_real(particle_comps["yold"], y); @@ -263,6 +265,7 @@ PhysicalParticleContainer::CheckAndAddParticle(Real x, Real y, Real z, particle_tile.push_back_real(particle_comps["uyold"], u[1]); particle_tile.push_back_real(particle_comps["uzold"], u[2]); } + // add particle AddOneParticle(0, 0, 0, x, y, z, attribs); } -- cgit v1.2.3 From b7dbfc6b22fb3d94310cc5f5e4b2f34fccfe49ce Mon Sep 17 00:00:00 2001 From: MaxThevenet Date: Mon, 27 May 2019 07:33:26 -0700 Subject: typo --- Source/Particles/PhysicalParticleContainer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Particles/PhysicalParticleContainer.cpp') diff --git a/Source/Particles/PhysicalParticleContainer.cpp b/Source/Particles/PhysicalParticleContainer.cpp index bf93375fe..c762bdbb3 100644 --- a/Source/Particles/PhysicalParticleContainer.cpp +++ b/Source/Particles/PhysicalParticleContainer.cpp @@ -224,7 +224,7 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, u_tmp[0] *= std::pow(-1,ix); y *= std::pow(-1,iy); u_tmp[1] *= std::pow(-1,iy); - CheckAndAddParticle(x, y, z, u_tmp, weight); + CheckAndAddParticle(x, y, z, u_tmp, weight/4); } } } else { -- cgit v1.2.3 From 2ddfcb10aeb31b496153658269147e203a362657 Mon Sep 17 00:00:00 2001 From: MaxThevenet Date: Tue, 28 May 2019 17:02:14 -0700 Subject: fix issue for symmetric beam --- Source/Particles/PhysicalParticleContainer.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'Source/Particles/PhysicalParticleContainer.cpp') diff --git a/Source/Particles/PhysicalParticleContainer.cpp b/Source/Particles/PhysicalParticleContainer.cpp index c762bdbb3..2c501985f 100644 --- a/Source/Particles/PhysicalParticleContainer.cpp +++ b/Source/Particles/PhysicalParticleContainer.cpp @@ -215,14 +215,16 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, if (plasma_injector->insideBounds(x, y, z)) { plasma_injector->getMomentum(u, x, y, z); if (do_symmetrize){ + std::array u_tmp; + Real x_tmp, y_tmp, z_tmp; // Add four particles to the beam: // (x,ux,y,uy) (-x,-ux,y,uy) (x,ux,-y,-uy) (-x,-ux,-y,-uy) - std::array u_tmp = u; for (int ix=0; ix<2; ix++){ for (int iy=0; iy<2; iy++){ - x *= std::pow(-1,ix); + u_tmp = u; + x_tmp = x*std::pow(-1,ix); u_tmp[0] *= std::pow(-1,ix); - y *= std::pow(-1,iy); + y_tmp = y*std::pow(-1,iy); u_tmp[1] *= std::pow(-1,iy); CheckAndAddParticle(x, y, z, u_tmp, weight/4); } -- cgit v1.2.3 From 6c20c43e0ecc9a03b21ef6c57adb2f13911004e2 Mon Sep 17 00:00:00 2001 From: MaxThevenet Date: Tue, 28 May 2019 17:19:53 -0700 Subject: typos --- Source/Particles/PhysicalParticleContainer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'Source/Particles/PhysicalParticleContainer.cpp') diff --git a/Source/Particles/PhysicalParticleContainer.cpp b/Source/Particles/PhysicalParticleContainer.cpp index 2c501985f..6faf7a127 100644 --- a/Source/Particles/PhysicalParticleContainer.cpp +++ b/Source/Particles/PhysicalParticleContainer.cpp @@ -216,7 +216,7 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, plasma_injector->getMomentum(u, x, y, z); if (do_symmetrize){ std::array u_tmp; - Real x_tmp, y_tmp, z_tmp; + Real x_tmp, y_tmp; // Add four particles to the beam: // (x,ux,y,uy) (-x,-ux,y,uy) (x,ux,-y,-uy) (-x,-ux,-y,-uy) for (int ix=0; ix<2; ix++){ @@ -226,7 +226,8 @@ PhysicalParticleContainer::AddGaussianBeam(Real x_m, Real y_m, Real z_m, u_tmp[0] *= std::pow(-1,ix); y_tmp = y*std::pow(-1,iy); u_tmp[1] *= std::pow(-1,iy); - CheckAndAddParticle(x, y, z, u_tmp, weight/4); + CheckAndAddParticle(x_tmp, y_tmp, z, + u_tmp, weight/4); } } } else { -- cgit v1.2.3