int solve_spiro(spiro_seg *s, int nseg) { bandmat *m; double *v; int *perm; int nmat = count_vec(s, nseg); int n_alloc = nmat; double norm; int i; if (nmat == 0) return 0; if (s[0].ty != '{' && s[0].ty != 'v') n_alloc *= 3; if (n_alloc < 5) n_alloc = 5; m = (bandmat *)malloc(sizeof(bandmat) * n_alloc); v = (double *)malloc(sizeof(double) * n_alloc); perm = (int *)malloc(sizeof(int) * n_alloc); for (i = 0; i < 10; i++) { norm = spiro_iter(s, m, perm, v, nseg); #ifdef VERBOSE printf("%% norm = %g\n", norm); #endif if (norm < 1e-12) break; } free(m); free(v); free(perm); return 0; }
static int solve_spiro(spiro_seg *s, int nseg) { int i, converged, nmat, n_alloc; bandmat *m; double *v; int *perm; double norm; nmat = count_vec(s, nseg); n_alloc = nmat; if (nmat == 0) return 1; /* just means no convergence problems */ if (s[0].ty != '{' && s[0].ty != 'v') n_alloc *= 3; if (n_alloc < 5) n_alloc = 5; m = (bandmat *)malloc(sizeof(bandmat) * n_alloc); v = (double *)malloc(sizeof(double) * n_alloc); perm = (int *)malloc(sizeof(int) * n_alloc); i = converged = 0; /* not solved (yet) */ if ( m!=NULL && v!=NULL && perm!=NULL ) { while (i++ < 60) { norm = spiro_iter(s, m, perm, v, nseg, nmat); #ifdef VERBOSE printf("iteration #%d, %% norm = %g\n", i, norm); #endif if (check_finiteness(s, nseg)) break; if (norm < 1e-12) { converged = 1; break; } } #ifdef VERBOSE if (converged==0) fprintf(stderr, "ERROR: LibSpiro: failed to converge after %d attempts to converge.\n", i); } else { fprintf(stderr, "ERROR: LibSpiro: failed to alloc memory.\n"); #endif } free(m); free(v); free(perm); return converged; }