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

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

7
#include <dune/common/hybridutilities.hh>
8
#include <dune/common/timer.hh>
9
#include <dune/functions/functionspacebases/subspacebasis.hh>
10
#include <dune/grid/common/capabilities.hh>
11
12
#include <dune/typetree/childextraction.hh>

13
#include <amdis/AdaptInfo.hpp>
14
#include <amdis/BackupRestore.hpp>
15
#include <amdis/Assembler.hpp>
16
#include <amdis/GridFunctionOperator.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
17
#include <amdis/io/FileWriterCreator.hpp>
18
#include <amdis/linearalgebra/SymmetryStructure.hpp>
19

20
21
namespace AMDiS {

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

39
40
41
42
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_MESH) ||
        adoptFlag.isSet(INIT_SYSTEM) ||
        adoptFlag.isSet(INIT_FE_SPACE))) {
43
      adoptGrid(adoptProblem->grid_, adoptProblem->boundaryManager_);
44
    }
45
  }
46

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

50
51
52
  if (initFlag.isSet(INIT_MESH)) {
    int globalRefinements = 0;
    Parameters::get(gridName_ + "->global refinements", globalRefinements);
53
    if (globalRefinements > 0)
54
      grid_->globalRefine(globalRefinements);
55
56
57
58

    bool loadBalance = false;
    Parameters::get(gridName_ + "->load balance", loadBalance);
    if (loadBalance)
59
      loadBalance = grid_->loadBalance();
60

61
    if (globalBasis_ && (globalRefinements > 0 || loadBalance))
62
      globalBasis_->update(globalBasis_->gridView());
63
  }
64

65
  // create fespace
66
  if (globalBasis_) {
67
    warning("globalBasis already created");
68
69
70
71
  }
  else {
    if (initFlag.isSet(INIT_FE_SPACE) ||
        (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
72
      createGlobalBasis();
73
    }
74

75
76
    if (adoptProblem &&
        (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
Praetorius, Simon's avatar
Praetorius, Simon committed
77
      adoptGlobalBasis(adoptProblem->globalBasis_);
78
    }
79
  }
80

81
  if (!globalBasis_)
82
    warning("no globalBasis created\n");
83

84
85
86
  // create system
  if (initFlag.isSet(INIT_SYSTEM))
    createMatricesAndVectors();
87

88
  if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Praetorius, Simon's avatar
Praetorius, Simon committed
89
    systemMatrix_ = adoptProblem->systemMatrix_;
90
91
    solution_ = adoptProblem->solution_;
    rhs_ = adoptProblem->rhs_;
Praetorius, Simon's avatar
Praetorius, Simon committed
92
    estimates_ = adoptProblem->estimates_;
93
  }
94

95

96
  // create solver
97
  if (linearSolver_) {
98
99
100
101
102
    warning("solver already created\n");
  }
  else {
    if (initFlag.isSet(INIT_SOLVER))
      createSolver();
103

104
    if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
105
106
      test_exit(!linearSolver_, "solver already created\n");
      linearSolver_ = adoptProblem->linearSolver_;
107
    }
108
109
  }

110
  if (!linearSolver_) {
111
    warning("no solver created\n");
112
113
  }

114
115
116
117
118
  // create marker
    if (initFlag.isSet(INIT_MARKER))
      createMarker();

    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
119
      marker_ = adoptProblem->marker_;
120

121

122
123
124
  // create file writer
  if (initFlag.isSet(INIT_FILEWRITER))
    createFileWriter();
125

126
  solution_->resizeZero();
127
}
128

129

