C Function

Description of the C function component in Schematic Editor, which enables importing functions from the C programing language.

The C Function allows you to implement a component with an arbitrary function using the C programming language.

Like any other Signal Processing component, the C Function consists of inputs, outputs and functions that define the functionality of the block. This section explains in details the options which are available for configuring the C Function in each of its tabs and sub-tabs.

General tab

In this tab, you define the general properties of the C Function which include its name, its execution rate, inputs, outputs, global variables and parameters. Figure 1 shows the user interface of the General tab.

Inputs and outputs

Input and output terminals are defined inside the Inputs and Outputs tabs, respectively.

To add a new terminal to the C Function component, simply press the blue plus-sign button for adding a new element to the table (Figure 2).

Terminal name

Every terminal on the component must have a unique name. When a new terminal is added, its name is set automatically. To rename the terminal, double-click the name you want to change and type in a new name.

When naming a terminal, it is important to follow these two rules:

  • The Namecolumn cannot be empty, and
  • The Namecannot contain whitespaces.

Figure 1: General tab

Figure 2: Button for adding a new element to the table

Terminal type

You can use the Type column to define the terminal’s signal type. The signal type can be set to the following four values (Figure 3):

  • inherit,
  • real,
  • int, and
  • unit.

Figure 3: Changing the terminal’s signal type

The signal value type inherit has different meanings for input and output terminals.

If the signal type of an input terminal is set to inherit, then the terminal will inherit the signal type from the terminal that is connected to it.

If the signal type of an output terminal is set to inherit, then the signal type of the terminal will be determined by the internal rule.

The values real, intandunit are used to explicitly set a signal type to the terminal. If the signal type of the input terminal is set explicitly, only the terminal with the same signal type can be connected to it.

For further information about signal data types, please take a look at the section Signal types.

Direct feedthrough

The Direct feedthrough column (Figure 4) contains a checkbox which determines whether the terminal is a direct feedthrough terminal or not.

If a terminal is defined as direct feedthrough, it means that its current value determines the current value of one of the component's outputs.

For further information about direct feedthrough terminals, please take a look at the sectionsComponent Sorting and Algebraic loops.

Figure 4: Direct feedthrough column

Terminal removal

To remove a terminal, simply press the blue minus-sign button for terminal removal (Figure 5) in the row of the terminal you want to remove and press the OK button.

Figure 5: Button for terminal removal

Global variables

Global variables are accessible from every component’s function (Figure 6 and Figure 7). Values of global variables are preserved among different function calls.

Variable name

Terminal naming rules defined in section Terminal name must be also applied for naming global variables.

Variable type

You can use the Type column to define a global variable’s signal type. The signal type can be set to (Figure 6): real, int and unit.

For further information about signal data types, please take a look at the section Signal types .

Figure 6: Changing the global variable’s signal type

Variable removal

Variables are removed in the same way as the terminals (please, see the section Terminal removal).

Figure 7: Example of accessing global variable global_var1 in ¬output_fnc function

Figure 8: Example of accessing global variable global_var1 in init_fnc function

Parameters

Parameters allow you to pass external variables into the C Function. These external variables are propagated through the namespace into the C Function.

To make an external variable visible within the C Function, you must declare it inside the parameters table (Figure 6), otherwise an error will be raised (Figure 9).

Parameters can be accessed inside the component’s functions in the same way as the global variables are accessed.

Figure 9: Error raised because parameter param1 is not defined in the namespace

Parameter name

Terminal naming rules defined in section Terminal name must be also applied for naming parameters.

Parameter type

You can use the Type column to define a parameter’s signal type. The signal type can be set to: real, int and unit.

For further information about signal data types, please take a look at the section Signal types.

Parameter removal

Variables are removed in the same way as the terminals (please, see the section Inputs and outputs).

Functions

The Functions tab allows you to define arbitrary init, output and update functions.

Once you implement the functions, you can check if the code is syntactically correct by pressing the Check syntax button (Figure 10).

Figure 10: Check syntax button

If the code is syntactically correct, you will get the No errors found message, otherwise the Code validator dialog will be shown (Figure 11). Every error message inside the Code validator dialog has a header and content. If you click on the header, you will be automatically redirected to the function that caused an error.

Figure 11: Dialog for showing errors in the C Function

C Function has a built-in support for mathematical functions and constants from the standard math.h library

Init function (init_fnc)

The init_fnc function is used to initialize component state variables and is called at the beginning of a simulation

Output function (output_fnc)

The output_fnc function is used to update the output signals of the component and is called at each simulation step.

Update function (update_fnc)

The update_fnc function is used to update the state variables of the component and is called at each simulation step after the output_fnc function.