Specify Initial Conditions for Bus Elements
Bus initialization is a special form of signal initialization. For general information about initializing signals, seeInitialize Signals and Discrete States. For details about initializing arrays of buses, seeInitialize Arrays of Buses.
Bus initialization specifies the bus element values that Simulink®uses for the first execution of a block that uses that bus. By default, the initial value for a bus element is the ground value (represented by0
). Bus initialization involves specifying nonzero initial conditions.
You can use bus initialization features to:
Specify initial conditions for signals that have different data types.
Apply a different initial condition for each element in the bus.
Specify initial conditions for a subset of elements in a bus without specifying initial conditions for all the elements.
Use the same initial conditions for multiple blocks, signals, or models.
Blocks That Support Bus Initialization
You can initialize bus values that input to a block if that block meets both of these conditions:
Has an initial value or initial condition block parameter
Supports buses
These blocks support bus initialization:
Data Store Memory
IC
Memory
Merge
Outport(when the block is inside a conditionally executed context)
Receive
Rate Transition
Unit Delay
For example, theUnit Delayblock is a bus-capable block. Its Block Parameters dialog box has anInitial conditionsparameter.
You cannot initialize a bus that has:
Variable-size signals
Frame-based signals
Set Diagnostics to Support Bus Initialization
To enable bus initialization, before you start a simulation, set theUnderspecified initialization detectionconfiguration parameter tosimplified
.
Create Initial Condition Structures
You can create partial or full initial condition structures to represent initial values for a bus. To create an initial condition structure, use one of these approaches:
Define a MATLAB®structure in the MATLAB base or Simulink model workspace. You can manually define the structure, or alternatively for full structures, you can use the
Simulink.Bus.createMATLABstruct
function.In the Block Parameters dialog box for a block that supports bus initialization, for the initial condition parameter specify an expression that evaluates to a structure.
For information about defining MATLAB structures, seeStructure Arrays.
The field that you specify in an initial condition structure must match these data attributes of the bus element exactly:
Name
Dimension
Complexity
For example, if you define a bus element to be a real [2x2] double array, then in the initial condition structure, define the value to initialize that bus element to be a real [2x2] double array.
显式地指定字段nitial condition structure for every bus element that has an enumerated (enum
) data type.
Control Data Types of Structure Fields
If any of the elements of the target bus use a data type other thandouble
, you can use different techniques to control the data types of the fields of initial condition structures. The technique that you choose can influence the efficiency and readability of the generated code. SeeControl Data Types of Initial Condition Structure Fields.
Create Full Structures for Initialization
A full initial condition structure provides an initial value for every element of a bus. The initial condition structure mirrors the bus hierarchy and reflects the attributes of the bus elements.
Specifying full structures during code generation offers these advantages:
Generates more readable code
Supports a modeling style that explicitly initializes all signals
Use theSimulink.Bus.createMATLABStruct
function to streamline the creation of a full MATLAB initial condition structure with the same hierarchy, names, and data attributes as a bus. This function fills all of the elements that you do not specify with ground values for those elements.
You can use several different kinds of input with the function, including:
A
Simulink.Bus
object nameAn array of port handles
You can invoke the function from theBus Editor. Select theBus
object for which you want to create a full MATLAB structure. Then, clickMATLAB Structure. For an example, seeInitialize Buses and Arrays of Buses Using Bus Editor.
To detect when structure parameters are not consistent in shape (hierarchy and names) with the associated bus, use the Model Advisor.
On theModelingtab, clickModel Advisor.
ClickOK.
UnderBy Task>Modeling Signals and Parameters using Buses, selectCheck structure parameter usage with bus signals.
Click theRun This Checkbutton.
The Model Advisor identifies partial initial condition structures.
After you create the structure, you can edit it in the MATLAB Editor.
Create Partial Structures for Initialization
部分提供了ini初始条件结构tial values for a subset of the elements of a bus. If you use a partial initial condition structure, during simulation, Simulink creates a full initial condition structure to represent all the bus elements. Simulink assigns the respective ground value to each element for which the partial initial condition structure does not explicitly assign a value.
Specifying partial structures for block parameter values can be useful during the iterative process of creating a model. Partial structures enable you to focus on a subset of signals in a bus. When you use partial structures, Simulink initializes unspecified signals implicitly.
When you define a partial initial condition structure:
Include only fields that are in the bus.
Omit one or more fields that are in the bus.
Make the field in the initial condition structure correspond to the nesting level of the bus element.
Within the same nesting level in both the structure and the bus, optionally specify the structure fields in a different order than the bus elements.
Note
The value of an initial condition structure must lie within the design minimum and maximum range of the corresponding bus element. Simulink performs this range checking when you do an update diagram or simulate the model.
Suppose that you have a bus,Top
, composed of three elements:A
,B
, andC
, with these characteristics:
A
is a nested bus, with two signal elements.B
is a single signal.C
is a nested bus that includes busA
as a nested bus.
Theex_bus_initial_conditions
model includes theTop
bus.
Here is a summary of theTop
bus hierarchy and the data type, dimension, and complexity of the bus elements.
Top A (sub1) A1 (double) A2 (int8, 5x1, complex) B (double) C (sub2) C1 (int16) C2 (sub1) A1 (double) A2 (int8, 5x1, complex)
In these examples,K
is an initial condition structure specified for the initial value of theUnit Delayblock. The initial condition structure corresponds to theTop
bus in theex_bus_initial_conditions
model. Here are some valid initial condition specifications.
Valid Syntax | Description |
---|---|
K.A.A1 = 3 |
Initialize the bus element |
K = struct('C',struct('C1',int16(4))) |
The bus element |
K = struct('B',3,'A',struct('A1',4)) |
Bus elements |
Invalid Partial Initial Condition Structures
In the following examples,K
is an initial condition structure specified for the initial value of theUnit Delayblock. The initial condition structure corresponds to theTop
bus in theex_bus_initial_conditions
model.
这三个初始条件规范notvalid:
Invalid Syntax | Reason the Syntax Is Invalid |
---|---|
K.A.A2= 3 |
Value dimension and complexity do not match. The bus element |
K.C.C2 = 3 |
You cannot use a scalar value to initialize initial condition substructures. |
K = struct('B',3,'X',4) |
You cannot specify fields that are not in the bus ( |
Initialize Buses Using Block Parameters
Initialize a bus by setting the initial condition parameter for a block that receives a bus as input and that supports bus initialization (seeBlocks That Support Bus Initialization).
For example, the Block Parameters dialog box for theUnit Delayblock has anInitial conditionparameter.
For a block that supports bus initialization, you can replace the default value of0
using one of these approaches:
All three approaches require that you define an initial condition structure (seeCreate Initial Condition Structures). You cannot specify a nonzero scalar value or any other type of value other than0
, an initial condition structure, orSimulink.Parameter
object to initialize a bus.
Defining an initial condition structure as a MATLAB variable, rather than specifying the initial condition structure directly in the Block Parameters dialog box offers several advantages, including:
Reuse of the initial condition structure for multiple blocks
Using the initial condition structure as a tunable parameter in the generated code
MATLABStructure for Initialization
You can initialize a bus using a MATLAB structure that explicitly defines the initial conditions for the bus.
For example, in theInitial conditionparameter of theUnit Delayblock, you could type in a structure.
MATLABVariable for Initialization
You can initialize a bus using a MATLAB variable that you define as an initial condition structure with the appropriate values.
For example, you could define the following partial structure in the base workspace:
K = struct('A', struct('A1', 3),'B', 4);
You can then specify theK
structure as theInitial conditionparameter of the Unit Delay block:
Simulink.Parameter
Object For Initialization
You can initialize a bus using aSimulink.Parameter
object that uses an initial condition structure for theValue
property.
For example, you could define the partial structureP
in the base workspace (reflecting theex_bus_initial_conditions
model discussed in the previous section):
P = Simulink.Parameter; P.DataType = 'Bus: Top'; P.Value = Simulink.Bus.createMATLABStruct('Top'); P.Value.A.A1 = 3; P.Value.B = 5;
You can then specify theP
structure as theInitial conditionparameter of theUnit Delayblock:
See Also
Blocks
Functions
Simulink.BlockDiagram.addBusToVector
|Simulink.Bus.cellToObject
|Simulink.Bus.createMATLABStruct
|Simulink.Bus.createObject
|Simulink.Bus.objectToCell
|Simulink.Bus.save