PetscSolverFeti.cc 75.9 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.


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

namespace AMDiS {

  using namespace std;

30
  PetscSolverFeti::PetscSolverFeti(string name)
31
    : PetscSolver(name),
32 33 34 35 36 37
      primalDofMap(COMPONENT_WISE),
      dualDofMap(COMPONENT_WISE),
      interfaceDofMap(COMPONENT_WISE),
      localDofMap(COMPONENT_WISE),
      lagrangeMap(COMPONENT_WISE),
      interiorDofMap(COMPONENT_WISE),
38
      schurPrimalSolver(0),
39
      multiLevelTest(false),
Thomas Witkowski's avatar
Thomas Witkowski committed
40
      subdomain(NULL),
41
      massMatrixSolver(NULL),
42 43
      meshLevel(0),
      rStartInterior(0),
Thomas Witkowski's avatar
Thomas Witkowski committed
44
      nGlobalOverallInterior(0),
Thomas Witkowski's avatar
Thomas Witkowski committed
45
      printTimings(false),
Thomas Witkowski's avatar
Thomas Witkowski committed
46
      stokesMode(false),
47
      augmentedLagrange(false),
Thomas Witkowski's avatar
Thomas Witkowski committed
48
      pressureComponent(-1)
49 50 51 52
  {
    FUNCNAME("PetscSolverFeti::PetscSolverFeti()");

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

68 69 70 71 72 73 74 75
    preconditionerName = "";
    Parameters::get(initFileStr + "->right precon", preconditionerName);
    if (preconditionerName != "" && preconditionerName != "no") {
      ERROR_EXIT("FETI-DP does not support right preconditioning! (parameter \"%s->right precon\" has value \"%s\")\n",
		 initFileStr.c_str(), preconditionerName.c_str());
    }

    Parameters::get(initFileStr + "->feti->schur primal solver", schurPrimalSolver);
Thomas Witkowski's avatar
Thomas Witkowski committed
76 77 78
    TEST_EXIT(schurPrimalSolver == 0 || schurPrimalSolver == 1)
      ("Wrong solver \"%d\"for the Schur primal complement!\n", 
       schurPrimalSolver);
79

80 81 82 83 84 85 86 87 88 89 90
    Parameters::get(initFileStr + "->feti->stokes mode", stokesMode);
    if (stokesMode) {
      Parameters::get(initFileStr + "->feti->pressure component", pressureComponent);
      TEST_EXIT(pressureComponent >= 0)
	("FETI-DP in Stokes mode, no pressure component defined!\n");
    }
			   
    Parameters::get(initFileStr + "->feti->augmented lagrange", augmentedLagrange);

    Parameters::get(initFileStr + "->feti->symmetric", isSymmetric);

91
    Parameters::get("parallel->multi level test", multiLevelTest);
92 93
    if (multiLevelTest)
      meshLevel = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
94 95

    Parameters::get("parallel->print timings", printTimings);
96 97 98
  }


99
  void PetscSolverFeti::initialize()
100
  {
101 102
    FUNCNAME("PetscSolverFeti::initialize()");

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

106
    MeshLevelData& levelData = meshDistributor->getMeshLevelData();
Thomas Witkowski's avatar
Thomas Witkowski committed
107

Thomas Witkowski's avatar
Thomas Witkowski committed
108
    if (subdomain == NULL) {
109
      subdomain = new PetscSolverGlobalMatrix("");
110
      subdomain->setSymmetric(isSymmetric);
111 112
      //      subdomain->setHandleDirichletRows(false);
      subdomain->setHandleDirichletRows(true);
113

114
      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
115
	subdomain->setMeshDistributor(meshDistributor, 
116
				      mpiCommGlobal, mpiCommLocal);
117
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
118
	subdomain->setMeshDistributor(meshDistributor, 
119 120
				      levelData.getMpiComm(meshLevel - 1),
				      levelData.getMpiComm(meshLevel));
Thomas Witkowski's avatar
Thomas Witkowski committed
121
	subdomain->setLevel(meshLevel);
122 123
      }
    }
124

125 126 127 128
    primalDofMap.init(levelData, componentSpaces, feSpaces);   
    dualDofMap.init(levelData, componentSpaces, feSpaces, false);
    localDofMap.init(levelData, componentSpaces, feSpaces, meshLevel != 0);
    lagrangeMap.init(levelData, componentSpaces, feSpaces);
Thomas Witkowski's avatar
Thomas Witkowski committed
129

Thomas Witkowski's avatar
Thomas Witkowski committed
130
    if (stokesMode)
131
      interfaceDofMap.init(levelData, componentSpaces, feSpaces);
Thomas Witkowski's avatar
Thomas Witkowski committed
132

133
    if (fetiPreconditioner == FETI_DIRICHLET) {
134 135 136
      TEST_EXIT(meshLevel == 0)
	("Dirichlet preconditioner not yet implemented for multilevel FETI-DP\n");

137
      interiorDofMap.init(levelData, componentSpaces, feSpaces, false);
138
    }
139 140 141
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
142 143 144 145
  void PetscSolverFeti::createDirichletData(Matrix<DOFMatrix*> &mat)
  {
    FUNCNAME("PetscSolverFeti::createDirichletData()");

146 147
    return;

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
148
    int nComponents = mat.getSize();
149 150
    for (int component = 0; component < nComponents; component++) {
      DOFMatrix* dofMat = mat[component][component];
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
151 152
      if (!dofMat)
	continue;
153 154

      dirichletRows[component] = dofMat->getDirichletRows();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
155 156 157 158
    }
  }


159 160 161 162
  void PetscSolverFeti::createFetiData()
  {
    FUNCNAME("PetscSolverFeti::createFetiData()");

Thomas Witkowski's avatar
Thomas Witkowski committed
163 164
    double timeCounter = MPI::Wtime();

165 166
    MeshLevelData& levelData = meshDistributor->getMeshLevelData();

167 168 169 170
    primalDofMap.clear();
    dualDofMap.clear();
    lagrangeMap.clear();
    localDofMap.clear();
171
    if (fetiPreconditioner == FETI_DIRICHLET)
172 173
      interiorDofMap.clear();

174 175
    primalDofMap.setDofComm(meshDistributor->getDofComm());
    lagrangeMap.setDofComm(meshDistributor->getDofComm());
176

177 178
    primalDofMap.setMpiComm(levelData.getMpiComm(0), 0);
    dualDofMap.setMpiComm(levelData.getMpiComm(0), 0);
179
    lagrangeMap.setMpiComm(levelData.getMpiComm(0), 0);
180
    localDofMap.setMpiComm(levelData.getMpiComm(meshLevel), meshLevel);
181
    if (fetiPreconditioner == FETI_DIRICHLET)
182
      interiorDofMap.setMpiComm(levelData.getMpiComm(meshLevel), meshLevel);
183

184 185 186
    if (meshLevel == 0)
      localDofMap.setDofComm(meshDistributor->getDofComm());
    else
187 188
      localDofMap.setDofComm(meshDistributor->getDofCommSd());

Thomas Witkowski's avatar
Thomas Witkowski committed
189
    if (stokesMode) {
190 191 192 193 194
      interfaceDofMap.clear();
      interfaceDofMap.setDofComm(meshDistributor->getDofComm());
      interfaceDofMap.setMpiComm(levelData.getMpiComm(0), 0);
    }

195 196 197 198 199 200
    int nComponents = componentSpaces.size();
    for (int component = 0; component < nComponents; component++) {
      createPrimals(component);  
      createDuals(component);
      createInterfaceNodes(component);
      createIndexB(component);
201
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
202 203 204 205

    primalDofMap.update();
    dualDofMap.update();
    localDofMap.update();
206
    if (fetiPreconditioner == FETI_DIRICHLET)
207
      interiorDofMap.update();
Thomas Witkowski's avatar
Thomas Witkowski committed
208

Thomas Witkowski's avatar
Thomas Witkowski committed
209
    if (stokesMode)
210 211
      interfaceDofMap.update();

212 213 214
    for (int component = 0; component < nComponents; component++) {
      createLagrange(component);
      createAugmentedLagrange(component);
215
    }
216

217 218 219
    lagrangeMap.update();


220 221 222 223 224 225 226 227 228 229 230 231
    // === ===

    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();

232
      mpi::getDofNumbering(mpiCommGlobal, groupRowsInterior,
233 234 235 236 237 238 239 240 241
			   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);
    }

242
    MSG("FETI-DP data created on mesh level %d\n", meshLevel);
243 244
    for (unsigned int i = 0; i < componentSpaces.size(); i++) {
      const FiniteElemSpace *feSpace = componentSpaces[i];
245 246

      MSG("FETI-DP data for %d-ith component (FE space %p):\n", i, feSpace);
Thomas Witkowski's avatar
Thomas Witkowski committed
247

248
      if (i == pressureComponent) {
Thomas Witkowski's avatar
Thomas Witkowski committed
249
	MSG("  nRankInterface = %d  nOverallInterface = %d\n",
250 251
	    interfaceDofMap[i].nRankDofs, 
	    interfaceDofMap[i].nOverallDofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
252
      } else {
253
	MSG("  nRankPrimals = %d   nLocalPrimals = %d  nOverallPrimals = %d\n", 
254 255 256
	    primalDofMap[i].nRankDofs, 
	    primalDofMap[i].nLocalDofs,
	    primalDofMap[i].nOverallDofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
257 258
	
	MSG("  nRankDuals = %d  nOverallDuals = %d\n",
259 260
	    dualDofMap[i].nRankDofs, 
	    dualDofMap[i].nOverallDofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
261 262
	
	MSG("  nRankLagrange = %d  nOverallLagrange = %d\n",
263 264
	    lagrangeMap[i].nRankDofs, 
	    lagrangeMap[i].nOverallDofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
265
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
266
    }
267

Thomas Witkowski's avatar
Thomas Witkowski committed
268
    subdomain->setDofMapping(&localDofMap);
Thomas Witkowski's avatar
Thomas Witkowski committed
269 270 271
    subdomain->setCoarseSpaceDofMapping(&primalDofMap); 
    if (stokesMode)
      subdomain->setCoarseSpaceDofMapping(&interfaceDofMap, pressureComponent);
Thomas Witkowski's avatar
Thomas Witkowski committed
272 273

    if (printTimings) {
Thomas Witkowski's avatar
BLUB  
Thomas Witkowski committed
274
      MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
275
      timeCounter = MPI::Wtime() - timeCounter;
276
      MSG("FETI-DP timing 01: %.5f seconds (creation of basic data structures)\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
277 278
	  timeCounter);
    }
279 280 281
  }


282
  void PetscSolverFeti::createPrimals(int component)
Thomas Witkowski's avatar
Thomas Witkowski committed
283
  {
284
    FUNCNAME("PetscSolverFeti::createPrimals()");  
285

286
    if (component == pressureComponent)
287 288
      return;

289 290
    const FiniteElemSpace *feSpace = componentSpaces[component];

291 292 293
    // === Define all vertices on the interior boundaries of the macro mesh ===
    // === to be primal variables.                                          ===

Thomas Witkowski's avatar
Thomas Witkowski committed
294
    // Set of DOF indices that are considered to be primal variables.
295
    DofContainerSet& vertices = 
296
      meshDistributor->getBoundaryDofInfo(feSpace, meshLevel).geoDofs[VERTEX];
297 298

    DofIndexSet primals;
299
    for (DofContainerSet::iterator it = vertices.begin(); 
300
	 it != vertices.end(); ++it) {
301
      
302
      if (dirichletRows[component].count(**it))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
303
	continue;
Thomas Witkowski's avatar
Thomas Witkowski committed
304

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
305 306
      if (meshLevel == 0) {
	primals.insert(**it);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
307
      } else {
308
	double e = 1e-8;
Thomas Witkowski's avatar
Thomas Witkowski committed
309 310 311
	WorldVector<double> c;
	feSpace->getMesh()->getDofIndexCoords(*it, feSpace, c);

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
312 313 314 315 316 317 318
	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);
	}
      }
319
    }
320

321 322 323 324

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

325
    for (DofIndexSet::iterator it = primals.begin(); it != primals.end(); ++it)
326
      if (dofMap[feSpace].isRankDof(*it)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
327
	primalDofMap[component].insertRankDof(*it);
328
      } else
Thomas Witkowski's avatar
Thomas Witkowski committed
329
  	primalDofMap[component].insertNonRankDof(*it);
330 331 332
  }


333
  void PetscSolverFeti::createDuals(int component)
334 335
  {
    FUNCNAME("PetscSolverFeti::createDuals()");
336

337
    if (component == pressureComponent)
338 339
      return;

340 341
    const FiniteElemSpace *feSpace = componentSpaces[component];

342 343 344
    // === Create global index of the dual nodes on each rank. ===

    DofContainer allBoundaryDofs;
345
    meshDistributor->getAllBoundaryDofs(feSpace, meshLevel, allBoundaryDofs);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
346
    
347
    for (DofContainer::iterator it = allBoundaryDofs.begin();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
348
	 it != allBoundaryDofs.end(); ++it) {
349
      if (dirichletRows[component].count(**it))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
350 351
	continue;

Thomas Witkowski's avatar
Thomas Witkowski committed
352
      if (isPrimal(component, **it))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
353 354 355
	continue;

      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
356
	dualDofMap[component].insertRankDof(**it);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
357
      } else {
358
	if (dofMapSd[feSpace].isRankDof(**it))
Thomas Witkowski's avatar
Thomas Witkowski committed
359
	  dualDofMap[component].insertRankDof(**it);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
360 361
      }	  
    }
362 363 364
  }

  
365
  void PetscSolverFeti::createInterfaceNodes(int component)
366 367 368
  {
    FUNCNAME("PetscSolverFeti::createInterfaceNodes()");

369
    if (component != pressureComponent)
370 371
      return;

372
    const FiniteElemSpace *feSpace = componentSpaces[component];
373 374 375 376
    DofContainer allBoundaryDofs;
    meshDistributor->getAllBoundaryDofs(feSpace, meshLevel, allBoundaryDofs);

    for (DofContainer::iterator it = allBoundaryDofs.begin();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
377
	 it != allBoundaryDofs.end(); ++it) {
378
      if (dirichletRows[component].count(**it))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
379 380
	continue;      
      
381
      if (dofMap[feSpace].isRankDof(**it))
Thomas Witkowski's avatar
Thomas Witkowski committed
382
	interfaceDofMap[component].insertRankDof(**it);
383
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
384
	interfaceDofMap[component].insertNonRankDof(**it);      
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
385
    }
386 387 388
  }


389
  void PetscSolverFeti::createLagrange(int component)
390 391 392
  {
    FUNCNAME("PetscSolverFeti::createLagrange()");

393
    if (component == pressureComponent)
394 395
      return;

396
    const FiniteElemSpace *feSpace = componentSpaces[component];
397 398
    boundaryDofRanks[feSpace].clear();

Thomas Witkowski's avatar
Thomas Witkowski committed
399 400 401
    // 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
402
    map<int, std::set<DegreeOfFreedom> > sdRankDofs;
403

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
404
    if (meshLevel > 0) {
405
      StdMpi<vector<int> > stdMpi(mpiCommGlobal);
406 407 408 409 410 411 412 413 414

      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()) {
415
	  if (dofMapSd[feSpace].isRankDof(it.getDofIndex()))
416
	    subdomainRankDofs.push_back(1);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
417
	  else
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	    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
433 434
	   !it.end(); it.nextRank())
	for (; !it.endDofIter(); it.nextDof())
Thomas Witkowski's avatar
Thomas Witkowski committed
435
	  if (!isPrimal(component, it.getDofIndex()))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
436 437 438
	    if (stdMpi.getRecvData(it.getRank())[it.getDofCounter()] == 1)
	      sdRankDofs[it.getRank()].insert(it.getDofIndex());
    }
439

Thomas Witkowski's avatar
Thomas Witkowski committed
440
    if (dualDofMap[component].nLocalDofs == 0)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
441 442 443 444 445 446
      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)).         ===

447
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
448 449 450 451
    for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), 
			      meshLevel, feSpace); 
	 !it.end(); it.nextRank()) {
      for (; !it.endDofIter(); it.nextDof()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
452
	if (!isPrimal(component, it.getDofIndex())) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
453 454
	  boundaryDofRanks[feSpace][it.getDofIndex()].insert(mpiRank);

455
 	  if (meshLevel == 0 ||
Thomas Witkowski's avatar
Thomas Witkowski committed
456
 	      (meshLevel > 0 && sdRankDofs[it.getRank()].count(it.getDofIndex())))
457
	    boundaryDofRanks[feSpace][it.getDofIndex()].insert(it.getRank());	  
458 459 460 461
	}
      }
    }

