ProblemStat.inc.hpp 9.68 KB
Newer Older
1
2
#pragma once

3
4
#include <dune/typetree/childextraction.hh>

5
#include <dune/amdis/AdaptInfo.hpp>
6
#include <dune/amdis/Assembler.hpp>
7
8
#include <dune/amdis/common/Loops.hpp>
#include <dune/amdis/common/Timer.hpp>
9
#include <dune/amdis/linear_algebra/HierarchicWrapper.hpp>
10

11
12
namespace AMDiS {

13
14
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::initialize(
15
16
17
    Flag initFlag,
    Self* adoptProblem,
    Flag adoptFlag)
18
{
19
20
21
  // create grides
  if (grid) {
    warning("grid already created");
22
23
24
25
26
  }
  else {
    if (initFlag.isSet(CREATE_MESH) ||
        (!adoptFlag.isSet(INIT_MESH) &&
        (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE)))) {
27
      createGrid();
28
    }
29

30
31
32
33
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_MESH) ||
        adoptFlag.isSet(INIT_SYSTEM) ||
        adoptFlag.isSet(INIT_FE_SPACE))) {
34
      grid = adoptProblem->getGrid();
35
    }
36

37
    componentGrids.resize(nComponents, grid.get());
38
  }
39

40
41
  if (!grid)
    warning("no grid created");
42

43
  int globalRefinements = 0;
44
  Parameters::get(gridName + "->global refinements", globalRefinements);
45
  if (globalRefinements > 0) {
46
    grid->globalRefine(globalRefinements);
47
  }
48
49


50
  // create fespace
51
52
  if (globalBasis) {
    warning("globalBasis already created");
53
54
55
56
  }
  else {
    if (initFlag.isSet(INIT_FE_SPACE) ||
        (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
57
      createGlobalBasis();
58
    }
59

60
61
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
62
      globalBasis = adoptProblem->getGlobalBasis();
63
    }
64
  }
65

66
67
  if (!globalBasis)
    warning("no globalBasis created\n");
68
69


70
71
72
  // create system
  if (initFlag.isSet(INIT_SYSTEM))
    createMatricesAndVectors();
73

74
75
76
77
78
  if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
    solution = adoptProblem->getSolution();
    rhs = adoptProblem->getRhs();
    systemMatrix = adoptProblem->getSystemMatrix();
  }
79

80

81
82
83
84
85
86
87
  // create solver
  if (linearSolver) {
    warning("solver already created\n");
  }
  else {
    if (initFlag.isSet(INIT_SOLVER))
      createSolver();
88

89
90
91
    if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
      test_exit(!linearSolver, "solver already created\n");
      linearSolver = adoptProblem->getSolver();
92
    }
93
94
  }

95
96
  if (!linearSolver) {
    warning("no solver created\n");
97
98
  }

99

100
101
102
  // create file writer
  if (initFlag.isSet(INIT_FILEWRITER))
    createFileWriter();
103

104
105
  solution->compress();
}
106

107

108
// add matrix/vector operator terms
109

110
template <class GlobalBasis>
111
112
113
114
115
  template <class RowTreePath, class ColTreePath>
void ProblemStat<GlobalBasis>::addMatrixOperator(
    ElementOperator const& op,
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
116
{
117
118
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
119
120
121
122
  matrixOperators[i][j].element.push_back({std::make_shared<ElementOperator>(op), factor, estFactor});
  matrixOperators[i][j].changing = true;
}

123

124
template <class GlobalBasis>
125
126
127
128
129
  template <class RowTreePath, class ColTreePath>
void ProblemStat<GlobalBasis>::addMatrixOperator(
    IntersectionOperator const& op,
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
130
{
131
132
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
133
134
  matrixOperators[i][j].intersection.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor});
  matrixOperators[i][j].changing = true;
135
}
136

137

138
template <class GlobalBasis>
139
140
141
142
143
144
  template <class RowTreePath, class ColTreePath>
void ProblemStat<GlobalBasis>::addMatrixOperator(
    BoundaryType b,
    IntersectionOperator const& op,
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
145
{
146
147
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
148
149
  matrixOperators[i][j].boundary.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor, b});
  matrixOperators[i][j].changing = true;
150
}
151

152
#if 0
153
154
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
155
addMatrixOperator(std::map< std::pair<int,int>, ElementOperator> ops)
156
157
158
159
{
  for (auto op : ops){
    int r = op.first.first;
    int c = op.first.second;
160
161
    matrixOperators[r][c].element.push_back({std::make_shared<ElementOperator>(op.second)});
    matrixOperators[r][c].changing = true;
162
  }
163
}
164

165
166
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
167
addMatrixOperator(std::map< std::pair<int,int>, std::pair<ElementOperator,bool> > ops)
168
169
170
171
{
  for (auto op : ops) {
    int r = op.first.first;
    int c = op.first.second;
172
173
    matrixOperators[r][c].element.push_back({std::make_shared<ElementOperator>(op.second.first)});
    matrixOperators[r][c].changing = matrixOperators[r][c].changing || op.second.second;
174
  }
175
}
176
#endif
177
178


179
template <class GlobalBasis>
180
181
182
183
184
  template <class TreePath>
void ProblemStat<GlobalBasis>::addVectorOperator(
    ElementOperator const& op,
    TreePath path,
    double* factor, double* estFactor)
