Nektar++
BGFS-B.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: SurfaceMeshing.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: surfacemeshing object methods.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
37 
38 #include <limits>
39 #include <set>
40 
41 using namespace std;
42 namespace Nektar
43 {
44 namespace NekMeshUtils
45 {
46 
47 // this function will perform an update on the solution vector contained within
48 // opti
50 {
51 
52  Array<OneD, NekDouble> xi = opti->Getxi();
53  Array<OneD, NekDouble> ui = opti->Getui();
54  Array<OneD, NekDouble> li = opti->Getli();
55 
56  set<int> Fset;
57  Array<OneD, NekDouble> ti(xi.num_elements());
58  for (int i = 0; i < ti.num_elements(); i++)
59  {
60  if (J(i, 0) < 0)
61  {
62  ti[i] = (xi[i] - ui[i]) / J(i, 0);
63  }
64  else if (J(i, 0) > 0)
65  {
66  ti[i] = (xi[i] - li[i]) / J(i, 0);
67  }
68  else
69  {
70  ti[i] = numeric_limits<double>::max();
71  }
72  if (ti[i] > 0)
73  {
74  Fset.insert(i);
75  }
76  }
77 
78  // intitialise d
79  DNekMat d(xi.num_elements(), 1);
80  for (int i = 0; i < xi.num_elements(); i++)
81  {
82  if (fabs(ti[i]) < 1E-10)
83  {
84  d(i, 0) = 0;
85  }
86  else
87  {
88  d(i, 0) = -1.0 * J(i, 0);
89  }
90  }
91 
92  Array<OneD, NekDouble> xci(xi.num_elements());
93 
94  for (int i = 0; i < xci.num_elements(); i++)
95  {
96  if (xi[i] + d(i, 0) < li[i])
97  {
98  xci[i] = li[i];
99  Fset.erase(i);
100  continue;
101  }
102  else
103  {
104  xci[i] = xi[i] + d(i, 0);
105  }
106 
107  if (xi[i] + d(i, 0) > ui[i])
108  {
109  xci[i] = ui[i];
110  Fset.erase(i);
111  continue;
112  }
113  else
114  {
115  xci[i] = xi[i] + d(i, 0);
116  }
117  }
118 
119  DNekMat Z(xci.num_elements(), xci.num_elements(), 0.0);
120 
121  set<int>::iterator it;
122  for (int i = 0; i < xci.num_elements(); i++)
123  {
124  it = Fset.find(i);
125  if (it != Fset.end())
126  {
127  Z(i, i) = 1.0;
128  }
129  }
130 
131  DNekMat dx(xci.num_elements(), 1, 0.0);
132  for (int i = 0; i < xci.num_elements(); i++)
133  {
134  dx(i, 0) = xci[i] - xi[i];
135  }
136 
137  DNekMat rg = Z * (J + B * dx);
138 
139  DNekMat du = -1.0 * H * rg;
140 
141  NekDouble alpha = 1.0;
142  for (it = Fset.begin(); it != Fset.end(); it++)
143  {
144  int i = (*it);
145  if (li[i] - xci[i] > alpha * du(i, 0))
146  {
147  alpha = min(alpha, (li[i] - xci[i]) / du(i, 0));
148  }
149  else if (ui[i] - xci[i] < alpha * du(i, 0))
150  {
151  alpha = min(alpha, (ui[i] - xci[i]) / du(i, 0));
152  }
153  }
154 
155  DNekMat grad = alpha * du;
156 
157  Array<OneD, NekDouble> dk(xci.num_elements()), xibar(xci.num_elements());
158  for (int i = 0; i < xci.num_elements(); i++)
159  {
160  set<int>::iterator f = Fset.find(i);
161  if (f != Fset.end())
162  {
163  xibar[i] = xci[i] + grad(i, 0);
164  }
165  else
166  {
167  xibar[i] = xci[i];
168  }
169  }
170 
171  Vmath::Vsub(xci.num_elements(), &xibar[0], 1, &xi[0], 1, &dk[0], 1);
172 
173  NekDouble c = 0.0;
174  NekDouble r = 0.0;
175  NekDouble l = 0.0;
176  for (int i = 0; i < dk.num_elements(); i++)
177  {
178  c += 1E-4 * J(i, 0) * dk[i];
179  r += J(i, 0) * dk[i];
180  }
181 
182  /*cout << endl << J << endl << endl;
183 
184  for(int i = 0; i < dk.num_elements(); i++)
185  {
186  cout << dk[i] << endl;
187  }
188  cout << endl;*/
189 
190  // this section needs a case evaluation for edges on faces
191  NekDouble lam = 2.0;
192  int iterct = 0;
193  NekDouble fo = opti->F(xi);
194  NekDouble fn;
195  Array<OneD, NekDouble> tst(xi.num_elements());
196  do
197  {
198  if (iterct > 100)
199  {
200  // cout << "failed line search" << endl;
201  return false;
202  }
203  iterct++;
204 
205  lam *= 0.5;
206 
207  for (int i = 0; i < xi.num_elements(); i++)
208  {
209  tst[i] = xi[i] + lam * dk[i];
210  }
211 
212  fn = opti->F(tst);
213 
214  DNekMat jn = opti->dF(tst);
215 
216  l = 0.0;
217  for (int i = 0; i < dk.num_elements(); i++)
218  {
219  l += jn(i, 0) * dk[i];
220  }
221 
222  } while (fn > fo + c || fabs(l) > 1.0 * fabs(r));
223  // wolfe conditions
224 
225  // tst at this point is the new all vector
226  // now need to update hessians
227  DNekMat Jn = opti->dF(tst);
228  DNekMat y = Jn - J;
229  DNekMat yT = Jn - J;
230  yT.Transpose();
231  DNekMat s(dk.num_elements(), 1, 0.0);
232  for (int i = 0; i < dk.num_elements(); i++)
233  {
234  s(i, 0) = lam * dk[i];
235  }
236  DNekMat sT = s;
237  sT.Transpose();
238 
239  DNekMat d1 = yT * s;
240  DNekMat d2 = sT * B * s;
241  DNekMat d3 = sT * y;
242  DNekMat n1 = yT * H * y;
243 
244  NekDouble ynorm = 0.0;
245  for (int i = 0; i < dk.num_elements(); i++)
246  {
247  ynorm += y(i, 0) * y(i, 0);
248  }
249 
250  if (d3(0, 0) > 2.2E-16 * ynorm)
251  {
252  B = B + y * yT * (1.0 / d1(0, 0)) - B * s * sT * B * (1.0 / d2(0, 0));
253  H = H + (d3(0, 0) + n1(0, 0)) / d3(0, 0) / d3(0, 0) * s * sT -
254  1.0 / d3(0, 0) * (H * y * sT + s * yT * H);
255  }
256 
257  J = Jn;
258  opti->Update(tst);
259 
260  return true;
261 }
262 }
263 }
STL namespace.
std::shared_ptr< OptiObj > OptiObjSharedPtr
Definition: OptimiseObj.h:104
double NekDouble
void Vsub(int n, const T *x, const int incx, const T *y, const int incy, T *z, const int incz)
Subtract vector z = x-y.
Definition: Vmath.cpp:346
bool BGFSUpdate(OptiObjSharedPtr opti, DNekMat &J, DNekMat &B, DNekMat &H)
Definition: BGFS-B.cpp:49