pymor.vectorarrays package¶
Submodules¶
block module¶
-
class
pymor.vectorarrays.block.
BlockVectorArray
(blocks, space)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorArrayInterface
VectorArray
where each vector is a direct sum of sub-vectors.Given a list of equal length
VectorArrays
blocks
, thisVectorArray
represents the direct sums of the vectors contained in the arrays. The associatedVectorSpaceInterface
isBlockVectorSpace
.BlockVectorArray
can be used in conjunction withBlockOperator
.Methods
Attributes
BlockVectorArray
data
,imag
,num_blocks
,real
VectorArrayInterface
dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.block.
BlockVectorArrayView
(base, ind)[source]¶ Bases:
pymor.vectorarrays.block.BlockVectorArray
Methods
Attributes
BlockVectorArrayView
is_view
BlockVectorArray
data
,imag
,num_blocks
,real
VectorArrayInterface
dim
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.block.
BlockVectorSpace
(subspaces, id_=None)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorSpaceInterface
VectorSpaceInterface
ofBlockVectorArrays
.A
BlockVectorSpace
is defined by theVectorSpaces
of the individual subblocks which constitute a given array. In particular for a given :class`BlockVectorArray`U
, we have the identity(U.blocks[0].space, U.blocks[1].space, ..., U.blocks[-1].space) == U.space.
Parameters
- subspaces
- The tuple defined above.
constructions module¶
-
pymor.vectorarrays.constructions.
cat_arrays
(vector_arrays)[source]¶ Return a new
VectorArray
which is a concatenation of the arrays invector_arrays
.
interfaces module¶
-
class
pymor.vectorarrays.interfaces.
VectorArrayInterface
[source]¶ Bases:
pymor.core.interfaces.BasicInterface
Interface for vector arrays.
A vector array should be thought of as a list of (possibly high-dimensional) vectors. While the vectors themselves will be inaccessible in general (e.g. because they are managed by an external PDE solver code), operations on the vectors like addition can be performed via this interface.
It is assumed that the number of vectors is small enough such that scalar data associated to each vector can be handled on the Python side. As such, methods like
l2_norm
orgramian
will always returnNumPy arrays
.An implementation of the
VectorArrayInterface
viaNumPy arrays
is given byNumpyVectorArray
. In general, it is the implementors decision how memory is allocated internally (e.g. continuous block of memory vs. list of pointers to the individual vectors.) Thus, no general assumptions can be made on the costs of operations like appending to or removing vectors from the array. As a hint for ‘continuous block of memory’ implementations,zeros
provides areserve
keyword argument which allows to specify to what size the array is assumed to grow.As with
NumPy array
,VectorArrays
can be indexed with numbers, slices and lists or one-dimensionalNumPy arrays
. Indexing will always return a newVectorArray
which acts as a view into the original data. Thus, if the indexed array is modified viascal
oraxpy
, the vectors in the original array will be changed. Indices may be negative, in which case the vector is selected by counting from the end of the array. Moreover indices can be repeated, in which case the corresponding vector is selected several times. The resulting view will be immutable, however.Note
It is disallowed to append vectors to a
VectorArray
view or to remove vectors from it. Removing vectors from an array with existing views will lead to undefined behavior of these views. As such, it is generally advisable to make acopy
of a view for long term storage. Sincecopy
has copy-on-write semantics, this will usually cause little overhead.Methods
Attributes
VectorArrayInterface
data
,dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
data
¶ Implementors can provide a
data
property which returns aNumPy array
of shape(len(v), v.dim)
containing the data stored in the array. Access should be assumed to be slow and is mainly intended for debugging / visualization purposes or to once transfer data to pyMOR and further process it using NumPy. In the case ofNumpyVectorArray
, an actual view of the internally usedNumPy array
is returned, so changing it, will alter theVectorArray
. Thus, you cannot assume to own the data returned to you, in general.
-
dim
¶ The dimension of the vectors in the array.
-
is_view
¶ True
if the array is a view obtained by indexing another array.
-
space
¶ The
VectorSpaceInterface
the array belongs to.
-
__add__
(other)[source]¶ The pairwise sum of two
VectorArrays
.
-
__getitem__
(ind)[source]¶ Return a
VectorArray
view onto a subset of the vectors in the array.
-
__iadd__
(other)[source]¶ In-place pairwise addition of
VectorArrays
.
-
__isub__
(other)[source]¶ In-place pairwise difference of
VectorArrays
.
-
__radd__
(other)¶ The pairwise sum of two
VectorArrays
.
-
__rmul__
(other)¶ Product by a scalar.
-
__sub__
(other)[source]¶ The pairwise difference of two
VectorArrays
.
-
amax
()[source]¶ The maximum absolute value of the vectors contained in the array.
Returns
- max_ind
NumPy array
containing for each vector an index at which the maximum is attained.- max_val
NumPy array
containing for each vector the maximum absolute value of its components.
-
append
(other, remove_from_other=False)[source]¶ Append vectors to the array.
Parameters
- other
- A
VectorArray
containing the vectors to be appended. - remove_from_other
- If
True
, the appended vectors are removed fromother
. For list-like implementations this can be used to prevent unnecessary copies of the involved vectors.
-
axpy
(alpha, x)[source]¶ BLAS AXPY operation.
This method forms the sum
self = alpha*x + self
If the length of
x
is 1, the samex
vector is used for all vectors inself
. Otherwise, the lengths ofself
andx
have to agree. Ifalpha
is a scalar, eachx
vector is multiplied with the same factoralpha
. Otherwise,alpha
has to be a one-dimensionalNumPy array
of the same length asself
containing the coefficients for eachx
vector.Parameters
- alpha
- The scalar coefficient or one-dimensional
NumPy array
of coefficients with which the vectors inx
are multiplied. - x
- A
VectorArray
containing the x-summands.
-
check_ind
(ind)[source]¶ Check if
ind
is an admissable list of indices in the sense of the class documentation.
-
check_ind_unique
(ind)[source]¶ Check if
ind
is an admissable list of non-repeated indices in the sense of the class documentation.
-
components
(component_indices)[source]¶ Extract components of the vectors contained in the array.
Parameters
- component_indices
- List or 1D
NumPy array
of indices of the vector components that are to be returned.
Returns
A
NumPy array
result
such thatresult[i, j]
is thecomponent_indices[j]
-th component of thei
-th vector of the array.
-
copy
(deep=False)[source]¶ Returns a copy of a subarray.
All
VectorArray
implementations in pyMOR have copy-on-write semantics: if not specified otherwise by settingdeep
toTrue
, the returned copy will hold a handle to the same array data as the original array, and a deep copy of the data will only be performed when one of the arrays is modified.Note that for
NumpyVectorArray
, a deep copy is always performed when only some vectors in the array are copied.Parameters
- deep
- Ensure that an actual copy of the array data is made (see above).
Returns
A copy of the
VectorArray
.
-
dot
(other)[source]¶ Returns the inner products between
VectorArray
elements.Parameters
- other
- A
VectorArray
containing the second factors.
-
empty
(reserve=0)[source]¶ Create an empty
VectorArray
of the sameVectorSpaceInterface
.This is a shorthand for
self.space.zeros(0, reserve)
.Parameters
- reserve
- Hint for the backend to which length the array will grow.
Returns
An empty
VectorArray
.
-
l1_norm
()[source]¶ The l1-norms of the vectors contained in the array.
Returns
A
NumPy array
result
such thatresult[i]
contains the norm ofself[i]
.
-
l2_norm
()[source]¶ The l2-norms of the vectors contained in the array.
Returns
A
NumPy array
result
such thatresult[i]
contains the norm ofself[i]
.
-
l2_norm2
()[source]¶ The squared l2-norms of the vectors contained in the array.
Returns
A
NumPy array
result
such thatresult[i]
contains the norm ofself[i]
.
-
lincomb
(coefficients)[source]¶ Returns linear combinations of the vectors contained in the array.
Parameters
- coefficients
- A
NumPy array
of dimension 1 or 2 containing the linear coefficients.coefficients.shape[-1]
has to agree withlen(self)
.
Returns
A
VectorArray
result
such thatresult[i] = ∑ self[j] * coefficients[i,j]in case
coefficients
is of dimension 2, otherwiselen(result) == 1
andresult[0] = ∑ self[j] * coefficients[j].
-
normalize_ind
(ind)[source]¶ Normalize given indices such that they are independent of the array length.
-
pairwise_dot
(other)[source]¶ Returns the pairwise inner products between
VectorArray
elements.Parameters
- other
- A
VectorArray
containing the second factors.
-
scal
(alpha)[source]¶ BLAS SCAL operation (in-place scalar multiplication).
This method calculates
self = alpha*self
If
alpha
is a scalar, each vector is multiplied by this scalar. Otherwise,alpha
has to be a one-dimensionalNumPy array
of the same length asself
containing the factors for each vector.Parameters
- alpha
- The scalar coefficient or one-dimensional
NumPy array
of coefficients with which the vectors inself
are multiplied.
-
sup_norm
()[source]¶ The l-infinity–norms of the vectors contained in the array.
Returns
A
NumPy array
result
such thatresult[i]
contains the norm ofself[i]
.
-
zeros
(count=1, reserve=0)[source]¶ Create a
VectorArray
of null vectors of the sameVectorSpaceInterface
.This is a shorthand for
self.space.zeros(count, reserve)
.Parameters
- count
- The number of vectors.
- reserve
- Hint for the backend to which length the array will grow.
Returns
A
VectorArray
containingcount
vectors whith each component zero.
-
-
class
pymor.vectorarrays.interfaces.
VectorSpaceInterface
[source]¶ Bases:
pymor.core.interfaces.ImmutableInterface
Class describing a vector space.
Vector spaces act as factories for
VectorArrays
of vectors contained in them. As such, they hold all data necessary to createVectorArrays
of a given type (e.g. the dimension of the vectors, or a socket for communication with an external PDE solver).New
VectorArrays
of null vectors are created viazeros
. Themake_array
method builds a newVectorArray
from given raw data of the underlying linear algebra backend (e.g. aNumPy array
in the case ofNumpyVectorSpace
). Some vector spaces can create newVectorArrays
from a givenNumPy array
via thefrom_data
method.Each vector space has a string
id
to distinguish mathematically different spaces appearing in the formulation of a given problem.Vector spaces can be compared for equality via the
==
and!=
operators. To test if a givenVectorArray
is an element of the space, thein
operator can be used.Methods
Attributes
VectorSpaceInterface
dim
,id
,is_scalar
ImmutableInterface
add_with_arguments
,sid
,sid_ignore
,with_arguments
BasicInterface
logger
,logging_disabled
,name
,uid
-
id
¶ None, or a string describing the mathematical identity of the vector space (for instance to distinguish different components in an equation system).
-
dim
¶ The dimension (number of degrees of freedom) of the vectors contained in the space.
-
is_scalar
¶ Equivalent to
isinstance(space, NumpyVectorSpace) and space.dim == 1
.
-
empty
(reserve=0)[source]¶ Create an empty
VectorArray
This is a shorthand for
self.zeros(0, reserve)
.Parameters
- reserve
- Hint for the backend to which length the array will grow.
Returns
An empty
VectorArray
.
-
from_data
(data)[source]¶ Create a
VectorArray
from aNumPy array
Note that this method will not be supported by all vector space implementations.
Parameters
- data
NumPy
array.
Returns
A
VectorArray
withdata
as data.
-
make_array
(*args, **kwargs)[source]¶ Create a
VectorArray
from raw data.This method is used in the implementation of
Operators
andDiscretizations
to create newVectorArrays
from raw data of the underlying solver backends. The ownership of the data is transferred to the newly created array.The exact signature of this method depends on the wrapped solver backend.
-
zeros
(count=1, reserve=0)[source]¶ Create a
VectorArray
of null vectorsParameters
- count
- The number of vectors.
- reserve
- Hint for the backend to which length the array will grow.
Returns
A
VectorArray
containingcount
vectors with each component zero.
-
list module¶
-
class
pymor.vectorarrays.list.
CopyOnWriteVector
[source]¶ Bases:
pymor.vectorarrays.list.VectorInterface
Methods
CopyOnWriteVector
axpy
,copy
,from_instance
,scal
VectorInterface
amax
,components
,dot
,l1_norm
,l2_norm
,l2_norm2
,sup_norm
BasicInterface
disable_logging
,enable_logging
,has_interface_name
,implementor_names
,implementors
Attributes
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.list.
ListVectorArray
(vectors, space)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorArrayInterface
VectorArray
implemented as a Python list of vectors.This
VectorArray
implementation is the first choice when creating pyMOR wrappers for external solvers which are based on single vector objects. In order to do so, a wrapping subclass ofVectorInterface
has to be provided on which the implementation ofListVectorArray
will operate. The associatedVectorSpaceInterface
is a subclass ofListVectorSpace
.For an example, see
NumpyVector
,NumpyListVectorSpace
orFenicsVector
,FenicsVectorSpace
.Methods
Attributes
ListVectorArray
data
VectorArrayInterface
dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.list.
ListVectorArrayView
(base, ind)[source]¶ Bases:
pymor.vectorarrays.list.ListVectorArray
Methods
-
class
pymor.vectorarrays.list.
ListVectorSpace
[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorSpaceInterface
VectorSpaceInterface
ofListVectorArrays
.Methods
ListVectorSpace
from_data
,make_array
,make_vector
,space_from_dim
,space_from_vector_obj
,vector_from_data
,zero_vector
,zeros
VectorSpaceInterface
empty
ImmutableInterface
generate_sid
,with_
,__setattr__
BasicInterface
disable_logging
,enable_logging
,has_interface_name
,implementor_names
,implementors
-
class
pymor.vectorarrays.list.
NumpyListVectorSpace
(dim, id_=None)[source]¶ Bases:
pymor.vectorarrays.list.ListVectorSpace
Methods
NumpyListVectorSpace
make_vector
,space_from_dim
,space_from_vector_obj
,vector_from_data
,zero_vector
ListVectorSpace
from_data
,make_array
,zeros
VectorSpaceInterface
empty
ImmutableInterface
generate_sid
,with_
,__setattr__
BasicInterface
disable_logging
,enable_logging
,has_interface_name
,implementor_names
,implementors
-
class
pymor.vectorarrays.list.
NumpyVector
(array)[source]¶ Bases:
pymor.vectorarrays.list.CopyOnWriteVector
Vector stored in a NumPy 1D-array.
Methods
NumpyVector
amax
,components
,dot
,from_instance
,l1_norm
,l2_norm
,l2_norm2
CopyOnWriteVector
axpy
,copy
,scal
VectorInterface
sup_norm
BasicInterface
disable_logging
,enable_logging
,has_interface_name
,implementor_names
,implementors
Attributes
NumpyVector
data
,dim
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.list.
VectorInterface
[source]¶ Bases:
pymor.core.interfaces.BasicInterface
Interface for vectors used in conjunction with
ListVectorArray
.This interface must be staisfied by the individual entries of the vector
list
managed byListVectorArray
. All interface methods have a direct counterpart in theVectorArray
interface.Methods
VectorInterface
amax
,axpy
,components
,copy
,dot
,l1_norm
,l2_norm
,l2_norm2
,scal
,sup_norm
BasicInterface
disable_logging
,enable_logging
,has_interface_name
,implementor_names
,implementors
Attributes
BasicInterface
logger
,logging_disabled
,name
,uid
mpi module¶
Wrapper classes for building MPI distributed VectorArrays
.
This module contains several wrapper classes which allow to
transform single rank VectorArrays
into MPI distributed
VectorArrays
which can be used on rank 0 like ordinary
VectorArrays
.
The implementations are based on the event loop provided
by pymor.tools.mpi
.
-
class
pymor.vectorarrays.mpi.
MPIVectorArray
(obj_id, space)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorArrayInterface
MPI distributed
VectorArray
.Given a local
VectorArray
on each MPI rank, this wrapper class uses the event loop frompymor.tools.mpi
to build a global MPI distributed vector array from these local arrays.Instances of
MPIVectorArray
can be used on rank 0 like any other (non-distributed)VectorArray
.Note, however, that the implementation of the local VectorArrays needs to be MPI aware. For instance,
cls.dot
must perform the needed MPI communication to sum up the local inner products and return the sums on rank 0.Default implementations for all communication requiring interface methods are provided by
MPIVectorArrayAutoComm
(also seeMPIVectorArrayNoComm
).Note that resource cleanup is handled by
object.__del__
. Please be aware of the peculiarities of destructors in Python!The associated
VectorSpaceInterface
isMPIVectorSpace
.Methods
Attributes
VectorArrayInterface
data
,dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.mpi.
MPIVectorArrayAutoComm
(obj_id, space)[source]¶ Bases:
pymor.vectorarrays.mpi.MPIVectorArray
MPI distributed
VectorArray
.This is a subclass of
MPIVectorArray
which provides default implementations for all communication requiring interface methods for the case when the wrapped array is not MPI aware.Note, however, that depending on the discretization these default implementations might lead to wrong results (for instance in the presence of shared DOFs).
The associated
VectorSpaceInterface
isMPIVectorSpaceAutoComm
.Methods
Attributes
VectorArrayInterface
data
,dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.mpi.
MPIVectorArrayNoComm
(obj_id, space)[source]¶ Bases:
pymor.vectorarrays.mpi.MPIVectorArray
MPI distributed
VectorArray
.This is a subclass of
MPIVectorArray
which overrides all communication requiring interface methods to raiseNotImplementedError
.This is mainly useful as a security measure when wrapping arrays for which simply calling the respective method on the wrapped arrays would lead to wrong results and
MPIVectorArrayAutoComm
cannot be used either (for instance in the presence of shared DOFs).The associated
VectorSpaceInterface
isMPIVectorSpaceNoComm
.Methods
Attributes
VectorArrayInterface
data
,dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.mpi.
MPIVectorSpace
(local_spaces)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorSpaceInterface
VectorSpaceInterface
ofMPIVectorArrays
.Parameters
- local_spaces
tuple
of the differentVectorSpaces
of the localVectorArrays
on the MPI ranks. Alternatively, the length oflocal_spaces
may be 1, in which case the sameVectorSpaceInterface
is assumed for all ranks.
Methods
Attributes
MPIVectorSpace
array_type
,dim
VectorSpaceInterface
id
,is_scalar
ImmutableInterface
add_with_arguments
,sid
,sid_ignore
,with_arguments
BasicInterface
logger
,logging_disabled
,name
,uid
-
array_type
¶ alias of
MPIVectorArray
-
make_array
(obj_id)[source]¶ Create array from rank-local
VectorArray
instances.Parameters
- obj_id
ObjectId
of the MPI distributed instances ofcls
wrapped by this array.
Returns
The newly created : class:
MPIVectorArray
.
-
class
pymor.vectorarrays.mpi.
MPIVectorSpaceAutoComm
(local_spaces)[source]¶ Bases:
pymor.vectorarrays.mpi.MPIVectorSpace
VectorSpaceInterface
forMPIVectorArrayAutoComm
.Methods
Attributes
MPIVectorSpaceAutoComm
array_type
,dim
VectorSpaceInterface
id
,is_scalar
ImmutableInterface
add_with_arguments
,sid
,sid_ignore
,with_arguments
BasicInterface
logger
,logging_disabled
,name
,uid
-
array_type
¶ alias of
MPIVectorArrayAutoComm
-
-
class
pymor.vectorarrays.mpi.
MPIVectorSpaceNoComm
(local_spaces)[source]¶ Bases:
pymor.vectorarrays.mpi.MPIVectorSpace
VectorSpaceInterface
forMPIVectorArrayNoComm
.Methods
Attributes
MPIVectorSpaceNoComm
array_type
,dim
VectorSpaceInterface
id
,is_scalar
ImmutableInterface
add_with_arguments
,sid
,sid_ignore
,with_arguments
BasicInterface
logger
,logging_disabled
,name
,uid
-
array_type
¶ alias of
MPIVectorArrayNoComm
-
numpy module¶
-
class
pymor.vectorarrays.numpy.
NumpyVectorArray
(array, space)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorArrayInterface
VectorArray
implementation viaNumPy arrays
.This is the default
VectorArray
type used by allOperators
in pyMOR’s discretization toolkit. Moreover, all reducedOperators
are based onNumpyVectorArray
.This class is just a thin wrapper around the underlying
NumPy array
. Thus, while operations likeaxpy
ordot
will be quite efficient, removing or appending vectors will be costly.The associated
VectorSpaceInterface
isNumpyVectorSpace
.Methods
Attributes
NumpyVectorArray
data
,imag
,real
VectorArrayInterface
dim
,is_view
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.numpy.
NumpyVectorArrayView
(array, ind)[source]¶ Bases:
pymor.vectorarrays.numpy.NumpyVectorArray
Methods
Attributes
NumpyVectorArrayView
data
,is_view
NumpyVectorArray
imag
,real
VectorArrayInterface
dim
,space
BasicInterface
logger
,logging_disabled
,name
,uid
-
class
pymor.vectorarrays.numpy.
NumpyVectorSpace
(dim, id_=None)[source]¶ Bases:
pymor.vectorarrays.interfaces.VectorSpaceInterface
VectorSpaceInterface
ofNumpyVectorArrays
.Parameters
- dim
- The dimension of the vectors contained in the space.
- id
- See
id
.