Repository: yt-project/yt

Issue:
Random behavior of covering grid with derived field
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

This bug appears using Python 3. Python 2 does not have this problem. With Python 3, the output is not same every time and error message is in random order.

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

The file lasslab_hdf5_plt_cnt_0003 is in [http://use.yt/upload/2f1a02f0](url)

```bash
mpirun -np 2 python test.py
```
 test.py
```python
import numpy as np
import yt
from yt import derived_field
yt.enable_parallelism()

filename = 'lasslab_hdf5_plt_cnt_0003'
w = {'targ': 1.0, 'cham': 2.0}
ds = yt.load(filename)

def _var1(field, data):
    return data['dens'] * np.sum([data[var]*w[var] for var in ['targ', 'cham']], axis=0)
ds.add_field('var1', function=_var1, units='code_mass/code_length**3')

cg = ds.covering_grid(level=ds.max_level, left_edge=ds.domain_left_edge, \
                      dims=ds.domain_dimensions*2**ds.max_level)
print(cg['var1'][0, 0, 0])
```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

First time
```
1.0000385373132303e-06 code_mass/code_length**3
2.0000770746264607e-06 code_mass/code_length**3
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
```

Second time
```
0.0 code_mass/code_length**3
2.0000770746264607e-06 code_mass/code_length**3
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
```

Third time
```
2.0000770746264607e-06 code_mass/code_length**3
1.0000385373132303e-06 code_mass/code_length**3
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
```

And so on...

**Expected outcome**
This is the outcome in python2. Two processors should give same digits.
```
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
2.0000770746264607e-06 code_mass/code_length**3
2.0000770746264607e-06 code_mass/code_length**3
```

<!--A description of the expected outcome from the code snippet-->
<!--If this used to work in an earlier version of yt, please note the
version it used to work on-->

**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: Linux, openmpi
  * Python Version: python3
  * yt version: 3.5.dev0
  * Other Libraries (if applicable): h5py(parallel)

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->


Random behavior of covering grid with derived field
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

This bug appears using Python 3. Python 2 does not have this problem. With Python 3, the output is not same every time and error message is in random order.

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

The file lasslab_hdf5_plt_cnt_0003 is in [http://use.yt/upload/2f1a02f0](url)

```bash
mpirun -np 2 python test.py
```
 test.py
```python
import numpy as np
import yt
from yt import derived_field
yt.enable_parallelism()

filename = 'lasslab_hdf5_plt_cnt_0003'
w = {'targ': 1.0, 'cham': 2.0}
ds = yt.load(filename)

def _var1(field, data):
    return data['dens'] * np.sum([data[var]*w[var] for var in ['targ', 'cham']], axis=0)
ds.add_field('var1', function=_var1, units='code_mass/code_length**3')

cg = ds.covering_grid(level=ds.max_level, left_edge=ds.domain_left_edge, \
                      dims=ds.domain_dimensions*2**ds.max_level)
print(cg['var1'][0, 0, 0])
```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

First time
```
1.0000385373132303e-06 code_mass/code_length**3
2.0000770746264607e-06 code_mass/code_length**3
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
```

Second time
```
0.0 code_mass/code_length**3
2.0000770746264607e-06 code_mass/code_length**3
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
```

Third time
```
2.0000770746264607e-06 code_mass/code_length**3
1.0000385373132303e-06 code_mass/code_length**3
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
/home/yingchao/apps/mypython/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
```

And so on...

**Expected outcome**
This is the outcome in python2. Two processors should give same digits.
```
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
test.py:12: UserWarning: Because 'sampling_type' not specified, yt will assume a cell 'sampling_type'
  ds.add_field('var1', function=_var1, units='code_mass/code_length**3')
2.0000770746264607e-06 code_mass/code_length**3
2.0000770746264607e-06 code_mass/code_length**3
```

<!--A description of the expected outcome from the code snippet-->
<!--If this used to work in an earlier version of yt, please note the
version it used to work on-->

**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: Linux, openmpi
  * Python Version: python3
  * yt version: 3.5.dev0
  * Other Libraries (if applicable): h5py(parallel)

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->




Discussion:
Thanks for the report! I'll take a look at this.
Thanks for the report! I'll take a look at this.

Resolving patch:
diff --git a/yt/data_objects/data_containers.py b/yt/data_objects/data_containers.py
--- a/yt/data_objects/data_containers.py
+++ b/yt/data_objects/data_containers.py
@@ -1262,7 +1262,7 @@ def _identify_dependencies(self, fields_to_get, spatial = False):
             requested = self._determine_fields(list(set(fd.requested)))
             deps = [d for d in requested if d not in fields_to_get]
             fields_to_get += deps
-        return fields_to_get
+        return sorted(fields_to_get)
 
     def get_data(self, fields=None):
         if self._current_chunk is None:


Test patch: