CouplingBaseProblem2.h 6.95 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
/** \file CouplingBaseProblem.h */
Praetorius, Simon's avatar
Praetorius, Simon committed
2

Praetorius, Simon's avatar
Praetorius, Simon committed
3
4
#ifndef COUPLING_BASE_PROBLEM_H
#define COUPLING_BASE_PROBLEM_H
Praetorius, Simon's avatar
Praetorius, Simon committed
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

#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(); } };
    
Praetorius, Simon's avatar
Praetorius, Simon committed
60
61
62
  /// Functor for generic loops. Method initData() is called for each element in a sequence.
  struct FinalizeData { template<typename B> static void call(B& b) { b.finalizeData(); } };
    
Praetorius, Simon's avatar
Praetorius, Simon committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  /// 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) >
Praetorius, Simon's avatar
Praetorius, Simon committed
78
class CouplingBaseProblem : public CouplingIterationInterface,
Praetorius, Simon's avatar
Praetorius, Simon committed
79
80
81
82
83
84
85
86
87
88
			   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
89
90
    
  // constructors created by C-Macros for each number of arguments, using BOOST_PP-Macros
Praetorius, Simon's avatar
Praetorius, Simon committed
91
92
93
  
  #define COUPLED_BASEPROBLEM_CONSTRUCTOR(z, n, text) \
      template<BOOST_PP_ENUM(BOOST_PP_INC(n), BASEPROBLEM_TYPES, BaseProblemType)> \
Praetorius, Simon's avatar
Praetorius, Simon committed
94
      CouplingBaseProblem(std::string name_, \
Praetorius, Simon's avatar
Praetorius, Simon committed
95
96
97
98
99
100
101
102
103
104
			 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)> \
Praetorius, Simon's avatar
Praetorius, Simon committed
105
      CouplingBaseProblem(std::string name_, \
Praetorius, Simon's avatar
Praetorius, Simon committed
106
107
108
109
110
111
112
113
114
			 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)
  
Praetorius, Simon's avatar
Praetorius, Simon committed
115
  ~CouplingBaseProblem() { }  
Praetorius, Simon's avatar
Praetorius, Simon committed
116
117
  
  void initialize(Flag initFlag,
118
		  ProblemStatSeq *adoptProblem = NULL,
Praetorius, Simon's avatar
Praetorius, Simon committed
119
120
121
122
123
124
125
126
127
128
129
130
131
		  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();
  }  
  
Praetorius, Simon's avatar
Praetorius, Simon committed
132
133
  virtual void initData() {}
  virtual void finalizeData() {}  
Praetorius, Simon's avatar
Praetorius, Simon committed
134
135
136
137
138
139
140
141
142
143
144
  
  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();
Praetorius, Simon's avatar
Praetorius, Simon committed
145
146
147
    
    tools::FOR_EACH< detail::FinalizeData >::loop(baseProblems);
    finalizeData();
Praetorius, Simon's avatar
Praetorius, Simon committed
148
149
150
151
152
153
154
155
156
157
  }
  
  
  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)
Praetorius, Simon's avatar
Praetorius, Simon committed
158
  { FUNCNAME("CouplingBaseProblem::getSolution<i>(j)");
Praetorius, Simon's avatar
Praetorius, Simon committed
159
160
161
162
163
164
165
166
167
168
    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) 
Praetorius, Simon's avatar
Praetorius, Simon committed
169
  { FUNCNAME("CouplingBaseProblem::getFeSpace<i>(j)");
Praetorius, Simon's avatar
Praetorius, Simon committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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

Praetorius, Simon's avatar
Praetorius, Simon committed
189
#endif // COUPLING_BASE_PROBLEM_H