PetscSolverFeti.cc 75.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include "AMDiS.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
14
#include "MatrixVector.h"
15
#include "parallel/PetscHelper.h"
16
#include "parallel/PetscSolverFeti.h"
17
18
#include "parallel/PetscSolverFetiDebug.h"
#include "parallel/PetscSolverFetiMonitor.h"
19
#include "parallel/PetscSolverFetiStructs.h"
20
21
#include "parallel/PetscSolverFetiOperators.h"
#include "parallel/PetscSolverFetiTimings.h"
22
23
#include "parallel/StdMpi.h"
#include "parallel/MpiHelper.h"
24
#include "parallel/PetscSolverGlobalMatrix.h"
25
#include "io/VtkWriter.h"
26
27
28
29
30

namespace AMDiS {

  using namespace std;

31
32
  PetscSolverFeti::PetscSolverFeti()
    : PetscSolver(),
33
      schurPrimalSolver(0),
34
      multiLevelTest(false),
Thomas Witkowski's avatar
Thomas Witkowski committed
35
      subdomain(NULL),
36
      massMatrixSolver(NULL),
37
38
      meshLevel(0),
      rStartInterior(0),
Thomas Witkowski's avatar
Thomas Witkowski committed
39
      nGlobalOverallInterior(0),
Thomas Witkowski's avatar
Thomas Witkowski committed
40
      printTimings(false),
Thomas Witkowski's avatar
Thomas Witkowski committed
41
      stokesMode(false),
42
      augmentedLagrange(false),
Thomas Witkowski's avatar
Thomas Witkowski committed
43
      pressureComponent(-1)
44
45
46
47
48
49
  {
    FUNCNAME("PetscSolverFeti::PetscSolverFeti()");

    string preconditionerName = "";
    Parameters::get("parallel->solver->precon", preconditionerName);
    if (preconditionerName == "" || preconditionerName == "none") {
50
      MSG("Create FETI-DP solver with no preconditioner!\n");
51
52
      fetiPreconditioner = FETI_NONE;
    } else if (preconditionerName == "dirichlet") {
53
      MSG("Create FETI-DP solver with Dirichlet preconditioner!\n");
54
55
      fetiPreconditioner = FETI_DIRICHLET;
    } else if (preconditionerName == "lumped") {
56
      MSG("Create FETI-DP solver with lumped preconditioner!\n");
57
58
      fetiPreconditioner = FETI_LUMPED;
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
59
60
      ERROR_EXIT("Preconditioner \"%s\" not available!\n", 
		 preconditionerName.c_str());
61
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
62
63
64
65
66

    Parameters::get("parallel->feti->schur primal solver", schurPrimalSolver);
    TEST_EXIT(schurPrimalSolver == 0 || schurPrimalSolver == 1)
      ("Wrong solver \"%d\"for the Schur primal complement!\n", 
       schurPrimalSolver);
67
68

    Parameters::get("parallel->multi level test", multiLevelTest);
69
70
    if (multiLevelTest)
      meshLevel = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
71
72

    Parameters::get("parallel->print timings", printTimings);
73
74

    Parameters::get("parallel->feti->augmented lagrange", augmentedLagrange);
75
76

    Parameters::get("parallel->feti->symmetric", isSymmetric);
77
78
79
  }


80
  void PetscSolverFeti::initialize(vector<const FiniteElemSpace*> feSpaces)
81
  {
82
83
    FUNCNAME("PetscSolverFeti::initialize()");

84
85
86
    TEST_EXIT_DBG(meshLevel + 1 == meshDistributor->getMeshLevelData().getLevelNumber())
      ("Mesh hierarchy does not contain %d levels!\n", meshLevel + 1);

87
    MeshLevelData& levelData = meshDistributor->getMeshLevelData();
88
    vector<const FiniteElemSpace*>& uniqueFe = meshDistributor->getFeSpaces();
89

Thomas Witkowski's avatar
Thomas Witkowski committed
90

Thomas Witkowski's avatar
Thomas Witkowski committed
91
92
93
94
95
96
97
98
    stokesMode = false;
    Parameters::get("parallel->feti->stokes mode", stokesMode);
    if (stokesMode) {
      Parameters::get("parallel->feti->pressure component", pressureComponent);
      TEST_EXIT(pressureComponent >= 0)
	("FETI-DP in Stokes mode, no pressure component defined!\n");

      pressureFeSpace = feSpaces[pressureComponent];
Thomas Witkowski's avatar
Thomas Witkowski committed
99
100
    }
			   
Thomas Witkowski's avatar
Thomas Witkowski committed
101
102
    if (subdomain == NULL) {
      subdomain = new PetscSolverGlobalMatrix();
103
      subdomain->setSymmetric(isSymmetric);
104

105
      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
106
	subdomain->setMeshDistributor(meshDistributor, 
107
				      mpiCommGlobal, mpiCommLocal);
108
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
109
	subdomain->setMeshDistributor(meshDistributor, 
110
111
				      levelData.getMpiComm(meshLevel - 1),
				      levelData.getMpiComm(meshLevel));
Thomas Witkowski's avatar
Thomas Witkowski committed
112
	subdomain->setLevel(meshLevel);
113
114
      }
    }
115

116
117
118
119
    primalDofMap.init(levelData, feSpaces, uniqueFe);   
    dualDofMap.init(levelData, feSpaces, uniqueFe, false);
    localDofMap.init(levelData, feSpaces, uniqueFe, meshLevel != 0);
    lagrangeMap.init(levelData, feSpaces, uniqueFe);
Thomas Witkowski's avatar
Thomas Witkowski committed
120

Thomas Witkowski's avatar
Thomas Witkowski committed
121
    if (stokesMode)
Thomas Witkowski's avatar
Thomas Witkowski committed
122
      interfaceDofMap.init(levelData, feSpaces, uniqueFe);
Thomas Witkowski's avatar
Thomas Witkowski committed
123

124
    if (fetiPreconditioner == FETI_DIRICHLET) {
125
126
127
      TEST_EXIT(meshLevel == 0)
	("Dirichlet preconditioner not yet implemented for multilevel FETI-DP\n");

128
      interiorDofMap.init(levelData, feSpaces, uniqueFe, false);
129
    }
130
131
132
  }


Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
133
134
135
136
  void PetscSolverFeti::createDirichletData(Matrix<DOFMatrix*> &mat)
  {
    FUNCNAME("PetscSolverFeti::createDirichletData()");

Thomas Witkowski's avatar
Thomas Witkowski committed
137
138
139
140
141
    bool removeDirichletRows = false;
    Parameters::get("parallel->feti->remove dirichlet", removeDirichletRows);
    if (!removeDirichletRows)
      return;

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
142
143
144
145
146
147
148
149
150
    int nComponents = mat.getSize();
    for (int i = 0; i < nComponents; i++) {
      DOFMatrix* dofMat = mat[i][i];
      if (!dofMat)
	continue;
      
      const FiniteElemSpace *feSpace = dofMat->getRowFeSpace();
      std::set<DegreeOfFreedom>& dRows = dofMat->getDirichletRows();
      if (dirichletRows.count(feSpace)) {
151
152
153
154
155
156
157
158
159
	// === Run test if Dirichlet rows are all the same in all components ===
	// === of one FE space.                                              ===
	TEST_EXIT(dirichletRows[feSpace].size() == dRows.size())
	  ("Wrong number of dirichlet rows!\n");
	
	for (std::set<DegreeOfFreedom>::iterator it0 = dirichletRows[feSpace].begin(),
	       it1 = dRows.begin(); it1 != dRows.end(); ++it0, ++it1) {
	  TEST_EXIT(*it0 == *it1)("Wrong DOFs %d %d!\n", *it0, *it1);
	}
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
160
161
162
163
164
165
166
      } else {
	dirichletRows[feSpace] = dRows;
      }
    }
  }


167
168
169
170
  void PetscSolverFeti::createFetiData()
  {
    FUNCNAME("PetscSolverFeti::createFetiData()");

Thomas Witkowski's avatar
Thomas Witkowski committed
171
172
    double timeCounter = MPI::Wtime();

173
174
175
    TEST_EXIT(meshDistributor)("No mesh distributor object defined!\n");
    TEST_EXIT(meshDistributor->getFeSpaces().size() > 0)
      ("No FE space defined in mesh distributor!\n");
176

177
178
    MeshLevelData& levelData = meshDistributor->getMeshLevelData();

179
180
181
182
    primalDofMap.clear();
    dualDofMap.clear();
    lagrangeMap.clear();
    localDofMap.clear();
183
    if (fetiPreconditioner == FETI_DIRICHLET)
184
185
      interiorDofMap.clear();

186
187
    primalDofMap.setDofComm(meshDistributor->getDofComm());
    lagrangeMap.setDofComm(meshDistributor->getDofComm());
188

189
190
    primalDofMap.setMpiComm(levelData.getMpiComm(0), 0);
    dualDofMap.setMpiComm(levelData.getMpiComm(0), 0);
191
    lagrangeMap.setMpiComm(levelData.getMpiComm(0), 0);
192
    localDofMap.setMpiComm(levelData.getMpiComm(meshLevel), meshLevel);
193
    if (fetiPreconditioner == FETI_DIRICHLET)
194
      interiorDofMap.setMpiComm(levelData.getMpiComm(meshLevel), meshLevel);
195

196
197
198
    if (meshLevel == 0)
      localDofMap.setDofComm(meshDistributor->getDofComm());
    else
199
200
      localDofMap.setDofComm(meshDistributor->getDofCommSd());

Thomas Witkowski's avatar
Thomas Witkowski committed
201
    if (stokesMode) {
202
203
204
205
206
      interfaceDofMap.clear();
      interfaceDofMap.setDofComm(meshDistributor->getDofComm());
      interfaceDofMap.setMpiComm(levelData.getMpiComm(0), 0);
    }

207
208
    for (unsigned int i = 0; i < meshDistributor->getFeSpaces().size(); i++) {
      const FiniteElemSpace *feSpace = meshDistributor->getFeSpace(i);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
209
    
210
211
      createPrimals(feSpace);  

212
213
214
      createDuals(feSpace);

      createInterfaceNodes(feSpace);
215

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
216
      createIndexB(feSpace);     
217
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
218
219
220
221

    primalDofMap.update();
    dualDofMap.update();
    localDofMap.update();
222
    if (fetiPreconditioner == FETI_DIRICHLET)
223
      interiorDofMap.update();
Thomas Witkowski's avatar
Thomas Witkowski committed
224

Thomas Witkowski's avatar
Thomas Witkowski committed
225
    if (stokesMode)
226
227
      interfaceDofMap.update();

228
229
230
    for (unsigned int i = 0; i < meshDistributor->getFeSpaces().size(); i++) {
      const FiniteElemSpace *feSpace = meshDistributor->getFeSpace(i);
      createLagrange(feSpace);
231
      createAugmentedLagrange(feSpace);
232
    }
233

234
235
236
    lagrangeMap.update();


237
238
239
240
241
242
243
244
245
246
247
248
    // === ===

    if (meshLevel == 0) {
      rStartInterior = 0;
      nGlobalOverallInterior = localDofMap.getOverallDofs();
    } else {
      MeshLevelData& levelData = meshDistributor->getMeshLevelData();

      int groupRowsInterior = 0;
      if (levelData.getMpiComm(1).Get_rank() == 0)
	groupRowsInterior = localDofMap.getOverallDofs();

249
      mpi::getDofNumbering(mpiCommGlobal, groupRowsInterior,
250
251
252
253
254
255
256
257
258
			   rStartInterior, nGlobalOverallInterior);

      int tmp = 0;
      if (levelData.getMpiComm(1).Get_rank() == 0)
	tmp = rStartInterior;

      levelData.getMpiComm(1).Allreduce(&tmp, &rStartInterior, 1, MPI_INT, MPI_SUM);
    }

259
    MSG("FETI-DP data created on mesh level %d\n", meshLevel);
Thomas Witkowski's avatar
Thomas Witkowski committed
260
261
    for (unsigned int i = 0; i < meshDistributor->getFeSpaces().size(); i++) {
      const FiniteElemSpace *feSpace = meshDistributor->getFeSpace(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
262
      MSG("FETI-DP data for %d-ith FE space: %p\n", i, feSpace);
Thomas Witkowski's avatar
Thomas Witkowski committed
263

Thomas Witkowski's avatar
Thomas Witkowski committed
264
      if (feSpace == pressureFeSpace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
265
266
267
268
	MSG("  nRankInterface = %d  nOverallInterface = %d\n",
	    interfaceDofMap[feSpace].nRankDofs, 
	    interfaceDofMap[feSpace].nOverallDofs);
      } else {
269
	MSG("  nRankPrimals = %d   nLocalPrimals = %d  nOverallPrimals = %d\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
270
	    primalDofMap[feSpace].nRankDofs, 
271
	    primalDofMap[feSpace].nLocalDofs,
Thomas Witkowski's avatar
Thomas Witkowski committed
272
273
274
275
276
277
278
279
280
281
	    primalDofMap[feSpace].nOverallDofs);
	
	MSG("  nRankDuals = %d  nOverallDuals = %d\n",
	    dualDofMap[feSpace].nRankDofs, 
	    dualDofMap[feSpace].nOverallDofs);
	
	MSG("  nRankLagrange = %d  nOverallLagrange = %d\n",
	    lagrangeMap[feSpace].nRankDofs, 
	    lagrangeMap[feSpace].nOverallDofs);

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
282
283
284
// 	TEST_EXIT_DBG(localDofMap[feSpace].size() + primalDofMap[feSpace].size() == 
// 		      static_cast<unsigned int>(feSpace->getAdmin()->getUsedDofs()))
// 	  ("Should not happen!\n");	
Thomas Witkowski's avatar
Thomas Witkowski committed
285
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
286
    }
287

Thomas Witkowski's avatar
Thomas Witkowski committed
288
    subdomain->setDofMapping(&localDofMap);
Thomas Witkowski's avatar
Thomas Witkowski committed
289
290
291
    subdomain->setCoarseSpaceDofMapping(&primalDofMap); 
    if (stokesMode)
      subdomain->setCoarseSpaceDofMapping(&interfaceDofMap, pressureComponent);
Thomas Witkowski's avatar
Thomas Witkowski committed
292
293

    if (printTimings) {
Thomas Witkowski's avatar
BLUB    
Thomas Witkowski committed
294
      MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
295
      timeCounter = MPI::Wtime() - timeCounter;
296
      MSG("FETI-DP timing 01: %.5f seconds (creation of basic data structures)\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
297
298
	  timeCounter);
    }
299
300
301
  }


302
  void PetscSolverFeti::createPrimals(const FiniteElemSpace *feSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
303
  {
304
    FUNCNAME("PetscSolverFeti::createPrimals()");  
305

Thomas Witkowski's avatar
Thomas Witkowski committed
306
    if (feSpace == pressureFeSpace)
307
308
      return;

309
310
311
    // === Define all vertices on the interior boundaries of the macro mesh ===
    // === to be primal variables.                                          ===

Thomas Witkowski's avatar
Thomas Witkowski committed
312
    // Set of DOF indices that are considered to be primal variables.
313
    DofContainerSet& vertices = 
314
      meshDistributor->getBoundaryDofInfo(feSpace, meshLevel).geoDofs[VERTEX];
315
316

    DofIndexSet primals;
317
    for (DofContainerSet::iterator it = vertices.begin(); 
318
	 it != vertices.end(); ++it) {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
319
320
      if (dirichletRows[feSpace].count(**it))
	continue;
Thomas Witkowski's avatar
Thomas Witkowski committed
321

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
322
323
      if (meshLevel == 0) {
	primals.insert(**it);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
324
      } else {
325
	double e = 1e-8;
Thomas Witkowski's avatar
Thomas Witkowski committed
326
327
328
	WorldVector<double> c;
	feSpace->getMesh()->getDofIndexCoords(*it, feSpace, c);

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
329
330
331
332
333
334
335
	if (fabs(c[0]) < e || fabs(c[1]) < e ||
	    fabs(c[0] - 25.0) < e || fabs(c[1] - 25.0) < e ||
	    (fabs(c[0] - 12.5) < e && fabs(c[1] - 12.5) < e)) {
	  MSG("PRIMAL COORD %f %f\n", c[0], c[1]);
	  primals.insert(**it);
	}
      }
336
    }
337

338
339
340
341

    // === Calculate the number of primals that are owned by the rank and ===
    // === create local indices of the primals starting at zero.          ===

342
    for (DofIndexSet::iterator it = primals.begin(); it != primals.end(); ++it)
343
344
      if (meshDistributor->getDofMap()[feSpace].isRankDof(*it))
	primalDofMap[feSpace].insertRankDof(*it);
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
345
      else
346
  	primalDofMap[feSpace].insertNonRankDof(*it);
347
348
349
  }


350
  void PetscSolverFeti::createDuals(const FiniteElemSpace *feSpace)
351
352
  {
    FUNCNAME("PetscSolverFeti::createDuals()");
353

Thomas Witkowski's avatar
Thomas Witkowski committed
354
    if (feSpace == pressureFeSpace)
355
356
      return;

357
358
359
    // === Create global index of the dual nodes on each rank. ===

    DofContainer allBoundaryDofs;
360
    meshDistributor->getAllBoundaryDofs(feSpace, meshLevel, allBoundaryDofs);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
361
    
362
    for (DofContainer::iterator it = allBoundaryDofs.begin();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
363
364
365
366
367
368
369
370
371
372
373
	 it != allBoundaryDofs.end(); ++it) {
      if (dirichletRows[feSpace].count(**it))
	continue;

      if (isPrimal(feSpace, **it))
	continue;

      if (meshLevel == 0) {
	dualDofMap[feSpace].insertRankDof(**it);
      } else {
	if (meshDistributor->getDofMapSd()[feSpace].isRankDof(**it))
374
	  dualDofMap[feSpace].insertRankDof(**it);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
375
376
      }	  
    }
377
378
379
  }

  
380
381
382
383
  void PetscSolverFeti::createInterfaceNodes(const FiniteElemSpace *feSpace)
  {
    FUNCNAME("PetscSolverFeti::createInterfaceNodes()");

Thomas Witkowski's avatar
Thomas Witkowski committed
384
    if (feSpace != pressureFeSpace)
385
386
387
388
389
390
      return;

    DofContainer allBoundaryDofs;
    meshDistributor->getAllBoundaryDofs(feSpace, meshLevel, allBoundaryDofs);

    for (DofContainer::iterator it = allBoundaryDofs.begin();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
391
392
393
394
	 it != allBoundaryDofs.end(); ++it) {
      if (dirichletRows[feSpace].count(**it))
	continue;      
      
395
      if (meshDistributor->getDofMap()[feSpace].isRankDof(**it))
Thomas Witkowski's avatar
Thomas Witkowski committed
396
	interfaceDofMap[feSpace].insertRankDof(**it);
397
      else
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
398
399
	interfaceDofMap[feSpace].insertNonRankDof(**it);      
    }
400
401
402
  }


403
404
405
406
  void PetscSolverFeti::createLagrange(const FiniteElemSpace *feSpace)
  {
    FUNCNAME("PetscSolverFeti::createLagrange()");

Thomas Witkowski's avatar
Thomas Witkowski committed
407
    if (feSpace == pressureFeSpace)
408
409
      return;

410
411
    boundaryDofRanks[feSpace].clear();

Thomas Witkowski's avatar
Thomas Witkowski committed
412
413
414
    // Stores for all rank owned communication DOFs, if the counterpart is
    // a rank owned DOF in its subdomain. Thus, the following map stores to
    // each rank number all DOFs that fulfill this requirenment.
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
415
    map<int, std::set<DegreeOfFreedom> > sdRankDofs;
416

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
417
    if (meshLevel > 0) {
418
      StdMpi<vector<int> > stdMpi(mpiCommGlobal);
419
420
421
422
423
424
425
426
427

      for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), 
				meshLevel, feSpace);
	   !it.end(); it.nextRank()) {

	vector<int> subdomainRankDofs;
	subdomainRankDofs.reserve(it.getDofs().size());

	for (; !it.endDofIter(); it.nextDof()) {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
428
	  if (meshDistributor->getDofMapSd()[feSpace].isRankDof(it.getDofIndex()))
429
	    subdomainRankDofs.push_back(1);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
430
	  else
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
	    subdomainRankDofs.push_back(0);
	}

	stdMpi.send(it.getRank(), subdomainRankDofs);
      }	     

      for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), 
				meshLevel, feSpace);
	   !it.end(); it.nextRank())
	stdMpi.recv(it.getRank());

