PetscVecTools.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 
00030 #ifndef _PETSCVECTOOLS_HPP_
00031 #define _PETSCVECTOOLS_HPP_
00032 
00033 #include "UblasVectorInclude.hpp" // needs to be 'first'
00034 
00035 #include <petscvec.h>
00036 
00040 class PetscVecTools
00041 {
00042 public:
00047     static void Assemble(Vec vector);
00048 
00053     static void Display(Vec vector);
00054 
00059     static void Zero(Vec vector);
00060 
00068     static void SetElement(Vec vector, PetscInt row, double value);
00069 
00077     static void AddToElement(Vec vector, PetscInt row, double value);
00078 
00084     static unsigned GetSize(Vec vector);
00085 
00093     static void GetOwnershipRange(Vec vector, PetscInt& lo, PetscInt& hi);
00094 
00102     static double GetElement(Vec vector, PetscInt row);
00103     
00111     static void AddScaledVector(Vec y, Vec x, double scaleFactor);
00112 
00118     static void Scale(Vec vector, double scaleFactor);
00119     
00128     static void WAXPY(Vec w, double a, Vec x, Vec y);
00129 
00130 
00141     template<size_t VECTOR_SIZE>
00142     static void AddMultipleValues(Vec vector, unsigned* vectorIndices, c_vector<double, VECTOR_SIZE>& smallVector)
00143     {
00144         PetscInt indices_owned[VECTOR_SIZE];
00145         PetscInt num_indices_owned = 0;
00146         PetscInt global_row;
00147         PetscInt lo, hi;
00148         GetOwnershipRange(vector, lo, hi);
00149 
00150         for (unsigned row = 0; row<VECTOR_SIZE; row++)
00151         {
00152             global_row = vectorIndices[row];
00153             if (global_row >=lo && global_row <hi)
00154             {
00155                 indices_owned[num_indices_owned++] = global_row;
00156             }
00157         }
00158 
00159         if (num_indices_owned == VECTOR_SIZE)
00160         {
00161             VecSetValues(vector,
00162                          num_indices_owned,
00163                          indices_owned,
00164                          smallVector.data(),
00165                          ADD_VALUES);
00166         }
00167         else
00168         {
00169             // We need continuous data, if some of the rows do not belong to the processor their values
00170             // are not passed to VecSetValues
00171             double values[VECTOR_SIZE];
00172             unsigned num_values_owned = 0;
00173 
00174             for (unsigned row = 0; row<VECTOR_SIZE; row++)
00175             {
00176                 global_row = vectorIndices[row];
00177                 if (global_row >= lo && global_row < hi)
00178                 {
00179                     values[num_values_owned++] = smallVector(row);
00180                 }
00181             }
00182 
00183             VecSetValues(vector,
00184                          num_indices_owned,
00185                          indices_owned,
00186                          values,
00187                          ADD_VALUES);
00188         }
00189     }
00190 
00199     static void SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter& rFirstVariableScatterContext, VecScatter& rSecondVariableScatterContext);
00200 
00201 
00211     static void DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec);
00212 
00222     static void DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec);
00223 
00224 };
00225 
00226 #endif //_PETSCVECTOOLS_HPP_

Generated on Tue May 31 14:31:42 2011 for Chaste by  doxygen 1.5.5