VMTK
Loading...
Searching...
No Matches
vtkvmtkSparseMatrix Class Reference

Implements a row-based sparse matrix built from mesh neighborhoods or stencils, supporting basic linear algebra operations. More...

#include <vtkvmtkSparseMatrix.h>

Inheritance diagram for vtkvmtkSparseMatrix:
[legend]
Collaboration diagram for vtkvmtkSparseMatrix:
[legend]

Public Types

typedef vtkObject Superclass

Public Member Functions

virtual int IsA (const char *type)
vtkvmtkSparseMatrix * NewInstance () const
void Multiply (vtkvmtkDoubleVector *x, vtkvmtkDoubleVector *y)
void TransposeMultiply (vtkvmtkDoubleVector *x, vtkvmtkDoubleVector *y)
vtkvmtkSparseMatrixRow * GetRow (vtkIdType i)
virtual vtkIdType GetNumberOfRows ()
void CopyRowsFromStencils (vtkvmtkStencils *stencils)
void AllocateRowsFromNeighborhoods (vtkvmtkNeighborhoods *neighborhoods, int numberOfVariables=1)
void AllocateRowsFromDataSet (vtkDataSet *dataSet, int numberOfVariables=1)
void Initialize ()
void SetNumberOfRows (vtkIdType numberOfRows)
double GetElement (vtkIdType i, vtkIdType j)
void SetElement (vtkIdType i, vtkIdType j, double value)
void AddElement (vtkIdType i, vtkIdType j, double value)
void DeepCopy (vtkvmtkSparseMatrix *src)

Static Public Member Functions

static vtkvmtkSparseMatrix * New ()
static int IsTypeOf (const char *type)
static vtkvmtkSparseMatrix * SafeDownCast (vtkObjectBase *o)

Protected Member Functions

virtual vtkObjectBase * NewInstanceInternal () const
 vtkvmtkSparseMatrix ()
 ~vtkvmtkSparseMatrix ()

Protected Attributes

vtkvmtkSparseMatrixRow ** Array
vtkIdType NumberOfRows

Detailed Description

Implements a row-based sparse matrix built from mesh neighborhoods or stencils, supporting basic linear algebra operations.

vtkvmtkSparseMatrix stores one vtkvmtkSparseMatrixRow per matrix row, each row holding the off-diagonal (column id, value) pairs plus a separate diagonal element. Rows can be allocated from the point connectivity of a vtkDataSet (AllocateRowsFromDataSet) or from a vtkvmtkNeighborhoods object (AllocateRowsFromNeighborhoods) – both optionally replicated for several independent variables per point (numberOfVariables) to build a block-structured system – or its row structure and weights can be copied directly from a set of finite-element/ Laplacian vtkvmtkStencils (CopyRowsFromStencils). It supports matrix-vector multiplication (Multiply, TransposeMultiply) against a vtkvmtkDoubleVector and is the matrix type used by vtkvmtkLinearSystem / vtkvmtkOpenNLLinearSystemSolver and the finite-element assemblers (vtkvmtkFEAssembler subclasses) throughout vmtk's harmonic mapping, gradient, and vorticity filters.

See also
vtkvmtkSparseMatrixRow, vtkvmtkDoubleVector, vtkvmtkLinearSystem, vtkvmtkFEAssembler

Definition at line 49 of file vtkvmtkSparseMatrix.h.

Member Typedef Documentation

◆ Superclass

Definition at line 54 of file vtkvmtkSparseMatrix.h.

Constructor & Destructor Documentation

◆ vtkvmtkSparseMatrix()

vtkvmtkSparseMatrix::vtkvmtkSparseMatrix ( )
protected

Definition at line 32 of file vtkvmtkSparseMatrix.cxx.

33{
34 this->NumberOfRows = 0;
35 this->Array = NULL;
36}
vtkvmtkSparseMatrixRow ** Array

◆ ~vtkvmtkSparseMatrix()

vtkvmtkSparseMatrix::~vtkvmtkSparseMatrix ( )
protected

Definition at line 38 of file vtkvmtkSparseMatrix.cxx.

