Example #1
0
def data(request):
    """
    Run parcel simulation and return opened netdcf file

    """
    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfreq']  = p_dict['z_max'] / p_dict['w'] / p_dict['dt'] / 4
    p_dict['outfile']  = "test_mass.nc"
    p_dict['chem_dsl'] = True

    p_dict['out_bin'] = \
           '{"wradii": {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "lin", "nbin": 500, "moms": [0, 3]}, \
             "dradii": {"rght": 1e-4, "left": 1e-10, "drwt": "dry", "lnli": "lin", "nbin": 500, "moms": [0, 3]}}'

    # run parcel model
    parcel(**p_dict)

    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # remove all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #2
0
def data(request):

    # copy options from chem_conditions ...
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['outfile']  = "test_chem_kreidenweis.nc"

    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True
    p_dict['chem_rct'] = True

    p_dict['sd_conc']  = 1025
    p_dict['outfreq']  = int(p_dict['z_max'] / p_dict['dt'] / 100) * 4

    p_dict['out_bin']  =  p_dict['out_bin'][:-1] + \
      ', "chem"  : {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "log", "nbin": 100, "moms": ["H"]},\
         "chemd" : {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 100, "moms": ["S_VI", "H2O2_a", "O3_a"]},\
         "radii" : {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "log", "nbin": 100, "moms": [3]},\
         "specd" : {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 100, "moms": [0, 1, 3]}}'

    pp.pprint(p_dict)

    # run parcel
    parcel(**p_dict)

    #simulation results
    data = netcdf.netcdf_file(p_dict['outfile'],   "r")

    # removing all netcdf files after all tests                                      
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #3
0
def data(request):

    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfile']  = "test_init_spectrum.nc"
    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True

    p_dict['z_max']    = .05
    p_dict['dt']       = .1
    p_dict['w']        = .5
    p_dict['outfreq']  = 1
    p_dict['sd_conc']  = 1024 * 44

    p_dict['out_bin'] = '{"drad": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [0,3]}}'

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'],   "r")

    # removing all netcdf files after all tests                                      
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #4
0
def test_chem_off():
    outfile_on = "test_on.nc"
    pc.parcel(outfile=outfile_on,
              chem_dsl=True,
              chem_dsc=False,
              chem_rct=False,
              SO2_g=200e-12,
              O3_g=50e-9,
              H2O2_g=500e-12,
              CO2_g=360e-6,
              NH3_g=100e-12,
              HNO3_g=100e-12)
    nc_on = nc.netcdf_file(outfile_on)

    outfile_off = "test_off.nc"
    pc.parcel(outfile=outfile_off,
              chem_dsl=False,
              chem_dsc=False,
              chem_rct=False)
    nc_off = nc.netcdf_file(outfile_off)

    assert (nc_on.SO2_g > 0)
    assert (nc_off.SO2_g == 0)
    assert ("SO2_g" in nc_on.variables)
    assert ("SO2_a" in nc_on.variables)
    assert ("SO2_g" not in nc_off.variables)
    assert ("SO2_a" not in nc_off.variables)

    subprocess.call(["rm", outfile_on])
    subprocess.call(["rm", outfile_off])
Example #5
0
def data(request):

    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfile'] = "test_chem_closed_rct.nc"

    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True
    p_dict['chem_rct'] = True

    p_dict['sd_conc'] = 1

    p_dict['out_bin']  = p_dict['out_bin'][:-1] + \
        ', "chem"  : {"rght": 1e-4, "left": 1e-9, "drwt": "wet", "lnli": "log", "nbin": 1,\
                      "moms": ["SO2_a", "S_VI", "H", "CO2_a", "NH3_a", "HNO3_a", "O3_a", "H2O2_a"]}}'

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #6
0
def data(request):
    # initial values                                        
    RH_init = .99999
    T_init  = 280.
    p_init  = 100000.
    r_init  = common.eps * RH_init * common.p_vs(T_init) / (p_init - RH_init * common.p_vs(T_init))
    # lists to store RH_max and N at the end of the simulation from each test run 
    RH_list = []
    N_list  = []

    for dt in Dt_list:
        print "\nt time step", dt
        outfile_nc = "timesteptest_dt=" + str(dt) + ".nc" 
        parcel(dt=dt, outfreq = int(100/dt),   outfile = outfile_nc,\
                w = 1., T_0 = T_init, p_0 = p_init, r_0 = r_init, z_max = 200, \
                mean_r = 5e-8, gstdev = 1.5, n_tot = 1e9, sd_conc = 1000, \
                out_bin = '{"radii": {"rght": 1, "moms": [0], "drwt": "wet", "nbin": 1, "lnli": "lin", "left": 1e-06}}'
              )

        f_out  = netcdf.netcdf_file(outfile_nc, "r")
        RH_max = f_out.RH_max
        N_end  = f_out.variables["radii_m0"][-1,0] # concentration of drops > 1e-6 m
    
        RH_list.append((RH_max - 1)*100)  # [%]                                      
        N_list.append(N_end / 1e6)        # [1/mg]           

    data = {"RH" : RH_list, "N" : N_list, "dt" : Dt_list}

    # removing all netcdf files after all tests
    def removing_files():
         for file in glob.glob("timesteptest_dt*"):
            subprocess.call(["rm", file])
    request.addfinalizer(removing_files)
    return data
Example #7
0
def main(dt_list = [1e-3, 1.5e-3, 2e-3, 3e-3, 4e-3, 8e-3, 1e-2, 2e-2, 4e-2, 8e-2, 1e-1, 2e-1,  4e-1, 8e-1, 1.]):

    # initial values
    z_max  = 200.
    w       = 1.                                        
    RH_init = .99999
    T_init  = 280.
    p_init  = 100000.
    r_init  = common.eps * RH_init * common.p_vs(T_init) / (p_init - RH_init * common.p_vs(T_init))

    # lists to store RH_max and N at the end of the simulation from each test run 
    RH_list = []
    N_list  = []

    for dt in dt_list:
        print("\nt time step", dt)
        outfile_nc = "timesteptest_dt=" + str(dt) + ".nc"
        parcel(dt=dt, outfreq = int(z_max/w/dt),   outfile = outfile_nc,\
                w = w, T_0 = T_init, p_0 = p_init, r_0 = r_init, z_max = z_max, \
                mean_r = 5e-8, gstdev = 1.5, n_tot = 1e9, sd_conc = 1000, \
                radii = 1e-6 * pow(10, -3 + np.arange(26) * .2)
              )

        f_out  = netcdf.netcdf_file(outfile_nc, "r")
        RH_max = f_out.RH_max
        N_end  = sum(f_out.variables["conc"][-1, -9:]) # concentration of drops > 1e-6 m                                                                                  
        RH_list.append((RH_max - 1)*100)  # [%]                                      
        N_list.append(N_end / 1e6)        # [1/mg] 

        subprocess.call(["rm", outfile_nc])          

    data = {"RH" : RH_list, "N" : N_list, "dt" : dt_list}
    timestep_plot(data)
Example #8
0
def data(request):

    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['sd_conc']  = 1
    p_dict['outfile']  = "test_chem_closed_dsc.nc"
    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True

    p_dict['out_bin']  = p_dict['out_bin'][:-1] + \
        ', "radii" : {"rght": 1e-4, "left": 1e-9, "drwt": "wet", "lnli": "log", "nbin": 1, "moms": [3]}, \
           "chem"  : {"rght": 1e-4, "left": 1e-9, "drwt": "wet", "lnli": "log", "nbin": 1,\
                      "moms": ["O3_a", "H2O2_a", "H", "SO2_a", "S_VI", "CO2_a", "NH3_a", "HNO3_a"]}}'

    pp.pprint(p_dict)

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'],   "r")

    # removing all netcdf files after all tests                                      
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #9
0
def main():
    # copy options from chem_conditions ...
    opts_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    opts_dict['outfile'] = "test_init_spectrum.nc"
    opts_dict['chem_dsl'] = True

    opts_dict['z_max'] = .05
    opts_dict['dt'] = .1
    opts_dict['w'] = .5
    opts_dict['outfreq'] = 1
    opts_dict['sd_conc'] = 1024 * 44
    opts_dict['outfreq'] = 1

    opts_dict[
        'out_bin'] = '{"drad": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [0,3]}}'

    # run parcel
    parcel(**opts_dict)

    # simulation results
    data = netcdf.netcdf_file(opts_dict['outfile'], "r")

    # doing plotting
    plot_init_spectrum(data, outfolder="../outputs/")

    # cleanup
    subprocess.call(["rm", opts_dict['outfile']])
