-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcube_grid.cpp
523 lines (481 loc) · 10.8 KB
/
cube_grid.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
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <ctime>
using namespace std;
# include "cube_grid.hpp"
//****************************************************************************80
double *cube_grid ( int n, int ns[], double a[], double b[],
int c[] )
//****************************************************************************80
//
// Purpose:
//
// CUBE_GRID: grid points over the interior of a cube in 3D.
//
// Discussion:
//
// In 3D, a logically rectangular grid is to be created.
// In the I-th dimension, the grid will use S(I) points.
// The total number of grid points is
// N = product ( 1 <= I <= 3 ) S(I)
//
// Over the interval [A(i),B(i)], we have 5 choices for grid centering:
// 1: 0, 1/3, 2/3, 1
// 2: 1/5, 2/5, 3/5, 4/5
// 3: 0, 1/4, 2/4, 3/4
// 4: 1/4, 2/4, 3/4, 1
// 5: 1/8, 3/8, 5/8, 7/8
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 31 August 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of points.
// N = product ( 1 <= I <= 3 ) NS(I).
//
// Input, int NS[3], the number of points along
// each dimension.
//
// Input, double A[3], B[3], the endpoints for each dimension.
//
// Input, int C[3], the grid centering for each dimension.
// 1 <= C(*) <= 5.
//
// Output, double CUBE_GRID[3*N] = X(3*S(0)*S(1)*S(2)), the points.
//
{
int i;
int j;
static int m = 3;
int s;
double *x;
double *xs;
x = new double[m*n];
//
// Create the 1D grids in each dimension.
//
for ( i = 0; i < m; i++ )
{
s = ns[i];
xs = new double[s];
for ( j = 0; j < s; j++ )
{
if ( c[i] == 1 )
{
if ( s == 1 )
{
xs[j] = 0.5 * ( a[i] + b[i] );
}
else
{
xs[j] = ( ( double ) ( s - j - 1 ) * a[i]
+ ( double ) ( j ) * b[i] )
/ ( double ) ( s - 1 );
}
}
else if ( c[i] == 2 )
{
xs[j] = ( ( double ) ( s - j ) * a[i]
+ ( double ) ( j + 1 ) * b[i] )
/ ( double ) ( s + 1 );
}
else if ( c[i] == 3 )
{
xs[j] = ( ( double ) ( s - j ) * a[i]
+ ( double ) ( j - 2 ) * b[i] )
/ ( double ) ( s );
}
else if ( c[i] == 4 )
{
xs[j] = ( ( double ) ( s - j - 1 ) * a[i]
+ ( double ) ( j + 1 ) * b[i] )
/ ( double ) ( s );
}
else if ( c[i] == 5 )
{
xs[j] = ( ( double ) ( 2 * s - 2 * j - 1 ) * a[i]
+ ( double ) ( 2 * j + 1 ) * b[i] )
/ ( double ) ( 2 * s );
}
}
r8vec_direct_product ( i, s, xs, m, n, x );
delete [] xs;
}
return x;
}
//****************************************************************************80
void r8mat_transpose_print ( int m, int n, double a[], string title )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_TRANSPOSE_PRINT prints an R8MAT, transposed.
//
// Discussion:
//
// An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
// in column-major order.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 September 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input, double A[M*N], an M by N matrix to be printed.
//
// Input, string TITLE, a title.
//
{
r8mat_transpose_print_some ( m, n, a, 1, 1, m, n, title );
return;
}
//****************************************************************************80
void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo,
int ihi, int jhi, string title )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed.
//
// Discussion:
//
// An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
// in column-major order.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 April 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input, double A[M*N], an M by N matrix to be printed.
//
// Input, int ILO, JLO, the first row and column to print.
//
// Input, int IHI, JHI, the last row and column to print.
//
// Input, string TITLE, a title.
//
{
# define INCX 5
int i;
int i2;
int i2hi;
int i2lo;
int i2lo_hi;
int i2lo_lo;
int inc;
int j;
int j2hi;
int j2lo;
cout << "\n";
cout << title << "\n";
if ( m <= 0 || n <= 0 )
{
cout << "\n";
cout << " (None)\n";
return;
}
if ( ilo < 1 )
{
i2lo_lo = 1;
}
else
{
i2lo_lo = ilo;
}
if ( ihi < m )
{
i2lo_hi = m;
}
else
{
i2lo_hi = ihi;
}
for ( i2lo = i2lo_lo; i2lo <= i2lo_hi; i2lo = i2lo + INCX )
{
i2hi = i2lo + INCX - 1;
if ( m < i2hi )
{
i2hi = m;
}
if ( ihi < i2hi )
{
i2hi = ihi;
}
inc = i2hi + 1 - i2lo;
cout << "\n";
cout << " Row: ";
for ( i = i2lo; i <= i2hi; i++ )
{
cout << setw(7) << i - 1 << " ";
}
cout << "\n";
cout << " Col\n";
cout << "\n";
if ( jlo < 1 )
{
j2lo = 1;
}
else
{
j2lo = jlo;
}
if ( n < jhi )
{
j2hi = n;
}
else
{
j2hi = jhi;
}
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(5) << j - 1 << ":";
for ( i2 = 1; i2 <= inc; i2++ )
{
i = i2lo - 1 + i2;
cout << setw(14) << a[(i-1)+(j-1)*m];
}
cout << "\n";
}
}
return;
# undef INCX
}
//****************************************************************************80
void r8vec_direct_product ( int factor_index, int factor_order,
double factor_value[], int factor_num, int point_num, double x[] )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_DIRECT_PRODUCT creates a direct product of R8VEC's.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// To explain what is going on here, suppose we had to construct
// a multidimensional quadrature rule as the product of K rules
// for 1D quadrature.
//
// The product rule will be represented as a list of points and weights.
//
// The J-th item in the product rule will be associated with
// item J1 of 1D rule 1,
// item J2 of 1D rule 2,
// ...,
// item JK of 1D rule K.
//
// In particular,
// X(J) = ( X(1,J1), X(2,J2), ..., X(K,JK))
// and
// W(J) = W(1,J1) * W(2,J2) * ... * W(K,JK)
//
// So we can construct the quadrature rule if we can properly
// distribute the information in the 1D quadrature rules.
//
// This routine carries out that task.
//
// Another way to do this would be to compute, one by one, the
// set of all possible indices (J1,J2,...,JK), and then index
// the appropriate information. An advantage of the method shown
// here is that you can process the K-th set of information and
// then discard it.
//
// Example:
//
// Rule 1:
// Order = 4
// X(1:4) = ( 1, 2, 3, 4 )
//
// Rule 2:
// Order = 3
// X(1:3) = ( 10, 20, 30 )
//
// Rule 3:
// Order = 2
// X(1:2) = ( 100, 200 )
//
// Product Rule:
// Order = 24
// X(1:24) =
// ( 1, 10, 100 )
// ( 2, 10, 100 )
// ( 3, 10, 100 )
// ( 4, 10, 100 )
// ( 1, 20, 100 )
// ( 2, 20, 100 )
// ( 3, 20, 100 )
// ( 4, 20, 100 )
// ( 1, 30, 100 )
// ( 2, 30, 100 )
// ( 3, 30, 100 )
// ( 4, 30, 100 )
// ( 1, 10, 200 )
// ( 2, 10, 200 )
// ( 3, 10, 200 )
// ( 4, 10, 200 )
// ( 1, 20, 200 )
// ( 2, 20, 200 )
// ( 3, 20, 200 )
// ( 4, 20, 200 )
// ( 1, 30, 200 )
// ( 2, 30, 200 )
// ( 3, 30, 200 )
// ( 4, 30, 200 )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 April 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int FACTOR_INDEX, the index of the factor being processed.
// The first factor processed must be factor 0.
//
// Input, int FACTOR_ORDER, the order of the factor.
//
// Input, double FACTOR_VALUE[FACTOR_ORDER], the factor values
// for factor FACTOR_INDEX.
//
// Input, int FACTOR_NUM, the number of factors.
//
// Input, int POINT_NUM, the number of elements in the direct product.
//
// Input/output, double X[FACTOR_NUM*POINT_NUM], the elements of the
// direct product, which are built up gradually.
//
// Local Parameters:
//
// Local, int START, the first location of a block of values to set.
//
// Local, int CONTIG, the number of consecutive values to set.
//
// Local, int SKIP, the distance from the current value of START
// to the next location of a block of values to set.
//
// Local, int REP, the number of blocks of values to set.
//
{
static int contig = 0;
int i;
int j;
int k;
static int rep = 0;
static int skip = 0;
int start;
if ( factor_index == 0 )
{
contig = 1;
skip = 1;
rep = point_num;
for ( j = 0; j < point_num; j++ )
{
for ( i = 0; i < factor_num; i++ )
{
x[i+j*factor_num] = 0.0;
}
}
}
rep = rep / factor_order;
skip = skip * factor_order;
for ( i = 0; i < factor_order; i++ )
{
start = 0 + i * contig;
for ( k = 1; k <= rep; k++ )
{
for ( j = start; j < start + contig; j++ )
{
x[factor_index+j*factor_num] = factor_value[i];
}
start = start + skip;
}
}
contig = contig * factor_order;
return;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}