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'))
[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()
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.]