G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsStaticOpt.hpp
1 
16 
17 #ifdef gsHLBFGS_ENABLED
18 #include <gsHLBFGS/gsHLBFGS.h>
19 #endif
20 
21 #ifdef gsIpOpt_ENABLED
22 #include <gsIpOpt/gsIpOpt.h>
23 #endif
24 
25 #pragma once
26 
27 namespace gismo
28 {
29 
30 template <class T>
32 {
33  Base::defaultOptions();
34 }
35 
36 template <class T>
38 {
39  Base::getOptions();
40 }
41 
42 template <class T>
44 {
45  gsInfo<<"\t";
46  gsInfo<<std::setw(4)<<std::left<<"It.";
47  gsInfo<<std::setw(17)<<std::left<<"|R|";
48  gsInfo<<std::setw(17)<<std::left<<"|R|/|R0|";
49  gsInfo<<std::setw(17)<<std::left<<" Ek";
50  gsInfo<<std::setw(17)<<std::left<<" Ek/Ek0";
51  gsInfo<<std::setw(17)<<std::left<<"|dU|";
52  gsInfo<<std::setw(17)<<std::left<<"|dU|/|DU|";
53  gsInfo<<std::setw(17)<<std::left<<"|dU|/|U+DU|";
54  gsInfo<<std::setw(17)<<std::left<<"|dV|";
55  gsInfo<<"\n";
56 }
57 
58 template <class T>
60 {
61  gsInfo<<"\t";
62  gsInfo<<std::setw(4)<<std::left<<k;
63  gsInfo<<std::setw(17)<<std::left<<m_residual;
64  gsInfo<<std::setw(17)<<std::left<<m_residual/m_residualIni;
65  gsInfo<<std::setw(17)<<std::left<<m_Ek;
66  gsInfo<<std::setw(17)<<std::left<<m_Ek/m_Ek0;
67  gsInfo<<std::setw(17)<<std::left<<m_deltaU.norm();
68  gsInfo<<std::setw(17)<<std::left<<m_deltaU.norm()/m_DeltaU.norm();
69  gsInfo<<std::setw(17)<<std::left<<m_deltaU.norm()/(m_U+m_DeltaU).norm();
70  gsInfo<<std::setw(17)<<std::left<<m_v.norm();
71  gsInfo<<"\n";
72 }
73 
74 template <class T>
76 {
77  try
78  {
79  _solve();
80  m_status = gsStatus::Success;
81  }
82  catch (int errorCode)
83  {
84  if (errorCode==1)
85  m_status = gsStatus::NotConverged;
86  else if (errorCode==2)
87  m_status = gsStatus::AssemblyError;
88  else if (errorCode==3)
89  m_status = gsStatus::SolverError;
90  else
91  m_status = gsStatus::OtherError;
92  }
93  catch (...)
94  {
95  m_status = gsStatus::OtherError;
96  }
97  return m_status;
98 }
99 
100 template <class T>
102 {
103  m_Eks.clear();
104  m_Eks.reserve(m_maxIterations);
105 
106  if (m_verbose) initOutput();
107 
108  _start();
109 
110  m_Ek0 = m_Ek;
111  m_Eks.push_back(m_Ek);
112 
113  if (m_verbose != 0) stepOutput(0);
114  index_t resetIt = 0;
115  for (m_numIterations=1; m_numIterations!=m_maxIterations; m_numIterations++, resetIt++)
116  {
117  _iteration();
118  if ((m_c==0 && m_Ek_prev > m_Ek) || resetIt==m_resetIterations)// || (m_Ek/m_Ek_prev > 1/m_tolE && m_Ek_prev!=0))
119  {
120  resetIt = 0;
121  _peak();
122  }
123 
124  if (m_verbose!=0)
125  if (m_numIterations % m_verbose == 0 || m_verbose==-1 ) stepOutput(m_numIterations);
126 
127  m_Eks.push_back(m_Ek);
128 
129  m_residualOld = m_residual;
130 
131  if (m_residual/m_residualIni < m_tolF && m_Ek/m_Ek0 < m_tolE && m_deltaU.norm()/m_DeltaU.norm() < m_tolU)
132  {
133  m_U += m_DeltaU;
134  gsDebug <<"Converged: \n";
135  gsDebug <<"\t |R|/|R0| = "<<m_residual/m_residualIni<<" < tolF = "<<m_tolF<<"\n";
136  gsDebug <<"\t |E|/|E0| = "<<m_Ek/m_Ek0 <<" < tolE = "<<m_tolE<<"\n";
137  gsDebug <<"\t |U|/|U0| = "<<m_deltaU.norm()/m_DeltaU.norm()<<" < tolF = "<<m_tolU<<"\n";
138  break;
139  }
140  if (m_numIterations==m_maxIterations-1)
141  {
142  m_U += m_DeltaU;
143  gsInfo<<"Maximum iterations reached. Solution did not converge\n";
144  throw 1;
145  }
146  }
147  gsInfo<<"\n";
148 };
149 
150 template <class T>
152 {
153  this->reset();
154  getOptions();
155 }
156 
157 template <class T>
159 {
160  Base::reset();
161  m_status = gsStatus::NotStarted;
162 }
163 
164 template <class T>
166 {
167  this->reset();
168  defaultOptions();
169 }
170 
171 template <class T>
173 {
174  gsVector<T> resVec;
175  if (!m_residualFun(U, resVec))
176  throw 2;
177  return resVec;
178 }
179 
180 template <class T>
181 gsOptProblemStatic<T>::gsOptProblemStatic()
182 {
183 
184  // Number of design variables
185  m_numDesignVars = 2;
186  // Number of constraints
187  m_numConstraints = 0;
188  // Number of non-zeros in the Jacobian of the constraints
189  m_numConJacNonZero = 0;
190 
191  m_desLowerBounds.resize(m_numDesignVars);
192  m_desUpperBounds.resize(m_numDesignVars);
193 
194  m_desLowerBounds.setConstant(-1e19);
195  m_desUpperBounds.setConstant( 1e19);
196 
197  // we initialize x in bounds, in the upper right quadrant
198  m_curDesign.resize(m_numDesignVars,1);
199  m_curDesign.setZero();
200 }
201 
202 } // namespace gismo
Step did not converge.
void _init()
Initializes the method.
Definition: gsStaticOpt.hpp:165
#define gsDebug
Definition: gsDebug.h:61
void initOutput() override
See gsStaticBase.
Definition: gsStaticOpt.hpp:43
void reset() override
See gsStaticBase.
Definition: gsStaticOpt.hpp:158
Provides declaration of an optimization problem.
Assembly problem in step.
Assembly failed due to an error in the expression (e.g. overflow)
#define index_t
Definition: gsConfig.h:32
gsStatus
Definition: gsStructuralAnalysisTypes.h:20
Provides declaration of an optimization problem.
void getOptions() override
See gsStaticBase.
Definition: gsStaticOpt.hpp:37
void _solve()
See solve()
Definition: gsStaticOpt.hpp:101
Static solver using the Dynamic Relaxation method.
Definition: gsStaticOpt.h:27
void defaultOptions() override
See gsStaticBase.
Definition: gsStaticOpt.hpp:31
#define gsInfo
Definition: gsDebug.h:43
ALM has not started yet.
void initialize() override
See gsStaticBase.
Definition: gsStaticOpt.hpp:151
Provides declaration of the gradient descent method.
gsStatus solve() override
gsStaticBase base functions
Definition: gsStaticOpt.hpp:75
void stepOutput(index_t k) override
See gsStaticBase.
Definition: gsStaticOpt.hpp:59