39{
40 if (this->Array)
41 {
42 for (int i=0; i<this->NumberOfRows; i++)
43 {
44 this->Array[i]->Delete();
45 }
46 delete[] this->Array;
47 this->Array = NULL;
48 }
49}

Member Function Documentation

◆ New()

vtkvmtkSparseMatrix * vtkvmtkSparseMatrix::New ( )
static

◆ IsTypeOf()

int vtkvmtkSparseMatrix::IsTypeOf ( const char * type)
static

◆ IsA()

virtual int vtkvmtkSparseMatrix::IsA ( const char * type)
virtual

◆ SafeDownCast()

vtkvmtkSparseMatrix * vtkvmtkSparseMatrix::SafeDownCast ( vtkObjectBase * o)
static

◆ NewInstanceInternal()

virtual vtkObjectBase * vtkvmtkSparseMatrix::NewInstanceInternal ( ) const
protectedvirtual

◆ NewInstance()

vtkvmtkSparseMatrix * vtkvmtkSparseMatrix::NewInstance ( ) const

◆ Multiply()

void vtkvmtkSparseMatrix::Multiply ( vtkvmtkDoubleVector * x,
vtkvmtkDoubleVector * y )

Compute y = A*x, the matrix-vector product of this sparse matrix with x, storing the result in y. Values are clamped to +/-VTK_VMTK_LARGE_DOUBLE and snapped to zero below VTK_VMTK_PIVOTING_TOL to guard against overflow/underflow.

Definition at line 247 of file vtkvmtkSparseMatrix.cxx.

248{
249 vtkIdType i, j, numberOfRows, numberOfRowElements;
250 double yValue, xValue;
251
252 numberOfRows = this->GetNumberOfRows();
253 for (i=0; i<numberOfRows; i++)
254 {
255 yValue = 0.0;
256 numberOfRowElements = this->GetRow(i)->GetNumberOfElements();
257 for (j=0; j<numberOfRowElements; j++)
258 {
259 xValue = x->GetElement(this->GetRow(i)->GetElementId(j));
260 yValue += this->GetRow(i)->GetElement(j) * xValue;
261 }
262 xValue = x->GetElement(i);
263 yValue += this->GetRow(i)->GetDiagonalElement() * xValue;
264
265 if (fabs(yValue)<VTK_VMTK_PIVOTING_TOL)
266 {
267 yValue = 0.0;
268 }
269 else if (yValue>VTK_VMTK_LARGE_DOUBLE)
270 {
271 yValue = VTK_VMTK_LARGE_DOUBLE;
272 }
273 else if (yValue<-VTK_VMTK_LARGE_DOUBLE)
274 {
275 yValue = -VTK_VMTK_LARGE_DOUBLE;
276 }
277
278 y->SetElement(i,yValue);
279 }
280}
double GetElement(vtkIdType i)
void SetElement(vtkIdType i, double value)
virtual double GetDiagonalElement()
double GetElement(vtkIdType i)
vtkvmtkSparseMatrixRow * GetRow(vtkIdType i)
virtual vtkIdType GetNumberOfRows()
#define VTK_VMTK_LARGE_DOUBLE
#define VTK_VMTK_PIVOTING_TOL

◆ TransposeMultiply()

void vtkvmtkSparseMatrix::TransposeMultiply ( vtkvmtkDoubleVector * x,
vtkvmtkDoubleVector * y )

Compute y = A^T*x, the matrix-vector product of this sparse matrix's transpose with x, storing the result in y. Values are clamped/snapped the same way as Multiply().

Definition at line 282 of file vtkvmtkSparseMatrix.cxx.

