FieldComponents.h

00001 #ifndef gridripper_amr1d_FieldComponents_h
00002 #define gridripper_amr1d_FieldComponents_h
00003 
00004 #include <gridripper/math.h>
00005 #include "FieldWrapper.h"
00006 
00007 namespace gridripper { namespace amr1d {
00008 
00009 using namespace std;
00010 
00019 template<class K>
00020 class FieldComponents;
00021 
00022 
00024 template <>
00025 class FieldComponents<GReal_t>;
00026 
00027 
00029 template <>
00030 class FieldComponents<GComplex_t>;
00031 
00032 
00034 template <>
00035 class FieldComponents<GReal_t>: public FieldWrapper
00036 {
00037 private:
00038     int numComponents;
00039 
00040 public:
00042     FieldComponents<GReal_t>(GReal_t* d, int n):
00043         FieldWrapper(d, n), numComponents(n) {
00044     }
00045 
00047     FieldComponents<GReal_t>(int n):
00048         FieldWrapper(n), numComponents(n) {
00049     }
00050 
00052     FieldComponents<GReal_t>(const FieldComponents<GReal_t>& other):
00053         FieldWrapper(other), numComponents(other.getNumComponents()) {
00054     }
00055 
00056     int getNumComponents() const {
00057         return numComponents;
00058     }
00059 
00060     GReal_t operator [](int i) const {
00061         return data()[i];
00062     }
00063 
00064     GReal_t& operator [](int i) {
00065         return data()[i];
00066     }
00067 
00072     bool resize(int n)
00073     {
00074         if (ownsData) numComponents=n;
00075         return FieldWrapper::resize(n);
00076     }
00077 
00078     virtual FieldWrapper* cloneFieldWrapper() const {
00079         return new FieldComponents<GReal_t>(*this);
00080     }
00081 
00082     FieldComponents<GReal_t>& operator=(const FieldComponents<GReal_t>& other)
00083     {
00084         FieldWrapper::operator=(other);
00085         if (ownsData) numComponents=other.numComponents;
00086         return *this;
00087     }
00088 
00089 };
00090 
00091 
00093 template <>
00094 class FieldComponents<GComplex_t>: public FieldWrapper
00095 {
00096 private:
00097     int numComponents;
00098 
00099 public:
00101     FieldComponents<GComplex_t>(GReal_t* d, int n):
00102         FieldWrapper(d, n*2), numComponents(n) {
00103     }
00104 
00106     FieldComponents<GComplex_t>(int n):
00107         FieldWrapper(n*2), numComponents(n) {
00108     }
00109 
00111     FieldComponents<GComplex_t>(const FieldComponents<GComplex_t>& other):
00112         FieldWrapper(other), numComponents(other.getNumComponents()) {
00113     }
00114 
00115     int getNumComponents() const {
00116         return numComponents;
00117     }
00118 
00119     const GComplex_t& operator [](int i) const {
00120         return *(GComplex_t*)(&(data()[i*2]));
00121     }
00122 
00123     GComplex_t& operator [](int i) {
00124         return *(GComplex_t*)(&(data()[i*2]));
00125     }
00126 
00131     bool resize(int n)
00132     {
00133         if (ownsData) numComponents=n;
00134         return FieldWrapper::resize(2*n);
00135     }
00136 
00137     virtual FieldWrapper* cloneFieldWrapper() const {
00138         return new FieldComponents<GComplex_t>(*this);
00139     }
00140 
00141     FieldComponents<GComplex_t>& operator=(const FieldComponents<GComplex_t>& other)
00142     {
00143         FieldWrapper::operator=(other);
00144         if (ownsData) numComponents=other.numComponents;
00145         return *this;
00146     }
00147 
00148 };
00149 
00150 
00151 /*
00152 // Modified by Andras Laszlo: I commented out linear operations, 
00153 // as they were not in use. Ask me if you need them.
00154     FieldComponents& operator +=(const FieldComponents& a) {
00155         int n1 = getNumComponents();
00156         int n2 = a.getNumComponents();
00157         int n = n1 < n2? n1 : n2;
00158         for(unsigned i = 0; i < n; ++i) {
00159             (*this)[i] += a[i];
00160         }
00161         return *this;
00162     }
00163 
00164     FieldComponents& operator -=(const FieldComponents& a) {
00165         int n1 = getNumComponents();
00166         int n2 = a.getNumComponents();
00167         int n = n1 < n2? n1 : n2;
00168         for(unsigned i = 0; i < n; ++i) {
00169             (*this)[i] -= a[i];
00170         }
00171         return *this;
00172     }
00173 
00174     FieldComponents& operator *=(const T& a) {
00175         int n = getNumComponents();
00176         for(unsigned i = 0; i < n; ++i) {
00177             (*this)[i] *= a;
00178         }
00179         return *this;
00180     }
00181 
00182     FieldComponents& operator /=(const T& a) {
00183         int n = getNumComponents();
00184         for(unsigned i = 0; i < n; ++i) {
00185             (*this)[i] /= a;
00186         }
00187         return *this;
00188     }
00189 
00190 
00191 
00192 
00193 
00194 template<class T>
00195 FieldComponents<T> operator +(const FieldComponents<T>& a,
00196                               const FieldComponents<T>& b)
00197 {
00198     int n1 = a.getNumComponents();
00199     int n2 = b.getNumComponents();
00200     int n = n1 > n2? n1 : n2;
00201     FieldComponents<T> c(n);
00202     c = a;
00203     c += b;
00204     return c;
00205 }
00206 
00207 template<class T>
00208 FieldComponents<T> operator -(const FieldComponents<T>& a,
00209                               const FieldComponents<T>& b)
00210 {
00211     int n1 = a.getNumComponents();
00212     int n2 = b.getNumComponents();
00213     int n = n1 > n2? n1 : n2;
00214     FieldComponents<T> c(n);
00215     c = a;
00216     c -= b;
00217     return c;
00218 }
00219 
00220 template<class T>
00221 FieldComponents<T> operator *(const FieldComponents<T>& a, const T& b)
00222 {
00223     unsigned n = a.getNumComponents();
00224     FieldComponents<T> c(n);
00225     for(unsigned i = 0; i < n; ++i) {
00226         c[i] = a[i]*b;
00227     }
00228     return c;
00229 }
00230 
00231 template<class T>
00232 FieldComponents<T> operator *(const T& a, const FieldComponents<T>& b)
00233 {
00234     unsigned n = b.getNumComponents();
00235     FieldComponents<T> c(n);
00236     for(unsigned i = 0; i < n; ++i) {
00237         c[i] = a*b[i];
00238     }
00239     return c;
00240 }
00241 
00242 template<class T>
00243 FieldComponents<T> operator /(const FieldComponents<T>& a, const T& b)
00244 {
00245     unsigned n = a.getNumComponents();
00246     FieldComponents<T> c(n);
00247     for(unsigned i = 0; i < n; ++i) {
00248         c[i] = a[i]/b;
00249     }
00250     return c;
00251 }
00252 */
00253 
00254 
00255 } } // namespace gridripper::amr1d
00256 
00257 #endif /* gridripper_amr1d_FieldComponents_h */

Generated on Wed Jun 17 18:46:47 2009 for GridRipper by  doxygen 1.5.6