Beispiel #1
0
class TestInterface(unittest.TestCase):
    def setUp(self):
        self.model_part_size = model_part_size = 3
        self.parameters = {
            'interface_a': [{
                'model_part': 'mp1',
                'variables': ['pressure', 'traction']
            }, {
                'model_part': 'mp2',
                'variables': ['density']
            }],
            'interface_b': [{
                'model_part': 'mp1',
                'variables': ['pressure', 'displacement']
            }, {
                'model_part': 'mp2',
                'variables': ['density']
            }],
            'interface_c': [{
                'model_part': 'mp1',
                'variables': ['pressure', 'traction']
            }, {
                'model_part': 'mp3',
                'variables': ['density']
            }],
            'interface_d': [{
                'model_part': 'mp2',
                'variables': ['density']
            }, {
                'model_part': 'mp1',
                'variables': ['pressure', 'displacement']
            }],
        }

        self.model = Model()
        self.x0 = x0 = np.random.rand(3 * model_part_size)
        self.y0 = y0 = np.random.rand(3 * model_part_size)
        self.z0 = z0 = np.random.rand(3 * model_part_size)
        self.ids = ids = np.arange(0, model_part_size)
        np.random.shuffle(ids)
        self.model.create_model_part('mp1', x0[0:model_part_size],
                                     y0[0:model_part_size],
                                     z0[0:model_part_size], ids)
        self.model.create_model_part('mp2',
                                     x0[model_part_size:2 * model_part_size],
                                     y0[model_part_size:2 * model_part_size],
                                     z0[model_part_size:2 * model_part_size],
                                     ids)
        self.model.create_model_part(
            'mp3', x0[2 * model_part_size:3 * model_part_size],
            y0[2 * model_part_size:3 * model_part_size],
            z0[2 * model_part_size:3 * model_part_size], ids)

        self.interface = Interface(self.parameters['interface_a'], self.model)
        self.scalar_size = 1
        self.vector_size = 3
        self.pressure = np.random.rand(model_part_size, self.scalar_size)
        self.traction = np.random.rand(model_part_size, self.vector_size)
        self.temperature = np.random.rand(model_part_size, self.scalar_size)
        self.density = np.random.rand(model_part_size, self.scalar_size)
        self.interface_data = np.random.rand(model_part_size * 5)

    def test_instantiation(self):
        parameters = {'mp1': ['pressure']}
        self.assertRaises(TypeError, Interface, parameters, self.model)
        parameters = [{'model_part': 'mp1', 'variables': ['pressure']}, 2]
        self.assertRaises(TypeError, Interface, parameters, self.model)
        parameters = [{'model_part': 'mp1'}]
        self.assertRaises(KeyError, Interface, parameters, self.model)
        parameters = [{
            'model_part': 'mp1',
            'variables': ['pressure'],
            'extra': 2
        }]
        self.assertRaises(KeyError, Interface, parameters, self.model)
        parameters = [{'model_part': 'mp1', 'variables': 'pressure'}]
        self.assertRaises(TypeError, Interface, parameters, self.model)

        self.assertEqual(self.interface.parameters,
                         self.parameters['interface_a'])

    def test_properties(self):
        # check model_part_variable_pairs() method
        pairs_a = self.interface.model_part_variable_pairs
        pairs_b = self.interface.model_part_variable_pairs
        self.assertFalse(pairs_a is pairs_b)
        self.assertEqual(str(pairs_a), str(pairs_b))

        # check parameters() method
        parameters_bis = self.interface.parameters
        self.assertFalse(parameters_bis is self.parameters['interface_a'])
        self.assertEqual(str(parameters_bis),
                         str(self.parameters['interface_a']))

        # check model() method
        model_bis = self.interface.model
        self.assertTrue(model_bis is self.model)

    def test_model_part_variable_pairs(self):
        ref_result = [('mp1', 'pressure'), ('mp1', 'traction'),
                      ('mp2', 'density')]
        self.assertListEqual(ref_result,
                             self.interface.model_part_variable_pairs)

    def test_size(self):
        self.assertEqual(self.interface.size, 5 * self.model_part_size)

    def test_attribute_change(self):
        with self.assertRaises(AttributeError):
            self.interface.model_part_variable_pairs = ()
        with self.assertRaises(AttributeError):
            parameters = {'mp1': ['pressure']}
            self.interface.parameters = parameters
        with self.assertRaises(AttributeError):
            self.interface.size = 0

    def test_copy(self):
        self.interface.set_interface_data(self.interface_data)
        interface_copy = self.interface.copy()
        self.assertIsNot(self.interface, interface_copy)
        # interface_copy has the same values
        self.assertEqual(self.interface.model_part_variable_pairs,
                         interface_copy.model_part_variable_pairs)
        np.testing.assert_array_equal(self.interface.get_interface_data(),
                                      interface_copy.get_interface_data())
        # interface_copy not affected by change in self.interface
        interface_data = self.interface.get_interface_data()
        self.interface.set_interface_data(interface_data * 2)
        np.testing.assert_array_equal(interface_copy.get_interface_data(),
                                      interface_data)

    def test_get_variable_data(self):
        self.assertRaises(KeyError, self.interface.get_variable_data, 'mp2',
                          'pressure')

        model_part_variable_pair = ('mp1', 'pressure')
        variable_data = self.pressure
        # returns copy
        self.interface.set_variable_data(*model_part_variable_pair,
                                         variable_data)
        variable_data = self.interface.get_variable_data(
            *model_part_variable_pair)
        variable_data *= 2
        np.testing.assert_array_equal(
            self.interface.get_variable_data(*model_part_variable_pair),
            variable_data / 2)

    def test_set_variable_data(self):
        self.interface.set_variable_data('mp1', 'pressure', self.pressure)
        self.interface.set_variable_data('mp1', 'traction', self.traction)
        self.interface.set_variable_data('mp2', 'density', self.density)
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp1']['pressure'], self.pressure)
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp1']['traction'], self.traction)
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp2']['density'], self.density)
        # input is array with correct shape
        self.assertRaises(ValueError, self.interface.set_variable_data, 'mp1',
                          'pressure', self.traction)
        self.assertRaises(ValueError, self.interface.set_variable_data, 'mp1',
                          'pressure', self.pressure.flatten())
        self.assertRaises(ValueError, self.interface.set_variable_data, 'mp1',
                          'pressure', list(self.pressure))
        # copy is made of input
        self.interface.set_variable_data('mp1', 'pressure', self.pressure)
        self.pressure *= 2
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp1']['pressure'],
            self.pressure / 2)

    def test_get_interface_data(self):
        # output has correct size
        self.assertEqual(self.interface.get_interface_data().size,
                         self.interface.size)
        # correct output from variable data
        self.interface.set_variable_data('mp1', 'pressure', self.pressure)
        self.interface.set_variable_data('mp1', 'traction', self.traction)
        self.interface.set_variable_data('mp2', 'density', self.density)
        interface_data = np.concatenate(
            (self.pressure.flatten(), self.traction.flatten(),
             self.density.flatten()))
        np.testing.assert_equal(self.interface.get_interface_data(),
                                interface_data)
        # correct output from interface data
        self.interface.set_interface_data(self.interface_data)
        np.testing.assert_equal(self.interface.get_interface_data(),
                                self.interface_data)
        # returns copy
        self.interface.get_interface_data() * 2
        np.testing.assert_equal(self.interface.get_interface_data(),
                                self.interface_data)

    def test_set_interface_data(self):
        self.interface.set_interface_data(self.interface_data)
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp1']['pressure'].flatten(),
            self.interface_data[:self.scalar_size * self.model_part_size])
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp1']['traction'].flatten(),
            self.interface_data[self.scalar_size *
                                self.model_part_size:(self.scalar_size +
                                                      self.vector_size) *
                                self.model_part_size])
        np.testing.assert_array_equal(
            self.interface._Interface__data['mp2']['density'].flatten(),
            self.interface_data[(self.scalar_size + self.vector_size) *
                                self.model_part_size:])
        # input is array with correct shape
        self.assertRaises(ValueError, self.interface.set_interface_data,
                          self.pressure)
        self.assertRaises(ValueError, self.interface.set_interface_data,
                          self.interface_data.reshape(-1, 1))
        self.assertRaises(ValueError, self.interface.set_interface_data,
                          list(self.interface_data))
        # copy is made of input
        self.interface.set_interface_data(self.interface_data)
        self.interface_data *= 2
        np.testing.assert_array_equal(self.interface.get_interface_data(),
                                      self.interface_data / 2)

    def test_norm(self):
        self.interface.set_interface_data(self.interface_data)
        norm = np.linalg.norm(self.interface_data)
        self.assertEqual(self.interface.norm(), norm)

    def test_has_same_modelparts(self):
        self.interface.set_interface_data(self.interface_data)

        interface_a = self.interface.copy()
        interface_a.set_interface_data(np.random.rand(self.model_part_size *
                                                      5))
        self.assertTrue(self.interface.has_same_model_parts(interface_a))

        interface_b = Interface(self.parameters['interface_b'], self.model)
        interface_b.set_interface_data(self.interface_data)
        self.assertTrue(self.interface.has_same_model_parts(interface_b))

        interface_c = Interface(self.parameters['interface_c'], self.model)
        interface_c.set_interface_data(self.interface_data)
        self.assertFalse(self.interface.has_same_model_parts(interface_c))

        interface_d = Interface(self.parameters['interface_d'], self.model)
        interface_d.set_interface_data(self.interface_data)
        self.assertFalse(self.interface.has_same_model_parts(interface_d))

    def create_test_interfaces(self):
        interface_data1 = np.random.rand(self.interface.size)
        interface_data2 = np.random.rand(self.interface.size)
        interface1 = self.interface.copy()
        interface1.set_interface_data(interface_data1)
        interface2 = self.interface.copy()
        interface2.set_interface_data(interface_data2)
        return interface_data1, interface_data2, interface1, interface2

    def test_eq(self):
        self.interface.set_interface_data(self.interface_data)

        model_part_size = self.model_part_size
        x0 = self.x0
        y0 = self.y0
        z0 = self.z0
        ids = self.ids
        model2 = Model()
        model2.create_model_part('mp1', x0[0:model_part_size],
                                 y0[0:model_part_size], z0[0:model_part_size],
                                 ids)
        model2.create_model_part('mp2',
                                 x0[model_part_size:2 * model_part_size],
                                 y0[model_part_size:2 * model_part_size],
                                 z0[model_part_size:2 * model_part_size], ids)
        interface2 = Interface(self.parameters['interface_a'], model2)
        interface2.set_interface_data(self.interface_data)
        self.assertIsNot(self.interface, interface2)
        self.assertEqual(self.interface, interface2)

        interface3 = Interface(self.parameters['interface_b'], model2)
        interface3.set_interface_data(self.interface_data)
        self.assertNotEqual(self.interface, interface3)

        interface4 = interface2.copy()
        interface_data4 = self.interface_data.copy()
        interface_data4[np.random.randint(interface4.size)] = np.random.rand()
        interface4.set_interface_data(interface_data4)
        self.assertNotEqual(self.interface, interface4)

    def test_add(self):
        interface_data1, interface_data2, interface1, interface2 = self.create_test_interfaces(
        )

        interface_sum = interface1 + interface2
        self.interface.set_interface_data(interface_data1 + interface_data2)
        self.assertEqual(interface_sum, self.interface)
        interface3 = interface1.copy()
        interface3 += interface2
        self.assertEqual(interface3, self.interface)

        for number in (np.random.rand(1), float(np.random.rand(1)),
                       int(10 * np.random.rand(1))):
            interface_sum = interface1 + number
            self.interface.set_interface_data(interface_data1 + number)
            self.assertEqual(interface_sum, self.interface)
            interface_sum = number + interface1
            self.assertEqual(interface_sum, self.interface)
            interface3 = interface1.copy()
            interface3 += number
            self.assertEqual(interface3, self.interface)

        for other in ('a', True):
            with self.assertRaises(TypeError):
                _ = interface1 + other
            with self.assertRaises(TypeError):
                _ = other + interface1
            with self.assertRaises(TypeError):
                interface1 += other

    def test_sub(self):
        interface_data1, interface_data2, interface1, interface2 = self.create_test_interfaces(
        )

        interface_sum = interface1 - interface2
        self.interface.set_interface_data(interface_data1 - interface_data2)
        self.assertEqual(interface_sum, self.interface)
        interface3 = interface1.copy()
        interface3 -= interface2
        self.assertEqual(interface3, self.interface)

        for number in (np.random.rand(1), float(np.random.rand(1)),
                       int(10 * np.random.rand(1))):
            interface_sum = interface1 - number
            self.interface.set_interface_data(interface_data1 - number)
            self.assertEqual(interface_sum, self.interface)
            interface_sum = number - interface1
            self.assertEqual(interface_sum, self.interface)
            interface3 = interface1.copy()
            interface3 -= number
            self.assertEqual(interface3, self.interface)

        for other in ('a', True):
            with self.assertRaises(TypeError):
                _ = interface1 - other
            with self.assertRaises(TypeError):
                _ = other - interface1
            with self.assertRaises(TypeError):
                interface1 -= other

    def test_mul(self):
        interface_data1, interface_data2, interface1, interface2 = self.create_test_interfaces(
        )

        for number in (np.random.rand(1), float(np.random.rand(1)),
                       int(10 * np.random.rand(1))):
            interface_sum = interface1 * number
            self.interface.set_interface_data(interface_data1 * number)
            self.assertEqual(interface_sum, self.interface)
            interface_sum = number * interface1
            self.assertEqual(interface_sum, self.interface)
            interface3 = interface1.copy()
            interface3 *= number
            self.assertEqual(interface3, self.interface)

        for other in ('a', True, interface2):
            with self.assertRaises(TypeError):
                _ = interface1 * other
            with self.assertRaises(TypeError):
                _ = other * interface1
            with self.assertRaises(TypeError):
                interface1 *= other

    def test_truediv(self):
        interface_data1, interface_data2, interface1, interface2 = self.create_test_interfaces(
        )

        for number in (1 - np.random.rand(1), float(1 - np.random.rand(1)),
                       int(10 * (1 - np.random.rand(1)))):
            interface_sum = interface1 / number
            self.interface.set_interface_data(interface_data1 / number)
            self.assertEqual(interface_sum, self.interface)
            with self.assertRaises(TypeError):
                _ = number / interface1
            interface3 = interface1.copy()
            interface3 /= number
            self.assertEqual(interface3, self.interface)

        for other in ('a', True, interface2):
            with self.assertRaises(TypeError):
                _ = interface1 / other
            with self.assertRaises(TypeError):
                _ = other / interface1
            with self.assertRaises(TypeError):
                interface1 /= other
