# Comparing units from different datasetsΒΆ

Notebook

Units that refer to the internal simulation coordinate system will have different CGS conversion factors in different datasets. Depending on how a unit system is implemented, this could add an element of uncertainty when we compare dimensional array instances produced by different unit systems. Fortunately, this is not a problem for YTArray since all YTArray unit systems are defined in terms of physical CGS units.

As an example, let's load up two enzo datasets from different redshifts in the same cosmology simulation.

In [1]:
# A high redshift output from z ~ 8
import yt

print ("z = %s" % ds1.current_redshift)
print ("Internal length units = %s" % ds1.length_unit)
print ("Internal length units in cgs = %s" % ds1.length_unit.in_cgs())

z = 7.8843748886903
Internal length units = 128.0 Mpccm/h
Internal length units in cgs = 6.261455380881711e+25 cm

In [2]:
# A low redshift output from z ~ 0
print ("z = %s" % ds2.current_redshift)
print ("Internal length units = %s" % ds2.length_unit)
print ("Internal length units in cgs = %s" % ds2.length_unit.in_cgs())

z = 0.0013930880640796
Internal length units = 128.0 Mpccm/h
Internal length units in cgs = 5.5551728502644205e+26 cm


Given that these are from the same simulation in comoving units, the CGS length units are different by a factor of $(1+z_1)/(1+z_2)$:

In [3]:
print (ds2.length_unit.in_cgs()/ds1.length_unit.in_cgs() == (1+ds1.current_redshift)/(1+ds2.current_redshift))

True


It's not necessary to convert to CGS units either. yt will automatically account for the fact that a comoving megapaersec in the first output is physically different compared to a comoving megaparsec in the second output.

In [4]:
print (ds2.length_unit/ds1.length_unit)

8.872015389946235 dimensionless


Time series analysis is also straightforward. Since dimensional arrays and quantities carry around the conversion factors to CGS with them, we can safely pickle them, share them with other processors, or combine them without worrying about differences in unit definitions.

The following snippet, which iterates over a time series and saves the length_unit quantity to a storage dictionary. This should work correctly on one core or in a script run in parallel.

In [5]:
import yt
yt.enable_parallelism()

storage = {}

for sto, ds in ts.piter(storage=storage):
sto.result_id = float(ds.current_time.in_units('Gyr'))
sto.result = ds.length_unit

if yt.is_root():
for t in sorted(storage.keys()):
print (t, storage[t].in_units('Mpc'))

0.01676643071764393 1.8028169014084512 Mpc
0.3247763656792508 13.004379088331369 Mpc
0.6327904651859197 20.291994923620514 Mpc
0.9408035684815069 26.44507654199263 Mpc
1.2488152580528895 31.960551836194494 Mpc
1.5568305154147037 37.05004845073869 Mpc
1.8648412666457554 41.828976818284 Mpc
2.1728577331772887 46.36965467235576 Mpc
2.4808665104589602 50.72098300904238 Mpc
2.7888813002589496 54.91863310930029 Mpc
3.0968974445479778 58.98934575559256 Mpc
3.404905833522964 62.95398325137115 Mpc
3.7129179176792175 66.82957833722122 Mpc
4.020936765137611 70.6300703626599 Mpc
4.328947761986685 74.36695572216632 Mpc
4.636958798244155 78.05030128157921 Mpc
4.944969874821948 81.68874111248732 Mpc
5.252992034782318 85.28997665885913 Mpc
5.560995160120317 88.86037373217331 Mpc
5.869017156601832 92.40638871436397 Mpc
6.177030507189327 95.93314172723768 Mpc
6.485043781639327 99.44568665753908 Mpc
6.793056315509558 102.94856392171005 Mpc
7.101068563649808 106.44598125568383 Mpc
7.409081625664567 109.94185847103901 Mpc
7.717094733060914 113.43984038632928 Mpc
8.025097887021522 116.94324731677312 Mpc
8.333120584403355 120.45565879393497 Mpc
8.64113174988434 123.97979836398135 Mpc
8.949144573439849 127.51874858543704 Mpc
9.257158283960294 131.0753054467177 Mpc
9.565162728522733 134.65204334030335 Mpc
9.87318529299373 138.25187486919478 Mpc
10.181195510521588 141.87695100123045 Mpc
10.489207393535791 145.52986468697884 Mpc
10.797218723973884 149.21295107449208 Mpc
11.105233497957824 152.9285596811627 Mpc
11.413245940639204 156.6788693809309 Mpc
11.721259686169418 160.46612604443848 Mpc
12.029270109257261 164.29243333243653 Mpc
12.337283103026223 168.15998952325535 Mpc
12.645293686895343 172.0708262802806 Mpc
12.953319046939772 176.02723200663078 Mpc
13.2613311241452 180.03089125507208 Mpc