Esempio n. 1
0
def validate_on_mechanism(mech, temperature, pressure, tau, do_rhs, do_jac):
    xml = join(test_mech_directory, mech + '.xml')
    T = temperature
    Tin = T + 1000.
    p = pressure

    r = ChemicalMechanismSpec(xml, 'gas').griffon

    gas = Solution(xml)
    ns = gas.n_species

    y = np.ones(ns)  # equal masses in the reactor
    gas.TPY = T, p, y
    y = np.copy(gas.Y)
    rho = gas.density_mass

    xin = np.ones(ns)  # equal moles in the feed
    gas.TPX = Tin, p, xin
    yin = np.copy(gas.Y)
    rhoin = gas.density_mass

    state = hstack((rho, T, y[:-1]))

    rhsGRChemOnly = np.zeros(ns + 1)
    r.reactor_rhs_isochoric(state, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0, 0,
                            False, rhsGRChemOnly)
    rhsCN = rhs_cantera(p, T, y, rhoin, Tin, yin, tau, gas, rhsGRChemOnly)
    rhsGR = np.empty(ns + 1)
    r.reactor_rhs_isochoric(state, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0, 0,
                            True, rhsGR)

    if do_rhs:
        return max(abs(rhsGR - rhsCN) /
                   (abs(rhsCN) + 1.)) < 100. * sqrt(np.finfo(float).eps)

    if do_jac:
        jacGR = np.empty((ns + 1) * (ns + 1))
        r.reactor_jac_isochoric(state, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0, 0,
                                True, 0, rhsGR, jacGR)
        jacGR = jacGR.reshape((ns + 1, ns + 1), order='F')

        drho = 1.e-6
        dT = 1.e-6
        dY = 1.e-6
        jacFD = np.empty((ns + 1, ns + 1))
        rhsGR1, rhsGR2 = np.empty(ns + 1), np.empty(ns + 1)

        state_m = hstack((rho - drho, T, y[:-1]))
        state_p = hstack((rho + drho, T, y[:-1]))
        r.reactor_rhs_isochoric(state_m, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0,
                                0, True, rhsGR1)
        r.reactor_rhs_isochoric(state_p, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0,
                                0, True, rhsGR2)
        jacFD[:, 0] = (-rhsGR1 + rhsGR2) / (2. * drho)

        state_m = hstack((rho, T - dT, y[:-1]))
        state_p = hstack((rho, T + dT, y[:-1]))
        r.reactor_rhs_isochoric(state_m, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0,
                                0, True, rhsGR1)
        r.reactor_rhs_isochoric(state_p, rhoin, Tin, yin, tau, 0, 0, 0, 0, 0,
                                0, True, rhsGR2)
        jacFD[:, 1] = (-rhsGR1 + rhsGR2) / (2. * dT)

        for i in range(ns - 1):
            y_m1, y_p1 = np.copy(y), np.copy(y)
            y_m1[i] += -dY
            y_m1[-1] -= -dY
            y_p1[i] += dY
            y_p1[-1] -= dY
            state_m = hstack((rho, T, y_m1[:-1]))
            state_p = hstack((rho, T, y_p1[:-1]))
            r.reactor_rhs_isochoric(state_m, rhoin, Tin, yin, tau, 0, 0, 0, 0,
                                    0, 0, True, rhsGR1)
            r.reactor_rhs_isochoric(state_p, rhoin, Tin, yin, tau, 0, 0, 0, 0,
                                    0, 0, True, rhsGR2)
            jacFD[:, 2 + i] = (-rhsGR1 + rhsGR2) / (2. * dY)

        return max(abs(jacGR - jacFD) / (abs(jacGR) + 1.)) < 1.e-4