462 463 464 465

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

466
    StdMpi<vector<std::set<int> > > stdMpi(meshDistributor->getMpiComm());
467

468
    for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), meshLevel, feSpace);
469 470
	 !it.end(); it.nextRank())
      for (; !it.endDofIter(); it.nextDof())
Thomas Witkowski's avatar
Thomas Witkowski committed
471
	if (!isPrimal(component, it.getDofIndex()))
472 473 474
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && sdRankDofs[it.getRank()].count(it.getDofIndex())))
	    stdMpi.getSendData(it.getRank()).push_back(boundaryDofRanks[feSpace][it.getDofIndex()]);
475 476 477

    stdMpi.updateSendDataSize();

478
    for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), meshLevel, feSpace); 
479
	 !it.end(); it.nextRank()) {
480
      bool recvFromRank = false;
481
      for (; !it.endDofIter(); it.nextDof()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
482
	if (!isPrimal(component, it.getDofIndex())) {
483 484
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && 
485
	       dofMapSd[feSpace].isRankDof(it.getDofIndex()))) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
486 487 488
	    recvFromRank = true;
	    break;
	  }
489
	}
490
      }
491 492

      if (recvFromRank)
493
	stdMpi.recv(it.getRank());
494
    }
495

496 497
    stdMpi.startCommunication();

