|
| 1 | +#ifndef PROBLEM_GENERATOR_H |
| 2 | +#define PROBLEM_GENERATOR_H |
| 3 | + |
| 4 | +#include "enums.h" |
| 5 | +#include "global.h" |
| 6 | + |
| 7 | +#include "archetypes/energy_dist.h" |
| 8 | +#include "archetypes/particle_injector.h" |
| 9 | +#include "archetypes/problem_generator.h" |
| 10 | +#include "archetypes/spatial_dist.h" |
| 11 | +#include "archetypes/traits.h" |
| 12 | +#include "framework/domain/metadomain.h" |
| 13 | +#include "kernels/particle_moments.hpp" |
| 14 | + |
| 15 | +namespace user { |
| 16 | + using namespace ntt; |
| 17 | + |
| 18 | + template <Dimension D> |
| 19 | + struct EMFields { |
| 20 | + Inline auto ex2(const coord_t<D>&) const -> real_t { |
| 21 | + return 0.8; |
| 22 | + } |
| 23 | + |
| 24 | + Inline auto bx3(const coord_t<D>&) const -> real_t { |
| 25 | + return ONE; |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + template <Dimension D> |
| 30 | + struct NonUniformTargetDensity { |
| 31 | + Inline auto operator()(const coord_t<D>& x_Ph) const -> real_t { |
| 32 | + // example of a non-uniform target density that peaks at the center of the domain |
| 33 | + real_t r2 { ZERO }; |
| 34 | + for (auto d = 0u; d < D; ++d) { |
| 35 | + r2 += SQR(x_Ph[d] - 0.5); |
| 36 | + } |
| 37 | + return std::exp( |
| 38 | + -r2 / SQR(0.2)); // <-- characteristic width of the density profile |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + template <SimEngine::type S, class M> |
| 43 | + struct PGen : public arch::ProblemGenerator<S, M> { |
| 44 | + |
| 45 | + static constexpr auto engines { |
| 46 | + arch::traits::pgen::compatible_with<SimEngine::SRPIC>::value |
| 47 | + }; |
| 48 | + static constexpr auto metrics { |
| 49 | + arch::traits::pgen::compatible_with<Metric::Minkowski>::value |
| 50 | + }; |
| 51 | + static constexpr auto dimensions { |
| 52 | + arch::traits::pgen::compatible_with<Dim::_2D, Dim::_3D>::value |
| 53 | + }; |
| 54 | + |
| 55 | + using arch::ProblemGenerator<S, M>::D; |
| 56 | + using arch::ProblemGenerator<S, M>::C; |
| 57 | + using arch::ProblemGenerator<S, M>::params; |
| 58 | + |
| 59 | + EMFields<D> init_flds; |
| 60 | + |
| 61 | + const std::string target_density; |
| 62 | + |
| 63 | + inline PGen(const SimulationParams& p, const Metadomain<S, M>& metadomain) |
| 64 | + : arch::ProblemGenerator<S, M> { p } |
| 65 | + , target_density { params.template get<std::string>( |
| 66 | + "setup.target_density") } {} |
| 67 | + |
| 68 | + void CustomPostStep(timestep_t step, simtime_t time, Domain<S, M>& domain) { |
| 69 | + if (step % 100u != 0u) { |
| 70 | + return; |
| 71 | + } |
| 72 | + // perform replenishment and injection every 100 timesteps |
| 73 | + |
| 74 | + { // compute density of species #1 and #2 |
| 75 | + |
| 76 | + // saves the density to domain.fields.buff(:,:,:,0) |
| 77 | + const auto ni2 = domain.mesh.n_active(in::x2); |
| 78 | + const auto inv_n0 = ONE / params.template get<real_t>("scales.n0"); |
| 79 | + |
| 80 | + auto scatter_buff = Kokkos::Experimental::create_scatter_view( |
| 81 | + domain.fields.buff); |
| 82 | + Kokkos::deep_copy(domain.fields.buff, ZERO); |
| 83 | + for (const auto sp : std::vector<spidx_t> { 1, 2 }) { |
| 84 | + const auto& prtl_spec = domain.species[sp - 1]; |
| 85 | + Kokkos::parallel_for("ComputeDensity", |
| 86 | + prtl_spec.rangeActiveParticles(), |
| 87 | + kernel::ParticleMoments_kernel<S, M, FldsID::N, 3>( |
| 88 | + {}, |
| 89 | + scatter_buff, |
| 90 | + 0u, |
| 91 | + prtl_spec.i1, |
| 92 | + prtl_spec.i2, |
| 93 | + prtl_spec.i3, |
| 94 | + prtl_spec.dx1, |
| 95 | + prtl_spec.dx2, |
| 96 | + prtl_spec.dx3, |
| 97 | + prtl_spec.ux1, |
| 98 | + prtl_spec.ux2, |
| 99 | + prtl_spec.ux3, |
| 100 | + prtl_spec.phi, |
| 101 | + prtl_spec.weight, |
| 102 | + prtl_spec.tag, |
| 103 | + prtl_spec.mass(), |
| 104 | + prtl_spec.charge(), |
| 105 | + false, |
| 106 | + domain.mesh.metric, |
| 107 | + domain.mesh.flds_bc(), |
| 108 | + ni2, |
| 109 | + inv_n0, |
| 110 | + 0u)); |
| 111 | + } |
| 112 | + Kokkos::Experimental::contribute(domain.fields.buff, scatter_buff); |
| 113 | + } |
| 114 | + |
| 115 | + const auto energy_dist = arch::Maxwellian<S, M>( |
| 116 | + domain.mesh.metric, |
| 117 | + domain.random_pool(), |
| 118 | + 0.2); // <-- target temperature for injection |
| 119 | + if (target_density == "uniform") { |
| 120 | + // pass the computed density to the replenisher |
| 121 | + const auto replenish_sdist = arch::ReplenishUniform<S, M, 3>( |
| 122 | + domain.mesh.metric, |
| 123 | + domain.fields.buff, |
| 124 | + 0u, // <-- index in buff where the density is stored |
| 125 | + ONE); // <-- target density for replenishment |
| 126 | + arch::InjectNonUniform<S, M, decltype(energy_dist), decltype(energy_dist), decltype(replenish_sdist)>( |
| 127 | + params, |
| 128 | + domain, |
| 129 | + { 1, 2 }, |
| 130 | + { energy_dist, energy_dist }, |
| 131 | + replenish_sdist, |
| 132 | + ONE); |
| 133 | + } else { |
| 134 | + const auto target_density_profile = NonUniformTargetDensity<D> {}; |
| 135 | + const auto replenish_sdist = |
| 136 | + arch::Replenish<S, M, 3, decltype(target_density_profile)>( |
| 137 | + domain.mesh.metric, |
| 138 | + domain.fields.buff, |
| 139 | + 0u, // <-- index in buff where the density is stored |
| 140 | + target_density_profile, |
| 141 | + ONE); // <-- target density for replenishment |
| 142 | + arch::InjectNonUniform<S, M, decltype(energy_dist), decltype(energy_dist), decltype(replenish_sdist)>( |
| 143 | + params, |
| 144 | + domain, |
| 145 | + { 1, 2 }, |
| 146 | + { energy_dist, energy_dist }, |
| 147 | + replenish_sdist, |
| 148 | + ONE); |
| 149 | + } |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | +} // namespace user |
| 154 | + |
| 155 | +#endif |
0 commit comments