Beispiel #1
0
def run_open_mdao():
    if USE_SCALING:
        # prepare scaling
        global offset_weight
        global offset_stress
        global scale_weight
        global scale_stress
        runner = MultiRun(use_calcu=not USE_ABA, use_aba=USE_ABA, non_liner=False, project_name_prefix=PROJECT_NAME_PREFIX,
                          force_recalc=False)
        sur = Surrogate(use_abaqus=USE_ABA, pgf=False, show_plots=False, scale_it=False)
        res, surro = sur.auto_run(SAMPLE_HALTON, 16, SURRO_POLYNOM, run_validation=False)
        p = runner.new_project_r_t(range_rib[0], range_shell[0])
        offset_weight = p.calc_wight()
        p = runner.new_project_r_t(range_rib[1], range_shell[1])
        max_weight = p.calc_wight()
        offset_stress = surro.predict([range_rib[1], range_shell[1]])
        max_stress = surro.predict([range_rib[0], range_shell[0]])
        scale_weight = (max_weight - offset_weight)
        scale_stress = (max_stress - offset_stress)


    write_mdao_log('iter,time,ribs(float),ribs,shell,stress,weight')

    model = Group()
    indeps = IndepVarComp()

    indeps.add_output('ribs', (22 - offset_rib) / scale_rib)
    indeps.add_output('shell', (0.0024 - offset_shell) / scale_shell)

    model.add_subsystem('des_vars', indeps)
    model.add_subsystem('wing', WingStructureSurro())
    model.connect('des_vars.ribs', ['wing.ribs', 'con_cmp1.ribs'])
    model.connect('des_vars.shell', 'wing.shell')

    # design variables, limits and constraints
    model.add_design_var('des_vars.ribs', lower=(range_rib[0] - offset_rib) / scale_rib, upper=(range_rib[1] - offset_rib) / scale_rib)
    model.add_design_var('des_vars.shell', lower=(range_shell[0] - offset_shell) / scale_shell, upper=(range_shell[1] - offset_shell) / scale_shell)

    # objective
    model.add_objective('wing.weight', scaler=0.0001)

    # constraint
    print('constrain stress: ' + str((max_shear_strength - offset_stress) / scale_stress))
    model.add_constraint('wing.stress', upper=(max_shear_strength - offset_stress) / scale_stress)
    model.add_subsystem('con_cmp1', ExecComp('con1 = (ribs * '+str(scale_rib)+') - int(ribs[0] * '+str(scale_rib)+')'))
    model.add_constraint('con_cmp1.con1', upper=.5)

    prob = Problem(model)

    # setup the optimization
    if USE_PYOPTSPARSE:
        prob.driver = pyOptSparseDriver()
        prob.driver.options['optimizer'] = OPTIMIZER
        prob.driver.opt_settings['SwarmSize'] = 8
        prob.driver.opt_settings['stopIters'] = 5
    else:
        prob.driver = ScipyOptimizeDriver()
        prob.driver.options['optimizer'] = OPTIMIZER  # ['Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG', 'L-BFGS-B', 'TNC', 'COBYLA', 'SLSQP']
        prob.driver.options['tol'] = TOL
        prob.driver.options['disp'] = True
        prob.driver.options['maxiter'] = 1000
        #prob.driver.opt_settings['etol'] = 100

    prob.setup()
    prob.set_solver_print(level=0)
    prob.model.approx_totals()
    prob.setup(check=True, mode='fwd')
    prob.run_driver()

    print('done')
    print('ribs: ' + str((prob['wing.ribs'] * scale_rib) + offset_rib))
    print('shell: ' + str((prob['wing.shell'] * scale_shell) + offset_shell) + ' m')
    print('weight= ' + str((prob['wing.weight'] * scale_weight) + offset_weight))
    print('stress= ' + str((prob['wing.stress'] * scale_stress) + offset_stress) + ' ~ ' + str(prob['wing.stress']))
    print('execution counts wing: ' + str(prob.model.wing.executionCounter))
Beispiel #2
0
    def setup(self):
        super(VectorizedIntegrator, self).setup()

        ode_function = self.options['ode_function']
        method = self.options['method']
        starting_coeffs = self.options['starting_coeffs']
        formulation = self.options['formulation']

        has_starting_method = method.starting_method is not None
        is_starting_method = starting_coeffs is not None

        states = ode_function._states
        static_parameters = ode_function._static_parameters
        dynamic_parameters = ode_function._dynamic_parameters
        time_units = ode_function._time_options['units']

        starting_norm_times, my_norm_times = self._get_meta()

        glm_A, glm_B, glm_U, glm_V, num_stages, num_step_vars = self._get_method(
        )

        num_times = len(my_norm_times)

        # ------------------------------------------------------------------------------------

        integration_group = Group(assembled_jac_type='dense')
        self.add_subsystem('integration_group', integration_group)

        if formulation == 'optimizer-based':
            comp = IndepVarComp()
            for state_name, state in iteritems(states):
                comp.add_output('Y:%s' % state_name,
                                shape=(
                                    num_times - 1,
                                    num_stages,
                                ) + state['shape'],
                                units=state['units'])
                comp.add_design_var('Y:%s' % state_name)
            integration_group.add_subsystem('desvars_comp', comp)
        elif formulation == 'solver-based':
            comp = IndepVarComp()
            for state_name, state in iteritems(states):
                comp.add_output('Y:%s' % state_name,
                                val=0.,
                                shape=(
                                    num_times - 1,
                                    num_stages,
                                ) + state['shape'],
                                units=state['units'])
            integration_group.add_subsystem('dummy_comp', comp)

        comp = self._create_ode((num_times - 1) * num_stages)
        integration_group.add_subsystem('ode_comp', comp)
        if ode_function._time_options['targets']:
            self.connect(
                'time_comp.stage_times',
                [
                    '.'.join(('integration_group.ode_comp', t))
                    for t in ode_function._time_options['targets']
                ],
            )
        if len(static_parameters) > 0:
            self._connect_multiple(
                self._get_static_parameter_names('static_parameter_comp',
                                                 'out'),
                self._get_static_parameter_names('integration_group.ode_comp',
                                                 'targets'),
                [
                    np.array([0] * self._get_stage_norm_times(), np.int)
                    for _ in range(len(static_parameters))
                ])
        if len(dynamic_parameters) > 0:
            self._connect_multiple(
                self._get_dynamic_parameter_names('dynamic_parameter_comp',
                                                  'out'),
                self._get_dynamic_parameter_names('integration_group.ode_comp',
                                                  'targets'),
            )

        comp = VectorizedStageStepComp(
            states=states,
            time_units=time_units,
            num_times=num_times,
            num_stages=num_stages,
            num_step_vars=num_step_vars,
            glm_A=glm_A,
            glm_U=glm_U,
            glm_B=glm_B,
            glm_V=glm_V,
        )
        integration_group.add_subsystem('vectorized_stagestep_comp', comp)
        self.connect('time_comp.h_vec',
                     'integration_group.vectorized_stagestep_comp.h_vec')

        comp = VectorizedStepComp(
            states=states,
            time_units=time_units,
            num_times=num_times,
            num_stages=num_stages,
            num_step_vars=num_step_vars,
            glm_B=glm_B,
            glm_V=glm_V,
        )
        self.add_subsystem('vectorized_step_comp', comp)
        self.connect('time_comp.h_vec', 'vectorized_step_comp.h_vec')
        self._connect_multiple(
            self._get_state_names('starting_system', 'starting'),
            self._get_state_names(
                'integration_group.vectorized_stagestep_comp', 'y0'),
        )
        self._connect_multiple(
            self._get_state_names('starting_system', 'starting'),
            self._get_state_names('vectorized_step_comp', 'y0'),
        )

        comp = VectorizedOutputComp(
            states=states,
            num_starting_times=len(starting_norm_times),
            num_my_times=len(my_norm_times),
            num_step_vars=num_step_vars,
            starting_coeffs=starting_coeffs,
        )

        promotes = []
        promotes.extend(
            [get_name('state', state_name) for state_name in states])
        if is_starting_method:
            promotes.extend(
                [get_name('starting', state_name) for state_name in states])

        self.add_subsystem('output_comp', comp, promotes_outputs=promotes)
        if has_starting_method:
            self._connect_multiple(
                self._get_state_names('starting_system', 'state'),
                self._get_state_names('output_comp', 'starting_state'),
            )

        src_indices_to_ode = []
        src_indices_from_ode = []
        for state_name, state in iteritems(states):
            size = np.prod(state['shape'])
            shape = state['shape']

            src_indices_to_ode.append(
                np.arange(
                    (num_times - 1) * num_stages *
                    size).reshape(((num_times - 1) * num_stages, ) + shape))

            src_indices_from_ode.append(
                np.arange((num_times - 1) * num_stages * size).reshape((
                    num_times - 1,
                    num_stages,
                ) + shape))

        src_indices_to_ode = [
            np.array(idx).squeeze() for idx in src_indices_to_ode
        ]

        self._connect_multiple(
            self._get_state_names('vectorized_step_comp', 'y'),
            self._get_state_names('output_comp', 'y'),
        )

        self._connect_multiple(
            self._get_state_names('integration_group.ode_comp', 'rate_source'),
            self._get_state_names('vectorized_step_comp', 'F'),
            src_indices_from_ode,
        )
        self._connect_multiple(
            self._get_state_names('integration_group.ode_comp', 'rate_source'),
            self._get_state_names(
                'integration_group.vectorized_stagestep_comp', 'F'),
            src_indices_from_ode,
        )

        if formulation == 'solver-based':
            self._connect_multiple(
                self._get_state_names(
                    'integration_group.vectorized_stagestep_comp', 'Y_out'),
                self._get_state_names('integration_group.ode_comp', 'targets'),
                src_indices_to_ode,
            )
            self._connect_multiple(
                self._get_state_names('integration_group.dummy_comp', 'Y'),
                self._get_state_names(
                    'integration_group.vectorized_stagestep_comp', 'Y_in'),
            )
        elif formulation == 'optimizer-based':
            self._connect_multiple(
                self._get_state_names('integration_group.desvars_comp', 'Y'),
                self._get_state_names('integration_group.ode_comp', 'targets'),
                src_indices_to_ode,
            )
            self._connect_multiple(
                self._get_state_names('integration_group.desvars_comp', 'Y'),
                self._get_state_names(
                    'integration_group.vectorized_stagestep_comp', 'Y_in'),
            )
            for state_name, state in iteritems(states):
                integration_group.add_constraint(
                    'vectorized_stagestep_comp.Y_out:%s' % state_name,
                    equals=0.,
                    vectorize_derivs=True,
                )

        if has_starting_method:
            self.starting_system.options['formulation'] = self.options[
                'formulation']

        if formulation == 'solver-based':
            if 1:
                integration_group.nonlinear_solver = NonlinearBlockGS(
                    iprint=2, maxiter=40, atol=1e-14, rtol=1e-12)
            else:
                integration_group.nonlinear_solver = NewtonSolver(iprint=2,
                                                                  maxiter=100)

            if 1:
                integration_group.linear_solver = LinearBlockGS(iprint=1,
                                                                maxiter=40,
                                                                atol=1e-14,
                                                                rtol=1e-12)
            else:
                integration_group.linear_solver = DirectSolver(
                    assemble_jac=True, iprint=1)
