Partition Data for Model Reference Hierarchy Using Data Dictionaries - MATLAB & Simulink (original) (raw)
When you use model referencing to break a large system of models into smaller components and subcomponents, you can create data dictionaries to segregate the_design data_. Design data is the set of workspace variables that the models use to specify block parameters and signal characteristics. For basic information about data dictionaries, see What Is a Data Dictionary?.
To take this component-based approach to data management, create a shared dictionary that contains common data and a separate dictionary for each component that contains the data needed by that component.
Open the Example Model and Load Design Data
Open the example model ex_SystemModel
. This model is at the top of a reference hierarchy that includes the other example models.
Load the MAT files contained in the working directory to create design data in the base workspace.
load ProjectData_Contr.mat load ProjectData_ContrSub1.mat load ProjectData_ContrSub2.mat load ProjectData_ContrSubs.mat load ProjectData_Plant.mat load ProjectData_System.mat
Create a Dictionary for Each Component
This example shows how to partition design data into dictionaries. When you finish, each component in the system has a dictionary, and dictionary references allow the components to share data.
Explore Example Model Hierarchy
- In the model, update the diagram. Each bus signal in the model uses a
Simulink.Bus
object as a data type. The objects,SensorBus
andCtrlBus
, are in the base workspace.
The referenced modelsex_PlantComp_Lvl1
andex_ContrComp
use the bus objects for root-level inputs and outputs, which means the plant and controller components share the objects. - In the base workspace, double-click the
Simulink.NumericType
object namedFloatType
. Signals, parameters, and other data items in the controller component use this shared data type. - In the Model Explorer Model Hierarchy pane, expand the node
ex_SystemModel
.
Click theConfigurations
node. In theContents pane, the nodeReference to SimConfigSet
appears.SimConfigSet
is aSimulink.ConfigSet
object in the base workspace. To maintain configuration parameter uniformity for simulation, all of the models in the hierarchy refer toSimConfigSet
. - Right-click the node
Controller (ex_ContrComp)
and select . - In the Model Explorer Model Hierarchy pane, expand the new node
ex_ContrComp
. Click theConfigurations
node.
In the Contents pane, the nodeReference to CodeGenConfigSet
appears.CodeGenConfigSet
is aSimulink.ConfigSet
object in the base workspace. To maintain configuration parameter uniformity for code generation, the models in the controller component refer toCodeGenConfigSet
. The models in the plant component do not useCodeGenConfigSet
. - In the Model Hierarchy pane, selectBase Workspace. In theContents pane, right-click the variable
diff
and select . In the Select a system dialog box, selectex_SystemModel
and clickOK. If you see a message about updating the diagram, click OK.
In the Contents pane, the variablediff
is used by Constant blocks in the modelsex_ContrComp_Sub1_Lvl1
andex_ContrComp_Sub1_Lvl2
, which make up the first controller subcomponent. Similarly, other models in the hierarchy share the base workspace variablescoeff
,init
,mu
, andrho
.
The table shows the models that share each variable in the base workspace.
Variable Name | Models Using the Variable | Scope of Sharing |
---|---|---|
CtrlBus | Top-level models in the plant and controller components | Shared globally by entire system |
SensorBus | Top-level models in the plant and controller components | Shared globally by entire system |
SimConfigSet | All models in the hierarchy | Shared globally by entire system |
rho | ex_PlantComp_Lvl2 ,ex_ContrComp_Sub1_Lvl2, andex_ContrComp_Sub2_Lvl2 | Shared globally by entire system |
mu | ex_PlantComp_Lvl1 andex_PlantComp_Lvl2 | Shared by models in the plant component |
FloatType | All models in the controller component | Shared by controller component and subcomponents |
CodeGenConfigSet | All models in the controller component | Shared by controller component and subcomponents |
init | ex_ContrComp_Sub1_Lvl2 andex_ContrComp_Sub2_Lvl1 | Shared by controller subcomponents |
diff | ex_ContrComp_Sub1_Lvl1 andex_ContrComp_Sub1_Lvl2 | Shared by models in the first controller subcomponent |
coeff | ex_ContrComp_Sub2_Lvl1 andex_ContrComp_Sub2_Lvl2 | Shared by models in the second controller subcomponent |
Suppose that separate teams of developers maintain the plant component and the controller components. You can use data dictionaries to store and scope the shared design data.
Create Shared Global Dictionary
Create a shared global data dictionary that contains the data shared globally by the entire system.
- In the Model Explorer, select > > .
- Set the new dictionary name to
GlobalShare
and click Save. - In the Model Hierarchy pane, right-click the
GlobalShare
node and select . - In the Model Hierarchy pane, selectBase Workspace. In theContents pane, select the design data that are shared globally by the entire system:
CtrlBus
,SensorBus
, andrho
. - Right-click and select .
- In the Model Hierarchy pane, right-click the
Design Data
node underGlobalShare
and select . - Similarly, copy
SimConfigSet
from theBase Workspace and copy to theConfigurations
node underGlobalShare
.
Create Dictionary for Plant Component
Create a data dictionary for data shared by models in the Plant component. Add a reference from this dictionary to the shared global dictionary.
- In the Model Explorer, select > > .
- Set the new dictionary name to
Plant
and clickSave. - In the Model Hierarchy pane, select the node
Plant
. In the Dialog pane, underReferenced Dictionaries, clickAdd. - Double-click
GlobalShare.sldd
. - In the Model Hierarchy pane, right-click the node
Plant
and select .
Link Plant Component to Dictionary and Migrate Data
Link the Plant component to its component dictionary then migrate data shared by models in the Plant component from the base workspace to the dictionary.
- Open the model
ex_PlantComp_Lvl1
. - In the model, update the diagram.
- If the Diagnostic Viewer displays an error for multiple inconsistent definitions of
SimConfigSet
, selectUpdate others to match next to theGlobalShare
instance. This fix updates other definitions to be consistent with theGlobalShare
definition. - In the Modeling tab, underDesign, click Link to Data Dictionary.
- In the dialog box, click Browse.
- Double-click
Plant.sldd
. - In the Model Properties dialog box, clickApply. Click Change all models in response to the message about linking referenced models.
- In the Model Properties dialog box, clickMigrate data.
- In the Migrate Data dialog box, select Include data from referenced models and then clickMigrate.
- (Optional) In the Model Properties dialog box, clear Enable model access to base workspace.
- Remove the previous method for loading model data. In theModel Properties dialog box, on theCallbacks tab, clear the
PreLoadFcn
for the model. - Click OK.
Create Dictionary for Controller Component
Create a data dictionary that contains the data shared by models in the controller component. This dictionary can also reference the shared global dictionary.
- In the Model Explorer, select > > .
- Set the new dictionary name to
Controller
and click Save. - In the Model Hierarchy pane, select the node
Controller
. In the Dialog pane, underReferenced Dictionaries, clickAdd. - Double-click
GlobalShare.sldd
. - In the Model Hierarchy pane, right-click the node
Controller
and select .
Link Controller Component to Dictionary and Migrate Data
Link the Controller component to its component dictionary then migrate data shared by models in the Controller component from the base workspace to the dictionary.
- Open the model
ex_ContrComp
. - If the Diagnostic Viewer displays an error for multiple inconsistent definitions of
SimConfigSet
, selectUpdate others to match next to theGlobalShare
instance. - In the Modeling tab, underDesign, click Link to Data Dictionary.
- In the dialog box, click Browse.
- Double-click
Controller.sldd
. - In the Model Properties dialog box, clickApply. Click Change all models in response to the message about linking referenced models.
- In the Model Properties dialog box, clickMigrate data.
- In the Migrate Data dialog box, select Include data from referenced models and then clickMigrate.
- (Optional) In the Model Properties dialog box, clear Enable model access to base workspace.
- Remove the previous method for loading model data. In theModel Properties dialog box, on theCallbacks tab, clear the
PreLoadFcn
for the model. - Click OK.
Link System to Global Dictionary
Finally, link the top model to the global dictionary.
- Open the model
ex_SystemModel
. - In the Modeling tab, underDesign, click Link to Data Dictionary.
- In the dialog box, click Browse.
- Double-click
GlobalShare.sldd
. - In the Model Properties dialog box, clickOK. Click Change this model only in response to the message about linking referenced models.
Inspect Data Storage
In the Model Explorer Model Hierarchy pane, select the dictionary node Plant. In the Contents pane, to view the contents of Plant.sldd
, click Show Current System and Below . The contents of the Design Data and Configurations sections appear.
Similarly, view the contents of Controller.sldd
.
The DataSource column shows the variables and objects that each dictionary stores.
All of the globally shared variables, such as CtrlBus
andSensorBus
, reside in GlobalShare.sldd
. The variable init
, which both of the controller subcomponents share, resides in Controller.sldd
.
If the development team assigned to the controller component must make changes to the globally shared variables, they access the GlobalShare
dictionary file. Similarly, if the team must make changes to the variableinit
, they must access the Controller
dictionary file.
Inspect Dictionary Hierarchy
To view the entire dictionary and model hierarchy, perform a dependency analysis.
- Open your saved model
ex_SystemModel
. - On the Modeling tab, in theDesign section, click Dependency Analyzer.
The system model, ex_SystemModel
, is linked to the dictionary GlobalShare.sldd
. The plant component and the controller component are each linked to a separate dictionary. To access the shared data, the component dictionaries reference the dictionaryGlobalShare.sldd
. These dictionaries form a reference hierarchy.
Strategies to Discover Shared Data
To learn how the models in a model reference hierarchy share data, use these techniques:
- In an open model, on the Modeling tab, select > . The Model Explorer displays the variables that the model uses, as well as the variables that referenced models use. You can then right-click a variable and select to display all of the models that use the variable. For more information, see Edit and Manage Workspace Variables by Using Model Explorer.
- At the command prompt, use the function Simulink.findVars to determine the variables a model uses. You can then use the functionintersect to determine the variables two models, components, or subcomponents share.