Exemplo n.º 1
0
    def _model_converged(self, model, data_handle):
        """Check a single model's output for convergence

        Compare data output for each param over recent iterations.

        Parameters
        ----------
        model: :class:`smif.model.Model`
        data_handle: :class:`smif.data_layer.DataHandle`

        Returns
        -------
        bool
            True if converged otherwise, False
        """
        prev_data_handle = DataHandle(
            data_handle._store,
            data_handle._modelrun_name,
            data_handle._current_timestep,
            data_handle._timesteps,
            model,
            self._current_iteration - 1,  # access previous iteration
            data_handle._decision_iteration)
        return all(
            np.allclose(data_handle.get_data(param.name),
                        prev_data_handle.get_data(param.name),
                        rtol=self.relative_tolerance,
                        atol=self.absolute_tolerance)
            for param in model.inputs.metadata)
Exemplo n.º 2
0
    def test_get_data(self, mock_store, mock_model):
        """should allow read access to input data
        """
        data_handle = DataHandle(mock_store, 1, 2015, [2015, 2020], mock_model)
        expected = np.array([[1.0]])
        actual = data_handle.get_data("test")
        assert actual == expected

        mock_store.read_results.assert_called_with(
            1,
            'test_source',  # read from source model
            'test_output',  # using source model output name
            'test_regions',
            'test_intervals',
            2015,
            None,
            None)
Exemplo n.º 3
0
    def test_get_data_with_conversion(self, mock_store,
                                      mock_model_with_conversion):
        """should convert liters to milliliters (1 -> 0.001)
        """
        data_handle = DataHandle(mock_store, 1, 2015, [2015, 2020],
                                 mock_model_with_conversion)
        expected = np.array([[0.001]])
        actual = data_handle.get_data("test")
        assert actual == expected

        mock_store.read_results.assert_called_with(
            1,
            'test_source',  # read from source model
            'test_output',  # using source model output name
            'test_regions',
            'test_intervals',
            2015,
            None,
            None)