Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SteepestDescent Class Reference

#include <SteepestDescent.hpp>

Inheritance diagram for SteepestDescent:
Collaboration diagram for SteepestDescent:

Public Member Functions

 SteepestDescent (ObjectiveFunction *of)
 
virtual ~SteepestDescent ()
 
void set_maximum_iteration (int iter)
 
void set_lower_gradient_bound (double gradc)
 
 SteepestDescent (ObjectiveFunction *of)
 
virtual ~SteepestDescent ()
 
void set_maximum_iteration (int iter)
 
void set_lower_gradient_bound (double gradc)
 
- Public Member Functions inherited from VertexMover
virtual ~VertexMover ()
 
virtual double loop_over_mesh (MeshSet &ms, MsqError &err)
 Improves the quality of the MeshSet, calling some methods specified in a class derived from VertexMover. More...
 
virtual ~VertexMover ()
 
virtual double loop_over_mesh (MeshSet &ms, MsqError &err)
 This is the "run" function of PatchDataUser. It can do anything really. More...
 
- Public Member Functions inherited from QualityImprover
virtual ~QualityImprover ()
 
void set_name (msq_std::string name)
 provides a name to the QualityImprover (use it in constructor). More...
 
virtual msq_std::string get_name ()
 retrieves the QualityImprover name. A default name should be set in the constructor. More...
 
virtual AlgorithmType get_algorithm_type ()
 Return the algorithm type (to avoid RTTI use). More...
 
void set_inner_termination_criterion (TerminationCriterion *crit)
 Sets in the termination criterion for the concrete solver's optimization. More...
 
void set_outer_termination_criterion (TerminationCriterion *crit)
 Sets in the termination criterion for the outer loop over patches. More...
 
virtual ~QualityImprover ()
 
void set_name (msq_std::string name)
 provides a name to the QualityImprover (use it in constructor). More...
 
virtual msq_std::string get_name ()
 retrieves the QualityImprover name. A default name should be set in the constructor. More...
 
virtual AlgorithmType get_algorithm_type ()
 Return the algorithm type (to avoid RTTI use). More...
 
void set_inner_termination_criterion (TerminationCriterion *crit)
 Sets in the termination criterion for the concrete solver's optimization. More...
 
void set_outer_termination_criterion (TerminationCriterion *crit)
 Sets in the termination criterion for the outer loop over patches. More...
 
- Public Member Functions inherited from PatchDataUser
virtual ~PatchDataUser ()
 
virtual void set_patch_type (PatchData::PatchType patch_type, MsqError &err, int param1=0, int param2=0)
 Sets the Patch Type. More...
 
PatchData::PatchType get_patch_type ()
 Returns the Patch Type. More...
 
int get_nb_layers (MsqError &err)
 Returns number of layers (if relevant for partition algorythm). More...
 
void add_culling_method (enum PatchData::culling_method cm)
 Sets on the culling method passed as argument. More...
 
void no_culling_method ()
 Sets off all culling methods. More...
 
void remove_culling_method (enum PatchData::culling_method cm)
 Sets off the culling method passed as argument. More...
 
long unsigned int get_culling_method_bits ()
 Returns the bitset containing culling methods flags. More...
 
void set_all_parameters (PatchDataParameters &params)
 
PatchDataParametersget_all_parameters ()
 Returns the PatchDataParameters object. More...
 
void set_global_patch (PatchData *pd, MsqError &err)
 Sets the Global Patch, so that it can be use by contiguoug PatchDataUser. More...
 
PatchDataget_global_patch ()
 Returns the Global Patch. More...
 
void no_global_patch ()
 Sets the Global Patch pointer to NULL. More...
 
virtual ~PatchDataUser ()
 
virtual void set_patch_type (PatchData::PatchType patch_type, MsqError &err, int param1=0, int param2=0)
 Sets the Patch Type. More...
 
PatchData::PatchType get_patch_type ()
 Returns the Patch Type. More...
 
int get_nb_layers (MsqError &err)
 Returns number of layers (if relevant for partition algorythm). More...
 
void add_culling_method (enum PatchData::culling_method cm)
 Sets on the culling method passed as argument. More...
 
void no_culling_method ()
 Sets off all culling methods. More...
 
void remove_culling_method (enum PatchData::culling_method cm)
 Sets off the culling method passed as argument. More...
 
long unsigned int get_culling_method_bits ()
 Returns the bitset containing culling methods flags. More...
 
void set_all_parameters (PatchDataParameters &params)
 
PatchDataParametersget_all_parameters ()
 Returns the PatchDataParameters object. More...
 
void set_global_patch (PatchData *pd, MsqError &err)
 Sets the Global Patch, so that it can be use by contiguoug PatchDataUser. More...
 
