include/qccomplexfield.h

00001 /***************************************************************************
00002  *   Quantum Construct (qC++)                                                                  *
00003  *   The Quantum Physics Computational Library                                         *
00004  *   Copyright (C) 2005 by Shekhar S. Chandra                                      *
00005  *   Shekhar.Chandra@sci.monash,edu.au                                     *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 /*
00023         Ver Info - QCComplexField.h
00024         1.0.0 - Original Implementation
00025 */
00026 
00027 #ifndef QCCOMPLEXFIELD_H
00028 #define QCCOMPLEXFIELD_H
00029 
00030 #include <complex>
00031 
00032 #include "qcfield.h"
00033 
00034 using namespace std;
00035 
00039 typedef complex<fieldType> complexType;
00040 
00043 static complexType i(0,1);
00046 static complexType complexZero(0,0);
00047 
00051 enum formatType{CSV = 0, GNU};
00052 
00059 template <int rank>
00060 class QCComplexField : public QCField<complexType,rank>
00061 {
00062 public:
00063         // -------------------------------------------------------
00069         QCComplexField(void);
00074         QCComplexField(int noOfRows, int noOfCols);
00078         ~QCComplexField(void);
00080 
00081         // -------------------------------------------------------
00087         void initializeToZero();
00092         void initializeToUnity(bool realPart, bool imagPart);
00094 
00095         // -------------------------------------------------------
00101         Array<fieldType,rank> realPart();
00105         Array<fieldType,rank> imaginaryPart();
00107 
00108         // -------------------------------------------------------
00114         bool writeRealToFile(const char* fileName);
00118         bool writeImagToFile(const char* fileName);
00122         bool writeNormToFile(const char* fileName);
00126         void setFormatCSV()
00127         {       outputFormat = CSV;     }
00131         void setFormatGNU()
00132         {       outputFormat = GNU;     }
00134 private:
00135         formatType outputFormat; 
00136 };
00137 
00138 template <int rank>
00139 QCComplexField<rank>::QCComplexField(void)
00140 {
00141         setFormatCSV();
00142 }
00143 
00144 template <int rank>
00145 QCComplexField<rank>::QCComplexField(int noOfRows, int noOfCols)
00146 {
00147         this->setSize(noOfRows,noOfCols);
00148 }
00149 
00150 template <int rank>
00151 QCComplexField<rank>::~QCComplexField(void)
00152 {
00153 }
00154 
00155 template <int rank>
00156 void QCComplexField<rank>::initializeToZero()
00157 {
00158         complexType tmp(0.0,0.0);
00159         if(this->Field.rows() > 0 || this->Field.cols() > 0)
00160                 this->Field = tmp;
00161 }
00162 
00163 template <int rank>
00164 void QCComplexField<rank>::initializeToUnity(bool realPart, bool imagPart)
00165 {
00166         complexType tmp(0.0,0.0);
00167 
00168         if(realPart)
00169                 tmp = tmp + 1.0;
00170         if(imagPart)
00171                 tmp = tmp + i;
00172 
00173         if(this->Field.rows() > 0 || this->Field.cols() > 0)
00174                 this->Field = tmp;
00175 }
00176 
00177 template <int rank>
00178 Array<fieldType,rank> QCComplexField<rank>::realPart()
00179 {
00180         return real(this->Field);
00181 }
00182 
00183 template <int rank>
00184 Array<fieldType,rank> QCComplexField<rank>::imaginaryPart()
00185 {
00186         return imag(this->Field);
00187 }
00188 
00189 template <int rank>
00190 bool QCComplexField<rank>::writeRealToFile(const char* fileName)
00191 {
00192         ofstream outFile(fileName,ios::out);
00193         outFile.precision(filePrecision);
00194 
00195         if(outFile.fail())
00196         {
00197                 cerr << "File Error: " << fileName << endl;
00198                 return false;
00199         }
00200         else
00201         {
00202                 if(outputFormat == CSV)
00203                 {
00204                         for(int k = 0; k < this->Field.cols(); k ++)
00205                         {
00206                                 for(int j = 0; j < this->Field.rows(); j ++)
00207                                         outFile << this->Field(j,k).real() << " , ";
00208                                 outFile << endl;
00209                         }
00210                 }
00211                 else if (outputFormat == GNU)
00212                 {
00213                         outFile << "#The following is the Real part of the Solution" << endl;
00214                         outFile << "#x values \t\t y values \t\t Magnitude" << endl;
00215                         for(int k = 0; k < this->Field.cols(); k ++)
00216                         {
00217                                 for(int j = 0; j < this->Field.rows(); j ++)
00218                                 {
00219                                         outFile << j << "\t\t" << k << "\t\t" << this->Field(j,k).real() << endl;
00220                                 }
00221                         }
00222                 }
00223                 outFile.close();
00224                 return true;
00225         }
00226 }
00227 
00228 template <int rank>
00229 bool QCComplexField<rank>::writeImagToFile(const char* fileName)
00230 {
00231         ofstream outFile(fileName,ios::out);
00232         outFile.precision(filePrecision);
00233 
00234         if(outFile.fail())
00235         {
00236                 cerr << "File Error: " << fileName << endl;
00237                 return false;
00238         }
00239         else
00240         {
00241                 if (outputFormat == CSV)
00242                 {
00243                         for(int k = 0; k < this->Field.cols(); k ++)
00244                         {
00245                                 for(int j = 0; j < this->Field.rows(); j ++)
00246                                         outFile << this->Field(j,k).imag() << " , ";
00247                                 outFile << endl;
00248                         }
00249                 }
00250                 else if (outputFormat == GNU)
00251                 {
00252                         outFile << "#The following is the Imaginary part of the Solution" << endl;
00253                         outFile << "#x values \t\t y values \t\t Magnitude" << endl;
00254                         for(int k = 0; k < this->Field.cols(); k ++)
00255                         {
00256                                 for(int j = 0; j < this->Field.rows(); j ++)
00257                                 {
00258                                         outFile << j << "\t\t" << k << "\t\t" << this->Field(j,k).imag() << endl;
00259                                 }
00260                         }
00261                 }
00262                 outFile.close();
00263                 return true;
00264         }
00265 }
00266 
00267 template <int rank>
00268 bool QCComplexField<rank>::writeNormToFile(const char* fileName)
00269 {
00270         ofstream outFile(fileName,ios::out);
00271         outFile.precision(filePrecision);
00272 
00273         if(outFile.fail())
00274         {
00275                 cerr << "File Error: " << fileName << endl;
00276                 return false;
00277         }
00278         else
00279         {
00280                 if (outputFormat == CSV)
00281                 {
00282                         if(rank == 2)
00283                         {
00284                                 for(int k = 0; k < this->Field.cols(); k ++)
00285                                 {
00286                                         for(int j = 0; j < this->Field.rows(); j ++)
00287                                                 outFile << norm(this->Field(j,k)) << " , ";
00288                                         outFile << endl;
00289                                 }
00290                         }
00291                         else
00292                         {
00293                                 int l = this->Field.cols()/2;
00294                                 for(int k = 0; k < this->Field.cols(); k ++)
00295                                 {
00296                                         for(int j = 0; j < this->Field.rows(); j ++)
00297                                                 outFile << norm(this->Field(j,k,l)) << " , ";
00298                                         outFile << endl;
00299                                 }
00300                         }
00301                 }
00302                 else if (outputFormat == GNU)
00303                 {
00304                         outFile << "#The following is the Norm of the Solution" << endl;
00305                         outFile << "#x values \t\t y values \t\t Magnitude" << endl;
00306                         for(int k = 0; k < this->Field.cols(); k ++)
00307                         {
00308                                 for(int j = 0; j < this->Field.rows(); j ++)
00309                                 {
00310                                         outFile << j << "\t\t" << k << "\t\t" << norm(this->Field(j,k)) << endl;
00311                                 }
00312                         }
00313                 }
00314                 outFile.close();
00315                 return true;
00316         }
00317 }
00318 #endif

Generated on Sat May 13 13:22:48 2006 for Quantum Construct (qC++) by  doxygen 1.4.6-NO