IndepVarComp#

An IndepVarComp is used to define independent variables.

Independent variables are those that are set externally to the model—therefore, they are called model inputs. From the perspective of a component, they are component outputs that do not depend on any component inputs. From the perspective of a model, they can be viewed as design variables or model parameters that are set by the user or driver, prior to running the model.

In general, you no longer have to define these because OpenMDAO defines and uses them automatically for all unconnected inputs in your model. However, there are some special cases where an IndepVarComp is required (see Distributed Variables.)

The IndepVarComp class is instantiated directly (without defining a subclass). The name, initial value, and other options of the independent variable(s) to be declared can be either passed in during instantiation, or declared via the add_output method.

IndepVarComp Constructor#

IndepVarComp.__init__(name=None, val=1.0, **kwargs)[source]

Initialize all attributes.

Method Signature#

IndepVarComp.add_output(name, val=1.0, **kwargs)[source]

Add an independent variable to this component.

Parameters:
namestr

Name of the variable in this component’s namespace.

valfloat or ndarray

The initial value of the variable being added in user-defined units. Default is 1.0.

**kwargsnamed args

Remaining args passed to the base class add_output.

Returns:
dict

Metadata for added variable.

Usage#

1. Define one independent variable and set its value.

"""Define one independent variable and set its value."""
import openmdao.api as om

comp = om.IndepVarComp('indep_var')
prob = om.Problem()
prob.model.add_subsystem('indep_var', comp, promotes=['*'])
prob.setup()

print(prob.get_val('indep_var'))
[1790952219.786923] [runnervm8df0l:7909 :0]        ib_iface.c:1269 UCX  ERROR mana_0: iface 0x55de216e1e30 failed to create UD QP TX wr:256 sge:6 inl:64 resp:0 RX wr:4096 sge:1 resp:0 failed: Operation not supported
[1790952219.787183] [runnervm8df0l:7909 :0]      ucp_worker.c:1412 UCX  ERROR uct_iface_open(ud_verbs/mana_0:1) failed: Input/output error
[1.]
[runnervm8df0l:07909] pml_ucx.c:313  Error: Failed to create UCP worker
prob.set_val('indep_var', 2.0)
print(prob.get_val('indep_var'))
[2.]

2. Define one independent variable with a default value.

"""Define one independent variable with a default value."""
import openmdao.api as om

comp = om.IndepVarComp('indep_var', val=2.0)
prob = om.Problem()
prob.model.add_subsystem('indep_var', comp, promotes=['*'])
prob.setup()
print(prob.get_val('indep_var'))
[2.]

3. Define one independent variable with a default value and additional options.

"""Define one independent variable with a default value and additional options."""
import openmdao.api as om

comp = om.IndepVarComp('indep_var', val=2.0, units='m', lower=0, upper=10)
prob = om.Problem()
prob.model.add_subsystem('indep_var', comp, promotes=['*'])
prob.setup()
print(prob.get_val('indep_var'))
[2.]

4. Define one independent array variable.

"""Define one independent array variable."""
import numpy as np

import openmdao.api as om

array = np.array([
    [1., 2.],
    [3., 4.],
])

comp = om.IndepVarComp('indep_var', val=array)
prob = om.Problem()
prob.model.add_subsystem('indep_var', comp, promotes=['*'])
prob.setup()
print(prob.get_val('indep_var'))
[[1. 2.]
 [3. 4.]]

5. Define two independent variables using the add_output method with additional options.

"""Define two independent variables using the add_output method."""
import openmdao.api as om

comp = om.IndepVarComp()
comp.add_output('indep_var_1', val=1.0)
comp.add_output('indep_var_2', val=2.0)
prob = om.Problem()
prob.model.add_subsystem('indep_var', comp, promotes=['*'])
prob.setup()

print(prob.get_val('indep_var_1'))
[1.]
print(prob.get_val('indep_var_2'))
[2.]