X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=energy.c;h=f28135a3bf4d2bc02ce9f700c89d0d6c77dfba20;hp=139e7e1304d11937fe5d5261e6dba81d9ad55c3e;hb=62710d55de09f02e31a9fc2cc7e3ec5de44651e4;hpb=2c2c1cc2745430048709dc3b11aef7071d802c86 diff --git a/energy.c b/energy.c index 139e7e1..f28135a 100644 --- a/energy.c +++ b/energy.c @@ -6,22 +6,26 @@ #include "bgl.h" #include "mgraph.h" +#include +#include + #define BEST_F "best" #define INITIAL_F "initial" static double edgewise_vertex_displacement_cost(const Vertices vertices); -static double noncircular_rim_cost(Vertices vertices); +static double noncircular_rim_cost(const Vertices vertices); static void compute_vertex_areas(const Vertices vertices, double areas[N]); static double best_energy= DBL_MAX; static void flushoutput(void); +static void diee(const char *what) { perror(what); exit(16); } static void cost(double *energy, double tweight, double tcost); #define COST(weight, compute) cost(&energy, (weight), (compute)) /*---------- main energy computation and subroutines ----------*/ -static double compute_energy(Vertices vertices) { +static double compute_energy(const Vertices vertices) { double vertex_areas[N], energy; compute_vertex_areas(vertices,vertex_areas); @@ -34,7 +38,9 @@ static double compute_energy(Vertices vertices) { printf("| total %# e |", energy); if (energy < best_energy) { - FILE *best; + FILE *best_f; + int r; + printf(" BEST"); best_f= fopen(BEST_F ".new","wb"); if (!best_f) diee("fopen new best"); @@ -55,10 +61,12 @@ static void cost(double *energy, double tweight, double tcost) { } static void flushoutput(void) { - if (fflush(stdout) || ferror(stdout)) { perror("stdout"); exit(-1); } + if (fflush(stdout) || ferror(stdout)) diee("stdout"); } static void compute_vertex_areas(const Vertices vertices, double areas[N]) { + int v0,v1,v2, e1,e2, k; + FOR_VERTEX(v0) { double total= 0.0; int count= 0; @@ -74,7 +82,7 @@ static void compute_vertex_areas(const Vertices vertices, double areas[N]) { e2v[k]= vertices[v2][k] - vertices[v0][k]; } xprod(av, e1v, e2v); - total += hypotD1(av); + total += magnD(av); count++; } areas[v0]= total / count; @@ -114,23 +122,23 @@ static void gsldie(const char *what, int status) { static gsl_multimin_fminimizer *minimiser; -static const stop_epsilon= 1e-4; +static const double stop_epsilon= 1e-4; #define DIM (N*D3) static double minfunc_f(const gsl_vector *x, void *params) { assert(x->size == DIM); assert(x->stride == 1); - return compute_energy((Vertices)x->data); + return compute_energy((const double(*)[D3])x->data); } int main(int argc, const char *const *argv) { - struct gsl_multimin_function multimin_function; + gsl_multimin_function multimin_function; double size; Vertices initial, step_size; - FILE *initial; + FILE *initial_f; gsl_vector initial_gsl, step_size_gsl; - int r; + int r, v, vx,vy, k; if (argc>1) { fputs("takes no arguments\n",stderr); exit(8); } @@ -153,27 +161,16 @@ int main(int argc, const char *const *argv) { initial_gsl.owner= 0; step_size_gsl= initial_gsl; - initial_gsl.data= initial; - step_size_gsl.data= step_size; + initial_gsl.data= (double*)initial; + step_size_gsl.data= (double*)step_size; FOR_VERTEX(v) K step_size[v][k]= 1e-3; FOR_RIM_VERTEX(vx,vy,v) step_size[v][3] *= 0.1; - - for (vy=0; vy