Repository navigation
Bethe-Heitler pair production emission policy - #242
mancininilay wants to merge 12 commits into
Conversation
Host and device code for pair production by a proton on a prescribed
isotropic photon field, p + gamma -> p + e- + e+, in the Born approximation
on a static source:
- total cross section (Maximon 1968) and the differential cross sections
in the lepton energy and angle (Motz, Olsen & Koch 1969, formula 3D-2000),
with series that stay accurate at the threshold;
- photon spectra (monochromatic, power law, blackbody) and the rates of
interaction J(Gamma) and of energy loss I(Gamma), exact in the proton
velocity, tabulated in Gamma;
- tables for sampling one interaction in the rest frame of the proton
(photon energy, lepton energies and angles), their device mirror and the
boost of the outcome to the simulation frame.
All the arithmetic of the tables is in double precision.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
New emission type `bethe_heitler`. At each step an emitting particle interacts with probability tau * J(Gamma_p) * dt / pair_weight (one uniform draw, at most one interaction per step); the outcome is drawn from the rest-frame tables, the particle loses pair_weight * (E- + E+ - eps) of kinetic energy along its momentum, and an electron and a positron of weight pair_weight * weight are inserted at its position. The created particles are charged, unlike those of the existing policies. The policy writes their previous position equal to their position, so that the current deposit sees no displacement for a lepton that is not pushed in the step in which it is created (lepton species listed before the emitting one); the pusher overwrites it otherwise. Charge is conserved in both orders. A slot is reserved in both lepton species before the recoil is applied: when either species is full the interaction does not happen and is counted, and nothing is written out of bounds. The tables are built once per run on the first push, kept on the device and released by a Kokkos finalize hook. Counters (pairs, particles above the tables, skipped interactions, dropped pairs, absorbed photon energy) are available on the host through arch::emission::bh::GetCounters(). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Species keyword `emission = "bethe_heitler"` and the table `[radiation.emission.bethe_heitler]`: lepton species, nominal optical depth (n_ph * alpha * r_e^2 over a unit length), physical mass ratio, pair weight, limits on the probability per step and on the loss per interaction, upper edge of the tables and the photon spectrum. Schema updated and input.default.toml regenerated from it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ARCHETYPES::bethe_heitler, three checks on the pusher and deposit kernels:
- charge: the in-plane current of leptons not yet pushed is exactly zero,
and the discrete continuity equation holds whether or not the leptons
are pushed in the step in which they are created;
- rate and loss: number of pairs against the expectation and mean loss per
interaction against the tabulated value, within 4 standard errors;
- refusals and capacity: a full lepton species gives no recoil and is
counted; a probability above the limit and pair_weight != 1 without
particle weights raise.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The charge test used fresh lepton species, where the previous positions of the electron and the positron slots coincide and a missing initialisation of the previous position cancels between the two species. The new case sets the previous positions of the lepton slots to two different points of the cell of the emitters and checks, before the leptons are pushed, that each species alone deposits no in-plane current and that the continuity equation holds. A control with the insertion of the built-in policies (no write of the previous position) on the same state must violate both. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The factory compared the charges of the two lepton species approximately, so a positron charge one unit in the last place away from minus the electron charge was accepted, and every pair would then have added the difference to the charge on the grid. The stored charges must now cancel exactly. The test calls the factory with both cases. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…th unit A NaN optical depth or probability bound passed the range checks, which are comparisons, and switched the process off silently. The parser and the policy now refuse non-finite values. The description of the parameters says that the cross sections are those of a proton (Z = 1) and that the optical depth is taken over one code unit of length; input.default.toml is regenerated. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The tables are built on host threads, as many as the hardware offered, so every MPI rank started that many. The build now uses at most the processors the process is allowed to run on (its affinity mask on Linux), and with a threaded host execution space at most its number of threads. Every table node is computed by one call, so the tables do not depend on the number of threads. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The reused-slot case of the charge test set the previous positions of the lepton slots to two points of the cell of the emitters, so only the offsets within the cell differed from those of the new leptons: a policy that wrote the offsets and not the cell indices passed. A second arm puts the two points in the two cells diagonally opposite across the cell of the emitters, so the cell indices differ as well, in both directions, while every displacement the deposit can be given stays within one cell. The same-cell arm is kept. Each arm has its control without the write, and the numbers of every arm are printed before they are checked. The test is two-dimensional: the previous position in the third direction is not exercised. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The finite checks covered the parameters of the input block. Two other scalars reach the policy: a NaN timestep, or minus infinity, passed the bound on the probability in GetTables, and an emitting species of infinite mass passed `mass > 0` in the factory. Both are refused now. A zero timestep is treated as before. The constructor of the policy takes the two as given, and says so. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`thread_limit()` returned a reference to a static that the build of the tables lowered. It now returns a value, the number of processors the calling thread may run on where that can be read, and the build computes its count once (that number, and with a threaded host execution space at most its threads) and passes it to the builders, the table of the Born cross section included. No function changes shared state. The comment says what the number is: a count read at the time of the call, not a guarantee about limits on processor time or later changes of the mask. The tables are unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The control wrapper of the charge test has `emitted_species_indices` and `numbers_injected` because the pusher policy requires them of an emission policy, but the test read the pairs from the policy it wraps, so the two were never called and nvcc said so. The test now does what the engine does with either policy: it asks it for the species it names and the numbers it has inserted, and checks that these are the electrons and the positrons. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Validation details for this pull request (commit 43d68f4). This adds Bethe-Heitler pair production by protons on a prescribed photon field as an emission policy of the pusher, following your suggestion at the meeting of November 19. A species with At each step an emitting particle inside the range of the tables is a candidate for one interaction with probability tau J(Gamma_p) dt / pair_weight. J is a tabulated rate and tau = n_ph alpha r_e^2 L, where n_ph is the photon density and L is the physical length of one code unit of length. It is defined like the Thomson optical depth of the two-body module, with alpha r_e^2 in place of sigma_T. The particle is treated as a proton of Lorentz factor Gamma_p = 1 + K / mass_ratio, where K is its kinetic energy in electron rest energies, computed from the time-centred momentum that the pusher passes to the policy. A reduced mass ratio in the dynamics therefore does not change the energy scale of the process at a given K. This maps the energies and does not make the reduced system dynamically similar to the real one. One uniform number decides whether the particle is a candidate, so there is at most one interaction per particle per step and the probability has to be small. The run stops if the probability exceeds max_probability (default 0.1) anywhere on the tables, and a check in the kernel guards the same bound while the run goes on. The tables are checked when they are built, which in a run is at the first push of an emitting species that has particles, and again whenever the time step, the optical depth, the pair weight or the two bounds change. A candidate uses eight more uniform numbers to draw the outcome in the rest frame of the proton from the Born cross sections (Maximon 1968 for the total, formula 3D-2000 of Motz, Olsen and Koch 1969 for the lepton energy and angle). The proton is taken at rest and without recoil in that frame. The tables end at a cutoff that I chose for this approximation, given under the limits below. It is a chosen limit of validity and not a point at which the accuracy was shown to stop. The outcome is boosted to the simulation frame. For an interaction that is accepted the policy then computes the change of momentum that removes pair_weight times D from the kinetic energy of that time-centred momentum, along its direction, where D is the energy of the pair minus the energy of the photon. The pusher adds this change to the momentum at the end of the push. One electron and one positron of weight pair_weight times the weight of the particle are inserted at its position. Without fields the two momenta are the same and the particle loses pair_weight times D up to rounding. In a run without fields (at the earlier commit 66aa6fb) the energy lost by the emitting particles and the energy of the pairs minus the absorbed photons agree to 3e-9 in single precision and 2e-14 in double. With fields the two momenta differ and the energy removed is not exactly that. In a uniform magnetic field that turns the momentum by an angle Phi in one step, the recoil is taken along a direction Phi / 2 away from the final momentum and the particle loses less. For a small loss the relative shortfall is about beta^2 Phi^2 / 8, with beta the speed of the particle, so Phi^2 / 8 for an ultrarelativistic one. In two cooling runs that start at Phi = 0.1 and 0.4 the measured shortfall was about 1.1 times that. The factor belongs to those runs, in which the rotation per step grows as the particle cools, and is not a general coefficient. A candidate is refused, with no Bethe-Heitler recoil and no pair, and counted, in two cases: one of the lepton species is full, or the loss would exceed max_loss_fraction (default 0.04) of the kinetic energy of the particle. A particle above the upper edge of the tables is never a candidate. It is counted at every step, and no random number is drawn for it. Two points differ from the existing policies and deserve your attention. The first is charge. The created particles are charged, and The second is capacity. The policy reserves a slot in both lepton species, with one atomic counter compared against the free capacity, before it applies the recoil. If either species is full the interaction does not happen, it is counted, and a warning is logged once per rank in the life of the process. In this path nothing is written out of bounds, which I checked with Kokkos bounds checking. The existing synchrotron and Compton policies do not have this guard. I will report that separately with a small reproduction. In a run the tables are built once, at the first push of an emitting species that has particles. They are built again if the spectrum, the mass ratio or gamma_max change. The build runs on host threads. On my machine the unit test, most of which is this build, takes about seven seconds with eight threads and about forty with one (approximate). The build starts up to as many threads as the processors the calling thread may run on (the affinity mask on Linux; the hardware concurrency elsewhere, or if the mask cannot be read), and with a threaded host execution space at most its threads. A rank that is bound to its cores therefore does not start threads for the cores of its neighbours. Code that reads the Born table before the policy builds its tables starts that one table with the default count. This is a count taken when the tables are built and not a guarantee: a limit on processor time that leaves the mask wide is not seen, and a later change of the mask is not followed. Tables and counters live in one process-wide object, with views on the device and copies on the host, which is released by a Kokkos finalize hook, because the policy itself is rebuilt at every push. It is meant for the ordinary case of one engine per process: it is not safe against builds started from several host threads at once, nor against initialising Kokkos twice in one process. If you would rather have them owned by the domain or the engine I am happy to move them. The counters (pairs, particles above the tables, skipped interactions, dropped pairs, absorbed photon energy) can be read on the host with arch::emission::bh::GetCounters(), for use in a problem generator. They belong to the rank and start from zero whenever the tables are built, so also after a restart: the counts are not carried across a restart. The change adds one emission type, one branch in MakePusherPolicyEmission, the parameters under radiation.emission.bethe_heitler with the schema and the regenerated default input, and one test, ARCHETYPES::bethe_heitler, which takes the time given above. The test is in 2D and checks the following. The in-plane current of leptons that are not yet pushed is exactly zero, and the continuity equation holds within a tolerance, in both orders of the species. The same holds when the previous positions of the lepton slots were set to other points, in the same cell and in neighbouring cells, and a control that leaves them unset fails. Leaving out each of the parts that were tested (the whole write, the cell indices, the offsets, one direction, one species) makes the test fail. The third direction is not covered. The number of pairs and the mean loss agree with the tables of the policy, which tests the sampling against the tables and not the tables against the physics. The refusals work: a probability above the bound at startup and at runtime, a pair weight without particle weights, a parameter or a time step that is not finite, an emitting species of infinite mass, and lepton charges that do not cancel exactly. A full lepton species is not an error: the pairs that do not fit are skipped and counted, which the test also checks. Nothing in the engine, the update algorithm of the pusher, the deposit or the injector is modified. The only edit under kernels/pushers is the new branch of the factory in sr_policies.h. With no species using the policy, fields and particles were bit-identical to 1.6.0rc on a Serial build in one test (at 66aa6fb). Most of the new lines are the cross sections and the sampler, under src/archetypes/emission/bethe_heitler. They come from a module that I had validated against a separate reference implementation, also written by me, with reviews at the time. For this pull request the referee added an anchor that depends on neither: a quadrature of the published cross section (Blumenthal 1970, equation 10). Against it the tabulated mean loss per interaction is accurate to better than 1e-5 at proton Lorentz factors of 6, 20, 40 and 80 for photons of 0.5 m_e c^2. The mean loss of the sampler is high by 8e-5, 6e-4, 8e-4 and 9.5e-4 at the same points. This comes mainly from the resolution in photon energy of the rest-frame tables of the sampler (16 nodes per decade, and 256 cells in lepton energy and in angle). In the referee's refinement it decreases, to 8e-5 at 80 with four times the nodes and twice the cells. I have not changed the resolution in this pull request. The tests of the policy itself are these, all run at 66aa6fb. The mean energy of cooling protons follows the equations built from the tables, with the correction for the spread of the energies taken from the measured variance, within four standard errors, from Lorentz factors of 6 and of 3. Its variance follows the equation for one draw per step within 1.2 percent, against a limit of 1.8, at the registered points that were reached (one point of the run from a Lorentz factor of 3 was not reached and has no verdict). The pair spectra pass their registered tests against reference histograms at Lorentz factors of 3 and 10. The reference is my own implementation, and the runs of one machine share their random numbers, so they count as one sample. A run from a Lorentz factor of 300 on photons of 0.05 m_e c^2 stays within 0.07 percent of the published loss formula, for that bath and that trajectory. With the largest probability per step between 0.0125 and 0.1 at the same optical depth, the mean energy at fixed times agrees within the statistical errors. No effect of the time step is resolved at that sensitivity. The rule of this comparison was fixed after the runs, and it is not a measurement of the order of convergence. The runs are on a separate branch of my fork and I can share the numbers. On one A100 (CUDA 12.8, Kokkos 5.2.1, single and double precision, one rank), at the earlier commit 66aa6fb, the unit test, the Gauss-law runs, the mean cooling and its variance pass. At the present head the unit test was built with CUDA and run on the same kind of GPU, in both precisions: it passes, and the compiler gives no warning for the new code. The pair spectra pass their registered test, but a quantity outside that test is off: in that sample the mean electron energy is 4.5 standard errors below the expected one. A replay on a CPU of the generator streams that the Kokkos pool has on that GPU reproduces the sample, and all 209510 pairs created in the first step agree with the replay to 3e-14. This comparison is mine and has not been repeated by the referee. So for those outcomes the sampler computes on the GPU what it computes on the CPU from the same random numbers. Whether those numbers are an unlucky sample or show a weakness of the streams as the pool seeds them is not settled. Sixteen other seeds, replayed on the CPU, give nothing unusual, which is an indication and not a proof. The pool has a fixed seed and no input parameter for it, so I could not draw an independent sample on the GPU without changing the source. Limits I know of. The cross section is for a proton, Z = 1, whatever the charge of the emitting species. The lepton species must have mass 1 (within the tolerance of the comparison in the code), so the unit of mass is the electron mass. Photon energies must stay below 2 m_e c^2 in the simulation frame. A particle above the upper edge of the tables has no Bethe-Heitler interaction at all: it is counted at every step and a warning is logged once, and it has neither this cooling nor pairs until it comes back below the edge. Everything else acts on it as usual. By default the edge is where the photon energy in the frame of the proton reaches min(0.05 mass_ratio, 1000) m_e c^2, a Lorentz factor of about 91.8 for photons of 0.5 m_e c^2, and gamma_max can lower it. The edge is a convention. In a run that accelerates particles beyond it those particles have no Bethe-Heitler losses and make no pairs, so the result is not physical for them. The bound on the loss removes rare interactions with a large loss. For photons of 0.5 m_e c^2 and pair_weight 1 no interaction can exceed it at Lorentz factors of 6 and 20. At 40 none was skipped in 1e8 draws, the probability being 2.6e-9 by the referee's calculation. At 80 and 91.8 it skips 5 and 7 interactions in a million, which carry 0.12 and 0.20 percent of the mean loss. The effect grows with pair_weight: at pair_weight 5 the bound removes 1.0, 3.2 and 5.9 percent of the mean loss at Lorentz factors of 20, 40 and 80, again by the referee's calculation. The validation campaign ran in the engine in 2D Cartesian with the zig-zag deposit, on CPUs with OpenMP and with MPI on 1, 2 and 4 ranks, and on one A100, all at the earlier commit 66aa6fb. At the present head the unit test has been rerun, on CPUs and on one A100, the later commits being checks of the input, tests and the thread count of the table build. It compiles in spherical geometry and has not been run there. It has not been run with MPI on GPUs, with the GCA pusher or across a restart. |
|
One correction to the description, found while testing with MPI on GPUs. The description says that when a lepton species is full the candidate is skipped and counted. That is what happens in a build without MPI. In a build with MPI the run stops in the same step: Since I opened this PR I also ran the policy with MPI on 1, 2 and 4 A100s, one rank per GPU, at the same commit. The pair rate and the mean loss agree with the reference within 1.7 standard errors. Charge is conserved across rank boundaries in both species orders. A reconnection run on four GPUs completes, and its pair count is within 0.003% of the prediction from the ion spectrum. So MPI on GPUs is now tested, for up to four ranks on one node. |
This adds Bethe-Heitler pair production by protons on a prescribed photon field as an emission policy, as we discussed on November 19. A species with
emission = "bethe_heitler"creates electron-positron pairs and loses the corresponding energy. The photons are not simulated. They are an isotropic field with a monochromatic, power-law or blackbody spectrum set in the input.How it works. At each step a particle inside the range of the tables draws an interaction candidate with probability
optical_depth * J(Gamma_p) * dt / pair_weight. J is tabulated andGamma_p = 1 + K / mass_ratio, so at fixed kinetic energy K a reduced dynamical mass ratio does not change the energy scale. One uniform number decides, so there is at most one interaction per particle per step, and the run stops if the probability exceedsmax_probability. The outcome is drawn from the Born cross sections in the frame of the proton. The recoil is computed on the time-centred momentum, so the energy balance is exact to rounding without fields and approximate with them.Two points for your attention.
kernel::InjectParticledoes not set the previous position, and a lepton species listed before the emitting one is deposited before its first push. The policy therefore writes the previous position equal to the birth position. The two lepton charges must be exactly opposite.Limits. The cross sections are those of a proton (Z = 1). A particle above the upper edge of the tables does not interact and is counted. A candidate that would remove more than
max_loss_fractionof the kinetic energy is skipped, which lowers the cooling at largepair_weight.What was run. The validation campaign is in 2D Cartesian on CPUs (OpenMP, MPI on up to 4 ranks) and on one A100, at the earlier commit 66aa6fb. The tables and the sampled outcomes are byte-identical since. The unit test passes on CPUs and on one A100 at this commit (43d68f4). On the A100 the pair spectrum passes its histogram test, but the mean electron energy of that sample is 4.5 standard errors low, and the role of the random streams is not settled. There is no engine validation in 3D, in spherical geometry, with the GCA pusher, with MPI on GPUs or across a restart.
Nothing in the engine, the update algorithm of the pusher, the deposit or the injector is modified. The new test is
ARCHETYPES::bethe_heitler. The code was written with Claude and refereed independently with Codex. Validation details are in the first comment.