Skip to content

About the Process

Garima edited this page Jan 31, 2025 · 3 revisions

Documentation for OpenModelica GUI Project


1. Project Overview

This project focuses on creating a desktop application for running simulations compiled from OpenModelica models. The app enables users to:

  • Select a compiled model executable.
  • Input time parameters (startTime, stopTime).
  • Execute simulations and view real-time output within the app.

This documentation highlights the major modifications and improvements made during the project, including model changes, GUI enhancements, and code quality improvements.


2. Model Changes

This section outlines the modifications made to the .mo model files to achieve balanced equations, prevent simulation errors, and ensure compatibility with OpenModelica simulations. The changes primarily involve introducing flow-potential relationships, fixing redundant or missing equations, and adjusting annotations for simulation control.

2.1 TwoConnectedTanks.mo

Before Modification:

protected
  annotation(
    Diagram(coordinateSystem(extent = {{-100, 20}, {60, -120}})));

After Modification:

protected
  annotation(
    Diagram(coordinateSystem(extent = {{-100, 20}, {60, -120}})),
    __OpenModelica_simulationFlags(lv = "LOG_STDOUT,LOG_ASSERT,LOG_STATS", s = "dassl", variableFilter = ".*", maxIntegrationOrder = "3"),
    __OpenModelica_commandLineOptions = "--matchingAlgorithm=PFPlusExt --indexReductionMethod=dynamicStateSelection -d=initialization,NLSanalyticJacobian"
  );

Changes Summary:

  • Added simulation flags (__OpenModelica_simulationFlags) to control solver settings and logging options.
  • Added command line options (__OpenModelica_commandLineOptions) to resolve equation balancing and initialization issues.

2.2 Tank.mo

Before Modification:

if time <= 5 then
  Qo = 0;
else
  Qo = sqrt(h);
end if;

After Modification:

Qo = max(0, if time <= 5 then 0 else sqrt(h));

Changes Summary:

  • Simplified Qo assignment using the max() function to prevent negative values.
  • Flow-Potential Relationship: Introduced flowConnect.P = h to define the potential variable (h).

2.3 Tank2.mo

Before Modification:

Q1 = flowConnect.F;
T = V / Q1;

After Modification:

flowConnect.P = h;

Changes Summary:

  • Removed the Q1 assignment and the unnecessary variable T.
  • Defined the potential variable P in the connector as flowConnect.P.

2.4 FlowConnect.mo

Before Modification:

connector FlowConnect
  Real F;

After Modification:

connector FlowConnect
  Real P;      // Potential variable
  flow Real F; // Flow variable to fix imbalance error

Changes Summary:

  • Added the potential variable P to represent state variables like height or pressure.
  • Defined F as a flow variable to fix system imbalance errors.
  • These changes resolved over-determined equation errors and ensured that simulations ran successfully.

3. GUI Application Development

The Python GUI, built using PyQt6, underwent various updates for functionality, usability, and aesthetics. After the base development was done, the code updates happened in parts as I worked on simulating the model and making the interface interactive and user friendly. Here are a few notable modifications I did while coding the application.

3.1. Executable Selection

  • The Browse button allows users to select the OpenModelica-generated executable.
  • The executable path is validated to ensure the file exists.

3.2. Time Input Fields

  • The startTime and stopTime inputs were modified to display vertically instead of horizontally due to layout spacing issues.
  • Labels and input fields were aligned to reduce excessive spacing.

Features under Method

  • At first the fields were placed under one function init_ui as I added and removed feature attributes.
  • Now, all of them are under separate methods for ease of maintainibility and reading.

Layout Change:

  • The fields were placed horizontally with large gaps between them.
  • The fields are vertically aligned, with reduced space between labels and input fields.

3.3. Output Display

  • A QTextEdit widget was added below the Run button to display real-time simulation output.
  • The app streams both stdout and stderr during execution, displaying success or error messages dynamically.

3.4. Gradient Background

  • A gradient background was added to the main window for a modern look.
  • Example colors (#ffecd2, #fcb69f) were used, which can be customized.

It must be noted that I took the suggestion of applying OOP concepts and keeping the code pythonic and PEP8 abiding, so some sections were completely altered as I experimented with what worked and what didn't; both functionally and non-functionally.

4. Simulation Execution Process

4.1. Command Structure

The simulation is executed using a subprocess command with flags:

command = [
    app_path,
    "-override",
    f"startTime={start_time},stopTime={stop_time},stepSize=0.002",
    "-r=" + output_file,
    "-lv=LOG_STDOUT,LOG_STATS",
]
  • -override: Dynamically sets simulation parameters.
  • startTime, stopTime, stepSize: Control the simulation range and time steps.
  • -lv: Enables log output for both standard output and statistics.

4.2. Streaming Output

Real-time output is handled by a subprocess that streams logs to the QTextEdit widget:

process = subprocess.Popen(
    command,
    cwd=working_dir,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    text=True,
    env=os.environ.copy(),
)

The output is updated in the GUI using:

for line in iter(process.stdout.readline, ""):
    self.output_display.append(line)

5. Code Quality Improvements

  • PEP8 Compliance: The code adheres to PEP8 guidelines for readability and maintainability.
  • Linters and Formatters:
    • flake8: Configured to analyze code for errors and style issues.
    • black: Automatically formats the code according to PEP8 standards.
  • Ignored Warnings: Some irrelevant warnings (e.g., too many lines in a function) were bypassed using # noqa comments where appropriate.

Configuration Files:

  • .pylintrc: Custom rules for Pylint analysis.
  • .pre-commit-config.yaml: Defines pre-commit hooks to ensure code is linted and formatted before commits.

6. Simulation Flags

Refer to the official [OpenModelica Simulation Flags Documentation](https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/simulationflags.html) for more details on available flags. Some key flags used in the project are:

  • -override: Sets simulation parameters dynamically.
  • -lv=LOG_STDOUT,LOG_STATS: Streams simulation output and statistics.
  • stepSize: Determines the interval between simulation points.

7. Environment Variables

Ensure that OpenModelica is added to the system's PATH environment variable. This allows the application to locate and execute simulation-related files without errors.


8. Troubleshooting

1. Missing DLLs

  • Error: libSimulationRuntimeC.dll not found
  • Solution: Ensure that all dependent DLLs are in the same directory as the executable or properly referenced through the environment variables.

2. Simulation Errors

  • Check the output display for details on errors during execution.
  • Validate input parameters (startTime, stopTime) and ensure correct file permissions.

3. Permission Issues

  • Run the application with appropriate permissions if access to files or directories is restricted.

9. Future Enhancements

  • Allow dynamic adjustment of additional simulation parameters.
  • Add a settings menu for customizing simulation flags and paths.
  • Enable output export to various formats (e.g., CSV, JSON).

10. Conclusion

The OpenModelica Simulation Desktop App offers a robust and user-friendly solution for managing and executing model simulations. The changes and enhancements made throughout the project aim to improve both functionality and maintainability while adhering to industry best practices.