CouplingBaseProblem2.h 6.54 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/** \file CoupledBaseProblem.h */

#ifndef COUPLED_BASE_PROBLEM_H
#define COUPLED_BASE_PROBLEM_H

#include "AMDiS.h"

// coupling structures
#include "CouplingIterationInterface.h"
#include "CouplingTimeInterface.h"
#include "CouplingProblemStat.h"
#include "BaseProblem.h"
#include "ProblemInstat.h"

#include <boost/preprocessor/repetition/enum_params.hpp>
#include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>

#include "boost/tuple/tuple.hpp"
#include "boost/static_assert.hpp"

#include "GenericLoops.h"

#define MAX_NUM_COUPLED_PROBLEMS 10

namespace AMDiS {

namespace detail {
  
  /// for generic loops this struct can be passed
  struct AddProblem {
    template<typename CouplingProblemStatType, typename BaseProblemType>
    static void call(CouplingProblemStatType& couplingProblemStat, BaseProblemType& baseProblem) {
      for (size_t j = 0; j < baseProblem.getNumProblems(); j++)
	couplingProblemStat.addProblem(baseProblem.getProblem(j));
    }
  };
    
  /// for generic loops this struct can be passed
  struct AddTimeInterface {
    template<typename CouplingProblemStatType, typename BaseProblemType>
    static void call(CouplingProblemStatType& couplingProblemStat, BaseProblemType& baseProblem) {
      couplingProblemStat.addTimeInterface(&baseProblem);
    }
  };
    
  /// for generic loops this struct can be passed
  struct AddIterationInterface {
    template<typename CouplingProblemStatType, typename BaseProblemType>
    static void call(CouplingProblemStatType& couplingProblemStat, BaseProblemType& baseProblem) {
      couplingProblemStat.addIterationInterface(&baseProblem);
    }
  };
    
  /// Functor for generic loops. Method initData() is called for each element in a sequence.
  struct InitData { template<typename B> static void call(B& b) { b.initData(); } };
    
  /// Functor for generic loops. Method fillOperators() is called for each element in a sequence.
  struct FillOperators { template<typename B> static void call(B& b) { b.fillOperators(); } };
    
