-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtriangle_symq_rule_prb.cpp
619 lines (556 loc) · 14.5 KB
/
triangle_symq_rule_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
# include <cstdlib>
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cmath>
# include <cstring>
using namespace std;
# include "triangle_symq_rule.hpp"
int main ( );
void test01 ( );
void test02 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[] );
void test03 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[], string header );
void test04 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[], string header );
void test05 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[] );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for TRIANGLE_SYMQ_RULE_PRB.
//
// Discussion:
//
// TRIANGLE_SYMQ_RULE_PRB tests the TRIANGLE_SYMQ_RULE library.
//
// Licensing:
//
// This code is distributed under the GNU GPL license.
//
// Modified:
//
// 30 June 2014
//
// Author:
//
// Original FORTRAN77 version by Hong Xiao, Zydrunas Gimbutas.
// This C++ version by John Burkardt.
//
// Reference:
//
// Hong Xiao, Zydrunas Gimbutas,
// A numerical algorithm for the construction of efficient quadrature
// rules in two and higher dimensions,
// Computers and Mathematics with Applications,
// Volume 59, 2010, pages 663-676.
//
{
int degree;
string header;
int itype;
int numnodes;
double vert1[2];
double vert2[2];
double vert3[2];
timestamp ( );
cout << "\n";
cout << "TRIANGLE_SYMQ_RULE_PRB\n";
cout << " C++ version\n";
cout << " Test the TRIANGLE_SYMQ_RULE library.\n";
test01 ( );
for ( itype = 0; itype <= 2; itype++ )
{
if ( itype == 0 )
{
cout << "\n";
cout << " Region is user-defined triangle.\n";
vert1[0] = 1.0;
vert1[1] = 0.0;
vert2[0] = 4.0;
vert2[1] = 4.0;
vert3[0] = 0.0;
vert3[1] = 3.0;
header = "user08";
degree = 8;
}
else if ( itype == 1 )
{
cout << "\n";
cout << " Region is standard equilateral triangle.\n";
vert1[0] = -1.0;
vert1[1] = -1.0 / sqrt ( 3.0 );
vert2[0] = +1.0;
vert2[1] = -1.0 / sqrt ( 3.0 );
vert3[0] = 0.0;
vert3[1] = 2.0 / sqrt ( 3.0 );
header = "equi08";
degree = 8;
}
else if ( itype == 2 )
{
cout << "\n";
cout << " Region is the simplex (0,0),(1,0),(0,1).\n";
vert1[0] = 0.0;
vert1[1] = 0.0;
vert2[0] = 1.0;
vert2[1] = 0.0;
vert3[0] = 0.0;
vert3[1] = 1.0;
header = "simp08";
degree = 8;
}
cout << "\n";
cout << " Triangle:\n";
cout << "\n";
cout << vert1[0] << " " << vert1[1] << "\n";
cout << vert2[0] << " " << vert2[1] << "\n";
cout << vert3[0] << " " << vert3[1] << "\n";
//
// Determine the size of the rule.
//
numnodes = rule_full_size ( degree );
//
// Retrieve a rule and print it.
//
test02 ( degree, numnodes, vert1, vert2, vert3 );
//
// Get a rule, and write data files that gnuplot can use to plot the points.
//
test03 ( degree, numnodes, vert1, vert2, vert3, header );
test04 ( degree, numnodes, vert1, vert2, vert3, header );
test05 ( degree, numnodes, vert1, vert2, vert3 );
}
//
// Terminate.
//
cout << "\n";
cout << "TRIANGLE_SYMQ_RULE_PRB\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void test01 ( )
//****************************************************************************80
//
// Purpose:
//
// TEST01 tests TRIANGLE_TO_SIMPLEX, TRIANGLE_TO_REF, REF_TO_TRIANGLE, SIMPLEX_TO_TRIANGLE.
//
// Licensing:
//
// This code is distributed under the GNU GPL license.
//
// Modified:
//
// 30 June 2014
//
// Author:
//
// John Burkardt
//
{
int i;
double *rp1;
double rv1[2];
double rv2[2];
double rv3[2];
int seed;
double *sp1;
double *sp2;
double sv1[2];
double sv2[2];
double sv3[2];
double *tp1;
double *tp2;
double tv1[2];
double tv2[2];
double tv3[2];
cout << "\n";
cout << "TEST01\n";
cout << " Map points from one triangle to another.\n";
cout << "\n";
cout << " R = reference triangle\n";
cout << " S = simplex\n";
cout << " T = user-defined triangle.\n";
cout << " REF_TO_TRIANGLE: R => T\n";
cout << " SIMPLEX_TO_TRIANGLE: S => T\n";
cout << " TRIANGLE_TO_REF: T => R\n";
cout << " TRIANGLE_TO_SIMPLEX: T => S\n";
//
// Reference triangle
//
rv1[0] = -1.0;
rv1[1] = -1.0 / sqrt ( 3.0 );
rv2[0] = +1.0;
rv2[1] = -1.0 / sqrt ( 3.0 );
rv3[0] = 0.0;
rv3[1] = 2.0 / sqrt ( 3.0 );
//
// Simplex
//
sv1[0] = 0.0;
sv1[1] = 0.0;
sv2[0] = 1.0;
sv2[1] = 0.0;
sv3[0] = 0.0;
sv3[1] = 1.0;
//
// User triangle.
//
tv1[0] = 1.0;
tv1[1] = 0.0;
tv2[0] = 4.0;
tv2[1] = 4.0;
tv3[0] = 0.0;
tv3[1] = 3.0;
seed = 123456789;
for ( i = 1; i <= 5; i++ )
{
sp1 = r8vec_uniform_01_new ( 2, seed );
if ( 1.0 < sp1[0] + sp1[1] )
{
sp1[0] = 1.0 - sp1[0];
sp1[1] = 1.0 - sp1[1];
}
tp1 = simplex_to_triangle ( tv1, tv2, tv3, sp1 );
rp1 = triangle_to_ref ( tv1, tv2, tv3, tp1 );
tp2 = ref_to_triangle ( tv1, tv2, tv3, rp1 );
sp2 = triangle_to_simplex ( tv1, tv2, tv3, tp2 );
cout << "\n";
cout << " SP1: " << sp1[0] << " " << sp1[1] << "\n";
cout << " TP1: " << tp1[0] << " " << tp1[1] << "\n";
cout << " RP1: " << rp1[0] << " " << rp1[1] << "\n";
cout << " TP2: " << tp2[0] << " " << tp2[1] << "\n";
cout << " SP2: " << sp2[0] << " " << sp2[1] << "\n";
delete [] rp1;
delete [] sp1;
delete [] sp2;
delete [] tp1;
delete [] tp2;
}
return;
}
//****************************************************************************80
void test02 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[] )
//****************************************************************************80
//
// Purpose:
//
// TEST02 calls TRIASYMQ for a quadrature rule of given order and region.
//
// Licensing:
//
// This code is distributed under the GNU GPL license.
//
// Modified:
//
// 28 June 2014
//
// Author:
//
// Original FORTRAN77 version by Hong Xiao, Zydrunas Gimbutas.
// This C++ version by John Burkardt.
//
// Reference:
//
// Hong Xiao, Zydrunas Gimbutas,
// A numerical algorithm for the construction of efficient quadrature
// rules in two and higher dimensions,
// Computers and Mathematics with Applications,
// Volume 59, 2010, pages 663-676.
//
// Parameters:
//
// Input, int DEGREE, the desired total polynomial degree exactness
// of the quadrature rule. 0 <= DEGREE <= 50.
//
// Input, int NUMNODES, the number of nodes to be used by the rule.
//
// Input, double VERT1[2], VERT2[2], VERT3[2], the
// vertices of the triangle.
//
{
double area;
double d;
int j;
double *rnodes;
double *weights;
cout << "\n";
cout << "TEST02\n";
cout << " Symmetric quadrature rule for a triangle.\n";
cout << " Polynomial exactness degree DEGREE = " << degree << "\n";
area = triangle_area ( vert1, vert2, vert3 );
//
// Retrieve and print a symmetric quadrature rule.
//
rnodes = new double[2*numnodes];
weights = new double[numnodes];
triasymq ( degree, vert1, vert2, vert3, rnodes, weights, numnodes );
cout << "\n";
cout << " NUMNODES = " << numnodes << "\n";
cout << "\n";
cout << " J W X Y\n";
cout << "\n";
for ( j = 0; j < numnodes; j++ )
{
cout << j << " "
<< weights[j] << " "
<< rnodes[0+j*2] << " "
<< rnodes[1+j*2] << "\n";
}
d = r8vec_sum ( numnodes, weights );
cout << " Sum " << d << "\n";
cout << " Area " << area << "\n";
delete [] rnodes;
delete [] weights;
return;
}
//****************************************************************************80
void test03 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[], string header )
//****************************************************************************80
//
// Purpose:
//
// TEST03 calls TRIASYMQ_GNUPLOT to generate graphics files.
//
// Licensing:
//
// This code is distributed under the GNU GPL license.
//
// Modified:
//
// 30 June 2014
//
// Author:
//
// Original FORTRAN77 version by Hong Xiao, Zydrunas Gimbutas.
// This C++ version by John Burkardt.
//
// Reference:
//
// Hong Xiao, Zydrunas Gimbutas,
// A numerical algorithm for the construction of efficient quadrature
// rules in two and higher dimensions,
// Computers and Mathematics with Applications,
// Volume 59, 2010, pages 663-676.
//
// Parameters:
//
// Input, int DEGREE, the desired total polynomial degree exactness
// of the quadrature rule. 0 <= DEGREE <= 50.
//
// Input, int NUMNODES, the number of nodes to be used by the rule.
//
// Input, double VERT1[2], VERT2[2], VERT3[2], the
// vertices of the triangle.
//
// Input, string HEADER, an identifier for the graphics filenames.
//
{
double *rnodes;
double *weights;
cout << "\n";
cout << "TEST03\n";
cout << " TRIASYMQ_GNUPLOT creates gnuplot graphics files.\n";
cout << " Polynomial exactness degree DEGREE = " << degree << "\n";
rnodes = new double[2*numnodes];
weights = new double[numnodes];
triasymq ( degree, vert1, vert2, vert3, rnodes, weights, numnodes );
cout << " Number of nodes = " << numnodes << "\n";
triasymq_gnuplot ( vert1, vert2, vert3, numnodes, rnodes, header );
delete [] rnodes;
delete [] weights;
return;
}
//****************************************************************************80
void test04 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[], string header )
//****************************************************************************80
//
// Purpose:
//
// TEST04 gets a rule and writes it to a file.
//
// Licensing:
//
// This code is distributed under the GNU GPL license.
//
// Modified:
//
// 30 June 2014
//
// Author:
//
// Original FORTRAN77 version by Hong Xiao, Zydrunas Gimbutas.
// This C++ version by John Burkardt.
//
// Reference:
//
// Hong Xiao, Zydrunas Gimbutas,
// A numerical algorithm for the construction of efficient quadrature
// rules in two and higher dimensions,
// Computers and Mathematics with Applications,
// Volume 59, 2010, pages 663-676.
//
// Parameters:
//
// Input, int DEGREE, the desired total polynomial degree exactness
// of the quadrature rule. 0 <= DEGREE <= 50.
//
// Input, int NUMNODES, the number of nodes to be used by the rule.
//
// Input, double VERT1[2], VERT2[2], VERT3[2], the
// vertices of the triangle.
//
// Input, string HEADER, an identifier for the filenames.
//
{
int j;
double *rnodes;
ofstream rule_unit;
string rule_filename;
double *weights;
cout << "\n";
cout << "TEST04\n";
cout << " Get a quadrature rule for a triangle.\n";
cout << " Then write it to a file.\n";
cout << " Polynomial exactness degree DEGREE = " << degree << "\n";
//
// Retrieve a symmetric quadrature rule.
//
rnodes = new double[2*numnodes];
weights = new double[numnodes];
triasymq ( degree, vert1, vert2, vert3, rnodes, weights, numnodes );
//
// Write the points and weights to a file.
//
rule_filename = header + ".txt";
rule_unit.open ( rule_filename.c_str ( ) );
for ( j = 0; j < numnodes; j++ )
{
rule_unit << rnodes[0+j*2] << " "
<< rnodes[1+j*2] << " "
<< weights[j] << "\n";
}
rule_unit.close ( );
cout << "\n";
cout << " Quadrature rule written to file '" << rule_filename << "'\n";
delete [] rnodes;
delete [] weights;
return;
}
//****************************************************************************80
void test05 ( int degree, int numnodes, double vert1[], double vert2[],
double vert3[] )
//****************************************************************************80
//
// Purpose:
//
// TEST05 calls TRIASYMQ for a quadrature rule of given order and region.
//
// Licensing:
//
// This code is distributed under the GNU GPL license.
//
// Modified:
//
// 28 June 2014
//
// Author:
//
// Original FORTRAN77 version by Hong Xiao, Zydrunas Gimbutas.
// This C++ version by John Burkardt.
//
// Reference:
//
// Hong Xiao, Zydrunas Gimbutas,
// A numerical algorithm for the construction of efficient quadrature
// rules in two and higher dimensions,
// Computers and Mathematics with Applications,
// Volume 59, 2010, pages 663-676.
//
// Parameters:
//
// Input, int DEGREE, the desired total polynomial degree
// exactness of the quadrature rule. 0 <= DEGREE <= 50.
//
// Input, int NUMNODES, the number of nodes to be used by the rule.
//
// Input, double VERT1[2], VERT2[2], VERT3[2], the
// vertices of the triangle.
//
{
double area;
double d;
int i;
int j;
int npols;
double *pols;
double *r;
double *rints;
double *rnodes;
double scale;
double *weights;
double z[2];
cout << "\n";
cout << "TEST05\n";
cout << " Compute a quadrature rule for a triangle.\n";
cout << " Check it by integrating orthonormal polynomials.\n";
cout << " Polynomial exactness degree DEGREE = " << degree << "\n";
area = triangle_area ( vert1, vert2, vert3 );
//
// Retrieve a symmetric quadrature rule.
//
rnodes = new double[2*numnodes];
weights = new double[numnodes];
triasymq ( degree, vert1, vert2, vert3, rnodes, weights, numnodes );
//
// Construct the matrix of values of the orthogonal polynomials
// at the user-provided nodes
//
npols = ( degree + 1 ) * ( degree + 2 ) / 2;
rints = new double[npols];
for ( j = 0; j < npols; j++ )
{
rints[j] = 0.0;
}
for ( i = 0; i < numnodes; i++ )
{
z[0] = rnodes[0+i*2];
z[1] = rnodes[1+i*2];
r = triangle_to_ref ( vert1, vert2, vert3, z );
pols = ortho2eva ( degree, r );
for ( j = 0; j < npols; j++ )
{
rints[j] = rints[j] + weights[i] * pols[j];
}
delete [] pols;
delete [] r;
}
scale = sqrt ( sqrt ( 3.0 ) ) / sqrt ( area );
for ( j = 0; j < npols; j++ )
{
rints[j] = rints[j] * scale;
}
d = pow ( rints[0] - sqrt ( area ), 2 );
for ( j = 1; j < npols; j++ )
{
d = d + rints[j] * rints[j];
}
d = sqrt ( d ) / ( double ) ( npols );
cout << "\n";
cout << " RMS integration error = " << d << "\n";
delete [] rints;
delete [] rnodes;
delete [] weights;
return;
}