Ejemplo n.º 1
0
int main()
{
  // Try to read mesh contents from a .msh file (for information about the
  // structure of these files, see FreeFem++ documentation).
  TriangleMesh mesh;
  try {
    mesh.read_file_msh("squared-mesh-2x2.msh"); }
  catch (...) {
    std::cerr << "Error reading mesh file" << std::endl;
    exit(1); }

  // Define quadrature rule with nodes located at vertices of the reference triangle
  VerticesQuadRule quad_rule;

  // Define a finite element space on current mesh
  P1_FE_Space fe_space(mesh, quad_rule);

  // Start chronometer
  auto start = std::chrono::high_resolution_clock::now();

  // Repeat several times (to increase computation time)
  for (int num=0; num<20; ++num) {

    // For each cell, r:
    for (Index r=0; r<mesh.get_ncel(); ++r)
      {
	std::cout << "###### Cell: r=" << r << std::endl << std::endl;

	// In C++11, one can use "auto" instead of "FiniteElement"
	auto fe = fe_space.get_element(r); // Build a finite element on cell r

	// Get x-derivatives of all the shape functions on curent element.
	//
	// The get_dx_phi() member function returns (a const reference
	// to) a vector containng FE_Functions (one for each dof). A
	// FE_Function objetct contains the values resulting from the
	// evaluation of the corresponding shape function on the
	// quadrature points.
	//
	// You can use "auto" instead of "std::vector<FE_Function>",
	// but performance seems better using references ("auto&")
	auto& dx_phi = fe.get_dx_phi();

	// Get also y-derivatives of shape functions
	auto& dy_phi = fe.get_dy_phi();

	Index ndofs = fe.get_ndofs();

	// For each degree of freedom, i:
	for (Index i = 0; i < ndofs; ++i)
	  {
	    // For each degree of freedom, j:
	    for (Index j = 0; j < ndofs; ++j)
	      {
		// Compute integral of product of x-derivatives
		Real Kx_ij = fe.integrate(dx_phi[i], dx_phi[j]);
		std::cout << "Kx[" << i << "][" << j << "]=" << Kx_ij << std::endl;

		// Compute integral of product of y-derivatives
		Real Ky_ij = fe.integrate(dy_phi[i], dy_phi[j]);
		std::cout << "Ky[" << i << "][" << j << "]=" << Ky_ij << std::endl;

		// Compute integral on element r of gradient(phi_i)*gradient(phi_j),
		// i.e. dx(phi_i)*dx(phi_j) + dy(phi_i)*dy(phi_j)
		Real K_ij = Kx_ij + Ky_ij;
		std::cout << "K[" << i << "][" << j << "]=" << K_ij << std::endl;

		std::cout << std::endl;
	      }
	  }
      }
  }

  // Store final time and print elapsed time
  auto end = std::chrono::high_resolution_clock::now();
  std::cout << "Elapsed time: " <<
    std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() <<
    " miliseconds" << std::endl;
}