Package pypower :: Module main
[hide private]
[frames] | no frames]

Source Code for Module pypower.main

  1  # Copyright (C) 2011 Richard Lincoln 
  2  # 
  3  # PYPOWER is free software: you can redistribute it and/or modify 
  4  # it under the terms of the GNU General Public License as published 
  5  # by the Free Software Foundation, either version 3 of the License, 
  6  # or (at your option) any later version. 
  7  # 
  8  # PYPOWER is distributed in the hope that it will be useful, 
  9  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 11  # GNU General Public License for more details. 
 12  # 
 13  # You should have received a copy of the GNU General Public License 
 14  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 15   
 16  """Main entry-points for PYPOWER. 
 17  """ 
 18   
 19  import sys 
 20  from sys import stderr 
 21   
 22  from optparse import OptionParser, OptionGroup, OptionValueError 
 23   
 24  from pypower.api import \ 
 25      ppver, ppoption, runpf, runopf, runuopf, runopf_w_res 
 26   
 27  from pypower.api import \ 
 28      case4gs, case6ww, case9, case9Q, case14, case24_ieee_rts, case30, \ 
 29      case30Q, case30pwl, case39, case57, case118, case300, t_case30_userfcns 
 30   
 31  from pypower.ppoption import \ 
 32      PF_OPTIONS, OPF_OPTIONS, OUTPUT_OPTIONS, PDIPM_OPTIONS 
 33   
 34  from pypower.t.test_pypower import test_pf, test_opf 
 35   
 36   
 37  TYPE_MAP = {bool: 'choice', float: 'float', int: 'int'} 
 38   
 39  AFFIRMATIVE = ('True', 'Yes', 'true', 'yes', '1', 'Y', 'y') 
 40  NEGATIVE = ('False', 'No', 'false', 'no', '0', 'N', 'n') 
 41   
 42  CASES = {'case4gs': case4gs, 'case6ww': case6ww, 'case9': case9, 
 43      'case9Q': case9Q, 'case14': case14, 'case24_ieee_rts': case24_ieee_rts, 
 44      'case30': case30, 'case30Q': case30Q, 'case30pwl': case30pwl, 
 45      'case39': case39, 'case57': case57, 'case118': case118, 'case300': case300, 
 46      'case30_userfcns': t_case30_userfcns} 
 47   
 48   
