Exemplo n.º 1
0
def build_model_run(model_run_config):
    """Builds the model run

    Arguments
    ---------
    model_run_config: dict
        A valid model run configuration dict with objects

    Returns
    -------
    `smif.modelrun.ModelRun`
    """
    try:
        builder = ModelRunBuilder()
        builder.construct(model_run_config)
        modelrun = builder.finish()
    except AssertionError as error:
        err_type, err_value, err_traceback = sys.exc_info()
        traceback.print_exception(err_type, err_value, err_traceback)
        err_msg = str(error)
        if err_msg:
            LOGGER.error("An AssertionError occurred (%s) see details above.", err_msg)
        else:
            LOGGER.error("An AssertionError occurred, see details above.")
        exit(-1)

    return modelrun
Exemplo n.º 2
0
def get_model_run(get_model_run_config_data):

    config_data = get_model_run_config_data

    builder = ModelRunBuilder()
    builder.construct(config_data)
    return builder.finish()
Exemplo n.º 3
0
def run_model(args):
    """Runs the model specified in the args.model argument

    """
    model_config = validate_config(args)

    try:
        builder = ModelRunBuilder()
        builder.construct(model_config)
        modelrun = builder.finish()
    except AssertionError as error:
        err_type, err_value, err_traceback = sys.exc_info()
        traceback.print_exception(err_type, err_value, err_traceback)
        err_msg = str(error)
        if len(err_msg) > 0:
            LOGGER.error("An AssertionError occurred (%s) see details above.",
                         err_msg)
        else:
            LOGGER.error("An AssertionError occurred, see details above.")
        exit(-1)

    LOGGER.info("Running model run %s", modelrun.name)
    modelrun.run()

    output_file = args.output_file
    LOGGER.info("Writing results to %s", output_file)
    dump(modelrun.sos_model.results, output_file)
    print("Model run complete")
Exemplo n.º 4
0
    def test_serialize(self, get_model_run_config_data):
        builder = ModelRunBuilder()

        config = get_model_run_config_data

        builder.construct(config)
        model_run = builder.finish()

        config = model_run.as_dict()
        assert config == config
Exemplo n.º 5
0
 def test_run_timesteps(self, get_model_run_config_data):
     """should error that timesteps are empty
     """
     config_data = get_model_run_config_data
     config_data['timesteps'] = []
     builder = ModelRunBuilder()
     builder.construct(config_data)
     model_run = builder.finish()
     store = Mock()
     with raises(ModelRunError) as ex:
         model_run.run(store)
     assert 'No timesteps specified' in str(ex)
Exemplo n.º 6
0
def get_model_run(setup_project_folder, setup_region_data):
    path = setup_project_folder
    water_supply_wrapper_path = str(
        path.join('models', 'water_supply', '__init__.py'))

    config_data = {
        'timesteps': [2010, 2011, 2012],
        'dependencies': [],
        'region_sets': {},
        'interval_sets': {},
        'planning': [],
        'scenario_metadata': [{
            'name': 'raininess',
            'temporal_resolution': 'yearly',
            'spatial_resolution': 'BSOA',
            'units': 'ml'
        }],
        'scenario_data': {
            "raininess": [{
                'year': 2010,
                'value': 3,
                'region': 'oxford',
                'interval': 1
            }, {
                'year': 2011,
                'value': 5,
                'region': 'oxford',
                'interval': 1
            }, {
                'year': 2012,
                'value': 1,
                'region': 'oxford',
                'interval': 1
            }]
        },
        "sector_model_data": [{
            "name": "water_supply",
            "path": water_supply_wrapper_path,
            "classname": "WaterSupplySectorModel",
            "inputs": [],
            "outputs": [],
            "initial_conditions": [],
            "interventions": [],
            "parameters": []
        }]
    }

    builder = ModelRunBuilder()
    builder.construct(config_data)
    return builder.finish()
Exemplo n.º 7
0
    def test_builder(self, get_model_run_config_data):

        config_data = get_model_run_config_data

        builder = ModelRunBuilder()
        builder.construct(config_data)

        modelrun = builder.finish()

        assert modelrun.name == 'unique_model_run_name'
        assert modelrun.timestamp == '2017-09-20T12:53:23+00:00'
        assert modelrun.model_horizon == [2010, 2011, 2012]
        assert modelrun.status == 'Built'
        assert modelrun.scenarios == {'raininess': 'high_raininess'}
        assert modelrun.narratives == config_data['narratives']
Exemplo n.º 8
0
    def test_builder(self, get_model_runconfig_data):

        config_data = get_model_runconfig_data

        builder = ModelRunBuilder()
        builder.construct(config_data)

        modelrun = builder.finish()

        assert isinstance(modelrun.sos_model, SosModel)

        assert modelrun.name == ''
        assert modelrun.model_horizon == [2010, 2011, 2012]
        assert modelrun.status == 'Built'
        assert modelrun.strategies is None
        assert modelrun.narratives is None