nonlin2.cc 6.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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().
   */
21
22
23
  double operator()(const WorldVector<double>& x) const {
    return exp(-10.0 * (x * x));
  }
24
25
26
27
28
29
30
31
};

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

  const double& operator()(const WorldVector<double>& x) const {
32
    return 0.0;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  };
};

/** \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().
   */
54
  double operator()(const WorldVector<double>& x) const {
55
    int dow = x.getSize();
56
57
58
59
60
    double r2 = x*x;
    double ux = exp(-10.0*r2);
    double ux4 = ux*ux*ux*ux;
    return ux4 -(400.0*r2 - 20.0*dow)*ux;
  }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
};

/** \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().
   */
76
77
78
  double operator()(const double& x) const {
    return x * x * x;
  }
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
};

// ===========================================================================
// ===== 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);
}