filematrix.h

Go to the documentation of this file.
00001 /* Ergo, version 3.2, a program for linear scaling electronic structure
00002  * calculations.
00003  * Copyright (C) 2012 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
00004  * 
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  * 
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  * 
00018  * Primary academic reference:
00019  * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
00020  * with Linearly Scaling Computational Time and Memory Usage,
00021  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
00022  * J. Chem. Theory Comput. 7, 340 (2011),
00023  * <http://dx.doi.org/10.1021/ct100611z>
00024  * 
00025  * For further information about Ergo, see <http://www.ergoscf.org>.
00026  */
00027 
00028 #ifndef FILE_MATRIX_HEADER
00029 #define FILE_MATRIX_HEADER
00030 
00031 #include "matrix_typedefs.h"
00032 #include "realtype.h"
00033 
00034 #include <exception>
00035 #include "Failure.h"
00036 
00037 template<typename Treal, typename TMatrixType>
00038   class FileMatrix 
00039 {
00040  public:
00041   FileMatrix();
00042   ~FileMatrix();
00043   TMatrixType M;
00044   void AssignFileName(const char* fileName);
00045   void ReadFromFile();
00046   void WriteToFile();
00047  private:
00048   char* FileName;
00049 };
00050 
00051 template<typename Treal, typename TMatrixType>
00052   FileMatrix<Treal, TMatrixType>::FileMatrix()
00053 {
00054   FileName = NULL;
00055 }
00056 
00057 template<typename Treal, typename TMatrixType>
00058   FileMatrix<Treal, TMatrixType>::~FileMatrix()
00059 {
00060   if(FileName != NULL)
00061     {
00062       // Remove file if it exists
00063       unlink(FileName);
00064       // free memory used for file name string.
00065       delete FileName;
00066       FileName = NULL;
00067     }
00068 }
00069 
00070 template<typename Treal, typename TMatrixType>
00071   void FileMatrix<Treal, TMatrixType>::AssignFileName(const char* fileName)
00072 {
00073   if(fileName != NULL)
00074     {
00075       FileName = new char[strlen(fileName)+1];
00076       strcpy(FileName, fileName);
00077     }
00078   else
00079     throw "Error in FileMatrix::AssignFileName: fileName == NULL";
00080 }
00081 
00082 template<typename Treal, typename TMatrixType>
00083   void FileMatrix<Treal, TMatrixType>::WriteToFile()
00084 {
00085   if(FileName != NULL)
00086     {
00087       int noOfBytes = 0;
00088       M.write_to_buffer_count(noOfBytes);
00089       Treal* buffer = (ergo_real*)new char[noOfBytes];
00090       M.write_to_buffer(buffer, noOfBytes);
00091       FILE* f = fopen(FileName, "wb");
00092       if(f == NULL)
00093         throw "error in FileMatrix::WriteToFile, in fopen";
00094       if(fwrite(buffer, sizeof(char), noOfBytes, f) != (unsigned int)noOfBytes)
00095         throw "error in FileMatrix::WriteToFile, in fwrite";
00096       fclose(f);
00097       delete buffer;
00098       // Free memory used by matrix.
00099       M.clear();
00100     }
00101 }
00102 
00103 template<typename Treal, typename TMatrixType>
00104   void FileMatrix<Treal, TMatrixType>::ReadFromFile()
00105 {
00106   if(FileName != NULL)
00107     {
00108       // open file
00109       FILE* f = fopen(FileName, "rb");
00110       if(f == NULL)
00111         throw "error in FileMatrix::ReadFromFile, in fopen";
00112       // get file size
00113       fseek(f, 0L, SEEK_END);
00114       int fileSize = ftell(f);
00115       fseek(f, 0L, SEEK_SET);
00116       if(fileSize <= 0)
00117         throw "error in FileMatrix::ReadFromFile, (fileSize <= 0)";
00118       // allocate buffer
00119       char* buffer = new char[fileSize];
00120       // read file
00121       if(fread(buffer, sizeof(char), fileSize, f) != (unsigned int)fileSize)
00122         throw "error in FileMatrix::ReadFromFile, in fread";
00123       // close file
00124       fclose(f);
00125       // Create matrix
00126       M.read_from_buffer(buffer, fileSize);
00127       delete buffer;
00128     }
00129 }
00130 
00131 
00132 #endif
00133 

Generated on Wed Nov 21 09:32:39 2012 for ergo by  doxygen 1.4.7