G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsAPALMBase.hpp
Go to the documentation of this file.
1 
14 #pragma once
15 
16 namespace gismo
17 {
18 
19 template <class T, class solution_t >
20 gsAPALMBase<T,solution_t>::gsAPALMBase( gsALMBase<T> * ALM,
21  const gsAPALMData<T,solution_t> & Data)
22 :
23 m_ALM(ALM),
24 m_data(Data)
25 {
26  this->_defaultOptions();
27 }
28 
29 template <class T, class solution_t >
30 void gsAPALMBase<T,solution_t>::_defaultOptions()
31 {
32  m_options.addSwitch("Verbose","Verbosity",false);
33 }
34 
35 template <class T, class solution_t >
36 void gsAPALMBase<T,solution_t>::_getOptions()
37 {
38  m_verbose = m_options.getSwitch("verbose");
39 }
40 
41 template <class T, class solution_t >
42 void gsAPALMBase<T,solution_t>::initialize()
43 {
44  this->_getOptions();
45 }
46 
47 template <class T, class solution_t >
48 void gsAPALMBase<T,solution_t>::serialSolve(index_t Nsteps)
49 {
50  bool bisected = false;
51  T s = 0;
52  T dL = m_ALM->getLength();
53  T dL0 = dL;
54 
55  // Initialize solutions
56  T Lold, L0;
57  gsMatrix<T> Uold, U0;
58  Uold.setZero(m_ALM->numDofs(),1);
59  U0.setZero(m_ALM->numDofs(),1);
60  L0 = Lold = 0.0;
61 
62  // Store initial solution
63  m_solutions.push_back({U0,L0});
64  m_times.push_back(s);
65  m_ALM->setSolution(U0,L0);
66 
67  for (index_t k=0; k!=Nsteps; k++)
68  {
69  s+=dL;
70  if (m_verbose) gsInfo<<"Load step "<< k<<"\t"<<"dL = "<<dL<<"; curve time = "<<s<<"\n";
71 
72  // Set a step
73  m_ALM->step();
74  // If not converged, bisect the arc-length
75  if (!(m_ALM->converged()))
76  {
77  s -= dL;
78  if (m_verbose) gsInfo<<"Error: Loop terminated, arc length method did not converge.\n";
79  dL = dL / 2.;
80  m_ALM->setLength(dL);
81  m_ALM->setSolution(Uold,Lold);
82  bisected = true;
83  k -= 1;
84  continue;
85  }
86 
87  Uold = m_ALM->solutionU();
88  Lold = m_ALM->solutionL();
89 
90  T lambda = m_ALM->solutionL();
91  m_solutions.push_back({m_ALM->solutionU(),lambda});
92  m_times.push_back(s);
93 
94  this->serialStepOutput(m_ALM->solutionU(),lambda);
95 
96  // // add data point to fit
97  // index_t N = m_ALM->solutionU().rows();
98  // gsVector<T> sol(N+1);
99  // sol.head(N) = m_ALM->solutionU();
100  // sol.at(N) = lambda;
101  // cfitter.addDataPoint(sol,s);
102  // if (k>deg_z+1)
103  // cfitter.compute();
104  //
105 
106 
107  if (!bisected)
108  {
109  dL = dL0;
110  m_ALM->setLength(dL);
111  }
112  bisected = false;
113 
114  } // end of steps
115 }
116 
117 template <class T, class solution_t >
118 void gsAPALMBase<T,solution_t>::parallelSolve()
119 {
120  solution_t start, guess, reference;
121  index_t ID;
122  real_t tstart = 0;
123  real_t tend = 0;
124  real_t dt, dt0;
125  index_t it = 0;
126  index_t itmax = 100;
127  real_t TOL = 1e-2;
128  gsVector<> DeltaU;
129  real_t DeltaL;
130  index_t Nintervals;
131  std::vector<solution_t> stepSolutions;
132  std::vector<real_t> stepTimes;
133  std::vector<real_t> distances;
134  bool bisected = false;
135  real_t dt_rem = 0;
136 
137  T Lguess, Lold, L0;
138  gsMatrix<T> Uguess,Uold, U0;
139 
140  while (!m_data.empty() && it < itmax)
141  {
142  Nintervals = 3;
143  stepSolutions.resize(Nintervals);
144  stepTimes.resize(Nintervals);
145  distances.resize(Nintervals+1);
146 
147  std::tie(ID,tstart,tend,dt0,start,guess) = m_data.pop();
148  std::tie(Uold,Lold) = start;
149  std::tie(Uguess,Lguess) = guess;
150 
151  gsMatrix<> Uori = Uold;
152  real_t Lori = Lold;
153 
154  dt0 = dt0 / Nintervals;
155  dt = dt0;
156 
157  m_ALM->setLength(dt);
158  m_ALM->setSolution(Uold,Lold);
159  // m_ALM->resetStep();
160  m_ALM->setPrevious(Uguess,Lguess);
161  gsDebug<<"Start - ||u|| = "<<Uold.norm()<<", L = "<<Lold<<"\n";
162  gsDebug<<"Guess - ||u|| = "<<Uguess.norm()<<", L = "<<Lguess<<"\n";
163 
164  gsVector<> tmpU = Uold-Uguess;
165  real_t tmpL = Lold-Lguess;
166 
167 
168  real_t s = 0;
169 
170  gsInfo<<"Starting with ID "<<ID<<" from (|U|,L) = ("<<Uold.norm()<<","<<Lold<<"), curve time = "<<tstart<<"\n";
171  for (index_t k = 0; k!=Nintervals; k++)
172  {
173  gsDebug<<"Interval "<<k+1<<" of "<<Nintervals<<"\n";
174  gsDebug<<"Start - ||u|| = "<<Uold.norm()<<", L = "<<Lold<<"\n";
175  m_ALM->step();
176  if (!(m_ALM->converged()))
177  {
178  gsInfo<<"Error: Loop terminated, arc length method did not converge.\n";
179  dt = dt / 2.;
180  dt_rem += dt; // add the remainder of the interval to dt_rem
181  m_ALM->setLength(dt);
182  m_ALM->setSolution(Uold,Lold);
183  bisected = true;
184  k -= 1;
185  continue;
186  }
187  GISMO_ENSURE(m_ALM->converged(),"Loop terminated, arc length method did not converge.\n");
188 
189  stepSolutions.at(k) = std::make_pair(m_ALM->solutionU(),m_ALM->solutionL());
190  stepTimes.at(k) = tstart + dt;
191  DeltaU = m_ALM->solutionU() - Uold;
192  DeltaL = m_ALM->solutionL() - Lold;
193 
194  distances.at(k) = m_ALM->distance(DeltaU,DeltaL);
195  gsDebugVar(m_ALM->distance(DeltaU,DeltaL));
196 
197  s += distances.at(k);
198 
199  Uold = m_ALM->solutionU();
200  Lold = m_ALM->solutionL();
201 
202  if (!bisected) // if bisected = false
203  dt = dt0;
204  else
205  {
206  // if the current interval has finished, but was refined before.
207  // The next interval should have the remaining length.
208  // Also, Nintervals should increase
209  //
210  dt = dt_rem;
211  Nintervals++;
212  stepSolutions.resize(Nintervals);
213  stepTimes.resize(Nintervals);
214  distances.resize(Nintervals+1);
215  }
216 
217  m_ALM->setLength(dt);
218  dt_rem = 0;
219  bisected = false;
220  }
221 
222  gsDebugVar(s);
223 
224  bool success = m_data.getReferenceByID(ID,reference);
225  GISMO_ASSERT(success,"Reference not found");
226 
227  DeltaU = reference.first - m_ALM->solutionU();
228  DeltaL = reference.second - m_ALM->solutionL();
229  distances.back() = m_ALM->distance(DeltaU,DeltaL);
230  // gsDebugVar(m_ALM->distance(DeltaU,DeltaL));
231 
232  // DeltaU = m_ALM->solutionU() - Uori;
233  // DeltaL = m_ALM->solutionL() - Lori;
234  // gsDebugVar(m_ALM->distance(DeltaU,DeltaL));
235 
236  s += distances.back();
237  // gsDebugVar(s);
238 
243 
244  m_data.submit(ID,distances,stepTimes,stepSolutions);
245  m_data.addJobs(ID);
246  m_data.finishJob(ID);
247 
248  it++;
249  }
250 
251  m_data.printQueue();
252  std::tie(m_times,m_solutions) = m_data.getFlatSolution();
253 
254  m_data.printKnots();
255 
256  gsDebugVar(gsAsVector(m_times));
257 
258 }
259 
260 } // namespace gismo
#define gsDebug
Definition: gsDebug.h:61
#define index_t
Definition: gsConfig.h:32
#define GISMO_ENSURE(cond, message)
Definition: gsDebug.h:102
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
#define gsInfo
Definition: gsDebug.h:43