CrossProductComp#

CrossProductComp performs a cross product between two 3-vector inputs. It may be vectorized to provide the result at one or more points simultaneously.

\[ c_i = \bar{a}_i \times \bar{b}_i \]

The first dimension of the inputs holds the vectorized dimension. The default vec_size is 1, providing the cross product of \(a\) and \(b\) at a single point. The lengths of \(a\) and \(b\) at each point must be 3.

The shape of \(a\) and \(b\) will always be (vec_size, 3), but the connection rules of OpenMDAO allow the incoming connection to have shape (3,) when vec_size is 1, since the storage order of the underlying data is the same. The output vector c of CrossProductComp will always have shape (vec_size, 3).

CrossProductComp Options#

Options for CrossProductComp allow the user to rename the input variables \(a\) and \(b\) and the output \(c\), as well as specifying their units.

OptionDefaultAcceptable ValuesAcceptable TypesDescription
a_nameaN/A['str']The variable name for vector a.
a_unitsN/AN/A['str']The units for vector a.
always_optFalse[True, False]['bool']If True, force nonlinear operations on this component to be included in the optimization loop even if this component is not relevant to the design variables and responses.
b_namebN/A['str']The variable name for vector b.
b_unitsN/AN/A['str']The units for vector b.
c_namecN/A['str']The variable name for vector c.
c_unitsN/AN/A['str']The units for vector c.
distributedFalse[True, False]['bool']If True, set all variables in this component as distributed across multiple processes
run_root_onlyFalse[True, False]['bool']If True, call compute, compute_partials, linearize, apply_linear, apply_nonlinear, and compute_jacvec_product only on rank 0 and broadcast the results to the other ranks.
vec_size1N/A['int']The number of points at which the cross product is computed

CrossProductComp Constructor#

The call signature for the CrossProductComp constructor is:

CrossProductComp.__init__(**kwargs)[source]

Initialize the Cross Product component.

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

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

Add a new output product to the cross 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 vector 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,).

CrossProductComp Example#

In the following example CrossProductComp is used to compute torque as the cross product of force (\(F\)) and radius (\(r\)) 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()

p.model.add_subsystem(name='cross_prod_comp',
                      subsys=om.CrossProductComp(vec_size=n,
                                                 a_name='r', b_name='F', c_name='torque',
                                                 a_units='m', b_units='N', c_units='N*m'),
                      promotes_inputs=['r', 'F'])

p.setup()

p.set_val('r', np.random.rand(n, 3))
p.set_val('F', np.random.rand(n, 3))

p.run_model()

# Check the output in units of ft*lbf to ensure that our units work as expected.
expected = []
for i in range(n):
    a_i = p.get_val('r')[i, :]
    b_i = p.get_val('F')[i, :]
    expected.append(np.cross(a_i, b_i) * 0.73756215)

    actual_i = p.get_val('cross_prod_comp.torque', units='ft*lbf')[i]
    rel_error = np.abs(expected[i] - actual_i)/actual_i
    assert np.all(rel_error < 1e-8), f"Relative error: {rel_error}"

print(p.get_val('cross_prod_comp.torque', units='ft*lbf'))
[[-1.65210442e-01 -6.89001051e-02  4.00609595e-01]
 [ 3.46247683e-01 -5.18114255e-01  2.63777954e-01]
 [ 3.94206258e-02 -5.85519390e-01  3.67599035e-01]
 [ 3.49172101e-02 -3.04885035e-01  4.22709988e-01]
 [ 6.57359102e-02  1.82349629e-02 -4.81396470e-02]
 [ 5.82323318e-01 -5.47531471e-01  2.19517928e-01]
 [-4.31768275e-02 -1.70223308e-01  9.26492270e-02]
 [ 1.52146910e-01 -1.29351844e-01 -5.79058132e-02]
 [ 1.11411615e-01 -3.65779802e-01  6.96573221e-02]
 [ 9.83241532e-03 -3.73015252e-02  1.33233289e-02]
 [ 3.23650434e-02 -6.85267368e-02  8.69204761e-03]
 [-9.14606997e-02 -5.91131825e-02  9.27638685e-02]
 [ 1.44467761e-02 -3.68604130e-01  6.98883947e-02]
 [ 1.30652220e-01  4.41131512e-01 -1.95936457e-01]
 [ 4.70405453e-01 -6.10494512e-01  1.58502188e-01]
 [-2.01705908e-02 -3.47838944e-01  3.42481661e-01]
 [-3.71084336e-01  3.40030396e-01  4.07056788e-01]
 [ 4.54355622e-02 -5.05464238e-01  5.38924447e-01]
 [-6.12003707e-02  3.26275532e-02  9.36590100e-02]
 [ 1.03815194e-01 -1.17293355e-04 -7.05625222e-02]
 [ 3.76224928e-03 -5.80512972e-02  8.06495146e-02]
 [ 5.13252154e-02 -2.68049895e-02 -4.01761203e-02]
 [ 4.44525259e-01 -4.39448129e-01 -3.94375007e-01]
 [-1.54513279e-01  2.54857760e-01 -6.90532937e-02]]

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()

