BaseProblem.hh 6.69 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
using namespace AMDiS;

template<typename ProblemType>
BaseProblem<ProblemType>::BaseProblem(const std::string &name_) :
  ProblemInstatBase(name_,NULL),
  prob(NULL),
  secureIteration(false),
  oldMeshChangeIdx(0),
  nTimesteps(-1),
  dim(1),
  dow(1),
  oldTimestep(0.0)
{
  // create basic problems
  prob = new ProblemType(name + "->space");
  dow = Global::getGeo(WORLD);

  Initfile::get(name + "->secure iteration", secureIteration);
};


template<typename ProblemType>
void BaseProblem<ProblemType>::initialize(Flag initFlag,
                              ProblemStat *adoptProblem,
                              Flag adoptFlag)
{ FUNCNAME("BaseProblem::initialize()");

  prob->initialize(initFlag, adoptProblem, adoptFlag);
  dim = getMesh()->getDim();
};


template<typename ProblemType>
Flag BaseProblem<ProblemType>::initDataFromFile(AdaptInfo *adaptInfo)
{ FUNCNAME("BaseProblem::initDataFromFile()");

  Flag initFlag;
  bool readDataFromFile = false, readArhFiles = false, readDatFiles = false;
  Initfile::get(name + "->read data from file", readDataFromFile, 2);
  if (!readDataFromFile)
    return initFlag;
  
  Initfile::get(name + "->read arh files", readArhFiles, 2);
  Initfile::get(name + "->read dat files", readDatFiles, 2);
  if (readArhFiles && readDatFiles) {
    WARNING("You can not read data from both formats, .arh and .dat! The .arh-format is selected.\n");
  }

  std::string data_file="", phase_file="";

  // read data and mesh from arh-files/dat-files
  MSG("read data from file...\n");
  if (readArhFiles) {
    Initfile::get(name + "->data file", data_file);
    if (data_file.size() == 0) return initFlag;
    if (!file_exists(data_file))
      throw(std::runtime_error("The file '" + data_file + "' does not exist!"));
    
    std::vector<DOFVector<double>*> solutions;
    for (size_t i = 0; i < prob->getNumComponents(); i++)
      solutions.push_back(prob->getSolution()->getDOFVector(i));
    ArhReader::read(data_file, prob->getMesh(), solutions);
  } else if(readDatFiles) {
    bool preserveMacroFileInfo = false;
    Parameters::get(prob->getMesh()->getName() + "->preserve macroFileInfo", preserveMacroFileInfo);
    if (prob->getMesh()->getMacroFileInfo() == NULL || !preserveMacroFileInfo)
      throw(std::runtime_error("Yout have to set the flag 'mesh_name->preserve macroFileInfo' to 'true' ("+boost::lexical_cast<std::string>(preserveMacroFileInfo)+"), in order to read .dat-files"));
    
    std::string filename = "";
    for (size_t i = 0; i < prob->getNumComponents(); i++) {
      Parameters::get(name + "->value file["+boost::lexical_cast<std::string>(i)+"]",filename);
      if (!file_exists(filename))
	throw(std::runtime_error("The file '" + filename + "'does not exist!"));
      ValueReader::readValue(filename,prob->getMesh(),prob->getSolution()->getDOFVector(i),prob->getMesh()->getMacroFileInfo());
    }
  } else
    throw(std::runtime_error("Parameter 'read data from file' set to 'true', but no input format specified!"));
  
  initFlag.setFlag(DATA_ADOPTED);
  initFlag.setFlag(MESH_ADOPTED);

  return initFlag;
};


template<typename ProblemType>
void BaseProblem<ProblemType>::beginIteration(AdaptInfo *adaptInfo) 
{ FUNCNAME("BaseProblem::beginIteration()");
	
  MSG("\n");
  MSG(("[[ <"+name+"> iteration ]]\n").c_str());
    
  // assemble some blocks only once, if some conditions are fullfilled
  #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  bool fixedMatrixCell = false;
  #else
  bool fixedMatrixCell = adaptInfo->getTimestepNumber() > 0
    && std::abs(adaptInfo->getTimestep() - oldTimestep) < DBL_TOL
    && oldMeshChangeIdx == getMesh()->getChangeIndex();
  #endif
  for(size_t i = 0; i < fixedMatrixTimestep.size(); ++i) {
    prob->setAssembleMatrixOnlyOnce(
      fixedMatrixTimestep[i].first, 
      fixedMatrixTimestep[i].second,
      fixedMatrixCell);
  }
};


