def test_calc_gradient_multiple_params(self): prob = Problem() prob.root = FanIn() prob.setup(check=False) prob.run() param_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] # check that calc_gradient returns proper dict value when mode is 'fwd' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[-6.]])) # check that calc_gradient returns proper array value when mode is 'fwd' J = prob.calc_gradient(param_list, unknown_list, mode='fwd', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]])) # check that calc_gradient returns proper dict value when mode is 'rev' J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[-6.]])) # check that calc_gradient returns proper array value when mode is 'rev' J = prob.calc_gradient(param_list, unknown_list, mode='rev', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]])) # check that calc_gradient returns proper dict value when mode is 'fd' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='dict') np.testing.assert_almost_equal(J['comp3.y']['p2.x2'], np.array([[35.]])) np.testing.assert_almost_equal(J['comp3.y']['p1.x1'], np.array([[-6.]])) # check that calc_gradient returns proper array value when mode is 'fd' J = prob.calc_gradient(param_list, unknown_list, mode='fd', return_format='array') np.testing.assert_almost_equal(J, np.array([[-6., 35.]]))
def test_fan_in(self): prob = Problem() prob.root = FanIn() prob.root.ln_solver = ScipyGMRES() prob.setup(check=False) prob.run() indep_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)
def test_fan_in(self): prob = Problem() prob.root = FanIn() prob.root.ln_solver = DirectSolver() prob.root.ln_solver.options['jacobian_method'] = 'assemble' prob.setup(check=False) prob.run() indep_list = ['p1.x1', 'p2.x2'] unknown_list = ['comp3.y'] J = prob.calc_gradient(indep_list, unknown_list, mode='fwd', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6) J = prob.calc_gradient(indep_list, unknown_list, mode='rev', return_format='dict') assert_rel_error(self, J['comp3.y']['p1.x1'][0][0], -6.0, 1e-6) assert_rel_error(self, J['comp3.y']['p2.x2'][0][0], 35.0, 1e-6)