def test_parab_subbed_Pcomps(self): model = Problem(impl=impl) root = model.root = Group() root.ln_solver = lin_solver() par = root.add('par', ParallelGroup()) par.add('s1', MP_Point(root=2.0)) par.add('s2', MP_Point(root=3.0)) root.add('sumcomp', ExecComp('sum = x1+x2')) root.connect('par.s1.c.y', 'sumcomp.x1') root.connect('par.s2.c.y', 'sumcomp.x2') driver = model.driver = pyOptSparseDriver() driver.options['optimizer'] = OPTIMIZER driver.options['print_results'] = False driver.add_desvar('par.s1.p.x', lower=-100, upper=100) driver.add_desvar('par.s2.p.x', lower=-100, upper=100) driver.add_objective('sumcomp.sum') model.setup(check=False) model.run() if not MPI or self.comm.rank == 0: assert_rel_error(self, model['par.s1.p.x'], 2.0, 1.e-6) if not MPI or self.comm.rank == 1: assert_rel_error(self, model['par.s2.p.x'], 3.0, 1.e-6)
def test_parab_subbed_Pcomps(self): model = Problem(impl=impl) root = model.root = Group() root.ln_solver = lin_solver() par = root.add('par', ParallelGroup()) par.add('s1', MP_Point(root=2.0)) par.add('s2', MP_Point(root=3.0)) root.add('sumcomp', ExecComp('sum = x1+x2')) root.connect('par.s1.c.y', 'sumcomp.x1') root.connect('par.s2.c.y', 'sumcomp.x2') driver = model.driver = pyOptSparseDriver() driver.add_param('par.s1.p.x', low=-100, high=100) driver.add_param('par.s2.p.x', low=-100, high=100) driver.add_objective('sumcomp.sum') model.setup(check=False) model.run() if not MPI or self.comm.rank == 0: assert_rel_error(self, model['par.s1.p.x'], 2.0, 1.e-6) if not MPI or self.comm.rank == 1: assert_rel_error(self, model['par.s2.p.x'], 3.0, 1.e-6)
def test_parab_subbed_Pcomps(self): model = Problem(impl=impl) root = model.root = Group() root.ln_solver = lin_solver() par = root.add("par", ParallelGroup()) par.add("s1", MP_Point(root=2.0)) par.add("s2", MP_Point(root=3.0)) root.add("sumcomp", ExecComp("sum = x1+x2")) root.connect("par.s1.c.y", "sumcomp.x1") root.connect("par.s2.c.y", "sumcomp.x2") driver = model.driver = pyOptSparseDriver() driver.add_param("par.s1.p.x", low=-100, high=100) driver.add_param("par.s2.p.x", low=-100, high=100) driver.add_objective("sumcomp.sum") model.setup(check=False) model.run() if not MPI or self.comm.rank == 0: assert_rel_error(self, model["par.s1.p.x"], 2.0, 1.0e-6) if not MPI or self.comm.rank == 1: assert_rel_error(self, model["par.s2.p.x"], 3.0, 1.0e-6)
def test_bounds_backtracking(self): class SimpleImplicitComp1(Component): """ A Simple Implicit Component with an additional output equation. f(x,z) = xz + z - 4 y = x + 2z Sol: when x = 0.5, z = 2.666 Sol: when x = 2.0, z = 1.333 Coupled derivs: y = x + 8/(x+1) dy_dx = 1 - 8/(x+1)**2 = -2.5555555555555554 z = 4/(x+1) dz_dx = -4/(x+1)**2 = -1.7777777777777777 """ def __init__(self): super(SimpleImplicitComp1, self).__init__() # Params self.add_param('x', 0.5) # Unknowns self.add_output('y', 0.0) # States self.add_state('z', 2.0, lower=1.4, upper=2.5) self.maxiter = 10 self.atol = 1.0e-12 def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" x = params['x'] z = unknowns['z'] resids['z'] = x*z + z - 4.0 # Output equations need to evaluate a residual just like an explicit comp. resids['y'] = x + 2.0*z - unknowns['y'] def linearize(self, params, unknowns, resids): """Analytical derivatives.""" J = {} # Output equation J[('y', 'x')] = np.array([1.0]) J[('y', 'z')] = np.array([2.0]) # State equation J[('z', 'z')] = np.array([params['x'] + 1.0]) J[('z', 'x')] = np.array([unknowns['z']]) return J class SimpleImplicitComp2(Component): """ A Simple Implicit Component with an additional output equation. f(x,z) = xz + z - 4 y = x + 2z Sol: when x = 0.5, z = 2.666 Sol: when x = 2.0, z = 1.333 Coupled derivs: y = x + 8/(x+1) dy_dx = 1 - 8/(x+1)**2 = -2.5555555555555554 z = 4/(x+1) dz_dx = -4/(x+1)**2 = -1.7777777777777777 """ def __init__(self): super(SimpleImplicitComp2, self).__init__() # Params self.add_param('x', 0.5) # Unknowns self.add_output('y', 0.0) # States self.add_state('z', 2.0, lower=1.5, upper=2.5) self.maxiter = 10 self.atol = 1.0e-12 def solve_nonlinear(self, params, unknowns, resids): pass def apply_nonlinear(self, params, unknowns, resids): """ Don't solve; just calculate the residual.""" x = params['x'] z = unknowns['z'] resids['z'] = x*z + z - 4.0 # Output equations need to evaluate a residual just like an explicit comp. resids['y'] = x + 2.0*z - unknowns['y'] def linearize(self, params, unknowns, resids): """Analytical derivatives.""" J = {} # Output equation J[('y', 'x')] = np.array([1.0]) J[('y', 'z')] = np.array([2.0]) # State equation J[('z', 'z')] = np.array([params['x'] + 1.0]) J[('z', 'x')] = np.array([unknowns['z']]) return J #------------------------------------------------------ # Test that Newton doesn't drive it past lower bounds #------------------------------------------------------ top = Problem(impl=impl) top.root = Group() par = top.root.add('par', ParallelGroup()) par.add('comp1', SimpleImplicitComp1()) par.add('comp2', SimpleImplicitComp2()) top.root.ln_solver = lin_solver() top.root.nl_solver = Newton() top.root.nl_solver.options['maxiter'] = 5 top.root.add('px1', IndepVarComp('x', 1.0)) top.root.add('px2', IndepVarComp('x', 1.0)) # TODO: This is to get around a bug in checks. It should be fixed soon. par.ln_solver = lin_solver() top.root.connect('px1.x', 'par.comp1.x') top.root.connect('px2.x', 'par.comp2.x') top.setup(check=False) top['px1.x'] = 2.0 top['px2.x'] = 2.0 top.run() # Comp2 has a tighter lower bound. This test makes sure that we # allgathered and used the lowest alpha. if top.root.par.comp1.is_active(): self.assertEqual(top['par.comp1.z'], 1.5) if top.root.par.comp2.is_active(): self.assertEqual(top['par.comp2.z'], 1.5)
J = {} J['obj', 'x'] = self.A return J if __name__ == '__main__': #from openmdao.test.mpi_util import mpirun_tests #mpirun_tests() if OPT is None: raise unittest.SkipTest("pyoptsparse is not installed") if OPTIMIZER is None: raise unittest.SkipTest("pyoptsparse is not providing SNOPT or SLSQP") size = 100 prob = Problem(impl=impl) root = prob.root = Group() root.add('p', IndepVarComp('x', val=np.ones(shape=(size, )))) root.add('c', LinearSolver(size=size, A=np.eye(size), b=np.arange(0, size))) root.connect('p.x', 'c.x') root.ln_solver = lin_solver() prob.setup() prob.run() err = np.sum(np.abs(prob['c.obj'])) result = prob['p.x'] print(result) print(err)