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 Components.

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.

Parameters
namestr or None

name of the variable. If None, variables should be defined external to this class by calling add_output.

valfloat or ndarray

value of the variable if a single variable is being defined.

**kwargsdict

keyword arguments.

Method Signature

IndepVarComp.add_output(name, val=1.0, shape=None, units=None, res_units=None, desc='', lower=None, upper=None, ref=None, ref0=None, res_ref=None, tags=None, shape_by_conn=False, copy_shape=None, distributed=None)[source]

Add an independent variable to this component.

Parameters
namestr

name of the variable in this component’s namespace.

valfloat or list or tuple or ndarray

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

shapeint or tuple or list or None

Shape of this variable, only required if val is not an array. Default is None.

unitsstr or None

Units in which the output variables will be provided to the component during execution. Default is None, which means it has no units.

res_unitsNone

This argument is deprecated because it was unused.

descstr

description of the variable

lowerNone

This argument is deprecated because it was unused.

upperNone

This argument is deprecated because it was unused.

refNone

This argument is deprecated because it was unused.

ref0None

This argument is deprecated because it was unused.

res_refNone

This argument is deprecated because it was unused.

tagsstr or list of strs

User defined tags that can be used to filter what gets listed when calling list_outputs.

shape_by_connbool

If True, shape this output to match its connected input(s).

copy_shapestr or None

If a str, that str is the name of a variable. Shape this output to match that of the named variable.

distributedbool

If True, this variable is a distributed variable, so it can have different sizes/values across MPI processes.

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(comp).setup()

print(prob.get_val('indep_var'))
[1.]
prob.set_val('indep_var', 2.0)
print(prob.get_val('indep_var'))
2.0

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(comp).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(comp).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(comp).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(comp).setup()

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