-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoot-finding.py
More file actions
177 lines (149 loc) · 5 KB
/
Copy pathRoot-finding.py
File metadata and controls
177 lines (149 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Author: J. K. de Wit
This code computes Bernstein-wave dispersion branches in a homogeneous plasma
(electrostatic, perpendicular limit) by solving D(k,ω)=0
with SciPy's Newton-Raphon's method. Each branch is initialized near ω ≈ n|ω_c|
at the largest k and continued to smaller k using the previous root
as the next initial guess.
"""
import numpy as np
import scipy.constants as sc
from scipy.special import ive
from scipy import optimize
import matplotlib.pyplot as plt
# Helper functions for characteristic frequencies and velocities.
def wp_f(n, q, m):
return np.sqrt(n*q*q/(m*sc.epsilon_0))
def wc_f(B, q, m):
return q*B/m
def vth_f(T_eV, m):
return np.sqrt(2*T_eV*sc.e/m)
# Pre-computes values for faster computations.
def precompute_tables(B_T, species, harmonics, k_list):
k_list = np.asarray(k_list, dtype=np.float64)
tables = []
for sp in species:
name = sp['name']
ns = float(sp['n_m3'])
Ts = float(sp['T_eV'])
qs = float(sp['q_C'])
ms = float(sp['m_kg'])
wps = wp_f(ns, qs, ms)
wcs = wc_f(B_T, qs, ms)
wc_mag = abs(wcs)
vths = vth_f(Ts, ms)
Nh = int(harmonics.get(name, 5))
n_vec = np.arange(-Nh, Nh + 1, dtype=np.float64)
kappa = 0.5*(vths*k_list/wc_mag)**2
Ivals = ive(n_vec[None, :], kappa[:, None]).astype(np.float64)
nI = (n_vec[None, :]*Ivals).astype(np.float64)
pref = 2.0*wps*wps/(vths*vths)
tables.append({
'name': name,
'wc': wc_mag,
'n_vec': n_vec,
'nI': nI,
'pref': pref
})
return tables
# This function computes the dispersion function.
def D_dispersion(w, i, k2, tables):
val = k2
for tab in tables:
wc = tab['wc']
a = w / wc
denom = tab['n_vec'] - a
nI_row = tab['nI'][i, :]
pref = tab['pref']
val += pref * np.sum(nI_row / denom)
return val
# This function computes the derivative of the dispersion funktion
# with respect to the angular frequency. Used for Newton-Rahpon's
# method.
def dDdw_dispersion(w, i, k2, tables):
val = 0.0
for tab in tables:
wc = tab['wc']
a = w / wc
denom = tab['n_vec'] - a
nI_row = tab['nI'][i, :]
pref = tab['pref']
val += (pref / wc) * np.sum(nI_row / (denom * denom))
return val
# The main function that solves the roots for the k array.
def solve_dispersion_branches(B_T, species, harmonics, k_list, w0_list, rtol=1e-5, maxiter=60):
k_list = np.asarray(k_list, dtype=np.float64)
Nk = k_list.size
tables = precompute_tables(B_T, species, harmonics, k_list)
branches = []
for w0 in w0_list:
w_list = np.zeros(Nk, dtype=np.float64)
w = float(w0)
for i in range(Nk):
k2 = k_list[i] * k_list[i]
sol = optimize.root_scalar(
D_dispersion,
x0=w,
fprime=dDdw_dispersion,
args=(i, k2, tables),
method="newton",
rtol=rtol,
maxiter=maxiter
)
if sol.converged and np.isfinite(sol.root):
w = float(sol.root)
w_list[i] = w
branches.append(w_list)
return np.array(branches)
# Main function of the script. Plasma parameters are defined here.
# The example includes electrons and a single ion species, modeled
# as protons.
def main():
B_T = 0.05
Te_eV = 5.0
Ti_eV = 5.0
ne_m3 = 5e16
ni_m3 = 5e16
species = [
{'name': 'e', 'n_m3': ne_m3, 'T_eV': Te_eV, 'q_C': -sc.e, 'm_kg': sc.m_e},
{'name': 'i', 'n_m3': ni_m3, 'T_eV': Ti_eV, 'q_C': +sc.e, 'm_kg': sc.m_p},
]
harmonics = {'e': 6, 'i': 6}
# The k values computed. These parameters highlight the
# electron Bernstein branches for the chosen parameters.
k_min = 1e3
k_max = 40e3
Nk = 1000
k_list = np.linspace(k_max, k_min, Nk)
rtol = 1e-5
maxiter = 60
# The initial guess at large k, chosen here to be
# electron Bernstein branches. In this example,
# set species[1]['q_C'] and species[1]['m_kg']
# to see the ion branches. Set also k_min = 10 and k_max = 1e3.
wce_mag = abs(wc_f(B_T, species[0]['q_C'], species[0]['m_kg']))
# Compute first 4 branches.
harmonic_numbers = [1, 2, 3, 4]
w0_list = [wce_mag * n * 1.005 for n in harmonic_numbers]
w_branches = solve_dispersion_branches(
B_T=B_T,
species=species,
harmonics=harmonics,
k_list=k_list,
w0_list=w0_list,
rtol=rtol,
maxiter=maxiter
)
# Plotting dispersion branches.
fig, ax = plt.subplots(figsize=(6.0, 3.0))
for idx, n in enumerate(harmonic_numbers):
ax.plot(k_list, w_branches[idx] / (2*np.pi*1e9), lw=1.5, label=f"n={n}")
ax.grid(True)
ax.set_xlim(k_min, k_max)
ax.set_xlabel("k [1/m]")
ax.set_ylabel("f [GHz]")
ax.legend(title="Cyclotron harmonic")
fig.tight_layout()
plt.show()
if __name__ == "__main__":
main()