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

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

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

    Parameters::get("parallel->multi level test", multiLevelTest);
75 76
    if (multiLevelTest)
      meshLevel = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
77 78

    Parameters::get("parallel->print timings", printTimings);
79 80

    Parameters::get("parallel->feti->augmented lagrange", augmentedLagrange);
81 82

    Parameters::get("parallel->feti->symmetric", isSymmetric);
83 84 85
  }


86
  void PetscSolverFeti::initialize(vector<const FiniteElemSpace*> feSpaces)
87
  {
88 89
    FUNCNAME("PetscSolverFeti::initialize()");

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

93
    MeshLevelData& levelData = meshDistributor->getMeshLevelData();
94
    vector<const FiniteElemSpace*>& uniqueFe = meshDistributor->getFeSpaces();
95

Thomas Witkowski's avatar
Thomas Witkowski committed
96

Thomas Witkowski's avatar
Thomas Witkowski committed
97 98 99 100 101 102 103 104
    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
105 106
    }
			   
Thomas Witkowski's avatar
Thomas Witkowski committed
107 108
    if (subdomain == NULL) {
      subdomain = new PetscSolverGlobalMatrix();
109
      subdomain->setSymmetric(isSymmetric);
110
      subdomain->setHandleDirichletRows(false);
111

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

123 124 125 126
    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
127

Thomas Witkowski's avatar
Thomas Witkowski committed
128
    if (stokesMode)
Thomas Witkowski's avatar
Thomas Witkowski committed
129
      interfaceDofMap.init(levelData, feSpaces, uniqueFe);
Thomas Witkowski's avatar
Thomas Witkowski committed
130

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

135
      interiorDofMap.init(levelData, feSpaces, uniqueFe, false);
136
    }
137 138 139
  }


Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
140 141 142 143 144 145 146 147 148 149 150 151 152
  void PetscSolverFeti::createDirichletData(Matrix<DOFMatrix*> &mat)
  {
    FUNCNAME("PetscSolverFeti::createDirichletData()");

    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)) {
153 154 155 156 157 158 159 160 161
	// === 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
162 163 164 165 166 167 168
      } else {
	dirichletRows[feSpace] = dRows;
      }
    }
  }


169 170 171 172
  void PetscSolverFeti::createFetiData()
  {
    FUNCNAME("PetscSolverFeti::createFetiData()");

Thomas Witkowski's avatar
Thomas Witkowski committed
173 174
    double timeCounter = MPI::Wtime();

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

179 180
    MeshLevelData& levelData = meshDistributor->getMeshLevelData();

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

188 189
    primalDofMap.setDofComm(meshDistributor->getDofComm());
    lagrangeMap.setDofComm(meshDistributor->getDofComm());
190

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
209 210
    for (unsigned int i = 0; i < meshDistributor->getComponentFeSpaces().size(); i++) {
      const FiniteElemSpace *feSpace = meshDistributor->getComponentFeSpace(i);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
211
    
Thomas Witkowski's avatar
Thomas Witkowski committed
212
      createPrimals(i, feSpace);  
213

Thomas Witkowski's avatar
Thomas Witkowski committed
214
      createDuals(i, feSpace);
215

Thomas Witkowski's avatar
Thomas Witkowski committed
216
      createInterfaceNodes(i, feSpace);
217

Thomas Witkowski's avatar
Thomas Witkowski committed
218
      createIndexB(i, feSpace);     
219
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
220 221 222 223

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

Thomas Witkowski's avatar
Thomas Witkowski committed
227
    if (stokesMode)
228 229
      interfaceDofMap.update();

Thomas Witkowski's avatar
Thomas Witkowski committed
230 231 232 233
    for (unsigned int i = 0; i < meshDistributor->getComponentFeSpaces().size(); i++) {
      const FiniteElemSpace *feSpace = meshDistributor->getComponentFeSpace(i);
      createLagrange(i, feSpace);
      createAugmentedLagrange(i, feSpace);
234
    }
235

236 237 238
    lagrangeMap.update();


239 240 241 242 243 244 245 246 247 248 249 250
    // === ===

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

251
      mpi::getDofNumbering(mpiCommGlobal, groupRowsInterior,
252 253 254 255 256 257 258 259 260
			   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);
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
266
      if (feSpace == pressureFeSpace) {
Thomas Witkowski's avatar
Thomas Witkowski committed
267 268 269 270
	MSG("  nRankInterface = %d  nOverallInterface = %d\n",
	    interfaceDofMap[feSpace].nRankDofs, 
	    interfaceDofMap[feSpace].nOverallDofs);
      } else {
271
	MSG("  nRankPrimals = %d   nLocalPrimals = %d  nOverallPrimals = %d\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
272
	    primalDofMap[feSpace].nRankDofs, 
273
	    primalDofMap[feSpace].nLocalDofs,
Thomas Witkowski's avatar
Thomas Witkowski committed
274 275 276 277 278 279 280 281 282 283
	    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
284 285 286
// 	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
287
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
288
    }
289

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

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


Thomas Witkowski's avatar
Thomas Witkowski committed
304 305
  void PetscSolverFeti::createPrimals(int component,
				      const FiniteElemSpace *feSpace)
Thomas Witkowski's avatar
Thomas Witkowski committed
306
  {
307
    FUNCNAME("PetscSolverFeti::createPrimals()");  
308

Thomas Witkowski's avatar
Thomas Witkowski committed
309
    if (feSpace == pressureFeSpace)
310 311
      return;

312 313 314
    // === Define all vertices on the interior boundaries of the macro mesh ===
    // === to be primal variables.                                          ===

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

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

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

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
333 334 335 336 337 338 339
	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);
	}
      }
340
    }
