ProblemStat.inc.hpp 10.3 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
// #include <amdis/Estimator.hpp>
17

18
19
namespace AMDiS {

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

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

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

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


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

65
66
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
67
68
      globalBasis_ = adoptProblem->globalBasis_;
      initGlobalBasis(*globalBasis_);
69
    }
70
  }
71

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


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

80
  if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
81
    solution_ = adoptProblem->solution_;
82
    estimates_ = adoptProblem->estimates_;
83
84
    rhs_ = adoptProblem->rhs_;
    systemMatrix_ = adoptProblem->systemMatrix_;
85
  }
86

87

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

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

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

106
107
108
109
110
  // create marker
    if (initFlag.isSet(INIT_MARKER))
      createMarker();

    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
111
      marker_ = adoptProblem->marker_;
112

113

114
115
116
  // create file writer
  if (initFlag.isSet(INIT_FILEWRITER))
    createFileWriter();
117

118
  solution_->compress();
119
}
120

121

122
123
124
template <class Traits>
void ProblemStat<Traits>::createMarker()
{
125
  AMDiS::forEachNode_(localView_->tree(), [&,this](auto const& node, auto treePath)
126
  {
127
    std::string componentName = name_ + "->marker[" + to_string(treePath) + "]";
128
129
130
131

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

132
133
    std::string tp = to_string(treePath);
    auto newMarker = Marker<Traits>::createMarker(componentName, tp, estimates_[tp], grid_);
134
    if (newMarker)
135
136
      marker_.push_back(std::move(newMarker));

137
138
    // If there is more than one marker, and all components are defined
    // on the same grid, the maximum marking has to be enabled.
139
140
141
    // TODO: needs to be checked!
    if (marker_.size() > 1)
      marker_.back()->setMaximumMarking(true);
142
143
144
145
  });
}


146
147
template <class Traits>
void ProblemStat<Traits>::createFileWriter()
148
{
149
  AMDiS::forEachNode_(localView_->tree(), [&,this](auto const& node, auto treePath)
150
  {
151
    std::string componentName = name_ + "->output[" + to_string(treePath) + "]";
152
153
154
155

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

156
    auto writer = makeFileWriterPtr(componentName, this->getSolution(treePath));
157
    filewriter_.push_back(std::move(writer));
158
159
160
161
  });
}


162
// add matrix/vector operator terms
163

164
template <class Traits>
165
  template <class Operator, class RowTreePath, class ColTreePath>
