Skip to content

Commit

Permalink
wip compile & run with spawn & optimica
Browse files Browse the repository at this point in the history
  • Loading branch information
vtnate committed Jun 7, 2022
1 parent 047d30f commit a919b6a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
14 changes: 14 additions & 0 deletions compiling_with_spawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Examples to setup Spawn in GMT

## Installing Spawn in a container

`https://github.com/NREL/MegaBOP/blob/main/main/Dockerfile#L16-L22`

## Compiling a model

```spawn modelica --create-fmu mixed_loads.Districts.DistrictEnergySystem --jmodelica --modelica-path /home/kbenne/Desktop/modelica/mixed_loads /home/kbenne/Development/modelica-buildings/Buildings/```

- Note that both the MBL and the model folder are passed (with a space) to the `--modelica-path` parameter.
- Note that the MBL path includes `Buildings/` at the end, in addition to the path used in MODELICAPATH elsewhere in the GMT.

https://github.com/urbanopt/docker-spawn-modelica#running-the-examples
57 changes: 57 additions & 0 deletions geojson_modelica_translator/modelica/lib/runner/spawn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse
import os
import re


def compile_fmu(model_name, modelica_path):
"""
Compile a modelica model.
This function shamelessly stolen from https://github.com/NREL/MegaBOP/blob/main/pymodelica/compiler.py#L11
"""
# Spawn's implementation doesn't currently handle *.mo files in the modelica_path,
# it is expecting only directories,
# however BOPTEST's parser will include wrapped.mo in the modelica_path.
# This is a workaround to elliminate .mo items from modelica_path
modelica_path = ' '.join([p for p in modelica_path if not re.match(r'.*\.mo$', p)])

cmd = f'spawn modelica --modelica-path {modelica_path} --fmu-type ME --create-fmu {model_name}'
os.system(cmd)
return f'{model_name}.fmu'


def run(fmu_name, model_name, output_path, log_level):
"""
Run a modelica model.
"""


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('action', help='Action to perform on the model, complile, run, compile_and_run')
parser.add_argument('model', help='Name of the model to run, if debug, then will use test PID model.')
# Since this command is passed with jm_ipython, you can't use the -- args (e.g., --compile).
# So we are just passing in the action and then the model to act on.
# The actions can be (e.g., complile, run, compile_and_run)
args = parser.parse_args()

if args.action == 'help':
print(parser.print_help())

fmu_name = None
if args.action == 'compile' or args.action == 'compile_and_run':
model = args.model
if args.model == 'debug':
model = "Buildings.Controls.OBC.CDL.Continuous.Validation.LimPID"
else:
if os.path.isfile(args.model):
model = args.model.replace(os.path.sep, '.')[:-3]
fmu_name = compile(model)

if args.action == 'run' or args.action == 'compile_and_run':
# Run the FMU either that is passed in or from the previous step
if not fmu_name:
fmu_name = args.model
if os.path.exists(fmu_name):
run(fmu_name)
else:
print("FMU model does not exist: {}".format(fmu_name))
Empty file.
8 changes: 7 additions & 1 deletion geojson_modelica_translator/modelica/modelica_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ def _subprocess_call_to_docker(self, run_path: Path, file_to_run: Str, action: S
run_model = os.path.relpath(file_to_run, run_path)
logger.info(f"{action_log_map[action]}: {run_model} in {run_path}")
p = subprocess.Popen(
['./jm_ipython.sh', 'jmodelica.py', action, run_model],
['spawn.py', '--buildings-library /working/buildings-library', '--compile',
'--optimica', '--optimica-license-file=mylicense.txt', '/gmt/my-model', ],
# ^-- generates an FMU
# below runs the fmu
['spawn.py', '--buildings-library /working/buildings-library', '--run', '/gmt/my-model',
'--start-time=0', '--end-time=86400'],
# ['./jm_ipython.sh', 'jmodelica.py', action, run_model],
stdout=stdout_log,
stderr=subprocess.STDOUT,
cwd=run_path
Expand Down

0 comments on commit a919b6a

Please sign in to comment.