sum_of_matrices.cc
Go to the documentation of this file.
1 //LIC// ====================================================================
2 //LIC// This file forms part of oomph-lib, the object-oriented,
3 //LIC// multi-physics finite-element library, available
4 //LIC// at http://www.oomph-lib.org.
5 //LIC//
6 //LIC// Version 1.0; svn revision $LastChangedRevision$
7 //LIC//
8 //LIC// $LastChangedDate$
9 //LIC//
10 //LIC// Copyright (C) 2006-2016 Matthias Heil and Andrew Hazel
11 //LIC//
12 //LIC// This library is free software; you can redistribute it and/or
13 //LIC// modify it under the terms of the GNU Lesser General Public
14 //LIC// License as published by the Free Software Foundation; either
15 //LIC// version 2.1 of the License, or (at your option) any later version.
16 //LIC//
17 //LIC// This library is distributed in the hope that it will be useful,
18 //LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 //LIC// Lesser General Public License for more details.
21 //LIC//
22 //LIC// You should have received a copy of the GNU Lesser General Public
23 //LIC// License along with this library; if not, write to the Free Software
24 //LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 //LIC// 02110-1301 USA.
26 //LIC//
27 //LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
28 //LIC//
29 //LIC//====================================================================
30 
31 
32 #include "matrices.h"
33 #include "double_vector.h"
34 #include "sum_of_matrices.h"
35 
36 
37 namespace oomph
38 {
39 
40  // =================================================================
41  /// \short Matrix-vector multiplication for a sumofmatrices class. Just
42  /// delegate each multiplication to the appropriate class then add up the
43  /// results.
44  // =================================================================
46  {
47  // We assume that appropriate checks and initialisation on x and soln are
48  // carried out within the individual matrix multiplys.
49 
50  // Multiply for the main matrix
51  Main_matrix_pt->multiply(x, soln);
52 
53  // Now add contribution for the added matrices
54  for(unsigned i_matrix=0; i_matrix<Added_matrix_pt.size(); i_matrix++)
55  {
56  // If possible copy the matrix distribution, otherwise it isn't
57  // distributed so make a serial LinearAlgebraDistribution object.
58  LinearAlgebraDistribution col_dist, row_dist;
59  OomphCommunicator serial_comm; // Serial communcator (does nothing)
60  col_dist.build(&serial_comm, added_matrix_pt(i_matrix)->ncol(), false);
61  row_dist.build(&serial_comm, added_matrix_pt(i_matrix)->nrow(), false);
62 
63  // Create temporary output DoubleVector
64  DoubleVector temp_soln(row_dist);
65 
66  // Create a const iterator for the map (faster than .find() or []
67  // access, const means can't change the map via the iterator).
68  std::map<unsigned, unsigned>::const_iterator it;
69 
70  // Pull out the appropriate values into a temp vector
71  //??ds not parallel
72  DoubleVector temp_x(col_dist);
73  for(it = Col_map_pt[i_matrix]->main_to_added_mapping_pt()->begin();
74  it != Col_map_pt[i_matrix]->main_to_added_mapping_pt()->end();
75  it++)
76  {
77  temp_x[it->second] = x[it->first];
78  }
79 
80  // Perform the multiplication
81  Added_matrix_pt[i_matrix]->multiply(temp_x, temp_soln);
82 
83  // Add result to solution vector
84  //??ds not parallel
85  for(it = Row_map_pt[i_matrix]->main_to_added_mapping_pt()->begin();
86  it != Row_map_pt[i_matrix]->main_to_added_mapping_pt()->end();
87  it++)
88  {
89  soln[it->first] += temp_soln[it->second];
90  }
91  }
92 
93  }
94 
95 }
Vector< DoubleMatrixBase * > Added_matrix_pt
List of pointers to the matrices that are added to the main matrix.
virtual void multiply(const DoubleVector &x, DoubleVector &soln) const =0
Multiply the matrix by the vector x: soln=Ax.
void build(const OomphCommunicator *const comm_pt, const unsigned &first_row, const unsigned &nrow_local, const unsigned &nrow=0)
Sets the distribution. Takes first_row, nrow_local and nrow as arguments. If nrow is not provided or ...
unsigned long nrow() const
Return the number of rows of the main matrix.
Describes the distribution of a distributable linear algebra type object. Typically this is a contain...
DoubleMatrixBase * added_matrix_pt(const unsigned &i) const
Vector< const AddedMainNumberingLookup *> Row_map_pt
List of maps between row numbers of the main matrix and the added matrices.
DoubleMatrixBase * Main_matrix_pt
Pointer to the matrix which we are adding the others to.
Vector< const AddedMainNumberingLookup *> Col_map_pt
List of maps between col numbers of the main matrix and the added matrices.
void multiply(const DoubleVector &x, DoubleVector &soln) const
Multiply: just call multiply on each of the matrices and add up the results (with appropriate bookeep...
A vector in the mathematical sense, initially developed for linear algebra type applications. If MPI then this vector can be distributed - its distribution is described by the LinearAlgebraDistribution object at Distribution_pt. Data is stored in a C-style pointer vector (double*)
Definition: double_vector.h:61
unsigned long ncol() const
Return the number of columns of the main matrix.
An oomph-lib wrapper to the MPI_Comm communicator object. Just contains an MPI_Comm object (which is ...
Definition: communicator.h:57