def test_linearGS_simul_element_and_full_connection(self):
        # Added because of a bug with array slices for Linear GS

        top = Assembly()
        top.add('comp1', ArrayComp2D())
        top.add('comp2', ArrayComp2D())

        top.add('driver', SimpleDriver())
        top.driver.workflow.add(['comp1', 'comp2'])
        top.connect('comp1.y', 'comp2.x')
        top.driver.add_parameter('comp1.x[0][0]', low=-10, high=10)
        top.driver.add_objective('comp1.y[0][0]')
        top.driver.add_constraint('comp2.y[0][1] < 0')
        top.driver.gradient_options.lin_solver = 'linear_gs'
        top.driver.gradient_options.maxiter = 1

        top.run()

        J = top.driver.calc_gradient(mode='forward')
        assert_rel_error(self, J[0, 0], 2.0, .000001)
        assert_rel_error(self, J[1, 0], 39.0, .000001)

        J = top.driver.calc_gradient(mode='adjoint')
        assert_rel_error(self, J[0, 0], 2.0, .000001)
        assert_rel_error(self, J[1, 0], 39.0, .000001)
Пример #2
0
    def test_nested_2Darray_simul_element_and_full_connection2(self):
        # Slightly different config

        top = Assembly()
        top.add('nest', Assembly())
        top.nest.add('comp1', ArrayComp2D())
        top.nest.add('comp2', ArrayComp2D())
        top.driver.gradient_options.lin_solver = 'petsc_ksp'
        top.nest.driver.gradient_options.lin_solver = 'petsc_ksp'

        top.nest.driver.workflow.add(['comp1', 'comp2'])
        top.nest.connect('comp1.y', 'comp2.x')
        top.nest.create_passthrough('comp1.x')
        top.nest.create_passthrough('comp1.y')
        top.nest.add('yy', Array(iotype='out'))
        top.nest.connect('comp2.y', 'yy')

        top.add('driver', SimpleDriver())
        top.driver.workflow.add(['nest'])
        top.driver.add_parameter('nest.x[0][0]', low=-10, high=10)
        top.driver.add_objective('nest.yy[0][0]')
        top.driver.add_constraint('nest.y[0][1] < 0')
        top.run()

        J = top.driver.calc_gradient(mode='forward')
        assert_rel_error(self, J[0, 0], 24.0, .000001)
        assert_rel_error(self, J[1, 0], 4.0, .000001)

        J = top.driver.calc_gradient(mode='adjoint')
        assert_rel_error(self, J[0, 0], 24.0, .000001)
        assert_rel_error(self, J[1, 0], 4.0, .000001)

        J = top.driver.calc_gradient(mode='fd')
        assert_rel_error(self, J[0, 0], 24.0, .000001)
        assert_rel_error(self, J[1, 0], 4.0, .000001)
    def test_PA_slices(self):

        top = set_as_top(Assembly())
        top.add('comp', ArrayComp2D())
        top.add('driver', SimpleDriver())
        top.driver.workflow.add('comp')
        top.comp.force_fd = True
        top.driver.gradient_options.directional_fd = True

        top.driver.add_parameter('comp.x', low=-100, high=100)
        top.driver.add_constraint('comp.y[0][-1] < 1.0')
        top.driver.add_constraint('sum(comp.y) < 4.0')

        top.run()
        J = top.driver.workflow.calc_gradient(mode='forward')

        assert_rel_error(self, J[0, 0], 4.0, 0.001)
        assert_rel_error(self, J[0, 1], 2.0, 0.001)
        assert_rel_error(self, J[0, 2], 6.0, 0.001)
        assert_rel_error(self, J[0, 3], 5.0, 0.001)