283{
284 vtkIdType i, j, id, numberOfRows, numberOfRowElements;
285 double xValue, yValue;
286
287 yValue = 0.0;
288
289 y->Fill(0.0);
290
291 numberOfRows = this->GetNumberOfRows();
292 for (i=0; i<numberOfRows; i++)
293 {
294 numberOfRowElements = this->GetRow(i)->GetNumberOfElements();
295 xValue = x->GetElement(i);
296 for (j=0; j<numberOfRowElements; j++)
297 {
298 id = this->GetRow(i)->GetElementId(j);
299 yValue = y->GetElement(id);
300 yValue += this->GetRow(i)->GetElement(j) * xValue;
301 y->SetElement(id,yValue);
302 }
303 xValue = x->GetElement(i);
304 yValue += this->GetRow(i)->GetDiagonalElement() * xValue;
305
306 if (fabs(yValue)<VTK_VMTK_PIVOTING_TOL)
307 {
308 yValue = 0.0;
309 }
310 else if (yValue>VTK_VMTK_LARGE_DOUBLE)
311 {
312 yValue = VTK_VMTK_LARGE_DOUBLE;
313 }
314 else if (yValue<-VTK_VMTK_LARGE_DOUBLE)
315 {
316 yValue = -VTK_VMTK_LARGE_DOUBLE;
317 }
318
319 y->SetElement(i,yValue);
320 }
321}
void Fill(double value)
vtkIdType GetElementId(vtkIdType i)

◆ GetRow()

vtkvmtkSparseMatrixRow * vtkvmtkSparseMatrix::GetRow ( vtkIdType i)
inline

Get a row given a row id.

Definition at line 72 of file vtkvmtkSparseMatrix.h.

72{ return this->Array[i]; }

◆ GetNumberOfRows()

virtual vtkIdType vtkvmtkSparseMatrix::GetNumberOfRows ( )
virtual

Get the number of rows currently allocated in the matrix. Use SetNumberOfRows() to change it.

◆ CopyRowsFromStencils()

void vtkvmtkSparseMatrix::CopyRowsFromStencils ( vtkvmtkStencils * stencils)

Initialize the matrix with one row per stencil in stencils, copying each stencil's neighbor ids, weights, and center weight into the corresponding matrix row (see vtkvmtkSparseMatrixRow::CopyStencil). Replaces any previously allocated rows.

Definition at line 86 of file vtkvmtkSparseMatrix.cxx.

87{
88 vtkIdType i;
89 vtkIdType numberOfStencils;
90
91 if (stencils==NULL)
92 {
93 vtkErrorMacro(<<"No stencils provided.");
94 return;
95 }
96
97 numberOfStencils = stencils->GetNumberOfStencils();
98
99 this->Initialize();
100 this->SetNumberOfRows(numberOfStencils);
101
102 for (i=0; i<numberOfStencils; i++)
103 {
104 this->GetRow(i)->CopyStencil(stencils->GetStencil(i));
105 }
106}
void CopyStencil(vtkvmtkStencil *stencil)
void SetNumberOfRows(vtkIdType numberOfRows)
vtkvmtkStencil * GetStencil(vtkIdType ptId)
vtkIdType GetNumberOfStencils()

◆ AllocateRowsFromNeighborhoods()

void vtkvmtkSparseMatrix::AllocateRowsFromNeighborhoods ( vtkvmtkNeighborhoods * neighborhoods,
int numberOfVariables = 1 )

Initialize the matrix with numberOfVariables * neighborhoods->GetNumberOfNeighborhoods() rows, one per (point, variable) pair, allocating each row's column structure (values left at 0) from the corresponding neighborhood's point ids, replicated across all variable blocks so the matrix can represent a coupled multi-variable system. Replaces any previously allocated rows.

Definition at line 108 of file vtkvmtkSparseMatrix.cxx.