341

342 343 344 345

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

346
    for (DofIndexSet::iterator it = primals.begin(); it != primals.end(); ++it)
347
      if (meshDistributor->getDofMap()[feSpace].isRankDof(*it)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
348
	primalDofMap[component].insertRankDof(*it);
349
      } else
Thomas Witkowski's avatar
Thomas Witkowski committed
350
  	primalDofMap[component].insertNonRankDof(*it);
351 352 353
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
354 355
  void PetscSolverFeti::createDuals(int component,
				    const FiniteElemSpace *feSpace)
356 357
  {
    FUNCNAME("PetscSolverFeti::createDuals()");
358

Thomas Witkowski's avatar
Thomas Witkowski committed
359
    if (feSpace == pressureFeSpace)
360 361
      return;

362 363 364
    // === Create global index of the dual nodes on each rank. ===

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

Thomas Witkowski's avatar
Thomas Witkowski committed
372
      if (isPrimal(component, **it))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
373 374 375
	continue;

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

  
Thomas Witkowski's avatar
Thomas Witkowski committed
385 386
  void PetscSolverFeti::createInterfaceNodes(int component,
					     const FiniteElemSpace *feSpace)
387 388 389
  {
    FUNCNAME("PetscSolverFeti::createInterfaceNodes()");

Thomas Witkowski's avatar
Thomas Witkowski committed
390
    if (feSpace != pressureFeSpace)
391 392 393 394 395 396
      return;

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

    for (DofContainer::iterator it = allBoundaryDofs.begin();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
397 398 399 400
	 it != allBoundaryDofs.end(); ++it) {
      if (dirichletRows[feSpace].count(**it))
	continue;      
      
401
      if (meshDistributor->getDofMap()[feSpace].isRankDof(**it))
Thomas Witkowski's avatar
Thomas Witkowski committed
402
	interfaceDofMap[component].insertRankDof(**it);
403
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
404
	interfaceDofMap[component].insertNonRankDof(**it);      
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
405
    }
406 407 408
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
409 410
  void PetscSolverFeti::createLagrange(int component,
				       const FiniteElemSpace *feSpace)
411 412 413
  {
    FUNCNAME("PetscSolverFeti::createLagrange()");

Thomas Witkowski's avatar
Thomas Witkowski committed
414
    if (feSpace == pressureFeSpace)
415 416
      return;

417 418
    boundaryDofRanks[feSpace].clear();

Thomas Witkowski's avatar
Thomas Witkowski committed
419 420 421
    // 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
422
    map<int, std::set<DegreeOfFreedom> > sdRankDofs;
423

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
424
    if (meshLevel > 0) {
425
      StdMpi<vector<int> > stdMpi(mpiCommGlobal);
426 427 428 429 430 431 432 433 434

      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
435
	  if (meshDistributor->getDofMapSd()[feSpace].isRankDof(it.getDofIndex()))
436
	    subdomainRankDofs.push_back(1);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
437
	  else
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	    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
453 454
	   !it.end(); it.nextRank())
	for (; !it.endDofIter(); it.nextDof())
Thomas Witkowski's avatar
Thomas Witkowski committed
455
	  if (!isPrimal(component, it.getDofIndex()))
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
456 457 458
	    if (stdMpi.getRecvData(it.getRank())[it.getDofCounter()] == 1)
	      sdRankDofs[it.getRank()].insert(it.getDofIndex());
    }
459

Thomas Witkowski's avatar
Thomas Witkowski committed
460
    if (dualDofMap[component].nLocalDofs == 0)
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
461 462 463 464 465 466
      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)).         ===

