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))
def test_recording_remote_voi(self): # Create a parallel model model = Group() model.add_subsystem('par', ParallelGroup()) model.par.add_subsystem('G1', Mygroup()) model.par.add_subsystem('G2', Mygroup()) model.connect('par.G1.y', 'Obj.y1') model.connect('par.G2.y', 'Obj.y2') model.add_subsystem('Obj', ExecComp('obj=y1+y2')) model.add_objective('Obj.obj') # Configure driver to record VOIs on both procs driver = ScipyOptimizeDriver(disp=False) driver.recording_options['record_desvars'] = True driver.recording_options['record_responses'] = True driver.recording_options['record_objectives'] = True driver.recording_options['record_constraints'] = True driver.recording_options['includes'] = ['par.G1.y', 'par.G2.y'] driver.add_recorder(self.recorder) # Create problem and run driver prob = Problem(model, driver) prob.setup() t0, t1 = run_driver(prob) prob.cleanup() # Since the test will compare the last case recorded, just check the # current values in the problem. This next section is about getting those values # These involve collective gathers so all ranks need to run this expected_outputs = prob.driver.get_design_var_values() expected_outputs.update(prob.driver.get_objective_values()) expected_outputs.update(prob.driver.get_constraint_values()) # includes for outputs are specified as promoted names but we need absolute names prom2abs = model._var_allprocs_prom2abs_list['output'] abs_includes = [ prom2abs[n][0] for n in prob.driver.recording_options['includes'] ] # Absolute path names of includes on this rank rrank = model.comm.rank rowned = model._owning_rank local_includes = [n for n in abs_includes if rrank == rowned[n]] # Get values for all vars on this rank inputs, outputs, residuals = model.get_nonlinear_vectors() # Get values for includes on this rank local_vars = {n: outputs[n] for n in local_includes} # Gather values for includes on all ranks all_vars = model.comm.gather(local_vars, root=0) if prob.comm.rank == 0: # Only on rank 0 do we have all the values. The all_vars variable is a list of # dicts from all ranks 0,1,... In this case, just ranks 0 and 1 dct = all_vars[-1] for d in all_vars[:-1]: dct.update(d) expected_includes = { 'par.G1.Cy.y': dct['par.G1.Cy.y'], 'par.G2.Cy.y': dct['par.G2.Cy.y'], } expected_outputs.update(expected_includes) coordinate = [0, 'ScipyOptimize_SLSQP', (driver.iter_count - 1, )] expected_data = ((coordinate, (t0, t1), expected_outputs, None), ) assertDriverIterDataRecorded(self, expected_data, self.eps)
model.connect('CT2Comp.CT2','CT1Comp.CT2') model.add_subsystem('ATComp',ATComp(num_panel=airfoil.NUM_SAMPLES)) 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()
def test_recording_remote_voi(self): # Create a parallel model model = Group() model.add_subsystem('par', ParallelGroup()) model.par.add_subsystem('G1', Mygroup()) model.par.add_subsystem('G2', Mygroup()) model.connect('par.G1.y', 'Obj.y1') model.connect('par.G2.y', 'Obj.y2') model.add_subsystem('Obj', ExecComp('obj=y1+y2')) model.add_objective('Obj.obj') # Configure driver to record VOIs on both procs driver = ScipyOptimizeDriver(disp=False) driver.recording_options['record_desvars'] = True driver.recording_options['record_responses'] = True driver.recording_options['record_objectives'] = True driver.recording_options['record_constraints'] = True driver.recording_options['includes'] = ['par.G1.y', 'par.G2.y'] driver.add_recorder(self.recorder) # Create problem and run driver prob = Problem(model, driver) prob.add_recorder(self.recorder) prob.setup() t0, t1 = run_driver(prob) prob.record_iteration('final') t2 = time() prob.cleanup() # Since the test will compare the last case recorded, just check the # current values in the problem. This next section is about getting those values # These involve collective gathers so all ranks need to run this expected_outputs = driver.get_design_var_values() expected_outputs.update(driver.get_objective_values()) expected_outputs.update(driver.get_constraint_values()) # includes for outputs are specified as promoted names but we need absolute names prom2abs = model._var_allprocs_prom2abs_list['output'] abs_includes = [prom2abs[n][0] for n in prob.driver.recording_options['includes']] # Absolute path names of includes on this rank rrank = model.comm.rank rowned = model._owning_rank local_includes = [n for n in abs_includes if rrank == rowned[n]] # Get values for all vars on this rank inputs, outputs, residuals = model.get_nonlinear_vectors() # Get values for includes on this rank local_vars = {n: outputs[n] for n in local_includes} # Gather values for includes on all ranks all_vars = model.comm.gather(local_vars, root=0) if prob.comm.rank == 0: # Only on rank 0 do we have all the values. The all_vars variable is a list of # dicts from all ranks 0,1,... In this case, just ranks 0 and 1 dct = all_vars[-1] for d in all_vars[:-1]: dct.update(d) expected_includes = { 'par.G1.Cy.y': dct['par.G1.Cy.y'], 'par.G2.Cy.y': dct['par.G2.Cy.y'], } expected_outputs.update(expected_includes) coordinate = [0, 'ScipyOptimize_SLSQP', (driver.iter_count-1,)] expected_data = ((coordinate, (t0, t1), expected_outputs, None),) assertDriverIterDataRecorded(self, expected_data, self.eps) expected_data = (('final', (t1, t2), expected_outputs),) assertProblemDataRecorded(self, expected_data, self.eps)
# add_eq('') add_var('hv_RPM') # hover RMP add_var('cr_P') # cruise power add_var('hv_P') # hover power add_var('cr_T') # cruise Thrust add_var('hv_T') # hover Thrust add_var('cr_Q') # cruise Torque? add_var('hv_Q') # hover Torque? group = Group() group.add_subsystem('indep_var_comp', indep_var_comp, promotes=['*']) group.add_subsystem('equations_group', equations_group, promotes=['*']) group.add_objective('x', scaler=1) #group.add_constraint('b',lower=0.5,upper = 1,) # this is how you add a contraint prob = Problem(model=group) prob.driver = ScipyOptimizeDriver() prob.driver.options['optimizer'] = 'SLSQP' prob.setup() prob.run_model() #print(prob['Con_q']) prob.run_driver() prob.model.list_outputs() # print(prob.list_problem_vars())
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 # driver.opt_settings['Major feasibility tolerance'] = 1.0e-6