AddSubtractComp#

AddSubtractComp performs elementwise addition or subtraction between two or more compatible inputs. It may be vectorized to provide the result at one or more points simultaneously.

\[ result = a * \textrm{scaling factor}_a + b * \textrm{scaling factor}_b + c * \textrm{scaling factor}_c + ... \]

AddSubtractComp Constructor#

The call signature for the AddSubtractComp constructor is:

AddSubtractComp.__init__(output_name=None, input_names=None, vec_size=1, length=1, val=1.0, scaling_factors=None, **kwargs)[source]

Allow user to create an addition/subtracton system with one-liner.

Using the AddSubtractComp#

The add_equation method is used to set up a system of inputs to be added/subtracted (with scaling factors). Each time the user adds an equation, all of the inputs and outputs must be of identical shape (this is a requirement for element-wise addition/subtraction). The units must also be compatible between all inputs and the output of each equation.

Method Signature#

AddSubtractComp.add_equation(output_name, input_names, vec_size=1, length=1, val=1.0, units=None, res_units=None, desc='', lower=None, upper=None, ref=1.0, ref0=0.0, res_ref=None, scaling_factors=None, tags=None)[source]

Add an addition/subtraction relation.

Parameters:
output_namestr

(required) Name of the result variable in this component’s namespace.

input_namesiterable

(required) names of the input variables for this system.

vec_sizeint

Length of the first dimension of the input and output vectors (i.e number of rows, or vector length for a 1D vector) Default is 1.

lengthint

Length of the second dimension of the input and output vectors (i.e. number of columns) Default is 1 which results in input/output vectors of size (vec_size,).

valfloat or list or tuple or ndarray

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

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_unitsstr or None

Units in which the residuals of this output will be given to the user when requested. Default is None, which means it has no units.

descstr

Description of the variable.

lowerfloat or list or tuple or ndarray or Iterable or None

Lower bound(s) in user-defined units. It can be (1) a float, (2) an array_like consistent with the shape arg (if given), or (3) an array_like matching the shape of val, if val is array_like. A value of None means this output has no lower bound. Default is None.

upperfloat or list or tuple or ndarray or or Iterable None

Upper bound(s) in user-defined units. It can be (1) a float, (2) an array_like consistent with the shape arg (if given), or (3) an array_like matching the shape of val, if val is array_like. A value of None means this output has no upper bound. Default is None.

reffloat or ndarray

Scaling parameter. The value in the user-defined units of this output variable when the scaled value is 1. Default is 1.

ref0float or ndarray

Scaling parameter. The value in the user-defined units of this output variable when the scaled value is 0. Default is 0.

res_reffloat or ndarray

Scaling parameter. The value in the user-defined res_units of this output’s residual when the scaled value is 1. Default is 1.

scaling_factorsiterable of numeric

Scaling factors to apply to each input. Use [1,1,…] for addition, [1,-1,…] for subtraction Must be same length as input_names Default is None which results in a scaling factor of 1 on each input (element-wise addition).

tagsstr or list of strs

User defined tags that can be used to filter what gets listed when calling list_inputs and list_outputs and also when listing results from case recorders.

AddSubtractComp Example#

In the following example AddSubtractComp is used to add thrust, drag, lift, and weight forces. Note the scaling factor of -1 for the drag force and weight force.

import numpy as np
import openmdao.api as om

n = 3

p = om.Problem()
model = p.model

# Construct an adder/subtracter here. create a relationship through the add_equation method
adder = om.AddSubtractComp()
adder.add_equation('total_force', input_names=['thrust', 'drag', 'lift', 'weight'],
                   vec_size=n, length=2, scaling_factors=[1, -1, 1, -1], units='kN')
# Note the scaling factors. we assume all forces are positive sign upstream

# The vector represents forces at 3 time points (rows) in 2 dimensional plane (cols)
p.model.add_subsystem(name='totalforcecomp', subsys=adder,
                      promotes_inputs=['thrust', 'drag', 'lift', 'weight'])

p.setup()

# Set thrust to exceed drag, weight to equal lift for this scenario
p['thrust'][:, 0] = [500, 600, 700]
p['drag'][:, 0] = [400, 400, 400]
p['weight'][:, 1] = [1000, 1001, 1002]
p['lift'][:, 1] = [1000, 1000, 1000]

p.run_model()

# Verify the results
expected_i = np.array([[100, 200, 300], [0, -1, -2]]).T
print(p.get_val('totalforcecomp.total_force', units='kN'))
[[100.   0.]
 [200.  -1.]
 [300.  -2.]]