PatchDataget_global_patch ()
 Returns the Global Patch. More...
 
void no_global_patch ()
 Sets the Global Patch pointer to NULL. More...
 

Protected Member Functions

virtual void initialize (PatchData &pd, MsqError &err)
 
virtual void optimize_vertex_positions (PatchData &pd, MsqError &err)
 
virtual void initialize_mesh_iteration (PatchData &pd, MsqError &err)
 
virtual void terminate_mesh_iteration (PatchData &pd, MsqError &err)
 
virtual void cleanup ()
 
virtual void initialize (PatchData &pd, MsqError &err)
 
virtual void optimize_vertex_positions (PatchData &pd, MsqError &err)
 
virtual void initialize_mesh_iteration (PatchData &pd, MsqError &err)
 
virtual void terminate_mesh_iteration (PatchData &pd, MsqError &err)
 
virtual void cleanup ()
 
- Protected Member Functions inherited from VertexMover
 VertexMover ()
 
size_t check_feasible (PatchData &pd, MsqError &err)
 CHECK FEASIBLE IS NOT YET IMPLEMENTED. More...
 
 VertexMover ()
 
size_t check_feasible (PatchData &pd, MsqError &err)
 CHECK FEASIBLE IS NOT YET IMPLEMENTED. More...
 
- Protected Member Functions inherited from QualityImprover
 QualityImprover ()
 
const MeshSetget_mesh_set () const
 
MeshSetget_mesh_set ()
 
void set_mesh_set (MeshSet *ms)
 
TerminationCriterionget_outer_termination_criterion ()
 return the outer termination criterion pointer More...
 
TerminationCriterionget_inner_termination_criterion ()
 return the inner termination criterion pointer More...
 
 QualityImprover ()
 
const MeshSetget_mesh_set () const
 
MeshSetget_mesh_set ()
 
void set_mesh_set (MeshSet *ms)
 
TerminationCriterionget_outer_termination_criterion ()
 return the outer termination criterion pointer More...
 
TerminationCriterionget_inner_termination_criterion ()
 return the inner termination criterion pointer More...
 
- Protected Member Functions inherited from PatchDataUser
 PatchDataUser ()
 
 PatchDataUser ()
 

Private Attributes

double gradientLessThan
 
int maxIteration
 

Additional Inherited Members

- Public Types inherited from PatchDataUser
enum  AlgorithmType {
  QUALITY_IMPROVER, QUALITY_ASSESSOR, MESH_TRANSFORM, TARGET_CALCULATOR,
  QUALITY_IMPROVER, QUALITY_ASSESSOR, MESH_TRANSFORM, TARGET_CALCULATOR
}
 
enum  AlgorithmType {
  QUALITY_IMPROVER, QUALITY_ASSESSOR, MESH_TRANSFORM, TARGET_CALCULATOR,
  QUALITY_IMPROVER, QUALITY_ASSESSOR, MESH_TRANSFORM, TARGET_CALCULATOR
}
 
- Protected Attributes inherited from VertexMover
ObjectiveFunctionobjFunc
 

Detailed Description

This is a very basic implementation of the steepest descent optimization algorythm. It works on patches of any size but the step size is hard-wired. Obvisouly, this is for testing purposed only.

Definition at line 55 of file includeLinks/SteepestDescent.hpp.

Constructor & Destructor Documentation

Definition at line 44 of file QualityImprover/VertexMover/SteepestDescent/SteepestDescent.cpp.

References PatchData::GLOBAL_PATCH, SteepestDescent::gradientLessThan, SteepestDescent::maxIteration, VertexMover::objFunc, QualityImprover::set_name(), and PatchDataUser::set_patch_type().

44  :
45  VertexMover()
46 {
47  objFunc=of;
48  MsqError err;
49  gradientLessThan=.01;
50  maxIteration=6;
51  this->set_name("SteepestDescent");
53 }
virtual void set_patch_type(PatchData::PatchType patch_type, MsqError &err, int param1=0, int param2=0)
Sets the Patch Type.
void set_name(msq_std::string name)
provides a name to the QualityImprover (use it in constructor).

Here is the call graph for this function:

virtual ~SteepestDescent ( )
inlinevirtual

Definition at line 60 of file includeLinks/SteepestDescent.hpp.

60 { }
virtual ~SteepestDescent ( )
inlinevirtual

Member Function Documentation

void cleanup ( )
protectedvirtual

Implements VertexMover.

Definition at line 192 of file QualityImprover/VertexMover/SteepestDescent/SteepestDescent.cpp.

193 {
194  // cout << "- Executing SteepestDescent::iteration_end()\n";
195 }
virtual void cleanup ( )
protectedvirtual

Implements VertexMover.

virtual void initialize ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

void initialize ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