  /// Functor for generic loops. Method fillBoundaryConditions() is called for each element in a sequence.
  struct FillBoundaryConditions { template<typename B> static void call(B& b) { b.fillBoundaryConditions(); } };
  
} // end namespace detail

/**
  * \ingroup Problem 
  *
  * \brief Structur to couple BaseProblems of variouse types
  */
template<class ProblemType=ProblemStat, 
	 BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(MAX_NUM_COUPLED_PROBLEMS, class BaseProblemType, void) >
class CoupledBaseProblem : public CouplingIterationInterface,
			   public CouplingTimeInterface,
			   public CouplingProblemStatImpl<ProblemType>
{
public:
  typedef CouplingProblemStatImpl<ProblemType> CProblemStat;
  typedef typename boost::tuples::detail::tie_mapper
    < BOOST_PP_ENUM_PARAMS(MAX_NUM_COUPLED_PROBLEMS, BaseProblemType) >::type BaseProblemsTupleType;

  #define BASEPROBLEM_ARGS(z, n, text) BOOST_PP_COMMA_IF(n) _BaseProblemType ## n &prob ## n
  #define BASEPROBLEM_TYPES(z, n, text) class _BaseProblemType ## n
  
  #define COUPLED_BASEPROBLEM_CONSTRUCTOR(z, n, text) \
      template<BOOST_PP_ENUM(BOOST_PP_INC(n), BASEPROBLEM_TYPES, BaseProblemType)> \
      CoupledBaseProblem(std::string name_, \
			 BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(n), BASEPROBLEM_ARGS, nil) ) \
      : CProblemStat(name_),\
	baseProblems( BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(n), prob) ), name(name_) \
      { dow = Global::getGeo(WORLD); } \
  /****************************************************************************/
  
  #define BASEPROBLEM_ARGS2(z, n, text) BOOST_PP_COMMA_IF(n) _BaseProblemType ## n *prob ## n
  
  #define COUPLED_BASEPROBLEM_CONSTRUCTOR2(z, n, text) \
      template<BOOST_PP_ENUM(BOOST_PP_INC(n), BASEPROBLEM_TYPES, BaseProblemType)> \
      CoupledBaseProblem(std::string name_, \
			 BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(n), BASEPROBLEM_ARGS2, nil) ) \
      : CProblemStat(name_),\
	baseProblems( BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(n), *prob) ), name(name_) \
      { dow = Global::getGeo(WORLD); } \
  /****************************************************************************/
  BOOST_PP_REPEAT(MAX_NUM_COUPLED_PROBLEMS, COUPLED_BASEPROBLEM_CONSTRUCTOR, nil)

  BOOST_PP_REPEAT(MAX_NUM_COUPLED_PROBLEMS, COUPLED_BASEPROBLEM_CONSTRUCTOR2, nil)
  
  ~CoupledBaseProblem() { }  
  
  void initialize(Flag initFlag,
		  ProblemStat *adoptProblem = NULL,
		  Flag adoptFlag = INIT_NOTHING) _OVERRIDE_
  {  
    tools::FOR_EACH< detail::AddProblem >::loop2(*this, baseProblems);
    tools::FOR_EACH< detail::AddIterationInterface >::loop2(*this, baseProblems);
    tools::FOR_EACH< detail::AddTimeInterface >::loop2(*this, baseProblems);

    // initialize all ProblemStat
    CProblemStat::initialize(initFlag, adoptProblem, adoptFlag);
    
    // set mesh dimension
    dim = CProblemStat::getMesh(0)->getDim();
  }  
  
  virtual void initData() {}  
  
  virtual void initTimeInterface()
  {
    tools::FOR_EACH< detail::InitData >::loop(baseProblems);
    initData();
    
    tools::FOR_EACH< detail::FillOperators >::loop(baseProblems);
    fillCouplingOperators();
    
    tools::FOR_EACH< detail::FillBoundaryConditions >::loop(baseProblems);
    fillCouplingBoundaryConditions();
  }
  
  
  virtual void fillCouplingOperators() {}
  virtual void fillCouplingBoundaryConditions() {}
  
  
  /// get the j-th solution-vector of the i-th problem
  template<int i>
  DOFVector<double> *getSolution(int j)
  { FUNCNAME("CoupledBaseProblem::getSolution<i>(j)");
    BOOST_STATIC_ASSERT_MSG(0 <= i && i < _LENGTH_<BaseProblemsTupleType>::value , "BaseProblem-index out of range");
    
    TEST_EXIT(0 <= j && j <= _GET_<i>(baseProblems).getNumComponents())("Indices out of range!\n");
    return _GET_<i>(baseProblems).getSolution()->getDOFVector(j);
  }
  
  
  /// pointer to the j-th feSpace of the i-th problem
  template<int i>
  inline const FiniteElemSpace* getFeSpace(int j=0) 
  { FUNCNAME("CoupledBaseProblem::getFeSpace<i>(j)");
    BOOST_STATIC_ASSERT_MSG(0 <= i && i < _LENGTH_<BaseProblemsTupleType>::value , "BaseProblem index out of range");
    
    TEST_EXIT(0 <= j && j <= _GET_<i>(baseProblems).getNumComponents())("Indices out of range!\n");
    return _GET_<i>(baseProblems).getFeSpace(j); 
  }
  
  std::string getName() { return name; }

protected:
  BaseProblemsTupleType baseProblems;

  unsigned dim;	// dimension of the meshes
  unsigned dow;	// dimension of the world
  
  std::string name;
};

} // end namespace AMDiS

#endif // COUPLED_BASE_PROBLEM_H