Antione
Antione

Reputation: 109

Problem using ExternalCodeComp in Openmdao

I am trying out the example problem for ExternalCodeComp as given in openmdao docs.

The optimization code is

import openmdao.api as om
from openmdao.components.tests.test_external_code_comp import ParaboloidExternalCodeCompFD

prob = om.Problem()
model = prob.model

model.add_subsystem('p', ParaboloidExternalCodeCompFD(), promotes_inputs=['x', 'y'])

# find optimal solution with SciPy optimize
# solution (minimum): x = 6.6667; y = -7.3333
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'

prob.model.add_design_var('p.x', lower=-50, upper=50)
prob.model.add_design_var('p.y', lower=-50, upper=50)

prob.model.add_objective('p.f_xy')

prob.driver.options['tol'] = 1e-9
prob.driver.options['disp'] = True

prob.setup()

# Set input values
prob.set_val('p.x', 3.0)
prob.set_val('p.y', -4.0)

prob.run_driver()
print(prob.get_val('p.x'))
print(prob.get_val('p.y'))

However, I get the following error at prob.setup().

Exception has occurred: RuntimeError
Group (<model>): Output not found for design variable 'p.x'.

What does this mean ? I dont know if I am missing something basic. The problem only occurs when I try to optimize it. There is no problem when I just use the external code in a model (as given in the docs).

Upvotes: 1

Views: 98

Answers (1)

Justin Gray
Justin Gray

Reputation: 5710

In this line you promoted the inputs x and y

model.add_subsystem('p', ParaboloidExternalCodeCompFD(), promotes_inputs=['x', 'y'])

Notice that you did not promote the output f_xy

so from the top level of the model the correct paths are: p.f_xy for the output, but x and y for the inputs.

Thus the correct way to add the design variables and set the values was to use x and y.

import openmdao.api as om
from openmdao.components.tests.test_external_code_comp import ParaboloidExternalCodeCompFD

prob = om.Problem()
model = prob.model

model.add_subsystem('p', ParaboloidExternalCodeCompFD(), promotes_inputs=['x', 'y'])

# find optimal solution with SciPy optimize
# solution (minimum): x = 6.6667; y = -7.3333
prob.driver = om.ScipyOptimizeDriver()
prob.driver.options['optimizer'] = 'SLSQP'

prob.model.add_design_var('x', lower=-50, upper=50)
prob.model.add_design_var('y', lower=-50, upper=50)

prob.model.add_objective('p.f_xy')

prob.driver.options['tol'] = 1e-9
prob.driver.options['disp'] = True

prob.setup()

# Set input values
prob.set_val('x', 3.0)
prob.set_val('y', -4.0)

prob.run_driver()
print(prob.get_val('x'))
print(prob.get_val('y'))

Upvotes: 0

Related Questions