Example #1
0
int main(int argc, char* argv[])
{
  // Load the mesh.
  Mesh mesh;
  H2DReader mloader;
  mloader.load("domain.mesh", &mesh);

  // Perform initial mesh refinements.
  mesh.refine_towards_vertex(3, CORNER_REF_LEVEL);

  // Create an H1 space with default shapeset.
  H1Space space(&mesh, bc_types, essential_bc_values, P_INIT);
  int ndof = get_num_dofs(&space);
  info("ndof = %d", ndof);

  // Initialize the weak formulation.
  WeakForm wf;
  wf.add_matrix_form(callback(bilinear_form));
  wf.add_vector_form(callback(linear_form));
  wf.add_vector_form_surf(callback(linear_form_surf));

  // Initialize the linear problem.
  LinearProblem lp(&wf, &space);

  // Select matrix solver.
  Matrix* mat; Vector* rhs; CommonSolver* solver;
  init_matrix_solver(matrix_solver, ndof, mat, rhs, solver);

  // Assemble stiffness matrix and rhs.
  lp.assemble(mat, rhs);

  // Solve the matrix problem.
  if (!solver->solve(mat, rhs)) error ("Matrix solver failed.\n");

  // Convert coefficient vector into a Solution.
  Solution* sln = new Solution(&space, rhs);

  // Visualize the approximation.
  ScalarView view("Solution", new WinGeom(0, 0, 440, 350));
  view.show(sln);

  // Compute and show gradient magnitude.
  // (Note that the gradient at the re-entrant
  // corner needs to be truncated for visualization purposes.)
  ScalarView gradview("Gradient", new WinGeom(450, 0, 400, 350));
  MagFilter grad(Tuple<MeshFunction *>(sln, sln), 
                 Tuple<int>(H2D_FN_DX, H2D_FN_DY));
  gradview.show(&grad);

  // Wait for the views to be closed.
  View::wait();
  return 0;
}
Example #2
0
int main(int argc, char* argv[])
{
  // Load the mesh.
  Mesh mesh;
  H2DReader mloader;
  mloader.load("domain.mesh", &mesh);

  // Perform initial mesh refinements.
  for(int i=0; i < INIT_REF_NUM; i++) mesh.refine_all_elements();
  mesh.refine_towards_vertex(3, CORNER_REF_LEVEL);

  // Enter boundary markers.
  BCTypes bc_types;
  bc_types.add_bc_dirichlet(BDY_LEFT);
  bc_types.add_bc_neumann(Hermes::vector<int>(BDY_OUTER, BDY_INNER));
  bc_types.add_bc_newton(BDY_BOTTOM);

  // Enter Dirichlet boudnary values.
  BCValues bc_values;
  bc_values.add_const(BDY_LEFT, T1);

  // Create an H1 space with default shapeset.
  H1Space space(&mesh, &bc_types, &bc_values, P_INIT);
  int ndof = Space::get_num_dofs(&space);
  info("ndof = %d", ndof);

  // Initialize the weak formulation.
  WeakForm wf;
  wf.add_matrix_form(callback(bilinear_form));
  wf.add_matrix_form_surf(callback(bilinear_form_surf), BDY_BOTTOM);
  wf.add_vector_form_surf(callback(linear_form_surf), BDY_BOTTOM);

  // Initialize the FE problem.
  bool is_linear = true;
  DiscreteProblem dp(&wf, &space, 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 solution.
  Solution sln;

  // Assemble the stiffness matrix and right-hand side vector.
  info("Assembling the stiffness matrix and right-hand side vector.");
  dp.assemble(matrix, rhs);

  // Solve the linear system and if successful, obtain the solution.
  info("Solving the matrix problem.");
  if(solver->solve())
    Solution::vector_to_solution(solver->get_solution(), &space, &sln);
  else
    error ("Matrix solver failed.\n");

  // Visualize the solution.
  ScalarView view("Solution", new WinGeom(0, 0, 440, 350));
  view.show(&sln);

  // Compute and show gradient magnitude.
  // (Note that the gradient at the re-entrant
  // corner needs to be truncated for visualization purposes.)
  ScalarView gradview("Gradient", new WinGeom(450, 0, 400, 350));
  MagFilter grad(Hermes::vector<MeshFunction *>(&sln, &sln), 
                 Hermes::vector<int>(H2D_FN_DX, H2D_FN_DY));
  gradview.show(&grad);

  // Wait for all views to be closed.
  View::wait();

  // Clean up.
  delete solver;
  delete matrix;
  delete rhs;

  return 0;
}
Example #3
0
int main(int argc, char* argv[])
{

  /*** RECEIVE DATA ***/

  // Check the number of command line arguments.
  if(argc != 2) error("Configuration file missing.");

  // Open configuration file.
  FILE* f = fopen(argv[1], "r");
  if(f == NULL) error("Cannot open file %s.", argv[1]);

  // Read number of initial uniform mesh refinements.
  int init_ref_num;
  if(!Get(f, &init_ref_num)) error("Could not read number of initial mesh refinements.");

  // Read initial polynomial degree of elements.
  int init_p;
  if(!Get(f, &init_p)) error("Could not read number of initial polynomial degree.");

  // Read number of material markers.
  int n_mat_markers;
  if(!Get(f, &n_mat_markers)) error("Could not read number of material markers.");
  if(n_mat_markers <= 0) error("At least one material marker must be given.");

  // Read list of material markers.
  std::vector<int> mat_markers;
  for (int i = 0; i < n_mat_markers; i++) {
    int tmp;
    if(!Get(f, &tmp)) error("Could not read a material marker.");
    mat_markers.push_back(tmp);
  }

  // Read list of c1 constants.
  std::vector<double> c1_array;
  for (int i = 0; i < n_mat_markers; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a c1 constant.");
    c1_array.push_back(tmp);
  }

  // Read list of c2 constants.
  std::vector<double> c2_array;
  for (int i = 0; i < n_mat_markers; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a c2 constant.");
    c2_array.push_back(tmp);
  }

  // Read list of c3 constants.
  std::vector<double> c3_array;
  for (int i = 0; i < n_mat_markers; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a c3 constant.");
    c3_array.push_back(tmp);
  }

  // Read list of c4 constants.
  std::vector<double> c4_array;
  for (int i = 0; i < n_mat_markers; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a c4 constant.");
    c4_array.push_back(tmp);
  }

  // Read list of c5 constants.
  std::vector<double> c5_array;
  for (int i = 0; i < n_mat_markers; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a c5 constant.");
    c5_array.push_back(tmp);
  }

  // Read number of Dirichlet boundary markers.
  int n_bc_dirichlet;
  if(!Get(f, &n_bc_dirichlet)) error("Could not read number of Dirichlet boundary markers.");

  // Read list of Dirichlet boundary markers.
  std::vector<int> bdy_markers_dirichlet;
  for (int i = 0; i < n_bc_dirichlet; i++) {
    int tmp;
    if(!Get(f, &tmp)) error("Could not read a VALUE boundary marker.");
    bdy_markers_dirichlet.push_back(tmp);
  }

  // Read list of Dirichlet boundary values.
  std::vector<double> bdy_values_dirichlet;
  for (int i = 0; i < n_bc_dirichlet; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a Dirichlet boundary value.");
    bdy_values_dirichlet.push_back(tmp);
  }

  // Read number of Neumann boundary markers.
  int n_bc_neumann;
  if(!Get(f, &n_bc_neumann)) error("Could not read number of Neumann boundary markers.");

  // Read list of Neumann boundary markers.
  std::vector<int> bdy_markers_neumann;
  for (int i = 0; i < n_bc_neumann; i++) {
    int tmp;
    if(!Get(f, &tmp)) error("Could not read a Neumann boundary marker.");
    bdy_markers_neumann.push_back(tmp);
  }

  // Read list of Neumann boundary values.
  std::vector<double> bdy_values_neumann;
  for (int i = 0; i < n_bc_neumann; i++) {
    double tmp;
    if(!Get(f, &tmp)) error("Could not read a Neumann boundary value.");
    bdy_values_neumann.push_back(tmp);
  }

  // Read number of Newton boundary markers.
  int n_bc_newton;
  if(!Get(f, &n_bc_newton)) error("Could not read number of Newton boundary markers.");

  // Read list of Newton boundary markers.
  std::vector<int> bdy_markers_newton;
  for (int i = 0; i < n_bc_newton; i++) {
    int tmp;
    if(!Get(f, &tmp)) error("Could not read a Newton boundary marker.");
    bdy_markers_newton.push_back(tmp);
  }

  // Read list of Newton boundary value pairs.
  std::vector<double_pair> bdy_values_newton;
  for (int i = 0; i < n_bc_newton; i++) {
    double tmp1, tmp2;
    if(!Get(f, &tmp1)) error("Could not read a Newton boundary value (first in pair).");
    if(!Get(f, &tmp2)) error("Could not read a Newton boundary value (second in pair).");
    bdy_values_newton.push_back(std::make_pair(tmp1, tmp2));
  }

  /*** FEED THE DATA INTO THE ELECTROSTATICS MODULE ***/

  // Initialize the ModuleBasic class.
  ModuleBasic B;

  // Set mesh filename.
  B.set_mesh_str("\na = 1.0  # size of the mesh\nb = sqrt(2)/2\n\nvertices =\n{\n  { 0, -a },    # vertex 0\n  { a, -a },    # vertex 1\n  { -a, 0 },    # vertex 2\n  { 0, 0 },     # vertex 3\n  { a, 0 },     # vertex 4\n  { -a, a },    # vertex 5\n  { 0, a },     # vertex 6\n  { a*b, a*b }  # vertex 7\n}\n\nelements =\n{\n  { 0, 1, 4, 3, 0 },  # quad 0\n  { 3, 4, 7, 0 },     # tri 1\n  { 3, 7, 6, 0 },     # tri 2\n  { 2, 3, 6, 5, 0 }   # quad 3\n}\n\nboundaries =\n{\n  { 0, 1, 1 },\n  { 1, 4, 2 },\n  { 3, 0, 4 },\n  { 4, 7, 2 },\n  { 7, 6, 2 },\n  { 2, 3, 4 },\n  { 6, 5, 2 },\n  { 5, 2, 3 }\n}\n\ncurves =\n{\n  { 4, 7, 45 },  # +45 degree circular arcs\n  { 7, 6, 45 }\n}\n");
  
  // Set number of initial uniform mesh refinements.
  B.set_initial_mesh_refinement(init_ref_num);

  // Set initial polynomial degree of elements.
  B.set_initial_poly_degree(init_p);

  // Set material markers.
  B.set_material_markers(mat_markers);

  // Set c1 array.
  B.set_c1_array(c1_array);

  // Set c2 array.
  B.set_c2_array(c2_array);

  // Set c3 array.
  B.set_c3_array(c3_array);

  // Set c4 array.
  B.set_c4_array(c4_array);

  // Set c5 array.
  B.set_c5_array(c5_array);

  if (n_bc_dirichlet > 0) {
    // Set Dirichlet boundary markers.
    B.set_dirichlet_markers(bdy_markers_dirichlet);

    // Set Dirichlet boundary values.
    B.set_dirichlet_values(bdy_values_dirichlet);
  }

  if (n_bc_neumann > 0) {
    // Set Neumann boundary markers.
    B.set_neumann_markers(bdy_markers_neumann);

    // Set Neumann boundary values.
    B.set_neumann_values(bdy_values_neumann);
  }

  if (n_bc_newton > 0) {
    // Set Newton boundary markers.
    B.set_newton_markers(bdy_markers_newton);

    // Set Newton boundary values.
    B.set_newton_values(bdy_values_newton);
  }

  /*** SOLVE THE PROBLEM ***/

  Solution phi;
  bool success = B.calculate(&phi);
  if (!success) error("Computation failed.");

  /*** SHOW THE SOLUTION ***/

  ScalarView view("Solution", new WinGeom(0, 0, 440, 350));
  view.show(&phi);

  // Compute and show gradient magnitude.
  // (Note that the gradient at the re-entrant
  // corner needs to be truncated for visualization purposes.)
  ScalarView gradview("Gradient", new WinGeom(450, 0, 440, 350));
  MagFilter grad(Hermes::Tuple<MeshFunction *>(&phi, &phi), Hermes::Tuple<int>(H2D_FN_DX, H2D_FN_DY));
  gradview.show(&grad);

  // Wait for the views to be closed.
  View::wait();

  return 1;
}
Example #4
0
int main(int argc, char* argv[])
{
  // Instantiate a class with global functions.
  Hermes2D hermes2d;

  // Load the mesh.
  Mesh mesh;
  H2DReader mloader;
  mloader.load("domain.mesh", &mesh);

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

  // Initialize boundary conditions
  DefaultEssentialBCConst bc_essential("Bottom", T1);
  EssentialBCs bcs(&bc_essential);

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

  // Initialize the weak formulation.
  CustomWeakFormPoissonNewton wf(LAMBDA, ALPHA, T0, "Heat flux");

  // Initialize the FE problem.
  DiscreteProblem dp(&wf, &space);

  // 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);

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

  // Perform Newton's iteration.
  if (!hermes2d.solve_newton(coeff_vec, &dp, solver, matrix, rhs)) error("Newton's iteration failed.");

  // Translate the resulting coefficient vector into the Solution sln.
  Solution sln;
  Solution::vector_to_solution(coeff_vec, &space, &sln);

  // Visualize the solution.
  ScalarView view("Solution", new WinGeom(0, 0, 300, 400));
  view.show(&sln, HERMES_EPS_VERYHIGH);
  ScalarView gradview("Gradient", new WinGeom(310, 0, 300, 400));
  MagFilter grad(Hermes::vector<MeshFunction *>(&sln, &sln), 
                 Hermes::vector<int>(H2D_FN_DX, H2D_FN_DY));
  gradview.show(&grad, HERMES_EPS_VERYHIGH);

  // Wait for all views to be closed.
  View::wait();

  // Clean up.
  delete solver;
  delete matrix;
  delete rhs;
  delete [] coeff_vec;

  return 0;
}