plasmacoll 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- plasmacoll/__init__.py +70 -0
- plasmacoll/_types.py +11 -0
- plasmacoll/background.py +114 -0
- plasmacoll/constants.py +28 -0
- plasmacoll/cross_sections.py +431 -0
- plasmacoll/hydrogen.py +145 -0
- plasmacoll/markers.py +168 -0
- plasmacoll/mcc.py +643 -0
- plasmacoll/process.py +118 -0
- plasmacoll/py.typed +0 -0
- plasmacoll/reactor.py +203 -0
- plasmacoll/tests/__init__.py +0 -0
- plasmacoll/tests/unit/__init__.py +0 -0
- plasmacoll/tests/unit/_helpers.py +62 -0
- plasmacoll/tests/unit/test_background_and_process.py +109 -0
- plasmacoll/tests/unit/test_constants.py +14 -0
- plasmacoll/tests/unit/test_cross_sections.py +216 -0
- plasmacoll/tests/unit/test_hydrogen.py +75 -0
- plasmacoll/tests/unit/test_markers.py +84 -0
- plasmacoll/tests/unit/test_mcc.py +723 -0
- plasmacoll/tests/unit/test_reactor.py +168 -0
- plasmacoll/tests/unit/test_vibrational.py +294 -0
- plasmacoll/vibrational.py +195 -0
- plasmacoll-0.1.0.dist-info/METADATA +193 -0
- plasmacoll-0.1.0.dist-info/RECORD +28 -0
- plasmacoll-0.1.0.dist-info/WHEEL +5 -0
- plasmacoll-0.1.0.dist-info/licenses/LICENSE.txt +21 -0
- plasmacoll-0.1.0.dist-info/top_level.txt +1 -0
plasmacoll/__init__.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""Null-collision Monte Carlo collisions of charged particles with neutral gases.
|
|
2
|
+
|
|
3
|
+
plasmacoll collides markers (positions, velocities and weights in NumPy or CuPy
|
|
4
|
+
arrays, through :mod:`cunumpy`) with prescribed Maxwellian neutral backgrounds:
|
|
5
|
+
elastic and backscatter collisions, excitation, ionization, attachment,
|
|
6
|
+
detachment and charge transfer, with cross sections from tables, LXCat files
|
|
7
|
+
or functions. It is the collision step of a particle-in-cell code, or with
|
|
8
|
+
:class:`ZeroDReactor` a homogeneous reactor for testing cross-section sets.
|
|
9
|
+
|
|
10
|
+
Example:
|
|
11
|
+
>>> import plasmacoll
|
|
12
|
+
>>> from plasmacoll.constants import ELECTRON_MASS, ATOMIC_MASS
|
|
13
|
+
>>> mcc = plasmacoll.MonteCarloCollisions(
|
|
14
|
+
... species_masses={"e": ELECTRON_MASS},
|
|
15
|
+
... backgrounds=[plasmacoll.NeutralBackground("Ar", 1e21, 300.0, 39.95 * ATOMIC_MASS)],
|
|
16
|
+
... processes={"e": [plasmacoll.CollisionProcess(
|
|
17
|
+
... "elastic", "Ar", plasmacoll.CrossSection.constant(1e-19))]},
|
|
18
|
+
... seed=1,
|
|
19
|
+
... )
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from plasmacoll.background import DensityProfile, NeutralBackground
|
|
23
|
+
from plasmacoll.cross_sections import (
|
|
24
|
+
CrossSection,
|
|
25
|
+
LXCatProcess,
|
|
26
|
+
find_lxcat_process,
|
|
27
|
+
read_lxcat,
|
|
28
|
+
)
|
|
29
|
+
from plasmacoll.hydrogen import proton_hydrogen_processes
|
|
30
|
+
from plasmacoll.markers import MarkerSet, ParticleArrays
|
|
31
|
+
from plasmacoll.mcc import (
|
|
32
|
+
MCCDiagnostics,
|
|
33
|
+
MCCResult,
|
|
34
|
+
MonteCarloCollisions,
|
|
35
|
+
NewMarkers,
|
|
36
|
+
make_rng,
|
|
37
|
+
)
|
|
38
|
+
from plasmacoll.process import CollisionProcess
|
|
39
|
+
from plasmacoll.reactor import ReactorHistory, ReactorState, ZeroDReactor
|
|
40
|
+
from plasmacoll.vibrational import (
|
|
41
|
+
VibrationalDistribution,
|
|
42
|
+
boltzmann_level_populations,
|
|
43
|
+
estimate_electronegativity,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
__version__ = "0.1.0"
|
|
47
|
+
|
|
48
|
+
__all__ = [
|
|
49
|
+
"CollisionProcess",
|
|
50
|
+
"CrossSection",
|
|
51
|
+
"DensityProfile",
|
|
52
|
+
"LXCatProcess",
|
|
53
|
+
"MCCDiagnostics",
|
|
54
|
+
"MCCResult",
|
|
55
|
+
"MarkerSet",
|
|
56
|
+
"MonteCarloCollisions",
|
|
57
|
+
"NeutralBackground",
|
|
58
|
+
"NewMarkers",
|
|
59
|
+
"ParticleArrays",
|
|
60
|
+
"ReactorHistory",
|
|
61
|
+
"ReactorState",
|
|
62
|
+
"VibrationalDistribution",
|
|
63
|
+
"ZeroDReactor",
|
|
64
|
+
"boltzmann_level_populations",
|
|
65
|
+
"estimate_electronegativity",
|
|
66
|
+
"find_lxcat_process",
|
|
67
|
+
"make_rng",
|
|
68
|
+
"proton_hydrogen_processes",
|
|
69
|
+
"read_lxcat",
|
|
70
|
+
]
|
plasmacoll/_types.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"""Typing aliases shared by the modules of plasmacoll."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from os import PathLike
|
|
6
|
+
from typing import Any, TypeAlias
|
|
7
|
+
|
|
8
|
+
#: A NumPy or CuPy array, as returned by :mod:`cunumpy` for the active backend.
|
|
9
|
+
Array: TypeAlias = Any
|
|
10
|
+
#: A file path given as a string or a path object.
|
|
11
|
+
PathLikeStr: TypeAlias = str | PathLike[str]
|
plasmacoll/background.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Neutral gas backgrounds: a Maxwellian at rest, uniform or with a density profile."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import cunumpy as xp
|
|
10
|
+
|
|
11
|
+
from plasmacoll._types import Array
|
|
12
|
+
from plasmacoll.constants import BOLTZMANN
|
|
13
|
+
|
|
14
|
+
__all__ = ["DensityProfile", "NeutralBackground"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True)
|
|
18
|
+
class DensityProfile:
|
|
19
|
+
"""A piecewise-linear relative neutral density along one coordinate axis.
|
|
20
|
+
|
|
21
|
+
``factors`` (dimensionless, >= 0) scale the background's reference density
|
|
22
|
+
at the tabulated ``positions`` (m, strictly increasing) of coordinate
|
|
23
|
+
``axis``. Outside the table the end values are used.
|
|
24
|
+
|
|
25
|
+
Attributes:
|
|
26
|
+
axis: The column of the marker positions the profile depends on.
|
|
27
|
+
positions: Table positions in m.
|
|
28
|
+
factors: Density factors at ``positions``.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
axis: int
|
|
32
|
+
positions: Array
|
|
33
|
+
factors: Array
|
|
34
|
+
|
|
35
|
+
def __post_init__(self) -> None:
|
|
36
|
+
"""Validate and normalize the table."""
|
|
37
|
+
positions = xp.asarray(self.positions, dtype=float)
|
|
38
|
+
factors = xp.asarray(self.factors, dtype=float)
|
|
39
|
+
if positions.ndim != 1 or positions.shape != factors.shape:
|
|
40
|
+
raise ValueError("positions and factors must be 1D arrays of equal length")
|
|
41
|
+
if positions.size < 2:
|
|
42
|
+
raise ValueError("a density profile needs at least two points")
|
|
43
|
+
if bool(xp.any(xp.diff(positions) <= 0.0)):
|
|
44
|
+
raise ValueError("profile positions must be strictly increasing")
|
|
45
|
+
if bool(xp.any(factors < 0.0)):
|
|
46
|
+
raise ValueError("profile factors must be >= 0")
|
|
47
|
+
if self.axis < 0:
|
|
48
|
+
raise ValueError("profile axis must be >= 0")
|
|
49
|
+
object.__setattr__(self, "positions", positions)
|
|
50
|
+
object.__setattr__(self, "factors", factors)
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def max_factor(self) -> float:
|
|
54
|
+
"""Return the largest factor, which bounds the density."""
|
|
55
|
+
return float(xp.max(self.factors))
|
|
56
|
+
|
|
57
|
+
def __call__(self, positions: Array) -> Array:
|
|
58
|
+
"""Return the density factor at marker ``positions`` (shape ``(N, ndim)``)."""
|
|
59
|
+
return xp.interp(positions[:, self.axis], self.positions, self.factors)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
@dataclass(frozen=True)
|
|
63
|
+
class NeutralBackground:
|
|
64
|
+
"""A stationary Maxwellian neutral gas.
|
|
65
|
+
|
|
66
|
+
The density is ``density`` everywhere, or ``density * profile(x)`` with a
|
|
67
|
+
:class:`DensityProfile`; ``density`` is then the reference density the
|
|
68
|
+
profile's factors scale.
|
|
69
|
+
|
|
70
|
+
Attributes:
|
|
71
|
+
name: The name processes refer to the background by.
|
|
72
|
+
density: Number density in m^-3.
|
|
73
|
+
temperature: Temperature in K.
|
|
74
|
+
mass: Mass of one neutral particle in kg.
|
|
75
|
+
profile: Optional spatial variation of the density.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
name: str
|
|
79
|
+
density: float
|
|
80
|
+
temperature: float
|
|
81
|
+
mass: float
|
|
82
|
+
profile: DensityProfile | None = None
|
|
83
|
+
|
|
84
|
+
def __post_init__(self) -> None:
|
|
85
|
+
"""Validate the background parameters."""
|
|
86
|
+
if self.density < 0.0:
|
|
87
|
+
raise ValueError("background density must be >= 0")
|
|
88
|
+
if self.temperature < 0.0:
|
|
89
|
+
raise ValueError("background temperature must be >= 0")
|
|
90
|
+
if self.mass <= 0.0:
|
|
91
|
+
raise ValueError("background mass must be > 0")
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def max_density_factor(self) -> float:
|
|
95
|
+
"""Return the largest factor ``density`` is scaled by (1 if uniform)."""
|
|
96
|
+
return 1.0 if self.profile is None else self.profile.max_factor
|
|
97
|
+
|
|
98
|
+
def density_factor(self, positions: Array) -> Array | float:
|
|
99
|
+
"""Return the factor ``density`` is scaled by at ``positions``."""
|
|
100
|
+
return 1.0 if self.profile is None else self.profile(positions)
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def thermal_speed(self) -> float:
|
|
104
|
+
"""Return the 1D thermal speed ``sqrt(k T / M)`` in m/s."""
|
|
105
|
+
return math.sqrt(BOLTZMANN * self.temperature / self.mass)
|
|
106
|
+
|
|
107
|
+
def sample_velocities(self, rng: Any, num: int) -> Array:
|
|
108
|
+
"""Sample ``num`` neutral velocities (3 components) from the Maxwellian.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
rng: A NumPy or CuPy ``Generator``.
|
|
112
|
+
num: The number of velocities.
|
|
113
|
+
"""
|
|
114
|
+
return rng.standard_normal((num, 3)) * self.thermal_speed
|
plasmacoll/constants.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Physical constants in SI units (CODATA 2018).
|
|
2
|
+
|
|
3
|
+
The elementary charge and the Boltzmann constant are exact since the 2019
|
|
4
|
+
redefinition of the SI; the masses are the CODATA 2018 recommended values.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
#: Elementary charge in C, also the number of J in one eV (exact).
|
|
10
|
+
ELEMENTARY_CHARGE = 1.602176634e-19
|
|
11
|
+
#: Boltzmann constant in J/K (exact).
|
|
12
|
+
BOLTZMANN = 1.380649e-23
|
|
13
|
+
#: Electron mass in kg.
|
|
14
|
+
ELECTRON_MASS = 9.1093837015e-31
|
|
15
|
+
#: Proton mass in kg.
|
|
16
|
+
PROTON_MASS = 1.67262192369e-27
|
|
17
|
+
#: Atomic mass constant (1 u, 1/12 of the mass of carbon-12) in kg.
|
|
18
|
+
ATOMIC_MASS = 1.66053906660e-27
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def kelvin_to_ev(temperature: float) -> float:
|
|
22
|
+
"""Return the thermal energy ``k T`` of ``temperature`` (K) in eV."""
|
|
23
|
+
return BOLTZMANN * temperature / ELEMENTARY_CHARGE
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def ev_to_kelvin(energy: float) -> float:
|
|
27
|
+
"""Return the temperature (K) whose thermal energy ``k T`` is ``energy`` (eV)."""
|
|
28
|
+
return energy * ELEMENTARY_CHARGE / BOLTZMANN
|
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
"""Tabulated collision cross sections and readers for LXCat files and plain tables.
|
|
2
|
+
|
|
3
|
+
A :class:`CrossSection` is a table of cross section (m^2) against collision
|
|
4
|
+
energy (eV), interpolated linearly between the table points, held at the last
|
|
5
|
+
value beyond the table and zero below the threshold. Tables come from a
|
|
6
|
+
two-column text file (:meth:`CrossSection.from_table`), from an LXCat file
|
|
7
|
+
(:func:`read_lxcat`), or from a function sampled on an energy grid
|
|
8
|
+
(:meth:`CrossSection.from_function`).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from collections.abc import Callable, Iterable, Sequence
|
|
14
|
+
from dataclasses import dataclass, field
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
import cunumpy as xp
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
from plasmacoll._types import Array, PathLikeStr
|
|
21
|
+
from plasmacoll.constants import ELEMENTARY_CHARGE
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"LXCAT_PROCESS_KINDS",
|
|
25
|
+
"CrossSection",
|
|
26
|
+
"LXCatProcess",
|
|
27
|
+
"find_lxcat_process",
|
|
28
|
+
"read_lxcat",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
#: The process keywords an LXCat cross-section file can contain.
|
|
32
|
+
LXCAT_PROCESS_KINDS = ("ELASTIC", "EFFECTIVE", "EXCITATION", "IONIZATION", "ATTACHMENT")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class CrossSection:
|
|
37
|
+
"""A cross section tabulated against collision energy.
|
|
38
|
+
|
|
39
|
+
``energy`` is in eV and ``sigma`` in m^2. Values are interpolated linearly
|
|
40
|
+
between table points, as prescribed by the Turner et al. (2013) benchmark.
|
|
41
|
+
Beyond the last point the last value is used. Below ``threshold`` (eV)
|
|
42
|
+
the cross section is zero.
|
|
43
|
+
|
|
44
|
+
Attributes:
|
|
45
|
+
energy: Table energies in eV, non-decreasing.
|
|
46
|
+
sigma: Cross sections in m^2 at ``energy``, non-negative.
|
|
47
|
+
threshold: Energy in eV below which the cross section is zero.
|
|
48
|
+
label: A name for plots and messages.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
energy: Array
|
|
52
|
+
sigma: Array
|
|
53
|
+
threshold: float = 0.0
|
|
54
|
+
label: str = ""
|
|
55
|
+
|
|
56
|
+
def __post_init__(self) -> None:
|
|
57
|
+
"""Validate and normalize the table."""
|
|
58
|
+
energy = xp.asarray(self.energy, dtype=float)
|
|
59
|
+
sigma = xp.asarray(self.sigma, dtype=float)
|
|
60
|
+
if energy.ndim != 1 or energy.shape != sigma.shape:
|
|
61
|
+
raise ValueError("energy and sigma must be 1D arrays of equal length")
|
|
62
|
+
if energy.size < 2:
|
|
63
|
+
raise ValueError("a cross-section table needs at least two points")
|
|
64
|
+
if bool(xp.any(xp.diff(energy) < 0.0)):
|
|
65
|
+
raise ValueError("cross-section energies must be non-decreasing")
|
|
66
|
+
if bool(xp.any(sigma < 0.0)):
|
|
67
|
+
raise ValueError("cross sections must be non-negative")
|
|
68
|
+
if self.threshold < 0.0:
|
|
69
|
+
raise ValueError("threshold must be >= 0")
|
|
70
|
+
object.__setattr__(self, "energy", energy)
|
|
71
|
+
object.__setattr__(self, "sigma", sigma)
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def max_energy(self) -> float:
|
|
75
|
+
"""Return the largest tabulated energy in eV."""
|
|
76
|
+
return float(self.energy[-1])
|
|
77
|
+
|
|
78
|
+
@classmethod
|
|
79
|
+
def constant(
|
|
80
|
+
cls,
|
|
81
|
+
sigma: float,
|
|
82
|
+
threshold: float = 0.0,
|
|
83
|
+
max_energy: float = 1.0e6,
|
|
84
|
+
label: str = "",
|
|
85
|
+
) -> CrossSection:
|
|
86
|
+
"""Return a cross section that is ``sigma`` (m^2) at every energy above threshold.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
sigma: The cross section in m^2.
|
|
90
|
+
threshold: Energy in eV below which the cross section is zero.
|
|
91
|
+
max_energy: Last table energy in eV; the value continues beyond it anyway.
|
|
92
|
+
label: A name for plots and messages.
|
|
93
|
+
"""
|
|
94
|
+
return cls(
|
|
95
|
+
energy=xp.asarray([0.0, float(max_energy)]),
|
|
96
|
+
sigma=xp.asarray([float(sigma), float(sigma)]),
|
|
97
|
+
threshold=threshold,
|
|
98
|
+
label=label or f"constant {sigma:.3g} m^2",
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
@classmethod
|
|
102
|
+
def maxwell_molecule(
|
|
103
|
+
cls,
|
|
104
|
+
rate_coefficient: float,
|
|
105
|
+
mass: float,
|
|
106
|
+
threshold: float = 0.0,
|
|
107
|
+
min_energy: float = 1.0e-6,
|
|
108
|
+
max_energy: float = 1.0e6,
|
|
109
|
+
num: int = 2001,
|
|
110
|
+
label: str = "",
|
|
111
|
+
) -> CrossSection:
|
|
112
|
+
"""Return ``sigma = K / g``, whose collision frequency ``n K`` is speed independent.
|
|
113
|
+
|
|
114
|
+
``g = sqrt(2 E / mass)`` is the relative speed at energy ``E``, so pass
|
|
115
|
+
the reduced mass for a process with ``energy_frame="center_of_mass"``
|
|
116
|
+
and the incident mass for ``energy_frame="lab"``. With a constant
|
|
117
|
+
frequency the number of collisions, and the relaxation of moments such
|
|
118
|
+
as the temperature, have closed-form solutions, which makes this the
|
|
119
|
+
cross section of choice for benchmarks. The table has ``num``
|
|
120
|
+
logarithmically spaced points from ``min_energy`` to ``max_energy`` (eV);
|
|
121
|
+
below and above them the end values are used.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
rate_coefficient: ``K = sigma g`` in m^3/s.
|
|
125
|
+
mass: The mass in kg that converts energy to relative speed.
|
|
126
|
+
threshold: Energy in eV below which the cross section is zero.
|
|
127
|
+
min_energy: First table energy in eV.
|
|
128
|
+
max_energy: Last table energy in eV.
|
|
129
|
+
num: Number of table points.
|
|
130
|
+
label: A name for plots and messages.
|
|
131
|
+
"""
|
|
132
|
+
if rate_coefficient < 0.0:
|
|
133
|
+
raise ValueError("rate_coefficient must be >= 0")
|
|
134
|
+
if mass <= 0.0:
|
|
135
|
+
raise ValueError("mass must be > 0")
|
|
136
|
+
if not 0.0 < min_energy < max_energy:
|
|
137
|
+
raise ValueError("need 0 < min_energy < max_energy")
|
|
138
|
+
energy = np.geomspace(min_energy, max_energy, num)
|
|
139
|
+
speed = np.sqrt(2.0 * energy * ELEMENTARY_CHARGE / mass)
|
|
140
|
+
return cls(
|
|
141
|
+
energy=xp.asarray(energy),
|
|
142
|
+
sigma=xp.asarray(rate_coefficient / speed),
|
|
143
|
+
threshold=threshold,
|
|
144
|
+
label=label or f"Maxwell molecule K = {rate_coefficient:.3g} m^3/s",
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
@classmethod
|
|
148
|
+
def from_function(
|
|
149
|
+
cls,
|
|
150
|
+
function: Callable[[np.ndarray], np.ndarray],
|
|
151
|
+
energy: Iterable[float] | Array,
|
|
152
|
+
threshold: float = 0.0,
|
|
153
|
+
label: str = "",
|
|
154
|
+
) -> CrossSection:
|
|
155
|
+
"""Tabulate ``function(energy)`` (m^2) on the energy grid ``energy`` (eV).
|
|
156
|
+
|
|
157
|
+
The function is called once with a NumPy array of the grid energies.
|
|
158
|
+
Choose the grid fine enough that linear interpolation between its
|
|
159
|
+
points follows the function, for example ``numpy.geomspace`` for a
|
|
160
|
+
function that varies on a logarithmic scale.
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
function: Cross section in m^2 as a function of energy in eV.
|
|
164
|
+
energy: The table energies in eV, non-decreasing.
|
|
165
|
+
threshold: Energy in eV below which the cross section is zero.
|
|
166
|
+
label: A name for plots and messages.
|
|
167
|
+
"""
|
|
168
|
+
grid = np.asarray(xp.to_numpy(xp.asarray(energy, dtype=float)))
|
|
169
|
+
values = np.asarray(function(grid), dtype=float)
|
|
170
|
+
return cls(
|
|
171
|
+
energy=xp.asarray(grid),
|
|
172
|
+
sigma=xp.asarray(values),
|
|
173
|
+
threshold=threshold,
|
|
174
|
+
label=label,
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
@classmethod
|
|
178
|
+
def vibrational_mixture(
|
|
179
|
+
cls,
|
|
180
|
+
level_cross_sections: Sequence[CrossSection],
|
|
181
|
+
populations: Sequence[float] | Array,
|
|
182
|
+
label: str = "",
|
|
183
|
+
) -> CrossSection:
|
|
184
|
+
"""Combine level-resolved cross sections into a population-weighted mean.
|
|
185
|
+
|
|
186
|
+
``sigma_mix(E) = sum_v w_v * sigma_v(E)``, with ``w_v`` normalized from
|
|
187
|
+
``populations`` (they need not already sum to 1). A level's own
|
|
188
|
+
threshold and tail behaviour are respected because each ``sigma_v(E)``
|
|
189
|
+
is evaluated through its own :meth:`__call__` before summing, and a
|
|
190
|
+
table point is placed on either side of every threshold so that the
|
|
191
|
+
step is not blurred into a ramp.
|
|
192
|
+
|
|
193
|
+
This performs the weighted average only; it does not supply the
|
|
194
|
+
per-level cross sections. Use it with level-resolved data (for example
|
|
195
|
+
H2(v) dissociative attachment from Wadehra, Phys. Rev. A 29, 106 (1984),
|
|
196
|
+
or an LXCat entry) and the populations of a
|
|
197
|
+
:class:`~plasmacoll.vibrational.VibrationalDistribution`.
|
|
198
|
+
|
|
199
|
+
If ``populations`` covers only some of a distribution's levels, the
|
|
200
|
+
omitted levels' population is redistributed among the included ones by
|
|
201
|
+
the normalization; pass the full set of weights if that is not the
|
|
202
|
+
intended mixture.
|
|
203
|
+
|
|
204
|
+
Args:
|
|
205
|
+
level_cross_sections: One cross section per level.
|
|
206
|
+
populations: The weight of each level, non-negative.
|
|
207
|
+
label: A name for plots and messages.
|
|
208
|
+
"""
|
|
209
|
+
if len(level_cross_sections) == 0:
|
|
210
|
+
raise ValueError("level_cross_sections must not be empty")
|
|
211
|
+
if len(level_cross_sections) != len(populations):
|
|
212
|
+
raise ValueError(
|
|
213
|
+
"level_cross_sections and populations must have equal length, "
|
|
214
|
+
f"got {len(level_cross_sections)} and {len(populations)}"
|
|
215
|
+
)
|
|
216
|
+
weights = np.asarray(xp.to_numpy(xp.asarray(populations, dtype=float)))
|
|
217
|
+
if np.any(weights < 0.0):
|
|
218
|
+
raise ValueError("populations must be non-negative")
|
|
219
|
+
total_weight = float(np.sum(weights))
|
|
220
|
+
if total_weight <= 0.0:
|
|
221
|
+
raise ValueError("populations must sum to a positive value")
|
|
222
|
+
weights = weights / total_weight
|
|
223
|
+
|
|
224
|
+
threshold_points = [
|
|
225
|
+
point
|
|
226
|
+
for level in level_cross_sections
|
|
227
|
+
if level.threshold > 0.0
|
|
228
|
+
for point in (level.threshold * (1.0 - 1.0e-9), level.threshold)
|
|
229
|
+
]
|
|
230
|
+
energy = xp.unique(
|
|
231
|
+
xp.concatenate(
|
|
232
|
+
[level.energy for level in level_cross_sections]
|
|
233
|
+
+ ([xp.asarray(threshold_points)] if threshold_points else [])
|
|
234
|
+
)
|
|
235
|
+
)
|
|
236
|
+
sigma = xp.zeros_like(energy)
|
|
237
|
+
for level, weight in zip(level_cross_sections, weights, strict=True):
|
|
238
|
+
if weight > 0.0:
|
|
239
|
+
sigma = sigma + float(weight) * level(energy)
|
|
240
|
+
return cls(
|
|
241
|
+
energy=energy,
|
|
242
|
+
sigma=sigma,
|
|
243
|
+
threshold=0.0, # already applied in each level's contribution
|
|
244
|
+
label=label or "vibrational mixture",
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
@classmethod
|
|
248
|
+
def from_table(
|
|
249
|
+
cls,
|
|
250
|
+
path: PathLikeStr,
|
|
251
|
+
threshold: float = 0.0,
|
|
252
|
+
delimiter: str | None = None,
|
|
253
|
+
energy_unit: float = 1.0,
|
|
254
|
+
sigma_unit: float = 1.0,
|
|
255
|
+
label: str = "",
|
|
256
|
+
) -> CrossSection:
|
|
257
|
+
"""Read a two-column (energy, sigma) text table.
|
|
258
|
+
|
|
259
|
+
Lines that do not start with two numbers (headers, comments) are skipped.
|
|
260
|
+
|
|
261
|
+
Args:
|
|
262
|
+
path: The text file.
|
|
263
|
+
threshold: Energy in eV below which the cross section is zero.
|
|
264
|
+
delimiter: Column separator; whitespace if None.
|
|
265
|
+
energy_unit: Factor converting the first column to eV.
|
|
266
|
+
sigma_unit: Factor converting the second column to m^2.
|
|
267
|
+
label: A name for plots and messages; the file name if empty.
|
|
268
|
+
"""
|
|
269
|
+
energies = []
|
|
270
|
+
sigmas = []
|
|
271
|
+
for line in Path(path).read_text().splitlines():
|
|
272
|
+
parts = line.replace(delimiter, " ").split() if delimiter else line.split()
|
|
273
|
+
if len(parts) < 2:
|
|
274
|
+
continue
|
|
275
|
+
try:
|
|
276
|
+
energy, sigma = float(parts[0]), float(parts[1])
|
|
277
|
+
except ValueError:
|
|
278
|
+
continue
|
|
279
|
+
energies.append(energy)
|
|
280
|
+
sigmas.append(sigma)
|
|
281
|
+
return cls(
|
|
282
|
+
energy=xp.asarray(energies) * energy_unit,
|
|
283
|
+
sigma=xp.asarray(sigmas) * sigma_unit,
|
|
284
|
+
threshold=threshold,
|
|
285
|
+
label=label or Path(path).stem,
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
def with_threshold(self, threshold: float) -> CrossSection:
|
|
289
|
+
"""Return a copy of this table with another threshold (eV)."""
|
|
290
|
+
return CrossSection(
|
|
291
|
+
energy=self.energy,
|
|
292
|
+
sigma=self.sigma,
|
|
293
|
+
threshold=threshold,
|
|
294
|
+
label=self.label,
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
def __call__(self, energy: Array | float) -> Array:
|
|
298
|
+
"""Return the cross section in m^2 at ``energy`` (eV)."""
|
|
299
|
+
energy = xp.asarray(energy, dtype=float)
|
|
300
|
+
sigma = xp.interp(energy, self.energy, self.sigma)
|
|
301
|
+
return xp.where(energy < self.threshold, 0.0, sigma)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
@dataclass(frozen=True)
|
|
305
|
+
class LXCatProcess:
|
|
306
|
+
"""One process block of an LXCat cross-section file.
|
|
307
|
+
|
|
308
|
+
Attributes:
|
|
309
|
+
kind: The block keyword, one of :data:`LXCAT_PROCESS_KINDS`.
|
|
310
|
+
target: The left side of the reaction line, e.g. ``Ar``.
|
|
311
|
+
product: The right side of the reaction line, e.g. ``Ar^+`` (empty if none).
|
|
312
|
+
parameter: The mass ratio ``m_e/M`` for ``ELASTIC`` and ``EFFECTIVE``,
|
|
313
|
+
the threshold in eV for ``EXCITATION`` and ``IONIZATION``, zero for
|
|
314
|
+
``ATTACHMENT``.
|
|
315
|
+
cross_section: The table.
|
|
316
|
+
comments: The ``KEY: value`` lines of the block, e.g. ``PROCESS``.
|
|
317
|
+
"""
|
|
318
|
+
|
|
319
|
+
kind: str
|
|
320
|
+
target: str
|
|
321
|
+
product: str
|
|
322
|
+
parameter: float
|
|
323
|
+
cross_section: CrossSection
|
|
324
|
+
comments: dict[str, str] = field(default_factory=dict)
|
|
325
|
+
|
|
326
|
+
@property
|
|
327
|
+
def mass_ratio(self) -> float | None:
|
|
328
|
+
"""Return ``m_e/M`` for ELASTIC and EFFECTIVE processes, None otherwise."""
|
|
329
|
+
if self.kind in ("ELASTIC", "EFFECTIVE"):
|
|
330
|
+
return self.parameter
|
|
331
|
+
return None
|
|
332
|
+
|
|
333
|
+
@property
|
|
334
|
+
def threshold(self) -> float:
|
|
335
|
+
"""Return the energy threshold in eV (zero for momentum transfer)."""
|
|
336
|
+
return self.cross_section.threshold
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def _split_reaction(target_line: str) -> tuple[str, str]:
|
|
340
|
+
"""Split an LXCat target line such as ``Ar -> Ar^+`` into its two sides."""
|
|
341
|
+
for arrow in ("<->", "->"):
|
|
342
|
+
if arrow in target_line:
|
|
343
|
+
target, _, product = target_line.partition(arrow)
|
|
344
|
+
return target.strip(), product.strip()
|
|
345
|
+
return target_line, ""
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def read_lxcat(path: PathLikeStr) -> list[LXCatProcess]:
|
|
349
|
+
"""Parse an LXCat cross-section file.
|
|
350
|
+
|
|
351
|
+
Each process block starts with a keyword (``ELASTIC``, ``EFFECTIVE``,
|
|
352
|
+
``EXCITATION``, ``IONIZATION`` or ``ATTACHMENT``), followed by the target
|
|
353
|
+
line, a parameter line for all but ``ATTACHMENT`` (mass ratio for
|
|
354
|
+
momentum transfer, threshold in eV otherwise), optional ``KEY: value``
|
|
355
|
+
comment lines, and a table of energy (eV) and cross section (m^2) framed
|
|
356
|
+
by lines of dashes. Everything outside the blocks is skipped.
|
|
357
|
+
|
|
358
|
+
Args:
|
|
359
|
+
path: The file, as downloaded from https://lxcat.net.
|
|
360
|
+
|
|
361
|
+
Returns:
|
|
362
|
+
The processes in the order of the file.
|
|
363
|
+
"""
|
|
364
|
+
lines = Path(path).read_text(errors="replace").splitlines()
|
|
365
|
+
processes = []
|
|
366
|
+
index = 0
|
|
367
|
+
while index < len(lines):
|
|
368
|
+
kind = lines[index].strip().upper()
|
|
369
|
+
if kind not in LXCAT_PROCESS_KINDS:
|
|
370
|
+
index += 1
|
|
371
|
+
continue
|
|
372
|
+
target_line = lines[index + 1].strip()
|
|
373
|
+
index += 2
|
|
374
|
+
target, product = _split_reaction(target_line)
|
|
375
|
+
parameter = 0.0
|
|
376
|
+
if kind != "ATTACHMENT":
|
|
377
|
+
parameter = float(lines[index].split()[0])
|
|
378
|
+
index += 1
|
|
379
|
+
comments: dict[str, str] = {}
|
|
380
|
+
while index < len(lines) and not lines[index].startswith("-----"):
|
|
381
|
+
key, sep, value = lines[index].partition(":")
|
|
382
|
+
if sep:
|
|
383
|
+
comments[key.strip()] = value.strip()
|
|
384
|
+
index += 1
|
|
385
|
+
index += 1
|
|
386
|
+
energies = []
|
|
387
|
+
sigmas = []
|
|
388
|
+
while index < len(lines) and not lines[index].startswith("-----"):
|
|
389
|
+
parts = lines[index].split()
|
|
390
|
+
if len(parts) >= 2:
|
|
391
|
+
energies.append(float(parts[0]))
|
|
392
|
+
sigmas.append(float(parts[1]))
|
|
393
|
+
index += 1
|
|
394
|
+
index += 1
|
|
395
|
+
threshold = parameter if kind in ("EXCITATION", "IONIZATION") else 0.0
|
|
396
|
+
processes.append(
|
|
397
|
+
LXCatProcess(
|
|
398
|
+
kind=kind,
|
|
399
|
+
target=target,
|
|
400
|
+
product=product,
|
|
401
|
+
parameter=parameter,
|
|
402
|
+
cross_section=CrossSection(
|
|
403
|
+
energy=xp.asarray(energies),
|
|
404
|
+
sigma=xp.asarray(sigmas),
|
|
405
|
+
threshold=threshold,
|
|
406
|
+
label=comments.get("PROCESS", f"{kind} {target_line}"),
|
|
407
|
+
),
|
|
408
|
+
comments=comments,
|
|
409
|
+
),
|
|
410
|
+
)
|
|
411
|
+
return processes
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def find_lxcat_process(
|
|
415
|
+
processes: Sequence[LXCatProcess],
|
|
416
|
+
wanted: str,
|
|
417
|
+
) -> LXCatProcess:
|
|
418
|
+
"""Return the first process whose kind or ``PROCESS`` comment is ``wanted``.
|
|
419
|
+
|
|
420
|
+
Args:
|
|
421
|
+
processes: Processes from :func:`read_lxcat`.
|
|
422
|
+
wanted: A kind such as ``"IONIZATION"`` or a full ``PROCESS`` comment
|
|
423
|
+
such as ``"E + Ar -> E + E + Ar+, Ionization"``.
|
|
424
|
+
|
|
425
|
+
Raises:
|
|
426
|
+
KeyError: If no process matches.
|
|
427
|
+
"""
|
|
428
|
+
for process in processes:
|
|
429
|
+
if wanted in (process.kind, process.comments.get("PROCESS", "")):
|
|
430
|
+
return process
|
|
431
|
+
raise KeyError(f"No LXCat process {wanted!r}")
|