-
Notifications
You must be signed in to change notification settings - Fork 0
/
white-dwarf.c
69 lines (58 loc) · 1.87 KB
/
white-dwarf.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
/*
* The program solves the following system of two
* first order differential equations, which describe
* the mass, density and radius of a white dwarf star.
*
* This program exports radius vs density vs mass.
*
*
* The step-size of the integrator is automatically
* adjusted by the controller to maintain the
* requested accuracy.
*/
#include <stdio.h>
#include <math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_odeiv2.h>
int dwarf_eqs (double r, const double y[], double f[], void *params);
int
main (void)
{
size_t neqs = 2; /* number of equations */
double eps_abs = 1.e-8, eps_rel = 0.; /* desired precision */
double stepsize = 1e-6; /* initial integration step */
double rho_c = 10.; /* central density */
double r = 1.e-3, r1 = 5.; /* time interval */
int status;
/*
* Initial conditions
*/
double y[2] = { 0, rho_c };
/*
* Explicit embedded Runge-Kutta-Fehlberg (4,5) method.
* This method is a good general-purpose integrator.
*/
gsl_odeiv2_step *s = gsl_odeiv2_step_alloc (gsl_odeiv2_step_rkf45, neqs);
gsl_odeiv2_control *c = gsl_odeiv2_control_y_new (eps_abs,
eps_rel);
gsl_odeiv2_evolve *e = gsl_odeiv2_evolve_alloc (neqs);
gsl_odeiv2_system sys = { dwarf_eqs, NULL, neqs, &rho_c };
/*
* Evolution loop
*/
while ((r < r1) && (y[1] > 0))
{
status = gsl_odeiv2_evolve_apply (e, c, s, &sys, &r,
r1, &stepsize, y);
if (status != GSL_SUCCESS)
{
printf ("Troubles: % .5e % .5e % .5e\n", r, y[0], y[1]);
break;
}
printf ("% .5e % .5e % .5ex\n", r, y[0], y[1]);
}
gsl_odeiv2_evolve_free (e);
gsl_odeiv2_control_free (c);
gsl_odeiv2_step_free (s);
return 0;
}