예제 #1
0
def test_XMLParser_functionality():
    """Test various parts of XMLParser."""

    xml_filename = "tests/test_xml_files/rxns.xml"
    parser = XMLParser(xml_filename)

    # get_species() routine
    assert parser.get_species() == ({'H': None,'O': None, 'OH': None,
                                    'H2': None, 'H2O': None, 'O2': None})

    # get_rxn_type() routine
    assert parser.reaction_list[0].rxn_type == 'Elementary'

    # get_rate_coeffs_components() routine
    assert (parser.reaction_list[0].rate_coeffs_components ==
            {'A': 35200000000.0, 'E': 71400.0})

    # get_is_reversible() routine
    assert parser.reaction_list[0].is_reversible == False

    # get_rxn_equation() routine
    assert parser.reaction_list[0].rxn_equation == 'H + O2 =] OH + O'

    # get_reactant_stoich_coeffs() routine
    assert (parser.reaction_list[0].reactant_stoich_coeffs ==
            {'H': 1, 'H2': 0, 'H2O': 0, 'O': 0, 'O2': 1, 'OH': 0})

    # get_product_stoich_coeffs() routine
    assert (parser.reaction_list[0].product_stoich_coeffs ==
            {'H': 0, 'H2': 0, 'H2O': 0, 'O': 1, 'O2': 0, 'OH': 1})
예제 #2
0
def test_faulty_b_in_arr():
    """Test when b found in Arrhenius (vs. modified Arrhenius)."""
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/faulty_A_arr.xml"
        parser = XMLParser(xml_filename)

    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/faulty_A_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)
예제 #3
0
def test_convert_to_SI_units_when_no_units():
    """Test when set convert_to_SI_units to True but no units found in XML."""
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/A_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/A_mod_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)
예제 #4
0
def test_rxn_sys_rev_reaction_antioch():
    """Test against Antioch rev rxn results"""
    xml_filename = "tests/test_xml_files/rxns_reversible_antioch.xml"
    xml_parser = XMLParser(xml_filename)
    species = xml_parser.get_species()
    sql_parser = SQLParser(TEST_DB_PATH, species)
    thermo_coeffs = sql_parser.get_thermo_coeffs()

    # Condition #1
    temp = 900.0000000000000000
    concentrations = ({
        'H': 5.0000000000000000e-01,
        'O': 0.0000000000000000e+00,
        'OH': 0.0000000000000000e+00,
        'H2': 2.0000000000000000e+00,
        'H2O': 0.0000000000000000e+00,
        'O2': 1.0000000000000000e+00,
        'HO2': 0.0000000000000000e+00,
        'H2O2': 0.0000000000000000e+00
    })
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()
    assert numpy.isclose(rates['H'], -1.2191202810057324e+13, atol=1e-16)
    assert numpy.isclose(rates['O'], 1.2191204233198564e+13, atol=1e-16)
    assert numpy.isclose(rates['OH'], 1.2191204233198564e+13, atol=1e-16)
    assert numpy.isclose(rates['H2'], -1.4231412404922757e+06, atol=1e-16)
    assert numpy.isclose(rates['H2O'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['O2'], -1.2191205656339805e+13, atol=1e-16)
    assert numpy.isclose(rates['HO2'], 1.4231412404922757e+06, atol=1e-16)
    assert numpy.isclose(rates['H2O2'], 0.0000000000000000e+00, atol=1e-16)

    # Condition #2
    temp = 2500.0000000000000000
    concentrations = ({
        'H': 5.0000000000000000e-01,
        'O': 0.0000000000000000e+00,
        'OH': 0.0000000000000000e+00,
        'H2': 2.0000000000000000e+00,
        'H2O': 0.0000000000000000e+00,
        'O2': 1.0000000000000000e+00,
        'HO2': 0.0000000000000000e+00,
        'H2O2': 0.0000000000000000e+00
    })
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()
    assert numpy.isclose(rates['H'], -3.3100422956557074e+13, atol=1e-16)
    assert numpy.isclose(rates['O'], 3.3300992971255586e+13, atol=1e-16)
    assert numpy.isclose(rates['OH'], 3.3300992971255586e+13, atol=1e-16)
    assert numpy.isclose(rates['H2'], -2.0057001469851071e+11, atol=1e-16)
    assert numpy.isclose(rates['H2O'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['O2'], -3.3501562985954098e+13, atol=1e-16)
    assert numpy.isclose(rates['HO2'], 2.0057001469851071e+11, atol=1e-16)
    assert numpy.isclose(rates['H2O2'], 0.0000000000000000e+00, atol=1e-16)
예제 #5
0
def test_rev_rxn_sys():
    """Returns a valid reaction system with reversible reaction(s)."""
    xml_filename = "tests/test_xml_files/rev_rxn.xml"
    xml_parser = XMLParser(xml_filename)
    species = xml_parser.get_species()
    sql_parser = SQLParser(TEST_DB_PATH, species)
    thermo_coeffs = sql_parser.get_thermo_coeffs()
    temp = 500  # "low" temperature range in NASA coeffs database
    concentrations = {'H': 1, 'O2': 1, 'H2O': 1}
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    return rxnsys
예제 #6
0
def test_rxn_sys_get_reaction_rate_for_3_rxns():
    """Tests function to get reaction rate for a given system of reactions (more than 1 reaction)."""
    xml_filename = "tests/test_xml_files/rxnsys.xml"
    xml_parser = XMLParser(xml_filename)
    species = xml_parser.get_species()
    sql_parser = SQLParser(TEST_DB_PATH, species)
    thermo_coeffs = sql_parser.get_thermo_coeffs()
    temp = 10
    concentrations = {'H': 1, 'O2': 1, 'OH': 1, 'O': 1, 'H2O': 1, 'H2': 1}
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()
    assert rates['H'] == -10.
    assert rates['O2'] == -15.
    assert rates['H2O'] == 40.
    assert rates['H2'] == -20.
    assert rates['O'] == -10.
    assert rates['OH'] == 0.
예제 #7
0
def test_rxn_sys_invalid_temperature():
    """Tests setting up reaction system with invalid temperatures."""
    xml_filename = "tests/test_xml_files/rxns_mixed.xml"
    xml_parser = XMLParser(xml_filename)
    species = xml_parser.get_species()
    sql_parser = SQLParser(TEST_DB_PATH, species)
    thermo_coeffs = sql_parser.get_thermo_coeffs()
    concentrations = {'H': 1, 'O2': 2, 'OH': 1, 'O': 4, 'H2O': 0, 'H2': 1}

    temp = 0
    with pytest.raises(ValueError):
        rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                                concentrations)

    temp = -100
    with pytest.raises(ValueError):
        rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                                concentrations)
