CppNoddy  0.92
Loading...
Searching...
No Matches
DistributedLinearSystem.h
Go to the documentation of this file.
1/// \file DistributedLinearSystem.h
2/// Specification of a sparse-storage distributed linear system class.
3
4#if defined(PETSC_Z) || defined(PETSC_D)
5
6#ifndef DISTRIBUTEDLINEARSYSTEM_H
7#define DISTRIBUTEDLINEARSYSTEM_H
8
9#include <DistributedMatrix.h>
10#include <DistributedVector.h>
11#include <Exceptions.h>
12#include <vector>
13
14namespace CppNoddy {
15
16 /// A linear system class for vector right-hand sides.
17 /// The class is constructed for distributed problems of the form
18 /// \f[ A_{NxN} \,{\underline x}_i = B_{1xN} \f].
19 template <typename _Type>
20 class DistributedLinearSystem {
21
22 public:
23
24 /// Constructor for a distributed linear system object.
25 /// \param pA A pointer to the 'A matrix', an NxN double/complex distributed matrix
26 /// \param pB A pointer to the 'B vector' a size N double/complex distributed vector
27 DistributedLinearSystem(DistributedMatrix<_Type>* pA, DistributedVector<_Type>* pB ) {
28 m_pA = pA;
29 m_pB = pB;
30 KSPCreate(PETSC_COMM_WORLD,&m_ksp);
31 // default the relative tolerance "rtol" to 1.e-10 for iterative solvers
32 // KSPSetTolerances( m_ksp, 1.e-10, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT );
33 }
34
35 /// Destructor for a linear system object.
36 ~DistributedLinearSystem(){
37 //VecDestroy(&m_x);
38 KSPDestroy(&m_ksp);
39 }
40
41 /// Solve the sparse system in place with soln overwriting B
42 void solve() {
43 // construct using the PETSc matrix
44 KSPSetOperators(m_ksp,*(m_pA->get_pMat()),*(m_pA->get_pMat()));
45
46 /////////////////////////////
47 // default solver is MUMPS //
48 /////////////////////////////
49 /*
50 KSPSetType(m_ksp,KSPPREONLY);
51 // preconditioner
52 KSPGetPC(m_ksp,&m_pc);
53 // hardwire a DIRECT SOLVER via MUMPS
54 PCSetType(m_pc,PCLU);
55 PCFactorSetMatSolverType(m_pc,MATSOLVERMUMPS);
56 PCFactorSetUpMatSolverType(m_pc);
57 */
58 /////////////////////////////
59
60
61 // override the KSP member data using command line options
62 KSPSetFromOptions(m_ksp);
63 KSPSetUp(m_ksp);
64 // We'll hard wire the solution back into the RHS vector
65 KSPSolve(m_ksp,*(m_pB->get_pVec()),*(m_pB->get_pVec()));
66 KSPView(m_ksp, PETSC_VIEWER_STDOUT_WORLD);
67 }
68
69 void view() {
70 KSPView(m_ksp, PETSC_VIEWER_STDOUT_WORLD);
71 }
72
73 /// Get a sequential vector from the distributed RHS vector
74 /// \return A sequential PETSc Vec object -- obviously not deleted
75 /// when the DistributedLinearSystem object destructs.
76 Vec get_seq_solution() {
77 // context for the scatter
78 VecScatter ctx;
79 Vec v_seq;
80 // manual says no need for VecCreate
81 VecScatterCreateToAll(*(m_pB->get_pVec()), &ctx, &v_seq);
82 // scatter the data to v_seq
83 VecScatterBegin(ctx, *(m_pB->get_pVec()), v_seq, INSERT_VALUES, SCATTER_FORWARD);
84 VecScatterEnd(ctx, *(m_pB->get_pVec()), v_seq, INSERT_VALUES, SCATTER_FORWARD);
85 // clean up
86 VecScatterDestroy(&ctx);
87 return v_seq;
88 }
89
90 /// Get the solution as a CppNoddy::DenseVector<_Type> vector
91 /// \return The entire solution vector as a sequential DenseVector<_Type>
92 DenseVector<_Type> get_solution() {
93 // convert the distributed vec solution to a sequential one
94 Vec seq_soln = get_seq_solution();
95 // this "array" is getting a pointer not copying data
96 PetscScalar* array;
97 VecGetArray(seq_soln,&array);
98 // now copy to the CppNoddy densevctor using the constructor
99 PetscInt size(0);
100 VecGetSize(seq_soln,&size);
101 DenseVector<_Type> soln(size,array);
102 return soln;
103 }
104
105 /*
106 /// Factorise the Ax=B system
107 void factorise(){
108 // construct using the PETSc matrix
109 KSPSetOperators(m_ksp,*(m_pA->get_pMat()),*(m_pA->get_pMat()));
110 //
111 KSPSetType(m_ksp,KSPPREONLY);
112 // preconditioner
113 KSPGetPC(m_ksp,&m_pc);
114 // hardwire a DIRECT SOLVER via MUMPS
115 PCSetType(m_pc,PCLU);
116 PCFactorSetMatSolverType(m_pc,MATSOLVERMUMPS);
117 PCFactorSetUpMatSolverType(m_pc);
118 //call MatGetFactor() to create F
119 PCFactorGetMatrix(m_pc,&m_F);
120 // allow override using command line options?
121 KSPSetFromOptions(m_ksp);
122 KSPSetUp(m_ksp);
123 }
124
125 /// Resolve the same system using the same factorisation
126 void solve_using_factorisation();
127 */
128
129 private:
130 // pointer to a distributed LHS matrix
131 DistributedMatrix<_Type>* m_pA;
132 // pointer to the distributed RHS vector
133 DistributedVector<_Type>* m_pB;
134 // Solver
135 KSP m_ksp;
136 // Preconditioner
137 PC m_pc;
138 // factors
139 Mat m_F;
140 };
141
142} //end namepsace
143
144#endif // include guard
145
146#endif // PETSc_D or PETSc_Z
A matrix class that constructs a SPARSE/DISTRIBUTED matrix using PETSc.
A class that constructs a SPARSE/DISTRIBUTED vector using PETSc.
The collection of CppNoddy exceptions.
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt