Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ObjectiveFunction/ObjectiveFunction.cpp
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  ***************************************************************** */
37 #include "ObjectiveFunction.hpp"
38 #include "MsqVertex.hpp"
39 #include "MsqDebug.hpp"
40 #include "MsqFreeVertexIndexIterator.hpp"
41 
42 namespace Mesquite {
43 
66  Vector3D *const &grad,
67  double &OF_val,
68  MsqError &err,
69  size_t array_size)
70 {
71  size_t num_vtx=pd.num_vertices();
72  if(num_vtx!=array_size && array_size>0)
73  MSQ_DBGOUT(1) << "\nArray size not equal to the number of vertices.\n";
74 
75  OF_val = 0.; // in case of return false.
76  MsqVertex* vertices=pd.get_vertex_array(err);
77  double flocal=0;
78  double flocald=0;
79  double eps=0;
80  size_t m=0;
81  short j;
82 
83  if(useLocalGradient){
84  //********************useLocalGradient***************************
85  //if useLocalGradient is turned on, do more efficient computation
86  PatchData sub_patch;
87  for (m=0; m<num_vtx; ++m) {
88  if (vertices[m].is_free_vertex()) {
89  pd.get_subpatch(m, sub_patch, err); MSQ_ERRZERO(err);
90  //If sub_patch is not in the feasible region, do not
91  //calculate anything. Just return false.
92  bool b = evaluate(sub_patch,flocal,err);
93  if(MSQ_CHKERR(err) || !b) {
94  return false;
95  }
96 
97  //loop over the three coords x,y,z
98  for(j=0;j<3;++j){
99  eps=get_eps(sub_patch, flocald, j, (&vertices[m]), err); MSQ_ERRZERO(err);
100  //PRINT_INFO("\nin obj num grad j=%i, eps=%20.19f",j,eps);
101  if(eps==0){
102  MSQ_SETERR(err)("Dividing by zero in Objective Functions numerical grad",
104  return false;
105  }
106  grad[m][j]=(flocald-flocal)/eps;
107  }
108  }
109  else {
110  for(j=0;j<3;++j)
111  grad[m][j] = 0.0;
112  }
113  }
114  evaluate(pd, OF_val, err); MSQ_ERRZERO(err);
115  }
116  else {
117  //********************DO NOT useLocalGradient********************
118  //if useLocalGradient is turned off, we do inefficient computation
119  for (m=0; m<num_vtx; ++m) {
120 
121  if (vertices[m].is_free_vertex()) {
122  //If pd is not in the feasible region, do not calculate anything.
123  //Just return false.
124  bool b = evaluate(pd,flocal,err);
125  if(MSQ_CHKERR(err) || !b) {
126  return false;
127  }
128  OF_val = flocal;
129  //loop over the three coords x,y,z
130  for(j=0;j<3;++j){
131  eps=get_eps(pd, flocald, j, (&vertices[m]), err); MSQ_ERRZERO(err);
132  //PRINT_INFO("\nin obj num grad j=%i, eps=%20.19f",j,eps);
133  if(eps==0){
134  MSQ_SETERR(err)("Dividing by zero in Objective Functions numerical grad",
136  return false;
137  }
138  grad[m][j]=(flocald-flocal)/eps;
139  }
140  }
141  else {
142  for(j=0;j<3;++j)
143  grad[m][j] = 0.0;
144  }
145  //PRINT_INFO(" gradx = %f, grady = %f, gradz = %f\n",grad[m][0],grad[m][1],grad[m][2]);
146  }//end loop over all vertices
147  }
148  //*****************END of DO NOT useLocalGradient*****************
149  return true;
150 }
151 
153  Vector3D *const &grad,
154  double &OF_val,
155  MsqError &err, size_t array_size){
157  bool result = compute_numerical_gradient(patch, grad, OF_val, err, array_size);
158  return !MSQ_CHKERR(err) && result;
159  }
160 
162  MsqHessian &/*hessian*/,
163  Vector3D *const &/*grad*/,
164  double &/*OF_val*/,
165  MsqError &err) {
166  MSQ_SETERR(err)("Analytic hessian not implemented for this Objective "
167  "Function. Feasible Newton algorythm cannot be used.\n",
169  return false;
170  }
171 
172 
173 
174 } // namespace Mesquite
175 
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.
virtual bool compute_analytical_hessian(PatchData &, MsqHessian &, Vector3D *const &, double &, MsqError &)
Used to hold the error state and return it to the application.
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...
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)
Definition: patch.h:74
void get_subpatch(size_t center_vertex_index, PatchData &pd_to_fill, MsqError &err)
Fills a PatchData with the elements attached to a center vertex.
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.
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
size_t num_vertices() const
number of vertices in the patch.
const MsqVertex * get_vertex_array(MsqError &err) const
Returns a pointer to the start of the vertex array.
j indices j
Definition: Indexing.h:6
object is in an invalid state
#define MSQ_DBGOUT(flag)
Check debug flag and return ostream associated with flag.
MsqVertex is the Mesquite object that stores information about the vertices in the mesh...