1
1
"""Acceptance computation"""
2
2
3
- import numpy
4
- from .boundary import GridMode
5
-
6
- # noinspection PyProtectedMember
7
- from .boundary import boundary_search
8
- from typing import Optional , Sequence
9
- import multiprocessing
10
- from ..lattice import Lattice , Refpts , frequency_control
11
-
3
+ from __future__ import annotations
12
4
13
5
__all__ = [
14
6
"get_acceptance" ,
18
10
"get_momentum_acceptance" ,
19
11
]
20
12
13
+ import multiprocessing
14
+ from typing import Sequence
15
+
16
+ import numpy as np
17
+
18
+ from .boundary import GridMode
19
+
20
+ # noinspection PyProtectedMember
21
+ from .boundary import boundary_search
22
+ from ..lattice import Lattice , Refpts , frequency_control
23
+
21
24
22
25
@frequency_control
23
26
def get_acceptance (
24
27
ring : Lattice ,
25
28
planes ,
26
29
npoints ,
27
30
amplitudes ,
28
- nturns : Optional [ int ] = 1024 ,
29
- refpts : Optional [ Refpts ] = None ,
30
- dp : Optional [ float ] = None ,
31
+ nturns : int | None = 1024 ,
32
+ refpts : Refpts | None = None ,
33
+ dp : float | None = None ,
31
34
offset : Sequence [float ] = None ,
32
35
bounds = None ,
33
- grid_mode : Optional [ GridMode ] = GridMode .RADIAL ,
34
- use_mp : Optional [ bool ] = False ,
35
- verbose : Optional [ bool ] = True ,
36
- divider : Optional [ int ] = 2 ,
37
- shift_zero : Optional [ float ] = 1.0e-6 ,
38
- start_method : Optional [ str ] = None ,
36
+ grid_mode : GridMode | None = GridMode .RADIAL ,
37
+ use_mp : bool | None = False ,
38
+ verbose : bool | None = True ,
39
+ divider : int | None = 2 ,
40
+ shift_zero : float | None = 1.0e-6 ,
41
+ start_method : str | None = None ,
39
42
):
40
43
# noinspection PyUnresolvedReferences
41
44
r"""Computes the acceptance at ``repfts`` observation points
@@ -57,7 +60,7 @@ def get_acceptance(
57
60
dp: static momentum offset
58
61
offset: initial orbit. Default: closed orbit
59
62
bounds: defines the tracked range: range=bounds*amplitude.
60
- It can be use to select quadrants. For example, default values are:
63
+ It can be used to select quadrants. For example, default values are:
61
64
62
65
* :py:attr:`.GridMode.CARTESIAN`: ((-1, 1), (0, 1))
63
66
* :py:attr:`GridMode.RADIAL/RECURSIVE <.GridMode.RADIAL>`: ((0, 1),
@@ -76,9 +79,9 @@ def get_acceptance(
76
79
start_method: Python multiprocessing start method. The default
77
80
``None`` uses the python default that is considered safe.
78
81
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
79
- The default for linux is ``'fork'``, the default for MacOS and
80
- Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed- up
81
- the calculation or to solve runtime errors, however it is
82
+ The default for linux is ``'fork'``, the default for macOS and
83
+ Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
84
+ the calculation or to solve runtime errors, however it is
82
85
considered unsafe.
83
86
84
87
Returns:
@@ -91,9 +94,9 @@ def get_acceptance(
91
94
92
95
Examples:
93
96
94
- >>> bf,sf,gf = ring.get_acceptance(planes, npoints, amplitudes)
95
- >>> plt.plot(*gf,'.' )
96
- >>> plt.plot(*sf,'.' )
97
+ >>> bf, sf, gf = ring.get_acceptance(planes, npoints, amplitudes)
98
+ >>> plt.plot(*gf, "." )
99
+ >>> plt.plot(*sf, "." )
97
100
>>> plt.plot(*bf)
98
101
>>> plt.show()
99
102
@@ -114,7 +117,7 @@ def get_acceptance(
114
117
115
118
if verbose :
116
119
nproc = multiprocessing .cpu_count ()
117
- print ("\n {0 } cpu found for acceptance calculation" . format ( nproc ) )
120
+ print (f "\n { nproc } cpu found for acceptance calculation" )
118
121
if use_mp :
119
122
nprocu = nproc
120
123
print ("Multi-process acceptance calculation selected..." )
@@ -125,27 +128,25 @@ def get_acceptance(
125
128
print ("Single process acceptance calculation selected..." )
126
129
if nproc > 1 :
127
130
print ("Consider use_mp=True for parallelized computations" )
128
- np = numpy .atleast_1d (npoints )
131
+ npts = np .atleast_1d (npoints )
129
132
na = 2
130
- if len (np ) == 2 :
131
- na = np [1 ]
132
- npp = numpy .prod (npoints )
133
- rpp = 2 * numpy .ceil (numpy .log2 (np [0 ])) * numpy .ceil (na / nprocu )
133
+ if len (npts ) == 2 :
134
+ na = npts [1 ]
135
+ npp = np .prod (npoints )
136
+ rpp = 2 * np .ceil (np .log2 (npts [0 ])) * np .ceil (na / nprocu )
134
137
mpp = npp / nprocu
135
138
if rpp > mpp :
136
139
cond = grid_mode is GridMode .RADIAL or grid_mode is GridMode .CARTESIAN
137
140
else :
138
141
cond = grid_mode is GridMode .RECURSIVE
139
142
if rpp > mpp and not cond :
140
- print ("The estimated load for grid mode is {0}" .format (mpp ))
141
- print ("The estimated load for recursive mode is {0}" .format (rpp ))
142
- print (
143
- "{0} or {1} is recommended" .format (GridMode .RADIAL , GridMode .CARTESIAN )
144
- )
143
+ print (f"The estimated load for grid mode is { mpp } " )
144
+ print (f"The estimated load for recursive mode is { rpp } " )
145
+ print (f"{ GridMode .RADIAL } or { GridMode .CARTESIAN } is recommended" )
145
146
elif rpp < mpp and not cond :
146
- print ("The estimated load for grid mode is {0}" . format ( mpp ) )
147
- print ("The estimated load for recursive mode is {0}" . format ( rpp ) )
148
- print ("{0 } is recommended". format ( GridMode . RECURSIVE ) )
147
+ print (f "The estimated load for grid mode is { mpp } " )
148
+ print (f "The estimated load for recursive mode is { rpp } " )
149
+ print (f" { GridMode . RECURSIVE } is recommended" )
149
150
150
151
b , s , g = boundary_search (
151
152
ring ,
@@ -172,16 +173,16 @@ def get_1d_acceptance(
172
173
plane : str ,
173
174
resolution : float ,
174
175
amplitude : float ,
175
- nturns : Optional [ int ] = 1024 ,
176
- refpts : Optional [ Refpts ] = None ,
177
- dp : Optional [ float ] = None ,
176
+ nturns : int | None = 1024 ,
177
+ refpts : Refpts | None = None ,
178
+ dp : float | None = None ,
178
179
offset : Sequence [float ] = None ,
179
- grid_mode : Optional [ GridMode ] = GridMode .RADIAL ,
180
- use_mp : Optional [ bool ] = False ,
181
- verbose : Optional [ bool ] = False ,
182
- divider : Optional [ int ] = 2 ,
183
- shift_zero : Optional [ float ] = 1.0e-6 ,
184
- start_method : Optional [ str ] = None ,
180
+ grid_mode : GridMode | None = GridMode .RADIAL ,
181
+ use_mp : bool | None = False ,
182
+ verbose : bool | None = False ,
183
+ divider : int | None = 2 ,
184
+ shift_zero : float | None = 1.0e-6 ,
185
+ start_method : str | None = None ,
185
186
):
186
187
r"""Computes the 1D acceptance at ``refpts`` observation points
187
188
@@ -208,7 +209,7 @@ def get_1d_acceptance(
208
209
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
209
210
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
210
211
use_mp: Use python multiprocessing (:py:func:`.patpass`,
211
- default use :py:func:`.lattice_pass`). In case multi-processing
212
+ default use :py:func:`.lattice_pass`). In case multiprocessing
212
213
is not enabled, ``grid_mode`` is forced to
213
214
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
214
215
verbose: Print out some information
@@ -218,15 +219,15 @@ def get_1d_acceptance(
218
219
start_method: Python multiprocessing start method. The default
219
220
``None`` uses the python default that is considered safe.
220
221
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
221
- The default for linux is ``'fork'``, the default for MacOS and
222
- Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed- up
223
- the calculation or to solve runtime errors, however it is considered
222
+ The default for linux is ``'fork'``, the default for macOS and
223
+ Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
224
+ the calculation or to solve runtime errors, however it is considered
224
225
unsafe.
225
226
226
227
Returns:
227
228
boundary: (len(refpts),2) array: 1D acceptance
228
- tracked: (n,) array: Coordinates of tracked particles
229
229
survived: (n,) array: Coordinates of surviving particles
230
+ tracked: (n,) array: Coordinates of tracked particles
230
231
231
232
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
232
233
with one array per ref. point.
@@ -244,10 +245,10 @@ def get_1d_acceptance(
244
245
"""
245
246
if not use_mp :
246
247
grid_mode = GridMode .RECURSIVE
247
- assert len (numpy .atleast_1d (plane )) == 1 , "1D acceptance: single plane required"
248
- assert numpy .isscalar (resolution ), "1D acceptance: scalar args required"
249
- assert numpy .isscalar (amplitude ), "1D acceptance: scalar args required"
250
- npoint = numpy .ceil (amplitude / resolution )
248
+ assert len (np .atleast_1d (plane )) == 1 , "1D acceptance: single plane required"
249
+ assert np .isscalar (resolution ), "1D acceptance: scalar args required"
250
+ assert np .isscalar (amplitude ), "1D acceptance: scalar args required"
251
+ npoint = np .ceil (amplitude / resolution )
251
252
if grid_mode is not GridMode .RECURSIVE :
252
253
assert (
253
254
npoint > 1
@@ -268,7 +269,7 @@ def get_1d_acceptance(
268
269
shift_zero = shift_zero ,
269
270
offset = offset ,
270
271
)
271
- return numpy .squeeze (b ), s , g
272
+ return np .squeeze (b ), s , g
272
273
273
274
274
275
def get_horizontal_acceptance (
@@ -298,7 +299,7 @@ def get_horizontal_acceptance(
298
299
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
299
300
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
300
301
use_mp: Use python multiprocessing (:py:func:`.patpass`,
301
- default use :py:func:`.lattice_pass`). In case multi-processing
302
+ default use :py:func:`.lattice_pass`). In case multiprocessing
302
303
is not enabled, ``grid_mode`` is forced to
303
304
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
304
305
verbose: Print out some information
@@ -308,15 +309,15 @@ def get_horizontal_acceptance(
308
309
start_method: Python multiprocessing start method. The default
309
310
``None`` uses the python default that is considered safe.
310
311
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
311
- The default for linux is ``'fork'``, the default for MacOS and
312
- Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed- up
313
- the calculation or to solve runtime errors, however it is considered
312
+ The default for linux is ``'fork'``, the default for macOS and
313
+ Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
314
+ the calculation or to solve runtime errors, however it is considered
314
315
unsafe.
315
316
316
317
Returns:
317
318
boundary: (len(refpts),2) array: 1D acceptance
318
- tracked: (n,) array: Coordinates of tracked particles
319
319
survived: (n,) array: Coordinates of surviving particles
320
+ tracked: (n,) array: Coordinates of tracked particles
320
321
321
322
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
322
323
with one array per ref. point.
@@ -362,7 +363,7 @@ def get_vertical_acceptance(
362
363
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
363
364
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
364
365
use_mp: Use python multiprocessing (:py:func:`.patpass`,
365
- default use :py:func:`.lattice_pass`). In case multi-processing
366
+ default use :py:func:`.lattice_pass`). In case multiprocessing
366
367
is not enabled, ``grid_mode`` is forced to
367
368
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
368
369
verbose: Print out some information
@@ -372,15 +373,15 @@ def get_vertical_acceptance(
372
373
start_method: Python multiprocessing start method. The default
373
374
``None`` uses the python default that is considered safe.
374
375
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
375
- The default for linux is ``'fork'``, the default for MacOS and
376
- Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed- up
377
- the calculation or to solve runtime errors, however it is considered
376
+ The default for linux is ``'fork'``, the default for macOS and
377
+ Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
378
+ the calculation or to solve runtime errors, however it is considered
378
379
unsafe.
379
380
380
381
Returns:
381
382
boundary: (len(refpts),2) array: 1D acceptance
382
- tracked: (n,) array: Coordinates of tracked particles
383
383
survived: (n,) array: Coordinates of surviving particles
384
+ tracked: (n,) array: Coordinates of tracked particles
384
385
385
386
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
386
387
with one array per ref. point.
@@ -426,7 +427,7 @@ def get_momentum_acceptance(
426
427
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
427
428
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
428
429
use_mp: Use python multiprocessing (:py:func:`.patpass`,
429
- default use :py:func:`.lattice_pass`). In case multi-processing is
430
+ default use :py:func:`.lattice_pass`). In case multiprocessing is
430
431
not enabled, ``grid_mode`` is forced to
431
432
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
432
433
verbose: Print out some information
@@ -436,15 +437,15 @@ def get_momentum_acceptance(
436
437
start_method: Python multiprocessing start method. The default
437
438
``None`` uses the python default that is considered safe.
438
439
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
439
- The default for linux is ``'fork'``, the default for MacOS and
440
- Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed- up
441
- the calculation or to solve runtime errors, however it is considered
440
+ The default for linux is ``'fork'``, the default for macOS and
441
+ Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
442
+ the calculation or to solve runtime errors, however it is considered
442
443
unsafe.
443
444
444
445
Returns:
445
446
boundary: (len(refpts),2) array: 1D acceptance
446
- tracked: (n,) array: Coordinates of tracked particles
447
447
survived: (n,) array: Coordinates of surviving particles
448
+ tracked: (n,) array: Coordinates of tracked particles
448
449
449
450
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
450
451
with one array per ref. point.
0 commit comments