Praetorius, Simon's avatar
Praetorius, Simon committed
130
131
132
133
134
135
136
137
138
template <class Traits>
void ProblemStat<Traits>::
restore(Flag initFlag)
{
  std::string grid_filename = Parameters::get<std::string>(name_ + "->restore->grid").value();
  std::string solution_filename = Parameters::get<std::string>(name_ + "->restore->solution").value();
  test_exit(filesystem::exists(grid_filename), "Restore file '{}' not found.", grid_filename);
  test_exit(filesystem::exists(solution_filename), "Restore file '{}' not found.", solution_filename);

Praetorius, Simon's avatar
Praetorius, Simon committed
139
140
141
  // TODO(SP): implement BAckupRestore independent of wrapped grid
  using HostGrid = typename Grid::HostGrid;

Praetorius, Simon's avatar
Praetorius, Simon committed
142
  // restore grid from file
143
  if (Dune::Capabilities::hasBackupRestoreFacilities<HostGrid>::v)
Praetorius, Simon's avatar
Praetorius, Simon committed
144
    adoptGrid(std::shared_ptr<HostGrid>(Dune::BackupRestoreFacility<HostGrid>::restore(grid_filename)));
Praetorius, Simon's avatar
Praetorius, Simon committed
145
  else
Praetorius, Simon's avatar
Praetorius, Simon committed
146
    adoptGrid(std::shared_ptr<HostGrid>(BackupRestoreByGridFactory<HostGrid>::restore(grid_filename)));
Praetorius, Simon's avatar
Praetorius, Simon committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

  // create fespace
  if (initFlag.isSet(INIT_FE_SPACE) || initFlag.isSet(INIT_SYSTEM))
    createGlobalBasis();

  // create system
  if (initFlag.isSet(INIT_SYSTEM))
    createMatricesAndVectors();

  // create solver
  if (linearSolver_)
    warning("solver already created\n");
  else if (initFlag.isSet(INIT_SOLVER))
    createSolver();

  // create marker
  if (initFlag.isSet(INIT_MARKER))
    createMarker();

  // create file writer
  if (initFlag.isSet(INIT_FILEWRITER))
    createFileWriter();

170
  solution_->resize(sizeInfo(*globalBasis_));
Praetorius, Simon's avatar
Praetorius, Simon committed
171
172
173
174
  solution_->restore(solution_filename);
}


Praetorius, Simon's avatar
Praetorius, Simon committed
175
176
177
178
template <class Traits>
void ProblemStat<Traits>::createGrid()
{
  Parameters::get(name_ + "->mesh", gridName_);
Praetorius, Simon's avatar
Praetorius, Simon committed
179
180

  MeshCreator<Grid> creator(gridName_);
181
  grid_ = creator.create();
182
183

  Dune::Timer t;
184
185
186
  bool loadBalance = grid_->loadBalance();
  if (loadBalance)
    info(2,"load balance needed {} seconds", t.elapsed());
187

188
  boundaryManager_ = std::make_shared<BoundaryManager<Grid>>(grid_);
189
190
  if (!creator.boundaryIds().empty())
    boundaryManager_->setBoundaryIds(creator.boundaryIds());
Praetorius, Simon's avatar
Praetorius, Simon committed
191

192
193
194
195
196
197
198
  info(3,"Create grid:");
  info(3,"#elements = {}"   , grid_->size(0));
  info(3,"#faces/edges = {}", grid_->size(1));
  info(3,"#vertices = {}"   , grid_->size(dim));
  info(3,"overlap-size = {}", grid_->leafGridView().overlapSize(0));
  info(3,"ghost-size = {}"  , grid_->leafGridView().ghostSize(0));
  info(3,"");
Praetorius, Simon's avatar
Praetorius, Simon committed
199
200
201
202
203
204
205
206
207
208
209
}


template <class T, class GV>
using HasCreate = decltype(T::create(std::declval<GV>()));


template <class Traits>
void ProblemStat<Traits>::createGlobalBasis()
{
  createGlobalBasisImpl(Dune::Std::is_detected<HasCreate,Traits,GridView>{});
210
  initGlobalBasis();
Praetorius, Simon's avatar
Praetorius, Simon committed
211
212
213
214
215
216
217
}


