ProblemStat.inc.hpp 8.25 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
13
#include <dune/amdis/common/Loops.hpp>
#include <dune/amdis/common/Timer.hpp>
14

15
16
namespace AMDiS {

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

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

41
    componentGrids.resize(nComponents, grid.get());
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->getGlobalBasis();
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
79
80
81
82
  if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
    solution = adoptProblem->getSolution();
    rhs = adoptProblem->getRhs();
    systemMatrix = adoptProblem->getSystemMatrix();
  }
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
95
    if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
      test_exit(!linearSolver, "solver already created\n");
      linearSolver = adoptProblem->getSolver();
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
116
117
118
119
120
121
122
{
  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;

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


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& op,
135
136
    RowTreePath row, ColTreePath col,
    double* factor, double* estFactor)
137
{
138
139
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
140

141
  auto localAssembler = makeLocalAssemblerPtr<Element>(op, i, j);
142

143
  matrixOperators[i][j].element.push_back({localAssembler, factor, estFactor});
144
  matrixOperators[i][j].changing = true;
145
}
146

147

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

160
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(op, i, j);
161

162
163
  matrixOperators[i][j].boundary.push_back({localAssembler, factor, estFactor, b});
  matrixOperators[i][j].changing = true;
164
}
165
166


167
template <class Traits>
168
  template <class Operator, class TreePath>
169
void ProblemStat<Traits>::addVectorOperator(
170
    Operator const& op,
171
172
    TreePath path,
    double* factor, double* estFactor)
173
{
174
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
175

176
  auto localAssembler = makeLocalAssemblerPtr<Element>(op, i);
177

178
  rhsOperators[i].element.push_back({localAssembler, factor, estFactor});
179
  rhsOperators[i].changing = true;
180
}
181

182

183
template <class Traits>
184
  template <class Operator, class TreePath>
185
void ProblemStat<Traits>::addVectorOperator(
186
    BoundaryType b,
187
    Operator const& op,
188
189
    TreePath path,
    double* factor, double* estFactor)
190
{
191
  auto i = child(globalBasis->localView().tree(), makeTreePath(path));
192
  using Intersection = typename GridView::Intersection;
193

194
  auto localAssembler = makeLocalAssemblerPtr<Intersection>(op, i);
195

196
197
  rhsOperators[i].boundary.push_back({localAssembler, factor, estFactor, b});
  rhsOperators[i].changing = true;
198
}
199
200


201
// Adds a Dirichlet boundary condition
202
template <class Traits>
203
  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
204
void ProblemStat<Traits>::
205
addDirichletBC(Predicate const& predicate, RowTreePath row, ColTreePath col, Values const& values)
206
207
208
{
  static_assert( Concepts::Functor<Predicate, bool(WorldVector)>,
    "Function passed to addDirichletBC for predicate does not model the Functor<bool(WorldVector)> concept");
209

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

213
214
  auto i = child(globalBasis->localView().tree(), makeTreePath(row));
  auto j = child(globalBasis->localView().tree(), makeTreePath(col));
215

216
217
  using Range = decltype(values(std::declval<WorldVector>()));
  using BcType = DirichletBC<WorldVector,Range>;
218

219
220
  auto bc = std::make_shared<BcType>(predicate, values);
  constraints[i][i].push_back(bc);
221
}
222

223

224
225
template <class Traits>
void ProblemStat<Traits>::
226
227
228
229
230
231
232
233
234
solve(AdaptInfo& adaptInfo, bool createMatrixData, bool storeMatrixData)
{
  Timer t;

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

  solution->compress();
235
236
237
238
239

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

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

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

268

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

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

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

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

284

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

295
} // end namespace AMDiS