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

OptionDefaultAcceptable ValuesAcceptable TypesDescription
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.
distributedFalse[True, False]['bool']If True, set all variables in this component as distributed across multiple processes
in_nameaN/A['str']The variable name for input vector.
length3N/A['int']The length of the input vector at each point
mag_namea_magN/A['str']The variable name for output vector magnitude.
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.
unitsN/AN/A['str']The units of the input vector.
vec_size1N/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.

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.

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.49795141 2.6816422  2.70413202 2.70332911 2.57557242 2.79179718
 2.27233549 2.89215941 2.54704004 2.46765744 2.55694846 3.054348
 2.83304762 2.34835474 2.30490184 3.12789329 2.04766464 3.0246854
 2.02901392 2.51505562 2.30050687 2.95919872 2.50905247 2.26728719
 2.4185627  2.26084348 2.11512945 3.06974951 2.73802279 2.384015
 2.38214963 2.75902086 2.72716004 2.46214747 2.62805962 2.48891985
 2.49103495 2.31873309 2.87196156 2.77070588 1.98896654 2.71816632
 2.75500411 3.0034726  2.22240506 2.65903451 2.52787691 2.61272587
 2.44903832 2.87493956 3.0445416  2.42409161 2.75065784 2.62018788
 1.93065664 2.47257231 2.90373374 2.19479356 2.77228937 2.6147602
 2.43477886 2.76487981 2.85534365 2.41886052 2.52172016 2.74544504
 2.67108015 2.91322688 2.47628526 2.75213374 2.3811767  2.20333354
 2.81853853 2.02151294 2.59191222 3.1182031  2.57719061 2.6144263
 2.38439894 2.90117617 2.89646155 2.1018919  2.22121261 2.72584702
 2.66510201 2.81779245 2.58165595 2.71361354 2.37024109 1.87754424
 2.42055752 2.61228962 2.87792069 3.02684774 3.31332771 2.58034028
 2.57163158 2.35957259 2.35709088 2.51051466]

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.

import numpy as np

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.32329934 2.60691768 2.8568042  2.85535249 2.50267174 2.90141522
 2.77659765 2.6171108  2.28519419 2.78948542 1.91833067 2.4808794
 2.15060891 2.62651687 2.72350105 2.60387584 2.58145482 3.2180209
 2.52796296 3.05054779 2.57704036 3.17204445 2.76523811 2.73597275
 2.58432699 2.92430693 2.56491188 2.74472865 2.83609567 2.0984813
 2.85466431 2.34453454 1.98935021 2.30700722 3.28892629 1.94785274
 2.77651144 3.04867634 2.35113993 2.5524799  2.77698497 2.42564889
 1.93764063 2.21026099 2.64983202 2.30591685 2.20369044 2.51257437
 2.88165095 2.89129511 2.75084447 2.481848   2.67149001 2.28233089
 2.91225847 3.10180279 2.27134678 2.44568261 2.78689935 2.70502598
 2.9226176  2.67325509 3.05100229 2.5025404  2.51189551 2.3805529
 2.04708709 2.9179729  2.47875132 3.0401525  2.80778249 2.83319899
 2.39103583 2.30184142 2.66596025 2.94315219 2.79458513 2.84104484
 2.30475172 2.58031409 3.07770334 2.84347089 2.70145318 2.31602471
 1.99244603 2.42277141 2.78316773 2.47443799 2.44826545 2.88321864
 2.2862289  2.07083061 2.87619156 2.68412194 2.93607527 2.69525124
 2.16370964 2.65865934 2.82846595 2.48691145]
print(p.get_val('vec_mag_comp.b_mag'))
[2.62765116 2.60919333 3.14295549 2.3213948  2.85495479 2.26804546
 2.80051226 2.72162119 2.69269069 2.57439636 2.41618725 2.65417017
 3.02180081 2.31128634 2.82233761 2.96632383 2.24831334 3.12187149
 2.38237096 3.15852736 2.55057333 3.04360357 3.17720672 2.36488567
 2.81688523 2.71080527 2.32857951 2.26026158 1.88788586 2.4130153
 2.82542293 3.15926081 2.10922254 2.25995524 2.75933406 2.30687735
 3.14228477 2.73278926 2.82140173 2.56763276 2.59459315 2.66716787
 3.04382076 2.05413049 2.35808373 2.18925152 2.723087   3.07257116
 2.94205656 2.23474406 2.4981091  2.40302664 2.81850148 2.44262199
 3.07629326 2.42632674 2.79837955 2.82941179 3.00986156 2.82055211
 2.90296234 2.21502509 3.03139361 2.28876648 2.8875978  2.97146028
 2.19651521 2.64090327 2.56671346 2.71897994 3.11058014 2.10156705
 2.63051817 2.69616357 2.57863194 2.99184529 2.17899693 2.83066101
 2.95846793 3.13119614 2.0276903  2.90159986 2.17244633 2.70173503
 2.62621129 2.67780101 2.84573285 2.68400934 2.57110835 3.16802401
 3.10858021 2.72907409 2.30596275 2.36140714 2.98685696 2.85345731
 2.52563625 2.47866505 2.46943724 2.51195598]