vector.py#

Define the base Vector and Transfer classes.

class openmdao.vectors.vector.Vector(name, kind, system, parent_vector=None, alloc_complex=False)[source]

Bases: object

Base Vector class.

This class is instantiated for inputs, outputs, and residuals. It provides a dictionary interface and an arithmetic operations interface.

Parameters:
namestr

The name of the vector: ‘nonlinear’ or ‘linear’.

kindstr

The kind of vector, ‘input’, ‘output’, or ‘residual’.

system<System>

The owning system.

parent_vectorVector

Parent vector.

alloc_complexbool

Whether to allocate any imaginary storage to perform complex step. Default is False.

Attributes:
_resolver<NameResolver>

Name resolver for the owning system.

_lookupfunction

Function to lookup a name in the name resolver.

_namestr

The name of the vector: ‘nonlinear’ or ‘linear’.

_iotypestr

Type: ‘input’ for input vectors; ‘output’ for output/residual vectors.

_kindstr

Specific kind of vector, either ‘input’, ‘output’, or ‘residual’.

_viewsdict

Dictionary mapping absolute variable names to the ndarray views.

_isrootbool

Whether this vector is the root vector.

_namesset([str, …])

Set of variables that are relevant in the current context.

_alloc_complexbool

If True, then space for the complex vector is also allocated.

_datandarray

Actual allocated data.

_parent_sliceslice

Slice of the parent vector that this vector represents.

_under_complex_stepbool

When True, this vector is under complex step, and data is swapped with the complex data.

_scalingtuple

If scaling is active, this is a tuple of (scale_factor, adder) for _data.

_has_solver_refbool

This is set to True only when a ref is defined on a solver.

_nlvecVector or None

Nonlinear vector.

read_onlybool

When True, values in the vector cannot be changed via the user __setitem__ API.

Methods

add_scal_vec(val, vec)

Perform in-place addition of a vector times a scalar.

add_to_slice(slc, val)

Add a value to a slice of the vector.

asarray([copy])

Return a flat array representation of this vector.

dot(vec)

Compute the dot product of the current vec and the incoming vec.

get_hash([alg])

Return a hash string for the array contained in this Vector.

get_info(name)

Get the info object for the given variable.

get_mask()

Return a mask for the vector based on the current matvec context.

get_norm()

Return the norm of this vector.

get_slice(slc)

Get a slice of the vector.

get_val(name[, flat])

Get the variable value.

iscomplex()

Return True if this vector contains complex values.

items([relative_to])

Return (name, value) for variables contained in this vector.

keys()

Return variable names of variables contained in this vector (relative names).

nvars()

Return the number of variables in this Vector.

ranges()

Yield (name, start, stop) for variables contained in this vector.

set_complex_step_mode(active)

Turn on or off complex stepping mode.

set_val(val[, idxs])

Set the data array of this vector to a scalar or array value, with optional indices.

set_vals(vals)

Set the data array of this vector using a value or iter of values, one for each variable.

set_var(name, val[, idxs, flat, var_name])

Set the array view corresponding to the named variable, with optional indexing.

set_vec(vec)

Set the value of this vector to that of the incoming vector.

values()

Return values of variables contained in this vector.

__contains__(name)[source]

Check if the variable is found in this vector.

Parameters:
namestr

Promoted or relative variable name in the owning system’s namespace.

Returns:
bool

True or False.

__getitem__(name)[source]

Get the variable value.

Parameters:
namestr

Promoted or relative variable name in the owning system’s namespace.

Returns:
float or ndarray

variable value.

__init__(name, kind, system, parent_vector=None, alloc_complex=False)[source]

Initialize all attributes.

__iter__()[source]

Return an iterator over variables involved in the current mat-vec product (relative names).

Returns:
listiterator

iterator over the variable names.

__setitem__(name, value)[source]

Set the variable value.

Parameters:
namestr

Promoted or relative variable name in the owning system’s namespace.

valuefloat or list or tuple or ndarray

variable value to set

add_scal_vec(val, vec)[source]

Perform in-place addition of a vector times a scalar.

Must be implemented by the subclass.

Parameters:
valint or float

