CouplingProblemStat.h 8.77 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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// ============================================================================
// ==                                                                        ==
// == 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 CouplingProblemStat.h */

#ifndef AMDIS_COUPLING_PROBLEM_STAT_H
#define AMDIS_COUPLING_PROBLEM_STAT_H

#include <vector>
#include <set>
#include <list>
#include "AMDiS_fwd.h"
#include "ProblemStat.h"
#include "Initfile.h"
#include <boost/lexical_cast.hpp>

namespace AMDiS {

  using namespace std;


  /** \brief
   * This class defines a coupled stationary problem definition in sequential
   * computations. 
   */
43
44
  template<typename ProblemStatType>
  class CouplingProblemStatImpl
45
46
47
  {
  public:
    /// Constructor
48
    CouplingProblemStatImpl(std::string name_)
49
50
51
52
53
54
55
56
      : name(name_),
        nComponents(0),
        nMeshes(0),
        refinementManager(NULL),
        coarseningManager(NULL)
    {}

    /// Destructor
57
    virtual ~CouplingProblemStatImpl() {}
58
59

    /// add problem by number
60
    virtual void addProblem(ProblemStatType* prob)
61
62
    {
      problems.push_back(prob);
Praetorius, Simon's avatar
Praetorius, Simon committed
63
      nComponents += prob->getNumComponents();
64
65
66
67
    };

    /// Initialisation of the problem.
    virtual void initialize(Flag initFlag,
68
                            CouplingProblemStatImpl<ProblemStatType> *adoptProblem = NULL,
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
                            Flag adoptFlag = INIT_NOTHING)
    {

      // create one refinement-/coarseningmanager for all problems
      if (refinementManager != NULL && coarseningManager != NULL) { 
        WARNING("refinement-/coarseningmanager already created\n");
      } else {
        if (!adoptProblem)
          createRefCoarseManager();
        else {
          refinementManager = adoptProblem->refinementManager;
          coarseningManager = adoptProblem->coarseningManager;
        }
      }
  
      if (refinementManager == NULL || coarseningManager == NULL) 
        WARNING("no refinement-/coarseningmanager created\n");

      // create Meshes and FeSpaces


      // all problems must have the same dimension (?)
      int dim = 0;
      Parameters::get(name + "->dim", dim);
      TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
                    name.c_str());

96
97
98
      std::map<std::string, Mesh*> meshByName;
      std::vector< std::set<Mesh*> > meshesForProblems(problems.size());
      std::map<std::pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
99
100
101
102
      for (size_t i = 0; i < problems.size(); ++i) {
        TEST_EXIT(problems[i])("problem[%d] does not exist!\n",i);
        for (size_t j = 0; j < problems[i]->getNumComponents(); j++) {
          // mesh
103
104
105
106
107
108
109

	  string meshName("");
	  Parameters::get(problems[i]->getName() + "->mesh", meshName);
	  TEST_EXIT(meshName != "")("No mesh name specified for \"%s->mesh\"!\n",
				    problems[i]->getName().c_str());

          if (meshByName.find(meshName) == meshByName.end()) {
110
            Mesh *newMesh = new Mesh(meshName, dim);
111
            meshByName[meshName] = newMesh;
112
113
114
115
            meshes.push_back(newMesh);
            meshesForProblems[i].insert(newMesh);
            nMeshes++;
          } else
116
            meshesForProblems[i].insert(meshByName[meshName]);
117

118
          problems[i]->setComponentMesh(j, meshByName[meshName]);
119
120
121
122
123
            
          // feSpace
          int degree = 1;
          Parameters::get(problems[i]->getName() + "->polynomial degree[" + 
                          boost::lexical_cast<string>(j) + "]", degree);
124

125
          if (feSpaceMap[pair<Mesh*, int>(meshByName[meshName], degree)] == NULL) {
126
127
128
129
130
            stringstream s;
            s << problems[i]->getName() << "->feSpace[" << j << "]";
    
            FiniteElemSpace *newFeSpace = 
              FiniteElemSpace::provideFeSpace(NULL, Lagrange::getLagrange(dim, degree),
131
132
                                              meshByName[meshName], s.str());
            feSpaceMap[pair<Mesh*, int>(meshByName[meshName], degree)] = newFeSpace;
133
134
135
            feSpaces.push_back(newFeSpace);
          }
    
136
137
//           problems[i]->setComponentSpace(j, feSpaceMap[pair<Mesh*, int>(meshByName[meshName], degree)]);

138
139
140
        }
      }

141
      for (size_t i = 0; i < problems.size(); i++) {
142
143
144
145
        vector<Mesh*> problemMeshes(meshesForProblems[i].begin(), meshesForProblems[i].end());
        problems[i]->setMeshes(problemMeshes);
        problems[i]->setRefinementManager(refinementManager);
        problems[i]->setCoarseningManager(coarseningManager);
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
        problems[i]->initialize(INIT_ALL - INIT_MESH);
      }

      for (size_t i = 0; i < meshes.size(); i++) {
	int globalRefinements = 0;

	// If AMDiS is compiled for parallel computations, the global refinements are
	// ignored here. Later, each rank will add the global refinements to its
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
	Parameters::get(meshes[i]->getName() + "->global refinements",
			globalRefinements);
#endif

	bool initMesh = initFlag.isSet(INIT_MESH);

	// Initialize the meshes if there is no serialization file.
	if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
	  meshes[i]->initialize();

	// do global refinements
	if (initMesh && meshes[i])
	  refinementManager->globalRefine(meshes[i], globalRefinements);
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
      }
    }

