From a919b6a6f72e6d3ae647d39e4b616f1aa4facbaa Mon Sep 17 00:00:00 2001 From: Nathan Moore Date: Tue, 7 Jun 2022 11:01:45 -0600 Subject: [PATCH] wip compile & run with spawn & optimica --- compiling_with_spawn.md | 14 +++++ .../modelica/lib/runner/spawn.py | 57 +++++++++++++++++++ .../modelica/lib/runner/spawn_docker.sh | 0 .../modelica/modelica_runner.py | 8 ++- 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 compiling_with_spawn.md create mode 100644 geojson_modelica_translator/modelica/lib/runner/spawn.py create mode 100644 geojson_modelica_translator/modelica/lib/runner/spawn_docker.sh diff --git a/compiling_with_spawn.md b/compiling_with_spawn.md new file mode 100644 index 000000000..ae2360568 --- /dev/null +++ b/compiling_with_spawn.md @@ -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 diff --git a/geojson_modelica_translator/modelica/lib/runner/spawn.py b/geojson_modelica_translator/modelica/lib/runner/spawn.py new file mode 100644 index 000000000..14d975207 --- /dev/null +++ b/geojson_modelica_translator/modelica/lib/runner/spawn.py @@ -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)) diff --git a/geojson_modelica_translator/modelica/lib/runner/spawn_docker.sh b/geojson_modelica_translator/modelica/lib/runner/spawn_docker.sh new file mode 100644 index 000000000..e69de29bb diff --git a/geojson_modelica_translator/modelica/modelica_runner.py b/geojson_modelica_translator/modelica/modelica_runner.py index 81e60be04..1ea30fbb7 100644 --- a/geojson_modelica_translator/modelica/modelica_runner.py +++ b/geojson_modelica_translator/modelica/modelica_runner.py @@ -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