cpc = om.CrossProductComp(vec_size=n,
                          a_name='r', b_name='F', c_name='torque',
                          a_units='m', b_units='N', c_units='N*m')

cpc.add_product(vec_size=n,
                a_name='r', b_name='p', c_name='L',
                a_units='m', b_units='kg*m/s', c_units='kg*m**2/s')

p.model.add_subsystem(name='cross_prod_comp', subsys=cpc,
                      promotes_inputs=['r', 'F', 'p'])

p.setup()

p.set_val('r', np.random.rand(n, 3))
p.set_val('F', np.random.rand(n, 3))
p.set_val('p', np.random.rand(n, 3))

p.run_model()

# Check the output.
expected_T = []
expected_L = []
for i in range(n):
    a_i = p.get_val('r')[i, :]
    b_i = p.get_val('F')[i, :]
    expected_T.append(np.cross(a_i, b_i))

    actual_i = p.get_val('cross_prod_comp.torque')[i]
    rel_error = np.abs(expected_T[i] - actual_i)/actual_i
    assert np.all(rel_error < 1e-8), f"Relative error: {rel_error}"

    b_i = p.get_val('p')[i, :]
    expected_L.append(np.cross(a_i, b_i))

    actual_i = p.get_val('cross_prod_comp.L')[i]
    rel_error = np.abs(expected_L[i] - actual_i)/actual_i
    assert np.all(rel_error < 1e-8), f"Relative error: {rel_error}"

print(p.get_val('cross_prod_comp.torque'))
[[ 0.4879524  -0.53507824 -0.06205101]
 [-0.18493019  0.152133   -0.03763489]
 [-0.17846744  0.04470374  0.08204524]
 [ 0.15822724  0.1040712  -0.36428216]
 [ 0.10472059 -0.05163143 -0.06014863]
 [-0.10206074 -0.57182912  0.36115375]
 [-0.09085409  0.03522432  0.07761019]
 [-0.20809411  0.27806105 -0.23044175]
 [ 0.50143342  0.43280531 -0.55062981]
 [ 0.16080751  0.17255774 -0.2951243 ]
 [-0.06720107 -0.27510402  0.25616757]
 [-0.56043228  0.57541745 -0.13803466]
 [ 0.14434165 -0.03002649 -0.1038357 ]
 [-0.54003194 -0.07253665  0.5426759 ]
 [-0.00976868 -0.32448365  0.23014769]
 [-0.2409956   0.35014078 -0.21880967]
 [-0.0475958  -0.61787245  0.58495029]
 [ 0.09408147 -0.42805689  0.68671223]
 [-0.17170037  0.78913755 -0.21502113]
 [-0.0366704   0.05401474 -0.01077219]
 [ 0.82465925 -0.72579947 -0.25424272]
 [ 0.01651556 -0.03813076  0.0602175 ]
 [ 0.17408093 -0.00931059 -0.01752171]
 [ 0.23534144  0.41815576 -0.80161152]]
print(p.get_val('cross_prod_comp.L'))
[[ 0.74491184 -0.799582   -0.35035571]
 [-0.09639784  0.11018591 -0.02735145]
 [-0.47285204 -0.19376467  0.54850961]
 [ 0.32474714  0.07747751 -0.57461032]
 [ 0.07928329  0.04356048 -0.27233305]
 [-0.13839945 -0.50685757  0.478089  ]
 [ 0.09105667 -0.14907386  0.08873223]
 [-0.39642968  0.37426248 -0.2619328 ]
 [-0.19887102  0.41888667 -0.40160375]
 [ 0.11357658  0.2932212  -0.31606934]
 [-0.76901028  0.74949158  0.11718122]
 [-0.00390039 -0.09842672  0.06496604]
 [ 0.13797784 -0.00215205 -0.12168751]
 [ 0.08549921 -0.031979   -0.06739585]
 [-0.01360883 -0.30698219  0.31922045]
 [-0.45196869  0.6194657  -0.37708109]
 [ 0.02967893 -0.47033631  0.21169596]
 [ 0.10398329 -0.37002329  0.06126155]
 [-0.37404505  0.56580882 -0.1453261 ]
 [-0.19898732  0.05942386  0.06937692]
 [ 0.6739862  -0.88132679  0.595633  ]
 [ 0.03985691 -0.07786462  0.05551321]
 [-0.07667308  0.32340091 -0.53712759]
 [-0.00135276  0.35038745 -0.65477878]]