v0.14.0
Macros | Functions
gm_rule.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "gm_rule.h"

Go to the source code of this file.

Macros

#define TIME_SIZE   40
 

Functions

void comp_next (int n, int k, int a[], int *more, int *h, int *t)
 
void gm_rule_set (int rule, int dim_num, int point_num, double w[], double x[])
 
int gm_rule_size (int rule, int dim_num)
 
int i4_choose (int n, int k)
 
int i4_huge (void)
 
int i4_max (int i1, int i2)
 
int i4_min (int i1, int i2)
 
int i4_power (int i, int j)
 
doublemonomial_value (int dim_num, int point_num, double x[], int expon[])
 
double r8_abs (double x)
 
double r8_factorial (int n)
 
double r8vec_dot_product (int n, double a1[], double a2[])
 
doubler8vec_uniform_01_new (int n, int *seed)
 
double simplex_unit_monomial_int (int dim_num, int expon[])
 
double simplex_unit_monomial_quadrature (int dim_num, int expon[], int point_num, double x[], double w[])
 
doublesimplex_unit_sample (int dim_num, int n, int *seed)
 
doublesimplex_unit_to_general (int dim_num, int point_num, double t[], double ref[])
 
double simplex_unit_volume (int dim_num)
 
void timestamp (void)
 

Macro Definition Documentation

◆ TIME_SIZE

#define TIME_SIZE   40

Function Documentation

◆ comp_next()

void comp_next ( int  n,
int  k,
int  a[],
int *  more,
int *  h,
int *  t 
)

Definition at line 10 of file gm_rule.c.

120 {
121  int i;
122 
123  if ( !( *more ) )
124  {
125  *t = n;
126  *h = 0;
127  a[0] = n;
128  for ( i = 1; i < k; i++ )
129  {
130  a[i] = 0;
131  }
132  }
133  else
134  {
135  if ( 1 < *t )
136  {
137  *h = 0;
138  }
139  *h = *h + 1;
140  *t = a[*h-1];
141  a[*h-1] = 0;
142  a[0] = *t - 1;
143  a[*h] = a[*h] + 1;
144  }
145 
146  *more = ( a[k-1] != n );
147 
148  return;
149 }

◆ gm_rule_set()

void gm_rule_set ( int  rule,
int  dim_num,
int  point_num,
double  w[],
double  x[] 
)

Definition at line 152 of file gm_rule.c.

207 {
208  int *beta;
209  int beta_sum;
210  int d;
211  int dim;
212  int h;
213  int i;
214  int j;
215  int j_hi;
216  int k;
217  int more;
218  int n;
219  double one_pm;
220  int s;
221  int t;
222  double weight;
223 
224  s = rule;
225  d = 2 * s + 1;
226  k = 0;
227  n = dim_num;
228  one_pm = 1.0;
229 
230  beta = ( int * ) malloc ( ( dim_num + 1 ) * sizeof ( int ) );
231 
232  for ( i = 0; i <= s; i++ )
233  {
234  weight = ( double ) one_pm;
235 
236  j_hi = i4_max ( n, i4_max ( d, d + n - i ) );
237 
238  for ( j = 1; j <= j_hi; j++ )
239  {
240  if ( j <= n )
241  {
242  weight = weight * ( double ) ( j );
243  }
244  if ( j <= d )
245  {
246  weight = weight * ( double ) ( d + n - 2 * i );
247  }
248  if ( j <= 2 * s )
249  {
250  weight = weight / 2.0;
251  }
252  if ( j <= i )
253  {
254  weight = weight / ( double ) ( j );
255  }
256  if ( j <= d + n - i )
257  {
258  weight = weight / ( double ) ( j );
259  }
260  }
261 
262  one_pm = - one_pm;
263 
264  beta_sum = s - i;
265  more = 0;
266  h = 0;
267  t = 0;
268 
269  for ( ; ; )
270  {
271  comp_next ( beta_sum, dim_num + 1, beta, &more, &h, &t );
272 
273  w[k] = weight;
274  for ( dim = 0; dim < dim_num; dim++ )
275  {
276  x[dim+k*dim_num] = ( double ) ( 2 * beta[dim+1] + 1 )
277  / ( double ) ( d + n - 2 * i );
278  }
279  k = k + 1;
280 
281  if ( !more )
282  {
283  break;
284  }
285  }
286  }
287 
288  free ( beta );
289 
290  return;
291 }

◆ gm_rule_size()

int gm_rule_size ( int  rule,
int  dim_num 
)

