Exemplo n.º 1
0
    def test_parameter_sweep_recover(self, model):
        comm, rank, num_procs = _init_mpi()

        m = model
        m.fs.slack_penalty = 1000.
        m.fs.slack.setub(0)

        sweep_params = {
            'input_a': (m.fs.input['a'], 0.1, 0.9, 3),
            'input_b': (m.fs.input['b'], 0.0, 0.5, 3)
        }
        outputs = {
            'output_c': m.fs.output['c'],
            'output_d': m.fs.output['d'],
            'performance': m.fs.performance,
            'objective': m.objective
        }
        # Call the parameter_sweep function
        parameter_sweep(
            m,
            sweep_params,
            outputs,
            results_file='pytest_output/global_results_recover.csv',
            optimize_fct=_optimization,
            reinitialize_fct=_reinitialize,
            reinitialize_kwargs={'slack_penalty': 10.},
            mpi_comm=comm)

        # Check that the global results file is created
        assert os.path.isfile('pytest_output/global_results_recover.csv')

        if rank == 0:
            # Attempt to read in the data
            data = np.genfromtxt('pytest_output/global_results_recover.csv',
                                 skip_header=1,
                                 delimiter=',')

            # Compare the last row of the imported data to truth
            truth_data = [
                0.9, 0.5, 1.0, 1.0, 2.0,
                2.0 - 10. * ((2. * 0.9 - 1.) + (3. * 0.5 - 1.))
            ]
            assert np.allclose(data[-1], truth_data, equal_nan=True)
Exemplo n.º 2
0
    def test_parameter_sweep(self, model):
        comm, rank, num_procs = _init_mpi()

        m = model
        m.fs.slack_penalty = 1000.
        m.fs.slack.setub(0)

        sweep_params = {
            'input_a': (m.fs.input['a'], 0.1, 0.9, 3),
            'input_b': (m.fs.input['b'], 0.0, 0.5, 3)
        }
        outputs = {
            'output_c': m.fs.output['c'],
            'output_d': m.fs.output['d'],
            'performance': m.fs.performance
        }
        # Call the parameter_sweep function
        parameter_sweep(m,
                        sweep_params,
                        outputs,
                        results_file='pytest_output/global_results.csv',
                        optimize_fct=_optimization,
                        debugging_data_dir='pytest_output_debug',
                        mpi_comm=comm)

        # Check that the global results file is created
        assert os.path.isfile('pytest_output/global_results.csv')

        if rank == 0:
            # Check that all local output files have been created
            for k in range(num_procs):
                assert os.path.isfile(
                    'pytest_output_debug/local_results_%03d.csv' % (k))

            # Attempt to read in the data
            data = np.genfromtxt('pytest_output/global_results.csv',
                                 skip_header=1,
                                 delimiter=',')

            # Compare the last row of the imported data to truth
            truth_data = [0.9, 0.5, np.nan, np.nan, np.nan]
            assert np.allclose(data[-1], truth_data, equal_nan=True)
Exemplo n.º 3
0
    def test_build_and_divide_combinations(self):
        comm, rank, num_procs = _init_mpi()

        range_A = [0.0, 10.0]
        range_B = [1.0, 20.0]
        range_C = [2.0, 30.0]

        nn_A = 4
        nn_B = 5
        nn_C = 6

        param_dict = dict()
        param_dict['var_A'] = (None, range_A[0], range_A[1], nn_A)
        param_dict['var_B'] = (None, range_B[0], range_B[1], nn_B)
        param_dict['var_C'] = (None, range_C[0], range_C[1], nn_C)

        local_combo_array, full_combo_array = _build_and_divide_combinations(
            param_dict, rank, num_procs)

        test = np.array_split(full_combo_array, num_procs, axis=0)[rank]

        assert np.shape(full_combo_array)[0] == nn_A * nn_B * nn_C
        assert np.shape(full_combo_array)[1] == 3
        assert np.shape(local_combo_array)[1] == 3

        assert test[-1, 0] == pytest.approx(local_combo_array[-1, 0])
        assert test[-1, 1] == pytest.approx(local_combo_array[-1, 1])
        assert test[-1, 2] == pytest.approx(local_combo_array[-1, 2])

        if rank == 0:
            assert test[0, 0] == pytest.approx(range_A[0])
            assert test[0, 1] == pytest.approx(range_B[0])
            assert test[0, 2] == pytest.approx(range_C[0])

        if rank == num_procs - 1:
            assert test[-1, 0] == pytest.approx(range_A[1])
            assert test[-1, 1] == pytest.approx(range_B[1])
            assert test[-1, 2] == pytest.approx(range_C[1])
Exemplo n.º 4
0
    def test_aggregate_results(self):
        comm, rank, num_procs = _init_mpi()

        # print('Rank %d, num_procs %d' % (rank, num_procs))

        nn = 5
        np.random.seed(1)
        local_results = (rank + 1) * np.random.rand(nn, 2)
        global_values = np.random.rand(nn * num_procs, 4)

        global_results = _aggregate_results(local_results, global_values, comm,
                                            num_procs)

        assert np.shape(global_results)[1] == np.shape(local_results)[1]
        assert np.shape(global_results)[0] == np.shape(global_values)[0]

        if rank == 0:
            assert global_results[0, 0] == pytest.approx(local_results[0, 0])
            assert global_results[0, 1] == pytest.approx(local_results[0, 1])
            assert global_results[-1, 0] == pytest.approx(num_procs *
                                                          local_results[-1, 0])
            assert global_results[-1, 1] == pytest.approx(num_procs *
                                                          local_results[-1, 1])
Exemplo n.º 5
0
    def test_init_mpi(self):
        comm, rank, num_procs = _init_mpi()

        assert type(rank) == int
        assert type(num_procs) == int
        assert 0 <= rank < num_procs