      stdMpi.startCommunication();

      for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), 
				meshLevel, feSpace); 
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
446
447
448
449
450
451
	   !it.end(); it.nextRank())
	for (; !it.endDofIter(); it.nextDof())
	  if (!isPrimal(feSpace, it.getDofIndex()))
	    if (stdMpi.getRecvData(it.getRank())[it.getDofCounter()] == 1)
	      sdRankDofs[it.getRank()].insert(it.getDofIndex());
    }
452

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
453
454
455
456
457
458
459
    if (dualDofMap[feSpace].nLocalDofs == 0)
      return;


    // === Create for each dual node that is owned by the rank, the set ===
    // === of ranks that contain this node (denoted by W(x_j)).         ===

460
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
461
462
463
464
465
466
467
    for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), 
			      meshLevel, feSpace); 
	 !it.end(); it.nextRank()) {
      for (; !it.endDofIter(); it.nextDof()) {
	if (!isPrimal(feSpace, it.getDofIndex())) {
	  boundaryDofRanks[feSpace][it.getDofIndex()].insert(mpiRank);

468
 	  if (meshLevel == 0 ||
Thomas Witkowski's avatar
Thomas Witkowski committed
469
 	      (meshLevel > 0 && sdRankDofs[it.getRank()].count(it.getDofIndex())))
470
	    boundaryDofRanks[feSpace][it.getDofIndex()].insert(it.getRank());	  
471
472
473
474
	}
      }
    }

