VMTK
Loading...
Searching...
No Matches
vtkvmtkSparseMatrix.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: VMTK
4 Module: $RCSfile: vtkvmtkSparseMatrix.cxx,v $
5 Language: C++
6 Date: $Date: 2006/04/06 16:46:44 $
7 Version: $Revision: 1.3 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
22#include "vtkvmtkSparseMatrix.h"
25#include "vtkvmtkDoubleVector.h"
26#include "vtkvmtkConstants.h"
27#include "vtkObjectFactory.h"
28
29
31
33{
34 this->NumberOfRows = 0;
35 this->Array = NULL;
36}
37
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}
50
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}
64
65void vtkvmtkSparseMatrix::SetNumberOfRows(vtkIdType numberOfRows)
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}
85
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}
107
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}
151
152void vtkvmtkSparseMatrix::AllocateRowsFromDataSet(vtkDataSet* dataSet, int numberOfVariables)
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}
214
215double vtkvmtkSparseMatrix::GetElement(vtkIdType i, vtkIdType j)
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}
227
228void vtkvmtkSparseMatrix::SetElement(vtkIdType i, vtkIdType j, double value)
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}
240
241void vtkvmtkSparseMatrix::AddElement(vtkIdType i, vtkIdType j, double value)
242{
243 double currentValue = this->GetElement(i,j);
244 this->SetElement(i,j,currentValue+value);
245}
246
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}
281
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}
322
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}
332
virtual void SetDataSetPointId(vtkIdType)
void SetDataSet(vtkDataSet *dataSet)
Serves as the base class for vectors utilized in finite-element and other PDE evaluations.
void Fill(double value)
double GetElement(vtkIdType i)
void SetElement(vtkIdType i, double value)
Query the neighborhood surrounding the set point id in an data set item.
vtkIdType GetPointId(vtkIdType i)
virtual void Build() override=0
Create an empty, polydata, polydata manifold, polydata manifold extended, or unstructured grid neighb...
vtkvmtkNeighborhood * GetNeighborhood(vtkIdType ptId)
static vtkvmtkPolyDataNeighborhood * New()
Represents a single row of a vtkvmtkSparseMatrix: a sparse list of (column id, value) pairs plus a se...
virtual double GetDiagonalElement()
void SetNumberOfElements(vtkIdType numberOfElements)
void CopyStencil(vtkvmtkStencil *stencil)
vtkIdType GetElementIndex(vtkIdType id)
void SetElement(vtkIdType i, double element)
void SetElementId(vtkIdType i, vtkIdType id)
virtual void SetDiagonalElement(double)
static vtkvmtkSparseMatrixRow * New()
double GetElement(vtkIdType i)
vtkIdType GetElementId(vtkIdType i)
Implements a row-based sparse matrix built from mesh neighborhoods or stencils, supporting basic line...
void SetNumberOfRows(vtkIdType numberOfRows)
vtkvmtkSparseMatrixRow * GetRow(vtkIdType i)
void CopyRowsFromStencils(vtkvmtkStencils *stencils)
double GetElement(vtkIdType i, vtkIdType j)
void AllocateRowsFromNeighborhoods(vtkvmtkNeighborhoods *neighborhoods, int numberOfVariables=1)
void Multiply(vtkvmtkDoubleVector *x, vtkvmtkDoubleVector *y)
void AddElement(vtkIdType i, vtkIdType j, double value)
void TransposeMultiply(vtkvmtkDoubleVector *x, vtkvmtkDoubleVector *y)
vtkvmtkSparseMatrixRow ** Array
void DeepCopy(vtkvmtkSparseMatrix *src)
void SetElement(vtkIdType i, vtkIdType j, double value)
void AllocateRowsFromDataSet(vtkDataSet *dataSet, int numberOfVariables=1)
virtual vtkIdType GetNumberOfRows()
Create an empty, umbrella, area weighted umbrella, finite-element LP, or finite-volume finite-element...
vtkvmtkStencil * GetStencil(vtkIdType ptId)
vtkIdType GetNumberOfStencils()
static vtkvmtkUnstructuredGridNeighborhood * New()
#define VTK_VMTK_LARGE_DOUBLE
#define VTK_VMTK_PIVOTING_TOL
vtkStandardNewMacro(vtkvmtkSparseMatrix)