Target Language Compiler | ![]() ![]() |
Inlining Fortran (F-MEX) S-Functions
The capabilities of Fortran MEX S-functions can be fully inlined using a TLC block target file. With a simple F MEX S-function version of the ubiquitous "timestwo" function, this interface can be illustrated. Here is the sample Fortran S-function code:
C C FTIMESTWO.FOR C $ Revision: 1.1$ C C A sample FORTRAN representation of a C timestwo S-function. C Copyright 1990-2000 The MathWorks, Inc. C C===================================================== C Function: SIZES C C Abstract: C Set the size vector. C C SIZES returns a vector which determines model C characteristics. This vector contains the C sizes of the state vector and other C parameters. More precisely, C SIZE(1) number of continuous states C SIZE(2) number of discrete states C SIZE(3) number of outputs C SIZE(4) number of inputs C SIZE(5) number of discontinuous roots in C the system C SIZE(6) set to 1 if the system has direct C feedthrough of its inputs, C otherwise 0 C C===================================================== C SUBROUTINE SIZES(SIZE) C .. Array arguments .. INTEGER*4 SIZE(*) C .. Parameters .. INTEGER*4 NSIZES PARAMETER (NSIZES=6) SIZE(1) = 0 SIZE(2) = 0 SIZE(3) = 1 SIZE(4) = 1 SIZE(5) = 0 SIZE(6) = 1 RETURN END C C===================================================== C C Function: OUTPUT C C Abstract: C Perform output calculations for continuous C signals. C C===================================================== C .. Parameters .. SUBROUTINE OUTPUT(T, X, U, Y) REAL*8 T REAL*8 X(*), U(*), Y(*) Y(1) = U(1) * 2.0 RETURN END C C===================================================== C C Stubs for unused functions. C C===================================================== SUBROUTINE INITCOND(X0) REAL*8 X0(*) C --- Nothing to do. RETURN END SUBROUTINE DERIVS(T, X, U, DX) REAL*8 T, X(*), U(*), DX(*) C --- Nothing to do. RETURN END SUBROUTINE DSTATES(T, X, U, XNEW) REAL*8 T, X(*), U(*), XNEW(*) C --- Nothing to do. RETURN END SUBROUTINE DOUTPUT(T, X, U, Y) REAL*8 T, X(*), U(*), Y(*) C --- Nothing to do. RETURN END SUBROUTINE TSAMPL(T, X, U, TS, OFFSET) REAL*8 T,TS,OFFSET,X(*),U(*) C --- Nothing to do. RETURN END SUBROUTINE SINGUL(T, X, U, SING) REAL*8 T, X(*), U(*), SING(*) C --- Nothing to do. RETURN END
Copy the above code into file ftimestwo.for
in a convenient working directory.
Putting this into an S-function block in a simple model will illustrate the interface for inlining the S-function. Once your Fortran MEX environment is set up, prepare the code for use by compiling the S-function in a working directory along with the file simulink.for
from matlabroot
/simulink/src/
. This is done with the mex
command at the MATLAB command prompt:
And now reference this block from a simple Simulink model set with a fixed step solver and the grt
target.
The TLC code needed to inline this block is a modified form of the now familiar timestwo.tlc
. In your working directory, create a file named ftimestwo.tlc
and put this code into it.
%implements "ftimestwo" "C" %function Outputs(block, system) Output /* %<Type> Block: %<Name> */ %% /* Multiply input by two */ %assign rollVars = ["U", "Y"] %roll idx = RollRegions, lcv = RollThreshold, block, ... "Roller", rollVars %<LibBlockOutputSignal(0, "", lcv, idx)> = \ %<LibBlockInputSignal(0, "", lcv, idx)> * 2.0; %endroll %endfunction
Now you can generate code for the ftimestwo
Fortran MEX S-function. The resulting code fragment specific to ftimestwo
is
/* S-Function Block: <Root>/F-MEX S-Function */ /* Multiply input by two */ rtB.F_MEX_S_Function = rtB.Gain * 2.0;
![]() | Inlining M-File S-Functions | TLC Coding Conventions | ![]() |