Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
includeLinks/ObjectiveFunction.hpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Sandia Corporation and Argonne National
5  Laboratory. Under the terms of Contract DE-AC04-94AL85000
6  with Sandia Corporation, the U.S. Government retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
24  pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
25 
26  ***************************************************************** */
27 // -*- Mode : c++; tab-width: 3; c-tab-always-indent: t; indent-tabs-mode: nil; c-basic-offset: 3 -*-
28 
39 #ifndef OBJECTIVE_FUNCTION_HPP
40 #define OBJECTIVE_FUNCTION_HPP
41 
42 #include "Mesquite.hpp"
43 #include "MsqError.hpp"
44 #include "MsqVertex.hpp"
45 
46 #ifdef MSQ_USE_OLD_STD_HEADERS
47 # include <list.h>
48 #else
49 # include <list>
50 #endif
51 
52 namespace Mesquite
53 {
54  class PatchData;
55  class MsqHessian;
56  class QualityMetric;
57 
68  {
69  public:
71  useLocalGradient(false)
72  {}
73 
74 
75  // virtual destructor ensures use of polymorphism during destruction
77  {};
78 
82  virtual bool concrete_evaluate(PatchData &patch, double &fval,
83  MsqError &err)=0;
84 
94  bool evaluate(PatchData &patch, double &fval, MsqError &err)
95  {
96  bool return_bool = concrete_evaluate(patch, fval, err);
97  fval *= negateFlag;
98  return return_bool;
99  }
100 
103  {
106  };
108 
111  { gradType = grad; }
112 
113 
120  bool compute_gradient(PatchData &patch, Vector3D *const &grad,
121  double &OF_val, MsqError &err, size_t array_size=0);
122 
128  bool compute_hessian(PatchData &patch, MsqHessian &hessian,
129  Vector3D *const &grad,
130  double &OF_val,
131  MsqError &err);
132 
140  return qMetric;
141  }
142 
146  virtual msq_std::list<QualityMetric*> get_quality_metric_list()
147  {
148  msq_std::list<QualityMetric*> temp_list;
149  temp_list.push_front(qMetric);
150  return temp_list;
151  }
152 
155  {
156  qMetric=qm;
157  }
158 
164  void set_negate_flag(int neg)
165  {
166  negateFlag=neg;
167  }
168 
171  {
172  return negateFlag;
173  }
174 
175  protected:
176 
183  double &OF_val,
184  MsqError &err, size_t array_size);
185 
204  Vector3D *const &grad,
205  double &OF_val,
206  MsqError &err, size_t array_size);
207 
217  virtual bool compute_analytical_hessian(PatchData &/*patch*/,
218  MsqHessian &/*hessian*/,
219  Vector3D *const &/*grad*/,
220  double &/*OF_val*/,
221  MsqError &/*err*/);
222 
224  inline double get_eps(PatchData &pd, double &local_val,
225  int k,MsqVertex* vertex, MsqError &err);
226 
230  void set_use_local_gradient(bool new_bool)
231  {
232  useLocalGradient=new_bool;
233  }
234 
235 
236  private:
237 
239 
241 
249  };
250 
251 //BEGIN INLINE
252 
258  Vector3D *const &grad,
259  double &OF_val,
260  MsqError &err,
261  size_t array_size)
262  {
263  bool obj_bool = false;
264  switch(gradType){
265  case NUMERICAL_GRADIENT:
266  obj_bool=compute_numerical_gradient(patch, grad, OF_val,
267  err, array_size);
268  break;
269  case ANALYTICAL_GRADIENT:
270  obj_bool=compute_analytical_gradient(patch, grad, OF_val,
271  err, array_size);
272  break;
273  }
274  return !MSQ_CHKERR(err) && obj_bool;
275  }
276 
277 
283  MsqHessian &hessian,
284  Vector3D *const &grad,
285  double &OF_val,
286  MsqError &err)
287  {
288  bool result = compute_analytical_hessian(patch, hessian,
289  grad, OF_val, err);
290  return !MSQ_CHKERR(err) && result;
291  }
292 
293 
299  inline double ObjectiveFunction::get_eps(PatchData &pd, double &local_val,
300  int k,MsqVertex* vertex, MsqError& err)
301  {
302  double eps = 1.e-07;
303  // double rho=.5;
304  int imax=20;
305  int i=0;
306  bool feasible=false;
307  double tmp_var=0.0;
308  while (i<imax && !feasible)
309  {
310  i++;
311  //perturb kth coord val and check feas if needed
312  tmp_var=(*vertex)[k];
313  (*vertex)[k]+=eps;
314  feasible = evaluate(pd,local_val,err); MSQ_ERRZERO(err);
315  //if step was too big, shorten it
316  if(!feasible)
317  eps*=0.5;
318  //revert kth coord val
319  (*vertex)[k]=tmp_var;
320  }//end while looking for feasible eps
321  return eps;
322  }//end function get_eps
323 
324 
325 } //namespace
326 
327 
328 #endif // ObjectiveFunction_hpp
329 
330 
void set_gradient_type(GRADIENT_TYPE grad)
Set gradType to either NUMERICAL_GRADIENT or ANALYTICAL_GRADIENT.
#define MSQ_ERRZERO(err)
Return zero/NULL on error.
void set_quality_metric(QualityMetric *qm)
Set the value of qMetric.
virtual bool compute_analytical_hessian(PatchData &, MsqHessian &, Vector3D *const &, double &, MsqError &)
bool compute_hessian(PatchData &patch, MsqHessian &hessian, Vector3D *const &grad, double &OF_val, MsqError &err)
Calls compute_analytical_hessian. Function returns &#39;false&#39; if the patch is not within a required feas...
j indices k indices k
Definition: Indexing.h:6
Used to hold the error state and return it to the application.
Base class for concrete quality metrics.
bool evaluate(PatchData &patch, double &fval, MsqError &err)
Vector3D is the object that effeciently stores information about about three-deminsional vectors...
Vector3D is the object that effeciently stores the objective function Hessian each entry is a Matrix3...
every differentiable function should have an analytical gradient implemented.
virtual msq_std::list< QualityMetric * > get_quality_metric_list()
bool compute_numerical_gradient(PatchData &patch, Vector3D *const &grad, double &OF_val, MsqError &err, size_t array_size)
Non-virtual function which numerically computes the gradient of the Objective Function. Function returns &#39;false&#39; if the patch is not within a required feasible regeion. Otherwise, it returns &#39;true&#39;.
virtual bool compute_analytical_gradient(PatchData &patch, Vector3D *const &grad, double &OF_val, MsqError &err, size_t array_size)
void set_use_local_gradient(bool new_bool)
Sets useLocalGradient This variable determines whether compute_numercial_gradient can use the most ef...
Definition: patch.h:74
double get_eps(PatchData &pd, double &local_val, int k, MsqVertex *vertex, MsqError &err)
Returns eps used in the numerical gradient calculation.
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
QualityMetric * qMetric
Flag for numerical or analytical gradient.
bool compute_gradient(PatchData &patch, Vector3D *const &grad, double &OF_val, MsqError &err, size_t array_size=0)
Calls either compute_numerical_gradient or compute_analytical_gradient depending on the value of grad...
blockLoc i
Definition: read.cpp:79
void set_negate_flag(int neg)
Set the value of ObjectiveFunction&#39;s negateFlag. Unless composite, concrete ObjectiveFunctions should...
int negateFlag
Pointer to associated QualityMetric.
MsqVertex is the Mesquite object that stores information about the vertices in the mesh...
virtual bool concrete_evaluate(PatchData &patch, double &fval, MsqError &err)=0
Base class for concrete Objective Functions ObjectiveFunction contains a pointer to a QualityMetric...