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

3
4
5
6
#include <map>
#include <string>
#include <utility>

7
8
#include <dune/typetree/childextraction.hh>

9
#include <dune/amdis/AdaptInfo.hpp>
10
#include <dune/amdis/Assembler.hpp>
11
#include <dune/amdis/FileWriter.hpp>
12
#include <dune/amdis/LocalAssembler.hpp>
13
#include <dune/amdis/GridFunctionOperator.hpp>
14
15
#include <dune/amdis/common/Loops.hpp>
#include <dune/amdis/common/Timer.hpp>
16

17
18
namespace AMDiS {

19
20
template <class Traits>
void ProblemStat<Traits>::initialize(
21
22
23
    Flag initFlag,
    Self* adoptProblem,
    Flag adoptFlag)
24
{
25
26
27
  // create grides
  if (grid) {
    warning("grid already created");
28
29
30
31
32
  }
  else {
    if (initFlag.isSet(CREATE_MESH) ||
        (!adoptFlag.isSet(INIT_MESH) &&
        (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE)))) {
33
      createGrid();
34
    }
35

36
37
38
39
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_MESH) ||
        adoptFlag.isSet(INIT_SYSTEM) ||
        adoptFlag.isSet(INIT_FE_SPACE))) {
40
      grid = adoptProblem->getGrid();
41
    }
42

43
    componentGrids.resize(nComponents, grid.get());
44
  }
45

46
47
  if (!grid)
    warning("no grid created");
48

49
  int globalRefinements = 0;
50
  Parameters::get(gridName + "->global refinements", globalRefinements);
51
  if (globalRefinements > 0) {
52
    grid->globalRefine(globalRefinements);
53
  }
54
55


56
  // create fespace
57
58
  if (globalBasis) {
    warning("globalBasis already created");
59
60
61
62
  }
  else {
    if (initFlag.isSet(INIT_FE_SPACE) ||
        (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
63
      createGlobalBasis();
64
    }
65

66
67
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
68
      globalBasis = adoptProblem->getGlobalBasis();
69
    }
70
  }
71

72
73
  if (!globalBasis)
    warning("no globalBasis created\n");
74
75


76
77
78
  // create system
  if (initFlag.isSet(INIT_SYSTEM))
    createMatricesAndVectors();
79

80
81
82
83
84
  if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
    solution = adoptProblem->getSolution();
    rhs = adoptProblem->getRhs();
    systemMatrix = adoptProblem->getSystemMatrix();
  }
85

86

87
88
89
90
91
92
93
  // create solver
  if (linearSolver) {
    warning("solver already created\n");
  }
  else {
    if (initFlag.isSet(INIT_SOLVER))
      createSolver();
94

95
96
97
    if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
      test_exit(!linearSolver, "solver already created\n");
      linearSolver = adoptProblem->getSolver();
98
    }
99
100
  }

101
102
  if (!linearSolver) {
    warning("no solver created\n");
103
104
  }

105

106
107
108
  // create file writer
  if (initFlag.isSet(INIT_FILEWRITER))
    createFileWriter();
109

110
111
  solution->compress();
}
112

113

114
115
template <class Traits>
void ProblemStat<Traits>::createFileWriter()
116
117
118
119
120
121
122
123
124
{
  auto localView = globalBasis->localView();
  forEachNode(localView.tree(), [&,this](auto const& node, auto treePath)
  {
    std::string componentName = name + "->output[" + to_string(treePath) + "]";

    if (!Parameters::get<std::string>(componentName + "->filename"))
      return;

125
    auto writer = makeFileWriterPtr<Traits>(componentName, globalBasis, treePath, solution);
126
127
128
129
130
    filewriter.push_back(writer);
  });
}


131
// add matrix/vector operator terms
132

133
template <class Traits>
134
  template <class Operator, class RowTreePath, class ColTreePath>
