forked from ReactionMechanismGenerator/RMG-Py
-
Notifications
You must be signed in to change notification settings - Fork 3
/
quantity.py
642 lines (534 loc) · 25.8 KB
/
quantity.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#!/usr/bin/env python
# encoding: utf-8
################################################################################
#
# RMG - Reaction Mechanism Generator
#
# Copyright (c) 2009-2011 by the RMG Team (rmg_dev@mit.edu)
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the 'Software'),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
################################################################################
"""
This module contains classes and methods for working with physical quantities,
particularly the :class:`Quantity` class for representing physical quantities.
"""
import numpy
import quantities as pq
import rmgpy.constants as constants
################################################################################
# Explicitly set the default units to SI
pq.set_default_units('si')
# These units are not defined by the quantities package, but occur frequently
# in data handled by RMG, so we define them manually
pq.UnitQuantity('kilocalories', pq.cal*1e3, symbol='kcal')
pq.UnitQuantity('kilojoules', pq.J*1e3, symbol='kJ')
pq.UnitQuantity('kilomoles', pq.mol*1e3, symbol='kmol')
pq.UnitQuantity('molecule', pq.mol/6.02214179e23, symbol='molecule')
pq.UnitQuantity('molecules', pq.mol/6.02214179e23, symbol='molecules')
pq.UnitQuantity('debye', 1.0/(constants.c*1e21)*pq.C*pq.m, symbol='De')
################################################################################
class QuantityError(Exception):
"""
An exception to be raised when an error occurs while working with physical
quantities in RMG. Pass a string describing the circumstances of the
exceptional behavior.
"""
pass
################################################################################
class Units(object):
"""
The :class:`Units` class provides a representation of the units of a
physical quantity. The attributes are:
=================== ========================================================
Attribute Description
=================== ========================================================
`units` A string representation of the units
=================== ========================================================
Functions that return the conversion factors to and from SI units are
provided.
"""
# A dict of conversion factors (to SI) for each of the frequent units
# Here we also define that cm^-1 is not to be converted to m^-1 (or Hz, J, K, etc.)
conversionFactors = {'cm^-1': 1.0}
def __init__(self, units=''):
self.units = units
def getConversionFactorToSI(self):
"""
Return the conversion factor for converting a quantity in a given set
of`units` to the SI equivalent units.
"""
try:
# Process several common units manually for speed
factor = Units.conversionFactors[self.units]
except KeyError:
# Fall back to (slow!) quantities package for less common units
factor = float(pq.Quantity(1.0, self.units).simplified)
# Cache the conversion factor so we don't ever need to use
# quantities to compute it again
Units.conversionFactors[self.units] = factor
return factor
def getConversionFactorFromSI(self):
"""
Return the conversion factor for converting a quantity to a given set
of `units` from the SI equivalent units.
"""
return 1.0 / self.getConversionFactorToSI()
################################################################################
class ScalarQuantity(Units):
"""
The :class:`ScalarQuantity` class provides a representation of a scalar
physical quantity, with optional units and uncertainty information. The
attributes are:
=================== ========================================================
Attribute Description
=================== ========================================================
`value` The numeric value of the quantity in the given units
`units` The units the value was specified in
`uncertainty` The numeric uncertainty in the value
`uncertaintyType` The type of uncertainty: ``'+|-'`` for additive, ``'*|/'`` for multiplicative
`value_si` The numeric value of the quantity in the corresponding SI units
=================== ========================================================
It is often more convenient to perform computations using SI units instead
of the given units of the quantity. For this reason, the SI equivalent of
the `value` attribute can be directly accessed using the `value_si`
attribute. This value is cached on the :class:`ScalarQuantity` object for
speed.
"""
def __init__(self, value, units='', uncertainty=None, uncertaintyType='+|-'):
Units.__init__(self, units)
self.value = value
self.uncertaintyType = uncertaintyType
self.uncertainty = float(uncertainty) if uncertainty is not None else 0.0
def __reduce__(self):
"""
Return a tuple of information used to pickle the scalar quantity.
"""
return (ScalarQuantity, (self.value, self.units, self.uncertainty, self.uncertaintyType))
def __str__(self):
"""
Return a string representation of the scalar quantity.
"""
result = '{0:g}'.format(self.value)
if self.uncertainty != 0.0:
result += ' {0} {1:g}'.format(self.uncertaintyType, self.uncertainty)
if self.units != '':
result += ' {0}'.format(self.units)
return result
def __repr__(self):
"""
Return a string representation that can be used to reconstruct the
scalar quantity.
"""
if self.units == '' and self.uncertainty == 0.0:
return '{0:g}'.format(self.value)
else:
result = '({0:g},{1!r}'.format(self.value, self.units)
if self.uncertainty != 0.0:
result += ',{0!r},{1:g}'.format(self.uncertaintyType, self.uncertainty)
result += ')'
return result
def copy(self):
"""
Return a copy of the quantity.
"""
return ScalarQuantity(self.value, self.units, self.uncertainty, self.uncertaintyType)
def getValue(self):
return self.value_si * self.getConversionFactorFromSI()
def setValue(self, v):
self.value_si = float(v) * self.getConversionFactorToSI()
value = property(getValue, setValue)
def equals(self, quantity):
"""
Return ``True`` if the everything in a quantity object matches
the parameters in this object. If there are lists of values or uncertainties,
each item in the list must be matching and in the same order.
Otherwise, return ``False``
(Originally intended to return warning if units capitalization was
different, however, Quantity object only parses units matching in case, so
this will not be a problem.)
"""
def approx_equal(x, y, atol = .01):
"""
Returns true if two float/double values are approximately equal
within a relative error of 1% or under a user specific absolute tolerance.
"""
return abs(x-y) <= 1e-2*abs(x) or abs(x-y) <= 1e-2*abs(y) or abs(x-y) <= atol
if isinstance(quantity, ScalarQuantity):
if (self.uncertaintyType == quantity.uncertaintyType and
approx_equal(self.uncertainty * self.getConversionFactorToSI(), quantity.uncertainty * quantity.getConversionFactorToSI()) and
self.units == quantity.units):
if self.units == "kcal/mol":
# set absolute tolerance to .01 kcal/mol = 42 J/mol
atol = 42
else:
# for other units, set it to .01
atol = .01
if not approx_equal(self.value_si, quantity.value_si, atol):
return False
return True
return False
def isUncertaintyAdditive(self):
"""
Return ``True`` if the uncertainty is specified in additive format
and ``False`` otherwise.
"""
return self.uncertaintyType == '+|-'
def isUncertaintyMultiplicative(self):
"""
Return ``True`` if the uncertainty is specified in multiplicative
format and ``False`` otherwise.
"""
return self.uncertaintyType == '*|/'
################################################################################
class ArrayQuantity(Units):
"""
The :class:`ScalarQuantity` class provides a representation of an array of
physical quantity values, with optional units and uncertainty information.
The attributes are:
=================== ========================================================
Attribute Description
=================== ========================================================
`value` The numeric value of the quantity in the given units
`units` The units the value was specified in
`uncertainty` The numeric uncertainty in the value
`uncertaintyType` The type of uncertainty: ``'+|-'`` for additive, ``'*|/'`` for multiplicative
`value_si` The numeric value of the quantity in the corresponding SI units
=================== ========================================================
It is often more convenient to perform computations using SI units instead
of the given units of the quantity. For this reason, the SI equivalent of
the `value` attribute can be directly accessed using the `value_si`
attribute. This value is cached on the :class:`ArrayQuantity` object for
speed.
"""
def __init__(self, value, units='', uncertainty=None, uncertaintyType='+|-'):
Units.__init__(self, units)
self.value = value
self.uncertaintyType = uncertaintyType
if uncertainty is None:
self.uncertainty = numpy.zeros_like(self.value)
elif isinstance(uncertainty, (int,float)):
self.uncertainty = numpy.ones_like(self.value) * uncertainty
else:
uncertainty = numpy.array(uncertainty)
if uncertainty.ndim != self.value.ndim:
raise QuantityError('The given uncertainty has {0:d} dimensions, while the given value has {1:d} dimensions.'.format(uncertainty.ndim, self.value.ndim))
for i in range(self.value.ndim):
if self.value.shape[i] != uncertainty.shape[i]:
raise QuantityError('Dimension {0:d} has {1:d} elements for the given value, but {2:d} elements for the given uncertainty.'.format(i, self.value.shape[i], uncertainty.shape[i]))
else:
self.uncertainty = uncertainty
def __reduce__(self):
"""
Return a tuple of information used to pickle the array quantity.
"""
return (ArrayQuantity, (self.value, self.units, self.uncertainty, self.uncertaintyType))
def __str__(self):
"""
Return a string representation of the array quantity.
"""
if self.value.ndim == 1:
value = '[{0}]'.format(','.join(['{0:g}'.format(float(v)) for v in self.value]))
elif self.value.ndim == 2:
value = []
for i in range(self.value.shape[0]):
value.append('[{0}]'.format(','.join(['{0:g}'.format(float(self.value[i,j])) for j in range(self.value.shape[1])])))
value = '[{0}]'.format(','.join(value))
if self.uncertainty.ndim == 1:
uncertainty = '[{0}]'.format(','.join(['{0:g}'.format(float(v)) for v in self.uncertainty]))
elif self.uncertainty.ndim == 2:
uncertainty = []
for i in range(self.uncertainty.shape[0]):
uncertainty.append('[{0}]'.format(','.join(['{0:g}'.format(float(self.uncertainty[i,j])) for j in range(self.uncertainty.shape[1])])))
uncertainty = '[{0}]'.format(','.join(uncertainty))
result = '{0}'.format(value)
if any(self.uncertainty != 0.0):
result += ' {0} {1}'.format(self.uncertaintyType, uncertainty)
if self.units != '':
result += ' {0}'.format(self.units)
return result
def __repr__(self):
"""
Return a string representation that can be used to reconstruct the
array quantity.
"""
if self.value.ndim == 1:
value = '[{0}]'.format(','.join(['{0:g}'.format(float(v)) for v in self.value]))
elif self.value.ndim == 2:
value = []
for i in range(self.value.shape[0]):
value.append('[{0}]'.format(','.join(['{0:g}'.format(float(self.value[i,j])) for j in range(self.value.shape[1])])))
value = '[{0}]'.format(','.join(value))
if self.uncertainty.ndim == 1:
uncertainty = '[{0}]'.format(','.join(['{0:g}'.format(float(v)) for v in self.uncertainty]))
elif self.uncertainty.ndim == 2:
uncertainty = []
for i in range(self.uncertainty.shape[0]):
uncertainty.append('[{0}]'.format(','.join(['{0:g}'.format(float(self.uncertainty[i,j])) for j in range(self.uncertainty.shape[1])])))
uncertainty = '[{0}]'.format(','.join(uncertainty))
if self.units == '' and not numpy.any(self.uncertainty != 0.0):
return '{0}'.format(value)
else:
result = '({0},{1!r}'.format(value, self.units)
if numpy.any(self.uncertainty != 0.0):
result += ',{0!r},{1}'.format(self.uncertaintyType, uncertainty)
result += ')'
return result
def copy(self):
"""
Return a copy of the quantity.
"""
return ArrayQuantity(self.value.copy(), self.units, self.uncertainty.copy(), self.uncertaintyType)
def getValue(self):
return self.value_si * self.getConversionFactorFromSI()
def setValue(self, v):
self.value_si = numpy.array(v) * self.getConversionFactorToSI()
value = property(getValue, setValue)
def equals(self, quantity):
"""
Return ``True`` if the everything in a quantity object matches
the parameters in this object. If there are lists of values or uncertainties,
each item in the list must be matching and in the same order.
Otherwise, return ``False``
(Originally intended to return warning if units capitalization was
different, however, Quantity object only parses units matching in case, so
this will not be a problem.)
"""
def approx_equal(x, y, atol = .01):
"""
Returns true if two float/double values are approximately equal
within a relative error of 1% or under a user specific absolute tolerance.
"""
return abs(x-y) <= 1e-2*abs(x) or abs(x-y) <= 1e-2*abs(y) or abs(x-y) <= atol
if isinstance(quantity, ArrayQuantity):
if (self.uncertaintyType == quantity.uncertaintyType and self.units == quantity.units):
if self.units == "kcal/mol":
# set absolute tolerance to .01 kcal/mol = 42 J/mol
atol = 42
else:
# for other units, set it to .01
atol = .01
if self.value.ndim != quantity.value.ndim:
return False
for i in range(self.value.ndim):
if self.value.shape[i] != quantity.value.shape[i]:
return False
for v1, v2 in zip(self.value.flat, quantity.value.flat):
if not approx_equal(v1, v2, atol):
return False
if self.uncertainty.ndim != quantity.uncertainty.ndim:
return False
for i in range(self.uncertainty.ndim):
if self.uncertainty.shape[i] != quantity.uncertainty.shape[i]:
return False
for v1, v2 in zip(self.uncertainty.flat, quantity.uncertainty.flat):
if not approx_equal(v1, v2, atol):
return False
return True
return False
def isUncertaintyAdditive(self):
"""
Return ``True`` if the uncertainty is specified in additive format
and ``False`` otherwise.
"""
return self.uncertaintyType == '+|-'
def isUncertaintyMultiplicative(self):
"""
Return ``True`` if the uncertainty is specified in multiplicative
format and ``False`` otherwise.
"""
return self.uncertaintyType == '*|/'
################################################################################
def Quantity(*args, **kwargs):
"""
Create a :class:`ScalarQuantity` or :class:`ArrayQuantity` object for a
given physical quantity. The physical quantity can be specified in several
ways:
* A scalar-like or array-like value (for a dimensionless quantity)
* An array of arguments (including keyword arguments) giving some or all of
the `value`, `units`, `uncertainty`, and/or `uncertaintyType`.
* A tuple of the form ``(value,)``, ``(value,units)``,
``(value,units,uncertainty)``, or
``(value,units,uncertaintyType,uncertainty)``
* An existing :class:`ScalarQuantity` or :class:`ArrayQuantity` object, for
which a copy is made
"""
# Initialize attributes
value = None
units = ''
uncertaintyType = '+|-'
uncertainty = None
if len(args) == 1 and len(kwargs) == 0 and args[0] is None:
return None
# Unpack args if necessary
if isinstance(args, tuple) and len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
# Process args
Nargs = len(args)
if Nargs == 1 and isinstance(args[0], (ScalarQuantity,ArrayQuantity)):
# We were given another quantity object, so make a (shallow) copy of it
other = args[0]
value = other.value
units = other.units
uncertaintyType = other.uncertaintyType
uncertainty = other.uncertainty
elif Nargs == 1:
# If one parameter is given, it should be a single value
value, = args
elif Nargs == 2:
# If two parameters are given, it should be a value and units
value, units = args
elif Nargs == 3:
# If three parameters are given, it should be a value, units and uncertainty
value, units, uncertainty = args
elif Nargs == 4:
# If four parameters are given, it should be a value, units, uncertainty type, and uncertainty
value, units, uncertaintyType, uncertainty = args
elif Nargs != 0:
raise QuantityError('Invalid parameters {0!r} passed to ArrayQuantity.__init__() method.'.format(args))
# Process kwargs
for k, v in kwargs.items():
if k == 'value':
if len(args) >= 1:
raise QuantityError('Multiple values for argument {0} passed to ArrayQuantity.__init__() method.'.format(k))
else:
value = v
elif k == 'units':
if len(args) >= 2:
raise QuantityError('Multiple values for argument {0} passed to ArrayQuantity.__init__() method.'.format(k))
else:
units = v
elif k == 'uncertainty':
if len(args) >= 3:
raise QuantityError('Multiple values for argument {0} passed to ArrayQuantity.__init__() method.'.format(k))
else:
uncertainty = v
elif k == 'uncertaintyType':
if len(args) >= 4:
raise QuantityError('Multiple values for argument {0} passed to ArrayQuantity.__init__() method.'.format(k))
else:
uncertaintyType = v
else:
raise QuantityError('Invalid keyword argument {0} passed to ArrayQuantity.__init__() method.'.format(k))
# Process units and uncertainty type parameters
if uncertaintyType not in ['+|-', '*|/']:
raise QuantityError('Unexpected uncertainty type "{0}"; valid values are "+|-" and "*|/".'.format(uncertaintyType))
if isinstance(value, (list,tuple,numpy.ndarray)):
return ArrayQuantity(value, units, uncertainty, uncertaintyType)
try:
value = float(value)
except TypeError:
return ArrayQuantity(value, units, uncertainty, uncertaintyType)
uncertainty = 0.0 if uncertainty is None else float(uncertainty)
return ScalarQuantity(value, units, uncertainty, uncertaintyType)
################################################################################
class UnitType:
"""
The :class:`UnitType` class represents a factory for producing
:class:`ScalarQuantity` or :class:`ArrayQuantity` objects of a given unit
type, e.g. time, volume, etc.
"""
def __init__(self, units, commonUnits=None, extraDimensionality=None):
self.units = units
self.dimensionality = pq.Quantity(1.0, units).simplified.dimensionality
self.commonUnits = commonUnits or []
self.extraDimensionality = {}
if extraDimensionality:
for unit, factor in extraDimensionality.items():
self.extraDimensionality[pq.Quantity(1.0, unit).simplified.dimensionality] = factor
def __call__(self, *args, **kwargs):
# Make a ScalarQuantity or ArrayQuantity object out of the given parameter
quantity = Quantity(*args, **kwargs)
if quantity is None:
return quantity
units = quantity.units
# If the units are in the common units, then we can do the conversion
# very quickly and avoid the slow calls to the quantities package
if units == self.units or units in self.commonUnits:
return quantity
# Check that the units are consistent with this unit type
# This uses the quantities package (slow!)
units = pq.Quantity(1.0, units)
dimensionality = units.simplified.dimensionality
if dimensionality == self.dimensionality:
pass
elif dimensionality in self.extraDimensionality:
quantity.value_si *= self.extraDimensionality[dimensionality]
quantity.units = self.units
else:
raise QuantityError('Invalid units {0!r}.'.format(quantity.units))
# Return the Quantity or ArrayQuantity object object
return quantity
Acceleration = UnitType('m/s^2')
Area = UnitType('m^2')
Concentration = UnitType('mol/m^3')
Dimensionless = UnitType('')
DipoleMoment = UnitType('C*m', extraDimensionality={
'De': 1.0 / (1.0e21 * constants.c),
})
Energy = Enthalpy = FreeEnergy = UnitType('J/mol', commonUnits=['kJ/mol', 'cal/mol', 'kcal/mol'])
Entropy = HeatCapacity = UnitType('J/(mol*K)', commonUnits=['kJ/(mol*K)', 'cal/(mol*K)', 'kcal/(mol*K)'])
Flux = UnitType('mol/(m^2*s)')
Frequency = UnitType('cm^-1', extraDimensionality={
's^-1': 1.0 / (constants.c * 100.),
'Hz': 1.0 / (constants.c * 100.),
'J': 1.0 / (constants.h * constants.c * 100.),
'K': constants.kB / (constants.h * constants.c * 100.),
})
Force = UnitType('N')
Inertia = UnitType('kg*m^2')
Length = UnitType('m')
Mass = UnitType('amu', extraDimensionality={'kg/mol': 1000.*constants.amu})
Momentum = UnitType('kg*m/s^2')
Power = UnitType('W')
Pressure = UnitType('Pa', commonUnits=['bar', 'atm', 'torr', 'psi', 'mbar'])
Temperature = UnitType('K', commonUnits=['degC', 'degF', 'degR'])
Time = UnitType('s')
Velocity = UnitType('m/s')
Volume = UnitType('m^3')
# RateCoefficient is handled as a special case since it can take various
# units depending on the reaction order
RATECOEFFICIENT_CONVERSION_FACTORS = {
(1.0/pq.s).dimensionality: 1.0,
(pq.m**3/pq.s).dimensionality: 1.0,
(pq.m**6/pq.s).dimensionality: 1.0,
(pq.m**9/pq.s).dimensionality: 1.0,
(pq.m**3/(pq.mol*pq.s)).dimensionality: 1.0,
(pq.m**6/(pq.mol**2*pq.s)).dimensionality: 1.0,
(pq.m**9/(pq.mol**3*pq.s)).dimensionality: 1.0,
}
RATECOEFFICIENT_COMMON_UNITS = ['s^-1', 'm^3/(mol*s)', 'cm^3/(mol*s)', 'm^3/(molecule*s)', 'cm^3/(molecule*s)']
def RateCoefficient(*args, **kwargs):
# Make a ScalarQuantity or ArrayQuantity object out of the given parameter
quantity = Quantity(*args, **kwargs)
if quantity is None:
return quantity
units = quantity.units
# If the units are in the common units, then we can do the conversion
# very quickly and avoid the slow calls to the quantities package
if units in RATECOEFFICIENT_COMMON_UNITS:
return quantity
dimensionality = pq.Quantity(1.0, quantity.units).simplified.dimensionality
try:
factor = RATECOEFFICIENT_CONVERSION_FACTORS[dimensionality]
quantity.value_si *= factor
except KeyError:
raise QuantityError('Invalid units {0!r}.'.format(quantity.units))
# Return the Quantity or ArrayQuantity object object
return quantity