template <class Traits>
void ProblemStat<Traits>::createGlobalBasisImpl(std::true_type)
{
  assert( bool(grid_) );
Praetorius, Simon's avatar
Praetorius, Simon committed
218
  static_assert(std::is_same<GridView, typename Grid::LeafGridView>::value, "");
219
220
  auto basis = Traits::create(name_, grid_->leafGridView());
  globalBasis_ = std::make_shared<GlobalBasis>(std::move(basis));
Praetorius, Simon's avatar
Praetorius, Simon committed
221
222
223
224
225
226
227
228
229
230
231
}


template <class Traits>
void ProblemStat<Traits>::createGlobalBasisImpl(std::false_type)
{
  error_exit("Cannot create GlobalBasis from type. Pass a BasisCreator instead!");
}


template <class Traits>
232
void ProblemStat<Traits>::initGlobalBasis()
Praetorius, Simon's avatar
Praetorius, Simon committed
233
{
234
  boundaryConditions_.init(*globalBasis_, *globalBasis_);
Praetorius, Simon's avatar
Praetorius, Simon committed
235
236
237
238
239
240
}


template <class Traits>
void ProblemStat<Traits>::createMatricesAndVectors()
{
241
  systemMatrix_ = std::make_shared<SystemMatrix>(globalBasis_, globalBasis_);
Praetorius, Simon's avatar
Praetorius, Simon committed
242
  solution_ = std::make_shared<SolutionVector>(globalBasis_);
243
  rhs_ = std::make_shared<SystemVector>(globalBasis_);
244

Praetorius, Simon's avatar
Praetorius, Simon committed
245
  auto localView = globalBasis_->localView();
246
  for_each_node(localView.tree(), [&,this](auto const& node, auto treePath) -> void
Praetorius, Simon's avatar
Praetorius, Simon committed
247
248
249
250
251
252
253
254
255
256
257
258
259
  {
    std::string i = to_string(treePath);
    estimates_[i].resize(globalBasis_->gridView().indexSet().size(0));
    for (std::size_t j = 0; j < estimates_[i].size(); j++)
      estimates_[i][j] = 0.0; // TODO: Remove when estimate() is implemented
  });
}


template <class Traits>
void ProblemStat<Traits>::createSolver()
{
  std::string solverName = "default";
260
  Parameters::get(name_ + "->solver", solverName);
Praetorius, Simon's avatar
Praetorius, Simon committed
261
262

  auto solverCreator
Praetorius, Simon's avatar
Praetorius, Simon committed
263
    = named(CreatorMap<LinearSolver>::getCreator(solverName, name_ + "->solver"));
Praetorius, Simon's avatar
Praetorius, Simon committed
264

265
  linearSolver_ = solverCreator->createWithString(name_ + "->solver");
Praetorius, Simon's avatar
Praetorius, Simon committed
266
267
268
}


269
270
271
template <class Traits>
void ProblemStat<Traits>::createMarker()
{
Praetorius, Simon's avatar
Praetorius, Simon committed
272
  marker_.clear();
273
  auto localView = globalBasis_->localView();
274
  for_each_node(localView.tree(), [&,this](auto const& node, auto treePath) -> void
275
  {
276
    std::string componentName = name_ + "->marker[" + to_string(treePath) + "]";
277
278
279
280

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

281
    std::string tp = to_string(treePath);
Praetorius, Simon's avatar
Praetorius, Simon committed
282
283
    auto newMarker
      = EstimatorMarker<Grid>::createMarker(componentName, tp, estimates_[tp], grid_);
284
    assert(bool(newMarker));
285
    this->addMarker(std::move(newMarker));
286
287
288
289
  });
}