135
void ProblemStat<Traits>::addMatrixOperator(
136
    Operator const& preOp,
137
138
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
139
{
140
141
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
142

143
  auto op = makeGridOperator(preOp, globalBasis->gridView());
144
  auto localAssembler = makeLocalAssemblerPtr<Element>(op, i, j);
145

146
  matrixOperators[i][j].element.push_back({localAssembler, factor, estFactor});
147
  matrixOperators[i][j].changing = true;
148
}
149

150

151
template <class Traits>
152
  template <class Operator, class RowTreePath, class ColTreePath>
153
void ProblemStat<Traits>::addMatrixOperator(
154
    BoundaryType b,
155
    Operator const& preOp,
156
157
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
158
{
159
160
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
161
  using Intersection = typename GridView::Intersection;
162

163
  auto op = makeGridOperator(preOp, globalBasis->gridView());
164
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(op, i, j);
165

166
167
  matrixOperators[i][j].boundary.push_back({localAssembler, factor, estFactor, b});
  matrixOperators[i][j].changing = true;
168
}
169
170


171
template <class Traits>
172
  template <class Operator, class TreePath>
173
void ProblemStat<Traits>::addVectorOperator(
174
    Operator const& preOp,
175
176
    TreePath path,
    double* factor, double* estFactor)
177
{
178
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
179

180
  auto op = makeGridOperator(preOp, globalBasis->gridView());
181
  auto localAssembler = makeLocalAssemblerPtr<Element>(op, i);
182

183
  rhsOperators[i].element.push_back({localAssembler, factor, estFactor});
184
  rhsOperators[i].changing = true;
185
}
186

187

188
template <class Traits>
189
  template <class Operator, class TreePath>
190
void ProblemStat<Traits>::addVectorOperator(
191
    BoundaryType b,
192
    Operator const& preOp,
193
194
    TreePath path,
    double* factor, double* estFactor)
195
{
196
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
197
  using Intersection = typename GridView::Intersection;
198

199
  auto op = makeGridOperator(preOp, globalBasis->gridView());
200
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(op, i);
201

202
203
  rhsOperators[i].boundary.push_back({localAssembler, factor, estFactor, b});
  rhsOperators[i].changing = true;
204
}
205
206


207
// Adds a Dirichlet boundary condition
208
template <class Traits>
209
  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
210
void ProblemStat<Traits>::
211
addDirichletBC(Predicate const& predicate, RowTreePath row, ColTreePath col, Values const& values)
212
213
{
  static_assert( Concepts::Functor<Predicate, bool(WorldVector)>,
214
    "Function passed to addDirichletBC for `predicate` does not model the Functor<bool(WorldVector)> concept");
215

216
217
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
218

219
  auto valueGridFct = makeGridFunction(values, globalBasis->gridView());
220

221
  using Range = typename decltype(valueGridFct)::Range;
222
  using BcType = DirichletBC<WorldVector,Range>;
223
224
225
  auto bc = std::make_shared<BcType>(predicate, valueGridFct);
  constraints[i][j].push_back(bc);
  // TODO: make DirichletBC an abstract class and add specialization with gridfunction type
226
}
227

228

229
230
template <class Traits>
void ProblemStat<Traits>::
231
232
233
234
235
236
237
238
239
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
  Timer t;

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

  solution->compress();
240

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  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());
    }
  }
256

257
258
259
260
261
262
263
264
265
266
267
  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!");
  }
}
268

269

270
271
template <class Traits>
void ProblemStat<Traits>::
272
buildAfterCoarsen(AdaptInfo& /*adaptInfo*/, Flag /*flag*/, bool asmMatrix, bool asmVector)
273
274
275
{
  Timer t;

276
  Assembler<Traits> assembler(*globalBasis, matrixOperators, rhsOperators, constraints);
277

278
279
280
  auto gv = leafGridView();
  assembler.update(gv);
  assembler.assemble(*systemMatrix, *solution, *rhs, asmMatrix, asmVector);
281

282
283
  msg("buildAfterCoarsen needed ", t.elapsed(), " seconds");
}
284

285

286
287
template <class Traits>
void ProblemStat<Traits>::
288
writeFiles(AdaptInfo& adaptInfo, bool force)
289
290
{
  Timer t;
291
292
  for (auto writer : filewriter)
    writer->writeFiles(adaptInfo, force);
293
294
  msg("writeFiles needed ", t.elapsed(), " seconds");
}
295

296
} // end namespace AMDiS