PetscSolver.cc 6.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
#include "AMDiS.h"
23
24
25
26
#include "DOFMatrix.h"
#include "Global.h"
#include "Initfile.h"
#include "parallel/MeshDistributor.h"
27
#include "parallel/MpiHelper.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
28
#include "parallel/ParallelDofMapping.h"
29
30
#include "parallel/PetscSolver.h"
#include "parallel/StdMpi.h"
31

32
namespace AMDiS { namespace Parallel {
Thomas Witkowski's avatar
Thomas Witkowski committed
33

34
35
  PetscSolver::PetscSolver(std::string name)
    : super(name),
36
      kspPrefix(""),
37
38
      removeRhsNullspace(false),    
      hasConstantNullspace(false),
39
      isSymmetric(false),
40
      handleDirichletRows(true)
41
  {
42
    std::string tmp("");
43
    Parameters::get(name + "->petsc prefix", tmp);
44
45
46
    setKspPrefix(tmp); 
    
    std::string kspStr = "";
47
    Parameters::get("parallel->solver->petsc->ksp", kspStr);
48
49
50
    if (!kspStr.size())
      Parameters::get(name + "->ksp", kspStr);
      
51
52
    if (kspStr != "")
      PetscOptionsInsertString(kspStr.c_str());
53

54
55
56
    Parameters::get(name + "->remove rhs null space", removeRhsNullspace);
    Parameters::get(name + "->has constant null space", hasConstantNullspace);
    Parameters::get(name + "->nullspace->const in comp", 
57
		    constNullspaceComponent);
58
59
60
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
61
  PetscSolver::~PetscSolver()
62
  {}
Thomas Witkowski's avatar
Thomas Witkowski committed
63
64


65
66
  void PetscSolver::init(std::vector<const FiniteElemSpace*> &fe0,
			 std::vector<const FiniteElemSpace*> &fe1,
Thomas Witkowski's avatar
bo eh  
Thomas Witkowski committed
67
			 bool createGlobalMapping)
68
69
70
71
  {
    FUNCNAME("PetscSolver::init()");

    TEST_EXIT(meshDistributor)("No mesh distributor object defined!\n");
72
73
74
    meshDistributor->setBoundaryDofRequirement(getBoundaryDofRequirement());

    super::init(fe0, fe1, createGlobalMapping);
75
76
  }

77
78
79
80
81
 int PetscSolver::solveLinearSystem(const SolverMatrix<Matrix<DOFMatrix*> >& A,
				    SystemVector& x,
				    SystemVector& b,
				    bool createMatrixData,
				    bool storeMatrixData)
82
 {
83
84
85
    FUNCNAME("PetscSolver::solveLinearSystem()");
    
    TEST_EXIT(meshDistributor)("No meshDistributor provided. Should not happen!\n");
86

87
    MPI::COMM_WORLD.Barrier();
88
    Timer t;
89
    
90
    if (createMatrixData)
91
92
      fillPetscMatrix(const_cast< Matrix< DOFMatrix* >* >(A.getOriginalMat()));    
    
93
    fillPetscRhs(&b);
94

95
    INFO(info, 8)("creation of parallel data structures needed %.5f seconds\n", 
96
		  t.elapsed());
97

98
    solvePetscMatrix(x, nullptr);
99

100
101
102
103
104
105
    if (!storeMatrixData) {
      destroyVectorData();
      destroyMatrixData();
    }
   
    return 0;
106
 }
107

108
109
110
111
112
113
114
115
  void PetscSolver::fillPetscMatrix(DOFMatrix* mat)
  {
    Matrix<DOFMatrix*> sysMat(1, 1);
    sysMat[0][0] = mat;
    fillPetscMatrix(&sysMat);
  }


116
117
118
  void PetscSolver::solve(Vec &rhs, Vec &sol)
  {
    PetscErrorCode solverError = KSPSolve(kspInterior, rhs, sol);
119
120
121
    
    PetscInt nIter = 0;
    KSPGetIterationNumber(kspInterior, &nIter);
122
    PetscReal residual_norm = -1.0;
123
124
    KSPGetResidualNorm(kspInterior, &residual_norm);
    
125
    if (residual_norm <= 0.0) {
126
127
128
129
      Vec r;
      createVec(*interiorMap, r);
      KSPBuildResidual(kspInterior, PETSC_NULL, PETSC_NULL, &r);
      VecNorm(r, NORM_2, &residual_norm);
130
    }
131
132
133
134
    
    setErrorCode(solverError);
    setIterations(nIter);
    setResidual(residual_norm);
135
136
  }

137

138
139
140
141
  void PetscSolver::solveGlobal(Vec &rhs, Vec &sol)
  {
    FUNCNAME("PetscSolver::solveGlobal()");

142
143
144
145
146
147
    int s, ls;
    VecGetSize(rhs, &s);
    VecGetLocalSize(rhs, &ls);

    MSG("Solve global %d %d\n", ls, s);

148
149
150
151
    ERROR_EXIT("Not implemented!\n");
  }


152
  void PetscSolver::copyVec(Vec& originVec, Vec& destVec, 
153
			    std::vector<int>& originIndex, std::vector<int>& destIndex)
154
155
  {
    IS originIs, destIs;
156
    ISCreateGeneral(domainComm, 
157
158
159
160
161
		    originIndex.size(), 
		    &(originIndex[0]),
		    PETSC_USE_POINTER,
		    &originIs);

162
    ISCreateGeneral(domainComm, 
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
		    destIndex.size(), 
		    &(destIndex[0]),
		    PETSC_USE_POINTER,
		    &destIs);

    VecScatter scatter;
    VecScatterCreate(originVec, originIs, destVec, destIs, &scatter);
    VecScatterBegin(scatter, originVec, destVec,
		    INSERT_VALUES, SCATTER_FORWARD);
    VecScatterEnd(scatter, originVec, destVec,
		  INSERT_VALUES, SCATTER_FORWARD);

    ISDestroy(&originIs);
    ISDestroy(&destIs);    
    VecScatterDestroy(&scatter);
  }

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
180
181
182
183
184
185
186
187

  bool PetscSolver::testMatrixSymmetric(Mat mat, bool advancedTest)
  {
    FUNCNAME("PetscSolver::testMatrixSymmetric()");

    Mat matTrans;
    MatTranspose(mat, MAT_INITIAL_MATRIX, &matTrans);

Thomas Witkowski's avatar
Thomas Witkowski committed
188
189
    bool isSym = true;

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    if (advancedTest) {
      int rowStart, rowEnd;
      MatGetOwnershipRange(mat, &rowStart, &rowEnd);

      PetscInt nCols, nColsTrans;
      const PetscInt *cols, *colsTrans;
      const PetscScalar *vals, *valsTrans;

      for (int i = rowStart; i < rowEnd; i++) {
	MatGetRow(mat, i, &nCols, &cols, &vals);
	MatGetRow(matTrans, i, &nColsTrans, &colsTrans, &valsTrans);

	if (nCols != nColsTrans) {
	  MSG("Symmetric test fails: mat row %d: %d nnz   mat col %d: %d nnz\n",
	      i, nCols, i, nColsTrans);
	  isSym = false;
	} else {
	  for (int j = 0; j < nCols; j++) {
	    if (cols[j] != colsTrans[j]) {
	      if (cols[j] > colsTrans[j]) {
		MSG("mat[%d][%d] does not exists: mat[%d][%d] = %e\n", 
		    i, colsTrans[j],
		    colsTrans[j], i, valsTrans[j]);
	      } else {
		MSG("mat[%d][%d] does not exists: mat[%d][%d] = %e\n", 
		    cols[j], i,
		    i, cols[j], vals[j]);
	      }
	      isSym = false;
	    } else {
	      double n = fabs(vals[j] - valsTrans[j]);
	      if (n > 1e-10) {
		MSG("value diff:  mat[%d][%d] = %e   mat[%d][%d] = %e\n",
		    i, cols[j], vals[j], colsTrans[j], i, valsTrans[j]);

		isSym = false;
	      }
	    }
	  }
	}

	MatRestoreRow(mat, i, &nCols, &cols, &vals);
	MatRestoreRow(matTrans, i, &nColsTrans, &colsTrans, &valsTrans);
      }
    } 
      
    MatAXPY(matTrans, -1.0, mat, DIFFERENT_NONZERO_PATTERN);
    double norm1, norm2;
    MatNorm(matTrans, NORM_FROBENIUS, &norm1);
    MatNorm(matTrans, NORM_INFINITY, &norm2);
    MatDestroy(&matTrans);
    
    MSG("Matrix norm test: %e %e\n", norm1, norm2);
    
Thomas Witkowski's avatar
Thomas Witkowski committed
244
    return (isSym && norm1 < 1e-10 && norm2 < 1e-10);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
245
  }
246
} } // end namespaces