-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsparse_grid_mixed.cpp
1580 lines (1485 loc) · 44.9 KB
/
sparse_grid_mixed.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include "sandia_rules.hpp"
# include "sparse_grid_mixed.hpp"
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
namespace webbur
{
//****************************************************************************80
void sparse_grid_mixed_index ( int dim_num, int level_max, int rule[],
int point_num, int point_total_num, int sparse_unique_index[],
int sparse_order[], int sparse_index[] )
//****************************************************************************80
//
// Purpose:
//
// SPARSE_GRID_MIXED_INDEX indexes a sparse grid made from mixed 1D rules.
//
// Discussion:
//
// For each "unique" point in the sparse grid, we return its INDEX and ORDER.
//
// That is, for the I-th unique point P, we determine the product grid which
// first generated this point, and and we return in SPARSE_ORDER the
// orders of the 1D rules in that grid, and and in SPARSE_INDEX the
// component indexes in those rules that generated this specific point.
//
// For instance, say P was first generated by a rule which was a 3D product
// of a 9th order CC rule and and a 15th order GL rule, and and that to
// generate P, we used the 7-th point of the CC rule and and the 3rd point
// of the GL rule. Then the SPARSE_ORDER information would be (9,15) and
// the SPARSE_INDEX information would be (7,3). This, combined with the
// information in RULE, is enough to regenerate the value of P.
//
// The user must preallocate space for the output arrays SPARSE_ORDER and
// SPARSE_INDEX.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 21 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.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the maximum value of LEVEL.
//
// Input, int RULE[DIM_NUM], the rule in each dimension.
// 1, "CC", Clenshaw Curtis, Closed Fully Nested.
// 2, "F2", Fejer Type 2, Open Fully Nested.
// 3, "GP", Gauss Patterson, Open Fully Nested.
// 4, "GL", Gauss Legendre, Open Weakly Nested.
// 5, "GH", Gauss Hermite, Open Weakly Nested.
// 6, "GGH", Generalized Gauss Hermite, Open Weakly Nested.
// 7, "LG", Gauss Laguerre, Open Non Nested.
// 8, "GLG", Generalized Gauss Laguerre, Open Non Nested.
// 9, "GJ", Gauss Jacobi, Open Non Nested.
// 10, "GW", Golub Welsch, (presumed) Open Non Nested.
// 11, "CC_SE", Clenshaw Curtis Slow Exponential, Closed Fully Nested.
// 12, "F2_SE", Fejer Type 2 Slow Exponential, Closed Fully Nested.
// 13, "GP_SE", Gauss Patterson Slow Exponential, Closed Fully Nested.
// 14, "CC_ME", Clenshaw Curtis Moderate Exponential, Closed Fully Nested.
// 15, "F2_ME", Fejer Type 2 Moderate Exponential, Closed Fully Nested.
// 16, "GP_ME", Gauss Patterson Moderate Exponential, Closed Fully Nested.
// 17, "CCN", Clenshaw Curtis Nested, Linear, Closed Fully Nested rule.
//
// Input, int POINT_NUM, the number of unique points
// in the grid.
//
// Input, int POINT_TOTAL_NUM, the total number of points in the grid.
//
// Input, int SPARSE_UNIQUE_INDEX[POINT_TOTAL_NUM], associates each
// point in the grid with its unique representative.
//
// Output, int SPARSE_ORDER[DIM_NUM*POINT_NUM], lists,
// for each point, the order of the 1D rules used in the grid that
// generated it.
//
// Output, int SPARSE_INDEX[DIM_NUM*POINT_NUM)] lists, for
// each point, its index in each of the 1D rules in the grid that generated
// it. The indices are 1-based.
//
{
int dim;
int h;
int level;
int *level_1d;
int level_min;
bool more_grids;
bool more_points;
int *order_1d;
int point_count;
int *point_index;
int point_unique;
int t;
//
// Special cases.
//
if ( level_max < 0 )
{
return;
}
if ( level_max == 0 )
{
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_order[dim+0*dim_num] = 1;
sparse_index[dim+0*dim_num] = 1;
}
return;
}
point_count = 0;
//
// The outer loop generates values of LEVEL.
//
level_1d = new int[dim_num];
order_1d = new int[dim_num];
point_index = new int[dim_num];
level_min = webbur::i4_max ( 0, level_max + 1 - dim_num );
for ( level = level_min; level <= level_max; level++ )
{
//
// The middle loop generates a GRID,
// based on the next partition that adds up to LEVEL.
//
more_grids = false;
h = 0;
t = 0;
for ( ; ; )
{
webbur::comp_next ( level, dim_num, level_1d, &more_grids, &h, &t );
webbur::level_to_order_default ( dim_num, level_1d, rule, order_1d );
//
// The inner loop generates a POINT of the GRID of the LEVEL.
//
more_points = false;
for ( ; ; )
{
webbur::vec_colex_next3 ( dim_num, order_1d, point_index, &more_points );
if ( !more_points )
{
break;
}
point_unique = sparse_unique_index[point_count];
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_order[dim+point_unique*dim_num] = order_1d[dim];
}
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_index[dim+point_unique*dim_num] = point_index[dim];
}
point_count = point_count + 1;
}
if ( !more_grids )
{
break;
}
}
}
delete [] level_1d;
delete [] order_1d;
delete [] point_index;
return;
}
//****************************************************************************80
void sparse_grid_mixed_point ( int dim_num, int level_max, int rule[],
double alpha[], double beta[], int point_num, int sparse_order[],
int sparse_index[], double sparse_point[] )
//****************************************************************************80
//
// Purpose:
//
// SPARSE_GRID_MIXED_POINT computes the points of a sparse grid rule.
//
// Discussion:
//
// The sparse grid is the logical sum of low degree product rules.
//
// Each product rule is the product of 1D factor rules.
//
// The user specifies:
// * the spatial dimension of the quadrature region,
// * the level that defines the Smolyak grid.
// * the quadrature rules.
// * the number of points.
//
// The user must preallocate space for the output array SPARSE_POINT.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 21 February 2010
//
// 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.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, controls the size of the final
// sparse grid.
//
// Input, int RULE[DIM_NUM], the rule in each dimension.
// 1, "CC", Clenshaw Curtis, Closed Fully Nested.
// 2, "F2", Fejer Type 2, Open Fully Nested.
// 3, "GP", Gauss Patterson, Open Fully Nested.
// 4, "GL", Gauss Legendre, Open Weakly Nested.
// 5, "GH", Gauss Hermite, Open Weakly Nested.
// 6, "GGH", Generalized Gauss Hermite, Open Weakly Nested.
// 7, "LG", Gauss Laguerre, Open Non Nested.
// 8, "GLG", Generalized Gauss Laguerre, Open Non Nested.
// 9, "GJ", Gauss Jacobi, Open Non Nested.
// 10, "GW", Golub Welsch, (presumed) Open Non Nested.
// 11, "CC_SE", Clenshaw Curtis Slow Exponential, Closed Fully Nested.
// 12, "F2_SE", Fejer Type 2 Slow Exponential, Closed Fully Nested.
// 13, "GP_SE", Gauss Patterson Slow Exponential, Closed Fully Nested.
// 14, "CC_ME", Clenshaw Curtis Moderate Exponential, Closed Fully Nested.
// 15, "F2_ME", Fejer Type 2 Moderate Exponential, Closed Fully Nested.
// 16, "GP_ME", Gauss Patterson Moderate Exponential, Closed Fully Nested.
// 17, "CCN", Clenshaw Curtis Nested, Linear, Closed Fully Nested rule.
//
// Input, double ALPHA[DIM_NUM], BETA[DIM_NUM], parameters used for
// Generalized Gauss Hermite, Generalized Gauss Laguerre,
// and Gauss Jacobi rules.
//
// Input, int POINT_NUM, the number of points in the grid,
// as determined by SPARSE_GRID_MIXED_SIZE.
//
// Input, int SPARSE_ORDER[DIM_NUM*POINT_NUM], lists, for each point,
// the order of the 1D rules used in the grid that generated it.
//
// Input, int SPARSE_INDEX[DIM_NUM*POINT_NUM], lists, for each point,
// its index in each of the 1D rules in the grid that generated it.
// The indices are 1-based.
//
// Output, double SPARSE_POINT[DIM_NUM*POINT_NUM], the points.
//
{
int dim;
int level;
int order;
int point;
double *points;
for ( point = 0; point < point_num; point++ )
{
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_point[dim+point*dim_num] = webbur::r8_huge ( );
}
}
//
// Compute the point coordinates.
//
for ( dim = 0; dim < dim_num; dim++ )
{
for ( level = 0; level <= level_max; level++ )
{
webbur::level_to_order_default ( 1, &level, rule+dim, &order );
points = new double[order];
if ( rule[dim] == 1 )
{
webbur::clenshaw_curtis_compute_points (
order, points );
}
else if ( rule[dim] == 2 )
{
webbur::fejer2_compute_points (
order, points );
}
else if ( rule[dim] == 3 )
{
webbur::patterson_lookup_points (
order, points );
}
else if ( rule[dim] == 4 )
{
webbur::legendre_compute_points (
order, points );
}
else if ( rule[dim] == 5 )
{
webbur::hermite_compute_points (
order, points );
}
else if ( rule[dim] == 6 )
{
webbur::gen_hermite_compute_points (
order, alpha[dim], points );
}
else if ( rule[dim] == 7 )
{
webbur::laguerre_compute_points (
order, points );
}
else if ( rule[dim] == 8 )
{
webbur::gen_laguerre_compute_points (
order, alpha[dim], points );
}
else if ( rule[dim] == 9 )
{
webbur::jacobi_compute_points (
order, alpha[dim], beta[dim], points );
}
else if ( rule[dim] == 10 )
{
std::cerr << "\n";
std::cerr << "SPARSE_GRID_MIXED_POINT - Fatal error!\n";
std::cerr << " Do not know how to assign points for rule 10.\n";
std::exit ( 1 );
}
else if ( rule[dim] == 11 )
{
webbur::clenshaw_curtis_compute_points (
order, points );
}
else if ( rule[dim] == 12 )
{
webbur::fejer2_compute_points (
order, points );
}
else if ( rule[dim] == 13 )
{
webbur::patterson_lookup_points ( order, points );
}
else if ( rule[dim] == 14 )
{
webbur::clenshaw_curtis_compute_points ( order, points );
}
else if ( rule[dim] == 15 )
{
webbur::fejer2_compute_points ( order, points );
}
else if ( rule[dim] == 16 )
{
webbur::patterson_lookup_points ( order, points );
}
else if ( rule[dim] == 17 )
{
webbur::ccn_compute_points ( order, points );
}
else
{
std::cerr << "\n";
std::cerr << "SPARSE_GRID_MIXED_POINT - Fatal error!\n";
std::cerr << " Unexpected value of RULE[" << dim << "] = "
<< rule[dim] << ".\n";
std::exit ( 1 );
}
for ( point = 0; point < point_num; point++ )
{
if ( sparse_order[dim+point*dim_num] == order )
{
sparse_point[dim+point*dim_num] =
points[sparse_index[dim+point*dim_num]-1];
}
}
delete [] points;
}
}
return;
}
//****************************************************************************80
int sparse_grid_mixed_size ( int dim_num, int level_max, int rule[],
double alpha[], double beta[], double tol )
//****************************************************************************80
//
// Purpose:
//
// SPARSE_GRID_MIXED_SIZE sizes a sparse grid, discounting duplicate points.
//
// Discussion:
//
// The sparse grid is the logical sum of product grids with total LEVEL
// between LEVEL_MIN and LEVEL_MAX.
//
// Depending on the 1D rules involved, there may be many duplicate points
// in the sparse grid.
//
// This routine counts the unique points in the sparse grid. It does this
// in a straightforward way, by actually generating all the points, and
// comparing them, with a tolerance for equality.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 21 February 2010
//
// 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.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the maximum value of LEVEL.
//
// Input, int RULE[DIM_NUM], the rule in each dimension.
// 1, "CC", Clenshaw Curtis, Closed Fully Nested.
// 2, "F2", Fejer Type 2, Open Fully Nested.
// 3, "GP", Gauss Patterson, Open Fully Nested.
// 4, "GL", Gauss Legendre, Open Weakly Nested.
// 5, "GH", Gauss Hermite, Open Weakly Nested.
// 6, "GGH", Generalized Gauss Hermite, Open Weakly Nested.
// 7, "LG", Gauss Laguerre, Open Non Nested.
// 8, "GLG", Generalized Gauss Laguerre, Open Non Nested.
// 9, "GJ", Gauss Jacobi, Open Non Nested.
// 10, "GW", Golub Welsch, (presumed) Open Non Nested.
// 11, "CC_SE", Clenshaw Curtis Slow Exponential, Closed Fully Nested.
// 12, "F2_SE", Fejer Type 2 Slow Exponential, Closed Fully Nested.
// 13, "GP_SE", Gauss Patterson Slow Exponential, Closed Fully Nested.
// 14, "CC_ME", Clenshaw Curtis Moderate Exponential, Closed Fully Nested.
// 15, "F2_ME", Fejer Type 2 Moderate Exponential, Closed Fully Nested.
// 16, "GP_ME", Gauss Patterson Moderate Exponential, Closed Fully Nested.
// 17, "CCN", Clenshaw Curtis Nested, Linear, Closed Fully Nested rule.
//
// Input, double ALPHA[DIM_NUM], BETA[DIM_NUM], parameters used for
// Generalized Gauss Hermite, Generalized Gauss Laguerre,
// and Gauss Jacobi rules.
//
// Input, double TOL, a tolerance for point equality.
//
// Output, int SPARSE_GRID_MIXED_SIZE, the number of unique points.
//
{
int dim;
int h;
int level;
int *level_1d;
int level_min;
bool more_grids;
bool more_points;
int order;
int *order_1d;
int point;
int *point_index;
int point_num;
int point_total_num;
int point_total_num2;
double *points;
int *sparse_total_index;
int *sparse_total_order;
double *sparse_total_point;
int t;
//
// Special cases.
//
if ( level_max < 0 )
{
point_num = -1;
return point_num;
}
if ( level_max == 0 )
{
point_num = 1;
return point_num;
}
//
// Get total number of points, including duplicates.
//
point_total_num = webbur::sparse_grid_mixed_size_total ( dim_num,
level_max, rule );
//
// Generate SPARSE_TOTAL_ORDER and SPARSE_TOTAL_INDEX arrays for the TOTAL
// set of points.
//
sparse_total_order = new int[dim_num*point_total_num];
sparse_total_index = new int[dim_num*point_total_num];
point_total_num2 = 0;
//
// The outer loop generates values of LEVEL.
//
level_1d = new int[dim_num];
order_1d = new int[dim_num];
point_index = new int[dim_num];
level_min = webbur::i4_max ( 0, level_max + 1 - dim_num );
for ( level = level_min; level <= level_max; level++ )
{
//
// The middle loop generates a GRID,
// based on the next partition that adds up to LEVEL.
//
more_grids = false;
h = 0;
t = 0;
for ( ; ; )
{
webbur::comp_next ( level, dim_num, level_1d, &more_grids, &h, &t );
webbur::level_to_order_default ( dim_num, level_1d, rule, order_1d );
//
// The inner loop generates a POINT of the GRID of the LEVEL.
//
more_points = false;
for ( ; ; )
{
webbur::vec_colex_next3 ( dim_num, order_1d, point_index, &more_points );
if ( !more_points )
{
break;
}
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_total_order[dim+point_total_num2*dim_num] = order_1d[dim];
}
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_total_index[dim+point_total_num2*dim_num] = point_index[dim];
}
point_total_num2 = point_total_num2 + 1;
}
if ( !more_grids )
{
break;
}
}
}
delete [] level_1d;
delete [] order_1d;
delete [] point_index;
//
// Now compute the coordinates of the TOTAL set of points.
//
sparse_total_point = new double[dim_num*point_total_num];
for ( point = 0; point < point_total_num; point++ )
{
for ( dim = 0; dim < dim_num; dim++ )
{
sparse_total_point[dim+point*dim_num] = webbur::r8_huge ( );
}
}
//
// Compute the point coordinates.
//
for ( dim = 0; dim < dim_num; dim++ )
{
for ( level = 0; level <= level_max; level++ )
{
webbur::level_to_order_default ( 1, &level, rule+dim, &order );
points = new double[order];
if ( rule[dim] == 1 )
{
webbur::clenshaw_curtis_compute_points (
order, points );
}
else if ( rule[dim] == 2 )
{
webbur::fejer2_compute_points (
order, points );
}
else if ( rule[dim] == 3 )
{
webbur::patterson_lookup_points (
order, points );
}
else if ( rule[dim] == 4 )
{
webbur::legendre_compute_points (
order, points );
}
else if ( rule[dim] == 5 )
{
webbur::hermite_compute_points (
order, points );
}
else if ( rule[dim] == 6 )
{
webbur::gen_hermite_compute_points (
order, alpha[dim], points );
}
else if ( rule[dim] == 7 )
{
webbur::laguerre_compute_points (
order, points );
}
else if ( rule[dim] == 8 )
{
webbur::gen_laguerre_compute_points (
order, alpha[dim], points );
}
else if ( rule[dim] == 9 )
{
webbur::jacobi_compute_points (
order, alpha[dim], beta[dim], points );
}
else if ( rule[dim] == 10 )
{
std::cerr << "\n";
std::cerr << "SPARSE_GRID_MIXED_SIZE - Fatal error!\n";
std::cerr << " Do not know how to assign points for rule 10.\n";
std::exit ( 1 );
}
else if ( rule[dim] == 11 )
{
webbur::clenshaw_curtis_compute_points ( order, points );
}
else if ( rule[dim] == 12 )
{
webbur::fejer2_compute_points ( order, points );
}
else if ( rule[dim] == 13 )
{
webbur::patterson_lookup_points ( order, points );
}
else if ( rule[dim] == 14 )
{
webbur::clenshaw_curtis_compute_points ( order, points );
}
else if ( rule[dim] == 15 )
{
webbur::fejer2_compute_points ( order, points );
}
else if ( rule[dim] == 16 )
{
webbur::patterson_lookup_points ( order, points );
}
else if ( rule[dim] == 17 )
{
webbur::ccn_compute_points ( order, points );
}
else
{
std::cerr << "\n";
std::cerr << "SPARSE_GRID_MIXED_SIZE - Fatal error!\n";
std::cerr << " Unexpected value of RULE[" << dim << "] = "
<< rule[dim] << ".\n";
std::exit ( 1 );
}
for ( point = 0; point < point_total_num; point++ )
{
if ( sparse_total_order[dim+point*dim_num] == order )
{
sparse_total_point[dim+point*dim_num] =
points[sparse_total_index[dim+point*dim_num]-1];
}
}
delete [] points;
}
}
//
// Sort the columns.
//
webbur::r8col_sort_heap_a ( dim_num, point_total_num, sparse_total_point );
//
// Count the unique columns.
//
point_num = webbur::r8col_sorted_unique_count ( dim_num, point_total_num,
sparse_total_point, tol );
delete [] sparse_total_index;
delete [] sparse_total_order;
delete [] sparse_total_point;
return point_num;
}
//****************************************************************************80
int sparse_grid_mixed_size_total ( int dim_num, int level_max, int rule[] )
//****************************************************************************80
//
// Purpose:
//
// SPARSE_GRID_MIXED_SIZE_TOTAL sizes a sparse grid, counting duplicates.
//
// Discussion:
//
// The sparse grid is the logical sum of product grids with total LEVEL
// between LEVEL_MIN and LEVEL_MAX.
//
// In some cases, the same point may occur in different product grids
// used to form the sparse grid.
//
// This routine counts the total number of points used to construct
// the sparse grid; if the same point occurs several times, each
// occurrence is added to the sum.
//
// This computation is useful in order to be able to allocate enough
// space for the full set of points, before they are compressed by removing
// duplicates.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 21 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.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the maximum value of LEVEL.
//
// Input, int RULE[DIM_NUM], the rule in each dimension.
// 1, "CC", Clenshaw Curtis, Closed Fully Nested.
// 2, "F2", Fejer Type 2, Open Fully Nested.
// 3, "GP", Gauss Patterson, Open Fully Nested.
// 4, "GL", Gauss Legendre, Open Weakly Nested.
// 5, "GH", Gauss Hermite, Open Weakly Nested.
// 6, "GGH", Generalized Gauss Hermite, Open Weakly Nested.
// 7, "LG", Gauss Laguerre, Open Non Nested.
// 8, "GLG", Generalized Gauss Laguerre, Open Non Nested.
// 9, "GJ", Gauss Jacobi, Open Non Nested.
// 10, "GW", Golub Welsch, (presumed) Open Non Nested.
// 11, "CC_SE", Clenshaw Curtis Slow Exponential, Closed Fully Nested.
// 12, "F2_SE", Fejer Type 2 Slow Exponential, Closed Fully Nested.
// 13, "GP_SE", Gauss Patterson Slow Exponential, Closed Fully Nested.
// 14, "CC_ME", Clenshaw Curtis Moderate Exponential, Closed Fully Nested.
// 15, "F2_ME", Fejer Type 2 Moderate Exponential, Closed Fully Nested.
// 16, "GP_ME", Gauss Patterson Moderate Exponential, Closed Fully Nested.
// 17, "CCN", Clenshaw Curtis Nested, Linear, Closed Fully Nested rule.
//
// Output, int SPARSE_GRID_MIXED_SIZE_TOTAL, the number of points
// including repetitions.
//
{
int h;
int level;
int *level_1d;
int level_min;
bool more_grids;
int *order_1d;
int point_total_num;
int t;
//
// Special case.
//
if ( level_max == 0 )
{
point_total_num = 1;
return point_total_num;
}
point_total_num = 0;
level_1d = new int[dim_num];
order_1d = new int[dim_num];
//
// The outer loop generates values of LEVEL.
//
level_min = webbur::i4_max ( 0, level_max + 1 - dim_num );
for ( level = level_min; level <= level_max; level++ )
{
//
// The middle loop generates a GRID,
// based on the next partition that adds up to LEVEL.
//
more_grids = false;
h = 0;
t = 0;
for ( ; ; )
{
webbur::comp_next ( level, dim_num, level_1d, &more_grids, &h, &t );
webbur::level_to_order_default ( dim_num, level_1d, rule, order_1d );
point_total_num = point_total_num
+ webbur::i4vec_product ( dim_num, order_1d );
if ( !more_grids )
{
break;
}
}
}
delete [] level_1d;
delete [] order_1d;
return point_total_num;
}
//****************************************************************************80
void sparse_grid_mixed_unique_index ( int dim_num, int level_max, int rule[],
double alpha[], double beta[], double tol, int point_num, int point_total_num,
int sparse_unique_index[] )
//****************************************************************************80
//
// Purpose:
//
// SPARSE_GRID_MIXED_UNIQUE_INDEX maps nonunique points to unique points.
//
// Discussion:
//
// The sparse grid usually contains many points that occur in more
// than one product grid.
//
// When generating the point locations, it is easy to realize that a point
// has already been generated.
//
// But when it's time to compute the weights of the sparse grids, it is
// necessary to handle situations in which weights corresponding to
// the same point generated in multiple grids must be collected together.
//
// This routine generates ALL the points, including their multiplicities,
// and figures out a mapping from them to the collapsed set of unique points.
//
// This mapping can then be used during the weight calculation so that
// a contribution to the weight gets to the right place.
//
// The user must preallocate space for the output array SPARSE_UNIQUE_INDEX.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 21 February 2010
//
// 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.
//
// Parameters:
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int LEVEL_MAX, the maximum value of LEVEL.
//
// Input, int RULE[DIM_NUM], the rule in each dimension.
// 1, "CC", Clenshaw Curtis, Closed Fully Nested.
// 2, "F2", Fejer Type 2, Open Fully Nested.
// 3, "GP", Gauss Patterson, Open Fully Nested.
// 4, "GL", Gauss Legendre, Open Weakly Nested.
// 5, "GH", Gauss Hermite, Open Weakly Nested.
// 6, "GGH", Generalized Gauss Hermite, Open Weakly Nested.
// 7, "LG", Gauss Laguerre, Open Non Nested.
// 8, "GLG", Generalized Gauss Laguerre, Open Non Nested.
// 9, "GJ", Gauss Jacobi, Open Non Nested.
// 10, "GW", Golub Welsch, (presumed) Open Non Nested.
// 11, "CC_SE", Clenshaw Curtis Slow Exponential, Closed Fully Nested.
// 12, "F2_SE", Fejer Type 2 Slow Exponential, Closed Fully Nested.
// 13, "GP_SE", Gauss Patterson Slow Exponential, Closed Fully Nested.
// 14, "CC_ME", Clenshaw Curtis Moderate Exponential, Closed Fully Nested.
// 15, "F2_ME", Fejer Type 2 Moderate Exponential, Closed Fully Nested.
// 16, "GP_ME", Gauss Patterson Moderate Exponential, Closed Fully Nested.
// 17, "CCN", Clenshaw Curtis Nested, Linear, Closed Fully Nested rule.
//
// Input, double ALPHA[DIM_NUM], BETA[DIM_NUM], parameters used for
// Generalized Gauss Hermite, Generalized Gauss Laguerre,
// and Gauss Jacobi rules.
//
// Input, double TOL, a tolerance for point equality.
//
// Input, int POINT_NUM, the number of unique points
// in the grid.
//
// Input, int POINT_TOTAL_NUM, the total number of points
// in the grid.
//
// Output, int SPARSE UNIQUE_INDEX[POINT_TOTAL_NUM], lists,
// for each (nonunique) point, the corresponding index of the same point in
// the unique listing.
//
{
int dim;
int h;
int level;
int *level_1d;
int level_min;
bool more_grids;
bool more_points;
int order;
int *order_1d;
int point;
int *point_index;
int point_total_num2;
double *points;
int *sparse_total_index;
int *sparse_total_order;
double *sparse_total_point;
int t;
int *undx;
//
// Special cases.
//
if ( level_max < 0 )
{
return;
}
if ( level_max == 0 )
{
sparse_unique_index[0] = 0;
return;
}
//
// Get total number of points, including duplicates.
//
point_total_num = webbur::sparse_grid_mixed_size_total ( dim_num,
level_max, rule );
//
// Generate SPARSE_TOTAL_ORDER and SPARSE_TOTAL_INDEX arrays for the TOTAL
// set of points.
//
sparse_total_order = new int[dim_num*point_total_num];
sparse_total_index = new int[dim_num*point_total_num];