Searching Code To Find Where a Variable is Defined

The Modelica language and Dymola are typically used to simulate complex systems. These system models are constructed from many smaller component and subsystem models, in a hierarchical structure with multiple levels. So searching code to find where variables or parameters are defined in a complex model can appear difficult, but in this blog post I’ll show how it can be easily done.

Model Structure

One of the key advantages of the object-orientated nature of the Modelica language is the ability to extend the behaviour and characteristics of existing models. The new model inherits the content of the model from which it extends, with additional elements added. The concept of inheritance has been covered in this previous blog post, Dymola Basics: 8. Dymola Model Inheritance.

Developers benefit from inheritance and extending models because:

  • Models can be reused to maximise development efficiency.
  • Model compatibility is ensured by defining interfaces and templates which are then extended to fully define the component.

When we model a complex system, we can replicate the hierarchical architecture of the real system by dividing it into its subsystems, and those into its component. Figure 1 shows the subsystem architecture of a VeSyMA car example, containing the subsystems for the engine, transmission, driveline, brakes, suspensions, subframes, body, wheels and controllers. Each subsystem is a complete model.

Figure 1: Vehicle subsystem architecture within VeSyMA large automatic family car example (VeSyMA.Automotive.RearWheelDrive.LargeFamilyAT)

Figure 1 – Vehicle subsystem architecture within VeSyMA large automatic family car example.

So a model of a complex system will probably have many levels in its construction. The image in figure 2 highlights the component browser for a vehicle acceleration experiment from the VeSyMA library. The component tree has been partially expanded to illustrate the number of sub-models within this vehicle experiment and the number of levels of inheritance these sub-models contain.

Figure 2: Expanded tree in the component browser for a vehicle experiments (VeSyMA.Experiments.Examples.AccelerationTest), showing many levels within the model.

Figure 2 – Partially expanded component tree in the component browser for a vehicle experiment, showing many levels within the model. The burgundy square icons represent where a model is extended.

While the structure of such a complex model is very organised, having so many subsystems and levels of inheritance can make it hard to understand exactly where a variable or parameter has been defined. Figure 3 below shows the text layer of the vehicle experiment in figure 2. At this top level we can only see the code extending the vehicle test template, redeclarations of the systems and a few parameter modifications. This is only a small fraction of the entire code which makes up the experiment.

Figure 3: The text layer of the vehicle experiment , VeSyMA.Experiments.Examples.AccelerationTest. This only shows a small portion of the entire code of the experiment model.

Figure 3 – The Text layer of the vehicle experiment shown in figure 2. This only shows a small portion of the entire code of the experiment model.

Searching the Code of a Model

In the vehicle acceleration experiment I’ve been using as an example, there is a Boolean parameter in the vehicle controlling whether the vehicle animation is generated for the simulation. In the vehicle’s parameter dialog, this parameter is linked to the world model’s parameter enableAnimation, as shown in figure 4. However the vehicle’s animation parameter value isn’t defined in the text of the vehicle experiment in figure 3. So where is this parameter setting defined in the model structure?

Figure 4: Vehicle animation parameter setting viewed in the parameter dialog GUI.

Figure 4 – Vehicle animation parameter setting viewed in the parameter dialog GUI.

Selecting an item in the component browser will display that class, so you can search the code at various levels within the experiment architecture, but that is potentially a slow and laborious method.

A quicker way is to look at the Used Classes layer of the model, which displays the code for the model and all the referenced classes used within it. This can be viewed using the Used Classes button in the Text tab, or the Used Classes Layer button in the bottom right corner, as highlighted in Figure 5.

Figure 5: The Used Classes layer of the vehicle experiments (VeSyMA.Experiments.Examples.AccelerationTest), where find button used for searching code

Figure 5 – The Used Classes layer of the vehicle experiment.

Comparing the code displayed in figure 3 and figure 5, you can see the additional code of the referenced classes in the Used Classes layer. This code will include the text layer of every subsystem and component at all levels of their inherited structure.

Now all the code is visible, including its source locations, we can use the Find button for searching code for the value referenced by the vehicles animation parameter, i.e. world.enableAnimation. In this case, the search takes us to the vehicle test template where the animation parameter is set. The video below demonstrates the search of the Used Classes layer of the vehicle experiment.

Video 1 – Searching code for a variable definition in a complex vehicle test model

If it’s the source of a signal on a signal bus that you want to determine instead of a variable or parameter setting, you may find this post useful, Searching of the Modelica Expandable Connectors’ Signals and their Connected Components in Dymola.

Written by: Hannah Hammond-Scott – Modelica Project Leader

Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion

CONTACT US

Got a question? Just fill in this form and send it to us and we'll get back to you shortly.

Sending

© Copyright 2010-2023 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?