    void createRefCoarseManager() 
    {
      FUNCNAME("ProblemStat::createRefCoarseManager()");
  
      int dim = 0;
      Parameters::get(name + "->dim", dim);
      TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
                    name.c_str());

      switch (dim) {
      case 1:
        coarseningManager = new CoarseningManager1d();
        refinementManager = new RefinementManager1d();
        break;
      case 2:
        coarseningManager = new CoarseningManager2d();
        refinementManager = new RefinementManager2d();
        break;
      case 3:
        coarseningManager = new CoarseningManager3d();
        refinementManager = new RefinementManager3d();
        break;
      default:
        ERROR_EXIT("invalid dim!\n");
      }
    }

    /// Returns number of managed problems
    virtual int getNumProblems() 
    { 
      return problems.size(); 
    }

    /// Implementation of ProblemStatBase::getNumComponents()
    virtual int getNumComponents() 
    { 
      return nComponents; 
    }

    /** \brief
     * Returns the problem with the given number. If only one problem
     * is managed by this master problem, the number hasn't to be given.
     */
215
    virtual ProblemStatType *getProblem(int number = 0)
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    { 
      return problems[number]; 
    }

    /// Returns \ref meshes[i]
    inline Mesh* getMesh(int number = 0) 
    {
      return meshes[number]; 
    }

    /// Returns \ref meshes
    inline vector<Mesh*> getMeshes() 
    {
      return meshes; 
    }

    /// Returns \ref refinementManager.
    inline RefinementManager* getRefinementManager(int comp = 0) 
    { 
      return refinementManager; 
    }

    /// Returns \ref refinementManager.
    inline CoarseningManager* getCoarseningManager(int comp = 0) 
    { 
      return coarseningManager; 
    }

    /// Returns the name of the problem
    inline virtual string getName() 
    { 
      return name; 
    }

  protected:
    
    /// Name of this problem.
    string name;

    /// Number of problem components
    int nComponents;

    /** \brief
     * Number of problem meshes. If all components are defined on the same mesh,
     * this number is 1. Otherwise, this variable is the number of different meshes
     * within the problem.
     */
    int nMeshes;

    /// FE spaces of this problem.
    vector<FiniteElemSpace*> feSpaces;

    /// Meshes of this problem.
    vector<Mesh*> meshes;

    /** \brief
     * All actions of mesh refinement are performed by refinementManager.
     * If new refinement algorithms should be realized, one has to override
     * RefinementManager and give one instance of it to AdaptStationary.
     */
    RefinementManager *refinementManager;

    /** \brief
     * All actions of mesh coarsening are performed by coarseningManager.
     * If new coarsening algorithms should be realized, one has to override
     * CoarseningManager and give one instance of it to AdaptStationary.
     */
    CoarseningManager *coarseningManager;

285
    vector<ProblemStatType*> problems;
286
  };
287
288
289


  typedef CouplingProblemStatImpl<ProblemStat> CouplingProblemStat;
290
291
292
}

#endif