def test_unfix_all(self): # Test with leaf nodes adder = Adder(n=3, x0=[1, 2, 3], dof_names=['x', 'y', 'z'], dof_fixed=[True, False, False]) iden = Identity(x=10, dof_fixed=True) adder_x = adder.x iden_x = iden.x self.assertEqual(len(adder_x), 2) self.assertEqual(adder.dof_size, 2) self.assertAlmostEqual(adder_x[0], 2) self.assertAlmostEqual(adder_x[1], 3) self.assertEqual(len(iden_x), 0) with self.assertRaises(ValueError): iden.x = [10] with self.assertRaises(ValueError): adder.x = [4, 5, 6] iden.unfix_all() adder.unfix_all() iden.x = [10] adder.x = [4, 5, 6] self.assertEqual(iden.dof_size, 1) self.assertEqual(adder.dof_size, 3) self.assertAlmostEqual(adder._dofs.loc['x', '_x'], 4) self.assertAlmostEqual(adder._dofs.loc['y', '_x'], 5) self.assertAlmostEqual(adder._dofs.loc['z', '_x'], 6) self.assertAlmostEqual(iden._dofs.loc['x0', '_x'], 10) # Check with Optimizable objects containing parents adder = Adder(n=3, x0=[1, 2, 3], dof_names=['x', 'y', 'z'], dof_fixed=[True, False, False]) iden = Identity(x=10, dof_fixed=True) test_obj = OptClassWithParents(10, opts_in=[iden, adder]) with self.assertRaises(ValueError): test_obj.x = np.array([20, 4, 5, 6, 25]) adder.unfix_all() test_obj.x = np.array([4, 5, 6, 25]) self.assertAlmostEqual(test_obj._dofs.loc['val', '_x'], 25) self.assertAlmostEqual(adder._dofs.loc['x', '_x'], 4) self.assertAlmostEqual(adder._dofs.loc['y', '_x'], 5) self.assertAlmostEqual(adder._dofs.loc['z', '_x'], 6) iden.unfix_all() test_obj.x = np.array([1, 1, 2, 3, 10]) self.assertAlmostEqual(iden._dofs.loc['x0', '_x'], 1) self.assertAlmostEqual(adder._dofs.loc['x', '_x'], 1) self.assertAlmostEqual(adder._dofs.loc['y', '_x'], 2) self.assertAlmostEqual(adder._dofs.loc['z', '_x'], 3) self.assertAlmostEqual(test_obj._dofs.loc['val', '_x'], 10)
def test_call(self): # Test for leaf nodes adder = Adder(n=3, x0=[1, 2, 3], dof_names=['x', 'y', 'z'], dof_fixed=[True, False, False]) self.assertAlmostEqual(adder(), 6.0) adder.fix('y') self.assertAlmostEqual(adder(), 6.0) iden = Identity(x=10, dof_fixed=True) self.assertAlmostEqual(iden(), 10.0) # Set dofs and call adder.x = [6] self.assertAlmostEqual(adder(), 9.0) adder.unfix_all() adder.x = [4, 5, 6] self.assertAlmostEqual(adder(), 15.0) iden.unfix_all() iden.x = [20] self.assertAlmostEqual(iden(), 20.0) # Call with arguments self.assertAlmostEqual(adder(x=[10, 11, 12]), 33) self.assertAlmostEqual(iden(x=[20]), 20) # Now call without arguments to make sure the previous value is returned self.assertAlmostEqual(adder(), 33) self.assertAlmostEqual(iden(), 20) # Fix dofs and now call adder.fix('x') self.assertAlmostEqual(adder([1, 2]), 13) adder.fix_all() self.assertAlmostEqual(adder(), 13) iden.fix_all() self.assertAlmostEqual(iden(), 20) # Check with Optimizable objects containing parents adder = Adder(n=3, x0=[1, 2, 3], dof_names=['x', 'y', 'z'], dof_fixed=[True, False, False]) iden = Identity(x=10, dof_fixed=True) test_obj1 = OptClassWithParents(20, opts_in=[iden, adder]) # Value returned by test_obj1 is (val + 2*iden())/(10.0 + adder()) self.assertAlmostEqual(test_obj1(), 2.5) # Set the parents nodes' x and call adder.x = [4, 5] self.assertAlmostEqual(test_obj1(), 2.0) # Set the dofs and call test_obj1.x = np.array([14, 15, 30]) self.assertAlmostEqual(test_obj1(), 1.25) # Set only the node local dofs and call test_obj1.local_x = [20] self.assertAlmostEqual(test_obj1(), 1.0) # Call with arguments self.assertAlmostEqual(test_obj1([2, 3, 20]), 2.5) # Followed by Call with no arguments self.assertAlmostEqual(test_obj1(), 2.5)