Definition at line 294 of file gm_rule.c.

341 {
342  int arg1;
343  int point_num;
344 
345  arg1 = dim_num + rule + 1;
346 
347  point_num = i4_choose ( arg1, rule );
348 
349  return point_num;
350 }

◆ i4_choose()

int i4_choose ( int  n,
int  k 
)

Definition at line 353 of file gm_rule.c.

397 {
398  int i;
399  int mn;
400  int mx;
401  int value;
402 
403  mn = i4_min ( k, n - k );
404 
405  if ( mn < 0 )
406  {
407  value = 0;
408  }
409  else if ( mn == 0 )
410  {
411  value = 1;
412  }
413  else
414  {
415  mx = i4_max ( k, n - k );
416  value = mx + 1;
417 
418  for ( i = 2; i <= mn; i++ )
419  {
420  value = ( value * ( mx + i ) ) / i;
421  }
422  }
423 
424  return value;
425 }

◆ i4_huge()

int i4_huge ( void  )

Definition at line 428 of file gm_rule.c.

452 {
453  static int value = 2147483647;
454 
455  return value;
456 }

◆ i4_max()

int i4_max ( int  i1,
int  i2 
)

Definition at line 459 of file gm_rule.c.

485 {
486  int value;
487 
488  if ( i2 < i1 )
489  {
490  value = i1;
491  }
492  else
493  {
494  value = i2;
495  }
496  return value;
497 }

◆ i4_min()

int i4_min ( int  i1,
int  i2 
)

Definition at line 500 of file gm_rule.c.

526 {
527  int value;
528 
529  if ( i1 < i2 )
530  {
531  value = i1;
532  }
533  else
534  {
535  value = i2;
536  }
537  return value;
538 }

◆ i4_power()

int i4_power ( int  i,
int  j 
)

Definition at line 541 of file gm_rule.c.

567 {
568  int k;
569  int value;
570 
571  if ( j < 0 )
572  {
573  if ( i == 1 )
574  {
575  value = 1;
576  }
577  else if ( i == 0 )
578  {
579  fprintf ( stderr, "\n" );
580  fprintf ( stderr, "I4_POWER - Fatal error!\n" );
581  fprintf ( stderr, " I^J requested, with I = 0 and J negative.\n" );
582  exit ( 1 );
583  }
584  else
585  {
586  value = 0;
587  }
588  }
589  else if ( j == 0 )
590  {
591  if ( i == 0 )
592  {
593  fprintf ( stderr, "\n" );
594  fprintf ( stderr, "I4_POWER - Fatal error!\n" );
595  fprintf ( stderr, " I^J requested, with I = 0 and J = 0.\n" );
596  exit ( 1 );
597  }
598  else
599  {
600  value = 1;
601  }
602  }
603  else if ( j == 1 )
604  {
605  value = i;
606  }
607  else
608  {
609  value = 1;
610  for ( k = 1; k <= j; k++ )
611  {
612  value = value * i;
613  }
614  }
615  return value;
616 }

◆ monomial_value()

double* monomial_value ( int  dim_num,
int  point_num,
double  x[],
int  expon[] 
)

Definition at line 619 of file gm_rule.c.

662 {
663  int dim;
664  int point;
665  double *value;
666 
667  value = ( double * ) malloc ( point_num * sizeof ( double ) );
668 
669  for ( point = 0; point < point_num; point++ )
670  {
671  value[point] = 1.0;
672  }
673 
674  for ( dim = 0; dim < dim_num; dim++ )
675  {
676  if ( 0 != expon[dim] )
677  {
678  for ( point = 0; point < point_num; point++ )
679  {
680  value[point] = value[point] * pow ( x[dim+point*dim_num], expon[dim] );
681  }
682  }
683  }
684 
685  return value;
686 }

◆ r8_abs()

double r8_abs ( double  x)

Definition at line 689 of file gm_rule.c.

715 {
716  double value;
717 
718  if ( 0.0 <= x )
719  {
720  value = + x;
721  }
722  else
723  {
724  value = - x;
725  }
726  return value;
727 }

◆ r8_factorial()

double r8_factorial ( int  n)

Definition at line 730 of file gm_rule.c.

761 {
762  int i;
763  double value;
764 
765  value = 1.0;
766 
767  for ( i = 1; i <= n; i++ )
768  {
769  value = value * ( double ) ( i );
770  }
771 
772  return value;
773 }

◆ r8vec_dot_product()

