[yt-svn] commit/yt: MatthewTurk: Merged in brittonsmith/yt/yt-3.0 (pull request #803)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Apr 14 06:13:03 PDT 2014


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/6f790e40d53b/
Changeset:   6f790e40d53b
Branch:      yt-3.0
User:        MatthewTurk
Date:        2014-04-14 15:12:46
Summary:     Merged in brittonsmith/yt/yt-3.0 (pull request #803)

Adding nuclei density, number density, and interpolated fields
Affected #:  6 files

diff -r 87fe2e111e03663c6da6948be519c390ebd831d8 -r 6f790e40d53b5c3274215f9e2f4f09fa1157b82c yt/fields/field_info_container.py
--- a/yt/fields/field_info_container.py
+++ b/yt/fields/field_info_container.py
@@ -61,6 +61,7 @@
         self.field_list = field_list
         self.slice_info = slice_info
         self.field_aliases = {}
+        self.species_names = []
         self.setup_fluid_aliases()
 
     def setup_fluid_fields(self):

diff -r 87fe2e111e03663c6da6948be519c390ebd831d8 -r 6f790e40d53b5c3274215f9e2f4f09fa1157b82c yt/fields/fluid_fields.py
--- a/yt/fields/fluid_fields.py
+++ b/yt/fields/fluid_fields.py
@@ -162,12 +162,22 @@
     registry.add_field((ftype, "metal_mass"),
                        function=_metal_mass,
                        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]
+        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"),
               function=_mean_molecular_weight,
-              units=r"")
+              units="")
 
     setup_gradient_fields(registry, (ftype, "pressure"), "dyne/cm**2",
                           slice_info)

diff -r 87fe2e111e03663c6da6948be519c390ebd831d8 -r 6f790e40d53b5c3274215f9e2f4f09fa1157b82c yt/fields/interpolated_fields.py
--- /dev/null
+++ b/yt/fields/interpolated_fields.py
@@ -0,0 +1,49 @@
+"""
+Fields from interpolating data tables.
+
+
+
+"""
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2014, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+
+import numpy as np
+
+from yt.fields.local_fields import add_field
+
+from yt.utilities.linear_interpolators import \
+    UnilinearFieldInterpolator, \
+    BilinearFieldInterpolator, \
+    TrilinearFieldInterpolator
+
+_int_class = {1: UnilinearFieldInterpolator,
+              2: BilinearFieldInterpolator,
+              3: TrilinearFieldInterpolator}
+
+def add_interpolated_field(name, units, table_data, axes_data, axes_fields,
+                           ftype="gas", particle_type=False, validators=None,
+                           truncate=True):
+    
+    if len(table_data.shape) not in _int_class:
+        raise RuntimeError("Interpolated field can only be created from 1d, 2d, or 3d data.")
+
+    if len(axes_fields) != len(axes_data) or len(axes_fields) != len(table_data.shape):
+        raise RuntimeError("Data dimension mismatch: data is %d, %d axes data provided, and %d axes fields provided." %
+                           (len(table_data.shape), len(axes_data), len(axes_fields)))
+
+    int_class = _int_class[len(table_data.shape)]
+    my_interpolator = int_class(table_data, axes_data, axes_fields, truncate=truncate)
+
+    def _interpolated_field(field, data):
+        return my_interpolator(data)
+    add_field((ftype, name), 
+              function=_interpolated_field,
+              units=units,
+              validators=validators, particle_type=particle_type)
+              

diff -r 87fe2e111e03663c6da6948be519c390ebd831d8 -r 6f790e40d53b5c3274215f9e2f4f09fa1157b82c yt/fields/species_fields.py
--- a/yt/fields/species_fields.py
+++ b/yt/fields/species_fields.py
@@ -14,15 +14,22 @@
 #-----------------------------------------------------------------------------
 
 import numpy as np
+import re
 
 from yt.utilities.physical_constants import \
     mh, \
     mass_sun_cgs, \
     amu_cgs
+from yt.utilities.physical_ratios import \
+    primordial_H_mass_fraction
 from yt.funcs import *
 from yt.utilities.chemical_formulas import \
     ChemicalFormula
 
+_primordial_mass_fraction = \
+  {"H": primordial_H_mass_fraction,
+   "He" : (1 - primordial_H_mass_fraction)}
+    
 # See YTEP-0003 for details, but we want to ensure these fields are all
 # populated:
 #
