Skip to content

Navigation Menu

Sign in
Sign up

Would it be possible to add the passing of functions to the External Function Interface? #4230

tburch-LSS started this conversation in Ideas
Discussion options

i have been contributing to the Modelica-GNU_ScientificLibrary package:

https://github.com/ORNL-Modelica/Modelica-GNU_ScientificLibrary

and have found that, for implementing many of the GSL capabilities, it would be quite helpful if one could pass Modelica functions to external C libraries. Within C itself, this is possible. i have quite a bit of experience with coding in C and also some Modelica experience, so i might be able to contribute here myself... anyone have any tips on where to start? e.g., where the source code for the interface is to be found? thanks!

You must be logged in to vote

Replies: 3 comments 6 replies

Comment options

perhaps this is seen as a problem more on the "tool vendor" side of things... however, without granting external code (C) access to Modelica functions, one is forced to add user-defined functions to separate .c files, which need to be pre-compiled and added to the external library before the model can compile / run within the OMEdit or Dymola environment.
building the C files, compiling them, and appending them to the library can perhaps be handled via Modelica.Utilities routines, but i am left wondering how one achieves this before the larger model attempts its own build / compile.

You must be logged in to vote
0 replies
Comment options

I believe that is a language issue, not a library issue; and it gets tricky because the Modelica language has "partial function applications"; which seems good if one wants to e.g., integrate a Modelica function - but it also makes the C-interface more complicated.

You must be logged in to vote
6 replies
Comment options

Ok, that was what I expected. The integrand case is exactly where "partial function application" is useful as you normally integrate f(p, x) for some parameter(s) p, i.e., it is not just a function but a "closure". That "closure" is in Modelica hidden as a partial function application; so we would need to expose that in the function interface.

Comment options

yes, i looked into the "partial function application" and see how that could be useful (GSL has a similar capability within the library). the point for me is that the GSL routines (quadrature and otherwise) would need access to the functions defined by the user in OMEdit or Dymola. so without the GSL library having access to Modelica functions, i really need a build-and-compile-before-the-main-compile option. not exactly sure how to do that. i'll keep looking and learning...
thanks again!

Comment options

Comment options

i am currently trying to incorporate GSL's adaptive integration routines:

https://www.gnu.org/software/gsl/doc/html/integration.html#qag-adaptive-integration

these require an external workspace (handled via C routines that form the GSL/Modelica interface), but also access to the function to be integrated, which i envisioned as being defined by the OMEdit/Dymola user within some larger model / context.

Comment options

i'm wondering if it's still possible for external library functions to call Modelica functions. i imagine that after flattening / translation, there are C versions of any Modelica user-defined functions. shouldn't it be possible for external-library routines to make calls to these functions?

Comment options

well, i managed to get this working... in some fashion. see the integration examples (and related functions) in v1.0.2:

https://github.com/tburch-LSS/Modelica-GNU_ScientificLibrary

one needs to "simulate" the 'CreateIntegrand' model first. this basically creates a C-version of the desired function to be integrated, then compiles, and archives it (together with the interface routines). then one can run any larger model which may then repeatedly use GSL integration routines with the function (see, e.g., 'ExampleIntegral'). the one wrinkle is that i had to resort to 'gcc' and 'ar' system commands to compile and archive. until i can find a workaround for this, windows users will have to implement mingw / msys to have access to these.

updating this to include other GSL routines, more than one integrand, and parameter passing should be simple enough.

however, i would still advocate expanding Modelica's capabilities either to include the passing of functions or to offer the choice to have simplistic versions of some of the translated functions to simplify interfacing.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /