Ejemplo n.º 1
0
int main(int argc, char* argv[])
{
  // Load the mesh.
  Hermes::Hermes2D::Mesh mesh;
  Hermes::Hermes2D::MeshReaderH2DXML mloader;
  mloader.load("domain.xml", &mesh);

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

  // This is here basically to show off that we can save a mesh with refinements and load it back again.
  mloader.save("domain2.xml", &mesh);
  mloader.load("domain2.xml", &mesh);

  // Initialize the weak formulation.
  CustomWeakFormPoisson wf(new Hermes::Hermes1DFunction<double>(LAMBDA_AL), "Aluminum", 
    new Hermes::Hermes1DFunction<double>(LAMBDA_CU), "Copper", new Hermes::Hermes2DFunction<double>(-VOLUME_HEAT_SRC));

  // Initialize essential boundary conditions.
  Hermes::Hermes2D::DefaultEssentialBCConst<double> bc_essential(Hermes::vector<std::string>("Bottom", "Inner", "Outer", "Left"),
    FIXED_BDY_TEMP);
  Hermes::Hermes2D::EssentialBCs<double> bcs(&bc_essential);

  // Create an H1 space with default shapeset.
  Hermes::Hermes2D::H1Space<double> space(&mesh, &bcs, P_INIT);
  int ndof = space.get_num_dofs();
  info("ndof = %d", ndof);

  // Initialize the FE problem.
  Hermes::Hermes2D::DiscreteProblem<double> dp(&wf, &space);

  // Initial coefficient vector for the Newton's method.
  double* coeff_vec = new double[ndof];
  memset(coeff_vec, 0, ndof*sizeof(double));

  // Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
  Hermes::Hermes2D::Solution<double> sln;
  Hermes::Hermes2D::NewtonSolver<double> newton(&dp, matrix_solver_type);
  try
  {
    newton.solve_keep_jacobian(coeff_vec, 1e-3, 10);
  }
  catch(Hermes::Exceptions::Exception e)
  {
    e.printMsg();
    Hermes::Hermes2D::Solution<double>::vector_to_solution(newton.get_sln_vector(), &space, &sln);

    // VTK output.
    if (VTK_VISUALIZATION)
    {
      // Output solution in VTK format.
      Hermes::Hermes2D::Views::Linearizer lin;
      bool mode_3D = true;
      lin.save_solution_vtk(&sln, "sln.vtk", "Temperature", mode_3D);
      info("Solution in VTK format saved to file %s.", "sln.vtk");

      // Output mesh and element orders in VTK format.
      Hermes::Hermes2D::Views::Orderizer ord;
      ord.save_orders_vtk(&space, "ord.vtk");
      info("Element orders in VTK format saved to file %s.", "ord.vtk");
    }
    // Visualize the solution.
    if (HERMES_VISUALIZATION)
    {
      Hermes::Hermes2D::Views::ScalarView view("Solution", new Hermes::Hermes2D::Views::WinGeom(0, 0, 440, 350));
      // Hermes uses adaptive FEM to approximate higher-order FE solutions with linear
      // triangles for OpenGL. The second parameter of View::show() sets the error
      // tolerance for that. Options are HERMES_EPS_LOW, HERMES_EPS_NORMAL (default),
      // HERMES_EPS_HIGH and HERMES_EPS_VERYHIGH. The size of the graphics file grows
      // considerably with more accurate representation, so use it wisely.
      view.show(&sln, Hermes::Hermes2D::Views::HERMES_EPS_HIGH);
      Hermes::Hermes2D::Views::View::wait();
    }

    // Clean up.
    delete [] coeff_vec;
  }

  Hermes::Hermes2D::Solution<double>::vector_to_solution(newton.get_sln_vector(), &space, &sln);

  // VTK output.
  if (VTK_VISUALIZATION)
  {
    // Output solution in VTK format.
    Hermes::Hermes2D::Views::Linearizer lin;
    bool mode_3D = true;
    lin.save_solution_vtk(&sln, "sln.vtk", "Temperature", mode_3D);
    info("Solution in VTK format saved to file %s.", "sln.vtk");

    // Output mesh and element orders in VTK format.
    Hermes::Hermes2D::Views::Orderizer ord;
    ord.save_orders_vtk(&space, "ord.vtk");
    info("Element orders in VTK format saved to file %s.", "ord.vtk");
  }
  // Visualize the solution.
  if (HERMES_VISUALIZATION)
  {
    Hermes::Hermes2D::Views::ScalarView view("Solution", new Hermes::Hermes2D::Views::WinGeom(0, 0, 440, 350));
    // Hermes uses adaptive FEM to approximate higher-order FE solutions with linear
    // triangles for OpenGL. The second parameter of View::show() sets the error
    // tolerance for that. Options are HERMES_EPS_LOW, HERMES_EPS_NORMAL (default),
    // HERMES_EPS_HIGH and HERMES_EPS_VERYHIGH. The size of the graphics file grows
    // considerably with more accurate representation, so use it wisely.
    view.show(&sln, Hermes::Hermes2D::Views::HERMES_EPS_HIGH);
    Hermes::Hermes2D::Views::View::wait();
  }

  // Clean up.
  delete [] coeff_vec;

  return 0;
}
Ejemplo n.º 2
0
int main(int argc, char* argv[])
{
    // Load the mesh.
    Hermes::Hermes2D::Mesh mesh;
    Hermes::Hermes2D::H2DReader mloader;
    mloader.load("domain.mesh", &mesh);

    // Perform initial mesh refinements (optional).
    int refinement_type = 2;            // Split elements vertically.
    for (int i = 0; i < INIT_REF_NUM; i++) mesh.refine_all_elements(refinement_type);

    // Show the mesh.
    Hermes::Hermes2D::Views::MeshView mview("Mesh", new Hermes::Hermes2D::Views::WinGeom(0, 0, 900, 250));
    if (HERMES_VISUALIZATION) {
        mview.show(&mesh);
        //mview.wait();
    }

    // Initialize the weak formulation.
    CustomWeakFormPoisson wf("Al", new Hermes::Hermes1DFunction<double>(LAMBDA_AL), "Cu",
                             new Hermes::Hermes1DFunction<double>(LAMBDA_CU), new Hermes::Hermes2DFunction<double>(-VOLUME_HEAT_SRC));

    // Initialize essential boundary conditions.
    Hermes::Hermes2D::DefaultEssentialBCConst<double> bc_essential(Hermes::vector<std::string>("Left", "Right"),
            FIXED_BDY_TEMP);
    Hermes::Hermes2D::EssentialBCs<double> bcs(&bc_essential);

    // Create an H1 space with default shapeset.
    Hermes::Hermes2D::H1Space<double> space(&mesh, &bcs, P_INIT);
    int ndof = space.get_num_dofs();
    info("ndof = %d", ndof);

    // Initialize the FE problem.
    Hermes::Hermes2D::DiscreteProblem<double> dp(&wf, &space);

    // Initial coefficient vector for the Newton's method.
    double* coeff_vec = new double[ndof];
    memset(coeff_vec, 0, ndof*sizeof(double));

    // Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
    Hermes::Hermes2D::Solution<double> sln;
    Hermes::Hermes2D::NewtonSolver<double> newton(&dp, matrix_solver_type);
    if (!newton.solve(coeff_vec))
        error("Newton's iteration failed.");
    else
        Hermes::Hermes2D::Solution<double>::vector_to_solution(newton.get_sln_vector(), &space, &sln);

    // Get info about time spent during assembling in its respective parts.
    dp.get_all_profiling_output(std::cout);

    // VTK output.
    if (VTK_VISUALIZATION)
    {
        // Output solution in VTK format.
        Hermes::Hermes2D::Views::Linearizer<double> lin;
        bool mode_3D = true;
        lin.save_solution_vtk(&sln, "sln.vtk", "Temperature", mode_3D);
        info("Solution in VTK format saved to file %s.", "sln.vtk");

        // Output mesh and element orders in VTK format.
        Hermes::Hermes2D::Views::Orderizer ord;
        ord.save_orders_vtk(&space, "ord.vtk");
        info("Element orders in VTK format saved to file %s.", "ord.vtk");
    }

    // Visualize the solution.
    if (HERMES_VISUALIZATION)
    {
        Hermes::Hermes2D::Views::ScalarView<double> view("Solution", new Hermes::Hermes2D::Views::WinGeom(0, 300, 900, 350));
        // Hermes uses adaptive FEM to approximate higher-order FE solutions with linear
        // triangles for OpenGL. The second parameter of View::show() sets the error
        // tolerance for that. Options are HERMES_EPS_LOW, HERMES_EPS_NORMAL (default),
        // HERMES_EPS_HIGH and HERMES_EPS_VERYHIGH. The size of the graphics file grows
        // considerably with more accurate representation, so use it wisely.
        view.show(&sln, Hermes::Hermes2D::Views::HERMES_EPS_HIGH);
        Hermes::Hermes2D::Views::View::wait();
    }

    // Clean up.
    delete [] coeff_vec;

    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
  // Load the mesh.
  MeshSharedPtr mesh(new Mesh);
  Hermes::Hermes2D::MeshReaderH2DXML mloader;
  mloader.load("domain.xml", mesh);

  // Refine all elements, do it INIT_REF_NUM-times.
  for(unsigned int i = 0; i < INIT_REF_NUM; i++)
    mesh->refine_all_elements();

  // Initialize essential boundary conditions.
  Hermes::Hermes2D::DefaultEssentialBCConst<double> bc_essential(Hermes::vector<std::string>("Bottom", "Inner", "Outer", "Left"),
    FIXED_BDY_TEMP);
  Hermes::Hermes2D::EssentialBCs<double> bcs(&bc_essential);

  // Initialize space->
  SpaceSharedPtr<double> space( new Hermes::Hermes2D::H1Space<double>(mesh, &bcs, P_INIT));

  std::cout << "Ndofs: " << space->get_num_dofs() << std::endl;

  // Initialize the weak formulation.
  CustomWeakFormPoisson wf("Aluminum", new Hermes::Hermes1DFunction<double>(LAMBDA_AL), "Copper",
    new Hermes::Hermes1DFunction<double>(LAMBDA_CU), new Hermes::Hermes2DFunction<double>(-VOLUME_HEAT_SRC));

  // Initialize the solution.
  MeshFunctionSharedPtr<double> sln(new Solution<double>);

  // Initialize linear solver.
  Hermes::Hermes2D::LinearSolver<double> linear_solver(&wf, space);
  
  // Solve the linear problem.
  try
  {
    linear_solver.solve();

    // Get the solution vector.
    double* sln_vector = linear_solver.get_sln_vector();

    // Translate the solution vector into the previously initialized Solution.
    Hermes::Hermes2D::Solution<double>::vector_to_solution(sln_vector, space, sln);

    MyVolumetricIntegralCalculator calc(sln, 1);
    std::cout << calc.calculate(Hermes::vector<std::string>("Bottom", "Inner", "Outer", "Left"))[0];

    // VTK output.
    if(VTK_VISUALIZATION)
    {
      // Output solution in VTK format.
      Hermes::Hermes2D::Views::Linearizer lin;
      bool mode_3D = false;
      lin.save_solution_vtk(sln, "sln.vtk", "Temperature", mode_3D, 1, Hermes::Hermes2D::Views::HERMES_EPS_LOW);

      // Output mesh and element orders in VTK format.
      Hermes::Hermes2D::Views::Orderizer ord;
      ord.save_mesh_vtk(space, "mesh.vtk");
      ord.save_orders_vtk(space, "ord.vtk");
      ord.save_markers_vtk(space, "markers.vtk");
    }

    if(HERMES_VISUALIZATION)
    {
      // Visualize the solution.
      Hermes::Hermes2D::Views::ScalarView viewS("Solution", new Hermes::Hermes2D::Views::WinGeom(0, 0, 500, 400));
      Hermes::Hermes2D::Views::OrderView viewSp("Space", new Hermes::Hermes2D::Views::WinGeom(0, 400, 500, 400));

      viewS.show(sln, Hermes::Hermes2D::Views::HERMES_EPS_LOW);
      viewSp.show(space);

      viewS.wait_for_close();
    }
  }
  catch(std::exception& e)
  {
    std::cout << e.what();
  }
  return 0;
}
Ejemplo n.º 4
0
int main(int argc, char* argv[])
{
  // Load the mesh.
  Hermes::Hermes2D::Mesh mesh;
  Hermes::Hermes2D::MeshReaderH2D mloader;
  mloader.load("domain.mesh", &mesh);

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

  // Initialize the weak formulation.
  CustomWeakFormPoissonNewton wf("Aluminum", new Hermes::Hermes1DFunction<double>(LAMBDA_AL), 
    "Copper", new Hermes::Hermes1DFunction<double>(LAMBDA_CU), 
    new Hermes::Hermes2DFunction<double>(-VOLUME_HEAT_SRC),
    "Outer", ALPHA, T_EXTERIOR);

  // Initialize boundary conditions.
  CustomDirichletCondition bc_essential(Hermes::vector<std::string>("Bottom", "Inner", "Left"),
    BDY_A_PARAM, BDY_B_PARAM, BDY_C_PARAM);
  Hermes::Hermes2D::EssentialBCs<double> bcs(&bc_essential);

  // Create an H1 space with default shapeset.
  Hermes::Hermes2D::H1Space<double> space(&mesh, &bcs, P_INIT);
  int ndof = space.get_num_dofs();
  info("ndof = %d", ndof);

  // Initialize the FE problem.
  Hermes::Hermes2D::DiscreteProblem<double> dp(&wf, &space);

  // Initial coefficient vector for the Newton's method.  
  double* coeff_vec = new double[ndof];
  memset(coeff_vec, 0, ndof*sizeof(double));

  // Initialize the Newton solver.
  Hermes::Hermes2D::NewtonSolver<double> newton(&dp, matrix_solver_type);

  // Perform Newton's iteration and translate the resulting coefficient vector into a Solution.
  Hermes::Hermes2D::Solution<double> sln;
  if (!newton.solve(coeff_vec)) 
    error("Newton's iteration failed.");
  else
    Hermes::Hermes2D::Solution<double>::vector_to_solution(newton.get_sln_vector(), &space, &sln);

  // VTK output.
  if (VTK_VISUALIZATION) {
    // Output solution in VTK format.
    Hermes::Hermes2D::Views::Linearizer lin(&sln);
    bool mode_3D = true;
    lin.save_solution_vtk("sln.vtk", "Temperature", mode_3D);
    info("Solution in VTK format saved to file %s.", "sln.vtk");

    // Output mesh and element orders in VTK format.
    Hermes::Hermes2D::Views::Orderizer ord;
    ord.save_orders_vtk(&space, "ord.vtk");
    info("Element orders in VTK format saved to file %s.", "ord.vtk");
  }

  // Visualize the solution.
  if (HERMES_VISUALIZATION) {
    Hermes::Hermes2D::Views::ScalarView view("Solution", new Hermes::Hermes2D::Views::WinGeom(0, 0, 440, 350));
    view.show(&sln, Hermes::Hermes2D::Views::HERMES_EPS_VERYHIGH);
    Hermes::Hermes2D::Views::View::wait();
  }

  // Clean up.
  delete [] coeff_vec;

  return 0;
}