-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtortoise.c
420 lines (360 loc) · 8.51 KB
/
tortoise.c
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
#include "header/tortoise.h"
#include "header/ui.h"
#include <math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_odeiv2.h>
#include <gsl/gsl_matrix.h>
#define TOR_SYS_DIM 1
#define TOR_MIN_INF -1000000
static GArray *m_xy;
static TPoint2D m_yx[NODES];
static double m_xyAnalytical[NODES];
/**
@brief Jacobian xy[]: rS/r(y)^2
*/
static int
jacoXY(double x,
const double y[],
double *dfdy,
double dfdt[],
void *params)
{
(void)(x);
struct tortoise_xyParams *par = (struct tortoise_xyParams*)params;
double rS = (par->rS);
gsl_matrix_view dfdy_mat = gsl_matrix_view_array(dfdy, 1, 1);
gsl_matrix *m = &dfdy_mat.matrix;
gsl_matrix_set (m, 0, 0, rS/(y[0]*y[0]));
dfdt[0] = 0.0;
return GSL_SUCCESS;
}
/**
@brief IV ODE to solve x[y]' that appear in the calculation of V
@param t Independent variable
@param y[] Left side of the system of equations
@param sys[] Right side of the system of equations
*/
static int
funcXY(double x,
const double y[],
double sys[],
void* params)
{
(void)(x);
struct tortoise_xyParams* par = (struct tortoise_xyParams*)params;
double rS = (par->rS);
sys[0] = 1 - rS/y[0];
return GSL_SUCCESS;
}
/**
@brief Jacobian yx[]: -rS/(r^2*(1 - rS/r)^2)
*/
static int
jacoYX(double x,
const double y[],
double *dfdy,
double dfdt[],
void *params)
{
(void)(y);
struct tortoise_xyParams *par = (struct tortoise_xyParams*)params;
double rS = (par->rS);
gsl_matrix_view dfdy_mat = gsl_matrix_view_array(dfdy, 1, 1);
gsl_matrix *m = &dfdy_mat.matrix;
gsl_matrix_set (m, 0, 0, -rS/(x*x*(1 - rS/x)*(1 - rS/x)));
dfdt[0] = 0.0;
return GSL_SUCCESS;
}
/**
@brief IV ODE to solve y[x]' that appear in the calculation of V
@param t Independent variable
@param y[] Left side of the system of equations
@param sys[] Right side of the system of equations
*/
static int
funcYX(double x,
const double y[],
double sys[],
void* params)
{
(void)(y);
struct tortoise_xyParams* par = (struct tortoise_xyParams*)params;
double rS = (par->rS);
sys[0] = 1.0/(1.0 - rS/x);
return GSL_SUCCESS;
}
/**
@brief Compare function to order doubles
*/
static gint
compare_double_gth(gconstpointer a,
gconstpointer b)
{
int result = 0;
double *aa = (double*)a;
double *bb = (double*)b;
if(*aa - *bb > 0)
result = 1;
else if(*aa - *bb < 0)
result = -1;
return result;
}
/**
@brief Compare function to find 'x' in 'yx'
*/
static gint
compare_yx_gth(gconstpointer a,
gconstpointer b)
{
int result = 0;
const TPoint2D *aa = a;
const TPoint2D *bb = b;
if(point2D_get_y(*aa) - point2D_get_y(*bb) < 0)
result = -1;
return result;
}
/* -----------------------------------------------------------------------------
PUBLIC
----------------------------------------------------------------------------- */
void
tortoise_init(void)
{
m_xy = g_array_new(FALSE, FALSE, sizeof(TPoint2D));
}
void
tortoise_destroy(void)
{
g_array_free(m_xy, FALSE);
}
/**
@brief Calculate coordintes 'yx' and 'xy'
*/
void
tortoise_calculate_yx_xy(void)
{
double a = ui_get_a();
double b = ui_get_b();
double rS = ui_get_rS();
struct tortoise_xyParams torParam = {rS};
/* Calculate yx from analytic expression */
tortoise_yx_analytical();
/* Now that we have y(x) we can use it to get the x'(y) IC */
double ci = b - rS*log(b/rS - 1);
ci = tortoise_get_yx_x(b);
tortoise_xy_integration(b, a, NODES, ci, &torParam);
tortoise_sort_asc();
}
int
tortoise_xy_integration(double a,
double b,
int nodes,
double ci,
void *param)
{
int status = GSL_SUCCESS;
struct tortoise_xyParams *par = (struct tortoise_xyParams*)(param);
double rS = par->rS;
int n = nodes - 1;
double h = (b - a)/ n;
double x0 = a;
double x1 = x0 + h;
double epsAbs = 0;
double epsRel = 1e-6;
struct tortoise_xyParams params = {rS};
gsl_odeiv2_system sys = {funcXY, jacoXY, TOR_SYS_DIM, ¶ms};
const gsl_odeiv2_step_type* T = gsl_odeiv2_step_rk8pd; /* rk2 rk4 rkf45 rk8pd, msbdf msadams rk4imp */
gsl_odeiv2_driver* d = gsl_odeiv2_driver_alloc_y_new (&sys, T, h, epsAbs, epsRel);
double y[1] = {ci};
int i;
for (i = 0; i < nodes; i++)
{
status = gsl_odeiv2_driver_apply(d, &x0, x1, y);
x0 = x1;
x1 = x0 + h;
if (status != GSL_SUCCESS)
{
printf ("error, return value = %d\n", status);
break;
}
/*
y[0] function value
y[1] derivative function value
*/
TPoint2D p2d = {x0, y[0]};
g_array_append_val(m_xy, p2d);
}
gsl_odeiv2_driver_free(d);
return status;
}
int
tortoise_yx_integration(double a,
double b,
int nodes,
double ci,
void *param)
{
int status = GSL_SUCCESS;
struct tortoise_xyParams *par = (struct tortoise_xyParams*)(param);
double rS = par->rS;
int n = nodes - 1;
double h = (b - a)/ n;
double x0 = a;
double x1 = x0 + h;
double epsAbs = 0;
double epsRel = 1e-6;
struct tortoise_xyParams params = {rS};
gsl_odeiv2_system sys = {funcYX, jacoYX, TOR_SYS_DIM, ¶ms};
const gsl_odeiv2_step_type* T = gsl_odeiv2_step_msbdf; /* rk2 rk4 rkf45 rk8pd, msbdf msadams rk4imp */
gsl_odeiv2_driver* d = gsl_odeiv2_driver_alloc_y_new (&sys, T, h, epsAbs, epsRel);
double y[1] = {ci};
int i;
for (i = 0; i < nodes; i++)
{
status = gsl_odeiv2_driver_apply(d, &x0, x1, y);
x0 = x1;
x1 = x0 + h;
if (status != GSL_SUCCESS)
{
printf ("error, return value = %d\n", status);
break;
}
/*
y[0] function value
y[1] derivative function value
*/
TPoint2D p2d = {x0, y[0]};
m_yx[i] = p2d;
}
gsl_odeiv2_driver_free(d);
return status;
}
/**
@brief Reverse the array
*/
void
tortoise_sort_asc(void)
{
g_array_sort(m_xy, (GCompareFunc)compare_double_gth);
}
GArray*
tortoise_get_xy(void)
{
return m_xy;
}
TPoint2D*
tortoise_get_yx(void)
{
return m_yx;
}
/**
@brief Search the 'x' in 'yx' that has the more approximate value of 'y'
NOTE In that case we have built 'x' so we've got the formula
*/
double
tortoise_get_xy_y(double x)
{
double a = ui_get_a();
double h = ui_get_h_forward();
gint i = (x - a)/h;
TPoint2D p2d = g_array_index(m_xy, TPoint2D, i);
return point2D_get_y(p2d);
}
/**
@brief Search the 'y' in 'yx' that has the more approximate value of 'x'
*/
double
tortoise_get_yx_x(double y)
{
double result;
GArray *tmp = g_array_new(FALSE, FALSE, sizeof(TPoint2D));
/* Short the array */
g_array_append_vals(tmp, &m_yx, NODES);
g_array_sort(tmp, (GCompareFunc)compare_double_gth);
/* Search for the more approximate value of 'y' */
guint index;
TPoint2D target = {0, y};
gboolean find = g_array_binary_search(tmp, &target, (GCompareFunc)compare_yx_gth, &index);
/*
We know that the value is >= but not optimal, so we search for it now that
we're close
*/
if(find)
{
gint i = index;
TPoint2D p2d = g_array_index(tmp, TPoint2D, i);
double yTarget = point2D_get_y(p2d);
while(yTarget - y > 0 && i >= 0)
{
i--;
p2d = g_array_index(tmp, TPoint2D, i);
yTarget = point2D_get_y(p2d);
}
p2d = g_array_index(tmp, TPoint2D, i);
result = point2D_get_x(p2d);
}
else
{
index = NODES - 1;
TPoint2D p2d = g_array_index(tmp, TPoint2D, index);
result = point2D_get_x(p2d);
}
g_array_free(tmp, FALSE);
return result;
}
TPoint2D
tortoise_get_yx_i(int i)
{
return m_yx[i];
}
/**
@brief Analytical result (forward)
*/
void
tortoise_xy_analytical(void)
{
double x = ui_get_a();
double h = ui_get_h_forward();
double rS = ui_get_rS();
int i;
for(i = 0; i < NODES; i++)
{
/*
NOTE I'm using yx because if I use xy the graphic is no so good (it should
be difficult to plot the exponential... I guest)
*/
double yx = x + rS*log(x/rS - 1);
/* log(x <= 0) = -inf */
if(isnan(yx))
yx = TOR_MIN_INF;
m_xyAnalytical[i] = yx;
x += h;
}
}
double*
tortoise_get_xy_analytical(void)
{
return m_xyAnalytical;
}
void
tortoise_yx_analytical(void)
{
double x = ui_get_a();
double h = ui_get_h_forward();
double rS = ui_get_rS();
int i;
for(i = 0; i < NODES; i++)
{
double yx = x + rS*log(x/rS - 1);
/* log(x <= 0) = -inf */
if(isnan(yx))
yx = TOR_MIN_INF;
TPoint2D p2d = {x, yx};
m_yx[i] = p2d;
x += h;
}
}
TPoint2D*
tortoise_get_yx_analytical(void)
{
return m_yx;
}