double r8vec_dot_product ( int  n,
double  a1[],
double  a2[] 
)

Definition at line 776 of file gm_rule.c.

804 {
805  int i;
806  double value;
807 
808  value = 0.0;
809  for ( i = 0; i < n; i++ )
810  {
811  value = value + a1[i] * a2[i];
812  }
813  return value;
814 }

◆ r8vec_uniform_01_new()

double* r8vec_uniform_01_new ( int  n,
int *  seed 
)

Definition at line 817 of file gm_rule.c.

884 {
885  int i;
886  int i4_huge = 2147483647;
887  int k;
888  double *r;
889 
890  if ( *seed == 0 )
891  {
892  fprintf ( stderr, "\n" );
893  fprintf ( stderr, "R8VEC_UNIFORM_01_NEW - Fatal error!\n" );
894  fprintf ( stderr, " Input value of SEED = 0.\n" );
895  exit ( 1 );
896  }
897 
898  r = ( double * ) malloc ( n * sizeof ( double ) );
899 
900  for ( i = 0; i < n; i++ )
901  {
902  k = *seed / 127773;
903 
904  *seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
905 
906  if ( *seed < 0 )
907  {
908  *seed = *seed + i4_huge;
909  }
910 
911  r[i] = ( double ) ( *seed ) * 4.656612875E-10;
912  }
913 
914  return r;
915 }

◆ simplex_unit_monomial_int()

double simplex_unit_monomial_int ( int  dim_num,
int  expon[] 
)

Definition at line 918 of file gm_rule.c.

957 {
958  int dim;
959  int i;
960  int k;
961  double value;
962 
963  value = 1.0;
964 
965  k = 0;
966 
967  for ( dim = 0; dim < dim_num; dim++ )
968  {
969  for ( i = 1; i <= expon[dim]; i++ )
970  {
971  k = k + 1;
972  value = value * ( double ) ( i ) / ( double ) ( k );
973  }
974  }
975 
976  for ( dim = 0; dim < dim_num; dim++ )
977  {
978  k = k + 1;
979  value = value / ( double ) ( k );
980  }
981 
982  return value;
983 }

◆ simplex_unit_monomial_quadrature()

double simplex_unit_monomial_quadrature ( int  dim_num,
int  expon[],
int  point_num,
double  x[],
double  w[] 
)

Definition at line 986 of file gm_rule.c.

1021 {
1022  double exact = 1.0;
1023  double quad;
1024  double quad_error;
1025  double scale;
1026  double *value;
1027  double volume;
1028 /*
1029  Get the exact value of the integral of the unscaled monomial.
1030 */
1031  scale = simplex_unit_monomial_int ( dim_num, expon );
1032 /*
1033  Evaluate the monomial at the quadrature points.
1034 */
1035  value = monomial_value ( dim_num, point_num, x, expon );
1036 /*
1037  Compute the weighted sum and divide by the exact value.
1038 */
1039  volume = simplex_unit_volume ( dim_num );
1040  quad = volume * r8vec_dot_product ( point_num, w, value ) / scale;
1041 
1042  free ( value );
1043 /*
1044  Error:
1045 */
1046  quad_error = r8_abs ( quad - exact );
1047 
1048  return quad_error;
1049 }

◆ simplex_unit_sample()

double* simplex_unit_sample ( int  dim_num,
int  n,
int *  seed 
)

Definition at line 1052 of file gm_rule.c.

1099 {
1100  double *e;
1101  int i;
1102  int j;
1103  double total;
1104  double *x;
1105 /*
1106  The construction begins by sampling DIM_NUM+1 points from the
1107  exponential distribution with parameter 1.
1108 */
1109  x = ( double * ) malloc ( dim_num * n * sizeof ( double ) );
1110 
1111  for ( j = 0; j < n; j++ )
1112  {
1113  e = r8vec_uniform_01_new ( dim_num+1, seed );
1114 
1115  for ( i = 0; i <= dim_num; i++ )
1116  {
1117  e[i] = -log ( e[i] );
1118  }
1119 
1120  total = 0.0;
1121  for ( i = 0; i <= dim_num; i++ )
1122  {
1123  total = total + e[i];
1124  }
1125 
1126  for ( i = 0; i < dim_num; i++ )
1127  {
1128  x[i+dim_num*j] = e[i] / total;
1129  }
1130  free ( e );
1131  }
1132 
1133  return x;
1134 }

◆ simplex_unit_to_general()

