# DotProductComp¶

DotProductComp performs a dot product between two compatible inputs. It may be vectorized to provide the result at one or more points simultaneously.

$c_i = \bar{a}_i \cdot \bar{b}_i$

## DotProductComp Options¶

The default vec_size is 1, providing the dot product of $$a$$ and $$b$$ at a single point. The lengths of $$a$$ and $$b$$ are provided by option length.

Other options for DotProductComp allow the user to rename the input variables $$a$$ and $$b$$ and the output $$c$$, as well as specifying their units.

Option Default Acceptable Values Acceptable Types Description Deprecation
a_name a N/A ['str'] The variable name for input vector a. N/A
a_units N/A N/A ['str'] The units for vector a. N/A
b_name b N/A ['str'] The variable name for input vector b. N/A
b_units N/A N/A ['str'] The units for vector b. N/A
c_name c N/A ['str'] The variable name for output vector c. N/A
c_units N/A N/A ['str'] The units for vector c. N/A
distributed False [True, False] ['bool'] True if ALL variables in this component are distributed across multiple processes. The 'distributed' option has been deprecated. Individual inputs and outputs should be set as distributed instead, using calls to add_input() or add_output().
length 3 N/A ['int'] The length of vectors a and b N/A
run_root_onlyFalse [True, False] ['bool'] If True, call compute/compute_partials/linearize/apply_linear/apply_nonlinear/compute_jacvec_product only on rank 0 and broadcast the results to the other ranks.N/A
vec_size 1 N/A ['int'] The number of points at which the dot product is computed N/A

## DotProductComp Constructor¶

The call signature for the DotProductComp constructor is:

DotProductComp.__init__(**kwargs)[source]

Initialize the Dot Product component.

Parameters
**kwargsdict of keyword arguments

Keyword arguments that will be mapped into the Component options.

## DotProductComp Usage¶

There are often situations when numerous products need to be computed, essentially in parallel. You can reduce the number of components required by having one DotProductComp perform multiple operations. This is also convenient when the different operations have common inputs.

The add_product method is used to create additional products after instantiation.

DotProductComp.add_product(c_name, a_name='a', b_name='b', c_units=None, a_units=None, b_units=None, vec_size=1, length=3)[source]

Add a new output product to the dot product component.

Parameters
c_namestr

The name of the vector product output.

a_namestr

The name of the first vector input.

b_namestr

The name of the second input.

c_unitsstr or None

The units of the output.

a_unitsstr or None

The units of input a.

b_unitsstr or None

The units of input b.

vec_sizeint

The number of points at which the dot vector product should be computed simultaneously. The shape of the output is (vec_size,).

lengthint

The length of the vectors a and b. Their shapes are (vec_size, length)

## DotProductComp Example¶

In the following example DotProductComp is used to compute instantaneous power as the dot product of force and velocity at 100 points simultaneously. Note the use of a_name, b_name, and c_name to assign names to the inputs and outputs. Units are assigned using a_units, b_units, and c_units. Note that no internal checks are performed to ensure that c_units are consistent with a_units and b_units.

import numpy as np
import openmdao.api as om

n = 24

p = om.Problem()

dp_comp = om.DotProductComp(vec_size=n, length=3, a_name='F', b_name='v', c_name='P',
a_units='N', b_units='m/s', c_units='W')

promotes_inputs=[('F', 'force'), ('v', 'vel')])

p.setup()

p.set_val('force', np.random.rand(n, 3))
p.set_val('vel', np.random.rand(n, 3))

p.run_model()

# Verify the results against numpy.dot in a for loop.
expected = []
for i in range(n):
a_i = p.get_val('force')[i, :]
b_i = p.get_val('vel')[i, :]
expected.append(np.dot(a_i, b_i))

actual_i = p.get_val('dot_prod_comp.P')[i]
rel_error = np.abs(expected[i] - actual_i)/actual_i
assert rel_error < 1e-9, f"Relative error: {rel_error}"

print(p.get_val('dot_prod_comp.P', units='kW'))

[0.00119509 0.00030904 0.00115209 0.00095503 0.00055517 0.00066951
0.0011517  0.00127161 0.0006953  0.0006086  0.00125149 0.00031739
0.00058402 0.00045227 0.00044378 0.00034219 0.00041866 0.00021366
0.00097809 0.00103028 0.00130734 0.00030592 0.00061032 0.00134651]


## DotProductComp Example with Multiple Products¶

When defining multiple products:

• An input name in one call to add_product may not be an output name in another call, and vice-versa.

• The units and shape of variables used across multiple products must be the same in each one.

n = 24

p = om.Problem()

dp_comp = om.DotProductComp(vec_size=n, length=3,
a_name='F', b_name='d', c_name='W',
a_units='N', b_units='m', c_units='J')

a_name='F', b_name='v', c_name='P',
a_units='N', b_units='m/s', c_units='W')

promotes_inputs=[('F', 'force'), ('d', 'disp'), ('v', 'vel')])

p.setup()

p.set_val('force', np.random.rand(n, 3))
p.set_val('disp', np.random.rand(n, 3))
p.set_val('vel', np.random.rand(n, 3))

p.run_model()

# Verify the results against numpy.dot in a for loop.
expected_P = []
expected_W = []
for i in range(n):
a_i = p.get_val('force')[i, :]

b_i = p.get_val('disp')[i, :]
expected_W.append(np.dot(a_i, b_i))

actual_i = p.get_val('dot_prod_comp.W')[i]
rel_error = np.abs(actual_i - expected_W[i])/actual_i
assert rel_error < 1e-9, f"Relative error: {rel_error}"

b_i = p.get_val('vel')[i, :]
expected_P.append(np.dot(a_i, b_i))

actual_i = p.get_val('dot_prod_comp.P')[i]
rel_error = np.abs(expected_P[i] - actual_i)/actual_i
assert rel_error < 1e-9, f"Relative error: {rel_error}"

print(p.get_val('dot_prod_comp.W', units='kJ'))

[0.00044857 0.0002424  0.00143153 0.00054608 0.00108236 0.00052906
0.00031975 0.00041498 0.0011419  0.00045185 0.00011824 0.00094415
0.00094297 0.00083623 0.00020529 0.00080746 0.0003732  0.00109004
0.00049333 0.00076868 0.00095792 0.00019611 0.00063463 0.00088151]

print(p.get_val('dot_prod_comp.P', units='kW'))

[1.31260804e-03 6.04160132e-04 9.94529281e-04 6.77159883e-04
9.13997093e-04 7.78598733e-04 3.95622550e-04 5.63197438e-04
1.01175979e-03 1.33178773e-04 7.33524362e-04 9.02445722e-04
2.13591972e-04 8.35474532e-04 2.65112854e-04 3.61587044e-04
3.86167357e-04 1.39591016e-03 8.95891399e-05 9.21132070e-04
1.41346154e-03 5.85706344e-04 9.65218706e-04 9.25563414e-04]