Beispiel #2
0
    def test_predictor(self):
        m = 10
        dz = 3
        a0 = 1
        a1 = 2
        a2 = 3
        a3 = 4
        a4 = 5
        variable = 'area'
        model_part_name = 'wall'
        interface_settings = [{
            'model_part': model_part_name,
            'variables': [variable]
        }]

        # create model and model_part
        model = data_structure.Model()
        ids = np.arange(0, m)
        x0 = np.zeros(m)
        y0 = np.zeros(m)
        z0 = np.arange(0, m * dz, dz)
        model.create_model_part(model_part_name, x0, y0, z0, ids)

        a0_array = np.full((m, 1), a0)

        # create interface
        interface = Interface(interface_settings, model)
        interface.set_variable_data(model_part_name, variable, a0_array)
        interface_as_array = interface.get_interface_data()

        # create predictor
        parameters = {'type': 'predictors.cubic'}
        predictor_cubic = create_instance(parameters)
        predictor_cubic.initialize(interface)

        # a linear relation should be predicted in the same way by linear, quadratic and cubic predictors
        predictor_cubic.initialize_solution_step()
        interface.set_interface_data(a1 * interface_as_array)
        predictor_cubic.update(interface)
        predictor_cubic.finalize_solution_step()
        predictor_cubic.initialize_solution_step()
        interface.set_interface_data(a2 * interface_as_array)
        predictor_cubic.update(interface)
        predictor_cubic.finalize_solution_step()
        predictor_cubic.initialize_solution_step()
        interface.set_interface_data(a3 * interface_as_array)
        predictor_cubic.update(interface)
        predictor_cubic.finalize_solution_step()

        predictor_cubic.initialize_solution_step()
        prediction_linear = predictor_cubic.linear(
            interface).get_interface_data()
        prediction_quadratic = predictor_cubic.quadratic(
            interface).get_interface_data()
        prediction_cubic = predictor_cubic.cubic(
            interface).get_interface_data()
        for i in range(m):
            self.assertAlmostEqual(a4, prediction_linear[i])
            self.assertAlmostEqual(a4, prediction_quadratic[i])
            self.assertAlmostEqual(a4, prediction_cubic[i])

        # rror if no update
        with self.assertRaises(Exception):
            predictor_cubic.initialize_solution_step()
            predictor_cubic.finalize_solution_step()

        # error if updated twice
        with self.assertRaises(Exception):
            predictor_cubic.initialize_solution_step()
            _ = predictor_cubic.predict(interface)
            _ = predictor_cubic.predict(interface)
            predictor_cubic.finalize_solution_step()

        # error if prediction after update
        with self.assertRaises(Exception):
            predictor_cubic.initialize_solution_step()
            _ = predictor_cubic.update(interface)
            _ = predictor_cubic.predict(interface)
            predictor_cubic.finalize_solution_step()