Definition at line 56 of file QualityImprover/VertexMover/SteepestDescent/SteepestDescent.cpp.

57 {
58 }
void initialize_mesh_iteration ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

Definition at line 60 of file QualityImprover/VertexMover/SteepestDescent/SteepestDescent.cpp.

61 {
62 }
virtual void initialize_mesh_iteration ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

virtual void optimize_vertex_positions ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

void optimize_vertex_positions ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

Definition at line 64 of file QualityImprover/VertexMover/SteepestDescent/SteepestDescent.cpp.

References TerminationCriterion::accumulate_inner(), ObjectiveFunction::compute_gradient(), PatchData::create_vertices_memento(), ObjectiveFunction::evaluate(), QualityImprover::get_inner_termination_criterion(), PatchData::get_vertex_array(), SteepestDescent::gradientLessThan, i, MsqError::INVALID_ARG, MsqError::INVALID_MESH, j, SteepestDescent::maxIteration, PatchData::move_free_vertices_constrained(), MSQ_CHKERR, MSQ_DBG, MSQ_DBGOUT, MSQ_ERRRTN, MSQ_FUNCTION_TIMER, MSQ_SETERR, n, MsqFreeVertexIndexIterator::next(), nvc::norm(), PatchData::num_free_vertices(), PatchData::num_vertices(), VertexMover::objFunc, MsqFreeVertexIndexIterator::reset(), PatchData::set_to_vertices_memento(), sqrt(), TerminationCriterion::terminate(), and MsqFreeVertexIndexIterator::value().

