示例#1
0
    def test_sellar_Newton_parallel(self):

        top = set_as_top(SellarMDFwithDerivs())
        top.replace('driver', NewtonSolver())

        top.driver.add_parameter('C2.y1', low=-1e99, high=1e99)
        top.driver.add_constraint('C1.y1 = C2.y1')
        top.driver.add_parameter('C1.y2', low=-1.e99, high=1.e99)
        top.driver.add_constraint('C2.y2 = C1.y2')

        expected = { 'C1.y1': 3.1598617768014536, 'C2.y2': 3.7551999159927316 }

        top.driver.iprint = 0
        top.driver.max_iteration = 20
        top.run()
        # print top.C1.y1, top.C2.y1
        # print top.C1.y2, top.C2.y2

        # gather the values back to the rank 0 process and compare to expected
        dist_answers = top._system.mpi.comm.gather([(k[0],v) for k,v in top._system.vec['u'].items()],
                                                   root=0)
        if self.comm.rank == 0:
            for answers in dist_answers:
                for name, val in answers:
                    if name in expected:
                        #print self.comm.rank, name, val[0]
                        assert_rel_error(self, val[0], expected[name], 0.001)
                        del expected[name]

            if expected:
                self.fail("not all expected values were found")
示例#2
0
    def test_initial_run(self):
        class MyComp(Component):

            x = Float(0.0, iotype='in', low=-100000, high=100000)
            xx = Float(0.0, iotype='in', low=-100000, high=100000)
            f_x = Float(iotype='out')
            y = Float(iotype='out')

            def execute(self):
                if self.xx != 1.0:
                    self.raise_exception("Lazy", RuntimeError)
                self.f_x = 2.0 * self.x
                self.y = self.x

        @add_delegate(HasParameters)
        class SpecialDriver(Driver):

            implements(IHasParameters)

            def execute(self):
                self.set_parameters([1.0])

        top = set_as_top(Assembly())
        top.add('comp', MyComp())
        top.add('driver', NewtonSolver())
        top.add('subdriver', SpecialDriver())
        top.driver.workflow.add('subdriver')
        top.subdriver.workflow.add('comp')

        top.subdriver.add_parameter('comp.xx')
        top.driver.add_parameter('comp.x')
        top.driver.add_constraint('comp.y = 1.0')

        top.run()
示例#3
0
    def configure(self):

        self.add('d1', Discipline(prob_size=2))
        self.add('d2', Discipline(prob_size=2))

        self.connect('d1.y_out', 'd2.y_in')
        self.connect('d2.y_out', 'd1.y_in')

        self.add('driver', NewtonSolver())
        self.driver.workflow.add(['d1', 'd2'])
        self.driver.newton = True
示例#4
0
    def configure(self):

        self.add('d1', Discipline(prob_size=2))
        self.add('d2', Discipline(prob_size=2))

        self.connect('d1.y_out', 'd2.y_in')
        #self.connect('d2.y_out', 'd1.y_in')

        self.add('driver', NewtonSolver())
        self.driver.workflow.add(['d1', 'd2'])
        self.driver.add_parameter('d1.y_in', low=-1e99, high=1e99)
        self.driver.add_constraint('d2.y_out = d1.y_in')
示例#5
0
    def configure(self):

        self.add('SysX', SysX())
        self.add('SysY', SysY())
        self.add('SysZ', SysZ())

        self.connect('SysX.x', 'SysY.x')
        self.connect('SysX.x', 'SysZ.x')
        self.connect('SysY.y', 'SysZ.y')
        self.connect('SysZ.z', 'SysX.z')
        self.connect('SysZ.z', 'SysY.z')

        self.add('driver', NewtonSolver())
        self.driver.workflow.add(['SysX', 'SysY', 'SysZ'])
