-
Notifications
You must be signed in to change notification settings - Fork 2
/
msolve.py
executable file
·83 lines (68 loc) · 2.3 KB
/
msolve.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
#!/usr/bin/env python3
"""msolve.py: Multi year solution wrapper openCEM"""
__version__ = "0.9.2"
__author__ = "José Zapata"
__copyright__ = "Copyright 2018, ITP Renewables, Australia"
__credits__ = ["José Zapata", "Dylan McConnell", "Navid Hagdadi"]
__license__ = "GPLv3"
__maintainer__ = "José Zapata"
__email__ = "[email protected]"
__status__ = "Development"
import argparse
import datetime
import os
import time
from cemo.multi import SolveTemplate
def valid_file(param):
base, ext = os.path.splitext(param)
if ext not in ('.cfg'):
raise argparse.ArgumentTypeError('File must have a cfg extension')
return param
# start the clock on the run
start_time = time.time()
# create parser object
parser = argparse.ArgumentParser(description="openCEM multiyear model solver")
# Multi year simulation option using a configuration file
parser.add_argument(
"config",
help="Specify a configuration file for simulation" +
" Note: Python configuration files named CONFIG.cfg",
type=lambda f: valid_file(f),
metavar='CONFIG')
# Obtain a solver name from command line, default cbc
parser.add_argument(
"--solver",
help="Specify solver used by model." +
" For Pyomo supported solvers installed in your system ",
type=str,
metavar='SOLVER',
default="cbc")
# Zip and upload result to custom directory
parser.add_argument(
"--log",
help="Request solver logging and traceback information",
action='store_true')
parser.add_argument(
"-k",
"--keepfiles",
help=
"Save generated files onto a folder with the same name as the configuration file",
action='store_true')
# parse arguments into args structure
args = parser.parse_args()
# Read configuration file name from
cfgfile = args.config
# create Multi year simulation
X = SolveTemplate(cfgfile, solver=args.solver, log=args.log)
# make a temporary directoy
if args.keepfiles:
path = cfgfile.split(".")[0] + '/'
if not os.path.exists(path):
os.mkdir(path)
X.tmpdir = path
# instruct the solver to launch the multi year simulation
print("openCEM msolve.py: Runtime %s (pre solver)" % str(
datetime.timedelta(seconds=(time.time() - start_time))))
X.solve()
print("openCEM msolve.py: Runtime %s (post solver)" % str(
datetime.timedelta(seconds=(time.time() - start_time))))