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. |
derivs_method | N/A | ['jax', 'cs', 'fd', None] | N/A | The method to use for computing derivatives |
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. |
use_jit | True | [True, False] | ['bool'] | If True, attempt to use jit on compute_primal, assuming jax or some other AD package is active. |
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.27552315 0.09015154 0.32642129]
[-0.28547124 0.26049952 0.18356765]
[ 0.06467487 -0.05567009 -0.0169711 ]
[ 0.32258791 -0.09724875 0.02582737]
[-0.00646378 0.2637579 -0.3772856 ]
[ 0.05608588 -0.00476165 -0.06131244]
[ 0.17031783 0.07609894 -0.08610533]
[-0.02052236 -0.15141477 0.14011406]
[ 0.35214672 -0.02604526 -0.19120463]
[ 0.47169486 -0.1009618 0.01898192]
[ 0.16233154 0.00353194 -0.06711355]
[ 0.4327916 0.07856089 -0.3528101 ]
[-0.11858921 0.1355119 -0.02021974]
[ 0.23080402 -0.08533338 -0.06618155]
[-0.05116748 0.08365089 -0.11184595]
[ 0.31922071 0.25614441 -0.46894068]
[ 0.01763938 0.29068538 -0.3660516 ]
[ 0.23618523 -0.23488373 0.00307549]
[-0.18096843 0.30223834 0.0252037 ]
[ 0.02424449 -0.08815109 -0.00112148]
[-0.04260624 -0.04737143 0.05567811]
[-0.07867266 0.096881 -0.03617708]
[ 0.06969678 -0.5052452 0.09641825]
[-0.31245655 0.5506112 -0.28535324]]
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.49182977 0.47134777 0.36519971]
[ 0.0822798 -0.25251062 0.23432994]
[ 0.13367387 0.28936714 -0.39750454]
[-0.23267328 0.4314423 -0.10699204]
[-0.01298496 0.31299792 -0.20907416]
[ 0.06603084 -0.06070183 -0.00991922]
[ 0.23629615 -0.1948616 -0.21591611]
[ 0.08928669 0.17559778 -0.30824714]
[-0.35875405 0.78205517 -0.00665897]
[ 0.0352977 0.15630458 -0.25712692]
[-0.00400855 0.11128524 -0.12275046]
[-0.11935751 -0.06730519 0.19615355]
[-0.0413827 -0.14254089 0.17023316]
[ 0.36564378 0.06549149 -0.26561991]
[ 0.15925382 -0.34430486 0.54195787]
[-0.21875044 0.24243801 -0.08626851]
[-0.14928186 0.0560513 -0.00635715]
[-0.41610338 0.41128367 -0.06515757]
[-0.20519934 0.02663389 0.13772304]
[ 0.3753127 -0.54635671 0.21491539]
[-0.32711085 0.22452786 0.35621749]
[ 0.1101724 -0.77363433 0.45820853]
[-0.17908124 0.22141047 -0.00359835]
[-0.39560525 -0.00626904 0.07843042]]
print(p.get_val('cross_prod_comp.L'))
[[-3.50799075e-01 1.66848783e-01 2.80173565e-01]
[ 1.87432287e-02 -4.96810419e-02 -5.82928675e-02]
[ 1.36518944e-03 -2.78736076e-01 2.58804033e-01]
[-2.83064168e-01 -1.53001673e-01 3.66901397e-01]
[-1.40139778e-01 2.42298126e-01 -1.03241905e-01]
[-5.73225726e-03 2.90350653e-03 4.07067516e-03]
[ 9.74685218e-02 -8.46081841e-02 2.91933562e-01]
[ 2.61678961e-01 2.90070132e-01 -5.09420628e-01]
[-5.20008785e-02 1.38573409e-01 -3.01370347e-03]
[ 8.64759580e-02 1.33755339e-01 -2.31040247e-01]
[ 3.21102652e-02 -3.08988003e-01 2.86727548e-01]
[-7.55114060e-02 5.09128825e-04 1.12000470e-01]
[ 1.97695934e-01 -4.22026609e-01 1.17206077e-01]
[-3.42992700e-02 4.06815248e-01 -2.02576484e-01]
[ 4.27368306e-01 -6.52060224e-01 -2.25408101e-01]
[-1.53977516e-01 2.92632916e-01 -1.87008113e-01]
[-3.45528488e-01 4.26017982e-01 -4.91307601e-02]
[-2.19325327e-01 3.68745608e-01 -7.20322275e-02]
[-3.22886945e-01 1.58311212e-01 1.41234166e-01]
[ 2.73824412e-01 -3.02519580e-01 -4.17355392e-01]
[ 2.17690427e-02 4.37790332e-02 -1.25850243e-01]
[ 7.46360491e-02 -4.35436261e-01 2.46630406e-01]
[-3.81241258e-01 1.77091795e-01 2.58469057e-02]
[-5.62215980e-01 5.69659350e-01 8.95958443e-02]]