@@ -50,7 +57,7 @@
     weight *= amu_cgs
     def _number_density(field, data):
         return data[ftype, "%s_density" % species] \
-             / amu_cgs
+             / weight
     return _number_density
 
 def _create_density_func(ftype, species):
@@ -102,3 +109,53 @@
                        function = _create_number_density_func(ftype, species),
                        particle_type = particle_type,
                        units = "cm**-3")
+
+def add_nuclei_density_fields(registry, ftype,
+                              particle_type = False):
+    elements = _get_all_elements(registry.species_names)
+    for element in elements:
+        registry.add_field((ftype, "%s_nuclei_density" % element),
+                           function = _nuclei_density,
+                           particle_type = particle_type,
+                           units = "cm**-3")
+    if len(elements) == 0:
+        for element in ["H", "He"]:
+            registry.add_field((ftype, "%s_nuclei_density" % element),
+                               function = _default_nuclei_density,
+                               particle_type = particle_type,
+                               units = "cm**-3")
+
+def _default_nuclei_density(field, data):
+    element = field.name[1][:field.name[1].find("_")]
+    return data["gas", "density"] * _primordial_mass_fraction[element] / \
+      ChemicalFormula(element).weight / amu_cgs
+        
+def _nuclei_density(field, data):
+    element = field.name[1][:field.name[1].find("_")]
+    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:
+        nucleus = species
+        if "_" in species:
+            nucleus = species[:species.find("_")]
+        num = _get_element_multiple(nucleus, element)
+        field_data += num * data["gas", "%s_number_density" % species]
+    return field_data
+
+def _get_all_elements(species_list):
+    elements = []
+    for species in species_list:
+        for item in re.findall('[A-Z][a-z]?|[0-9]+', species):
+            if not item.isdigit() and item not in elements \
+              and item != "El":
+                elements.append(item)
+    return elements
+    
+def _get_element_multiple(compound, element):
+    my_split = re.findall('[A-Z][a-z]?|[0-9]+', compound)
+    if element not in my_split:
+        return 0
+    loc = my_split.index(element)
+    if loc == len(my_split) - 1 or not my_split[loc + 1].isdigit():
+        return 1
+    return int(my_split[loc + 1])

diff -r 87fe2e111e03663c6da6948be519c390ebd831d8 -r 6f790e40d53b5c3274215f9e2f4f09fa1157b82c yt/frontends/enzo/fields.py
--- a/yt/frontends/enzo/fields.py
+++ b/yt/frontends/enzo/fields.py
@@ -21,6 +21,7 @@
 from yt.units.yt_array import \
     YTArray
 from yt.fields.species_fields import \
+    add_nuclei_density_fields, \
     add_species_field_by_density
 from yt.utilities.physical_constants import \
     mh, me, mp, \
@@ -42,7 +43,7 @@
     'HM'      : 'H_m1',
     'DI'      : 'D',
     'DII'     : 'D_p1',
-    'HD'      : 'HD',
+    'HDI'     : 'HD',
     'Electron': 'El'
 }
 
@@ -119,8 +120,10 @@
                            take_log=True,
                            units="code_mass/code_length**3")
         yt_name = known_species_names[species]
-        self.alias(("gas", "%s_density" % yt_name),
-                   ("enzo", "%s_Density" % species))
+        # don't alias electron density since mass is wrong
+        if species != "Electron":
+            self.alias(("gas", "%s_density" % yt_name),
+                       ("enzo", "%s_Density" % species))
         add_species_field_by_density(self, "gas", yt_name)
 
     def setup_species_fields(self):
@@ -135,7 +138,8 @@
                        units = "g/cm**3")
         for sp in species_names:
             self.add_species_field(sp)
-
+            self.species_names.append(known_species_names[sp])
+        add_nuclei_density_fields(self, "gas")
 
     def setup_fluid_fields(self):
         # Now we conditionally load a few other things.

diff -r 87fe2e111e03663c6da6948be519c390ebd831d8 -r 6f790e40d53b5c3274215f9e2f4f09fa1157b82c yt/utilities/physical_ratios.py
--- a/yt/utilities/physical_ratios.py
+++ b/yt/utilities/physical_ratios.py
@@ -94,6 +94,7 @@
 jansky_cgs = 1.0e-23
 # Cosmological constants
 rho_crit_g_cm3_h2 = 1.8788e-29
+primordial_H_mass_fraction = 0.76
 
 # Misc. Approximations
 mass_mean_atomic_cosmology = 1.22

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list