proteus.LinearAlgebraTools module

Tools for n-dimensional linear algebra

Vectors are just numpy arrays, as are dense matrices. Sparse matrices are CSR matrices. Parallel vector and matrix are built on top of those representations using PETSc.

Inheritance diagram of proteus.LinearAlgebraTools

proteus.LinearAlgebraTools.superlu_sparse_2_dense(sparse_matrix, output=False)[source]

Converts a sparse superluWrapper into a dense matrix.

Parameters:
  • sparse_matrix
  • output (str) – Out file name to store the matrix.
Returns:

dense_matrix – A numpy array storing the dense matrix.

Return type:

numpy array

Notes

This function should not be used for large matrices.

proteus.LinearAlgebraTools.petsc4py_sparse_2_dense(sparse_matrix, output=False)[source]

Converts a PETSc4Py matrix to a dense numpyarray.

Parameters:
  • sparse_matrix (PETSc4py matrix) –
  • output (str) – Output file name to store the matrix.
Returns:

dense_matrix – A numpy array with the dense matrix.

Return type:

numpy array

Notes

This function is very inefficient for large matrices.

proteus.LinearAlgebraTools.superlu_2_petsc4py(sparse_superlu)[source]

Copy a sparse superlu matrix to a sparse petsc4py matrix

Parameters:sparse_matrix (proteus.superluWrappers.SparseMatrix) –
Returns:sparse_matrix
Return type:PETSc4py matrix
class proteus.LinearAlgebraTools.ParVec(array, blockSize, n, N, nghosts=None, subdomain2global=None, blockVecType='simple')[source]

A parallel vector built on top of daetk’s wrappers for petsc

scatter_forward_insert()[source]
scatter_reverse_add()[source]
class proteus.LinearAlgebraTools.ParVec_petsc4py(array=None, bs=None, n=None, N=None, nghosts=None, subdomain2global=None, blockVecType='simple', ghosts=None, proteus2petsc_subdomain=None, petsc2proteus_subdomain=None)[source]

Bases: petsc4py.PETSc.Vec

Parallel vector using petsc4py’s wrappers for PETSc WIP – This function builds the local to global mapping for the PETSc parallel vectors. At this point it only works when the variables can be interwoven (eg. stablized elements where velocity and pressure come from the same space). We would like to extend this functionality to include finite element spaces that cannot be interwoven such as Taylor Hood.

scatter_forward_insert()[source]
scatter_reverse_add()[source]
save(filename)[source]

Saves to disk using a PETSc binary viewer.

class proteus.LinearAlgebraTools.ParMat_petsc4py(ghosted_csr_mat=None, par_bs=None, par_n=None, par_N=None, par_nghost=None, subdomain2global=None, blockVecType='simple', pde=None, par_nc=None, par_Nc=None, proteus_jacobian=None, nzval_proteus2petsc=None)[source]

Bases: petsc4py.PETSc.Mat

Parallel matrix based on petsc4py’s wrappers for PETSc.

save(filename)[source]

Saves to disk using a PETSc binary viewer.

proteus.LinearAlgebraTools.Vec(n)[source]

Build a vector of length n (using numpy)

For example:

>>> Vec(3)
array([ 0.,  0.,  0.])
proteus.LinearAlgebraTools.Mat(m, n)[source]

Build an m x n matrix (using numpy)

For example:

>>> Mat(2,3)
array([[ 0.,  0.,  0.],
      [ 0.,  0.,  0.]])
proteus.LinearAlgebraTools.SparseMatFromDict(nr, nc, aDict)[source]

Build a nr x nc sparse matrix from a dictionary representation

proteus.LinearAlgebraTools.SparseMat(nr, nc, nnz, nzval, colind, rowptr)[source]

Build a nr x nc sparse matrix from the CSR data structures

Parameters:
  • nr (int) – The number of rows.
  • nc (int) – The number of columns.
  • nnz (int) – The number of non-zero matrix entries.
  • nzval (numpy array) – Array with non-zero matrix entries.
  • colind (numpy array of 32bit integers) – CSR column array.
  • rowptr (numpy array of 32bit integers) – CSR row pointer.
Returns:

sparse_matrix – superlu sparse matrix in CSR format.

Return type:

proteus.superluWrappers.SparseMatrix

Note

For the superluWrapper, both the colind and rowptr should use 32-bit integer data types.

class proteus.LinearAlgebraTools.SparseMatShell(ghosted_csr_mat)[source]

Build a parallel matrix shell from CSR data structures.

Parameters:ghosted_csr_mat
create(A)[source]
mult(A, x, y)[source]
proteus.LinearAlgebraTools.l2Norm(x)[source]

Compute the parallel \(l_2\) norm

proteus.LinearAlgebraTools.l1Norm(x)[source]

Compute the parallel \(l_1\) norm

The \(l_1\) norm of a vector \(\mathbf{x} \in \mathbb{R}^n\) is

\[\| \mathbf{x} \|_{1} = \sum_{i=0} |x_i|\]

If Python is running in parallel, then the sum is over all dimensions on all processors so that the input must not contain “ghost” entries.

This implemtation works for a distributed array with no ghost components (each component must be on a single processor).

Parameters:x – numpy array of length n
Returns:float
proteus.LinearAlgebraTools.lInfNorm(x)[source]

Compute the parallel \(l_{\infty}\) norm

The \(l_{\infty}\) norm of a vector \(\mathbf{x} \in \mathbb{R}^n\) is

\[\|x\|_{\infty} = \max_i |x_i|\]

This implemtation works for a distributed array with no ghost components (each component must be on a single processor).

Parameters:x – numpy array of length n
Returns:float
proteus.LinearAlgebraTools.wDot(x, y, h)[source]

Compute the parallel weighted dot product of vectors x and y using weight vector h.

The weighted dot product is defined for a weight vector \(\mathbf{h}\) as

\[(\mathbf{x},\mathbf{y})_h = \sum_{i} h_{i} x_{i} y_{i}\]

All weight vector components should be positive.

Parameters:x,y,h – numpy arrays for vectors and weight
Returns:the weighted dot product
proteus.LinearAlgebraTools.wl2Norm(x, h)[source]

Compute the parallel weighted l_2 norm with weight h

proteus.LinearAlgebraTools.wl1Norm(x, h)[source]

Compute the parallel weighted l_1 norm with weight h

proteus.LinearAlgebraTools.wlInfNorm(x, h)[source]

Compute the parallel weighted l_{infty} norm with weight h

proteus.LinearAlgebraTools.energyDot(x, y, A)[source]

Compute the “energy” dot product x^t A y (not parallel)

proteus.LinearAlgebraTools.energyNorm(x, A)[source]

Compute the “energy” norm x^t A x (not parallel)

proteus.LinearAlgebraTools.l2NormAvg(x)[source]

Compute the arithmetic averaged l_2 norm (root mean squared norm)

proteus.LinearAlgebraTools.rmsNorm(x)[source]

Compute the arithmetic averaged l_2 norm (root mean squared norm)

proteus.LinearAlgebraTools.l2Norm_local(x)[source]

Compute the l_2 norm for just local (processor) system (not parallel)

class proteus.LinearAlgebraTools.WeightedNorm(shape, atol, rtol)[source]

Compute the weighted norm for time step control (not currently parallel)

setWeight(y)[source]
norm(y, type)[source]