Beispiel #1
0
def cooling_rate(field, data, my_chemistry):

    fc = FluidContainer(my_chemistry, data["PartType0", "Masses"].shape[0])

    fc["density"][:] = data["PartType0", "Density"]
    fc["metal"][:] = data["PartType0", "Density"] * data["PartType0",
                                                         "Metallicity"]
    fc["x-velocity"][:] = data["PartType0", "Velocities"][:, 0]
    fc["y-velocity"][:] = data["PartType0", "Velocities"][:, 1]
    fc["z-velocity"][:] = data["PartType0", "Velocities"][:, 2]
    fc["energy"][:] = data["PartType0", "InternalEnergy"]

    fc.calculate_temperature()
    fc.calculate_cooling_time()

    dt = yt.units.s
    fc.solve_chemistry(dt.value / my_chemistry.time_units)

    de = (data["PartType0", "InternalEnergy"].value - fc["energy"])
    dE = de * data["PartType0", "Masses"].value
    dE *= yt.units.Msun * yt.units.pc**2 / yt.units.Myr**2

    dE_dt = dE / dt

    return dE_dt
Beispiel #2
0
def temperature(field, data, my_chemistry):

    fc = FluidContainer(my_chemistry, data["PartType0", "Masses"].shape[0])

    fc["density"][:] = data["PartType0", "Density"]
    fc["metal"][:] = data["PartType0", "Density"] * data["PartType0",
                                                         "Metallicity"]
    fc["x-velocity"][:] = data["PartType0", "Velocities"][:, 0]
    fc["y-velocity"][:] = data["PartType0", "Velocities"][:, 1]
    fc["z-velocity"][:] = data["PartType0", "Velocities"][:, 2]
    fc["energy"][:] = data["PartType0", "InternalEnergy"]

    fc.calculate_temperature()

    return fc["temperature"] * yt.units.Kelvin
Beispiel #3
0
def prepare_model(cds, start_time, profile_index, fc=None):
    time_data = cds.data["time"]
    time_index = np.abs(time_data - start_time).argmin()

    if ('data', 'specific_thermal_energy') not in cds.field_list:
        cds.add_field(('data', 'specific_thermal_energy'),
                      sampling_type="local",
                      function=_specific_thermal_energy,
                      units="erg/g")

    efields = [
        "external_pressure", "dark_matter", "metallicity",
        "turbulent_velocity", "H2_p0_dissociation_rate",
        "H_p0_ionization_rate", "He_p0_ionization_rate",
        "He_p1_ionization_rate", "photo_gamma"
    ]
    external_data = {}

    field_list = [field for field in cds.field_list if field[1] != "time"]
    if ('data', 'specific_thermal_energy') not in field_list:
        field_list.append(('data', 'specific_thermal_energy'))
    field_data = dict((field, cds.data[field][time_index:, profile_index])
                      for field in field_list)

    if fc is None:
        fc = FluidContainer(cds.grackle_data, 1)

    fields = _get_needed_fields(fc.chemistry_data)
    for gfield in fields:
        yfield, units = _field_map[gfield]
        pfield = ("data", yfield[1])

        fc[gfield][:] = field_data[pfield][0].to(units)
        # get external data to be used in fluid container
        if pfield[1] in efields:
            efields.pop(efields.index(pfield[1]))
            external_data[gfield] = field_data[pfield].to(units).d

    # get extra solely external data fields
    for efield in efields:
        yfield, units = _field_map[efield]
        external_data[efield] = field_data[yfield].to(units).d

    if 'de' in fc:
        fc['de'] *= (mp / me).d

    field_data["time"] = time_data[time_index:] - time_data[time_index]
    external_data["time"] = field_data["time"].to("s").d / \
      fc.chemistry_data.time_units

    return fc, external_data, field_data
