示例#1
0
 def test_store_and_check_existing_test(self):
     kwargs = {
         'mechanism': 'gri30.cti',
         'initial_temp': 300,
         'initial_press': 101325,
         'fuel': 'CH4',
         'oxidizer': 'N2O',
         'equivalence': 1,
         'diluent': 'N2',
         'diluent_mol_frac': 0.1,
         'inert': 'None',
     }
     test_db = generate_db_name()
     test_table_name = 'test_table'
     test_table = db.Table(
         test_db,
         test_table_name,
         testing=True
     )
     test_table.store_test_row(**{
         **kwargs,
         'cj_speed': 456,
         'ind_len_west': 1,
         'ind_len_gav': 2,
         'ind_len_ng': 3,
         'cell_size_west': 4,
         'cell_size_gav': 5,
         'cell_size_ng': 6
     })
     assert test_table._check_existing_test(**kwargs)
示例#2
0
 def test_fetch_rows_multiple(self):
     test_db = generate_db_name()
     test_table_name = 'test_table'
     test_table = db.Table(
         test_db,
         test_table_name,
         testing=True
         )
     kwargs = [{
         'mechanism': 'gri30.cti',
         'initial_temp': 300,
         'initial_press': 101325,
         'fuel': 'CH4',
         'oxidizer': 'N2O',
         'equivalence': 1,
         'diluent': 'N2',
         'diluent_mol_frac': 0.1,
         'inert': 'None',
         'cj_speed': 1986.12354679687543,
         'ind_len_west': 1,
         'ind_len_gav': 2,
         'ind_len_ng': 3,
         'cell_size_west': 4,
         'cell_size_gav': 5,
         'cell_size_ng': 6
     }, {
         'mechanism': 'Mevel2017.cti',
         'initial_temp': 550,
         'initial_press': 2*101325,
         'fuel': 'H2',
         'oxidizer': 'O2',
         'equivalence': 1.125,
         'diluent': 'None',
         'diluent_mol_frac': 0,
         'inert': 'N2',
         'cj_speed': 2112,
         'ind_len_west': 2.1,
         'ind_len_gav': 2.2,
         'ind_len_ng': 2.3,
         'cell_size_west': 2.4,
         'cell_size_gav': 2.5,
         'cell_size_ng': 2.6
     }]
     for kw in kwargs:
         test_table.store_test_row(**kw)
     test_rows = test_table.fetch_test_rows()
     checks = []
     good_answer = {
         key: [value, kwargs[1][key]] for key, value in kwargs[0].items()
     }
     for key, value in test_rows.items():
         if 'date' not in key and 'rxn_table_id' not in key:
             if (isinstance(kwargs[0][key], str)
                     or isinstance(kwargs[0][key], int)):
                 for good, test in zip(good_answer[key], test_rows[key]):
                     checks.append(test == good)
             else:
                 checks.append(allclose(good_answer[key], value))
     assert all(checks)
示例#3
0
 def test_store_test_row_update(self):
     test_db = generate_db_name()
     test_table_name = 'test_table'
     test_table = db.Table(
         test_db,
         test_table_name,
         testing=True
         )
     kwargs_init = {
         'mechanism': 'gri30.cti',
         'initial_temp': 300,
         'initial_press': 101325,
         'fuel': 'CH4',
         'oxidizer': 'N2O',
         'equivalence': 1,
         'diluent': 'N2',
         'diluent_mol_frac': 0.1,
         'inert': 'None',
         'cj_speed': 1986.12354679687543,
         'ind_len_west': 1,
         'ind_len_gav': 2,
         'ind_len_ng': 3,
         'cell_size_west': 4,
         'cell_size_gav': 5,
         'cell_size_ng': 6
     }
     kwargs_repl = {
         'mechanism': 'gri30.cti',
         'initial_temp': 300,
         'initial_press': 101325,
         'fuel': 'CH4',
         'oxidizer': 'N2O',
         'equivalence': 1,
         'diluent': 'N2',
         'diluent_mol_frac': 0.1,
         'inert': 'None',
         'cj_speed': 2112,
         'ind_len_west': 2.1,
         'ind_len_gav': 2.2,
         'ind_len_ng': 2.3,
         'cell_size_west': 2.4,
         'cell_size_gav': 2.5,
         'cell_size_ng': 2.6
     }
     test_table.store_test_row(**kwargs_init)
     test_table.store_test_row(**kwargs_repl, overwrite_existing=True)
     test_table.store_test_row(**kwargs_init, overwrite_existing=False)
     test_rows = test_table.fetch_test_rows()
     checks = []
     for key, value in test_rows.items():
         if 'date' not in key and 'rxn_table_id' not in key:
             if (isinstance(kwargs_repl[key], str)
                     or isinstance(kwargs_repl[key], int)):
                 checks.append(kwargs_repl[key] == value[0])
             else:
                 checks.append(isclose(kwargs_repl[key], value[0]))
     assert all(checks)
