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 
117  virtual void
118  WriteToFile(const ControlVector<VECTOR> &v, std::string name,
119  std::string dof_type)=0;
120 
121  /******************************************************/
122 
129  virtual void
130  WriteToFile(const std::vector<double> &v, std::string outfile) =0;
131 
132  void
134  {
135  OutputHandler_ = OH;
136  }
137  void
139  {
140  ExceptionHandler_ = OH;
141  }
142 
145  {
146  assert(ExceptionHandler_);
147  return ExceptionHandler_;
148  }
151  {
152  assert(OutputHandler_);
153  return OutputHandler_;
154  }
155 
164  double
165  GetFunctionalValue(std::string name) const
166  {
167  typename std::map<std::string, unsigned int>::const_iterator it =
168  GetFunctionalPosition().find(name);
169  if (it == GetFunctionalPosition().end())
170  {
171  throw DOpEException(
172  "Did not find " + name + " in the list of functionals.",
173  "ReducedProblemInterface_Base::GetFunctionalValue");
174  }
175  unsigned int pos = it->second;
176 
177  if (GetFunctionalValues()[pos].size() != 1)
178  {
179  if (GetFunctionalValues()[0].size() == 0)
180  throw DOpEException(
181  "Apparently the Functional in question was never evaluated!",
182  "ReducedProblemInterface_Base::GetFunctionalValue");
183  else
184  throw DOpEException(
185  "The Functional has been evaluated too many times! \n\tMaybe you should use GetTimeFunctionalValue.",
186  "ReducedProblemInterface_Base::GetFunctionalValue");
187 
188  }
189  else
190  {
191  return GetFunctionalValues()[pos][0];
192  }
193  }
194 
202  const std::vector<double>&
203  GetTimeFunctionalValue(std::string name) const
204  {
205  typename std::map<std::string, unsigned int>::const_iterator it =
206  GetFunctionalPosition().find(name);
207  if (it == GetFunctionalPosition().end())
208  {
209  throw DOpEException(
210  "Did not find " + name + " in the list of functionals.",
211  "ReducedProblemInterface_Base::GetFunctionalValue");
212  }
213  unsigned int pos = it->second;
214 
215  if (GetFunctionalValues()[pos].size != 1)
216  {
217  if (GetFunctionalValues()[0].size() == 0)
218  throw DOpEException(
219  "Apparently the Functional in question was never evaluated!",
220  "ReducedProblemInterface_Base::GetTimeFunctionalValue");
221  else
222  throw DOpEException(
223  "The Functional has been evaluated too many times! \n\tMaybe you should use GetTimeFunctionalValue.",
224  "ReducedProblemInterface_Base::GetTimeFunctionalValue");
225  }
226  else
227  {
228  return GetFunctionalValues()[pos];
229  }
230  }
231 
232 
242  void AddUserDomainData(std::string name,const VECTOR* new_data)
243  {
244  if (user_domain_data_.find(name) != user_domain_data_.end())
245  {
246  throw DOpEException(
247  "Adding multiple Data with name " + name + " is prohibited!",
248  "ReducedProblemInterface::AddUserDomainData");
249  }
250  user_domain_data_.insert(
251  std::pair<std::string, const VECTOR*>(name, new_data));
252  }
253 
254 
260  std::string name)
261  {
262  typename std::map<std::string, const VECTOR *>::iterator it =
263  user_domain_data_.find(name);
264  if (it == user_domain_data_.end())
265  {
266  throw DOpEException(
267  "Deleting Data " + name + " is impossible! Data not found",
268  "Integrator::DeleteDomainData");
269  }
270  user_domain_data_.erase(it);
271  }
272  protected:
278  void
280  {
281  //Initializing Functional Values
282  GetFunctionalValues().resize(N);
283  for (unsigned int i = 0; i < GetFunctionalValues().size(); i++)
284  {
285  GetFunctionalValues()[i].resize(0);
286  }
287  }
288  std::vector<std::vector<double> >&
290  {
291  return functional_values_;
292  }
293 
294  const std::vector<std::vector<double> >&
296  {
297  return functional_values_;
298  }
299 
300  const std::map<std::string, const VECTOR*>&
302  {
303  return user_domain_data_;
304  }
305 
316  virtual const std::map<std::string, unsigned int>&
318  {
319  throw DOpEException("Method not implemented",
320  "ReducedProblemInterface_Base::GetFunctionalPosition");
321  }
322  ;
323 
324  private:
325  DOpEExceptionHandler<VECTOR>* ExceptionHandler_;
326  DOpEOutputHandler<VECTOR>* OutputHandler_;
327  std::vector<std::vector<double> > functional_values_;
328  std::map<std::string, const VECTOR*> user_domain_data_;
329  };
330 
335  template<typename PROBLEM, typename VECTOR>
337  {
338  public:
339  ReducedProblemInterface(PROBLEM *OP, int base_priority = 0)
340  : ReducedProblemInterface_Base<VECTOR>()
341  {
342  OP_ = OP;
343  base_priority_ = base_priority;
344  post_index_ = "_" + this->GetProblem()->GetName();
345  }
347  {
348  }
349 
350  /******************************************************/
351 
357  virtual void
359  {
360  this->GetProblem()->ReInit("reduced");
361  }
362 
363  /******************************************************/
364 
375  virtual bool
377  ConstraintVector<VECTOR>& g) = 0;
378 
379  /******************************************************/
380 
388  virtual void
390  ControlVector<VECTOR>& ub)= 0;
391 
392  /******************************************************/
393 
403  virtual void
405  ControlVector<VECTOR>& gradient,
406  ControlVector<VECTOR>& gradient_transposed)=0;
407 
408  /******************************************************/
409 
415  virtual double
417 
418  /******************************************************/
419 
426  virtual void
428 
429  /******************************************************/
430 
440  virtual void
442  const ControlVector<VECTOR>& direction,
443  ControlVector<VECTOR>& hessian_direction,
444  ControlVector<VECTOR>& hessian_direction_transposed)=0;
445 
446  virtual void
448  const ControlVector<VECTOR>& /*direction*/,
449  ControlVector<VECTOR>& /*hessian_direction*/)
450  {
451  throw DOpEException("Method not implemented",
452  "ReducedProblemInterface::ComputeReducedHessianInverseVector");
453  }
454 
468  virtual void
470  const ControlVector<VECTOR>& /*q*/,
471  const ConstraintVector<VECTOR>& /*g*/,
472  ControlVector<VECTOR>& /*gradient*/,
473  ControlVector<VECTOR>& /*gradient_transposed*/)
474 
475  {
476  throw DOpEException("Method not implemented",
477  "ReducedProblemInterface::ComputeReducedGradientOfGlobalConstraints");
478  }
479 
480  /*****************************************************/
485  void
486  SetProblemType(std::string type, unsigned int num = 0)
487  {
488  this->GetProblem()->SetType(type, num);
489  }
490  PROBLEM*
492  {
493  return OP_;
494  }
495 
496  const PROBLEM*
497  GetProblem() const
498  {
499  return OP_;
500  }
504  template<class DWRC>
505  void
506  InitializeDWRC(DWRC& dwrc)
507  {
508  dwrc.Initialize(GetProblem()->GetSpaceTimeHandler(),
509  GetProblem()->GetControlNBlocks(),
510  GetProblem()->GetControlBlockComponent(),
511  GetProblem()->GetStateNBlocks(),
512  GetProblem()->GetStateBlockComponent());
513  }
514 
515  protected:
516  virtual const std::map<std::string, unsigned int>&
518  {
519  return GetProblem()->GetFunctionalPosition();
520  }
521  std::string
522  GetPostIndex() const
523  {
524  return post_index_;
525  }
526  int
528  {
529  return base_priority_;
530  }
531  private:
532  PROBLEM* OP_;
533  int base_priority_;
534  std::string post_index_;
535  };
536 
537 }
538 #endif
Definition: constraintvector.h:48
DOpEExceptionHandler< VECTOR > * GetExceptionHandler()
Definition: reducedprobleminterface.h:144
void DeleteUserDomainData(std::string name)
Definition: reducedprobleminterface.h:259
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:138
Definition: optproblemcontainer.h:70
void InitializeFunctionalValues(unsigned int N)
Definition: reducedprobleminterface.h:279
Definition: controlvector.h:49
DOpEOutputHandler< VECTOR > * GetOutputHandler()
Definition: reducedprobleminterface.h:150
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:491
void AddUserDomainData(std::string name, const VECTOR *new_data)
Definition: reducedprobleminterface.h:242
Definition: outputhandler.h:42
virtual const std::map< std::string, unsigned int > & GetFunctionalPosition() const
Definition: reducedprobleminterface.h:317
std::string GetPostIndex() const
Definition: reducedprobleminterface.h:522
double GetFunctionalValue(std::string name) const
Definition: reducedprobleminterface.h:165
std::vector< std::vector< double > > & GetFunctionalValues()
Definition: reducedprobleminterface.h:289
virtual void ComputeReducedHessianInverseVector(const ControlVector< VECTOR > &, const ControlVector< VECTOR > &, ControlVector< VECTOR > &)
Definition: reducedprobleminterface.h:447
void InitializeDWRC(DWRC &dwrc)
Definition: reducedprobleminterface.h:506
virtual void ComputeReducedGradientOfGlobalConstraints(unsigned int, const ControlVector< VECTOR > &, const ConstraintVector< VECTOR > &, ControlVector< VECTOR > &, ControlVector< VECTOR > &)
Definition: reducedprobleminterface.h:469
void RegisterOutputHandler(DOpEOutputHandler< VECTOR > *OH)
Definition: reducedprobleminterface.h:133
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:339
~ReducedProblemInterface()
Definition: reducedprobleminterface.h:346
Definition: reducedprobleminterface.h:336
const std::map< std::string, const VECTOR * > & GetUserDomainData() const
Definition: reducedprobleminterface.h:301
virtual const std::map< std::string, unsigned int > & GetFunctionalPosition() const
Definition: reducedprobleminterface.h:517
int GetBasePriority() const
Definition: reducedprobleminterface.h:527
virtual void ComputeReducedFunctionals(const ControlVector< VECTOR > &q)=0
const std::vector< double > & GetTimeFunctionalValue(std::string name) const
Definition: reducedprobleminterface.h:203
virtual double ComputeReducedCostFunctional(const ControlVector< VECTOR > &q)=0
const std::vector< std::vector< double > > & GetFunctionalValues() const
Definition: reducedprobleminterface.h:295
void SetProblemType(std::string type, unsigned int num=0)
Definition: reducedprobleminterface.h:486
virtual void ComputeReducedGradient(const ControlVector< VECTOR > &q, ControlVector< VECTOR > &gradient, ControlVector< VECTOR > &gradient_transposed)=0
const PROBLEM * GetProblem() const
Definition: reducedprobleminterface.h:497
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:358
Definition: optproblemcontainer.h:72