nonlin2.cc 7.76 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
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
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
215
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "AMDiS.h"

using namespace std;
using namespace AMDiS;

// ===========================================================================
// ===== function definitions ================================================
// ===========================================================================

/** \brief
 * Dirichlet boundary function
 */
class G : public AbstractFunction<double, WorldVector<double> >
{
public:
  MEMORY_MANAGED(G);

  /** \brief
   * Implementation of AbstractFunction::operator().
   */
  const double& operator()(const WorldVector<double>& x) const {
    static double result;
    result = exp(-10.0*(x*x));
    return result;
  };
};

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

  const double& operator()(const WorldVector<double>& x) const {
    static double result = 0.0;
    return result;
  };
};

/** \brief
 * RHS function
 */
class F : public AbstractFunction<double, WorldVector<double> >
{
public:
  MEMORY_MANAGED(F);

  /** \brief
   * Constructor
   */
  F(int degree)
    : AbstractFunction<double, WorldVector<double> >(degree)
  {};

  /** \brief
   * Implementation of AbstractFunction::operator().
   */
  const double& operator()(const WorldVector<double>& x) const {
    static double result;
    int dow = x.getSize();
    double r2 = x*x, ux = exp(-10.0*r2), ux4 = ux*ux*ux*ux;
    result = ux4 -(400.0*r2 - 20.0*dow)*ux;
    return result;
  };
};

/** \brief
 * Needed for zero order term.
 */
class X3 : public AbstractFunction<double, double>
{
public:
  MEMORY_MANAGED(X3);

  X3() : AbstractFunction<double, double>(3) {};

  /** \brief
   * Implementation of AbstractFunction::operator().
   */
  const double& operator()(const double& x) const {
    static double result = 0.0;
    result = x * x * x;
    return result;
  };
};

#if 0
class NewtonMethod : public ProblemIterationInterface
{
public:
  NewtonMethod(ProblemScal *problem, ProblemScal *newton)
    : problemNonlin(problem),
      newtonStep(newton)
  {
    newtonTolerance = 1e-8;
    newtonMaxIter = 100;
  };

  
  Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION)
  {
    Flag flag;
    DOFVector<double> *correction = newtonStep->getSolution();
    DOFVector<double> *solution = problemNonlin->getSolution();
    int newtonIteration = 0;
    double res = 0.0;
    do {    
      newtonIteration++;
      newtonStep->buildAfterCoarsen(adaptInfo, flag);    
      newtonStep->solve(adaptInfo);
      res = correction->L2Norm();
      *solution -= *correction;
      MSG("newton iteration %d: residual %f (tol: %f)\n",
	  newtonIteration, res, newtonTolerance);
    } while((res > newtonTolerance) && (newtonIteration < newtonMaxIter));    
  };

  int getNumProblems() { return 2; };

  ProblemStatBase *getProblem(int number = 0) 
  {
    FUNCNAME("NewtonMethod::getProblem()");
    switch(number) {
    case 0: 
      return problemNonlin;
      break;
    case 1:
      return newtonStep;
      break;
    default:
      ERROR_EXIT("invalid problem number\n");
      return NULL;
    }
  };

private:
  ProblemScal *problemNonlin;
  ProblemScal *newtonStep;
  double newtonTolerance;
  int newtonMaxIter;
};
#endif

// ===========================================================================
// ===== class NonLin ========================================================
// ===========================================================================

/** \brief
 * Non linear problem.
 */
class NonLin : public ProblemScal
{
public:
  NonLin(const char *name)
    : ProblemScal(name)
  {
    newtonTolerance = 1e-8;
    GET_PARAMETER(0, std::string(name) + "->newton->tolerance", "%f", 
		  &newtonTolerance);    
    newtonMaxIter = 50;
    GET_PARAMETER(0, std::string(name) + "->newton->max iteration", "%d", 
		  &newtonMaxIter);    
  };

  void initialize(Flag initFlag,
		  ProblemScal *adoptProblem = NULL,
		  Flag adoptFlag = INIT_NOTHING)
  {
    ProblemScal::initialize(initFlag, adoptProblem, adoptFlag);
    correction = NEW DOFVector<double>(this->getFESpace(), "old solution");
    correction->set(0.0);

    dirichletZero = NEW DirichletBC(1, &zero, feSpace_);
    dirichletG    = NEW DirichletBC(1, &g, feSpace_);

    systemMatrix_->getBoundaryManager()->addBoundaryCondition(dirichletZero);
    solution_->getBoundaryManager()->addBoundaryCondition(dirichletG);
    rhs_->getBoundaryManager()->addBoundaryCondition(dirichletZero);
    correction->getBoundaryManager()->addBoundaryCondition(dirichletZero);
  };