66 {
67  MSQ_FUNCTION_TIMER( "SteepestDescent::optimize_vertex_positions" );
68  //PRINT_INFO("\no Performing Steepest Descent optimization.\n");
69  // Get the array of vertices of the patch. Free vertices are first.
70  int num_vertices = pd.num_vertices();
71  msq_std::vector<Vector3D> gradient(num_vertices), dk(num_vertices);
72  int nb_iterations = 0;
73  double norm=10e6;
74  bool sd_bool=true;//bool for OF values
75  double smallest_edge = 0.4; // TODO -- update -- used for step_size
76  TerminationCriterion* term_crit=get_inner_termination_criterion();
77 
78  // does the steepest descent iteration until stopping is required.
79  while ( (nb_iterations<maxIteration &&
80  norm>gradientLessThan ) && !term_crit->terminate()) {
81 
82  ++nb_iterations;
83  double original_value = 0.0;
84  //get intial objective function value, original_value, and gradient
85  objFunc->compute_gradient(pd, &gradient[0], original_value,
86  err, gradient.size()); MSQ_ERRRTN(err);
87 
88  // Prints out free vertices coordinates.
89  if (MSQ_DBG(3)) {
90  int num_free_vertices = pd.num_free_vertices(err); MSQ_ERRRTN(err);
91  MSQ_DBGOUT(3) << "\n o Free vertices ("<< num_free_vertices <<")original coordinates:\n ";
92  MsqVertex* toto1 = pd.get_vertex_array(err); MSQ_ERRRTN(err);
93  MsqFreeVertexIndexIterator ind1(&pd, err); MSQ_ERRRTN(err);
94  ind1.reset();
95  while (ind1.next()) {
96  MSQ_DBGOUT(3) << "\t\t\t" << toto1[ind1.value()];
97  }
98  }
99 
100  // computes the gradient norm
101  norm=0;
102  for (int n=0; n<num_vertices; ++n)
103  norm += gradient[n] % gradient[n]; // dot product
104  norm = sqrt(norm);
105  MSQ_DBGOUT(3) << " o gradient norm: " << norm << msq_stdio::endl;
106 
107  if (norm <= gradientLessThan) {
108  break;
109  }
110 
111  // ******** Chooses the search direction ********
112  // i.e., -gradient for the steepest descent
113  for (int i=0; i<num_vertices; ++i)
114  for (int j=0; j<3; ++j)
115  dk[i][j] = -gradient[i][j] / norm;
116 
117  // ******* Improve Quality *******
118 
119  //set an error if initial patch is invalid.
120  if(!sd_bool){
121  MSQ_SETERR(err)("SteepestDescent passed invalid initial patch.",
123  return;
124  }
125  MSQ_DBGOUT(3) << " o original_value: " << original_value << msq_stdio::endl;
126 
127  double new_value = original_value+1;
128  // reduces the step size until we get an improvement
129  int nb_iter = 0;
130 
131  // saves the PatchData coordinates in a memento
132  PatchDataVerticesMemento* pd_previous_coords;
133  pd_previous_coords = pd.create_vertices_memento(err);
134  if (MSQ_CHKERR(err)) { delete pd_previous_coords; return; }
135  // Loop to find a step size that improves quality
136  double step_size = smallest_edge;
137  while (new_value > original_value
138  && nb_iter < 10 ) {
139  nb_iter++;
140  // change vertices coordinates in PatchData according to descent
141  //direction.
142  pd.move_free_vertices_constrained(&dk[0], dk.size(), step_size, err);
143  if (MSQ_CHKERR(err)) { delete pd_previous_coords; return; }
144  // and evaluate the objective function with the new node positions.
145  sd_bool=objFunc->evaluate(pd, new_value, err);
146  if (MSQ_CHKERR(err)) { delete pd_previous_coords; return; }
147  if(!sd_bool){
148  MSQ_SETERR(err)("SteepestDescent created invalid patch.",
150  delete pd_previous_coords;
151  return;
152  }
153  MSQ_DBGOUT(3) << " o step_size: " << step_size << msq_stdio::endl;
154  MSQ_DBGOUT(3) << " o new_value: " << new_value << msq_stdio::endl;
155 
156  // if no improvement
157  if (new_value > original_value) {
158  // undoes node movement
159  pd.set_to_vertices_memento( pd_previous_coords, err );
160  if (MSQ_CHKERR(err)) { delete pd_previous_coords; return; }
161  // and reduces step size to try again.
162  step_size /= 2;
163  }
164  }
165 
166  // Prints out free vertices coordinates.
167  if (MSQ_DBG(3)) {
168  MSQ_DBGOUT(3) << " o Free vertices new coordinates: \n";
169  MsqVertex* toto1 = pd.get_vertex_array(err);
170  if (MSQ_CHKERR(err)) { delete pd_previous_coords; return; }
171  MsqFreeVertexIndexIterator ind(&pd, err);
172  if (MSQ_CHKERR(err)) { delete pd_previous_coords; return; }
173  ind.reset();
174  while (ind.next()) {
175  MSQ_DBGOUT(3) << "\t\t\t" << toto1[ind.value()];
176  }
177  }
178 
179  delete pd_previous_coords; // user manages the memento.
180 
181  term_crit->accumulate_inner( pd, err ); MSQ_ERRRTN(err);
182 
183  }
184 }
#define MSQ_DBG(flag)
Check if a debug flag is activated - evaluates to a bool.
bool evaluate(PatchData &patch, double &fval, MsqError &err)
double sqrt(double d)
Definition: double.h:73
T norm(const NVec< DIM, T > &v)
invalid function argument passed
#define MSQ_CHKERR(err)
Mesquite&#39;s Error Checking macro.
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...
#define MSQ_SETERR(err)
Macro to set error - use err.clear() to clear.
blockLoc i
Definition: read.cpp:79
const NT & n
TerminationCriterion * get_inner_termination_criterion()
return the inner termination criterion pointer
j indices j
Definition: Indexing.h:6
#define MSQ_DBGOUT(flag)
Check debug flag and return ostream associated with flag.
#define MSQ_ERRRTN(err)
If passed error is true, return from a void function.

Here is the call graph for this function:

void set_lower_gradient_bound ( double  gradc)
inline

Sets a minimum value for the gradient. If the gradient is below that value, we stop iterating.

Definition at line 70 of file src/QualityImprover/VertexMover/SteepestDescent/SteepestDescent.hpp.

References SteepestDescent::gradientLessThan.

void set_lower_gradient_bound ( double  gradc)
inline

Sets a minimum value for the gradient. If the gradient is below that value, we stop iterating.

Definition at line 70 of file includeLinks/SteepestDescent.hpp.

References SteepestDescent::gradientLessThan.

void set_maximum_iteration ( int  iter)
inline

sets the maximum number of iteration of the steepest descent algorythm, i.e. the number of times we compute the gradient and try to move the nodes in the opposite direction. This is different from the number of passes over the mesh.

Definition at line 65 of file includeLinks/SteepestDescent.hpp.

References SteepestDescent::maxIteration.

void set_maximum_iteration ( int  iter)
inline

sets the maximum number of iteration of the steepest descent algorythm, i.e. the number of times we compute the gradient and try to move the nodes in the opposite direction. This is different from the number of passes over the mesh.

Definition at line 65 of file src/QualityImprover/VertexMover/SteepestDescent/SteepestDescent.hpp.

References SteepestDescent::maxIteration.

void terminate_mesh_iteration ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

Definition at line 187 of file QualityImprover/VertexMover/SteepestDescent/SteepestDescent.cpp.

188 {
189  // cout << "- Executing SteepestDescent::iteration_complete()\n";
190 }
virtual void terminate_mesh_iteration ( PatchData pd,
MsqError err 
)
protectedvirtual

Implements VertexMover.

Member Data Documentation


The documentation for this class was generated from the following files: