FieldComponents.h

00001 #ifndef gridripper_amr1d_FieldComponents_h
00002 #define gridripper_amr1d_FieldComponents_h
00003 
00004 #include <gridripper/math.h>
00005 #include <gridripper/amr1d/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 
00057     FieldComponents<GReal_t>():
00058         FieldWrapper(0), numComponents(0) {
00059     }
00060 
00061     int getNumComponents() const {
00062         return numComponents;
00063     }
00064 
00065     GReal_t operator [](int i) const {
00066         return data()[i];
00067     }
00068 
00069     GReal_t& operator [](int i) {
00070         return data()[i];
00071     }
00072 
00077     bool resize(int n)
00078     {
00079         if (ownsData) numComponents=n;
00080         return FieldWrapper::resize(n);
00081     }
00082 
00083     virtual FieldWrapper* cloneFieldWrapper() const {
00084         return new FieldComponents<GReal_t>(*this);
00085     }
00086 
00087     FieldComponents<GReal_t>& operator=(const FieldComponents<GReal_t>& other)
00088     {
00089         FieldWrapper::operator=(other);
00090         if (ownsData) numComponents=other.numComponents;
00091         return *this;
00092     }
00093 
00094 };
00095 
00096 
00098 template <>
00099 class FieldComponents<GComplex_t>: public FieldWrapper
00100 {
00101 private:
00102     int numComponents;
00103 
00104 public:
00106     FieldComponents<GComplex_t>(GReal_t* d, int n):
00107         FieldWrapper(d, n*2), numComponents(n) {
00108     }
00109 
00111     FieldComponents<GComplex_t>(int n):
00112         FieldWrapper(n*2), numComponents(n) {
00113     }
00114 
00116     FieldComponents<GComplex_t>(const FieldComponents<GComplex_t>& other):
00117         FieldWrapper(other), numComponents(other.getNumComponents()) {
00118     }
00119 
00121     FieldComponents<GComplex_t>():
00122         FieldWrapper(0*2), numComponents(0) {
00123     }
00124 
00125     int getNumComponents() const {
00126         return numComponents;
00127     }
00128 
00129     const GComplex_t& operator [](int i) const {
00130         return *(GComplex_t*)(&(data()[i*2]));
00131     }
00132 
00133     GComplex_t& operator [](int i) {
00134         return *(GComplex_t*)(&(data()[i*2]));
00135     }
00136 
00141     bool resize(int n)
00142     {
00143         if (ownsData) numComponents=n;
00144         return FieldWrapper::resize(2*n);
00145     }
00146 
00147     virtual FieldWrapper* cloneFieldWrapper() const {
00148         return new FieldComponents<GComplex_t>(*this);
00149     }
00150 
00151     FieldComponents<GComplex_t>& operator=(const FieldComponents<GComplex_t>& other)
00152     {
00153         FieldWrapper::operator=(other);
00154         if (ownsData) numComponents=other.numComponents;
00155         return *this;
00156     }
00157 
00158 };
00159 
00160 
00161 /*
00162 // Modified by Andras Laszlo: I commented out linear operations, 
00163 // as they were not in use. Ask me if you need them.
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 FieldComponents& a) {
00175         int n1 = getNumComponents();
00176         int n2 = a.getNumComponents();
00177         int n = n1 < n2? n1 : n2;
00178         for(unsigned i = 0; i < n; ++i) {
00179             (*this)[i] -= a[i];
00180         }
00181         return *this;
00182     }
00183 
00184     FieldComponents& operator *=(const T& a) {
00185         int n = getNumComponents();
00186         for(unsigned i = 0; i < n; ++i) {
00187             (*this)[i] *= a;
00188         }
00189         return *this;
00190     }
00191 
00192     FieldComponents& operator /=(const T& a) {
00193         int n = getNumComponents();
00194         for(unsigned i = 0; i < n; ++i) {
00195             (*this)[i] /= a;
00196         }
00197         return *this;
00198     }
00199 
00200 
00201 
00202 
00203 
00204 template<class T>
00205 FieldComponents<T> operator +(const FieldComponents<T>& a,
00206                               const FieldComponents<T>& b)
00207 {
00208     int n1 = a.getNumComponents();
00209     int n2 = b.getNumComponents();
00210     int n = n1 > n2? n1 : n2;
00211     FieldComponents<T> c(n);
00212     c = a;
00213     c += b;
00214     return c;
00215 }
00216 
00217 template<class T>
00218 FieldComponents<T> operator -(const FieldComponents<T>& a,
00219                               const FieldComponents<T>& b)
00220 {
00221     int n1 = a.getNumComponents();
00222     int n2 = b.getNumComponents();
00223     int n = n1 > n2? n1 : n2;
00224     FieldComponents<T> c(n);
00225     c = a;
00226     c -= b;
00227     return c;
00228 }
00229 
00230 template<class T>
00231 FieldComponents<T> operator *(const FieldComponents<T>& a, const T& b)
00232 {
00233     unsigned n = a.getNumComponents();
00234     FieldComponents<T> c(n);
00235     for(unsigned i = 0; i < n; ++i) {
00236         c[i] = a[i]*b;
00237     }
00238     return c;
00239 }
00240 
00241 template<class T>
00242 FieldComponents<T> operator *(const T& a, const FieldComponents<T>& b)
00243 {
00244     unsigned n = b.getNumComponents();
00245     FieldComponents<T> c(n);
00246     for(unsigned i = 0; i < n; ++i) {
00247         c[i] = a*b[i];
00248     }
00249     return c;
00250 }
00251 
00252 template<class T>
00253 FieldComponents<T> operator /(const FieldComponents<T>& a, const T& b)
00254 {
00255     unsigned n = a.getNumComponents();
00256     FieldComponents<T> c(n);
00257     for(unsigned i = 0; i < n; ++i) {
00258         c[i] = a[i]/b;
00259     }
00260     return c;
00261 }
00262 */
00263 
00264 
00265 } } // namespace gridripper::amr1d
00266 
00267 #endif /* gridripper_amr1d_FieldComponents_h */