Run Your Model#

Once setup() is done, you can then execute your model with run_model()

run_model() will execute one pass through your model.

Examples

A basic setup() executing a single pass through the model.

Paraboloid class definition

class Paraboloid(om.ExplicitComponent):
    """
    Evaluates the equation f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3.
    """

    def setup(self):
        self.add_input('x', val=0.0)
        self.add_input('y', val=0.0)

        self.add_output('f_xy', val=0.0)

    def setup_partials(self):
        self.declare_partials('*', '*')

    def compute(self, inputs, outputs):
        """
        f(x,y) = (x-3)^2 + xy + (y+4)^2 - 3

        Optimal solution (minimum): x = 6.6667; y = -7.3333
        """
        x = inputs['x']
        y = inputs['y']

        outputs['f_xy'] = (x-3.0)**2 + x*y + (y+4.0)**2 - 3.0

    def compute_partials(self, inputs, partials):
        """
        Jacobian for our paraboloid.
        """
        x = inputs['x']
        y = inputs['y']

        partials['f_xy', 'x'] = 2.0*x - 6.0 + y
        partials['f_xy', 'y'] = 2.0*y + 8.0 + x

import openmdao.api as om
from openmdao.test_suite.components.paraboloid import Paraboloid

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

model.add_subsystem('comp', Paraboloid(), promotes=['x', 'y', 'f_xy'])

prob.setup()

prob.set_val('x', 2.)
prob.set_val('y', 10.)
prob.run_model()
print(prob.get_val('f_xy'))
[214.]
prob.set_val('x', 0.)
prob.set_val('y', 0.)
prob.run_model()
print(prob.get_val('f_xy'))
[22.]
prob.setup()

prob.set_val('x', 4.)
prob.set_val('y', 8.)
prob.run_model()
print(prob.get_val('f_xy'))
[174.]