Main Content

加速模型模拟nd Analyses

What Is Acceleration?

Normally, when simulating or analyzing a model in SimBiology®, the model is expressed in MATLAB®code. You can accelerate the simulation by converting the model to compiled C code, which executes faster. Because this compilation step has a small time overhead, acceleration is not recommended for individual simulations of small models. However, for large models, or for repeated simulations during analysis, acceleration can provide a significant speed increase that outweighs the small time overhead.

When to Accelerate

The functionality to accelerate simulations performs optimally under the following conditions:

  • Running repeated simulations with different initial conditions

  • Running very long simulations (for example, simulations that take longer than a minute to run)

Prerequisites for Accelerating Simulations and Analyses

To prepare your models for accelerated simulations, install and set up a compiler:

  1. Install a C compiler (if one is not already installed on your system). For a current list of supported compilers, seeSupported and Compatible Compilers.

  2. Ensure that any user-defined functions in your model can be used for code generation from MATLAB, so they can convert to compiled C. For more information, seeLanguage, Function, and Object support for C and C++ code generation(MATLAB Coder)or contactMathWorks Technical Support.

Note

  • On Windows®, if you have not installed another compiler, SimBiology uses the lcc-win64 compiler for model accelerations. If you have installed another supported compiler, it will be selected automatically. For better performance of the acceleration functionality, you may want to install asupported compilerother than lcc-win64, and it will be selected automatically.

Accelerate Simulations Programmatically

Usesbioaccelerateif you are accelerating a SimBiology model. For aSimFunction objectand an exported model (SimBiology.export.Model), use the correspondingacceleratemethod.

Using sbioaccelerate

Follow the two-step process for acceleration.

  1. Runsbioaccelerate为加速准备您的模型模拟。Use the same input arguments that you plan to use withsbiosimulatein the next step. For example:

    sbioaccelerate(model,configset,doses);

    For a very large model, this step may take a minute or longer to complete.

  2. Runsbiosimulatewith the same input arguments that you used withsbioaccelerate. For example:

    simdata= sbiosimulate(model,configset,doses);

If you pass in an array of doses tosbioaccelerate, you can simulate the model using any subset of these doses and do not need to run acceleration again.

For illustrated examples, see the following.

Using accelerate

ASimFunctionobject is automatically accelerated at the first function execution. Hence it is not necessary to accelerate the model before you create the object. However, manually accelerate using theacceleratemethod of the object if you want it accelerated in your deployment applications.

For exported model, seeaccelerate.

When to Rerun Acceleration

If you make any modifications to the model, such as changes to reactions or adding events, you need to rerun the acceleration, before running simulations.

However, there are exceptions. You donotneed to accelerate again if you are making the changes to:

Accelerate Simulations using SimBiology Model Analyzer

You can enable the model acceleration in theSimBiology Model Analyzerapp by checking thePrepare the model for accelerated simulationbox in theModelstep of the program.

Troubleshooting Accelerated Simulations

If you have custom functions, use persistent variables only for those (constant) variables that you do not want to recalculate or reload every function call. The reason is that during the acceleration process, SimBiology converts the model and custom functions to compiled C code. If you try to use a persistent variable to share data across generated (or compiled) C functions, you may have different results. For instance, if you use a persistent variable to count how many times a function is called, each compiled function will have a separate count. Those persistent variables in the corresponding compiled functions will be different from the one used in the MATLAB function that you defined.

If you specify custom functions in SimBiology expressions, you might see the following warning if your code is not compatible with code generation from MATLAB:

The SimBiologyExpressionand any user-defined functions could not be accelerated. Please check that these expressions and any user-defined functions are supported for code generation as described in the Code Generation from MATLAB documentation.

whereExpressionis any of the following:

  • Reaction rate/rule expression

  • Initial assignment rule expression

  • Repeated assignment rule expression

  • Event trigger expression

  • Event function expression

For more information, seeLanguage, Function, and Object support for C and C++ code generation(MATLAB Coder)or contactMathWorks Technical Support.

See Also

||||

Related Topics