37 #include "SteepestDescent.hpp"
38 #include "MsqFreeVertexIndexIterator.hpp"
39 #include "MsqTimer.hpp"
40 #include "MsqDebug.hpp"
70 int num_vertices = pd.num_vertices();
71 msq_std::vector<Vector3D> gradient(num_vertices), dk(num_vertices);
72 int nb_iterations = 0;
75 double smallest_edge = 0.4;
83 double original_value = 0.0;
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);
96 MSQ_DBGOUT(3) <<
"\t\t\t" << toto1[ind1.value()];
102 for (
int n=0;
n<num_vertices; ++
n)
103 norm += gradient[
n] % gradient[
n];
105 MSQ_DBGOUT(3) <<
" o gradient norm: " << norm << msq_stdio::endl;
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;
121 MSQ_SETERR(err)(
"SteepestDescent passed invalid initial patch.",
125 MSQ_DBGOUT(3) <<
" o original_value: " << original_value << msq_stdio::endl;
127 double new_value = original_value+1;
132 PatchDataVerticesMemento* pd_previous_coords;
133 pd_previous_coords = pd.create_vertices_memento(err);
134 if (
MSQ_CHKERR(err)) {
delete pd_previous_coords;
return; }
136 double step_size = smallest_edge;
137 while (new_value > original_value
142 pd.move_free_vertices_constrained(&dk[0], dk.size(), step_size, err);
143 if (
MSQ_CHKERR(err)) {
delete pd_previous_coords;
return; }
146 if (
MSQ_CHKERR(err)) {
delete pd_previous_coords;
return; }
148 MSQ_SETERR(err)(
"SteepestDescent created invalid patch.",
150 delete pd_previous_coords;
153 MSQ_DBGOUT(3) <<
" o step_size: " << step_size << msq_stdio::endl;
154 MSQ_DBGOUT(3) <<
" o new_value: " << new_value << msq_stdio::endl;
157 if (new_value > original_value) {
159 pd.set_to_vertices_memento( pd_previous_coords, err );
160 if (
MSQ_CHKERR(err)) {
delete pd_previous_coords;
return; }
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; }
175 MSQ_DBGOUT(3) <<
"\t\t\t" << toto1[ind.value()];
179 delete pd_previous_coords;
181 term_crit->accumulate_inner( pd, err );
MSQ_ERRRTN(err);
#define MSQ_DBG(flag)
Check if a debug flag is activated - evaluates to a bool.
virtual void terminate_mesh_iteration(PatchData &pd, MsqError &err)
virtual void optimize_vertex_positions(PatchData &pd, MsqError &err)
virtual void set_patch_type(PatchData::PatchType patch_type, MsqError &err, int param1=0, int param2=0)
Sets the Patch Type.
bool evaluate(PatchData &patch, double &fval, MsqError &err)
void set_name(msq_std::string name)
provides a name to the QualityImprover (use it in constructor).
virtual void initialize_mesh_iteration(PatchData &pd, MsqError &err)
T norm(const NVec< DIM, T > &v)
invalid function argument passed
#define MSQ_CHKERR(err)
Mesquite'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.
ObjectiveFunction * objFunc
virtual void initialize(PatchData &pd, MsqError &err)
TerminationCriterion * get_inner_termination_criterion()
return the inner termination criterion pointer
#define MSQ_FUNCTION_TIMER(NAME)
#define MSQ_DBGOUT(flag)
Check debug flag and return ostream associated with flag.
SteepestDescent(ObjectiveFunction *of)
#define MSQ_ERRRTN(err)
If passed error is true, return from a void function.