From 879ac7e5d24966eee8f1f4fe698c2840e86a5bf9 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 16 May 2024 15:48:51 -0400 Subject: [PATCH] reopt_jl logging for MicrogridUP. --- omf/models/microgridDesign.py | 9 ++++++--- omf/solvers/reopt_jl/__init__.py | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/omf/models/microgridDesign.py b/omf/models/microgridDesign.py index 946bb9616..156943da1 100644 --- a/omf/models/microgridDesign.py +++ b/omf/models/microgridDesign.py @@ -282,9 +282,12 @@ def work(modelDir, inputDict): json.dump(scenario, jsonFile) # Run REopt API script *** => switched to REopt.jl - reopt_jl.run_reopt_jl(modelDir, "Scenario_test_POST.json", outages=run_outages, max_runtime_s = max_runtime, tolerance = tolerance) - with open(pJoin(modelDir, 'results.json')) as jsonFile: - results = json.load(jsonFile) + try: + output = reopt_jl.run_reopt_jl(modelDir, "Scenario_test_POST.json", outages=run_outages, max_runtime_s = max_runtime, tolerance = tolerance) + with open(pJoin(modelDir, 'results.json')) as jsonFile: + results = json.load(jsonFile) + except FileNotFoundError: + raise RuntimeError(f"results.json file not found. Output: {output}") #getting REoptInputs to access default input values more easily with open(pJoin(modelDir, 'REoptInputs.json')) as jsonFile: diff --git a/omf/solvers/reopt_jl/__init__.py b/omf/solvers/reopt_jl/__init__.py index 10b7325cc..86d81340c 100644 --- a/omf/solvers/reopt_jl/__init__.py +++ b/omf/solvers/reopt_jl/__init__.py @@ -1,6 +1,7 @@ import json, time import os, platform import random +import subprocess thisDir = str(os.path.abspath(os.path.dirname(__file__))) @@ -287,19 +288,28 @@ def run_reopt_jl(path, inputFile="", loadFile="", default=False, outages=False, if run_with_sysimage: sysimage_path = os.path.normpath(os.path.join(thisDir,"reopt_jl.so")) - os.system(f'''julia --sysimage="{sysimage_path}" -e ' - using .REoptSolver; - ENV["NREL_DEVELOPER_API_KEY"]="{api_key}"; - REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}", {tolerance}) - ' ''') + command = f'''julia --sysimage="{sysimage_path}" -e ' + using .REoptSolver; + ENV["NREL_DEVELOPER_API_KEY"]="{api_key}"; + REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}", {tolerance})' + ''' else: project_path = os.path.normpath(os.path.join(thisDir,"REoptSolver")) - os.system(f'''julia --project="{project_path}" -e ' + command = f'''julia --project="{project_path}" -e ' using Pkg; Pkg.instantiate(); import REoptSolver; ENV["NREL_DEVELOPER_API_KEY"]="{api_key}"; REoptSolver.run("{path}", {outages_jl}, {microgrid_only_jl}, {max_runtime_s_jl}, "{api_key}", {tolerance}) - ' ''') + ' ''' + + # As each line becomes available, print to terminal and append to return variable. + output = [] + process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) + for line in process.stdout: + print(line, end="") + output.append(line) + return ''.join(output) + except Exception as e: print(e)