-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrossler.c
119 lines (100 loc) · 2.89 KB
/
rossler.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
/*
* rossler_cairo.c
* Copyright (C) Farooq Mela 2003-2013.
*
* Traces the path of a Rossler attractor.
* Duffing attractors are defined by 3 coupled differential equations,
*
* dx/dt = -(y + z)
* dy/dt = x + a * y
* dz/dt = b + z * (x - c)
*
* Common constants {a,b,c}: {0.2,0.2,5.7}
*
* 06-02-03
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <cairo/cairo.h>
#include "rk4.h"
// Constants.
const double a = 0.2, b = 0.2, c = 5.7;
void f(double t, const double y[], double dy[]) {
(void)t;
dy[0] = -(y[1] + y[2]);
dy[1] = y[0] + a * y[1];
dy[2] = b + y[2] * (y[0] - c);
}
int
main(void)
{
const int kWidth = 900, kHeight = 900;
const double kXLeft = -10, kXRight = 12;
const double kYBottom = -12, kYTop = 10;
const double h = 0.01;
const int kIterations = 1000000;
const char kOutput[] = "images/rossler.png";
clock_t start = clock();
#define N 3
// Initial condition.
double y[N] = { 0.1, 0.1, 0.1 };
// Initialize bounding box.
double min[N], max[N];
for (int j = 0; j < N; ++j)
min[j] = max[j] = y[j];
cairo_surface_t *cr_surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
kWidth, kHeight);
cairo_t *cr = cairo_create(cr_surf);
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "Cairo: %s\n",
cairo_status_to_string(cairo_status(cr)));
exit(1);
}
cairo_set_antialias(cr, CAIRO_ANTIALIAS_GOOD);
// Blank to white.
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
cairo_paint(cr);
cairo_set_line_width(cr, 0.002);
cairo_scale(cr, kWidth / (kXRight - kXLeft), kHeight / (kYBottom - kYTop));
cairo_translate(cr,
10.0,
-10.0);
// Trace the attractor.
for (int i=0; i<kIterations; ++i) {
// Compute next position.
double y_next[N];
rk4v(N, f, i * h, h, y, y_next);
for (int j = 0; j < N; ++j) {
if (min[j] > y_next[j]) min[j] = y_next[j]; else
if (max[j] < y_next[j]) max[j] = y_next[j];
}
// Draw.
cairo_set_source_rgba(cr, 0., 0., 0., 1.);
cairo_move_to(cr, y[0], y[1]);
cairo_line_to(cr, y_next[0], y_next[1]);
cairo_stroke(cr);
// Update position.
for (int j = 0; j < N; ++j)
y[j] = y_next[j];
}
clock_t render_finish = clock();
printf("Rendering: %.2fs\n",
(render_finish - start) / (double)CLOCKS_PER_SEC);
printf("x={%.2f,%.2f} y={%.2f,%.2f} z={%.2f,%.2f}\n",
min[0], max[0], min[1], max[1], min[2], max[2]);
cairo_status_t status = cairo_surface_write_to_png(cr_surf, kOutput);
if (status != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "Failed to save to %s: %s\n",
kOutput, cairo_status_to_string(status));
exit(1);
}
cairo_surface_destroy(cr_surf);
cairo_destroy(cr);
clock_t save_finish = clock();
printf("Saving to %s: %.2fs\n", kOutput,
(save_finish - render_finish) / (double)CLOCKS_PER_SEC);
exit(0);
}