-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsparse_grid_hermite_prb.cpp
810 lines (717 loc) · 19.4 KB
/
sparse_grid_hermite_prb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
using namespace std;
# include "sparse_grid_hermite.hpp"
int main ( );
void test01 ( int dim_min, int dim_max, int level_max_min, int level_max_max );
void test02 ( int dim_num, int level_max );
void test03 ( int dim_num, int level_max );
void test04 ( int dim_num, int level_max );
void test05 ( int dim_num, int level_max, int degree_max );
void test06 ( int dim_num, int level_max );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for SPARSE_GRID_HERMITE_PRB.
//
// Discussion:
//
// SPARSE_GRID_HERMITE_PRB tests the SPARSE_GRID_HERMITE library.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 26 December 2009
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Fabio Nobile, Raul Tempone, Clayton Webster,
// A Sparse Grid Stochastic Collocation Method for Partial Differential
// Equations with Random Input Data,
// SIAM Journal on Numerical Analysis,
// Volume 46, Number 5, 2008, pages 2309-2345.
//
{
int degree_max;
int dim_max;
int dim_min;
int dim_num;
int level_max;
int level_max_max;
int level_max_min;
timestamp ( );
cout << "\n";
cout << "SPARSE_GRID_HERMITE_PRB\n";
cout << " C++ version\n";
cout << " Test the SPARSE_GRID_HERMITE library.\n";
//
// Count number of points in sparse rule from DIM_MIN to DIM_MAX, LEVEL_MAX_MAX.
//
dim_min = 1;
dim_max = 5;
level_max_min = 0;
level_max_max = 10;
test01 ( dim_min, dim_max, level_max_min, level_max_max );
dim_min = 6;
dim_max = 10;
level_max_min = 0;
level_max_max = 10;
test01 ( dim_min, dim_max, level_max_min, level_max_max );
dim_min = 100;
dim_max = 100;
level_max_min = 0;
level_max_max = 2;
test01 ( dim_min, dim_max, level_max_min, level_max_max );
//
// Compute abstract grid indices of sparse grid points as selected from product grid
// for DIMENSION, LEVEL_MAX.
//
dim_num = 2;
level_max = 3;
test02 ( dim_num, level_max );
dim_num = 2;
level_max = 4;
test02 ( dim_num, level_max );
dim_num = 3;
level_max = 0;
test02 ( dim_num, level_max );
dim_num = 3;
level_max = 2;
test02 ( dim_num, level_max );
dim_num = 6;
level_max = 2;
test02 ( dim_num, level_max );
//
// Compute sparse Gauss-Hermite rule for DIMENSION, LEVEL_MAX.
//
dim_num = 2;
level_max = 0;
test03 ( dim_num, level_max );
dim_num = 2;
level_max = 3;
test03 ( dim_num, level_max );
dim_num = 2;
level_max = 4;
test03 ( dim_num, level_max );
dim_num = 3;
level_max = 0;
test03 ( dim_num, level_max );
dim_num = 3;
level_max = 2;
test03 ( dim_num, level_max );
//
// Test sum of weights for DIMENSION, LEVEL_MAX.
//
dim_num = 2;
level_max = 4;
test04 ( dim_num, level_max );
dim_num = 3;
level_max = 0;
test04 ( dim_num, level_max );
dim_num = 3;
level_max = 1;
test04 ( dim_num, level_max );
dim_num = 3;
level_max = 6;
test04 ( dim_num, level_max );
dim_num = 10;
level_max = 3;
test04 ( dim_num, level_max );
//
// Test monomial exactness for DIMENSION, LEVEL_MAX, DEGREE_MAX.
//
dim_num = 2;
level_max = 0;
degree_max = 3;
test05 ( dim_num, level_max, degree_max );
dim_num = 2;
level_max = 1;
degree_max = 5;
test05 ( dim_num, level_max, degree_max );
dim_num = 2;
level_max = 2;
degree_max = 7;
test05 ( dim_num, level_max, degree_max );
dim_num = 2;
level_max = 3;
degree_max = 9;
test05 ( dim_num, level_max, degree_max );
dim_num = 2;
level_max = 4;
degree_max = 22;
test05 ( dim_num, level_max, degree_max );
dim_num = 2;
level_max = 4;
degree_max = 11;
test05 ( dim_num, level_max, degree_max );
dim_num = 3;
level_max = 0;
degree_max = 2;
test05 ( dim_num, level_max, degree_max );
dim_num = 3;
level_max = 1;
degree_max = 4;
test05 ( dim_num, level_max, degree_max );
dim_num = 3;
level_max = 2;
degree_max = 6;
test05 ( dim_num, level_max, degree_max );
dim_num = 3;
level_max = 3;
degree_max = 8;
test05 ( dim_num, level_max, degree_max );
//
// Show how to write a rule to a file.
//
dim_num = 2;
level_max = 3;
test06 ( dim_num, level_max );
//
// Terminate.
//
cout << "\n";
cout << "SPARSE_GRID_HERMITE_PRB\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void test01 ( int dim_min, int dim_max, int level_max_min, int level_max_max )
//****************************************************************************80
//
// Purpose:
//
// TEST01 tests SPARSE_GRID_HERMITE_SIZE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 26 December 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_MIN, the minimum spatial dimension to consider.
//
// Input, int DIM_MAX, the maximum spatial dimension to consider.
//
// Input, int LEVEL_MAX_MIN, the minimum value of LEVEL_MAX to consider.
//
// Input, int LEVEL_MAX_MAX, the maximum value of LEVEL_MAX to consider.
//
{
int dim_num;
int level_max;
int point_num;
cout << "\n";
cout << "TEST01\n";
cout << " SPARSE_GRID_HERMITE_SIZE returns the number of distinct\n";
cout << " points in a Gauss-Hermite sparse grid.\n";
cout << "\n";
cout << " Note that, unlike most sparse grids, a sparse grid based on\n";
cout << " Gauss-Hermite points is almost entirely NOT nested.\n";
cout << "\n";
cout << " Hence the point counts should be much higher than for a grid of\n";
cout << " the same level, but using rules such as Fejer1 or Fejer2 or\n";
cout << " Gauss-Patterson or Newton-Cotes-Open or Newton-Cotes-Open-Half.\n";
cout << "\n";
cout << " Each sparse grid is of spatial dimension DIM,\n";
cout << " and is made up of all product grids of levels up to LEVEL_MAX.\n";
cout << "\n";
cout << " DIM";
for ( dim_num = dim_min; dim_num <= dim_max; dim_num++)
{
cout << " " << setw(10) << dim_num;
}
cout << "\n";
cout << "\n";
cout << " LEVEL_MAX\n";
cout << "\n";
for ( level_max = level_max_min; level_max <= level_max_max; level_max++ )
{
cout << " " << setw(10) << level_max;
for ( dim_num = dim_min; dim_num <= dim_max; dim_num++ )
{
point_num = sparse_grid_hermite_size ( dim_num, level_max );
cout << " " << setw(10) << point_num;
}
cout << "\n";
}
return;
}
//***************************************************************************80
void test02 ( int dim_num, int level_max )
//***************************************************************************80
//
// Purpose:
//
// TEST02 tests SPARSE_GRID_HERMITE_INDEX.
//
// Discussion:
//
// The routine computes abstract indices that describe the sparse grid
// of Gauss-Hermite points.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 October 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the level.
//
{
int dim;
int *grid_base;
int *grid_index;
int level_min;
int point;
int point_num;
cout << "\n";
cout << "TEST02:\n";
cout << " SPARSE_GRID_HERMITE_INDEX returns abstract indices for the\n";
cout << " points that make up a Gauss-Hermite sparse grid.\n";
level_min = i4_max ( 0, level_max + 1 - dim_num );
cout << "\n";
cout << " LEVEL_MIN = " << level_min << "\n";
cout << " LEVEL_MAX = " << level_max << "\n";
cout << " Spatial dimension DIM_NUM = " << dim_num << "\n";
point_num = sparse_grid_hermite_size ( dim_num, level_max );
cout << "\n";
cout << " Number of unique points in the grid = " << point_num << "\n";
//
// Compute the orders and points.
//
grid_index = new int[dim_num*point_num];
grid_base = new int[dim_num*point_num];
sparse_grid_hermite_index ( dim_num, level_max, point_num, grid_index,
grid_base );
//
// Now we're done. Print the merged grid data.
//
cout << "\n";
cout << " Grid index/base:\n";
cout << "\n";
for ( point = 0; point < point_num; point++ )
{
cout << " " << setw(4) << point << " ";
for ( dim = 0; dim < dim_num; dim++ )
{
cout << setw(6) << grid_index[dim+point*dim_num];
}
cout << "\n";
cout << " ";
for ( dim = 0; dim < dim_num; dim++ )
{
cout << setw(6) << grid_base[dim+point*dim_num];
}
cout << "\n";
}
delete [] grid_index;
delete [] grid_base;
return;
}
//***************************************************************************80
void test03 ( int dim_num, int level_max )
//***************************************************************************80
//
// Purpose:
//
// TEST03 call SPARSE_GRID_HERMITE to create a sparse Gauss-Hermite grid.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 October 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the level.
//
{
int dim;
double *grid_point;
double *grid_weight;
int level_min;
int point;
int point_num;
cout << "\n";
cout << "TEST03:\n";
cout << " SPARSE_GRID_HERMITE makes a sparse Gauss-Hermite grid.\n";
level_min = i4_max ( 0, level_max + 1 - dim_num );
cout << "\n";
cout << " LEVEL_MIN = " << level_min << "\n";
cout << " LEVEL_MAX = " << level_max << "\n";
cout << " Spatial dimension DIM_NUM = " << dim_num << "\n";
//
// Determine the number of points.
//
point_num = sparse_grid_hermite_size ( dim_num, level_max );
cout << "\n";
cout << " Number of unique points in the grid = " << point_num << "\n";
//
// Allocate space for the weights and points.
//
grid_weight = new double[point_num];
grid_point = new double[dim_num*point_num];
//
// Compute the weights and points.
//
sparse_grid_hermite ( dim_num, level_max, point_num, grid_weight, grid_point );
//
// Print them out.
//
cout << "\n";
cout << " Grid weights:\n";
cout << "\n";
for ( point = 0; point < point_num; point++ )
{
cout << " " << setw(4) << point
<< " " << fixed << setprecision(6) << setw(10)
<< grid_weight[point] << "\n";
}
cout << "\n";
cout << " Grid points:\n";
cout << "\n";
for ( point = 0; point < point_num; point++ )
{
cout << " " << setw(4) << point;
for ( dim = 0; dim < dim_num; dim++ )
{
cout << " " << fixed << setprecision(6) << setw(9)
<< grid_point[dim+point*dim_num];
}
cout << "\n";
}
delete [] grid_point;
delete [] grid_weight;
return;
}
//***************************************************************************80
void test04 ( int dim_num, int level_max )
//***************************************************************************80
//
// Purpose:
//
// TEST04 sums the weights and compares them to SQRT(PI)^DIM_NUM.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 October 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the level.
//
{
int dim;
double *grid_point;
double *grid_weight;
int level_min;
double pi = 3.141592653589793;
int point;
int point_num;
double weight_sum;
double weight_sum_error;
double weight_sum_exact;
cout << "\n";
cout << "TEST04:\n";
cout << " Compute the weights of a Gauss-Hermite sparse grid .\n";
cout << "\n";
cout << " As a simple test, sum these weights.\n";
cout << " They should sum to exactly sqrt ( pi^DIM_NUM ).\n";
level_min = i4_max ( 0, level_max + 1 - dim_num );
cout << "\n";
cout << " LEVEL_MIN = " << level_min << "\n";
cout << " LEVEL_MAX = " << level_max << "\n";
cout << " Spatial dimension DIM_NUM = " << dim_num << "\n";
//
// Determine the number of points.
//
point_num = sparse_grid_hermite_size ( dim_num, level_max );
cout << "\n";
cout << " Number of unique points in the grid = " << point_num << "\n";
//
// Allocate space for the weights and points.
//
grid_weight = new double[point_num];
grid_point = new double[dim_num*point_num];
//
// Compute the weights and points.
//
sparse_grid_hermite ( dim_num, level_max, point_num, grid_weight, grid_point );
//
// Sum the weights.
//
weight_sum = 0.0;
for ( point = 0; point < point_num; point++ )
{
weight_sum = weight_sum + grid_weight[point];
}
weight_sum_exact = sqrt ( pow ( pi, dim_num ) );
weight_sum_error = fabs ( weight_sum - weight_sum_exact );
cout << "\n";
cout << " Weight sum Exact sum Difference\n";
cout << "\n";
cout << scientific
<< " " << setw(14) << weight_sum
<< " " << setw(14) << weight_sum_exact
<< " " << setw(14) << weight_sum_error << "\n";
delete [] grid_point;
delete [] grid_weight;
return;
}
//****************************************************************************80
void test05 ( int dim_num, int level_max, int degree_max )
//****************************************************************************80
//
// Purpose:
//
// TEST05 tests a Gauss-Hermite sparse grid rule for monomial exactness.
//
// Discussion:
//
// This test is going to check EVERY monomial of total degree DEGREE_MAX
// or less. Even for a moderately high dimension of DIM_NUM = 10, you
// do NOT want to use a large value of DEGREE_MAX, since there are
//
// 1 monomials of total degree 0,
// DIM_NUM monomials of total degree 1,
// DIM_NUM^2 monomials of total degree 2,
// DIM_NUM^3 monomials of total degree 3, and so on.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 July 2008
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the level.
//
// Input, int DEGREE_MAX, the maximum monomial total degree to check.
//
{
int degree;
int dim;
bool error;
int *expon;
double *grid_point;
double *grid_weight;
int h;
int last;
int level_min;
bool more;
int point;
int point_num;
double quad_error;
int t;
double volume;
cout << "\n";
cout << "TEST05\n";
cout << " Check the exactness of a Gauss-Hermite sparse\n";
cout << " grid quadrature rule, applied to all monomials \n";
cout << " of orders 0 to DEGREE_MAX.\n";
level_min = i4_max ( 0, level_max + 1 - dim_num );
cout << "\n";
cout << " LEVEL_MIN = " << level_min << "\n";
cout << " LEVEL_MAX = " << level_max << "\n";
cout << " Spatial dimension DIM_NUM = " << dim_num << "\n";
cout << "\n";
cout << " The maximum total degree to be checked is DEGREE_MAX = " << degree_max << "\n";
//
// Determine the number of points in the rule.
//
point_num = sparse_grid_hermite_size ( dim_num, level_max );
cout << "\n";
cout << " Number of unique points in the grid = " << point_num << "\n";
//
// Allocate space for the weights and points.
//
grid_weight = new double[point_num];
grid_point = new double[dim_num*point_num];
//
// Compute the weights and points.
//
sparse_grid_hermite ( dim_num, level_max, point_num, grid_weight, grid_point );
//
// Explore the monomials.
//
expon = new int[dim_num];
cout << "\n";
cout << " Error Total Monomial\n";
cout << " Degree Exponents\n";
for ( degree = 0; degree <= degree_max; degree++ )
{
more = false;
h = 0;
t = 0;
cout << "\n";
for ( ; ; )
{
comp_next ( degree, dim_num, expon, &more, &h, &t );
quad_error = monomial_quadrature_hermite ( dim_num, expon, point_num,
grid_weight, grid_point );
cout << scientific
<< " " << setprecision(1) << setw(12) << quad_error
<< " " << setw(2) << degree
<< " ";
for ( dim = 0; dim < dim_num; dim++ )
{
cout << setw(3) << expon[dim];
}
cout << "\n";
if ( !more )
{
break;
}
}
}
delete [] expon;
delete [] grid_point;
delete [] grid_weight;
return;
}
//***************************************************************************80
void test06 ( int dim_num, int level_max )
//***************************************************************************80
//
// Purpose:
//
// TEST06 creates a sparse Gauss-Hermite grid and writes it to a file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 26 December 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the level.
//
{
int dim;
int level_min;
int point;
int point_num;
double *r;
string r_filename;
double *w;
string w_filename;
double *x;
string x_filename;
cout << "\n";
cout << "TEST06:\n";
cout << " Call SPARSE_GRID_HERMITE to make a sparse Gauss-Hermite grid.\n";
cout << " Write the data to a set of quadrature files.\n";
level_min = i4_max ( 0, level_max + 1 - dim_num );
cout << "\n";
cout << " LEVEL_MIN = " << level_min << "\n";
cout << " LEVEL_MAX = " << level_max << "\n";
cout << " Spatial dimension DIM_NUM = " << dim_num << "\n";
//
// Determine the number of points.
//
point_num = sparse_grid_hermite_size ( dim_num, level_max );
//
// Allocate space for the weights and points.
//
r = new double[dim_num*2];
w = new double[point_num];
x = new double[dim_num*point_num];
//
// Compute the weights and points.
//
for ( dim = 0; dim < dim_num; dim++ )
{
r[dim+0*dim_num] = - r8_huge ( );
r[dim+1*dim_num] = + r8_huge ( );
}
sparse_grid_hermite ( dim_num, level_max, point_num, w, x );
//
// Write the data out.
//
r_filename = "gh_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_r.txt";
w_filename = "gh_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_w.txt";
x_filename = "gh_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_x.txt";
r8mat_write ( r_filename, dim_num, 2, r );
r8mat_write ( w_filename, 1, point_num, w );
r8mat_write ( x_filename, dim_num, point_num, x );
cout << "\n";
cout << " R data written to \"" << r_filename << "\".\n";
cout << " W data written to \"" << w_filename << "\".\n";
cout << " X data written to \"" << x_filename << "\".\n";
delete [] r;
delete [] w;
delete [] x;
return;
}