CouplingIterationInterface.cc 4.4 KB
Newer Older
1
2
3
4
5
6
7
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
8
 * Authors:
9
10
11
12
13
14
15
16
17
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
18
 *
19
 ******************************************************************************/
20
21
22
23
24
25
26
27
28
29
30
31
32



/** \file CouplingIterationInterface.h */

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

namespace AMDiS {

  /// add problem by number
  void CouplingIterationInterface::addIterationInterface(ProblemIterationInterface* probIter, int number)
  {
33
    size_t idx = std::min(problems.size(), static_cast<size_t>(number));
34
35
36
    if (number < 0)
      idx = problems.size();
    std::vector<ProblemIterationInterface*>::iterator pos = problems.begin() + idx;
37
    std::vector<bool>::iterator pos2 = solveProblem.begin() + idx;
38
39

    problems.insert(pos, probIter);
40
    solveProblem.insert(pos2, true);
41
  }
42
43
44
45
46
47


  /// Called before each adaption loop iteration.
  void CouplingIterationInterface::beginIteration(AdaptInfo *adaptInfo)
  { FUNCNAME("CouplingIterationInterface::beginIteration()");
    MSG("\n");
Praetorius, Simon's avatar
Praetorius, Simon committed
48
49
50
51
    int nTimesteps = (adaptInfo->getNumberOfTimesteps()
			? adaptInfo->getNumberOfTimesteps()
			: static_cast<int>((adaptInfo->getEndTime()-adaptInfo->getStartTime())/adaptInfo->getTimestep())
		     );
52
    MSG("begin of iteration number: %d/%d\n",
53
      adaptInfo->getTimestepNumber() + 1,
54
      nTimesteps);
55
    MSG("==================================================\n");
56
  }
57
58
59
60
61
62
63
64


  /** \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)
65
  {
66
67
    Flag flag = 0;

68
69
70
71
72
73
    for (size_t i = 0; i < problems.size(); ++i) {
      if (solveProblem[i]) {
        problems[i]->beginIteration(adaptInfo);
        flag |= problems[i]->oneIteration(adaptInfo, toDo);
        problems[i]->endIteration(adaptInfo);
      }
74
75
76
    }

    return flag;
77
  }
78
79
80
81
82
83


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


  /// Returns number of managed problems
Praetorius, Simon's avatar
Praetorius, Simon committed
91
  int CouplingIterationInterface::getNumProblems() const
92
  {
93
94
    size_t num= 0;
    for (size_t i = 0; i < problems.size(); ++i)
95
96
      num += problems[i]->getNumProblems();
    return num;
97
  }
98
99
100
101
102
103
104
105


  /** \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)
  {
106
107
    size_t maxNum = getNumProblems();
    if (maxNum <= static_cast<size_t>(number))
108
      throw(std::runtime_error("Problem number out of range."));
109

110
    size_t sum = 0;
111
    ProblemStatBase *probIter = NULL;
112
113
    for (size_t i = 0; i < problems.size(); ++i) {
      if (sum + problems[i]->getNumProblems() <= static_cast<size_t>(number))
114
115
116
117
        sum += problems[i]->getNumProblems();
      else
        probIter = problems[i]->getProblem(number - sum);
    }
118
    if (probIter == NULL)
119
120
      throw(std::runtime_error("Problem not found. Should not happen, since number is in range."));
    return probIter;
121
  }
122
123
124


  /// Returns the name of the problem.
Praetorius, Simon's avatar
Praetorius, Simon committed
125
  std::string CouplingIterationInterface::getName(size_t number) const
126
  {
127
    if (static_cast<size_t>(problems.size()) <= number)
128
129
130
      throw(std::runtime_error("Problem number out of range."));

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

133
134
135
136
137
138
139
140
141

  void CouplingIterationInterface::setSolveProblem(std::string name, bool flag)
  {
    for (size_t i = 0; i < problems.size(); ++i) {
      if (problems[i]->getName() == name) {
        setSolveProblem(i, flag);
        break;
      }
    }
142
  }
143

144
} // namespace AMDiS