Chemical Reactor

ken126
reactor.py

function reactor( ) %Graphical reactor simulator. This function loads data from %expermimentaldata.dat and uses the students functions to simulate the %reaction %Some constants timeInterval=10; runtime=3*3600; % Add the UI components hs = addcomponents; % Make figure visible after adding components hs.fig.Visible = 'on'; function hs = addcomponents % add components, save handles in a struct hs.fig = figure('Visible','off',... 'Resize','off',... 'Tag','fig',... 'Name','Reactor Simulator',... 'NumberTitle','off',... 'ToolBar','none',... 'MenuBar','none'); hs.fig.Position=[hs.fig.Position(1:2) 800 hs.fig.Position(4:4)]; hs.cpanel=uipanel(hs.fig,'Position',[.01 .35 .11 .55],... 'Title','Simulate'); hs.exp1 = uicontrol('Parent',hs.cpanel,'Position',[5 180 75 30],... 'String','Data Set 1',... 'Tag','button',... 'Callback',@simrun); hs.exp2 = uicontrol('Parent',hs.cpanel,'Position',[5 140 75 30],... 'String','Data Set 2',... 'Tag','button',... 'Callback',@simrun); hs.exp3 = uicontrol('Parent',hs.cpanel,'Position',[5 100 75 30],... 'String','Data Set 3',... 'Tag','button',... 'Callback',@simrun); hs.exp4 = uicontrol('Parent',hs.cpanel,'Position',[5 60 75 30],... 'String','Data Set 4',... 'Tag','button',... 'Callback',@simrun); hs.exp5 = uicontrol('Parent',hs.cpanel,'Position',[5 20 75 30],... 'String','Data Set 5',... 'Tag','button',... 'Callback',@simrun); hs.reactorVessel=uipanel('Parent',hs.fig,... 'Position',[0.15 0.1 0.21 0.85],... 'Tag','p1',... 'BackgroundColor','w',... 'Title','Reactor Vessel'); hs.accumulant=uipanel('Parent',hs.reactorVessel,... 'Position',[0 0 1 0],... 'Tag','p1',... 'BackgroundColor','b',... 'BorderType','none',... 'BorderWidth',0 ); hs.simWindow=uipanel('Parent',hs.fig,... 'Position',[.38 .1 .6 .85],... 'Title','Status (Stopped)'); hs.simTime=uicontrol('Parent',hs.simWindow,... 'Position',[5 300 150 20],... 'HorizontalAlignment','left',... 'Style','text',... 'FontWeight','bold',... 'String','Time (h:m:s): 00:00:00'); hs.simAmount=uicontrol('Parent',hs.simWindow,... 'Position',[170 300 150 20],... 'HorizontalAlignment','left',... 'Style','text',... 'FontWeight','bold',... 'String','Accum (g.): 0.0000'); hs.simGraph=axes('Parent',hs.simWindow,... 'Position',[.10 .15 .8 .65],... 'Tag','ax'); hs.simGraph.Title.String='Simulation Progress'; hs.simGraph.XAxis.Label.String='Time (minutes)'; hs.simGraph.YAxis.Label.String='Accum. (g.)'; end function updateUI(running,time,qty,maxVal,maxTime) %Updates the screen based on current state of the simulation if (running) hs.simWindow.Title='Status (Running)'; [h,m,s]=hms(seconds(time)); hs.simTime.String=sprintf('Time (h:m:s): %02d:%02d:%02d',h,m,s); hs.simAmount.String=sprintf('Accum (g.): %.4f',qty/1000.0); hs.simGraph.XLim=[0 180]; hs.simGraph.YLim=[0 maxVal/1000.0]; hs.simGraph.Title.String='Simulation Progress'; hs.simGraph.XAxis.Label.String='Time (minutes)'; hs.simGraph.YAxis.Label.String='Accum. (g.)'; plot(time/60.0,qty/1000.0,'o'); hold on; newHeight=qty/maxVal; if (newHeight<0) newHeight=0; end hs.accumulant.Position=[0 0 1 newHeight]; else hs.simWindow.Title='Status (Stopped)'; hold off; end end function enableButtons(enable) %enables or disables the buttons on the simulator. The parameter is a %boolean value indicating whether to enable (true) or disable(false). str='off'; if enable str='on'; end hs.exp1.Enable=str; hs.exp2.Enable=str; hs.exp3.Enable=str; hs.exp4.Enable=str; hs.exp5.Enable=str; end function simrun(hObject,event) %Button click handler for simulation start events. enableButtons(false); cla; data=[]; expdata1=[];expdata2=[];expdata3=[];expdata4=[];expdata5=[]; load('experimentalData.mat'); if (hObject==hs.exp1) data=expdata1; elseif (hObject==hs.exp2) data=expdata2; elseif (hObject==hs.exp3) data=expdata3; elseif (hObject==hs.exp4) data=expdata4; elseif (hObject==hs.exp5) data=expdata5; else enableButtons(true); return; end coef=bestFit(data); hs.accumulant.Position=[0 0 50 50]; %Figure out the maximum (3 hours) value 3*3600 seconds maxVal=simulateReactor(coef,runtime); updateUI(false,0,0,maxVal,runtime); time=1; val=0; for i=1:5:runtime val=simulateReactor(coef,i); updateUI(true,i,val,maxVal); time=i; pause(.001); end updateUI(0,time,val,maxVal,runtime); hold on; plot(0:timeInterval/60.0:(size(data,2)-1)/6.0,data./1000.0,'bx'); enableButtons(true); end end