accelerInt  v0.1
fd_jacob.c
Go to the documentation of this file.
1 
6 #include "header.h"
7 #include "dydt.h"
8 #include <math.h>
9 #include <float.h>
10 #include <string.h>
11 #include "solver_options.h"
12 
14 #define FD_ORD 1
15 
24 void eval_jacob (const double t, const double pres, const double * cy, double * jac) {
25 
26  double y[NSP];
27  memcpy(y, cy, NSP * sizeof(double));
28  double dy[NSP];
29  dydt (t, pres, y, dy);
30 
31  // Finite difference coefficients
32  #if FD_ORD != 1
33  double x_coeffs[FD_ORD];
34  double y_coeffs[FD_ORD];
35 
36  if (FD_ORD == 2) {
37  // 2nd order central difference
38  x_coeffs[0] = -1.0;
39  x_coeffs[1] = 1.0;
40  y_coeffs[0] = -0.5;
41  y_coeffs[1] = 0.5;
42  } else if (FD_ORD == 4) {
43  // 4th order central difference
44  x_coeffs[0] = -2.0;
45  x_coeffs[1] = -1.0;
46  x_coeffs[2] = 1.0;
47  x_coeffs[3] = 2.0;
48  y_coeffs[0] = 1.0 / 12.0;
49  y_coeffs[1] = -2.0 / 3.0;
50  y_coeffs[2] = 2.0 / 3.0;
51  y_coeffs[3] = -1.0 / 12.0;
52  } else if (FD_ORD == 6) {
53  // 6th order central difference
54  x_coeffs[0] = -3.0;
55  x_coeffs[1] = -2.0;
56  x_coeffs[2] = -1.0;
57  x_coeffs[3] = 1.0;
58  x_coeffs[4] = 2.0;
59  x_coeffs[5] = 3.0;
60 
61  y_coeffs[0] = -1.0 / 60.0;
62  y_coeffs[1] = 3.0 / 20.0;
63  y_coeffs[2] = -3.0 / 4.0;
64  y_coeffs[3] = 3.0 / 4.0;
65  y_coeffs[4] = -3.0 / 20.0;
66  y_coeffs[5] = 1.0 / 60.0;
67  }
68  #endif
69 
70  double ewt[NSP];
71 
72  for (int i = 0; i < NSP; ++i) {
73  ewt[i] = ATOL + (RTOL * fabs(y[i]));
74  }
75 
76  // unit roundoff of machine
77  double srur = sqrt(DBL_EPSILON);
78 
79  double sum = 0.0;
80 
81  for (int i = 0; i < NSP; ++i) {
82  sum += (ewt[i] * dy[i]) * (ewt[i] * dy[i]);
83  }
84  double fac = sqrt(sum / ((double)(NSP)));
85  double r0 = 1000.0 * RTOL * DBL_EPSILON * ((double)(NSP)) * fac;
86 
87  double ftemp[NSP];
88 
89 
90  for (int j = 0; j < NSP; ++j) {
91  double yj_orig = y[j];
92  double r = fmax(srur * fabs(yj_orig), r0 / ewt[j]);
93 
94  #if FD_ORD==1
95  y[j] = yj_orig + r;
96  dydt (t, pres, y, ftemp);
97 
98 
99  for (int i = 0; i < NSP; ++i) {
100  jac[i + NSP*j] = (ftemp[i] - dy[i]) / r;
101  }
102  #else
103 
104  for (int i = 0; i < NSP; ++i) {
105  jac[i + NSP*j] = 0.0;
106  }
107 
108  for (int k = 0; k < FD_ORD; ++k) {
109  y[j] = yj_orig + x_coeffs[k] * r;
110  dydt (t, pres, y, ftemp);
111 
112 
113  for (int i = 0; i < NSP; ++i) {
114  jac[i + NSP*j] += y_coeffs[k] * ftemp[i];
115  }
116  }
117 
118  for (int i = 0; i < NSP; ++i) {
119  jac[i + NSP*j] /= r;
120  }
121 
122  #endif
123 
124  y[j] = yj_orig;
125  }
126 
127 }
Contains header definitions for the RHS function for the van der Pol example.
An example header file that defines system size and other required methods for integration of the van...
void dydt(const double t, const double mu, const double *__restrict__ y, double *__restrict__ dy)
An implementation of the RHS of the van der Pol equation.
Definition: dydt.c:22
void eval_jacob(const double t, const double pres, const double *cy, double *jac)
Computes a finite difference Jacobian of order FD_ORD of the RHS function dydt at the given pressure ...
Definition: fd_jacob.c:24
#define NSP
The IVP system size.
Definition: header.cuh:20
A file generated by Scons that specifies various options to the solvers.
#define RTOL
#define ATOL
#define FD_ORD
The finite difference order [Default: 1].
Definition: fd_jacob.c:14