def test_economic_loss_comparison_for_system_sanity(self): root_dir = os.path.dirname(os.path.abspath(__file__)) conf_file_path = os.path.join(root_dir, "simulation_setup", "test_scenario_pscoal_600MW.json") config = Configuration(conf_file_path) scenario = Scenario(config) hazards = HazardsContainer(config) infrastructure = ingest_model(config) response_list = calculate_response(hazards, scenario, infrastructure) economic_loss_array = response_list[5] input_pickle_filename \ = os.path.join(root_dir, "historical_data", "economic_loss_for_system_sanity_testing.p") historical_economic_loss_array \ = pickle.load(open(input_pickle_filename, 'rb')) self.assertTrue( np.array_equal(economic_loss_array, historical_economic_loss_array), str(len(economic_loss_array)) + '\n' + str(len(historical_economic_loss_array)))
def test_run_scenario_upper_limit(self): root_dir = os.path.dirname(os.path.abspath(__file__)) conf_file_path = os.path.join(root_dir, "simulation_setup", "test_scenario_upper_limit.json") config = Configuration(conf_file_path) scenario = Scenario(config) hazards = HazardsContainer(config) infrastructure = ingest_model(config) response_list = calculate_response(hazards, scenario, infrastructure) self.assertTrue(int(response_list[4][0][0]) == int(0))
def test_run_scenario_lower_limit(self): root_dir = os.path.dirname(os.path.abspath(__file__)) conf_file_path = os.path.join(root_dir, "simulation_setup", "test_scenario_lower_limit.json") config = Configuration(conf_file_path) scenario = Scenario(config) hazards = HazardsContainer(config) infrastructure = ingest_model(config) response_list = calculate_response(hazards, scenario, infrastructure) output_node_capacity = 0 with open(config.SYS_CONF_FILE, 'r') as f: json_infra_model = json.load(f) output_node_capacity \ = json_infra_model\ ["sysout_setup"]["output_node"]["output_node_capacity"] self.assertTrue( int(response_list[4][0][0]) == int(output_node_capacity))
def test_compare_economic_loss_for_existing_models(self): print("\n\n>> Initiating comparison with existing " "models...\n") conf_file_paths = [] root_dir = os.path.dirname(os.path.abspath(__file__)) for root, dir_names, file_names in os.walk(root_dir): for file_name in file_names: if "tests" in root: if "simulation_setup" in root: # adjusted to keep track of old values in old branches if 'test_scenario_pscoal_600MW' not in file_name and \ 'test_setup' not in file_name: conf_file_paths.append( os.path.join(root, file_name)) for conf_file_path in conf_file_paths: if os.path.isfile(conf_file_path): print("\nMatching results for : " + os.path.basename(conf_file_path)) config = Configuration(conf_file_path) scenario = Scenario(config) hazards = HazardsContainer(config) infrastructure = ingest_model(config) response_list = calculate_response(hazards, scenario, infrastructure) economic_loss_of_model = response_list[5] pickle_flename = os.path.join( root_dir, 'historical_data', "economic_loss_for_" + config.SCENARIO_NAME + '.p') history_economic_loss_for_model \ = pickle.load(open(pickle_flename, 'rb')) self.assertTrue( np.array_equal(economic_loss_of_model, history_economic_loss_for_model), conf_file_path) print("OK")
def main(): parser = argparse.ArgumentParser() parser.add_argument("-s", "--setup", type=str, help="Setup file for simulation scenario, and \n" "locations of inputs, outputs, and system model.") parser.add_argument("-v", "--verbose", type=str, help="Choose option for logging level from: \n" "DEBUG, INFO, WARNING, ERROR, CRITICAL.") args = parser.parse_args() level = logging.DEBUG if args.verbose is not None: if args.verbose.upper() == "DEBUG": level = logging.DEBUG elif args.verbose.upper() == "INFO": level = logging.INFO elif args.verbose.upper() == "WARNING": level = logging.WARNING elif args.verbose.upper() == "ERROR": level = logging.ERROR elif args.verbose.upper() == "CRITICAL": level = logging.CRITICAL rootLogger.set_log_level(level) if args.setup is not None: rootLogger.info('Simulation initiated...') # --------------------------------------------------------------------- # Configure simulation model. # Read data and control parameters and construct objects. config = Configuration(args.setup) scenario = Scenario(config) hazards = HazardsContainer(config) infrastructure = ingest_model(config) # --------------------------------------------------------------------- # Run simulation. # Get the results of running a simulation # # response_list = [ # {}, # hazard level vs component damage state index # {}, # hazard level vs infrastructure output # {}, # hazard level vs component response # {}, # hazard level vs component type response # [], # array of infrastructure output for each sample # []] # array infrastructure econ loss for each sample response_list = calculate_response(hazards, scenario, infrastructure) # --------------------------------------------------------------------- # Post simulation processing. # After the simulation has run the results are aggregated, saved # and the system fragility is calculated. write_system_response(response_list, infrastructure, scenario, hazards) economic_loss_array = response_list[5] plot_mean_econ_loss(scenario, economic_loss_array, hazards) if config.HAZARD_INPUT_METHOD == "hazard_array": pe_by_component_class(response_list, infrastructure, scenario, hazards) # --------------------------------------------------------------------- # Visualizations # Construct visualization for system topology sys_topology_view = SystemTopology(infrastructure, scenario) sys_topology_view.draw_sys_topology(viewcontext="as-built") # --------------------------------------------------------------------- else: print("Input file not found: " + str(args.setup)) rootLogger.info('End')