def test_corespeciesRate(self): """ Test if a specific core species rate is equal to 0 over time. """ c0 = {self.C2H5: 0.1, self.CH3: 0.1, self.CH4: 0.4, self.C2H6: 0.4} rxn1 = Reaction( reactants=[self.C2H6, self.CH3], products=[self.C2H5, self.CH4], kinetics=Arrhenius(A=(686.375*6, 'm^3/(mol*s)'), n=4.40721, Ea=(7.82799, 'kcal/mol'), T0=(298.15, 'K')) ) coreSpecies = [self.CH4, self.CH3, self.C2H6, self.C2H5] edgeSpecies = [] coreReactions = [rxn1] edgeReactions = [] sensitivity = [] terminationConversion = [] sensitivityThreshold = 0.001 ConstSpecies = ["CH4"] rxnSystem = LiquidReactor(self.T, c0, terminationConversion, sensitivity, sensitivityThreshold, ConstSpecies) # The test regarding the writing of constantSPCindices from input file is check with the previous test. rxnSystem.constSPCIndices = [0] rxnSystem.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) tlist = numpy.array([10**(i/10.0) for i in range(-130, -49)], numpy.float64) # Integrate to get the solution at each time point t, y, reactionRates, speciesRates = [], [], [], [] for t1 in tlist: rxnSystem.advance(t1) t.append(rxnSystem.t) self.assertEqual(rxnSystem.coreSpeciesRates[0], 0, "Core species rate has to be equal to 0 for species hold constant. Here it is equal to {0}".format(rxnSystem.coreSpeciesRates[0]))
def testComputeFlux(self): """ Test the liquid batch reactor with a simple kinetic model. """ rxn1 = Reaction(reactants=[self.C2H6, self.CH3], products=[self.C2H5, self.CH4], kinetics=Arrhenius(A=(686.375 * 6, 'm^3/(mol*s)'), n=4.40721, Ea=(7.82799, 'kcal/mol'), T0=(298.15, 'K'))) coreSpecies = [self.CH4, self.CH3, self.C2H6, self.C2H5] edgeSpecies = [] coreReactions = [rxn1] edgeReactions = [] c0 = {self.C2H5: 0.1, self.CH3: 0.1, self.CH4: 0.4, self.C2H6: 0.4} rxnSystem = LiquidReactor(self.T, c0, termination=[]) rxnSystem.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) tlist = numpy.array([10**(i / 10.0) for i in xrange(-130, -49)], numpy.float64) # Integrate to get the solution at each time point t, y, reactionRates, speciesRates = [], [], [], [] for t1 in tlist: rxnSystem.advance(t1) t.append(rxnSystem.t) # You must make a copy of y because it is overwritten by DASSL at # each call to advance() y.append(rxnSystem.y.copy()) reactionRates.append(rxnSystem.coreReactionRates.copy()) speciesRates.append(rxnSystem.coreSpeciesRates.copy()) # Convert the solution vectors to numpy arrays t = numpy.array(t, numpy.float64) reactionRates = numpy.array(reactionRates, numpy.float64) speciesRates = numpy.array(speciesRates, numpy.float64) # Check that we're computing the species fluxes correctly for i in xrange(t.shape[0]): self.assertAlmostEqual(reactionRates[i, 0], speciesRates[i, 0], delta=1e-6 * reactionRates[i, 0]) self.assertAlmostEqual(reactionRates[i, 0], -speciesRates[i, 1], delta=1e-6 * reactionRates[i, 0]) self.assertAlmostEqual(reactionRates[i, 0], -speciesRates[i, 2], delta=1e-6 * reactionRates[i, 0]) self.assertAlmostEqual(reactionRates[i, 0], speciesRates[i, 3], delta=1e-6 * reactionRates[i, 0]) # Check that we've reached equilibrium self.assertAlmostEqual(reactionRates[-1, 0], 0.0, delta=1e-2)
def test_corespeciesRate(self): "Test if a specific core species rate is equal to 0 over time" c0={self.C2H5: 0.1, self.CH3: 0.1, self.CH4: 0.4, self.C2H6: 0.4} rxn1 = Reaction(reactants=[self.C2H6,self.CH3], products=[self.C2H5,self.CH4], kinetics=Arrhenius(A=(686.375*6,'m^3/(mol*s)'), n=4.40721, Ea=(7.82799,'kcal/mol'), T0=(298.15,'K'))) coreSpecies = [self.CH4,self.CH3,self.C2H6,self.C2H5] edgeSpecies = [] coreReactions = [rxn1] edgeReactions = [] sensitivity=[] terminationConversion = [] sensitivityThreshold=0.001 ConstSpecies = ["CH4"] rxnSystem = LiquidReactor(self.T, c0, terminationConversion, sensitivity,sensitivityThreshold,ConstSpecies) ##The test regarding the writting of constantSPCindices from input file is check with the previous test. rxnSystem.constSPCIndices=[0] rxnSystem.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) tlist = numpy.array([10**(i/10.0) for i in range(-130, -49)], numpy.float64) # Integrate to get the solution at each time point t = []; y = []; reactionRates = []; speciesRates = [] for t1 in tlist: rxnSystem.advance(t1) t.append(rxnSystem.t) self.assertEqual(rxnSystem.coreSpeciesRates[0], 0,"Core species rate has to be equal to 0 for species hold constant. Here it is equal to {0}".format(rxnSystem.coreSpeciesRates[0]))
def test_compute_derivative(self): rxnList = [] rxnList.append(Reaction(reactants=[self.C2H6], products=[self.CH3,self.CH3], kinetics=Arrhenius(A=(686.375e6,'1/s'), n=4.40721, Ea=(7.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H6,self.CH3], products=[self.C2H5,self.CH4], kinetics=Arrhenius(A=(46.375*6,'m^3/(mol*s)'), n=3.40721, Ea=(6.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H6,self.CH3,self.CH3], products=[self.C2H5,self.C2H5,self.H2], kinetics=Arrhenius(A=(146.375*6,'m^6/(mol^2*s)'), n=2.40721, Ea=(8.82799,'kcal/mol'), T0=(298.15,'K')))) coreSpecies = [self.CH4,self.CH3,self.C2H6,self.C2H5, self.H2] edgeSpecies = [] coreReactions = rxnList edgeReactions = [] numCoreSpecies = len(coreSpecies) c0={self.CH4:0.2,self.CH3:0.1,self.C2H6:0.35,self.C2H5:0.15, self.H2:0.2} rxnSystem0 = LiquidReactor(self.T, c0,termination=[]) rxnSystem0.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) dfdt0 = rxnSystem0.residual(0.0, rxnSystem0.y, numpy.zeros(rxnSystem0.y.shape))[0] solver_dfdk = rxnSystem0.computeRateDerivative() #print 'Solver d(dy/dt)/dk' #print solver_dfdk integrationTime = 1e-8 modelSettings = ModelSettings(toleranceKeepInEdge = 0,toleranceMoveToCore=1,toleranceInterruptSimulation=0) simulatorSettings = SimulatorSettings() rxnSystem0.termination.append(TerminationTime((integrationTime,'s'))) rxnSystem0.simulate(coreSpecies, coreReactions, [], [], [],[], modelSettings=modelSettings,simulatorSettings=simulatorSettings) y0 = rxnSystem0.y dfdk = numpy.zeros((numCoreSpecies,len(rxnList))) # d(dy/dt)/dk c0={self.CH4:0.2,self.CH3:0.1,self.C2H6:0.35,self.C2H5:0.15, self.H2:0.2} for i in xrange(len(rxnList)): k0 = rxnList[i].getRateCoefficient(self.T) rxnList[i].kinetics.A.value_si = rxnList[i].kinetics.A.value_si*(1+1e-3) dk = rxnList[i].getRateCoefficient(self.T) - k0 rxnSystem = LiquidReactor(self.T, c0,termination=[]) rxnSystem.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) dfdt = rxnSystem.residual(0.0, rxnSystem.y, numpy.zeros(rxnSystem.y.shape))[0] dfdk[:,i]=(dfdt-dfdt0)/dk rxnSystem.termination.append(TerminationTime((integrationTime,'s'))) modelSettings = ModelSettings(toleranceKeepInEdge = 0,toleranceMoveToCore=1,toleranceInterruptSimulation=0) simulatorSettings = SimulatorSettings() rxnSystem.simulate(coreSpecies, coreReactions,[],[],[],[], modelSettings=modelSettings,simulatorSettings=simulatorSettings) rxnList[i].kinetics.A.value_si = rxnList[i].kinetics.A.value_si/(1+1e-3) # reset A factor for i in xrange(numCoreSpecies): for j in xrange(len(rxnList)): self.assertAlmostEqual(dfdk[i,j], solver_dfdk[i,j], delta=abs(1e-3*dfdk[i,j]))
def test_jacobian(self): """ Unit test for the jacobian function: Solve a reaction system and check if the analytical jacobian matches the finite difference jacobian """ coreSpecies = [self.CH4,self.CH3,self.C2H6,self.C2H5] edgeSpecies = [] rxn1 = Reaction(reactants=[self.C2H6,self.CH3], products=[self.C2H5,self.CH4], kinetics=Arrhenius(A=(686.375*6,'m^3/(mol*s)'), n=4.40721, Ea=(7.82799,'kcal/mol'), T0=(298.15,'K'))) coreReactions = [rxn1] edgeReactions = [] numCoreSpecies = len(coreSpecies) rxnList = [] rxnList.append(Reaction(reactants=[self.C2H6], products=[self.CH3,self.CH3], kinetics=Arrhenius(A=(686.375*6,'1/s'), n=4.40721, Ea=(7.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.CH3,self.CH3], products=[self.C2H6], kinetics=Arrhenius(A=(686.375*6,'m^3/(mol*s)'), n=4.40721, Ea=(7.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H6,self.CH3], products=[self.C2H5,self.CH4], kinetics=Arrhenius(A=(46.375*6,'m^3/(mol*s)'), n=3.40721, Ea=(6.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H5,self.CH4], products=[self.C2H6,self.CH3], kinetics=Arrhenius(A=(46.375*6,'m^3/(mol*s)'), n=3.40721, Ea=(6.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H5,self.CH4], products=[self.CH3,self.CH3,self.CH3], kinetics=Arrhenius(A=(246.375*6,'m^3/(mol*s)'), n=1.40721, Ea=(3.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.CH3,self.CH3,self.CH3], products=[self.C2H5,self.CH4], kinetics=Arrhenius(A=(246.375*6,'m^6/(mol^2*s)'), n=1.40721, Ea=(3.82799,'kcal/mol'), T0=(298.15,'K'))))# rxnList.append(Reaction(reactants=[self.C2H6,self.CH3,self.CH3], products=[self.C2H5,self.C2H5,self.H2], kinetics=Arrhenius(A=(146.375*6,'m^6/(mol^2*s)'), n=2.40721, Ea=(8.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H5,self.C2H5,self.H2], products=[self.C2H6,self.CH3,self.CH3], kinetics=Arrhenius(A=(146.375*6,'m^6/(mol^2*s)'), n=2.40721, Ea=(8.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H6,self.C2H6], products=[self.CH3,self.CH4,self.C2H5], kinetics=Arrhenius(A=(1246.375*6,'m^3/(mol*s)'), n=0.40721, Ea=(8.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.CH3,self.CH4,self.C2H5], products=[self.C2H6,self.C2H6], kinetics=Arrhenius(A=(46.375*6,'m^6/(mol^2*s)'), n=0.10721, Ea=(8.82799,'kcal/mol'), T0=(298.15,'K')))) for rxn in rxnList: coreSpecies = [self.CH4,self.CH3,self.C2H6,self.C2H5,self.H2] edgeSpecies = [] coreReactions = [rxn] c0={self.CH4:0.2,self.CH3:0.1,self.C2H6:0.35,self.C2H5:0.15, self.H2:0.2} rxnSystem0 = LiquidReactor(self.T, c0,termination=[]) rxnSystem0.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) dydt0 = rxnSystem0.residual(0.0, rxnSystem0.y, numpy.zeros(rxnSystem0.y.shape))[0] dN = .000001*sum(rxnSystem0.y) dN_array = dN*numpy.eye(numCoreSpecies) dydt = [] for i in xrange(numCoreSpecies): rxnSystem0.y[i] += dN dydt.append(rxnSystem0.residual(0.0, rxnSystem0.y, numpy.zeros(rxnSystem0.y.shape))[0]) rxnSystem0.y[i] -= dN # reset y to original y0 # Let the solver compute the jacobian solverJacobian = rxnSystem0.jacobian(0.0, rxnSystem0.y, dydt0, 0.0) # Compute the jacobian using finite differences jacobian = numpy.zeros((numCoreSpecies, numCoreSpecies)) for i in xrange(numCoreSpecies): for j in xrange(numCoreSpecies): jacobian[i,j] = (dydt[j][i]-dydt0[i])/dN self.assertAlmostEqual(jacobian[i,j], solverJacobian[i,j], delta=abs(1e-4*jacobian[i,j]))
def test_compute_derivative(self): rxnList = [] rxnList.append(Reaction(reactants=[self.C2H6], products=[self.CH3,self.CH3], kinetics=Arrhenius(A=(686.375e6,'1/s'), n=4.40721, Ea=(7.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H6,self.CH3], products=[self.C2H5,self.CH4], kinetics=Arrhenius(A=(46.375*6,'m^3/(mol*s)'), n=3.40721, Ea=(6.82799,'kcal/mol'), T0=(298.15,'K')))) rxnList.append(Reaction(reactants=[self.C2H6,self.CH3,self.CH3], products=[self.C2H5,self.C2H5,self.H2], kinetics=Arrhenius(A=(146.375*6,'m^6/(mol^2*s)'), n=2.40721, Ea=(8.82799,'kcal/mol'), T0=(298.15,'K')))) coreSpecies = [self.CH4,self.CH3,self.C2H6,self.C2H5, self.H2] edgeSpecies = [] coreReactions = rxnList edgeReactions = [] numCoreSpecies = len(coreSpecies) c0={self.CH4:0.2,self.CH3:0.1,self.C2H6:0.35,self.C2H5:0.15, self.H2:0.2} rxnSystem0 = LiquidReactor(self.T, c0,termination=[]) rxnSystem0.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) dfdt0 = rxnSystem0.residual(0.0, rxnSystem0.y, numpy.zeros(rxnSystem0.y.shape))[0] solver_dfdk = rxnSystem0.computeRateDerivative() #print 'Solver d(dy/dt)/dk' #print solver_dfdk integrationTime = 1e-8 rxnSystem0.termination.append(TerminationTime((integrationTime,'s'))) rxnSystem0.simulate(coreSpecies, coreReactions, [], [], 0, 1, 0) y0 = rxnSystem0.y dfdk = numpy.zeros((numCoreSpecies,len(rxnList))) # d(dy/dt)/dk c0={self.CH4:0.2,self.CH3:0.1,self.C2H6:0.35,self.C2H5:0.15, self.H2:0.2} for i in xrange(len(rxnList)): k0 = rxnList[i].getRateCoefficient(self.T) rxnList[i].kinetics.A.value_si = rxnList[i].kinetics.A.value_si*(1+1e-3) dk = rxnList[i].getRateCoefficient(self.T) - k0 rxnSystem = LiquidReactor(self.T, c0,termination=[]) rxnSystem.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) dfdt = rxnSystem.residual(0.0, rxnSystem.y, numpy.zeros(rxnSystem.y.shape))[0] dfdk[:,i]=(dfdt-dfdt0)/dk rxnSystem.termination.append(TerminationTime((integrationTime,'s'))) rxnSystem.simulate(coreSpecies, coreReactions, [], [], 0, 1, 0) rxnList[i].kinetics.A.value_si = rxnList[i].kinetics.A.value_si/(1+1e-3) # reset A factor for i in xrange(numCoreSpecies): for j in xrange(len(rxnList)): self.assertAlmostEqual(dfdk[i,j], solver_dfdk[i,j], delta=abs(1e-3*dfdk[i,j]))
def testComputeFlux(self): """ Test the liquid batch reactor with a simple kinetic model. """ rxn1 = Reaction( reactants=[self.C2H6, self.CH3], products=[self.C2H5, self.CH4], kinetics=Arrhenius(A=(686.375*6, 'm^3/(mol*s)'), n=4.40721, Ea=(7.82799, 'kcal/mol'), T0=(298.15, 'K')) ) coreSpecies = [self.CH4, self.CH3, self.C2H6, self.C2H5] edgeSpecies = [] coreReactions = [rxn1] edgeReactions = [] c0 = {self.C2H5: 0.1, self.CH3: 0.1, self.CH4: 0.4, self.C2H6: 0.4} rxnSystem = LiquidReactor(self.T, c0, 1, termination=[]) rxnSystem.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) tlist = numpy.array([10**(i/10.0) for i in xrange(-130, -49)], numpy.float64) # Integrate to get the solution at each time point t, y, reactionRates, speciesRates = [], [], [], [] for t1 in tlist: rxnSystem.advance(t1) t.append(rxnSystem.t) # You must make a copy of y because it is overwritten by DASSL at # each call to advance() y.append(rxnSystem.y.copy()) reactionRates.append(rxnSystem.coreReactionRates.copy()) speciesRates.append(rxnSystem.coreSpeciesRates.copy()) # Convert the solution vectors to numpy arrays t = numpy.array(t, numpy.float64) reactionRates = numpy.array(reactionRates, numpy.float64) speciesRates = numpy.array(speciesRates, numpy.float64) # Check that we're computing the species fluxes correctly for i in xrange(t.shape[0]): self.assertAlmostEqual(reactionRates[i, 0], speciesRates[i, 0], delta=1e-6*reactionRates[i, 0]) self.assertAlmostEqual(reactionRates[i, 0], -speciesRates[i, 1], delta=1e-6*reactionRates[i, 0]) self.assertAlmostEqual(reactionRates[i, 0], -speciesRates[i, 2], delta=1e-6*reactionRates[i, 0]) self.assertAlmostEqual(reactionRates[i, 0], speciesRates[i, 3], delta=1e-6*reactionRates[i, 0]) # Check that we've reached equilibrium self.assertAlmostEqual(reactionRates[-1, 0], 0.0, delta=1e-2)
def test_jacobian(self): """ Unit test for the jacobian function: Solve a reaction system and check if the analytical jacobian matches the finite difference jacobian. """ coreSpecies = [self.CH4, self.CH3, self.C2H6, self.C2H5, self.H2] edgeSpecies = [] numCoreSpecies = len(coreSpecies) c0 = {self.CH4: 0.2, self.CH3: 0.1, self.C2H6: 0.35, self.C2H5: 0.15, self.H2: 0.2} edgeReactions = [] rxnList = [] rxnList.append(Reaction( reactants=[self.C2H6], products=[self.CH3, self.CH3], kinetics=Arrhenius(A=(686.375*6, '1/s'), n=4.40721, Ea=(7.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.CH3, self.CH3], products=[self.C2H6], kinetics=Arrhenius(A=(686.375*6, 'm^3/(mol*s)'), n=4.40721, Ea=(7.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.C2H6, self.CH3], products=[self.C2H5, self.CH4], kinetics=Arrhenius(A=(46.375*6, 'm^3/(mol*s)'), n=3.40721, Ea=(6.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.C2H5, self.CH4], products=[self.C2H6, self.CH3], kinetics=Arrhenius(A=(46.375*6, 'm^3/(mol*s)'), n=3.40721, Ea=(6.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.C2H5, self.CH4], products=[self.CH3, self.CH3, self.CH3], kinetics=Arrhenius(A=(246.375*6, 'm^3/(mol*s)'), n=1.40721, Ea=(3.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.CH3, self.CH3, self.CH3], products=[self.C2H5, self.CH4], kinetics=Arrhenius(A=(246.375*6, 'm^6/(mol^2*s)'), n=1.40721, Ea=(3.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.C2H6, self.CH3, self.CH3], products=[self.C2H5, self.C2H5, self.H2], kinetics=Arrhenius(A=(146.375*6, 'm^6/(mol^2*s)'), n=2.40721, Ea=(8.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.C2H5, self.C2H5, self.H2], products=[self.C2H6, self.CH3, self.CH3], kinetics=Arrhenius(A=(146.375*6, 'm^6/(mol^2*s)'), n=2.40721, Ea=(8.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.C2H6, self.C2H6], products=[self.CH3, self.CH4, self.C2H5], kinetics=Arrhenius(A=(1246.375*6, 'm^3/(mol*s)'), n=0.40721, Ea=(8.82799, 'kcal/mol'), T0=(298.15, 'K')) )) rxnList.append(Reaction( reactants=[self.CH3, self.CH4, self.C2H5], products=[self.C2H6, self.C2H6], kinetics=Arrhenius(A=(46.375*6, 'm^6/(mol^2*s)'), n=0.10721, Ea=(8.82799, 'kcal/mol'), T0=(298.15, 'K')) )) # Analytical Jacobian for reaction 6 def jacobian_rxn6(c, kf, kr, s): c1, c2, c3, c4 = c[s[1]], c[s[2]], c[s[3]], c[s[4]] J = numpy.zeros((5, 5)) J[1, 1] = -4 * kf * c1 * c2 J[1, 2] = -2 * kf * c1 * c1 J[1, 3] = 4 * kr * c3 * c4 J[1, 4] = 2 * kr * c3 * c3 J[2, 1:] = 0.5 * J[1, 1:] J[3, 1:] = -J[1, 1:] J[4, 1:] = -0.5 * J[1, 1:] return J # Analytical Jacobian for reaction 7 def jacobian_rxn7(c, kf, kr, s): c1, c2, c3, c4 = c[s[1]], c[s[2]], c[s[3]], c[s[4]] J = numpy.zeros((5, 5)) J[1, 1] = -4 * kr * c1 * c2 J[1, 2] = -2 * kr * c1 * c1 J[1, 3] = 4 * kf * c3 * c4 J[1, 4] = 2 * kf * c3 * c3 J[2, 1:] = 0.5 * J[1, 1:] J[3, 1:] = -J[1, 1:] J[4, 1:] = -0.5 * J[1, 1:] return J for rxn_num, rxn in enumerate(rxnList): coreReactions = [rxn] rxnSystem0 = LiquidReactor(self.T, c0, 1, termination=[]) rxnSystem0.initializeModel(coreSpecies, coreReactions, edgeSpecies, edgeReactions) dydt0 = rxnSystem0.residual(0.0, rxnSystem0.y, numpy.zeros(rxnSystem0.y.shape))[0] dN = .000001*sum(rxnSystem0.y) # Let the solver compute the jacobian solverJacobian = rxnSystem0.jacobian(0.0, rxnSystem0.y, dydt0, 0.0) if rxn_num not in (6, 7): dydt = [] for i in xrange(numCoreSpecies): rxnSystem0.y[i] += dN dydt.append(rxnSystem0.residual(0.0, rxnSystem0.y, numpy.zeros(rxnSystem0.y.shape))[0]) rxnSystem0.y[i] -= dN # reset y # Compute the jacobian using finite differences jacobian = numpy.zeros((numCoreSpecies, numCoreSpecies)) for i in xrange(numCoreSpecies): for j in xrange(numCoreSpecies): jacobian[i, j] = (dydt[j][i]-dydt0[i])/dN self.assertAlmostEqual(jacobian[i, j], solverJacobian[i, j], delta=abs(1e-4*jacobian[i, j])) # The forward finite difference is very unstable for reactions # 6 and 7. Use Jacobians calculated by hand instead. elif rxn_num == 6: kforward = rxn.getRateCoefficient(self.T) kreverse = kforward / rxn.getEquilibriumConstant(self.T) jacobian = jacobian_rxn6(c0, kforward, kreverse, coreSpecies) for i in xrange(numCoreSpecies): for j in xrange(numCoreSpecies): self.assertAlmostEqual(jacobian[i, j], solverJacobian[i, j], delta=abs(1e-4*jacobian[i, j])) elif rxn_num == 7: kforward = rxn.getRateCoefficient(self.T) kreverse = kforward / rxn.getEquilibriumConstant(self.T) jacobian = jacobian_rxn7(c0, kforward, kreverse, coreSpecies) for i in xrange(numCoreSpecies): for j in xrange(numCoreSpecies): self.assertAlmostEqual(jacobian[i, j], solverJacobian[i, j], delta=abs(1e-4*jacobian[i, j]))