-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc2d.py
277 lines (258 loc) · 12 KB
/
gc2d.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#
# BSD 2-Clause License
#
# Copyright (c) 2021, Cristel Chandre
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as xp
import matplotlib.pyplot as plt
from numpy.fft import fft2, ifft2, ifftn, fftfreq
from scipy.interpolate import interpn
from scipy.special import jv, eval_chebyu
from gc2d_modules import run_method
from gc2d_dict import dict_list, Parallelization
import multiprocessing
from typing import Tuple
def run_case(dict) -> None:
if dict['Potential'] == 'KMdCN':
case = GC2Dk(dict)
elif dict['Potential'] == 'turbulent':
case = GC2Dt(dict)
run_method(case)
def main() -> None:
if Parallelization[0]:
if Parallelization[1] == 'all':
num_cores = multiprocessing.cpu_count()
else:
num_cores = min(multiprocessing.cpu_count(), Parallelization[1])
pool = multiprocessing.Pool(num_cores)
pool.map(run_case, dict_list)
else:
for dict in dict_list:
run_case(dict)
plt.show()
class GC2Dt:
def __repr__(self) -> str:
return "{self.__class__.__name__}({self.DictParams})".format(self=self)
def __str__(self) -> str:
if self.Method.endswith('_fo'):
return f'2D Guiding Center ({self.__class__.__name__}) for the turbulent potential for the full orbits'
elif self.Method.endswith('_gc') or self.Method == 'potentials':
return f'2D Guiding Center ({self.__class__.__name__}) for the turbulent potential with FLR = {self.FLR} and GC order = {self.GCorder}'
def __init__(self, dict_:dict) -> None:
for key in dict_:
setattr(self, key, dict_[key])
self.DictParams = dict_
xp.random.seed(27)
self.phases = 2 * xp.pi * xp.random.random((self.M, self.M))
n = xp.meshgrid(xp.arange(1, self.M+1), xp.arange(1, self.M+1), indexing='ij')
self.xy_ = 2 * (xp.linspace(0, 2 * xp.pi, self.N+1, dtype=xp.float64),)
nm = xp.meshgrid(fftfreq(self.N, d=1/self.N), fftfreq(self.N, d=1/self.N), indexing='ij')
sqrt_nm = xp.sqrt(nm[0]**2 + nm[1]**2)
self.elts_nm = sqrt_nm, xp.angle(nm[0] + 1j * nm[1])
fft_phi = xp.zeros((self.N, self.N), dtype=xp.complex128)
fft_phi[1:self.M+1, 1:self.M+1] = (self.A / (n[0]**2 + n[1]**2)**1.5).astype(xp.complex128) * xp.exp(1j * self.phases)
fft_phi[sqrt_nm > self.M] = 0
self.phi = ifft2(fft_phi) * (self.N**2)
self.pad = lambda psi: xp.pad(psi, ((0, 1),), mode='wrap')
self.derivs = lambda psi: [self.pad(ifft2(1j * nm[_] * fft2(psi))) for _ in range(2)]
if self.FLR[0] == 'all':
flr1_coeff = jv(0, self.rho * sqrt_nm)
elif self.FLR[0] == 'pade':
flr1_coeff = 1 / (1 + self.rho**2 * sqrt_nm**2 / 4)
else:
flr1_coeff = 1
self.phi_gc1_1 = ifft2(flr1_coeff * fft_phi) * (self.N**2)
if self.Method.endswith('_fo'):
self.dim = 4
stack = self.derivs(self.phi)
if self.check_energy:
self.dim += 1
stack = (*stack, self.pad(self.phi))
else:
self.dim = 2
if self.GCorder == 1:
stack = self.derivs(self.phi_gc1_1)
if self.check_energy:
self.dim += 1
stack = (*stack, self.pad(self.phi_gc1_1))
elif self.GCorder == 2:
if self.FLR[1] == 'all' and (self.rho != 0):
flr2_coeff = -sqrt_nm * jv(1, self.rho * sqrt_nm) / self.rho
elif self.FLR[1] == 'pade' and (self.rho != 0):
flr2_coeff = -(sqrt_nm**2 / 2) / (1 + self.rho**2 * sqrt_nm**2 / 8)
else:
flr2_coeff = -sqrt_nm**2 / 2
self.flr2 = lambda psi: ifft2(fft2(psi) * flr2_coeff)
self.phi_gc2_0 = self.eta * (2 * self.phi_gc1_1 * self.flr2(self.phi.conj()) - self.flr2(xp.abs(self.phi)**2)).real / 2
self.phi_gc2_2 = self.eta * (2 * self.phi_gc1_1 * self.flr2(self.phi) - self.flr2(self.phi**2)) / 2
stack = (*self.derivs(self.phi_gc1_1), *self.derivs(self.phi_gc2_0), *self.derivs(self.phi_gc2_2))
if self.check_energy:
self.dim += 1
stack += (self.pad(self.phi_gc1_1), self.pad(self.phi_gc2_2))
self.Dphi = xp.moveaxis(xp.stack(stack), 0, -1)
def eqn(self, t:float, y:xp.ndarray) -> xp.ndarray:
vars = xp.split(y, self.dim)
r = xp.moveaxis(xp.asarray(vars[:2]) % (2 * xp.pi), 0, -1)
fields = xp.moveaxis(interpn(self.xy_, self.Dphi, r), 0, 1)
dphidx, dphidy = fields[:2]
if self.Method.endswith('_gc'):
dy_gc = xp.concatenate((-(dphidy * xp.exp(-1j * t)).imag, (dphidx * xp.exp(-1j * t)).imag), axis=None)
if self.GCorder == 1:
if not self.check_energy:
return dy_gc
phi = fields[2]
dk = (phi * xp.exp(-1j * t)).real
return xp.concatenate((dy_gc, dk), axis=None)
dphidx_0, dphidy_0, dphidx_2, dphidy_2 = fields[2:6]
dy_gc += xp.concatenate((-dphidy_0.real + (dphidy_2 * xp.exp(-2j * t)).real, dphidx_0.real - (dphidx_2 * xp.exp(-2j * t)).real), axis=None)
if self.GCorder == 2:
if not self.check_energy:
return dy_gc
phi1, phi2 = fields[6:8]
dk = (phi1 * xp.exp(-1j * t)).real + 2 * (phi2 * xp.exp(-2j * t)).imag
return xp.concatenate((dy_gc, dk), axis=None)
raise ValueError(f'GCorder={self.GCorder} not currently implemented')
elif self.Method.endswith('_fo'):
if self.eta == 0 or self.rho == 0:
raise ValueError('Eta or Rho cannot be zero for full orbits')
vx, vy = vars[2:4]
dvx = -(dphidx * xp.exp(-1j * t)).imag / self.rho * xp.sign(self.eta) + vy / (2 * self.eta)
dvy = -(dphidy * xp.exp(-1j * t)).imag / self.rho * xp.sign(self.eta) - vx / (2 * self.eta)
d_ = xp.concatenate((self.rho / (2 * xp.abs(self.eta)) * vx, self.rho / (2 * xp.abs(self.eta)) * vy, dvx, dvy), axis=None)
if not self.check_energy:
return d_
phi = fields[2]
dk = (phi * xp.exp(-1j * t)).real / (2 * self.eta)
return xp.concatenate((d_, dk), axis=None)
def compute_energy(self, t:float, *sol) -> xp.ndarray:
r = xp.moveaxis(xp.asarray(sol[:2]) % (2 * xp.pi), 0, -1)
k = sol[-1]
if self.dim <= 3:
phi_1 = interpn(self.xy_, self.pad(self.phi_gc1_1), r)
h = k + (phi_1 * xp.exp(-1j * t)).imag
if self.GCorder == 1:
return h
elif self.GCorder == 2:
phi_0 = interpn(self.xy_, self.pad(self.phi_gc2_0), r)
phi_2 = interpn(self.xy_, self.pad(self.phi_gc2_2), r)
h += phi_0 - (phi_2 * xp.exp(-2j * t)).real
return h
raise ValueError(f'GCorder={self.GCorder} not currently implemented')
else:
vx, vy = sol[2:4]
h = k + self.rho**2 / (8 * self.eta**2) * (vx**2 + vy**2) + (interpn(self.xy_, self.pad(self.phi), r) * xp.exp(-1j * t)).imag / (2 * self.eta)
return h
def fo2gc(self, t:float, *sol, order:int=1) -> Tuple[xp.ndarray, xp.ndarray]:
x, y, vx, vy = sol[:4]
v = vy + 1j * vx
theta, rho = xp.pi + xp.angle(v), self.rho * xp.abs(v)
x_gc, y_gc = x - rho * xp.cos(theta), y + rho * xp.sin(theta)
if order <= 1:
return x_gc, y_gc
grid, s1 = -self.antiderivative(self.phi, rho)
ds1dx, ds1dy = self.derivs(s1)
r_gc = xp.moveaxis(xp.asarray((x_gc, y_gc, theta)) % (2 * xp.pi), 0, -1)
x_gc -= 2 * self.eta * interpn(grid, (ds1dy * xp.exp(-1j * t)).imag, r_gc)
y_gc += 2 * self.eta * interpn(grid, (ds1dx * xp.exp(-1j * t)).imag, r_gc)
if order == 2:
return x_gc, y_gc
raise ValueError(f'fo2gc not available at order {order}')
def compute_mu(self, t:float, *sol, order:int=1) -> xp.ndarray:
x, y, vx, vy = sol[:4]
r = xp.moveaxis(xp.asarray((x, y)) % (2 * xp.pi), 0, -1)
mu = self.rho**2 * (vx**2 + vy**2) / 2
if order == 0:
return mu
r_gc = xp.moveaxis(xp.asarray(self.fo2gc(t, *sol, order=1)) % (2 * xp.pi), 0, -1)
phi_c = interpn(self.xy_, self.pad(self.phi), r)
mu += 2 * self.eta * ((phi_c - interpn(self.xy_, self.pad(self.phi_gc1_1), r_gc)) * xp.exp(-1j * t)).imag
if order == 1:
return mu
phi_gc = interpn(self.xy_, self.pad(self.flr2(self.phi)), r_gc)
phi_2_0 = 2 * phi_c * phi_gc.conj() - interpn(self.xy_, self.pad(self.flr2(xp.abs(self.phi)**2)), r_gc)
phi_2_2 = 2 * phi_c * phi_gc - interpn(self.xy_, self.pad(self.flr2(self.phi**2)), r_gc)
mu += self.eta**2 * (phi_2_2 * xp.exp(-2j * t) - phi_2_0).real
if order == 2:
return mu
raise ValueError(f'compute_mu not available at order {order}')
def antiderivative(self, phi:xp.ndarray, rho:float, N:int=2**8) -> xp.ndarray:
n = xp.arange(1, N//2 + 1)
jvn = xp.moveaxis(xp.asarray([jv(n_, rho * self.elts_nm[0]) for n_ in n]), 0, -1)
ja = 1j**n * jvn / (1j * n) * xp.exp(1j * n * self.elts_nm[1].reshape(self.N, self.N, 1))
ja = xp.concatenate((xp.zeros((self.N, self.N, 1)), ja[:, :, :N//2 - 1], xp.flip((-1)**n * ja.conj(), axis=2)), axis=2)
return self.xy_ + (xp.linspace(0, 2 * xp.pi, N + 1, dtype=xp.float64),), ifftn(fft2(phi) * ja) * N
class GC2Dk:
def __repr__(self) -> str:
return f'{self.__class__.__name__}({self.DictParams})'
def __str__(self) -> str:
return f'2D Guiding Center ({self.__class__.__name__}) for the KMdCN potential with FLR = {self.FLR} and GC order = {self.GCorder}'
def __init__(self, dict_:dict) -> None:
for key in dict_:
setattr(self, key, dict_[key])
self.DictParams = dict_
if self.FLR[0] == 'all':
flr1_coeff = jv(0, self.rho * xp.sqrt(2))
elif self.FLR[0] == 'pade':
flr1_coeff = 1 / (1 + self.rho**2 / 2)
else:
flr1_coeff = 1
self.A1 = self.A * flr1_coeff
if self.FLR[1] == 'all':
flr2_coeff20 = -2 * (jv(1, self.rho * 2) - xp.sqrt(2) * jv(0, self.rho * xp.sqrt(2)) * jv(1, self.rho * xp.sqrt(2))) / self.rho
flr2_coeff22 = -xp.sqrt(2) * (jv(1, self.rho * 2 * xp.sqrt(2)) - jv(0, self.rho * xp.sqrt(2)) * jv(1, self.rho * xp.sqrt(2))) / self.rho
else:
flr2_coeff20, flr2_coeff22 = 0, -1
self.A20 = -(self.A**2) * self.eta * flr2_coeff20
self.A22 = -(self.A**2) * self.eta * flr2_coeff22
def compute_coeffs(self, t:float) -> Tuple[float, float]:
cheby_coeff = eval_chebyu(self.M-1, xp.cos(t))
alpha = 0.5 + (xp.cos((self.M+1) * t) + xp.cos(self.M * t)) * cheby_coeff
beta = 0.5 + (xp.cos((self.M+1) * t) - xp.cos(self.M * t)) * cheby_coeff
return alpha, beta
def eqn_gc(self, t, y:xp.ndarray) -> xp.ndarray:
x_, y_ = xp.split(y, 2)
alpha, beta = self.compute_coeffs(t)
smxy, spxy = xp.sin(x_ - y_), xp.sin(x_ + y_)
dy_gc1 = self.A1 * xp.concatenate((-alpha * smxy + beta * spxy, -alpha * smxy - beta * spxy), axis=None)
if self.GCorder == 1:
return dy_gc1
elif self.GCorder == 2:
v20 = xp.split(self.A20 * alpha * beta * xp.sin(2 * y), 2)
v2m2 = self.A22 * (alpha**2) * xp.sin(2 * (x_ - y_))
v2p2 = self.A22 * (beta**2) * xp.sin(2 * (x_ + y_))
dy_gc2 = 2 * xp.concatenate((v20[1] + v2p2 - v2m2, -v20[0] - v2p2 - v2m2), axis=None)
return dy_gc1 + dy_gc2
def eqn_fo(self, t:float, y:xp.ndarray) -> xp.ndarray:
if self.eta == 0 or self.rho == 0:
raise ValueError('Eta or Rho cannot be zero for full orbits')
x_, y_, vx, vy = xp.split(y, 4)
alpha, beta = self.compute_coeffs(t)
smxy, spxy = xp.sin(x_ - y_), xp.sin(x_ + y_)
dphidx = -alpha * smxy - beta * spxy
dphidy = alpha * smxy - beta * spxy
dvx = -self.A * dphidx / self.rho * xp.sign(self.eta) + vy / (2 * self.eta)
dvy = -self.A * dphidy / self.rho * xp.sign(self.eta) - vx / (2 * self.eta)
return xp.concatenate((self.rho / (2 * xp.abs(self.eta)) * vx, self.rho / (2 * xp.abs(self.eta)) * vy, dvx, dvy), axis=None)
if __name__ == '__main__':
main()