185
{
186
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
187
188
  rhsOperators[i].element.push_back({std::make_shared<ElementOperator>(op), factor, estFactor});
  rhsOperators[i].changing = true;
189
190
}

191

192
template <class GlobalBasis>
193
194
195
196
197
  template <class TreePath>
void ProblemStat<GlobalBasis>::addVectorOperator(
    IntersectionOperator const& op,
    TreePath path,
    double* factor, double* estFactor)
198
{
199
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
200
201
  rhsOperators[i].intersection.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor});
  rhsOperators[i].changing = true;
202
}
203

204

205
template <class GlobalBasis>
206
207
208
209
210
211
  template <class TreePath>
void ProblemStat<GlobalBasis>::addVectorOperator(
    BoundaryType b,
    IntersectionOperator const& op,
    TreePath path,
    double* factor, double* estFactor)
212
{
213
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
214
215
  rhsOperators[i].boundary.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor, b});
  rhsOperators[i].changing = true;
216
}
217

218
#if 0
219
220
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
221
addVectorOperator(std::map< int, ElementOperator> ops)
222
223
{
  for (auto op : ops) {
224
225
    rhsOperators[op.first].element.push_back({std::make_shared<ElementOperator>(op.second)});
    rhsOperators[op.first].changing = true;
226
227
  }
}
228

229
230
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
231
addVectorOperator(std::map< int, std::pair<ElementOperator, bool> > ops)
232
233
{
  for (auto op : ops) {
234
235
    rhsOperators[op.first].element.push_back({std::make_shared<ElementOperator>(op.second.first)});
    rhsOperators[op.first].changing = rhsOperators[op.first].changing || op.second.second;
236
  }
237
}
238
#endif
239
240


241
// Adds a Dirichlet boundary condition
242
template <class GlobalBasis>
243
  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
244
void ProblemStat<GlobalBasis>::
245
addDirichletBC(Predicate const& predicate, RowTreePath row, ColTreePath col, Values const& values)
246
247
248
{
  static_assert( Concepts::Functor<Predicate, bool(WorldVector)>,
    "Function passed to addDirichletBC for predicate does not model the Functor<bool(WorldVector)> concept");
249

250
251
  static_assert( Concepts::Callable<Values, WorldVector>,
    "Function passed to addDirichletBC for values does not model the Callable<WorldVector> concept");
252

253
254
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
255

256
257
  using Range = decltype(values(std::declval<WorldVector>()));
  using BcType = DirichletBC<WorldVector,Range>;
258

259
260
  auto bc = std::make_shared<BcType>(predicate, values);
  constraints[i][i].push_back(bc);
261
}
262

263

264
265
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
  Timer t;

  SolverInfo solverInfo(name + "->solver");
  solverInfo.setCreateMatrixData(createMatrixData);
  solverInfo.setStoreMatrixData(storeMatrixData);

  solution->compress();
  linearSolver->solve(systemMatrix->getMatrix(),
                      solution->getVector(), rhs->getVector(),
                      solverInfo);

  if (solverInfo.getInfo() > 0) {
    msg("solution of discrete system needed ", t.elapsed(), " seconds");

    if (solverInfo.getAbsResidual() >= 0.0) {
      if (solverInfo.getRelResidual() >= 0.0)
        msg("Residual norm: ||b-Ax|| = ", solverInfo.getAbsResidual(),
                          ", ||b-Ax||/||b|| = ", solverInfo.getRelResidual());
      else
        msg("Residual norm: ||b-Ax|| = ", solverInfo.getAbsResidual());
    }
  }
290

291
292
293
294
295
296
297
298
299
300
301
  if (solverInfo.doBreak()) {
    std::stringstream tol_str;
    if (solverInfo.getAbsTolerance() > 0
        && solverInfo.getAbsResidual() > solverInfo.getAbsTolerance())
      tol_str << "absTol = " << solverInfo.getAbsTolerance() << " ";
    if (solverInfo.getRelTolerance() > 0
        && solverInfo.getRelResidual() > solverInfo.getRelTolerance())
      tol_str << "relTol = " << solverInfo.getRelTolerance() << " ";
    error_exit("Tolerance ", tol_str.str(), " could not be reached!");
  }
}
302

303

304
305
306
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
buildAfterCoarsen(AdaptInfo& /*adaptInfo*/, Flag /*flag*/, bool asmMatrix, bool asmVector)
307
308
309
{
  Timer t;

310
  Assembler<GlobalBasis> assembler(*globalBasis, matrixOperators, rhsOperators, constraints);
311

312
313
314
  auto gv = leafGridView();
  assembler.update(gv);
  assembler.assemble(*systemMatrix, *solution, *rhs, asmMatrix, asmVector);
315

316
317
  msg("buildAfterCoarsen needed ", t.elapsed(), " seconds");
}
318

319

320
321
322
template <class GlobalBasis>
void ProblemStat<GlobalBasis>::
writeFiles(AdaptInfo& adaptInfo, bool /*force*/)
323
324
{
  Timer t;
325
  // filewriter->write(adaptInfo.getTime(), *solution);
326
327
  msg("writeFiles needed ", t.elapsed(), " seconds");
}
328

329
} // end namespace AMDiS