]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Performance tweaks with assumptions on the size of passed vectors.
authorWolfgang Bangerth <bangerth@math.tamu.edu>
Mon, 18 May 1998 08:40:46 +0000 (08:40 +0000)
committerWolfgang Bangerth <bangerth@math.tamu.edu>
Mon, 18 May 1998 08:40:46 +0000 (08:40 +0000)
git-svn-id: https://svn.dealii.org/trunk@305 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/deal.II/include/dofs/dof_accessor.h
deal.II/deal.II/source/dofs/dof_accessor.cc
deal.II/deal.II/source/dofs/dof_handler.cc
deal.II/deal.II/source/numerics/assembler.cc
deal.II/deal.II/source/numerics/base.cc
deal.II/deal.II/source/numerics/matrices.cc

index 09fa11588141261f3ae5a76f01c1438d2684bffe..1f9b25ff0df25137e54a1103d43805c4bfefd5bc 100644 (file)
@@ -87,6 +87,10 @@ class DoFAccessor {
                                      * Exception
                                      */
     DeclException0 (ExcVectorNotEmpty);
+                                    /**
+                                     * Exception
+                                     */
+    DeclException0 (ExcVectorDoesNotMatch);
 
   protected:
                                     /**
@@ -202,9 +206,8 @@ class DoFLineAccessor :  public DoFAccessor<dim>, public BaseClass {
                                      * on vertex 0, dofs on vertex 1, 
                                      * dofs on line.
                                      *
-                                     * The dof indices are appended to the end
-                                     * of the vector. It is assumed that
-                                     * the vector be empty beforehand.
+                                     * It is assumed that the vector already
+                                     * has the right size beforehand.
                                      */
     void get_dof_indices (vector<int> &dof_indices) const;
 
@@ -289,9 +292,8 @@ class DoFQuadAccessor :  public DoFAccessor<dim>, public BaseClass {
                                      * dofs on line 0, dofs on line 1, etc,
                                      * dofs on quad 0, etc.
                                      *
-                                     * The dof indices are appended to the end
-                                     * of the vector. It is assumed that
-                                     * the vector be empty beforehand.
+                                     * It is assumed that the vector already
+                                     * has the right size beforehand.
                                      */
     void get_dof_indices (vector<int> &dof_indices) const;
 
@@ -489,10 +491,6 @@ class DoFCellAccessor :  public DoFSubstructAccessor<dim> {
     void get_dof_values (const dVector  &values,
                         vector<double> &dof_values) const;
 
-                                    /**
-                                     * Exception
-                                     */
-    DeclException0 (ExcVectorDoesNotMatch);
                                     /**
                                      *  Exception
                                      */
index 18b5c5416f31161e3251db014b5e2b21d7ae71fc..07cbbee0682650c057daffa9c31b42b8f8edde60 100644 (file)
@@ -89,14 +89,16 @@ void
 DoFLineAccessor<dim,BaseClass>::get_dof_indices (vector<int> &dof_indices) const {
   Assert (dof_handler != 0, ExcInvalidObject());
   Assert (&dof_handler->get_selected_fe() != 0, ExcInvalidObject());
-  Assert (dof_indices.size() == 0, ExcVectorNotEmpty());
-  
-  dof_indices.reserve (dof_handler->get_selected_fe().total_dofs);
+  Assert (dof_indices.size() == (2*dof_handler->get_selected_fe().dofs_per_vertex +
+                                dof_handler->get_selected_fe().dofs_per_line),
+         ExcVectorDoesNotMatch());
+
+  vector<int>::iterator next = dof_indices.begin();
   for (unsigned int vertex=0; vertex<2; ++vertex)
     for (unsigned int d=0; d<dof_handler->get_selected_fe().dofs_per_vertex; ++d)
-      dof_indices.push_back (vertex_dof_index(vertex,d));
+      *next++ = vertex_dof_index(vertex,d);
   for (unsigned int d=0; d<dof_handler->get_selected_fe().dofs_per_line; ++d)
-    dof_indices.push_back (dof_index(d));
+    *next++ = dof_index(d);
 };
 
 
@@ -200,17 +202,20 @@ void
 DoFQuadAccessor<dim,BaseClass>::get_dof_indices (vector<int> &dof_indices) const {
   Assert (dof_handler != 0, ExcInvalidObject());
   Assert (&dof_handler->get_selected_fe() != 0, ExcInvalidObject());
-  Assert (dof_indices.size() == 0, ExcVectorNotEmpty());
-  
-  dof_indices.reserve (dof_handler->get_selected_fe().total_dofs);
+  Assert (dof_indices.size() == (4*dof_handler->get_selected_fe().dofs_per_vertex +
+                                4*dof_handler->get_selected_fe().dofs_per_line +
+                                dof_handler->get_selected_fe().dofs_per_quad),
+         ExcVectorDoesNotMatch());
+
+  vector<int>::iterator next = dof_indices.begin();
   for (unsigned int vertex=0; vertex<4; ++vertex)
     for (unsigned int d=0; d<dof_handler->get_selected_fe().dofs_per_vertex; ++d)
-      dof_indices.push_back (vertex_dof_index(vertex,d));
+      *next++ = vertex_dof_index(vertex,d);
   for (unsigned int line=0; line<4; ++line)
     for (unsigned int d=0; d<dof_handler->get_selected_fe().dofs_per_line; ++d)
-      dof_indices.push_back (this->line(line)->dof_index(d));
+      *next++ = this->line(line)->dof_index(d);
   for (unsigned int d=0; d<dof_handler->get_selected_fe().dofs_per_quad; ++d)
-    dof_indices.push_back (dof_index(d));
+    *next++ = dof_index(d);
 };
 
 
index c5cdcbc89cd8dd312fb989ac730435da504e9972..68326925b7f2f773cebc98183b44cf06bd391162 100644 (file)
@@ -1193,13 +1193,10 @@ void DoFHandler<dim>::transfer_cell (const typename DoFHandler<dim>::cell_iterat
     if (new_cell->active() && old_cell->active())
                                       // both cells have no children
       {
-       vector<int> old_dofs, new_dofs;
+       vector<int> old_dofs(selected_fe->total_dofs);
+       vector<int> new_dofs(selected_fe->total_dofs);
        old_cell->get_dof_indices (old_dofs);
        new_cell->get_dof_indices (new_dofs);
-       Assert (old_dofs.size() == selected_fe->total_dofs,
-               ExcInternalError ());
-       Assert (new_dofs.size() == selected_fe->total_dofs,
-               ExcInternalError ());
 
                                         // copy dofs one-by-one
        for (unsigned int j=0; j<old_dofs.size(); ++j)
@@ -1218,21 +1215,15 @@ void DoFHandler<dim>::transfer_cell (const typename DoFHandler<dim>::cell_iterat
            };
          
                                           // numbers of old dofs
-         vector<int> old_dof_indices;
+         vector<int> old_dof_indices (selected_fe->total_dofs);
          old_cell->get_dof_indices (old_dof_indices);
 
-         Assert (old_dof_indices.size() == selected_fe->total_dofs,
-                 ExcInternalError ());
-
          for (unsigned int c=0; c<GeometryInfo<dim>::children_per_cell; ++c)
            {
                                               // numbers of child dofs
-             vector<int> child_dof_indices;
+             vector<int> child_dof_indices(selected_fe->total_dofs);
              child[c]->get_dof_indices (child_dof_indices);
 
-             Assert (child_dof_indices.size() == selected_fe->total_dofs,
-                     ExcInternalError ());
-
              for (unsigned int k=0; k<selected_fe->total_dofs; ++k)
                for (unsigned int j=0; j<selected_fe->total_dofs; ++j)
                  if (selected_fe->prolongate(c)(k,j) != 0.) 
@@ -1250,21 +1241,15 @@ void DoFHandler<dim>::transfer_cell (const typename DoFHandler<dim>::cell_iterat
            };
          
                                                   // numbers of new dofs
-         vector<int> new_dof_indices;
+         vector<int> new_dof_indices(selected_fe->total_dofs);
          new_cell->get_dof_indices(new_dof_indices);
-
-         Assert (new_dof_indices.size() == selected_fe->total_dofs,
-                 ExcInternalError ());
          
          for (unsigned int c=0; c<GeometryInfo<dim>::children_per_cell; ++c)
            {
                                               // numbers of child dofs
-             vector<int> child_dof_indices;
+             vector<int> child_dof_indices (selected_fe->total_dofs);
              child[c]->get_dof_indices (child_dof_indices);
 
-             Assert (child_dof_indices.size() == selected_fe->total_dofs,
-                     ExcInternalError ());
-
              for (unsigned int k=0; k<selected_fe->total_dofs; ++k)
                for (unsigned int j=0; j<selected_fe->total_dofs; ++j)
                  if (selected_fe->restrict(c)(k,j) != 0.)
@@ -1289,13 +1274,10 @@ void DoFHandler<dim>::transfer_cell (const typename DoFHandler<dim>::cell_iterat
     if (new_cell->active() && old_cell->active())
                                       // both cells have no children
       {
-       vector<int> old_dofs, new_dofs;
+       vector<int> old_dofs (selected_fe->total_dofs);
+       vector<int> new_dofs (selected_fe->total_dofs);
        old_cell->get_dof_indices (old_dofs);
        new_cell->get_dof_indices (new_dofs);
-       Assert (old_dofs.size() == selected_fe->total_dofs,
-               ExcInternalError ());
-       Assert (new_dofs.size() == selected_fe->total_dofs,
-               ExcInternalError ());
 
                                         // copy dofs one-by-one
        for (unsigned int j=0; j<old_dofs.size(); ++j)
@@ -1314,21 +1296,15 @@ void DoFHandler<dim>::transfer_cell (const typename DoFHandler<dim>::cell_iterat
            };
          
                                           // numbers of old dofs
-         vector<int> old_dof_indices;
+         vector<int> old_dof_indices (selected_fe->total_dofs);
          old_cell->get_dof_indices (old_dof_indices);
 
-         Assert (old_dof_indices.size() == selected_fe->total_dofs,
-                 ExcInternalError ());
-
          for (unsigned int c=0; c<GeometryInfo<dim>::children_per_cell; ++c)
            {
                                               // numbers of child dofs
-             vector<int> child_dof_indices;
+             vector<int> child_dof_indices (selected_fe->total_dofs);
              child[c]->get_dof_indices (child_dof_indices);
 
-             Assert (child_dof_indices.size() == selected_fe->total_dofs,
-                     ExcInternalError ());
-
              for (unsigned int k=0; k<selected_fe->total_dofs; ++k)
                for (unsigned int j=0; j<selected_fe->total_dofs; ++j)
                  if (selected_fe->prolongate(c)(k,j) != 0.) 
@@ -1347,21 +1323,15 @@ void DoFHandler<dim>::transfer_cell (const typename DoFHandler<dim>::cell_iterat
            };
          
                                                   // numbers of new dofs
-         vector<int> new_dof_indices;
+         vector<int> new_dof_indices (selected_fe->total_dofs);
          new_cell->get_dof_indices(new_dof_indices);
-
-         Assert (new_dof_indices.size() == selected_fe->total_dofs,
-                 ExcInternalError ());
          
          for (unsigned int c=0; c<GeometryInfo<dim>::children_per_cell; ++c)
            {
                                               // numbers of child dofs
-             vector<int> child_dof_indices;
+             vector<int> child_dof_indices (selected_fe->total_dofs);
              child[c]->get_dof_indices (child_dof_indices);
 
-             Assert (child_dof_indices.size() == selected_fe->total_dofs,
-                     ExcInternalError ());
-
              for (unsigned int k=0; k<selected_fe->total_dofs; ++k)
                for (unsigned int j=0; j<selected_fe->total_dofs; ++j)
                  if (selected_fe->restrict(c)(k,j) != 0.)
@@ -1482,12 +1452,11 @@ void DoFHandler<dim>::distribute_cell_to_dof_vector (const dVector &cell_data,
   active_cell_iterator cell = begin_active(),
                       endc = end();
   unsigned int present_cell = 0;
-  vector<int> dof_indices;
   const unsigned int total_dofs = selected_fe->total_dofs;
+  vector<int> dof_indices (total_dofs);
   
   for (; cell!=endc; ++cell, ++present_cell) 
     {
-      dof_indices.resize (0);
       cell->get_dof_indices (dof_indices);
       for (unsigned int i=0; i<total_dofs; ++i)
        {
index ebe0a4b57324705566e3b201bb3fcc18320f78af..69c917d04e6c945cdff8b0890c56d02d7552480f 100644 (file)
@@ -142,7 +142,7 @@ void Assembler<dim>::assemble (const Equation<dim> &equation) {
 
 
                                   // get indices of dofs
-  vector<int> dofs;
+  vector<int> dofs (n_dofs);
   get_dof_indices (dofs);
   
                                   // distribute cell matrix
index 6f28164669f2067210ff2d361eb72ea559506cef..1a0618c8e8d72ffdf180faeac4b0665101f2ef05 100644 (file)
@@ -59,10 +59,10 @@ template <int dim>
 void ProblemBase<dim>::clear () {
   tria        = 0;
   dof_handler = 0;
-  system_sparsity.reinit (0,0,0);
+  system_sparsity.reinit (1,1,1);
   system_matrix.clear ();
-  right_hand_side.reinit (0);
-  solution.reinit (0);
+  right_hand_side.reinit (1);
+  solution.reinit (1);
   constraints.clear ();
 };
 
@@ -212,7 +212,7 @@ void ProblemBase<dim>::integrate_difference (const Function<dim>      &exact_sol
                                             // and assign that to the vector
                                             // \psi.
            const unsigned int n_q_points = q.n_quadrature_points;
-           vector<double>   psi;
+           vector<double>   psi (n_q_points);
 
                                             // in praxi: first compute
                                             // exact solution vector
@@ -300,7 +300,7 @@ void ProblemBase<dim>::integrate_difference (const Function<dim>      &exact_sol
                                             // same procedure as above, but now
                                             // psi is a vector of gradients
            const unsigned int n_q_points = q.n_quadrature_points;
-           vector<Point<dim> >   psi;
+           vector<Point<dim> >   psi (n_q_points);
 
                                             // in praxi: first compute
                                             // exact solution vector
@@ -503,12 +503,9 @@ ProblemBase<dim>::make_boundary_value_list (const FunctionMap        &dirichlet_
   FunctionMap::const_iterator function_ptr;
 
                                   // field to store the indices of dofs
-                                  // initialize once to get the size right
-                                  // for the following fields.
-  vector<int>         face_dofs;
-  face->get_dof_indices (face_dofs);
+  vector<int>         face_dofs (fe.dofs_per_face);
   vector<Point<dim> > dof_locations (face_dofs.size(), Point<dim>());
-  vector<double>      dof_values;
+  vector<double>      dof_values (fe.dofs_per_face);
        
   for (; face!=endf; ++face)
     if ((function_ptr = dirichlet_bc.find(face->boundary_indicator())) !=
@@ -519,8 +516,6 @@ ProblemBase<dim>::make_boundary_value_list (const FunctionMap        &dirichlet_
                                         // get indices, physical location and
                                         // boundary values of dofs on this
                                         // face
-       face_dofs.erase (face_dofs.begin(), face_dofs.end());
-       dof_values.erase (dof_values.begin(), dof_values.end());
        face->get_dof_indices (face_dofs);
        fe.get_face_ansatz_points (face, boundary, dof_locations);
        function_ptr->second->value_list (dof_locations, dof_values);
index 53fc60946d381c4370a30df5b096dd29345b45ed..cad018acfe5128bda59f4795ed2b4a134ef6c62d 100644 (file)
@@ -163,7 +163,7 @@ void MassMatrix<dim>::assemble (dFMatrix            &cell_matrix,
 
   if (coefficient != 0)
     {
-      vector<double> coefficient_values;
+      vector<double> coefficient_values (fe_values.n_quadrature_points);
       coefficient->value_list (fe_values.get_quadrature_points(),
                               coefficient_values);
       for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point)
@@ -194,12 +194,12 @@ void MassMatrix<dim>::assemble (dFMatrix            &cell_matrix,
   
   const dFMatrix       &values    = fe_values.get_shape_values ();
   const vector<double> &weights   = fe_values.get_JxW_values ();
-  vector<double>        rhs_values;
+  vector<double>        rhs_values (fe_values.n_quadrature_points);
   right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values);
 
   if (coefficient != 0)
     {
-      vector<double> coefficient_values;
+      vector<double> coefficient_values (fe_values.n_quadrature_points);
       coefficient->value_list (fe_values.get_quadrature_points(),
                               coefficient_values);
       for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point)
@@ -239,7 +239,7 @@ void MassMatrix<dim>::assemble (dVector             &rhs,
   
   const dFMatrix       &values    = fe_values.get_shape_values ();
   const vector<double> &weights   = fe_values.get_JxW_values ();
-  vector<double>        rhs_values;
+  vector<double>        rhs_values(fe_values.n_quadrature_points);
   right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values);
 
   for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point)
@@ -270,13 +270,13 @@ void LaplaceMatrix<dim>::assemble (dFMatrix            &cell_matrix,
   
   const vector<vector<Point<dim> > >&gradients = fe_values.get_shape_grads ();
   const dFMatrix       &values    = fe_values.get_shape_values ();
-  vector<double>        rhs_values;
+  vector<double>        rhs_values(fe_values.n_quadrature_points);
   const vector<double> &weights   = fe_values.get_JxW_values ();
   right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values);
 
   if (coefficient != 0)
     {
-      vector<double> coefficient_values;
+      vector<double> coefficient_values(fe_values.n_quadrature_points);
       coefficient->value_list (fe_values.get_quadrature_points(),
                               coefficient_values);
       for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point)
@@ -318,7 +318,7 @@ void LaplaceMatrix<dim>::assemble (dFMatrix            &cell_matrix,
    
   if (coefficient != 0)
     {
-      vector<double> coefficient_values;
+      vector<double> coefficient_values(fe_values.n_quadrature_points);
       coefficient->value_list (fe_values.get_quadrature_points(),
                               coefficient_values);
       for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point)
@@ -348,7 +348,7 @@ void LaplaceMatrix<dim>::assemble (dVector             &rhs,
   
   const dFMatrix       &values    = fe_values.get_shape_values ();
   const vector<double> &weights   = fe_values.get_JxW_values ();
-  vector<double>        rhs_values;
+  vector<double>        rhs_values(fe_values.n_quadrature_points);
   right_hand_side->value_list (fe_values.get_quadrature_points(), rhs_values);
    
   for (unsigned int point=0; point<fe_values.n_quadrature_points; ++point)

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.