Example #1
0
def validate_model(model_json):
    """Validate a UWG model JSON file against the UWG schema.
    \n
    Args:\n
        model_json: Full path to a UWG model JSON file. Note that this will not check if
rural and new .epw file paths are valid since these can be overridden by CLI arguments.
    """
    try:
        assert os.path.isfile(
            model_json), 'No JSON file found at {}.'.format(model_json)

        # validate the Model JSON
        click.echo('Validating Model JSON ...')
        schema_model.UWG.parse_file(model_json)
        click.echo('Pydantic validation passed.')
        with open(model_json) as json_file:
            data = json.load(json_file)
        UWG.from_dict(data)
        click.echo('Python re-serialization passed.')
        click.echo('Congratulations! Your UWG model JSON is valid!')
    except Exception as e:
        print('Model validation failed.\n{}'.format(e))
        sys.exit(1)
    else:
        sys.exit(0)
Example #2
0
def simulate_json_model(model_json, epw_path, new_epw_dir, new_epw_name):
    """Simulate a UWG model from a JSON model file.

    \b
    Args:
        model_json: Full path to a JSON model file.
        epw_path: Full path of the rural .epw file that will be morphed.
    """
    try:
        with open(model_json) as json_file:
            data = json.load(json_file)

        uwg_model = UWG.from_dict(
            data, epw_path=epw_path, new_epw_dir=new_epw_dir, new_epw_name=new_epw_name)
        uwg_model.generate()
        uwg_model.simulate()
        uwg_model.write_epw()
    except Exception as e:
        _logger.exception('UWG model simulation failed.\n{}'.format(e))
        sys.exit(1)
    else:
        sys.exit(0)
Example #3
0
def test_init():
    """Test initialization methods."""

    test_dir = os.path.abspath(os.path.dirname(__file__))
    param_path = os.path.join(test_dir, 'parameters',
                              'initialize_singapore.uwg')
    epw_path = os.path.join(test_dir, 'epw', 'SGP_Singapore.486980_IWEC.epw')

    refBEM, refSch = UWG.load_refDOE()
    refBEM[0][2][0].building.shgc = 0.9
    ref_bem_vec = [refBEM[0][2][0], refBEM[2][2][0]]
    ref_sch_vec = [refSch[0][2][0], refSch[2][2][0]]

    # base init
    UWG(epw_path)

    # from param_file
    UWG.from_param_file(param_path, epw_path)

    # from args
    UWG.from_param_args(bldheight=10.0,
                        blddensity=0.5,
                        vertohor=0.5,
                        zone='1A',
                        treecover=0.1,
                        grasscover=0.1,
                        epw_path=epw_path)
    model = UWG.from_param_args(10.0,
                                0.5,
                                0.5,
                                treecover=0.1,
                                grasscover=0.1,
                                zone='1A',
                                ref_bem_vector=[],
                                ref_sch_vector=[],
                                epw_path=epw_path)
    model.generate()
    assert model.ref_bem_vector == []
    assert model.ref_sch_vector == []

    UWG.from_param_args(10.0,
                        0.5,
                        0.5,
                        0.1,
                        0.1,
                        '1A',
                        ref_bem_vector=ref_bem_vec,
                        ref_sch_vector=ref_sch_vec,
                        epw_path=epw_path)
    with pytest.raises(AssertionError):
        UWG.from_param_args(10.0,
                            0.5,
                            0.5,
                            0.1,
                            0.1,
                            '1A',
                            ref_bem_vector=ref_bem_vec,
                            ref_sch_vector=ref_sch_vec[:1])
    with pytest.raises(AssertionError):
        UWG.from_param_args(10.0,
                            0.5,
                            0.5,
                            0.1,
                            0.1,
                            '1A',
                            ref_bem_vector=None,
                            ref_sch_vector=ref_sch_vec)
    with pytest.raises(Exception):
        # No epw_path
        model = UWG.from_param_args(bldheight=10.0,
                                    blddensity=0.5,
                                    vertohor=0.5,
                                    grasscover=0.1,
                                    treecover=0.1,
                                    zone='1A')
        model.generate()

    # from dict
    data = UWG.from_param_args(10.0, 0.5, 0.5, 0.1, 0.1,
                               '1A').to_dict(include_refDOE=False)
    UWG.from_dict(data)
    model1 = UWG.from_param_args(10.0,
                                 0.5,
                                 0.5,
                                 0.1,
                                 0.1,
                                 '1A',
                                 ref_bem_vector=ref_bem_vec,
                                 ref_sch_vector=ref_sch_vec)
    data = model1.to_dict(include_refDOE=True)
    model2 = UWG.from_dict(data, epw_path=epw_path)
    model2.generate()
    assert model2.ref_bem_vector[0].building.shgc == pytest.approx(0.9,
                                                                   abs=1e-10)
    assert model2.refBEM[0][2][0].building.shgc == pytest.approx(0.9,
                                                                 abs=1e-10)