Exemple #1
0
    def test_cyclic_dependencies(self,
                                 get_sos_model_config_with_summed_dependency):
        config_data = get_sos_model_config_with_summed_dependency

        builder = SosModelBuilder()

        with raises(NotImplementedError):
            builder.construct(config_data)
Exemple #2
0
 def test_set_max_iterations(self, get_sos_model_config):
     """Test constructing from single dict config
     """
     config = get_sos_model_config
     config['max_iterations'] = 125
     builder = SosModelBuilder()
     builder.construct(config)
     sos_model = builder.finish()
     assert sos_model.max_iterations == 125
Exemple #3
0
 def test_set_convergence_relative_tolerance(self, get_sos_model_config):
     """Test constructing from single dict config
     """
     config = get_sos_model_config
     config['convergence_relative_tolerance'] = 0.1
     builder = SosModelBuilder()
     builder.construct(config)
     sos_model = builder.finish()
     assert sos_model.convergence_relative_tolerance == 0.1
Exemple #4
0
 def test_set_convergence_absolute_tolerance(self, get_sos_model_config):
     """Test constructing from single dict config
     """
     config = get_sos_model_config
     config['convergence_absolute_tolerance'] = 0.0001
     builder = SosModelBuilder()
     builder.construct(config, [2010, 2011, 2012])
     sos_model = builder.finish()
     assert sos_model.convergence_absolute_tolerance == 0.0001
Exemple #5
0
    def test_load_scenario_models(self, get_sos_model_config):
        config = get_sos_model_config
        builder = SosModelBuilder()
        builder.construct(config, [2010])
        sos_model = builder.finish()
        scenario = sos_model.models['raininess']

        expected = {'raininess': {'raininess': np.array([[3.]])}}

        assert scenario.simulate(2010) == expected
Exemple #6
0
    def test_construct(self, get_sos_model_config):
        """Test constructing from single dict config
        """
        config = get_sos_model_config
        builder = SosModelBuilder()
        builder.construct(config)
        sos_model = builder.finish()

        assert isinstance(sos_model, SosModel)
        assert list(sos_model.sector_models.keys()) == ['water_supply']
        assert isinstance(sos_model.models['water_supply'], SectorModel)
Exemple #7
0
    def test_builder_interventions(self, get_sos_model_config):

        builder = SosModelBuilder()
        config_data = get_sos_model_config
        builder.construct(config_data, config_data['timesteps'])
        sos_model = builder.finish()

        assert sos_model.sector_models == ['water_supply']
        assert sos_model.intervention_names == [
            "water_asset_a", "water_asset_b", "water_asset_c"
        ]
Exemple #8
0
    def test_missing_planning_asset(self, get_sos_model_config):
        config = get_sos_model_config
        config["planning"] = [{
            "name": "test_intervention",
            "build_date": 2012
        }]
        builder = SosModelBuilder()
        builder.construct(config, [2010, 2011, 2012])

        with raises(AssertionError) as ex:
            builder.finish()
        assert "Intervention 'test_intervention' in planning file not found" in str(
            ex.value)
Exemple #9
0
    def test_scenarios(self, get_sos_model_config):
        """Test constructing from single dict config
        """
        config = get_sos_model_config
        builder = SosModelBuilder()
        builder.construct(config, [2010, 2011, 2012])
        sos_model = builder.finish()

        assert isinstance(sos_model, SosModel)
        assert sos_model.sector_models == ['water_supply']
        assert isinstance(sos_model.models['water_supply'], SectorModel)
        assert isinstance(sos_model.models['raininess'], ScenarioModel)

        actual = sos_model.models['raininess']._data
        np.testing.assert_equal(
            actual, np.array([[[3.]], [[5.]], [[1.]]], dtype=float))
Exemple #10
0
    def test_simple_dependency(self, get_sos_model_config_with_dep):

        config_data = get_sos_model_config_with_dep

        builder = SosModelBuilder()
        builder.construct(config_data, config_data['timesteps'])
        sos_model = builder.finish()

        sos_model.check_dependencies()
        graph = sos_model.dependency_graph

        scenario = sos_model.models['raininess']

        assert 'water_supply' in sos_model.models
        assert sos_model.models['water_supply'] in graph.nodes()
        deps = sos_model.models['water_supply'].deps
        assert 'raininess' in deps.keys()
        expected = Dependency(scenario, scenario.model_outputs['raininess'])
        assert deps['raininess'] == expected

        assert 'raininess' in sos_model.models
        assert sos_model.models['raininess'] in graph.nodes()
