The first step you have to perform is to create the folder into which the following files should be put. For this introduction go to the user/+xxyy (xxyy is your user short hand, xx are the first two letters of your last name, yy are the first two letters of your first name. If you have not created that folder already please do so now) folder within your V-HAB directory and add the folder +introduction and within this folder add the new folder +systems.

1.4.1.1 System File

Most project in V-HAB will start with the requirement of simulating a specific system. This system with its physical components and properties is reflected in the simulation as code in the system file. To create a new empty system file go to the home tab in Matlab and use new a class to generate an empty class file. Then input the following code and save the file into the +introduction/+systems folder to create an empty setup file.

classdef Example < vsys
    properties (SetAccess = protected, GetAccess = public)
       
    end
   
    methods
        function this = Example(oParent, sName)
            this@vsys(oParent, sName, 30);
            eval(this.oRoot.oCfgParams.configCode(this));
           
        end
       
        function createMatterStructure(this)
            createMatterStructure@vsys(this);
           
        end
       
        function createSolverStructure(this)
            createSolverStructure@vsys(this);
           
        end
    end
   
     methods (Access = protected)
        function exec(this, ~)
            exec@vsys(this);
           
        end
     end
end

All systems in V-HAB follow this basic framework. In the first line the class for this system is defined by specifying the class name (Example in this case) and inheriting from the vsys class that contains general code for the V-HAB systems. The name of the system has to be equal to the filename. The next part in the program are the properties which this class should have. These properties are the only variables that will be accessible by other objects but only if the GetAccess is set to public. If the GetAccess is set to private only the specific object to which the property belongs will be able to access it which is useful for variables that should be available from one time step to another. The SetAccess defines who is allowed to write new values into the property with the access restriction being the same as for the GetAccess. For the methods of the class two different access levels are defined. The Example(), createMatterStructure() and createSolverStructure() functions are public methods that can be called by other objects while the exec() function is protected and can only be called by the specific object to which it belongs.

The Example() function is used to define the class for the system Example and is required to have the same name as the file and the class. The third input provided in line 8 (this@vsys(oParent, sName, 30);) defines the time step which is used for the exec() function. In this case 30 seconds are used which means that the exec() function will be called every 30 seconds. Other possible inputs are -1, 0 and any other number larger than 0. For -1 the exec() is called in every tick of the simulation, for 0 it uses the minimum time step as defined in the timer. Any other number will be treated as a time step in seconds for the exec() function.

The next function is the part where the physical system is translated into code and the function is therefore called createMatterStructure() to reflect the fact that the physical structure of the system is represented as code here. This function is only executed once at the beginning of each simulation to create the system! The following chapters will introduce the programming of this section in more detail. For the moment to obtain a system that can be executed simply add the following code to the function.

function createMatterStructure(this)
	createMatterStructure@vsys(this);

	matter.store(this, 'Cabin', 1);
	matter.phases.gas(this.toStores.Cabin, 'CabinAir', struct('N2', 1), 1, 293.15);
end

Detailed explanation about the adding of stores and phases will be provided in 1.4.2 Stores and Phases.

After including the physical representation of the system, the numerical representation has to be added in the createSolverStructure() function. As the name solver implies, the properties specified in this function are used to numerically calculate the system. Because the current system is still empty except for one phase, you do not have to add anything to this function yet. Like the createMatterStructure() function the createSolverStructure() function is also executed only once at the beginning of the simulation to initiate all the required parameters.

The last function of a V-HAB system is called the exec() function and it is the only function of a V-HAB system that is called while the simulation is running, hence the name exec(ute). The time step for the function is defined during the object generation for this class as mentioned above. While the simulation is running the exec function is called according to the specified time step which allows the programming of control mechanism for the system or change the system state. Detailed explanations about how to use the exec() function will be provided later on.

Important: If you want code to be executed during the simulation, it has to be in the exec() function not in the createMatterStructure()/createSolverStructure() functions!

1.4.1.2 Setup File

Every V-HAB project requires a setup file that is used to start the program using e.g. the function call vhab.exec('tutorials.simple_flow.setup') in the command window. It is also used to define the simulation length and the logging and plotting for the system. To generate the setup file go to the home tab in Matlab and use new a class to generate an empty class file. Then input the following code and save the file into the +introduction folder to create an empty setup file.

Empty Setup File
classdef setup < simulation.infrastructure
    properties

    end

    methods
        function this = setup(ptConfigParams, tSolverParams) 

        end

        function configureMonitors(this)

        end

        function plot(this)
        end
    end
end

As you can see the setup file is split into the three functions setup(), configureMonitors() and plot(). The setup() function is used to instantiate (initiate an instance of) the root system, which contains the code for the actual simulation of the system, and defines the time that should be simulated. In the configureMonitors() the values that should be logged and basic plotting commands are defined. The plot() function can be called after a simulation has finished to execute the already defined plotting commands. Additionally, you can add your own individual plotting commands to this function. For all of this to work correctly the functions have to be filled with the respective code. 