示例#6
0
    def test_newton_nested(self):
        # Make sure derivatives across the newton-solved system are correct.

        top = set_as_top(Assembly())
        top.add('driver', SimpleDriver())

        top.add('d1', Discipline1_WithDerivatives())
        top.d1.x1 = 1.0
        top.d1.y1 = 1.0
        top.d1.y2 = 1.0
        top.d1.z1 = 5.0
        top.d1.z2 = 2.0

        top.add('d2', Discipline2_WithDerivatives())
        top.d2.y1 = 1.0
        top.d2.y2 = 1.0
        top.d2.z1 = 5.0
        top.d2.z2 = 2.0

        top.connect('d1.y1', 'd2.y1')

        top.add('solver', NewtonSolver())
        top.solver.atol = 1e-9
        top.solver.workflow.add(['d1', 'd2'])
        top.solver.add_parameter('d1.y2', low=-1e99, high=1e99)
        top.solver.add_constraint('d1.y2 = d2.y2')

        top.driver.workflow.add(['solver'])
        top.driver.add_parameter('d1.z1', low=-100, high=100)
        top.driver.add_objective('d1.y1 + d1.y2')

        top.run()

        J = top.driver.calc_gradient(mode='forward')
        print J
        assert_rel_error(self, J[0][0], 10.77542099, 1e-5)

        J = top.driver.calc_gradient(mode='adjoint')
        print J
        assert_rel_error(self, J[0][0], 10.77542099, 1e-5)

        top.driver.gradient_options.fd_step = 1e-7
        top.driver.gradient_options.fd_form = 'central'
        J = top.driver.calc_gradient(mode='fd')
        print J
        assert_rel_error(self, J[0][0], 10.77542099, 1e-5)
示例#7
0
    def configure(self):

        self.add('d1', Discipline1_WithDerivatives())
        self.d1.x1 = 1.0
        self.d1.y1 = 1.0
        self.d1.y2 = 1.0
        self.d1.z1 = 5.0
        self.d1.z2 = 2.0

        self.add('d2', Discipline2_WithDerivatives())
        self.d2.y1 = 1.0
        self.d2.y2 = 1.0
        self.d2.z1 = 5.0
        self.d2.z2 = 2.0

        self.connect('d1.y1', 'd2.y1')
        self.connect('d2.y2', 'd1.y2')

        self.add('driver', NewtonSolver())
        self.driver.workflow.add(['d1', 'd2'])
示例#8
0
    def test_equation(self):

        top = set_as_top(Assembly())

        top.add('precomp', ExecCompWithDerivatives(['y=x'], ['dy_dx = 1']))
        top.precomp.x = 1.0

        expr = ['y = 3.0*x*x -4.0*x']
        deriv = ['dy_dx = 6.0*x -4.0']

        top.add('comp', ExecCompWithDerivatives(expr, deriv))
        top.driver.workflow.add(['comp'])

        top.add('driver', NewtonSolver())
        top.driver.add_parameter('comp.x')
        top.driver.add_constraint('precomp.y - comp.y = 1.0 - 2.0')

        top.run()

        print top.comp.x, top.comp.y
        assert_rel_error(self, top.comp.x, -0.38742588, 1e-4)
示例#9
0
    def configure(self):

        self.add('d1', Discipline1())
        self.d1.x1 = 1.0
        self.d1.y1 = 1.0
        self.d1.y2 = 1.0
        self.d1.z1 = 5.0
        self.d1.z2 = 2.0

        self.add('d2', Discipline2_WithDerivatives())
        self.d2.y1 = 1.0
        self.d2.y2 = 1.0
        self.d2.z1 = 5.0
        self.d2.z2 = 2.0

        self.connect('d1.y1', 'd2.y1')
        #self.connect('d2.y2', 'd1.y2')

        self.add('driver', NewtonSolver())
        self.driver.workflow.add(['d1', 'd2'])
        self.driver.add_parameter('d1.y2', low=-1e99, high=1e99)
        self.driver.add_constraint('d1.y2 = d2.y2')
示例#10
0
    def test_general_solver(self):

        a = set_as_top(Assembly())
        comp = a.add('comp', ExecComp(exprs=["f=a * x**n + b * x - c"]))
        comp.n = 77.0 / 27.0
        comp.a = 1.0
        comp.b = 1.0
        comp.c = 10.0
        comp.x = 0.0

        driver = a.add('driver', NewtonSolver())
        driver.workflow.add('comp')

        driver.add_parameter('comp.x', 0, 100)
        driver.add_constraint('comp.f=0')
        self.top.driver.gradient_options.fd_step = 0.01
        self.top.driver.gradient_options.fd_step_type = 'relative'

        a.run()

        assert_rel_error(self, a.comp.x, 2.06720359226, .0001)
        assert_rel_error(self, a.comp.f, 0, .0001)