MittelmannBndryCntrlDiri3Dsin.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright (C) 2005, 2007 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: MittelmannBndryCntrlDiri3Dsin.hpp 2005 2011-06-06 12:55:16Z stefan $
  6. //
  7. // Authors: Andreas Waechter IBM 2005-10-18
  8. // Olaf Schenk (Univ. of Basel) 2007-08-01
  9. // modified MittelmannBndryCntrlDiri.hpp for 3-dim problem
  10. #ifndef __MITTELMANNBNDRYCNTRLDIRI3DSIN_HPP__
  11. #define __MITTELMANNBNDRYCNTRLDIRI3DSIN_HPP__
  12. #include "RegisteredTNLP.hpp"
  13. #ifdef HAVE_CONFIG_H
  14. #include "config.h"
  15. #else
  16. #include "configall_system.h"
  17. #endif
  18. #ifdef HAVE_CMATH
  19. # include <cmath>
  20. #else
  21. # ifdef HAVE_MATH_H
  22. # include <math.h>
  23. # else
  24. # error "don't have header file for math"
  25. # endif
  26. #endif
  27. #ifdef HAVE_CSTDIO
  28. # include <cstdio>
  29. #else
  30. # ifdef HAVE_STDIO_H
  31. # include <stdio.h>
  32. # else
  33. # error "don't have header file for stdio"
  34. # endif
  35. #endif
  36. using namespace Ipopt;
  37. /** Base class for boundary control problems with Dirichlet boundary
  38. * conditions, as formulated by Hans Mittelmann as Examples 1-4 in
  39. * "Optimization Techniques for Solving Elliptic Control Problems
  40. * with Control and State Constraints. Part 2: Boundary Control"
  41. *
  42. * Here, the control variables are identical to the values of y on
  43. * the boundary, and therefore we don't need any explicit
  44. * optimization variables for u.
  45. */
  46. class MittelmannBndryCntrlDiriBase3Dsin : public RegisteredTNLP
  47. {
  48. public:
  49. /** Constructor. */
  50. MittelmannBndryCntrlDiriBase3Dsin();
  51. /** Default destructor */
  52. virtual ~MittelmannBndryCntrlDiriBase3Dsin();
  53. /**@name Overloaded from TNLP */
  54. //@{
  55. /** Method to return some info about the nlp */
  56. virtual bool get_nlp_info(Index& n, Index& m, Index& nnz_jac_g,
  57. Index& nnz_h_lag, IndexStyleEnum& index_style);
  58. /** Method to return the bounds for my problem */
  59. virtual bool get_bounds_info(Index n, Number* x_l, Number* x_u,
  60. Index m, Number* g_l, Number* g_u);
  61. /** Method to return the starting point for the algorithm */
  62. virtual bool get_starting_point(Index n, bool init_x, Number* x,
  63. bool init_z, Number* z_L, Number* z_U,
  64. Index m, bool init_lambda,
  65. Number* lambda);
  66. /** Method to return the objective value */
  67. virtual bool eval_f(Index n, const Number* x, bool new_x, Number& obj_value);
  68. /** Method to return the gradient of the objective */
  69. virtual bool eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f);
  70. /** Method to return the constraint residuals */
  71. virtual bool eval_g(Index n, const Number* x, bool new_x, Index m, Number* g);
  72. /** Method to return:
  73. * 1) The structure of the jacobian (if "values" is NULL)
  74. * 2) The values of the jacobian (if "values" is not NULL)
  75. */
  76. virtual bool eval_jac_g(Index n, const Number* x, bool new_x,
  77. Index m, Index nele_jac, Index* iRow, Index *jCol,
  78. Number* values);
  79. /** Method to return:
  80. * 1) The structure of the hessian of the lagrangian (if "values" is NULL)
  81. * 2) The values of the hessian of the lagrangian (if "values" is not NULL)
  82. */
  83. virtual bool eval_h(Index n, const Number* x, bool new_x,
  84. Number obj_factor, Index m, const Number* lambda,
  85. bool new_lambda, Index nele_hess, Index* iRow,
  86. Index* jCol, Number* values);
  87. //@}
  88. /** Method for returning scaling parameters */
  89. virtual bool get_scaling_parameters(Number& obj_scaling,
  90. bool& use_x_scaling, Index n,
  91. Number* x_scaling,
  92. bool& use_g_scaling, Index m,
  93. Number* g_scaling);
  94. /** @name Solution Methods */
  95. //@{
  96. /** This method is called after the optimization, and could write an
  97. * output file with the optimal profiles */
  98. virtual void finalize_solution(SolverReturn status,
  99. Index n, const Number* x, const Number* z_L, const Number* z_U,
  100. Index m, const Number* g, const Number* lambda,
  101. Number obj_valu,
  102. const IpoptData* ip_data,
  103. IpoptCalculatedQuantities* ip_cq);
  104. //@}
  105. protected:
  106. /** Method for setting the internal parameters that define the
  107. * problem. It must be called by the child class in its
  108. * implementation of InitializeParameters. */
  109. void SetBaseParameters(Index N, Number alpha, Number lb_y,
  110. Number ub_y, Number lb_u, Number ub_u,
  111. Number d_const);
  112. /**@name Functions that defines a particular instance. */
  113. //@{
  114. /** Target profile function for y */
  115. virtual Number y_d_cont(Number x1, Number x2, Number x3) const =0;
  116. //@}
  117. private:
  118. /**@name Methods to block default compiler methods.
  119. * The compiler automatically generates the following three methods.
  120. * Since the default compiler implementation is generally not what
  121. * you want (for all but the most simple classes), we usually
  122. * put the declarations of these methods in the private section
  123. * and never implement them. This prevents the compiler from
  124. * implementing an incorrect "default" behavior without us
  125. * knowing. (See Scott Meyers book, "Effective C++")
  126. *
  127. */
  128. //@{
  129. MittelmannBndryCntrlDiriBase3Dsin(const MittelmannBndryCntrlDiriBase3Dsin&);
  130. MittelmannBndryCntrlDiriBase3Dsin& operator=(const MittelmannBndryCntrlDiriBase3Dsin&);
  131. //@}
  132. /**@name Problem specification */
  133. //@{
  134. /** Number of mesh points in one dimension (excluding boundary) */
  135. Index N_;
  136. /** Step size */
  137. Number h_;
  138. /** h_ squaredd */
  139. Number hh_;
  140. /** overall lower bound on y */
  141. Number lb_y_;
  142. /** overall upper bound on y */
  143. Number ub_y_;
  144. /** overall lower bound on u */
  145. Number lb_u_;
  146. /** overall upper bound on u */
  147. Number ub_u_;
  148. /** Constant value of d appearing in elliptical equation */
  149. Number d_const_;
  150. /** Weighting parameter for the control target deviation functional
  151. * in the objective */
  152. Number alpha_;
  153. /** Array for the target profile for y */
  154. Number* y_d_;
  155. //@}
  156. /**@name Auxilliary methods */
  157. //@{
  158. /** Translation of mesh point indices to NLP variable indices for
  159. * y(x_ijk) */
  160. inline Index y_index(Index i, Index j, Index k) const
  161. {
  162. return k + (N_+2)*j + (N_+2)*(N_+2)*i;
  163. }
  164. /** Translation of interior mesh point indices to the corresponding
  165. * PDE constraint number */
  166. inline Index pde_index(Index i, Index j, Index k) const
  167. {
  168. return (k-1) + N_*(j-1) + N_*N_*(i-1);
  169. }
  170. /** Compute the grid coordinate for given index in x1 direction */
  171. inline Number x1_grid(Index i) const
  172. {
  173. return h_*(Number)i;
  174. }
  175. /** Compute the grid coordinate for given index in x2 direction */
  176. inline Number x2_grid(Index i) const
  177. {
  178. return h_*(Number)i;
  179. }
  180. /** Compute the grid coordinate for given index in x3 direction */
  181. inline Number x3_grid(Index i) const
  182. {
  183. return h_*(Number)i;
  184. }
  185. //@}
  186. };
  187. /** Class implementating Example 1 */
  188. class MittelmannBndryCntrlDiri3Dsin : public MittelmannBndryCntrlDiriBase3Dsin
  189. {
  190. public:
  191. MittelmannBndryCntrlDiri3Dsin()
  192. {}
  193. virtual ~MittelmannBndryCntrlDiri3Dsin()
  194. {}
  195. virtual bool InitializeProblem(Index N)
  196. {
  197. if (N<1) {
  198. printf("N has to be at least 1.");
  199. return false;
  200. }
  201. printf("olaf N %d has to be at least 1.", N);
  202. Number alpha = 0.1;
  203. Number lb_y = -1e20;
  204. Number ub_y = 3.5;
  205. Number lb_u = 0.;
  206. Number ub_u = 10.;
  207. Number d_const = -20.;
  208. SetBaseParameters(N, alpha, lb_y, ub_y, lb_u, ub_u, d_const);
  209. return true;
  210. }
  211. protected:
  212. /** Target profile function for y */
  213. virtual Number y_d_cont(Number x1, Number x2, Number x3) const
  214. {
  215. return 3. + 5.*(x1*(x1-1.)*x2*(x2-1.)*x3*(x3-1.));
  216. }
  217. private:
  218. /**@name hide implicitly defined contructors copy operators */
  219. //@{
  220. MittelmannBndryCntrlDiri3Dsin(const MittelmannBndryCntrlDiri3Dsin&);
  221. MittelmannBndryCntrlDiri3Dsin& operator=(const MittelmannBndryCntrlDiri3Dsin&);
  222. //@}
  223. };
  224. #endif