DOpE
reducedprobleminterface.h
Go to the documentation of this file.
1 
24 #ifndef SOLVER_INTERFACE_H_
25 #define SOLVER_INTERFACE_H_
26 
27 #include "dopeexceptionhandler.h"
28 #include "outputhandler.h"
29 #include "controlvector.h"
30 #include "constraintvector.h"
31 #include "dopetypes.h"
32 #include "dwrdatacontainer.h"
33 
34 #include <assert.h>
35 #include <deal.II/numerics/data_out.h>
36 #include <deal.II/lac/vector.h>
37 
38 namespace DOpE
39 {
40  //Predeclaration necessary
41  template<typename VECTOR>
42  class DOpEOutputHandler;
43  template<typename VECTOR>
44  class DOpEExceptionHandler;
46 
50  template<typename VECTOR>
51  class ReducedProblemInterface_Base
52  {
53  public:
55  {
56  ExceptionHandler_ = NULL;
57  OutputHandler_ = NULL;
58  }
59 
60  virtual
62  {
63  }
64  ;
70  virtual void
71  StateSizeInfo(std::stringstream& out)=0;
72 
73  /******************************************************/
74 
84  virtual void
85  WriteToFile(const VECTOR &v, std::string name, std::string outfile,
86  std::string dof_type, std::string filetype)=0;
87 
88  /******************************************************/
89 
99  virtual void
100  WriteToFileElementwise(const Vector<double> &/*v*/, std::string /*name*/,
101  std::string /*outfile*/, std::string /*dof_type*/,
102  std::string /*filetype*/)
103  {
104  throw DOpEException("NotImplemented", "WriteToFileElementwise");
105  }
106 
107  /******************************************************/
108 
116  virtual void
117  WriteToFile(const ControlVector<VECTOR> &v, std::string name,
118  std::string dof_type)=0;
119 
120  /******************************************************/
121 
128  virtual void
129  WriteToFile(const std::vector<double> &v, std::string outfile) =0;
130 
131  void
133  {
134  OutputHandler_ = OH;
135  }
136  void
138  {
139  ExceptionHandler_ = OH;
140  }
141 
144  {
145  assert(ExceptionHandler_);
146  return ExceptionHandler_;
147  }
150  {
151  assert(OutputHandler_);
152  return OutputHandler_;
153  }
154 
163  double
164  GetFunctionalValue(std::string name) const
165  {
166  typename std::map<std::string, unsigned int>::const_iterator it =
167  GetFunctionalPosition().find(name);
168  if (it == GetFunctionalPosition().end())
169  {
170  throw DOpEException(
171  "Did not find " + name + " in the list of functionals.",
172  "ReducedProblemInterface_Base::GetFunctionalValue");
173  }
174  unsigned int pos = it->second;
175 
176  if (GetFunctionalValues()[pos].size() != 1)
177  {
178  if (GetFunctionalValues()[0].size() == 0)
179  throw DOpEException(
180  "Apparently the Functional in question was never evaluated!",
181  "ReducedProblemInterface_Base::GetFunctionalValue");
182  else
183  throw DOpEException(
184  "The Functional has been evaluated too many times! \n\tMaybe you should use GetTimeFunctionalValue.",
185  "ReducedProblemInterface_Base::GetFunctionalValue");
186 
187  }
188  else
189  {
190  return GetFunctionalValues()[pos][0];
191  }
192  }
193 
201  const std::vector<double>&
202  GetTimeFunctionalValue(std::string name) const
203  {
204  typename std::map<std::string, unsigned int>::const_iterator it =
205  GetFunctionalPosition().find(name);
206  if (it == GetFunctionalPosition().end())
207  {
208  throw DOpEException(
209  "Did not find " + name + " in the list of functionals.",
210  "ReducedProblemInterface_Base::GetFunctionalValue");
211  }
212  unsigned int pos = it->second;
213 
214  if (GetFunctionalValues()[pos].size != 1)
215  {
216  if (GetFunctionalValues()[0].size() == 0)
217  throw DOpEException(
218  "Apparently the Functional in question was never evaluated!",
219  "ReducedProblemInterface_Base::GetTimeFunctionalValue");
220  else
221  throw DOpEException(
222  "The Functional has been evaluated too many times! \n\tMaybe you should use GetTimeFunctionalValue.",
223  "ReducedProblemInterface_Base::GetTimeFunctionalValue");
224  }
225  else
226  {
227  return GetFunctionalValues()[pos];
228  }
229  }
230 
231 
241  void AddUserDomainData(std::string name,const VECTOR* new_data)
242  {
243  if (user_domain_data_.find(name) != user_domain_data_.end())
244  {
245  throw DOpEException(
246  "Adding multiple Data with name " + name + " is prohibited!",
247  "ReducedProblemInterface::AddUserDomainData");
248  }
249  user_domain_data_.insert(
250  std::pair<std::string, const VECTOR*>(name, new_data));
251  }
252 
253 
259  std::string name)
260  {
261  typename std::map<std::string, const VECTOR *>::iterator it =
262  user_domain_data_.find(name);
263  if (it == user_domain_data_.end())
264  {
265  throw DOpEException(
266  "Deleting Data " + name + " is impossible! Data not found",
267  "Integrator::DeleteDomainData");
268  }
269  user_domain_data_.erase(it);
270  }
271  protected:
277  void
279  {
280  //Initializing Functional Values
281  GetFunctionalValues().resize(N);
282  for (unsigned int i = 0; i < GetFunctionalValues().size(); i++)
283  {
284  GetFunctionalValues()[i].resize(0);
285  }
286  }
287  std::vector<std::vector<double> >&
289  {
290  return functional_values_;
291  }
292 
293  const std::vector<std::vector<double> >&
295  {
296  return functional_values_;
297  }
298 
299  const std::map<std::string, const VECTOR*>&
301  {
302  return user_domain_data_;
303  }
304 
315  virtual const std::map<std::string, unsigned int>&
317  {
318  throw DOpEException("Method not implemented",
319  "ReducedProblemInterface_Base::GetFunctionalPosition");
320  }
321  ;
322 
323  private:
324  DOpEExceptionHandler<VECTOR>* ExceptionHandler_;
325  DOpEOutputHandler<VECTOR>* OutputHandler_;
326  std::vector<std::vector<double> > functional_values_;
327  std::map<std::string, const VECTOR*> user_domain_data_;
328  };
329 
334  template<typename PROBLEM, typename VECTOR>
336  {
337  public:
338  ReducedProblemInterface(PROBLEM *OP, int base_priority = 0)
339  : ReducedProblemInterface_Base<VECTOR>()
340  {
341  OP_ = OP;
342  base_priority_ = base_priority;
343  post_index_ = "_" + this->GetProblem()->GetName();
344  }
346  {
347  }
348 
349  /******************************************************/
350 
356  virtual void
358  {
359  this->GetProblem()->ReInit("reduced");
360  }
361 
362  /******************************************************/
363 
374  virtual bool
376  ConstraintVector<VECTOR>& g) = 0;
377 
378  /******************************************************/
379 
387  virtual void
389  ControlVector<VECTOR>& ub)= 0;
390 
391  /******************************************************/
392 
402  virtual void
404  ControlVector<VECTOR>& gradient,
405  ControlVector<VECTOR>& gradient_transposed)=0;
406 
407  /******************************************************/
408 
414  virtual double
416 
417  /******************************************************/
418 
425  virtual void
427 
428  /******************************************************/
429 
439  virtual void
441  const ControlVector<VECTOR>& direction,
442  ControlVector<VECTOR>& hessian_direction,
443  ControlVector<VECTOR>& hessian_direction_transposed)=0;
444 
445  virtual void
447  const ControlVector<VECTOR>& /*direction*/,
448  ControlVector<VECTOR>& /*hessian_direction*/)
449  {
450  throw DOpEException("Method not implemented",
451  "ReducedProblemInterface::ComputeReducedHessianInverseVector");
452  }
453 
467  virtual void
469  const ControlVector<VECTOR>& /*q*/,
470  const ConstraintVector<VECTOR>& /*g*/,
471  ControlVector<VECTOR>& /*gradient*/,
472  ControlVector<VECTOR>& /*gradient_transposed*/)
473 
474  {
475  throw DOpEException("Method not implemented",
476  "ReducedProblemInterface::ComputeReducedGradientOfGlobalConstraints");
477  }
478 
479  /*****************************************************/
484  void
485  SetProblemType(std::string type, unsigned int num = 0)
486  {
487  this->GetProblem()->SetType(type, num);
488  }
489  PROBLEM*
491  {
492  return OP_;
493  }
494 
495  const PROBLEM*
496  GetProblem() const
497  {
498  return OP_;
499  }
503  template<class DWRC>
504  void
505  InitializeDWRC(DWRC& dwrc)
506  {
507  dwrc.Initialize(GetProblem()->GetSpaceTimeHandler(),
508  GetProblem()->GetControlNBlocks(),
509  GetProblem()->GetControlBlockComponent(),
510  GetProblem()->GetStateNBlocks(),
511  GetProblem()->GetStateBlockComponent());
512  }
513 
514  protected:
515  virtual const std::map<std::string, unsigned int>&
517  {
518  return GetProblem()->GetFunctionalPosition();
519  }
520  std::string
521  GetPostIndex() const
522  {
523  return post_index_;
524  }
525  int
527  {
528  return base_priority_;
529  }
530  private:
531  PROBLEM* OP_;
532  int base_priority_;
533  std::string post_index_;
534  };
535 
536 }
537 #endif
Definition: constraintvector.h:48
DOpEExceptionHandler< VECTOR > * GetExceptionHandler()
Definition: reducedprobleminterface.h:143
void DeleteUserDomainData(std::string name)
Definition: reducedprobleminterface.h:258
virtual void WriteToFile(const VECTOR &v, std::string name, std::string outfile, std::string dof_type, std::string filetype)=0
virtual void StateSizeInfo(std::stringstream &out)=0
virtual bool ComputeReducedConstraints(const ControlVector< VECTOR > &q, ConstraintVector< VECTOR > &g)=0
void RegisterExceptionHandler(DOpEExceptionHandler< VECTOR > *OH)
Definition: reducedprobleminterface.h:137
Definition: optproblemcontainer.h:70
void InitializeFunctionalValues(unsigned int N)
Definition: reducedprobleminterface.h:278
Definition: controlvector.h:49
DOpEOutputHandler< VECTOR > * GetOutputHandler()
Definition: reducedprobleminterface.h:149
virtual void ComputeReducedHessianVector(const ControlVector< VECTOR > &q, const ControlVector< VECTOR > &direction, ControlVector< VECTOR > &hessian_direction, ControlVector< VECTOR > &hessian_direction_transposed)=0
PROBLEM * GetProblem()
Definition: reducedprobleminterface.h:490
void AddUserDomainData(std::string name, const VECTOR *new_data)
Definition: reducedprobleminterface.h:241
Definition: outputhandler.h:42
virtual const std::map< std::string, unsigned int > & GetFunctionalPosition() const
Definition: reducedprobleminterface.h:316
std::string GetPostIndex() const
Definition: reducedprobleminterface.h:521
double GetFunctionalValue(std::string name) const
Definition: reducedprobleminterface.h:164
std::vector< std::vector< double > > & GetFunctionalValues()
Definition: reducedprobleminterface.h:288
virtual void ComputeReducedHessianInverseVector(const ControlVector< VECTOR > &, const ControlVector< VECTOR > &, ControlVector< VECTOR > &)
Definition: reducedprobleminterface.h:446
void InitializeDWRC(DWRC &dwrc)
Definition: reducedprobleminterface.h:505
virtual void ComputeReducedGradientOfGlobalConstraints(unsigned int, const ControlVector< VECTOR > &, const ConstraintVector< VECTOR > &, ControlVector< VECTOR > &, ControlVector< VECTOR > &)
Definition: reducedprobleminterface.h:468
void RegisterOutputHandler(DOpEOutputHandler< VECTOR > *OH)
Definition: reducedprobleminterface.h:132
virtual void WriteToFileElementwise(const Vector< double > &, std::string, std::string, std::string, std::string)
Definition: reducedprobleminterface.h:100
ReducedProblemInterface(PROBLEM *OP, int base_priority=0)
Definition: reducedprobleminterface.h:338
~ReducedProblemInterface()
Definition: reducedprobleminterface.h:345
Definition: reducedprobleminterface.h:335
const std::map< std::string, const VECTOR * > & GetUserDomainData() const
Definition: reducedprobleminterface.h:300
virtual const std::map< std::string, unsigned int > & GetFunctionalPosition() const
Definition: reducedprobleminterface.h:516
int GetBasePriority() const
Definition: reducedprobleminterface.h:526
virtual void ComputeReducedFunctionals(const ControlVector< VECTOR > &q)=0
const std::vector< double > & GetTimeFunctionalValue(std::string name) const
Definition: reducedprobleminterface.h:202
virtual double ComputeReducedCostFunctional(const ControlVector< VECTOR > &q)=0
const std::vector< std::vector< double > > & GetFunctionalValues() const
Definition: reducedprobleminterface.h:294
void SetProblemType(std::string type, unsigned int num=0)
Definition: reducedprobleminterface.h:485
virtual void ComputeReducedGradient(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)=0
const PROBLEM * GetProblem() const
Definition: reducedprobleminterface.h:496
Definition: dopeexception.h:35
ReducedProblemInterface_Base()
Definition: reducedprobleminterface.h:54
virtual void GetControlBoxConstraints(ControlVector< VECTOR > &lb, ControlVector< VECTOR > &ub)=0
virtual ~ReducedProblemInterface_Base()
Definition: reducedprobleminterface.h:61
virtual void ReInit()
Definition: reducedprobleminterface.h:357
Definition: optproblemcontainer.h:72