ergo
filematrix.h
Go to the documentation of this file.
1 /* Ergo, version 3.4, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2014 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
28 #ifndef FILE_MATRIX_HEADER
29 #define FILE_MATRIX_HEADER
30 
31 #include "matrix_typedefs.h"
32 #include "realtype.h"
33 
34 #include <exception>
35 #include "Failure.h"
36 
37 template<typename Treal, typename TMatrixType>
38  class FileMatrix
39 {
40  public:
41  FileMatrix();
42  ~FileMatrix();
43  TMatrixType M;
44  void AssignFileName(const char* fileName);
45  void ReadFromFile();
46  void WriteToFile();
47  private:
48  char* FileName;
49 };
50 
51 template<typename Treal, typename TMatrixType>
53 {
54  FileName = NULL;
55 }
56 
57 template<typename Treal, typename TMatrixType>
59 {
60  if(FileName != NULL)
61  {
62  // Remove file if it exists
63  unlink(FileName);
64  // free memory used for file name string.
65  delete FileName;
66  FileName = NULL;
67  }
68 }
69 
70 template<typename Treal, typename TMatrixType>
72 {
73  if(fileName != NULL)
74  {
75  FileName = new char[strlen(fileName)+1];
76  strcpy(FileName, fileName);
77  }
78  else
79  throw "Error in FileMatrix::AssignFileName: fileName == NULL";
80 }
81 
82 template<typename Treal, typename TMatrixType>
84 {
85  if(FileName != NULL)
86  {
87  int noOfBytes = 0;
88  M.write_to_buffer_count(noOfBytes);
89  Treal* buffer = (ergo_real*)new char[noOfBytes];
90  M.write_to_buffer(buffer, noOfBytes);
91  FILE* f = fopen(FileName, "wb");
92  if(f == NULL)
93  throw "error in FileMatrix::WriteToFile, in fopen";
94  if(fwrite(buffer, sizeof(char), noOfBytes, f) != (unsigned int)noOfBytes)
95  throw "error in FileMatrix::WriteToFile, in fwrite";
96  fclose(f);
97  delete buffer;
98  // Free memory used by matrix.
99  M.clear();
100  }
101 }
102 
103 template<typename Treal, typename TMatrixType>
105 {
106  if(FileName != NULL)
107  {
108  // open file
109  FILE* f = fopen(FileName, "rb");
110  if(f == NULL)
111  throw "error in FileMatrix::ReadFromFile, in fopen";
112  // get file size
113  fseek(f, 0L, SEEK_END);
114  int fileSize = ftell(f);
115  fseek(f, 0L, SEEK_SET);
116  if(fileSize <= 0)
117  throw "error in FileMatrix::ReadFromFile, (fileSize <= 0)";
118  // allocate buffer
119  char* buffer = new char[fileSize];
120  // read file
121  if(fread(buffer, sizeof(char), fileSize, f) != (unsigned int)fileSize)
122  throw "error in FileMatrix::ReadFromFile, in fread";
123  // close file
124  fclose(f);
125  // Create matrix
126  M.read_from_buffer(buffer, fileSize);
127  delete buffer;
128  }
129 }
130 
131 
132 #endif
133 
double ergo_real
Definition: realtype.h:53
~FileMatrix()
Definition: filematrix.h:58
Definition: filematrix.h:38
char * FileName
Definition: filematrix.h:48
Header file with typedefs for matrix and vector types.
TMatrixType M
Definition: filematrix.h:43
void ReadFromFile()
Definition: filematrix.h:104
void AssignFileName(const char *fileName)
Definition: filematrix.h:71
FileMatrix()
Definition: filematrix.h:52
void WriteToFile()
Definition: filematrix.h:83