Beispiel #1
0
 def setUp(self):
     self._default = IterativeSolution()
     self._value1 = numpy.array([1.0, 2.0, 3.0])
     self._value2 = numpy.array([4.0, 5.0, 6.0])
     self._step1 = StepSolutionData(value=self._value1, time_point=1.0)
     self._step2 = StepSolutionData(value=self._value2, time_point=1.5)
     self._step3 = StepSolutionData(value=self._value1, time_point=1.5)
     self._step4 = StepSolutionData(value=self._value1, time_point=2.0)
     self._traj1 = TrajectorySolutionData()
     self._traj1.add_solution_data(value=self._value1, time_point=1.0)
     self._traj1.add_solution_data(value=self._value2, time_point=1.5)
     self._traj2 = TrajectorySolutionData()
     self._traj2.add_solution_data(value=self._value1, time_point=1.0)
     self._traj2.add_solution_data(value=self._value1, time_point=1.5)
     self._traj3 = TrajectorySolutionData()
     self._traj3.add_solution_data(value=self._value1, time_point=1.0)
     self._traj3.add_solution_data(value=self._value1, time_point=1.5)
     self._traj3.add_solution_data(value=self._value2, time_point=2.0)
Beispiel #2
0
class FullSolutionTest(NumpyAwareTestCase):
    def setUp(self):
        self._default = IterativeSolution()
        self._value1 = numpy.array([1.0, 2.0, 3.0])
        self._value2 = numpy.array([4.0, 5.0, 6.0])
        self._step1 = StepSolutionData(value=self._value1, time_point=1.0)
        self._step2 = StepSolutionData(value=self._value2, time_point=1.5)
        self._step3 = StepSolutionData(value=self._value1, time_point=1.5)
        self._step4 = StepSolutionData(value=self._value1, time_point=2.0)
        self._traj1 = TrajectorySolutionData()
        self._traj1.add_solution_data(value=self._value1, time_point=1.0)
        self._traj1.add_solution_data(value=self._value2, time_point=1.5)
        self._traj2 = TrajectorySolutionData()
        self._traj2.add_solution_data(value=self._value1, time_point=1.0)
        self._traj2.add_solution_data(value=self._value1, time_point=1.5)
        self._traj3 = TrajectorySolutionData()
        self._traj3.add_solution_data(value=self._value1, time_point=1.0)
        self._traj3.add_solution_data(value=self._value1, time_point=1.5)
        self._traj3.add_solution_data(value=self._value2, time_point=2.0)

    def test_adds_solutions_to_data_storage(self):
        self._default.add_solution(self._traj1)
        self.assertEqual(len(self._default.solutions), 1)
        self._default.add_solution(self._traj2, iteration=0)
        self.assertEqual(len(self._default.solutions), 2)
        self.assertNumpyArrayEqual(self._default.time_points, numpy.array([1.0, 1.5]))

        self.assertIsInstance(self._default.solution(0), TrajectorySolutionData)

    def test_checks_consistency_of_time_points_on_add(self):
        self._default.add_solution(self._traj1)
        self._default.add_solution(self._traj2, iteration=0)
        with warnings.catch_warnings(record=True) as w:
            warnings.filterwarnings("always")
            self._default.add_solution(self._traj3)
            self.assertEqual(len(w), 1)
            self.assertTrue(issubclass(w[-1].category, UserWarning))

    def test_provides_accessor_for_single_solution(self):
        self.assertIsNone(self._default.solution(-1),
                          "In case no solutions are stored, 'None' should always be returned.")

    def test_provides_array_of_all_solution_data_storages(self):
        self.assertEqual(self._default.solutions, [])

    def test_provides_time_points(self):
        self.assertIsNone(self._default.time_points)
    def setUp(self):
        self._default = TrajectorySolutionData()

        self._element1 = StepSolutionData(
            value=numpy.array([1.0, 2.0]),
            time_point=0.0,
            error=Error(numpy.array([1.0])),
            residual=Residual(numpy.array([0.5])),
        )
        self._element2 = StepSolutionData(
            value=numpy.array([1.0, 2.0]),
            time_point=0.5,
            error=Error(numpy.array([1.0])),
            residual=Residual(numpy.array([0.5])),
        )
class TrajectorySolutionDataTest(NumpyAwareTestCase):
    def setUp(self):
        self._default = TrajectorySolutionData()

        self._element1 = StepSolutionData(
            value=numpy.array([1.0, 2.0]),
            time_point=0.0,
            error=Error(numpy.array([1.0])),
            residual=Residual(numpy.array([0.5])),
        )
        self._element2 = StepSolutionData(
            value=numpy.array([1.0, 2.0]),
            time_point=0.5,
            error=Error(numpy.array([1.0])),
            residual=Residual(numpy.array([0.5])),
        )

    def test_solutions_can_be_added(self):
        self._default.add_solution_data(value=numpy.array([1.0, 2.0]), time_point=0.1)
        self.assertEqual(self._default.data.size, 1)
        self.assertNumpyArrayEqual(self._default.time_points, numpy.array([0.1]))
        self.assertNumpyArrayEqual(self._default.values, numpy.array([[1.0, 2.0]]))

        warnings.simplefilter("ignore")  # each of the following tests emits a warning about failed consistency
        self.assertRaises(ValueError, self._default.add_solution_data, value="not numpy.ndarray", time_point=1.0)
        self.assertRaises(
            ValueError, self._default.add_solution_data, value=numpy.array(["not", "object"]), time_point=1.0
        )
        self.assertRaises(
            ValueError,
            self._default.add_solution_data,
            value=numpy.array(["is", "StepSolutionData", False], dtype=object),
            time_point=1.0,
        )
        self.assertRaises(
            ValueError, self._default.add_solution_data, value=numpy.array([1.0, 2.0, 3.0]), time_point=1.0
        )
        warnings.resetwarnings()

    def test_provides_raw_data(self):
        self.assertNumpyArrayEqual(self._default.data, numpy.zeros(0, dtype=numpy.object))
        with self.assertRaises(AttributeError):
            self._default.data = "read-only"

    def test_provides_values(self):
        self.assertNumpyArrayEqual(self._default.values, numpy.zeros(0, dtype=numpy.object))
        with self.assertRaises(AttributeError):
            self._default.values = "read-only"

    def test_provides_time_points(self):
        self.assertNumpyArrayEqual(self._default.time_points, numpy.zeros(0, dtype=numpy.float))
        with self.assertRaises(AttributeError):
            self._default.time_points = "read-only"

    def test_provides_errors(self):
        self.assertNumpyArrayEqual(self._default.errors, numpy.zeros(0, dtype=numpy.object))
        with self.assertRaises(AttributeError):
            self._default.errors = "read-only"

    def test_provides_residuals(self):
        self.assertNumpyArrayEqual(self._default.residuals, numpy.zeros(0, dtype=numpy.object))
        with self.assertRaises(AttributeError):
            self._default.residuals = "read-only"

    def test_is_iterable(self):
        self._default.append(self._element1)
        self.assertTrue(self._element1 in self._default)
        self.assertFalse(self._element2 in self._default)
        self._default.append(self._element2)

        self.assertEqual(len(self._default), 2)
        self.assertEqual(self._default[0], self._element1)
        self.assertEqual(self._default[1], self._element2)

        self._default[1.0] = numpy.array([0.0, 0.5])
        self.assertNumpyArrayEqual(self._default[2].value, numpy.array([0.0, 0.5]))

        for elem in self._default:
            self.assertIsInstance(elem, StepSolutionData)