467
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
468 469 470 471
    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
472
	if (!isPrimal(component, it.getDofIndex())) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
473 474
	  boundaryDofRanks[feSpace][it.getDofIndex()].insert(mpiRank);

475
 	  if (meshLevel == 0 ||
Thomas Witkowski's avatar
Thomas Witkowski committed
476
 	      (meshLevel > 0 && sdRankDofs[it.getRank()].count(it.getDofIndex())))
477
	    boundaryDofRanks[feSpace][it.getDofIndex()].insert(it.getRank());	  
478 479 480 481
	}
      }
    }

482 483 484 485

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

486
    StdMpi<vector<std::set<int> > > stdMpi(meshDistributor->getMpiComm());
487

488
    for (DofComm::Iterator it(meshDistributor->getDofComm().getSendDofs(), meshLevel, feSpace);
489 490
	 !it.end(); it.nextRank())
      for (; !it.endDofIter(); it.nextDof())
Thomas Witkowski's avatar
Thomas Witkowski committed
491
	if (!isPrimal(component, it.getDofIndex()))
492 493 494
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && sdRankDofs[it.getRank()].count(it.getDofIndex())))
	    stdMpi.getSendData(it.getRank()).push_back(boundaryDofRanks[feSpace][it.getDofIndex()]);
495 496 497

    stdMpi.updateSendDataSize();

498
    for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), meshLevel, feSpace); 
499
	 !it.end(); it.nextRank()) {
500
      bool recvFromRank = false;
501
      for (; !it.endDofIter(); it.nextDof()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
502
	if (!isPrimal(component, it.getDofIndex())) {
503 504 505
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && 
 	       meshDistributor->getDofMapSd()[feSpace].isRankDof(it.getDofIndex()))) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
506 507 508
	    recvFromRank = true;
	    break;
	  }
509
	}
510
      }
511 512

      if (recvFromRank)
513
	stdMpi.recv(it.getRank());
514
    }
515

516 517
    stdMpi.startCommunication();

518
    for (DofComm::Iterator it(meshDistributor->getDofComm().getRecvDofs(), meshLevel, feSpace); 
519
	 !it.end(); it.nextRank()) {
520
      int i = 0;
521
      for (; !it.endDofIter(); it.nextDof())
Thomas Witkowski's avatar
Thomas Witkowski committed
522
	if (!isPrimal(component, it.getDofIndex()))
523 524 525
 	  if (meshLevel == 0 ||
 	      (meshLevel > 0 && 
 	       meshDistributor->getDofMapSd()[feSpace].isRankDof(it.getDofIndex())))	    
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
526 527
	    boundaryDofRanks[feSpace][it.getDofIndex()] = 
	      stdMpi.getRecvData(it.getRank())[i++];
528
	  else
Thomas Witkowski's avatar
Thomas Witkowski committed
529
	    lagrangeMap[component].insertNonRankDof(it.getDofIndex());
530 531
    }

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
532

533 534 535
    // === Reserve for each dual node, on the rank that owns this node, the ===
    // === appropriate number of Lagrange constraints.                      ===

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


Thomas Witkowski's avatar
Thomas Witkowski committed
551 552
  void PetscSolverFeti::createAugmentedLagrange(int component,
						const FiniteElemSpace *feSpace)
553 554 555 556 557 558 559 560
  {
    FUNCNAME("PetscSolverFeti::createAugmentedLagrange()");

    if (!augmentedLagrange)
      return;
  }


Thomas Witkowski's avatar
Thomas Witkowski committed
561 562
  void PetscSolverFeti::createIndexB(int component,
				     const FiniteElemSpace *feSpace)
