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.
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.
Option | Default | Acceptable Values | Acceptable Types | Description |
---|---|---|---|---|
a_name | a | N/A | ['str'] | The variable name for vector a. |
a_units | N/A | N/A | ['str'] | The units for vector a. |
always_opt | False | [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_name | b | N/A | ['str'] | The variable name for vector b. |
b_units | N/A | N/A | ['str'] | The units for vector b. |
c_name | c | N/A | ['str'] | The variable name for vector c. |
c_units | N/A | N/A | ['str'] | The units for vector c. |
distributed | False | [True, False] | ['bool'] | If True, set all variables in this component as distributed across multiple processes |
run_root_only | False | [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_size | 1 | N/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'))
[[-0.6581234 0.23333603 0.0320212 ]
[ 0.03119832 -0.01068579 0.01472166]
[-0.01584527 0.08644284 -0.05188506]
[-0.21762815 0.07101894 0.24033308]
[-0.12207918 0.06777732 -0.14050916]
[ 0.01680194 -0.0908313 0.00855329]
[-0.07018918 0.07179634 -0.11339767]
[-0.13888798 -0.07183469 0.14963397]
[-0.48636454 0.20331867 0.36321853]
[ 0.03571309 -0.13792384 0.55019701]
[-0.26901114 0.17861216 -0.20344853]
[-0.16988512 -0.05470837 0.32081965]
[ 0.03227937 0.28348027 -0.19535248]
[ 0.30192766 -0.50058545 0.153628 ]
[-0.02754652 0.21266065 -0.30733379]
[ 0.25395492 -0.12088745 -0.03794674]
[ 0.06009381 -0.07285757 0.05771592]
[ 0.31333095 -0.43819589 0.15787212]
[-0.12908647 0.07848182 -0.00920761]
[-0.00663559 0.25852205 -0.35829235]
[ 0.17117209 0.29672327 -0.19117536]
[ 0.16036944 0.14580788 -0.14606558]
[ 0.20219032 -0.32186525 0.29815082]
[-0.24382346 0.01100871 0.15089542]]
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'))
[[-1.50015325e-02 -8.01179520e-02 6.03980486e-02]
[-3.61562339e-01 3.33419747e-01 1.43946337e-01]
[ 8.19960751e-01 -6.10821439e-01 6.19055541e-01]
[-2.14405201e-01 2.15996745e-01 -1.18527755e-01]
[ 2.62695401e-01 -1.36347415e-01 -4.31160667e-01]
[-5.27838477e-01 4.16690855e-01 -2.80612175e-01]
[-5.91435553e-02 4.76762285e-01 -2.40019026e-01]
[ 6.34219873e-01 -2.21914923e-01 -4.24030019e-01]
[ 1.17091023e-01 3.82787911e-01 -1.08543404e-01]
[-9.24098181e-02 9.09034398e-01 -8.63570621e-01]
[-4.70570730e-01 4.24070930e-01 -8.21851335e-02]
[ 5.97093595e-02 -1.82537304e-02 -2.98898310e-02]
[ 5.94655525e-01 8.97472354e-03 -2.86858207e-01]
[-5.18424423e-01 2.09100663e-02 1.80996130e-01]
[ 2.10497502e-01 -1.33827978e-01 -2.47181434e-01]
[ 3.50435599e-02 3.98533794e-01 -6.49960205e-01]
[-2.58604642e-02 1.16901599e-02 3.34984004e-04]
[-5.10739562e-01 2.84851228e-01 1.58733279e-01]
[-3.82484661e-01 4.09814683e-01 1.63394535e-01]
[ 8.73278731e-01 1.49058501e-01 -6.00462239e-01]
[ 1.74496766e-01 -9.17270483e-02 -4.70848082e-01]
[ 7.47525628e-02 -8.23606037e-02 1.64485729e-02]
[ 2.62052603e-01 6.78108940e-02 -3.54411945e-01]
[-2.62449260e-01 2.61654004e-01 -8.73355894e-02]]
print(p.get_val('cross_prod_comp.L'))
[[-0.00367897 -0.14792749 0.02341947]
[-0.36020276 -0.11867062 0.49602661]
[ 0.81596917 -0.54344711 -0.17133619]
[ 0.06296006 0.0262942 -0.02754377]
[ 0.33011027 -0.35568773 0.20893686]
[-0.18185439 0.32756256 -0.24436953]
[-0.93682767 0.2591815 0.4473025 ]
[ 0.67366314 -0.23410935 -0.45211108]
[-0.78290159 0.58151571 0.01089113]
[ 0.41919577 0.20538982 -0.22169835]
[-0.27316837 0.02353403 0.00666931]
[ 0.19118981 -0.03173933 -0.15411996]
[-0.05624731 0.52006162 -0.52207026]
[-0.16239575 0.04800226 0.0382293 ]
[-0.06721351 0.06947404 -0.08602422]
[-0.4464141 0.17354034 -0.10528453]
[ 0.00997317 -0.01078498 0.00660413]
[-0.24694921 0.36555651 0.07040187]
[-0.36381188 0.72902831 0.03182698]
[ 0.47321617 0.33648333 -0.5853363 ]
[ 0.3387598 -0.28942429 -0.16140269]
[-0.33102915 0.20812434 0.16310707]
[ 0.18326846 0.17147451 -0.45687429]
[-0.36274536 0.29095175 -0.0440793 ]]