Example #10
0
def data(request):

    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfile'] = "test_init_spectrum.nc"
    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True

    p_dict['z_max'] = .05
    p_dict['dt'] = .1
    p_dict['w'] = .5
    p_dict['outfreq'] = 1
    p_dict['sd_conc'] = 1024 * 44

    p_dict[
        'out_bin'] = '{"drad": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [0,3]}}'

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #11
0
def data(request):
    """
    Run parcel simulation and return opened netdcf file

    """
    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfreq'] = p_dict['z_max'] / p_dict['w'] / p_dict['dt'] / 4
    p_dict['outfile'] = "test_mass.nc"
    p_dict['chem_dsl'] = True

    p_dict['out_bin'] = \
           '{"wradii": {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "lin", "nbin": 500, "moms": [0, 3]}, \
             "dradii": {"rght": 1e-4, "left": 1e-10, "drwt": "dry", "lnli": "lin", "nbin": 500, "moms": [0, 3]}}'

    # run parcel model
    parcel(**p_dict)

    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # remove all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #12
0
def test_RH_gt1(tmpdir, kappa):
    """ checking if RH at the end of simulation is close to 1 
    (i.e. cloud is created) 
    """
    str_f = str(tmpdir.join("test_pcl.nc"))
    pc.parcel(outfile=str_f,  outfreq=1, aerosol='{"ammonium_sulfate": {"kappa": ' + str(kappa) + ', "mean_r": [0.02e-6], "gstdev": [1.4], "n_tot": [60.0e6]}}')
    f_out = netcdf.netcdf_file(str_f, "r")
    assert f_out.variables["RH"][-1] >= .99
Example #13
0
def data(request):
    parcel(outfile="file_test.nc")
    fnc = netcdf.netcdf_file("file_test.nc")
    def removing_files():
        fnc.close()
        subprocess.call(["rm", "file_test.nc"])
    request.addfinalizer(removing_files)
    return fnc
Example #14
0
def main(dt=1):
    # running parcel model for different ways to solve for pressure  ...
    outfile = "thesis_plot.nc"
    parcel(dt=dt, outfreq=10, outfile=outfile)
    fnc = netcdf.netcdf_file(outfile)
    thesis_profiles(fnc)
    fnc.close()
    subprocess.call(["rm", outfile])
Example #15
0
def main(dt=1):
    # running parcel model for different ways to solve for pressure  ...            
    outfile = "onesim_plot.nc"
    parcel(dt=dt, outfreq = 10, outfile=outfile)
    fnc = netcdf.netcdf_file(outfile)
    plot_profiles(fnc)
    fnc.close()
    subprocess.call(["rm", outfile])
Example #16
0
def test_RH_gt1(tmpdir, kappa):
    """ checking if RH at the end of simulation is close to 1 
    (i.e. cloud is created) 
    """
    str_f = str(tmpdir.join("test_pcl.nc"))
    pc.parcel(outfile=str_f,  outfreq=1, kappa=kappa)
    f_out = netcdf.netcdf_file(str_f, "r")
    assert f_out.variables["RH"][-1] >= .99
Example #17
0
def main(dt=0.1):
    # running parcel model for different ways to solve for pressure  ...
    fnc = {}
    for pprof in pprof_list:
        outfile = "test_" + pprof + ".nc"
        parcel(dt=dt, outfreq=100, pprof=pprof, outfile=outfile)
        fnc[pprof] = netcdf.netcdf_file(outfile)
    plot_pressure_opt(fnc)
    subprocess.call(["rm", outfile])
Example #18
0
def test_smax(tmpdir, outfreq):
    str_f = str(tmpdir.join("test_pcl.nc"))
    print(str_f)
    pc.parcel(outfile=str_f, outfreq=outfreq)
    f_out = netcdf.netcdf_file(str_f, "r")
    if outfreq == 1:
        assert f_out.variables["RH"][:].max() == f_out.RH_max
    else:
        assert f_out.variables["RH"][:].max() <= f_out.RH_max
Example #19
0
def main(dt=0.1):
    # running parcel model for different ways to solve for pressure  ...            
    fnc = {}
    for pprof in pprof_list:
        outfile = "test_" + pprof + ".nc"
        parcel(dt=dt, outfreq = 100, pprof = pprof, outfile=outfile)
        fnc[pprof] = netcdf.netcdf_file(outfile)
    plot_pressure_opt(fnc)
    subprocess.call(["rm", outfile])
Example #20
0
def data(request):
    parcel(outfile="file_test.nc")
    fnc = netcdf.netcdf_file("file_test.nc")

    def removing_files():
        fnc.close()
        subprocess.call(["rm", "file_test.nc"])

    request.addfinalizer(removing_files)
    return fnc
Example #21
0
def main():
    # running parcel
    outfile = "test_spectrum.nc"
    parcel(dt = .5, sd_conc = 1024, outfreq = 40,  outfile=outfile,\
          out_bin = ["wradii:1e-9/1e-4/26/log/wet/0", "dradii:1e-9/1e-6/26/log/dry/0"])
    data = netcdf.netcdf_file(outfile, "r")
    # doing plotting 
    plot_spectrum(data, outfolder="../outputs/")
    # cleanup
    subprocess.call(["rm", outfile])
Example #22
0
def main():

    # initial condition
    RH_init = .95
    T_init  = 285.2
    p_init  = 95000.
    r_init  = fn.rh_to_rv(RH_init, T_init, p_init) 

    # calculate rhod for initial gas mixing ratio
    rhod_init   = fn.rhod_calc(T_init, p_init, r_init)
    # initial condition for trace geses
    SO2_g_init  = fn.mole_frac_to_mix_ratio(200e-12, p_init, cm.M_SO2,  T_init, rhod_init)
    O3_g_init   = fn.mole_frac_to_mix_ratio(50e-9,   p_init, cm.M_O3,   T_init, rhod_init)
    H2O2_g_init = fn.mole_frac_to_mix_ratio(500e-12, p_init, cm.M_H2O2, T_init, rhod_init)
    CO2_g_init  = fn.mole_frac_to_mix_ratio(360e-6,  p_init, cm.M_CO2,  T_init, rhod_init)
    NH3_g_init  = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_NH3,  T_init, rhod_init)
    HNO3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_HNO3, T_init, rhod_init)

    # output
    z_max       = 20. #200.
    dt          = .05
    outfreq     = int(z_max / dt / 100)
    w           = 1.
    sd_conc     = 2048

    # turn on chemistry
    chem_dsl = True
    chem_dsc = True
    chem_rct = True

    # define output for moments and chemistry
    out_bin_chem = '{"plt_rw":   {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [0, 1, 3]},\
                     "plt_rd":   {"rght": 1, "left": 0, "drwt": "dry", "lnli": "lin", "nbin": 1, "moms": [0, 1, 3]},\
                     "plt_ch":   {"rght": 1, "left": 0, "drwt": "dry", "lnli": "lin", "nbin": 1,\
                                  "moms": ["O3_a",   "H2O2_a", "H", "SO2_a", "S_VI", "CO2_a",  "NH3_a",  "HNO3_a"]}}'

    out_bin      = '{"plt_rw": {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [0, 1, 3]},\
                     "plt_rd": {"rght": 1, "left": 0, "drwt": "dry", "lnli": "lin", "nbin": 1, "moms": [0, 1, 3]}}'

    # run parcel model
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq,\
           T_0 = T_init, p_0 = p_init, r_0 = r_init,\
           SO2_g = SO2_g_init, O3_g  = O3_g_init,  H2O2_g = H2O2_g_init,\
           CO2_g = CO2_g_init, NH3_g = NH3_g_init, HNO3_g = HNO3_g_init,\
           outfile="test_plot_chem_closed.nc",\
           sd_conc = sd_conc,\
           chem_dsl = chem_dsl, chem_dsc = chem_dsc, chem_rct = chem_rct, \
           out_bin = out_bin_chem)

    data = netcdf.netcdf_file("test_plot_chem.nc", "r")

    plot_chem(data, output_folder = "../outputs", output_title = "/test_plot_chem_")

    netcdf.close()
    subprocess.call(["rm", "test_plot_chem.nc"])
