ROL
ROL_QuadratureHelpers.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 
45 namespace ROL {
46 
47 /* ======================================================================
48  Kronecker Products
49  ====================================================================== */
50 template <class Real>
52  /*
53  Compute the Kronecker Product of a Tensor Product rule and a 1D rule.
54  */
55  if (rule1.getNumPoints()==0) {
56  Quadrature<Real> rule(rule2);
57  return rule;
58  }
59  else {
60  // Initialize Arrays Containing Updated Nodes and Weights
61  int dim1 = rule1.getDimension();
62  int dim2 = rule2.getDimension();
63  Quadrature<Real> rule(dim1+dim2);
64 
65  Real weight = 0.0;
66  std::vector<Real> node2(dim2,0.0);
67 
68  // Perform Kronecker Products
69  // Compute Kronecker Product of Nodes
70  typename std::map<std::vector<Real>,int>::iterator it = rule.begin();
71  typename std::map<std::vector<Real>,int>::iterator it_i;
72  typename std::map<std::vector<Real>,int>::iterator it_j;
73  for (it_i=rule1.begin(); it_i!=rule1.end(); it_i++) {
74  for (it_j=rule2.begin(); it_j!=rule2.end(); it_j++) {
75  std::vector<Real> node = rule1.getNode(it_i);
76  node2 = rule2.getNode(it_j);
77  weight = rule1.getWeight(node)*rule2.getWeight(node2);
78  //node.push_back(node2[0]);
79  node.insert(node.end(),node2.begin(),node2.end());
80  rule.insert(it,node,weight);
81  it = rule.end();
82  }
83  }
84  return rule;
85  }
86 }
87 
88 int growthRule1D(int index, EROLGrowth growth, EROLBurkardt rule) {
89  //
90  // Compute the growth sequence for 1D quadrature rules according to growth.
91  // For more information on growth rules, see
92  //
93  // J. Burkardt. 1D Quadrature Rules For Sparse Grids.
94  // http://people.sc.fsu.edu/~jburkardt/presentations/sgmga_1d_rules.pdf.
95  //
96  // J. Burkardt. SGMGA: Sparse Grid Mixed Growth Anisotropic Rules.
97  // http://people.sc.fsu.edu/~jburkardt/cpp_src/sgmga/sgmga.html.
98  //
99  // Drew P. Kouri
100  // Sandia National Laboratories - CSRI
101  // May 27, 2011
102  //
103 
104  int level = index-1;
105  //int level = index;
106  if (rule==BURK_CLENSHAWCURTIS) { // Clenshaw-Curtis
107  if (growth==GROWTH_SLOWLIN) {
108  return level+1;
109  }
110  else if (growth==GROWTH_SLOWLINODD) {
111  return 2*((level+1)/2)+1;
112  }
113  else if (growth==GROWTH_MODLIN) {
114  return 2*level+1;
115  }
116  else if (growth==GROWTH_SLOWEXP) {
117  if (level==0) {
118  return 1;
119  }
120  else {
121  int o = 2;
122  while(o<2*level+1) {
123  o = 2*(o-1)+1;
124  }
125  return o;
126  }
127  }
128  else if (growth==GROWTH_MODEXP||growth==GROWTH_DEFAULT) {
129  if (level==0) {
130  return 1;
131  }
132  else {
133  int o = 2;
134  while (o<4*level+1) {
135  o = 2*(o-1)+1;
136  }
137  return o;
138  }
139  }
140  else if (growth==GROWTH_FULLEXP) {
141  if (level==0) {
142  return 1;
143  }
144  else {
145  return (int)pow(2.0,(double)level)+1;
146  }
147  }
148  }
149  else if (rule==BURK_FEJER2) { // Fejer Type 2
150  if (growth==GROWTH_SLOWLIN) {
151  return level+1;
152  }
153  else if (growth==GROWTH_SLOWLINODD) {
154  return 2*((level+1)/2)+1;
155  }
156  else if (growth==GROWTH_MODLIN) {
157  return 2*level+1;
158  }
159  else if (growth==GROWTH_SLOWEXP) {
160  int o = 1;
161  while (o<2*level+1) {
162  o = 2*o+1;
163  }
164  return o;
165  }
166  else if (growth==GROWTH_MODEXP||growth==GROWTH_DEFAULT) {
167  int o = 1;
168  while (o<4*level+1) {
169  o = 2*o+1;
170  }
171  return o;
172  }
173  else if (growth==GROWTH_FULLEXP) {
174  return (int)pow(2.0,(double)level+1.0)-1;
175  }
176  }
177 
178  else if (rule==BURK_PATTERSON) { // Gauss-Patterson
179  if (growth==GROWTH_SLOWLIN||
180  growth==GROWTH_SLOWLINODD||
181  growth==GROWTH_MODLIN) {
182  std::cout << "Specified Growth Rule Not Allowed!\n";
183  return 0;
184  }
185  else if (growth==GROWTH_SLOWEXP) {
186  if (level==0) {
187  return 1;
188  }
189  else {
190  int p = 5;
191  int o = 3;
192  while (p<2*level+1) {
193  p = 2*p+1;
194  o = 2*o+1;
195  }
196  return o;
197  }
198  }
199  else if (growth==GROWTH_MODEXP||growth==GROWTH_DEFAULT) {
200  if (level==0) {
201  return 1;
202  }
203  else {
204  int p = 5;
205  int o = 3;
206  while (p<4*level+1) {
207  p = 2*p+1;
208  o = 2*o+1;
209  }
210  return o;
211  }
212  }
213  else if (growth==GROWTH_FULLEXP) {
214  return (int)pow(2.0,(double)level+1.0)-1;
215  }
216  }
217 
218  else if (rule==BURK_LEGENDRE) { // Gauss-Legendre
219  if (growth==GROWTH_SLOWLIN) {
220  return level+1;
221  }
222  else if (growth==GROWTH_SLOWLINODD) {
223  return 2*((level+1)/2)+1;
224  }
225  else if (growth==GROWTH_MODLIN||growth==GROWTH_DEFAULT) {
226  return 2*level+1;
227  }
228  else if (growth==GROWTH_SLOWEXP) {
229  int o = 1;
230  while (2*o-1<2*level+1) {
231  o = 2*o+1;
232  }
233  return o;
234  }
235  else if (growth==GROWTH_MODEXP) {
236  int o = 1;
237  while (2*o-1<4*level+1) {
238  o = 2*o+1;
239  }
240  return o;
241  }
242  else if (growth==GROWTH_FULLEXP) {
243  return (int)pow(2.0,(double)level+1.0)-1;
244  }
245  }
246 
247  else if (rule==BURK_HERMITE) { // Gauss-Hermite
248  if (growth==GROWTH_SLOWLIN) {
249  return level+1;
250  }
251  else if (growth==GROWTH_SLOWLINODD) {
252  return 2*((level+1)/2)+1;
253  }
254  else if (growth==GROWTH_MODLIN||growth==GROWTH_DEFAULT) {
255  return 2*level+1;
256  }
257  else if (growth==GROWTH_SLOWEXP) {
258  int o = 1;
259  while (2*o-1<2*level+1) {
260  o = 2*o+1;
261  }
262  return o;
263  }
264  else if (growth==GROWTH_MODEXP) {
265  int o = 1;
266  while (2*o-1<4*level+1) {
267  o = 2*o+1;
268  }
269  return o;
270  }
271  else if (growth==GROWTH_FULLEXP) {
272  return (int)pow(2.0,(double)level+1.0)-1;
273  }
274  }
275 
276  else if (rule==BURK_LAGUERRE) { // Gauss-Laguerre
277  if (growth==GROWTH_SLOWLIN) {
278  return level+1;
279  }
280  else if (growth==GROWTH_SLOWLINODD) {
281  return 2*((level+1)/2)+1;
282  }
283  else if (growth==GROWTH_MODLIN||growth==GROWTH_DEFAULT) {
284  return 2*level+1;
285  }
286  else if (growth==GROWTH_SLOWEXP) {
287  int o = 1;
288  while (2*o-1<2*level+1) {
289  o = 2*o+1;
290  }
291  return o;
292  }
293  else if (growth==GROWTH_MODEXP) {
294  int o = 1;
295  while (2*o-1<4*level+1) {
296  o = 2*o+1;
297  }
298  return o;
299  }
300  else if (growth==GROWTH_FULLEXP) {
301  return (int)pow(2.0,(double)level+1.0)-1;
302  }
303  }
304 
305  else if (rule==BURK_CHEBYSHEV1) { // Gauss-Chebyshev Type 1
306  if (growth==GROWTH_SLOWLIN) {
307  return level+1;
308  }
309  else if (growth==GROWTH_SLOWLINODD) {
310  return 2*((level+1)/2)+1;
311  }
312  else if (growth==GROWTH_MODLIN||growth==GROWTH_DEFAULT) {
313  return 2*level+1;
314  }
315  else if (growth==GROWTH_SLOWEXP) {
316  int o = 1;
317  while (2*o-1<2*level+1) {
318  o = 2*o+1;
319  }
320  return o;
321  }
322  else if (growth==GROWTH_MODEXP) {
323  int o = 1;
324  while (2*o-1<4*level+1) {
325  o = 2*o+1;
326  }
327  return o;
328  }
329  else if (growth==GROWTH_FULLEXP) {
330  return (int)pow(2.0,(double)level+1.0)-1;
331  }
332  }
333 
334 
335  else if (rule==BURK_CHEBYSHEV2) { // Gauss-Chebyshev Type 2
336  if (growth==GROWTH_SLOWLIN) {
337  return level+1;
338  }
339  else if (growth==GROWTH_SLOWLINODD) {
340  return 2*((level+1)/2)+1;
341  }
342  else if (growth==GROWTH_MODLIN||growth==GROWTH_DEFAULT) {
343  return 2*level+1;
344  }
345  else if (growth==GROWTH_SLOWEXP) {
346  int o = 1;
347  while (2*o-1<2*level+1) {
348  o = 2*o+1;
349  }
350  return o;
351  }
352  else if (growth==GROWTH_MODEXP) {
353  int o = 1;
354  while (2*o-1<4*level+1) {
355  o = 2*o+1;
356  }
357  return o;
358  }
359  else if (growth==GROWTH_FULLEXP) {
360  return (int)pow(2.0,(double)level+1.0)-1;
361  }
362  }
363 
364  else if (rule==BURK_GENZKEISTER) { // Hermite-Genz-Keister
365  static int o_hgk[5] = { 1, 3, 9, 19, 35 };
366  static int p_hgk[5] = { 1, 5, 15, 29, 51 };
367  if (growth==GROWTH_SLOWLIN||
368  growth==GROWTH_SLOWLINODD||
369  growth==GROWTH_MODLIN) {
370  std::cout << "Specified Growth Rule Not Allowed!\n";
371  return 0;
372  }
373  else if (growth==GROWTH_SLOWEXP) {
374  int l = 0, p = p_hgk[l], o = o_hgk[l];
375  while (p<2*level+1 && l<4) {
376  l++;
377  p = p_hgk[l];
378  o = o_hgk[l];
379  }
380  return o;
381  }
382  else if (growth==GROWTH_MODEXP||growth==GROWTH_DEFAULT) {
383  int l = 0, p = p_hgk[l], o = o_hgk[l];
384  while (p<4*level+1 && l<4) {
385  l++;
386  p = p_hgk[l];
387  o = o_hgk[l];
388  }
389  return o;
390  }
391  else if (growth==GROWTH_FULLEXP) {
392  int l = level; l = std::max(l,0); l = std::min(l,4);
393  return o_hgk[l];
394  }
395  }
396 
397  else if (rule==BURK_TRAPEZOIDAL) { // Trapezoidal
398  if (growth==GROWTH_SLOWLIN) {
399  return level+1;
400  }
401  else if (growth==GROWTH_SLOWLINODD) {
402  return 2*((level+1)/2)+1;
403  }
404  else if (growth==GROWTH_MODLIN) {
405  return 2*level+1;
406  }
407  else if (growth==GROWTH_SLOWEXP) {
408  if (level==0) {
409  return 1;
410  }
411  else {
412  int o = 2;
413  while(o<2*level+1) {
414  o = 2*(o-1)+1;
415  }
416  return o;
417  }
418  }
419  else if (growth==GROWTH_MODEXP||growth==GROWTH_DEFAULT) {
420  if (level==0) {
421  return 1;
422  }
423  else {
424  int o = 2;
425  while (o<4*level+1) {
426  o = 2*(o-1)+1;
427  }
428  return o;
429  }
430  }
431  else if (growth==GROWTH_FULLEXP) {
432  if (level==0) {
433  return 1;
434  }
435  else {
436  return (int)pow(2.0,(double)level)+1;
437  }
438  }
439  }
440  return 0;
441 } // end growthRule1D
442 } // end ROL namespace
Quadrature< Real > kron_prod(Quadrature< Real > &rule1, Quadrature< Real > &rule2)
virtual std::vector< Real > getNode(typename std::map< std::vector< Real >, int >::iterator it)
EROLBurkardt
Enumeration of integration rules provided by John Burkardt.
virtual void insert(typename std::map< std::vector< Real >, int >::iterator it, std::vector< Real > point, Real weight)
virtual int getDimension() const
virtual std::map< std::vector< Real >, int >::iterator end()
virtual Real getWeight(int node)
virtual int getNumPoints() const
virtual std::map< std::vector< Real >, int >::iterator begin()
int growthRule1D(int index, EROLGrowth growth, EROLBurkardt rule)