// performs a check, whether two given blocks are similar double get_block_distance (block_t* ref_block, block_t* cmp_block, int const sigma, double const th_2d) { int const bs = ref_block->block_size; double ref_mat[bs][bs]; double cmp_mat[bs][bs]; double sub_mat[bs][bs]; double distance = 0.0; // subtract 128 for DCT transformation shift_values (bs, ref_block, ref_mat); shift_values (bs, cmp_block, cmp_mat); // perform DCT on reference block by two matrix multiplications dct_2d (bs, ref_mat); // perform DCT on compare block by two matrix multiplications dct_2d (bs, cmp_mat); // perform thresholding on reference block hard_threshold_2d (bs, ref_mat, th_2d, sigma); // perform thresholding on compare block hard_threshold_2d (bs, cmp_mat, th_2d, sigma); // subtract compare block from reference block subtract_blocks (bs, ref_mat, cmp_mat, sub_mat); // perform L2 norm on the difference of the two matrices distance = l2_norm(bs, sub_mat) / (double)bs; return distance; }
void Utility::l2_normalize(std::vector<float>& v){ float norm = l2_norm(v); if(norm == 0.f){ std::cerr << "0 norm" << std::endl; exit(1); } std::for_each(v.begin(), v.end(), [&](float& v){v /= norm;}); }
bool CVertexRingElement::_updateRotationMatrixSolidOrShell(const int isshell, const Vector3d* vertexpos) { const double ERRTOL = 1e-4; double3x3 A; int rank, dim=3; m_quat0 = m_quat; //save last quat, also use it as the initial guess m_R0 = m_R; //asemble the matrix const int strideP = sizeof(CVertexRingNode); const int strideQ = sizeof(Vector3d); const int strideW = sizeof(CVertexRingNode); const double *P = &m_pVertexRingNode[0].m_lpos0.x; const double *Q = &vertexpos[0].x; const double *Weight = &m_pVertexRingNode[0].m_weight; _computeLeastSquareMatrix(m_nv, P, strideP, Q, strideQ, Weight, strideW, A); //test the sign of delta const double delta = A.Det(); if (delta>=0){ //polar_decomp(dim, ERRTOL, A.x, m_R.x, rank); //return true; double Sxx=A.x[0], Sxy=A.x[1], Sxz=A.x[2]; double Syx=A.x[3], Syy=A.x[4], Syz=A.x[5]; double Szx=A.x[6], Szy=A.x[7], Szz=A.x[8]; Matrix2d m; m.x[0][0]=Sxx + Syy + Szz, m.x[0][1]=Syz - Szy, m.x[0][2]=Szx - Sxz, m.x[0][3]=Sxy - Syx; m.x[1][0]=m.x[0][1], m.x[1][1]=Sxx - Syy - Szz, m.x[1][2]=Sxy + Syx, m.x[1][3]=Szx + Sxz; m.x[2][0]=m.x[0][2], m.x[2][1]=m.x[1][2]; m.x[2][2]= - Sxx + Syy - Szz, m.x[2][3]= Syz + Szy; m.x[3][0]=m.x[0][3], m.x[3][1]=m.x[1][3], m.x[3][2]=m.x[2][3], m.x[3][3]= - Sxx - Syy + Szz; const double ERRTOL = 1e-8; double eigenvalue; Vector4d quat, guess(m_quat.w, m_quat.x, m_quat.y, m_quat.z); const bool r = RayleighQuotientIteration(m, guess, ERRTOL, quat, eigenvalue); m_quat.x=quat[1], m_quat.y=quat[2], m_quat.z=quat[3], m_quat.w=quat[0]; Quaternion *pquat = (Quaternion*)&m_quat.x; typedef double M33[3][3]; pquat->getRotationMatrix(*((M33*)&m_R.x)); return r; } //if solid, use lazzy approach to do nothing, since we don't know the axis to flip if (!isshell){ return false; } //compute relfection of the shell vertices using vertex normal bool r = true; const double MATRIXDIST = 1.0; Vector3d norm = m_normal; _computeReflectedLeastSquareMatrix(m_nv, P, strideP, Q, strideQ, Weight, strideW, norm, A); polar_decomp(dim, ERRTOL, A.x, m_R.x, rank); const double l2dist = l2_norm(m_R, m_R0); if (l2dist>=MATRIXDIST) r=false; return r; }
void recognize_glyph_one (contours gl, int& level, string& best, double& best_rec) { array<tree> disc1; array<double> cont1; invariants (gl, 1, disc1, cont1); array<tree> disc2; array<double> cont2; invariants (gl, 2, disc2, cont2); best= ""; best_rec= -100.0; for (int i=0; i<N(learned_names); i++) if (N(learned_glyphs[i]) == N(gl) && disc1 == learned_disc1[i]) { string name= learned_names[i]; array<double> cont= learned_cont1[i]; double dist= l2_norm (cont - cont1) / sqrt (N(cont1)); double rec = 1.0 - dist; if (rec > best_rec) { best_rec= rec; best= name; } //cout << name << ": " << 100.0 * rec << "%\n"; } if (best != "") { //cout << "disc= " << disc1 << "\n"; //cout << "cont= " << cont1 << "\n"; level= 1; return; } for (int i=0; i<N(learned_names); i++) if (N(learned_glyphs[i]) == N(gl) && disc2 == learned_disc2[i]) { string name= learned_names[i]; array<double> cont= learned_cont2[i]; double dist= l2_norm (cont - cont2) / sqrt (N(cont2)); double rec = 1.0 - dist; if (rec > best_rec) { best_rec= rec; best= name; } //cout << name << ": " << 100.0 * rec << "%\n"; } level= 2; }
int main(int argc, char **args) { int res = ERR_SUCCESS; #ifdef WITH_PETSC PetscInitialize(&argc, &args, (char *) PETSC_NULL, PETSC_NULL); #endif set_verbose(false); if (argc < 2) error("Not enough parameters"); H1ShapesetLobattoHex shapeset; printf("* Loading mesh '%s'\n", args[1]); Mesh mesh; Mesh3DReader mesh_loader; if (!mesh_loader.load(args[1], &mesh)) error("Loading mesh file '%s'\n", args[1]); printf("* Setup space #1\n"); H1Space space1(&mesh, &shapeset); space1.set_bc_types(bc_types); order3_t o1(2, 2, 2); printf(" - Setting uniform order to (%d, %d, %d)\n", o1.x, o1.y, o1.z); space1.set_uniform_order(o1); printf("* Setup space #2\n"); H1Space space2(&mesh, &shapeset); space2.set_bc_types(bc_types); order3_t o2(4, 4, 4); printf(" - Setting uniform order to (%d, %d, %d)\n", o2.x, o2.y, o2.z); space2.set_uniform_order(o2); int ndofs = 0; ndofs += space1.assign_dofs(); ndofs += space2.assign_dofs(ndofs); printf(" - Number of DOFs: %d\n", ndofs); printf("* Calculating a solution\n"); #if defined WITH_UMFPACK UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver solver(&mat, &rhs); #elif defined WITH_PARDISO PardisoMatrix mat; PardisoVector rhs; PardisoLinearSolver solver(&mat, &rhs); #elif defined WITH_PETSC PetscMatrix mat; PetscVector rhs; PetscLinearSolver solver(&mat, &rhs); #elif defined WITH_MUMPS MumpsMatrix mat; MumpsVector rhs; MumpsSolver solver(&mat, &rhs); #endif WeakForm wf(2); wf.add_matrix_form(0, 0, bilinear_form_1<double, scalar>, bilinear_form_1<ord_t, ord_t>, SYM); wf.add_vector_form(0, linear_form_1<double, scalar>, linear_form_1<ord_t, ord_t>); wf.add_matrix_form(1, 1, bilinear_form_2<double, scalar>, bilinear_form_2<ord_t, ord_t>, SYM); wf.add_vector_form(1, linear_form_2<double, scalar>, linear_form_2<ord_t, ord_t>); LinearProblem lp(&wf, Tuple<Space *>(&space1, &space2)); // assemble stiffness matrix Timer assemble_timer("Assembling stiffness matrix"); assemble_timer.start(); lp.assemble(&mat, &rhs); assemble_timer.stop(); // solve the stiffness matrix Timer solve_timer("Solving stiffness matrix"); solve_timer.start(); bool solved = solver.solve(); solve_timer.stop(); // output the measured values printf("%s: %s (%lf secs)\n", assemble_timer.get_name(), assemble_timer.get_human_time(), assemble_timer.get_seconds()); printf("%s: %s (%lf secs)\n", solve_timer.get_name(), solve_timer.get_human_time(), solve_timer.get_seconds()); if (solved) { // solution 1 Solution sln1(&mesh); sln1.set_coeff_vector(&space1, solver.get_solution()); ExactSolution esln1(&mesh, exact_sln_fn_1); // norm double h1_sln_norm1 = h1_norm(&sln1); double h1_err_norm1 = h1_error(&sln1, &esln1); printf(" - H1 solution norm: % le\n", h1_sln_norm1); printf(" - H1 error norm: % le\n", h1_err_norm1); double l2_sln_norm1 = l2_norm(&sln1); double l2_err_norm1 = l2_error(&sln1, &esln1); printf(" - L2 solution norm: % le\n", l2_sln_norm1); printf(" - L2 error norm: % le\n", l2_err_norm1); if (h1_err_norm1 > EPS || l2_err_norm1 > EPS) { // calculated solution is not enough precise res = ERR_FAILURE; } // solution 2 Solution sln2(&mesh); sln2.set_coeff_vector(&space2, solver.get_solution()); ExactSolution esln2(&mesh, exact_sln_fn_2); // norm double h1_sln_norm2 = h1_norm(&sln2); double h1_err_norm2 = h1_error(&sln2, &esln2); printf(" - H1 solution norm: % le\n", h1_sln_norm2); printf(" - H1 error norm: % le\n", h1_err_norm2); double l2_sln_norm2 = l2_norm(&sln2); double l2_err_norm2 = l2_error(&sln2, &esln2); printf(" - L2 solution norm: % le\n", l2_sln_norm2); printf(" - L2 error norm: % le\n", l2_err_norm2); if (h1_err_norm2 > EPS || l2_err_norm2 > EPS) { // calculated solution is not enough precise res = ERR_FAILURE; } #ifdef OUTPUT_DIR // output const char *of_name = OUTPUT_DIR "/solution.pos"; FILE *ofile = fopen(of_name, "w"); if (ofile != NULL) { GmshOutputEngine output(ofile); output.out(&sln1, "Uh_1"); output.out(&esln1, "U1"); output.out(&sln2, "Uh_2"); output.out(&esln2, "U2"); fclose(ofile); } else { warning("Can not open '%s' for writing.", of_name); } #endif } #ifdef WITH_PETSC mat.free(); rhs.free(); PetscFinalize(); #endif TRACE_END; return res; }
int main(int argc, char **args) { int res = ERR_SUCCESS; #ifdef WITH_PETSC PetscInitialize(&argc, &args, (char *) PETSC_NULL, PETSC_NULL); #endif set_verbose(false); TRACE_START("trace.txt"); DEBUG_OUTPUT_ON; SET_VERBOSE_LEVEL(0); if (argc < 5) error("Not enough parameters"); sscanf(args[2], "%d", &m); sscanf(args[3], "%d", &n); sscanf(args[4], "%d", &o); printf("* Loading mesh '%s'\n", args[1]); Mesh mesh; Mesh3DReader mloader; if (!mloader.load(args[1], &mesh)) error("Loading mesh file '%s'\n", args[1]); H1ShapesetLobattoHex shapeset; printf("* Setting the space up\n"); H1Space space(&mesh, &shapeset); space.set_bc_types(bc_types); int mx = maxn(4, m, n, o, 4); order3_t order(mx, mx, mx); // order3_t order(1, 1, 1); // order3_t order(m, n, o); printf(" - Setting uniform order to (%d, %d, %d)\n", mx, mx, mx); space.set_uniform_order(order); int ndofs = space.assign_dofs(); printf(" - Number of DOFs: %d\n", ndofs); printf("* Calculating a solution\n"); #if defined WITH_UMFPACK UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver solver(&mat, &rhs); #elif defined WITH_PARDISO PardisoMatrix mat; PardisoVector rhs; PardisoLinearSolver solver(&mat, &rhs); #elif defined WITH_PETSC PetscMatrix mat; PetscVector rhs; PetscLinearSolver solver(&mat, &rhs); #elif defined WITH_MUMPS MumpsMatrix mat; MumpsVector rhs; MumpsSolver solver(&mat, &rhs); #endif WeakForm wf; wf.add_matrix_form(bilinear_form<double, scalar>, bilinear_form<ord_t, ord_t>, SYM); wf.add_vector_form(linear_form<double, scalar>, linear_form<ord_t, ord_t>); wf.add_vector_form_surf(linear_form_surf<double, scalar>, linear_form_surf<ord_t, ord_t>); LinearProblem lp(&wf, &space); // assemble stiffness matrix printf(" - assembling...\n"); fflush(stdout); Timer assemble_timer; assemble_timer.start(); lp.assemble(&mat, &rhs); assemble_timer.stop(); printf("%s (%lf secs)\n", assemble_timer.get_human_time(), assemble_timer.get_seconds()); // solve the stiffness matrix printf(" - solving... "); fflush(stdout); Timer solve_timer; solve_timer.start(); bool solved = solver.solve(); solve_timer.stop(); printf("%s (%lf secs)\n", solve_timer.get_human_time(), solve_timer.get_seconds()); // mat.dump(stdout, "a"); // rhs.dump(stdout, "b"); if (solved) { Solution sln(&mesh); sln.set_coeff_vector(&space, solver.get_solution()); // printf("* Solution:\n"); // double *s = solver.get_solution(); // for (int i = 1; i <= ndofs; i++) { // printf(" x[% 3d] = % lf\n", i, s[i]); // } ExactSolution ex_sln(&mesh, exact_solution); // norm double h1_sln_norm = h1_norm(&sln); double h1_err_norm = h1_error(&sln, &ex_sln); printf(" - H1 solution norm: % le\n", h1_sln_norm); printf(" - H1 error norm: % le\n", h1_err_norm); double l2_sln_norm = l2_norm(&sln); double l2_err_norm = l2_error(&sln, &ex_sln); printf(" - L2 solution norm: % le\n", l2_sln_norm); printf(" - L2 error norm: % le\n", l2_err_norm); if (h1_err_norm > EPS || l2_err_norm > EPS) { // calculated solution is not enough precise res = ERR_FAILURE; } #if 0 //def OUTPUT_DIR printf("* Output\n"); // output const char *of_name = OUTPUT_DIR "/solution.pos"; FILE *ofile = fopen(of_name, "w"); if (ofile != NULL) { ExactSolution ex_sln(&mesh, exact_solution); DiffFilter eh(&sln, &ex_sln); // DiffFilter eh_dx(&mesh, &sln, &ex_sln, FN_DX, FN_DX); // DiffFilter eh_dy(&mesh, &sln, &ex_sln, FN_DY, FN_DY); // DiffFilter eh_dz(&mesh, &sln, &ex_sln, FN_DZ, FN_DZ); GmshOutputEngine output(ofile); output.out(&sln, "Uh"); // output.out(&sln, "Uh dx", FN_DX_0); // output.out(&sln, "Uh dy", FN_DY_0); // output.out(&sln, "Uh dz", FN_DZ_0); output.out(&eh, "Eh"); // output.out(&eh_dx, "Eh dx"); // output.out(&eh_dy, "Eh dy"); // output.out(&eh_dz, "Eh dz"); output.out(&ex_sln, "U"); // output.out(&ex_sln, "U dx", FN_DX_0); // output.out(&ex_sln, "U dy", FN_DY_0); // output.out(&ex_sln, "U dz", FN_DZ_0); fclose(ofile); } else { warning("Can not open '%s' for writing.", of_name); } #endif } else res = ERR_FAILURE; #ifdef WITH_PETSC mat.free(); rhs.free(); PetscFinalize(); #endif TRACE_END; return res; }
int main(int argc, char **args) { int res = ERR_SUCCESS; #ifdef WITH_PETSC PetscInitialize(&argc, &args, (char *) PETSC_NULL, PETSC_NULL); #endif if (argc < 2) error("Not enough parameters."); printf("* Loading mesh '%s'\n", args[1]); Mesh mesh1; H3DReader mesh_loader; if (!mesh_loader.load(args[1], &mesh1)) error("Loading mesh file '%s'\n", args[1]); #if defined RHS2 Ord3 order(P_INIT_X, P_INIT_Y, P_INIT_Z); printf(" - Setting uniform order to (%d, %d, %d)\n", order.x, order.y, order.z); // Create an H1 space with default shapeset. printf("* Setting the space up\n"); H1Space space(&mesh1, bc_types, essential_bc_values, order); int ndofs = space.assign_dofs(); printf(" - Number of DOFs: %d\n", ndofs); printf("* Calculating a solution\n"); // duplicate the mesh Mesh mesh2; mesh2.copy(mesh1); // do some changes mesh2.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ); mesh2.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ); Solution fsln(&mesh2); fsln.set_const(-6.0); #else // duplicate the mesh Mesh mesh2; mesh2.copy(mesh1); Mesh mesh3; mesh3.copy(mesh1); // change meshes mesh1.refine_all_elements(H3D_REFT_HEX_X); mesh2.refine_all_elements(H3D_REFT_HEX_Y); mesh3.refine_all_elements(H3D_REFT_HEX_Z); printf("* Setup spaces\n"); Ord3 o1(2, 2, 2); printf(" - Setting uniform order to (%d, %d, %d)\n", o1.x, o1.y, o1.z); H1Space space1(&mesh1, bc_types_1, essential_bc_values_1, o1); Ord3 o2(2, 2, 2); printf(" - Setting uniform order to (%d, %d, %d)\n", o2.x, o2.y, o2.z); H1Space space2(&mesh2, bc_types_2, essential_bc_values_2, o2); Ord3 o3(1, 1, 1); printf(" - Setting uniform order to (%d, %d, %d)\n", o3.x, o3.y, o3.z); H1Space space3(&mesh3, bc_types_3, essential_bc_values_3, o3); int ndofs = 0; ndofs += space1.assign_dofs(); ndofs += space2.assign_dofs(ndofs); ndofs += space3.assign_dofs(ndofs); printf(" - Number of DOFs: %d\n", ndofs); #endif #if defined WITH_UMFPACK MatrixSolverType matrix_solver = SOLVER_UMFPACK; #elif defined WITH_PETSC MatrixSolverType matrix_solver = SOLVER_PETSC; #elif defined WITH_MUMPS MatrixSolverType matrix_solver = SOLVER_MUMPS; #endif #ifdef RHS2 WeakForm wf; wf.add_matrix_form(bilinear_form<double, scalar>, bilinear_form<Ord, Ord>, HERMES_SYM); wf.add_vector_form(linear_form<double, scalar>, linear_form<Ord, Ord>, HERMES_ANY_INT, &fsln); // Initialize discrete problem. bool is_linear = true; DiscreteProblem dp(&wf, &space, is_linear); #elif defined SYS3 WeakForm wf(3); wf.add_matrix_form(0, 0, biform_1_1<double, scalar>, biform_1_1<Ord, Ord>, HERMES_SYM); wf.add_matrix_form(0, 1, biform_1_2<double, scalar>, biform_1_2<Ord, Ord>, HERMES_NONSYM); wf.add_vector_form(0, liform_1<double, scalar>, liform_1<Ord, Ord>); wf.add_matrix_form(1, 1, biform_2_2<double, scalar>, biform_2_2<Ord, Ord>, HERMES_SYM); wf.add_matrix_form(1, 2, biform_2_3<double, scalar>, biform_2_3<Ord, Ord>, HERMES_NONSYM); wf.add_vector_form(1, liform_2<double, scalar>, liform_2<Ord, Ord>); wf.add_matrix_form(2, 2, biform_3_3<double, scalar>, biform_3_3<Ord, Ord>, HERMES_SYM); // Initialize discrete problem. bool is_linear = true; DiscreteProblem dp(&wf, Hermes::vector<Space *>(&space1, &space2, &space3), is_linear); #endif // Time measurement. TimePeriod cpu_time; cpu_time.tick(); // Set up the solver, matrix, and rhs according to the solver selection. SparseMatrix* matrix = create_matrix(matrix_solver); Vector* rhs = create_vector(matrix_solver); Solver* solver = create_linear_solver(matrix_solver, matrix, rhs); // Initialize the preconditioner in the case of SOLVER_AZTECOO. if (matrix_solver == SOLVER_AZTECOO) { ((AztecOOSolver*) solver)->set_solver(iterative_method); ((AztecOOSolver*) solver)->set_precond(preconditioner); // Using default iteration parameters (see solver/aztecoo.h). } // Assemble stiffness matrix and load vector. dp.assemble(matrix, rhs); // Solve the linear system. If successful, obtain the solution. info("Solving the linear problem."); bool solved = solver->solve(); // Time measurement. cpu_time.tick(); // Print timing information. info("Solution and mesh with polynomial orders saved. Total running time: %g s", cpu_time.accumulated()); // Time measurement. TimePeriod sln_time; sln_time.tick(); if (solved) { #ifdef RHS2 // Solve the linear system. If successful, obtain the solution. info("Solving the linear problem."); Solution sln(&mesh1); Solution::vector_to_solution(solver->get_solution(), &space, &sln); // Set exact solution. ExactSolution ex_sln(&mesh1, exact_solution); // Norm. double h1_sln_norm = h1_norm(&sln); double h1_err_norm = h1_error(&sln, &ex_sln); printf(" - H1 solution norm: % le\n", h1_sln_norm); printf(" - H1 error norm: % le\n", h1_err_norm); double l2_sln_norm = l2_norm(&sln); double l2_err_norm = l2_error(&sln, &ex_sln); printf(" - L2 solution norm: % le\n", l2_sln_norm); printf(" - L2 error norm: % le\n", l2_err_norm); if (h1_err_norm > EPS || l2_err_norm > EPS) { // Calculated solution is not enough precise. res = ERR_FAILURE; } #elif defined SYS3 // Solution 1. Solution sln1(&mesh1); Solution sln2(&mesh2); Solution sln3(&mesh3); Solution::vector_to_solution(solver->get_solution(), &space1, &sln1); Solution::vector_to_solution(solver->get_solution(), &space2, &sln2); Solution::vector_to_solution(solver->get_solution(), &space3, &sln3); ExactSolution esln1(&mesh1, exact_sln_fn_1); ExactSolution esln2(&mesh2, exact_sln_fn_2); ExactSolution esln3(&mesh3, exact_sln_fn_3); // Norm. double h1_err_norm1 = h1_error(&sln1, &esln1); double h1_err_norm2 = h1_error(&sln2, &esln2); double h1_err_norm3 = h1_error(&sln3, &esln3); double l2_err_norm1 = l2_error(&sln1, &esln1); double l2_err_norm2 = l2_error(&sln2, &esln2); double l2_err_norm3 = l2_error(&sln3, &esln3); printf(" - H1 error norm: % le\n", h1_err_norm1); printf(" - L2 error norm: % le\n", l2_err_norm1); if (h1_err_norm1 > EPS || l2_err_norm1 > EPS) { // Calculated solution is not enough precise. res = ERR_FAILURE; } printf(" - H1 error norm: % le\n", h1_err_norm2); printf(" - L2 error norm: % le\n", l2_err_norm2); if (h1_err_norm2 > EPS || l2_err_norm2 > EPS) { // Calculated solution is not enough precise. res = ERR_FAILURE; } printf(" - H1 error norm: % le\n", h1_err_norm3); printf(" - L2 error norm: % le\n", l2_err_norm3); if (h1_err_norm3 > EPS || l2_err_norm3 > EPS) { // Calculated solution is not enough precise. res = ERR_FAILURE; } #endif #ifdef RHS2 out_fn_vtk(&sln, "solution"); #elif defined SYS3 out_fn_vtk(&sln1, "sln1"); out_fn_vtk(&sln2, "sln2"); out_fn_vtk(&sln3, "sln3"); #endif } else res = ERR_FAILURE; // Print timing information. info("Solution and mesh with polynomial orders saved. Total running time: %g s", sln_time.accumulated()); // Clean up. delete matrix; delete rhs; delete solver; return res; }
int main(int argc, char **argv) { int res = ERR_SUCCESS; #ifdef WITH_PETSC PetscInitialize(&argc, &argv, (char *) PETSC_NULL, PETSC_NULL); #endif set_verbose(false); if (argc < 5) error("Not enough parameters."); H1ShapesetLobattoHex shapeset; printf("* Loading mesh '%s'\n", argv[1]); Mesh mesh; Mesh3DReader mloader; if (!mloader.load(argv[1], &mesh)) error("Loading mesh file '%s'\n", argv[1]); printf("* Setting the space up\n"); H1Space space(&mesh, &shapeset); space.set_bc_types(bc_types); space.set_essential_bc_values(essential_bc_values); int o[3] = { 0, 0, 0 }; sscanf(argv[2], "%d", o + 0); sscanf(argv[3], "%d", o + 1); sscanf(argv[4], "%d", o + 2); order3_t order(o[0], o[1], o[2]); printf(" - Setting uniform order to (%d, %d, %d)\n", order.x, order.y, order.z); space.set_uniform_order(order); WeakForm wf; wf.add_matrix_form(bilinear_form<double, scalar>, bilinear_form<ord_t, ord_t>, SYM, ANY); wf.add_vector_form(linear_form<double, scalar>, linear_form<ord_t, ord_t>, ANY); LinearProblem lp(&wf); lp.set_space(&space); bool done = false; int iter = 0; do { Timer assemble_timer("Assembling stiffness matrix"); Timer solve_timer("Solving stiffness matrix"); printf("\n=== Iter #%d ================================================================\n", iter); printf("\nSolution\n"); #if defined WITH_UMFPACK UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver solver(&mat, &rhs); #elif defined WITH_PARDISO PardisoMatrix mat; PardisoVector rhs; PardisoLinearSolver solver(&mat, &rhs); #elif defined WITH_PETSC PetscMatrix mat; PetscVector rhs; PetscLinearSolver solver(&mat, &rhs); #elif defined WITH_MUMPS MumpsMatrix mat; MumpsVector rhs; MumpsSolver solver(&mat, &rhs); #endif int ndofs = space.assign_dofs(); printf(" - Number of DOFs: %d\n", ndofs); // assemble stiffness matrix printf(" - Assembling... "); fflush(stdout); assemble_timer.reset(); assemble_timer.start(); lp.assemble(&mat, &rhs); assemble_timer.stop(); printf("done in %s (%lf secs)\n", assemble_timer.get_human_time(), assemble_timer.get_seconds()); // solve the stiffness matrix printf(" - Solving... "); fflush(stdout); solve_timer.reset(); solve_timer.start(); bool solved = solver.solve(); solve_timer.stop(); if (solved) printf("done in %s (%lf secs)\n", solve_timer.get_human_time(), solve_timer.get_seconds()); else { res = ERR_FAILURE; printf("failed\n"); break; } printf("Reference solution\n"); #if defined WITH_UMFPACK UMFPackLinearSolver rsolver(&mat, &rhs); #elif defined WITH_PARDISO PardisoLinearSolver rsolver(&mat, &rhs); #elif defined WITH_PETSC PetscLinearSolver rsolver(&mat, &rhs); #elif defined WITH_MUMPS MumpsSolver rsolver(&mat, &rhs); #endif Mesh rmesh; rmesh.copy(mesh); rmesh.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ); Space *rspace = space.dup(&rmesh); rspace->copy_orders(space, 1); LinearProblem rlp(&wf); rlp.set_space(rspace); int rndofs = rspace->assign_dofs(); printf(" - Number of DOFs: %d\n", rndofs); printf(" - Assembling... "); fflush(stdout); assemble_timer.reset(); assemble_timer.start(); rlp.assemble(&mat, &rhs); assemble_timer.stop(); printf("done in %s (%lf secs)\n", assemble_timer.get_human_time(), assemble_timer.get_seconds()); printf(" - Solving... "); fflush(stdout); solve_timer.reset(); solve_timer.start(); bool rsolved = rsolver.solve(); solve_timer.stop(); if (rsolved) printf("done in %s (%lf secs)\n", solve_timer.get_human_time(), solve_timer.get_seconds()); else { res = ERR_FAILURE; printf("failed\n"); break; } Solution sln(&mesh); sln.set_coeff_vector(&space, solver.get_solution()); Solution rsln(&rmesh); rsln.set_coeff_vector(rspace, rsolver.get_solution()); printf("Adaptivity:\n"); H1Adapt hp(&space); double tol = hp.calc_error(&sln, &rsln) * 100; printf(" - tolerance: "); fflush(stdout); printf("% lf\n", tol); if (tol < TOLERANCE) { printf("\nDone\n"); ExactSolution ex_sln(&mesh, exact_solution); // norm double h1_sln_norm = h1_norm(&sln); double h1_err_norm = h1_error(&sln, &ex_sln); printf(" - H1 solution norm: % le\n", h1_sln_norm); printf(" - H1 error norm: % le\n", h1_err_norm); double l2_sln_norm = l2_norm(&sln); double l2_err_norm = l2_error(&sln, &ex_sln); printf(" - L2 solution norm: % le\n", l2_sln_norm); printf(" - L2 error norm: % le\n", l2_err_norm); if (h1_err_norm > EPS || l2_err_norm > EPS) { // calculated solution is not enough precise res = ERR_FAILURE; } break; } Timer t(""); printf(" - adapting... "); fflush(stdout); t.start(); hp.adapt(THRESHOLD); t.stop(); printf("done in %lf secs (refined %d element(s))\n", t.get_seconds(), hp.get_num_refined_elements()); iter++; } while (!done); #ifdef WITH_PETSC PetscFinalize(); #endif return res; }
int main(int argc, char **args) { int res = ERR_SUCCESS; #ifdef WITH_PETSC PetscInitialize(NULL, NULL, (char *) PETSC_NULL, PETSC_NULL); #endif set_verbose(false); TRACE_START("trace.txt"); DEBUG_OUTPUT_ON; SET_VERBOSE_LEVEL(0); try { for (int i = 0; i < 48; i++) { for (int j = 0; j < 48; j++) { // int i = 5; { // int j = 0; { printf("Config: %d, %d ", i, j); Mesh mesh; for (Word_t k = 0; k < countof(vtcs); k++) mesh.add_vertex(vtcs[k].x, vtcs[k].y, vtcs[k].z); Word_t h1[] = { hexs[0][i][0] + 1, hexs[0][i][1] + 1, hexs[0][i][2] + 1, hexs[0][i][3] + 1, hexs[0][i][4] + 1, hexs[0][i][5] + 1, hexs[0][i][6] + 1, hexs[0][i][7] + 1 }; mesh.add_hex(h1); Word_t h2[] = { hexs[1][j][0] + 1, hexs[1][j][1] + 1, hexs[1][j][2] + 1, hexs[1][j][3] + 1, hexs[1][j][4] + 1, hexs[1][j][5] + 1, hexs[1][j][6] + 1, hexs[1][j][7] + 1 }; mesh.add_hex(h2); // bc for (Word_t k = 0; k < countof(bnd); k++) { Word_t facet_idxs[Quad::NUM_VERTICES] = { bnd[k][0] + 1, bnd[k][1] + 1, bnd[k][2] + 1, bnd[k][3] + 1 }; mesh.add_quad_boundary(facet_idxs, bnd[k][4]); } mesh.ugh(); // mesh.dump(); // Element *hx[] = { mesh.elements[1], mesh.elements[2] }; // printf("[%d, %d]\n", hx[0]->get_face_orientation(1), hx[1]->get_face_orientation(2)); // Word_t fidx[4]; // hx[1]->get_face_vertices(2, fidx); // printf("FI: %d, %d, %d, %d\n", fidx[0], fidx[1], fidx[2], fidx[3]); printf("\n"); #ifdef OUTPUT_DIR BEGIN_BLOCK // output the mesh const char *of_name = OUTPUT_DIR "/ref.msh"; FILE *ofile = fopen(of_name, "w"); if (ofile != NULL) { GmshOutputEngine output(ofile); output.out(&mesh); fclose(ofile); } else { warning("Can not open '%s' for writing.", of_name); } END_BLOCK #endif H1ShapesetLobattoHex shapeset; // printf("* Setting the space up\n"); H1Space space(&mesh, &shapeset); space.set_bc_types(bc_types); space.set_essential_bc_values(essential_bc_values); #ifdef XM_YN_ZO order3_t ord(4, 4, 4); #elif defined XM_YN_ZO_2 order3_t ord(4, 4, 4); #elif defined X2_Y2_Z2 order3_t ord(2, 2, 2); #endif // printf(" - Setting uniform order to (%d, %d, %d)\n", dir_x, dir_y, dir_z); space.set_uniform_order(ord); space.assign_dofs(); // printf("* Calculating a solution\n"); #if defined WITH_UMFPACK UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver solver(&mat, &rhs); #elif defined WITH_PARDISO PardisoLinearSolver solver; #elif defined WITH_PETSC PetscMatrix mat; PetscVector rhs; PetscLinearSolver solver(&mat, &rhs); #elif defined WITH_MUMPS MumpsMatrix mat; MumpsVector rhs; MumpsSolver solver(&mat, &rhs); #endif WeakForm wf; #ifdef DIRICHLET wf.add_matrix_form(bilinear_form<double, scalar>, bilinear_form<ord_t, ord_t>, SYM); wf.add_vector_form(linear_form<double, scalar>, linear_form<ord_t, ord_t>); #elif defined NEWTON wf.add_matrix_form(bilinear_form<double, scalar>, bilinear_form<ord_t, ord_t>, SYM); wf.add_matrix_form_surf(bilinear_form_surf<double, scalar>, bilinear_form_surf<ord_t, ord_t>); wf.add_vector_form(linear_form<double, scalar>, linear_form<ord_t, ord_t>); wf.add_vector_form_surf(linear_form_surf<double, scalar>, linear_form_surf<ord_t, ord_t>); #endif LinProblem lp(&wf); lp.set_space(&space); // assemble stiffness matrix lp.assemble(&mat, &rhs); // solve the stiffness matrix bool solved = solver.solve(); if (!solved) throw ERR_FAILURE; // { // char file_name[1024]; // sprintf(file_name, "%s/matrix-%d-%d", OUTPUT_DIR, i, j); // FILE *file = fopen(file_name, "w"); // if (file != NULL) { // solver.dump_matrix(file, "A"); // solver.dump_rhs(file, "b"); // // fclose(file); // } // } Solution sln(&mesh); sln.set_fe_solution(&space, solver.get_solution()); ExactSolution exsln(&mesh, exact_solution); // norm double h1_sln_norm = h1_norm(&sln); double h1_err_norm = h1_error(&sln, &exsln); printf(" - H1 solution norm: % le\n", h1_sln_norm); printf(" - H1 error norm: % le\n", h1_err_norm); double l2_sln_norm = l2_norm(&sln); double l2_err_norm = l2_error(&sln, &exsln); printf(" - L2 solution norm: % le\n", l2_sln_norm); printf(" - L2 error norm: % le\n", l2_err_norm); assert(h1_sln_norm > 0 && h1_err_norm > 0); assert(l2_sln_norm > 0 && l2_err_norm > 0); // // out fn // char fname[4096]; // sprintf(fname, "%s/cfg-%d-%d.pos", OUTPUT_DIR, i, j); // FILE *fnf = fopen(fname, "w"); // assert(fnf != NULL); // GmshOutputEngine out(fnf); // char var[64]; // sprintf(var, "%d_%d", i, j); // out.out(&sln, var); // fclose(fnf); // // char mfname[4096]; // sprintf(mfname, "%s/mesh-%d-%d.ref", OUTPUT_DIR, i, j); // FILE *mfnf = fopen(mfname, "w"); // assert(mfnf != NULL); // GmshOutputEngine outm(mfnf); // outm.out(&mesh); // fclose(mfnf); if (h1_err_norm > EPS || l2_err_norm > EPS) { // calculated solution is not enough precise printf("Solution is not precise enough.\n"); throw ERR_FAILURE; } printf("Passed\n"); } } } catch (int e) { res = e; printf("Failed\n"); } #ifdef WITH_PETSC PetscFinalize(); #endif TRACE_END; return res; }
void fem(size_t n, double errors[2], double (*fn_f)(double, double), double (*fn_g)(unsigned char, double, double), double (*fn_u)(double, double)) { mesh m; crs_matrix mat; double * u, * rhs; double local_stiffness[3][3]; double local_load[3]; size_t elem; /* 1. Allocate and generate mesh */ get_mesh(&m, n); #ifdef PRINT_DEBUG print_mesh(&m); #endif /* 2. Allocate the linear system */ init_matrix(&mat, &m); u = (double *) malloc(sizeof(double) * m.n_vertices); if (u == NULL) err_exit("Allocation of solution vector failed!"); memset(u, 0, sizeof(double) * m.n_vertices); rhs = (double *) malloc(sizeof(double) * m.n_vertices); if (rhs == NULL) err_exit("Allocation of right hand side failed!"); memset(rhs, 0, sizeof(double) * m.n_vertices); /* 3. Assemble the matrix */ for (elem = 0; elem < m.n_triangles; ++elem) { /* Compute local stiffness and load */ get_local_stiffness(local_stiffness, &m, elem); get_local_load(local_load, &m, elem, fn_f); #ifdef PRINT_DEBUG print_local_stiffness(local_stiffness); print_local_load(local_load); #endif /* insert into global matrix and rhs */ assemble_local2global_stiffness(local_stiffness, &mat, &m, elem); assemble_local2global_load(local_load, rhs, &m, elem); } #ifdef PRINT_DEBUG printf("Matrix after assembly:\n"); print_matrix(&mat); printf("rhs after assembly:\n"); for(size_t i = 0; i < m.n_vertices; ++i) { printf("%5.2f\n", rhs[i]); } printf("\n"); #endif /* 4. Apply boundary conditions */ apply_dbc(&mat, rhs, &m, fn_g); #ifdef PRINT_DEBUG printf("Matrix after application of BCs:\n"); print_matrix(&mat); printf("rhs after application of BCs:\n"); for(size_t i = 0; i < m.n_vertices; ++i) { printf("%5.2f\n", rhs[i]); } printf("\n"); #endif /* 5. Solve the linear system */ solve(&mat, u, rhs); /* 6. Evaluate error */ errors[0] = l2_norm(u, &m, fn_u); errors[1] = inf_norm(u, &m, fn_u); /* free allocated resources */ free(u); free(rhs); rhs = NULL; free_matrix(&mat); free_mesh(&m); }
int main(int argc, char **argv) { int res = ERR_SUCCESS; #ifdef WITH_PETSC PetscInitialize(&argc, &argv, (char *) PETSC_NULL, PETSC_NULL); #endif set_verbose(false); if (argc < 3) error("Not enough parameters"); printf("* Loading mesh '%s'\n", argv[1]); Mesh mesh; H3DReader mesh_loader; if (!mesh_loader.load(argv[1], &mesh)) error("Loading mesh file '%s'\n", argv[1]); int o; sscanf(argv[2], "%d", &o); printf(" - Setting uniform order to %d\n", o); printf("* Setting the space up\n"); H1Space space(&mesh, bc_types, essential_bc_values, o); int ndofs = space.assign_dofs(); printf(" - Number of DOFs: %d\n", ndofs); printf("* Calculating a solution\n"); #if defined WITH_UMFPACK UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver solver(&mat, &rhs); #elif defined WITH_PARDISO PardisoMatrix mat; PardisoVector rhs; PardisoLinearSolver solver(&mat, &rhs); #elif defined WITH_PETSC PetscMatrix mat; PetscVector rhs; PetscLinearSolver solver(&mat, &rhs); #elif defined WITH_MUMPS MumpsMatrix mat; MumpsVector rhs; MumpsSolver solver(&mat, &rhs); #endif WeakForm wf; wf.add_matrix_form(bilinear_form<double, scalar>, bilinear_form<Ord, Ord>, SYM); wf.add_vector_form(linear_form<double, scalar>, linear_form<Ord, Ord>); DiscreteProblem dp(&wf, &space, true); // assemble stiffness matrix Timer assemble_timer("Assembling stiffness matrix"); assemble_timer.start(); dp.assemble(&mat, &rhs); assemble_timer.stop(); // solve the stiffness matrix Timer solve_timer("Solving stiffness matrix"); solve_timer.start(); bool solved = solver.solve(); solve_timer.stop(); // output the measured values printf("%s: %s (%lf secs)\n", assemble_timer.get_name(), assemble_timer.get_human_time(), assemble_timer.get_seconds()); printf("%s: %s (%lf secs)\n", solve_timer.get_name(), solve_timer.get_human_time(), solve_timer.get_seconds()); if (solved) { Solution sln(&mesh); sln.set_coeff_vector(&space, solver.get_solution()); ExactSolution ex_sln(&mesh, exact_solution); // norm double h1_sln_norm = h1_norm(&sln); double h1_err_norm = h1_error(&sln, &ex_sln); printf(" - H1 solution norm: % le\n", h1_sln_norm); printf(" - H1 error norm: % le\n", h1_err_norm); double l2_sln_norm = l2_norm(&sln); double l2_err_norm = l2_error(&sln, &ex_sln); printf(" - L2 solution norm: % le\n", l2_sln_norm); printf(" - L2 error norm: % le\n", l2_err_norm); if (h1_err_norm > EPS || l2_err_norm > EPS) { // calculated solution is not enough precise res = ERR_FAILURE; } #if 0 //def OUTPUT_DIR // output const char *of_name = OUTPUT_DIR "/solution.pos"; FILE *ofile = fopen(of_name, "w"); if (ofile != NULL) { ExactSolution ex_sln(&mesh, exact_solution); DiffFilter eh(&sln, &ex_sln); // DiffFilter eh_dx(&mesh, &sln, &ex_sln, FN_DX, FN_DX); // DiffFilter eh_dy(&mesh, &sln, &ex_sln, FN_DY, FN_DY); // DiffFilter eh_dz(&mesh, &sln, &ex_sln, FN_DZ, FN_DZ); GmshOutputEngine output(ofile); output.out(&sln, "Uh"); // output.out(&sln, "Uh dx", FN_DX_0); // output.out(&sln, "Uh dy", FN_DY_0); // output.out(&sln, "Uh dz", FN_DZ_0); output.out(&eh, "Eh"); // output.out(&eh_dx, "Eh dx"); // output.out(&eh_dy, "Eh dy"); // output.out(&eh_dz, "Eh dz"); output.out(&ex_sln, "U"); // output.out(&ex_sln, "U dx", FN_DX_0); // output.out(&ex_sln, "U dy", FN_DY_0); // output.out(&ex_sln, "U dz", FN_DZ_0); fclose(ofile); } else { warning("Can not open '%s' for writing.", of_name); } #endif } #ifdef WITH_PETSC mat.free(); rhs.free(); PetscFinalize(); #endif TRACE_END; return res; }