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

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

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

10
11
12
13
14
15
#include <amdis/AdaptInfo.hpp>
#include <amdis/Assembler.hpp>
#include <amdis/FileWriter.hpp>
#include <amdis/LocalAssembler.hpp>
#include <amdis/GridFunctionOperator.hpp>
#include <amdis/common/Loops.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

44
45
  if (!grid)
    warning("no grid created");
46

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


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

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

70
71
  if (!globalBasis)
    warning("no globalBasis created\n");
72
73


74
75
76
  // create system
  if (initFlag.isSet(INIT_SYSTEM))
    createMatricesAndVectors();
77

78
  if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
79
80
81
    solution = adoptProblem->solution;
    rhs = adoptProblem->rhs;
    systemMatrix = adoptProblem->systemMatrix;
82
  }
83

84

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

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

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

103

104
105
106
  // create file writer
  if (initFlag.isSet(INIT_FILEWRITER))
    createFileWriter();
107

108
109
  solution->compress();
}
110

111

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

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

123
    auto writer = makeFileWriterPtr(componentName, this->getSolution(treePath));
Praetorius, Simon's avatar
Praetorius, Simon committed
124
    filewriter.push_back(std::move(writer));
125
126
127
128
  });
}


129
// add matrix/vector operator terms
130

131
template <class Traits>
132
  template <class Operator, class RowTreePath, class ColTreePath>
133
void ProblemStat<Traits>::addMatrixOperator(
134
    Operator const& preOp,
135
136
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
137
{
138
  static_assert( Concepts::PreTreePath<RowTreePath>,
139
      "row must be a valid treepath, or an integer/index-constant");
140
  static_assert( Concepts::PreTreePath<ColTreePath>,
141
142
      "col must be a valid treepath, or an integer/index-constant");

143
144
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
145

146
  auto op = makeGridOperator(preOp, globalBasis->gridView());
147
  auto localAssembler = makeLocalAssemblerPtr<Element>(std::move(op), i, j);
148

149
  matrixOperators[i][j].element.push_back({localAssembler, factor, estFactor});
150
  matrixOperators[i][j].changing = true;
151
}
152

153

154
template <class Traits>
155
  template <class Operator, class RowTreePath, class ColTreePath>
156
void ProblemStat<Traits>::addMatrixOperator(
157
    BoundaryType b,
158
    Operator const& preOp,
159
160
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
161
{
162
  static_assert( Concepts::PreTreePath<RowTreePath>,
163
      "row must be a valid treepath, or an integer/index-constant");
164
  static_assert( Concepts::PreTreePath<ColTreePath>,
165
166
      "col must be a valid treepath, or an integer/index-constant");

167
168
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
169
  using Intersection = typename GridView::Intersection;
170

171
  auto op = makeGridOperator(preOp, globalBasis->gridView());
172
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(std::move(op), i, j);
173

174
175
  matrixOperators[i][j].boundary.push_back({localAssembler, factor, estFactor, b});
  matrixOperators[i][j].changing = true;
176
}
177
178


179
template <class Traits>
180
  template <class Operator, class TreePath>
181
void ProblemStat<Traits>::addVectorOperator(
182
    Operator const& preOp,
183
184
    TreePath path,
    double* factor, double* estFactor)
185
{
186
  static_assert( Concepts::PreTreePath<TreePath>,
187
188
      "path must be a valid treepath, or an integer/index-constant");

189
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
190

191
  auto op = makeGridOperator(preOp, globalBasis->gridView());
192
  auto localAssembler = makeLocalAssemblerPtr<Element>(std::move(op), i);
193

194
  rhsOperators[i].element.push_back({localAssembler, factor, estFactor});
195
  rhsOperators[i].changing = true;
196
}
197

198

199
template <class Traits>
200
  template <class Operator, class TreePath>
201
void ProblemStat<Traits>::addVectorOperator(
202
    BoundaryType b,
203
    Operator const& preOp,
204
205
    TreePath path,
    double* factor, double* estFactor)
206
{
207
  static_assert( Concepts::PreTreePath<TreePath>,
208
209
      "path must be a valid treepath, or an integer/index-constant");

210
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
211
  using Intersection = typename GridView::Intersection;
212

213
  auto op = makeGridOperator(preOp, globalBasis->gridView());
214
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(std::move(op), i);
215

216
217
  rhsOperators[i].boundary.push_back({localAssembler, factor, estFactor, b});
  rhsOperators[i].changing = true;
218
}
219
220


221
// Adds a Dirichlet boundary condition
222
template <class Traits>
223
  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
224
void ProblemStat<Traits>::
225
addDirichletBC(Predicate const& predicate, RowTreePath row, ColTreePath col, Values const& values)
226
227
{
  static_assert( Concepts::Functor<Predicate, bool(WorldVector)>,
228
    "Function passed to addDirichletBC for `predicate` does not model the Functor<bool(WorldVector)> concept");
229

230
231
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
232

233
  auto valueGridFct = makeGridFunction(values, globalBasis->gridView());
234

235
  using Range = RangeType_t<decltype(i)>;
236
  using BcType = DirichletBC<WorldVector,Range>;
237
238
239
  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
240
}
241

242

243
244
template <class Traits>
void ProblemStat<Traits>::
245
246
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
247
  Dune::Timer t;
248
249
250
251
252
253

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

  solution->compress();
254

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  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());
    }
  }
270

271
272
273
274
275
276
277
278
279
280
281
  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!");
  }
}
282

283

284
285
template <class Traits>
void ProblemStat<Traits>::
286
buildAfterCoarsen(AdaptInfo& /*adaptInfo*/, Flag /*flag*/, bool asmMatrix, bool asmVector)
287
{
288
  Dune::Timer t;
289

290
  Assembler<Traits> assembler(*globalBasis, matrixOperators, rhsOperators, constraints);
291
  assembler.assemble(*systemMatrix, *solution, *rhs, asmMatrix, asmVector);
292

293
294
  msg("buildAfterCoarsen needed ", t.elapsed(), " seconds");
}
295

296

297
298
template <class Traits>
void ProblemStat<Traits>::
299
writeFiles(AdaptInfo& adaptInfo, bool force)
300
{
301
  Dune::Timer t;
302
303
  for (auto writer : filewriter)
    writer->writeFiles(adaptInfo, force);
304
305
  msg("writeFiles needed ", t.elapsed(), " seconds");
}
306

307
} // end namespace AMDiS