ParallelDofMapping.h 21.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// 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.



/** \file FeSpaceMapping.h */

23
24
25
26
#ifndef AMDIS_FE_SPACE_MAPPING_H
#define AMDIS_FE_SPACE_MAPPING_H

#include <mpi.h>
27
#include <vector>
28
#include <map>
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
29
#include <set>
30
31
#include <petsc.h>
#include <petscis.h>
32
33
#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
34
35

#include "AMDiS_fwd.h"
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
36
#include "parallel/DofComm.h"
37
38
#include "parallel/MpiHelper.h"
#include "parallel/ParallelTypes.h"
39
#include "parallel/StdMpi.h"
40
41

namespace AMDiS {
Thomas Witkowski's avatar
Thomas Witkowski committed
42

43
44
  using namespace std;

45
  /** \brief
46
47
48
   * Is used to store matrix indices to all DOFs in rank's subdomain. Thus, the
   * class defines a mapping from component number and DOF index to a global
   * matrix index. This class does not calculate the indices by itself!
49
50
51
52
53
54
55
56
57
58
59
60
   */
  class DofToMatIndex
  {
  public:
    DofToMatIndex() {}

    /// Reset the data structure.
    inline void clear()
    {
      data.clear();
    }

61
62
63
64
65
66
67
    /** Add a new mapping for a given DOF.
     * 
     * \param[in]   component       Component number for which the mapping 
     *                              is defined.
     * \param[in]   dof             DOF index
     * \param[in]   globalMatIndex  Global matrix index.
     */
68
69
70
71
72
    inline void add(int component, DegreeOfFreedom dof, int globalMatIndex)
    {
      data[component][dof] = globalMatIndex;
    }

73
    /// Maps a global DOF index to the global matrix index for a specific 
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    /// system component number.
    inline int get(int component, DegreeOfFreedom dof)
    {
      FUNCNAME("DofToMatIndex::get()");

      TEST_EXIT_DBG(data.count(component))
	("No mapping data for component %d available!\n", component);

      TEST_EXIT_DBG(data[component].count(dof))
	("Mapping for DOF %d in component %d does not exists!\n",
	 dof, component);

      return data[component][dof];
    }

89
90
91
92
93
    /// Returns for a given matrix index the component and (local or global) DOF
    /// index. As the data structure is not made for this kind of reverse
    /// search, this is very slow and should be only used for debugging.
    void getReverse(int rowIndex, int &component, int &dofIndex);

94
95
96
  private:
    /// The mapping data. For each system component there is a specific map that
    /// maps global DOF indices to global matrix indices.
97
    map<int, boost::container::flat_map<DegreeOfFreedom, int> > data;
98
99
100
  };


101
102
103
104
105
  /**
   * This class defines the parallel mapping of DOFs for one FE space. It is used
   * by the class \ref ParallelDofMapping to specifiy the mapping for a set of 
   * FE spaces.
   */
106
  class ComponentDofMap
107
108
  {
  public:
109
110
    /// This constructor exists only to create std::map of this class and make
    /// use of the operator [] for read access. Should never be called.
111
    ComponentDofMap() 
112
113
114
    {
      ERROR_EXIT("Should not be called!\n");
    }
115
116
     
    /// This is the only valid constructur to be used. 
117
    ComponentDofMap(MeshLevelData* ld);
118
119

    /// Clears all data of the mapping.
120
    void clear();
121

122
123
    /// Maps a DOF index to both, the local and global index of the mapping. The
    /// global index must not be set.
Thomas Witkowski's avatar
Thomas Witkowski committed
124
    MultiIndex& operator[](DegreeOfFreedom d)
125
    {
126
      TEST_EXIT_DBG(dofMap.count(d))("DOF %d is not in map!\n", d);
127

128
      return dofMap[d];
129
    }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

    /** \brief
     * Searchs the map for a given DOF. It does not fail, if the DOF is not 
     * mapped by this mapping. In this case, it returns false. If the DOF is 
     * mapped, the result is stored and the function returns true.
     *
     * \param[in]    dof     DOF index for which a mapping is searched.
     * \param[out]   index   In the case that the DOF is mapped, the result
     *                       is stored here.
     */
    inline bool find(DegreeOfFreedom dof, MultiIndex& index)
    {
      DofMap::iterator it = dofMap.find(dof);
      if (it == dofMap.end())
	return false;

      index = it->second;
      return true;
    }
149
    
150
151
    /// Inserts a new DOF to rank's mapping. The DOF is assumed to be owend by
    /// the rank.
152
    void insertRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
153
    {
154
      FUNCNAME("ComponentDofMap::insertRankDof()");
155
      
156
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
157
      
158
159
      dofMap[dof0].local = dof1;
      nLocalDofs++;
Thomas Witkowski's avatar
Thomas Witkowski committed
160
      if (dof1 != -1)
161
	nRankDofs++;
162
163
    }
    
164
165
    /// Inserts a new DOF to rank's mapping. The DOF exists in rank's subdomain
    /// but is owned by a different rank, thus it is part of an interior boundary.
166
    void insertNonRankDof(DegreeOfFreedom dof0, DegreeOfFreedom dof1 = -1)
167
    {
168
      FUNCNAME("ComponentDofMap::insertNonRankDof()");
169
      
170
      TEST_EXIT_DBG(dofMap.count(dof0) == 0)("Should not happen!\n");
171
      
172
173
174
      dofMap[dof0].local = dof1;
      nLocalDofs++;
      nonRankDofs.insert(dof0);
175
176
    }
    
177
    /// Checks if a given DOF is in the DOF mapping.
178
    bool isSet(DegreeOfFreedom dof)
179
    {
180
      return static_cast<bool>(dofMap.count(dof));
181
    }
182

Thomas Witkowski's avatar
Blbu  
Thomas Witkowski committed
183
184
185
    /// Checks if a given DOF is a rank owned DOF of the DOF mapping. The DOF
    /// must be a DOF of the mapping (this is not checked here), otherwise the
    /// result is meaningsless.
186
    bool isRankDof(DegreeOfFreedom dof)
187
    {
188
      return !(static_cast<bool>(nonRankDofs.count(dof)));
189
    }
Thomas Witkowski's avatar
Blbu  
Thomas Witkowski committed
190
191
192
193
194

    bool isRankGlobalDof(int dof)
    {
      return (dof >= rStartDofs && dof < rStartDofs + nRankDofs);
    }
195
    
196
    /// Returns number of DOFs in the mapping.
197
    unsigned int size()
198
    {
199
      return dofMap.size();
200
201
    }
    
202
    /// Returns the raw data of the mapping.
203
    DofMap& getMap()
204
    {
205
      return dofMap;
206
    }
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
207

208
209
210
211
212
213
214
215
216
217
    DofMap::iterator begin()
    {
      return dofMap.begin();
    }

    DofMap::iterator end()
    {
      return dofMap.end();
    }

218
219
    /// Recomputes the mapping.
    void update();
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
220

221
    /// Sets the FE space this mapping corresponds to.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
222
223
224
225
226
    void setFeSpace(const FiniteElemSpace *fe)
    {
      feSpace = fe;
    }

227
228
    /// Informs the mapping whether the mapping will include DOFs that are not
    /// owned by the rank.
229
    void setNonLocal(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
230
    {
231
      isNonLocal = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
232
233
    }

234
    /// Informs the mapping whether a global index must be computed.
235
    void setNeedGlobalMapping(bool b)
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
236
    {
237
      needGlobalMapping = b;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
238
239
    }

240
    /// Sets the DOF communicator.
241
    void setDofComm(DofComm &dc)
Thomas Witkowski's avatar
Thomas Witkowski committed
242
    {
243
      dofComm = &dc;
Thomas Witkowski's avatar
Thomas Witkowski committed
244
245
    }

246
247
248
249
250
251
    void setMpiComm(MPI::Intracomm &m, int l)
    {
      mpiComm = m;
      meshLevel = l;
    }

252
  private:
253
    /// Computes a global mapping from the local one.
254
    void computeGlobalMapping();
255
256
257

    /// Computes the global indices of all DOFs in the mapping that are not owned
    /// by the rank.
258
    void computeNonLocalIndices();
259
260

  private:
261
    MeshLevelData *levelData;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
262

263
264
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
265

266
267
268
269
    MPI::Intracomm mpiComm;

    int meshLevel;

270
    /// The FE space this mapping belongs to. This is used only the get the
271
    /// correct DOF communicator in \ref dofComm.
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
272
273
    const FiniteElemSpace *feSpace;

274
    /// Mapping data from DOF indices to local and global indices.
275
    DofMap dofMap;
276

277
    /// Set of all DOFs that are in mapping but are not owned by the rank.
278
    boost::container::flat_set<DegreeOfFreedom> nonRankDofs;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
279

280
    /// If true, a global index mapping will be computed for all DOFs.
Thomas Witkowski's avatar
Thomas Witkowski committed
281
    bool needGlobalMapping;
Thomas Witkowski's avatar
FETI-DP  
Thomas Witkowski committed
282

283
284
285
    /// Is true if there are DOFs in at least one subdomain that are not owned
    /// by the rank. If the value is false, each rank contains only DOFs that
    /// are also owned by this rank.
286
    bool isNonLocal;
287

288
289
  public:
    /// 
290
    int nRankDofs, nLocalDofs, nOverallDofs, rStartDofs;
291
  };
292

293

294
295
296
297
  class ComponentIterator {
  public:
    virtual ComponentDofMap& operator*() = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
298
299
    virtual ComponentDofMap* operator->() = 0;

300
301
302
303
304
305
306
    virtual bool end() = 0;
    
    virtual void next() = 0;

    virtual void reset() = 0;
  };

307

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
  class ComponentDataInterface
  {
  public:
    virtual ComponentDofMap& operator[](int compNumber) = 0;

    virtual ComponentDofMap& operator[](const FiniteElemSpace *feSpace) = 0;

    virtual bool isDefinedFor(int compNumber) const = 0;

    virtual ComponentIterator& getIteratorData() = 0;
    
    virtual ComponentIterator& getIteratorComponent() = 0;

    virtual void init(vector<const FiniteElemSpace*> &f0,
		      vector<const FiniteElemSpace*> &f1,
323
324
		      bool isNonLocal,
		      MeshLevelData &levelData) = 0;
325

Thomas Witkowski's avatar
Thomas Witkowski committed
326
327
328
329
330
    vector<const FiniteElemSpace*>& getFeSpaces()
    {
      return feSpaces;
    }

331
332
333
334
335
336
337
338
339
340
341
342
343
  protected:
    /// The FE spaces for all components.
    vector<const FiniteElemSpace*> feSpaces;

    /// The set of all FE spaces. It uniquly contains all different FE spaces
    /// from \ref feSpaces.
    vector<const FiniteElemSpace*> feSpacesUnique;
  };


  class ComponentDataEqFeSpace : ComponentDataInterface
  {
  public:
344
345
346
347
348
    ComponentDataEqFeSpace()
      : iterData(this),
	iterComponent(this)
    {}

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    ComponentDofMap& operator[](int compNumber)
    {
      const FiniteElemSpace *feSpace = feSpaces[compNumber];
      return componentData.find(feSpace)->second;
    }

    ComponentDofMap& operator[](const FiniteElemSpace *feSpace)
    {
      TEST_EXIT_DBG(componentData.count(feSpace))("No data for FE space!\n");;
      
      return componentData.find(feSpace)->second;
    }

    bool isDefinedFor(int compNumber) const
    {
      const FiniteElemSpace *feSpace = feSpaces[compNumber];
      return static_cast<bool>(componentData.count(feSpace));
    }

    ComponentIterator& getIteratorData()
    {
      iterData.reset();
      return iterData;
    }
    
    ComponentIterator& getIteratorComponent()
    {
      iterComponent.reset();
      return iterComponent;
    }

    void init(vector<const FiniteElemSpace*> &f0,
	      vector<const FiniteElemSpace*> &f1,
382
383
	      bool isNonLocal,
	      MeshLevelData &levelData);
384
385


386
  protected:
387

388
389
    void addFeSpace(const FiniteElemSpace* feSpace,
		    MeshLevelData &levelData);
390
391
392

    class IteratorData : public ComponentIterator {
    public:
393
394
395
396
      IteratorData(ComponentDataEqFeSpace *d)
	: data(d)
      {}

397
398
      ComponentDofMap& operator*()
      {
399
	(*data)[*it];
400
401
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
402
403
      ComponentDofMap* operator->()
      {
404
	&((*data)[*it]);
Thomas Witkowski's avatar
Thomas Witkowski committed
405
406
      }

407
408
      bool end()
      {
409
	return (it != data->feSpacesUnique.end());
410
411
412
413
      }

      void next()
      {
414
	++it;
415
416
417
418
      }

      void reset()
      {
419
	it = data->feSpacesUnique.begin();
420
      }
421
422
423
424
425

    protected:
      ComponentDataEqFeSpace *data;

      vector<const FiniteElemSpace*>::iterator it;
426
427
428
429
    };

    class IteratorComponent : public ComponentIterator {
    public:
430
431
432
433
      IteratorComponent(ComponentDataEqFeSpace *d)
	: data(d)
      {}

434
435
      ComponentDofMap& operator*()
      {
436
	(*data)[*it];
437
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
438
439
440

      ComponentDofMap* operator->()
      {
441
	&((*data)[*it]);
Thomas Witkowski's avatar
Thomas Witkowski committed
442
443
      }

444
445
      bool end()
      {
446
	return (it != data->feSpaces.end());
447
448
449
450
      }

      void next()
      {
451
	++it;
452
453
454
455
      }

      void reset()
      {
456
	it = data->feSpaces.begin();
457
      }
458
459
460
461
462

    protected:
      ComponentDataEqFeSpace *data;

      vector<const FiniteElemSpace*>::iterator it;
463
464
465
466
467
468
469
470
    };
    

    map<const FiniteElemSpace*, ComponentDofMap> componentData;

    IteratorData iterData;

    IteratorComponent iterComponent;
471
472
473
474

    friend class IteratorData;
    
    friend class IteratorComponent;
475
476
477
478
479
480
  };


  class ComponentDataDiffFeSpace : ComponentDataInterface
  {
  public:
481
482
483
484
    ComponentDataDiffFeSpace()
      : iter(this)
    {}

485
486
487
488
489
490
491
492
493
494
495
496
497
498
    ComponentDofMap& operator[](int compNumber)
    {
      TEST_EXIT_DBG(componentData.count(compNumber))("No data for component %d!\n", compNumber);

      return componentData.find(compNumber)->second;
    }

    ComponentDofMap& operator[](const FiniteElemSpace *feSpace)
    {
      ERROR_EXIT("BLUB\n");
    }
    
    ComponentIterator& getIteratorData()
    {
499
500
      iter.reset();
      return iter;
501
502
503
504
    }
    
    ComponentIterator& getIteratorComponent()
    {
505
506
      iter.reset();
      return iter;
507
508
509
510
511
512
513
    }

    bool isDefinedFor(int compNumber) const
    {
      return (static_cast<unsigned int>(compNumber) < componentData.size());
    }

514
515
516
517
518
    void init(vector<const FiniteElemSpace*> &f0,
	      vector<const FiniteElemSpace*> &f1,
	      bool isNonLocal,
	      MeshLevelData &levelData);

519
  protected:
520
521
522
523
524
    void addComponent(unsigned int component,
		      const FiniteElemSpace* feSpace,
		      MeshLevelData &levelData);

    class Iterator : public ComponentIterator {
525
    public:
526
527
528
529
530
      Iterator(ComponentDataDiffFeSpace *d)
	: data(d),
	  componentCounter(-1)
      {}

531
532
      ComponentDofMap& operator*()
      {
533
	(*data)[componentCounter];
534
535
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
536
537
      ComponentDofMap* operator->()
      {
538
	&((*data)[componentCounter]);
Thomas Witkowski's avatar
Thomas Witkowski committed
539
540
541
      }


542
543
      bool end()
      {
544
	return (it != data->feSpaces.end());
545
546
547
548
      }

      void next()
      {
549
550
	++it;
	++componentCounter;
551

552
553
	if (it == data->feSpaces.end())
	  componentCounter = -1;
554
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
555

556
      void reset()
Thomas Witkowski's avatar
Thomas Witkowski committed
557
      {
558
559
	it = data->feSpaces.begin();
	componentCounter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
560
561
      }

562
563
    protected:
      ComponentDataDiffFeSpace *data;
564

565
      vector<const FiniteElemSpace*>::iterator it;
566

567
      int componentCounter;
568
569
570
571
    };

    map<unsigned int, ComponentDofMap> componentData;

572
    Iterator iter;
573

574
    friend class Iterator;
575
576
577
  };


578
579
580
581
582
  /**
   * Implements the mapping from sets of distributed DOF indices to local and
   * global indices. The mapping works for a given set of FE spaces. Furthermore,
   * this class may compute the matrix indices of the set of DOF indices.
   */
Thomas Witkowski's avatar
Thomas Witkowski committed
583
  class ParallelDofMapping
584
585
  {
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
586
    ParallelDofMapping() 
587
      : levelData(NULL),
588
	dofComm(NULL),
589
	isNonLocal(true),
590
	needMatIndexFromGlobal(false),
591
592
593
594
595
	nRankDofs(1),
	nLocalDofs(1),
	nOverallDofs(1),
	rStartDofs(1)
    {
596
597
598
599
      nRankDofs = -1;
      nLocalDofs = -1;
      nOverallDofs = -1;
      rStartDofs = -1;
600
    } 
601

602
603
604
605
    /** \brief Initialize the parallel DOF mapping.
     *
     * \param[in]  m                  MPI communicator.
     * \param[in]  fe                 The FE spaces of all components of the 
606
607
608
609
     *                                PDE to be solved.
     * \param[in]  uniqueFe           Unique list of FE spaces. Thus, two
     *                                arbitrary elements of this list are always
     *                                different.
610
     * \param[in]  isNonLocal         If true, at least one rank's mapping con-
611
612
     *                                taines DOFs that are not owend by the rank.
     */
613
    void init(MeshLevelData& mld,
614
	      vector<const FiniteElemSpace*> &fe,
615
	      vector<const FiniteElemSpace*> &uniqueFe,
616
617
618
619
620
621
622
623
624
625
626
627
	      bool isNonLocal = true);

    /// In the case of having only one FE space, this init function can be used.
    void init(MeshLevelData& mld,
	      const FiniteElemSpace *feSpace,
	      bool isNonLocal = true)
    {
      vector<const FiniteElemSpace*> feSpaces;
      feSpaces.push_back(feSpace);
      init(mld, feSpaces, feSpaces, isNonLocal);
    }

628
    void setMpiComm(MPI::Intracomm &m, int l);
629
630
631
    
    /// Clear all data.
    void clear();
632
633
634

    /// Set the DOF communicator objects that are required to exchange information
    /// about DOFs that are on interior boundaries.
635
    void setDofComm(DofComm &dofComm);
636

637
638
639
    /// Returns the DOF communicator.
    DofComm& getDofComm()
    {
640
641
642
      FUNCNAME("ParallelDofMapping::getDofComm()");

      TEST_EXIT_DBG(dofComm)("No DOF communicator object defined!\n");
643
644
645
646

      return *dofComm;
    }

647
648
649
    /// Changes the computation of matrix indices based of either local or
    /// global DOF indices, see \ref needMatIndexFromGlobal
    void setComputeMatIndex(bool global)
650
651
652
653
    {
      needMatIndexFromGlobal = global;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
654
655
656
657
658
    inline bool isMatIndexFromGlobal()
    {
      return needMatIndexFromGlobal;
    }

659

660
661
662
663
    /// Access the DOF mapping for a given component number.
    inline ComponentDofMap& operator[](int compNumber)
    {
      return (*data)[compNumber];
664
665
    }

666
667
    /// Access the DOF mapping for a given FE space    
    inline ComponentDofMap& operator[](const FiniteElemSpace *feSpace) 
Thomas Witkowski's avatar
Thomas Witkowski committed
668
    {
669
      return (*data)[feSpace];
Thomas Witkowski's avatar
Thomas Witkowski committed
670
671
    }

672
    inline bool isDefinedFor(int compNumber) const
Thomas Witkowski's avatar
Thomas Witkowski committed
673
    {
674
      return data->isDefinedFor(compNumber);
Thomas Witkowski's avatar
Thomas Witkowski committed
675
676
    }

677
    /// Returns the number of solution components the mapping is defined on.
Thomas Witkowski's avatar
Thomas Witkowski committed
678
679
680
681
    inline int getNumberOfComponents() const
    {      
      return static_cast<int>(data->getFeSpaces().size());
    }
682

683
    /// Returns \ref nRankDofs, thus the number of DOFs owned by the rank.
684
    inline int getRankDofs()
685
    {
686
      TEST_EXIT_DBG(nRankDofs >= 0)("Should not happen!\n");
687

688
      return nRankDofs;
689
690
    }

691
    /// Returns \ref nLocalDofs, thus the number of DOFs in ranks subdomain.
692
    inline int getLocalDofs()
Thomas Witkowski's avatar
Thomas Witkowski committed
693
    {
694
      TEST_EXIT_DBG(nLocalDofs >= 0)("Should not happen!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
695

696
      return nLocalDofs;
Thomas Witkowski's avatar
Thomas Witkowski committed
697
698
    }

699
    /// Returns \ref nOverallDofs, thus the number of all DOFs in this mapping.
700
    inline int getOverallDofs()
701
    {
702
      TEST_EXIT_DBG(nOverallDofs >= 0)("Should not happen!\n");
703

704
      return nOverallDofs;
705
706
    }

707
708
    /// Returns \ref rStartDofs, thus the smallest global index of a DOF that is
    /// owned by the rank.
709
    inline int getStartDofs()
710
    {
711
      TEST_EXIT_DBG(rStartDofs >= 0)("Should not happen!\n");
712

713
      return rStartDofs;
714
715
    }

716
    /// Update the mapping.
Thomas Witkowski's avatar
Thomas Witkowski committed
717
    void update();
718

719
720
721
    /// Update the mapping.
    void update(vector<const FiniteElemSpace*>& feSpaces);

722
723
    /// Returns the global matrix index of a given DOF for a given 
    /// component number.
724
    inline int getMatIndex(int ithComponent, DegreeOfFreedom d)
Thomas Witkowski's avatar
Thomas Witkowski committed
725
    {
726
      return dofToMatIndex.get(ithComponent, d);
Thomas Witkowski's avatar
Thomas Witkowski committed
727
728
    }

729
730
731
732
733
734
735
    /// Returns the component number and local/global DOF index for a given
    /// matrix row index. Should be used for debugging only!
    inline void getReverseMatIndex(int index, int &component, int &dofIndex)
    {
      dofToMatIndex.getReverse(index, component, dofIndex);
    }

736
737
    /// Returns the local matrix index of a given DOF for a given 
    /// component number.
738
    inline int getLocalMatIndex(int ithComponent, DegreeOfFreedom d)
739
    {
740
      return dofToMatIndex.get(ithComponent, d) - rStartDofs;
741
742
    }

743
744
745
746
747
    // inline const FiniteElemSpace* getFeSpace(int i = 0)
    // {      
    //   TEST_EXIT_DBG(i < feSpacesUnique.size())("Wrong component number!\n");
    //   return feSpacesUnique[i];
    // }
Thomas Witkowski's avatar
Thomas Witkowski committed
748

Thomas Witkowski's avatar
Thomas Witkowski committed
749
750
751
752
753
754
755
756
757
758
759
760
    // Writes all data of this object to an output stream.
    void serialize(ostream &out)
    {
      ERROR_EXIT("MUST BE IMPLEMENTED!\n");
    }

    // Reads the object data from an input stream.
    void deserialize(istream &in)
    {
      ERROR_EXIT("MUST BE IMPLEMENTED!\n");
    }

761
    /// Compute local and global matrix indices.
762
    void computeMatIndex(bool globalIndex);
763

764
765
766
767
    void createIndexSet(IS &is, 
			int firstComponent, 
			int nComponents);

768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
    /// Create a parallel distributed PETSc vector based on this mapping.
    inline void createVec(Vec &vec)
    {
      VecCreateMPI(mpiComm, getRankDofs(), getOverallDofs(), &vec);
    }

    /// Create a parallel distributed PETsc vector based on this mapping but
    /// with a different (larger) global size. This is used in multi-level 
    /// method to embed a local vector into a subdomain spaned by several
    /// ranks.
    inline void createVec(Vec &vec, int nGlobalRows)
    {
      VecCreateMPI(mpiComm, getRankDofs(), nGlobalRows, &vec);
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
783
784
785
786
787
    inline void createLocalVec(Vec &vec)
    {
      VecCreateSeq(PETSC_COMM_SELF, getRankDofs(), &vec);
    }

788
789
  protected:
    /// Compute \ref nRankDofs.
790
    int computeRankDofs();
791
792

    /// Compute \ref nLocalDofs.
793
    int computeLocalDofs();
794
795

    /// Compute \ref nOverallDofs.
796
    int computeOverallDofs();
797
798

    /// Compute \ref rStartDofs.
799
    int computeStartDofs();
800

801
  private:
802
803
804
805
    MPI::Intracomm mpiComm;

    int meshLevel;

806
    MeshLevelData *levelData;
807

808
809
    /// DOF communicator for all DOFs on interior boundaries.
    DofComm *dofComm;
810

811
812
813
    /// Is true if there are DOFs in at least one subdomain that are not owned
    /// by the rank. If the value is false, each rank contains only DOFs that
    /// are also owned by this rank.
814
    bool isNonLocal;
815
816
817
818
819

    /// If matrix indices should be computed, this variable defines if the
    /// mapping from DOF indices to matrix row indices is defined on local
    /// or global DOF indices. If true, the mapping is to specify and to use
    /// on global ones, otherwise on local DOF indices.
Thomas Witkowski's avatar
Thomas Witkowski committed
820
821
822
823
824
    /// In most scenarios the mapping stored on local DOF indices is what we
    /// want to have. Only when periodic boundary conditions are used together
    /// with some global matrix approache, the matrix indices must be stored
    /// also for DOFs that are not part of the local subdomain. Thus, the
    /// mapping will be stored on global DOF indices.
825
826
    bool needMatIndexFromGlobal;

827
828
    /// Maps from components to DOF mappings.
    ComponentDataInterface *data;
Thomas Witkowski's avatar
Thomas Witkowski committed
829

830
    /// Number of DOFs owned by rank.
831
    int nRankDofs;
832
833

    /// Number of DOFs in rank's subdomain.
834
    int nLocalDofs;
835
836

    /// Number of global DOFs (this value is thus the same on all ranks).
837
    int nOverallDofs;
838
839

    /// Smallest global index of a DOF owned by the rank.
840
    int rStartDofs;
841
842
843

    /// Mapping from global DOF indices to global matrix indices under 
    /// consideration of possibly multiple components.
844
    DofToMatIndex dofToMatIndex;
845
846
847
848
  };
}

#endif