Scalar.

vec<Vector>

This vector times val is added to self.

add_to_slice(slc, val)[source]

Add a value to a slice of the vector.

Parameters:
slcslice

Slice of the vector.

valfloat or ndarray

Value to add.

asarray(copy=False)[source]

Return a flat array representation of this vector.

If copy is True, return a copy. Otherwise, try to avoid it.

Parameters:
copybool

If True, return a copy of the array.

Returns:
ndarray

Array representation of this vector.

cite = ''
distributed = False
dot(vec)[source]

Compute the dot product of the current vec and the incoming vec.

Must be implemented by the subclass.

Parameters:
vec<Vector>

The incoming vector being dotted with self.

property dtype

Return the dtype of this vector.

Returns:
dtype

The dtype of this vector.

get_hash(alg=<built-in function openssl_sha1>)[source]

Return a hash string for the array contained in this Vector.

Parameters:
algfunction

Algorithm used to generate the hash. Default is hashlib.sha1.

Returns:
str

The hash string.

get_info(name)[source]

Get the info object for the given variable.

Parameters:
namestr

Name of the variable.

Returns:
_VecData

Info object for the variable.

get_mask()[source]

Return a mask for the vector based on the current matvec context.

Returns:
slice or index array

Values at this index will be zeroed out.

get_norm()[source]

Return the norm of this vector.

Must be implemented by the subclass.

Returns:
float

Norm of this vector.

get_slice(slc)[source]

Get a slice of the vector.

Parameters:
slcslice

Slice of the vector.

Returns:
ndarray

Slice of the vector.

get_val(name, flat=True)[source]

Get the variable value.

Parameters:
namestr

Promoted or relative variable name in the owning system’s namespace.

flatbool

If True, return the flat value.

Returns:
float or ndarray

Variable value.

iscomplex()[source]

Return True if this vector contains complex values.

This checks the type of the values, not whether they have a nonzero imaginary part.

Returns:
bool

True if this vector contains complex values.

items(relative_to=None)[source]

Return (name, value) for variables contained in this vector.

Parameters:
relative_tostr or None

If not None, return names relative to this pathname.

Yields:
str

Relative name of each variable.

ndarray or float

Value of each variable.

keys()[source]

Return variable names of variables contained in this vector (relative names).

Returns:
listiterator (Python 3.x) or list (Python 2.x)

The variable names.

nvars()[source]

Return the number of variables in this Vector.

Returns:
int

Number of variables in this Vector.

ranges()[source]

Yield (name, start, stop) for variables contained in this vector.

Yields:
str

Name of each variable.

int

Start index of the variable.

int

Stop index of the variable.

set_complex_step_mode(active)[source]

Turn on or off complex stepping mode.

Parameters:
activebool

Complex mode flag; set to True prior to commencing complex step.

set_val(val, idxs=slice(None, None, None))[source]

Set the data array of this vector to a scalar or array value, with optional indices.

Must be implemented by the subclass.

Parameters:
valfloat or ndarray

Scalar or array to set data array to.

idxsint or slice or tuple of ints and/or slices

The locations where the data array should be updated.

set_vals(vals)[source]

Set the data array of this vector using a value or iter of values, one for each variable.

The values must be in the same order and size as the variables appear in this Vector.

Parameters:
valsiter of ndarrays

Values for each variable contained in this vector, in the proper order.

set_var(name, val, idxs=slice(None, None, None), flat=False, var_name=None)[source]

Set the array view corresponding to the named variable, with optional indexing.

Parameters:
namestr

The name of the variable.

valfloat or ndarray

Scalar or array to set data array to.

idxsint or slice or tuple of ints and/or slices

The locations where the data array should be updated.

flatbool

If True, set into flattened variable.

var_namestr or None

If specified, the variable name to use when reporting errors. This is useful when setting an AutoIVC value that the user only knows by a connected input name.

set_vec(vec)[source]

Set the value of this vector to that of the incoming vector.

Must be implemented by the subclass.

Parameters:
vec<Vector>

The vector whose values self is set to.

values()[source]

Return values of variables contained in this vector.

Yields:
ndarray or float

Value of each variable.