-
-
Notifications
You must be signed in to change notification settings - Fork 0
About the Process
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.
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.
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.
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).
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.
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.
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.
- The Browse button allows users to select the OpenModelica-generated executable.
- The executable path is validated to ensure the file exists.
- The
startTime
andstopTime
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.
- A QTextEdit widget was added below the Run button to display real-time simulation output.
- The app streams both
stdout
andstderr
during execution, displaying success or error messages dynamically.
- 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.
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.
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)
- 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.
-
.pylintrc
: Custom rules for Pylint analysis. -
.pre-commit-config.yaml
: Defines pre-commit hooks to ensure code is linted and formatted before commits.
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.
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.
- 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.
- Check the output display for details on errors during execution.
- Validate input parameters (
startTime
,stopTime
) and ensure correct file permissions.
- Run the application with appropriate permissions if access to files or directories is restricted.
- 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).
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.