• No results found

CHAPTER 3 NON-HYBRID VCC SYSTEM SIMULATION

3.2 Components

3.2.1 S-Functions

S-Functions, or system-functions, are a powerful way of combining the advantages of the block based Simulink environment and the code based MATLAB scripts to model dynamic systems. In its most general form, the S-Function is a MATLAB script that contains a number of embedded functions which execute in a specific sequence to simulate the system as if it were modeled in the Simulink block based environment. S-Functions are able to model discrete or continuous components and systems, as well as both static and dynamic components and systems. The sequence of steps for flow of information in a Simulink based simulation is displayed below in Figure 3.4.

Figure 3.4: Sequence of Steps in Simulink

In order to complete each of the steps shown in Figure 3.4, the S-Function contains sub- functions which are executed by the solver in a particular order. A description of the purpose of each sub-function in the order they are called is listed below.

1. Setup – Provides the interface between the S-Function and the block in the Simulink environment. Sets up basic characteristics and allocates memory for objects such as parameters, inputs, outputs, continuous states, as well as initializes the number and order of sub-functions within the S-Function. Only run once per simulation.

2. Post Propagation Setup – Sets up the memory allocation for the discrete state vector, as well as the number and properties of each of the discrete states. Only run once per simulation.

3. Start – Initializes discrete and continuous states. Only run once at the beginning of the simulation.

4. Outputs – Calculates and then passes the output values to the output ports of the Simulink block containing the S-Function. Run first after initialization, and then at every time step of the simulation.

5. Update – Calculates discrete state values and assigns these to the discrete state vector. Called and run at each time step of the simulation.

6. Derivatives – Calculates the values of the derivatives of each continuous state, and stores them within the derivative vector. Called and run at each time step of the simulation.

After the derivative function has been called, Simulink will numerically integrate the derivative vector using the solver chosen by the user and check for convergence of the solutions to a prescribed tolerance set in the Simulink interface. If the solutions converge, the simulation will restart the cycle at the output stage for the next time step. If they do not convergence, the simulation with decrease the current time step size and recalculate derivatives and reintegrate until either convergence is achieved or a minimum threshold for step size is reached.

S-Functions for each individual component can be viewed and edited by users. They are contained inside the Component S-Functions folder within the Thermosys 4 file structure. If a user would like to create a new component S-Function, it is suggested to begin from a previously existing S-Function and modify the code accordingly so that the basic structure is pre- configured. Additional details on S-Function development can be found in the MATLAB help directory.

3.2.2 Component Blocks

In order to interface MATLAB S-Functions with the Simulink environment, Level 2 MATLAB S-Function blocks are used which provide the tools for this interfacing. The basic structure of such a block is found within the User-Defined Functions tab of the Simulink Library Browser. However, the Thermosys 4 library described earlier already contains pre-configured blocks for each component S-Function.

An close up of the exterior of the Level 2 MATLAB S-Function block for the EEV is shown in Figure 3.5 with labels over the input and output signal ports.

Figure 3.5: S-Function Block Exterior for the EEV

An example of the interior of a Level 2 MATLAB S-Function block is shown in Figure 3.6. This is accessed by right clicking on the block and selecting Look Under Mask.

Figure 3.6: S-Function Block Interior for the EEV

In this dialog box, the component S-Function is interfaced by typing its filename into the S-function name input box. If done correctly, clicking on the Edit box will then bring up the S- Function in another window. However, if the block is already part of a library that is locked by default (such as the library in Thermosys 4), the library will have to be unlocked first before accessing the S-Function code. Once completed, selecting OK will then update the exterior of the

block to display the number of input and output ports given by the number initialized in the setup sub-function of the S-Function code.

The Parameters input box is where the variable names of the mask parameters (discussed in the next section) are inputted in order to allow them to be passed into the S-Function for use. The number of parameters in this input box should match the value given in the setup sub- function of the component S-Function code.

3.2.3 Component Masks

Component masks represent the way in which user supplied parameters and initialization calculations are handled in the Simulink S-Function code structure. Figure 3.7 shows an example of the user input mask for the condenser model.

Figure 3.7: Condenser Mask Example

Users can then edit the parameters of the components (such as geometric parameters, operating conditions, etc.). These masks are accessed from the Simulink model environment by double clicking on an S-Function block once the masks are created.

To begin creating a mask in a new S-Function block, the user must right click on the block and select Create Mask. (Note: this becomes Edit Mask after the first time it is selected). After this a window will appear similar to that shown in Figure 3.8.

Figure 3.8: Mask Editor Dialog Box

The different tabs at the top of this dialog box correspond to different parts of the mask structure that need to be specified in order to create a mask. Each is briefly explained below.

1. Icon & Ports – Controls the visual graphics on the exterior of the Level 2 MATLAB S-Function block. Allows for pictures, port by port labels, colors, etc.

2. Parameters – Creates and sets properties for user inputs such as those shown in Figure 3.7. Allows various types of inputs such as value, checkbox, dropdown box, etc.

3. Initialization – Customizable initialization script utilizing user inputs that runs when any of the inputs are updated. This script can be used to calculate mask parameters from user inputs which are then passed into the S-Function through the component block.

4. Documentation – Allows for input of block description and customizable help files which can be easily accessed from the Simulink environment.

Once a mask is initialized for a component block, the following steps detail the sequence of actions to create a simple mask.

1. Define the necessary user inputs and their properties in the parameters tab.

2. Create initialization code to calculate necessary parameters from user inputs in the initialization tab.

3. (Optional) Create help instructions such as component, input-output port, or user input descriptions in the documentation tab.

4. (Optional) Add graphics or port labels to the block in the Icons & Ports tab.

Related documents