In the presentation Transferring the System Modeler code base to OCaml, Leonardo Laguna Ruiz shares his experience of transitioning the System Modeler codebase from Metamodelica to OCaml. Ruiz, an electrical engineer with a Ph.D. and a background in functional programming, discusses the reasons for the transition and the challenges they faced during the process. System Modeler is a multi-domain simulation environment with a central kernel responsible for model compilation, interpretation, and communication with clients like ModelCenter and SimulationCenter. The kernel was previously written in MetaModelica, but the rest of the clients were in C++. Ruiz explains that he had been using F# to develop System Modeler’s model compiler, simulator, and optimization engine for five years and had also learned other functional languages like Haskell and Lisp. Overall, the benefits of using OCaml in System Modeler include performance improvements, bug fixes, improved testing, and accelerated development. Ruiz mentions that the conversion took approximately one and a half to two years with the help of two other developers and that the productivity gains have been significant, reducing bug-finding time from weeks to a few hours.
Transferring the System Modeler code base to OCaml: A comprehensive overview
A brief history of system modeler
System Modeler, developed by Wolfram’s MathCore subsidiary, is a powerful multi-domain simulation environment. Initially created with Modelica and then transitioning to MetaModelica, System Modeler has evolved since its inception in 1998. Ruiz was tasked with improving the kernel, responsible for model compilation, interpretation, and simulation. At the time, System Modeler’s backend was tightly coupled with OpenModelica, but this dependency became a pain point, causing instability and integration issues. To regain control and stability, the team decided to migrate the kernel to OCaml.
Why OCaml?
Several reasons made OCaml the standout candidate for the migration. First, OCaml’s static typing and strong type inference allowed for early error detection, leading to cleaner, more reliable code. Ruiz emphasized OCaml’s ability to handle symbolic data and pattern matching, crucial for the complex data structures in System Modeler. Additionally, OCaml’s functional programming paradigm simplified concurrent programming, making it easier to manage asynchronous workflows.
One of the most significant factors in choosing OCaml was its performance. When comparing execution times and code size across various languages — including Metamodelica, C++, F#, and Mathematica — OCaml emerged as a fast and concise language. It offered a good balance of execution speed and readable, maintainable code.
The Challenges of Metamodelica
Before the switch, the System Modeler kernel used Metamodelica, an extension of Modelica that adopted ML-style types, pattern matching, and first-class functions. Despite these features, Metamodelica posed several challenges. Ruiz pointed out its verbosity and lack of essential modern programming features like anonymous functions, closures, and proper error handling. Debugging was especially difficult, with only print statements available for tracing issues. These limitations, combined with the overhead of maintaining compatibility with OpenModelica, made it clear that a new approach was needed.
The migration process
Ruiz’s team didn’t just jump into OCaml. They first developed a tool to analyze OpenModelica’s code, extracting only the necessary parts for System Modeler. Eventually, they decided to create their own language for the backend to ensure full control and stability.
The migration process involved translating Metamodelica code to OCaml, a task that proved challenging but rewarding. Automatic translation was difficult due to differences in how the two languages handled certain constructs, but the team developed a converter to make the process smoother. This tool was essential in translating error handling paths, performing simplifications, and generating idiomatic OCaml code.
One of the key benefits of the switch to OCaml was the reduction in code size and improved performance. Ruiz shared examples where the converted OCaml code was not only faster but also more readable than its Metamodelica counterpart. Error handling became clearer and more manageable, thanks to OCaml’s robust error propagation mechanisms.
Performance gains
The results of the migration were impressive. In some models, the new OCaml-based System Modeler kernel ran up to ten times faster than the old Metamodelica version. The introduction of OCaml’s advanced features, such as functors (parametrized packages of functions), allowed the team to optimize the code further. Ruiz highlighted how OCaml’s static typing and integrated tools, like the time-traveling debugger, helped catch bugs early and improve the overall development process.
The road ahead
While the migration process took about two years, the productivity gains have been immense. Bug-finding times, which previously took weeks, were reduced to just hours. Ruiz and his team are still refining the System Modeler codebase, but the transition to OCaml has laid a strong foundation for future development.
As for the possibility of using other languages like Julia or Rust, Ruiz mentioned that while those were considered, OCaml’s balance of speed, ease of use, and functional programming features made it the best choice for their specific needs.
Conclusion
Leonardo Laguna Ruiz’s presentation on transferring the System Modeler codebase to OCaml offered a fascinating glimpse into the challenges and rewards of migrating a complex codebase to a new language. By choosing OCaml, the team achieved significant performance improvements and streamlined their development process. OCaml’s functional programming capabilities, coupled with its performance and robust tooling, made it an ideal choice for the System Modeler kernel, setting the stage for continued innovation and growth.