Example #1
0
import os
import unittest
import json
from collections import OrderedDict
from sifra.logger import rootLogger, logging
rootLogger.set_log_level(logging.CRITICAL)


class TestReadingInfrastructureModelJsonFile(unittest.TestCase):
    def setUp(self):

        self.required_headers = [
            "component_list", "node_conn_df", "sysinp_setup", "sysout_setup"
        ]
        self.required_component_headers = [
            "component_class", "component_type", "cost_fraction",
            "node_cluster", "node_type", "operating_capacity", "longitude",
            "latitude", "damages_states_constructor"
        ]

        self.project_root_dir = os.path.dirname(
            os.path.dirname(os.path.abspath(__file__)))
        self.model_json_files = []

        for root, dir_names, file_names in os.walk(self.project_root_dir):
            for file_name in file_names:
                if "models" in root:
                    if "config" not in root:
                        if ".json" in file_name:
                            self.model_json_files.append(
                                os.path.join(root, file_name))
Example #2
0
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')
Example #3
0
import unittest
import os
from sifra.logger import rootLogger
from os.path import exists
from sifra.configuration import Configuration
import logging
rootLogger.set_log_level(logging.INFO)


class TestInputConfFile(unittest.TestCase):
    def setUp(self):

        self.conf_file_paths = []

        root = os.path.join(os.getcwd(), 'simulation_setup')
        for root, dir_names, file_names in os.walk(root):
            for file_name in file_names:
                if file_name.endswith('.json'):
                    if 'simulation_setup' in root:
                        conf_file_path = os.path.join(root, file_name)
                        self.conf_file_paths.append(conf_file_path)

        self.confs = []
        for conf_file_path in self.conf_file_paths:
            conf = Configuration(conf_file_path)
            self.confs.append(conf)

    def test_dose_file_exist(self):
        for conf_file_path in self.conf_file_paths:
            self.assertEqual(exists(conf_file_path), True)