-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmptest.py
113 lines (93 loc) · 4.22 KB
/
mptest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
/***************************************************************************
SimstockQGIS
copyright : (C) 2023 by UCL
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import multiprocessing as mp
#import numpy as np
#import time
import os
import platform
import subprocess
import argparse
import json
import pandas as pd
import sys
# Add psutil location to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "eppy-scripts"))
try:
import psutil as pu
except:
pass
parser = argparse.ArgumentParser()
parser.add_argument("cwd", help="The path to the user cwd")
parser.add_argument("--singlecore", action="store_true", help="If specified, simulation will use single core")
args = parser.parse_args()
class EP_Run():
def __init__(self, cwd):
self.plugin_dir = os.path.dirname(__file__)
self.EP_DIR = os.path.join(self.plugin_dir, "EnergyPlus")
self.cwd = cwd
self.idf_dir = os.path.join(cwd, "idf_files")
self.preprocessed_df = pd.read_csv(os.path.join(self.plugin_dir, "sa_preprocessed.csv"))
self.idf_files = [os.path.join(self.idf_dir, f"{bi}.idf") for bi in self.preprocessed_df[self.preprocessed_df["shading"]==False]["bi"].unique()]
# Load config file
with open(os.path.join(self.plugin_dir, "config.json"), "r") as read_file:
self.config = json.load(read_file)
self.epw_file = os.path.join(self.cwd, self.config["epw"])
# Find the computer's operating system and find energyplus version
system = platform.system().lower()
if system in ['windows', 'linux', 'darwin']:
self.energyplusexe = os.path.join(self.EP_DIR, 'ep8.9_{}/energyplus'.format(system))
self.readvarseso = os.path.join(self.EP_DIR, 'ep8.9_{}/ReadVarsESO'.format(system))
def run_ep(self, idf_file):
output_dir = idf_file[:-4]
output_path = os.path.join(self.idf_dir, output_dir)
# Run the EnergyPlus simulation
out = subprocess.run([self.energyplusexe, '-d', output_dir, '-w', self.epw_file, idf_file],
cwd = self.idf_dir, capture_output=True, text=True) #no readvarseso
if out.returncode == 1:
raise RuntimeError(out.stderr+f"\nCheck the EnergyPlus err file '{os.path.join(output_path, 'eplusout.err')}'")
# Generate the .rvi file
self.generate_rvi(output_path)
# Call ReadVarsESO to produce the results csv
self.run_readvarseso(output_path)
@staticmethod
def generate_rvi(output_path):
with open (os.path.join(output_path, "results-rvi.rvi"), "w") as f:
f.write("eplusout.eso\neplusout.csv\n0")
def run_readvarseso(self, output_path):
subprocess.run([self.readvarseso, "results-rvi.rvi", "unlimited"], cwd=output_path)
def run_ep_multi(self, cores):
p = mp.Pool(cores)
p.map(self.run_ep, self.idf_files)
p.close()
def run_ep_single(self):
for i, idf_file in enumerate(self.idf_files):
print(f"Starting simulation {i+1} of {len(self.idf_files)}")
self.run_ep(idf_file)
def main():
cwd = args.cwd
runner = EP_Run(cwd)
# Single-core simulation
if args.singlecore:
runner.run_ep_single()
# Multi-core simulation
else:
try:
cores = pu.cpu_count(logical=False) - 1 #use one less core than available
except:
cores = mp.cpu_count() - 1
runner.run_ep_multi(cores)
if __name__ == '__main__':
main()