ProblemStat.inc.hpp 10.2 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
#include <dune/amdis/FileWriter.hpp>
8
9
#include <dune/amdis/common/Loops.hpp>
#include <dune/amdis/common/Timer.hpp>
10

11
12
namespace AMDiS {

13
14
template <class Traits>
void ProblemStat<Traits>::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
109
template <class Traits>
void ProblemStat<Traits>::createFileWriter()
110
111
112
113
114
115
116
117
118
{
  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;

119
    auto writer = makeFileWriterPtr<Traits>(componentName, globalBasis, treePath, solution);
120
121
122
123
124
    filewriter.push_back(writer);
  });
}


125
// add matrix/vector operator terms
126

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

140

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

154

155
template <class Traits>
156
  template <class RowTreePath, class ColTreePath>
157
void ProblemStat<Traits>::addMatrixOperator(
158
159
160
161
    BoundaryType b,
    IntersectionOperator const& op,
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
162
{
163
164
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
165
166
  matrixOperators[i][j].boundary.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor, b});
  matrixOperators[i][j].changing = true;
167
}
168

169
#if 0
170
171
template <class Traits>
void ProblemStat<Traits>::
172
addMatrixOperator(std::map< std::pair<int,int>, ElementOperator> ops)
173
174
175
176
{
  for (auto op : ops){
    int r = op.first.first;
    int c = op.first.second;
177
178
    matrixOperators[r][c].element.push_back({std::make_shared<ElementOperator>(op.second)});
    matrixOperators[r][c].changing = true;
179
  }
180
}
181

182
183
template <class Traits>
void ProblemStat<Traits>::
184
addMatrixOperator(std::map< std::pair<int,int>, std::pair<ElementOperator,bool> > ops)
185
186
187
188
{
  for (auto op : ops) {
    int r = op.first.first;
    int c = op.first.second;
189
190
    matrixOperators[r][c].element.push_back({std::make_shared<ElementOperator>(op.second.first)});
    matrixOperators[r][c].changing = matrixOperators[r][c].changing || op.second.second;
191
  }
192
}
193
#endif
194
195


196
template <class Traits>
197
  template <class TreePath>
198
void ProblemStat<Traits>::addVectorOperator(
199
200
201
    ElementOperator const& op,
    TreePath path,
    double* factor, double* estFactor)
202
{
203
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
204
205
  rhsOperators[i].element.push_back({std::make_shared<ElementOperator>(op), factor, estFactor});
  rhsOperators[i].changing = true;
206
207
}

208

209
template <class Traits>
210
  template <class TreePath>
211
void ProblemStat<Traits>::addVectorOperator(
212
213
214
    IntersectionOperator const& op,
    TreePath path,
    double* factor, double* estFactor)
215
{
216
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
217
218
  rhsOperators[i].intersection.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor});
  rhsOperators[i].changing = true;
219
}
220

221

222
template <class Traits>
223
  template <class TreePath>
224
void ProblemStat<Traits>::addVectorOperator(
225
226
227
228
    BoundaryType b,
    IntersectionOperator const& op,
    TreePath path,
    double* factor, double* estFactor)
229
{
230
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
231
232
  rhsOperators[i].boundary.push_back({std::make_shared<IntersectionOperator>(op), factor, estFactor, b});
  rhsOperators[i].changing = true;
233
}
234

235
#if 0
236
237
template <class Traits>
void ProblemStat<Traits>::
238
addVectorOperator(std::map< int, ElementOperator> ops)
239
240
{
  for (auto op : ops) {
241
242
    rhsOperators[op.first].element.push_back({std::make_shared<ElementOperator>(op.second)});
    rhsOperators[op.first].changing = true;
243
244
  }
}
245

246
247
template <class Traits>
void ProblemStat<Traits>::
248
addVectorOperator(std::map< int, std::pair<ElementOperator, bool> > ops)
249
250
{
  for (auto op : ops) {
251
252
    rhsOperators[op.first].element.push_back({std::make_shared<ElementOperator>(op.second.first)});
    rhsOperators[op.first].changing = rhsOperators[op.first].changing || op.second.second;
253
  }
254
}
255
#endif
256
257


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

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

270
271
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
272

273
274
  using Range = decltype(values(std::declval<WorldVector>()));
  using BcType = DirichletBC<WorldVector,Range>;
275

276
277
  auto bc = std::make_shared<BcType>(predicate, values);
  constraints[i][i].push_back(bc);
278
}
279

280

281
282
template <class Traits>
void ProblemStat<Traits>::
283
284
285
286
287
288
289
290
291
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
  Timer t;

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

  solution->compress();
292
293
294
295
296

  msg("matrix: ",num_rows(systemMatrix->getMatrix()), " x ", num_cols(systemMatrix->getMatrix()));
  msg("x: ",num_rows(solution->getVector()));
  msg("rhs: ",num_rows(rhs->getVector()));

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  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());
    }
  }
312

313
314
315
316
317
318
319
320
321
322
323
  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!");
  }
}
324

325

326
327
template <class Traits>
void ProblemStat<Traits>::
328
buildAfterCoarsen(AdaptInfo& /*adaptInfo*/, Flag /*flag*/, bool asmMatrix, bool asmVector)
329
330
331
{
  Timer t;

332
  Assembler<Traits> assembler(*globalBasis, matrixOperators, rhsOperators, constraints);
333

334
335
336
  auto gv = leafGridView();
  assembler.update(gv);
  assembler.assemble(*systemMatrix, *solution, *rhs, asmMatrix, asmVector);
337

338
339
  msg("buildAfterCoarsen needed ", t.elapsed(), " seconds");
}
340

341

342
343
template <class Traits>
void ProblemStat<Traits>::
344
writeFiles(AdaptInfo& adaptInfo, bool force)
345
346
{
  Timer t;
347
348
  for (auto writer : filewriter)
    writer->writeFiles(adaptInfo, force);
349
350
  msg("writeFiles needed ", t.elapsed(), " seconds");
}
351

352
} // end namespace AMDiS