109{
110 if (neighborhoods==NULL)
111 {
112 vtkErrorMacro(<<"No neighborhoods provided.");
113 return;
114 }
115
116 int numberOfNeighborhoods = neighborhoods->GetNumberOfNeighborhoods();
117
118 int numberOfRows = numberOfVariables*numberOfNeighborhoods;
119
120 this->Initialize();
121 this->SetNumberOfRows(numberOfRows);
122
123 int i;
124 for (i=0; i<numberOfRows; i++)
125 {
126 vtkvmtkSparseMatrixRow* row = this->GetRow(i);
127 vtkIdType pointId = i % numberOfNeighborhoods;
128 vtkIdType variableId = i / numberOfNeighborhoods;
129 vtkvmtkNeighborhood* neighborhood = neighborhoods->GetNeighborhood(pointId);
130 int numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
131 int numberOfElements = numberOfNeighborhoodPoints + (numberOfVariables-1)*(numberOfNeighborhoodPoints+1);
132 row->SetNumberOfElements(numberOfElements);
133 int index = 0;
134 for (int n=0; n<numberOfVariables; n++)
135 {
136 for (int j=0; j<numberOfNeighborhoodPoints; j++)
137 {
138 row->SetElementId(index,neighborhood->GetPointId(j)+n*numberOfNeighborhoods);
139 row->SetElement(index,0.0);
140 index++;
141 }
142 if (n != variableId)
143 {
144 row->SetElementId(index,pointId+n*numberOfNeighborhoods);
145 row->SetElement(index,0.0);
146 index++;
147 }
148 }
149 }
150}
vtkIdType GetPointId(vtkIdType i)
vtkvmtkNeighborhood * GetNeighborhood(vtkIdType ptId)
void SetNumberOfElements(vtkIdType numberOfElements)
void SetElement(vtkIdType i, double element)
void SetElementId(vtkIdType i, vtkIdType id)

◆ AllocateRowsFromDataSet()

void vtkvmtkSparseMatrix::AllocateRowsFromDataSet ( vtkDataSet * dataSet,
int numberOfVariables = 1 )

Initialize the matrix the same way as AllocateRowsFromNeighborhoods, but building the point neighborhoods internally from the connectivity of dataSet (must be a vtkPolyData or vtkUnstructuredGrid). Replaces any previously allocated rows.

Definition at line 152 of file vtkvmtkSparseMatrix.cxx.

153{
154 if (dataSet==NULL)
155 {
156 vtkErrorMacro(<<"No dataSet provided.");
157 return;
158 }
159
160 vtkvmtkNeighborhood* neighborhood;
161 if (vtkPolyData::SafeDownCast(dataSet))
162 {
163 neighborhood = vtkvmtkPolyDataNeighborhood::New();
164 }
165 else if (vtkUnstructuredGrid::SafeDownCast(dataSet))
166 {
168 }
169 else
170 {
171 vtkErrorMacro("DataSet not vtkPolyData or vtkUnstructuredGrid");
172 return;
173 }
174
175 neighborhood->SetDataSet(dataSet);
176
177 int numberOfNeighborhoods = dataSet->GetNumberOfPoints();
178
179 int numberOfRows = numberOfVariables*numberOfNeighborhoods;
180
181 this->Initialize();
182 this->SetNumberOfRows(numberOfRows);
183
184 int i;
185 for (i=0; i<numberOfRows; i++)
186 {
187 vtkvmtkSparseMatrixRow* row = this->GetRow(i);
188 vtkIdType pointId = i % numberOfNeighborhoods;
189 vtkIdType variableId = i / numberOfNeighborhoods;
190 neighborhood->SetDataSetPointId(pointId);
191 neighborhood->Build();
192 int numberOfNeighborhoodPoints = neighborhood->GetNumberOfPoints();
193 int numberOfElements = numberOfNeighborhoodPoints + (numberOfVariables-1)*(numberOfNeighborhoodPoints+1);
194 row->SetNumberOfElements(numberOfElements);
195 int index = 0;
196 for (int n=0; n<numberOfVariables; n++)
197 {
198 for (int j=0; j<numberOfNeighborhoodPoints; j++)
199 {
200 row->SetElementId(index,neighborhood->GetPointId(j)+n*numberOfNeighborhoods);
201 row->SetElement(index,0.0);
202 index++;
203 }
204 if (n != variableId)
205 {
206 row->SetElementId(index,pointId+n*numberOfNeighborhoods);
207 row->SetElement(index,0.0);
208 index++;
209 }
210 }
211 }
212 neighborhood->Delete();
213}
virtual void SetDataSetPointId(vtkIdType)
void SetDataSet(vtkDataSet *dataSet)
virtual void Build() override=0
static vtkvmtkPolyDataNeighborhood * New()
static vtkvmtkUnstructuredGridNeighborhood * New()

◆ Initialize()

void vtkvmtkSparseMatrix::Initialize ( )

Release all allocated rows and reset the matrix to zero rows.

Definition at line 51 of file vtkvmtkSparseMatrix.cxx.

