couple.cc 4.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "AMDiS.h"

using namespace std;
using namespace AMDiS;

class G : public AbstractFunction<double, WorldVector<double> >
{
public:
  MEMORY_MANAGED(G);

11
12
13
  double operator()(const WorldVector<double>& x) const {
    return exp(-10.0 * (x * x));
  }
14
15
16
17
18
19
20
21
22
};

class F : public AbstractFunction<double, WorldVector<double> >
{
public:
  MEMORY_MANAGED(F);

  F(int degree) : AbstractFunction<double, WorldVector<double> >(degree) {};

23
  double operator()(const WorldVector<double>& x) const {
24
    int dow = x.getSize();
25
26
27
28
    double r2 = (x * x);
    double ux = exp(-10.0 * r2);
    return -(400.0 * r2 - 20.0 * dow) * ux;
  }
29
30
31
32
33
34
35
36
};

class MyCoupledIteration : public ProblemIterationInterface
{
public:
  MyCoupledIteration(ProblemStatBase *prob1,
		     ProblemStatBase *prob2)
    : problem1(prob1),
37
38
39
      problem2(prob2),
      name("MyCoupledIteration")
  {}
40
41
42
43
44
45
46

  void beginIteration(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("StandardProblemIteration::beginIteration()");
    MSG("\n");
    MSG("begin of iteration number: %d\n", adaptInfo->getSpaceIteration()+1);
    MSG("=============================\n");
47
  }
48
49
50
51
52
53

  void endIteration(AdaptInfo *adaptInfo) {
    FUNCNAME("StandardProblemIteration::endIteration()");
    MSG("\n");
    MSG("end of iteration number: %d\n", adaptInfo->getSpaceIteration()+1);
    MSG("=============================\n");
54
  }
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

  Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION) 
  {
    Flag flag, markFlag;
    if(toDo.isSet(MARK)) markFlag = problem1->markElements(adaptInfo);
    if(toDo.isSet(ADAPT) && markFlag.isSet(MESH_REFINED)) flag = problem1->refineMesh(adaptInfo);

    if(toDo.isSet(BUILD)) problem1->buildAfterCoarsen(adaptInfo, markFlag);
    if(toDo.isSet(SOLVE)) problem1->solve(adaptInfo);
 
    if(toDo.isSet(BUILD)) problem2->buildAfterCoarsen(adaptInfo, markFlag);
    if(toDo.isSet(SOLVE)) problem2->solve(adaptInfo);

    if(toDo.isSet(ESTIMATE)) problem1->estimate(adaptInfo);    
    return flag;
70
  }
71
72
73
74

  int getNumProblems() 
  {
    return 2;
75
  }
76
77
78
79

  ProblemStatBase *getProblem(int number = 0) 
  {
    FUNCNAME("CoupledIteration::getProblem()");
80
81
82
83
84
85

    if (number == 0) 
      return problem1;
    if (number == 1) 
      return problem2;

86
87
    ERROR_EXIT("invalid problem number\n");
    return NULL;
88
89
90
91
92
93
94
95
96
97
98
99
  }

  const std::string& getName()
  {
    return name;
  }

  void serialize(std::ostream&)
  {}

  void deserialize(std::istream&)
  {}
100
101
102

private:
  ProblemStatBase *problem1;
103

104
  ProblemStatBase *problem2;
105
106

  std::string name;
107
108
109
110
111
112
113
114
115
};

class Identity : public AbstractFunction<double, double>
{
public:
  MEMORY_MANAGED(Identity);

  Identity(int degree) : AbstractFunction<double, double>(degree) {};

116
117
118
  double operator()(const double& x) const {
    return x;
  }
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
181
182
183
184
185
186
};

int main(int argc, char* argv[])
{
  FUNCNAME("main");
  TEST_EXIT(argc == 2)("usage: couple initfile\n");
  Parameters::init(true, argv[1]);

  // ===== create and init the first problem ===== 
  ProblemScal problem1("problem1");
  problem1.initialize(INIT_ALL);

  // ===== create and init the second problem ===== 
  Flag initFlag = 
    INIT_FE_SPACE |
    INIT_SYSTEM |
    INIT_SOLVER |
    INIT_FILEWRITER;

  Flag adoptFlag =
    CREATE_MESH |
    INIT_MESH;

  ProblemScal problem2("problem2");
  problem2.initialize(initFlag,
		      &problem1,
		      adoptFlag);

  // ===== add boundary conditions for problem1 =====
  problem1.addDirichletBC(1, NEW G);

  // ===== add boundary conditions for problem1 =====
  //problem2.addDirichletBC(1, NEW G);

  // ===== create operators for problem1 =====
  Operator matrixOperator1(Operator::MATRIX_OPERATOR, problem1.getFESpace());
  matrixOperator1.addSecondOrderTerm(NEW Laplace_SOT);
  problem1.addMatrixOperator(&matrixOperator1);

  int degree = problem1.getFESpace()->getBasisFcts()->getDegree();
  Operator rhsOperator1(Operator::VECTOR_OPERATOR, problem1.getFESpace());
  rhsOperator1.addZeroOrderTerm(NEW CoordsAtQP_ZOT(NEW F(degree)));
  problem1.addVectorOperator(&rhsOperator1);

  // ===== create operators for problem2 =====
  Operator matrixOperator2(Operator::MATRIX_OPERATOR, problem2.getFESpace());
  matrixOperator2.addZeroOrderTerm(NEW Simple_ZOT);
  problem2.addMatrixOperator(&matrixOperator2);
  
  Operator rhsOperator2(Operator::VECTOR_OPERATOR, problem2.getFESpace());
  rhsOperator2.addZeroOrderTerm(NEW VecAtQP_ZOT(problem1.getSolution(), 
						NEW Identity(degree)));
  problem2.addVectorOperator(&rhsOperator2);

  // ===== create adaptation loop and iteration interface =====
  AdaptInfo *adaptInfo = NEW AdaptInfo("couple->adapt", 1);
  MyCoupledIteration coupledIteration(&problem1, &problem2);
  AdaptStationary *adapt = NEW AdaptStationary("couple->adapt",
					       &coupledIteration,
					       adaptInfo);

  // ===== start adaptation loop =====
  adapt->adapt();

  // ===== write solution ===== 
  problem1.writeFiles(adaptInfo, true);
  problem2.writeFiles(adaptInfo, true);
}