CPP-snippets 0.0.1
A silly C++ project to use for demonstrating code integration
Loading...
Searching...
No Matches
particles.hpp
1#pragma once
2
3#include "Eigen/Core"
4#include <random>
5
6/**
7 * @class Particles
8 * @brief Stores and advances a dynamically sized set of 1D superparticles.
9 *
10 * Handles particle emission, leapfrog pushing under electric fields, charge
11 * deposition onto grid nodes, and absorbing boundary flux diagnostics.
12 */
13class Particles {
14
15private:
16 /**
17 * @brief Return index of the cell containing position x.
18 * @throws std::out_of_range if x lies outside the domain.
19 */
20 int find_cell_index(const Eigen::VectorXd& node_pos, double x) const;
21 Eigen::VectorXd get_kinetic_energies() const;
22
23public:
24
25 /** @brief Particle positions (1D). */
26 Eigen::VectorXd m_positions;
27
28 /** @brief Previous positions for boundary‐crossing detection. */
29 Eigen::VectorXd m_pos_prev;
30
31 /** @brief Half-step velocities for leapfrog integration. */
32 Eigen::VectorXd m_vel_half;
33
34 /** @brief Charge of a single physical particle (C). */
35 double m_charge;
36
37 /** @brief Mass of a single physical particle (kg). */
38 double m_mass;
39
40 /** @brief Superparticle statistical weight. */
41 double m_weight = 1.0;
42
43 // ---------------------------------------------------------------------
44 // Constructors
45 // ---------------------------------------------------------------------
46
47 /** @brief Default constructor, creates an empty particle set. */
48 Particles() = default;
49
50 /**
51 * @brief Construct a particle set with N superparticles.
52 * @param N Number of particles.
53 * @param q Charge per physical particle.
54 * @param m Mass per physical particle.
55 * @param w Statistical weight per superparticle.
56 */
57 Particles(int N, double q, double m, double w);
58
59 /**
60 * @brief Resize particle arrays to contain N superparticles.
61 * @param N New total particle count.
62 */
63 void resize(int N);
64
65 double interp_linear(const Eigen::VectorXd& x_nodes, const Eigen::VectorXd& E_nodes, double x);
66 // ---------------------------------------------------------------------
67 // Particle creation and pushing
68 // ---------------------------------------------------------------------
69
70 /**
71 * @brief Emit N particles at position xpos with Maxwellian velocity distribution.
72 * @param N Number of emitted particles.
73 * @param xpos Injection position.
74 * @param dt Injection time intervall.
75 * @param energy_eV Thermal energy (eV) for Maxwellian sampling.
76 * @param rng Random generator for velocity/time sampling.
77 */
78 void emit(
79 int N, double xpos, double dt, double energy_eV,
80 const Eigen::VectorXd& node_pos, const Eigen::VectorXd& efield, std::mt19937& rng);
81
82
83 /**
84 * @brief Advance particles using leapfrog (velocity half-step scheme).
85 * @param dt Time step.
86 * @param efield Cell averaged electric fields.
87 * @param node_pos Nodal positions.
88 */
89 void push_leapfrog(double dt, const Eigen::VectorXd& node_pos, const Eigen::VectorXd& efield);
90
91 // ---------------------------------------------------------------------
92 // Diagnostics and charge deposition
93 // ---------------------------------------------------------------------
94
95 /**
96 * @brief Compute absorbed particle fluxes on four electrodes.
97 *
98 * Flux order:
99 * 0: filament
100 * 1: control electrode
101 * 2: acceleration grid
102 * 3: ion collector
103 *
104 * Randomized absorption at the control electrode and acceleration grid
105 * is controlled by probabilities p_ce and p_ag.
106 *
107 * @param el_pos Positions of the four electrodes.
108 * @param rng Random generator for absorption events.
109 * @param p_ce Absorption probability at control electrode.
110 * @param p_ag Absorption probability at acceleration grid.
111 * @return Vector of absorbed charges per boundary.
112 */
113 Eigen::Vector4d fluxes(const Eigen::Vector4d& el_pos,
114 std::mt19937& rng,
115 double p_ce,
116 double p_ag);
117
118 /**
119 * @brief Deposit particle charge onto grid nodes (CIC scheme).
120 * @param node_pos Node positions.
121 * @param dx Cell widths.
122 * @return Charge density vector (C/m) at nodes.
123 */
124 Eigen::VectorXd deposit_charge(const Eigen::VectorXd& node_pos,
125 const Eigen::VectorXd& dx) const;
126
127
128 Eigen::VectorXd energy_distribution(int n_bins, double W_max) const;
129};
130
Particles()=default
Default constructor, creates an empty particle set.
Eigen::Vector4d fluxes(const Eigen::Vector4d &el_pos, std::mt19937 &rng, double p_ce, double p_ag)
Compute absorbed particle fluxes on four electrodes.
Eigen::VectorXd deposit_charge(const Eigen::VectorXd &node_pos, const Eigen::VectorXd &dx) const
Deposit particle charge onto grid nodes (CIC scheme).
double m_weight
Superparticle statistical weight.
Definition particles.hpp:41
void emit(int N, double xpos, double dt, double energy_eV, const Eigen::VectorXd &node_pos, const Eigen::VectorXd &efield, std::mt19937 &rng)
Emit N particles at position xpos with Maxwellian velocity distribution.
Definition particles.cpp:71
void resize(int N)
Resize particle arrays to contain N superparticles.
Definition particles.cpp:18
Eigen::VectorXd m_positions
Particle positions (1D).
Definition particles.hpp:26
void push_leapfrog(double dt, const Eigen::VectorXd &node_pos, const Eigen::VectorXd &efield)
Advance particles using leapfrog (velocity half-step scheme).
double m_mass
Mass of a single physical particle (kg).
Definition particles.hpp:38
double m_charge
Charge of a single physical particle (C).
Definition particles.hpp:35
Eigen::VectorXd m_pos_prev
Previous positions for boundary‐crossing detection.
Definition particles.hpp:29
Eigen::VectorXd m_vel_half
Half-step velocities for leapfrog integration.
Definition particles.hpp:32