Skip to content

Commit

Permalink
reopt_jl logging for MicrogridUP.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjankovic committed May 16, 2024
1 parent 6d4a837 commit 879ac7e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
9 changes: 6 additions & 3 deletions omf/models/microgridDesign.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 17 additions & 7 deletions omf/solvers/reopt_jl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json, time
import os, platform
import random
import subprocess

thisDir = str(os.path.abspath(os.path.dirname(__file__)))

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 879ac7e

Please sign in to comment.