VectorMagnitudeComp

VectorMagnitudeComp computes the magnitude (L2 norm) of a single input of some given length. It may be vectorized to provide the result at one or more points simultaneously.

\[\lvert a_i \rvert = \sqrt{\bar{a}_i \cdot \bar{a}_i}\]

VectorMagnitudeComp Options

The default vec_size is 1, providing the magnitude of \(a\) at a single point. The length of \(a\) is provided by option length.

Other options for VectorMagnitudeComp allow the user to rename the input variable \(a\) and the output \(a_mag\), as well as specifying their units.

Option

Default

Acceptable Values

Acceptable Types

Description

distributed

False

[True, False]

[‘bool’]

True if the component has variables that are distributed across multiple processes.

in_name

a

N/A

[‘str’]

The variable name for input vector.

length

3

N/A

[‘int’]

The length of the input vector at each point

mag_name

a_mag

N/A

[‘str’]

The variable name for output vector magnitude.

units

None

N/A

[‘str’]

The units of the input vector.

vec_size

1

N/A

[‘int’]

The number of points at which the vector magnitude is computed

VectorMagnitudeComp Constructor

The call signature for the VectorMagnitudeComp constructor is:

VectorMagnitudeComp.__init__(**kwargs)[source]

Initialize the Vector Magnitude component.

Parameters
**kwargsdict of keyword arguments

Keyword arguments that will be mapped into the Component options.

VectorMagnitudeComp Usage

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

The add_magnitude method is used to create additional magnitude calculations after instantiation.

VectorMagnitudeComp.add_magnitude(mag_name, in_name, units=None, vec_size=1, length=3)[source]

Add a new output magnitude to the vector magnitude component.

Parameters
mag_namestr

The name of the output vector magnitude.

in_namestr

The name of the input vector.

unitsstr or None

The units of the input vector.

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)

VectorMagnitudeComp Example

In the following example VectorMagnitudeComp is used to compute the radius vector magnitude given a radius 3-vector at 100 points simultaneously. Note the use of in_name and mag_name to assign names to the inputs and outputs. Units are assigned using units. The units of the output magnitude are the same as those for the input.

"""
A simple example to compute the magnitude of 3-vectors at at 100 points simultaneously.
"""
import numpy as np
import openmdao.api as om

n = 100

p = om.Problem()

comp = om.VectorMagnitudeComp(vec_size=n, length=3,
                              in_name='r', mag_name='r_mag', units='km')

p.model.add_subsystem(name='vec_mag_comp', subsys=comp,
                      promotes_inputs=[('r', 'pos')])

p.setup()

