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, NULL, 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(FORM_CB(bilinear_form), SYM); wf.add_vector_form(FORM_CB(linear_form)); 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()); // mat.dump(stdout, "a"); // rhs.dump(stdout, "b"); 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; // } #ifdef AOUTPUT_DIR // output const char *of_name = OUTPUT_DIR "/solution.pos"; FILE *ofile = fopen(of_name, "w"); if (ofile != NULL) { DiffFilter eh(&sln, &ex_sln); // DiffFilter eh_dx(&sln, &ex_sln, FN_DX, FN_DX); // DiffFilter eh_dy(&sln, &ex_sln, FN_DY, FN_DY); // DiffFilter eh_dz(&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 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 < 3) error("Not enough parameters"); HcurlShapesetLobattoHex shapeset; printf("* Loading mesh '%s'\n", argv[1]); Mesh mesh; Mesh3DReader mesh_loader; if (!mesh_loader.load(argv[1], &mesh)) error("Loading mesh file '%s'\n", argv[1]); printf("* Setting the space up\n"); HcurlSpace space(&mesh, &shapeset); space.set_bc_types(bc_types); int order; sscanf(argv[2], "%d", &order); int dir_x = order, dir_y = order, dir_z = order; order3_t o(dir_x, dir_y, dir_z); printf(" - Setting uniform order to (%d, %d, %d)\n", o.x, o.y ,o.z); space.set_uniform_order(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; PardisoSolver 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_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>); LinearProblem lp(&wf, &space); // 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(); //#ifdef OUTPUT_DIR mat.dump(stdout, "a"); rhs.dump(stdout, "b"); //#endif if (solved) { scalar *s = solver.get_solution(); Solution sln(&mesh); sln.set_coeff_vector(&space, s); printf("* Solution:\n"); for (int i = 1; i <= ndofs; i++) { printf(" x[% 3d] = " SCALAR_FMT "\n", i, SCALAR(s[i])); } // 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()); // norm ExactSolution ex_sln(&mesh, exact_solution); double hcurl_sln_norm = hcurl_norm(&sln); double hcurl_err_norm = hcurl_error(&sln, &ex_sln); printf(" - Hcurl solution norm: % le\n", hcurl_sln_norm); printf(" - Hcurl error norm: % le\n", hcurl_err_norm); double l2_sln_norm = l2_norm_hcurl(&sln); double l2_err_norm = l2_error_hcurl(&sln, &ex_sln); printf(" - L2 solution norm: % le\n", l2_sln_norm); printf(" - L2 error norm: % le\n", l2_err_norm); if (hcurl_err_norm > EPS || l2_err_norm > EPS) { // calculated solution is not enough precise res = ERR_FAILURE; } #if 0 //def OUTPUT_DIR // output printf("starting output\n"); const char *of_name = OUTPUT_DIR "/solution.vtk"; FILE *ofile = fopen(of_name, "w"); if (ofile != NULL) { ExactSolution ex_sln(&mesh, exact_solution_0, exact_solution_1, exact_solution_2); RealPartFilter real_sln(&mesh, &sln, FN_VAL); ImagPartFilter imag_sln(&mesh, &sln, FN_VAL); DiffFilter eh(&mesh, &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); VtkOutputEngine output(ofile); output.out(&real_sln, "real_Uh", FN_VAL); output.out(&imag_sln, "imag_Uh", FN_VAL); output.out(&real_sln, "real_Uh_0", FN_VAL_0); output.out(&real_sln, "real_Uh_1", FN_VAL_1); output.out(&real_sln, "real_Uh_2", FN_VAL_2); output.out(&imag_sln, "imag_Uh_0", FN_VAL_0); output.out(&imag_sln, "imag_Uh_1", FN_VAL_1); output.out(&imag_sln, "imag_Uh_2", FN_VAL_2); fclose(ofile); } else { warning("Can not open '%s' for writing.", of_name); } #endif } #ifdef WITH_PETSC mat.free(); rhs.free(); PetscFinalize(); #endif 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); 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; }
bool test_lin_indep(Shapeset *shapeset) { _F_ printf("I. linear independency\n"); UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver solver(&mat, &rhs); ShapeFunction fu(shapeset), fv(shapeset); int n = Hex::NUM_VERTICES * 1 + // 1 vertex fn Hex::NUM_EDGES * shapeset->get_num_edge_fns(H3D_MAX_ELEMENT_ORDER) + Hex::NUM_FACES * shapeset->get_num_face_fns(order2_t(H3D_MAX_ELEMENT_ORDER, H3D_MAX_ELEMENT_ORDER)) + shapeset->get_num_bubble_fns(Ord3(H3D_MAX_ELEMENT_ORDER, H3D_MAX_ELEMENT_ORDER, H3D_MAX_ELEMENT_ORDER)); printf("number of functions = %d\n", n); int *fn_idx = new int [n]; int m = 0; // vertex fns for (int i = 0; i < Hex::NUM_VERTICES; i++, m++) fn_idx[m] = shapeset->get_vertex_index(i); // edge fns for (int i = 0; i < Hex::NUM_EDGES; i++) { int order = H3D_MAX_ELEMENT_ORDER; int *edge_idx = shapeset->get_edge_indices(i, 0, order); for (int j = 0; j < shapeset->get_num_edge_fns(order); j++, m++) fn_idx[m] = edge_idx[j]; } // face fns for (int i = 0; i < Hex::NUM_FACES; i++) { order2_t order(H3D_MAX_ELEMENT_ORDER, H3D_MAX_ELEMENT_ORDER); int *face_idx = shapeset->get_face_indices(i, 0, order); for (int j = 0; j < shapeset->get_num_face_fns(order); j++, m++) fn_idx[m] = face_idx[j]; } // bubble Ord3 order(H3D_MAX_ELEMENT_ORDER, H3D_MAX_ELEMENT_ORDER, H3D_MAX_ELEMENT_ORDER); int *bubble_idx = shapeset->get_bubble_indices(order); for (int j = 0; j < shapeset->get_num_bubble_fns(order); j++, m++) fn_idx[m] = bubble_idx[j]; // precalc structure mat.prealloc(n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mat.pre_add_ij(i, j); mat.alloc(); rhs.alloc(n); printf("assembling matrix "); for (int i = 0; i < n; i++) { fu.set_active_shape(fn_idx[i]); printf("."); fflush(stdout); // prevent caching of output (to see that it did not freeze) for (int j = 0; j < n; j++) { fv.set_active_shape(fn_idx[j]); double value = l2_product(&fu, &fv); mat.add(i, j, value); } } printf("\n"); for (int i = 0; i < n; i++) rhs.add(i, 0.0); printf("solving matrix\n"); // solve the system if (solver.solve()) { double *sln = solver.get_solution(); bool indep = true; for (int i = 1; i < n + 1; i++) { if (sln[i] >= EPS) { indep = false; break; } } if (indep) printf("ok\n"); else printf("Shape functions are not linearly independent\n"); } else { printf("Shape functions are not linearly independent\n"); } delete [] fn_idx; return true; }
/*********************************************************************************** * main program * ***********************************************************************************/ int main(int argc, char **argv) { #ifdef WITH_PETSC PetscInitialize(&argc, &argv, (char *) PETSC_NULL, PETSC_NULL); PetscPushErrorHandler(PetscIgnoreErrorHandler, PETSC_NULL); // Disable PETSc error handler. #endif // Load the initial mesh. Mesh mesh; Mesh3DReader mloader; mloader.load("l-beam.mesh3d", &mesh); // Initial uniform mesh refinements. printf("Performing %d initial mesh refinements.\n", INIT_REF_NUM); for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ); Word_t (nelem) = mesh.get_num_elements(); printf("New number of elements is %d.\n", (int) nelem); // Initialize the shapeset and the cache. H1ShapesetLobattoHex shapeset; #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 // Create H1 spaces x-displacement component. H1Space xdisp(&mesh, &shapeset); xdisp.set_bc_types(bc_types_x); xdisp.set_uniform_order(order3_t(P_INIT, P_INIT, P_INIT)); // Create H1 spaces y-displacement component. H1Space ydisp(&mesh, &shapeset); ydisp.set_bc_types(bc_types_y); ydisp.set_uniform_order(order3_t(P_INIT, P_INIT, P_INIT)); // Create H1 spaces z-displacement component. H1Space zdisp(&mesh, &shapeset); zdisp.set_bc_types(bc_types_z); zdisp.set_uniform_order(order3_t(P_INIT, P_INIT, P_INIT)); // Assign DOF. int ndofs = 0; ndofs += xdisp.assign_dofs(ndofs); ndofs += ydisp.assign_dofs(ndofs); ndofs += zdisp.assign_dofs(ndofs); printf(" - Number of DOFs: %d\n", ndofs); // Initialized the Weak formulation. WeakForm wf(3); wf.add_matrix_form(0, 0, bilinear_form_0_0<double, scalar>, bilinear_form_0_0<ord_t, ord_t>, SYM); wf.add_matrix_form(0, 1, bilinear_form_0_1<double, scalar>, bilinear_form_0_1<ord_t, ord_t>, SYM); wf.add_matrix_form(0, 2, bilinear_form_0_2<double, scalar>, bilinear_form_0_2<ord_t, ord_t>, SYM); wf.add_vector_form_surf(0, surf_linear_form_0<double, scalar>, surf_linear_form_0<ord_t, ord_t>); wf.add_matrix_form(1, 1, bilinear_form_1_1<double, scalar>, bilinear_form_1_1<ord_t, ord_t>, SYM); wf.add_matrix_form(1, 2, bilinear_form_1_2<double, scalar>, bilinear_form_1_2<ord_t, ord_t>, SYM); wf.add_vector_form_surf(1, surf_linear_form_1<double, scalar>, surf_linear_form_1<ord_t, ord_t>); wf.add_matrix_form(2, 2, bilinear_form_2_2<double, scalar>, bilinear_form_2_2<ord_t, ord_t>, SYM); wf.add_vector_form_surf(2, surf_linear_form_2<double, scalar>, surf_linear_form_2<ord_t, ord_t>, 5); // Initialize the mesh problem. LinearProblem lp(&wf); lp.set_spaces(Tuple<Space *>(&xdisp, &ydisp, &zdisp)); // Assemble stiffness matrix printf(" - Assembling... "); fflush(stdout); Timer tmr_assemble; tmr_assemble.start(); bool assembled = lp.assemble(&mat, &rhs); tmr_assemble.stop(); if (assembled) printf("done in %s (%lf secs)\n", tmr_assemble.get_human_time(), tmr_assemble.get_seconds()); else error("failed!"); // Solve the stiffness matrix. printf(" - Solving... "); fflush(stdout); Timer tmr_solve; tmr_solve.start(); bool solved = solver.solve(); tmr_solve.stop(); if (solved) printf("done in %s (%lf secs)\n", tmr_solve.get_human_time(), tmr_solve.get_seconds()); else { printf("failed\n"); } // Construct a solution. double *s = solver.get_solution(); Solution xsln(&mesh), ysln(&mesh), zsln(&mesh); xsln.set_fe_solution(&xdisp, s); ysln.set_fe_solution(&ydisp, s); zsln.set_fe_solution(&zdisp, s); // Output the solutions. printf(" - Output... "); fflush(stdout); out_fn(&xsln, &ysln, &zsln, "disp"); printf("done\n"); #ifdef WITH_PETSC mat.free(); rhs.free(); PetscFinalize(); #endif return 1; }
/*********************************************************************************** * main program * ************************************************************************************/ int main(int argc, char **args) { #ifdef WITH_PETSC PetscInitialize(NULL, NULL, PETSC_NULL, PETSC_NULL); PetscPushErrorHandler(PetscIgnoreErrorHandler, PETSC_NULL); // Disable PETSc error handler. #endif // Load the inital mesh. Mesh mesh; Mesh3DReader mesh_loader; mesh_loader.load("hexahedron.mesh3d", &mesh); // Initial uniform mesh refinements. printf("Performing %d initial mesh refinements.\n", INIT_REF_NUM); for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ); Word_t (nelem) = mesh.get_num_elements(); printf("New number of elements is %d.\n", nelem); //Initialize the shapeset and the cache. H1ShapesetLobattoHex shapeset; //Matrix solver. #if defined WITH_UMFPACK UMFPackMatrix mat; UMFPackVector rhs; UMFPackLinearSolver 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 // Graphs of DOF convergence. GnuplotGraph graph; graph.set_captions("", "Degrees of Freedom", "Error [%]"); graph.set_log_y(); graph.add_row("Total error", "k", "-", "O"); // Create H1 space to setup the problem. H1Space space(&mesh, &shapeset); space.set_bc_types(bc_types); space.set_essential_bc_values(essential_bc_values); space.set_uniform_order(order3_t(P_INIT, P_INIT, P_INIT)); // Initialize the weak formulation. WeakForm wf; wf.add_matrix_form(biform<double, double>, biform<ord_t, ord_t>, SYM, ANY); wf.add_vector_form(liform<double, double>, liform<ord_t, ord_t>, ANY); // Initialize the coarse mesh problem. LinProblem lp(&wf); lp.set_space(&space); // Adaptivity loop. int as = 0; bool done = false; do { printf("\n---- Adaptivity step %d:\n", as); printf("\nSolving on coarse mesh:\n"); // Procedures for coarse mesh problem. // Assign DOF. int ndof = space.assign_dofs(); printf(" - Number of DOF: %d\n", ndof); // Assemble stiffness matrix and rhs. printf(" - Assembling... "); fflush(stdout); if (lp.assemble(&mat, &rhs)) printf("done in %lf secs.\n", lp.get_time()); else error("failed!"); // Solve the system. printf(" - Solving... "); fflush(stdout); bool solved = solver.solve(); if (solved) printf("done in %lf secs.\n", solver.get_time()); else { printf("Failed.\n"); break; } // Construct a solution. Solution sln(&mesh); sln.set_fe_solution(&space, solver.get_solution()); // Output the orders and the solution. if (do_output) { out_orders(&space, "order", as); out_fn(&sln, "sln", as); } // Solving fine mesh problem. printf("Solving on fine mesh:\n"); // Matrix solver. #if defined WITH_UMFPACK UMFPackLinearSolver rsolver(&mat, &rhs); #elif defined WITH_PETSC PetscLinearSolver rsolver(&mat, &rhs); #elif defined WITH_MUMPS MumpsSolver rsolver(&mat, &rhs); #endif // Construct the refined mesh for reference(refined) solution. Mesh rmesh; rmesh.copy(mesh); rmesh.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ); // Setup space for the reference (globally refined) solution. Space *rspace = space.dup(&rmesh); rspace->copy_orders(space, 1); // Initialize the mesh problem for reference solution. LinProblem rlp(&wf); rlp.set_space(rspace); // Assign DOF. int rndof = rspace->assign_dofs(); printf(" - Number of DOF: %d\n", rndof); // Assemble stiffness matric and rhs. printf(" - Assembling... "); fflush(stdout); if (rlp.assemble(&mat, &rhs)) printf("done in %lf secs.\n", rlp.get_time()); else error("failed!"); // Solve the system. printf(" - Solving... "); fflush(stdout); bool rsolved = rsolver.solve(); if (rsolved) printf("done in %lf secs.\n", rsolver.get_time()); else { printf("failed.\n"); break; } // Construct the reference(refined) solution. Solution rsln(&rmesh); rsln.set_fe_solution(rspace, rsolver.get_solution()); // Compare coarse and fine mesh. // Calculate the error estimate wrt. refined mesh solution. double err = h1_error(&sln, &rsln); printf(" - H1 error: % lf\n", err * 100); // Save it to the graph. graph.add_value(0, ndof, err * 100); if (do_output) graph.save("conv.gp"); // Calculate error estimates for adaptivity. printf("Adaptivity\n"); printf(" - calculating error: "); fflush(stdout); H1Adapt hp(&space); double err_est = hp.calc_error(&sln, &rsln) * 100; printf("% lf %%\n", err_est); // If error is too large, adapt the mesh. if (err_est < ERR_STOP) { printf("\nDone\n"); break; } printf(" - adapting... "); fflush(stdout); hp.adapt(THRESHOLD); printf("done in %lf secs (refined %d element(s)).\n", hp.get_adapt_time(), hp.get_num_refined_elements()); if (rndof >= NDOF_STOP) { printf("\nDone.\n"); break; } // Clean up. delete rspace; // Next adaptivity step. as++; mat.free(); rhs.free(); } while (!done); #ifdef WITH_PETSC PetscFinalize(); #endif return 1; }