498
    for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), meshLevel, feSpace); 
499
	 !it.end(); it.nextRank()) {
500
      int i = 0;
501
      for (; !it.endDofIter(); it.nextDof())
Thomas Witkowski's avatar
Thomas Witkowski committed
502
	if (!isPrimal(component, it.getDofIndex()))
503 504
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && 
505
	       dofMapSd[feSpace].isRankDof(it.getDofIndex())))	    
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
506 507
	    boundaryDofRanks[feSpace][it.getDofIndex()] = 
	      stdMpi.getRecvData(it.getRank())[i++];
508
	  else
Thomas Witkowski's avatar
Thomas Witkowski committed
509
	    lagrangeMap[component].insertNonRankDof(it.getDofIndex());
510 511
    }

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
512

513 514 515
    // === Reserve for each dual node, on the rank that owns this node, the ===
    // === appropriate number of Lagrange constraints.                      ===

516
    int nRankLagrange = 0;
517
    DofMap& dualMap = dualDofMap[component].getMap();
518
    for (DofMap::iterator it = dualMap.begin(); it != dualMap.end(); ++it) {
519
      if (dofMap[feSpace].isRankDof(it->first)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
520
	lagrangeMap[component].insertRankDof(it->first, nRankLagrange);
521
	int degree = boundaryDofRanks[feSpace][it->first].size();
522
	nRankLagrange += (degree * (degree - 1)) / 2;
Thomas Witkowski's avatar
Thomas Witkowski committed
523
      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
524
	lagrangeMap[component].insertNonRankDof(it->first);
525 526
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
527
    lagrangeMap[component].nRankDofs = nRankLagrange;
528 529 530
  }


531
  void PetscSolverFeti::createAugmentedLagrange(int component)
532 533 534 535 536 537 538 539
  {
    FUNCNAME("PetscSolverFeti::createAugmentedLagrange()");

    if (!augmentedLagrange)
      return;
  }


540
  void PetscSolverFeti::createIndexB(int component)
541
  {
542
    FUNCNAME("PetscSolverFeti::createIndexB()");
543

544 545

    const FiniteElemSpace *feSpace = componentSpaces[component];
546
    DOFAdmin* admin = feSpace->getAdmin();
547 548 549 550

    // === 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.                                 ===
551

552
    int nLocalInterior = 0;    
553
    for (int i = 0; i < admin->getUsedSize(); i++) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
554
      if (admin->isDofFree(i) ||
Thomas Witkowski's avatar
Thomas Witkowski committed
555 556 557
	  isPrimal(component, i) ||
	  isDual(component, i) ||
	  isInterface(component, i) ||
558
	  dirichletRows[component].count(i))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
559
	continue;      
560

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
561
      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
562
	localDofMap[component].insertRankDof(i, nLocalInterior);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
563 564
	
	if (fetiPreconditioner == FETI_DIRICHLET)
565
	  interiorDofMap[component].insertRankDof(i, nLocalInterior);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
566 567 568
	
	nLocalInterior++;	
      } else {
569
	if (dofMapSd[feSpace].isRankDof(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
570
	  localDofMap[component].insertRankDof(i);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
571
	else
Thomas Witkowski's avatar
Thomas Witkowski committed
572
	  localDofMap[component].insertNonRankDof(i);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
573 574 575
	
	TEST_EXIT_DBG(fetiPreconditioner == FETI_NONE)
	  ("Not yet implemnted!\n");	
576
      }
577
    }
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
578
    
579 580
    // === And finally, add the global indicies of all dual nodes. ===

581 582
    for (DofMap::iterator it = dualDofMap[component].getMap().begin();
	 it != dualDofMap[component].getMap().end(); ++it) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
583
      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
584
	localDofMap[component].insertRankDof(it->first);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
585
      } else {
586
	if (dofMapSd[feSpace].isRankDof(it->first))
Thomas Witkowski's avatar
Thomas Witkowski committed
587
	  localDofMap[component].insertRankDof(it->first);
588
	else 
Thomas Witkowski's avatar
Thomas Witkowski committed
589
	  localDofMap[component].insertNonRankDof(it->first);
590
      }
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
591
    }
