How to use parse_token method in yandex-tank

Best Python code snippet using yandex-tank

Data.py

Source:Data.py Github

copy

Full Screen

...29def alert(alert_dict):30 print(alert_dict)31 32#gets a value from a specific place on a line and turns it into the desired value type33def parse_token(token, val_type, default=None, isIntFloat = False):34 val = None35 if len(token) > 0:36 if isIntFloat:37 token = re.search(r'\d+', token).group()38 val = val_type(token)39 elif default is not None:40 val = val_type(default)41 else:42 try:43 print('required field missing data, token: %s, val_type: %s' % (token, val_type))44 raise Exception('empty field not allowed')45 except Exception as e:46 traceback.print_exc()47 raise e48 #raise Exception('empty field not allowed')49 return val50#removes comments at the end of the row, and raises an exception if 51#the row is missing/has extra fields52def pad_row(row, new_row_len):53 try:54 if len(row) != new_row_len:55 print(len(row), new_row_len)56 if len(row) < new_row_len:57 print('missing field, row:')58 print(row)59 raise Exception('missing field not allowed')60 elif len(row) > new_row_len:61 row = remove_end_of_line_comment_from_row(row, '/')62 '''if len(row) > new_row_len:63 alert(64 {'data_type': 'Data',65 'error_message': 'extra field, please ensure that all rows have the correcct number of fields',66 'diagnostics': str(row)})'''67 68 else:69 row = remove_end_of_line_comment_from_row(row, '/')70 except Exception as e:71 traceback.print_exc()72 raise e73 return row74 75#adds blanks to the end of the row to make parsing easier76def make_full_row(row, new_row_len):77 row_len = len(row)78 row_len_diff = new_row_len - row_len79 row_new = row80 if row_len_diff > 0:81 row_new = row + row_len_diff * ['']82 return row_new83 84#checks if the row is missing fields85def check_row_missing_fields(row, row_len_expected):86 try:87 if len(row) < row_len_expected:88 print('missing field, row:')89 print(row)90 raise Exception('missing field not allowed')91 except Exception as e:92 traceback.print_exc()93 raise e94def remove_end_of_line_comment_from_row_first_occurence(row, end_of_line_str):95 index = [r.find(end_of_line_str) for r in row]96 len_row = len(row)97 entries_with_end_of_line_strs = [i for i in range(len_row) if index[i] > -1]98 num_entries_with_end_of_line_strs = len(entries_with_end_of_line_strs)99 if num_entries_with_end_of_line_strs > 0:100 first_entry_with_end_of_line_str = min(entries_with_end_of_line_strs)101 len_row_new = first_entry_with_end_of_line_str + 1102 row_new = [row[i] for i in range(len_row_new)]103 row_new[len_row_new - 1] = remove_end_of_line_comment(row_new[len_row_new - 1], end_of_line_str)104 else:105 row_new = [r for r in row]106 return row_new107def remove_end_of_line_comment_from_row(row, end_of_line_str):108 index = [r.find(end_of_line_str) for r in row]109 len_row = len(row)110 entries_with_end_of_line_strs = [i for i in range(len_row) if index[i] > -1]111 num_entries_with_end_of_line_strs = len(entries_with_end_of_line_strs)112 if num_entries_with_end_of_line_strs > 0:113 #last_entry_with_end_of_line_str = min(entries_with_end_of_line_strs)114 #len_row_new = last_entry_with_end_of_line_str + 1115 row_new = [r for r in row]116 #row_new = [row[i] for i in range(len_row_new)]117 for i in entries_with_end_of_line_strs:118 row_new[i] = remove_end_of_line_comment(row_new[i], end_of_line_str)119 #row_new[len_row_new - 1] = remove_end_of_line_comment(row_new[len_row_new - 1], end_of_line_str)120 else:121 #row_new = [r for r in row]122 row_new = row123 return row_new124def remove_end_of_line_comment(token, end_of_line_str):125 126 token_new = token127 index = token_new.find(end_of_line_str)128 if index > -1:129 token_new = token_new[0:index]130 return token_new131 132#data class 133class Data:134 '''In physical units, i.e. data convention, i.e. input and output data files'''135 #here read/write for other file types (.INL, .CON) can be added136 def __init__(self):137 self.raw = Raw()138 139 def read(self, raw_name):140 self.raw.read(raw_name)141 def write(self, raw_name):142 self.raw.write(raw_name)143 144 145#RAW file class 146class Raw:147 '''In physical units, i.e. data convention, i.e. input and output data files'''148 def __init__(self):149 self.case_identification = CaseIdentification()150 self.buses = {}151 self.loads = {}152 self.fixed_shunts = {}153 self.generators = {}154 self.nontransformer_branches = {}155 self.transformers = {}156 self.areas = {}157 self.switched_shunts = {}158 self.zones = {}159 self.owners = {}160 self.interareas = {}161 self.vsc = {}162 self.two_terminal = {}163 self.facts_device = {}164 self.multi_section_lines = {}165 self.imp_corr_tables = {}166 self.two_wind_xfmrs = {}167 self.three_wind_xfmrs = {}168 self.slack_buses = []169 170 #all of the "get" functions reference the classes of the type they are getting 171 def get_buses(self):172 return sorted(self.buses.values(), key=(lambda r: r.i))173 def get_loads(self):174 return sorted(self.loads.values(), key=(lambda r: (r.i, r.id)))175 def get_fixed_shunts(self):176 return sorted(self.fixed_shunts.values(), key=(lambda r: (r.i, r.id)))177 def get_generators(self):178 return sorted(self.generators.values(), key=(lambda r: (r.i, r.id)))179 def get_nontransformer_branches(self):180 return sorted(self.nontransformer_branches.values(), key=(lambda r: (r.i, r.j, r.ckt)))181 def get_two_xfmrs(self):182 return sorted(self.two_wind_xfmrs.values(), key=(lambda r: (r.i, r.j, r.k, r.ckt)))183 184 def get_three_xfmrs(self):185 186 return sorted(self.three_wind_xfmrs.values(), key=(lambda r: (r.i, r.j, r.k, r.ckt)))187 def get_areas(self):188 return sorted(self.areas.values(), key=(lambda r: r.i))189 190 def get_switched_shunts(self):191 return sorted(self.switched_shunts.values(), key=(lambda r: r.i))192 193 def get_zones(self):194 return sorted(self.zones.values(), key = (lambda r: r.i))195 196 def get_owners(self):197 return sorted(self.owners.values(), key = (lambda r: r.i))198 199 def get_interareas(self):200 return sorted(self.interareas.values(), key = (lambda r: (r.arfrom, r.arto, r.trid)))201 202 def get_vsc(self):203 return sorted(self.vsc.values(), key = (lambda r: r.name))204 205 def get_two_term_dc(self):206 return sorted(self.two_terminal.values(), key = (lambda r: (r.name, r.ipi, r.ipi)))207 208 def get_facts_device(self):209 return sorted(self.facts_device.values(), key = (lambda r: (r.i,r.j)))210 211 def get_multi_section_line(self):212 return sorted(self.multi_section_lines.values(), key = (lambda r: (r.i, r.j, r.id)))213 214 def get_ic_tables(self):215 return sorted(self.imp_corr_tables.values(), key = (lambda r: r.i))216 ### Writing ###217 218 219 def switched_shunts_combine_blocks_steps(self):220 for r in self.switched_shunts.values():221 b_min_max = r.compute_bmin_bmax()222 b_min = b_min_max[0]223 b_max = b_min_max[1]224 r.n1 = 0225 r.b1 = 0.0226 r.n2 = 0227 r.b2 = 0.0228 r.n3 = 0229 r.b3 = 0.0230 r.n4 = 0231 r.b4 = 0.0232 r.n5 = 0233 r.b5 = 0.0234 r.n6 = 0235 r.b6 = 0.0236 r.n7 = 0237 r.b7 = 0.0238 r.n8 = 0239 r.b8 = 0.0240 if b_max > 0.0:241 r.n1 = 1242 r.b1 = b_max243 if b_min < 0.0:244 r.n2 = 1245 r.b2 = b_min246 elif b_min < 0.0:247 r.n1 = 1248 r.b1 = b_min249 ### Reading ###250 def find_delim(self, lines): #finds if the delimiter is space or comma251 first_line = lines[0]252 if "," in first_line:253 return ","254 else:255 return " "256 257 #reads the raw file258 def read(self, file_name):259 with open(file_name, 'r') as in_file:260 lines = in_file.readlines()261 delimiter_str = self.find_delim(lines)262 263 quote_str = "'"264 skip_initial_space = True265 rows = csv.reader(266 lines,267 delimiter=delimiter_str,268 quotechar=quote_str,269 skipinitialspace=skip_initial_space)270 rows = [[t.strip() for t in r] for r in rows]271 self.read_from_rows(rows, file_name)272 #self.set_areas_from_buses()273 274 #checks of a row is the end of the whole file 275 def row_is_file_end(self, row):276 is_file_end = False277 if len(row) == 0:278 is_file_end = True279 if row[0][:1] in {'','q','Q'}:280 is_file_end = True281 return is_file_end282 283 #checks if a row is the end of a data type/start of a new data type284 def row_is_section_end(self, row): 285 temp_row = ' '.join(row)286 new_row = remove_end_of_line_comment(temp_row, ' /')287 288 is_section_end = False289 if (new_row == '0'):290 is_section_end = True291 return is_section_end292 293 def row_is_key(self, row):294 if row[0][0] == '@':295 return True296 return False297 298 #actually does the work of reading from the raw file299 300 301 def read_from_rows(self, rows, file_name):302 303 row_num = 0304 if self.row_is_key(rows[row_num]):305 row_num += 1306 cid_rows = rows[row_num:(row_num + 3)]307 self.case_identification.record_2 = ' '.join(cid_rows[1])308 self.case_identification.record_3 = ' '.join(cid_rows[2])309 #case ID data310 if len(rows[row_num]) == 2:311 self.case_identification.read_from_rows(rows, 30)312 else:313 self.case_identification.read_from_rows(rows)314 version = self.case_identification.rev315 row_num += 2316 317 if version == 34:318 while True: #system wide data319 row_num += 1320 row = rows[row_num]321 if self.row_is_file_end(row):322 return323 if self.row_is_section_end(row):324 break325 while True: #bus data326 327 row_num += 1328 row = rows[row_num]329 while True:330 331 332 if rows[row_num][0][0] == '@':333 334 row_num += 1335 else:336 337 break338 row = rows[row_num]339 if self.row_is_file_end(row):340 return341 if self.row_is_section_end(row):342 break343 row = rows[row_num]344 bus = Bus(version)345 bus.read_from_row(row, version)346 self.buses[bus.i] = bus347 if bus.ide == 3:348 #print(bus.i)349 self.slack_buses.append(bus.i)350 s_r = row_num351 while True: #load data352 row_num += 1353 row = rows[row_num]354 while True:355 if rows[row_num][0][0] == '@':356 row_num += 1357 else:358 break359 row = rows[row_num]360 if self.row_is_file_end(row):361 return362 if self.row_is_section_end(row):363 break364 row = rows[row_num]365 load = Load(version)366 load.read_from_row(row, version)367 self.loads[(load.i, load.id)] = load368 if version != 30: #version 30 does not have fixed bus shunt data369 while True: #fixed bus shunt data370 row_num += 1371 row = rows[row_num]372 while True:373 if rows[row_num][0][0] == '@':374 row_num += 1375 else:376 break377 row = rows[row_num]378 if self.row_is_file_end(row):379 return380 if self.row_is_section_end(row):381 break382 row = rows[row_num]383 fixed_shunt = FixedShunt()384 fixed_shunt.read_from_row(row, version)385 self.fixed_shunts[(fixed_shunt.i, fixed_shunt.id)] = fixed_shunt386 while True: #generator data387 row_num += 1388 row = rows[row_num]389 while True:390 if rows[row_num][0][0] == '@':391 row_num += 1392 else:393 break394 row = rows[row_num]395 if self.row_is_file_end(row):396 return397 if self.row_is_section_end(row):398 break399 row = rows[row_num]400 generator = Generator()401 generator.read_from_row(row, version)402 self.generators[(generator.i, generator.id)] = generator403 while True: #branch data404 row_num += 1405 row = rows[row_num]406 while True:407 if rows[row_num][0][0] == '@':408 row_num += 1409 else:410 break411 row = rows[row_num]412 if self.row_is_file_end(row):413 return414 if self.row_is_section_end(row):415 break416 row = rows[row_num]417 nontransformer_branch = NontransformerBranch()418 nontransformer_branch.read_from_row(row, version)419 self.nontransformer_branches[(420 nontransformer_branch.i,421 nontransformer_branch.j,422 nontransformer_branch.ckt)] = nontransformer_branch423 424 if version == 34:425 while True: #system switching device data426 row_num += 1427 row = rows[row_num]428 while True:429 if rows[row_num][0][0] == '@':430 row_num += 1431 else:432 row = rows[row_num]433 break434 435 if self.row_is_file_end(row):436 return437 if self.row_is_section_end(row):438 break439 440 while True: #transformer data441 row_num += 1442 row = rows[row_num]443 444 while True:445 if rows[row_num][0][0] == '@':446 row_num += 1447 else:448 break449 row = rows[row_num]450 if self.row_is_file_end(row):451 return452 if self.row_is_section_end(row):453 break454 455 456 if row[2] == '0': #two winding transformer457 two_xfmr = Two_xfmr(version)458 row2 = rows[row_num+1]459 row3 = rows[row_num+2]460 row4 = rows[row_num+3]461 two_xfmr.read_from_row(row,row2, row3, row4, version)462 self.two_wind_xfmrs[two_xfmr.i, two_xfmr.j, two_xfmr.ckt] = two_xfmr463 row_num += 3464 else: #three winding transformer465 three_xfmr = Three_xfmr()466 row2 = rows[row_num+1]467 row3 = rows[row_num+2]468 row4 = rows[row_num+3]469 row5 = rows[row_num+4]470 three_xfmr.read_from_row(row, row2, row3, row4, row5, version)471 self.three_wind_xfmrs[three_xfmr.i, three_xfmr.j, three_xfmr.k, three_xfmr.ckt] = three_xfmr472 row_num += 4473 474 while True: # areas 475 row_num += 1476 row = rows[row_num]477 while True:478 if rows[row_num][0][0] == '@':479 row_num += 1480 else:481 break482 row = rows[row_num]483 if self.row_is_file_end(row):484 return485 if self.row_is_section_end(row):486 break487 row = rows[row_num]488 area = Area()489 area.read_from_row(row, version)490 self.areas[area.i] = area491 492 while True: #two-terminal DC transmission line data493 row_num += 1494 row = rows[row_num]495 while True:496 if rows[row_num][0][0] == '@':497 row_num += 1498 else:499 break500 row = rows[row_num]501 if self.row_is_file_end(row):502 return503 if self.row_is_section_end(row):504 break505 row = rows[row_num]506 row2 = rows[row_num+1]507 row3 = rows[row_num+2]508 two_term_dc_line = Two_terminal_dc()509 two_term_dc_line.read_from_row(row, row2, row3, version)510 self.two_terminal[(two_term_dc_line.name, two_term_dc_line.ipr, two_term_dc_line.ipi)] = two_term_dc_line511 row_num +=2512 while True: #VSC DC transmission line data513 row_num += 1514 row = rows[row_num]515 while True:516 if rows[row_num][0][0] == '@':517 row_num += 1518 else:519 break520 row = rows[row_num]521 if self.row_is_file_end(row):522 return523 if self.row_is_section_end(row):524 break525 526 vsc_dc_line = VSC()527 row = rows[row_num]528 row2 = rows[row_num+1]529 row3 = rows[row_num+2]530 vsc_dc_line.read_from_row(row, row2, row3, version)531 self.vsc[vsc_dc_line.name] = vsc_dc_line532 row_num += 2533 if version == 30:534 while True: #switched shunt data535 row_num += 1536 row = rows[row_num]537 while True:538 if rows[row_num][0][0] == '@':539 row_num += 1540 else:541 break542 row = rows[row_num]543 if self.row_is_file_end(row):544 return545 if self.row_is_section_end(row):546 break547 row = rows[row_num]548 switched_shunt = SwitchedShunt()549 switched_shunt.read_from_row(row, version)550 self.switched_shunts[switched_shunt.i] = switched_shunt551 while True: #xfmr impedance corr. tables552 553 row_num += 1554 row = rows[row_num]555 while True:556 if rows[row_num][0][0] == '@':557 row_num += 1558 else:559 break560 row = rows[row_num]561 if self.row_is_file_end(row):562 return563 if self.row_is_section_end(row):564 break565 if version == 3:566 break567 row = rows[row_num]568 ic_table = Impedance_corr()569 ic_table.read_from_row(row, version)570 self.imp_corr_tables[ic_table.i] = ic_table571 while True: #Multi-terminal DC transmission line data572 row_num += 1573 row = rows[row_num]574 while True:575 if rows[row_num][0][0] == '@':576 row_num += 1577 else:578 break579 row = rows[row_num]580 if self.row_is_file_end(row):581 return582 if self.row_is_section_end(row):583 break584 row = rows[row_num]585 while True: #multi-section line grouping data586 row_num += 1587 row = rows[row_num]588 while True:589 if rows[row_num][0][0] == '@':590 row_num += 1591 else:592 break593 row = rows[row_num]594 if self.row_is_file_end(row):595 return596 if self.row_is_section_end(row):597 break598 row = rows[row_num]599 ms_line_grouping = Multi_section_line_grouping()600 ms_line_grouping.read_from_row(row, version)601 self.multi_section_lines[(ms_line_grouping.i, ms_line_grouping.j, ms_line_grouping.id)] =ms_line_grouping602 while True: # zone data603 row_num += 1604 row = rows[row_num]605 while True:606 if rows[row_num][0][0] == '@':607 row_num += 1608 else:609 break610 row = rows[row_num]611 if self.row_is_file_end(row):612 return613 if self.row_is_section_end(row):614 break615 row = rows[row_num]616 zone = Zone()617 zone.read_from_row(row, version)618 self.zones[zone.i] = zone619 while True: #interarea transfer data620 row_num += 1621 row = rows[row_num]622 while True:623 if rows[row_num][0][0] == '@':624 row_num += 1625 else:626 break627 row = rows[row_num]628 if self.row_is_file_end(row):629 return630 if self.row_is_section_end(row):631 break632 row = rows[row_num]633 interarea = Interarea_transfer()634 interarea.read_from_row(row, version)635 self.interareas[(interarea.arfrom, interarea.arto, interarea.trid)] = interarea636 while True: #owner data637 row_num += 1638 row = rows[row_num]639 while True:640 if rows[row_num][0][0] == '@':641 row_num += 1642 else:643 break644 row = rows[row_num]645 if self.row_is_file_end(row):646 return647 if self.row_is_section_end(row):648 break649 row = rows[row_num]650 owner = Owner()651 owner.read_from_row(row, version)652 self.owners[owner.i] = owner653 while True: #facts device data 654 row_num += 1655 row = rows[row_num]656 while True:657 if rows[row_num][0][0] == '@':658 row_num += 1659 else:660 break661 row = rows[row_num]662 if self.row_is_file_end(row):663 return664 if self.row_is_section_end(row):665 break666 row = rows[row_num]667 fact = FACTS_device()668 fact.read_from_row(row, version)669 self.facts_device[(fact.i,fact.j)] = fact670 if version != 30:671 while True: #switched shunt data672 row_num += 1673 row = rows[row_num]674 while True:675 if rows[row_num][0][0] == '@':676 row_num += 1677 else:678 break679 row = rows[row_num]680 if self.row_is_file_end(row):681 return682 if self.row_is_section_end(row):683 break684 row = rows[row_num]685 switched_shunt = SwitchedShunt()686 switched_shunt.read_from_row(row, version)687 self.switched_shunts[switched_shunt.i] = switched_shunt688 if version == 34 or version == 33:689 while True: #GNE device data690 row_num += 1691 row = rows[row_num]692 while True:693 if rows[row_num][0][0] == '@':694 row_num += 1695 else:696 break697 if self.row_is_file_end(row):698 return699 if self.row_is_section_end(row):700 break701 row = rows[row_num]702 if version == 34:703 while True: #induction machine data704 row_num += 1705 row = rows[row_num]706 while True:707 if rows[row_num][0][0] == '@':708 row_num += 1709 else:710 break711 if self.row_is_file_end(row):712 return713 if self.row_is_section_end(row):714 break715 row = rows[row_num]716 while True: #substation data717 row_num += 1718 row = rows[row_num]719 while True:720 if rows[row_num][0][0] == '@':721 row_num += 1722 else:723 break724 if self.row_is_file_end(row):725 return726 if self.row_is_section_end(row):727 break728 row = rows[row_num]729 730 731#### Data Type Classes #### 732 733class CaseIdentification:734 def __init__(self):735 self.ic = 0736 self.sbase = 100.0737 self.rev = 33738 self.xfrrat = 0739 self.nxfrat = 1740 self.basfrq = 60.0741 self.record_2 = 'GRID OPTIMIZATION COMPETITION'742 self.record_3 = 'INPUT DATA FILES ARE RAW '743 744 def read_record_1_from_row(self, row, rev = 33):745 if rev == 30:746 self.ic = parse_token(row[0], int, 0)747 self.sbase = parse_token(row[1], float, default=None)748 self.rev = 30749 else:750 row = row[0:6]751 row = pad_row(row, 6)752 row[5] = remove_end_of_line_comment(row[5], '/')753 754 self.sbase = parse_token(row[1], float, default=None)755 756 self.ic = parse_token(row[0], int, 0)757 self.rev = parse_token(row[2], int, 33)758 #self.xfrrat = (1 if (parse_token(row[3], float, 0.0) > 0.0) else 0)759 #self.nxfrat = (1 if (parse_token(row[4], float, 1.0) > 0.0) else 0)760 self.xfrrat = parse_token(row[3], int, 0)761 self.nxfrat = parse_token(row[4], int, 1)762 self.basfrq = parse_token(row[5], float, 60.0) # need to remove end of line comment763 def get_other_records(self, row1, row2):764 self.record_2 = row1765 self.record_3 = row2766 767 def read_from_rows(self, rows, rev = 33):768 row_num = 0769 while True:770 if rows[row_num][0][0] == '@':771 row_num += 1772 else:773 break774 775 self.read_record_1_from_row(rows[row_num], rev)776 #self.record_2 = '' # not preserving these at this point777 #self.record_3 = '' # do that later778class Bus:779 def __init__(self, version = 32):780 self.i = None # no default allowed - we want this to throw an error781 self.name = 12*' '782 self.baskv = 0.0783 self.ide = 1784 self.area = 1785 self.zone = 1786 self.owner = 1787 self.vm = 1.0788 self.va = 0.0789 self.version = version790 if self.version in (33, 34):791 self.nvhi = 1.1792 self.nvlo = 0.9793 self.evhi = 1.1794 self.evlo = 0.9795 if self.version == 30:796 self.gl = 0.0797 self.bl = 0.0798 def clean_name(self):799 self.name = ''800 def read_from_row(self, row, rev):801 goal_rows = 9802 if rev == 32:803 goal_rows = 9804 elif rev == 34 or rev==33:805 goal_rows = 13806 elif rev == 30:807 goal_rows = 11808 809 row = pad_row(row, goal_rows)810 if rev == 30:811 self.i = parse_token(row[0], int, default=None)812 self.name = parse_token(row[1], str, 12*' ')813 self.baskv = parse_token(row[2], float, 0.0)814 self.ide = parse_token(row[3], int, 1)815 self.gl = parse_token(row[4], float, default=0.0)816 self.bl = parse_token(row[5], float, default=0.0)817 self.area = parse_token(row[6], int, default=1)818 self.zone = parse_token(row[7], int, 1)819 self.vm = parse_token(row[8], float, default=1.0)820 self.va = parse_token(row[9], float, default=0.0)821 self.owner = parse_token(row[10], int, 1)822 else:823 self.i = parse_token(row[0], int, default=None)824 self.area = parse_token(row[4], int, default=1)825 self.vm = parse_token(row[7], float, default=1.0)826 self.va = parse_token(row[8], float, default=0.0)827 if id in (33, 34):828 self.nvhi = parse_token(row[9], float, default=1.1)829 self.nvlo = parse_token(row[10], float, default=0.9)830 self.evhi = parse_token(row[11], float, default=1.1)831 self.evlo = parse_token(row[12], float, default=0.9)832 833 self.name = parse_token(row[1], str, 12*' ')834 self.baskv = parse_token(row[2], float, 0.0)835 self.ide = parse_token(row[3], int, 1)836 self.zone = parse_token(row[5], int, 1)837 self.owner = parse_token(row[6], int, 1)838 839 840class Load:841 def __init__(self, version = 32):842 self.i = None # no default allowed - should be an error843 self.id = '1'844 self.status = 1845 self.area = 1 # default is area of bus self.i, but this is not available yet846 self.zone = 1847 self.pl = 0.0848 self.ql = 0.0849 self.ip = 0.0850 self.iq = 0.0851 self.yp = 0.0852 self.yq = 0.0853 self.owner = 1854 self.scale = 1855 if version == 33:856 pass857 #self.intrpt = 0858 if version == 34:859 #self.intrpt = 0860 self.dgenp = 0.0861 self.dgenq = 0.0862 self.dgenm = 0863 864 865 def clean_rev(self):866 '''remove spaces and non-allowed characters867 hope that this does not introduce duplication'''868 self.rev = clean_short_str(self.id)869 def read_from_row(self, row, rev):870 #row = pad_row(row, 13)871 self.i = parse_token(row[0], int, default=None)872 self.id = parse_token(row[1], str, default='1').strip()873 self.status = parse_token(row[2], int, default=1)874 self.pl = parse_token(row[5], float, default=0.0)875 self.ql = parse_token(row[6], float, default=0.0)876 877 self.area = parse_token(row[3], int, 1)878 self.zone = parse_token(row[4], int, 1)879 self.ip = parse_token(row[7], float, 0.0)880 self.iq = parse_token(row[8], float, 0.0)881 self.yp = parse_token(row[9], float, 0.0)882 self.yq = parse_token(row[10], float, 0.0)883 self.owner = parse_token(row[11], int, 1)884 if rev != 30:885 self.scale = parse_token(row[12], int, 1)886 if rev == 33:887 if len(row) == 12:888 self.intrp = parse_token(row[13], int, 0)889 if rev==34:890 pass891 '''#self.intrpt = parse_token(row[13], int, 0)892 self.dgenp = parse_token(row[14], float, 0.0)893 self.dgenq = parse_token(row[15], float, 0.0)894 self.dgenm = parse_token(row[15], float, 0) '''895 896class FixedShunt:897 def __init__(self):898 self.i = None # no default allowed899 self.rev = '1'900 self.status = 1901 self.gl = 0.0902 self.bl = 0.0903 904 def check_id_len_1_or_2(self):905 if not(len(self.id) in [1, 2]):906 alert(907 {'data_type': 'FixedShunt',908 'error_message': 'fails id string len 1 or 2. Please ensure that the id field of every fixed shunt is a 1- or 2-character string with no blank characters',909 'diagnostics': {910 'i': self.i,911 'id': self.id}})912 def read_from_row(self, row, id):913 #row = pad_row(row, 5)914 self.i = parse_token(row[0], int, default=None)915 self.id = parse_token(row[1], str, default='1').strip()916 self.status = parse_token(row[2], int, default=1)917 self.gl = parse_token(row[3], float, default=0.0)918 self.bl = parse_token(row[4], float, default=0.0)919 920class Generator:921 def __init__(self):922 self.i = None # no default allowed923 self.id = '1'924 self.pg = 0.0925 self.qg = 0.0926 self.qt = 9999.0927 self.qb = -9999.0928 self.vs = 1.0929 self.ireg = 0930 self.mbase = 100.0 # need to take default value for this from larger Raw class931 self.zr = 0.0932 self.zx = 1.0933 self.rt = 0.0934 self.xt = 0.0935 self.gtap = 1.0936 self.stat = 1937 self.rmpct = 100.0938 self.pt = 9999.0939 self.pb = -9999.0940 self.o1 = 1941 self.f1 = 1.0942 self.o2 = 0943 self.f2 = 1.0944 self.o3 = 0945 self.f3 = 1.0946 self.o4 = 0947 self.f4 = 1.0948 self.wmod = 0949 self.wpf = 1.0950 def read_from_row(self, row, rev):951 if len(row) < 16:952 print('error: data missing from generator')953 else:954 if rev != 30 and len(row)>26:955 self.wmod = parse_token(row[-2], int, 0, True)956 self.wpf = parse_token(row[-1], float, 0)957 if len(row)<28:958 diff = 28-len(row)959 row += ['']*diff960 #row = pad_row(row, 28)961 self.i = parse_token(row[0], int, default=None)962 self.id = parse_token(row[1], str, default='1').strip()963 self.pg = parse_token(row[2], float, default=0.0)964 self.qg = parse_token(row[3], float, default=0.0)965 self.qt = parse_token(row[4], float, default=9999.0)966 self.qb = parse_token(row[5], float, default=-9999.0)967 self.stat = parse_token(row[14], int, default=1)968 self.pt = parse_token(row[16], float, default=9999.0)969 self.pb = parse_token(row[17], float, default=-9999.0)970 971 self.vs = parse_token(row[6], float, 1.0)972 self.ireg = parse_token(row[7], int, 0)973 self.mbase = parse_token(row[8], float, 100.0)974 self.zr = parse_token(row[9], float, 0.0)975 self.zx = parse_token(row[10], float, 1.0)976 self.rt = parse_token(row[11], float, 0.0)977 self.xt = parse_token(row[12], float, 0.0)978 self.gtap = parse_token(row[13], float, 1.0)979 self.rmpct = parse_token(row[15], float, 100.0)980 self.o1 = parse_token(row[18], int, 1)981 self.f1 = parse_token(row[19], float, 1.0, True)982 self.o2 = parse_token(row[20], int, 0)983 self.f2 = parse_token(row[21], float, 1.0, True)984 self.o3 = parse_token(row[22], int, 0)985 self.f3 = parse_token(row[23], float, 1.0, True)986 self.o4 = parse_token(row[24], int, 0)987 self.f4 = parse_token(row[25], float, 1.0, True)988class NontransformerBranch:989 def __init__(self, version = 32):990 self.i = None # no default991 self.j = None # no default992 self.ckt = '1'993 self.r = None # no default994 self.x = None # no default995 self.b = 0.0996 self.ratea = 0.0997 self.rateb = 0.0998 self.ratec = 0.0999 self.gi = 0.01000 self.bi = 0.01001 self.gj = 0.01002 self.bj = 0.01003 self.st = 11004 self.met = 11005 self.len = 0.01006 self.o1 = 11007 self.f1 = 1.01008 self.o2 = 01009 self.f2 = 1.01010 self.o3 = 01011 self.f3 = 1.01012 self.o4 = 01013 self.f4 = 1.01014 if version == 34:1015 self.name = ' '*121016 1017 def read_from_row(self, row, rev):1018 if rev == 34:1019 if len(row)< 34:1020 diff = 34-len(row)1021 row += ['']*diff1022 self.i = parse_token(row[0], int, default=None)1023 self.j = parse_token(row[1], int, default=None)1024 self.ckt = parse_token(row[2], str, default='1').strip()1025 self.r = parse_token(row[3], float, default=None)1026 self.x = parse_token(row[4], float, default=None)1027 self.b = parse_token(row[5], float, default=0.0)1028 self.name = parse_token(row[6], str, ' '*12)1029 self.ratea = parse_token(row[7], float, default=0.0)1030 self.rateb = parse_token(row[8], float, 0.0)1031 self.ratec = parse_token(row[9], float, default=0.0)1032 self.st = parse_token(row[23], int, default=None)1033 1034 1035 self.gi = parse_token(row[19], float, 0.0)1036 self.bi = parse_token(row[20], float, 0.0)1037 self.gj = parse_token(row[21], float, 0.0)1038 self.bj = parse_token(row[22], float, 0.0)1039 self.met = parse_token(row[24], int, 1)1040 self.len = parse_token(row[25], float, 0.0)1041 self.o1 = parse_token(row[26], float, 1)1042 self.f1 = parse_token(row[27], float, 1.0)1043 self.o2 = parse_token(row[28], float, 0)1044 self.f2 = parse_token(row[29], float, 1.0)1045 self.o3 = parse_token(row[30], float, 0)1046 self.f3 = parse_token(row[31], float, 1.0)1047 self.o4 = parse_token(row[32], float, 0)1048 self.f4 = parse_token(row[33], float, 1.0)1049 else:1050 if len(row)< 24:1051 diff = 24-len(row)1052 row += ['']*diff1053 #row = pad_row(row, 24)1054 self.i = parse_token(row[0], int, default=None)1055 self.j = parse_token(row[1], int, default=None)1056 self.ckt = parse_token(row[2], str, default='1').strip()1057 self.r = parse_token(row[3], float, default=None)1058 self.x = parse_token(row[4], float, default=None)1059 self.b = parse_token(row[5], float, default=0.0)1060 self.ratea = parse_token(row[6], float, default=0.0)1061 self.rateb = parse_token(row[7], float, 0.0)1062 self.ratec = parse_token(row[8], float, default=0.0)1063 self.st = parse_token(row[13], int, default=None)1064 1065 1066 self.gi = parse_token(row[9], float, 0.0)1067 self.bi = parse_token(row[10], float, 0.0)1068 self.gj = parse_token(row[11], float, 0.0)1069 self.bj = parse_token(row[12], float, 0.0)1070 self.met = parse_token(row[14], int, 1)1071 self.len = parse_token(row[15], float, 0.0)1072 self.o1 = parse_token(row[16], float, 1)1073 self.f1 = parse_token(row[17], float, 1.0)1074 self.o2 = parse_token(row[18], float, 0)1075 self.f2 = parse_token(row[19], float, 1.0)1076 self.o3 = parse_token(row[20], float, 0)1077 self.f3 = parse_token(row[21], float, 1.0)1078 self.o4 = parse_token(row[22], float, 0)1079 self.f4 = parse_token(row[23], float, 1.0)1080 1081 1082#two winding transformer1083class Two_xfmr:1084 1085 def __init__(self, version):1086 self.i = None # no default1087 self.j = None # no default1088 self.k = 01089 self.ckt = '1'1090 self.cw = 11091 self.cz = 11092 self.cm = 11093 self.mag1 = 0.01094 self.mag2 = 0.01095 self.nmetr = 21096 self.name = 12*' '1097 self.stat = 11098 self.o1 = 11099 self.f1 = 1.01100 self.o2 = 01101 self.f2 = 1.01102 self.o3 = 01103 self.f3 = 1.01104 self.o4 = 01105 self.f4 = 1.01106 1107 self.r1_2 = 0.01108 self.x1_2 = None # no default allowed1109 self.sbase1_2 = 100.01110 self.windv1 = 1.01111 self.nomv1 = 0.01112 self.ang1 = 0.01113 self.rata1 = 0.01114 self.ratb1 = 0.01115 self.ratc1 = 0.01116 self.cod1 = 01117 self.cont1 = 01118 self.rma1 = 1.11119 self.rmi1 = 0.91120 self.vma1 = 1.11121 self.vmi1 = 0.91122 self.ntp1 = 331123 self.tab1 = 01124 self.cr1 = 0.01125 self.cx1 = 0.01126 self.windv2 = 1.01127 self.nomv2 = 0.01128 1129 if version in (33, 34):1130 self.vecgrp = ' '*121131 self.cnxa1 = 0.01132 1133 def read_from_row(self, row, row2, row3, row4, version):1134 #two winding xfmrs have 4 rows of data 1135 #row 11136 1137 self.i = parse_token(row[0], str)1138 self.j = parse_token(row[1], str)1139 self.k = 01140 self.ckt = parse_token(row[3], str)1141 self.cw = parse_token(row[4], int, 1)1142 self.cz = parse_token(row[5], int, 1)1143 self.cm = parse_token(row[6], int, 1)1144 self.mag1 = parse_token(row[7], float, 0.0)1145 self.mag2 = parse_token(row[8], float, 0.0)1146 self.nmetr = parse_token(row[9], int, 2)1147 self.name = parse_token(row[10], str, 12*' ')1148 self.stat = parse_token(row[11], int, 1)1149 self.o1 = parse_token(row[12], int, 1)1150 self.f1 = parse_token(row[13], float, 1.0)1151 if (len(row) > 15):1152 self.o2 = parse_token(row[14], int, 0)1153 self.f2 = parse_token(row[15], float, 1.0)1154 if (len(row) > 17):1155 self.o3 = parse_token(row[16], int, 0)1156 self.f3 = parse_token(row[17], float, 1.0)1157 if (len(row) > 19):1158 self.o4 = parse_token(row[18], int, 0)1159 self.f4 = parse_token(row[19], float, 1.0)1160 if version in (33, 34):1161 self.vecgrp = parse_token(row[20], str, ' '*12)1162 1163 #row21164 self.r1_2 = parse_token(row2[0], float, 0.0)1165 self.x1_2 = parse_token(row2[1], float )1166 self.sbase1_2 = parse_token(row2[2], float, 100.0)1167 1168 #row3 1169 self.windv1 = parse_token(row3[0], float, 1.0)1170 self.nomv1 = parse_token(row3[1], float, 0.0)1171 self.ang1 = parse_token(row3[2], float, 0.0)1172 self.rata1 = parse_token(row3[3], float, 0.0)1173 self.ratb1 = parse_token(row3[4], float, 0.0)1174 self.ratc1 = parse_token(row3[5], float, 0.0)1175 self.cod1 = parse_token(row3[6], float, 0)1176 self.cont1 = parse_token(row3[7], float, 0)1177 self.rma1 = parse_token(row3[8], float, 1.1)1178 self.rmi1 = parse_token(row3[9], float, 0.9)1179 self.vma1 = parse_token(row3[10], float, 1.1)1180 self.vmi1 = parse_token(row3[11], float, 0.9)1181 self.ntp1 = parse_token(row3[12], float, 33 )1182 self.tab1 = parse_token(row3[13], float, 0)1183 self.cr1 = parse_token(row3[14], float, 0.0)1184 self.cx1 = parse_token(row3[15], float, 0.0)1185 if version in (33, 34):1186 self.cnxa1 = parse_token(row3[16], float, 0.0)1187 1188 #row41189 self.windv2 = parse_token(row4[0], float, 1.0)1190 self.nomv2 = parse_token(row4[1], float, 0.0)1191class Three_xfmr:1192 def __init__(self, version = 32):1193 self.i = None # no default1194 self.j = None # no default1195 self.k = 01196 self.ckt = '1'1197 self.cw = 11198 self.cz = 11199 self.cm = 11200 self.mag1 = 0.01201 self.mag2 = 0.01202 self.nmetr = 21203 self.name = 12*' '1204 self.stat = 11205 self.o1 = 11206 self.f1 = 1.01207 self.o2 = 01208 self.f2 = 1.01209 self.o3 = 01210 self.f3 = 1.01211 self.o4 = 01212 self.f4 = 1.01213 1214 self.r1_2 = 0.01215 self.x1_2 = 0.01216 self.sbase1_2 = 0.01217 self.r2_3 = 0.01218 self.x2_3 = 0.01219 self.sbase2_3 = 0.01220 self.r3_1 = 0.01221 self.x3_1 = 0.01222 self.sbase3_1 = 0.01223 self.vmstar = 1.01224 self.anstar = 0.01225 1226 self.windv1 = 1.01227 self.nomv1 = 0.01228 self.ang1 = 0.01229 self.rata1, self.ratb1, self.ratv1 = 0.0, 0.0, 0.01230 self.cod1 = 01231 self.cont1 = 01232 self.rma1 = 1.11233 self.rmi1 = 0.91234 self.vma1 = 1.11235 self.vmi1 = 0.91236 self.ntp1 = 331237 self.tab1 = 01238 self.cr1 = 0.01239 self.cx1 = 0.01240 1241 self.windv2 = 1.01242 self.nomv2 = 0.01243 self.ang2 = 0.01244 self.rata2, self.ratb2, self.ratv2 = 0.0, 0.0, 0.01245 self.cod2 = 01246 self.cont2 = 01247 self.rma2 = None1248 self.rmi2 = None1249 self.vma2 = None1250 self.vmi2 = None1251 self.ntp2 = None1252 self.tab2 = None1253 self.cr2 = None1254 self.cx2 = None1255 1256 self.windv3 = 1.01257 self.nomv3 = 0.01258 self.ang3 = 0.01259 self.rata3, self.ratb3, self.ratv3 = 0.0, 0.0, 0.01260 self.cod3 = 01261 self.cont3 = 01262 self.rma3 = None1263 self.rmi3 = None1264 self.vma3 = None1265 self.vmi3 = None1266 self.ntp3 = 331267 self.tab3 = 01268 self.cr3 = 0.01269 self.cx3 = 0.01270 1271 if version in(33, 34):1272 self.vecgrp = ' '*121273 self.cnxa1 = 0.01274 self.cnxa2 = 0.01275 self.cnxa3 = 0.01276 1277 def read_from_row(self, row, row2, row3, row4, row5, version):1278 #row 11279 self.i = parse_token(row[0], int, '')1280 self.j = parse_token(row[1], int, '')1281 self.k = parse_token(row[2], int, 0)1282 self.ckt = parse_token(row[3], str, '1')1283 self.cw = parse_token(row[4], int, 1)1284 self.cz = parse_token(row[5], int, 1)1285 self.cm = parse_token(row[6], int, 1)1286 self.mag1 = parse_token(row[7], float, 0.0)1287 self.mag2 = parse_token(row[8], float, 0.0)1288 self.nmetr = parse_token(row[9], int, 2)1289 self.name = parse_token(row[10], str, 12*' ')1290 self.stat = parse_token(row[11], int, 1)1291 self.o1 = parse_token(row[12], int, 0)1292 self.f1 = parse_token(row[13], float, 1.0)1293 if (len(row) > 15):1294 self.o2 = parse_token(row[14], int, 0)1295 self.f2 = parse_token(row[15], float, 1.0)1296 if (len(row) > 17):1297 self.o3 = parse_token(row[16], int, 0)1298 self.f3 = parse_token(row[17], float, 1.0)1299 if (len(row) > 19):1300 self.o4 = parse_token(row[18], int, 0)1301 self.f4 = parse_token(row[19], float, 1.0)1302 if version in (33, 34):1303 self.vecgrp = parse_token(row[20], str, ' '*12)1304 1305 #row21306 self.r1_2 = parse_token(row2[0], float, 0.0)1307 self.x1_2 = parse_token(row2[1], float, 0.0)1308 self.sbase1_2 = parse_token(row2[2], float, 0.0)1309 self.r2_3 = parse_token(row2[3], float, 0.0)1310 self.x2_3 = parse_token(row2[4], float, 0.0)1311 self.sbase2_3 = parse_token(row2[5], float, 0.0)1312 self.r3_1 = parse_token(row2[6], float, 0.0)1313 self.x3_1 = parse_token(row2[7], float, 0.0)1314 self.sbase3_1 = parse_token(row2[8], float, 0.0)1315 self.vmstar = parse_token(row2[9], float, 1.0)1316 self.anstar = parse_token(row2[10], float, 0.0)1317 1318 #row 31319 self.windv1 = parse_token(row3[0], float, 1.0)1320 self.nomv1 = parse_token(row3[1], float, 0.0)1321 self.ang1 = parse_token(row3[2], float, 0.0)1322 self.rata1 = parse_token(row3[3], float, 0.0)1323 self.ratb1 = parse_token(row3[4], float, 0.0)1324 self.ratc1 = parse_token(row3[5], float, 0.0)1325 self.cod1 = parse_token(row3[6], float, 0)1326 self.cont1 = parse_token(row3[7], float, 0)1327 self.rma1 = parse_token(row3[8], float, 1.1)1328 self.rmi1 = parse_token(row3[9], float, 0.9)1329 self.vma1 = parse_token(row3[10], float, 1.1)1330 self.vmi1 = parse_token(row3[11], float, 0.9)1331 self.ntp1 = parse_token(row3[12], float, 33)1332 self.tab1 = parse_token(row3[13], float, 0)1333 self.cr1 = parse_token(row3[14], float, 0.0)1334 self.cx1 = parse_token(row3[15], float, 0.0)1335 if version in (33, 34):1336 self.cnxa1 = parse_token(row3[16], float, 0.0)1337 1338 #row 41339 self.windv2 = parse_token(row4[0], float, 1.0)1340 self.nomv2 = parse_token(row4[1], float, 0.0)1341 self.ang2 = parse_token(row4[2], float, 0.0)1342 self.rata2 = parse_token(row4[3], float, 0.0)1343 self.ratb2 = parse_token(row4[4], float, 0.0)1344 self.ratc2 = parse_token(row4[5], float, 0.0)1345 self.cod2 = parse_token(row4[6], float, 0)1346 self.cont2 = parse_token(row4[7], float, 0)1347 self.rma2 = parse_token(row4[8], float, 1.1)1348 self.rmi2 = parse_token(row4[9], float, 0.9)1349 self.vma2 = parse_token(row4[10], float, 1.1)1350 self.vmi2 = parse_token(row4[11], float, 0.9)1351 self.ntp2 = parse_token(row4[12], float, 33)1352 self.tab2 = parse_token(row4[13], float, 0)1353 self.cr2 = parse_token(row4[14], float, 0.0)1354 self.cx2 = parse_token(row4[15], float, 0.0)1355 if version in (33, 34):1356 self.cnxa2 = parse_token(row4[16], float, 0.0)1357 1358 #row 51359 self.windv3 = parse_token(row5[0], float, 1.0)1360 self.nomv3 = parse_token(row5[1], float, 0.0)1361 self.ang3 = parse_token(row5[2], float, 0.0)1362 self.rata3 = parse_token(row5[3], float, 0.0)1363 self.ratb3 = parse_token(row5[4], float, 0.0)1364 self.ratc3 = parse_token(row5[5], float, 0.0)1365 self.cod3 = parse_token(row5[6], float, 0)1366 self.cont3 = parse_token(row5[7], float, 0)1367 self.rma3 = parse_token(row5[8], float, 1.1)1368 self.rmi3 = parse_token(row5[9], float, 0.9)1369 self.vma3 = parse_token(row5[10], float, 1.1)1370 self.vmi3 = parse_token(row5[11], float, 0.9)1371 self.ntp3 = parse_token(row5[12], float, 33)1372 self.tab3 = parse_token(row5[13], float, 0)1373 if len(row) > 14:1374 self.cr3 = parse_token(row[14], float, 0.0)1375 if len(row) > 15:1376 self.cx3 = parse_token(row[15], float, 0.0)1377 if version in (33, 34):1378 self.cnxa3 = parse_token(row5[16], float, 0.0)1379 1380class Area:1381 def __init__(self):1382 self.i = None # no default1383 self.isw = 01384 self.pdes = 0.01385 self.ptol = 10.01386 self.arname = 12*' '1387 def clean_arname(self):1388 self.arname = ''1389 def check(self):1390 self.check_i_pos()1391 def read_from_row(self, row, version):1392 #row = pad_row(row, 5)1393 self.i = parse_token(row[0], int, default=None)1394 self.isw = parse_token(row[1], int, 0)1395 self.pdes = parse_token(row[2], float, 0.0)1396 self.ptol = parse_token(row[3], float, 10.0)1397 self.arname = parse_token(row[4], str, 12*' ')1398class Zone:1399 def __init__(self):1400 self.i = None # no default1401 self.zoname = 12*' '1402 def clean_zoname(self):1403 self.zoname = ''1404 1405 def read_from_row(self, row, version):1406 #row = pad_row(row, 2)1407 self.i = parse_token(row[0], int, default=None)1408 1409 self.zoname = parse_token(row[1], str, 12*' ')1410class SwitchedShunt:1411 def __init__(self):1412 1413 self.i = None # no default1414 self.modsw = 11415 self.adjm = 01416 self.stat = 11417 self.vswhi = 1.01418 self.vswlo = 1.01419 self.swrem = 01420 self.rmpct = 100.01421 self.rmidnt = 12*' '1422 self.binit = 0.01423 self.n1 = 01424 self.b1 = 0.01425 self.n2 = 01426 self.b2 = 0.01427 self.n3 = 01428 self.b3 = 0.01429 self.n4 = 01430 self.b4 = 0.01431 self.n5 = 01432 self.b5 = 0.01433 self.n6 = 01434 self.b6 = 0.01435 self.n7 = 01436 self.b7 = 0.01437 self.n8 = 01438 self.b8 = 0.01439 1440 def read_from_row(self, row, version):1441 if version in (32, 33, 34):1442 row = make_full_row(row, 26) #to account for varying numbers of n/b1443 #row = pad_row(row, 26)1444 self.i = parse_token(row[0], int, default=None)1445 self.stat = parse_token(row[3], int, default=1)1446 self.binit = parse_token(row[9], float, default=0.0)1447 self.n1 = parse_token(row[10], int, default=0)1448 self.b1 = parse_token(row[11], float, default=0.0)1449 self.n2 = parse_token(row[12], int, default=0)1450 self.b2 = parse_token(row[13], float, default=0.0)1451 self.n3 = parse_token(row[14], int, default=0)1452 self.b3 = parse_token(row[15], float, default=0.0)1453 self.n4 = parse_token(row[16], int, default=0)1454 self.b4 = parse_token(row[17], float, default=0.0)1455 self.n5 = parse_token(row[18], int, default=0)1456 self.b5 = parse_token(row[19], float, default=0.0)1457 self.n6 = parse_token(row[20], int, default=0)1458 self.b6 = parse_token(row[21], float, default=0.0)1459 self.n7 = parse_token(row[22], int, default=0)1460 self.b7 = parse_token(row[23], float, default=0.0)1461 self.n8 = parse_token(row[24], int, default=0)1462 self.b8 = parse_token(row[25], float, default=0.0)1463 1464 1465 self.modsw = parse_token(row[1], int, 1)1466 self.adjm = parse_token(row[2], int, 0)1467 self.vswhi = parse_token(row[4], float, 1.0)1468 self.vswlo = parse_token(row[5], float, 1.0)1469 self.swrem = parse_token(row[6], int, 0)1470 self.rmpct = parse_token(row[7], float, 100.0)1471 self.rmidnt = parse_token(row[8], str, 12*' ')1472 elif version == 30:1473 row = make_full_row(row, 24)1474 self.i = parse_token(row[0], int, default=None)1475 self.modsw = parse_token(row[1], int, 1)1476 self.vswhi = parse_token(row[2], float, 1.0)1477 self.vswlo = parse_token(row[3], float, 1.0)1478 self.swrem = parse_token(row[4], int, 0)1479 self.rmpct = parse_token(row[5], float, 100.0)1480 self.rmidnt = parse_token(row[6], str, 12*' ')1481 self.binit = parse_token(row[7], float, default=0.0)1482 self.n1 = parse_token(row[8], int, default=0)1483 self.b1 = parse_token(row[9], float, default=0.0)1484 self.n2 = parse_token(row[10], int, default=0)1485 self.b2 = parse_token(row[11], float, default=0.0)1486 self.n3 = parse_token(row[12], int, default=0)1487 self.b3 = parse_token(row[13], float, default=0.0)1488 self.n4 = parse_token(row[14], int, default=0)1489 self.b4 = parse_token(row[15], float, default=0.0)1490 self.n5 = parse_token(row[16], int, default=0)1491 self.b5 = parse_token(row[17], float, default=0.0)1492 self.n6 = parse_token(row[18], int, default=0)1493 self.b6 = parse_token(row[19], float, default=0.0)1494 self.n7 = parse_token(row[20], int, default=0)1495 self.b7 = parse_token(row[21], float, default=0.0)1496 self.n8 = parse_token(row[22], int, default=0)1497 self.b8 = parse_token(row[23], float, default=0.0)1498class Two_terminal_dc:1499 def __init__(self):1500 #first line1501 self.name = None #no default allowed1502 self.mdc = 01503 self.rdc = None #no default allowed1504 self.setvl = None #no default allowed1505 self.vschd = None #no default allowed1506 self.vcmod = 0.01507 self.rcomp = 0.01508 self.delti = 0.01509 self.meter = 'I'1510 self.dcvmin = 0.01511 self.cccitmx = 201512 self.cccacc = 1.01513 #second line1514 self.ipr = None #no default allowed1515 self.nbr = None #no default allowed1516 self.anmxr = None #no default allowed1517 self.anmnr = None #no default allowed1518 self.rcr = None #no default allowed1519 self.xcr = None #no default allowed1520 self.ebasr = None #no default allowed1521 self.trr = 1.01522 self.tapr = 1.01523 self.tmxr = 1.51524 self.tmnr = 0.511525 self.stpr = 0.006251526 self.icr = 01527 self.ifr = 01528 self.itr = 01529 self.idr = '1'1530 self.xcapr = 0.01531 #third line1532 self.ipi = None1533 self.nbi = None1534 self.anmxi = None1535 self.anmni = None1536 self.rci = None1537 self.xci = None1538 self.ebasi = None1539 self.tri = None1540 self.tapi = None1541 self.tmxi = None1542 self.tmni = None1543 self.stpi = None1544 self.ici = None1545 self.ifi = None1546 self.iti = None1547 self.idi = None1548 self.xcapi = None1549 1550 def read_from_row(self, row1, row2, row3, version):1551 #first line1552 self.name = parse_token(row1[0], str)1553 self.mdc = parse_token(row1[1], int, 0)1554 self.rdc = parse_token(row1[2], float)1555 self.setvl = parse_token(row1[3], float)1556 self.vschd = parse_token(row1[4], float)1557 self.vcmod = parse_token(row1[5], float, 0.0)1558 self.rcomp = parse_token(row1[6], float, 0.0)1559 self.delti = parse_token(row1[7], float, 0.0)1560 self.meter = parse_token(row1[8], str, 'I')1561 self.dcvmin = parse_token(row1[9], float, 0.0)1562 self.cccitmx = parse_token(row1[10], int, 20)1563 self.cccacc = parse_token(row1[11], float, 1.0)1564 #start second line1565 1566 self.ipr = parse_token(row2[0], int)1567 self.nbr = parse_token(row2[1], int)1568 self.anmxr = parse_token(row2[2], float)1569 self.anmnr = parse_token(row2[3], float)1570 self.rcr = parse_token(row2[4], float)1571 self.xcr = parse_token(row2[5], float)1572 self.ebasr = parse_token(row2[6], float)1573 self.trr = parse_token(row2[7], float, 1.0)1574 self.tapr = parse_token(row2[8], float, 1.0)1575 self.tmxr = parse_token(row2[9], float, 1.5)1576 self.tmnr = parse_token(row2[10], float, 0.51)1577 self.stpr = parse_token(row2[11], float, 0.00625)1578 self.icr = parse_token(row2[12], str, '0')1579 self.ifr = parse_token(row2[13], int, 0)1580 self.itr = parse_token(row2[14], int, 0)1581 self.idr = parse_token(row2[15], str, '1')1582 self.xcapr =parse_token(row2[16], float, 0.0)1583 #start third line (inverter qualities)1584 self.ipi = parse_token(row3[0], int)1585 self.nbi = parse_token(row3[1], int)1586 self.anmxi = parse_token(row3[2], float)1587 self.anmni = parse_token(row3[3], float)1588 self.rci = parse_token(row3[4], float)1589 self.xci = parse_token(row3[5], float)1590 self.ebasi = parse_token(row3[6], float)1591 self.tri = parse_token(row3[7], float)1592 self.tapi = parse_token(row3[8], float)1593 self.tmxi = parse_token(row3[9], float)1594 self.tmni = parse_token(row3[10], float)1595 self.stpi = parse_token(row3[11], float)1596 self.ici = parse_token(row3[12], float)1597 self.ifi = parse_token(row3[13], int)1598 self.iti = parse_token(row3[14], int)1599 self.idi = parse_token(row3[15], str)1600 self.xcapi = parse_token(row3[16], float)1601 1602class VSC:1603 1604 def __init__(self):1605 #line one1606 self.name = None1607 self.mdc = 11608 self.rdc = None1609 self.o1 = 11610 self.o2, self.o3, self.o4 = 0, 0 , 01611 self.f1, self.f2, self.f3, self.f4, = 1, 1, 1, 11612 #line two1613 self.ibus = None1614 self.type = None1615 self.mode = 11616 self.dcset = None1617 self.acset = 1.01618 self.aLoss, self.bLoss = 0.0, 0.01619 self.minLoss = 0.01620 self.smax = 0.01621 self.imax = 0.01622 self.pwf = 1.01623 self.maxq, self.minq = 9999.0, -9999.01624 self.remot = '0'1625 self.rmpct = 100.01626 #line three1627 self.ibus2 = None1628 self.type2 = None1629 self.mode2 = 11630 self.dcset2 = None1631 self.acset2 = 1.01632 self.aLoss2, self.bLoss2 = 0.0, 0.01633 self.minLoss2 = 0.01634 self.smax2 = 0.01635 self.imax2 = 0.01636 self.pwf2 = 1.01637 self.maxq2, self.minq2 = 9999.0, -9999.01638 self.remot2 = '0'1639 self.rmpct2 = 100.01640 def read_from_row(self, row1, row2, row3, version):1641 if len(row1) <5:1642 print("missing data from VSC line")1643 if len(row1) < 11:1644 diff = 11-len(row1)1645 '''eachDiff = diff/21646 row1 = row1[:]+['']*eachDiff+row1[:]*eachDiff'''1647 row1 += ['']*diff1648 #line one1649 self.name = parse_token(row1[0], str)1650 self.mdc = parse_token(row1[1], int, 1)1651 self.rdc = parse_token(row1[2], float)1652 self.o1 = parse_token(row1[3], float, 1)1653 self.o2 = parse_token(row1[4], float, 0)1654 self.o3 = parse_token(row1[5], float, 0)1655 self.o4 = parse_token(row1[6], float, 0)1656 self.f1 = parse_token(row1[7], float, 1.0)1657 self.f2 = parse_token(row1[8], float, 1.0)1658 self.f3 = parse_token(row1[9], float, 1.0)1659 self.f4 = parse_token(row1[10], float, 1.0)1660 #line two1661 self.ibus = parse_token(row2[0], str)1662 self.type = parse_token(row2[1], int)1663 self.mode = parse_token(row2[2], int, 1)1664 self.dcset =parse_token(row2[3], float)1665 self.acset = parse_token(row2[4], float, 1.0)1666 self.aLoss = parse_token(row2[5], float, 0.0)1667 self.bLoss = parse_token(row2[6], float, 0.0)1668 self.minLoss = parse_token(row2[7], float, 0.0)1669 self.smax = parse_token(row2[8], float, 0.0)1670 self.imax = parse_token(row2[9], float, 0.0)1671 self.pwf = parse_token(row2[10], float, 1.0)1672 self.maxq = parse_token(row2[11], float, 9999.0)1673 self.minq = parse_token(row2[12],float, -9999.0)1674 self.remot = parse_token(row2[13], str, '0')1675 self.rmpct = parse_token(row2[14], float, 100.0)1676 #line three1677 self.ibus2 = parse_token(row3[0], str)1678 self.type2 = parse_token(row3[1], int)1679 self.mode2 = parse_token(row3[2], int, 1)1680 self.dcset2 =parse_token(row3[3], float)1681 self.acset2 = parse_token(row3[4], float, 1.0)1682 self.aLoss2 = parse_token(row3[5], float, 0.0)1683 self.bLoss2 = parse_token(row3[6], float, 0.0)1684 self.minLoss2 = parse_token(row3[7], float, 0.0)1685 self.smax2 = parse_token(row3[8], float, 0.0)1686 self.imax2 = parse_token(row3[9], float, 0.0)1687 self.pwf2 = parse_token(row3[10], float, 1.0)1688 self.maxq2 = parse_token(row3[11], float, 9999.0)1689 self.minq2 = parse_token(row3[12],float, -9999.0)1690 self.remot2 = parse_token(row3[13], str, '0')1691 self.rmpct2 = parse_token(row3[14], float, 100.0)1692 1693 1694 1695class Impedance_corr:1696 1697 def __init__(self):1698 self.i = None #no deafult allowed1699 self.t1, self.t2, self.t3 = 0.0, 0.0, 0.01700 self.t4, self.t5, self.t6 = 0.0, 0.0, 0.01701 self.t7, self.t8, self.t9 = 0.0, 0.0, 0.01702 self.t10, self.t11 = 0.0, 0.01703 self.f1, self.f2, self.f3 = 0.0, 0.0, 0.01704 self.f4, self.f5, self.f6 = 0.0, 0.0, 0.01705 self.f7, self.f8, self.f9 = 0.0, 0.0, 0.01706 self.f10, self.f11 = 0.0, 0.01707 1708 def read_from_row(self, row, version):1709 if version == 34:1710 pass1711 else:1712 row = make_full_row(row, 23)1713 self.i = parse_token(row[0], int)1714 self.t1 = parse_token(row[1], float, 0.0)1715 self.f1 = parse_token(row[2], float, 0.0)1716 self.t2 = parse_token(row[3], float, 0.0)1717 self.f2 = parse_token(row[4], float, 0.0)1718 self.t3 = parse_token(row[5], float, 0.0)1719 self.f3 = parse_token(row[6], float, 0.0)1720 self.t4 = parse_token(row[7], float, 0.0)1721 self.f4 = parse_token(row[8], float, 0.0)1722 self.t5 = parse_token(row[9], float, 0.0)1723 self.f5 = parse_token(row[10], float, 0.0)1724 self.t6 = parse_token(row[11], float, 0.0)1725 self.f6 = parse_token(row[12], float, 0.0)1726 self.t7 = parse_token(row[13], float, 0.0)1727 self.f7 = parse_token(row[14], float, 0.0)1728 self.t8 = parse_token(row[15], float, 0.0)1729 self.f8 = parse_token(row[16], float, 0.0)1730 self.t9 = parse_token(row[17], float, 0.0)1731 self.f9 = parse_token(row[18], float, 0.0)1732 self.t10 = parse_token(row[19], float, 0.0)1733 self.f10 = parse_token(row[20], float, 0.0)1734 self.t11 = parse_token(row[21], float, 0.0)1735 self.f11 = parse_token(row[22], float, 0.0)1736 1737 1738 1739class Multi_term_DC:1740 1741 def __init__(self):1742 pass1743 1744 1745class Owner:1746 1747 def __init__(self):1748 self.i = None1749 self.owname = 12*' '1750 1751 def read_from_row(self, row, version):1752 self.i = parse_token(row[0], int)1753 self.owname = parse_token(row[1], str, 12*' ')1754 1755 1756 1757 1758 1759 1760class Interarea_transfer:1761 def __init__(self):1762 self.arfrom = None1763 self.arto = None1764 self.trid = None1765 self.ptran = 0.01766 1767 def read_from_row(self, row, version):1768 self.arfrom = parse_token(row[0], int)1769 self.arto = parse_token(row[1], int)1770 self.trid = parse_token(row[2], str, '1')1771 self.ptran = parse_token(row[3],float, 0.0 )1772 1773 1774class FACTS_device:1775 def __init__(self):1776 self.name = None1777 self.i = None1778 self.j = 01779 self.mode = 11780 self.pdes = 0.01781 self.qdes = 0.01782 self.vset = 1.01783 self.shmx = 9999.01784 self.trmx = 9999.01785 self.vtmn = 0.91786 self.vtmx = 1.11787 self.vsmx = 1.01788 self.imx = 0.01789 self.linx = 0.051790 self.rmpct = 100.01791 self.owner = 11792 self.set1 = 0.01793 self.set2 = 0.01794 self.vsref = 01795 self.remot = 01796 self.mname = ''1797 1798 def read_from_row(self, row, version):1799 self.name = parse_token(row[0], str)1800 self.i = parse_token(row[1], int)1801 self.j = parse_token(row[2], int)1802 self.mode = parse_token(row[3], int)1803 self.pdes = parse_token(row[4], float, 0.0)1804 self.qdes = parse_token(row[5], float, 0.0)1805 self.vset = parse_token(row[6], float, 1.0)1806 self.shmx = parse_token(row[7], float, 9999.0)1807 self.trmx = parse_token(row[8], float, 9999.0)1808 self.vtmn = parse_token(row[9], float,0.9)1809 self.vtmx = parse_token(row[10], float,1.1)1810 self.vsmx = parse_token(row[11], float, 1.0)1811 self.imx = parse_token(row[12], float, 0.0)1812 self.linx = parse_token(row[13], float, 0.05)1813 self.rmpct = parse_token(row[14], float, 100.0)1814 self.owner = parse_token(row[15], int, 1)1815 self.set1 = parse_token(row[16], float,0.0)1816 self.set2 = parse_token(row[17], float, 0.0)1817 self.vsref = parse_token(row[18], int, 0)1818 if version != 30:1819 self.remot = parse_token(row[19], int, 0)1820 self.mname = parse_token(row[20], str, '')1821 1822 1823class Multi_section_line_grouping:1824 1825 def __init__(self):1826 self.i = None #from bus number1827 self.j = None #to bus number1828 self.id = '&1'1829 self.met = 11830 self.dum1, self.dum2, self.dum3 = None, None, None1831 self.dum4, self.dum5, self.dum6 = None, None, None1832 self.dum7, self.dum8, self.dum9 = None, None, None1833 1834 def read_from_row(self,row, version):1835 if len(row)<5:1836 print('missing information form multi section line grouping')1837 elif len(row)<13:1838 diff = 13-len(row)1839 row += ['']*diff1840 1841 self.i = parse_token(row[0], int)1842 self.j = parse_token(row[1], int)1843 self.id = parse_token(row[2], str, '&1')1844 self.met = parse_token(row[3], int, 1)1845 self.dum1 = parse_token(row[4], int)1846 if len(row) > 5:1847 self.dum2 = parse_token(row[5], int, 0)1848 if len(row) > 6:1849 self.dum3 = parse_token(row[6], int, 0)1850 if len(row) > 7:1851 self.dum4 = parse_token(row[7], int, 0)1852 if len(row) > 8:1853 self.dum5 = parse_token(row[8], int, 0)1854 if len(row) > 9:1855 self.dum6 = parse_token(row[9], int, 0)1856 if len(row) > 10:1857 self.dum7 = parse_token(row[10], int, 0)1858 if len(row) > 11:1859 self.dum8 = parse_token(row[11], int, 0)1860 if len(row) > 12:1861 self.dum9 = parse_token(row[12], int, 0)1862 1863 1864class Sys_switching_dev_data: #only in version 341865 def __init__(self):1866 self.i = None1867 self.j = None1868 self.cktid = '1'1869 self.x = None1870 self.rate1 = 0.01871 self.rate2 = 0.01872 self.rate3 = 0.01873 self.rate4 = 0.01874 self.rate5 = 0.01875 self.rate6 = 0.01876 self.rate7 = 0.01877 self.rate8 = 0.01878 self.rate9 = 0.01879 self.rate10 = 0.01880 self.rate11 = 0.01881 self.rate12 = 0.01882 self.status = None1883 self.nstatus = None1884 self.meterd = None1885 self.stype = None1886 self.name = None 1887 1888 def read_from_row(self, row, version):1889 self.i = parse_token(row[0], int)1890 self.j = parse_token(row[0], int)1891 self.cktid = parse_token(row[0], str)1892 self.x = parse_token(row[0], int)1893 self.rate1 = parse_token(row[0], float)1894 self.rate2 = parse_token(row[0], float)1895 self.rate3 = parse_token(row[0], float)1896 self.rate4 = parse_token(row[0], float)1897 self.rate5 = parse_token(row[0], float)1898 self.rate6 = parse_token(row[0], float)1899 self.rate7 = parse_token(row[0], float)1900 self.rate8 = parse_token(row[0], float)1901 self.rate9 = parse_token(row[0], float)1902 self.rate10 = parse_token(row[0], float)1903 self.rate11 = parse_token(row[0], float)1904 self.rate12 = parse_token(row[0], float)1905 self.status = parse_token(row[0], int)1906 self.nstatus = parse_token(row[0], int)1907 self.meterd = parse_token(row[0], str)1908 self.stype = parse_token(row[0], int)1909 self.name = parse_token(row[0], str) 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 ...