예제 #8
0
def test_unit_checks():
    """Tests for unit checks."""

    # Arrhenius-type reaction without R
    xml_filename = "tests/test_xml_files/unit_check_arr.xml"
    parser = XMLParser(xml_filename, convert_to_SI_units=True)
    A = parser.reaction_list[0].rate_coeffs_components['A']
    E = parser.reaction_list[0].rate_coeffs_components['E']
    assert numpy.isclose(A, 35200, atol=1e-16)
    assert numpy.isclose(E, 298737.6, atol=1e-16)

    # Arrhenius-type reaction with R
    xml_filename = "tests/test_xml_files/unit_check_arr_with_R.xml"
    parser = XMLParser(xml_filename, convert_to_SI_units=False)
    A = parser.reaction_list[0].rate_coeffs_components['A']
    E = parser.reaction_list[0].rate_coeffs_components['E']
    R = parser.reaction_list[0].rate_coeffs_components['R']
    assert numpy.isclose(A, 3.52e+10, atol=1e-16)
    assert numpy.isclose(E, 7.14e+04, atol=1e-16)
    assert numpy.isclose(R, 8.3144598, atol=1e-16)

    # Modified Arrhenius-type reaction without R
    xml_filename = "tests/test_xml_files/unit_check_modarr.xml"
    parser = XMLParser(xml_filename, convert_to_SI_units=True)
    A = parser.reaction_list[0].rate_coeffs_components['A']
    E = parser.reaction_list[0].rate_coeffs_components['E']
    b = parser.reaction_list[0].rate_coeffs_components['b']
    assert numpy.isclose(A, 35200, atol=1e-16)
    assert numpy.isclose(E, 298737.6, atol=1e-16)
    assert numpy.isclose(b, 2.7, atol=1e-16)

    # Modified Arrhenius-type reaction with R
    xml_filename = "tests/test_xml_files/unit_check_modarr_with_R.xml"
    parser = XMLParser(xml_filename, convert_to_SI_units=False)
    A = parser.reaction_list[0].rate_coeffs_components['A']
    E = parser.reaction_list[0].rate_coeffs_components['E']
    b = parser.reaction_list[0].rate_coeffs_components['b']
    R = parser.reaction_list[0].rate_coeffs_components['R']
    assert numpy.isclose(A, 3.52e+10, atol=1e-16)
    assert numpy.isclose(E, 7.14e+04, atol=1e-16)
    assert numpy.isclose(b, 2.7, atol=1e-16)
    assert numpy.isclose(R, 8.3144598, atol=1e-16)