Пример #4
0
    def test_array(self):

        top = set_as_top(Assembly())
        nest = top.add('nest', Assembly())
        top.driver.workflow.add('nest')
        top.driver.gradient_options.lin_solver = 'petsc_ksp'

        nest.add('comp', ArrayComp2D())
        nest.driver.workflow.add('comp')
        nest.create_passthrough('comp.x')
        nest.create_passthrough('comp.y')

        top.run()

        J = top.driver.calc_gradient(inputs = ['nest.x'], outputs=['nest.y'])
        diff = J - top.nest.comp.J
        assert_rel_error(self, diff.max(), 0.0, .000001)

        J = top.driver.calc_gradient(inputs = ['nest.x'], outputs=['nest.y'], mode='adjoint')
        diff = J - top.nest.comp.J
        assert_rel_error(self, diff.max(), 0.0, .000001)
    def test_nested_2Darray(self):

        top = Assembly()
        top.add('nest', Assembly())
        top.nest.add('comp', ArrayComp2D())

        top.driver.workflow.add(['nest'])
        top.nest.driver.workflow.add(['comp'])
        top.nest.create_passthrough('comp.x')
        top.nest.create_passthrough('comp.y')
        top.run()

        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x',
        ],
                                              outputs=['nest.y'],
                                              mode='forward')

        diff = J - top.nest.comp.J
        assert_rel_error(self, diff.max(), 0.0, .000001)

        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[0, 0]',
        ],
                                              outputs=['nest.y[0, 0]'],
                                              mode='forward')

        diff = J - top.nest.comp.J[0, 0]
        assert_rel_error(self, diff.max(), 0.0, .000001)

        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[0, 1]',
        ],
                                              outputs=['nest.y[1, 0]'],
                                              mode='forward')

        diff = J - top.nest.comp.J[1, 2]
        assert_rel_error(self, diff.max(), 0.0, .000001)

        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[0, 1]',
        ],
                                              outputs=['nest.y[1, 0]'],
                                              mode='fd')

        diff = J - top.nest.comp.J[1, 2]
        assert_rel_error(self, diff.max(), 0.0, .000001)

        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[0, -1]',
        ],
                                              outputs=['nest.y[-1, 0]'],
                                              mode='forward')

        diff = J - top.nest.comp.J[1, 2]
        assert_rel_error(self, diff.max(), 0.0, .000001)

        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[0, -1]',
        ],
                                              outputs=['nest.y[-1, 0]'],
                                              mode='fd')

        diff = J - top.nest.comp.J[1, 2]
        assert_rel_error(self, diff.max(), 0.0, .000001)

        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        Jsub = top.nest.comp.J[2:3, 2:3]
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[1][:]',
        ],
                                              outputs=['nest.y[1][:]'],
                                              mode='forward')

        diff = J - Jsub

        top.run()
        top.driver.workflow.config_changed()
        top.nest.driver.workflow.config_changed()
        J = top.driver.workflow.calc_gradient(inputs=[
            'nest.x[1][:]',
        ],
                                              outputs=['nest.y[1][:]'],
                                              mode='fd')
        diff = J - Jsub
Пример #6
0
    def test_nested_array_full_and_partial_passthrough(self):

        top = Assembly()
        top.add('nest', Assembly())
        top.nest.add('comp1', ArrayComp2D())
        top.nest.add('comp2', ArrayComp2D())
        top.driver.gradient_options.lin_solver = 'petsc_ksp'
        top.nest.driver.gradient_options.lin_solver = 'petsc_ksp'

        top.nest.driver.workflow.add(['comp1', 'comp2'])
        top.nest.create_passthrough('comp1.x', 'x')
        top.nest.create_passthrough('comp1.y', 'y1')
        top.nest.create_passthrough('comp2.y', 'y2')
        top.nest.connect('x[-1, -1]', 'comp2.x[-1, -1]')

        top.add('driver', SimpleDriver())
        top.driver.workflow.add(['nest'])
        top.run()

        Jbase = top.nest.comp1.provideJ()

        J = top.driver.calc_gradient(inputs=['nest.x'],
                                     outputs=['nest.y1', 'nest.y2'],
                                     mode='fd')
        diff = abs(J[0:4, :] - Jbase)
        assert_rel_error(self, diff.max(), 0.0, .00001)
        diff = abs(J[0:4, -1] - Jbase[:, -1])
        assert_rel_error(self, diff.max(), 0.0, .00001)
        diff = abs(J[4:, :-1])
        assert_rel_error(self, diff.max(), 0.0, .00001)

        J = top.driver.calc_gradient(inputs=['nest.x'],
                                     outputs=['nest.y1', 'nest.y2'],
                                     mode='forward')
        diff = abs(J[0:4, :] - Jbase)
        assert_rel_error(self, diff.max(), 0.0, .00001)
        diff = abs(J[0:4, -1] - Jbase[:, -1])
        assert_rel_error(self, diff.max(), 0.0, .00001)
        diff = abs(J[4:, :-1])
        assert_rel_error(self, diff.max(), 0.0, .00001)

        Jdict = top.driver.calc_gradient(inputs=['nest.x'],
                                         outputs=['nest.y1', 'nest.y2'],
                                         mode='forward',
                                         return_format='dict')
        diff = Jdict['nest.y1']['nest.x'] - Jbase
        assert_rel_error(self, diff.max(), 0.0, .00001)

        diff = Jdict['nest.y2']['nest.x'] - J[4:, :]
        assert_rel_error(self, diff.max(), 0.0, .00001)

        J = top.driver.calc_gradient(inputs=['nest.x'],
                                     outputs=['nest.y1', 'nest.y2'],
                                     mode='adjoint')
        diff = abs(J[0:4, :] - Jbase)
        assert_rel_error(self, diff.max(), 0.0, .00001)
        diff = abs(J[0:4, -1] - Jbase[:, -1])
        assert_rel_error(self, diff.max(), 0.0, .00001)
        diff = abs(J[4:, :-1])
        assert_rel_error(self, diff.max(), 0.0, .00001)

        Jdict = top.driver.calc_gradient(inputs=['nest.x'],
                                         outputs=['nest.y1', 'nest.y2'],
                                         mode='adjoint',
                                         return_format='dict')
        diff = Jdict['nest.y1']['nest.x'] - Jbase
        assert_rel_error(self, diff.max(), 0.0, .00001)

        diff = Jdict['nest.y2']['nest.x'] - J[4:, :]
        assert_rel_error(self, diff.max(), 0.0, .00001)