Empty Setup File
function this = setup(ptConfigParams, tSolverParams) 
	ttMonitorConfig = struct();
	this@simulation.infrastructure('Introduction_System', ptConfigParams, tSolverParams, ttMonitorConfig);


	xxyy.introduction.systems.Example(this.oSimulationContainer,'Example');


	%% Simulation length
	this.fSimTime = 3600; % In seconds
	this.bUseTime = true;
end

The call this@simulation.infrastructure is used to instantiate the parent class for the setup file. The variables ptConfigParams, tSolverParams and ttMonitorConfig can be used to define advanced settings for the system which will not be discussed for this introduction. The only setting you should be aware of is the option to save your simulation in specific intervalls while it is running which can be achieved by defining the ttMonitorConfig struct with the following values:

ttMonitorConfig = struct('oLogger', struct('cParams', {{ true, 10000 }}));

The value 10000 represents after how many ticks the simulation data will be dumped to the hard drive (into the data runs folder within V-HAB), what value makes sense depends on your simulation and how fast it calculates each tick. This option can be used for longer simulation where you want to prevent data loss because of a PC crash or where the amount of data becomes very large.

Then the root system has to be defined which is done by providing the path to the system file like this xxyy.systems.Example (don't forget to replace xxyy with your shorthand) and providing the required inputs for the system file. In this case the only inputs are the Container object and the name for the system (in this case Example). It is also possible to add inputs to both the setup and the system files, which can help to cover different use cases with one file. The final step performed in the setup file is to define the simulation length in seconds. In this case 3600 seconds or one hour will be simulated. Note that the simulated time is different from the real time that is required for the calculations. For simple systems like the one shown in the simulation, the required time for the simulation will be much shorter than the simulated time. For larger simulations, however, that can quickly change. Detailed instructions on how to program this function will be provided in 1.4.10 Advanced Plotting For now, just add the following code to the function to have some very basic logging and plotting available as output for the simulation.

Empty Setup File
function configureMonitors(this)
	%% Logging
	oLogger = this.toMonitors.oLogger;
	oLogger.addValue('Example.toStores.Cabin.toPhases.CabinAir', 'fPressure', 'Pa', 'Total Cabin Pressure');
end

The last function within the setup is plot() which is used after a simulation has finished or was stopped to create the figures with the results.

Empty Setup File
function plot(this)
	% Plotting the results
	% See http://www.mathworks.de/de/help/matlab/ref/plot.html for
	% further information
	close all % closes all currently open figures
	 
	% Tries to load stored data from the hard drive if that option
	% was activated (see ttMonitorConfig). Otherwise it only 
	% displays that no data was found 
	try
		this.toMonitors.oLogger.readDataFromMat;
	catch
    	disp('no data outputted yet')
	end


	%% Define plots
	% Defines the plotter object
	oPlotter = plot@simulation.infrastructure(this);


	% Define a single plot
	oPlot{1} = oPlotter.definePlot({'"Total Cabin Pressure"'},'Total Cabin Pressure');
	
	% Define a single figure
	oPlotter.defineFigure(oPlot,  'Total Cabin Pressure');
	
	oPlotter.plot();
end

Since the system currently is very small the plotting is also very minimalistic at the moment. But the chapter 1.4.10 Advanced Plotting will provide more insight into the creation of plots after the system is enhanced.

1.4.1.3

If you have followed the instructions so far you should be able to run the simulation by typing the command vhab.exec('xxyy.introduction.setup') into the command window and hitting the enter key. If you get any error message while trying to run the simulation go through the previous steps again and check if you have missed something. If not you can go ahead and plot the simulation using the oLastSimObj.plot command (just paste this into the command window and hit enter) which should give you the following output:

For now, just be happy if your outputs look the same as the figure provided here. If that is the case, you managed to create the necessary framework to plunge into the actual programming of a life support system in V-HAB in the next chapters. If no matter what you try, you are not able to reproduce the results you can go ahead and ask your advisor for some help.

You should also be aware of the option to pause a simulation by creating a STOP.m file in the base folder of V-HAB (STEPS). If you want to restart a simulation or continue a simulation that has finished you can use the command oLastSimObj.advanceTo(fTime) where you have to specify the time to which the simulation is supposed to continue running before stopping again.

1.4.1.4 Logging and Plotting

The different options you have for logging and plotting will be described as you build the system, but you have probably noticed the small save button in the bottom left corner of the figure. That button allows you to create good looking identical images of your plots easily. Just try clicking on it, it will ask you where to store the file and what data type you want to use in a normal explorer window. A good option to save plots is in the pdf vector graphic format so select that and some name for the plot for now. after that a new pop up window should open that looks like this:

In this window you can select some basic options to have your plot look the way you want it to look in your thesis. For example you can set the font size and font (you should use the same font as for the text in your thesis) as well as the image size. After you hit done the plot will change to the specified format and a new window will tell you that you can change things now before saving by using the normal matlab plot options. After you have implemented all changes you want you can hit ok on that window and the plot will be saved!


  • Keine Stichwörter