Example #23
0
def data(request):
    # initial condition
    RH_init = .999999
    T_init  = 300.
    p_init  = 100000.
    r_init  = cm.eps * RH_init * cm.p_vs(T_init) / (p_init - RH_init * cm.p_vs(T_init))

    # calculate rhod for gas init cond
    th_0      = T_init * (cm.p_1000 / p_init)**(cm.R_d / cm.c_pd)
    rhod_init = cm.rhod(p_init, th_0, r_init)

    # init cond for trace gases
    SO2_g_init  = fn.mole_frac_to_mix_ratio(200e-12, p_init, cm.M_SO2,  T_init, rhod_init)
    O3_g_init   = fn.mole_frac_to_mix_ratio(50e-9,   p_init, cm.M_O3,   T_init, rhod_init)
    H2O2_g_init = fn.mole_frac_to_mix_ratio(500e-12, p_init, cm.M_H2O2, T_init, rhod_init)
    CO2_g_init  = fn.mole_frac_to_mix_ratio(360e-6,  p_init, cm.M_CO2,  T_init, rhod_init)
    NH3_g_init  = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_NH3,  T_init, rhod_init)
    HNO3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_HNO3, T_init, rhod_init)

    # aerosol size distribution
    mean_r = .08e-6 / 2
    gstdev = 2.
    n_tot  = 566.e6

    # output
    sd_conc     = 1
    outfreq     = 50
    z_max       = 300.
    outfile     = "test_chem_henry.nc"
    dt          = 0.1
    wait        = 100

    parcel(dt = dt, z_max = z_max, outfreq = outfreq, wait=wait,\
          T_0 = T_init, p_0 = p_init, r_0 = r_init,\
          SO2_g = SO2_g_init, O3_g  = O3_g_init,  H2O2_g = H2O2_g_init,\
          CO2_g = CO2_g_init, NH3_g = NH3_g_init, HNO3_g = HNO3_g_init,\
          outfile = outfile,\
          chem_dsl = True, chem_dsc = True, chem_rct = False,\
          aerosol = \
            '{"test": {"kappa": 0.5, "mean_r": [' + str(mean_r) + '], "gstdev": [' + str(gstdev) + '], "n_tot": [' + str(n_tot) + ']}}',\
          sd_conc = sd_conc,\
          out_bin = \
            '{"radii": {"rght": 1.0, "left": 0.0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [0, 3]},\
              "chem" : {"rght": 1.0, "left": 0.0, "drwt": "wet", "lnli": "lin", "nbin": 1,\
               "moms": ["O3_a", "H2O2_a", "SO2_a", "CO2_a", "NH3_a", "HNO3_a", "H"]}}')

    data = netcdf.netcdf_file("test_chem_henry.nc", "r")

    # removing all netcdf files after all tests                                      
    def removing_files():
        subprocess.call(["rm", "test_chem_henry.nc"])

    request.addfinalizer(removing_files)
    return data
Example #24
0
def data(request):
    data = {}
    data["dt"] = request.param
    for pprof in Pprof_list:
        filename = "profopttest_" + pprof + str(request.param) + ".nc"
        parcel(dt=request.param, outfreq = 100, pprof = pprof, outfile=filename)
        data[pprof] = netcdf.netcdf_file(filename)
    # removing all netcdf files after all tests
    def removing_files():
        for file in glob.glob("profopttest_pprof*"):
            subprocess.call(["rm", file])
    request.addfinalizer(removing_files)
    return data
Example #25
0
def main():
    RH_init = .99999
    T_init = 300.
    p_init = 100000.
    r_init = cm.eps * RH_init * cm.p_vs(T_init) / (p_init -
                                                   RH_init * cm.p_vs(T_init))

    # calculate rhod for initial gas mixing ratio
    th_0 = T_init * (cm.p_1000 / p_init)**(cm.R_d / cm.c_pd)
    rhod_init = cm.rhod(p_init, th_0, r_init)

    SO2_g_init = fn.mole_frac_to_mix_ratio(200e-12, p_init, cm.M_SO2, T_init,
                                           rhod_init)
    O3_g_init = fn.mole_frac_to_mix_ratio(50e-9, p_init, cm.M_O3, T_init,
                                          rhod_init)
    H2O2_g_init = fn.mole_frac_to_mix_ratio(500e-12, p_init, cm.M_H2O2, T_init,
                                            rhod_init)
    CO2_g_init = fn.mole_frac_to_mix_ratio(360e-6, p_init, cm.M_CO2, T_init,
                                           rhod_init)
    NH3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_NH3, T_init,
                                           rhod_init)
    HNO3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_HNO3, T_init,
                                            rhod_init)

    outfreq = 50
    z_max = 50.
    outfile = "test_chem_henry.nc"
    dt = 0.1
    wait = 1000
    sd_conc = 1

    # run parcel run!
    parcel(dt = dt, z_max = z_max, outfreq = outfreq, wait=wait,\
            T_0 = T_init, p_0 = p_init, r_0 = r_init, sd_conc=sd_conc,\
            SO2_g = SO2_g_init, O3_g  = O3_g_init,  H2O2_g = H2O2_g_init,\
            CO2_g = CO2_g_init, NH3_g = NH3_g_init, HNO3_g = HNO3_g_init,\
            outfile = outfile,\
            chem_dsl = True, chem_dsc = False, chem_rct = False,\
            out_bin = \
            '{"radii": {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [0, 3]},\
              "chem" : {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1,\
                  "moms": ["O3_a", "H2O2_a", "SO2_a", "CO2_a", "NH3_a", "HNO3_a", "H"]}}'                                                                                         )

    data = netcdf.netcdf_file(outfile, "r")

    #plot
    plot_henry(data, output_folder="../outputs")

    #cleanup
    data.close()
    subprocess.call(["rm", "test_chem_henry.nc"])
Example #26
0
def data(request):
    """
    run parcel simulations with different initial sd_conc
    return data with values of sd_conc and initial dry mass of the aerosol

    """
    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['chem_dsl'] = True
    p_dict['z_max'] = .05
    p_dict['outfreq'] = 1
    p_dict[
        'out_bin'] = '{"drad": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [3]}}'

    # lists to store sd_conc at the total dry mass at t=0 of each test run
    out_sd_conc = []
    out_m3_dry = []

    for sd_conc in sd_conc_list:
        print("sd_conc = ", sd_conc)

        p_dict['outfile'] = "convergence_test_sd_conc=" + str(sd_conc) + ".nc"
        p_dict['sd_conc'] = sd_conc

        # run parcel simulation
        parcel(**p_dict)

        # read data
        f_out = netcdf.netcdf_file(p_dict['outfile'], "r")
        mom3_init = f_out.variables["drad_m3"][0, :]
        rhod_init = f_out.variables["rhod"][0]
        chem_rho = getattr(f_out, "chem_rho")

        # initial dry mass of aerosol [kg/kg dry air]
        ini = mom3_init.sum() * 4. / 3 * math.pi * chem_rho

        out_sd_conc.append(sd_conc)
        out_m3_dry.append(ini)

    data = {"sd_conc": out_sd_conc, "dry_mass": out_m3_dry}

    # removing all netcdf files after all tests
    def removing_files():
        for file in glob.glob("convergence_test_sd_conc*"):
            subprocess.call(["rm", file])

    request.addfinalizer(removing_files)
    return data
