{"id":50,"date":"2009-03-20T15:30:37","date_gmt":"2009-03-20T15:30:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/2009\/03\/20\/my-favorite-new-feature-in-r2009a-simstate\/"},"modified":"2017-04-06T08:43:19","modified_gmt":"2017-04-06T13:43:19","slug":"my-favorite-new-feature-in-r2009a-simstate","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2009\/03\/20\/my-favorite-new-feature-in-r2009a-simstate\/","title":{"rendered":"My Favorite New Feature in R2009a: SimState"},"content":{"rendered":"<p>I am so excited about\r\n<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/simulink\/ug\/bry62t3.html\"> \r\n    saving and restoring the simulation state in Simulink<\/a>.  This is \r\n    my favorite feature of R2009a because it has been long anticipated \r\n    and it enables important new capabilities for simulations.  While the \r\n    only interface change was the addition of a check box under Data \r\n    Import\/Export in the configuration parameters, this has affected \r\n    every block that stores some kind of state information, including \r\n    Stateflow. Who better to explain this than the developer who has spent the last \r\ncouple of years enhancing Simulink to save the entire simulation state?  In \r\nthis post, I present guest blogger \r\n<a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/28604\">Zhi Han<\/a>.<\/p>\r\n\r\n<h2>What is the state of my last simulation?<\/h2>\r\n<p>By Zhi Han<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/simstate_files\/zhi_han.jpg\" alt=\"Simulink developer Zhi Han\"><\/p>\r\n\r\n<p>The capability for saving and restoring the state of simulation has \r\nbeen one of the long-standing requests from users of Simulink. The \r\nmotivation comes two-fold. Sometimes simulation takes a very long time, \r\nand often it is desirable to break a long simulation into several stages. \r\nIn addition, if you simulate a model many times, most of the simulations \r\nshare a common phase such as the start-up of the system. In these cases, \r\nit is desirable to save the state of the system at the end of the \r\nsimulation and restart a simulation later from that saved state. <\/p>\r\n\r\n<p>Prior to R2009a, Simulink provides the ability to save the final state \r\nand load the initial state of a model. However, Simulink could only loads \r\nthe logged states. Logged states are continuous state and discrete work \r\nvectors used as state, which is only a subset of the variables used in \r\nsimulation. In real-life models, the set of logged state is often not \r\nenough to capture the complete state of simulation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/simstate_files\/image001.png\"><\/p>\r\n\r\n<p>As an experiment, let us open a Simulink demo model\r\n    <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/examples\/simulating-systems-with-variable-transport-delay-phenomena.html\"><tt>sldemo_VariableTransportDelay<\/tt><\/a>. The model uses Variable Transport Delay blocks\r\nto capture the delay between the vertical displacement of the front wheel and\r\nrear wheel of a running car.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/simstate_files\/car.png\"><\/p>\r\n\r\n<p>Models with Transport Delay blocks are usually difficult to restore to \r\ntheir state because, when you save the \"Final states\", Simulink does not \r\nsave the state of the transport delay in the structure format or the array \r\nformat. As a demonstration, we simulate the model from 0 to 5 seconds and \r\nsave the \"Final states\" in structure format in the workspace. Then we load \r\nthis saved state and simulate from 5 to 10 seconds.<\/p>\r\n\r\n<p><img \r\nsrc=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/simstate_files\/image002.jpg\"><\/p>\r\n\r\n<p>The blue\r\nlines are the results of a nonstop simulation, which serves as our baseline. The\r\nred dashed lines are the results of our experiment. It can be seen that beginning\r\nfrom 5 seconds on the restored simulation does not match the nonstop\r\nsimulation.<\/p>\r\n\r\n<p>In R2009a\r\nSimulink introduces the feature to \r\n<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/simulink\/ug\/bry62t3.html\">save \r\nand restore the complete set of states<\/a>\r\nused in simulation; this is the <strong>SimState<\/strong> of the model. By \r\nsaving the SimState at the end of a simulation, Simulink is able to \r\nrestore all the simulation variables as they were and reproduce the exact \r\nsame simulation results as a nonstop simulation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/simstate_files\/image003.png\"><\/p>\r\n\r\n<p>Now, we check the box and repeat our experiment. By restoring the \r\ncomplete simulation state, the saved and restored simulation results \r\nmatch the nonstop simulation.<\/p>\r\n\r\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2009Q1\/simstate_files\/image004.png\"><\/p>\r\n\r\n<p> Here are some important features of the SimState: <\/p>\r\n\r\n<p><strong>Timing information <\/strong><\/p>\r\n\r\n<p>The\r\nSimState retains the timing information of the model of the last simulation,\r\nincluding the start time and the <em>current<\/em> simulation time: the last time that\r\nthe simulation runs. Upon restoration, Simulink restores both the start \r\ntime and the simulation time. This is very important to handle correctly \r\ntime-varying systems. This detail of the SimState has the following \r\neffects that may surprise the user:<\/p>\r\n\r\n<ul>\r\n    <li>The start time specified in the restored simulation, if different \r\n    from the start time of the last simulation, overrides with the start \r\n    time stored in the SimState.<\/li>\r\n\r\n    <li>When loading a SimState, the user must make sure the stop time \r\n    is greater than the last simulation time; otherwise, Simulink \r\n    immediately stops the simulation after loading the SimState to the model.<\/li>\r\n<\/ul>\r\n\r\n<p><strong>Structural changes <\/strong><\/p>\r\n\r\n<p>Simulink checks the structure and the configuration parameters of the \r\nmodel with the information stored in the SimState to verify that the model \r\nhas not changed before loading the SimState. Structural changes, such as \r\nadding or deleting a block, renaming a block, or changing the connection \r\nbetween blocks would make the model incompatible with the SimState.<\/p>\r\n\r\n<p><strong>Accessing the states<\/strong><\/p>\r\n\r\n<p>The data stored in the SimState is accessible to the user. You can \r\nchange the values saved in SimState to initialize the model to a \r\ndifferent operating point than the last simulation. For the logged \r\nstates, the values are available in structure format as \r\n<tt>xFinal.loggedStates<\/tt>. For a Stateflow chart, the state is \r\navailable using the following:<\/p>\r\n\r\n<p><tt>&gt;&gt; state = xFinal.getBlockSimState('path_to_the_Stateflow_Chart')<\/tt><\/p>\r\n\r\nTo set the state of a Stateflow chart, use the following function.\r\n\r\n<p><tt>&gt;&gt; state = xFinal.setBlockSimState('path_to_the_Stateflow_Chart',x)<\/tt><\/p>\r\n\r\n<p>Check out the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/toolbox\/simulink\/ug\/bry62t3.html\">SimState \r\ndocumentation<\/a> for the full details on how this new capability works. \r\n\r\n<p><strong>Now it's your turn<\/strong><\/p>\r\n\r\n<p>Do you initialize simulations with the state from a previous run?  Did you \r\nrequest this capability?  Leave a \r\n<a href=\"https:\/\/blogs.mathworks.com\/seth\/?p=50&#comment\">comment here<\/a> \r\nand tell us what you think.<\/p>","protected":false},"excerpt":{"rendered":"<p>I am so excited about\r\n \r\n    saving and restoring the simulation state in Simulink.  This is \r\n    my favorite feature of R2009a because it has been long anticipated \r\n    and it enables important... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2009\/03\/20\/my-favorite-new-feature-in-r2009a-simstate\/\">read more >><\/a><\/p>","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[65,10,16],"tags":[83,441],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/50"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=50"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/50\/revisions"}],"predecessor-version":[{"id":6416,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/50\/revisions\/6416"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}