563
  {
564
    FUNCNAME("PetscSolverFeti::createIndexB()");
565

566
    DOFAdmin* admin = feSpace->getAdmin();
567 568 569 570

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

572
    int nLocalInterior = 0;    
573
    for (int i = 0; i < admin->getUsedSize(); i++) {
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
574
      if (admin->isDofFree(i) ||
Thomas Witkowski's avatar
Thomas Witkowski committed
575 576 577
	  isPrimal(component, i) ||
	  isDual(component, i) ||
	  isInterface(component, i) ||
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
578 579
	  dirichletRows[feSpace].count(i))
	continue;      
580

Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
581
      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
582
	localDofMap[component].insertRankDof(i, nLocalInterior);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
583 584
	
	if (fetiPreconditioner == FETI_DIRICHLET)
585
	  interiorDofMap[component].insertRankDof(i, nLocalInterior);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
586 587 588 589
	
	nLocalInterior++;	
      } else {
	if (meshDistributor->getDofMapSd()[feSpace].isRankDof(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
590
	  localDofMap[component].insertRankDof(i);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
591
	else
Thomas Witkowski's avatar
Thomas Witkowski committed
592
	  localDofMap[component].insertNonRankDof(i);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
593 594 595
	
	TEST_EXIT_DBG(fetiPreconditioner == FETI_NONE)
	  ("Not yet implemnted!\n");	
596
      }
597
    }
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
598
    
599 600
    // === And finally, add the global indicies of all dual nodes. ===

601
    for (DofMap::iterator it = dualDofMap[feSpace].getMap().begin();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
602 603
	 it != dualDofMap[feSpace].getMap().end(); ++it) {
      if (meshLevel == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
604
	localDofMap[component].insertRankDof(it->first);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
605
      } else {
606
	if (meshDistributor->getDofMapSd()[feSpace].isRankDof(it->first))
Thomas Witkowski's avatar
Thomas Witkowski committed
607
	  localDofMap[component].insertRankDof(it->first);
608
	else 
Thomas Witkowski's avatar
Thomas Witkowski committed
609
	  localDofMap[component].insertNonRankDof(it->first);
610
      }
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
611
    }
612 613 614
  }


615
  void PetscSolverFeti::createMatLagrange(vector<const FiniteElemSpace*> &feSpaces)
616 617 618
  {
    FUNCNAME("PetscSolverFeti::createMatLagrange()");

Thomas Witkowski's avatar
Thomas Witkowski committed
619
    double wtime = MPI::Wtime();
620
    int mpiRank = meshDistributor->getMpiRank();
Thomas Witkowski's avatar
Thomas Witkowski committed
621

622 623
    // === Create distributed matrix for Lagrange constraints. ===

624 625 626 627 628
    MatCreateAIJ(mpiCommGlobal,
		 lagrangeMap.getRankDofs(), localDofMap.getRankDofs(),
		 lagrangeMap.getOverallDofs(), nGlobalOverallInterior,
		 2, PETSC_NULL, 2, PETSC_NULL,
		 &mat_lagrange);
629
    MatSetOption(mat_lagrange, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
630

631 632 633
    Vec vec_scale_lagrange;
    lagrangeMap.createVec(vec_scale_lagrange);

634 635 636 637 638 639 640
    // === 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
641
    for (unsigned int k = 0; k < feSpaces.size(); k++) {
642
      DofMap &dualMap = dualDofMap[feSpaces[k]].getMap();
643

644
      for (DofMap::iterator it = dualMap.begin(); it != dualMap.end(); ++it) {
645
	TEST_EXIT_DBG(boundaryDofRanks[feSpaces[k]].count(it->first))
Thomas Witkowski's avatar
Thomas Witkowski committed
646 647 648
	  ("Should not happen!\n");
	
	// Global index of the first Lagrange constriant for this node.
649
	int index = lagrangeMap.getMatIndex(k, it->first);
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
650

Thomas Witkowski's avatar
Thomas Witkowski committed
651
	// Copy set of all ranks that contain this dual node.
652 653
	vector<int> W(boundaryDofRanks[feSpaces[k]][it->first].begin(), 
		      boundaryDofRanks[feSpaces[k]][it->first].end());
Thomas Witkowski's avatar
Thomas Witkowski committed
654 655
	// Number of ranks that contain this dual node.
	int degree = W.size();
Thomas Witkowski's avatar
Blub  
Thomas Witkowski committed
656 657

	TEST_EXIT_DBG(degree > 1)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
658
	
659
	int counter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
660 661 662
	for (int i = 0; i < degree; i++) {
	  for (int j = i + 1; j < degree; j++) {
	    if (W[i] == mpiRank || W[j] == mpiRank) {
663 664 665 666 667
	      MatSetValue(mat_lagrange, 
			  index + counter, 
			  localDofMap.getMatIndex(k, it->first) + rStartInterior,
			  (W[i] == mpiRank ? 1.0 : -1.0),
			  INSERT_VALUES);
668
	    }
669 670 671 672 673 674 675 676 677 678 679 680 681 682
	    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);
683 684 685 686 687 688 689
	  }
	}
      }
    }

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

691

Thomas Witkowski's avatar
So on  
Thomas Witkowski committed
692 693 694 695 696 697 698 699 700 701 702 703
    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);


704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    // === 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
719 720
    if (printTimings) {
      MPI::COMM_WORLD.Barrier();
Thomas Witkowski's avatar
Thomas Witkowski committed
721 722
      MSG("FETI-DP timing 05: %.5f seconds (creation of lagrange constraint matrix)\n", 
	  MPI::Wtime() - wtime);
Thomas Witkowski's avatar
BLUB  
Thomas Witkowski committed
723
    }
724 725
  }

