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 <numerics/data_out.h>
36 
37 #include <lac/vector.h>
38 
39 namespace DOpE
40 {
41  //Predeclaration necessary
42  template<typename VECTOR>
43  class DOpEOutputHandler;
44  template<typename VECTOR>
45  class DOpEExceptionHandler;
47 
51  template<typename VECTOR>
52  class ReducedProblemInterface_Base
53  {
54  public:
56  {
57  _ExceptionHandler = NULL;
58  _OutputHandler = NULL;
59  }
60 
61  virtual
63  {
64  }
65  ;
71  virtual void
72  StateSizeInfo(std::stringstream& out)=0;
73 
74  /******************************************************/
75 
85  virtual void
86  WriteToFile(const VECTOR &v, std::string name, std::string outfile,
87  std::string dof_type, std::string filetype)=0;
88 
89  /******************************************************/
90 
100  virtual void
101  WriteToFileElementwise(const Vector<double> &/*v*/, std::string /*name*/,
102  std::string /*outfile*/, std::string /*dof_type*/,
103  std::string /*filetype*/)
104  {
105  throw DOpEException("NotImplemented", "WriteToFileElementwise");
106  }
107 
108  /******************************************************/
109 
119  virtual void
120  WriteToFile(const ControlVector<VECTOR> &v, std::string name,
121  std::string outfile, std::string dof_type, std::string filetype)=0;
122 
123  /******************************************************/
124 
131  virtual void
132  WriteToFile(const std::vector<double> &v, std::string outfile) =0;
133 
134  void
136  {
137  _OutputHandler = OH;
138  }
139  void
141  {
142  _ExceptionHandler = OH;
143  }
144 
147  {
148  assert(_ExceptionHandler);
149  return _ExceptionHandler;
150  }
153  {
154  assert(_OutputHandler);
155  return _OutputHandler;
156  }
157 
166  double
167  GetFunctionalValue(std::string name) const
168  {
169  typename std::map<std::string, unsigned int>::const_iterator it =
170  GetFunctionalPosition().find(name);
171  if (it == GetFunctionalPosition().end())
172  {
173  throw DOpEException(
174  "Did not find " + name + " in the list of functionals.",
175  "ReducedProblemInterface_Base::GetFunctionalValue");
176  }
177  unsigned int pos = it->second;
178 
179  if (GetFunctionalValues()[pos].size() != 1)
180  {
181  if (GetFunctionalValues()[0].size() == 0)
182  throw DOpEException(
183  "Apparently the Functional in question was never evaluated!",
184  "ReducedProblemInterface_Base::GetFunctionalValue");
185  else
186  throw DOpEException(
187  "The Functional has been evaluated too many times! \n\tMaybe you should use GetTimeFunctionalValue.",
188  "ReducedProblemInterface_Base::GetFunctionalValue");
189 
190  }
191  else
192  {
193  return GetFunctionalValues()[pos][0];
194  }
195  }
196 
204  const std::vector<double>&
205  GetTimeFunctionalValue(std::string name) const
206  {
207  typename std::map<std::string, unsigned int>::const_iterator it =
208  GetFunctionalPosition().find(name);
209  if (it == GetFunctionalPosition().end())
210  {
211  throw DOpEException(
212  "Did not find " + name + " in the list of functionals.",
213  "ReducedProblemInterface_Base::GetFunctionalValue");
214  }
215  unsigned int pos = it->second;
216 
217  if (GetFunctionalValues()[pos].size != 1)
218  {
219  if (GetFunctionalValues()[0].size() == 0)
220  throw DOpEException(
221  "Apparently the Functional in question was never evaluated!",
222  "ReducedProblemInterface_Base::GetTimeFunctionalValue");
223  else
224  throw DOpEException(
225  "The Functional has been evaluated too many times! \n\tMaybe you should use GetTimeFunctionalValue.",
226  "ReducedProblemInterface_Base::GetTimeFunctionalValue");
227  }
228  else
229  {
230  return GetFunctionalValues()[pos];
231  }
232  }
233 
234 
244  void AddUserDomainData(std::string name,const VECTOR* new_data)
245  {
246  if (_user_domain_data.find(name) != _user_domain_data.end())
247  {
248  throw DOpEException(
249  "Adding multiple Data with name " + name + " is prohibited!",
250  "ReducedProblemInterface::AddUserDomainData");
251  }
252  _user_domain_data.insert(
253  std::pair<std::string, const VECTOR*>(name, new_data));
254  }
255 
256 
262  std::string name)
263  {
264  typename std::map<std::string, const VECTOR *>::iterator it =
265  _user_domain_data.find(name);
266  if (it == _user_domain_data.end())
267  {
268  throw DOpEException(
269  "Deleting Data " + name + " is impossible! Data not found",
270  "Integrator::DeleteDomainData");
271  }
272  _user_domain_data.erase(it);
273  }
274  protected:
280  void
282  {
283  //Initializing Functional Values
284  GetFunctionalValues().resize(N);
285  for (unsigned int i = 0; i < GetFunctionalValues().size(); i++)
286  {
287  GetFunctionalValues()[i].resize(0);
288  }
289  }
290  std::vector<std::vector<double> >&
292  {
293  return _functional_values;
294  }
295 
296  const std::vector<std::vector<double> >&
298  {
299  return _functional_values;
300  }
301 
302  const std::map<std::string, const VECTOR*>&
304  {
305  return _user_domain_data;
306  }
307 
318  virtual const std::map<std::string, unsigned int>&
320  {
321  throw DOpEException("Method not implemented",
322  "ReducedProblemInterface_Base::GetFunctionalPosition");
323  }
324  ;
325 
326  private:
327  DOpEExceptionHandler<VECTOR>* _ExceptionHandler;
328  DOpEOutputHandler<VECTOR>* _OutputHandler;
329  std::vector<std::vector<double> > _functional_values;
330  std::map<std::string, const VECTOR*> _user_domain_data;
331  };
332 
337  template<typename PROBLEM, typename VECTOR>
339  {
340  public:
341  ReducedProblemInterface(PROBLEM *OP, int base_priority = 0)
342  : ReducedProblemInterface_Base<VECTOR>()
343  {
344  _OP = OP;
345  _base_priority = base_priority;
346  _post_index = "_" + this->GetProblem()->GetName();
347  }
349  {
350  }
351 
352  /******************************************************/
353 
359  virtual void
361  {
362  this->GetProblem()->ReInit("reduced");
363  }
364 
365  /******************************************************/
366 
377  virtual bool
379  ConstraintVector<VECTOR>& g) = 0;
380 
381  /******************************************************/
382 
390  virtual void
392  ControlVector<VECTOR>& ub)= 0;
393 
394  /******************************************************/
395 
405  virtual void
407  ControlVector<VECTOR>& gradient,
408  ControlVector<VECTOR>& gradient_transposed)=0;
409 
410  /******************************************************/
411 
417  virtual double
419 
420  /******************************************************/
421 
428  virtual void
430 
431  /******************************************************/
432 
442  virtual void
444  const ControlVector<VECTOR>& direction,
445  ControlVector<VECTOR>& hessian_direction,
446  ControlVector<VECTOR>& hessian_direction_transposed)=0;
447 
448  virtual void
450  const ControlVector<VECTOR>& /*direction*/,
451  ControlVector<VECTOR>& /*hessian_direction*/)
452  {
453  throw DOpEException("Method not implemented",
454  "ReducedProblemInterface::ComputeReducedHessianInverseVector");
455  }
456 
470  virtual void
472  const ControlVector<VECTOR>& /*q*/,
473  const ConstraintVector<VECTOR>& /*g*/,
474  ControlVector<VECTOR>& /*gradient*/,
475  ControlVector<VECTOR>& /*gradient_transposed*/)
476 
477  {
478  throw DOpEException("Method not implemented",
479  "ReducedProblemInterface::ComputeReducedGradientOfGlobalConstraints");
480  }
481 
482  /*****************************************************/
487  void
488  SetProblemType(std::string type, unsigned int num = 0)
489  {
490  this->GetProblem()->SetType(type, num);
491  }
492  PROBLEM*
494  {
495  return _OP;
496  }
497 
498  const PROBLEM*
499  GetProblem() const
500  {
501  return _OP;
502  }
506  template<class DWRC>
507  void
508  InitializeDWRC(DWRC& dwrc)
509  {
510  dwrc.Initialize(GetProblem()->GetSpaceTimeHandler(),
511  GetProblem()->GetControlNBlocks(),
512  GetProblem()->GetControlBlockComponent(),
513  GetProblem()->GetStateNBlocks(),
514  GetProblem()->GetStateBlockComponent());
515  }
516 
517  protected:
518  virtual const std::map<std::string, unsigned int>&
520  {
521  return GetProblem()->GetFunctionalPosition();
522  }
523  std::string
524  GetPostIndex() const
525  {
526  return _post_index;
527  }
528  int
530  {
531  return _base_priority;
532  }
533  private:
534  PROBLEM* _OP;
535  int _base_priority;
536  std::string _post_index;
537  };
538 
539 }
540 #endif
Definition: constraintvector.h:47
DOpEExceptionHandler< VECTOR > * GetExceptionHandler()
Definition: reducedprobleminterface.h:146
void DeleteUserDomainData(std::string name)
Definition: reducedprobleminterface.h:261
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:140
Definition: optproblemcontainer.h:70
void InitializeFunctionalValues(unsigned int N)
Definition: reducedprobleminterface.h:281
Definition: controlvector.h:48
DOpEOutputHandler< VECTOR > * GetOutputHandler()
Definition: reducedprobleminterface.h:152
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:493
void AddUserDomainData(std::string name, const VECTOR *new_data)
Definition: reducedprobleminterface.h:244
Definition: outputhandler.h:42
virtual const std::map< std::string, unsigned int > & GetFunctionalPosition() const
Definition: reducedprobleminterface.h:319
std::string GetPostIndex() const
Definition: reducedprobleminterface.h:524
double GetFunctionalValue(std::string name) const
Definition: reducedprobleminterface.h:167
std::vector< std::vector< double > > & GetFunctionalValues()
Definition: reducedprobleminterface.h:291
virtual void ComputeReducedHessianInverseVector(const ControlVector< VECTOR > &, const ControlVector< VECTOR > &, ControlVector< VECTOR > &)
Definition: reducedprobleminterface.h:449
void InitializeDWRC(DWRC &dwrc)
Definition: reducedprobleminterface.h:508
virtual void ComputeReducedGradientOfGlobalConstraints(unsigned int, const ControlVector< VECTOR > &, const ConstraintVector< VECTOR > &, ControlVector< VECTOR > &, ControlVector< VECTOR > &)
Definition: reducedprobleminterface.h:471
void RegisterOutputHandler(DOpEOutputHandler< VECTOR > *OH)
Definition: reducedprobleminterface.h:135
virtual void WriteToFileElementwise(const Vector< double > &, std::string, std::string, std::string, std::string)
Definition: reducedprobleminterface.h:101
ReducedProblemInterface(PROBLEM *OP, int base_priority=0)
Definition: reducedprobleminterface.h:341
~ReducedProblemInterface()
Definition: reducedprobleminterface.h:348
Definition: reducedprobleminterface.h:338
const std::map< std::string, const VECTOR * > & GetUserDomainData() const
Definition: reducedprobleminterface.h:303
virtual const std::map< std::string, unsigned int > & GetFunctionalPosition() const
Definition: reducedprobleminterface.h:519
int GetBasePriority() const
Definition: reducedprobleminterface.h:529
virtual void ComputeReducedFunctionals(const ControlVector< VECTOR > &q)=0
const std::vector< double > & GetTimeFunctionalValue(std::string name) const
Definition: reducedprobleminterface.h:205
virtual double ComputeReducedCostFunctional(const ControlVector< VECTOR > &q)=0
const std::vector< std::vector< double > > & GetFunctionalValues() const
Definition: reducedprobleminterface.h:297
void SetProblemType(std::string type, unsigned int num=0)
Definition: reducedprobleminterface.h:488
virtual void ComputeReducedGradient(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)=0
const PROBLEM * GetProblem() const
Definition: reducedprobleminterface.h:499
Definition: dopeexception.h:35
ReducedProblemInterface_Base()
Definition: reducedprobleminterface.h:55
virtual void GetControlBoxConstraints(ControlVector< VECTOR > &lb, ControlVector< VECTOR > &ub)=0
virtual ~ReducedProblemInterface_Base()
Definition: reducedprobleminterface.h:62
virtual void ReInit()
Definition: reducedprobleminterface.h:360
Definition: optproblemcontainer.h:72