592 593 594
  }


595
  void PetscSolverFeti::createMatLagrange()
596 597 598
  {
    FUNCNAME("PetscSolverFeti::createMatLagrange()");

Thomas Witkowski's avatar
Thomas Witkowski committed
599
    double wtime = MPI::Wtime();
600
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Thomas Witkowski committed
601

602 603
    // === Create distributed matrix for Lagrange constraints. ===

604 605 606 607 608
    MatCreateAIJ(mpiCommGlobal,
		 lagrangeMap.getRankDofs(), localDofMap.getRankDofs(),
		 lagrangeMap.getOverallDofs(), nGlobalOverallInterior,
		 2, PETSC_NULL, 2, PETSC_NULL,
		 &mat_lagrange);
609
    MatSetOption(mat_lagrange, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
610

611 612 613
    Vec vec_scale_lagrange;
    lagrangeMap.createVec(vec_scale_lagrange);

614 615 616 617 618 619 620
    // === 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.                                                     ===

621
    for (unsigned int component = 0; component < componentSpaces.size(); component++) {
622
      DofMap &dualMap = dualDofMap[component].getMap();
623

624
      for (DofMap::iterator it = dualMap.begin(); it != dualMap.end(); ++it) {
625
	TEST_EXIT_DBG(boundaryDofRanks[componentSpaces[component]].count(it->first))
Thomas Witkowski's avatar
Thomas Witkowski committed
626 627 628
	  ("Should not happen!\n");
	
	// Global index of the first Lagrange constriant for this node.
629
	int index = lagrangeMap.getMatIndex(component, it->first);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
630

Thomas Witkowski's avatar
Thomas Witkowski committed
631
	// Copy set of all ranks that contain this dual node.
632 633
	vector<int> W(boundaryDofRanks[componentSpaces[component]][it->first].begin(), 
		      boundaryDofRanks[componentSpaces[component]][it->first].end());
Thomas Witkowski's avatar
Thomas Witkowski committed
634 635
	// Number of ranks that contain this dual node.
	int degree = W.size();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
636 637

	TEST_EXIT_DBG(degree > 1)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
638
	
639
	int counter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
640 641 642
	for (int i = 0; i < degree; i++) {
	  for (int j = i + 1; j < degree; j++) {
	    if (W[i] == mpiRank || W[j] == mpiRank) {
643 644
	      MatSetValue(mat_lagrange, 
			  index + counter, 
645
			  localDofMap.getMatIndex(component, it->first) + rStartInterior,
646 647
			  (W[i] == mpiRank ? 1.0 : -1.0),
			  INSERT_VALUES);
648
	    }
649 650 651 652 653 654 655
	    counter++;
	  }
	}

	// === Create scaling factors for scaling the lagrange matrix, which ===
	// === is required for FETI-DP preconditioners.                      ===
	
656
	if (dofMap[componentSpaces[component]].isRankDof(it->first)) {
657 658 659 660 661 662
	  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);
663 664 665 666 667 668 669
	  }
	}
      }
    }

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

