def upec(n_failures=100): """if we happen to have a stage one that kills the shunt bus then most bugs go away. use this to get some data.""" clean_files() clean = True data = """ [upec] opf # remove generator g33 # set all demand 0.7686144 # remove bus 6 """ scenario = text_to_scenario(data) psat = read_psat("rts2.m") tmp_psat = scenario_to_psat(scenario, psat) report = single_simulate(tmp_psat, scenario.simtype, scenario.title) new_psat = report_to_psat(report, tmp_psat) prob = read_probabilities("rts.net") failure_batch = make_failures(prob, n_failures) failure_batch.scenarios.insert(0, Scenario("basecase")) batch_simulate(failure_batch, new_psat, 100, clean) filename = scenario.title + ".bch2" with open(filename, "w") as result_file: failure_batch.csv_write(result_file)
def example1(n=100): """make `n` outages, simulate them, and save the resulting batch""" psat = read_psat("rts.m") prob = read_probabilities("rts.net") batch = make_failures(prob, n) batch_simulate(batch, psat, 30) with open("rts.bch", "w") as result_file: batch.write(result_file)
def simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file): clean_files() print "[C] simulate %d unique states with %d unique contingencies" % ( len(outage_batch), len(failure_batch)) for n, scenario in enumerate(outage_batch): try: print "[C] simulating state", n + 1, "of", int( math.ceil(len(outage_batch))) report = simulate_scenario(psat, scenario, False) scenario_psat = report_to_psat(report, psat) clean_files() mismatch_file.write( as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") mismatch_file.write( as_csv([scenario.title] + list(scenario_psat.get_stats())) + "\n") mismatch_file.write( as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") print "[C] simulating state - prep done." for x in failure_batch: x.result = None batch_simulate(failure_batch, scenario_psat, 100, True, mismatch_file) filename = scenario.title + ".txt" with open(filename, "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Outage Case %d Stats ----" % n failure_batch.write_stats(sys.stdout) summary_file.write("Outage Case %d Stats\n" % n) summary_file.write("%s\n" % ("-" * 80)) failure_batch.write_stats(summary_file) print "[C] simulating state", n + 1, "done" except Exception as exce: print "[E] Error Caught at main.simulate_cases (%s)" % scenario.title print exce raise
def simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file): clean_files() print "[C] simulate %d unique states with %d unique contingencies" % ( len(outage_batch), len(failure_batch)) for n, scenario in enumerate(outage_batch): try: print "[C] simulating state", n + 1, "of", int(math.ceil(len(outage_batch))) report = simulate_scenario(psat, scenario, False) scenario_psat = report_to_psat(report, psat) clean_files() mismatch_file.write(as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") mismatch_file.write(as_csv([scenario.title] + list(scenario_psat.get_stats())) + "\n") mismatch_file.write(as_csv("---- ---- ---- ---- ---- ----".split()) + "\n") print "[C] simulating state - prep done." for x in failure_batch: x.result = None batch_simulate(failure_batch, scenario_psat, 100, True, mismatch_file) filename = scenario.title + ".txt" with open(filename, "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Outage Case %d Stats ----" % n failure_batch.write_stats(sys.stdout) summary_file.write("Outage Case %d Stats\n" % n) summary_file.write("%s\n" % ("-"*80)) failure_batch.write_stats(summary_file) print "[C] simulating state", n + 1, "done" except Exception as exce: print "[E] Error Caught at main.simulate_cases (%s)" % scenario.title print exce raise
def test007(): """batch and single should gave same results""" clean_files() psat = read_psat("rts2.m") # prob = read_probabilities("rts.net") # batch = make_outages(prob, 2) data = """ [batch0] opf remove generator g1 remove generator g4 remove generator g31 set all demand 0.3987456 result pass [batch1] opf remove generator g22 remove generator g24 set all demand 0.6670332 result fail """ batch = SimulationBatch() batch.read(StringIO(data)) batch_simulate(batch, psat, 10, False) with open("rts.bch", "w") as result_file: batch.write(result_file) for n, scenario in enumerate(batch): scenario.title = "single" + str(n) report = simulate_scenario(psat, scenario, False) scenario.result = report_in_limits(report) with open("rts2.bch", "w") as result_file: batch.write(result_file)
def generate_cases(n_outages=10, n_failures=1000, sim=True, full_sim=True): timer_begin = time.clock() timer_start = timer_begin print "[G] start simulation with %d states and %d contingencies." % (n_outages, n_failures) if full_sim: Ensure(n_outages and n_failures and sim, "can only do full sim if we have everything") clean_files() batch_size = 100 psat = read_psat("rts.m") prob = read_probabilities("rts.net") # create the base cases by sampling for outages # simulate these and print to a file. # it should contain `n_outages` outages. summary_file = open("summary.txt", "w") mismatch_file = open("mismatch.txt", "w") print "Base Stats: mis= %f gen= %f load= %f lim ( %f < X < %f )" % psat.get_stats() mismatch_file.write(as_csv("title mismatch gen load min max".split()) + "\n") mismatch_file.write(as_csv(["base"] + list(psat.get_stats())) + "\n") try: if n_outages: outage_batch = make_outage_cases(prob, n_outages) if sim: batch_simulate(outage_batch, psat, batch_size, True, mismatch_file) with open("outage.txt", "w") as result_file: outage_batch.csv_write(result_file) print "-" * 80 print "---- Outage Stats ----" outage_batch.write_stats(sys.stdout) summary_file.write("%s\n" % ("-"*80)) summary_file.write("Outage Stats\n") summary_file.write("%s\n" % ("-"*80)) outage_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] outages created in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() # do the same for one hour changes to the system. if n_failures: failure_batch = make_failure_cases(prob, n_failures) if sim: batch_simulate(failure_batch, psat, batch_size, True, mismatch_file) with open("failure.txt", "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Failure Stats ----" failure_batch.write_stats(sys.stdout) summary_file.write("Failure Stats\n") summary_file.write("%s\n" % ("-"*80)) failure_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] failures created in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() # simulate each of the changes to each base case if full_sim: simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] full sim in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() finally: timer_end = time.clock() timer_time = (timer_end - timer_begin) print "[G] total time %d seconds." % int(math.ceil(timer_time))
def generate_cases(n_outages=10, n_failures=1000, sim=True, full_sim=True): timer_begin = time.clock() timer_start = timer_begin print "[G] start simulation with %d states and %d contingencies." % ( n_outages, n_failures) if full_sim: Ensure(n_outages and n_failures and sim, "can only do full sim if we have everything") clean_files() batch_size = 100 psat = read_psat("rts.m") prob = read_probabilities("rts.net") # create the base cases by sampling for outages # simulate these and print to a file. # it should contain `n_outages` outages. summary_file = open("summary.txt", "w") mismatch_file = open("mismatch.txt", "w") print "Base Stats: mis= %f gen= %f load= %f lim ( %f < X < %f )" % psat.get_stats( ) mismatch_file.write( as_csv("title mismatch gen load min max".split()) + "\n") mismatch_file.write(as_csv(["base"] + list(psat.get_stats())) + "\n") try: if n_outages: outage_batch = make_outage_cases(prob, n_outages) if sim: batch_simulate(outage_batch, psat, batch_size, True, mismatch_file) with open("outage.txt", "w") as result_file: outage_batch.csv_write(result_file) print "-" * 80 print "---- Outage Stats ----" outage_batch.write_stats(sys.stdout) summary_file.write("%s\n" % ("-" * 80)) summary_file.write("Outage Stats\n") summary_file.write("%s\n" % ("-" * 80)) outage_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] outages created in %d seconds." % int( math.ceil(timer_time)) timer_start = time.clock() # do the same for one hour changes to the system. if n_failures: failure_batch = make_failure_cases(prob, n_failures) if sim: batch_simulate(failure_batch, psat, batch_size, True, mismatch_file) with open("failure.txt", "w") as result_file: failure_batch.csv_write(result_file) print "-" * 80 print "---- Failure Stats ----" failure_batch.write_stats(sys.stdout) summary_file.write("Failure Stats\n") summary_file.write("%s\n" % ("-" * 80)) failure_batch.write_stats(summary_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] failures created in %d seconds." % int( math.ceil(timer_time)) timer_start = time.clock() # simulate each of the changes to each base case if full_sim: simulate_cases(outage_batch, failure_batch, psat, summary_file, mismatch_file) timer_end = time.clock() timer_time = (timer_end - timer_start) print "[G] full sim in %d seconds." % int(math.ceil(timer_time)) timer_start = time.clock() finally: timer_end = time.clock() timer_time = (timer_end - timer_begin) print "[G] total time %d seconds." % int(math.ceil(timer_time))