52{
53 if (this->Array)
54 {
55 for (int i=0; i<this->NumberOfRows; i++)
56 {
57 this->Array[i]->Delete();
58 }
59 delete[] this->Array;
60 this->Array = NULL;
61 }
62 this->NumberOfRows = 0;
63}

◆ SetNumberOfRows()

void vtkvmtkSparseMatrix::SetNumberOfRows ( vtkIdType numberOfRows)

Set the number of rows in the matrix, (re)allocating a fresh vtkvmtkSparseMatrixRow for each row (any previously allocated rows are released). Rows are created empty; use GetRow()->SetNumberOfElements() or one of the Allocate.../CopyRowsFromStencils() helpers to populate them.

Definition at line 65 of file vtkvmtkSparseMatrix.cxx.

66{
67 //deallocate previous rows, allocate new ones
68 if (this->Array)
69 {
70 for (int i=0; i<this->NumberOfRows; i++)
71 {
72 this->Array[i]->Delete();
73 }
74 delete[] this->Array;
75 this->Array = NULL;
76 }
77
78 this->NumberOfRows = numberOfRows;
79 this->Array = new vtkvmtkSparseMatrixRow*[numberOfRows];
80 for (int i=0; i<this->NumberOfRows; i++)
81 {
83 }
84}
static vtkvmtkSparseMatrixRow * New()

◆ GetElement()

double vtkvmtkSparseMatrix::GetElement ( vtkIdType i,
vtkIdType j )

Get the value at row i, column j (the diagonal element when i == j, otherwise the off-diagonal element identified by column id j, which must already exist in the row's sparsity pattern).

Definition at line 215 of file vtkvmtkSparseMatrix.cxx.

216{
217 vtkvmtkSparseMatrixRow* row = this->GetRow(i);
218 if (i != j)
219 {
220 return row->GetElement(row->GetElementIndex(j));
221 }
222 else
223 {
224 return row->GetDiagonalElement();
225 }
226}
vtkIdType GetElementIndex(vtkIdType id)

◆ SetElement()

void vtkvmtkSparseMatrix::SetElement ( vtkIdType i,
vtkIdType j,
double value )

Set the value at row i, column j (the diagonal element when i == j, otherwise the off-diagonal element identified by column id j, which must already exist in the row's sparsity pattern).

Definition at line 228 of file vtkvmtkSparseMatrix.cxx.

229{
230 vtkvmtkSparseMatrixRow* row = this->GetRow(i);
231 if (i != j)
232 {
233 return row->SetElement(row->GetElementIndex(j),value);
234 }
235 else
236 {
237 return row->SetDiagonalElement(value);
238 }
239}
virtual void SetDiagonalElement(double)

◆ AddElement()

void vtkvmtkSparseMatrix::AddElement ( vtkIdType i,
vtkIdType j,
double value )

Add value to the current value at row i, column j (equivalent to SetElement(i,j,GetElement(i,j)+value)).

Definition at line 241 of file vtkvmtkSparseMatrix.cxx.

242{
243 double currentValue = this->GetElement(i,j);
244 this->SetElement(i,j,currentValue+value);
245}
double GetElement(vtkIdType i, vtkIdType j)
void SetElement(vtkIdType i, vtkIdType j, double value)

◆ DeepCopy()

void vtkvmtkSparseMatrix::DeepCopy ( vtkvmtkSparseMatrix * src)

Standard DeepCopy method: copies the number of rows and the contents of every row from src.

Definition at line 323 of file vtkvmtkSparseMatrix.cxx.

324{
325 this->SetNumberOfRows(src->NumberOfRows);
326
327 for (int i=0; i<this->NumberOfRows; i++)
328 {
329 this->Array[i]->DeepCopy(src->GetRow(i));
330 }
331}

Member Data Documentation

◆ Array

vtkvmtkSparseMatrixRow** vtkvmtkSparseMatrix::Array
protected

Definition at line 144 of file vtkvmtkSparseMatrix.h.

◆ NumberOfRows

vtkIdType vtkvmtkSparseMatrix::NumberOfRows
protected

Definition at line 145 of file vtkvmtkSparseMatrix.h.


The documentation for this class was generated from the following files: