ProblemVec.cc 41.8 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
2
#include <sstream>

3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "ProblemVec.h"
#include "RecoveryEstimator.h"
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Estimator.h"
#include "Marker.h"
#include "AdaptInfo.h"
#include "FileWriter.h"
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
17
#include "DualTraverse.h"
18
19
20
21
22
23
24
25
#include "Mesh.h"
#include "OEMSolver.h"
#include "Preconditioner.h"
#include "MatVecMultiplier.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
26
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
27
#include "TraverseParallel.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
28
#include "VtkWriter.h"
29
30
31
32
33
34
35
36
37
38

namespace AMDiS {

  void ProblemVec::initialize(Flag initFlag,
			      ProblemVec *adoptProblem,
			      Flag adoptFlag)
  {
    FUNCNAME("ProblemVec::initialize()");
    
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
39
    if (meshes.size() != 0) { 
40
41
42
43
44
45
46
47
48
49
50
      WARNING("meshes 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))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
51
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
52
	componentMeshes = adoptProblem->componentMeshes;
Thomas Witkowski's avatar
Thomas Witkowski committed
53
54
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
55
56
57

	// If the adopt problem has fewer components than this problem, but only one
	// mesh for all component, than scal up the componentMeshes array.
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
59
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
60
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
61
62
63
	  componentMeshes.resize(nComponents);
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++) {
	    componentMeshes[i] = componentMeshes[0];
64
65
66
	  }
	}

67
68
69
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
70
    if (meshes.size() == 0) 
71
72
73
      WARNING("no mesh created\n");

    // === create fespace ===
74
    if (feSpaces.size() != 0) {
75
76
77
78
79
80
81
82
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
	  (initFlag.isSet(INIT_SYSTEM)&&!adoptFlag.isSet(INIT_FE_SPACE))) {
	createFESpace();
      } 
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
83
84
	feSpaces = adoptProblem->getFESpaces();
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
85
	traverseInfo = adoptProblem->traverseInfo;
86
87
88

	// If the adopt problem has fewer components than this problem, but only one
	// fe space for all component, than scal up the componentSpaces array.
Thomas Witkowski's avatar
Thomas Witkowski committed
89
90
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
91
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
92
93
94
	  componentSpaces.resize(nComponents);
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++) {
	    componentSpaces[i] = componentSpaces[0];
95
96
97
	  }
	}

98
99
100
      }
    }

101
    if (feSpaces.size() == 0) 