template<typename ProblemType>
Flag BaseProblem<ProblemType>::oneIteration(AdaptInfo *adaptInfo, 
                                Flag toDo) 
{ FUNCNAME("BaseProblem::oneIteration()");

  Flag flag, markFlag;
  
  if (toDo.isSet(MARK))
    markFlag = prob->markElements(adaptInfo);
  else
    markFlag = 3;

  // refine
  if (toDo.isSet(ADAPT) && markFlag.isSet(MESH_REFINED))
    flag = prob->refineMesh(adaptInfo);  
  // coarsen
  if (toDo.isSet(ADAPT) && markFlag.isSet(MESH_COARSENED))
    flag |= prob->coarsenMesh(adaptInfo);

  #ifdef HAVE_PARALLEL_DOMAIN_AMDIS
  MPI::COMM_WORLD.Barrier();
  #endif

  #ifndef NONLIN_PROBLEM
  if (toDo.isSet(BUILD)) {    
    prob->getSolver()->setMultipleRhs(false);
    prob->buildAfterCoarsen(adaptInfo, markFlag, true, true);
  }
  #endif

  if (toDo.isSet(SOLVE)) {
    if (secureIteration) {
      try {
	prob->solve(adaptInfo);
      } catch(std::exception &e) {
	WARNING(("Could not solve system. Simulation will be stoped here! ERROR: "
	  + std::string(e.what()) + "\n").c_str());
	adaptInfo->setTime(adaptInfo->getEndTime());
	adaptInfo->setTimestepNumber(adaptInfo->getNumberOfTimesteps());
	return flag;
      } catch(...) {
	WARNING("Could not solve system. Simulation will be stoped here! Unknown ERROR\n");
	adaptInfo->setTime(adaptInfo->getEndTime());
	adaptInfo->setTimestepNumber(adaptInfo->getNumberOfTimesteps());
	return flag;
      }
    } else {
      prob->solve(adaptInfo);
    }
  }

  if (toDo.isSet(ESTIMATE))
    prob->estimate(adaptInfo);

  oldTimestep = adaptInfo->getTimestep();
  oldMeshChangeIdx = getMesh()->getChangeIndex();

  return flag;
};


template<typename ProblemType>
void BaseProblem<ProblemType>::endIteration(AdaptInfo *adaptInfo) 
{ FUNCNAME("BaseProblem::endIteration()");
	
  MSG("\n");
  MSG(("[[ end of <"+name+"> iteration ]]\n").c_str());
};


template<typename ProblemType>
void BaseProblem<ProblemType>::closeTimestep(AdaptInfo *adaptInfo)
{ FUNCNAME("BaseProblem::closeTimestep()");

  int outputPeriod = 1;
  Initfile::get("user parameter->write every ith timestep", outputPeriod);
  if (adaptInfo->getTimestepNumber() % outputPeriod == 0
    && adaptInfo->getStartTime() < adaptInfo->getEndTime()) {
    writeFiles(adaptInfo, false);
  }
};



template<typename ProblemType>
void BaseProblem<ProblemType>::addTimeOperator(ProblemStat *prob, int i, int j)
{ FUNCNAME("BaseProblem::addTimeOperator()");

  Operator *op = new Operator(prob->getFeSpace(i), prob->getFeSpace(j));
    op->addZeroOrderTerm(new Simple_ZOT);
  Operator *opRhs = new Operator(prob->getFeSpace(i));
    opRhs->addZeroOrderTerm(new VecAtQP_ZOT(prob->getSolution()->getDOFVector(j), NULL));

  prob->addMatrixOperator(op, i, j, getInvTau(), getInvTau());
  prob->addVectorOperator(opRhs, i, getInvTau(), getInvTau());
};


template<typename ProblemType>
void BaseProblem<ProblemType>::addTimeOperator(RosenbrockStationary *prob, int i, int j)
{ FUNCNAME("BaseProblem::addTimeOperator()");

  prob->addTimeOperator(i,j);
};