double* simplex_unit_to_general ( int  dim_num,
int  point_num,
double  t[],
double  ref[] 
)

Definition at line 1137 of file gm_rule.c.

1192 {
1193  int dim;
1194  double *phy;
1195  int point;
1196  int vertex;
1197 
1198  phy = ( double * ) malloc ( dim_num * point_num * sizeof ( double ) );
1199 //
1200 // The image of each point is initially the image of the origin.
1201 //
1202 // Insofar as the pre-image differs from the origin in a given vertex
1203 // direction, add that proportion of the difference between the images
1204 // of the origin and the vertex.
1205 //
1206  for ( point = 0; point < point_num; point++ )
1207  {
1208  for ( dim = 0; dim < dim_num; dim++ )
1209  {
1210  phy[dim+point*dim_num] = t[dim+0*dim_num];
1211 
1212  for ( vertex = 1; vertex < dim_num + 1; vertex++ )
1213  {
1214  phy[dim+point*dim_num] = phy[dim+point*dim_num]
1215  + ( t[dim+vertex*dim_num] - t[dim+0*dim_num] ) * ref[vertex-1+point*dim_num];
1216  }
1217  }
1218  }
1219 
1220  return phy;
1221 }

◆ simplex_unit_volume()

double simplex_unit_volume ( int  dim_num)

Definition at line 1224 of file gm_rule.c.

1254 {
1255  int i;
1256  double volume;
1257 
1258  volume = 1.0;
1259  for ( i = 1; i <= dim_num; i++ )
1260  {
1261  volume = volume / ( ( double ) i );
1262  }
1263 
1264  return volume;
1265 }

◆ timestamp()

void timestamp ( void  )

Definition at line 1268 of file gm_rule.c.

1296 {
1297 # define TIME_SIZE 40
1298 
1299  static char time_buffer[TIME_SIZE];
1300  const struct tm *tm;
1301  size_t len;
1302  time_t now;
1303 
1304  (void)(len);
1305 
1306  now = time ( NULL );
1307  tm = localtime ( &now );
1308 
1309  len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm );
1310 
1311  fprintf ( stdout, "%s\n", time_buffer );
1312 
1313  return;
1314 # undef TIME_SIZE
1315 }
a2
constexpr double a2
Definition: hcurl_check_approx_in_2d.cpp:39
r8vec_uniform_01_new
double * r8vec_uniform_01_new(int n, int *seed)
Definition: gm_rule.c:817
i4_max
int i4_max(int i1, int i2)
Definition: gm_rule.c:459
scale
double scale
Definition: plastic.cpp:119
sdf.r
int r
Definition: sdf.py:8
monomial_value
double * monomial_value(int dim_num, int point_num, double x[], int expon[])
Definition: gm_rule.c:619
comp_next
void comp_next(int n, int k, int a[], int *more, int *h, int *t)
Definition: gm_rule.c:10
a1
constexpr double a1
Definition: hcurl_check_approx_in_2d.cpp:38
a
constexpr double a
Definition: approx_sphere.cpp:30
r8_abs
double r8_abs(double x)
Definition: gm_rule.c:689
double
i4_choose
int i4_choose(int n, int k)
Definition: gm_rule.c:353
r8vec_dot_product
double r8vec_dot_product(int n, double a1[], double a2[])
Definition: gm_rule.c:776
h
double h
Definition: photon_diffusion.cpp:60
t
constexpr double t
plate stiffness
Definition: plate.cpp:58
i
FTensor::Index< 'i', SPACE_DIM > i
Definition: hcurl_divergence_operator_2d.cpp:27
convert.n
n
Definition: convert.py:82
TIME_SIZE
#define TIME_SIZE
i4_min
int i4_min(int i1, int i2)
Definition: gm_rule.c:500
j
FTensor::Index< 'j', 3 > j
Definition: matrix_function.cpp:19
simplex_unit_volume
double simplex_unit_volume(int dim_num)
Definition: gm_rule.c:1224
sdf_hertz_2d_axisymm_plane.d
float d
Definition: sdf_hertz_2d_axisymm_plane.py:4
i4_huge
int i4_huge(void)
Definition: gm_rule.c:428
sdf_wavy_2d.w
int w
Definition: sdf_wavy_2d.py:6
simplex_unit_monomial_int
double simplex_unit_monomial_int(int dim_num, int expon[])
Definition: gm_rule.c:918
k
FTensor::Index< 'k', 3 > k
Definition: matrix_function.cpp:20
convert.int
int
Definition: convert.py:64