166
void ProblemStat<Traits>::addMatrixOperator(
167
    Operator const& preOp,
168
169
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
170
{
171
  static_assert( Concepts::PreTreePath<RowTreePath>,
172
      "row must be a valid treepath, or an integer/index-constant");
173
  static_assert( Concepts::PreTreePath<ColTreePath>,
174
175
      "col must be a valid treepath, or an integer/index-constant");

176
177
  auto i = child(localView_->tree(), makeTreePath(row));
  auto j = child(localView_->tree(), makeTreePath(col));
178

179
  auto op = makeLocalOperator(preOp, globalBasis_->gridView());
180
  auto localAssembler = makeLocalAssemblerPtr<Element>(std::move(op), i, j);
181

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

186

187
template <class Traits>
188
  template <class Operator, class RowTreePath, class ColTreePath>
189
void ProblemStat<Traits>::addMatrixOperator(
190
    BoundaryType b,
191
    Operator const& preOp,
192
193
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
194
{
195
  static_assert( Concepts::PreTreePath<RowTreePath>,
196
      "row must be a valid treepath, or an integer/index-constant");
197
  static_assert( Concepts::PreTreePath<ColTreePath>,
198
199
      "col must be a valid treepath, or an integer/index-constant");

200
201
  auto i = child(localView_->tree(), makeTreePath(row));
  auto j = child(localView_->tree(), makeTreePath(col));
202
  using Intersection = typename GridView::Intersection;
203

204
  auto op = makeLocalOperator(preOp, globalBasis_->gridView());
205
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(std::move(op), i, j);
206

207
208
  matrixOperators_[i][j].boundary.push_back({localAssembler, factor, estFactor, b});
  matrixOperators_[i][j].changing = true;
209
}
210
211


212
template <class Traits>
213
  template <class Operator, class TreePath>
214
void ProblemStat<Traits>::addVectorOperator(
215
    Operator const& preOp,
216
217
    TreePath path,
    double* factor, double* estFactor)
218
{
219
  static_assert( Concepts::PreTreePath<TreePath>,
220
221
      "path must be a valid treepath, or an integer/index-constant");

222
  auto i = child(localView_->tree(), makeTreePath(path));
223

224
  auto op = makeLocalOperator(preOp, globalBasis_->gridView());
225
  auto localAssembler = makeLocalAssemblerPtr<Element>(std::move(op), i);
226

227
228
  rhsOperators_[i].element.push_back({localAssembler, factor, estFactor});
  rhsOperators_[i].changing = true;
229
}
230

231

232
template <class Traits>
233
  template <class Operator, class TreePath>
234
void ProblemStat<Traits>::addVectorOperator(
235
    BoundaryType b,
236
    Operator const& preOp,
237
238
    TreePath path,
    double* factor, double* estFactor)
239
{
240
  static_assert( Concepts::PreTreePath<TreePath>,
241
242
      "path must be a valid treepath, or an integer/index-constant");

243
  auto i = child(localView_->tree(), makeTreePath(path));
244
  using Intersection = typename GridView::Intersection;
245

246
  auto op = makeLocalOperator(preOp, globalBasis_->gridView());
247
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(std::move(op), i);
248

249
250
  rhsOperators_[i].boundary.push_back({localAssembler, factor, estFactor, b});
  rhsOperators_[i].changing = true;
251
}
252
253


254
// Adds a Dirichlet boundary condition
255
template <class Traits>
256
  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
257
void ProblemStat<Traits>::
258
addDirichletBC(Predicate const& predicate, RowTreePath row, ColTreePath col, Values const& values)
259
260
{
  static_assert( Concepts::Functor<Predicate, bool(WorldVector)>,
261
    "Function passed to addDirichletBC for `predicate` does not model the Functor<bool(WorldVector)> concept");
262

263
264
  auto i = child(localView_->tree(), makeTreePath(row));
  auto j = child(localView_->tree(), makeTreePath(col));
265

266
  auto valueGridFct = makeGridFunction(values, globalBasis_->gridView());
267

268
  using Range = RangeType_t<decltype(i)>;
269
  using BcType = DirichletBC<WorldVector,Range>;
270
  auto bc = std::make_shared<BcType>(predicate, valueGridFct);
271
  constraints_[i][j].push_back(bc);
272
  // TODO: make DirichletBC an abstract class and add specialization with gridfunction type
273
}
274

275

276
277
template <class Traits>
void ProblemStat<Traits>::
278
279
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
280
  Dune::Timer t;
281

282
  SolverInfo solverInfo(name_ + "->solver");
283
284
285
  solverInfo.setCreateMatrixData(createMatrixData);
  solverInfo.setStoreMatrixData(storeMatrixData);

286
  solution_->compress();
287

288
  linearSolver_->solve(systemMatrix_->getMatrix(), solution_->getVector(), rhs_->getVector(),
289
290
291
292
293
294
295
296
297
298
299
300
301
                      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());
    }
  }
302

303
304
305
306
307
308
309
310
311
312
313
  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!");
  }
}
314

315

316
317
318
319
320
321
template <class Traits>
Flag ProblemStat<Traits>::markElements(AdaptInfo& adaptInfo)
{
  Dune::Timer t;

  Flag markFlag = 0;
322
  for (auto& currentMarker : marker_)
323
    markFlag |= currentMarker->markGrid(adaptInfo);
324
325
326
327
328
329
330

  msg("markElements needed ", t.elapsed(), " seconds");

  return markFlag;
}


331
332
template <class Traits>
void ProblemStat<Traits>::
333
buildAfterCoarsen(AdaptInfo& /*adaptInfo*/, Flag /*flag*/, bool asmMatrix, bool asmVector)
334
{
335
  Dune::Timer t;
336

337
  Assembler<Traits> assembler(*globalBasis_, matrixOperators_, rhsOperators_, constraints_);
338
  auto gv = grid_->leafGridView();
339
340
  globalBasis_->update(gv);
  assembler.assemble(*systemMatrix_, *solution_, *rhs_, asmMatrix, asmVector);
341

342
343
  msg("buildAfterCoarsen needed ", t.elapsed(), " seconds");
}
344

345

346
347
template <class Traits>
void ProblemStat<Traits>::
348
writeFiles(AdaptInfo& adaptInfo, bool force)
349
{
350
  Dune::Timer t;
351
  for (auto writer : filewriter_)
352
    writer->writeFiles(adaptInfo, force);
353
354
  msg("writeFiles needed ", t.elapsed(), " seconds");
}
355

356

357
} // end namespace AMDiS