Example #27
0
def data(request):
    data = {}
    data["dt"] = request.param
    for pprof in Pprof_list:
        filename = "profopttest_" + pprof + str(request.param) + ".nc"
        parcel(dt=request.param, outfreq=100, pprof=pprof, outfile=filename)
        data[pprof] = netcdf.netcdf_file(filename)
    # removing all netcdf files after all tests
    def removing_files():
        for file in glob.glob("profopttest_pprof*"):
            subprocess.call(["rm", file])

    request.addfinalizer(removing_files)
    return data
Example #28
0
def data(request):
    """
    run parcel simulations with different initial sd_conc
    return data with values of sd_conc and initial dry mass of the aerosol

    """
    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['chem_dsl'] = True
    p_dict['z_max']    = .05
    p_dict['outfreq']  = 1
    p_dict['out_bin']  = '{"drad": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [3]}}'

    # lists to store sd_conc at the total dry mass at t=0 of each test run
    out_sd_conc = []
    out_m3_dry  = []

    for sd_conc in sd_conc_list:
        print "sd_conc = ", sd_conc
 
        p_dict['outfile'] = "convergence_test_sd_conc=" + str(sd_conc) + ".nc" 
        p_dict['sd_conc'] = sd_conc

        # run parcel simulation
        parcel(**p_dict)

        # read data
        f_out     = netcdf.netcdf_file(p_dict['outfile'], "r")
        mom3_init = f_out.variables["drad_m3"][0,:]
        rhod_init = f_out.variables["rhod"][0]
        chem_rho  = getattr(f_out, "chem_rho")

        # initial dry mass of aerosol [kg/kg dry air]
        ini = mom3_init.sum()  * 4./3 * math.pi * chem_rho
    
        out_sd_conc.append(sd_conc)
        out_m3_dry.append(ini)

    data = {"sd_conc" : out_sd_conc, "dry_mass" : out_m3_dry}

    # removing all netcdf files after all tests
    def removing_files():
         for file in glob.glob("convergence_test_sd_conc*"):
            subprocess.call(["rm", file])
    request.addfinalizer(removing_files)
    return data
Example #29
0
def main():

    outfile = "test_spectrum.nc"
    out_bin = '{"wradii": {"rght": 1e-4, "left": 1e-9, "drwt": "wet", "lnli": "log", "nbin": 26, "moms": [0]},\
                "dradii": {"rght": 1e-6, "left": 1e-9, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [0]}}'

    # run parcel run!
    parcel(dt = .5, sd_conc = 1024, outfreq = 40,  outfile = outfile, out_bin = out_bin)

    data = netcdf.netcdf_file(outfile, "r")

    # plotting 
    plot_spectrum(data, outfolder="../outputs/")

    # cleanup
    subprocess.call(["rm", outfile])
Example #30
0
def test_cmdline(tmpdir, arg):
  # calling from Python
  file = str(tmpdir.join("test.nc"))
  pc.parcel(outfile=file, **arg)
  # renaming the output file
  subprocess.call(["mv", file, str(tmpdir.join("test_pyt.nc"))])

  # calling via subprocess
  # creating a list with all provided arguments
  list_arg = ["python", "parcel.py", "--outfile="+file]
  for key, value in arg.items():
    list_arg.append("--" + key + "=" + str(value))

  subprocess.check_call(list_arg)

  # comparing if the output is the same
  subprocess.check_call(["diff", file, str(tmpdir.join("test_pyt.nc"))])
Example #31
0
def test_chem_closed():
    """
    Checking if the total mass of SO_2, O_3 and H2O2 in the closed chemical system 
    with only dissocoation present remains constant

    """
    #TODO - the same for dissociation
    #TODO - think of a similar test for reactions
    SO2_g_init  =  200e-12
    O3_g_init   =  50e-9
    H2O2_g_init =  500e-12
    outfreq     = 20
    z_max       = 200.
    outfile     = "test_chem_closed.nc"
    dt          = .01

    # run parcel
    parcel(dt = dt, z_max = z_max, outfreq = outfreq,\
           SO2_g_0 = SO2_g_init, O3_g_0 = O3_g_init, H2O2_g_0 = H2O2_g_init,\
           chem_sys = 'closed',   outfile = outfile,\
           chem_dsl = True, chem_dsc = False, chem_rct = False,\
           out_bin = '{"chem": {"rght": 1, "moms": ["O3_a", "H2O2_a", "SO2_a"], "drwt": "wet", "nbin": 1, "lnli": "lin", "left": 0}}',)

    f = netcdf.netcdf_file(outfile,   "r")

    Mass      = [cm.M_SO2, cm.M_O3, cm.M_H2O2]
    chem_list = ["SO2",       "O3",    "H2O2"]
    eps       = [4e-9,       4e-11,      2e-4]
                                        #TODO why so big?

    # convert aqueous phase within droplets to gase phase (mole fraction)
    def aq_to_g(chem_aq, rhod, T, M, p):
        return chem_aq * rhod * cm.R * T / M  / p

    for chem in chem_list : 
        ini = f.variables[chem+"_g"][0] 
        #final gas phase = current gas phase + mass dissolved into droplets
        end = f.variables[chem+"_g"][-1] + \
              aq_to_g(f.variables[chem+"_a"][-1], f.variables["rhod"][-1], f.variables["T"][-1],\
                    Mass[chem_list.index(chem)], f.variables["p"][-1])

        assert np.isclose(end, ini, atol=0, rtol=eps[chem_list.index(chem)])
    
    # cleanup
    subprocess.call(["rm", outfile])
Example #32
0
def test_cmdline(tmpdir, arg):
    # calling from Python
    file = str(tmpdir.join("test.nc"))
    pc.parcel(outfile=file, **arg)
    # renaming the output file
    subprocess.call(["mv", file, str(tmpdir.join("test_pyt.nc"))])

    # calling via subprocess
    # creating a list with all provided arguments
    list_arg = ["python", "parcel.py", "--outfile=" + file]
    for key, value in arg.items():
        list_arg.append("--" + key + "=" + str(value))

    subprocess.check_call(list_arg)

    # comparing if the output is the same
    # this test might fail if libcloudph++ was compiled with -Ofast flag (should work with -O3)
    subprocess.check_call(["diff", file, str(tmpdir.join("test_pyt.nc"))])
Example #33
0
def main():
    RH_init = .99999
    T_init  = 300.
    p_init  = 100000.
    r_init  = cm.eps * RH_init * cm.p_vs(T_init) / (p_init - RH_init * cm.p_vs(T_init))

    # calculate rhod for initial gas mixing ratio
    th_0      = T_init * (cm.p_1000 / p_init)**(cm.R_d / cm.c_pd)
    rhod_init = cm.rhod(p_init, th_0, r_init)

    SO2_g_init  = fn.mole_frac_to_mix_ratio(200e-12, p_init, cm.M_SO2,  T_init, rhod_init)
    O3_g_init   = fn.mole_frac_to_mix_ratio(50e-9,   p_init, cm.M_O3,   T_init, rhod_init)
    H2O2_g_init = fn.mole_frac_to_mix_ratio(500e-12, p_init, cm.M_H2O2, T_init, rhod_init)
    CO2_g_init  = fn.mole_frac_to_mix_ratio(360e-6,  p_init, cm.M_CO2,  T_init, rhod_init)
    NH3_g_init  = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_NH3,  T_init, rhod_init)
    HNO3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_HNO3, T_init, rhod_init)

    outfreq     = 50
    z_max       = 50.
    outfile     = "test_chem_henry.nc"
    dt          = 0.1
    wait        = 1000
    sd_conc     = 1

    # run parcel run!
    parcel(dt = dt, z_max = z_max, outfreq = outfreq, wait=wait,\
            T_0 = T_init, p_0 = p_init, r_0 = r_init, sd_conc=sd_conc,\
            SO2_g = SO2_g_init, O3_g  = O3_g_init,  H2O2_g = H2O2_g_init,\
            CO2_g = CO2_g_init, NH3_g = NH3_g_init, HNO3_g = HNO3_g_init,\
            outfile = outfile,\
            chem_dsl = True, chem_dsc = False, chem_rct = False,\
            out_bin = \
            '{"radii": {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [0, 3]},\
              "chem" : {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1,\
                  "moms": ["O3_a", "H2O2_a", "SO2_a", "CO2_a", "NH3_a", "HNO3_a", "H"]}}')

    data = netcdf.netcdf_file(outfile,"r")

    #plot
    plot_henry(data, output_folder = "../outputs")

    #cleanup
    data.close()
    subprocess.call(["rm", "test_chem_henry.nc"])