Exemple #11
0
    def test_scenario_dependency(self, get_sos_model_config,
                                 setup_region_data):
        """Expect successful build with dependency on scenario data

        Should raise error if no spatial or temporal sets are defined
        """
        config = get_sos_model_config
        config["sector_model_data"][0]["inputs"] = [{
            'name': 'raininess',
            'spatial_resolution': 'blobby',
            'temporal_resolution': 'annual',
            'units': 'ml'
        }]

        builder = SosModelBuilder()
        with raises(ValueError):
            builder.construct(config, [2010, 2011, 2012])

        builder.region_register.register(
            RegionSet('blobby', setup_region_data['features']))

        interval_data = [{'id': 'ultra', 'start': 'P0Y', 'end': 'P1Y'}]
        builder.interval_register.register(IntervalSet('mega', interval_data))
        builder.construct(config, [2010, 2011, 2012])
Exemple #12
0
def get_model_run_definition(args):
    """Builds the model run

    Returns
    -------
    dict
        The complete sos_model_run configuration dictionary with contained
        ScenarioModel, SosModel and SectorModel objects

    """
    handler = DatafileInterface(args.directory)
    load_region_sets(handler)
    load_interval_sets(handler)
    load_units(handler)

    try:
        model_run_config = handler.read_sos_model_run(args.modelrun)
    except DataNotFoundError:
        LOGGER.error("Model run %s not found. Run 'smif list' to see available model runs.",
                     args.modelrun)
        exit(-1)

    LOGGER.info("Running %s", model_run_config['name'])
    LOGGER.debug("Model Run: %s", model_run_config)
    sos_model_config = handler.read_sos_model(model_run_config['sos_model'])

    sector_model_objects = []
    for sector_model in sos_model_config['sector_models']:
        sector_model_config = handler.read_sector_model(sector_model)

        absolute_path = os.path.join(args.directory,
                                     sector_model_config['path'])
        sector_model_config['path'] = absolute_path

        intervention_files = sector_model_config['interventions']
        intervention_list = []
        for intervention_file in intervention_files:
            interventions = handler.read_interventions(intervention_file)
            intervention_list.extend(interventions)
        sector_model_config['interventions'] = intervention_list

        initial_condition_files = sector_model_config['initial_conditions']
        initial_condition_list = []
        for initial_condition_file in initial_condition_files:
            initial_conditions = handler.read_initial_conditions(initial_condition_file)
            initial_condition_list.extend(initial_conditions)
        sector_model_config['initial_conditions'] = initial_condition_list

        sector_model_builder = SectorModelBuilder(sector_model_config['name'])
        LOGGER.debug("Sector model config: %s", sector_model_config)
        sector_model_builder.construct(sector_model_config,
                                       model_run_config['timesteps'])
        sector_model_object = sector_model_builder.finish()

        sector_model_objects.append(sector_model_object)
        LOGGER.debug("Model inputs: %s", sector_model_object.inputs.names)

    LOGGER.debug("Sector models: %s", sector_model_objects)
    sos_model_config['sector_models'] = sector_model_objects

    scenario_objects = []
    for scenario_set, scenario_name in model_run_config['scenarios'].items():
        scenario_definition = handler.read_scenario_definition(scenario_name)
        LOGGER.debug("Scenario definition: %s", scenario_definition)

        scenario_model_builder = ScenarioModelBuilder(scenario_set)
        scenario_model_builder.construct(scenario_definition)
        scenario_objects.append(scenario_model_builder.finish())

    LOGGER.debug("Scenario models: %s", [model.name for model in scenario_objects])
    sos_model_config['scenario_sets'] = scenario_objects

    sos_model_builder = SosModelBuilder()
    sos_model_builder.construct(sos_model_config)
    sos_model_object = sos_model_builder.finish()

    LOGGER.debug("Model list: %s", list(sos_model_object.models.keys()))

    model_run_config['sos_model'] = sos_model_object
    narrative_objects = get_narratives(handler,
                                       model_run_config['narratives'])
    model_run_config['narratives'] = narrative_objects

    return model_run_config
Exemple #13
0
 def _add_sos_model(self, config_data):
     """
     """
     builder = SosModelBuilder()
     builder.construct(config_data, self.model_run.model_horizon)
     self.model_run.sos_model = builder.finish()