Full Screen

Full Screen

psse.py

Source:psse.py Github

copy

Full Screen

...5import csv6import os7import sys8import math9def parse_token(token, val_type, default):10 val = None11 if len(token) > 0:12 val = val_type(token)13 else:14 val = val_type(default)15 return val16def pad_row(row, new_row_len):17 row_len = len(row)18 row_len_diff = new_row_len - row_len19 row_new = row20 if row_len_diff > 0:21 row_new = row + row_len_diff * ['']22 return row_new23def remove_end_of_line_comment(token, end_of_line_str):24 25 token_new = token26 index = token_new.find("/")27 if index > -1:28 token_new = token_new[0:index]29 return token_new30class Psse:31 '''In physical units, i.e. data convention, i.e. PSSE data files'''32 def __init__(self):33 self.raw = Raw()34 self.rop = Rop()35 self.inl = Inl()36 self.con = Con()37 38class Raw:39 '''In physical units, i.e. data convention, i.e. PSSE data files'''40 def __init__(self):41 self.case_identification = CaseIdentification()42 self.buses = {}43 self.loads = {}44 self.fixed_shunts = {}45 self.generators = {}46 self.nontransformer_branches = {}47 self.transformers = {}48 self.areas = {}49 self.switched_shunts = {}50 def set_areas_from_buses(self):51 52 area_i_set = set([b.area for b in self.buses.values()])53 def area_set_i(area, i):54 area.i = i55 return area56 self.areas = {i:area_set_i(Area(), i) for i in area_i_set}57 58 # TODO59 def write(self, file_name):60 '''write a RAW file'''61 62 def read(self, file_name):63 with open(file_name, 'r') as in_file:64 lines = in_file.readlines()65 delimiter_str = ","66 quote_str = "'"67 skip_initial_space = True68 rows = csv.reader(69 lines,70 delimiter=delimiter_str,71 quotechar=quote_str,72 skipinitialspace=skip_initial_space)73 rows = [[t.strip() for t in r] for r in rows]74 self.read_from_rows(rows)75 self.set_areas_from_buses()76 77 def row_is_file_end(self, row):78 is_file_end = False79 if len(row) == 0:80 is_file_end = True81 if row[0][:1] in {'','q','Q'}:82 is_file_end = True83 return is_file_end84 85 def row_is_section_end(self, row):86 is_section_end = False87 if row[0][:1] == '0':88 is_section_end = True89 return is_section_end90 91 def read_from_rows(self, rows):92 row_num = 093 row = rows[row_num]94 self.case_identification.read_from_row(row)95 row_num += 296 while True:97 row_num += 198 row = rows[row_num]99 if self.row_is_file_end(row):100 return101 if self.row_is_section_end(row):102 break103 bus = Bus()104 bus.read_from_row(row)105 self.buses[bus.i] = bus106 while True:107 row_num += 1108 row = rows[row_num]109 if self.row_is_file_end(row):110 return111 if self.row_is_section_end(row):112 break113 load = Load()114 load.read_from_row(row)115 self.loads[(load.i, load.id)] = load116 while True:117 row_num += 1118 row = rows[row_num]119 if self.row_is_file_end(row):120 return121 if self.row_is_section_end(row):122 break123 fixed_shunt = FixedShunt()124 fixed_shunt.read_from_row(row)125 self.fixed_shunts[(fixed_shunt.i, fixed_shunt.id)] = fixed_shunt126 while True:127 row_num += 1128 row = rows[row_num]129 if self.row_is_file_end(row):130 return131 if self.row_is_section_end(row):132 break133 generator = Generator()134 generator.read_from_row(row)135 self.generators[(generator.i, generator.id)] = generator136 while True:137 row_num += 1138 row = rows[row_num]139 if self.row_is_file_end(row):140 return141 if self.row_is_section_end(row):142 break143 nontransformer_branch = NontransformerBranch()144 nontransformer_branch.read_from_row(row)145 self.nontransformer_branches[(146 nontransformer_branch.i,147 nontransformer_branch.j,148 nontransformer_branch.ckt)] = nontransformer_branch149 while True:150 row_num += 1151 row = rows[row_num]152 if self.row_is_file_end(row):153 return154 if self.row_is_section_end(row):155 break156 transformer = Transformer()157 num_rows = transformer.get_num_rows_from_row(row)158 rows_temp = rows[159 row_num:(row_num + num_rows)]160 transformer.read_from_rows(rows_temp)161 self.transformers[(162 transformer.i,163 transformer.j,164 transformer.k,165 transformer.ckt)] = transformer166 row_num += (num_rows - 1)167 while True: # areas - for now just make a set of areas based on bus info168 row_num += 1169 row = rows[row_num]170 if self.row_is_file_end(row):171 return172 if self.row_is_section_end(row):173 break174 area = Area()175 area.read_from_row(row)176 self.areas[area.i] = area177 while True:178 row_num += 1179 row = rows[row_num]180 if self.row_is_file_end(row):181 return182 if self.row_is_section_end(row):183 break184 while True:185 row_num += 1186 row = rows[row_num]187 if self.row_is_file_end(row):188 return189 if self.row_is_section_end(row):190 break191 while True:192 row_num += 1193 row = rows[row_num]194 if self.row_is_file_end(row):195 return196 if self.row_is_section_end(row):197 break198 while True:199 row_num += 1200 row = rows[row_num]201 if self.row_is_file_end(row):202 return203 if self.row_is_section_end(row):204 break205 while True:206 row_num += 1207 row = rows[row_num]208 if self.row_is_file_end(row):209 return210 if self.row_is_section_end(row):211 break212 while True: # zone213 row_num += 1214 row = rows[row_num]215 if self.row_is_file_end(row):216 return217 if self.row_is_section_end(row):218 break219 while True:220 row_num += 1221 row = rows[row_num]222 if self.row_is_file_end(row):223 return224 if self.row_is_section_end(row):225 break226 while True:227 row_num += 1228 row = rows[row_num]229 if self.row_is_file_end(row):230 return231 if self.row_is_section_end(row):232 break233 while True:234 row_num += 1235 row = rows[row_num]236 if self.row_is_file_end(row):237 return238 if self.row_is_section_end(row):239 break240 while True:241 row_num += 1242 row = rows[row_num]243 if self.row_is_file_end(row):244 return245 if self.row_is_section_end(row):246 break247 switched_shunt = SwitchedShunt()248 switched_shunt.read_from_row(row)249 self.switched_shunts[switched_shunt.i] = switched_shunt250class Rop:251 '''In physical units, i.e. data convention, i.e. PSSE data files'''252 def __init__(self):253 #self.generator_dispatch_records = GeneratorDispatchRecord() # needs to be a dictionary254 self.generator_dispatch_records = {}255 self.active_power_dispatch_records = {}256 self.piecewise_linear_cost_functions = {}257 258 def trancostfuncfrom_phase_0(self,rawdata):259 ds=self.active_power_dispatch_records.get((4, '1'))260 for r in rawdata.generators.values():261 ds=self.active_power_dispatch_records.get((r.i,r.id))262 #update piecewise linear info263 self.active_power_dispatch_records.get((r.i,r.id)).npairs=10264 self.active_power_dispatch_records.get((r.i,r.id)).costzero=self.active_power_dispatch_records.get((r.i,r.id)).constc265 for i in range(self.active_power_dispatch_records.get((r.i,r.id)).npairs):266 # the points will be power followed by cost, ie[power0 cost0 power 1 cost1 ....]267 self.active_power_dispatch_records.get((r.i,r.id)).points.append(r.pb+i*(r.pt-r.pb)/(self.active_power_dispatch_records.get((r.i,r.id)).npairs-1))268 self.active_power_dispatch_records.get((r.i,r.id)).points.append(self.active_power_dispatch_records.get((r.i,r.id)).constc+self.active_power_dispatch_records.get((r.i,r.id)).linearc*(r.pb+i*(r.pt-r.pb)/10)+self.active_power_dispatch_records.get((r.i,r.id)).quadraticc*pow(r.pb+i*(r.pt-r.pb)/10,2))269 270 271 def read_from_phase_0(self, file_name):272 273 '''takes the generator.csv file as input'''274 with open(file_name, 'r') as in_file:275 lines = in_file.readlines()276 del lines[0]277 delimiter_str = ","278 quote_str = "'"279 skip_initial_space = True280 rows = csv.reader(281 lines,282 delimiter=delimiter_str,283 quotechar=quote_str,284 skipinitialspace=skip_initial_space)285 #[[t.strip() for t in r] for r in rows]286 for r in rows:287 indi=1288 gen_dispatch = QuadraticCostFunctions()289 gen_dispatch.read_from_csv(r)290 291 gen_dispatch.read_from_csv_quadraticinfo(r)292 while (indi<4):293 r2=rows.next()294 gen_dispatch.read_from_csv_quadraticinfo(r2)295 indi=indi+1296 print gen_dispatch.bus,gen_dispatch.genid, gen_dispatch.constc,gen_dispatch.linearc,gen_dispatch.quadraticc297 self.active_power_dispatch_records[gen_dispatch.bus,gen_dispatch.genid] = gen_dispatch298 #print gen_dispatch.bus,gen_dispatch.genid, gen_dispatch.constc299 #ds=self.active_power_dispatch_records.get((4, '1'))300 301 302 def write(self, file_name,rawdata):303 '''writes the ROP data to an ROP-formatted file'''304 file = open("C:\SyncDrive\PNNL_Project\ACOPF Competition\eval_phase1\\testfile.rop","w") 305 file.write(" 0 / PSS/E-30.0, MON, SEP 20 2004 17:32\n") 306 file.write(" 0 / End of Bus Voltage Constraint data, begin Adjustable Bus Shunt data\n") 307 file.write(" 0 / End of Adjustable Bus Shunt data, begin Bus Load data\n")308 file.write(" 0 / End of Bus Load data, begin Adjustable Bus Load Tables\n")309 file.write(" 0 / End of Adjustable Bus Load Tables, begin Generator Dispatch data\n")310 index=1311 for r in rawdata.generators.values():312 row=str(r.i)+"\t"+str(r.id)+"\t"+"1.000000"+"\t"+str(index)+"\n"313 #ds=self.active_power_dispatch_records.get((r.i,r.id))314 file.write(row)315 index=index+1316 317 file.write(" 0 / End of Generator Dispatch data, begin Active Power Dispatch Tables\n")318 index=1319 for r in rawdata.generators.values():320 row=str(index)+"\t"+str(r.pt)+"\t"+str(r.pb)+"\t"+"1.000000"+"\t"+"2"+"\t"+str(r.stat)+"\t"+str(index)+"\n"321 #ds=self.active_power_dispatch_records.get((r.i,r.id))322 file.write(row)323 index=index+1324 325 file.write(" 0 / End of Active Power Dispatch Tables, begin Generation Reserve data\n")326 file.write(" 0 / End of Generation Reserve data, begin Generation Reactive Capability data\n")327 file.write(" 0 / End of Generation Reactive Capability data, begin Adjustable Branch Reactance data\n")328 file.write(" 0 / End of Adjustable Branch Reactance data, begin Piece - wise Linear Cost Tables\n")329 index=1330 for r in rawdata.generators.values():331 row=str(index)+"\t"+"LINEAR "+str(index)+"\t"+str(self.active_power_dispatch_records.get((r.i,r.id)).npairs)+"\n"332 #ds=self.active_power_dispatch_records.get((r.i,r.id))333 file.write(row)334 for r2 in range(self.active_power_dispatch_records.get((r.i,r.id)).npairs):335 row=str(self.active_power_dispatch_records.get((r.i,r.id)).points[r2*2])+"\t"336 row=row+str(self.active_power_dispatch_records.get((r.i,r.id)).points[r2*2+1])+"\n"337 file.write(row)338 index=index+1339 file.write(" 0 / End of Piece-wise Linear Cost Tables, begin Piece-wise Quadratic Cost Tables\n")340 341 file.write(" 0 / End of Piece-wise Quadratic Cost Tables, begin Polynomial Cost Tables\n")342 343 file.write(" 0 / End of Polynomial Cost Tables, begin Period Reserve data\n")344 file.write(" 0 / End of Period Reserve data, begin Branch Flow Constraint data\n")345 file.write(" 0 / End of Branch Flow Constraint data, begin Interface Flow data\n")346 file.write(" 0 / End of Interface Flow data, begin Linear Constraint Equation Dependency data\n")347 file.write(" 0 / End of Linear Constraint Equation Dependency data, begin 2-terminal dc Line Constraint data\n")348 file.write(" 0 / End of 2-terminal dc Line Constraint data")349 file.close() 350 351 def read(self, file_name):352 with open(file_name, 'r') as in_file:353 lines = in_file.readlines()354 delimiter_str = ","355 quote_str = "'"356 skip_initial_space = True357 rows = csv.reader(358 lines,359 delimiter=delimiter_str,360 quotechar=quote_str,361 skipinitialspace=skip_initial_space)362 rows = [[t.strip() for t in r] for r in rows]363 self.read_from_rows(rows)364 365 def row_is_file_end(self, row):366 is_file_end = False367 if len(row) == 0:368 is_file_end = True369 if row[0][:1] in {'','q','Q'}:370 is_file_end = True371 return is_file_end372 373 def row_is_section_end(self, row):374 is_section_end = False375 if row[0][:1] == '0':376 is_section_end = True377 return is_section_end378 379 def read_from_rows(self, rows):380 row_num = -1381 while True:382 row_num += 1383 row = rows[row_num]384 if self.row_is_file_end(row):385 return386 if self.row_is_section_end(row):387 break388 while True:389 row_num += 1390 row = rows[row_num]391 if self.row_is_file_end(row):392 return393 if self.row_is_section_end(row):394 break395 while True:396 row_num += 1397 row = rows[row_num]398 if self.row_is_file_end(row):399 return400 if self.row_is_section_end(row):401 break402 while True:403 row_num += 1404 row = rows[row_num]405 if self.row_is_file_end(row):406 return407 if self.row_is_section_end(row):408 break409 while True:410 row_num += 1411 row = rows[row_num]412 if self.row_is_file_end(row):413 return414 if self.row_is_section_end(row):415 break416 while True:417 row_num += 1418 row = rows[row_num]419 if self.row_is_file_end(row):420 return421 if self.row_is_section_end(row):422 break423 generator_dispatch_record = GeneratorDispatchRecord()424 generator_dispatch_record.read_from_row(row)425 self.generator_dispatch_records[(426 generator_dispatch_record.bus,427 generator_dispatch_record.genid)] = generator_dispatch_record428 while True:429 row_num += 1430 row = rows[row_num]431 if self.row_is_file_end(row):432 return433 if self.row_is_section_end(row):434 break435 active_power_dispatch_record = ActivePowerDispatchRecord()436 active_power_dispatch_record.read_from_row(row)437 self.active_power_dispatch_records[438 active_power_dispatch_record.tbl] = (439 active_power_dispatch_record)440 while True:441 row_num += 1442 row = rows[row_num]443 if self.row_is_file_end(row):444 return445 if self.row_is_section_end(row):446 break447 while True:448 row_num += 1449 row = rows[row_num]450 if self.row_is_file_end(row):451 return452 if self.row_is_section_end(row):453 break454 while True:455 row_num += 1456 row = rows[row_num]457 if self.row_is_file_end(row):458 return459 if self.row_is_section_end(row):460 break461 while True:462 row_num += 1463 row = rows[row_num]464 if self.row_is_file_end(row):465 return466 if self.row_is_section_end(row):467 break468 #piecewise_linear_cost_function = PiecewiseLinearCostFunction(ActivePowerDispatchRecord)469 piecewise_linear_cost_function = PiecewiseLinearCostFunction()470 num_rows = piecewise_linear_cost_function.get_num_rows_from_row(row)471 rows_temp = rows[472 row_num:(row_num + num_rows)]473 piecewise_linear_cost_function.read_from_rows(rows_temp)474 self.piecewise_linear_cost_functions[475 piecewise_linear_cost_function.ltbl] = (476 piecewise_linear_cost_function)477 row_num += (num_rows - 1)478class Inl:479 '''In physical units, i.e. data convention, i.e. PSSE data files'''480 def __init__(self):481 self.generator_inl_records = {}482 # TODO483 def read_from_phase_0(self, file_name):484 '''takes the generator.csv file as input'''485 # TODO486 def write(self, file_name):487 '''writes the INL data to an INL-formatted file'''488 def read(self, file_name):489 with open(file_name, 'r') as in_file:490 lines = in_file.readlines()491 delimiter_str = ","492 quote_str = "'"493 skip_initial_space = True494 rows = csv.reader(495 lines,496 delimiter=delimiter_str,497 quotechar=quote_str,498 skipinitialspace=skip_initial_space)499 rows = [[t.strip() for t in r] for r in rows]500 self.read_from_rows(rows)501 502 def row_is_file_end(self, row):503 is_file_end = False504 if len(row) == 0:505 is_file_end = True506 if row[0][:1] in {'','q','Q'}:507 is_file_end = True508 return is_file_end509 510 def row_is_section_end(self, row):511 is_section_end = False512 if row[0][:1] == '0':513 is_section_end = True514 return is_section_end515 516 def read_from_rows(self, rows):517 row_num = -1518 while True:519 row_num += 1520 row = rows[row_num]521 if self.row_is_file_end(row):522 return523 if self.row_is_section_end(row):524 break525 generator_inl_record = GeneratorInlRecord()526 generator_inl_record.read_from_row(row)527 self.generator_inl_records[(528 generator_inl_record.i,529 generator_inl_record.id)] = generator_inl_record530 531class Con:532 '''In physical units, i.e. data convention, i.e. PSSE data files'''533 def __init__(self):534 self.contingencies = {}535 def read_from_phase_0(self, file_name):536 '''takes the contingency.csv file as input'''537 with open(file_name, 'r') as in_file:538 lines = in_file.readlines()539 delimiter_str = " "540 quote_str = "'"541 skip_initial_space = True542 del lines[0]543 rows = csv.reader(544 lines,545 delimiter=delimiter_str,546 quotechar=quote_str,547 skipinitialspace=skip_initial_space)548 rows = [[t.strip() for t in r] for r in rows]549 quote_str = "'"550 contingency = Contingency()551 #there is no contingency label for continency.csv552 for r in rows:553 tmprow=r[0].split(',')554 if tmprow[1].upper()=='B' or tmprow[1].upper()=='T':555 contingency.label ="LINE-"+tmprow[2]+"-"+tmprow[3]+"-"+tmprow[4]556 branch_out_event = BranchOutEvent()557 branch_out_event.read_from_csv(tmprow)558 contingency.branch_out_events.append(branch_out_event)559 self.contingencies[contingency.label] = branch_out_event560 elif tmprow[1].upper()=='G':561 contingency.label = "GEN-"+tmprow[2]+"-"+tmprow[3]562 generator_out_event = GeneratorOutEvent()563 generator_out_event.read_from_csv(tmprow)564 contingency.generator_out_events.append(generator_out_event)565 self.contingency.generator_out_event.read_from_csv(tmprow)566 def write(self, file_name):567 file = open(file_name,"w") 568 #for r in self.contingencies.values():569 for r in self.contingencies:570 row="CONTINGENCY "+r+"\n"571 #ds=self.active_power_dispatch_records.get((r.i,r.id))572 file.write(row)573 if cmp(r[0:4],'LINE')==0:574 row="DISCONNECT LINE FROM BUS "+str(self.contingencies.get(r).i)+" TO BUS "+str(self.contingencies.get(r).j)+ " CKT " + str(self.contingencies.get(r).ckt)+"\n"575 elif cmp(r[0:3],'GEN')==0: 576 row="REMOVE MACHINE "+str(self.contingencies.get(r).id)+" FROM BUS "+str(self.contingencies.get(r).i)+"\n"577 file.write(row)578 file.write("END\n")579 file.write("END\n") 580 def read(self, file_name):581 with open(file_name, 'r') as in_file:582 lines = in_file.readlines()583 delimiter_str = " "584 quote_str = "'"585 skip_initial_space = True586 rows = csv.reader(587 lines,588 delimiter=delimiter_str,589 quotechar=quote_str,590 skipinitialspace=skip_initial_space)591 rows = [[t.strip() for t in r] for r in rows]592 self.read_from_rows(rows)593 594 def row_is_file_end(self, row):595 is_file_end = False596 if len(row) == 0:597 is_file_end = True598 if row[0][:1] in {'','q','Q'}:599 is_file_end = True600 return is_file_end601 602 #def row_is_section_end(self, row):603 #604 # is_section_end = False605 # if row[0][:1] == '0':606 # is_section_end = True607 # return is_section_end608 def is_contingency_start(self, row):609 return (row[0].upper() == 'CONTINGENCY')610 def is_end(self, row):611 return (row[0].upper() == 'END')612 def is_branch_out_event(self, row):613 return (614 row[0].upper() in {'DISCONNECT', 'OPEN', 'TRIP'} and615 row[1].upper() in {'BRANCH', 'LINE'})616 def is_three_winding(self, row):617 #print row618 if len(row) < 9:619 return False620 elif row[8].upper() == 'TO':621 return True622 else:623 return False624 def is_generator_out_event(self, row):625 return(626 row[0].upper() == 'REMOVE' and627 row[1].upper() in {'UNIT', 'MACHINE'})628 629 def read_from_rows(self, rows):630 row_num = -1631 in_contingency = False632 while True:633 row_num += 1634 row = rows[row_num]635 if self.row_is_file_end(row):636 return637 #if self.row_is_section_end(row):638 # break639 if self.is_contingency_start(row):640 in_contingency = True641 contingency = Contingency()642 contingency.label = row[1]643 if self.is_end(row):644 if in_contingency:645 self.contingencies[contingency.label] = contingency646 in_contingency = False647 else:648 break649 if self.is_branch_out_event(row):650 branch_out_event = BranchOutEvent()651 if self.is_three_winding(row):652 branch_out_event.read_three_winding_from_row(row)653 else:654 branch_out_event.read_from_row(row)655 contingency.branch_out_events.append(branch_out_event)656 if self.is_generator_out_event(row):657 generator_out_event = GeneratorOutEvent()658 generator_out_event.read_from_row(row)659 contingency.generator_out_events.append(generator_out_event)660class CaseIdentification:661 def __init__(self):662 self.ic = None663 self.sbase = None664 self.rev = None665 self.xfrrat = None666 self.nxfrat = None667 self.basfrq = None668 def read_from_row(self, row):669 row = pad_row(row, 6)670 row[5] = remove_end_of_line_comment(row[5], '/')671 self.ic = parse_token(row[0], int, 0)672 self.sbase = parse_token(row[1], float, 100.0)673 self.rev = parse_token(row[2], int, 33)674 self.xfrrat = parse_token(row[3], float, -1.0)675 self.nxfrat = parse_token(row[4], float, 1.0)676 self.basfrq = parse_token(row[5], float, 60.0) # need to remove end of line comment677class Bus:678 def __init__(self):679 self.i = None680 self.name = None681 self.baskv = None682 self.ide = None683 self.area = None684 self.zone = None685 self.owner = None686 self.vm = None687 self.va = None688 self.nvhi = None689 self.nvlo = None690 self.evhi = None691 self.evlo = None692 def read_from_row(self, row):693 row = pad_row(row, 13)694 self.i = parse_token(row[0], int, '')695 self.name = parse_token(row[1], str, 12*' ')696 self.baskv = parse_token(row[2], float, 0.0)697 self.ide = parse_token(row[3], int, 1)698 self.area = parse_token(row[4], int, 1)699 self.zone = parse_token(row[5], int, 1)700 self.owner = parse_token(row[6], int, 1)701 self.vm = parse_token(row[7], float, 1.0)702 self.va = parse_token(row[8], float, 0.0)703 self.nvhi = parse_token(row[9], float, 1.1)704 self.nvlo = parse_token(row[10], float, 0.9)705 self.evhi = parse_token(row[11], float, 1.1)706 self.evlo = parse_token(row[12], float, 0.9)707 708class Load:709 def __init__(self):710 self.i = None711 self.id = None712 self.status = None713 self.area = None714 self.zone = None715 self.pl = None716 self.ql = None717 self.ip = None718 self.iq = None719 self.yp = None720 self.yq = None721 self.owner = None722 self.scale = None723 self.intrpt = None724 def read_from_row(self, row):725 row = pad_row(row, 14)726 self.i = parse_token(row[0], int, '')727 self.id = parse_token(row[1], str, '1')728 self.status = parse_token(row[2], int, 1)729 self.area = parse_token(row[3], int, 0)730 self.zone = parse_token(row[4], int, 0)731 self.pl = parse_token(row[5], float, 0.0)732 self.ql = parse_token(row[6], float, 0.0)733 self.ip = parse_token(row[7], float, 0.0)734 self.iq = parse_token(row[8], float, 0.0)735 self.yp = parse_token(row[9], float, 0.0)736 self.yq = parse_token(row[10], float, 0.0)737 self.owner = parse_token(row[11], int, 0)738 self.scale = parse_token(row[12], int, 1)739 self.intrpt = parse_token(row[13], int, 0)740class FixedShunt:741 def __init__(self):742 self.i = None743 self.id = None744 self.status = None745 self.gl = None746 self.bl = None747 def read_from_row(self, row):748 row = pad_row(row, 5)749 self.i = parse_token(row[0], int, '')750 self.id = parse_token(row[1], str, '1')751 self.status = parse_token(row[2], int, 1)752 self.gl = parse_token(row[3], float, 0.0)753 self.bl = parse_token(row[4], float, 0.0)754class Generator:755 def __init__(self):756 self.i = None757 self.id = None758 self.pg = None759 self.qg = None760 self.qt = None761 self.qb = None762 self.vs = None763 self.ireg = None764 self.mbase = None765 self.zr = None766 self.zx = None767 self.rt = None768 self.xt = None769 self.gtap = None770 self.stat = None771 self.rmpct = None772 self.pt = None773 self.pb = None774 self.o1 = None775 self.f1 = None776 self.o2 = None777 self.f2 = None778 self.o3 = None779 self.f3 = None780 self.o4 = None781 self.f4 = None782 self.wmod = None783 self.wpf = None784 def read_from_row(self, row):785 row = pad_row(row, 28)786 self.i = parse_token(row[0], int, '')787 self.id = parse_token(row[1], str, '1')788 self.pg = parse_token(row[2], float, 0.0)789 self.qg = parse_token(row[3], float, 0.0)790 self.qt = parse_token(row[4], float, 9999.0)791 self.qb = parse_token(row[5], float, -9999.0)792 self.vs = parse_token(row[6], float, 1.0)793 self.ireg = parse_token(row[7], int, 0)794 self.mbase = parse_token(row[8], float, 0.0)795 self.zr = parse_token(row[9], float, 0.0)796 self.zx = parse_token(row[10], float, 1.0)797 self.rt = parse_token(row[11], float, 0.0)798 self.xt = parse_token(row[12], float, 0.0)799 self.gtap = parse_token(row[13], float, 1.0)800 self.stat = parse_token(row[14], int, 1)801 self.rmpct = parse_token(row[15], float, 100.0)802 self.pt = parse_token(row[16], float, 9999.0)803 self.pb = parse_token(row[17], float, -9999.0)804 self.o1 = parse_token(row[18], int, 0)805 self.f1 = parse_token(row[19], float, 1.0)806 self.o2 = parse_token(row[20], int, 0)807 self.f2 = parse_token(row[21], float, 1.0)808 self.o3 = parse_token(row[22], int, 0)809 self.f3 = parse_token(row[23], float, 1.0)810 self.o4 = parse_token(row[24], int, 0)811 self.f4 = parse_token(row[25], float, 1.0)812 self.wmod = parse_token(row[26], int, 0)813 self.wpf = parse_token(row[27], float, 1.0)814class NontransformerBranch:815 def __init__(self):816 self.i = None817 self.j = None818 self.ckt = None819 self.r = None820 self.x = None821 self.b = None822 self.ratea = None823 self.rateb = None824 self.ratec = None825 self.gi = None826 self.bi = None827 self.gj = None828 self.bj = None829 self.st = None830 self.met = None831 self.len = None832 self.o1 = None833 self.f1 = None834 self.o2 = None835 self.f2 = None836 self.o3 = None837 self.f3 = None838 self.o4 = None839 self.f4 = None840 def read_from_row(self, row):841 row = pad_row(row, 24)842 self.i = parse_token(row[0], int, '')843 self.j = parse_token(row[1], int, '')844 self.ckt = parse_token(row[2], str, '1')845 self.r = parse_token(row[3], float, 0.0)846 self.x = parse_token(row[4], float, 0.0)847 self.b = parse_token(row[5], float, 0.0)848 self.ratea = parse_token(row[6], float, 0.0)849 self.rateb = parse_token(row[7], float, 0.0)850 self.ratec = parse_token(row[8], float, 0.0)851 self.gi = parse_token(row[9], float, 0.0)852 self.bi = parse_token(row[10], float, 0.0)853 self.gj = parse_token(row[11], float, 0.0)854 self.bj = parse_token(row[12], float, 0.0)855 self.st = parse_token(row[13], int, 1)856 self.met = parse_token(row[14], int, 1)857 self.len = parse_token(row[15], float, 0.0)858 self.o1 = parse_token(row[16], int, 0)859 self.f1 = parse_token(row[17], float, 1.0)860 self.o2 = parse_token(row[18], int, 0)861 self.f2 = parse_token(row[19], float, 1.0)862 self.o3 = parse_token(row[20], int, 0)863 self.f3 = parse_token(row[21], float, 1.0)864 self.o4 = parse_token(row[22], int, 0)865 self.f4 = parse_token(row[23], float, 1.0)866class Transformer:867 def __init__(self):868 self.i = None869 self.j = None870 self.k = None871 self.ckt = None872 self.cw = None873 self.cz = None874 self.cm = None875 self.mag1 = None876 self.mag2 = None877 self.nmetr = None878 self.name = None879 self.stat = None880 self.o1 = None881 self.f1 = None882 self.o2 = None883 self.f2 = None884 self.o3 = None885 self.f3 = None886 self.o4 = None887 self.f4 = None888 self.vecgrp = None889 self.r12 = None890 self.x12 = None891 self.sbase12 = None892 self.r23 = None893 self.x23 = None894 self.sbase23 = None895 self.r31 = None896 self.x31 = None897 self.sbase31 = None898 self.vmstar = None899 self.anstar = None900 self.windv1 = None901 self.nomv1 = None902 self.ang1 = None903 self.rata1 = None904 self.ratb1 = None905 self.ratc1 = None906 self.cod1 = None907 self.cont1 = None908 self.rma1 = None909 self.rmi1 = None910 self.vma1 = None911 self.vmi1 = None912 self.ntp1 = None913 self.tab1 = None914 self.cr1 = None915 self.cx1 = None916 self.cnxa1 = None917 self.windv2 = None918 self.nomv2 = None919 self.ang2 = None920 self.rata2 = None921 self.ratb2 = None922 self.ratc2 = None923 self.cod2 = None924 self.cont2 = None925 self.rma2 = None926 self.rmi2 = None927 self.vma2 = None928 self.vmi2 = None929 self.ntp2 = None930 self.tab2 = None931 self.cr2 = None932 self.cx2 = None933 self.cnxa2 = None934 self.windv3 = None935 self.nomv3 = None936 self.ang3 = None937 self.rata3 = None938 self.ratb3 = None939 self.ratc3 = None940 self.cod3 = None941 self.cont3 = None942 self.rma3 = None943 self.rmi3 = None944 self.vma3 = None945 self.vmi3 = None946 self.ntp3 = None947 self.tab3 = None948 self.cr3 = None949 self.cx3 = None950 self.cnxa3 = None951 @property952 def num_windings(self):953 num_windings = 0954 if self.k is None:955 num_windings = 0956 elif self.k == 0:957 num_windings = 2958 else:959 num_windings = 3960 return num_windings961 962 def get_num_rows_from_row(self, row):963 num_rows = 0964 k = parse_token(row[2], int, 0)965 if k == 0:966 num_rows = 4967 else:968 num_rows = 5969 return num_rows970 def read_from_rows(self, rows):971 full_rows = self.pad_rows(rows)972 row = self.flatten_rows(full_rows)973 self.read_from_row(row)974 975 def pad_rows(self, rows):976 rows_new = rows977 if len(rows_new) == 4:978 rows_new.append([])979 rows_len = [len(r) for r in rows_new]980 rows_len_new = [21, 11, 17, 17, 17]981 rows_len_increase = [rows_len_new[i] - rows_len[i] for i in range(5)]982 # check no negatives in increase983 rows_new = [rows_new[i] + rows_len_increase[i]*[''] for i in range(5)]984 return rows_new985 def flatten_rows(self, rows):986 row = [t for r in rows for t in r]987 return row988 989 def read_from_row(self, row):990 self.i = parse_token(row[0], int, '')991 self.j = parse_token(row[1], int, '')992 self.k = parse_token(row[2], int, 0)993 self.ckt = parse_token(row[3], str, '1')994 self.cw = parse_token(row[4], int, 1)995 self.cz = parse_token(row[5], int, 1)996 self.cm = parse_token(row[6], int, 1)997 self.mag1 = parse_token(row[7], float, 0.0)998 self.mag2 = parse_token(row[8], float, 0.0)999 self.nmetr = parse_token(row[9], int, 2)1000 self.name = parse_token(row[10], str, 12*' ')1001 self.stat = parse_token(row[11], int, 1)1002 self.o1 = parse_token(row[12], int, 0)1003 self.f1 = parse_token(row[13], float, 1.0)1004 #self.o2 = parse_token(row[14], int, 0)1005 self.f2 = parse_token(row[15], float, 1.0)1006 self.o3 = parse_token(row[16], int, 0)1007 self.f3 = parse_token(row[17], float, 1.0)1008 self.o4 = parse_token(row[18], int, 0)1009 self.f4 = parse_token(row[19], float, 1.0)1010 self.vecgrp = parse_token(row[20], str, 12*' ')1011 self.r12 = parse_token(row[21], float, 0.0)1012 self.x12 = parse_token(row[22], float, 0.0)1013 self.sbase12 = parse_token(row[23], float, 0.0)1014 self.r23 = parse_token(row[24], float, 0.0)1015 self.x23 = parse_token(row[25], float, 0.0)1016 self.sbase23 = parse_token(row[26], float, 0.0)1017 self.r31 = parse_token(row[27], float, 0.0)1018 self.x31 = parse_token(row[28], float, 0.0)1019 self.sbase31 = parse_token(row[29], float, 0.0)1020 self.vmstar = parse_token(row[30], float, 1.0)1021 self.anstar = parse_token(row[31], float, 0.0)1022 self.windv1 = parse_token(row[32], float, 1.0)1023 self.nomv1 = parse_token(row[33], float, 0.0)1024 self.ang1 = parse_token(row[34], float, 0.0)1025 self.rata1 = parse_token(row[35], float, 0.0)1026 self.ratb1 = parse_token(row[36], float, 0.0)1027 self.ratc1 = parse_token(row[37], float, 0.0)1028 self.cod1 = parse_token(row[38], int, 0)1029 self.cont1 = parse_token(row[39], int, 0)1030 self.rma1 = parse_token(row[40], float, 1.1)1031 self.rmi1 = parse_token(row[41], float, 0.9)1032 self.vma1 = parse_token(row[42], float, 1.1)1033 self.vmi1 = parse_token(row[43], float, 0.9)1034 self.ntp1 = parse_token(row[44], int, 33)1035 self.tab1 = parse_token(row[45], int, 0)1036 self.cr1 = parse_token(row[46], float, 0.0)1037 self.cx1 = parse_token(row[47], float, 0.0)1038 self.cnxa1 = parse_token(row[48], float, 0.0)1039 self.windv2 = parse_token(row[49], float, 1.0)1040 self.nomv2 = parse_token(row[50], float, 0.0)1041 self.ang2 = parse_token(row[51], float, 0.0)1042 self.rata2 = parse_token(row[52], float, 0.0)1043 self.ratb2 = parse_token(row[53], float, 0.0)1044 self.ratc2 = parse_token(row[54], float, 0.0)1045 self.cod2 = parse_token(row[55], int, 0)1046 self.cont2 = parse_token(row[56], int, 0)1047 self.rma2 = parse_token(row[57], float, 1.1)1048 self.rmi2 = parse_token(row[58], float, 0.9)1049 self.vma2 = parse_token(row[59], float, 1.1)1050 self.vmi2 = parse_token(row[60], float, 0.9)1051 self.ntp2 = parse_token(row[61], int, 33)1052 self.tab2 = parse_token(row[62], int, 0)1053 self.cr2 = parse_token(row[63], float, 0.0)1054 self.cx2 = parse_token(row[64], float, 0.0)1055 self.cnxa2 = parse_token(row[65], float, 0.0)1056 self.windv3 = parse_token(row[66], float, 1.0)1057 self.nomv3 = parse_token(row[67], float, 0.0)1058 self.ang3 = parse_token(row[68], float, 0.0)1059 self.rata3 = parse_token(row[69], float, 0.0)1060 self.ratb3 = parse_token(row[70], float, 0.0)1061 self.ratc3 = parse_token(row[71], float, 0.0)1062 self.cod3 = parse_token(row[72], int, 0)1063 self.cont3 = parse_token(row[73], int, 0)1064 self.rma3 = parse_token(row[74], float, 1.1)1065 self.rmi3 = parse_token(row[75], float, 0.9)1066 self.vma3 = parse_token(row[76], float, 1.1)1067 self.vmi3 = parse_token(row[77], float, 0.9)1068 self.ntp3 = parse_token(row[78], int, 33)1069 self.tab3 = parse_token(row[79], int, 0)1070 self.cr3 = parse_token(row[80], float, 0.0)1071 self.cx3 = parse_token(row[81], float, 0.0)1072 self.cnxa3 = parse_token(row[82], float, 0.0)1073class Area:1074 def __init__(self):1075 self.i = None1076 self.isw = None1077 self.pdes = None1078 self.ptol = None1079 self.arname = None1080 def read_from_row(self, row):1081 row = pad_row(row, 5)1082 self.i = parse_token(row[0], int, '')1083 self.isw = parse_token(row[1], int, 0)1084 self.pdes = parse_token(row[2], float, 0.0)1085 self.ptol = parse_token(row[3], float, 10.0)1086 self.arname = parse_token(row[4], str, 12*' ')1087class Zone:1088 def __init__(self):1089 self.i = None1090 self.zoname = None1091 1092 def read_from_row(self, row):1093 row = pad_row(row, 2)1094 self.i = parse_token(row[0], int, '')1095 self.zoname = parse_token(row[1], str, 12*' ')1096class SwitchedShunt:1097 def __init__(self):1098 self.i = None1099 self.modsw = None1100 self.adjm = None1101 self.stat = None1102 self.vswhi = None1103 self.vswlo = None1104 self.swrem = None1105 self.rmpct = None1106 self.rmidnt = None1107 self.binit = None1108 self.n1 = None1109 self.b1 = None1110 self.n2 = None1111 self.b2 = None1112 self.n3 = None1113 self.b3 = None1114 self.n4 = None1115 self.b4 = None1116 self.n5 = None1117 self.b5 = None1118 self.n6 = None1119 self.b6 = None1120 self.n7 = None1121 self.b7 = None1122 self.n8 = None1123 self.b8 = None1124 def read_from_row(self, row):1125 row = pad_row(row, 26)1126 self.i = parse_token(row[0], int, '')1127 self.modsw = parse_token(row[1], int, 1)1128 self.adjm = parse_token(row[2], int, 0)1129 self.stat = parse_token(row[3], int, 1)1130 self.vswhi = parse_token(row[4], float, 1.0)1131 self.vswlo = parse_token(row[5], float, 1.0)1132 self.swrem = parse_token(row[6], int, 0)1133 self.rmpct = parse_token(row[7], float, 100.0)1134 self.rmidnt = parse_token(row[8], str, 12*' ')1135 self.binit = parse_token(row[9], float, 0.0)1136 self.n1 = parse_token(row[10], int, 0)1137 self.b1 = parse_token(row[11], float, 0.0)1138 self.n2 = parse_token(row[12], int, 0)1139 self.b2 = parse_token(row[13], float, 0.0)1140 self.n3 = parse_token(row[14], int, 0)1141 self.b3 = parse_token(row[15], float, 0.0)1142 self.n4 = parse_token(row[16], int, 0)1143 self.b4 = parse_token(row[17], float, 0.0)1144 self.n5 = parse_token(row[18], int, 0)1145 self.b5 = parse_token(row[19], float, 0.0)1146 self.n6 = parse_token(row[20], int, 0)1147 self.b6 = parse_token(row[21], float, 0.0)1148 self.n7 = parse_token(row[22], int, 0)1149 self.b7 = parse_token(row[23], float, 0.0)1150 self.n8 = parse_token(row[24], int, 0)1151 self.b8 = parse_token(row[25], float, 0.0)1152 1153class GeneratorDispatchRecord:1154 def __init__(self):1155 self.bus = None1156 self.genid = None1157 self.disp = None1158 self.dsptbl = None1159 def read_from_row(self, row):1160 row = pad_row(row, 4)1161 self.bus = parse_token(row[0], int, '')1162 self.genid = parse_token(row[1], str, '1')1163 self.disp = parse_token(row[2], float, 1.0)1164 self.dsptbl = parse_token(row[3], int, 0)1165 def read_from_csv(self, row):1166 self.bus = parse_token(row[0], int, '')1167 self.genid = parse_token(row[1], str, '1')1168 1169class ActivePowerDispatchRecord:1170 def __init__(self):1171 self.tbl = None1172 self.pmax = None1173 self.pmin = None1174 self.fuelcost = None1175 self.ctyp = None1176 self.status = None1177 self.ctbl = None1178 def read_from_row(self, row):1179 row = pad_row(row, 7)1180 self.tbl = parse_token(row[0], int, '')1181 self.pmax = parse_token(row[1], float, 9999.0)1182 self.pmin = parse_token(row[2], float, -9999.0)1183 self.fuelcost = parse_token(row[3], float, 1.0)1184 self.ctyp = parse_token(row[4], int, 1)1185 self.status = parse_token(row[5], int, 1)1186 self.ctbl = parse_token(row[6], int, 0)1187class PiecewiseLinearCostFunction():1188 def __init__(self):1189 self.ltbl = None1190 self.label = None1191 self.costzero =None1192 self.npairs = None1193 self.points = []1194 def read_from_row(self, row):1195 self.ltbl = parse_token(row[0], int, '')1196 self.label = parse_token(row[1], str, '')1197 self.npairs = parse_token(row[2], int, 0)1198 for i in range(self.npairs):1199 point = Point()1200 point.read_from_row(1201 row[(3 + 2*i):(5 + 2*i)])1202 self.points.append(point)1203 def get_num_rows_from_row(self, row):1204 num_rows = parse_token(row[2], int, 0) + 11205 return num_rows1206 def flatten_rows(self, rows):1207 row = [t for r in rows for t in r]1208 return row1209 def read_from_rows(self, rows):1210 self.read_from_row(self.flatten_rows(rows))1211 1212class QuadraticCostFunctions(GeneratorDispatchRecord,PiecewiseLinearCostFunction):1213 def __init__(self):1214 GeneratorDispatchRecord.__init__(self)1215 PiecewiseLinearCostFunction.__init__(self)1216 self.constc = None1217 self.linearc = None1218 self.quadraticc = None1219 self.powerfactor = None1220 def read_from_csv_quadraticinfo(self, row):1221 if parse_token(row[2], int, '')==0:1222 self.constc = parse_token(row[3], float, 0.0)1223 elif parse_token(row[2], int, '')==1:1224 self.linearc = parse_token(row[3], float, 0.0)1225 elif parse_token(row[2], int, '')==2: 1226 self.quadraticc = parse_token(row[3], float, 0.0)1227 elif parse_token(row[2], int, '')==9: 1228 self.powerfactor = parse_token(row[3], float, 0.0)1229class GeneratorInlRecord:1230 def __init__(self):1231 self.i = None1232 self.id = None1233 self.h = None1234 self.pmax = None1235 self.pmin = None1236 self.r = None1237 self.d = None1238 def read_from_row(self, row):1239 row = pad_row(row, 7)1240 self.i = parse_token(row[0], int, '')1241 self.id = parse_token(row[1], str, '1')1242 self.h = parse_token(row[2], float, 4.0)1243 self.pmax = parse_token(row[3], float, 1.0)1244 self.pmin = parse_token(row[4], float, 0.0)1245 self.r = parse_token(row[5], float, 0.05)1246 self.d = parse_token(row[6], float, 0.0)1247 1248class Contingency:1249 def __init__(self):1250 self.label = ''1251 self.branch_out_events = []1252 self.generator_out_events = []1253class Point:1254 def __init__(self):1255 self.x = None1256 self.y = None1257 def read_from_row(self, row):1258 row = pad_row(row, 2)1259 self.x = parse_token(row[0], float, 0.0)1260 self.y = parse_token(row[1], float, 0.0)1261class BranchOutEvent:1262 def __init__(self):1263 self.i = None1264 self.j = None1265 self.ckt = None1266 def read_from_row(self, row):1267 row = pad_row(row, 10)1268 self.i = parse_token(row[4], int, '')1269 self.j = parse_token(row[7], int, '')1270 self.ckt = parse_token(row[9], str, '1')1271 def read_from_csv(self, row):1272 self.i = parse_token(row[2], int, '')1273 self.j = parse_token(row[3], int, '')1274 self.ckt = parse_token(row[4], str, '1')1275 def read_three_winding_from_row(self, row):1276 row = pad_row(row, 13)1277 self.i = parse_token(row[4], int, '')1278 self.j = parse_token(row[7], int, '')1279 self.k = parse_token(row[10], int, '')1280 self.ckt = parse_token(row[12], str, '1')1281class GeneratorOutEvent:1282 def __init__(self):1283 self.i = None1284 self.id = None1285 def read_from_csv(self, row):1286 self.i = parse_token(row[2], int, '')1287 self.id = parse_token(row[3], str, '1')1288 def read_from_row(self, row):1289 self.i = parse_token(row[5], int, '')...

