-
Notifications
You must be signed in to change notification settings - Fork 193
Would it be possible to add the passing of functions to the External Function Interface? #4230
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!
All reactions
Replies: 3 comments 6 replies
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.
All reactions
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.
All reactions
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.
All reactions
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!
All reactions
I haven't looked deeply into GSL but I assume you mean:
https://www.gnu.org/software/gsl/doc/html/roots.html#c.gsl_function
and some other cases like https://www.gnu.org/software/gsl/doc/html/multimin.html#c.gsl_multimin_function_fdf
All reactions
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.
All reactions
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?
All reactions
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.