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

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

70 71 72 73 74 75 76 77
    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
78 79 80
    TEST_EXIT(schurPrimalSolver == 0 || schurPrimalSolver == 1)
      ("Wrong solver \"%d\"for the Schur primal complement!\n", 
       schurPrimalSolver);
81

82 83 84 85 86 87 88 89 90 91 92
    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);

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

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


101
  void PetscSolverFeti::initialize()
102
  {
103 104
    FUNCNAME("PetscSolverFeti::initialize()");

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

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

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

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

126 127 128 129
    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
130

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

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

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


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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

216 217 218
    lagrangeMap.update();


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

320 321 322 323

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

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


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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

446
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
447 448 449 450
    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
451
	if (!isPrimal(component, it.getDofIndex())) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
452 453
	  boundaryDofRanks[feSpace][it.getDofIndex()].insert(mpiRank);

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

461 462 463 464

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

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

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

    stdMpi.updateSendDataSize();

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

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

495 496
    stdMpi.startCommunication();

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

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
511

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

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


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

    if (!augmentedLagrange)
      return;
  }


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

543 544

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

670

Thomas Witkowski's avatar
So on  
Thomas Witkowski committed
671 672 673 674 675 676 677 678 679 680 681 682
    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);


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
  void PetscSolverFeti::createMatAugmentedLagrange()
725 726 727 728 729 730 731 732 733 734
  {
    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) {
737 738 739
      if ((it->rankObj.subObj == FACE ||
	   (it->rankObj.subObj == EDGE && 
	    testWirebasketEdge(it->rankObj, feSpaces[0]))) && 
740 741 742 743 744
	  allEdges.count(it->rankObj) == 0) {
	bool dirichletOnlyEdge = true;

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

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

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

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

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

776
    in