290
291
template <class Traits>
void ProblemStat<Traits>::createFileWriter()
292
{
Praetorius, Simon's avatar
Praetorius, Simon committed
293
  FileWriterCreator<SolutionVector> creator(solution_, boundaryManager_);
Praetorius, Simon's avatar
Praetorius, Simon committed
294

Praetorius, Simon's avatar
Praetorius, Simon committed
295
  filewriter_.clear();
296
  auto localView = globalBasis_->localView();
Praetorius, Simon's avatar
Praetorius, Simon committed
297
  for_each_node(localView.tree(), [&](auto const& /*node*/, auto treePath) -> void
298
  {
299
    std::string componentName = name_ + "->output[" + to_string(treePath) + "]";
Praetorius, Simon's avatar
Praetorius, Simon committed
300
301
302
303
304
305
306
    auto format = Parameters::get<std::vector<std::string>>(componentName + "->format");

    if (!format && to_string(treePath).empty()) {
      // alternative for root treepath
      componentName = name_ + "->output";
      format = Parameters::get<std::vector<std::string>>(componentName + "->format");
    }
307

Praetorius, Simon's avatar
Praetorius, Simon committed
308
    if (!format)
309
310
      return;

Praetorius, Simon's avatar
Praetorius, Simon committed
311
312
313
314
315
    for (std::string const& type : format.value()) {
      auto writer = creator.create(type, componentName, treePath);
      if (writer)
        filewriter_.push_back(std::move(writer));
    }
316
317
318
319
  });
}


320
// Adds a Dirichlet boundary condition
321
template <class Traits>
322
  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
323
void ProblemStat<Traits>::
324
addDirichletBC(Predicate const& predicate, RowTreePath row, ColTreePath col, Values const& values)
325
326
{
  static_assert( Concepts::Functor<Predicate, bool(WorldVector)>,
327
    "Function passed to addDirichletBC for `predicate` does not model the Functor<bool(WorldVector)> concept");
328

329
330
331
  auto localView = globalBasis_->localView();
  auto i = child(localView.tree(), makeTreePath(row));
  auto j = child(localView.tree(), makeTreePath(col));
332
333
  auto rowBasis = Dune::Functions::subspaceBasis(*globalBasis_, makeTreePath(row));
  auto colBasis = Dune::Functions::subspaceBasis(*globalBasis_, makeTreePath(col));
334

335
  auto valueGridFct = makeGridFunction(values, this->gridView());
336

337
338
339
  auto bc = makeDirichletBC<SystemMatrix, SolutionVector, SystemVector>(
              std::move(rowBasis), std::move(colBasis), {predicate}, valueGridFct);
  boundaryConditions_[i][j].push_back(makeUniquePtr(std::move(bc)));
340
341
342
343
344
345
346
347
348
349
350
351
}


// Adds a Dirichlet boundary condition
template <class Traits>
  template <class RowTreePath, class ColTreePath, class Values>
void ProblemStat<Traits>::
addDirichletBC(BoundaryType id, RowTreePath row, ColTreePath col, Values const& values)
{
  auto localView = globalBasis_->localView();
  auto i = child(localView.tree(), makeTreePath(row));
  auto j = child(localView.tree(), makeTreePath(col));
352
353
  auto rowBasis = Dune::Functions::subspaceBasis(*globalBasis_, makeTreePath(row));
  auto colBasis = Dune::Functions::subspaceBasis(*globalBasis_, makeTreePath(col));
354

355
  auto valueGridFct = makeGridFunction(values, this->gridView());
356

357
358
359
  auto bc = makeDirichletBC<SystemMatrix, SolutionVector, SystemVector>(
              std::move(rowBasis), std::move(colBasis), {*boundaryManager_, id}, valueGridFct);
  boundaryConditions_[i][j].push_back(makeUniquePtr(std::move(bc)));
360
361
362
363
364
365
366
367
}


template <class Traits>
void ProblemStat<Traits>::
addPeriodicBC(BoundaryType id, WorldMatrix const& matrix, WorldVector const& vector)
{
  auto localView = globalBasis_->localView();
368
369
370
371
  auto basis = Dune::Functions::subspaceBasis(*globalBasis_, treepath());
  auto bc = makePeriodicBC<SystemMatrix, SolutionVector, SystemVector>(
              std::move(basis), {*boundaryManager_, id}, {matrix, vector});
  boundaryConditions_[localView.tree()][localView.tree()].push_back(makeUniquePtr(std::move(bc)));
372
}
373

374

375
376
template <class Traits>
void ProblemStat<Traits>::
377
378
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
379
  Dune::Timer t;
380

381
  SolverInfo solverInfo(name_ + "->solver");
382
383
384
  solverInfo.setCreateMatrixData(createMatrixData);
  solverInfo.setStoreMatrixData(storeMatrixData);

385
  solution_->resize();
Praetorius, Simon's avatar
Praetorius, Simon committed
386
  linearSolver_->solve(*systemMatrix_, *solution_, *rhs_, solverInfo);
387

Praetorius, Simon's avatar
Praetorius, Simon committed
388
  if (solverInfo.info() > 0) {
389
    msg("solution of discrete system needed {} seconds", t.elapsed());
390

Praetorius, Simon's avatar
Praetorius, Simon committed
391
392
    if (solverInfo.absResidual() >= 0.0) {
      if (solverInfo.relResidual() >= 0.0)
393
        msg("Residual norm: ||b-Ax|| = {}, ||b-Ax||/||b|| = {}",
Praetorius, Simon's avatar
Praetorius, Simon committed
394
          solverInfo.absResidual(), solverInfo.relResidual());
395
      else
Praetorius, Simon's avatar
Praetorius, Simon committed
396
        msg("Residual norm: ||b-Ax|| = {}", solverInfo.absResidual());
397
398
    }
  }
399

400
  test_exit(!solverInfo.doBreak() && !solverInfo.error(), "Could not solver the linear system!");
401
}
402

403

404
template <class Traits>
Praetorius, Simon's avatar
Praetorius, Simon committed
405
406
Flag ProblemStat<Traits>::
markElements(AdaptInfo& adaptInfo)
407
408
409
410
{
  Dune::Timer t;

  Flag markFlag = 0;
411
  for (auto& currentMarker : marker_)
412
    markFlag |= currentMarker.second->markGrid(adaptInfo);
413

414
  msg("markElements needed {} seconds", t.elapsed());
415
416
417
418
419

  return markFlag;
}


420
421
422
423
424
425
template <class Traits>
Flag ProblemStat<Traits>::
globalCoarsen(int n)
{
  Dune::Timer t;
  bool adapted = false;
426
  // TODO(FM): Find a less expensive alternative to the loop adaption
427
  for (int i = 0; i < n; ++i) {
428
    // mark all entities for coarsening
429
430
431
    for (const auto& element : elements(grid_->leafGridView()))
      grid_->mark(-1, element);

432
433
434
435
436
437
438
    bool adaptedInLoop = grid_->preAdapt();
    adaptedInLoop |= grid_->adapt();
    grid_->postAdapt();
    if (!adaptedInLoop)
      break;
    else
      adapted = true;
439
440
441
442
443
444
445
446
447
  }

  msg("globalCoarsen needed {} seconds", t.elapsed());
  return adapted ? MESH_ADAPTED : Flag(0);
}


// grid has globalRefine(int, AdaptDataHandleInterface&)
template <class G>
448
449
using HasGlobalRefineADHI = decltype(
  std::declval<G>().globalRefine(1,std::declval<typename G::ADHI&>()));
450
451
452

template <class Traits>
Flag ProblemStat<Traits>::
453
globalRefine(int n)
454
455
{
  Dune::Timer t;
456
457
458
459
  if constexpr (Dune::Std::is_detected<HasGlobalRefineADHI, Grid>::value)
    grid_->globalRefine(n, globalBasis_->globalRefineCallback());
  else
    grid_->globalRefine(n);
460
461

  msg("globalRefine needed {} seconds", t.elapsed());
462
  return n > 0 ? MESH_ADAPTED : Flag(0);
463
464
465
}


