ProblemStat.inc.hpp 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#pragma once

#include <dune/istl/matrixindexset.hh>

#include "Timer.hpp"

namespace AMDiS
{
    template <class Parameters>
    void ProblemStat<Parameters>::initialize(Flag initFlag,
					    ProblemStat* adoptProblem,
					    Flag adoptFlag) 
    {
	// === create meshes ===
	if (mesh) {
	    WARNING("mesh already created\n");
	}
	else {
	    if (initFlag.isSet(CREATE_MESH) ||
		(!adoptFlag.isSet(INIT_MESH) &&
		(initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE)))) {
		createMesh();
	    }

	    if (adoptProblem &&
		(adoptFlag.isSet(INIT_MESH) ||
		adoptFlag.isSet(INIT_SYSTEM) ||
		adoptFlag.isSet(INIT_FE_SPACE))) {
		mesh = adoptProblem->getMesh();
	    }
	    
	    componentMeshes.resize(nComponents, mesh.get());
	}

	if (!mesh) {
	    WARNING("no mesh created\n");
	}
	
	
	// === create fespace ===
	if (feSpaces) {
	    WARNING("feSpaces already created\n");
	}
	else {
	    if (initFlag.isSet(INIT_FE_SPACE) ||
		(initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
		createFeSpaces();
	    }

	    if (adoptProblem &&
		(adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
		feSpaces = adoptProblem->getFeSpaces();
	    }
	}
	
	
	int globalRefinements = 0;
	Parameters::get(meshName + "->global refinements", globalRefinements);
	mesh->globalRefine(globalRefinements);
    }
  
  
    template <class Parameters>
    void ProblemStat<Parameters>::addMatrixOperator(OperatorType& op, 
						    int i, int j,
						    double* factor = NULL, 
						    double* estFactor = NULL)
    {
	// TODO: currently the factors are ignored
	assert( factor == NULL && estFactor == NULL );
	
	if (!systemMatrix[i][j])
	    systemMatrix[i][j] = new DOFMatrix;
	matrixOperators[std::make_pair(i,j)].push_back(std::make_shared<OperatorType>(op));
    }

    
    template <class Parameters>
    void ProblemStat<Parameters>::addVectorOperator(OperatorType& op, 
						    int i,
						    double* factor = NULL, 
						    double* estFactor = NULL)
    {
	// TODO: currently the factors are ignored
	assert( factor == NULL && estFactor == NULL );
	
	if (!rhs[i])
	    rhs[i] = new DOFVector;
	vectorOperators[i].push_back(std::make_shared<OperatorType>(op));
    }

  
  
  /// Adds a Dirichlet boundary condition
  template <class Parameters>
    template <class Predicate, class Values>
  void ProblemStat<Parameters>::addDirichletBC(Predicate const& predicate, 
					       int row, int col, 
					       Values const& values)
  {
    static_assert(Dune::Functions::Concept::isCallable<Predicate, WorldVector>(), 
      "Function passed to addDirichletBC for predicate does not model the Callable<WorldVector> concept");
    
    static_assert(Dune::Functions::Concept::isCallable<Values, WorldVector>(), 
      "Function passed to addDirichletBC for values does not model the Callable<WorldVector> concept");

    TEST_EXIT(row >= 0 && row < nComponents, "row number out of range: " << row << "\n");
    TEST_EXIT(col >= 0 && col < nComponents, "col number out of range: " << col << "\n");
    
    boundaryConditionSet = true;

    using BcType = DirichletBC<WorldVector>;
    std::shared_ptr<BcType> dirichlet = std::make_shared<BcType>(predicate, values);
      
    dirichletBc[std::make_pair(row, col)].push_back( dirichlet );
  }
  
  
  template <class Parameters>
  void ProblemStat<Parameters>::solve(AdaptInfo& adaptInfo, 
				      bool createMatrixData, 
				      bool storeMatrixData)
  {
//     if (!solver) {
//       WARNING("no solver\n");
//       return;
//     }

    Timer t;
    
    // NOTE: just to show the priciple, with initial solution 0
    For<0, nComponents>::loop([this](auto const i) {
	solution[i].resize(getFeSpace<i>().size());
	solution[i] = 0.0;
    });
    
    // Technicality : turn the matrix into a linear operator
    MatrixAdapter<SystemMatrix, SystemVector, SystemVector> op(systemMatrix);

    // Sequential incomplete LU decomposition as the preconditioner
    SeqILU0<SystemMatrix, SystemVector, SystemVector> ilu0(systemMatrix, 1.0);

    // Preconditioned conjugate−gradient solver
    CGSolver<SystemVector> cg(op, ilu0, 1.e-4, 500, 2);

    // storing some statistics
    InverseOperatorResult statistics;

    // solve the linear system
    cg.apply(solution, rhs, statistics);
    
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n",
		  t.elapsed());

    adaptInfo.setSolverIterations(solver->getIterations());
    adaptInfo.setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo.setSolverTolerance(solver->getTolerance());
    adaptInfo.setSolverResidual(solver->getResidual());
  }
  
  
  template <class Parameters>
  void ProblemStat<Parameters>::buildAfterCoarsen(AdaptInfo& /*adaptInfo*/, Flag flag,
						  bool asmMatrix, bool asmVector)
  {
    Timer t;

    For<0, nComponents>::loop([this, asmMatrix, asmVector](auto const _r) 
    {
      static constexpr int rowComponent = int(_r);
      auto const& rowFeSpace = std::get<rowComponent>(feSpaces);
      
      if (asmVector) {
	rhs[rowComponent].resize(meshView.size(dim));
        rhs[rowComponent] = 0.0;
      }

      For<0, nComponents>::loop([this, asmMatrix, asmVector](auto const _c) 
      {
	static constexpr int colComponent = int(_c);
        auto const& colFeSpace = std::get<colComponent>(feSpaces);
	
	auto row_col = std::make_pair(rowComponent, colComponent);
	
        // The DOFMatrix which should be assembled
        DOFMatrix& matrix = systemMatrix[rowComponent][colComponent];
	  
	if (asmMatrix) {
	  // TODO: calc occupationPattern only once for each feSpace
	  MatrixIndexSet occupationPattern;
	  getOccupationPattern(rowFeSpace, colFeSpace, occupationPattern);
	  occupationPattern.exportIdx(matrix);

	  // TODO: only set to zero if not keep the matrix
	  matrix = 0.0;

	  // init boundary condition
	  for (auto bc : dirichletBc) {
	    if (bc.first.first == rowComponent) {
		bc.second->init(bc.first.second == colComponent, 
				rowFeSpace, colFeSpace, &matrix, 
				&rhs[rowComponent], &solution[colComponent]);
	    }
	  }
	  
	  assemble(row_col, rowFeSpace, colFeSpace, &matrix,
		  ((rowComponent == colComponent && asmVector) ? &rhs[rowComponent] : NULL));

	  // finish boundary condition
	  for (auto bc : dirichletBc) {
	    if (bc.first.first == rowComponent) {
		bc.second->finish(bc.first.second == colComponent, 
				  rowFeSpace, colFeSpace, &matrix, 
				  &rhs[rowComponent], &solution[colComponent]);
	    }
	  }
	}
      }
    }
    
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", t.elapsed());
  }
  
  
  
  // Get the occupation pattern
  template <class Parameters>
    template <class RowFeSpace, class ColFeSpace>
  void ProblemStat<Parameters>::getOccupationPattern(const RowFeSpace& rowFeSpace, 
						     const ColFeSpace& colFeSpace, 
						     MatrixIndexSet& nb)
  {
      nb.resize(rowFeSpace.size(), colFeSpace.size());

      // A loop over all elements of the grid
      auto rowView = rowFeSpace.localView();
      auto rowIndexSet = rowFeSpace.localIndexSet();
      
      auto colView = colFeSpace.localView();
      auto colIndexSet = colFeSpace.localIndexSet();

      for (const auto& element : elements(*meshView)) {
	  rowView.bind(element);
	  rowIndexSet.bind(rowView); // TODO: can this be moved out of the loop?
	  
	  colView.bind(element);
	  colIndexSet.bind(colView); // TODO: can this be moved out of the loop?
	  
	  for (size_t i = 0; i < rowIndexSet.size(); ++i) {
	      // The global index of the i-th vertex of the element
	      auto row = rowIndexSet.index(i);
	      for (size_t j = 0; j < colIndexSet.size(); ++j) {
		  // The global index of the j-th vertex of the element
		  auto col = colIndexSet.index(j);
		  nb.add(row, col);
	      }
	  }
      }
  }


  template <class Parameters>
    template <class RowFeSpace, class ColFeSpace>
  void ProblemStat<Parameters>::assemble(std::pair<int, int> row_col,
					 const RowFeSpace& rowFeSpace, 
					 const ColFeSpace& colFeSpace, 
					 DOFMatrix* matrix,
					 DOFVector* rhs)
  {
      auto rowView = rowFeSpace.localView();
      auto rowIndexSet = rowFeSpace.localIndexSet();
      
      auto colView = colFeSpace.localView();
      auto colIndexSet = colFeSpace.localIndexSet();
      
      for (const auto& element : elements(meshView)) {
	  rowView.bind(element);
	  rowIndexSet.bind(rowView);
	  
	  colView.bind(element);
	  colIndexSet.bind(colView);
	  
	  if (matrix) {
	      ElementMatrix elementMatrix;

	      // DOFMatrix::assemble {
	      bool add = getElementMatrix(rowView, colView, elementMatrix, matrixOperators[row_col]);

	      // DOFMatrix::addElementMatrix {
	      if (add) {
		  for (size_t i = 0; i < elementMatrix.N(); ++i) {
		      // The global index of the i−th vertex of the element
		      const auto row = rowIndexSet.index(i);
		      for (size_t j = 0; j < elementMatrix.M(); ++j) {
			  // The global index of the j−th vertex of the element
			  const auto col = colIndexSet.index(j);
			  (*matrix)[row][col] += elementMatrix[i][j];
		      }
		  }
	      }
	      // }
	      // }
	  }
	  
	  if (rhs) {
	      ElementVector elementVector;
	      
	      // DOFVectorBase<double>::assemble {
	      bool add = getElementVector(rowView, elementVector, vectorOperators[row_col.first]);
	      
	      // DOFVectorBase<T>::addElementVector {
	      if (add) {
		  for (size_t i = 0; i < elementVector.size(); ++i) {
		      // The global index of the i-th vertex
		      const auto row = rowIndexSet.index(i);
		      (*rhs)[row] += elementVector[i];
		  }
	      }
	      // }
	      // }
	  }
      }
  }
  
  
  template <class Parameters>
    template <class RowView, class ColView>
  void ProblemStat<Parameters>::getElementMatrix(RowView const& rowView,
						 ColView const& colView,
						 ElementMatrix& elementMatrix,
						 std::list<std::shared_ptr<Operator>>& operators)
  {
    const auto& rowFE = rowView.tree().finiteElement();
    const auto& colFE = colView.tree().finiteElement();

    elementMatrix.setSize(rowFE.size(), colFE.size());
    elementMatrix = 0.0; // fills the entire matrix with zeroes

    for (auto op : operators)
      op->getElementMatrix(rowView, colView, elementMatrix);
    
    return !operators.empty();
  }
  
  
  
  template <class Parameters>
    template <class RowView>
  void ProblemStat<Parameters>::getElementVector(RowView const& rowView,
						 ElementVector& elementVector,
						 std::list<std::shared_ptr<Operator>>& operators)
  {
    const auto& rowFE = rowView.tree().finiteElement();

    // Set all entries to zero
    elementVector.resize(rowFE.size());
    elementVector = 0.0;

    for (auto op : operators)
      op->getElementVector(rowView, elementVector);
    
    return !operators.empty();
  }

} // end namespace AMDiS