File kernel/src/simulationTools/BlockCSRMatrix.hpp

Go to the source code of this file

Definition of a compressed row sparse block matrix of SiconosMatrix*


typedef boost::numeric::ublas::compressed_matrix<double*, boost::numeric::ublas::basic_row_major<unsigned int>, 0, boost::numeric::ublas::unbounded_array<std::size_t>> CompressedRowMat
class BlockCSRMatrix
#include <>

Definition of a compressed sparse row matrix of SiconosMatrix, used in OneStepNSProblem to store the M matrix.

This class defines a specific compressed row sparse storage for blocks matrices, each block being a SiconosMatrix*.

It handles:

  • a SparseMat (boost-ublas) of SiconosMatrix*

  • a vector<SiconosMatrix*> which handles the non-null blocks

  • three vector<int> (IndexInt) to save non-null blocks position in row, columns and the list of the sizes of diagonal blocks.

  • two int, the number of blocks in a row and the number of non null blocks.

Each block of the current object represents the connection between two coupled Interactions,

(for example for Lagrangian systems, a single \( H W^{-1} H^t \) block or for first order systems \( hCW^{-1}B \) …).

This objects is built using an index set of SP::Interaction, that represents the “active” constraints in the OSNS problem and a map<SP::Interaction u1, <SP::Interaction u2, SP::SiconosMatrix block> >, block being the link between u1 and u2. Only Interaction present in the index set are picked out in the map.

A convert method is also implemented to create a SparseBlockStructuredMatrix which is Numerics-readable.

As an example, consider the index set I={u1, u3, u5, u8} and the map where non null blocks are (ui,ui), (u1,u3), (u1,u8), (u3,u1), (u8,u1). Each block being a pointer to a 3x3 matrix. Then the resulting matrix has 4 X 4 blocks, with 8 non-null blocks and looks like:

\[\begin{split} M=\left\lbrace\begin{array}{cccc} b11 & b13 & 0 & b18 \\ b31 & b22 & 0 & 0 \\ 0 & 0 & b33&0 \\ b81 & 0 & 0 & b44 \end{array}\right. \end{split}\]

with nc = 4, nbNonNullBlocks = 8, RowPos = [0 0 0 1 1 2 3 3], RowCol = [0 1 3 0 1 2 0 3] and _diagsize0 = [3 6 9 12].

We use std::vector (which may seems redundent with the double* of the numerics SparseBlockStructuredMatrix) because memory can be reserved during construction or initialized and then vectors are resized when the object is filled in. This avoid some call to malloc/free at each iteration.

Public Functions


Default constructor -> empty matrix.

BlockCSRMatrix(unsigned int n)

Constructor with dimension (number of blocks)


n – number of blocks in a row/column (only square matrices allowed)

BlockCSRMatrix(InteractionsGraph &indexSet)

Constructor from index set.


indexSet – the index set of the active constraints



inline unsigned int numberOfBlocksInARow() const

get size (in block-components)


unsigned int NumberOfBlocksInARow

unsigned int getNbNonNullBlocks() const

get total number of non-null blocks


unsigned int

inline SP::SparseBlockStructuredMatrix getNumericsMatSparse()

get the numerics-readable structure



inline SP::CompressedRowMat getMSparse()

get the ublas sparse mat



inline IndexInt::value_type getSizeOfDiagonalBlock(int i) const

get the dimension of the square-diagonal block number num


i – block position


unsigned int

inline SP::IndexInt getPositionsIndex(bool i)

get the index of blocks position (i=0 -> rows, i=1 -> columns)


i – unsigned int, 0 for rows, 1 for columns



void fill(InteractionsGraph &indexSet)

fill the current class using an index set


indexSet – set of the active constraints

void fillW(InteractionsGraph &indexSet)

fill the matrix with the Mass matrix


only for NewtonEulerDS


indexSet – of the active constraints

void fillH(InteractionsGraph &indexSet)

fill the matrix with the H matrix


only for NewtonEuler3DR


indexSet – of the active constraints

void convert()

fill the numerics structure _sparseBlockStructuredMatrix using _blockCSR

void display() const

display the current matrix

Private Functions

BlockCSRMatrix(const BlockCSRMatrix&)

Private copy constructor => no copy nor pass by value.

BlockCSRMatrix &operator=(const BlockCSRMatrix&)

Private assignment -> forbidden.



Private Members

unsigned int _nr

Number of blocks rows (first dimension of the block matrix)

unsigned int _nc

Number of blocks columns (second dimension of the block matrix)

SP::CompressedRowMat _blockCSR

Sparse-Block Boost Matrix.

Each block is a SiconosMatrix

SP::SparseBlockStructuredMatrix _sparseBlockStructuredMatrix

Specific structure required when a (Numerics) solver block is used.

SP::IndexInt _diagsize0

Vector used to save the sum of rows of diagonal blocks of M: _diagsize0[i] = _diagsize0[i-1] + ni, ni being the size of the diagonal block at row(block) i.

SP::IndexInt _diagsize1

Vector used to save the sum of dim of diagonal blocks of M: _diagsize0[i] = _diagsize0[i-1] + ni, ni being the size of the diagonal block at row(block) i.

SP::IndexInt rowPos

List of non null blocks positions (in row)

SP::IndexInt colPos

List of non null blocks positions (in col)