475
476
477
478

    // === Communicate these sets for all rank owned dual nodes to other ===
    // === ranks that also have this node.                               ===

479
    StdMpi<vector<std::set<int> > > stdMpi(meshDistributor->getMpiComm());
480

481
    for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), meshLevel, feSpace);
482
483
	 !it.end(); it.nextRank())
      for (; !it.endDofIter(); it.nextDof())
484
	if (!isPrimal(feSpace, it.getDofIndex()))
485
486
487
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && sdRankDofs[it.getRank()].count(it.getDofIndex())))
	    stdMpi.getSendData(it.getRank()).push_back(boundaryDofRanks[feSpace][it.getDofIndex()]);
488
489
490

    stdMpi.updateSendDataSize();

491
    for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), meshLevel, feSpace); 
492
	 !it.end(); it.nextRank()) {
493
      bool recvFromRank = false;
494
      for (; !it.endDofIter(); it.nextDof()) {
495
	if (!isPrimal(feSpace, it.getDofIndex())) {
496
497
498
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && 
 	       meshDistributor->getDofMapSd()[feSpace].isRankDof(it.getDofIndex()))) {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
499
500
501
	    recvFromRank = true;
	    break;
	  }
502
	}
503
      }
504
505

      if (recvFromRank)
506
	stdMpi.recv(it.getRank());