Example #34
0
def main():

    # copy options from chem_conditions ...
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['outfile'] = "Kreidenweis.nc"

    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True
    p_dict['chem_rct'] = True

    p_dict['sd_conc'] = 1025
    p_dict['outfreq'] = int(p_dict['z_max'] / p_dict['dt'] / 100) * 4

    p_dict['out_bin'] = '{\
                  "chem"  : {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "log", "nbin": 100, "moms": ["H"]},\
                  "chemd" : {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 100, "moms": ["S_VI"]},\
                  "radii" : {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "log", "nbin": 100, "moms": [0, 1, 3]},\
                   "specd": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 100, "moms": [0, 1, 3]},\
                  "plt_rw": {"rght": 1,    "left": 0,     "drwt": "wet", "lnli": "lin", "nbin": 1,   "moms": [0, 1, 3]},\
                  "plt_rd": {"rght": 1,    "left": 0,     "drwt": "dry", "lnli": "lin", "nbin": 1,   "moms": [0, 1, 3]},\
                  "plt_ch": {"rght": 1,    "left": 0,     "drwt": "dry", "lnli": "lin", "nbin": 1,\
                             "moms": ["O3_a", "H2O2_a", "H", "OH", "SO2_a", "S_VI", "CO2_a", "NH3_a", "HNO3_a"]}}'

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # do the plotting
    plot_fig1(data,
              output_folder="../outputs",
              output_title="/Kreidenweis_fig1")
    plot_fig2(data,
              output_folder="../outputs",
              output_title="/Kreidenweis_fig2")
    plot_fig3(data,
              output_folder="../outputs",
              output_title="/Kreidenweis_fig3")

    # cleanup
    subprocess.call(["rm", p_dict['outfile']])
Example #35
0
def main(dt_list=[1e-3, 1.5e-3, 2e-3, 3e-3, 4e-3, 8e-3, 1e-2, 2e-2, 4e-2, 8e-2, 1e-1, 2e-1, 4e-1, 8e-1, 1.0]):

    # initial values
    z_max = 200.0
    w = 1.0
    RH_init = 0.99999
    T_init = 280.0
    p_init = 100000.0
    r_init = common.eps * RH_init * common.p_vs(T_init) / (p_init - RH_init * common.p_vs(T_init))

    # lists to store RH_max and N at the end of the simulation from each test run
    RH_list = []
    N_list = []

    for dt in dt_list:
        print "\nt time step", dt
        outfile_nc = "timesteptest_dt=" + str(dt) + ".nc"
        parcel(
            dt=dt,
            outfreq=int(z_max / w / dt),
            outfile=outfile_nc,
            w=w,
            T_0=T_init,
            p_0=p_init,
            r_0=r_init,
            z_max=z_max,
            mean_r=5e-8,
            gstdev=1.5,
            n_tot=1e9,
            sd_conc=1000,
            radii=1e-6 * pow(10, -3 + np.arange(26) * 0.2),
        )

        f_out = netcdf.netcdf_file(outfile_nc, "r")
        RH_max = f_out.RH_max
        N_end = sum(f_out.variables["conc"][-1, -9:])  # concentration of drops > 1e-6 m
        RH_list.append((RH_max - 1) * 100)  # [%]
        N_list.append(N_end / 1e6)  # [1/mg]

        subprocess.call(["rm", outfile_nc])

    data = {"RH": RH_list, "N": N_list, "dt": dt_list}
    timestep_plot(data)
Example #36
0
def test_chem_off():
  outfile_on = "test_on.nc"
  pc.parcel(outfile=outfile_on, chem_dsl = True, chem_dsc = True, chem_rct = True,
            SO2_g_0 = 200e-12, O3_g_0 = 50e-9, H2O2_g_0 = 500e-12
           )
  nc_on = nc.netcdf_file(outfile_on)

  outfile_off = "test_off.nc"
  pc.parcel(outfile=outfile_off, chem_dsl = False, chem_dsc = False, chem_rct = False)
  nc_off = nc.netcdf_file(outfile_off)

  assert(nc_on.SO2_g_0 > 0)
  assert(nc_off.SO2_g_0 == 0)
  assert(nc_on.variables.has_key("SO2_g"))
  assert(nc_on.variables.has_key("SO2_a"))
  assert(not nc_off.variables.has_key("SO2_g"))
  assert(not nc_off.variables.has_key("SO2_a"))

  subprocess.call(["rm", outfile_on])
  subprocess.call(["rm", outfile_off])
Example #37
0
def data(request):

    p_dict = {}
    p_dict['outfile'] = "test_moms.nc"
    p_dict['sd_conc'] = 1000
    p_dict['large_tail'] = True
    p_dict['outfreq'] = 1000
    p_dict['dt'] = 1.
    p_dict['w'] = 0.5
    p_dict['z_max'] = 500.
    p_dict['RH_0'] = 0.9999
    p_dict['T_0'] = 273.15 + 10
    p_dict['p_0'] = 90000.
    p_dict['wait'] = 0

    # initial aerosol: 1-mode ammonium sulfate lognormal
    p_dict[
        'aerosol'] = '{"ammonium_sulfate": {"kappa":  0.61, "mean_r": [0.02e-6], "gstdev": [1.4], "n_tot":  [100e6]}}'

    # output for size distribution moments
    p_dict['out_bin'] = '{\
        "dry_moms": {"rght": 1e-6, "left": 1e-9, "drwt": "dry", "lnli": "log", "nbin": 1,  "moms": [0,1,2,3,4,5,6]},\
        "dry_dstr": {"rght": 1e-6, "left": 1e-9, "drwt": "dry", "lnli": "log", "nbin": 50, "moms": [0]},\
        "wet_moms": {"rght": 1e-4, "left": 1e-6, "drwt": "wet", "lnli": "log", "nbin": 1,  "moms": [0,1,2,3,4,5,6]},\
        "wet_dstr": {"rght": 1e-4, "left": 1e-6, "drwt": "wet", "lnli": "log", "nbin": 50, "moms": [0]}\
        }'

    # run parcel
    parcel(**p_dict)

    #simulation results
    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #38
0
def data(request):
    """
    Run parcel simulation and return opened netdcf file
    """
    outfile = "test_spectrum.nc"

    # running parcel model  ...
    parcel(dt = .5, sd_conc = 1024, outfreq = 40,  outfile=outfile, chem_dsl = True, out_bin = \
            '{"linwradii": {"rght": 0.0001,  "left": 1e-09, "drwt": "wet", "lnli": "lin", "nbin": 26, "moms": [0, 1, 3]}, \
              "lindradii": {"rght": 1e-06,  "left": 1e-09, "drwt": "dry", "lnli": "lin", "nbin": 26, "moms": [0, 1, 3]}, \
              "wradii":    {"rght": 0.0001,  "left": 1e-09, "drwt": "wet", "lnli": "log", "nbin": 26, "moms": [0, 1, 3]}, \
              "dradii":    {"rght": 1e-06,  "left": 1e-09, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [0, 1, 3]}}'\
          )

    data = netcdf.netcdf_file(outfile, "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", outfile])

    request.addfinalizer(removing_files)
    return data