Esempio n. 2
0
def validate_on_mechanism(mech,
                          temperature,
                          pressure,
                          test_rhs=True,
                          test_jac=True):
    xml = join(test_mech_directory, mech + '.xml')

    r = ChemicalMechanismSpec(xml, 'gas').griffon

    gas = Solution(xml)
    ns = gas.n_species

    T = temperature
    p = pressure

    gas.TPX = T, p, ones(ns)
    y = gas.Y
    rho = gas.density_mass

    state = hstack((rho, T, y[:-1]))

    rhsGR = np.empty(ns + 1)
    rhsGRTemporary = np.empty(ns + 1)
    jacGR = np.empty((ns + 1) * (ns + 1))
    r.reactor_rhs_isochoric(state, 0, 0, np.ndarray(1), 0, 0, 0, 0, 0, 0, 0,
                            False, rhsGR)
    r.reactor_jac_isochoric(state, 0, 0, np.ndarray(1), 0, 0, 0, 0, 0, 0, 0,
                            False, 0, rhsGRTemporary, jacGR)
    jacGR = jacGR.reshape((ns + 1, ns + 1), order='F')

    def cantera_rhs(rho_arg, T_arg, Y_arg):
        gas.TDY = T_arg, rho_arg, Y_arg
        w = gas.net_production_rates * gas.molecular_weights
        e = gas.standard_int_energies_RT * gas.T * gas_constant / gas.molecular_weights
        cv = gas.cv_mass
        rhs = zeros(ns + 1)
        rhs[0] = 0.
        rhs[1] = -sum(w * e) / (rho_arg * cv)
        rhs[2:] = w[:-1] / rho
        return rhs

    rhsCN = cantera_rhs(rho, T, y)

    if test_rhs:
        pass_rhs = max(abs(rhsGR - rhsCN) /
                       (abs(rhsCN) + 1.)) < 100. * sqrt(np.finfo(float).eps)

    if test_jac:
        jacFD = zeros((ns + 1, ns + 1))
        wm1 = zeros(ns + 1)
        wp1 = zeros(ns + 1)
        drho = 1.e-4
        dT = 1.e-2
        dY = 1.e-6

        state_m = hstack((rho - drho, T, y[:-1]))
        state_p = hstack((rho + drho, T, y[:-1]))
        r.reactor_rhs_isochoric(state_m, 0, 0, np.ndarray(1), 0, 0, 0, 0, 0, 0,
                                0, False, wm1)
        r.reactor_rhs_isochoric(state_p, 0, 0, np.ndarray(1), 0, 0, 0, 0, 0, 0,
                                0, False, wp1)
        jacFD[:, 0] = (-wm1 + wp1) / (2. * drho)

        state_m = hstack((rho, T - dT, y[:-1]))
        state_p = hstack((rho, T + dT, y[:-1]))
        r.reactor_rhs_isochoric(state_m, 0, 0, np.ndarray(1), 0, 0, 0, 0, 0, 0,
                                0, False, wm1)
        r.reactor_rhs_isochoric(state_p, 0, 0, np.ndarray(1), 0, 0, 0, 0, 0, 0,
                                0, False, wp1)
        jacFD[:, 1] = (-wm1 + wp1) / (2. * dT)

        for i in range(ns - 1):
            y_m1, y_p1 = copy(y), copy(y)
            y_m1[i] += -dY
            y_m1[-1] -= -dY
            y_p1[i] += dY
            y_p1[-1] -= dY
            state_m = hstack((rho, T, y_m1[:-1]))
            state_p = hstack((rho, T, y_p1[:-1]))
            r.reactor_rhs_isochoric(state_m, 0, 0, np.ndarray(1), 0, 0, 0, 0,
                                    0, 0, 0, False, wm1)
            r.reactor_rhs_isochoric(state_p, 0, 0, np.ndarray(1), 0, 0, 0, 0,
                                    0, 0, 0, False, wp1)
            jacFD[:, 2 + i] = (-wm1 + wp1) / (2. * dY)

        gas.TDY = T, rho, y
        cv = gas.cv_mass
        cvi = gas.standard_cp_R * gas_constant / gas.molecular_weights
        w = gas.net_production_rates * gas.molecular_weights
        e = gas.standard_int_energies_RT * gas.T * gas_constant / gas.molecular_weights

        gas.TDY = T + dT, rho, y
        wp = gas.net_production_rates * gas.molecular_weights
        cvp = gas.cv_mass

        gas.TDY = T - dT, rho, y
        wm = gas.net_production_rates * gas.molecular_weights
        cvm = gas.cv_mass

        wsensT = (wp - wm) / (2. * dT)
        cvsensT = (cvp - cvm) / (2. * dT)

        jacFD11 = np.copy(jacFD[1, 1])
        jacSemiFD11 = -1. / cv * (1. / rho * (sum(wsensT * e) + sum(cvi * w)) +
                                  cvsensT * rhsGR[1])
        pass_jac = max(abs(jacGR - jacFD) / (abs(jacGR) + 1.)) < 1.e-4

    if test_rhs:
        return pass_rhs
    if test_jac:
        if not pass_jac:
            print(jacGR[1, 1])
            print(jacSemiFD11)
            print(jacFD11)
            print(abs(jacGR - jacFD) / (abs(jacGR) + 1.))
        return pass_jac