Full Screen

Full Screen

parse.py

Source:parse.py Github

copy

Full Screen

...45 def wrapped_parse(content, *args, **kwargs):46 if isinstance(content, (str, bytes)):47 content = list(lex(content)), 0, set()48 content, result = parse(content, *args, **kwargs)49 parse_token(content, 'end')50 return result51 else:52 return parse(content, *args, **kwargs)53 return wrapped_parse54@parser55def parse_token(content, *keys, optional=False):56 tokens, index, checked = content57 key, i, _ = tokens[index]58 if key in keys:59 return (tokens, index + 1, set()), tokens[index]60 checked = checked | set(keys)61 if optional:62 return (tokens, index, checked), None63 keys = list(map(repr, sorted(checked)))64 if len(keys) == 1:65 keys = keys[0]66 else:67 keys = ', '.join(keys[:-1]) + ' or ' + keys[-1]68 raise ValueError(f'{i}: got {key!r}, expected {keys}')69@parser70def parse_int(content):71 index = content[1]72 content, name = parse_name(content)73 try:74 return content, int(name)75 except ValueError:76 raise ValueError(f'{index}: expected an int') from None77def normalize_enum(value):78 return value.lower().replace('-', '').replace('_', '')79@parser80def parse_enum(content, *values):81 index = content[1]82 content, name = parse_name(content)83 name = normalize_enum(name)84 values = {normalize_enum(value): value for value in values}85 try:86 return content, values[name]87 except KeyError:88 raise ValueError(f'{index}: unexpected value: {name!r}') from None89WEEKDAYS = {90 'mon': 0,91 'tue': 1,92 'wed': 2,93 'thu': 3,94 'fri': 4,95 'sat': 5,96 'sun': 6,97}98MONTHS = {99 'jan': 1,100 'feb': 2,101 'mar': 3,102 'apr': 4,103 'may': 5,104 'jun': 6,105 'jul': 7,106 'aug': 8,107 'sep': 9,108 'oct': 10,109 'nov': 11,110 'dec': 12,111}112@parser113def parse_date(content):114 index = content[1]115 content, weekday = parse_enum(content, *WEEKDAYS)116 weekday = WEEKDAYS[weekday]117 content, _ = parse_token(content, ',')118 content, day = parse_int(content)119 content, month = parse_enum(content, *MONTHS)120 month = MONTHS[month]121 content, year = parse_int(content)122 content, hour = parse_int(content)123 content, _ = parse_token(content, ':')124 content, minute = parse_int(content)125 content, _ = parse_token(content, ':')126 content, second = parse_int(content)127 content, _ = parse_enum(content, 'GMT', 'UTC')128 try:129 date = datetime(130 year, month, day, hour, minute, second, tzinfo=timezone.utc,131 )132 assert date.weekday() == weekday133 except (ValueError, AssertionError):134 raise ValueError(f'{index}: invalid date') from None135 return content, date136@parser137def parse_path(content):138 _, token = parse_token(content, 'string', optional=True)139 if token:140 return parse_value(content)141 parts = []142 while True:143 content, token = parse_token(content, 'name', '/', optional=True)144 if token is None:145 break146 parts.append(token[2])147 return content, ''.join(parts)148@parser149def parse_name(content):150 content, (_, _, name) = parse_token(content, 'name')151 return content, name152@parser153def parse_value(content):154 content, (key, _, value) = parse_token(content, 'name', 'string')155 if key == 'string':156 raw_chars = iter(value[1:-1])157 str_chars = []158 for char in raw_chars:159 if char == '\\':160 char = next(raw_chars)161 str_chars.append(char)162 value = ''.join(str_chars)163 return content, value164@parser165def parse_params(content):166 params = {}167 content, more_params = parse_token(content, ';', optional=True)168 while more_params:169 content, param_key = parse_name(content)170 content, _ = parse_token(content, '=')171 content, param_value = parse_value(content)172 params[param_key] = param_value173 content, more_params = parse_token(content, ';', optional=True)174 return content, params175@parser176def parse_content_disposition(content):177 content, value = parse_value(content)178 content, params = parse_params(content)179 return content, (value, params)180@parser181def parse_media_type(content):182 content, type_ = parse_value(content)183 content, _ = parse_token(content, '/')184 content, subtype = parse_value(content)185 return content, f'{type_}/{subtype}'186@parser187def parse_content_type(content):188 if content is None:189 return None, {}190 content, media_type = parse_media_type(content)191 content, params = parse_params(content)192 return content, (media_type, params)193@parser194def parse_cookies(content):195 cookies = {}196 while True:197 _, token = parse_token(content, 'end', optional=True)198 if token is not None:199 break200 content, name = parse_name(content)201 content, _ = parse_token(content, '=')202 content, value = parse_value(content)203 cookies[name] = value204 content, token = parse_token(content, ';', optional=True)205 if token is None:206 break207 return content, cookies208@parser209def parse_set_cookie(content):210 content, cookie_name = parse_name(content)211 content, _ = parse_token(content, '=')212 content, cookie_value = parse_value(content)213 params = {'secure': False, 'http_only': False}214 content, more_params = parse_token(content, ';', optional=True)215 while more_params:216 content, param_name = parse_enum(217 content, 'expires', 'max_age', 'domain', 'path', 'secure',218 'http_only', 'same_site'219 )220 if param_name == 'expires':221 content, _ = parse_token(content, '=')222 content, param_value = parse_date(content)223 elif param_name == 'max_age':224 content, _ = parse_token(content, '=')225 content, param_value = parse_int(content)226 elif param_name == 'domain':227 content, _ = parse_token(content, '=')228 content, param_value = parse_value(content)229 elif param_name == 'path':230 content, _ = parse_token(content, '=')231 content, param_value = parse_path(content)232 elif param_name == 'secure':233 param_value = True234 elif param_name == 'http_only':235 param_value = True236 elif param_name == 'same_site':237 content, _ = parse_token(content, '=')238 content, param_value = parse_enum(content, 'lax', 'strict', 'none')239 params[param_name] = param_value240 content, more_params = parse_token(content, ';', optional=True)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run yandex-tank automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful