Skip to content

Commit

Permalink
fix(eplus): make load_idf parallel safe
Browse files Browse the repository at this point in the history
  • Loading branch information
taoning committed Nov 5, 2024
1 parent 4c20524 commit 9946950
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions frads/eplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import inspect
import json
import tempfile
import os
from pathlib import Path
from typing import Callable, Dict, List, Optional, Union

Expand Down Expand Up @@ -691,17 +692,24 @@ def load_idf(fpath: Union[str, Path]) -> dict:
raise ValueError(f"File {fpath} is not an IDF file.")
if not fpath.exists():
raise FileNotFoundError(f"File {fpath} not found.")
api = EnergyPlusAPI()
state = api.state_manager.new_state()
api.runtime.set_console_output_status(state, False)
api.runtime.run_energyplus(state, ["--convert-only", str(fpath)])
api.state_manager.delete_state(state)
epjson_path = Path(fpath.with_suffix(".epJSON").name)
if not epjson_path.exists():
raise FileNotFoundError(f"Converted {str(epjson_path)} not found.")
with open(epjson_path) as f:
json_data = json.load(f)
epjson_path.unlink()
fpath = fpath.absolute()
original_dir = os.getcwd()
with tempfile.TemporaryDirectory() as td:
try:
os.chdir(td)
api = EnergyPlusAPI()
state = api.state_manager.new_state()
api.runtime.set_console_output_status(state, False)
api.runtime.run_energyplus(state, ["--convert-only", str(fpath)])
api.state_manager.delete_state(state)
epjson_path = Path(fpath.with_suffix(".epJSON").name)
if not epjson_path.exists():
raise FileNotFoundError(f"Converted {str(epjson_path)} not found.")
with open(epjson_path) as f:
json_data = json.load(f)
epjson_path.unlink()
finally:
os.chdir(original_dir)
return json_data


Expand Down

0 comments on commit 9946950

Please sign in to comment.