-
Notifications
You must be signed in to change notification settings - Fork 306
/
symbols.py
324 lines (261 loc) · 8.41 KB
/
symbols.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
"""
Functions that deal with string representations of atomic symbols
and numbers.
"""
from .particle_class import Particle
from .particle_input import particle_input
# The @particle_input decorator takes the inputs for a function or
# method and passes through the corresponding instance of the Particle
# class via the argument annotated with Particle. If the argument is
# named element, isotope, or ion; then this decorator will raise an
# InvalidElementError, InvalidIsotopeError, or InvalidIonError,
# respectively. The Particle class constructor will raise an
# InvalidParticleError if the input does not correspond to a valid
# particle.
__all__ = [
"atomic_symbol",
"isotope_symbol",
"ionic_symbol",
"particle_input",
"element_name",
]
@particle_input
def atomic_symbol(element: Particle) -> str:
"""
Return the atomic symbol.
Parameters
----------
element: `str`, `int`, or `~plasmapy.atomic.Particle`
A `str` representing an element, isotope, or ion; or an
`int` or `str` representing an atomic number.
Returns
-------
symbol: `str`
The atomic symbol of the element, isotope, or ion.
Raises
------
`~plasmapy.utils.InvalidElementError`
If the argument is a valid particle but not a valid element.
`~plasmapy.utils.InvalidParticleError`
If the argument does not correspond to a valid particle.
`TypeError`
If the argument is not a `str` or `int`.
See Also
--------
element_name, isotope_symbol, ionic_symbol, particle_symbol
Notes
-----
This function returns the symbol of the element rather than the
symbol of an isotope or ion. For example, `'deuterium'`, `'T'`, or
`'hydrogen-2'` will yield `'H'`; `'alpha'` will yield `'He'`; and
`'iron-56'` or `'Fe-56'` will yield `'Fe'`.
This function is case insensitive when there is no potential for
ambiguity associated with case. However, this function will return
`'H'` for hydrogen for lower case `'p'` but capital `'P'` if the
argument is `'P'` for phosphorus. This function will return `'N'`
for nitrogen if the argument is capital `'N'`, but will not accept
lower case `'n'` for neutrons.
Examples
--------
>>> atomic_symbol('helium')
'He'
>>> atomic_symbol(42)
'Mo'
>>> atomic_symbol('D')
'H'
>>> atomic_symbol('C-13')
'C'
>>> atomic_symbol('alpha')
'He'
>>> atomic_symbol('79')
'Au'
>>> atomic_symbol('N') # Nitrogen
'N'
>>> atomic_symbol('P'), atomic_symbol('p') # Phosphorus, proton
('P', 'H')
"""
return element.element
@particle_input
def isotope_symbol(isotope: Particle, mass_numb: int = None) -> str:
"""
Return the symbol representing an isotope.
Parameters
----------
isotope: `str`, `int`, or `~plasmapy.atomic.Particle`
A `str` representing an element, isotope, or ion or an
`int` representing an atomic number
mass_numb: `int` or `str`, optional
The mass number of the isotope.
Returns
-------
symbol: `str`
The isotopic symbol. The result will generally be returned as
something like `'He-4'` or `'Au-197'`. This function will
return `'D'` for deuterium and `'T'` for tritium.
Raises
------
`~plasmapy.utils.InvalidIsotopeError`
If the argument is a valid particle but not a valid isotope.
`~plasmapy.utils.InvalidParticleError`
If the argument does not correspond to a valid particle
or contradictory information is provided.
`TypeError`
If the argument is not a `str`, `int`, or `Particle`.
Warns
-----
`~plasmapy.utils.AtomicWarning`
If redundant isotope information is provided.
See Also
--------
atomic_symbol, ionic_symbol, particle_symbol
Examples
--------
>>> isotope_symbol('He', 4)
'He-4'
>>> isotope_symbol(79, 197)
'Au-197'
>>> isotope_symbol('hydrogen-2')
'D'
>>> isotope_symbol('carbon-13')
'C-13'
>>> isotope_symbol('alpha')
'He-4'
"""
return isotope.isotope
@particle_input(require="element", any_of=('charged', 'uncharged'))
def ionic_symbol(particle: Particle, mass_numb: int = None, Z: int = None) -> str:
"""
Return the ionic symbol of an ion or neutral atom.
Parameters
----------
particle: `int`, `str`, or `~plasmapy.atomic.Particle`
A `str` representing an element, isotope, or ion; or an
`int` representing an atomic number.
mass_numb: `int` or `str`, optional
The mass number of the ion or neutral atom.
Z: `int` or `str`, optional
The integer charge of the ion or neutral atom.
Returns
-------
symbol: `str`
The ionic symbol. The result will generally be returned as
something like `'He-4 2+'`, `'D 1+'`, or `'p+'`.
Raises
------
`~plasmapy.utils.InvalidIonError`
If the arguments correspond to a valid particle but not a valid
ion or neutral charged particle.
`~plasmapy.utils.InvalidParticleError`
If arguments do not correspond to a valid particle or
contradictory information is provided.
`TypeError`
If `particle` is not a `str`, `int`, or
`~plasmapy.atomic.Particle`; or if either of `mass_numb` or `Z`
is not an `int` or `str` representing an integer.
Warns
-----
`~plasmapy.utils.AtomicWarning`
If redundant mass number or charge information is provided.
See Also
--------
atomic_symbol, isotope_symbol, particle_symbol
Examples
--------
>>> ionic_symbol('alpha')
'He-4 2+'
>>> ionic_symbol(79, mass_numb=197, Z=12)
'Au-197 12+'
>>> ionic_symbol('proton')
'p+'
>>> ionic_symbol('D', Z=1)
'D 1+'
>>> ionic_symbol('H-1', Z=0)
'H-1 0+'
"""
return particle.ionic_symbol
@particle_input
def particle_symbol(particle: Particle, mass_numb: int = None, Z: int = None) -> str:
"""
Return the symbol of a particle.
Parameters
----------
particle: `int`, `str`, or `~plasmapy.atomic.Particle`
A `str` representing a particle, element, isotope, or ion or an
`int` representing an atomic number
mass_numb: `int` or `str`, optional
The mass number of an isotope.
Z: `int` or `str`, optional
The integer charge of an ion.
Returns
-------
symbol: `str`
The particle symbol, containing charge and mass number
information when available. The result will generally be
returned as something like `'e-'`, `'Fe'`, `'He-4 2+'`,
`'D'`, `'n'`, `'mu-'`, or `'p+'`.
Raises
------
`~plasmapy.utils.InvalidParticleError`
If arguments do not correspond to a valid particle or
contradictory information is provided.
`TypeError`
If ion is not a `str`, `int`, or `~plasmapy.atomic.Particle`; or
if either of `mass_numb` or `Z` is not an `int` or a `str`
representing an integer.
Warns
-----
`~plasmapy.utils.AtomicWarning`
If redundant mass number or charge information is provided.
See Also
--------
atomic_symbol, isotope_symbol, ionic_symbol
Examples
--------
>>> particle_symbol('electron')
'e-'
>>> particle_symbol('proton')
'p+'
>>> particle_symbol('alpha')
'He-4 2+'
>>> particle_symbol('H-1', Z=-1)
'H-1 1-'
"""
return particle.particle
@particle_input
def element_name(element: Particle) -> str:
"""
Return the name of an element.
Parameters
----------
argument : `str`, `int`, or `~plasmapy.atomic.Particle`
A `str` representing an element, isotope, or ion or an
`int` representing an atomic number
Returns
-------
name : `str`
The name of the element.
Raises
------
`~plasmapy.utils.InvalidElementError`
If the argument is a valid particle but not a valid element.
`~plasmapy.utils.InvalidParticleError`
If the argument does not correspond to a valid particle.
`TypeError`
If the argument is not a `str` or `int`.
See Also
--------
atomic_symbol, isotope_symbol, ionic_symbol, particle_symbol
Examples
--------
>>> element_name("H")
'hydrogen'
>>> element_name("T")
'hydrogen'
>>> element_name("alpha")
'helium'
>>> element_name(42)
'molybdenum'
>>> element_name("C-12")
'carbon'
"""
return element.element_name