Beispiel #4
0
def _data_to_fc(data, size=None, fc=None):
    if size is None:
        size = data['gas', 'density'].size
    if fc is None:
        fc = FluidContainer(data.ds.grackle_data, size)

    flatten = len(data['gas', 'density'].shape) > 1

    fields = _get_needed_fields(fc.chemistry_data)
    for gfield in fields:
        yfield, units = _field_map[gfield]
        fdata = data[yfield].to(units)

        if flatten:
            fdata = fdata.flatten()
        fc[gfield][:] = fdata

    if 'de' in fc:
        fc['de'] *= (mp / me)

    return fc
Beispiel #5
0
        my_chemistry.density_units = mass_hydrogen_cgs  # rho = 1.0 is 1.67e-24 g
        my_chemistry.length_units = cm_per_mpc  # 1 Mpc in cm
        my_chemistry.time_units = sec_per_Myr  # 1 Myr in s
        my_chemistry.velocity_units = my_chemistry.a_units * \
            (my_chemistry.length_units / my_chemistry.a_value) / \
            my_chemistry.time_units

        # set initial density and temperature
        initial_temperature = 100.
        #initial_density   = 1.0e3/0.9 * mass_hydrogen_cgs
        initial_density = 1.e3 * mass_hydrogen_cgs
        final_time = 3.2e3  # 0.05 #1.e3 #1. # 1.e3 # 1.e3 # in Myr

        rval = my_chemistry.initialize()

        fc = FluidContainer(my_chemistry, 1)
        fc["density"][:] = initial_density / my_chemistry.density_units
        fc["HII"][:] = tiny_number * fc["density"]
        fc["HI"][:] = (0.65 - 4.9e-4 * metallicity[i] -
                       2.9e-4 * metallicity[i]) * fc["density"]
        fc["HeI"][:] = tiny_number * fc["density"]
        fc["HeII"][:] = tiny_number * fc["density"]
        fc["HeIII"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 1:
            fc["H2I"][:] = 0.30 * fc["density"]
            fc["H2II"][:] = tiny_number * fc["density"]
            fc["HM"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 2:
            fc["HDI"][:] = 1.e-9 * fc["density"]
            fc["DI"][:] = (3.e-5 - fc["HDI"][:]) * fc["density"]
            fc["DII"][:] = tiny_number * fc["density"]
Beispiel #6
0
        my_chemistry.a_units
    my_chemistry.density_units = mass_hydrogen_cgs  # rho = 1.0 is 1.67e-24 g
    my_chemistry.length_units = cm_per_mpc  # 1 Mpc in cm
    my_chemistry.time_units = sec_per_Myr  # 1 Myr in s
    my_chemistry.set_velocity_units()

    # set initial density and temperature
    initial_temperature = 50000.  # start the gas at this temperature
    # then begin collapse
    initial_density = 1.0e-1 * mass_hydrogen_cgs  # g / cm^3
    # stopping condition
    final_density = 1.e12 * mass_hydrogen_cgs

    rval = my_chemistry.initialize()

    fc = FluidContainer(my_chemistry, 1)
    fc["density"][:] = initial_density / my_chemistry.density_units
    fc["HI"][:] = 0.76 * fc["density"]
    fc["HII"][:] = tiny_number * 0.76 * fc["density"]
    fc["HeI"][:] = (1.0 - 0.76) * fc["density"]
    fc["HeII"][:] = tiny_number * fc["density"]
    fc["HeIII"][:] = tiny_number * fc["density"]
    fc["de"][:] = 2e-4 * mass_electron_cgs / mass_hydrogen_cgs * fc["density"]
    if my_chemistry.primordial_chemistry > 1:
        fc["H2I"][:] = tiny_number * fc["density"]
        fc["H2II"][:] = tiny_number * fc["density"]
        fc["HM"][:] = tiny_number * fc["density"]
    if my_chemistry.primordial_chemistry > 2:
        fc["DI"][:] = 2.0 * 3.4e-5 * fc["density"]
        fc["DII"][:] = tiny_number * fc["density"]
        fc["HDI"][:] = tiny_number * fc["density"]
Beispiel #7
0
    # Set units
    my_chemistry.comoving_coordinates = 0  # proper units
    my_chemistry.a_units = 1.0
    my_chemistry.a_value = 1. / (1. + current_redshift) / \
        my_chemistry.a_units
    my_chemistry.density_units = mass_hydrogen_cgs  # rho = 1.0 is 1.67e-24 g
    my_chemistry.length_units = cm_per_mpc  # 1 Mpc in cm
    my_chemistry.time_units = sec_per_Myr  # 1 Myr in s
    my_chemistry.velocity_units = my_chemistry.a_units * \
        (my_chemistry.length_units / my_chemistry.a_value) / \
        my_chemistry.time_units

    rval = my_chemistry.initialize()

    fc = FluidContainer(my_chemistry, 1)
    fc["density"][:] = density
    if my_chemistry.primordial_chemistry > 0:
        fc["HI"][:] = 0.76 * fc["density"]
        fc["HII"][:] = tiny_number * fc["density"]
        fc["HeI"][:] = (1.0 - 0.76) * fc["density"]
        fc["HeII"][:] = tiny_number * fc["density"]
        fc["HeIII"][:] = tiny_number * fc["density"]
    if my_chemistry.primordial_chemistry > 1:
        fc["H2I"][:] = tiny_number * fc["density"]
        fc["H2II"][:] = tiny_number * fc["density"]
        fc["HM"][:] = tiny_number * fc["density"]
        fc["de"][:] = tiny_number * fc["density"]
    if my_chemistry.primordial_chemistry > 2:
        fc["DI"][:] = 2.0 * 3.4e-5 * fc["density"]
        fc["DII"][:] = tiny_number * fc["density"]
Beispiel #8
0
def _calculate_cooling_metallicity(field, data, fc):
    gfields = _get_needed_fields(fc.chemistry_data)
    if field.name[1].endswith('tdt'):
        tdfield = 'total_dynamical_time'
    else:
        tdfield = 'dynamical_time'
    td = data['gas', tdfield].to('code_time').d
    flatten = len(td.shape) > 1
    if flatten:
        td = td.flatten()
    fc_mini = FluidContainer(data.ds.grackle_data, 1)

    fc.calculate_cooling_time()

    def cdrat(Z, my_td):
        fc_mini['metal'][:] = Z * fc_mini['density']
        fc_mini.calculate_cooling_time()
        return my_td + fc_mini['cooling_time'][0]

    field_data = data.ds.arr(np.zeros(td.size), '')
    if isinstance(data, FieldDetector):
        return field_data

    if field_data.size > 200000:
        my_str = "Reticulating splines"
        if ytcfg.getboolean("yt", "__parallel"):
            my_str = "P%03d %s" % \
                (ytcfg.getint("yt", "__global_parallel_rank"),
                 my_str)
        pbar = get_pbar(my_str, field_data.size, parallel=True)
    else:
        pbar = DummyProgressBar()
    for i in range(field_data.size):
        pbar.update(i)
        if td[i] + fc['cooling_time'][i] > 0:
            continue
        for mfield in gfields:
            fc_mini[mfield][:] = fc[mfield][i]
        success = False
        if i > 0 and field_data[i - 1] > 0:
            try:
                field_data[i] = brentq(cdrat,
                                       0.1 * field_data[i - 1],
                                       10 * field_data[i - 1],
                                       args=(td[i]),
                                       xtol=1e-6)
                success = True
            except:
                pass
        if not success:
            bds = np.logspace(-2, 2, 5)
            for bd in bds:
                try:
                    field_data[i] = brentq(cdrat,
                                           1e-6,
                                           bd,
                                           args=(td[i]),
                                           xtol=1e-6)
                    success = True
                    break
                except:
                    continue
            if not success:
                field_data[i] = np.nan
                # field_data[i] = 0. # hack for imaging
    pbar.finish()

    if flatten:
        field_data = field_data.reshape(data.ActiveDimensions)
    return field_data
Beispiel #9
0
def run_grackle(initial_temperature, density, final_time, safety_factor=1e-2):
    current_redshift = 0.
    tiny_number = 1.0e-20

    pygrackle.evolve_constant_density = _new_evolve_constant_density

    # Set solver parameters
    my_chemistry = chemistry_data()
    my_chemistry.three_body_rate = 4
    my_chemistry.use_grackle = 1
    my_chemistry.with_radiative_cooling = 0
    my_chemistry.primordial_chemistry = 2
    my_chemistry.metal_cooling = 0
    my_chemistry.UVbackground = 0
    my_chemistry.self_shielding_method = 0
    my_chemistry.H2_self_shielding = 0
    grackle_dir = os.path.dirname(
        os.path.dirname(
            os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
    my_chemistry.grackle_data_file = os.sep.join(
        [grackle_dir, "input", "CloudyData_UVB=HM2012.h5"])

    # Set units
    my_chemistry.comoving_coordinates = 0  # proper units
    my_chemistry.a_units = 1.0
    my_chemistry.three_body_rate = 4
    my_chemistry.a_value = 1. / (1. + current_redshift) / \
        my_chemistry.a_units
    my_chemistry.density_units = 1.66053904e-24  # 1u: amu # rho = 1.0 is 1.67e-24 g
    my_chemistry.length_units = 1.0  #  cm
    my_chemistry.time_units = 1.0  #  s
    my_chemistry.velocity_units = my_chemistry.a_units * \
        (my_chemistry.length_units / my_chemistry.a_value) / \
        my_chemistry.time_units

    rval = my_chemistry.initialize()

    fc = FluidContainer(my_chemistry, 1)
    fc["density"][:] = density

    tiny_number = 1.0e-20
    if my_chemistry.primordial_chemistry > 0:
        # these are in mass_density
        fc["HI"][:] = 0.76 * fc["density"] / 3.
        fc["HII"][:] = 0.76 * fc["density"] / 3.
        fc["HeI"][:] = (1 - 0.76) * fc["density"] / 2.
        fc["HeII"][:] = (1 - 0.76) * fc["density"] / 2.
        fc["HeIII"][:] = tiny_number

    if my_chemistry.primordial_chemistry > 1:

        fc["H2I"][:] = 0.76 * fc["density"] / 3.
        fc["H2II"][:] = tiny_number  #0.76 * fc["density"] /4.
        fc["HM"][:] = tiny_number  #0.76 * fc["density"] /4.

    if my_chemistry.primordial_chemistry > 2:
        fc["DI"][:] = 2.0 * 3.4e-5 * fc["density"]
        fc["DII"][:] = tiny_number * fc["density"]
        fc["HDI"][:] = tiny_number * fc["density"]
    if my_chemistry.metal_cooling == 1:
        fc["metal"][:] = 0.1 * fc["density"] * \
          my_chemistry.SolarMetalFractionByMass

    fc["x-velocity"][:] = 0.0
    fc["y-velocity"][:] = 0.0
    fc["z-velocity"][:] = 0.0

    fc["energy"][:] = initial_temperature / \
        fc.chemistry_data.temperature_units
    fc.calculate_temperature()
    fc["energy"][:] *= initial_temperature / fc["temperature"]

    # let gas cool at constant density
    data = pygrackle.evolve_constant_density(fc,
                                             final_time=final_time,
                                             safety_factor=safety_factor)

    return data
Beispiel #10
0
        my_chemistry.time_units = sec_per_Myr  # 1 Myr in s
        my_chemistry.velocity_units = my_chemistry.a_units * \
            (my_chemistry.length_units / my_chemistry.a_value) / \
            my_chemistry.time_units

        # set initial density and temperature
        initial_temperature = 300.
        #initial_temperature = 40000. # start the gas at this temperature
        # then begin collapse
        initial_density = 1.e-1 * mass_hydrogen_cgs  # g / cm^3
        # stopping condition
        final_density = 1.e10 * mass_hydrogen_cgs

        rval = my_chemistry.initialize()

        fc = FluidContainer(my_chemistry, 1)
        fc["density"][:] = initial_density / my_chemistry.density_units
        fc["HII"][:] = 1.e-4 * fc["density"]
        fc["HeI"][:] = 8.333e-2 * fc["density"]
        fc["HeII"][:] = tiny_number * fc["density"]
        fc["HeIII"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 1:
            fc["H2I"][:] = 1.e-6 * fc["density"]
            fc["H2II"][:] = tiny_number * fc["density"]
            fc["HM"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 2:
            fc["HDI"][:] = 1.e-9 * fc["density"]
            fc["DI"][:] = (3.e-5 - fc["HDI"][:]) * fc["density"]
            fc["DII"][:] = tiny_number * fc["density"]
        if my_chemistry.metal_cooling == 1:
            fc["metal"][:] = metallicity[i] * my_chemistry.SolarMetalFractionByMass * \
def run_grackle(density = 1e10, initial_temperature = 1e3, final_time = 1e5, safety_factor=1e-2):
    current_redshift = 0.
    tiny_number = 1.0e-20

    pygrackle.evolve_constant_density = _new_evolve_constant_density

    # Set solver parameters
    my_chemistry = chemistry_data()
    my_chemistry.three_body_rate = 4
    my_chemistry.use_grackle = 1
    my_chemistry.with_radiative_cooling = 1
    my_chemistry.primordial_chemistry = 2
    my_chemistry.metal_cooling = 0
    my_chemistry.UVbackground = 0
    my_chemistry.self_shielding_method = 0
    my_chemistry.H2_self_shielding = 0
    my_chemistry.cie_cooling = 0
    grackle_dir = "/home/kwoksun2/grackle"
    my_chemistry.grackle_data_file = os.sep.join(
        [grackle_dir, "input", "CloudyData_UVB=HM2012.h5"])

    # Set units
    my_chemistry.comoving_coordinates = 0 # proper units
    my_chemistry.a_units = 1.0
    my_chemistry.three_body_rate = 4
    my_chemistry.a_value = 1. / (1. + current_redshift) / \
        my_chemistry.a_units
    my_chemistry.density_units = 1.66053904e-24 # 1u: amu # rho = 1.0 is 1.67e-24 g
    my_chemistry.length_units = 1.0         #  cm
    my_chemistry.time_units = 1.0           #  s
    my_chemistry.velocity_units = my_chemistry.a_units * \
        (my_chemistry.length_units / my_chemistry.a_value) / \
        my_chemistry.time_units

    rval = my_chemistry.initialize()

    fc = FluidContainer(my_chemistry, 1)
    fc["density"][:] = density

    tiny_number = 1.0e-20
    if my_chemistry.primordial_chemistry > 0:
        # these are in mass_density
        fc["HI"][:] = 0.76 * fc["density"] /3.
        fc["HII"][:] =  0.76 * fc["density"] /3.
        fc["HeI"][:] =  (1 - 0.76) * fc["density"] /2.
        fc["HeII"][:] =  (1 - 0.76) * fc["density"] /2.
        fc["HeIII"][:] =  tiny_number

    if my_chemistry.primordial_chemistry > 1:

        fc["H2I"][:]  = 0.76 * fc["density"] /3.
        fc["H2II"][:] = tiny_number
        fc["HM"][:]   = tiny_number

        fc["de"][:] = fc["HII"][:] + fc["HeII"][:]/4. + fc["HeIII"][:]/4.*2.0

    if my_chemistry.primordial_chemistry > 2:
        fc["DI"][:] = 2.0 * 3.4e-5 * fc["density"]
        fc["DII"][:] = tiny_number * fc["density"]
        fc["HDI"][:] = tiny_number * fc["density"]
    if my_chemistry.metal_cooling == 1:
        fc["metal"][:] = 0.1 * fc["density"] * \
          my_chemistry.SolarMetalFractionByMass

    fc["x-velocity"][:] = 0.0
    fc["y-velocity"][:] = 0.0
    fc["z-velocity"][:] = 0.0

    fc["energy"][:] = initial_temperature / \
        fc.chemistry_data.temperature_units
    fc.calculate_temperature()
    fc["energy"][:] *= initial_temperature / fc["temperature"]

    tic = timeit.default_timer()
    # let gas cool at constant density
    data = pygrackle.evolve_constant_density(
        fc, final_time=final_time,
        safety_factor=safety_factor)
    toc = timeit.default_timer()
    run_time = toc - tic
    print("time lapsed", run_time)

    # convert grackle language to dengo language
    name_map_dict = { 'HI':'H_1',
                  'HII':'H_2',
                  'HeI':'He_1',
                  'HeII':'He_2',
                  'HeIII':'He_3',
                  'H2I' : 'H2_1',
                  'H2II': 'H2_2',
                  'HM' : 'H_m0',
                    'de':'de',
                    'temperature':'T',
                    'time': 't',
                    'energy':'ge'}

    weight_map_dict = { 'HI':1.00794,
                  'HII':1.00794,
                  'HeI':4.002602,
                  'HeII':4.002602,
                  'HeIII':4.002602,
                   'H2II': 2.01588,
                   'H2I' : 2.01588,
                   'HM' : 1.00794,
                    'de':1.00794,
                      'time': 1.0,
                      'temperature': 1.0,
                      'energy': 1.0}

    data_grackle = {}
    for key in name_map_dict.keys():
        key_dengo = name_map_dict[key]
        if key_dengo in ["t","T","ge"]:
            data_grackle[key_dengo] = data[key].value
        else:
            data_grackle[key_dengo] = data[key].value / weight_map_dict[key] / u.amu_cgs.value
    save_obj(data_grackle, 'grackle_data')
    return data_grackle, run_time
Beispiel #12
0
        my_chemistry.velocity_units = my_chemistry.a_units * \
            (my_chemistry.length_units / my_chemistry.a_value) / \
            my_chemistry.time_units

        # self-shielding cross sections in CGS
        my_chemistry.hi_pi_avg_cross_section  = 2.49E-18
        my_chemistry.hi_ph_avg_cross_section  = 2.49E-18
        my_chemistry.hei_ph_avg_cross_section = 4.1294e-18
        my_chemistry.hei_pi_avg_cross_section = 4.1294e-18
        my_chemistry.heii_ph_avg_cross_section = 0.0
        my_chemistry.heii_pi_avg_cross_section = 0.0


        rval = my_chemistry.initialize()

        fc = FluidContainer(my_chemistry, 1)
        fc["density"][:] = density
        if my_chemistry.primordial_chemistry > 0:
            fc["HI"][:] = 0.76 * fc["density"]
            fc["HII"][:] = tiny_number * fc["density"]
            fc["HeI"][:] = (1.0 - 0.76) * fc["density"]
            fc["HeII"][:] = tiny_number * fc["density"]
            fc["HeIII"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 1:
            fc["H2I"][:] = tiny_number * fc["density"]
            fc["H2II"][:] = tiny_number * fc["density"]
            fc["HM"][:] = tiny_number * fc["density"]
            fc["de"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 2:
            fc["DI"][:] = 2.0 * 3.4e-5 * fc["density"]
            fc["DII"][:] = tiny_number * fc["density"]
Beispiel #13
0
        my_chemistry.density_units  = mass_hydrogen_cgs # rho = 1.0 is 1.67e-24 g
        my_chemistry.length_units   = cm_per_mpc        # 1 Mpc in cm
        my_chemistry.time_units     = sec_per_Myr       # 1 Myr in s
        my_chemistry.velocity_units = my_chemistry.a_units * \
            (my_chemistry.length_units / my_chemistry.a_value) / \
            my_chemistry.time_units

        # set initial density and temperature
        initial_temperature = 100.
        #initial_density   = 1.0e3/0.9 * mass_hydrogen_cgs
        initial_density = 1.e3 * mass_hydrogen_cgs
        final_time = 3.2e3 # 0.05 #1.e3 #1. # 1.e3 # 1.e3 # in Myr

        rval = my_chemistry.initialize()

        fc = FluidContainer(my_chemistry, 1)
        fc["density"][:] = initial_density / my_chemistry.density_units
        fc["HII"][:] = tiny_number * fc["density"]
        fc["HI"][:] = (0.65 - 4.9e-4 * metallicity[i] - 2.9e-4 * metallicity[i])* fc["density"]
        fc["HeI"][:] = tiny_number * fc["density"]
        fc["HeII"][:] = tiny_number * fc["density"]
        fc["HeIII"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 1:
            fc["H2I"][:] = 0.30 * fc["density"]
            fc["H2II"][:] = tiny_number * fc["density"]
            fc["HM"][:] = tiny_number * fc["density"]
        if my_chemistry.primordial_chemistry > 2:
            fc["HDI"][:] = 1.e-9 * fc["density"]
            fc["DI"][:] = (3.e-5 - fc["HDI"][:]) * fc["density"]
            fc["DII"][:] = tiny_number * fc["density"]
        if my_chemistry.metal_cooling == 1:
def cooling_cell(density=12.2,
                 initial_temperature=2.0E4,
                 final_time=30.0,
                 metal_fraction=4.0E-4,
                 make_plot=False,
                 save_output=False,
                 primordial_chemistry=2,
                 outname=None,
                 save_H2_fraction=False,
                 return_result=False,
                 verbose=False,
                 H2_converge=None,
                 *args,
                 **kwargs):

    current_redshift = 0.

    # Set solver parameters
    my_chemistry = chemistry_data()
    my_chemistry.use_grackle = 1
    my_chemistry.with_radiative_cooling = 1
    my_chemistry.primordial_chemistry = primordial_chemistry
    my_chemistry.metal_cooling = 1
    my_chemistry.UVbackground = 1
    my_chemistry.self_shielding_method = 3

    if primordial_chemistry > 1:
        my_chemistry.H2_self_shielding = 2
        my_chemistry.h2_on_dust = 1
        my_chemistry.three_body_rate = 4

    grackle_dir = "/home/aemerick/code/grackle-emerick/"
    my_chemistry.grackle_data_file = os.sep.join(  #['/home/aemerick/code/grackle-emerick/input/CloudyData_UVB=HM2012.h5'])
        [grackle_dir, "input", "CloudyData_UVB=HM2012_shielded.h5"])

    # set the factors
    my_chemistry.LW_factor = kwargs.get("LW_factor", 1.0)
    my_chemistry.k27_factor = kwargs.get("k27_factor", 1.0)
    #if 'LW_factor' in kwargs.keys():
    #    my_chemistry.LW_factor = kwargs['LW_factor']
    #else:
    #    my_chemistry.LW_factor = 1.0

    #if 'k27_factor' in kwargs.keys():
    #    my_chemistry.k27_factor = kwargs['k27_factor']
    #else:
    #    my_chemistry.k27_factor = 1.0

    # Set units
    my_chemistry.comoving_coordinates = 0  # proper units
    my_chemistry.a_units = 1.0
    my_chemistry.a_value = 1. / (1. + current_redshift) / \
        my_chemistry.a_units
    my_chemistry.density_units = mass_hydrogen_cgs  # rho = 1.0 is 1.67e-24 g
    my_chemistry.length_units = cm_per_mpc  # 1 Mpc in cm
    my_chemistry.time_units = sec_per_Myr  # 1 Myr in s
    my_chemistry.velocity_units = my_chemistry.a_units * \
        (my_chemistry.length_units / my_chemistry.a_value) / \
        my_chemistry.time_units

    rval = my_chemistry.initialize()

    fc = FluidContainer(my_chemistry, 1)
    fc["density"][:] = density
    if my_chemistry.primordial_chemistry > 0:
        fc["HI"][:] = 0.76 * fc["density"]
        fc["HII"][:] = tiny_number * fc["density"]
        fc["HeI"][:] = (1.0 - 0.76) * fc["density"]
        fc["HeII"][:] = tiny_number * fc["density"]
        fc["HeIII"][:] = tiny_number * fc["density"]
    if my_chemistry.primordial_chemistry > 1:
        fc["H2I"][:] = tiny_number * fc["density"]
        fc["H2II"][:] = tiny_number * fc["density"]
        fc["HM"][:] = tiny_number * fc["density"]
        fc["de"][:] = tiny_number * fc["density"]
        fc['H2_self_shielding_length'][:] = 1.8E-6
    if my_chemistry.primordial_chemistry > 2:
        fc["DI"][:] = 2.0 * 3.4e-5 * fc["density"]
        fc["DII"][:] = tiny_number * fc["density"]
        fc["HDI"][:] = tiny_number * fc["density"]
    if my_chemistry.metal_cooling == 1:
        fc["metal"][:] = metal_fraction * fc["density"] * \
          my_chemistry.SolarMetalFractionByMass

    fc["x-velocity"][:] = 0.0
    fc["y-velocity"][:] = 0.0
    fc["z-velocity"][:] = 0.0

    fc["energy"][:] = initial_temperature / \
        fc.chemistry_data.temperature_units
    fc.calculate_temperature()
    fc["energy"][:] *= initial_temperature / fc["temperature"]

    # timestepping safety factor
    safety_factor = 0.001

    # let gas cool at constant density

    #if verbose:
    print("Beginning Run")
    data = evolve_constant_density(fc,
                                   final_time=final_time,
                                   H2_converge=H2_converge,
                                   safety_factor=safety_factor,
                                   verbose=verbose)
    #else:
    #    print "Beginning Run"

    #    with NoStdStreams():
    #        data = evolve_constant_density(
    #            fc, final_time=final_time, H2_converge = 1.0E-6,
    #            safety_factor=safety_factor)
    #    print "Ending Run"

    if make_plot:
        p1, = plt.loglog(data["time"].to("Myr"),
                         data["temperature"],
                         color="black",
                         label="T")
        plt.xlabel("Time [Myr]")
        plt.ylabel("T [K]")

        data["mu"] = data["temperature"] / \
            (data["energy"] * (my_chemistry.Gamma - 1.) *
             fc.chemistry_data.temperature_units)
        plt.twinx()
        p2, = plt.semilogx(data["time"].to("Myr"),
                           data["mu"],
                           color="red",
                           label="$\\mu$")
        plt.ylabel("$\\mu$")
        plt.legend([p1, p2], ["T", "$\\mu$"], fancybox=True, loc="center left")
        plt.savefig("cooling_cell.png")

    # save data arrays as a yt dataset
    if outname is None:
        outname = 'cooling_cell_%.2f_%.2f' % (my_chemistry.k27_factor,
                                              my_chemistry.LW_factor)

    if save_output:

        yt.save_as_dataset({}, outname + '.h5', data)

    if my_chemistry.primordial_chemistry > 1:
        H2_fraction = (data['H2I'] + data['H2II']) / data['density']
    else:
        H2_fraction = np.zeros(np.size(data['density']))

    if save_H2_fraction:
        #np.savetxt(outname + ".dat", [data['time'], H2_fraction])

        f = open("all_runs_d_%.2f.dat" % (density), "a")
        #        f.write("# k27 LW f_H2 T time\n")
        f.write("%8.8E %8.8E %8.8E %8.8E %8.8E \n" %
                (my_chemistry.k27_factor, my_chemistry.LW_factor,
                 H2_fraction[-1], data['temperature'][-1], data['time'][-1]))
        f.close()

    if return_result:
        return data
    else:
        return