Пример #1
0
def run_parameter_sweep(
    csv_results_file_name=None,
    h5_results_file_name=None,
    seed=None,
    use_LHS=False,
    read_sweep_params_from_file=False,
    sweep_params_fname="mc_sweep_params.yaml",
    read_model_defauls_from_file=False,
    defaults_fname="default_configuration.yaml",
):

    # Set up the solver
    solver = get_solver()

    # Build, set, and initialize the system (these steps will change depending on the underlying model)
    m = build()
    set_operating_conditions(m,
                             water_recovery=0.5,
                             over_pressure=0.3,
                             solver=solver)
    initialize_system(m, solver=solver)

    # Simulate once outside the parameter sweep to ensure everything is appropriately initialized
    solve(m, solver=solver)

    # Check if we need to read in the default model values from a file
    if read_model_defauls_from_file:
        set_defaults_from_yaml(m, defaults_fname)

    # Define the sampling type and ranges for three different variables
    if read_sweep_params_from_file:
        sweep_params = get_sweep_params_from_yaml(m, sweep_params_fname)
    else:
        sweep_params = get_sweep_params(m, use_LHS=use_LHS)

    # Define the outputs to be saved
    outputs = {}
    outputs["EC"] = m.fs.costing.specific_energy_consumption
    outputs["LCOW"] = m.fs.costing.LCOW

    # Run the parameter sweep study using num_samples randomly drawn from the above range
    num_samples = 10

    # Run the parameter sweep
    global_results = parameter_sweep(
        m,
        sweep_params,
        outputs,
        csv_results_file_name=csv_results_file_name,
        h5_results_file_name=h5_results_file_name,
        optimize_function=optimize,
        optimize_kwargs={
            "solver": solver,
            "check_termination": False
        },
        num_samples=num_samples,
        seed=seed,
    )

    return global_results
    def test_set_defaults_from_yaml_error(self, model,
                                          get_default_yaml_file_error):
        m = model
        filename, truth_values = get_default_yaml_file_error

        with pytest.raises(ValueError):
            set_defaults_from_yaml(m, filename)

        os.remove(filename)
    def test_set_defaults_from_yaml(self, model, get_default_yaml_file):
        m = model
        filename, truth_values = get_default_yaml_file

        set_defaults_from_yaml(m, filename)

        for key, truth_value in truth_values.items():
            component = m.find_component(key)
            if component is m.fs.b:  # don't set non-mutable params
                assert value(component) == 2.0
            else:
                assert value(component) == truth_value