49 -def option_callback(option, opt, value, parser, *args, **kw_args):
50 if isinstance(value, str): 51 if value in AFFIRMATIVE: 52 value = True 53 elif value in NEGATIVE: 54 value = False 55 else: 56 choices = ", ".join(map(repr, option.choices)) 57 raise OptionValueError( 58 _("option %s: invalid choice: %r (choose from %s)") 59 % (opt, value, choices)) 60 61 opt_name = opt[2:].upper() 62 63 ppopt = args[0] 64 ppopt[opt_name] = value
65 66
67 -def add_options(group, options, *callback_args, **callback_kwargs):
68 for name, default_val, help in options: 69 long_opt = '--%s' % name 70 71 kw_args = { 72 'default': default_val, 73 'help': '%s [default: %%default]' % help, 74 'type': TYPE_MAP.get(type(default_val), 'string'), 75 'action': "callback", 76 'callback': option_callback, 77 'callback_args': callback_args, 78 'callback_kwargs': callback_kwargs 79 } 80 81 if isinstance(default_val, bool): 82 kw_args['choices'] = AFFIRMATIVE + NEGATIVE 83 84 group.add_option(long_opt, **kw_args)
85 86
87 -def parse_options(args, usage, opf=False):
88 """Parse command line options. 89 90 @param opf: Include OPF options? 91 """ 92 v = ppver('all') 93 parser = OptionParser( 94 usage="""usage: %%prog [options] [casedata] 95 96 %s 97 98 If 'casedata' is provided it specifies the name of the input data file 99 containing the case data.""" % usage, 100 version='PYPOWER (%%prog) Version %s, %s' % (v["Version"], v["Date"]) 101 ) 102 103 parser.add_option("-t", "--test", action="store_true", dest="test", 104 default=False, help="run tests and exit") 105 106 parser.add_option("-c", "--testcase", default='case9', choices=CASES.keys(), 107 help="built-in test case, choose from: %s" % str(CASES.keys())[1:-1]) 108 109 parser.add_option("-o", "--outfile", dest='fname', default='', 110 type='string', help="""pretty printed output will be 111 appended to a file with the name specified. Defaults to stdout.""") 112 113 parser.add_option("-s", "--solvedcase", default='', type='string', 114 help="""the solved case will be written 115 to a case file with the specified name in PYPOWER format. If solvedcase ends 116 with '.mat' the case is saves as a MAT-file otherwise it saves it as a Python 117 file.""") 118 119 ppopt = ppoption() 120 121 if opf: 122 opf_options = OptionGroup(parser, 'OPF Options') 123 pdipm_options = OptionGroup(parser, 'PDIPM Options') 124 125 opf_options.add_option("-u", "--uopf", action="store_true", 126 help="""runs an optimal power flow with the unit-decommitment 127 heuristic""") 128 129 opf_options.add_option("-r", "--w_res", action="store_true", 130 help="""runs an optimal power flow with fixed zonal reserves""") 131 132 add_options(opf_options, OPF_OPTIONS, ppopt) 133 add_options(pdipm_options, PDIPM_OPTIONS, ppopt) 134 135 parser.add_option_group(opf_options) 136 parser.add_option_group(pdipm_options) 137 else: 138 pf_options = OptionGroup(parser, 'Power Flow Options') 139 140 add_options(pf_options, PF_OPTIONS, ppopt) 141 142 parser.add_option_group(pf_options) 143 144 output_options = OptionGroup(parser, 'Output Options') 145 146 add_options(output_options, OUTPUT_OPTIONS, ppopt) 147 148 parser.add_option_group(output_options) 149 150 151 options, args = parser.parse_args(args) 152 153 # casedata, fname, solvedcase = case9(), '', '' # defaults 154 155 nargs = len(args) 156 if nargs > 1: 157 stderr.write('Too many arguments') 158 parser.print_help() 159 sys.exit(2) 160 elif nargs == 1: 161 casedata = args[0] 162 else: 163 try: 164 casedata = CASES[options.testcase]() 165 except KeyError: 166 stderr.write("Invalid case choice: %r (choose from %s)\n" % \ 167 (options.testcase, CASES.keys())) 168 sys.exit(2) 169 170 return options, casedata, ppopt, options.fname, options.solvedcase
171 172
173 -def exit(success):
174 sys.exit(0 if success else 2)
175 176
177 -def pf(args=sys.argv[1:]):
178 usage = 'Runs a power flow.' 179 options, casedata, ppopt, fname, solvedcase = \ 180 parse_options(args, usage) 181 if options.test: 182 sys.exit(test_pf()) 183 _, success = runpf(casedata, ppopt, fname, solvedcase) 184 exit(success)
185 186
187 -def opf(args=sys.argv[1:]):
188 usage = 'Runs an optimal power flow.' 189 options, casedata, ppopt, fname, solvedcase = \ 190 parse_options(args, usage, True) 191 192 if options.test: 193 sys.exit(test_opf()) 194 195 if options.uopf: 196 if options.w_res: 197 stderr.write('uopf and opf_w_res are mutex\n') 198 r = runuopf(casedata, ppopt, fname, solvedcase) 199 elif options.w_res: 200 r = runopf_w_res(casedata, ppopt, fname, solvedcase) 201 else: 202 r = runopf(casedata, ppopt, fname, solvedcase) 203 exit(r['success'])
204 205 206 if __name__ == '__main__': 207 pf(['-h']) 208 # pf(['-c', 'case9', '--out_all=-1', '-s', '/tmp/out.py']) 209