102
103
104
105
106
107
108
      WARNING("no feSpace created\n");

    // === create system ===
    if (initFlag.isSet(INIT_SYSTEM)) {
      createMatricesAndVectors();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
109
110
111
      solution = adoptProblem->getSolution();
      rhs = adoptProblem->getRHS();
      systemMatrix = adoptProblem->getSystemMatrix();
112
113
114
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
115
    if (solver) {
116
117
118
119
120
121
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
122
123
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
124
125
126
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
127
    if (!solver) 
128
129
130
131
132
133
134
      WARNING("no solver created\n");

    // === create estimator ===
    if (initFlag.isSet(INIT_ESTIMATOR)) {
      createEstimator();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
135
      estimator = adoptProblem->getEstimator();
136
137
138
139
140
141
142
    } 

    // === create marker ===
    if (initFlag.isSet(INIT_MARKER)) {
      createMarker();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
143
      marker = adoptProblem->getMarker();
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    } 


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

    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
    // to be read from disk. Either by providing the parameter -rs when executing
    // the program or in the init file. The -rs parameter is always checked first,
    // because it can be added automatically when  rescheduling the program
    // before timeout of the runqueue.

    int readSerialization = 0;
162
    std::string serializationFilename = "";
163
164
165
166
167
168
169
    GET_PARAMETER(0, "argv->rs", &serializationFilename);

    // If the parameter -rs is set, we do nothing here, because the problem will be
    // deserialized in the constructor of a following AdaptInstationary initialization.
    if (!serializationFilename.compare("")) {
      int readSerializationWithAdaptInfo = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
170
      GET_PARAMETER(0, name + "->input->read serialization", "%d", 
171
		    &readSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
172
      GET_PARAMETER(0, name + "->input->serialization with adaptinfo", "%d",
173
174
175
176
177
178
179
		    &readSerializationWithAdaptInfo);

      // The serialization file is only read, if the adaptInfo part should not be used.
      // If the adaptInfo part should be also read, the serialization file will be read
      // in the constructor of the AdaptInstationary problem, because we do not have here
      // the adaptInfo object.
      if (readSerialization && !readSerializationWithAdaptInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
180
	GET_PARAMETER(0, name + "->input->serialization filename", 
181
182
183
184
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
185
	std::ifstream in(serializationFilename.c_str());
186
187
188
	deserialize(in);
	in.close();
      } else {
189
	int globalRefinements = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
190
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
191
192
		      &globalRefinements);

193
	// Initialize the meshes if there is no serialization file.
Thomas Witkowski's avatar
Thomas Witkowski committed
194
	for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
195
	  if (initFlag.isSet(INIT_MESH) && 
Thomas Witkowski's avatar
Thomas Witkowski committed
196
197
198
199
	      meshes[i] && 
	      !(meshes[i]->isInitialized())) {
	    meshes[i]->initialize();	    
	    refinementManager->globalRefine(meshes[i], globalRefinements);
200
201
202
203
204
205
206
207
208
209
210
211
	  }
	}	
      }
    }

    doOtherStuff();
  }

  void ProblemVec::createMesh() 
  {
    FUNCNAME("ProblemVec::createMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
212
    componentMeshes.resize(nComponents);
213
    std::map<int, Mesh*> meshForRefinementSet;
214
215
    char number[3];

216
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
217
    GET_PARAMETER(0, name + "->mesh", &meshName);
218
    TEST_EXIT(meshName != "")("no mesh name specified\n");
219
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
220
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
221
    TEST_EXIT(dim)("no problem dimension specified!\n");
222

223
    for (int i = 0; i < nComponents; i++) {
224
      sprintf(number, "%d", i);
225
      int refSet = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
226
      GET_PARAMETER(0, name + "->refinement set[" + number + "]", "%d", &refSet);
227
      if (refSet < 0) {
228
229
	refSet = 0;
      }
230
      if (meshForRefinementSet[refSet] == NULL) {
231
232
	Mesh *newMesh = NEW Mesh(meshName, dim);
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
233
234
	meshes.push_back(newMesh);
	nMeshes++;
235
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
236
      componentMeshes[i] = meshForRefinementSet[refSet];
237
238
239
    }
    switch(dim) {
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
240
241
      coarseningManager = NEW CoarseningManager1d();
      refinementManager = NEW RefinementManager1d();
242
243
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
244
245
      coarseningManager = NEW CoarseningManager2d();
      refinementManager = NEW RefinementManager2d();
246
247
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
248
249
      coarseningManager = NEW CoarseningManager3d();
      refinementManager = NEW RefinementManager3d();
250
251
252
253
254
255
256
257
258
259
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

  void ProblemVec::createFESpace()
  {
    FUNCNAME("ProblemVec::createFESpace()");

260
    std::map< std::pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
261
    int dim = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
262
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
263
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
264

265
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
266
    traverseInfo.resize(nComponents);
267

268
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
269
      char number[3];
270
      sprintf(number, "%d", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
271
272
      int degree = 1;
      GET_PARAMETER(0, name + "->polynomial degree[" + number + "]","%d", &degree);
273

274
      TEST_EXIT(componentSpaces[i] == NULL)("feSpace already created\n");
275

Thomas Witkowski's avatar
Thomas Witkowski committed
276
      if (feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
277
278
279
	stringstream s;
	s << name << "->feSpace[" << i << "]";

280
	FiniteElemSpace *newFESpace = 
Thomas Witkowski's avatar
Thomas Witkowski committed
281
282
	  FiniteElemSpace::provideFESpace(NULL, Lagrange::getLagrange(dim, degree),
					  componentMeshes[i], s.str());
Thomas Witkowski's avatar
Thomas Witkowski committed
283
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] = newFESpace;
284
	feSpaces.push_back(newFESpace);
285
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
286
287
288
289
290
291
292
293
      componentSpaces[i] = feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
    }

    for (int i = 0; i < nComponents; i++) {
      for (int j = 0; j < nComponents; j++) {
	traverseInfo.getMatrix(i, j).setFESpace(componentSpaces[i], componentSpaces[j]);
      }
      traverseInfo.getVector(i).setFESpace(componentSpaces[i]);
294
295
296
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
297
298
299
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      if (meshes[i]->getNumberOfDOFs(VERTEX) == 0) {
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
300
	ln_dof[VERTEX]= 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
301
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
302
303
304
305
306
307
308
309
310
311
      }
    }
  }

  void ProblemVec::createMatricesAndVectors()
  {
    FUNCNAME("ProblemVec::createMatricesAndVectors()");

    // === create vectors and system matrix ===

Thomas Witkowski's avatar
Thomas Witkowski committed
312
313
314
315
    systemMatrix = NEW Matrix<DOFMatrix*>(nComponents, nComponents);
    systemMatrix->set(NULL);
    rhs = NEW SystemVector("rhs", componentSpaces, nComponents);
    solution = NEW SystemVector("solution", componentSpaces, nComponents);
316
317

    char number[10];
318
    std::string numberedName;
Thomas Witkowski's avatar
Thomas Witkowski committed
319
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
320
      (*systemMatrix)[i][i] = NEW DOFMatrix(componentSpaces[i], 
321
					     componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
322
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
323
      sprintf(number, "[%d]", i);
324
      numberedName = "rhs" + std::string(number);
Thomas Witkowski's avatar
Thomas Witkowski committed
325
326
327
      rhs->setDOFVector(i, NEW DOFVector<double>(componentSpaces[i], numberedName));
      numberedName = name + std::string(number);
      solution->setDOFVector(i, NEW DOFVector<double>(componentSpaces[i], 
328
						       numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
329
330
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
331
332
333
    }

    // === create matVec ===
Thomas Witkowski's avatar
Thomas Witkowski committed
334
    matVec = NEW StandardMatVec<Matrix<DOFMatrix*>, SystemVector>(systemMatrix);
335
336
337
338
339
340
341
  }

  void ProblemVec::createSolver()
  {
    FUNCNAME("ProblemVec::createSolver()");

    // === create solver ===
342
    std::string solverType("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
343
    GET_PARAMETER(0, name + "->solver", &solverType);
344
345
346
347
348
349
    OEMSolverCreator<SystemVector> *solverCreator = 
      dynamic_cast<OEMSolverCreator<SystemVector>*>(
						    CreatorMap<OEMSolver<SystemVector> >
						    ::getCreator(solverType)
						    );
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
350
351
352
    solverCreator->setName(name + "->solver");
    solver = solverCreator->create();
    solver->initParameters();
353
354

    // === create preconditioners ===
355
    std::string preconType("no");
356
357

    PreconditionerScal *scalPrecon;
358
    PreconditionerVec *vecPrecon = NEW PreconditionerVec(nComponents);
359

Thomas Witkowski's avatar
Thomas Witkowski committed
360
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);
361
362
363
364
365
    CreatorInterface<PreconditionerScal> *preconCreator =
      CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
366
	setName(name + "->solver->left precon");
367

Thomas Witkowski's avatar
Thomas Witkowski committed
368
      for (int i = 0; i < nComponents; i++) {
369
	dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
370
	  setSizeAndRow(nComponents, i);
371
372
    
	scalPrecon = preconCreator->create();
Thomas Witkowski's avatar
Thomas Witkowski committed
373
374
	for (int j = 0; j < nComponents; j++) {
	  scalPrecon->setMatrix(&(*systemMatrix)[i][j], j);
375
376
377
	}
	vecPrecon->setScalarPrecon(i, scalPrecon);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
378
      leftPrecon = vecPrecon;
379
380
381
    }


382
    vecPrecon = NEW PreconditionerVec(nComponents);
383

Thomas Witkowski's avatar
Thomas Witkowski committed
384
    GET_PARAMETER(0, name + "->solver->right precon", &preconType);
385
386
387
    preconCreator = 
      CreatorMap<PreconditionerScal>::getCreator(preconType);

Thomas Witkowski's avatar
Thomas Witkowski committed
388
    if (!preconCreator->isNullCreator()) {
389
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
390
	setName(name + "->solver->left precon");
391
392


Thomas Witkowski's avatar
Thomas Witkowski committed
393
      for (int i = 0; i < nComponents; i++) {
394
	dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
395
	  setSizeAndRow(nComponents, i);
396
397
    
	scalPrecon = preconCreator->create();
Thomas Witkowski's avatar
Thomas Witkowski committed
398
399
	for (int j = 0; j < nComponents; j++) {
	  scalPrecon->setMatrix(&(*systemMatrix)[i][j], j);
400
401
402
	}
	vecPrecon->setScalarPrecon(i, scalPrecon);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
403
      rightPrecon = vecPrecon;
404
405
406
407
    }


    // === create vector creator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
408
    solver->setVectorCreator(NEW SystemVector::Creator("temp",
409
							componentSpaces, 
410
							nComponents));
411
412
413
414
415
416
417
  }

  void ProblemVec::createEstimator()
  {
    FUNCNAME("ProblemVec::createEstimator()");

    // create and set leaf data prototype
Thomas Witkowski's avatar
Thomas Witkowski committed
418
419
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      meshes[i]->setElementDataPrototype
420
421
422
423
	(NEW LeafDataEstimatableVec(NEW LeafDataCoarsenableVec));
    }  

    char number[3];
424
    std::string estName;
425

Thomas Witkowski's avatar
Thomas Witkowski committed
426
427
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
428
      sprintf(number, "%d", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
429
      estName = name + "->estimator[" + std::string(number) + "]";
430
431

      // === create estimator ===
432
      std::string estimatorType("no");
433
434
435
436
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
	dynamic_cast<EstimatorCreator*>(
					CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
437
      if (estimatorCreator) {
438
439
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
440
	if (estimatorType == "recovery") {
441
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
442
	    setSolution(solution->getDOFVector(i));
443
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
444
	estimator[i] = estimatorCreator->create();
445
446
447
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
450
451
452
      if (estimator[i]) {
	for (int j = 0; j < nComponents; j++) {
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				   solution->getDOFVector(j), 
				   rhs->getDOFVector(j));
453
454
455
456
457
458
459
460
461
	}
      }
    }
  }

  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

462
    std::string numberedName;
463
    char number[10];
Thomas Witkowski's avatar
Thomas Witkowski committed
464
    int nMarkersCreated = 0;
465

466
    for (int i = 0; i < nComponents; i++) {
467
      sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
468
      numberedName = name + "->marker" + std::string(number);
469
470
      marker[i] = Marker::createMarker(numberedName, i);
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
471
472
473
474
475
476
	nMarkersCreated++;

	// If there is more than one marker, and all components are defined
	// on the same mesh, the maximum marking has to be enabled.
 	if ((nMarkersCreated > 1) && (nMeshes == 1))
 	  marker[i]->setMaximumMarking(true);
477
478
479
480
481
482
483
484
485
486
      }
    }
  }

  void ProblemVec::createFileWriter()
  {
    FUNCNAME("ProblemVec::createFileWriter()");
  

    // Create one filewriter for all components of the problem
Thomas Witkowski's avatar
Thomas Witkowski committed
487
    std::string numberedName  = name + "->output";
488
    std::string filename = "";
489
490
491
    GET_PARAMETER(0, numberedName + "->filename", &filename);

    if (filename != "") {
492
      std::vector< DOFVector<double>* > solutionList(nComponents);
493

494
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
495
	TEST_EXIT(componentMeshes[0] == componentMeshes[i])
496
497
	  ("All Meshes have to be equal to write a vector file.\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
498
	solutionList[i] = solution->getDOFVector(i);
499
500
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
501
      fileWriters.push_back(NEW FileWriter(numberedName,
Thomas Witkowski's avatar
Thomas Witkowski committed
502
					    componentMeshes[0],
503
504
505
506
507
508
					    solutionList));
    }


    // Create own filewriters for each components of the problem
    char number[10];
509
    for (int i = 0; i < nComponents; i++) {
510
      sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
511
      numberedName  = name + "->output" + std::string(number);
512
513
514
515
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

      if (filename != "") {
Thomas Witkowski's avatar
Thomas Witkowski committed
516
	fileWriters.push_back(NEW FileWriter(numberedName, 
Thomas Witkowski's avatar
Thomas Witkowski committed
517
					      componentMeshes[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
518
					      solution->getDOFVector(i)));
519
520
521
522
523
524
      }
    }


    // Check for serializer
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
525
    GET_PARAMETER(0, name + "->write serialization", "%d", &writeSerialization);
526
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
527
528
      MSG("Use are using the obsolete parameter: %s->write serialization\n", name.c_str());
      MSG("Please use instead the following parameter: %s->output->write serialization\n", name.c_str());
529
530
531
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
532
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
533
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
534
      fileWriters.push_back(NEW Serializer<ProblemVec>(this));
535
536
537
538
539
540
541
    }
  }

  void ProblemVec::doOtherStuff()
  {
  }

542
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
543
544
545
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
546
    if (!solver) {
547
548
549
550
551
552
553
554
555
      WARNING("no solver\n");
      return;
    }

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

    clock_t first = clock();
Thomas Witkowski's avatar
Thomas Witkowski committed
556
    int iter = solver->solve(matVec, solution, rhs, leftPrecon, rightPrecon, fixedMatrix);
557
    
558
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
559
560
    INFO(info, 8)("solution of discrete system needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
561
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
562
563
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
564
565
566
567
#endif


    adaptInfo->setSolverIterations(iter);
Thomas Witkowski's avatar
Thomas Witkowski committed
568
569
570
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
571
572
573
574
575
576
  }

  void ProblemVec::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::estimate()");

Thomas Witkowski's avatar
Thomas Witkowski committed
577
578
    VtkWriter::writeFile(solution->getDOFVector(0), "test.vtu");

579
580
    clock_t first = clock();

581
582
583
584
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

585
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
586
      computeError(adaptInfo);
587
588
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
589
	Estimator *scalEstimator = estimator[i];
590
591
592
593
594
595
596
597
598
599
	
	if (scalEstimator) {
	  scalEstimator->estimate(adaptInfo->getTimestep());
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
	  adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	  adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	} else {
	  WARNING("no estimator for component %d\n" , i);
	}
600
601
602
      }
    }

603
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
604
605
    INFO(info, 8)("estimation of the error needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
606
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
607
608
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
609
610
611

#endif

612
613
614
615
616
617
618
619
620
621
622
  }

  Flag ProblemVec::markElements(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::markElements()");

    // to enforce albert-like behavior: refinement even if space tolerance
    // here is reached already because of time adaption
    allowFirstRefinement();

    Flag markFlag = 0;
623
    for (int i = 0; i < nComponents; i++) {
624
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
625
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
626
627
628
629
      } else {
	WARNING("no marker for component %d\n", i);
      }
    }
630
    
631
632
633
634
635
636
637
    return markFlag;
  }

  Flag ProblemVec::refineMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::refineMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
638
    int nMeshes = static_cast<int>(meshes.size());
639
    Flag refineFlag = 0;
640
    for (int i = 0; i < nMeshes; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
641
      refineFlag |= refinementManager->refineMesh(meshes[i]);
642
643
644
645
646
647
648
649
    }
    return refineFlag;
  }

  Flag ProblemVec::coarsenMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::coarsenMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
650
    int nMeshes = static_cast<int>(meshes.size());
651
    Flag coarsenFlag = 0;
652
653
    for (int i = 0; i < nMeshes; i++) {
      if (adaptInfo->isCoarseningAllowed(i)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
654
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
655
656
657
658
659
660
661
662
663
664
665

	WARNING("coarsening for component %d no allowed\n", i);
      }
    }
    return coarsenFlag;
  }

  Flag ProblemVec::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    FUNCNAME("ProblemVec::oneIteration()");

Thomas Witkowski's avatar
Thomas Witkowski committed
666
    if (allowFirstRef) {
667
      for (int i = 0; i < nComponents; i++) {
668
669
	adaptInfo->allowRefinement(true, i);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
670
      allowFirstRef = false;
671
    } else {
672
      for (int i = 0; i < nComponents; i++) {
673
674
675
676
677
678
679
680
681
682
683
	if (adaptInfo->spaceToleranceReached(i)) {
	  adaptInfo->allowRefinement(false, i);
	} else {
	  adaptInfo->allowRefinement(true, i);	
	}
      }
    }

    return StandardProblemIteration::oneIteration(adaptInfo, toDo);
  }

684
685
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
686
687
688
689
690
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

691
692
693
694
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
695
696
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      meshes[i]->dofCompress();
697
698
699
700
    }

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
701
702
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
703
704
705
706
707
708
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
709
    if (useGetBound) {
710
711
712
      assembleFlag |= Mesh::FILL_BOUND;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
713

714
    for (int i = 0; i < nComponents; i++) {
715
      MSG("%d DOFs for %s\n", 
716
717
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());
718

Thomas Witkowski's avatar
Thomas Witkowski committed
719
      rhs->getDOFVector(i)->set(0.0);
720
      for (int j = 0; j < nComponents; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
721
	if ((*systemMatrix)[i][j]) {
722
723
	  // The matrix should not be deleted, if it was assembled before
	  // and it is marked to be assembled only once.
Thomas Witkowski's avatar
Thomas Witkowski committed
724
725
	  if (!(assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) && asmMatrix) {
	    (*systemMatrix)[i][j]->clear();
726
727
	  }
	}
728
729
730
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
731
732
    traverseInfo.updateStatus();

Thomas Witkowski's avatar
Thomas Witkowski committed
733
    for (int i = 0; i < nComponents; i++) {
734
      for (int j = 0; j < nComponents; j++) {
735
736
737
738
	// Only if this variable is true, the current matrix will be assembled.	
	bool assembleMatrix = true;
	// The DOFMatrix which should be assembled (or not, if assembleMatrix
	// will be set to false).
Thomas Witkowski's avatar
Thomas Witkowski committed
739
	DOFMatrix *matrix = (*systemMatrix)[i][j];
740

741
742
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
Thomas Witkowski's avatar
Thomas Witkowski committed
743
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
744
745
	  assembleMatrix = false;
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
746
747

	// If there is no DOFMatrix, e.g., if it is completly 0, do not assemble.
748
749
750
751
	if (!matrix) {
	  assembleMatrix = false;
	}

752
753
754
755
	if (!asmMatrix) {
	  assembleMatrix = false;
	}

756
757
758
759
760
761
762
	// If the matrix should not be assembled, the rhs vector has to be considered.
	// This will be only done, if i == j. So, if both is not true, we can jump
	// to the next matrix.
	if (!assembleMatrix && i != j) {
	  continue;
	}

763
764
765
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

Thomas Witkowski's avatar
Thomas Witkowski committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
	if (traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_NO_AUX ||
	    traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_WITH_AUX) {

	  // Row fe space and col fe space are both equal

	  if (traverseInfo.getStatus(i) == SingleComponentInfo::EQ_SPACES_NO_AUX ||
	      traverseInfo.getStatus(i) == SingleComponentInfo::EQ_SPACES_WITH_AUX) {

	    // The simplest case: either the right hand side has no operaters, no aux
	    // fe spaces, or all aux fe spaces are equal to the row and col fe space.

	    assembleOnOneMesh(componentSpaces[i],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);

	  } else if (traverseInfo.getStatus(i) == SingleComponentInfo::EQ_SPACES_WITH_DIF_AUX) {

	    // Row fe space and col fe space are both equal, but right hand side has at
785
	    // least one another aux fe space. 
Thomas Witkowski's avatar
Thomas Witkowski committed
786
787
788
789
790
791

	    assembleOnOneMesh(componentSpaces[i],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);

792
793
	    assembleOnDifMeshes2(componentSpaces[i], 
				 traverseInfo.getAuxFESpace(i, j),
Thomas Witkowski's avatar
Thomas Witkowski committed
794
795
				 assembleFlag,
				 NULL,
796
				 ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
797
798
799
800
801
802
803

	  } else {
	    ERROR_EXIT("Possible? If yes, not yet implemented!\n");
	  }

	} else if (traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_WITH_DIF_AUX) {
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
804
805
806
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
807
			    ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
808
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
809
810
811
812
	  assembleOnDifMeshes2(componentSpaces[i],
			       traverseInfo.getAuxFESpace(i, j),
			       assembleFlag,
			       assembleMatrix ? matrix : NULL,
813
			       ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
814
815
816
817

	} else if (traverseInfo.getStatus(i, j) ==  SingleComponentInfo::DIF_SPACES_NO_AUX ||
		   traverseInfo.getStatus(i, j) ==  SingleComponentInfo::DIF_SPACES_WITH_AUX) {

Thomas Witkowski's avatar
Thomas Witkowski committed
818
819
820
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
821
822
823
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);	  

	  TEST_EXIT_DBG(matrix->getUsedSize() == componentSpaces[i]->getAdmin()->getUsedSize())
824
	    ("Assembled matrix has wrong dimension!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
825
826
827
828
829
	  TEST_EXIT_DBG(matrix->getNumCols() == componentSpaces[j]->getAdmin()->getUsedSize())
	    ("Assembled matrix has wrong dimension!\n");	  

	} else {
	  ERROR_EXIT("Not yet implemented!\n");
830
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
831

832
833
834
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->exitMatrix(matrix);	  
	
Thomas Witkowski's avatar
Thomas Witkowski committed
835
	assembledMatrix[i][j] = true;
836
837
      }

838
      // And now assemble boundary conditions on the vectors
Thomas Witkowski's avatar
Thomas Witkowski committed
839
840
      assembleBoundaryConditions(rhs->getDOFVector(i),
				 solution->getDOFVector(i),
841
842
				 componentMeshes[i],
				 assembleFlag);
843
    }
844
845

#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
846
847
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
848
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
849
850
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
851
#endif
852
853
854
855
856
857
  }

  void ProblemVec::writeFiles(AdaptInfo *adaptInfo, bool force) 
  {
    FUNCNAME("ProblemVec::writeFiles()");

858
859
860
861
862
863
864
    clock_t first = clock();

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
865
    int size = static_cast<int>(fileWriters.size());
866
867
868
869
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 1)
#endif
    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
870
      fileWriters[i]->writeFiles(adaptInfo, force);
871
    }
872
873
    
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
874
875
876
    INFO(info, 8)("writeFiles needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()),
		  omp_get_wtime() - wtime);
877
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
878
879
    INFO(info, 8)("writeFiles needed %.5f seconds\n",
		  TIME_USED(first, clock()));
880
#endif
881
882
  }

883
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
884
885
886
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

Thomas Witkowski's avatar
Thomas Witkowski committed
887
    solution->interpol(fct);
888
889
890
891
892
  }

  void ProblemVec::addMatrixOperator(Operator *op, 
				     int i, int j,
				     double *factor,
893
894
				     double *estFactor,
				     bool fake)
895
896
  {
    FUNCNAME("ProblemVec::addMatrixOperator()");
Thomas Witkowski's avatar
Thomas Witkowski committed
897
   
Thomas Witkowski's avatar
Thomas Witkowski committed
898
    if (!(*systemMatrix)[i][j]) {
899
      TEST_EXIT(i != j)("should have been created already\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
900
901
902
903
      (*systemMatrix)[i][j] = NEW DOFMatrix(componentSpaces[i], componentSpaces[j], "");
      (*systemMatrix)[i][j]->setCoupleMatrix(true);
      (*systemMatrix)[i][j]->getBoundaryManager()->
	setBoundaryConditionMap((*systemMatrix)[i][i]->getBoundaryManager()->
904
905
				getBoundaryConditionMap());
    }    
Thomas Witkowski's avatar
Thomas Witkowski committed
906

907
    (*systemMatrix)[i][j]->addOperator(op, factor, estFactor);
Thomas Witkowski's avatar
Thomas Witkowski committed
908

909
910
911
912
913
914
915
916
917
918
919
    if (!fake) {    
      traverseInfo.getMatrix(i, j).setAuxFESpaces(op->getAuxFESpaces()); 
      
      for (int k = 0; k < static_cast<int>(op->getAuxFESpaces().size()); k++) {
	if ((op->getAuxFESpaces())[k]->getMesh() != componentSpaces[i]->getMesh() ||
	    (op->getAuxFESpaces())[k]->getMesh() != componentSpaces[j]->getMesh()) {
	  op->setNeedDualTraverse(true);
	  break;
	}
      }    
    } 
920
921
922
923
  }

  void ProblemVec::addVectorOperator(Operator *op, int i,
				     double *factor,
924
925
				     double *estFactor,
				     bool fake)
926
927
928
  {
    FUNCNAME("ProblemVec::addVectorOperator()");

Thomas Witkowski's avatar
Thomas Witkowski committed
929
930
    rhs->getDOFVector(i)->addOperator(op, factor, estFactor);

931
932
933
934
935
936
937
938
939
940
    if (!fake) {
      traverseInfo.getVector(i).setAuxFESpaces(op->getAuxFESpaces()); 
      
      for (int j = 0; j < static_cast<int>(op->getAuxFESpaces().size()); j++) {
	if ((op->getAuxFESpaces())[j]->getMesh() != componentSpaces[i]->getMesh()) {
	  op->setNeedDualTraverse(true);
	  break;
	}
      }    
    }
941
942
943
944
945
946
947
948
949
  }

  void ProblemVec::addDirichletBC(BoundaryType type, int system,
				  AbstractFunction<double, WorldVector<double> >* b)
  {
    FUNCNAME("ProblemVec::addDirichletBC()");

    DirichletBC *dirichlet = new DirichletBC(type, 
					     b, 
950
					     componentSpaces[system]);
951
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
952
953
      if (systemMatrix && (*systemMatrix)[system][i]) {
	(*systemMatrix)[system][i]->getBoundaryManager()->addBoundaryCondition(dirichlet);
954
955
      }
    }
956

Thomas Witkowski's avatar
Thomas Witkowski committed
957
958
    if (rhs)
      rhs->getDOFVector(system)->getBoundaryManager()->addBoundaryCondition(dirichlet);
959

Thomas Witkowski's avatar
Thomas Witkowski committed
960
961
    if (solution)
      solution->getDOFVector(system)->getBoundaryManager()->addBoundaryCondition(dirichlet);
962
963
964
965
966
967
968
969
970
  }

  void ProblemVec::addNeumannBC(BoundaryType type, int row, int col, 
				AbstractFunction<double, WorldVector<double> > *n)
  {
    FUNCNAME("ProblemVec::addNeumannBC()");

    NeumannBC *neumann = 
      new NeumannBC(type, n, 
971
972
		    componentSpaces[row], 
		    componentSpaces[col]);
Thomas Witkowski's avatar
Thomas Witkowski committed
973
974
    if (rhs)
      rhs->getDOFVector(row)->getBoundaryManager()->addBoundaryCondition(neumann);