507
    }
508

509
510
    stdMpi.startCommunication();

511
    for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), meshLevel, feSpace); 
512
	 !it.end(); it.nextRank()) {
513
      int i = 0;
514
      for (; !it.endDofIter(); it.nextDof())
515
	if (!isPrimal(feSpace, it.getDofIndex()))
516
517
518
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && 
 	       meshDistributor->getDofMapSd()[feSpace].isRankDof(it.getDofIndex())))	    
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
519
520
	    boundaryDofRanks[feSpace][it.getDofIndex()] = 
	      stdMpi.getRecvData(it.getRank())[i++];
521
522
	  else
	    lagrangeMap[feSpace].insertNonRankDof(it.getDofIndex());
523
524
    }

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
525

526
527
528
    // === Reserve for each dual node, on the rank that owns this node, the ===
    // === appropriate number of Lagrange constraints.                      ===

529
    int nRankLagrange = 0;
530
    DofMap& dualMap = dualDofMap[feSpace].getMap();
531
    for (DofMap::iterator it = dualMap.begin(); it != dualMap.end(); ++it) {
532
533
      if (meshDistributor->getDofMap()[feSpace].isRankDof(it->first)) {
	lagrangeMap[feSpace].insertRankDof(it->first, nRankLagrange);
534
	int degree = boundaryDofRanks[feSpace][it->first].size();
535
	nRankLagrange += (degree * (degree - 1)) / 2;
Thomas Witkowski's avatar
Thomas Witkowski committed
536
      } else {
537
	lagrangeMap[feSpace].insertNonRankDof(it->first);
538
539
      }
    }
540
    lagrangeMap[feSpace].nRankDofs = nRankLagrange;
541
542
543
  }


544
545
546
547
548
549
550
551
552
  void PetscSolverFeti::createAugmentedLagrange(const FiniteElemSpace *feSpace)
  {
    FUNCNAME("PetscSolverFeti::createAugmentedLagrange()");

    if (!augmentedLagrange)
      return;
  }


553
  void PetscSolverFeti::createIndexB(const FiniteElemSpace *feSpace)
554
  {
555
    FUNCNAME("PetscSolverFeti::createIndexB()");
556

557
    DOFAdmin* admin = feSpace->getAdmin();
558
559
560
561

    // === To ensure that all interior node on each rank are listen first in ===
    // === the global index of all B nodes, insert all interior nodes first, ===
    // === without defining a correct index.                                 ===
562

563
    int nLocalInterior = 0;    
564
    for (int i = 0; i < admin->getUsedSize(); i++) {
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
565
566
567
568
569
570
      if (admin->isDofFree(i) ||
	  isPrimal(feSpace, i) ||
	  isDual(feSpace, i) ||
	  isInterface(feSpace, i) ||
	  dirichletRows[feSpace].count(i))
	continue;      
571

Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
      if (meshLevel == 0) {
	localDofMap[feSpace].insertRankDof(i, nLocalInterior);
	
	if (fetiPreconditioner == FETI_DIRICHLET)
	  interiorDofMap[feSpace].insertRankDof(i, nLocalInterior);
	
	nLocalInterior++;	
      } else {
	if (meshDistributor->getDofMapSd()[feSpace].isRankDof(i))
	  localDofMap[feSpace].insertRankDof(i);
	else
	  localDofMap[feSpace].insertNonRankDof(i);
	
	TEST_EXIT_DBG(fetiPreconditioner == FETI_NONE)
	  ("Not yet implemnted!\n");	
587
      }
588
    }
Thomas Witkowski's avatar
FETI-DP    
Thomas Witkowski committed
589
    
590
591
    // === And finally, add the global indicies of all dual nodes. ===

592
    for (DofMap::iterator it = dualDofMap[feSpace].getMap().begin();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
593
594
	 it != dualDofMap[feSpace].getMap().end(); ++it) {
      if (meshLevel == 0) {
595
	localDofMap[feSpace].insertRankDof(it->first);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
596
      } else {
597
598
599
600
601
	if (meshDistributor->getDofMapSd()[feSpace].isRankDof(it->first))
	  localDofMap[feSpace].insertRankDof(it->first);
	else 
	  localDofMap[feSpace].insertNonRankDof(it->first);
      }
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
602
    }
603
604
605
  }


