Skip to content

Commit 982590a

Browse files
authored
Add support for Windows (#23)
* Fix no artifacts where needed in CI [skip ci] * Added Windows CI * Use environment file for Modelica * Specify libraries in CI * Use latest PyDelica version and use context manager for junk cleanup * Try apt installing om libs * Use OM Docker image * Do not use Sudo * run apt update * Bump version number * Update citation file [skip ci] * Updated changelog [skip ci]
1 parent a35a669 commit 982590a

19 files changed

+182
-129
lines changed

.github/workflows/build_run.yml .github/workflows/build_run_linux.yml

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
name: Power Balance Models
1+
name: Power Balance Models Ubuntu
22
on: [push]
33
jobs:
44
test_modelica_build:
5+
container:
6+
image: openmodelica/openmodelica:v1.18.0-minimal
57
name: Test Modelica Build (${{ matrix.os }})
68
runs-on: ${{ matrix.os }}
79
strategy:
@@ -10,9 +12,8 @@ jobs:
1012
os: [ubuntu-latest]
1113
steps:
1214
- uses: actions/checkout@v2
13-
- uses: artemis-beta/setup-openmodelica@v2
14-
with:
15-
msl-version: '3.2.3'
15+
- name: Install OpenModelica libraries
16+
run: apt update && apt -y install omlib-modelica-3.2.3 omlib-modelicaservices-3.2.3 omlib-complex-3.2.3
1617
- name: Compile models
1718
run: |
1819
mkdir build
@@ -54,6 +55,8 @@ jobs:
5455
path: power_balance/profiles/mat_profile_files/*.mat
5556
retention-days: 1
5657
model_run_om:
58+
container:
59+
image: openmodelica/openmodelica:v1.18.0-minimal
5760
name: Model Run Open Modelica (${{ matrix.os }})
5861
runs-on: ${{ matrix.os }}
5962
strategy:
@@ -72,14 +75,15 @@ jobs:
7275
with:
7376
name: om-plasma-profiles
7477
path: power_balance/profiles/mat_profile_files
75-
- uses: artemis-beta/setup-openmodelica@v2
76-
with:
77-
msl-version: '3.2.3'
78+
- name: Install OpenModelica libraries
79+
run: apt update && apt -y install omlib-modelica-3.2.3 omlib-modelicaservices-3.2.3 omlib-complex-3.2.3
7880
- name: Run Models
7981
run: |
8082
cd build
8183
for i in $(ls -I "*.h" -I "*.c" -I "*.o" -I "*.json" -I "*.xml" -I "*.makefile" -I "*.mat"); do echo "Running model $i"; chmod +x ./$i; ./$i; done
8284
model_run_api:
85+
container:
86+
image: openmodelica/openmodelica:v1.18.0-minimal
8387
name: Model Run API (${{ matrix.os }} Python${{ matrix.python-version }})
8488
runs-on: ${{ matrix.os }}
8589
strategy:
@@ -96,9 +100,8 @@ jobs:
96100
python-version: ${{ matrix.python-version }}
97101
architecture: x64
98102
- uses: Gr1N/setup-poetry@v7
99-
- uses: artemis-beta/setup-openmodelica@v2
100-
with:
101-
msl-version: '3.2.3'
103+
- name: Install OpenModelica libraries
104+
run: apt update && apt -y install omlib-modelica-3.2.3 omlib-modelicaservices-3.2.3 omlib-complex-3.2.3
102105
- name: Run Models
103106
run: |
104107
poetry install
@@ -111,6 +114,8 @@ jobs:
111114
path: coverage_default
112115
retention-days: 1
113116
regression_tests:
117+
container:
118+
image: openmodelica/openmodelica:v1.18.0-minimal
114119
name: Regression Tests (${{ matrix.os }} Python${{ matrix.python-version }})
115120
runs-on: ${{ matrix.os }}
116121
strategy:
@@ -127,9 +132,8 @@ jobs:
127132
python-version: ${{ matrix.python-version }}
128133
architecture: x64
129134
- uses: Gr1N/setup-poetry@v7
130-
- uses: artemis-beta/setup-openmodelica@v2
131-
with:
132-
msl-version: '3.2.3'
135+
- name: Install OpenModelica libraries
136+
run: apt update && apt -y install omlib-modelica-3.2.3 omlib-modelicaservices-3.2.3 omlib-complex-3.2.3
133137
- name: Run Regression Tests
134138
run: |
135139
poetry install
@@ -142,6 +146,8 @@ jobs:
142146
path: coverage_regression
143147
retention-days: 1
144148
unit_tests:
149+
container:
150+
image: openmodelica/openmodelica:v1.18.0-minimal
145151
name: Unit Tests (${{ matrix.os }} Python${{ matrix.python-version }})
146152
runs-on: ${{ matrix.os }}
147153
strategy:
@@ -158,9 +164,8 @@ jobs:
158164
python-version: ${{ matrix.python-version }}
159165
architecture: x64
160166
- uses: Gr1N/setup-poetry@v7
161-
- uses: artemis-beta/setup-openmodelica@v2
162-
with:
163-
msl-version: '3.2.3'
167+
- name: Install OpenModelica libraries
168+
run: apt update && apt -y install omlib-modelica-3.2.3 omlib-modelicaservices-3.2.3 omlib-complex-3.2.3
164169
- name: Run Unit Tests
165170
run: |
166171
poetry install

.github/workflows/site_build_and_benchmarks_main.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Benchmarks and Docs
22
on: [push]
33
jobs:
44
run_benchmarks:
5+
container:
6+
image: openmodelica/openmodelica:v1.18.0-minimal
57
runs-on: ubuntu-latest
68
name: Run Benchmarks on main
79
if: ${{ github.ref_name == 'main' }}
@@ -10,16 +12,15 @@ jobs:
1012
uses: actions/checkout@v2
1113
with:
1214
fetch-depth: 0
15+
- name: Install OpenModelica libraries
16+
run: apt -y install omlib-modelica-3.2.3 omlib-modelicaservices-3.2.3 omlib-complex-3.2.3
1317
- name: Configure Git credentials
1418
uses: OleksiyRudenko/gha-git-credentials@v2
1519
with:
1620
1721
token: '${{ secrets.GITHUB_TOKEN }}'
1822
- name: Set up Python
1923
uses: actions/setup-python@v2
20-
- uses: artemis-beta/setup-openmodelica@v2
21-
with:
22-
msl-version: '3.2.3'
2324
- name: Setup ASV
2425
run: |
2526
python -m pip install asv virtualenv click

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## v1.1.0 - 2022-03-09
4+
5+
* Added support for Windows.
6+
* Use context manager to ensure removal of temporary files.
7+
* Compatible with PyDelica v0.4.2
8+
39
## v1.0.0 - 2022-02-09
410

511
* First full version of Power Balance ready for open sourcing.
@@ -62,4 +68,4 @@
6268

6369
## pre-api - 2021-01-13
6470

65-
* Creation of `Tokamak.Interdependencies` Modelica model with components for Magnets, Cryogenics and Detritation.
71+
* Creation of `Tokamak.Interdependencies` Modelica model with components for Magnets, Cryogenics and Detritation.

CITATION.cff

+3-4
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ authors:
6767
given-names: Timothy
6868

6969
cff-version: "1.2.0"
70-
date-released: "2022-03-01"
70+
date-released: "2022-03-09"
7171
keywords:
7272
- "power balance"
7373
- fusion
@@ -76,8 +76,8 @@ keywords:
7676
license: LGPL-2.1+
7777
message: "If you use this software, please cite it using these metadata."
7878
title: "Power Balance Models"
79-
commit: "469eaa5570cdc53a0ed91d3af4ce13e7061b2484"
80-
version: "1.0.1"
79+
commit: "56067cf8297e90689484fc7c6429880e788c534c"
80+
version: "1.1.0"
8181
repository-code: "https://github.com/ukaea/powerbalance"
8282
url: "https://ukaea.github.io/powerbalance/"
8383
references:
@@ -163,4 +163,3 @@ references:
163163
authors:
164164
- given-names: "John D."
165165
family-names: "Hunter"
166-

benchmarks/bench_models.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import pydelica
88

9+
from power_balance.environment import MODELICA_ENVIRONMENT
10+
911
MODELS_DIR = os.path.join(pathlib.Path(__file__).parents[1], "power_balance", "models")
1012

1113
MODELS = [
@@ -32,11 +34,14 @@ class ModelBuild:
3234

3335
def setup(self, model):
3436
self.session = pydelica.Session()
35-
self.session.use_library("Modelica", "3.2.3")
37+
self.session.use_libraries(MODELICA_ENVIRONMENT)
3638

3739
def time_model_build(self, model):
3840
_model_path = os.path.join(MODELS_DIR, f"{model.split('.')[0]}.mo")
3941
if "Tokamak" in model:
4042
self.session.build_model(_model_path, model, extra_models=MODELS)
4143
else:
4244
self.session.build_model(_model_path, model)
45+
46+
def teardown(self, model):
47+
self.session._compiler.clear_cache()

docs/API/advanced_api.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,17 @@ Out[6]:
3838
'tokamak.interdependencies.ratiotype',
3939
'tokamak.interdependencies.systempressure',
4040
...
41+
In [7]: p.clear_cache()
4142
```
4243

4344
!!! warning "Parameter setting"
4445
All parameters including those that are protected are listed via `PowerBalance.get_parameters()` for
4546
the purposes of inspection. Only modifiable parameters can be updated, these are listed by running `PowerBalance.modifiable_parameters()`.
4647

47-
48+
!!! warning "PyDelica cache"
49+
On Windows temporary directories are not deleted on log out. PyDelica uses these to perform builds and store
50+
the model binaries. It is therefore recommended unless using `PowerBalance` as a context manager you should
51+
run the `clear_cache` method to wipe these build folders upon completion.
4852

4953

5054

docs/API/getting_started.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ print(data_frame)
4949
data frames are very powerful objects, you can apply cuts to them and perform operations on subsets, see the [Pandas documentation](https://pandas.pydata.org/docs/) for details.
5050

5151
## Creating your own scripts
52-
The main class used to initialise and run a simulation via the OpenModelica backend within PBM is the `PowerBalance` class.
52+
The main class used to initialise and run a simulation via the OpenModelica backend within PBM is the `PowerBalance` class. It is recommended that the class be used via a context manager to ensure that model build directories are removed upon completion (this is done automatically when using the CLI).
5353

5454
```python
5555
from power_balance.core import PowerBalance
5656

5757
# Initialise the PBM class fetching parameters
5858
# and models from the default locations
59-
pbm_instance = PowerBalance()
59+
# Use context manager to ensure junk collection
60+
with PowerBalance() as pbm_instance:
6061

61-
# Run the simulation with the configuration
62-
pbm_instance.run_simulation()
62+
# Run the simulation with the configuration
63+
pbm_instance.run_simulation()
6364

64-
# Open the plots in the browser window
65-
pbm_instance.launch_browser()
65+
# Open the plots in the browser window
66+
pbm_instance.launch_browser()
6667
```

poetry.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

power_balance/cli/session.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ def pbm_main(
6666
if _args["from_session"]:
6767
_check_session_directories(_args)
6868

69-
pbm_instance = pbm_core.PowerBalance(
69+
with pbm_core.PowerBalance(
7070
config=_args["config"],
7171
no_browser=_args["no_browser"],
7272
parameter_directory=_args["param_dir"],
7373
profiles_directory=_args["profiles_dir"],
7474
modelica_file_dir=_args["model_dir"],
7575
print_intro=True,
76-
)
77-
pbm_instance.run_simulation(_args["outputdir"])
76+
) as pbm_instance:
77+
pbm_instance.run_simulation(_args["outputdir"])
7878

79-
if not no_browser:
80-
pbm_instance.launch_browser()
79+
if not no_browser:
80+
pbm_instance.launch_browser()
8181

8282

8383
def _check_session_directories(_args):

power_balance/core.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import itertools
2525
import logging
2626
import os
27-
import platform
2827
import re
2928
import shutil
3029
import subprocess
@@ -41,6 +40,7 @@
4140
import power_balance
4241
import power_balance.browser as pbm_browser
4342
import power_balance.configs as pbm_config
43+
import power_balance.environment as pbm_env
4444
import power_balance.exceptions as pbm_exc
4545
import power_balance.modelica_templating.pfmagnets as pbm_pfmagnet_templates
4646
import power_balance.models as pbm_models
@@ -157,15 +157,7 @@ def __init__(
157157
self.power_data: typing.Dict[str, pd.DataFrame] = {}
158158
self.pydelica_session = pydelica.Session(_pde_ll)
159159

160-
# PBM only compatible with MSL 3.2.3 at present but library switch
161-
# only works with UNIX based OSes
162-
if platform.system() != "Windows":
163-
self.pydelica_session.use_library("Modelica", "3.2.3")
164-
else:
165-
self._logger.warning(
166-
"Cannot set MSL version explicitly on Windows, "
167-
"assuming MSL is version 3.2.3"
168-
)
160+
self.pydelica_session.use_libraries(pbm_env.MODELICA_ENVIRONMENT)
169161

170162
self.configuration = pbm_config.read_options_from_config(config)
171163

@@ -227,6 +219,21 @@ def __init__(
227219
self._check_for_model_mods()
228220
self.read_models_from_directory()
229221

222+
def clear_cache(self) -> None:
223+
"""Clear the PyDelica session cache
224+
225+
As we do not use pydelica.Session as a context manager we need to do
226+
the cache clear manually (else risking leaving a lot of temporary
227+
directories on Windows)
228+
"""
229+
self.pydelica_session._compiler.clear_cache()
230+
231+
def __enter__(self):
232+
return self
233+
234+
def __exit__(self, *args, **kwargs):
235+
self.clear_cache()
236+
230237
def _deduce_profile_max_values(self) -> typing.Dict[str, float]:
231238
"""
232239
For the current parameter set deduce the maximum values for profiles

power_balance/environment/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os.path
2+
3+
import toml
4+
5+
MODELICA_ENVIRONMENT = toml.load(
6+
open(os.path.join(os.path.dirname(__file__), "modelica.toml"))
7+
).values()
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Modelica]
2+
version="3.2.3"
3+
name="Modelica"
4+
5+
[ModelicaServices]
6+
version="3.2.3"
7+
name="ModelicaServices"
8+
9+
[Complex]
10+
version="3.2.3"
11+
name="Complex"

power_balance/validation/config.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,15 @@ def check_sweep(cls, values: typing.Dict[str, typing.Any]):
152152
def check_model_list(cls, values: typing.Dict):
153153
modelica_file_dir = values["modelica_file_directory"]
154154

155-
_local_models = list(
156-
get_local_models(
157-
model_file_dir=modelica_file_dir,
158-
names_only=True,
159-
session=pydelica.Session(),
160-
quiet=True,
155+
with pydelica.Session() as pd_session:
156+
_local_models = list(
157+
get_local_models(
158+
model_file_dir=modelica_file_dir,
159+
names_only=True,
160+
session=pd_session,
161+
quiet=True,
162+
)
161163
)
162-
)
163164

164165
for model in values["models"]:
165166
if model not in _local_models:

0 commit comments

Comments
 (0)