# Consistency between Modelica language external functions, and source code FMUs #545

Open
opened this Issue Mar 8, 2019 · 1 comment

Projects
None yet
3 participants
Collaborator

### hubertus65 commented Mar 8, 2019

 For all Modelica tools, it should be possible to generate a source code FMU from Modelica. Therefore, source code FMUs should allow at least the same languages as the Modelica external function interface. This will also have consequences for Modelica Libraries, where sources for external dependencies are not always included. This is handled separately in the Modelica specification repo.
Collaborator Author

### hubertus65 commented Mar 8, 2019

 For reference, here is the text from the Modelica external function interface. Unfortunately, in latex source, but probably still readable. \section{External Function Interface}\doublelabel{external-function-interface} Here, the word function is used to refer to an arbitrary external routine, whether or not the routine has a return value or returns its result via output parameters (or both). The Modelica external function call interface provides the following: \begin{itemize} \item Support for external functions written in C (specifically C89) and FORTRAN 77. Other languages, e.g. C++ and Fortran 90, may be supported in the future, and provided the function is link-compatible with C89 or FORTRAN 77 it can be written in any language. \item Mapping of argument types from Modelica to the target language and back. \item Natural type conversion rules in the sense that there is a mapping from Modelica to standard libraries of the target language. \item Handling arbitrary parameter order for the external function. \item Passing arrays to and from external functions where the dimension sizes are passed as explicit integer parameters. \item Handling of external function parameters which are used both for input and output, by passing an output that has a declaration assignment to the external function. {[}\emph{Declaration assignments are executed prior to calling the external function.}{]} \end{itemize} The format of an external function declaration is as follows. \begin{lstlisting}[language=grammar] function IDENT string-comment { component-clause ";" } [ protected { component-clause ";" } ] external [ language-specification ] [ external-function-call ] [annotation ] ";" [ annotation ";" ] end IDENT; \end{lstlisting} Components in the public part of an external function declaration shall be declared either as input or output. {[}\emph{This is just as for any other function. The components in the protected part allow local variables for temporary storage to be declared.}{]} The language-specification must currently be one of "builtin", "C", "C\ldots{}" (for one of the specific C-standards like C89, C99, and C11 -- which specifies that it relies on the C standard library of that version) or "FORTRAN 77". Unless the external language is specified, it is assumed to be "C". \emph{{[}The intended use of e.g. "C99" is to detect if the user tries to link with a C99-function using a C89 compiler.{]} } The "builtin" specification is only used for functions that are defined to be built-in in Modelica. The external-function call mechanism for "builtin" functions is implementation-defined. {[}\emph{Typically, for functions from the standard C-library, the prototype of the function is provided but no library annotation. Currently, there are no other builtin functions defined in Modelica.}{]} {[}\emph{Example}: \begin{lstlisting}[language=modelica] package Modelica package Math function sin input Real x; output Real y; external "builtin" y=sin(x); end sin; end Math; end Modelica; model UserModel parameter Real p=Modelica.Math.sin(2); end UserModel; \end{lstlisting} {]} The external-function-call specification allows functions whose prototypes do not match the default assumptions as defined below to be called. It also gives the name used to call the external function. If the external call is not given explicitly, this name is assumed to be the same as the Modelica name. The only permissible kinds of expressions in the argument list are component references, scalar constants, and the function size applied to an array and a constant dimension number. The annotations are used to pass additional information to the compiler when necessary. A component reference to a component that is part of an input or output is treated the same way as a top-level input or output in the external call.