606
  void PetscSolverFeti::createMatLagrange(vector<const FiniteElemSpace*> &feSpaces)
607
608
609
  {
    FUNCNAME("PetscSolverFeti::createMatLagrange()");

Thomas Witkowski's avatar
Thomas Witkowski committed
610
    double wtime = MPI::Wtime();
611
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Thomas Witkowski committed
612

613
614
    // === Create distributed matrix for Lagrange constraints. ===

615
616
617
618
619
    MatCreateAIJ(mpiCommGlobal,
		 lagrangeMap.getRankDofs(), localDofMap.getRankDofs(),
		 lagrangeMap.getOverallDofs(), nGlobalOverallInterior,
		 2, PETSC_NULL, 2, PETSC_NULL,
		 &mat_lagrange);
620
    MatSetOption(mat_lagrange, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
621

622
623
624
    Vec vec_scale_lagrange;
    lagrangeMap.createVec(vec_scale_lagrange);

625
626
627
628
629
630
631
    // === Create for all duals the corresponding Lagrange constraints. On ===
    // === each rank we traverse all pairs (n, m) of ranks, with n < m,    ===
    // === that contain this node. If the current rank number is r, and    ===
    // === n == r, the rank sets 1.0 for the corresponding constraint, if  ===
    // === m == r, than the rank sets -1.0 for the corresponding           ===
    // === constraint.                                                     ===

Thomas Witkowski's avatar
Thomas Witkowski committed
632
    for (unsigned int k = 0; k < feSpaces.size(); k++) {
633
      DofMap &dualMap = dualDofMap[feSpaces[k]].getMap();
634

635
      for (DofMap::iterator it = dualMap.begin(); it != dualMap.end(); ++it) {
636
	TEST_EXIT_DBG(boundaryDofRanks[feSpaces[k]].count(it->first))
Thomas Witkowski's avatar
Thomas Witkowski committed
637
638
639
	  ("Should not happen!\n");
	
	// Global index of the first Lagrange constriant for this node.
640
	int index = lagrangeMap.getMatIndex(k, it->first);
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
641

Thomas Witkowski's avatar
Thomas Witkowski committed
642
	// Copy set of all ranks that contain this dual node.
643
644
	vector<int> W(boundaryDofRanks[feSpaces[k]][it->first].begin(), 
		      boundaryDofRanks[feSpaces[k]][it->first].end());
Thomas Witkowski's avatar
Thomas Witkowski committed
645
646
	// Number of ranks that contain this dual node.
	int degree = W.size();
Thomas Witkowski's avatar
Blub    
Thomas Witkowski committed
647
648

	TEST_EXIT_DBG(degree > 1)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
649
	
650
	int counter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
651
652
653
	for (int i = 0; i < degree; i++) {
	  for (int j = i + 1; j < degree; j++) {
	    if (W[i] == mpiRank || W[j] == mpiRank) {
654
655
656
657
658
	      MatSetValue(mat_lagrange, 
			  index + counter, 
			  localDofMap.getMatIndex(k, it->first) + rStartInterior,
			  (W[i] == mpiRank ? 1.0 : -1.0),
			  INSERT_VALUES);
659
	    }
660
661
662
663
664
665
666
667
668
669
670
671
672
673
	    counter++;
	  }
	}

	// === Create scaling factors for scaling the lagrange matrix, which ===
	// === is required for FETI-DP preconditioners.                      ===
	
	if (meshDistributor->getDofMap()[feSpaces[k]].isRankDof(it->first)) {
	  int nConstraints = (degree * (degree - 1)) / 2;
	  for (int i = 0; i < nConstraints; i++) {
	    VecSetValue(vec_scale_lagrange,
			index + i,
			1.0 / static_cast<double>(degree),
			INSERT_VALUES);
674
675
676
677
678
679
680
	  }
	}
      }
    }

    MatAssemblyBegin(mat_lagrange, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(mat_lagrange, MAT_FINAL_ASSEMBLY);
Thomas Witkowski's avatar
Thomas Witkowski committed
681

682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697

    // === If required, create \ref mat_lagrange_scaled ===

    VecAssemblyBegin(vec_scale_lagrange);
    VecAssemblyEnd(vec_scale_lagrange);

    if (fetiPreconditioner != FETI_NONE || stokesMode) {
      MatDuplicate(mat_lagrange, MAT_COPY_VALUES, &mat_lagrange_scaled);
      MatDiagonalScale(mat_lagrange_scaled, vec_scale_lagrange, PETSC_NULL);
    }

    VecDestroy(&vec_scale_lagrange);


    // === Print final timings. ===

Thomas Witkowski's avatar
BLUB    
Thomas Witkowski committed
698
699
    if (printTimings) {
      MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
700
701
      MSG("FETI-DP timing 05: %.5f seconds (creation of lagrange constraint matrix)\n", 
	  MPI::Wtime() - wtime);
Thomas Witkowski's avatar
BLUB    
Thomas Witkowski committed
702
    }
703
704
  }

705

Thomas Witkowski's avatar
Thomas Witkowski committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
  bool PetscSolverFeti::testWirebasketEdge(BoundaryObject &edge, const FiniteElemSpace *feSpace)
  {
    Element *el = edge.el;
    int i0 = el->getVertexOfEdge(edge.ithObj, 0);
    int i1 = el->getVertexOfEdge(edge.ithObj, 1);
    DegreeOfFreedom d0 = el->getDof(i0, 0);
    DegreeOfFreedom d1 = el->getDof(i1, 0);
    WorldVector<double> c0, c1;
    el->getMesh()->getDofIndexCoords(d0, feSpace, c0);
    el->getMesh()->getDofIndexCoords(d1, feSpace, c1);
    bool xe = fabs(c0[0] - c1[0]) < 1e-8;
    bool ye = fabs(c0[1] - c1[1]) < 1e-8;
    bool ze = fabs(c0[2] - c1[2]) < 1e-8;
    int counter = static_cast<int>(xe) + static_cast<int>(ye) + static_cast<int>(ze);
    return (counter == 2);
  }
722

723

724
725
726
727
728
729
730
731
732
733
734
  void PetscSolverFeti::createMatAugmentedLagrange(vector<const FiniteElemSpace*> &feSpaces)
  {
    FUNCNAME("PetscSolverFeti::createMatAugmentedLagrange()");

    if (!augmentedLagrange)
      return;

    double wtime = MPI::Wtime();

    nOverallEdges = 0;
    InteriorBoundary &intBound = meshDistributor->getIntBoundary();
Thomas Witkowski's avatar
Thomas Witkowski committed
735
    std::set<BoundaryObject> allEdges;
736
    for (InteriorBoundary::iterator it(intBound.getOwn()); !it.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
737
738
      if (it->rankObj.subObj == EDGE && 
	  testWirebasketEdge(it->rankObj, feSpaces[0]) && 
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
	  allEdges.count(it->rankObj) == 0) {
	bool dirichletOnlyEdge = true;

	DofContainer edgeDofs;
	it->rankObj.el->getAllDofs(feSpaces[0], it->rankObj, edgeDofs);
	for (DofContainer::iterator dit = edgeDofs.begin();
	     dit != edgeDofs.end(); ++dit) {
	  if (dirichletRows[feSpaces[0]].count(**dit) == 0) {
	    dirichletOnlyEdge = false;
	    break;
	  }
	}

	if (!dirichletOnlyEdge)
	  allEdges.insert(it->rankObj);
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
756
757

    nRankEdges = allEdges.size();    
758
759
760
761
    int rStartEdges = 0;
    mpi::getDofNumbering(mpiCommGlobal, nRankEdges, rStartEdges, nOverallEdges);

    MSG("nRankEdges = %d, nOverallEdges = %d\n", nRankEdges, nOverallEdges);
762
    
763
764
765
766
767
    nRankEdges *= feSpaces.size();
    rStartEdges *= feSpaces.size();
    nOverallEdges *= feSpaces.size();

    MatCreateAIJ(mpiCommGlobal,
768
769
		 nRankEdges, lagrangeMap.getRankDofs(),
		 nOverallEdges, lagrangeMap.getOverallDofs(),
770
771
772
773
		 1, PETSC_NULL, 1, PETSC_NULL, 
		 &mat_augmented_lagrange);
    MatSetOption(mat_augmented_lagrange, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

774
    int rowCounter = rStartEdges;
Thomas Witkowski's avatar
Thomas Witkowski committed
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
    for (std::set<BoundaryObject>::iterator edgeIt = allEdges.begin(); 
	 edgeIt != allEdges.end(); ++edgeIt) {
      for (int i = 0; i < feSpaces.size(); i++) {
	DofContainer edgeDofs;
	edgeIt->el->getAllDofs(feSpaces[i], *edgeIt, edgeDofs);
	
	for (DofContainer::iterator it = edgeDofs.begin();
	     it != edgeDofs.end(); ++it) {
	  TEST_EXIT_DBG(isPrimal(feSpaces[i], **it) == false)
	    ("Should not be primal!\n");
	  
	  if (dirichletRows[feSpaces[i]].count(**it))
	    continue;
	  
	  int col = lagrangeMap.getMatIndex(i, **it);
	  double value = 1.0;
	  MatSetValue(mat_augmented_lagrange, rowCounter, col, value, INSERT_VALUES);
792
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
793
794
795
	
	rowCounter++;
      }      
796
797
798
799
800
801
802
803
804
805
806
807
808
    }

    MatAssemblyBegin(mat_augmented_lagrange, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(mat_augmented_lagrange, MAT_FINAL_ASSEMBLY);

    if (printTimings) {
      MPI::COMM_WORLD.Barrier();
      MSG("FETI-DP timing 05a: %.5f seconds (creation of augmented lagrange constraint matrix)\n", 
	  MPI::Wtime() - wtime);
    }
  }


809
  void PetscSolverFeti::createSchurPrimalKsp(vector<const FiniteElemSpace*> &feSpaces)
810
  {
811
    FUNCNAME("PetscSolverFeti::createSchurPrimalKsp()");
812

Thomas Witkowski's avatar
Thomas Witkowski committed
813
    if (schurPrimalSolver == 0) {
814
815
      MSG("Create iterative schur primal solver!\n");

816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
      if (augmentedLagrange == false) {
	schurPrimalData.subSolver = subdomain;
	
	localDofMap.createVec(schurPrimalData.tmp_vec_b, nGlobalOverallInterior);
	primalDofMap.createVec(schurPrimalData.tmp_vec_primal);
	
	MatCreateShell(mpiCommGlobal,
		       primalDofMap.getRankDofs(), 
		       primalDofMap.getRankDofs(), 
		       primalDofMap.getOverallDofs(), 
		       primalDofMap.getOverallDofs(),
		       &schurPrimalData, 
		       &mat_schur_primal);
	MatShellSetOperation(mat_schur_primal, MATOP_MULT, 
			     (void(*)(void))petscMultMatSchurPrimal);	
      } else {
	schurPrimalAugmentedData.subSolver = subdomain;
833
	schurPrimalAugmentedData.nestedVec = true;
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852

	localDofMap.createVec(schurPrimalAugmentedData.tmp_vec_b0, nGlobalOverallInterior);
	localDofMap.createVec(schurPrimalAugmentedData.tmp_vec_b1, nGlobalOverallInterior);
	primalDofMap.createVec(schurPrimalAugmentedData.tmp_vec_primal);
	lagrangeMap.createVec(schurPrimalAugmentedData.tmp_vec_lagrange);

	schurPrimalAugmentedData.mat_lagrange = &mat_lagrange;
	schurPrimalAugmentedData.mat_augmented_lagrange = &mat_augmented_lagrange;

	MatCreateShell(mpiCommGlobal,
		       primalDofMap.getRankDofs() + nRankEdges, 
		       primalDofMap.getRankDofs() + nRankEdges, 
		       primalDofMap.getOverallDofs() + nOverallEdges, 
		       primalDofMap.getOverallDofs() + nOverallEdges,
		       &schurPrimalAugmentedData, 
		       &mat_schur_primal);
	MatShellSetOperation(mat_schur_primal, MATOP_MULT, 
			     (void(*)(void))petscMultMatSchurPrimalAugmented);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
853

854
      KSPCreate(mpiCommGlobal, &ksp_schur_primal);
Thomas Witkowski's avatar
Thomas Witkowski committed
855
      KSPSetOperators(ksp_schur_primal, mat_schur_primal, mat_schur_primal, SAME_NONZERO_PATTERN);
856
857
      KSPSetOptionsPrefix(ksp_schur_primal, "schur_primal_");
      KSPSetType(ksp_schur_primal, KSPGMRES);
Thomas Witkowski's avatar
Thomas Witkowski committed
858
859
      KSPSetFromOptions(ksp_schur_primal);
    } else {
860
861
      MSG("Create direct schur primal solver!\n");

862
863
      double wtime = MPI::Wtime();

864
865
866
      TEST_EXIT_DBG(meshLevel == 0)
	("Does not support for multilevel, check usage of localDofMap.\n");

867

868
      // === Create explicit matrix representation of the Schur primal system. ===
869

870
871
872
873
      if (!augmentedLagrange)
	createMatExplicitSchurPrimal();
      else
	createMatExplicitAugmentedSchurPrimal();
874
875
876


      // === Create KSP solver object and set appropriate solver options. ====
877

878
      KSPCreate(mpiCommGlobal, &ksp_schur_primal);
879
      KSPSetOperators(ksp_schur_primal, mat_schur_primal, mat_schur_primal,
880
		      SAME_NONZERO_PATTERN);
881
882
883
884
885
886
      KSPSetOptionsPrefix(ksp_schur_primal, "schur_primal_");
      KSPSetType(ksp_schur_primal, KSPPREONLY);
      PC pc_schur_primal;      
      KSPGetPC(ksp_schur_primal, &pc_schur_primal);
      PCSetType(pc_schur_primal, PCLU);
      PCFactorSetMatSolverPackage(pc_schur_primal, MATSOLVERMUMPS);
887
      KSPSetFromOptions(ksp_schur_primal);
Thomas Witkowski's avatar
Thomas Witkowski committed
888

889
890
891

      // === And finally print timings, if required. ===

Thomas Witkowski's avatar
Thomas Witkowski committed
892
      if (printTimings) {
Thomas Witkowski's avatar
BLUB    
Thomas Witkowski committed
893
	MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
894
895
896
897
898
899
900
901
902
903
	MatInfo minfo;
	MatGetInfo(mat_schur_primal, MAT_GLOBAL_SUM, &minfo);
	MSG("Schur primal matrix nnz = %f\n", minfo.nz_used);
	
	MSG("FETI-DP timing 06: %.5f seconds (creation of schur primal matrix)\n",
	    MPI::Wtime() - wtime);

	wtime = MPI::Wtime();
	KSPSetUp(ksp_schur_primal);
	KSPSetUpOnBlocks(ksp_schur_primal);
Thomas Witkowski's avatar
BLUB    
Thomas Witkowski committed
904
	MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
905
906
907
	MSG("FETI-DP timing 07: %.5f seconds (factorization of primal schur matrix).\n",
	    MPI::Wtime() - wtime);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
908
    }
909
910
911
  }


912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
  void PetscSolverFeti::createMatExplicitSchurPrimal()
  {
    FUNCNAME("PetscSolverFeti::createMatExplicitSchurPrimal()");

    int creationMode = 0;
    Parameters::get("parallel->feti->schur primal creation mode", creationMode);
    if (creationMode == 0) {
      // matK = inv(A_BB) A_BPi
      Mat matK;
      petsc_helper::blockMatMatSolve(subdomain->getSolver(), 
				     subdomain->getMatInteriorCoarse(),
				     matK);
      
      
      // mat_schur_primal = A_PiPi - A_PiB inv(A_BB) A_BPi
      //                  = A_PiPi - A_PiB matK
      MatMatMult(subdomain->getMatCoarseInterior(), matK, MAT_INITIAL_MATRIX, 
		 PETSC_DEFAULT, &mat_schur_primal);
      MatAYPX(mat_schur_primal, -1.0, subdomain->getMatCoarse(), DIFFERENT_NONZERO_PATTERN);
      
      MatDestroy(&matK);
    } else {
      Mat tmp;
      
      schurPrimalData.subSolver = subdomain;
      
      localDofMap.createVec(schurPrimalData.tmp_vec_b, nGlobalOverallInterior);
      primalDofMap.createVec(schurPrimalData.tmp_vec_primal);
      
      MatCreateShell(mpiCommGlobal,
		     primalDofMap.getRankDofs(), 
		     primalDofMap.getRankDofs(), 
		     primalDofMap.getOverallDofs(), 
		     primalDofMap.getOverallDofs(),
		     &schurPrimalData, 
		     &tmp);
      MatShellSetOperation(tmp, MATOP_MULT, 
			   (void(*)(void))petscMultMatSchurPrimal);
      MatComputeExplicitOperator(tmp, &mat_schur_primal);

      MatDestroy(&tmp);
      schurPrimalData.subSolver = NULL;
      VecDestroy(&schurPrimalData.tmp_vec_b);
      VecDestroy(&schurPrimalData.tmp_vec_primal);
    }
  }
  
  
  void PetscSolverFeti::createMatExplicitAugmentedSchurPrimal()
  {
    FUNCNAME("PetscSolverFeti::createMatExplicitAugmentedSchurPrimal()");

    int creationMode = 1;
    Parameters::get("parallel->feti->schur primal creation mode", creationMode);
    if (creationMode == 0) {
      // qj = -Q J
      Mat qj;
      MatMatMult(mat_augmented_lagrange, mat_lagrange, MAT_INITIAL_MATRIX,
		 PETSC_DEFAULT, &qj);
      MatScale(qj, -1.0);
      
      
      // matTmp = inv(A_BB) A_BPi
      Mat matTmp;
      petsc_helper::blockMatMatSolve(subdomain->getSolver(), 
				     subdomain->getMatInteriorCoarse(),
				     matTmp);
      
      
      // mat00 = A_PiPi - A_PiB inv(A_BB) A_BPi
      //       = A_PiPi - A_PiB matTmp
      Mat mat00;
      MatMatMult(subdomain->getMatCoarseInterior(), matTmp, MAT_INITIAL_MATRIX, 
		 PETSC_DEFAULT, &mat00);
      MatAYPX(mat00, -1.0, subdomain->getMatCoarse(), DIFFERENT_NONZERO_PATTERN);
      
      
      // mat10 = -Q J inv(A_BB) A_BPi
      //       = qj matTmp
      Mat mat10;
      MatMatMult(qj, matTmp, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &mat10); 
      
      
      // matTmp = inv(A_BB) trans(J) trans(Q)
      Mat qT, jTqT;
      MatTranspose(mat_augmented_lagrange, MAT_INITIAL_MATRIX, &qT);
      MatTransposeMatMult(mat_lagrange, qT, MAT_INITIAL_MATRIX, PETSC_DEFAULT, 
			  &jTqT);
      petsc_helper::blockMatMatSolve(subdomain->getSolver(), jTqT, matTmp);
      MatDestroy(&qT);
      MatDestroy(&jTqT);
      
      // mat01 = -A_PiB inv(A_BB) trans(J) trans(Q)
      //       = -A_PiB matTmp
      Mat mat01;
      MatMatMult(subdomain->getMatCoarseInterior(), matTmp, MAT_INITIAL_MATRIX, 
		 PETSC_DEFAULT, &mat01);
      MatScale(mat01, -1.0);
      
      // mat11 = -Q J inv(A_BB) trans(J) trans(Q)
      //       = qj matTmp
      Mat mat11;
      MatMatMult(qj, matTmp, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &mat11);
      
      MatDestroy(&matTmp);
      MatDestroy(&qj);
      
      Mat nestMat[4] = {mat00, mat01, mat10, mat11};
      MatCreateNest(PETSC_COMM_WORLD, 2, PETSC_NULL, 2, PETSC_NULL, nestMat, &matTmp);
      petsc_helper::matNestConvert(matTmp, mat_schur_primal);
      
      MatDestroy(&mat00);
      MatDestroy(&mat01);
      MatDestroy(&mat10);
      MatDestroy(&mat11);
      MatDestroy(&matTmp);
    } else {
      Mat tmp;

      schurPrimalAugmentedData.subSolver = subdomain;
      schurPrimalAugmentedData.nestedVec = false;

      localDofMap.createVec(schurPrimalAugmentedData.tmp_vec_b0, nGlobalOverallInterior);
      localDofMap.createVec(schurPrimalAugmentedData.tmp_vec_b1, nGlobalOverallInterior);
      primalDofMap.createVec(schurPrimalAugmentedData.tmp_vec_primal);
      lagrangeMap.createVec(schurPrimalAugmentedData.tmp_vec_lagrange);

      schurPrimalAugmentedData.mat_lagrange = &mat_lagrange;
      schurPrimalAugmentedData.mat_augmented_lagrange = &mat_augmented_lagrange;
      
      MatCreateShell(mpiCommGlobal,
		     primalDofMap.getRankDofs() + nRankEdges, 
		     primalDofMap.getRankDofs() + nRankEdges, 
		     primalDofMap.getOverallDofs() + nOverallEdges, 
		     primalDofMap.getOverallDofs() + nOverallEdges,
		     &schurPrimalAugmentedData, 
		     &tmp);
      MatShellSetOperation(tmp, MATOP_MULT, 
			   (void(*)(void))petscMultMatSchurPrimalAugmented);

      MatComputeExplicitOperator(tmp, &mat_schur_primal);

      MatDestroy(&tmp);
      schurPrimalAugmentedData.subSolver = NULL;
      schurPrimalAugmentedData.mat_lagrange = NULL;
      schurPrimalAugmentedData.mat_augmented_lagrange = NULL;
      VecDestroy(&schurPrimalAugmentedData.tmp_vec_b0);
      VecDestroy(&schurPrimalAugmentedData.tmp_vec_b1);
      VecDestroy(&schurPrimalAugmentedData.tmp_vec_primal);
      VecDestroy(&schurPrimalAugmentedData.tmp_vec_lagrange);
    }
  }


1066
1067
1068
1069
  void PetscSolverFeti::destroySchurPrimalKsp()
  {
    FUNCNAME("PetscSolverFeti::destroySchurPrimal()");

1070
    if (schurPrimalSolver == 0) {
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
      if (augmentedLagrange == false) {
	schurPrimalData.subSolver = NULL;
	
	VecDestroy(&schurPrimalData.tmp_vec_b);
	VecDestroy(&schurPrimalData.tmp_vec_primal);
      } else {
	schurPrimalAugmentedData.subSolver = NULL;
	schurPrimalAugmentedData.mat_lagrange = NULL;
	schurPrimalAugmentedData.mat_augmented_lagrange = NULL;

	VecDestroy(&schurPrimalAugmentedData.tmp_vec_b0);
	VecDestroy(&schurPrimalAugmentedData.tmp_vec_b1);
	VecDestroy(&schurPrimalAugmentedData.tmp_vec_primal);
	VecDestroy(&schurPrimalAugmentedData.tmp_vec_lagrange);
      }
1086
    }
1087
1088
1089
    
    MatDestroy(&mat_schur_primal);
    KSPDestroy(&ksp_schur_primal);
1090
1091
1092
  }


1093
  void PetscSolverFeti::createFetiKsp(vector<const FiniteElemSpace*> &feSpaces)
1094
1095
1096
  {
    FUNCNAME("PetscSolverFeti::createFetiKsp()");

1097
1098
    // === Create FETI-DP solver object. ===

1099
1100
1101
1102
    fetiData.mat_lagrange = &mat_lagrange;
    fetiData.subSolver = subdomain;
    fetiData.ksp_schur_primal = &ksp_schur_primal;

Thomas Witkowski's avatar
Thomas Witkowski committed
1103
    localDofMap.createVec(fetiData.tmp_vec_b0, nGlobalOverallInterior);
1104
    lagrangeMap.createVec(fetiData.tmp_vec_lagrange);
Thomas Witkowski's avatar
Thomas Witkowski committed
1105
    primalDofMap.createVec(fetiData.tmp_vec_primal0);
1106

Thomas Witkowski's avatar
Thomas Witkowski committed
1107
    if (stokesMode == false) {      
1108
1109
1110
1111
1112
1113
      MatCreateShell(mpiCommGlobal,
		     lagrangeMap.getRankDofs(), 
		     lagrangeMap.getRankDofs(),
		     lagrangeMap.getOverallDofs(), 
		     lagrangeMap.getOverallDofs(),
		     &fetiData, &mat_feti);
1114
1115
1116
1117
1118
1119
1120
1121
1122
      if (augmentedLagrange == false) {
	MatShellSetOperation(mat_feti, MATOP_MULT, 
			     (void(*)(void))petscMultMatFeti);
      } else {
	fetiData.mat_augmented_lagrange = &mat_augmented_lagrange;
	primalDofMap.createVec(fetiData.tmp_vec_primal1);
	MatShellSetOperation(mat_feti, MATOP_MULT, 
			     (void(*)(void))petscMultMatFetiAugmented);	
      }
1123
    }  else {
1124
1125
      TEST_EXIT_DBG(!augmentedLagrange)("Not yet implemented!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
1126
1127
1128
1129
      localDofMap.createVec(fetiData.tmp_vec_b1, nGlobalOverallInterior);
      primalDofMap.createVec(fetiData.tmp_vec_primal1);
      interfaceDofMap.createVec(fetiData.tmp_vec_interface);

1130
1131
1132
1133
1134
      MatCreateShell(mpiCommGlobal,
		     interfaceDofMap.getRankDofs() + lagrangeMap.getRankDofs(), 
		     interfaceDofMap.getRankDofs() + lagrangeMap.getRankDofs(),
		     interfaceDofMap.getOverallDofs() + lagrangeMap.getOverallDofs(), 
		     interfaceDofMap.getOverallDofs() + lagrangeMap.getOverallDofs(),
1135
		     &fetiData, &