示例#4
0
 def test__create_pert_table(self):
     test_db = generate_db_name()
     test_table_name = 'test_table'
     test_table = db.Table(
         test_db,
         test_table_name,
         testing=True
         )
     test_table.store_test_row(
         mechanism='gri30.cti',
         initial_temp=300,
         initial_press=101325,
         fuel='CH4',
         oxidizer='N2O',
         equivalence=1,
         diluent='N2',
         diluent_mol_frac=0.1,
         inert='None',
         cj_speed=1986.12354679687543,
         ind_len_west=1,
         ind_len_gav=2,
         ind_len_ng=3,
         cell_size_west=4,
         cell_size_gav=5,
         cell_size_ng=6
     )
     info = test_table.fetch_test_rows(
         mechanism='gri30.cti',
         initial_temp=300,
         initial_press=101325,
         fuel='CH4',
         oxidizer='N2O',
         equivalence=1,
         diluent='N2',
         diluent_mol_frac=0.1,
     )
     [rxn_id] = info['rxn_table_id']
     actual_columns = set(test_table.pert_columns(rxn_id))
     assert not {
         'rxn_no',
         'rxn',
         'k_i',
         'ind_len_west',
         'ind_len_gav',
         'ind_len_ng',
         'cell_size_west',
         'cell_size_gav',
         'cell_size_ng',
         'sens_ind_len_west',
         'sens_ind_len_gav',
         'sens_ind_len_ng',
         'sens_cell_size_west',
         'sens_cell_size_gav',
         'sens_cell_size_ng',
     }.difference(actual_columns)
示例#5
0
    def test_delete_test(self):
        test_db = generate_db_name()
        test_table_name = 'test_table'
        test_table = db.Table(
            test_db,
            test_table_name,
            testing=True
            )
        rxn_table_id = test_table.store_test_row(
            mechanism='gri30.cti',
            initial_temp=300,
            initial_press=101325,
            fuel='CH4',
            oxidizer='N2O',
            equivalence=1,
            diluent='N2',
            diluent_mol_frac=0.1,
            inert='None',
            cj_speed=1986.12354679687543,
            ind_len_west=1,
            ind_len_gav=2,
            ind_len_ng=3,
            cell_size_west=4,
            cell_size_gav=5,
            cell_size_ng=6
        )
        test_table.delete_test(rxn_table_id)
        empties = []
        with test_table.con as con:
            cur = con.cursor()
            cur.execute(
                    """
                    SELECT * FROM {:s} WHERE rxn_table_id='{:s}';
                    """.format(test_table_name, rxn_table_id)
            )
            empties.append(cur.fetchall())
            cur.execute(
                    """
                    SELECT name FROM sqlite_master WHERE type='table' AND
                    name LIKE 'PERT_%';
                    """
            )
            empties.append(cur.fetchall())
            cur.execute(
                    """
                    SELECT name FROM sqlite_master WHERE type='table' AND 
                    name LIKE 'BASE_%';
                    """.format(test_db)
            )
            empties.append(cur.fetchall())

        assert all([len(chk) == 0 for chk in empties])
