Esempio n. 1
0
int main(int argc, char **args) 
{
  // Time measurement.
  TimePeriod cpu_time;
  cpu_time.tick();

  // Load the mesh. 
  Mesh mesh;
  ExodusIIReader mloader;
  mloader.load("brick_with_hole_hex.e", &mesh);

  // Perform initial mesh refinement. 
  for (int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(H3D_H3D_H3D_REFT_HEX_XYZ);

  // Create H1 space with default shapeset for x-displacement component. 
  H1Space xdisp(&mesh, bc_types_x, essential_bc_values, Ord3(P_INIT_X, P_INIT_Y, P_INIT_Z));
  
  // Create H1 space with default shapeset for y-displacement component. 
  H1Space ydisp(&mesh, bc_types_y, essential_bc_values, Ord3(P_INIT_X, P_INIT_Y, P_INIT_Z));
  
  // Create H1 space with default shapeset for z-displacement component. 
  H1Space zdisp(&mesh, bc_types_z, essential_bc_values, Ord3(P_INIT_X, P_INIT_Y, P_INIT_Z));
  
  // Initialize weak formulation.
  WeakForm wf(3);
  wf.add_matrix_form(0, 0, callback(bilinear_form_0_0), HERMES_SYM);
  wf.add_matrix_form(0, 1, callback(bilinear_form_0_1), HERMES_SYM);
  wf.add_matrix_form(0, 2, callback(bilinear_form_0_2), HERMES_SYM);
  wf.add_vector_form_surf(0, callback(surf_linear_form_x), bdy_force);

  wf.add_matrix_form(1, 1, callback(bilinear_form_1_1), HERMES_SYM);
  wf.add_matrix_form(1, 2, callback(bilinear_form_1_2), HERMES_SYM);
  wf.add_vector_form_surf(1, callback(surf_linear_form_y), bdy_force);

  wf.add_matrix_form(2, 2, callback(bilinear_form_2_2), HERMES_SYM);
  wf.add_vector_form_surf(2, callback(surf_linear_form_z), bdy_force);

  // Initialize discrete problem.
  bool is_linear = true;
  DiscreteProblem dp(&wf, Hermes::Tuple<Space *>(&xdisp, &ydisp, &zdisp), is_linear);

  // 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.
  info("Assembling the linear problem (ndof: %d).", Space::get_num_dofs(Hermes::Tuple<Space *>(&xdisp, &ydisp, &zdisp)));
  dp.assemble(matrix, rhs);

  // Solve the linear system. If successful, obtain the solution.
  info("Solving the linear problem.");
  Solution xsln(xdisp.get_mesh());
  Solution ysln(ydisp.get_mesh());
  Solution zsln(zdisp.get_mesh());
  if(solver->solve()) Solution::vector_to_solutions(solver->get_solution(), 
                      Hermes::Tuple<Space *>(&xdisp, &ydisp, &zdisp), Hermes::Tuple<Solution *>(&xsln, &ysln, &zsln));
  else error ("Matrix solver failed.\n");

  // Output all components of the solution.
  if (solution_output) out_fn_vtk(&xsln, &ysln, &zsln, "sln");
  
  // Time measurement.
  cpu_time.tick();

  // Print timing information.
  info("Solutions saved. Total running time: %g s.", cpu_time.accumulated());

  // Clean up.
  delete matrix;
  delete rhs;
  delete solver;
  
  return 0;
}
Esempio n. 2
0
/***********************************************************************************
 * 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;
}