MFEM v2.0
vector.hpp
Go to the documentation of this file.
00001 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
00002 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
00003 // reserved. See file COPYRIGHT for details.
00004 //
00005 // This file is part of the MFEM library. For more information and source code
00006 // availability see http://mfem.googlecode.com.
00007 //
00008 // MFEM is free software; you can redistribute it and/or modify it under the
00009 // terms of the GNU Lesser General Public License (as published by the Free
00010 // Software Foundation) version 2.1 dated February 1999.
00011 
00012 #ifndef MFEM_VECTOR
00013 #define MFEM_VECTOR
00014 
00015 // Data type vector
00016 
00017 #include <math.h>
00018 #include "../general/array.hpp"
00019 
00021 class Vector
00022 {
00023 protected:
00024 
00025    int size, allocsize;
00026    double * data;
00027 
00028 public:
00029 
00031    Vector () { allocsize = size = 0; data = 0; };
00032 
00034    Vector(const Vector &);
00035 
00037    explicit Vector (int s);
00038 
00039    Vector (double *_data, int _size)
00040    { data = _data; size = _size; allocsize = -size; }
00041 
00043    void Load (istream ** in, int np, int * dim);
00044 
00046    void Load(istream &in, int Size);
00047 
00049    void Load(istream &in) { int s; in >> s; Load (in, s); };
00050 
00052    void SetSize(int s);
00053 
00054    void SetData(double *d) { data = d; }
00055 
00056    void SetDataAndSize(double *d, int s)
00057    { data = d; size = s; allocsize = -s; }
00058 
00059    void MakeDataOwner() { allocsize = abs(allocsize); }
00060 
00062    void Destroy();
00063 
00065    inline int Size() const {return size;};
00066 
00067    // double *GetData() { return data; }
00068 
00069    inline double *GetData() const { return data; }
00070 
00071    inline operator double *() { return data; }
00072 
00073    inline operator const double *() const { return data; }
00074 
00076    inline void StealData(double **p) { *p = data; data = 0; size = 0; }
00077 
00079    double & Elem (int i);
00080 
00082    const double & Elem (int i) const;
00083 
00085    inline double & operator() (int i);
00086 
00088    inline const double & operator() (int i) const;
00089 
00090    double operator*(const double *) const;
00091 
00093    double operator*(const Vector &v) const;
00094 
00096    Vector & operator=(const Vector &v);
00097 
00099    Vector & operator=(double value);
00100 
00101    Vector & operator*=(double c);
00102 
00103    Vector & operator/=(double c);
00104 
00105    Vector & operator-=(double c);
00106 
00107    Vector & operator-=(const Vector &v);
00108 
00109    Vector & operator+=(const Vector &v);
00110 
00112    Vector & Add(const double a, const Vector &Va);
00113 
00115    Vector & Set(const double a, const Vector &x);
00116 
00117    void SetVector (const Vector &v, int offset);
00118 
00120    void Neg();
00121 
00123    friend void swap(Vector *v1, Vector *v2);
00124 
00126    friend void add(const Vector &v1, const Vector &v2, Vector &v);
00127 
00129    friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
00130 
00132    friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
00133 
00135    friend void add (const double a, const Vector &x,
00136                     const double b, const Vector &y, Vector &z);
00137 
00139    friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
00140 
00142    friend void subtract(const double a, const Vector &x,
00143                         const Vector &y, Vector &z);
00144 
00145    void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
00146    void GetSubVector(const Array<int> &dofs, double *elem_data) const;
00147 
00148    void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
00149    void SetSubVector(const Array<int> &dofs, double *elem_data);
00150 
00152    void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
00153    void AddElementVector(const Array<int> & dofs, double *elem_data);
00154    void AddElementVector(const Array<int> & dofs, const double a,
00155                          const Vector & elemvect);
00156 
00158    void Print(ostream & out = cout, int width = 8) const;
00159 
00161    void Print_HYPRE(ostream &out) const;
00162 
00164    void Randomize(int seed = 0);
00166    double Norml2();
00168    double Normlinf();
00170    double Norml1();
00172    double Max();
00174    double Min();
00176    double DistanceTo (const double *p) const;
00177 
00179    ~Vector ();
00180 };
00181 
00182 // Inline methods
00183 
00184 inline Vector::Vector (int s)
00185 {
00186    if (s > 0)
00187    {
00188       allocsize = size = s;
00189       data = new double[s];
00190    }
00191    else
00192    {
00193       allocsize = size = 0;
00194       data = NULL;
00195    }
00196 }
00197 
00198 inline void Vector::SetSize(int s)
00199 {
00200    if (s == size)
00201       return;
00202    if (s <= abs(allocsize))
00203    {
00204       size = s;
00205       return;
00206    }
00207    if (allocsize > 0)
00208       delete [] data;
00209    allocsize = size = s;
00210    data = new double[s];
00211 }
00212 
00213 inline void Vector::Destroy()
00214 {
00215    if (allocsize > 0)
00216       delete [] data;
00217    allocsize = size = 0;
00218    data = NULL;
00219 }
00220 
00221 inline double & Vector::operator() (int i)
00222 {
00223 #ifdef MFEM_DEBUG
00224    if (data == 0 || i < 0 || i >= size)
00225       mfem_error ("Vector::operator()");
00226 #endif
00227 
00228    return data[i];
00229 }
00230 
00231 inline const double & Vector::operator() (int i) const
00232 {
00233 #ifdef MFEM_DEBUG
00234    if (data == 0 || i < 0 || i >= size)
00235       mfem_error ("Vector::operator() const");
00236 #endif
00237 
00238    return data[i];
00239 }
00240 
00241 inline Vector::~Vector()
00242 {
00243    if (allocsize > 0)
00244       delete [] data;
00245 }
00246 
00247 inline double Distance(const double *x, const double *y, const int n)
00248 {
00249    double d = 0.0;
00250 
00251    for (int i = 0; i < n; i++)
00252       d += (x[i]-y[i])*(x[i]-y[i]);
00253 
00254    return sqrt(d);
00255 }
00256 
00257 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines