-
-
Save stackoverflow38/0219eda12d4c56ce84c68d201d1f1926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
start_time = time.time() | |
from openmdao.api import Problem, Group, IndepVarComp | |
from openmdao.api import ScipyOptimizeDriver | |
from WRAPPER import WRAPPER | |
top = Problem() | |
top.model = model = Group() | |
# <codecell> | |
inputs_comp = IndepVarComp() | |
inputs_comp.add_output('DV1', 3) | |
inputs_comp.add_output('DV2', 1) | |
inputs_comp.add_output('DV3', 17.0) | |
# <codecell> | |
" Add components/subsystems to the Group-model- " | |
model.add_subsystem('inputs_comp', inputs_comp) | |
model.add_subsystem('WRAPPER', WRAPPER()) | |
model.connect('inputs_comp.DV1', 'WRAPPER.DV1') | |
model.connect('inputs_comp.DV2', 'WRAPPER.DV2') | |
model.connect('inputs_comp.DV3', 'WRAPPER.DV3') | |
# <codecell> | |
"Select driver and optimizer set driver options. " | |
top.driver = ScipyOptimizeDriver() | |
top.driver.options['optimizer'] = 'SLSQP' | |
#top.driver.options['optimizer'] = 'COBYLA' | |
top.driver.options['tol'] = 1e-4 | |
top.driver.options['disp'] = True | |
# <codecell> | |
" Select design variables and their range " | |
top.model.add_design_var('inputs_comp.DV1', lower=-2, upper=8) | |
top.model.add_design_var('inputs_comp.DV2', lower=-5, upper=2) | |
top.model.add_design_var('inputs_comp.DV3', lower=16, upper=20) | |
# <codecell> | |
top.model.add_objective('WRAPPER.OBJ',scaler=-1) | |
top.setup() | |
top.setup(check=True, mode='rev') | |
top.run_driver() | |
print(top['inputs_comp.DV1']) | |
print(top['inputs_comp.DV2']) | |
print(top['inputs_comp.DV3']) | |
print("--- %s seconds ---" % (time.time() - start_time)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openmdao.api import ExternalCode | |
import numpy as np | |
class WRAPPER(ExternalCode): | |
def setup(self): | |
self.add_input('DV1') | |
self.add_input('DV2') | |
self.add_input('DV3') | |
self.add_output('OBJ') | |
self.input_file = 'WRAPPER_input.dat' | |
self.output_file = 'WRAPPER_output.dat' | |
# providing these is optional; the component will verify that any input | |
# files exist before execution and that the output files exist after. | |
self.options['external_input_files'] = [self.input_file,] | |
self.options['external_output_files'] = [self.output_file,] | |
self.options['command'] = [ | |
'python', 'gf_run.py', self.input_file, self.output_file | |
] | |
self.declare_partials(of='OBJ', wrt=['DV1','DV2','DV3'], method='fd',form='central', step=1e-4) | |
def compute(self, inputs, outputs): | |
DV1 = inputs['DV1'] | |
DV2 = inputs['DV2'] | |
DV3 = inputs['DV3'] | |
# generate the input file for the paraboloid external code | |
np.savetxt(self.input_file,[DV1,DV2,DV3]) | |
# the parent compute function actually runs the external code | |
super(WRAPPER, self).compute(inputs, outputs) | |
# parse the output file from the external code | |
file_contents=np.loadtxt(self.output_file) | |
outputs['OBJ'] = file_contents[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment