CouplingIterationInterface.cc 3.99 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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ==  http://www.amdis-fem.org                                              ==
// ==                                                                        ==
// ============================================================================
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.



/** \file CouplingIterationInterface.h */

#include "Flag.h"
#include "CouplingIterationInterface.h"

namespace AMDiS {

  /// add problem by number
  void CouplingIterationInterface::addIterationInterface(ProblemIterationInterface* probIter, int number)
  {
    unsigned idx = std::min(problems.size(), static_cast<unsigned>(number)); 
    if (number < 0)
      idx = problems.size();
    std::vector<ProblemIterationInterface*>::iterator pos = problems.begin() + idx;

    problems.insert(pos, probIter);
  };


  /// Called before each adaption loop iteration.
  void CouplingIterationInterface::beginIteration(AdaptInfo *adaptInfo)
  { FUNCNAME("CouplingIterationInterface::beginIteration()");
    MSG("\n");
    MSG("begin of iteration number: %d/%d\n", 
      adaptInfo->getTimestepNumber() + 1,
      adaptInfo->getNumberOfTimesteps());
    MSG("==================================================\n");
  };


  /** \brief
    * Determines the execution order of the single adaption steps. If adapt is
    * true, mesh adaption will be performed. This allows to avoid mesh adaption,
    * e.g. in timestep adaption loops of timestep adaptive strategies.
    */
  Flag CouplingIterationInterface::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  { FUNCNAME("CouplingIterationInterface::oneIteration()");
    Flag flag = 0;

    for (unsigned i = 0; i < problems.size(); ++i) {
      problems[i]->beginIteration(adaptInfo);
      flag |= problems[i]->oneIteration(adaptInfo, toDo);
      problems[i]->endIteration(adaptInfo);
    }

    return flag;
  };


  /// Called after each adaption loop iteration.
  void CouplingIterationInterface::endIteration(AdaptInfo *adaptInfo)
  { FUNCNAME("CouplingIterationInterface::endIteration()");
    MSG("\n");
    MSG("end of iteration number: %d\n", 
      adaptInfo->getTimestepNumber() + 1);
    MSG("==================================================\n");
  };


  /// Returns number of managed problems
  int CouplingIterationInterface::getNumProblems()
  {
    int num= 0;
    for (unsigned i = 0; i < problems.size(); ++i)
      num += problems[i]->getNumProblems();
    return num;
  };


  /** \brief
    * Returns the problem with the given number. If only one problem
    * is managed by this master problem, the number hasn't to be given.
    */
  ProblemStatBase *CouplingIterationInterface::getProblem(int number)
  {
    int maxNum = getNumProblems();
    if (maxNum <= number || number <  0)
      throw(std::runtime_error("Problem number out of range."));
    
    int sum = 0;
    ProblemStatBase *probIter = NULL;
    for (unsigned i = 0; i < problems.size(); ++i) {
      if (sum + problems[i]->getNumProblems() <= number)
        sum += problems[i]->getNumProblems();
      else
        probIter = problems[i]->getProblem(number - sum);
    }
    if (probIter == NULL)
      throw(std::runtime_error("Problem not found. Should not happen, since number is in range."));
    return probIter;
  };


  /// Returns the name of the problem.
  std::string CouplingIterationInterface::getName(int number)
  {
    if (static_cast<int>(problems.size()) <= number || number <  0)
      throw(std::runtime_error("Problem number out of range."));

    return problems[number]->getName();
  };

} // namespace AMDiS