ModiGen: A Large Language Model-Based Workflow for Multi-Task Modelica Code Generation

Jiahui Xiang, Tong Ye, Peiyu Liu, Yinan Zhang, Wenhai WangarXiv 2025

Abstract

Modelica is a widely adopted language for simulating complex physical systems, yet effective model creation and optimization require substantial domain expertise. Although large language models (LLMs) have demonstrated promising capabilities in code generation, their application to modeling remains largely unexplored. To address this gap, we have developed benchmark datasets specifically designed to evaluate the performance of LLMs in generating Modelica component models and test cases. Our evaluation reveals substantial limitations in current LLMs, as the generated code often fails to simulate successfully. To overcome these challenges, we propose a specialized workflow that integrates supervised fine-tuning, graph retrieval-augmented generation, and feedback optimization to improve the accuracy and reliability of Modelica code generation. The evaluation results demonstrate significant performance gains: the maximum improvement in pass@1 reached 0.3349 for the component generation task and 0.2457 for the test case generation task. This research underscores the potential of LLMs to advance intelligent modeling tools and offers valuable insights for future developments in system modeling and engineering applications.

Introduction

While Large Language Models (LLMs) have achieved remarkable proficiency in general-purpose programming languages like Python or Java, their application to domain-specific languages like Modelica remains a significant challenge. Modelica is a "non-causal," equation-based language used to simulate complex physical systems such as automotive engines, power grids, and thermal systems. Unlike traditional imperative programming, Modelica describes what a system is rather than how to calculate its state, requiring deep domain expertise to ensure physical consistency and simulation stability.

Existing LLMs often suffer from a "modeling gap," where they generate code that appears syntactically correct but fails to compile or violates fundamental physical laws. The ModiGen workflow was developed to bridge this gap by enhancing LLMs with domain-specific knowledge and iterative refinement capabilities.

Methodology

The ModiGen workflow consists of a three-stage pipeline designed to transform general-purpose LLMs into specialized modeling assistants: Preparation, Generation, and Verification.

The ModiGen workflow architecture, illustrating the Preparation, Generation, and Verification stages.
  1. Supervised Fine-Tuning (SFT): The researchers curated a massive dataset from 44 Modelica libraries. Using Parameter-Efficient Fine-Tuning (LoRA), they adapted open-source models like CodeLlama and StarCoder2 to the specific structural requirements and syntax of Modelica.
  2. Graph Retrieval-Augmented Generation (GraphRAG): To ensure physical consistency, ModiGen utilizes a "Property Graph" that maps relationships between components, signals, and parameters. The LLM queries this graph during generation to respect physical constraints and interface requirements.
  3. Feedback Optimization: This stage implements an iterative debugging loop. If a generated model fails to simulate, the system captures compiler error messages and feeds them back to the LLM for automated refinement.

Data Resources

A core contribution of this research is the creation of specialized datasets for Modelica instruction tuning and benchmarking. These resources provide a foundation for training and evaluating models on diverse engineering domains, including thermodynamics, robotics, and electrical circuits.

Distribution of Modelica instances across different categories in Dataset_all and Dataset_sft.
  • Dataset_all: A comprehensive collection of 9,456 Modelica instances extracted from 44 open-source and commercial libraries.
  • Dataset_sft: A refined instruction-tuning dataset containing 3,202 high-quality pairs for supervised fine-tuning.
  • Benchmark Suite: Two evaluation datasets consisting of 129 components and 127 test cases designed to measure functional accuracy.

Results and Discussion

The implementation of the ModiGen workflow yielded dramatic improvements in code generation reliability. The synergy between GraphRAG and Feedback Optimization proved particularly effective, with the combined workflow outperforming the individual components.

Key performance highlights include:

  • Significant Pass@1 Gains: The maximum improvement in the pass@1 metric reached 0.3349 for component generation and 0.2457 for test case generation.
  • Surpassing Proprietary Models: After applying the ModiGen workflow, fine-tuned open-source models like CodeLlama-34B achieved a pass@1 of 0.5558, surpassing the initial performance of top-tier proprietary models like GPT-4o and Claude-3.
  • Functional Consistency: GraphRAG was found to be essential for ensuring that generated code not only runs but also produces physically accurate simulation results.

Conclusion

ModiGen demonstrates that while general LLMs struggle with the complexities of physical system modeling, a specialized workflow combining fine-tuning, structured knowledge retrieval, and iterative feedback can achieve state-of-the-art results. Future work will focus on addressing "multi-physics" interactions—where mechanical, electrical, and thermal domains overlap—and expanding the workflow to support holistic, system-level design for complex engineering projects like aircraft or power plants.

Summary updated

BibTeX
@misc{xiang2025modigen,
  author = {Jiahui Xiang and Tong Ye and Peiyu Liu and Yinan Zhang and Wenhai Wang},
  title = {{ModiGen: A Large Language Model-Based Workflow for Multi-Task Modelica Code Generation}},
  year = {2025},
  eprint = {2503.18460},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.18460},
}
Figure