As with all code related work, you will likely encounter bugs and have to debug your model if your work with V-HAB. This chapter is intended to provide some tips and tricks with regard to this.

1.5.1 MATLAB Breakpoints

Breakpoints allow you to define specific points in your code at which the execution should stop to allow you to debug it. There is a good MATLAB guide for breakpoints  which explains how to use them in detail. There is also an option to enable breakpoints on error, which means MATLAB will automatically stop in case an error is encountered, which is very useful when debugging a V-HAB model. Another good use case for break points is if you e.g. want to define a control logic in an exec function or define a calculation in the update function of a component. You can simply set a breakpoint at that location and basically write and test your case with all variables working in the command window as they work within the files.

1.5.2 Navigating V-HAB Objects

If you run a V-HAB simulation you will receive an oLastSimObj as output of the simulation. This is an object with a predefined architecture. Here we discuss how you can navigate this object and other objects within V-HAB. If you double click the oLastSimObj a variables explorer will open in MATLAB:

It shows all porperties of the variable you opened (in this case oLastSimObj) and while many are not really of interest for you, the variable oSimulationContainer contains your V-HAB model (here the simple_flow tutorial). Within it you have to navigate through the variable toChildren to your actual system (in this case it is called Example, but for you it will be called differently):

In this you will find the variables toStores which contains all stores of the system or toChildren which contains all subsystems. The structure tries to be readable and is als common for all V-HAB systems, so once you have a basic understanding of it it will be easy to navigate through the model this way, which is usefull if you want to check values deep within it or just want to find paths to some values to use in your code. 

1.5.3 Debugging Small Time Steps

Another common problem when you create V-HAB models may be small time steps or you just generally want to improve the performance of your model. If that is the case you first need to find the parts of your model that are responsible for the small time steps. For this purpose a time step observer is available in V-HAB which tracks the time steps and can tell you what part of your model created the smallest time step in the last 100 ticks. You can add this observer in the setup file. For example if you want to add it to the simple_flow tutorial the code looks like this:

function this = setup(varargin)
    ttMonitorConfig = struct();
  	ttMonitorConfig.oTimeStepObserver.sClass = 'simulation.monitors.timestepObserver';
	% These are optional parameters that will change the
    % defaults for the reporting limit and the number of ticks in the
    % observation history. The following line just sets the defaults.
	% (Report all timesteps >= 0 and store the last 100 ticks)
 	ttMonitorConfig.oTimeStepObserver.cParams = { 0 , 100 };
	% IMPORTANT: You have to add ttMonitorConfig to your system definition below!

    % First we call the parent constructor and tell it the name of
    % this simulation we are creating.
    this@simulation.infrastructure('Tutorial_Simple_Flow', containers.Map(), struct(), ttMonitorConfig);

    % Creating the 'Example' system as a child of the root system
    % of this simulation. 
    tutorials.simple_flow.systems.Example(this.oSimulationContainer, 'Example');

    % Setting the simulation duration to one hour. Time is always
    % in units of seconds in V-HAB.
    this.fSimTime = 3600;
end

If you then run the simple_flow tutorial, you will not notice any change, as the observer silently executes in the background. However, you can either stop the simulation or let it finish and then use the command:

tools.findSmallestTimeStep(oLastSimObj)

Which produces the output:

In the system Example in Store 'Tank_1', Phase 'Tank_1_Phase_1', a minimal time step of 20 seconds was used in Simulation Tick 1021

In the system Example in Store 'Tank_2', Phase 'Tank_2_Phase_1', a minimal time step of 20 seconds was used in Simulation Tick 1021

In the system Example in Store Tank_1 in Capacity Tank_1_Phase_1 a minimal time step of 20 seconds was used in Simulation Tick 1021 for the function @(~)this.registerUpdateTemperature()

In the system Example in Store Tank_2 in Capacity Tank_2_Phase_1 a minimal time step of 20 seconds was used in Simulation Tick 1021 for the function @(~)this.registerUpdateTemperature()

The output tells you which phase in which system or which other component in which part of your model is responsible for the smallest time step. In this case, the smallest time steps are 20 seconds, which is the default maximum time step in V-HAB for phases. You may change this value using the setTimeStepProperties  function of the phase and/or capacity. You can access information on how to use these functions and their possible inputs by using doc matter.phase.setTimeStepProperties  or doc thermal.capacity.setTimeStepProperties Note that you must first use or run vhab.init()  a V-HAB simulation for these commands to work.


Alternativly to the observer you can also set a breackpoint in the tick function of core.event.timer and use the following commands to find the smallest time step:

aiResponsibleCallbacks = find(this.afTimeSteps == min(this.afTimeSteps(~this.abDependent)))

This will produce an output of integers which correspond to the timer callbacks responsible for the smallest time steps. You can access the object of these callbacks by using

CallBack = functions (this.cCallBacks{aiResponsibleCallbacks(1)})

In the callback a struct containing the responsible objects can be found in:

CallBack.workspace{1}

After you have identified the responsible objects you can place a break point in the corresponding calculateTimeStep  function to further debug why the small time step occurs.

  • Keine Stichwörter