示例#6
0
    def test_fetch_single_base_rxn(self):
        test_db = generate_db_name()
        test_table_name = 'test_table'
        test_table = db.Table(
            test_db,
            test_table_name,
            testing=True
            )
        kwargs_init = {
            'mechanism': 'gri30.cti',
            'initial_temp': 300,
            'initial_press': 101325,
            'fuel': 'CH4',
            'oxidizer': 'N2O',
            'equivalence': 1,
            'diluent': 'N2',
            'diluent_mol_frac': 0.1,
            'inert': 'None',
            'cj_speed': 1986.12354679687543,
            'ind_len_west': 1,
            'ind_len_gav': 2,
            'ind_len_ng': 3,
            'cell_size_west': 4,
            'cell_size_gav': 5,
            'cell_size_ng': 6
        }
        rxn_table_id = test_table.store_test_row(**kwargs_init)

        gas = ct.Solution(kwargs_init['mechanism'])
        gas.TP = kwargs_init['initial_temp'], kwargs_init['initial_press']
        gas.set_equivalence_ratio(
            kwargs_init['equivalence'],
            kwargs_init['fuel'],
            kwargs_init['oxidizer']
        )
        test_table.store_base_rxn_table(
            rxn_table_id,
            gas
        )

        checks = []
        rates = gas.forward_rate_constants
        for idx in range(gas.n_reactions):
            test_rxn = test_table.fetch_single_base_rxn(
                rxn_table_id,
                idx
            )
            checks.append([
                test_rxn[0] == idx,
                test_rxn[1] == gas.reaction_equation(idx),
                test_rxn[2] == rates[idx]
            ])
示例#7
0
    def test_store_base_table(self):
        test_db = generate_db_name()
        test_table_name = 'test_table'
        test_table = db.Table(
            test_db,
            test_table_name,
            testing=True
            )
        kwargs_init = {
            'mechanism': 'gri30.cti',
            'initial_temp': 300,
            'initial_press': 101325,
            'fuel': 'CH4',
            'oxidizer': 'N2O',
            'equivalence': 1,
            'diluent': 'N2',
            'diluent_mol_frac': 0.1,
            'inert': 'None',
            'cj_speed': 1986.12354679687543,
            'ind_len_west': 1,
            'ind_len_gav': 2,
            'ind_len_ng': 3,
            'cell_size_west': 4,
            'cell_size_gav': 5,
            'cell_size_ng': 6
        }
        rxn_table_id = test_table.store_test_row(**kwargs_init)

        gas = ct.Solution(kwargs_init['mechanism'])
        gas.TP = kwargs_init['initial_temp'], kwargs_init['initial_press']
        gas.set_equivalence_ratio(
            kwargs_init['equivalence'],
            kwargs_init['fuel'],
            kwargs_init['oxidizer']
        )
        test_table.store_base_rxn_table(
            rxn_table_id,
            gas
        )

        test_rows = test_table.fetch_base_rxn_table(rxn_table_id)
        checks = [gas.n_reactions == len(test_rows)]
        for row in test_rows:
            current_tests = [
                gas.reaction_equation(row[0]) == row[1],
                isclose(gas.forward_rate_constants[row[0]], row[2])
            ]
            checks.append(current_tests)

        assert all(checks)
示例#8
0
 def test_fetch_rows_blank_table(self):
     test_db = generate_db_name()
     test_table_name = 'test_table'
     test_table = db.Table(
         test_db,
         test_table_name,
         testing=True
         )
     test_rows = test_table.fetch_test_rows()
     assert all([
         not set(db.Table._test_table_args)
         .difference(set(test_rows.keys())),
         *[item == [] for item in test_rows.values()]
     ])
示例#9
0
    def test_check_for_stored_base_data(self):
        test_db = generate_db_name()
        test_table_name = 'test_table'
        test_table = db.Table(
            test_db,
            test_table_name,
            testing=True
            )
        kwargs_init = {
            'mechanism': 'gri30.cti',
            'initial_temp': 300,
            'initial_press': 101325,
            'fuel': 'CH4',
            'oxidizer': 'N2O',
            'equivalence': 1,
            'diluent': 'N2',
            'diluent_mol_frac': 0.1,
            'inert': 'None',
            'cj_speed': 1986.12354679687543,
            'ind_len_west': 1,
            'ind_len_gav': 2,
            'ind_len_ng': 3,
            'cell_size_west': 4,
            'cell_size_gav': 5,
            'cell_size_ng': 6
        }
        rxn_table_id = test_table.store_test_row(**kwargs_init)
        test_false = test_table.check_for_stored_base_data(rxn_table_id)

        gas = ct.Solution(kwargs_init['mechanism'])
        gas.TP = kwargs_init['initial_temp'], kwargs_init['initial_press']
        gas.set_equivalence_ratio(
            kwargs_init['equivalence'],
            kwargs_init['fuel'],
            kwargs_init['oxidizer']
        )
        test_table.store_base_rxn_table(
            rxn_table_id,
            gas
        )
        test_true = test_table.check_for_stored_base_data(rxn_table_id)
        assert all([test_true, not test_false])