Example #39
0
def data(request):
    """
    Run parcel simulation and return opened netdcf file
    """
    outfile = "test_spectrum.nc"

    # running parcel model  ...
    parcel(dt = .5, sd_conc = 1024, outfreq = 40,  outfile=outfile, chem_dsl = True, out_bin = \
            '{"linwradii": {"rght": 0.0001,  "left": 1e-09, "drwt": "wet", "lnli": "lin", "nbin": 26, "moms": [0, 1, 3]}, \
              "lindradii": {"rght": 1e-06,  "left": 1e-09, "drwt": "dry", "lnli": "lin", "nbin": 26, "moms": [0, 1, 3]}, \
              "wradii":    {"rght": 0.0001,  "left": 1e-09, "drwt": "wet", "lnli": "log", "nbin": 26, "moms": [0, 1, 3]}, \
              "dradii":    {"rght": 1e-06,  "left": 1e-09, "drwt": "dry", "lnli": "log", "nbin": 26, "moms": [0, 1, 3]}}'\
          )

    data = netcdf.netcdf_file(outfile, "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", outfile])

    request.addfinalizer(removing_files)
    return data
Example #40
0
def main():

    # copy options from chem_conditions ...
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['outfile']  = "Kreidenweis.nc"

    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True
    p_dict['chem_rct'] = True

    p_dict['sd_conc']  = 1025
    p_dict['outfreq']  = int(p_dict['z_max'] / p_dict['dt'] / 100) * 4

    p_dict['out_bin']  = '{\
                  "chem"  : {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "log", "nbin": 100, "moms": ["H"]},\
                  "chemd" : {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 100, "moms": ["S_VI"]},\
                  "radii" : {"rght": 1e-4, "left": 1e-10, "drwt": "wet", "lnli": "log", "nbin": 100, "moms": [3]},\
                   "specd": {"rght": 1e-6, "left": 1e-10, "drwt": "dry", "lnli": "log", "nbin": 100, "moms": [0, 1, 3]},\
                  "plt_rw": {"rght": 1,    "left": 0,     "drwt": "wet", "lnli": "lin", "nbin": 1,   "moms": [0, 1, 3]},\
                  "plt_rd": {"rght": 1,    "left": 0,     "drwt": "dry", "lnli": "lin", "nbin": 1,   "moms": [0, 1, 3]},\
                  "plt_ch": {"rght": 1,    "left": 0,     "drwt": "dry", "lnli": "lin", "nbin": 1,\
                             "moms": ["O3_a", "H2O2_a", "H", "OH", "SO2_a", "S_VI", "CO2_a", "NH3_a", "HNO3_a"]}}'

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'],   "r")

    # do the plotting
    plot_fig1(data, output_folder = "../outputs", output_title = "/Kreidenweis_fig1")
    plot_fig2(data, output_folder = "../outputs", output_title = "/Kreidenweis_fig2")
    plot_fig3(data, output_folder = "../outputs", output_title = "/Kreidenweis_fig3")

    # cleanup
    subprocess.call(["rm", p_dict['outfile']])
Example #41
0
def data(request):
    # initial values
    RH_init = .99999
    T_init = 280.
    p_init = 100000.
    r_init = common.eps * RH_init * common.p_vs(T_init) / (
        p_init - RH_init * common.p_vs(T_init))
    # lists to store RH_max and N at the end of the simulation from each test run
    RH_list = []
    N_list = []

    for dt in Dt_list:
        print("\nt time step", dt)
        outfile_nc = "timesteptest_dt=" + str(dt) + ".nc"
        parcel(dt=dt, outfreq = int(100/dt),   outfile = outfile_nc,\
                w = 1., T_0 = T_init, p_0 = p_init, r_0 = r_init, z_max = 200, \
                sd_conc = 1000, \
                aerosol = '{"ammonium_sulfate": {"kappa": 0.61, "mean_r": [5e-8], "gstdev": [1.5], "n_tot": [1e9]}}',
                out_bin = '{"radii": {"rght": 1, "moms": [0], "drwt": "wet", "nbin": 1, "lnli": "lin", "left": 1e-06}}'
              )

        f_out = netcdf.netcdf_file(outfile_nc, "r")
        RH_max = f_out.RH_max
        N_end = f_out.variables["radii_m0"][
            -1, 0]  # concentration of drops > 1e-6 m

        RH_list.append((RH_max - 1) * 100)  # [%]
        N_list.append(N_end / 1e6)  # [1/mg]

    data = {"RH": RH_list, "N": N_list, "dt": Dt_list}

    # removing all netcdf files after all tests
    def removing_files():
        for file in glob.glob("timesteptest_dt*"):
            subprocess.call(["rm", file])

    request.addfinalizer(removing_files)
    return data
Example #42
0
def data(request):

    p_dict = {}
    p_dict['outfile']  = "test_moms.nc"
    p_dict['sd_conc']  = 1000
    p_dict['large_tail']  = True
    p_dict['outfreq']  = 1000
    p_dict['dt']       = 1.
    p_dict['w']        = 0.5
    p_dict['z_max']    = 500.
    p_dict['RH_0']     = 0.9999
    p_dict['T_0']      = 273.15 + 10
    p_dict['p_0']      = 90000.
    p_dict['wait']     = 0

    # initial aerosol: 1-mode ammonium sulfate lognormal
    p_dict['aerosol'] = '{"ammonium_sulfate": {"kappa":  0.61, "mean_r": [0.02e-6], "gstdev": [1.4], "n_tot":  [100e6]}}'

    # output for size distribution moments
    p_dict['out_bin'] = '{\
        "dry_moms": {"rght": 1e-6, "left": 1e-9, "drwt": "dry", "lnli": "log", "nbin": 1,  "moms": [0,1,2,3,4,5,6]},\
        "dry_dstr": {"rght": 1e-6, "left": 1e-9, "drwt": "dry", "lnli": "log", "nbin": 50, "moms": [0]},\
        "wet_moms": {"rght": 1e-4, "left": 1e-6, "drwt": "wet", "lnli": "log", "nbin": 1,  "moms": [0,1,2,3,4,5,6]},\
        "wet_dstr": {"rght": 1e-4, "left": 1e-6, "drwt": "wet", "lnli": "log", "nbin": 50, "moms": [0]}\
        }'

    # run parcel
    parcel(**p_dict)

    #simulation results
    data = netcdf.netcdf_file(p_dict['outfile'],   "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #43
0
def data(request):

    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfile']  = "test_chem_closed_dsl.nc"
    p_dict['chem_dsl'] = True

    pp.pprint(p_dict)

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'],   "r")

    # removing all netcdf files after all tests                                      
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    #request.addfinalizer(removing_files)
    return data
Example #44
0
def data(request):

    # copy options from chem_conditions
    p_dict = copy.deepcopy(parcel_dict)

    # modify options from chem_conditions
    p_dict['outfile'] = "test_chem_closed_dsl.nc"
    p_dict['chem_dsl'] = True

    pp.pprint(p_dict)

    # run parcel
    parcel(**p_dict)

    # simulation results
    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    #request.addfinalizer(removing_files)
    return data
Example #45
0
def data(request):

    # copy options from chem_conditions ...
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['outfile'] = "test_chem_kreidenweis.nc"

    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True
    p_dict['chem_rct'] = True

    p_dict['sd_conc'] = 1024
    p_dict['outfreq'] = 10 / (p_dict['dt'] * p_dict['w'])

    p_dict['out_bin']  =  p_dict['out_bin'][:-1] + \
      ', "chem"  : {"rght": 1e-4, "left": 1e-6,  "drwt": "wet", "lnli": "log", "nbin": 100, "moms": ["H"]},\
         "radii" : {"rght": 1e-4, "left": 1e-6,  "drwt": "wet", "lnli": "log", "nbin": 100, "moms": [3]},\
         "acti"  : {"rght": 1e-3, "left": 1e-6,  "drwt": "wet", "lnli": "log", "nbin": 1,   "moms": [0,3]},\
         "chemd" : {"rght": 1e-6, "left": 1e-8,  "drwt": "dry", "lnli": "log", "nbin": 100, "moms": ["S_VI", "H2O2_a", "O3_a"]},\
         "specd" : {"rght": 1e-6, "left": 1e-8,  "drwt": "dry", "lnli": "log", "nbin": 100, "moms": [0, 1, 3]}}'

    pp.pprint(p_dict)

    # run parcel
    parcel(**p_dict)

    #simulation results
    data = netcdf.netcdf_file(p_dict['outfile'], "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", p_dict['outfile']])

    request.addfinalizer(removing_files)
    return data
