From: Jean-Paul Pelteret Date: Thu, 7 May 2020 06:10:57 +0000 (+0200) Subject: Convert steps 3,4,5,6,8 to FEValues::dof_indices()Do some FeValues::quadrature_point_... X-Git-Tag: v9.2.0-rc1~99^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aa80e7fbdf03dde2d591b81daa95dfb2e6625c74;p=dealii.git Convert steps 3,4,5,6,8 to FEValues::dof_indices()Do some FeValues::quadrature_point_indices() conversions as well. --- diff --git a/examples/step-3/step-3.cc b/examples/step-3/step-3.cc index 91d6337d92..e8fd779d8e 100644 --- a/examples/step-3/step-3.cc +++ b/examples/step-3/step-3.cc @@ -437,8 +437,8 @@ void Step3::assemble_system() // determinant and the quadrature point weight (that one // gets together by the call to FEValues::JxW() ). Finally, // this is repeated for all shape functions $i$ and $j$: - for (unsigned int i = 0; i < dofs_per_cell; ++i) - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int i : fe_values.dof_indices()) + for (const unsigned int j : fe_values.dof_indices()) cell_matrix(i, j) += (fe_values.shape_grad(i, q_index) * // grad phi_i(x_q) fe_values.shape_grad(j, q_index) * // grad phi_j(x_q) @@ -449,7 +449,7 @@ void Step3::assemble_system() // hand side function, which we choose to be the function // with constant value one (more interesting examples will // be considered in the following programs). - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) cell_rhs(i) += (fe_values.shape_value(i, q_index) * // phi_i(x_q) 1 * // f(x_q) fe_values.JxW(q_index)); // dx @@ -463,14 +463,14 @@ void Step3::assemble_system() // Then again loop over all shape functions i and j and transfer the // local elements to the global matrix. The global numbers can be // obtained using local_dof_indices[i]: - for (unsigned int i = 0; i < dofs_per_cell; ++i) - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int i : fe_values.dof_indices()) + for (const unsigned int j : fe_values.dof_indices()) system_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_matrix(i, j)); // And again, we do the same thing for the right hand side vector. - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) system_rhs(local_dof_indices[i]) += cell_rhs(i); } diff --git a/examples/step-4/step-4.cc b/examples/step-4/step-4.cc index 324a55cf83..b34c5c2dfa 100644 --- a/examples/step-4/step-4.cc +++ b/examples/step-4/step-4.cc @@ -373,9 +373,9 @@ void Step4::assemble_system() // constant right hand side with value 1, we use the object representing // the right hand side and evaluate it at the quadrature points: for (const unsigned int q_index : fe_values.quadrature_point_indices()) - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int j : fe_values.dof_indices()) cell_matrix(i, j) += (fe_values.shape_grad(i, q_index) * // grad phi_i(x_q) fe_values.shape_grad(j, q_index) * // grad phi_j(x_q) @@ -409,9 +409,9 @@ void Step4::assemble_system() // and right hand side is done exactly as before, but here we have again // merged some loops for efficiency: cell->get_dof_indices(local_dof_indices); - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int j : fe_values.dof_indices()) system_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_matrix(i, j)); diff --git a/examples/step-5/step-5.cc b/examples/step-5/step-5.cc index e3f6f9a0c1..cac2c2d6ea 100644 --- a/examples/step-5/step-5.cc +++ b/examples/step-5/step-5.cc @@ -192,9 +192,9 @@ void Step5::assemble_system() { const double current_coefficient = coefficient(fe_values.quadrature_point(q_index)); - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int j : fe_values.dof_indices()) cell_matrix(i, j) += (current_coefficient * // a(x_q) fe_values.shape_grad(i, q_index) * // grad phi_i(x_q) @@ -209,9 +209,9 @@ void Step5::assemble_system() cell->get_dof_indices(local_dof_indices); - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int j : fe_values.dof_indices()) system_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_matrix(i, j)); diff --git a/examples/step-6/step-6.cc b/examples/step-6/step-6.cc index f88995e82b..8e1f3d8096 100644 --- a/examples/step-6/step-6.cc +++ b/examples/step-6/step-6.cc @@ -246,14 +246,14 @@ void Step6::setup_system() // // The rest of the code that forms the local contributions remains // unchanged. It is worth noting, however, that under the hood several things -// are different than before. First, the variables dofs_per_cell -// and n_q_points now are 9 each, where they were 4 -// before. Introducing such variables as abbreviations is a good strategy to -// make code work with different elements without having to change too much -// code. Secondly, the fe_values object of course needs to do -// other things as well, since the shape functions are now quadratic, rather -// than linear, in each coordinate variable. Again, however, this is something -// that is completely handled by the library. +// are different than before. First, the variable dofs_per_cell +// and return value of quadrature_formula.size() now are 9 each, +// where they were 4 before. Introducing such variables as abbreviations is a +// good strategy to make code work with different elements without having to +// change too much code. Secondly, the fe_values object of course +// needs to do other things as well, since the shape functions are now +// quadratic, rather than linear, in each coordinate variable. Again, however, +// this is something that is completely handled by the library. template void Step6::assemble_system() { @@ -265,7 +265,6 @@ void Step6::assemble_system() update_quadrature_points | update_JxW_values); const unsigned int dofs_per_cell = fe.dofs_per_cell; - const unsigned int n_q_points = quadrature_formula.size(); FullMatrix cell_matrix(dofs_per_cell, dofs_per_cell); Vector cell_rhs(dofs_per_cell); @@ -279,13 +278,13 @@ void Step6::assemble_system() fe_values.reinit(cell); - for (unsigned int q_index = 0; q_index < n_q_points; ++q_index) + for (const unsigned int q_index : fe_values.quadrature_point_indices()) { const double current_coefficient = coefficient(fe_values.quadrature_point(q_index)); - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int j : fe_values.dof_indices()) cell_matrix(i, j) += (current_coefficient * // a(x_q) fe_values.shape_grad(i, q_index) * // grad phi_i(x_q) diff --git a/examples/step-8/step-8.cc b/examples/step-8/step-8.cc index 7015c289bf..6551a774bb 100644 --- a/examples/step-8/step-8.cc +++ b/examples/step-8/step-8.cc @@ -334,17 +334,18 @@ namespace Step8 // // With this knowledge, we can assemble the local matrix // contributions: - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { const unsigned int component_i = fe.system_to_component_index(i).first; - for (unsigned int j = 0; j < dofs_per_cell; ++j) + for (const unsigned int j : fe_values.dof_indices()) { const unsigned int component_j = fe.system_to_component_index(j).first; - for (unsigned int q_point = 0; q_point < n_q_points; ++q_point) + for (const unsigned int q_point : + fe_values.quadrature_point_indices()) { cell_matrix(i, j) += // The first term is $\lambda \partial_i u_i, \partial_j @@ -390,12 +391,13 @@ namespace Step8 // Assembling the right hand side is also just as discussed in the // introduction: - for (unsigned int i = 0; i < dofs_per_cell; ++i) + for (const unsigned int i : fe_values.dof_indices()) { const unsigned int component_i = fe.system_to_component_index(i).first; - for (unsigned int q_point = 0; q_point < n_q_points; ++q_point) + for (const unsigned int q_point : + fe_values.quadrature_point_indices()) cell_rhs(i) += fe_values.shape_value(i, q_point) * rhs_values[q_point][component_i] * fe_values.JxW(q_point);