[yt-dev] Issue #830: Number density is only defined for multispecies datasets (yt_analysis/yt)

Nathan Goldbaum issues-reply at bitbucket.org
Sun Apr 20 20:04:03 PDT 2014


New issue 830: Number density is only defined for multispecies datasets
https://bitbucket.org/yt_analysis/yt/issue/830/number-density-is-only-defined-for

Nathan Goldbaum:

Right now querying the `number_density` field raises an IndexError in the field definition for a single fluid/color dataset.

The following diff fixes the issue for me and my enzo datasets, but clearly isn't the correct solution for all supported codes:


```
#!diff

diff -r 71196d4289bf yt/fields/fluid_fields.py
--- a/yt/fields/fluid_fields.py Sat Apr 19 20:14:16 2014 -0400
+++ b/yt/fields/fluid_fields.py Sun Apr 20 19:58:45 2014 -0700
@@ -164,15 +164,18 @@
                        units="g")

     def _number_density(field, data):
-        field_data = np.zeros_like(data["gas", "%s_number_density" % \
-                                        data.pf.field_info.species_names[0]])
-        for species in data.pf.field_info.species_names:
-            field_data += data["gas", "%s_number_density" % species]
+        try:
+            field_data = np.zeros_like(data["gas", "%s_number_density" % \
+                                            data.pf.field_info.species_names[0]])
+            for species in data.pf.field_info.species_names:
+                field_data += data["gas", "%s_number_density" % species]
+        except IndexError:
+            field_data = data["gas", "density"] / (mh * data.pf.parameters['Mu'])
         return field_data
     registry.add_field((ftype, "number_density"),
                        function = _number_density,
                        units="cm**-3")
-
+
     def _mean_molecular_weight(field, data):
         return (data[ftype, "density"] / (mh * data[ftype, "number_density"]))
     registry.add_field((ftype, "mean_molecular_weight"),
```

Do we have some way of signaling whether a dataset is multispecies? I could see modifying the `setup_species_fields` function in the enzo frontend to handle this for the case `ds.parameters['MultiSpecies'] == 0`, but I don't think there is yet a frontend-independent way to handle the mean molecular weight.

@brittonsmith @MatthewTurk what do you guys think?





More information about the yt-dev mailing list