Beispiel #3
0
model.connect('CT1Comp.CT1','ATComp.CT1')
model.connect('CT2Comp.CT2','ATComp.CT2')

model.add_subsystem('VelocityComp',VelocityComp(num_panel=airfoil.NUM_SAMPLES,aoa=airfoil.aoa))
model.connect('GammaComp.gamma','VelocityComp.gamma')
model.connect('thetaComp.theta','VelocityComp.theta')
model.connect('ATComp.AT','VelocityComp.AT')

model.add_subsystem('LiftComp',LiftComp(num_panel=airfoil.NUM_SAMPLES))
model.connect('VelocityComp.V','LiftComp.V')
model.connect('arcComp.S','LiftComp.S')

model.add_design_var('input.y')
model.add_objective('LiftComp.CL')
leftmost_id = int(airfoil.NUM_SAMPLES/2)
model.add_constraint('input.y',indices=[0,leftmost_id,-1],equals=[airfoil.boundaryPoints_Y[0],airfoil.boundaryPoints_Y[leftmost_id],airfoil.boundaryPoints_Y[-1]])
prob = Problem(model=model)



prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
prob.driver.options['maxiter'] = 300
prob.driver.options['tol'] = 1e-6
prob.set_solver_print(level=0)
prob.model.approx_totals()
prob.setup()

prob.run_driver()
airfoil.Get_PanelCoefficients()
print(airfoil.full_coefficientLift)
Beispiel #4
0
group.add_subsystem('vx_comp', ExecComp('vx = v * cos(theta)'))
group.add_subsystem('vy_comp', ExecComp('vy = v * sin(theta)'))
group.add_subsystem('integrator_group', integrator)

group.connect('final_time_comp.final_time', 'integrator_group.final_time')
group.connect('theta_comp.theta', 'vx_comp.theta')
group.connect('theta_comp.theta', 'vy_comp.theta')
group.connect('v_comp.v', 'vx_comp.v')
group.connect('v_comp.v', 'vy_comp.v')

group.connect('vx_comp.vx', 'integrator_group.initial_condition:vx')
group.connect('vy_comp.vy', 'integrator_group.initial_condition:vy')

group.add_design_var('final_time_comp.final_time', lower=1e-3)
group.add_design_var('theta_comp.theta', lower=0., upper=np.pi / 2.)
group.add_constraint('integrator_group.state:y', indices=[-1], equals=0.)
group.add_objective('integrator_group.state:x', index=-1, scaler=-1.)

prob = Problem()
prob.model = group

prob.driver = ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'
# from openmdao.api import pyOptSparseDriver
# prob.driver = driver = pyOptSparseDriver()
# driver.options['optimizer'] = 'SNOPT'
# driver.opt_settings['Verify level'] = 0
# driver.opt_settings['Major iterations limit'] = 200 #1000
# driver.opt_settings['Minor iterations limit'] = 1000
# driver.opt_settings['Iterations limit'] = 100000
# driver.opt_settings['Major step limit'] = 2.0