466
467
468
469
470
471
template <class Traits>
Flag ProblemStat<Traits>::
adaptGrid(AdaptInfo& adaptInfo)
{
  Dune::Timer t;

472
473
474
  bool adapted = grid_->preAdapt();
  adapted |= grid_->adapt();
  grid_->postAdapt();
475

476
  msg("adaptGrid needed {} seconds", t.elapsed());
477
  return adapted ? MESH_ADAPTED : Flag(0);
478
479
480
}


481
482
template <class Traits>
void ProblemStat<Traits>::
483
buildAfterAdapt(AdaptInfo& /*adaptInfo*/, Flag /*flag*/, bool asmMatrix, bool asmVector)
484
{
485
  Dune::Timer t;
486
  Dune::Timer t2;
487

488
  auto localView = globalBasis_->localView();
489
490
  for_each_node(localView.tree(), [&,this](auto const& rowNode, auto rowTp) -> void {
    for_each_node(localView.tree(), [&,this](auto const& colNode, auto colTp) -> void {
491
492
      for (auto bc : boundaryConditions_[rowNode][colNode])
        bc->init();
493
494
    });
  });
495
496
497
498
499
500
501
502

  t2.reset();

  // 1. init matrix and rhs vector and initialize dirichlet boundary conditions
  std::string symmetryStr = "unknown";
  Parameters::get(name_ + "->symmetry", symmetryStr);

  systemMatrix_->init(symmetryStructure(symmetryStr));
503
  rhs_->init(sizeInfo(*globalBasis_), asmVector);
504
505
506
507
508
509

  // statistic about system size
  if (Environment::mpiSize() > 1)
    msg("{} local DOFs, {} global DOFs", rhs_->localSize(), rhs_->globalSize());
  else
    msg("{} local DOFs", rhs_->localSize());
510
511

  // 2. traverse grid and assemble operators on the elements
Praetorius, Simon's avatar
Praetorius, Simon committed
512
  for (auto const& element : elements(gridView(), PartitionSet{})) {
513
    localView.bind(element);
514
515

    if (asmMatrix)
516
      systemMatrix_->assemble(localView, localView);
517
    if (asmVector)
518
      rhs_->assemble(localView);
519

520
    localView.unbind();
521
522
523
  }

  // 3. finish matrix insertion and apply dirichlet boundary conditions
524
525
  systemMatrix_->finish();
  rhs_->finish();
526

527
528
529
  info(2,"  assemble operators needed {} seconds", t2.elapsed());
  t2.reset();

530
  solution_->resize(sizeInfo(*globalBasis_));
531
532
  for_each_node(localView.tree(), [&,this](auto const& rowNode, auto row_tp) -> void {
    for_each_node(localView.tree(), [&,this](auto const& colNode, auto col_tp) -> void {
533
      // finish boundary condition
534
535
      for (auto bc : boundaryConditions_[rowNode][colNode])
        bc->apply(*systemMatrix_, *solution_, *rhs_);
536
537
    });
  });
538

539
540
  info(2,"  assemble boundary conditions needed {} seconds", t2.elapsed());

541
  msg("fill-in of assembled matrix: {}", systemMatrix_->nnz());
542
  msg("assemble needed {} seconds", t.elapsed());
543
}
544

545

546
547
template <class Traits>
void ProblemStat<Traits>::
548
writeFiles(AdaptInfo& adaptInfo, bool force)
549
{
550
  Dune::Timer t;
551
  for (auto writer : filewriter_)
Praetorius, Simon's avatar
Praetorius, Simon committed
552
    writer->write(adaptInfo, force);
553
  msg("writeFiles needed {} seconds", t.elapsed());
554
}
555

556
} // end namespace AMDiS