p.set_val('pos', 1.0 + 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('pos')[i, :]
    expected.append(np.sqrt(np.dot(a_i, a_i)))

    actual_i = p.get_val('vec_mag_comp.r_mag')[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('vec_mag_comp.r_mag'))
[2.92565784 2.16044183 2.31730727 2.33953592 2.26899682 2.44831971
 2.8386732  2.02193652 2.55865661 2.53510644 2.38975741 3.03360093
 2.44826068 2.22086739 2.5419794  2.27474131 3.00598107 2.32339781
 2.61135479 2.65356271 2.7235586  2.79145057 2.92496341 2.74002576
 2.70196544 2.9003804  2.84754857 2.90108702 3.20234351 2.43991197
 2.17750138 2.88518187 2.17039211 2.6627935  2.91347532 2.3225808
 2.16914616 2.17935671 2.0835086  2.21088238 3.13760964 2.50180197
 2.87620399 2.44233747 2.57487576 2.47013871 2.6010193  2.85370198
 2.57526253 3.38346775 2.68518479 2.55940814 2.64022517 2.65739808
 2.545411   2.2749338  2.67802082 3.15124567 3.1173942  3.15567888
 2.03617285 2.42121397 3.06216617 2.5599269  2.82587129 2.3875132
 2.3544145  2.69728429 2.99146444 2.18235626 2.46303262 2.89519733
 2.462069   2.74950607 2.23661957 2.81448121 2.05253251 2.46378828
 2.27939904 3.0769206  2.50597803 2.72223968 2.90637242 2.48807411
 2.69429217 2.16480382 2.96922629 2.95144277 2.886078   3.30481195
 2.47770486 2.72591846 2.64516299 2.78306224 2.4720782  1.93914569
 2.79151012 2.955982   2.74754299 2.82504778]

VectorMagnitudeComp Example with Multiple Magnitudes

Note that, when defining multiple magnitudes, an input name in one call to add_magnitude may not be an output name in another call, and vice-versa.

"""
A simple example to compute the magnitude of 3-vectors at at 100 points simultaneously.
"""
import numpy as np
import openmdao.api as om

n = 100

p = om.Problem()

comp = om.VectorMagnitudeComp(vec_size=n, length=3,
                              in_name='r', mag_name='r_mag', units='km')

comp.add_magnitude(vec_size=n, length=3,
                   in_name='b', mag_name='b_mag', units='ft')

p.model.add_subsystem(name='vec_mag_comp', subsys=comp,
                      promotes_inputs=['r', 'b'])

p.setup()

p.set_val('r', 1.0 + np.random.rand(n, 3))
p.set_val('b', 1.0 + np.random.rand(n, 3))

p.run_model()

# Verify the results against numpy.dot in a for loop.
expected_r = []
expected_b = []
for i in range(n):
    a_i = p.get_val('r')[i, :]
    expected_r.append(np.sqrt(np.dot(a_i, a_i)))

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

    b_i = p.get_val('b')[i, :]
    expected_b.append(np.sqrt(np.dot(b_i, b_i)))

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

print(p.get_val('vec_mag_comp.r_mag'))
[2.44273332 2.42839457 2.3401009  2.40447953 2.52398678 2.66250531
 2.78873692 2.98865895 3.25429976 2.44638211 2.17323385 3.04909232
 2.2362899  3.19523397 2.79262415 2.62703807 2.6295604  2.32549013
 2.59639421 2.69969564 2.59311527 2.35503502 2.74568506 2.58737757
 2.65948422 2.8418642  2.27647961 2.41329744 2.59090664 2.77993372
 2.62707356 2.48163943 2.1819349  2.60325653 1.95889216 1.98671363
 2.43552838 2.77468604 2.1960725  2.70742647 2.79981526 3.01017882
 3.14696409 2.45145259 2.38553801 2.6045309  1.92082882 2.5061063
 2.70411424 3.01608156 2.7964375  2.72821466 2.10752167 2.70555443
 2.721939   3.10582884 3.14396283 2.09503833 2.73552742 2.96563768
 2.59415821 2.42028586 2.21065511 2.28911066 2.77517191 2.68605644
 2.20125807 2.56304483 3.16449435 2.9259501  2.4764517  2.49416653
 2.53256546 2.35493512 2.15006805 2.94359608 2.67812535 2.6289887
 3.02297877 2.39041826 2.14773468 3.00264275 2.62262306 2.98057779
 2.75045669 2.55182828 2.6267111  2.67196277 2.78069645 2.70477302
 2.71303869 2.36341456 2.75012992 2.76536313 2.97973006 2.82082968
 2.61957957 2.60102487 2.8909568  2.57304028]
print(p.get_val('vec_mag_comp.b_mag'))
[2.56210475 2.88488994 2.10166336 2.44370877 2.93207278 2.47528235
 2.75011594 2.84988053 2.95498707 2.38815695 2.54690999 2.58829238
 2.18621864 2.4692216  2.44919723 2.54102619 2.7861883  2.62931674
 2.32225809 3.05334049 2.31470559 2.3686142  2.51664682 2.81514157
 3.16678953 2.33540434 2.81520193 2.94312324 3.07142005 2.82384925
 2.07031508 2.54797729 2.7499096  2.52394506 2.48753772 2.62783384
 2.79638573 2.23746548 2.29204809 2.67289346 2.61565809 3.22109541
 2.75856511 2.14043312 3.25511673 2.23538551 2.21418867 2.71373553
 2.41327205 3.08040186 2.19785082 2.53446177 2.8049886  2.71051694
 2.07780624 2.47821199 2.40507514 2.60970162 2.38541522 2.98350434
 2.53077341 3.15452231 2.76427572 2.32664596 2.80063192 2.82802824
 2.58804278 2.94394352 3.07058471 2.31799415 2.30582448 2.2419599
 2.64989596 3.02305857 2.78278037 3.24258045 2.18702176 2.65214598
 2.84937419 2.82246284 2.15822996 2.44989872 2.39188781 2.57517153
 2.52286514 2.98241005 2.6371966  2.63668508 2.1873197  2.99928575
 2.41339406 3.05595011 2.70361295 2.82062597 2.49675284 2.7394093
 3.24047301 2.67549541 2.66871978 2.15702731]