Example #46
0
def data(request):
    SO2_g_init  = 200e-12 
    O3_g_init   = 50e-9
    H2O2_g_init = 500e-12
    outfreq     = 20
    z_max       = 200.
    outfile     = "test_chem_dsl.nc"

    # running parcel model for open chem system  and only for dissolving chem species into droplets
    parcel(dt = .5, z_max = z_max, outfreq = outfreq, SO2_g_0 = SO2_g_init, O3_g_0 = O3_g_init, H2O2_g_0 = H2O2_g_init,\
            chem_sys = 'open',   outfile = outfile,\
            chem_dsl = True, chem_dsc = False, chem_rct = False,\
            out_bin = \
            '{"radii": {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [3]},\
              "chem" : {"rght": 1, "left": 0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": ["O3_a", "H2O2_a", "SO2_a"]}}')

    data = netcdf.netcdf_file(outfile,   "r")

    # removing all netcdf files after all tests                                      
    def removing_files():
        subprocess.call(["rm", outfile])

    request.addfinalizer(removing_files)
    return data
Example #47
0
def data(request):
    # initial condition
    SO2_g_init  = 200e-12 
    O3_g_init   = 50e-9
    H2O2_g_init = 500e-12
    outfreq     = 1000    
    z_max       = 250.
    dt          = .01
    w           = 1.

    # turn on chemistry
    chem_dsl = True
    chem_dsc = True
    chem_rct = True
    chem_spn = 10

    # define output for moments and chemistry
    out_bin_chem = '{"radii": {"rght": 1, "moms": [0, 1, 3], "drwt": "wet", "nbin": 1, "lnli": "lin", "left": 0}, "chem": {"rght": 1, "moms": ["O3_a", "H2O2_a", "SO2_a", "H", "OH", "HSO3_a", "SO3_a", "HSO4_a", "SO4_a", "S_VI"], "drwt": "wet", "nbin": 1, "lnli": "lin", "left": 0}, "radiidry": {"rght": 1, "moms": [0, 1, 3], "drwt": "dry", "nbin": 1, "lnli": "lin", "left": 0}}'
    out_bin      = '{"radii": {"rght": 1, "moms": [0, 1, 3], "drwt": "wet", "nbin": 1, "lnli": "lin", "left": 0}, "radiidry": {"rght": 1, "moms": [0, 1, 3], "drwt": "dry", "nbin": 1, "lnli": "lin", "left": 0}}'

    # running parcel model for open / closed chem system  ...
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq,\
            SO2_g_0 = SO2_g_init, O3_g_0 = O3_g_init, H2O2_g_0 = H2O2_g_init,\
            chem_sys = 'open',   outfile="test_chem_open.nc",\
            chem_dsl = chem_dsl, chem_dsc = chem_dsc, chem_rct = chem_rct, chem_spn = chem_spn, \
            out_bin = out_bin_chem)
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq,\
             SO2_g_0 = SO2_g_init, O3_g_0 = O3_g_init, H2O2_g_0 = H2O2_g_init,\
             chem_sys = 'closed', outfile="test_chem_closed.nc",\
             chem_dsl = chem_dsl, chem_dsc = chem_dsc, chem_rct = chem_rct, chem_spn = chem_spn, \
             out_bin = out_bin_chem)
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq, SO2_g_0=0, O3_g_0=0, H2O2_g_0=0,\
             outfile="test_chem_off.nc", out_bin = out_bin)

    # TODO - why do I have to repeat this import here?
    from scipy.io import netcdf

    data = {'open'   : netcdf.netcdf_file("test_chem_open.nc", "r"),\
            'closed' : netcdf.netcdf_file("test_chem_closed.nc", "r"),\
            'off'    : netcdf.netcdf_file("test_chem_off.nc", "r")}

    def removing_files():
        for name, netcdf in data.iteritems():
            subprocess.call(["rm", "test_chem_" + name + ".nc"])

    request.addfinalizer(removing_files)

    return data
Example #48
0
def main():
    # initial condition
    SO2_g_init  = 200e-12 
    O3_g_init   = 50e-9
    H2O2_g_init = 500e-12
    outfreq     = 1000    
    z_max       = 250.
    dt          = .01
    w           = 1.

    # turn on chemistry
    chem_dsl = True
    chem_dsc = True
    chem_rct = True
    chem_spn = 10

    # define output for moments and chemistry
    out_bin_chem = ["radii:0/1/1/lin/wet/0,1,3", "radiidry:0/1/1/lin/dry/0,1,3",\
                    "chem:0/1/1/lin/wet/O3_a,H2O2_a,SO2_a,H,OH,HSO3_a,SO3_a,HSO4_a,SO4_a,S_VI"]
    out_bin      = ["radii:0/1/1/lin/wet/0,1,3", "radiidry:0/1/1/lin/dry/0,1,3"]

    # running parcel model for open / closed chem system  ...
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq,\
            SO2_g_0 = SO2_g_init, O3_g_0 = O3_g_init, H2O2_g_0 = H2O2_g_init,\
            chem_sys = 'open',   outfile="test_chem_open.nc",\
            chem_dsl = chem_dsl, chem_dsc = chem_dsc, chem_rct = chem_rct, chem_spn = chem_spn, \
            out_bin = out_bin_chem)
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq,\
             SO2_g_0 = SO2_g_init, O3_g_0 = O3_g_init, H2O2_g_0 = H2O2_g_init,\
             chem_sys = 'closed', outfile="test_chem_closed.nc",\
             chem_dsl = chem_dsl, chem_dsc = chem_dsc, chem_rct = chem_rct, chem_spn = chem_spn, \
             out_bin = out_bin_chem)
    parcel(dt = dt, z_max = z_max, w = w, outfreq = outfreq, SO2_g_0=0, O3_g_0=0, H2O2_g_0=0,\
             outfile="test_chem_off.nc", out_bin = out_bin)

    # TODO - why do I have to repeat this import here?
    from scipy.io import netcdf

    data = {'open'   : netcdf.netcdf_file("test_chem_open.nc", "r"),\
            'closed' : netcdf.netcdf_file("test_chem_closed.nc", "r"),\
            'off'    : netcdf.netcdf_file("test_chem_off.nc", "r")}

    plot_chem(data, output_folder = "../outputs")

    for name, netcdf in data.iteritems():
        netcdf.close()
        subprocess.call(["rm", "test_chem_" + name + ".nc"])
Example #49
0
def test_supersat(tmpdir):
    """ checking if parcel rises an exception when RH_init>0 """
    str_f = str(tmpdir.join("test_pcl.nc"))
    with pytest.raises(Exception) as excinfo:
        pc.parcel(outfile=str_f, r_0=.1)
Example #50
0
def test_supersat(tmpdir):
    """ checking if parcel rises an exception when RH_init>0 """
    str_f = str(tmpdir.join("test_pcl.nc"))
    with pytest.raises(Exception) as excinfo:
        pc.parcel(outfile=str_f,  r_0=.1)
Example #51
0
def test_args(tmpdir, arg):
    """checking if parcel rises exceptions when arguments don't make sense """
    str_f = str(tmpdir.join("test_pcl.nc"))
    with pytest.raises(Exception) as excinfo:
        pc.parcel(outfile=str_f,  **arg)