예제 #9
0
def test_xml_files_with_missing_info():
    """Test various cases in which input XML files
    are missing information."""

    # Test when k is missing from constant type reaction
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/k_const.xml"
        parser = XMLParser(xml_filename)

    # Test when A is missing from Arrhenius type reaction
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/A_arr.xml"
        parser = XMLParser(xml_filename)

    # Test when E is missing from Arrhenius type reaction
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/E_arr.xml"
        parser = XMLParser(xml_filename)

    # Test when A is missing from modified Arrhenius type reaction
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/A_mod_arr.xml"
        parser = XMLParser(xml_filename)

    # Test when b is missing from modified Arrhenius type reaction
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/b_mod_arr.xml"
        parser = XMLParser(xml_filename)

    # Test when E is missing from modified Arrhenius type reaction
    with pytest.raises(ValueError):
        xml_filename = "tests/test_xml_files/E_mod_arr.xml"
        parser = XMLParser(xml_filename)
예제 #10
0
def test_unhandled_xml_components():
    """Test when parts of reaction in the XML file
    are unhandled/not implemented."""

    # Test when type of reaction is unhandled
    xml_filename = "tests/test_xml_files/unhandled_rxn.xml"
    with pytest.raises(NotImplementedError):
        parser = XMLParser(xml_filename)

    # Test when reaction rate coefficient is unhandled
    xml_filename = "tests/test_xml_files/unhandled_k.xml"
    with pytest.raises(NotImplementedError):
        parser = XMLParser(xml_filename)

    # Test when units are undhandled
    with pytest.raises(NotImplementedError):
        xml_filename = "tests/test_xml_files/madeup_units_4_A_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    with pytest.raises(NotImplementedError):
        xml_filename = "tests/test_xml_files/madeup_units_4_A_mod_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    with pytest.raises(NotImplementedError):
        xml_filename = "tests/test_xml_files/madeup_units_4_E_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    with pytest.raises(NotImplementedError):
        xml_filename = "tests/test_xml_files/madeup_units_4_E_mod_arr.xml"
        parser = XMLParser(xml_filename, convert_to_SI_units=True)
예제 #11
0
def test_invalid_component_values():
    """Tests when fields of XML files contain invalid values (non-float)."""

    # Test Arrhenius-type reaction when nonsensical R value (e.g. string)
    xml_filename = "tests/test_xml_files/unit_conversion_fail_arr_invalidR.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=False)

    # Test Arrhenius-type reaction when nonsensical A value (e.g. string)
    xml_filename = "tests/test_xml_files/unit_conversion_fail_arr_A.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    # Test Arrhenius-type reaction when nonsensical E value (e.g. string)
    xml_filename = "tests/test_xml_files/unit_conversion_fail_arr_E.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    # Test modified Arrhenius-type reaction when nonsensical R value (e.g. string)
    xml_filename = "tests/test_xml_files/unit_conversion_fail_modarr_invalidR.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=False)

    # Test modified Arrhenius-type reaction when nonsensical A value (e.g. string)
    xml_filename = "tests/test_xml_files/unit_conversion_fail_modarr_A.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    # Test modified Arrhenius-type reaction when nonsensical b value (e.g. string)        
    xml_filename = "tests/test_xml_files/unit_conversion_fail_modarr_b.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=True)

    # Test modified Arrhenius-type reaction when nonsensical E value (e.g. string)
    xml_filename = "tests/test_xml_files/unit_conversion_fail_modarr_E.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=True)