示例#10
0
 def test_store_pert_row_update(self):
     test_db = generate_db_name()
     test_table_name = 'test_table'
     test_table = db.Table(
         test_db,
         test_table_name,
         testing=True
         )
     test_kwargs = {
         'mechanism': 'gri30.cti',
         'initial_temp': 300,
         'initial_press': 101325,
         'fuel': 'CH4',
         'oxidizer': 'N2O',
         'equivalence': 1,
         'diluent': 'N2',
         'diluent_mol_frac': 0.1,
         'inert': 'None',
         'cj_speed': 1986.12354679687543,
         'ind_len_west': 1,
         'ind_len_gav': 2,
         'ind_len_ng': 3,
         'cell_size_west': 4,
         'cell_size_gav': 5,
         'cell_size_ng': 6
     }
     pert_kwargs_init = {
         'rxn_no': 100,
         'rxn': '3 food + 1 stomach => 1 poop',
         'k_i': 1.2e12,
         'ind_len_west': 1.1,
         'ind_len_gav': 2.2,
         'ind_len_ng': 3.3,
         'cell_size_west': 4.4,
         'cell_size_gav': 5.5,
         'cell_size_ng': 6.6,
         'sens_ind_len_west': 8.8,
         'sens_ind_len_gav': 9.9,
         'sens_ind_len_ng': 0.0,
         'sens_cell_size_west': 1.1,
         'sens_cell_size_gav': 2.2,
         'sens_cell_size_ng': 3.3,
     }
     pert_kwargs_repl = {
         'rxn_no': 100,
         'rxn': '3 food + 1 stomach => 1 poop',
         'k_i': 1.2e12,
         'ind_len_west': 11.1,
         'ind_len_gav': 12.2,
         'ind_len_ng': 13.3,
         'cell_size_west': 14.4,
         'cell_size_gav': 15.5,
         'cell_size_ng': 16.6,
         'sens_ind_len_west': 18.8,
         'sens_ind_len_gav': 19.9,
         'sens_ind_len_ng': 10.0,
         'sens_cell_size_west': 11.1,
         'sens_cell_size_gav': 12.2,
         'sens_cell_size_ng': 13.3,
     }
     table_id = test_table.store_test_row(**test_kwargs)
     test_table.store_perturbed_row(
         rxn_table_id=table_id,
         **pert_kwargs_init
     )
     test_table.store_perturbed_row(
         rxn_table_id=table_id,
         overwrite_existing=True,
         **pert_kwargs_repl,
     )
     test_table.store_perturbed_row(
         rxn_table_id=table_id,
         overwrite_existing=False,
         **pert_kwargs_init,
     )
     test_rows = test_table.fetch_pert_table(
         rxn_table_id=table_id,
         **pert_kwargs_repl
     )
     checks = []
     for key, value in test_rows.items():
         if 'date' not in key:
             if (isinstance(pert_kwargs_repl[key], str)
                     or isinstance(pert_kwargs_repl[key], int)):
                 checks.append(pert_kwargs_repl[key] == value[0])
             else:
                 checks.append(isclose(pert_kwargs_repl[key], value[0]))
     assert all(checks)
示例#11
0
        _oxidizer,
        _equivalence,
        _diluent_to_match,
        _diluent_mol_frac_to_match,
        _diluent,
        _initial_temp,
        _initial_press
    )

    # _diluent_mol_frac = 0
    _perturbation_fraction = 1e-2
    max_step_znd = 1e-4  # default 1e-4
    db_name = "sensitivity_2.sqlite"

    t = db.Table(
        db_name,
        'data'
    )
    exist_check = t.fetch_test_rows(
        mechanism=_mechanism,
        initial_temp=_initial_temp,
        initial_press=_initial_press,
        fuel=_fuel,
        oxidizer=_oxidizer,
        equivalence=_equivalence,
        diluent=_diluent,
        diluent_mol_frac=_diluent_mol_frac,
        inert=_inert
    )['rxn_table_id']

    reactions = []
    # noinspection PyCallByClass,PyArgumentList