  ~NonLin()
  { 
    DELETE correction; 
    DELETE dirichletZero;
    DELETE dirichletG;
  };

  void buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag) {};

  void solve(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("NonLin::solve()");
    DOFVector<double> *sol = solution_;
    solution_ = correction;
    double res = 0;
    int newtonIteration = 0;
    Flag flag;

    // fill boundary conditions
    sol->getBoundaryManager()->initVector(sol);

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh_, -1, 
					 Mesh::CALL_LEAF_EL | 
					 Mesh::FILL_COORDS |
					 Mesh::FILL_BOUND);

    while(elInfo) {
      sol->getBoundaryManager()->fillBoundaryConditions(elInfo, sol);
      elInfo = stack.traverseNext(elInfo);
    }

    sol->getBoundaryManager()->exitVector(sol);

    do {    
      newtonIteration++;
      ProblemScal::buildAfterCoarsen(adaptInfo, flag);    
      ProblemScal::solve(adaptInfo);
      *sol -= *correction;
      res = correction->L2Norm();
      MSG("newton iteration %d: residual %f (tol: %f)\n",
	  newtonIteration, res, newtonTolerance);
    } while((res > newtonTolerance) && (newtonIteration < newtonMaxIter));
    solution_ = sol;
  };

private:
  double newtonTolerance;
  int newtonMaxIter;
  DOFVector<double> *correction;
  Zero zero;
  G g;
  DirichletBC *dirichletZero;
  DirichletBC *dirichletG;
};

// ===========================================================================
// ===== main program ========================================================
// ===========================================================================

int main(int argc, char* argv[])
{
  FUNCNAME("main");

  // ===== check for init file =====
  TEST_EXIT(argc == 2)("usage: nonlin initfile\n");

  // ===== init parameters =====
  Parameters::init(false, argv[1]);

  NonLin nonlin("nonlin");
  nonlin.initialize(INIT_ALL);

  // === create adapt info ===
  AdaptInfo *adaptInfo = NEW AdaptInfo("nonlin->adapt", 1);

  // === create adapt ===
  AdaptStationary *adapt = NEW AdaptStationary("nonlin->adapt",
					       &nonlin,
					       adaptInfo);

  // ===== create operators =====
  double four = 4.0;
  double one = 1.0;
  double zero = 0.0;
  double minusOne = -1.0;

  Operator *nonlinOperator0 = NEW Operator(Operator::MATRIX_OPERATOR | 
					   Operator::VECTOR_OPERATOR, 
					   nonlin.getFESpace());

  nonlinOperator0->setUhOld(nonlin.getSolution());
  nonlinOperator0->addZeroOrderTerm(NEW VecAtQP_ZOT(nonlin.getSolution(), 
						    NEW X3));

  nonlin.addMatrixOperator(nonlinOperator0, &four, &one);
  nonlin.addVectorOperator(nonlinOperator0, &one, &zero);

  Operator *nonlinOperator2 = NEW Operator(Operator::MATRIX_OPERATOR | 
					   Operator::VECTOR_OPERATOR,
					   nonlin.getFESpace());

  nonlinOperator2->setUhOld(nonlin.getSolution());
  nonlinOperator2->addSecondOrderTerm(NEW Laplace_SOT);

  nonlin.addMatrixOperator(nonlinOperator2, &one, &one);
  nonlin.addVectorOperator(nonlinOperator2, &one, &zero);

  int degree = nonlin.getFESpace()->getBasisFcts()->getDegree();

  Operator* rhsFunctionOperator = NEW Operator(Operator::VECTOR_OPERATOR, 
					       nonlin.getFESpace());
  rhsFunctionOperator->addZeroOrderTerm(NEW CoordsAtQP_ZOT(NEW F(degree)));
  
  nonlin.addVectorOperator(rhsFunctionOperator, &minusOne, &one);

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

  nonlin.writeFiles(adaptInfo, true);
}