예제 #12
0
def test_rxn_sys_irrev_reaction_antioch():
    """Test against Antioch irrev rxn results"""
    xml_filename = "tests/test_xml_files/rxns_irreversible_antioch.xml"
    xml_parser = XMLParser(xml_filename)
    species = xml_parser.get_species()
    sql_parser = SQLParser(TEST_DB_PATH, species)
    thermo_coeffs = sql_parser.get_thermo_coeffs()

    # Condition #1
    temp = 2500.0000000000000000
    concentrations = ({
        'H': 5.0000000000000000e-01,
        'O': 0.0000000000000000e+00,
        'OH': 0.0000000000000000e+00,
        'H2': 2.0000000000000000e+00,
        'H2O': 0.0000000000000000e+00,
        'O2': 1.0000000000000000e+00,
        'HO2': 0.0000000000000000e+00,
        'H2O2': 0.0000000000000000e+00
    })
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()

    assert numpy.isclose(rates['H'], -3.3300992971255586e+13, atol=1e-16)
    assert numpy.isclose(rates['O'], 3.3300992971255586e+13, atol=1e-16)
    assert numpy.isclose(rates['OH'], 3.3300992971255586e+13, atol=1e-16)
    assert numpy.isclose(rates['H2'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['H2O'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['O2'], -3.3300992971255586e+13, atol=1e-16)
    assert numpy.isclose(rates['HO2'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['H2O2'], 0.0000000000000000e+00, atol=1e-16)

    # Condition #2
    temp = 2500.0000000000000000
    concentrations = ({
        'H': 5.0000000000000000e-01,
        'O': 1.0000000000000001e-01,
        'OH': 1.0000000000000000e-02,
        'H2': 2.0000000000000000e+00,
        'H2O': 2.5000000000000000e-01,
        'O2': 1.0000000000000000e+00,
        'HO2': 2.9999999999999999e-01,
        'H2O2': 2.0000000000000000e-02
    })
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()
    assert numpy.isclose(rates['H'], -3.7324963347340922e+13, atol=1e-16)
    assert numpy.isclose(rates['O'], 2.3071533925003262e+13, atol=1e-16)
    assert numpy.isclose(rates['OH'], 6.4368180909475500e+13, atol=1e-16)
    assert numpy.isclose(rates['H2'], -6.6439941741054521e+12, atol=1e-16)
    assert numpy.isclose(rates['H2O'], 4.9820020841399396e+11, atol=1e-16)
    assert numpy.isclose(rates['O2'], -2.9843856969218777e+13, atol=1e-16)
    assert numpy.isclose(rates['HO2'], -1.3498571473703539e+13, atol=1e-16)
    assert numpy.isclose(rates['H2O2'], -6.2652907852405969e+11, atol=1e-16)

    # Condition #3
    temp = 950.0000000000000000
    concentrations = ({
        'H': 5.0000000000000000e-01,
        'O': 0.0000000000000000e+00,
        'OH': 0.0000000000000000e+00,
        'H2': 2.0000000000000000e+00,
        'H2O': 0.0000000000000000e+00,
        'O2': 1.0000000000000000e+00,
        'HO2': 0.0000000000000000e+00,
        'H2O2': 0.0000000000000000e+00
    })
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()
    assert numpy.isclose(rates['H'], -1.3403448555187156e+13, atol=1e-16)
    assert numpy.isclose(rates['O'], 1.3403448555187156e+13, atol=1e-16)
    assert numpy.isclose(rates['OH'], 1.3403448555187156e+13, atol=1e-16)
    assert numpy.isclose(rates['H2'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['H2O'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['O2'], -1.3403448555187156e+13, atol=1e-16)
    assert numpy.isclose(rates['HO2'], 0.0000000000000000e+00, atol=1e-16)
    assert numpy.isclose(rates['H2O2'], 0.0000000000000000e+00, atol=1e-16)

    # Condition #4
    temp = 950.0000000000000000
    concentrations = ({
        'H': 5.0000000000000000e-01,
        'O': 1.0000000000000001e-01,
        'OH': 1.0000000000000000e-02,
        'H2': 2.0000000000000000e+00,
        'H2O': 2.5000000000000000e-01,
        'O2': 1.0000000000000000e+00,
        'HO2': 2.9999999999999999e-01,
        'H2O2': 2.0000000000000000e-02
    })
    rxnsys = ReactionSystem(xml_parser.reaction_list, thermo_coeffs, temp,
                            concentrations)
    rates = rxnsys.sort_reaction_rates()
    assert numpy.isclose(rates['H'], -2.5701654161377395e+13, atol=1e-16)
    assert numpy.isclose(rates['O'], 1.1995070447537494e+13, atol=1e-16)
    assert numpy.isclose(rates['OH'], 3.5250102331863312e+13, atol=1e-16)
    assert numpy.isclose(rates['H2'], 1.9231756318330654e+12, atol=1e-16)
    assert numpy.isclose(rates['H2O'], 3.1178427968785107e+11, atol=1e-16)
    assert numpy.isclose(rates['O2'], -1.0092501951521918e+13, atol=1e-16)
    assert numpy.isclose(rates['HO2'], -1.3353585162517070e+13, atol=1e-16)
    assert numpy.isclose(rates['H2O2'], -3.3239141550534125e+11, atol=1e-16)
예제 #13
0
def test_unit_conversion_fail_arr_onlyE_units():
    """Test when only a few (i.e. not all) components have units."""
    xml_filename = "tests/test_xml_files/unit_conversion_fail_arr_onlyEunits.xml"
    with pytest.raises(ValueError):
        parser = XMLParser(xml_filename, convert_to_SI_units=True)
예제 #14
0
def test_XMLParser_file_not_found():
    """Test when XML file is nonexistent."""
    with pytest.raises(OSError):
        parser = XMLParser("no_such_file")
예제 #15
0
def test_SQLParser_file_not_found():
    xml_filename = "tests/test_xml_files/rxn.xml"
    xml_parser = XMLParser(xml_filename)
    species = xml_parser.get_species()
    with pytest.raises(OSError):
        sql_parser = SQLParser("no_such_file", species)