Cstr_matlab

March 23, 2018 | Author: Babu Kaala Pandian | Category: Stoichiometry, Chemical Kinetics, Chemistry, Physical Sciences, Science


Comments



Description

Produced using MATLAB® software.MATLAB Files for General CSTR Reactor Model Page 1 of 35 MATLAB Files for General CSTR Reactor Model These program comprised by these files calculates the steady state concentrations and temperatures within a CSTR at steady state for a reaction network of arbitrary complexity. In addition to the files listed below, this program requires the following subroutines for the input and checking of data found in the MATLAB tutorial located on the 10.34 homepage : get_input_scalar.m assert_scalar.m assert_vector.m assert_matrix.m assert_structure.m File: CSTR_SS_F_vol_scan.m This program calculates the steady state concentrations and temperature as a function of the volumetric flow rate through the reactor and makes the appropriate plots. % CSTR_SS\CSTR_SS_F_vol_scan.m % % function iflag_main = CSTR_SS_F_vol_scan.m(); % % This MATLAB program calculates the steady state concentration and % temperature at steady state within a continuous stirred-tank reactor % in overflow mode. The model is written to support an arbitarily % complex reaction network. % % First, a function is called that reads in the reactor geometry and % reaction data from the keyboard. % % Then, program calls the built-in MATLAB nonlinear equation solver FSOLVE % to solve the set of model equations for the unknown concentrations and % temperature. % % This program repeats the calculation for multiple values of the inlet % flow rate and plots the results. % % K. Beers % MIT ChE % 10/20/2001 function iflag_main = CSTR_SS_F_vol_scan(); iflag_main = 0; http://web.mit.edu/10.34/www/CSTR_SS_files.htm 7/16/2002 MATLAB Files for General CSTR Reactor Model Page 2 of 35 % First, read in the problem definition data from the keyboard or % from an input file. imode_input = input('Select mode of input (0 = keyboard, 1 = file) : '); if(~imode_input) [ProbDim,Reactor,Inlet,Physical,Rxn,iflag] = read_program_input; if(iflag <= 0) iflag_main = -1; error(['CSTR_SS: Error returned from read_program_input = ', ... int2str(iflag)]); end else file_name = input('Enter input file name (without .m) : ','s'); [ProbDim,Reactor,Inlet,Physical,Rxn,iflag] = feval(file_name); end % We ask the user to input the range over which to plot the solution % as a function of the volumetric flowrate. check_real=1; check_sign=2; check_int=0; prompt = 'Enter min. Reactor.F_vol for scan : '; F_vol_min = get_input_scalar(prompt, ... check_real,check_sign,check_int); check_real=1; check_sign=2; check_int=0; prompt = 'Enter max. Reactor.F_vol for scan : '; F_vol_max = get_input_scalar(prompt, ... check_real,check_sign,check_int); num_grid = 50; % Ask user whether to use logarithmic or linear scale. check_real=1; check_sign=2; check_int=1; prompt = 'Use linear (0) or logarithmic (1) x-axis for plot? : '; i_use_log = get_input_scalar(prompt, ... check_real,check_sign,check_int); if(i_use_log) F_vol_grid = logspace(log10(F_vol_min),log10(F_vol_max),num_grid); else F_vol_grid = linspace(F_vol_min,F_vol_max,num_grid); end % We begin our simulation at the maximum volumetric flowrate, http://web.mit.edu/10.34/www/CSTR_SS_files.htm 7/16/2002 MATLAB Files for General CSTR Reactor Model Page 3 of 35 % because this gives the smallest conversion of product and % thus the reactor concentrations and temperature are likely to % be near those of the outlet. This gives us a natural guess % for the first simulation. Guess = Inlet; % We set the convergence parameters for the MATLAB nonlinear % equation solver. options = optimset('TolFun', 1e-8,'Display','off',... 'LargeScale','off'); % Next, we allocate space to store the results of each % calculation. State_plot.conc = zeros(ProbDim.num_species,num_grid); State_plot.Temp = linspace(0,0,num_grid); for iter = 1:num_grid igrid = num_grid - iter + 1; Reactor.F_vol = F_vol_grid(igrid); % We now stack the guess data into the state vector. x_guess = stack_state(Guess,ProbDim.num_species); % Next, the MATLAB nonlinear equation solver is % invoked to solve for the unknown concentrations and % temperature. If the calculation does not converge, % the user is asked to input a new guess. exitflag = 0; while(exitflag <= 0) [x_state,fval,exitflag,output] = fsolve(@CSTR_SS_calc_f,... x_guess,options, ... ProbDim,Reactor,Inlet,Physical,Rxn); if(exitflag > 0) break; end disp('CSTR_SS_F_vol_scan: fsolve did not converge'); Reactor.F_vol, x_guess, x_state, fval, ichoose = ... input('Stop (0), retry with old result (1) with new guess (2) : '); if(~ichoose) error('CSTR_SS_F_vol_scan: Stopped calculation'); elseif(ichoose==1) x_guess = x_state; else Guess.conc = input('Enter new guess of concentration vector : '); Guess.Temp = input('Enter new guess of temperature : '); http://web.mit.edu/10.34/www/CSTR_SS_files.htm 7/16/2002 xlabel('F_{vol} = Volumetric Flowrate'). list_neg = find(x_state < 0).State_plot..Temp(igrid) = State. else plot(F_vol_grid. % Use the results of this calculation as an estimate of the % solution for the next value of the flowrate. end title('CSTR model : Effect of F_{vol} on temperature'). for count=1:length(list_neg) k = list_neg(count). xlabel('F_{vol} = Volumetric Flowrate').ProbDim. x_state(k) = 0.num_species figure.State_plot. . if(i_use_log) semilogx(F_vol_grid.State_plot. of species # '.MATLAB Files for General CSTR Reactor Model Page 4 of 35 x_guess = stack_state(Guess.ProbDim.conc. ylabel(['c('.:)).. Guess = State. int2str(ispecies).34/www/CSTR_SS_files. end end % Unstack and store the results for later plotting.htm 7/16/2002 .conc(ispecies. else plot(F_vol_grid.num_species). ')']).Temp.:)).edu/10.Temp). end title(['CSTR model : Effect of F_{vol} on conc.igrid) = State.Temp).num_species). ylabel('T'). end figure. int2str(ispecies)]). State_plot. if(i_use_log) semilogx(F_vol_grid.State_plot. end State = unstack_state(x_state. http://web. make plots of each concentration and temperature as a function % of the volumetric flowrate through the reactor.conc(:. for ispecies = 1:ProbDim. State_plot.mit. end % Now.conc(ispecies. MATLAB Files for General CSTR Reactor Model Page 5 of 35 % Save the results to a binary output file.edu/10. return.mit. save CSTR_SS_results. iflag_main = 1.htm 7/16/2002 .mat. http://web.34/www/CSTR_SS_files. if(iflag <= 0) iflag_main = -1.iflag] = read_program_input.iflag] = feval(file_name).Physical. int2str(iflag)]).Rxn.htm 7/16/2002 .Rxn. % CSTR_SS\CSTR_SS. if(~imode_input) [ProbDim. a function is called that reads in the reactor geometry and % reaction data from the keyboard. % % Then.Reactor.Reactor.mit.Inlet.34/www/CSTR_SS_files.Physical.Inlet. % First. iflag_main = 0. program calls the built-in MATLAB nonlinear equation solver FSOLVE % to solve the set of model equations for the unknown concentrations and % temperature.. . The model is written to support an arbitarily % complex reaction network. 1 = file) : ').m().edu/10. Beers % MIT ChE % 10/20/2001 function iflag_main = CSTR_SS(). error(['CSTR_SS: Error returned from read_program_input = '. http://web.m This program calculates the steady state concentrations and temperature for a single simulation. read in the problem definition data from the keyboard or % from an input file..'s'). % % K.m % % function iflag_main = CSTR_SS.m) : '. % % First. end else file_name = input('Enter input file name (without . % % This MATLAB program calculates the steady state concentration and % temperature at steady state within a continuous stirred-tank reactor % in overflow mode.MATLAB Files for General CSTR Reactor Model Page 6 of 35 File: CSTR_SS. imode_input = input('Select mode of input (0 = keyboard. [ProbDim. x_guess = stack_state(Guess. options = optimset('TolFun'. check_int=0. Guess. Guess.ProbDim.check_int).ProbDim. the MATLAB nonlinear equation solver is invoked to solve for the % unknown concentrations and temperature. % We now stack the guess data into the state vector. % Next. the user is asked to input the initial guess of the solution.'off'. end % Unstack and print the results.exitflag.Physical. int2str(exitflag)]). [x_state.34/www/CSTR_SS_files. disp(' '). . check_real.Rxn).x_guess. we set the solver parameters for the MATLAB nonlinear % equation solver. http://web. check_real..options. disp('Input the guess of the solution : ').. check_sign=1.num_species)..check_sign. .fval..ProbDim. check_int=0..Reactor..Inlet. error(['CSTR_SS: fsolve exited with error = '.output] = .'Display'. % First.. for ispecies = 1:ProbDim..0.check_sign. ProbDim... Guess..MATLAB Files for General CSTR Reactor Model Page 7 of 35 end % Next. end check_real=1. prompt = 'Guess reactor temperature : '.num_species check_real=1. int2str(ispecies).mit.edu/10. ' : '].. State = unstack_state(x_state.. if(exitflag <=0) iflag_main = -2.. .htm 7/16/2002 .conc(ispecies) = get_input_scalar(prompt. check_sign=1. 'LargeScale'.num_species). 1e-8.Temp = get_input_scalar(prompt. prompt = ['Guess concentration of species # '. .check_int)..num_species)'.'off'). fsolve(@CSTR_SS_calc_f. .conc = linspace(0. 34/www/CSTR_SS_files. iflag_main = 1.mat.mit. disp(' '). http://web. disp('Calculated temperature : ').MATLAB Files for General CSTR Reactor Model Page 8 of 35 disp(' '). disp(State. disp('Calculated concentrations : ').Temp). save CSTR_SS_results.htm 7/16/2002 .conc). % Save the results to a binary output file. return. disp(State.edu/10. Cp_data(1.Temp = 1.Rxn. 0].num_rxn = 2. Reactor. % Set number of reactions ProbDim.Inlet.T_cool = 1. Reactor. CSTR_SS_input1().:) = [1 0 0 0]. Inlet.ProbDim. % Allocate storage for reaction data Rxn.:) = [1 0 0 0]. Physical. % Set number of species ProbDim.m This is an example input file for the simulation.num_species = 4. Reactor. Reactor.Cp_data(4.Cp_data(2. Reactor.U_HT = 1000. 2. % Set heat capacity data Physical.. 0.edu/10.A_HT = 2*pi*radius*height.F_cool = 1000.stoich_coeff = zeros(ProbDim. Reactor.m % % K.Cp_data(3.F_vol = 20.Cp_cool = 1.mit.. Reactor. % Set reactor data radius = 5.conc = [1.:) = [1 0 0 0]. Physical. % Set inlet concentrations and temperature Inlet. iflag = 0.Physical.num_rxn. % CSTR_SS\CSTR_SS_input4. Beers % MIT ChE % 10/20/2001 function [ProbDim.num_species).MATLAB Files for General CSTR Reactor Model Page 9 of 35 File: CSTR_SS_input4.Reactor.34/www/CSTR_SS_files.iflag] = .htm 7/16/2002 .m % % This input file sets parameters for a steady state % calculation of a single CSTR using the program CSTR_SS. height = 10.:) = [1 0 0 0].volume = height*pi*radius^2. Physical. http://web. Rxn. Rxn. % Set reaction # 2 data irxn = 2. return. Rxn.htm 7/16/2002 . Rxn.k_ref(irxn) = 0.is_rxn_elementary(irxn) = 0.num_rxn)'.E_activ(irxn) = 10.ratelaw_exp(irxn. Rxn.ProbDim. % Set reaction # 1 data irxn = 1.T_ref = linspace(0. iflag = 1.:) = [-1 -2 1 0].MATLAB Files for General CSTR Reactor Model Page 10 of 35 Rxn.0.is_rxn_elementary(irxn) = 0. Rxn.k_ref(irxn) = 1. Rxn.num_rxn)'.E_activ = linspace(0. Rxn.:) = [1 2 0 0].ProbDim.is_rxn_elementary = linspace(0. Rxn.mit.stoich_coeff(irxn.ProbDim.num_rxn)'.edu/10.0.delta_H(irxn) = -1. Rxn.k_ref = linspace(0.ProbDim.:) = [0 -1 -1 1].:) = [0 1 1 0]. Rxn.E_activ(irxn) = 1. Rxn.ProbDim. http://web.0. Rxn.ratelaw_exp = zeros(ProbDim. Rxn.num_rxn.stoich_coeff(irxn.T_ref(irxn) = 1.ratelaw_exp(irxn. Rxn.num_rxn)'. Rxn.34/www/CSTR_SS_files. Rxn. Rxn.T_ref(irxn) = 1.delta_H = linspace(0.num_species).0.0.ProbDim.delta_H(irxn) = -1.num_rxn)'. Rxn.1. mit.. % REACTOR DATA ----------------------------------------% PDL> Input first the reactor volume and heat transfer data : disp(' ').MATLAB Files for General CSTR Reactor Model Page 11 of 35 File: read_program_input. disp(' '). read_program_input().Physical. disp('The parameters are input in real units. disp(' ').Physical. disp(' '). disp('Input the system parameters : ').Reactor. http://web.Reactor. disp(' ').edu/10. disp('Input the reactor data : ').m % CSTR_SS\read_program_input.Inlet.Inlet. disp(' E = unit of energy').m % % function [ProbDim. % % This procedure reads in the simulation parameters that are % required to define a single CSTR simulation.edu % 10/20/2001 % % Version as of 10/20/2001 function [ProbDim. where ').iflag] = . func_name = 'read_program_input'. disp(' L = unit of length'). disp(' M = unit of mass').34/www/CSTR_SS_files. % read_program_input(). disp(' T = unit of temperature').Rxn. disp(' t = unit of time'). iflag = 0..iflag] = .Rxn.. % % Kenneth Beers % Massachusetts Institute of Technology % Department of Chemical Engineering % [email protected] 7/16/2002 .. positive.edu/10.A_HT check_real=1. the name % of the function that is making the assertion. % Reactor. Reactor.A_HT = get_input_scalar(prompt. disp(' '). prompt = 'Input volumetric flowrate through reactor (L^3/t) : '. check_real. prompt = 'Input the coolant inlet temperature (T) : '.MATLAB Files for General CSTR Reactor Model Page 12 of 35 % Perform assertion that a real scalar positive number has % been entered. Reactor. prompt = 'Input the jacket heat transfer coefficient (E/t/(L^2)/T) : '.check_int). .check_int). check_sign=1.. check_real. and values of % 1 for the three flags that tell the assertion routine to make % sure the value is real.. check_real. .check_int).... % Reactor.F_cool = get_input_scalar(prompt.mit.check_sign. check_int=0. check_int=0.. check_real. prompt = 'Input the coolant flowrate (L^3/t) : '. check_real. % Reactor. and not to check that it is % an integer. Reactor.Cp_cool http://web. .volume = get_input_scalar(prompt.. Reactor. % Reactor.check_sign. check_sign=1. Reactor.F_cool check_real=1...check_int). % Reactor. prompt = 'Input the jacket heat transfer area (L^2) : '. check_int=0.. check_sign=1..F_vol check_real=1. check_int=0. disp('Reactor coolant information : ').check_int). prompt = 'Input the volume of the reactor (L^3) : '. . check_sign=1.U_HT check_real=1. % Reactor. This is performed by a function assert_scalar % that gives first the value and name of the variable.check_sign.check_sign.34/www/CSTR_SS_files.U_HT = get_input_scalar(prompt.check_sign. check_int=0. check_sign=1.volume check_real=1.Temp_cool check_real=1. % Reactor..htm 7/16/2002 . disp('Reactor size information : ').check_int). check_real.T_cool = get_input_scalar(prompt. . Reactor.check_sign. check_int=0. check_sign=1. disp(' ').F_vol = get_input_scalar(prompt. . ..conc. Inlet..']). check_sign=1. . % ProbDim.. for ispecies = 1:ProbDim.edu/10. check_sign=2. ProbDim. check_int=1..Temp_in check_real=1.num_species check_real=1. prompt = 'Enter temperature of inlet : '. .conc_in(ispecies) check_real=1.. Inlet..check_int). prompt = 'Input the number of species : '.. Inlet. http://web. check_sign=1..check_int).check_real. check_int=0. disp(' ').. prompt. % PDL> Input number of species.num_species % Inlet. % PDL> Input inlet properties : % Inlet.num_species disp(' '). 'and temperature (T).Cp_cool = get_input_scalar(prompt. . disp(['Input the inlet concentrations (mol/L^3) '. prompt = ['Enter inlet concentration of species '. check_real.conc = linspace(0.check_sign. . check_int=0. check_int=0. ProbDim.0. prompt = 'Input the reactor coolant heat capacity (E/mol/T) : '.check_sign..ProbDim. ' : '].mit. int2str(ispecies).Temp disp(' '). end disp(' ').check_sign. .htm 7/16/2002 ..MATLAB Files for General CSTR Reactor Model Page 13 of 35 check_real=1. disp(' ').Temp = get_input_scalar(prompt. check_real.34/www/CSTR_SS_files.check_int). % Inlet.check_int). Reactor. disp(' ').check_sign. check_sign=1.conc(ispecies) = get_input_scalar( . Inlet.num_species = get_input_scalar(prompt.num_species)'. check_real. num_species. % PDL> Input the reaction data. check_real.Cp_data(ispecies.k_ref. disp(' '). check_sign=0.num_species prompt = ['Input molar heat capacity data of species '. % Physical. .check_int). ' : ']. % Rxn.check_int).check_sign.num_rxn = get_input_scalar(prompt.j) = . disp(' '). % ProbDim. . end end % REACTION DATA ------------------------------------------% PDL> Input the number of reactions.Cp_data(ispecies. Rxn. int2str(j-1). disp('Input molar heat capacities of each species (E/mol/T) : ').. % ProbDim. for j=1:4 prompt = ['Enter C'. one-by-one for each reaction : % Rxn. ' : ']. for ispecies = 1:ProbDim. check_real. disp(' ')..edu/10. Physical.. enter the kinetic data for the reaction network'). disp(' '). http://web. get_input_scalar(prompt. ProbDim. int2str(ispecies). check_int=1. disp('Temperature dependence : Cp = C0 + C1*T + C2*T^2 + C3*T^3').num_rxn disp(' ').htm 7/16/2002 . prompt = 'Enter the number of reactions : '. disp('Now...stoich_coeff. .34/www/CSTR_SS_files.mit.. check_int=0.ratelaw_exp.:) check_real=1.Cp_data = zeros(ProbDim. disp(prompt).. Rxn. disp(' ').Cp_data(num_species.4).num_rxn check_real=1.4) Physical..is_rxn_elementary.check_sign. disp(' ').MATLAB Files for General CSTR Reactor Model Page 14 of 35 % PHYSICAL DATA -------------------------------------------% PDL> Input physical data : % Physical. check_sign=1. num_species % Rxn.ProbDim.stoich_coeff(irxn.0.ProbDim. disp(' ').ispecies) check_real=1. disp(' '). Rxn. end http://web.num_species). Rxn. int2str(irxn)]).0.stoich_coeff = zeros(ProbDim. Rxn.num_rxn)'.num_rxn.ratelaw_exp = zeros(ProbDim. Rxn. Rxn. check_real. check_sign=0. Rxn. for species # '.ProbDim.num_rxn % We use a while loop to repeat the process of inputing the % reaction network until we accept it.ProbDim.ProbDim.ProbDim.. ' : '].num_rxn.0. for ispecies = 1:ProbDim.0. .. Rxn...T_ref = linspace(0.34/www/CSTR_SS_files.num_rxn)'. disp(' '). . int2str(ispecies).htm 7/16/2002 . Rxn. get_input_scalar(prompt.ispecies) = ..E_activ = linspace(0.E_activ. disp('Now enter the kinetic data for each reaction.num_rxn)'. iflag_accept_Rxn = 0. coeff.is_rxn_elementary = linspace(0.. disp(' '). check_int=0.delta_H = linspace(0. Rxn.num_rxn)'. This is because % inputing the kinetic data is most prone to error.check_int). while (iflag_accept_Rxn ~= 1) disp(' ').edu/10.0.ProbDim. disp(' ').T_ref. disp('Stoichiometric coefficients ---')..delta_H % allocate a structure for the reaction data Rxn.num_rxn)'. prompt = ['Enter stoich. disp(' '). .check_sign.k_ref = linspace(0.MATLAB Files for General CSTR Reactor Model % Page 15 of 35 Rxn.mit..num_species). for irxn = 1:ProbDim. disp(['Enter kinetic data for reaction # '.stoich_coeff(irxn.'). disp(' '). for ispecies = 1:ProbDim. find(Rxn.num_species % Rxn.ratelaw_exp(irxn.is_rxn_elementary(irxn) == 1) % initialize ratelaw_exp values to zero Rxn..edu/10..ratelaw_exp(irxn. int2str(ispecies) ' : '].. ['Enter the rate law exponent for species # '.ispecies) = . Rxn. linspace(0.htm 7/16/2002 ... % if the reaction is elementary. check_real.. -Rxn. prompt = ..check_sign.. % Rxn.. '(1 = yes.. get_input_scalar(prompt. . we need % to input separate values of the rate % law exponents else disp(' '). % make a list of all reactants list_reactants = .ratelaw_exp(irxn. check_int=1. 0 = no) : ']... check_sign=2. .34/www/CSTR_SS_files.ispecies).ProbDim.is_rxn_elementary(irxn) = .is_rxn_elementary(irxn) check_real=1.ispecies) check_real=1. the rate law exponent is the % negative of the stoichiometric coefficient for i_reactant = 1:length(list_reactants) ispecies = list_reactants(i_reactant).num_species). Rxn.ratelaw_exp(irxn.:) < 0). % for each reactant... prompt = ['Is this reaction elementary ? '. then the %rate law exponents can be obtained directly from % the stoichiometry coefficients if(Rxn.mit. end % if the reaction is not elementary.MATLAB Files for General CSTR Reactor Model Page 16 of 35 disp(' '). check_sign=0..check_int).. Rxn. .stoich_coeff(irxn.ispecies) = .stoich_coeff(irxn.0. http://web. check_int=0...:) = . % Rxn. end end % Now.k_ref(irxn) check_real=1. prompt. check_int=0. check_int=0.E_activ(irxn) = get_input_scalar( .check_sign. check_real. check_sign=1.k_ref(irxn) = get_input_scalar(prompt. check_int=0.T_ref(irxn) = get_input_scalar( . check_int=0. % Rxn.check_int). check_sign=2. disp(' '). Rxn. % Rxn. disp(' ').check_int). % Rxn.check_real. enter the reference rate law constants disp(' '). .E_activ(irxn) check_real=1. http://web. check_sign=2.mit. prompt = 'Enter the rate constant at reference T : '. check_real. Rxn.. prompt = ['Enter activation energy divided '..check_int).T_ref(irxn) check_real=1..MATLAB Files for General CSTR Reactor Model Page 17 of 35 get_input_scalar(prompt.check_int). . Rxn.htm 7/16/2002 . check_sign=0.check_real. the activation energy (divided by the value of % the ideal gas constant in the chosen units) and the heat % of reaction are input.delta_H(irxn) = get_input_scalar(prompt. prompt = ['Enter the kinetic data reference '. prompt.check_sign... disp(' '). 'temperature (T) : ']..34/www/CSTR_SS_files. .check_sign.check_int). 'by gas constant (T) : ']..delta_H(irxn) check_real=1.. prompt = 'Enter the heat of reaction (E / mol) : '. . % Finally . Rxn. check_real.edu/10...check_sign.check_sign.. disp(' '). .... else disp('Reaction is NOT elementary').. num2str(Rxn. ' '. list_reactants = . disp([int2str(ispecies). coeff. disp(' ').is_rxn_elementary(irxn) == 1) disp('Reaction is elementary')....ratelaw_exp(irxn. disp('Read-back of entered kinetic data : ').edu/10. : ').34/www/CSTR_SS_files. :'). for count = 1:length(list_ratelaw_species) ispecies = list_ratelaw_species(count). ' '. disp('Product species and stoich. disp('Rate law exponents : ').ispecies))]). for count=1:length(list_products) ispecies = list_products(count).. if(Rxn. disp([int2str(ispecies). disp(' '). num2str(Rxn.:) > 0). . .:) ~= 0).stoich_coeff(irxn. list_ratelaw_species = .mit.. coeff.htm 7/16/2002 . num2str(Rxn. disp(' '). end % Write the kinetic data. find(Rxn.:) < 0)..stoich_coeff(irxn.ispecies))]).. find(Rxn.. disp(' '). disp(' '). disp('Reactant species and stoich. list_products = .MATLAB Files for General CSTR Reactor Model Page 18 of 35 % We now write out the kinetic data to the % screen and ask if this is to be accepted. find(Rxn.stoich_coeff(irxn.ispecies))]). end % Tell whether the reaction is elementary.. for count=1:length(list_reactants) ispecies = list_reactants(count). disp(' ').ratelaw_exp(irxn. ... % List the reactants. end % List the products. end % Write the ratelaw exponents disp(' '). http://web.stoich_coeff(irxn. ' '. disp([int2str(ispecies). num2str(Rxn.check_sign=2.mit.. disp(' '). disp(' ').E_activ(irxn))]). http://web. disp([ 'k_ref = '. end % for while loop to accept data end % irxn for loop iflag = 1.check_int). check_real.edu/10. num2str(Rxn. disp(' ').delta_H(irxn))]).T_ref(irxn))]).check_int=1. disp(' ').k_ref(irxn))]). check_real=1. num2str(Rxn. disp([ 'delta_H = '. return. .MATLAB Files for General CSTR Reactor Model Page 19 of 35 disp([ 'T_ref = '.htm 7/16/2002 . num2str(Rxn. 1=yes) : '.. iflag_accept_Rxn = get_input_scalar(prompt. disp([ 'E_activ = '.34/www/CSTR_SS_files.check_sign. prompt = 'Accept these rate parameters? (0=no. num_species).check_sign = 2.name = 'conc'.htm 7/16/2002 . FieldType. % This flag controls what to do in case of an % assertion failure. % check for errors in input parameters % check dimensions assert_scalar(1.conc ifield = 1. FieldType. iflag = 0. % % This procedure stacks the concentration and temperature % data into a single master array. FieldType. func_name.1. func_name = 'stack_state'.is_numeric = 1.iflag] = stack_state(State. FieldType.check_real = 1. % . . FieldType.edu % 10/20/2001 % % Version as of 10/20/2001 function [x_state. http://web.num_species.. i_error = 2.mit..m % % function [x_state. % check if State is proper structure type StateType.m % CSTR_SS\stack_state.1.1).num_rows = num_species. See the assertion routines % for further details.edu/10.num_fields = 2.num_species).iflag] = stack_state(State.34/www/CSTR_SS_files. % % % Kenneth Beers % Massachusetts Institute of Technology % Department of Chemical Engineering % [email protected] Files for General CSTR Reactor Model Page 20 of 35 File: stack_state.'num_species'. FieldType.num_columns = 1. FieldType.. % call assertion routine for structure assert_structure(i_error.is_numeric = 1.check_int = 0. we stack the concentrations %PDL> Set pos_counter to zero pos_counter = 0.StateType). we stack the temperature x_state(pos_counter+1) = State.'State'. FieldType.conc(ispecies). %PDL> ENDFOR end %PDL> Next.Temp. x_state = linspace(0.name = 'Temp'.num_DOF)'.check_real = 1.check_sign = 1.htm 7/16/2002 .34/www/CSTR_SS_files. http://web. FieldType.field(ifield) = FieldType. FieldType.num_columns = 1. % PDL> Increment pos_counter by num_pts pos_counter = pos_counter + 1. func_name.State. StateType.MATLAB Files for General CSTR Reactor Model Page 21 of 35 FieldType. % allocate x_state column vector and % initialize to zeros num_DOF = num_species+1. StateType..num_species for ispecies = 1:num_species x_state(pos_counter+1) = State. %PDL> First. % . . FieldType.check_int = 0. FieldType.field(ifield) = FieldType. %PDL> FOR ispecies FROM 1 TO ProbDim.edu/10.mit. FieldType.num_rows = 1.Temp ifield = 2.0. mit.MATLAB Files for General CSTR Reactor Model Page 22 of 35 iflag = 1.htm 7/16/2002 .edu/10. return.34/www/CSTR_SS_files. http://web. 'num_species'. assert_vector(i_error.conc = linspace(0. . % Allocate space for State data. iflag = 0. check_int=1.MATLAB Files for General CSTR Reactor Model Page 23 of 35 File: unstack_state. i_error = 2.check_sign. See the assertion routines % for further details. % This flag controls what to do in case of an % assertion failure.0. func_name.. % % This procedure stacks the concentration and temperature % data into a single master array.'x_state'.num_species).iflag] = unstack_state(x_state.check_int). check_column = 1.check_int.check_column).m % CSTR_SS\unstack_state.x_state.edu/10. % check for errors in input parameters % check dimensions check_real=1.check_sign.iflag] = unstack_state(x_state. % check x_state vector num_DOF = num_species + 1. .. check_sign=0. % % % Kenneth Beers % Massachusetts Institute of Technology % Department of Chemical Engineering % kbeers@mit. assert_scalar(i_error. check_real.num_species)'. func_name = 'unstack_state'.num_species..mit.htm 7/16/2002 .34/www/CSTR_SS_files.check_real. State. check_real=1. check_sign=1. check_int=0.edu % 10/20/2001 % % Version as of 10/20/2001 function [State.func_name. http://web..m % % function [State. num_species).num_DOF. MATLAB Files for General CSTR Reactor Model Page 24 of 35 State.conc(ispecies) = x_state(pos_counter+1). %PDL> FOR ispecies FROM 1 TO ProbDim. %PDL> ENDFOR end %PDL> Next.num_species for ispecies = 1:num_species State.34/www/CSTR_SS_files. we stack the temperature State.Temp = 0. return.Temp = x_state(pos_counter+1). http://web. iflag = 1.edu/10. %PDL> First. % PDL> Increment pos_counter by num_pts pos_counter = pos_counter + 1.htm 7/16/2002 .mit. we unstack the concentrations %PDL> Set pos_counter to zero pos_counter = 0. iflag] = CSTR_SS_calc_f(x_state.. unstack the state vector.Temp^3..Reactor. Beers % MIT ChE % 10/20/2001 function [fval.0.Cp_data(ispecies.3)*Inlet. Physical. Physical.Cp_data(ispecies.mit. end Cp_in_avg = Cp_in_avg / sum(Inlet.num_species Cp_in(ispecies) = Physical..num_species)'... Cp_avg = Cp_avg + Cp(ispecies)*State..edu/10.Cp_data(ispecies..Cp_data(ispecies.ProbDim.4)*State. % molar average heat capactity of medium Cp_in = linspace(0. Cp_in_avg = 0......MATLAB Files for General CSTR Reactor Model Page 25 of 35 File: CSTR_SS_calc_f.Physical. Physical.conc(ispecies).0.conc(ispecies).conc).2)*Inlet.Cp_data(ispecies.Inlet. iflag = 0.Cp_data(ispecies. % molar average heat capacity of inlet for ispecies = 1:ProbDim. fval = linspace(0. State = unstack_state(x_state. Cp_avg = Cp_avg / sum(State. . % Next.ProbDim. ProbDim.num_species). Cp_in_avg = Cp_in_avg + Cp_in(ispecies)*State..num_species + 1.num_species)'.Cp_data(ispecies.Temp^3. Cp_avg = 0.3)*State. http://web.1) + .1) + . Cp(ispecies) = Physical. calculate the heat capacities for the inlet and % reactor temperatures for each species.num_DOF).4)*Inlet. Physical.0..Temp^2 + .ProbDim. num_DOF = ProbDim.Temp^2 + .Rxn).m % % This MATLAB file calculates the vector of function residuals that is % zero for a solution to the set of equations that models a steady-state % CSTR with an arbitrarily complex reaction network. Physical. % Next. % % K.Temp + .2)*State.34/www/CSTR_SS_files. Physical.htm 7/16/2002 .m % CSTR_SS_calc_f.Temp + . Cp = linspace(0.conc).Cp_data(ispecies. .mit. % enthalpy flux to coolant jacket Q_cool = Reactor..Cp_avg).conc(ispecies)). (State. Rxn.ispecies)*RxnRate..num_rxn source = source . http://web..ProbDim.stoich_coeff(irxn. for ispecies = 1:ProbDim. (1 ...U_HT*Reactor. if(iflag2 <= 0) iflag = iflag2.rate(irxn). int2str(iflag2)]).. end % enthalpy balance iDOF = ProbDim. iflag2] = reaction_network_model(...delta_H(irxn)*RxnRate...Reactor.1.State.Temp.34/www/CSTR_SS_files.num_rxn.conc(ispecies)*Cp(ispecies)*State. [RxnRate. .F_vol/Reactor. .num_rxn source = source + .num_species + 1.. end % Calculate the function vector. % enthalpy due to net chemical reaction source = 0.num_species fval(iDOF) = fval(iDOF) + . Inlet.Cp_cool* .num_species. error(['CSTR_SS_calc_f: reaction_network_model returns error = '.MATLAB Files for General CSTR Reactor Model Page 26 of 35 % Next.htm 7/16/2002 ..volume. end fval(iDOF) = fval(iDOF)*Reactor.exp(-Reactor..edu/10. end fval(iDOF) = fval(iDOF) + source.conc(ispecies) . for irxn = 1:ProbDim.volume * . source = 0.F_cool/Reactor.F_cool*Reactor.F_vol/Reactor. % Mass Balances on each species for ispecies = 1:ProbDim.conc(ispecies)*Cp_in(ispecies)*Inlet.T_cool) * ..Temp .Temp .A_HT/Reactor.rate(irxn).. ProbDim..conc.num_species fval(ispecies) = Reactor.Rxn. end fval(ispecies) = fval(ispecies) + source. for irxn = 1:ProbDim.Rxn. State.State. State.. % inlet/outlet fluxes of enthalpy fval(iDOF) = 0.Cp_cool)).Temp.. calculate the reaction rate using the general reaction network. (Inlet. Q_cool/Reactor. iflag = 1. http://web.edu/10.volume. return.htm 7/16/2002 .MATLAB Files for General CSTR Reactor Model Page 27 of 35 fval(iDOF) = fval(iDOF) .mit.34/www/CSTR_SS_files. . % reaction_network_model(num_species.edu/10. then the % rate law exponents are zero for the % product species and the negative of the % stoichiometric coefficient for the http://web.Cp).MATLAB Files for General CSTR Reactor Model Page 28 of 35 File: reaction_network_model.m % CSTR_SS\reaction_network_model. % . The rate laws are characterized by the % product of each concentration raised to an % exponential power...num_rxn.density.Temp_loc. . % Also.Rxn. according to an Arrhenius expression based % on an activation energy and the value of the rate % constant at a specified reference temperature. % conc_loc.stoich_coeff REAL(num_rxn. % % This procedure evaluates the rates of each reaction % and the derivatives of the rates with respect to the % concentrations and temperature for a general reaction % network. % .is_rxn_elementary INT(num_rxn) % if a reaction is elementary..num_species) % the stoichiometric coefficients % possibly fractional) of each % species in each reaction.m % % function [RxnRate.htm 7/16/2002 . iflag] = .mit. the contributions to the time derivatives of % the concentrations and the temperature due to the % total effect of reaction are returned.num_species) % the exponential power (possibly fractional) % to which the concentration of each species % is raised each reaction's rate law.34/www/CSTR_SS_files. The fields % are : % .ratelaw_exp REAL(num_rxn. % % INPUT : % ======= % num_species INT % The number of species % num_rxn INT % The number of reactions % conc REAL(num_species) % This is a column vector of the concentrations % of each species at a single point % Temp REAL % This is the temperature at a single point % % Rxn This structure contains the kinetic data % for the general reaction network. The rate constants are temperature % dependent. MATLAB Files for General CSTR Reactor Model Page 29 of 35 % reactant species. % Otherwise (default = 0).rate_deriv_c REAL(num_rxn. % . In this case.34/www/CSTR_SS_files.edu/10. we assume that % the reaction is not elementary and require % the user to input the values of % ratelaw_exp for reaction # irxn. we need % not enter the corresponding components of % ratelaw_exp since these are determined by % the corresponding values in stoich_coeff.T_ref REAL(num_rxn) % This is the value of the reference % temperature used to specify the % temperature dependence of each % rate constant.E_activ REAL(num_rxn) % the constant activation energies of % each reaction divided by the ideal % gas constant % .mit. % .delta_H REAL(num_rxn) % the constant heats of reaction % % density REAL % the density of the medium % Cp REAL % the heat capacity of the medium % % OUTPUT : % ======== % RxnRate data structure containing the following fields : % .time_deriv_T REAL % this is the time derivative of the temperature due to % the effect of all the reactions % .time_deriv_c REAL(num_species) % this is a column vector of the time derivatives of the % concentration due to all reactions % . % We specify that reaction number irxn is % elementary by setting % is_rxn_elementary(irxn) = 1.rate REAL(num_rxn) % this is a column vector of the rates of each reaction % .num_species) % this is a matrix of the partial derivatives of each reaction % rate with respect to the concentrations of each species % .k_ref REAL(num_rxn) % the rate constants of each reaction at a % specified reference temperature % .rate_deriv_T REAL(num_rxn) % this is a column vector of the partial derivatives of each % reaction rate with respect to the temperature %k REAL(num_rxn) % this is a column vector of the rate constant values at the % current temperature http://web.htm 7/16/2002 . func_name.. check_sign=1. % % Kenneth Beers % Massachusetts Institute of Technology % Department of Chemical Engineering % [email protected]_int).. assert_vector(i_error.num_rxn.num_species.'num_species'.. % this integer flag controls the action taken % when an assertion fails.MATLAB Files for General CSTR Reactor Model Page 30 of 35 % . check_int=1. .check_sign.check_column)..check_sign. % num_rxn check_real=1. http://web. See the assertion % routines for a description of its use. check_int=1. check_real=1. func_name = 'reaction_network_model'.34/www/CSTR_SS_files.num_rxn.check_real... % the source term value is [A]*[B]^2. reaction_network_model(num_species. assert_scalar(i_error.conc_loc.mit.check_real.dim. . . check_sign=1.edu/10. conc_loc. func_name. check_sign=0.check_int).'conc_loc'. . assert_scalar(i_error.'num_rxn'.edu % 7/2/2001 % % Version as of 7/25/2001 function [RxnRate.Temp_loc.Cp). in the rate law : % R = k*[A]*[B]^2. check_int=0.check_real. make sure all concentrations are non-negative list_neg = find(conc_loc < 0). check_int.Rxn. iflag = 0. % Check input % num_species check_real=1... i_error = 1. func_name. ...check_sign.. iflag] = .density. % conc_loc dim = num_species.. % now.htm 7/16/2002 . % For example.source_term REAL(num_rxn) % this is a column vector of the values in the rate law expression % that are dependent on concentration. check_column=0. FieldType. http://web.num_columns = num_species. FieldType. FieldType.edu/10. check_int=0.check_int = 1.field(ifield) = FieldType.check_sign.check_real = 1. FieldType.Temp_loc. end % Temp_loc check_real=1. check_sign=0. FieldType.num_rows = num_rxn. RxnType.struct_name = 'Rxn'. FieldType..check_sign = 0.htm 7/16/2002 .is_numeric = 1.is_numeric = 1. % Now set the assertion properties of each field.num_rows = num_rxn. FieldType. FieldType.is_numeric = 2.check_sign = 0.name = 'is_rxn_elementary'. conc_loc(ispecies) = 0.'Temp_loc'. % make sure the temperature is positive trace = 1e-20. FieldType.field(ifield) = FieldType. FieldType. FieldType.mit. % .field(ifield) = FieldType. FieldType.num_fields = 7.num_rows = num_rxn. FieldType.MATLAB Files for General CSTR Reactor Model Page 31 of 35 for count=1:length(list_neg) ispecies = list_neg(count). assert_scalar(i_error.check_int = 0. FieldType. ..ratelaw_exp ifield = 2. RxnType.k_ref ifield = 4.is_rxn_elementary ifield = 3. RxnType. FieldType.check_int = 0. % .check_real. FieldType. % . FieldType.num_columns = num_species.name = 'ratelaw_exp'.34/www/CSTR_SS_files. FieldType. FieldType.name = 'stoich_coeff'.stoich_coeff ifield = 1. end % Rxn RxnType.check_int). func_name.check_sign = 2. FieldType. if(Temp_loc <= trace) Temp_loc = trace. RxnType.check_real = 1.check_real = 1.num_columns = 1. % . FieldType. num_rows = num_rxn. FieldType. FieldType.num_columns = 1. FieldType. FieldType..num_columns = 1.check_int). FieldType. FieldType. check_int=0.is_numeric = 1.num_rows = num_rxn.name = 'delta_H'.check_real = 1.Cp. func_name.. FieldType.check_sign = 0. FieldType. % . FieldType. RxnType.delta_H ifield = 7. http://web. check_sign=1. FieldType.field(ifield) = FieldType.'density'.E_activ ifield = 6. FieldType. RxnType.field(ifield) = FieldType.is_numeric = 1. FieldType. FieldType.field(ifield) = FieldType.mit. func_name. assert_scalar(i_error.is_numeric = 1.check_sign.check_int = 0. RxnType. FieldType. assert_scalar(i_error.check_real = 1. FieldType.check_sign = 1.. .'Rxn'.check_int = 0.num_rows = num_rxn.check_real = 1.is_numeric = 1.RxnType).check_real. FieldType. FieldType. FieldType.num_rows = num_rxn.check_int = 0. FieldType.num_columns = 1.'Cp'. FieldType. FieldType.density. FieldType. % .check_int = 0.edu/10.name = 'k_ref'.T_ref ifield = 5. FieldType. % density check_real=1.check_int). % call assertion routine for structure assert_structure(i_error. FieldType. % heat capacity check_real=1. FieldType. check_int=0. FieldType.num_columns = 1.check_real = 1.name = 'T_ref'. % .check_real.check_sign = 2. FieldType.htm 7/16/2002 .check_sign = 2.Rxn.check_sign. check_sign=1.MATLAB Files for General CSTR Reactor Model Page 32 of 35 FieldType.name = 'E_activ'.func_name. .field(ifield) = FieldType.. RxnType.34/www/CSTR_SS_files. 0..k(irxn) * . (1/Temp_loc . RxnRate.E_activ(irxn)/(Temp_loc^2). calculate the rates and % their derivatives with respect to the % concentrations and temperatures % FOR irxn FROM 1 TO num_rxn for irxn = 1:num_rxn %PDL> Calculate rate constant at the current temperature factor_T = exp(-Rxn.mit.E_activ(irxn) * . ratelaw_vector(ispecies) = . %PDL> For every reaction.ratelaw_exp(irxn.ratelaw_exp(irxn. %PDL> Calculate the derivative of the rate constant with % respect to temperature d_rate_k_d_Temp = RxnRate. %PDL> Set ratelaw_vector to be of length num_species whose % elements are the concentrations of each species % raised to the power ratelaw_exp(irxn.34/www/CSTR_SS_files.MATLAB Files for General CSTR Reactor Model Page 33 of 35 %PDL> Initialize all output variables to zeros RxnRate. RxnRate. RxnRate.ispecies). RxnRate.rate = linspace(0.1.htm 7/16/2002 .ispecies). ratelaw_vector = linspace(1.time_deriv_T = 0.T_ref(irxn)))..num_rxn)'.. RxnRate.edu/10. RxnRate. automatically set corresponding % element to 1. for count=1:length(list_species) ispecies = list_species(count).source_term = linspace(0..num_rxn)'.num_rxn)'.:) ~= 0).rate_deriv_c = zeros(num_rxn.0.k_ref(irxn)*factor_T.rate_deriv_T = linspace(0..0. list_species = find(Rxn..time_deriv_c = linspace(0.0. % If the exponent is 0.1/Rxn.num_species)'. RxnRate.0.k(irxn) = Rxn. Rxn.num_rxn)'.num_species). conc_loc(ispecies) ^ Rxn. end %PDL> Calculate the ratelaw source term that is the product % of all elements of ratelaw_vector http://web.num_species)'.k = linspace(0. MATLAB Files for General CSTR Reactor Model Page 34 of 35 RxnRate. http://web..mit. RxnRate. %PDL> Set rxn_rate_deriv_T(irxn) to be equal to the product of % the temperature derivative of the rate constant times the % ratelaw source term RxnRate.ispecies)-1) % If ratelaw_exp(irxn.k(irxn) * .. d_rate_k_d_Temp * RxnRate. then do % special case where replace element with 1 vector_work = ratelaw_vector..ratelaw_exp(irxn. %PDL> Set vector_work = ratelaw_vector and replace the % ispecies element with % ratelaw_exp(irxn. else exponent = Rxn.ispecies)* % conc(ispecies)^(ratelaw_exp(irxn. %PDL> FOR EVERY ispecies WHERE % ratelaw_exp(irxn.rate_deriv_T(irxn) = . vector_work(ispecies) = exponent * .ratelaw_exp(irxn.htm 7/16/2002 .rate(irxn) = RxnRate.source_term(irxn).ispecies).source_term(irxn).rate_deriv_c(irxn.edu/10. RxnRate.ispecies) IS non-zero for count=1:length(list_species) ispecies = list_species(count).k(irxn) * prod(vector_work).ispecies) equal to the product of all components of this vector multiplied by the rate constant RxnRate.. %PDL> The rate of reaction # irxn is equal to the product of % the ratelaw source term with the value of the rate constant RxnRate. if(Rxn... (conc_loc(ispecies) ^ (exponent-1))..source_term(irxn) = prod(ratelaw_vector).ispecies) == 1) vector_work(ispecies) = 1. end % % % PDL> Set rxn_rate_deriv_c(irxn.ispecies) is exactly 1.34/www/CSTR_SS_files.ispecies) = .. stoich_coeff(irxn..rate(irxn).mit.time_deriv_c(ispecies) + . % % % PDL> Increment rxn_time_deriv_c(ispecies) by Rxn..rate(irxn).htm 7/16/2002 . %PDL> ENDFOR over reactions end iflag = 1..stoich_coeff(irxn.ispecies) IS non-zero list_species = find(Rxn.. (Rxn.34/www/CSTR_SS_files.edu/10.. for count=1:length(list_species) ispecies = list_species(count). return..time_deriv_c(ispecies) = . http://web.time_deriv_T ..:) ~= 0).stoich_coeff(irxn..stoich_coeff(irxn. % PDL> ENDFOR over participating species end % % % % PDL> Increment rxn_time_deriv_T by the negative of Rxn. RxnRate. RxnRate..ispecies) * .time_deriv_T = RxnRate.delta_H(irxn)/density/Cp)*RxnRate.ispecies) multiplied with the rxn_rate(irxn) RxnRate.MATLAB Files for General CSTR Reactor Model % Page 35 of 35 PDL> ENDFOR for sum over participating species end % % PDL> FOR EVERY ispecies WHERE Rxn. Rxn.delta_H divided by the product of density and heat capactity and then multiply by rxn_rate(irxn) RxnRate.
Copyright © 2024 DOKUMEN.SITE Inc.