def data(request):

    # copy options from chem_conditions ...
    p_dict = copy.deepcopy(parcel_dict)

    # ... and modify them for the current test
    p_dict['outfile'] = "test_chem_kreidenweis.nc"

    p_dict['chem_dsl'] = True
    p_dict['chem_dsc'] = True
    p_dict['chem_rct'] = True

    p_dict['outfreq'] = 10 / (p_dict['dt'] * p_dict['w'])

    p_dict['out_bin']  =  p_dict['out_bin'][:-1] + \
      ', "chem"  : {"rght": 1e-4, "left": 1e-6,  "drwt": "wet", "lnli": "log", "nbin": 100, "moms": ["H"]},\
         "radii" : {"rght": 1e-4, "left": 1e-6,  "drwt": "wet", "lnli": "log", "nbin": 100, "moms": [3]},\
         "acti"  : {"rght": 1e-3, "left": 1e-6,  "drwt": "wet", "lnli": "log", "nbin": 1,   "moms": [0]},\
         "chemd" : {"rght": 1e-6, "left": 1e-8,  "drwt": "dry", "lnli": "log", "nbin": 100, "moms": ["S_VI" ]}}'

    # create empty lists for storing simulation results
    sd_list = []
    pow_list = []
    RH_max_list = []
    N_drop_list = []
    pH_list = []
    tot_S_list = []

    # run parcel test for super-droplet numbers between 1 and 1024
    for pw in range(11):
        sd_num = pow(2, pw)

        p_dict['sd_conc'] = sd_num
        pp.pprint(p_dict)

        # run parcel
        parcel(**p_dict)

        # save the simulation results ...
        tmp_data = netcdf.netcdf_file(p_dict['outfile'], "r")

        # ... do the initial analysis ...
        p = tmp_data.variables["p"][-1]
        T = tmp_data.variables["T"][-1]
        rhod = tmp_data.variables["rhod"][-1]
        r3 = tmp_data.variables["radii_m3"][-1, :]
        n_H = tmp_data.variables["chem_H"][-1, :] / cm.M_H

        # water weighted average pH at the end of the simulation:
        nom = 0
        for it, val in enumerate(r3):
            if val > 0:
                nom += (n_H[it] / (4. / 3 * math.pi * val * 1e3)) * val
        den = np.sum(r3[:])  # to liters
        pH = -1 * np.log10(nom / den)

        # total sulfate formation:
        s6_ini = tmp_data.variables["chemd_S_VI"][0, :]
        s6_end = tmp_data.variables["chemd_S_VI"][-1, :]

        sulf_ppt = fn.mix_ratio_to_mole_frac(
            (np.sum(s6_end) - np.sum(s6_ini)), p, cm.M_H2SO4, T, rhod) * 1e12

        # number of droplets at RH = max:
        n_tot = tmp_data.variables["acti_m0"][12, 0] * rhod * 1e-6
        #     ^^ TODO - think of something better
        # maximum supersaturation:
        RH_max = (tmp_data.RH_max - 1) * 100

        # ... save the results ...
        sd_list.append(sd_num)
        pow_list.append(pw)
        RH_max_list.append(RH_max)
        N_drop_list.append(n_tot)
        pH_list.append(pH)
        tot_S_list.append(sulf_ppt)

        # ... and remove the simulation data files
        subprocess.call(["rm", p_dict['outfile']])

    # pack all the data into a dictionary
    data = {}
    data['number of super-droplets'] = sd_list
    data['power of two'] = pow_list
    data['cloud droplet conc. at CB'] = N_drop_list
    data['maximum supersaturation'] = RH_max_list
    data['water weighted average pH'] = pH_list
    data['total sulfate production'] = tot_S_list

    return data
Example #53
0
def data(request):
    # initial condition
    RH_init = .999999
    T_init = 300.
    p_init = 100000.
    r_init = cm.eps * RH_init * cm.p_vs(T_init) / (p_init -
                                                   RH_init * cm.p_vs(T_init))

    # calculate rhod for gas init cond
    th_0 = T_init * (cm.p_1000 / p_init)**(cm.R_d / cm.c_pd)
    rhod_init = cm.rhod(p_init, th_0, r_init)

    # init cond for trace gases
    SO2_g_init = fn.mole_frac_to_mix_ratio(200e-12, p_init, cm.M_SO2, T_init,
                                           rhod_init)
    O3_g_init = fn.mole_frac_to_mix_ratio(50e-9, p_init, cm.M_O3, T_init,
                                          rhod_init)
    H2O2_g_init = fn.mole_frac_to_mix_ratio(500e-12, p_init, cm.M_H2O2, T_init,
                                            rhod_init)
    CO2_g_init = fn.mole_frac_to_mix_ratio(360e-6, p_init, cm.M_CO2, T_init,
                                           rhod_init)
    NH3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_NH3, T_init,
                                           rhod_init)
    HNO3_g_init = fn.mole_frac_to_mix_ratio(100e-12, p_init, cm.M_HNO3, T_init,
                                            rhod_init)

    # aerosol size distribution
    mean_r = .08e-6 / 2
    gstdev = 2.
    n_tot = 566.e6

    # output
    sd_conc = 1
    outfreq = 50
    z_max = 300.
    outfile = "test_chem_henry.nc"
    dt = 0.1
    wait = 100

    parcel(dt = dt, z_max = z_max, outfreq = outfreq, wait=wait,\
          T_0 = T_init, p_0 = p_init, r_0 = r_init,\
          SO2_g = SO2_g_init, O3_g  = O3_g_init,  H2O2_g = H2O2_g_init,\
          CO2_g = CO2_g_init, NH3_g = NH3_g_init, HNO3_g = HNO3_g_init,\
          outfile = outfile,\
          chem_dsl = True, chem_dsc = True, chem_rct = False,\
          aerosol = \
            '{"test": {"kappa": 0.5, "mean_r": [' + str(mean_r) + '], "gstdev": [' + str(gstdev) + '], "n_tot": [' + str(n_tot) + ']}}',\
          sd_conc = sd_conc,\
          out_bin = \
            '{"radii": {"rght": 1.0, "left": 0.0, "drwt": "wet", "lnli": "lin", "nbin": 1, "moms": [0, 3]},\
              "chem" : {"rght": 1.0, "left": 0.0, "drwt": "wet", "lnli": "lin", "nbin": 1,\
               "moms": ["O3_a", "H2O2_a", "SO2_a", "CO2_a", "NH3_a", "HNO3_a", "H"]}}'                                                                                      )

    data = netcdf.netcdf_file("test_chem_henry.nc", "r")

    # removing all netcdf files after all tests
    def removing_files():
        subprocess.call(["rm", "test_chem_henry.nc"])

    request.addfinalizer(removing_files)
    return data
Example #54
0
def test_args(tmpdir, arg):
    """checking if parcel rises exceptions when arguments don't make sense """
    str_f = str(tmpdir.join("test_pcl.nc"))
    with pytest.raises(Exception) as excinfo:
        pc.parcel(outfile=str_f, **arg)
Example #55
0
# lgrngn-specific parameters
sd_conc = 44


def lognormal(lnr):
    mean_r = .08e-6 / 2
    stdev = 2
    n_tot = 566e6
    return n_tot * exp(-pow(
        (lnr - log(mean_r)), 2) / 2 / pow(log(stdev), 2)) / log(stdev) / sqrt(
            2 * pi)


kappa = .61  #TODO - check
# rho = 1.8 # TODO

chem_gas = {
    chem_species_t.SO2: 200e-12,
    chem_species_t.O3: 50e-9,
    chem_species_t.H2O2: 500e-12
}

# running all three
for rhs in [
        rhs_blk_2m(dt),
        rhs_lgrngn(dt, sd_conc, {kappa: lognormal}, chem_gas)
]:
    parcel(p_d, th_d, r_v, w, dt, nt, rhs)
    print p_d, arr_t([th_dry2std(th_d[0], r_v[0])]), r_v