671

Thomas Witkowski's avatar
So on  
Thomas Witkowski committed
672 673 674 675 676 677 678 679 680 681 682 683
    int nZeroRows = PetscSolverFetiDebug::testZeroRows(mat_lagrange);
    int m,n;
    MatGetSize(mat_lagrange, &m ,&n);
    MSG("Lagrange matrix has %d zero rows and global size of %d %d!\n", nZeroRows, m, n);

    PetscViewer petscView;
    PetscViewerBinaryOpen(PETSC_COMM_WORLD, "lagrange.mat", 
			  FILE_MODE_WRITE, &petscView);
    MatView(mat_lagrange, petscView);
    PetscViewerDestroy(&petscView);


684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
    // === 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
699 700
    if (printTimings) {
      MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
701 702
      MSG("FETI-DP timing 05: %.5f seconds (creation of lagrange constraint matrix)\n", 
	  MPI::Wtime() - wtime);
Thomas Witkowski's avatar
BLUB  
Thomas Witkowski committed
703
    }
704 705
  }

706

Thomas Witkowski's avatar
Thomas Witkowski committed
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
  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);
  }
723

724

725
  void PetscSolverFeti::createMatAugmentedLagrange()
726 727 728 729 730 731 732 733 734 735
  {
    FUNCNAME("PetscSolverFeti::createMatAugmentedLagrange()");

    if (!augmentedLagrange)
      return;

    double wtime = MPI::Wtime();

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

	DofContainer edgeDofs;
	it->rankObj.el->getAllDofs(feSpaces[0], it->rankObj, edgeDofs);
746
	
747 748
	for (DofContainer::iterator dit = edgeDofs.begin();
	     dit != edgeDofs.end(); ++dit) {
749
	  if (dirichletRows[0].count(**dit) == 0) {
750 751 752 753 754 755 756 757 758
	    dirichletOnlyEdge = false;
	    break;
	  }
	}

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

    nRankEdges = allEdges.size();    
761 762 763 764
    int rStartEdges = 0;
    mpi::getDofNumbering(mpiCommGlobal, nRankEdges, rStartEdges, nOverallEdges);

    MSG("nRankEdges = %d, nOverallEdges = %d\n", nRankEdges, nOverallEdges);
765
    
766 767 768
    nRankEdges *= componentSpaces.size();
    rStartEdges *= componentSpaces.size();
    nOverallEdges *= componentSpaces.size();
769 770

    MatCreateAIJ(mpiCommGlobal,
771 772
		 nRankEdges, lagrangeMap.getRankDofs(),
		 nOverallEdges, lagrangeMap.getOverallDofs(),
773
		 2, PETSC_NULL, 2, PETSC_NULL, 
774 775 776
		 &mat_augmented_lagrange);
    MatSetOption(mat_augmented_lagrange, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

777
    int rowCounter = rStartEdges;
Thomas Witkowski's avatar
Thomas Witkowski committed
778 779
    for (std::set<BoundaryObject>::iterator edgeIt = allEdges.begin(); 
	 edgeIt != allEdges.end(); ++edgeIt) {
780
      for (int component = 0; component < componentSpaces.size(); component++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
781
	DofContainer edgeDofs;
782
	edgeIt->el->getAllDofs(componentSpaces[component], *edgeIt, edgeDofs);