def test_fd_skip_keys(self): prob = Problem() root = prob.root = Group() comp = Component() comp.add_param('x', 0.) comp.add_param('y', 0.) comp.add_output('z', 0.) comp.solve_nonlinear = lambda p, u, r: u.__setitem__('z', 1.) comp._get_fd_params = lambda: ['x'] comp.jacobian = lambda a, b, c: {('z', 'x'): 0.} root.add('comp', comp, promotes=['x', 'y']) root.add('px', ParamComp('x', 0.), promotes=['*']) root.add('py', ParamComp('y', 0.), promotes=['*']) prob.setup(check=False) prob.run() try: prob.check_partial_derivatives(out_stream=None) except KeyError as err: self.fail('KeyError raised: {0}'.format(str(err)))
def test_fd_skip_keys(self): prob = Problem() root = prob.root = Group() comp = Component() comp.add_param('x', 0.) comp.add_param('y', 0.) comp.add_output('z', 0.) comp.solve_nonlinear = lambda p, u, r: u.__setitem__('z', 1.) comp._get_fd_params = lambda: ['x'] comp.jacobian = lambda a,b,c: {('z', 'x'): 0.} root.add('comp', comp, promotes=['x', 'y']) root.add('px', ParamComp('x', 0.), promotes=['*']) root.add('py', ParamComp('y', 0.), promotes=['*']) prob.setup(check=False) prob.run() try: prob.check_partial_derivatives() except KeyError as err: self.fail('KeyError raised: {0}'.format(str(err)))
def test_bad_size(self): class BadComp(SimpleArrayComp): def jacobian(self, params, unknowns, resids): """Analytical derivatives""" J = {} J[('y', 'x')] = np.zeros((3, 3)) return J prob = Problem() prob.root = Group() prob.root.add('comp', BadComp()) prob.root.add('p1', ParamComp('x', np.ones([2]))) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() try: data = prob.check_partial_derivatives(out_stream=None) except Exception as err: msg = "Jacobian in component 'comp' between the" + \ " variables 'x' and 'y' is the wrong size. " + \ "It should be 2 by 2" self.assertEqual(str(err), msg) else: self.fail("Error expected")
def test_bad_size(self): class BadComp(SimpleArrayComp): def jacobian(self, params, unknowns, resids): """Analytical derivatives""" J = {} J[('y', 'x')] = np.zeros((3, 3)) return J prob = Problem() prob.root = Group() prob.root.add('comp', BadComp()) prob.root.add('p1', IndepVarComp('x', np.ones([2]))) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() try: data = prob.check_partial_derivatives(out_stream=None) except Exception as err: msg = "derivative in component 'comp' of 'y' wrt 'x' is the wrong size. It should be (2, 2), but got (3, 3)" # Good ole Numpy raised_error = str(err) raised_error = raised_error.replace('3L', '3') self.assertEqual(raised_error, msg) else: self.fail("Error expected")
def test_problem_deriv_test(self): prob = Problem() prob.root = Group() prob.root.add('comp', SimpleCompWrongDeriv()) prob.root.add('p1', IndepVarComp('x', 2.0)) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) try: problem_derivatives_check(self, prob) except AssertionError as err: self.assertIn("not less than or equal to 1e-05", err.args[0])
def test_double_diamond_model(self): prob = Problem() prob.root = ConvergeDivergeGroups() prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][2], 0.0, 1e-5)
def test_big_boy_Jacobian(self): class MyComp(Component): def __init__(self, multiplier=2.0): super(MyComp, self).__init__() # Params self.add_param('x1', 3.0) self.add_param('x2', 5.0) # Unknowns self.add_output('y', 5.5) def solve_nonlinear(self, params, unknowns, resids): """ Doesn't do much. """ unknowns['y'] = 3.0*params['x1'] + 4.0*params['x2'] def jacobian(self, params, unknowns, resids): """Intentionally left out x2 derivative.""" J = {} J[('y', 'x1')] = np.array([[3.0]]) return J prob = Problem() prob.root = Group() prob.root.add('comp', MyComp()) prob.root.add('p1', IndepVarComp('x1', 3.0)) prob.root.add('p2', IndepVarComp('x2', 5.0)) prob.root.connect('p1.x1', 'comp.x1') prob.root.connect('p2.x2', 'comp.x2') prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) self.assertTrue(('y', 'x1') in data['comp']) self.assertTrue(('y', 'x2') in data['comp'])
def test_simple_implicit(self): prob = Problem() prob.root = Group() prob.root.add('comp', SimpleImplicitComp()) prob.root.add('p1', ParamComp('x', 0.5)) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][2], 0.0, 1e-5)
def test_simple_array_model(self): prob = Problem() prob.root = Group() prob.root.add('comp', SimpleArrayComp()) prob.root.add('p1', IndepVarComp('x', np.ones([2]))) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-5) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][0], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][1], 0.0, 1e-5) assert_rel_error(self, val2['rel error'][2], 0.0, 1e-5)
def test_simple_array_model2(self): prob = Problem() prob.root = Group() comp = prob.root.add('comp', ExecComp('y = mat.dot(x)', x=np.zeros((2,)), y=np.zeros((2,)), mat=np.array([[2.,7.],[5.,-3.]]))) p1 = prob.root.add('p1', ParamComp('x', np.ones([2]))) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) assert_rel_error(self, data['comp'][('y','x')]['abs error'][0], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['abs error'][1], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['abs error'][2], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['rel error'][0], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['rel error'][1], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['rel error'][2], 0.0, 1e-5)
def test_simple_array_model(self): prob = Problem() prob.root = Group() prob.root.add('comp', ExecComp(['y[0]=2.0*x[0]+7.0*x[1]', 'y[1]=5.0*x[0]-3.0*x[1]'], x=np.zeros([2]), y=np.zeros([2]))) prob.root.add('p1', ParamComp('x', np.ones([2]))) prob.root.connect('p1.x', 'comp.x') prob.setup(check=False) prob.run() data = prob.check_partial_derivatives(out_stream=None) assert_rel_error(self, data['comp'][('y','x')]['abs error'][0], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['abs error'][1], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['abs error'][2], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['rel error'][0], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['rel error'][1], 0.0, 1e-5) assert_rel_error(self, data['comp'][('y','x')]['rel error'][2], 0.0, 1e-5)
def test_basic(self): prob = Problem() prob.root = Group() prob.root.add('src', SrcComp()) prob.root.add('tgtF', TgtCompF()) prob.root.add('tgtC', TgtCompC()) prob.root.add('tgtK', TgtCompK()) prob.root.add('px1', IndepVarComp('x1', 100.0), promotes=['x1']) prob.root.connect('x1', 'src.x1') prob.root.connect('src.x2', 'tgtF.x2') prob.root.connect('src.x2', 'tgtC.x2') prob.root.connect('src.x2', 'tgtK.x2') prob.setup(check=False) prob.run() assert_rel_error(self, prob['src.x2'], 100.0, 1e-6) assert_rel_error(self, prob['tgtF.x3'], 212.0, 1e-6) assert_rel_error(self, prob['tgtC.x3'], 100.0, 1e-6) assert_rel_error(self, prob['tgtK.x3'], 373.15, 1e-6) # Make sure we don't convert equal units self.assertEqual(prob.root.params.metadata('tgtC.x2').get('unit_conv'), None) indep_list = ['x1'] unknown_list = ['tgtF.x3', 'tgtC.x3', 'tgtK.x3'] J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['tgtF.x3']['x1'][0][0], 1.8, 1e-6) assert_rel_error(self, J['tgtC.x3']['x1'][0][0], 1.0, 1e-6) assert_rel_error(self, J['tgtK.x3']['x1'][0][0], 1.0, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['tgtF.x3']['x1'][0][0], 1.8, 1e-6) assert_rel_error(self, J['tgtC.x3']['x1'][0][0], 1.0, 1e-6) assert_rel_error(self, J['tgtK.x3']['x1'][0][0], 1.0, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='fd', return_format='dict') assert_rel_error(self, J['tgtF.x3']['x1'][0][0], 1.8, 1e-6) assert_rel_error(self, J['tgtC.x3']['x1'][0][0], 1.0, 1e-6) assert_rel_error(self, J['tgtK.x3']['x1'][0][0], 1.0, 1e-6) # Need to clean up after FD gradient call, so just rerun. prob.run() # Make sure check partials handles conversion data = prob.check_partial_derivatives(out_stream=None) for key1, val1 in iteritems(data): for key2, val2 in iteritems(val1): assert_rel_error(self, val2['abs error'][0], 0.0, 1e-6) assert_rel_error(self, val2['abs error'][1], 0.0, 1e-6) assert_rel_error(self, val2['abs error'][2], 0.0, 1e-6) assert_rel_error(self, val2['rel error'][0], 0.0, 1e-6) assert_rel_error(self, val2['rel error'][1], 0.0, 1e-6) assert_rel_error(self, val2['rel error'][2], 0.0, 1e-6)