726

Thomas Witkowski's avatar
Thomas Witkowski committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
  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);
  }
743

744

745 746 747 748 749 750 751 752 753 754 755
  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
756
    std::set<BoundaryObject> allEdges;
757
    for (InteriorBoundary::iterator it(intBound.getOwn()); !it.end(); ++it) {
758 759 760
      if ((it->rankObj.subObj == FACE ||
	   (it->rankObj.subObj == EDGE && 
	    testWirebasketEdge(it->rankObj, feSpaces[0]))) && 
761 762 763 764 765
	  allEdges.count(it->rankObj) == 0) {
	bool dirichletOnlyEdge = true;

	DofContainer edgeDofs;
	it->rankObj.el->getAllDofs(feSpaces[0], it->rankObj, edgeDofs);
766
	
767 768 769 770 771 772 773 774 775 776 777 778
	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
779 780

    nRankEdges = allEdges.size();    
781 782 783 784
    int rStartEdges = 0;
    mpi::getDofNumbering(mpiCommGlobal, nRankEdges, rStartEdges, nOverallEdges);

    MSG("nRankEdges = %d, nOverallEdges = %d\n", nRankEdges, nOverallEdges);
785
    
786 787 788 789 790
    nRankEdges *= feSpaces.size();
    rStartEdges *= feSpaces.size();
    nOverallEdges *= feSpaces.size();

    MatCreateAIJ(mpiCommGlobal,
791 792
		 nRankEdges, lagrangeMap.getRankDofs(),
		 nOverallEdges, lagrangeMap.getOverallDofs(),
793
		 2, PETSC_NULL, 2, PETSC_NULL, 
794 795 796
		 &mat_augmented_lagrange);
    MatSetOption(mat_augmented_lagrange, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);

797
    int rowCounter = rStartEdges;
Thomas Witkowski's avatar
Thomas Witkowski committed
798 799
    for (std::set<BoundaryObject>::iterator edgeIt = allEdges.begin(); 
	 edgeIt != allEdges.end(); ++edgeIt) {
Thomas Witkowski's avatar
Thomas Witkowski committed
800
      for (int component = 0; component < feSpaces.size(); component++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
801
	DofContainer edgeDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
802
	edgeIt->el->getAllDofs(feSpaces[component], *edgeIt, edgeDofs);
Thomas Witkowski's avatar
Thomas Witkowski committed
803 804 805
	
	for (DofContainer::iterator it = edgeDofs.begin();
	     it != edgeDofs.end(); ++it) {
Thomas Witkowski's avatar
Thomas Witkowski committed
806
	  TEST_EXIT_DBG(isPrimal(component, **it) == false)
Thomas Witkowski's avatar
Thomas Witkowski committed
807 808
	    ("Should not be primal!\n");
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
809
	  if (dirichletRows[feSpaces[component]].count(**it))
Thomas Witkowski's avatar
Thomas Witkowski committed
810 811
	    continue;