ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkFEMLinearSystemWrapper.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkFEMLinearSystemWrapper_h
20 #define itkFEMLinearSystemWrapper_h
21 
22 #include "itkMacro.h"
23 #include "itkFEMSolution.h"
24 #include "itkFEMException.h"
25 #include "ITKFEMExport.h"
26 
27 #include <vector>
28 #include <typeinfo>
29 #include <string>
30 
31 namespace itk
32 {
33 namespace fem
34 {
54 class ITKFEM_EXPORT LinearSystemWrapper : public Solution
55 {
56 public:
59  typedef Self * Pointer;
60  typedef const Self * ConstPointer;
61 
62  typedef std::vector<unsigned int> ColumnArray;
63 
69  m_Order(0), m_NumberOfMatrices(1), m_NumberOfVectors(1), m_NumberOfSolutions(1)
70  {
71  }
72  /* , m_PrimaryMatrixSetupFunction(0), m_PrimaryVectorSetupFunction(0),
73  m_PrimarySolutionSetupFunction(0) {} */
75 
80  virtual ~LinearSystemWrapper();
81 
86  virtual void Clean();
87 
93  void SetSystemOrder(unsigned int N)
94  {
95  m_Order = N;
96  }
97 
101  unsigned int GetSystemOrder() const
102  {
103  return m_Order;
104  }
105 
110  void SetNumberOfMatrices(unsigned int nMatrices)
111  {
112  m_NumberOfMatrices = nMatrices;
113  }
114 
115  /*
116  * Set the maximum number of entries permitted in a matrix
117  * \param matrixIndex index of matrix to set value for
118  * \param maxNonZeros maximum number of entries allowed in matrix
119  * \note in general this function does nothing, however it may
120  * redefined by the derived wrapper if necessary
121  */
122  // virtual void SetMaximumNonZeroValuesInMatrix(unsigned int maxNonZeroValues)
123  // = 0;
124 
128  unsigned int GetNumberOfMatrices() const
129  {
130  return m_NumberOfMatrices;
131  }
132 
137  void SetNumberOfVectors(unsigned int nVectors)
138  {
139  m_NumberOfVectors = nVectors;
140  }
141 
145  unsigned int GetNumberOfVectors() const
146  {
147  return m_NumberOfVectors;
148  }
149 
154  void SetNumberOfSolutions(unsigned int nSolutions)
155  {
156  m_NumberOfSolutions = nSolutions;
157  }
158 
162  unsigned int GetNumberOfSolutions() const
163  {
164  return m_NumberOfSolutions;
165  }
166 
174  virtual void InitializeMatrix(unsigned int matrixIndex = 0) = 0;
175 
180  virtual bool IsMatrixInitialized(unsigned int matrixIndex = 0) = 0;
181 
186  virtual void DestroyMatrix(unsigned int matrixIndex = 0) = 0;
187 
194  virtual void InitializeVector(unsigned int vectorIndex = 0) = 0;
195 
200  virtual bool IsVectorInitialized(unsigned int vectorIndex = 0) = 0;
201 
206  virtual void DestroyVector(unsigned int vectorIndex = 0) = 0;
207 
214  virtual void InitializeSolution(unsigned int solutionIndex = 0) = 0;
215 
220  virtual bool IsSolutionInitialized(unsigned int solutionIndex = 0) = 0;
221 
225  virtual void DestroySolution(unsigned int solutionIndex = 0) = 0;
226 
233  virtual Float GetMatrixValue(unsigned int i, unsigned int j, unsigned int matrixIndex = 0) const = 0;
234 
242  virtual void SetMatrixValue(unsigned int i, unsigned int j, Float value, unsigned int matrixIndex = 0) = 0;
243 
251  virtual void AddMatrixValue(unsigned int i, unsigned int j, Float value, unsigned int matrixIndex = 0) = 0;
252 
265  virtual void GetColumnsOfNonZeroMatrixElementsInRow(unsigned int row, ColumnArray & cols,
266  unsigned int matrixIndex = 0);
267 
273  virtual Float GetVectorValue(unsigned int i, unsigned int vectorIndex = 0) const = 0;
274 
281  virtual void SetVectorValue(unsigned int i, Float value, unsigned int vectorIndex = 0) = 0;
282 
289  virtual void AddVectorValue(unsigned int i, Float value, unsigned int vectorIndex = 0) = 0;
290 
298  virtual void SetSolutionValue(unsigned int i, Float value, unsigned int solutionIndex = 0) = 0;
299 
307  virtual void AddSolutionValue(unsigned int i, Float value, unsigned int solutionIndex = 0) = 0;
308 
316  virtual void Solve(void) = 0;
317 
323  virtual void SwapMatrices(unsigned int matrixIndex1, unsigned int matrixIndex2) = 0;
324 
332  virtual void CopyMatrix(unsigned int matrixIndex1, unsigned int matrixIndex2);
333 
339  virtual void SwapVectors(unsigned int vectorIndex1, unsigned int vectorIndex2) = 0;
340 
346  virtual void SwapSolutions(unsigned int solutionIndex1, unsigned int solutionIndex2) = 0;
347 
353  virtual void ScaleMatrix(Float scale, unsigned int matrixIndex = 0);
354 
360  void ScaleVector(Float scale, unsigned int vectorIndex = 0);
361 
367  void ScaleSolution(Float scale, unsigned int solutionIndex = 0);
368 
375  virtual void MultiplyMatrixMatrix(unsigned int resultMatrixIndex, unsigned int leftMatrixIndex,
376  unsigned int rightMatrixIndex) = 0;
377 
384  virtual void AddMatrixMatrix(unsigned int matrixIndex1, unsigned int matrixIndex2);
385 
392  virtual void AddVectorVector(unsigned int vectorIndex1, unsigned int vectorIndex2);
393 
400  virtual void MultiplyMatrixVector(unsigned int resultVectorIndex, unsigned int matrixIndex, unsigned int vectorIndex);
401 
408  virtual void MultiplyMatrixSolution(unsigned int resultVectorIndex, unsigned int matrixIndex, unsigned int solutionIndex);
409 
415  virtual void CopySolution2Vector(unsigned int solutionIndex, unsigned int vectorIndex) = 0;
416 
422  virtual void CopyVector2Solution(unsigned int vectorIndex, unsigned int solutionIndex) = 0;
423 
429  virtual void CopyVector(unsigned int vectorSource, unsigned int vectorDestination);
430 
437  virtual void OptimizeMatrixStorage(unsigned int matrixIndex, unsigned int tempMatrixIndex);
438 
444  virtual void ReverseCuthillMckeeOrdering(ColumnArray & newNumbering, unsigned int matrixIndex = 0);
445 
446 protected:
447 
449  unsigned int m_Order;
450 
454  unsigned int m_NumberOfMatrices;
455 
459  unsigned int m_NumberOfVectors;
460 
464  unsigned int m_NumberOfSolutions;
465 
466  /*
467  * Function used to prepare primary matrix for numerical solving
468  */
469  // void (*m_PrimaryMatrixSetupFunction)(LinearSystemWrapper *lsw);
470 
471  /*
472  * Function used to prepare primary vector for numerical solving
473  */
474  /* void (*m_PrimaryVectorSetupFunction)(LinearSystemWrapper *lsw);*/
475 
476  /*
477  * Function used to prepare primary matrix for numerical solving
478  */
479  /* void (*m_PrimarySolutionSetupFunction)(LinearSystemWrapper *lsw); */
480 
481 private:
482 
486  void CuthillMckeeOrdering(ColumnArray & newNumbering, int startingRow, unsigned int matrixIndex = 0);
487 
488  void FollowConnectionsCuthillMckeeOrdering(unsigned int rowNumber, ColumnArray & rowDegree,
489  ColumnArray & newNumbering, unsigned int nextRowNumber,
490  unsigned int matrixIndex = 0);
491 
494 
496  const LinearSystemWrapper & operator=(const LinearSystemWrapper &);
497 
498 };
499 
500 class ITK_ABI_EXPORT FEMExceptionLinearSystem : public FEMException
501 {
502 public:
508  FEMExceptionLinearSystem(const char *file, unsigned int lineNumber, std::string location, std::string moreDescription);
509 
511  virtual ~FEMExceptionLinearSystem()
512  ITK_NOEXCEPT ITK_OVERRIDE;
513 
515  itkTypeMacro(FEMExceptionLinearSystem, FEMException);
516 };
517 
518 class ITK_ABI_EXPORT FEMExceptionLinearSystemBounds : public FEMException
519 {
520 public:
526  FEMExceptionLinearSystemBounds(const char *file, unsigned int lineNumber, std::string location,
527  std::string moreDescription,
528  unsigned int index1);
529 
534  FEMExceptionLinearSystemBounds(const char *file, unsigned int lineNumber, std::string location,
535  std::string moreDescription, unsigned int index1,
536  unsigned int index2);
537 
540  ITK_NOEXCEPT ITK_OVERRIDE;
541 
543  itkTypeMacro(FEMExceptionLinearSystem, FEMException);
544 };
545 }
546 } // end namespace itk::fem
547 
548 #endif // #ifndef itkFEMLinearSystemWrapper_h
void SetNumberOfMatrices(unsigned int nMatrices)
void SetNumberOfSolutions(unsigned int nSolutions)
void SetNumberOfVectors(unsigned int nVectors)
std::vector< unsigned int > ColumnArray
Declaration of several exception classes that are used within the FEM code.
Defines all functions required by Solver class to allocate, assemble and solve a linear system of equ...
Provides functions to access the values of the solution vector.
Base class for all exception&#39;s that can occur within FEM classes.