C/C++ library to handle FMU - modelica

Do you know if there is a C library to handle FMU and run simulations including a good solver?
As far as I know there are:
FMUSDK from QTronic
FMI Library from Modelon
Both can open FMUs, but only let running FMU for co-simulation with a simple Euler solver.
Libraries including a good solver handling discontinuities, but not in C, are:
PyFMI from Modelon: For Python
JFMY from Ptolemy: For Java (not sure if this includes a good solver)

I don't think the FMUSDK is really maintained, so the FMI Library is probably the better choice between those two.
To improve the solver, you'd probably have to pair the FMI Library with a solver like Sundials and figure out how to stitch those two together. Note, this is exactly what Modelon has done with the PyFMI library. While it is a Python library, I suspect you could probably find a relatively easy way to integrate into a non-Python project as long as you were able to use C code to integrate them (which apparently you are).
I suspect calling PyFMI from C is going to be easier than stitching FMIL together with Sundials on your own.

Related

How to perform local sensitivity analysis in modelica

I want to do local senstivity analysis in Dymola to evaluate different parameters affecting the energy consumption in a building (for multi-familyhouse).
Can anyone give me some suggestions, how to do it in Dymola (Modelica) software?
You could export you model as FMU and then follow one of many the different paths for sensitivity analysis with FMUs:
PyFMI + SAlib Sensitivity Analysis using PyFMI - FMU in for-loop
FMI Toolbox for Excel by Modelon http://www.modelon.com/products/modelon-deployment-suite/fmi-add-in-for-excel/
openTURNS + FMI https://www.researchgate.net/publication/321624302_A_Probabilistic_take_on_system_modeling_with_Modelica_and_Python, https://github.com/openturns/otfmi, http://www.openturns.org/
...
Did you take a look at the "Design" library that comes with Dymola? It contains functions to sweep and perturb parameters.
You can load it in File -> Libraries -> Design. Some examples on how to apply the library can be found in the first chapers of the Dymola Manual 2. From the question I guess the Calibration package should be most useful for you.
One approach could be to do the modelling in Dymola (or OpenModelica, Simulation X, whatever), and then do the parameter sweep, pre- and post-processing, plotting, optimization and so on from Python. Dymola has a Python interface, so you can start simulation, read results, change parameters and so on from Python. Everything that can be done from the GUI is in theory also possible from the Python interface.
A Matlab interface and a Javascript interface are also available.

Using a MATLAB code on Scilab

Is it possible to use a MATLAB code on Scilab? Is that what is meant when saying that Scilab is a "clone" from MATLAB?
There is a tool to automatically convert Matlab source to Scilab source, it's called M2SCI. A script parses the Matlab source code and replaces Matlab-specific functions by Scilab ones. See the documentation of the mfile2sci function.
Yes you can use MATLAB code on scilab. See these links for more information:
http://help.scilab.org/docs/5.4.0/fr_FR/section_36184e52ee88ad558380be4e92d3de21.html
http://help.scilab.org/docs/5.4.0/en_US/index.html
I would not bet on it. But if your code is simple enough chances are good.
Problems are:
There is encrypted p-code in Matlab that Scilab will not be able to open.
Matlab usually comes with a number of toolboxes that might not be available to you (i think especially Simulink)
last but not least (i don't know about scilab) there usually are minute differences in how functions are implemented.
There are a number of projects out there trying to replicate/replace MATLAB:
Julia language: which has a relatively similar syntax to MATLAB and offers great performance, but still lacks a lot of toolboxes/libraries, as well as not having a GUI like MATLAB. I think this has the brightest future among all MATLAB alternatives.
Python language and its libraries NumPy and matplotlib: which is the most used alternative. I think at this moment the community is a couple of orders of magnitude even bigger than MATLAB. Python is the de facto standard in machine learning and data science at the moment. But still, the syntax and memory concept is a bit far from what people are used to in the MATLAB ecosystem. There are also no equivalent to SIMULINK, although Spyder and Jupyter projects have come a long way in terms of the development environment.
Octave: is basically a clone of MATLAB to a point they consider any incompatibility as a bug. If you have a long MATLAB code that you don't want to touch, this is the safest bet. But again no alternative for SIMULINK.
SciLab and it's fork ScicoLab are the best alternatives in terms of GUI, having a SIMULINK replica xcos / scicos and a graphical user interface development features. However the community is not as big as Octave and the syntax is not completely compatible. Sadly the Scilab development team has gone through a devastating family crisis leading to the software falling behind.
Honorary mention of Modelica language implementations OpenModelica and jModelica for being a superior alternative to SIMULINK-SimScape. You should know that you can load Modelica scrips also in xcos and scicos. If you want to kno wmore about JModelica you may see this post.
you may check the MATLAB's Alternativeto page to see more Free and Open source alternatives.

iOS5 Objective-C library for numerical analysis or GNU Octave wrapper class?

I'm doing some numerical estimation and correction with the Kalman filter, and would like to better estimate my parameters of Q and R, preferably dynamically.
http://en.wikipedia.org/wiki/Kalman_filter#Estimation_of_the_noise_covariances_Qk_and_Rk
That article mentions that GNU Octave is currently the best way of determining these parameters from data:
http://en.wikipedia.org/wiki/GNU_Octave#C.2B.2B_integration
Unfortunately it is written for Matlab, and there's supposedly a C++ implementation. I'm very weak in C++ and would not even know how to import a C++ library and link it properly in XCode. All of my C++ libraries to date have been wrapped in 3rd party Objective-C classes.
Has anyone used the C++ implementation for scientific computing or engineering applications on iPhone? I'd appreciate any pointers or tutorials on how to do this kind of analysis with Objective-C.
Additional keywords:
estimating covariance from data
Autocovariance Least-Squares (ALS) technique
noise covariance
Thank you!
I do not know of any such C++ library, if you fancy doing numerical analysis on iOS, the best way to go is the accelerate framework, specifically (from this description):
Linear Algebra: LAPACK and BLAS
The Basic Linear Algebra Subprograms (BLAS) and Linear Algebra Package
(LAPACK) libraries contain—as you would expect—functions to perform
linear algebra computations such as solving simultaneous linear
equations, least squares solutions of linear equations, and eigenvalue
problems. The BLAS library serves as a building block for the LAPACK
library. The BLAS and LAPACK libraries are widely distributed and
industry standard computational libraries. They are available on a
number of different platforms and architectures. So, if you are
already using these libraries you should feel right at home, as the
APIs are exactly the same on Mac OS X.
You'll need a fairly good grounding in C, pointers, arrays and such though, no way around it I feel. There is a detailed description of how to use these linear algebra primitives to implement kalman filtering (although this is using R, so probably not of mush use to you).
This is a SO post on Kalman Filtering which expressed my opinion quite well. I'm afraid I think the chances of finding a magic Objective-C wrapper for Kalman Filtering are fairly low, though I would be very happy to be proven wrong!

MATLAB programming best practices

I'm looking for resources on how to structure medium- to large-scale MATLAB projects, especially ones that involve several independent modules. How do I manage global configuration variables, how do I structure the project into folders, how do I manage couplings between modules, etc.
Is there some kind of standard text on this subject? It looks as if most MATLAB textbooks have been written by scientists or engineers. What I'm looking for, I guess, is any MATLAB textbook written by a software engineer. :-)
MATLAB is an unusual choice for a large-scale projects and is as much suited for such task as assembler, COBOL or SQL. If you still choose MATLAB then at least automatically test the code! All kind of tests - integration tests, unit tests, load tests! And of course use a version control system.
As said, MATLAB was not created with large projects in mind therefore the only best practice regarding project structure, modules, coupling is the common sense.
If you are taking over an existing large MATLAB project then I am sorry with you, refactoring will be nightmare. If you are going to start a new large project with MATLAB then you are crazy - there are much better alternatives to MATLAB that are not that bad regarding numeric performance. Large project implies that almost all code is business logic, not numerics, therefore why for God's sake MATLAB?
Large project implies well structured components, which implies OO, which is the weak point of MATLAB because it sacrifices heap performance for numeric performance to the degree of unusability.
My experience:
I spent years in in a half-million LOC MATLAB project.
I have seen painless transition of multiple large MATLAB projects to C#.
With MATLAB you still have to use large amounts of Java for decent looking GUI, C/C++ MEX for fast not numeric parts like imports, maybe SQL, etc. With Java (or better C#) with a free numeric library you have only one language which is perfectly suited for everything you need in a large project.
I am not saying that MATLAB is bad - it rules for rapid prototyping and numerics! And Simulink has no alternatives (but can be compiled and used from everywhere).
You may want to have a look at "The Elements of Matlab Style" (review by Loren Shure).
Also, this review of good coding practices might be useful.

MATLAB vs Python for programming Probability Based Program

I am writing programs that are based on robots navigating through mazes (would involve stochastic programming).
Since it will involve heavy matrix handling (plus point for MATLAB) and simulating a robot (plus point for Prolog), I am in a dilemma between the choice of MATLAB and Prolog.
Note: I do have MATLAB at my work environment, hence cost is not an issue.
As mentioned previously, I am not sure if you are looking for comparisons between MATLAB and Python or MATLAB and Prolog. I can speak to the former, at least: MATLAB provides fast linear algebraic computation and a great IDE... and that's about it. Python will cost you much fewer headaches (and dollars), and you can manage "heavy matrix handling" nearly as easily if you tack on Numpy in particular, or SciPy in general.
Also, VPython (Visual Python) is a great 3D visualization tool that uses Numpy under the hood. I developed a robot simulator using VPython; you can see screenshots and example code (for simple wall-following maze navigation) that you can check out in a recent blog post.