bool Scene::solve_newton_step(const std::vector<FT>& b, std::vector<FT>& x) { if (m_timer_on) Timer::start_timer(m_timer, COLOR_BLUE, "LinearSolver"); unsigned nb = 0; std::map<unsigned, unsigned> indices; for (unsigned i = 0; i < m_vertices.size(); ++i) { Vertex_handle vi = m_vertices[i]; if (vi->is_hidden()) continue; indices[vi->get_index()] = nb++; } SparseMatrix L(nb, nb); build_laplacian(0.5, indices, L); bool ok = solve_linear_system(L, x, b); if (!ok) { std::cout << red << "linear solver failed" << white << std::endl; return false; } if (m_timer_on) Timer::stop_timer(m_timer, COLOR_BLUE); return true; }
/*--------------------------------------------------------------------*/ uint32 factor_mpqs(msieve_obj *obj, mp_t *n, factor_list_t *factor_list) { uint32 bits, fb_size; prime_list_t prime_list; fb_t *factor_base; uint32 *modsqrt_array; sieve_param_t params; relation_t *relation_list = NULL; uint32 num_relations = 0; la_col_t *cycle_list = NULL; uint32 num_cycles = 0; poly_t *poly_list = NULL; mp_t *poly_a_list = NULL; uint64 *bitfield = NULL; uint32 multiplier; uint32 factor_found = 0; /* Calculate the factor base bound */ bits = mp_bits(n); get_sieve_params(bits, ¶ms); if (params.fb_size < 100) params.fb_size = 100; logprintf(obj, "commencing quadratic sieve (%u-digit input)\n", strlen(mp_sprintf(n, 10, obj->mp_sprintf_buf))); /* Make a prime list, use it to build a factor base */ fill_prime_list(&prime_list, 2 * params.fb_size + 100, MAX_FB_PRIME); build_factor_base(n, &prime_list, &factor_base, &modsqrt_array, ¶ms.fb_size, &multiplier); fb_size = params.fb_size; logprintf(obj, "using multiplier of %u\n", multiplier); free(prime_list.list); /* Proceed with the algorithm */ do_sieving(obj, n, &poly_a_list, &poly_list, factor_base, modsqrt_array, ¶ms, multiplier, &relation_list, &num_relations, &cycle_list, &num_cycles); free(modsqrt_array); if (relation_list == NULL || cycle_list == NULL || cycle_list == NULL || poly_a_list == NULL || poly_list == NULL) { free(factor_base); return 0; } solve_linear_system(obj, fb_size, &bitfield, relation_list, cycle_list, &num_cycles); if (bitfield != NULL && num_cycles > 0) { factor_found = find_factors(obj, n, factor_base, fb_size, cycle_list, num_cycles, relation_list, bitfield, multiplier, poly_a_list, poly_list, factor_list); } free(factor_base); free(bitfield); free_cycle_list(cycle_list, num_cycles); qs_free_relation_list(relation_list, num_relations); free(poly_list); free(poly_a_list); return factor_found; }
int main() { // create mesh Mesh mesh(N_eq); mesh.create(A, B, N_elem); mesh.set_uniform_poly_order(P_init); // boundary conditions mesh.set_bc_left_natural(0); mesh.set_bc_right_natural(0); int N_dof = mesh.assign_dofs(); printf("N_dof = %d\n", N_dof); // register weak forms DiscreteProblem dp(&mesh); dp.add_matrix_form(0, 0, jacobian_vol); dp.add_vector_form(0, residual_vol); dp.add_matrix_form_surf(0, 0, jacobian_surf_left, BOUNDARY_LEFT); dp.add_vector_form_surf(0, residual_surf_left, BOUNDARY_LEFT); dp.add_matrix_form_surf(0, 0, jacobian_surf_right, BOUNDARY_RIGHT); dp.add_vector_form_surf(0, residual_surf_right, BOUNDARY_RIGHT); // allocate Jacobi matrix and residual Matrix *mat; double *y_prev = new double[N_dof]; double *res = new double[N_dof]; // zero initial condition for the Newton's method for(int i=0; i<N_dof; i++) y_prev[i] = 0; // Newton's loop int newton_iterations = 0; while (1) { // zero the matrix: mat = new DenseMatrix(N_dof); // construct residual vector dp.assemble_matrix_and_vector(mat, res, y_prev); if (DEBUG) { printf("RHS:"); for(int i=0; i<N_dof; i++) printf("%f ", res[i]); printf("\n"); } // calculate L2 norm of residual vector double res_norm = 0; for(int i=0; i<N_dof; i++) res_norm += res[i]*res[i]; res_norm = sqrt(res_norm); // if residual norm less than TOL, quit // latest solution is in y_prev printf("Residual L2 norm: %.15f\n", res_norm); if (DEBUG) printf("TOL: %.15f\n", TOL); if(res_norm < TOL) break; // changing sign of vector res for(int i=0; i<N_dof; i++) res[i]*= -1; //mat->print(); // solving the matrix system solve_linear_system(mat, res); // DEBUG: print solution if(DEBUG) { printf("New Y:\n"); for(int i=0; i<N_dof; i++) { printf("%g ", res[i]); } printf("\n"); } // updating y_prev by new solution which is in res for(int i=0; i<N_dof; i++) y_prev[i] += res[i]; newton_iterations++; printf("Finished Newton iteration: %d\n", newton_iterations); } printf("Total number of Newton iterations: %d\n", newton_iterations); Linearizer l(&mesh); const char *out_filename = "solution.gp"; l.plot_solution(out_filename, y_prev); printf("Done.\n"); return 1; }
void write_cells ( int ncfn, char output_file[ncfn], int nVar, int nSpt, int *SptType, int **Spt, double *lft, int CellSize, int nMCells, CellStack *MCells ) { FILE *fp; int i,k,m,kk,d,dd; double A[nVar][nVar]; double rhs[nVar]; int *labels = (int*)calloc(CellSize,sizeof(int)); if((fp=fopen(output_file,"w")) == NULL) { printf("Opening the file %s for writing failed.\n",output_file); exit(1); } fprintf(fp,"%d\n",nVar); fprintf(fp,"%d\n",nSpt); for(i=0; i<nSpt; i++) fprintf(fp,"%d ",SptType[i]); fprintf(fp,"\n"); fprintf(fp,"%d\n",nMCells); for(kk=0; kk<nMCells; kk++) { labels = Cs_Cur(MCells); d = 0; dd = -1; for(i=0; i<nSpt; i++) { for(m=0; m<SptType[i]; m++ ) { dd = dd+1; for(k=0; k<nVar; k++) A[dd][k] = Spt[labels[d+m+1]][k] - Spt[labels[d+m]][k]; rhs[dd] = lft[labels[d+m]] - lft[labels[d+m+1]]; } d = d + SptType[i] + 1; } solve_linear_system(nVar,A,rhs); for(k=0; k<nVar; k++) fprintf(fp,"%2.14E\n",rhs[k]); fprintf(fp,"%2.14E\n",1.000); d = -1; for(i=0; i<nSpt; i++) { fprintf(fp,"%d\n",SptType[i]+1); for(m=0; m<SptType[i]+1; m++) { d++; for(k=0; k<nVar; k++) fprintf(fp,"%d ",Spt[labels[d]][k]); fprintf(fp," %2.14E\n",lft[labels[d]]); } } fprintf(fp,"%d\n",0); if(kk != nMCells-1) Cs_Pop(MCells); } fclose(fp); free(labels); }