chiark / gitweb /
curveopt: fix handling of empty lines from findcurve
[moebius3.git] / findcurve.c
1 /*
2  */
3
4 #include <math.h>
5 #include <string.h>
6
7 #include <gsl/gsl_errno.h>
8 #include <gsl/gsl_sf.h>
9 #include <gsl/gsl_siman.h>
10 #include <gsl/gsl_randist.h>
11
12 #include "symbolic.c"
13
14 #define J_END_COL(i) \
15   for (j=0; j<PN; j++) gsl_matrix_set(J,i,j,J_COL[j]);
16
17 static inline _Bool IS_SMALL(double v) {
18   return v < 1E6;
19 }
20
21 static inline double sinc(double x) {
22   return gsl_sf_sinc(x / M_PI);
23 }
24
25 static int NP;
26 static double *INPUT; /* dyanmic array, on main's stack */
27 static double PREP[NPREP];
28
29 static void printcore(const double *X) {
30   int i, j;
31   DECLARE_F_G;
32   CALCULATE_F_G;
33   printf("[");
34   for (i=0; i<NP; i++)
35     for (j=0; j<3; j++)
36       printf(" %25.18g,", POINT(i)[j]);
37   printf(" ]\n");
38 }
39
40 static void prepare(double X[] /* startpoint */) {
41   /* fills in PREP and startpoint */
42   PREPARE;
43 }
44
45 //#define DEBUG
46
47 static double cb_Efunc(void *xp) {
48   const double *X = xp;
49   int P;
50   DECLARE_F_G;
51   CALCULATE_F_G;
52
53 #ifdef DEBUG
54   int i,j;
55   printf(" Efunc\n");
56   for (j=0; j<3; j++) {
57     for (P=0; P<NP; P++)
58       printf(" %7.4f", POINT(P)[j]);
59     printf("\n");
60   }
61   printf("             ");
62 #endif
63
64   CALCULATE_COST;
65   return cost;
66 }
67
68 static void cb_step(const gsl_rng *rng, void *xp, double step_size) {
69   double *x = xp;
70   int i;
71   double step[NX];
72   gsl_ran_dir_nd(rng, NX, step);
73   for (i=0; i<NX; i++)
74     x[i] += step_size * step[i];
75   //printf("\n cb_step %p %10.7f [", xp, step_size);
76   //for (i=0; i<N; i++) printf(" %10.7f,", step[i]);
77   //printf("]\n");
78 }
79
80 static double cb_metric(void *xp, void *yp) {
81   const double *x=xp, *y=yp;
82   int i;
83   double s;
84   for (i=0, s=0; i<NX; i++) {
85     double d = x[i] - y[i];
86     s += d*d;
87   }
88   return sqrt(s);
89 }
90
91 static void __attribute__((unused)) cb_print(void *xp) {
92   const double *x = xp;
93   printf("\n");
94   printcore(x);
95 }
96
97 static double scan1double(void) {
98   double v;
99   int r;
100
101   r = scanf("%lf",&v);
102   if (ferror(stdin)) { perror("stdin"); exit(-1); }
103   if (feof(stdin)) exit(0);
104   if (r!=1) { fputs("bad input\n",stderr); exit(-1); }
105   return v;
106 }
107
108 int main(int argc, const char *const *argv) {
109   double epsilon;
110   int i;
111
112   NP = atoi(argv[1]);
113   epsilon = atof(argv[2]);
114
115   gsl_rng *rng = gsl_rng_alloc(gsl_rng_ranlxd2);
116
117   double input[NINPUT]; INPUT = input;
118   double startpoint[NX];
119
120   for (;;) {
121     /* NINPUT + 1 doubles: startpoint, epsilon for residual */
122     for (i=0; i<NINPUT; i++)
123       INPUT[i] = scan1double();
124
125     gsl_rng_set(rng,0);
126
127     gsl_siman_params_t siman_params = {
128       .k = 1.0,
129       .t_initial = 0.5,
130       .mu_t = 1.001,
131       .t_min = epsilon * 1E-3,
132       .iters_fixed_T = 100,
133       .n_tries = 10,
134       .step_size = 0.05,
135     };
136
137     prepare(startpoint);
138
139     gsl_siman_solve(rng,
140                     startpoint,
141                     cb_Efunc, cb_step, cb_metric,
142                     0, // cb_print,
143                     0,0,0, sizeof(startpoint), siman_params);
144
145     printcore(startpoint);
146
147     printf("[]\n");
148     fflush(stdout);
149   }
150 }