CheMPS2
DMRGSCFmatrix.cpp
1 /*
2  CheMPS2: a spin-adapted implementation of DMRG for ab initio quantum chemistry
3  Copyright (C) 2013-2016 Sebastian Wouters
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 2 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 along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #include <math.h>
21 #include <string>
22 #include <sstream>
23 
24 #include "MyHDF5.h"
25 #include "DMRGSCFmatrix.h"
26 #include "MPIchemps2.h"
27 
28 using std::string;
29 
31 
32  this->iHandler = iHandler;
33  this->num_irreps = iHandler->getNirreps();
34 
35  entries = new double*[ num_irreps ];
36  for ( int irrep = 0; irrep < num_irreps; irrep++ ){
37  const int NORB = iHandler->getNORB( irrep );
38  entries[ irrep ] = new double[ NORB * NORB ];
39  }
40 
41 }
42 
44 
45  for ( int irrep = 0; irrep < num_irreps; irrep++ ){
46  delete [] entries[ irrep ];
47  }
48  delete [] entries;
49 
50 }
51 
52 void CheMPS2::DMRGSCFmatrix::set( const int irrep, const int p, const int q, const double val ){
53 
54  entries[ irrep ][ p + iHandler->getNORB( irrep ) * q ] = val;
55 
56 }
57 
59 
60  for ( int irrep = 0; irrep < num_irreps; irrep++ ){
61  const int size = iHandler->getNORB( irrep ) * iHandler->getNORB( irrep );
62  for ( int counter = 0; counter < size; counter++ ){
63  entries[ irrep ][ counter ] = 0.0;
64  }
65  }
66 
67 }
68 
70 
71  clear();
72  for ( int irrep = 0; irrep < num_irreps; irrep++ ){
73  const int NORB = iHandler->getNORB( irrep );
74  for ( int diag = 0; diag < NORB; diag++ ){
75  entries[ irrep ][ ( NORB + 1 ) * diag ] = 1.0;
76  }
77  }
78 
79 }
80 
81 double CheMPS2::DMRGSCFmatrix::get( const int irrep, const int p, const int q ) const{
82 
83  return entries[ irrep ][ p + iHandler->getNORB( irrep ) * q ];
84 
85 }
86 
87 double * CheMPS2::DMRGSCFmatrix::getBlock( const int irrep ){
88 
89  return entries[ irrep ];
90 
91 }
92 
94 
95  // Should in principle check whether iHandler matches
96  double rms_diff = 0.0;
97  for ( int irrep = 0; irrep < num_irreps; irrep++ ){
98  const int NORB = iHandler->getNORB( irrep );
99  for ( int row = 0; row < NORB; row++ ){
100  for ( int col = 0; col < NORB; col++ ){
101  const double diff = this->get( irrep, row, col ) - other->get( irrep, row, col );
102  rms_diff += diff * diff;
103  }
104  }
105  }
106  rms_diff = sqrt( rms_diff );
107  return rms_diff;
108 
109 }
110 
111 void CheMPS2::DMRGSCFmatrix::write( const string filename, const DMRGSCFindices * idx, double ** storage ){
112 
113  hid_t file_id = H5Fcreate( filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT );
114  hid_t group_id = H5Gcreate( file_id, "/Data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
115 
116  for ( int irrep = 0; irrep < idx->getNirreps(); irrep++ ){
117 
118  std::stringstream irrepname;
119  irrepname << "irrep_" << irrep;
120 
121  hsize_t dimarray = idx->getNORB( irrep ) * idx->getNORB( irrep );
122  hid_t dataspace_id = H5Screate_simple( 1, &dimarray, NULL );
123  hid_t dataset_id = H5Dcreate( group_id, irrepname.str().c_str(), H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT );
124  H5Dwrite( dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, storage[ irrep ] );
125 
126  H5Dclose( dataset_id );
127  H5Sclose( dataspace_id );
128 
129  }
130 
131  H5Gclose( group_id );
132  H5Fclose( file_id );
133 
134 }
135 
136 void CheMPS2::DMRGSCFmatrix::read( const string filename, const int n_irreps, double ** storage ){
137 
138  hid_t file_id = H5Fopen( filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT );
139  hid_t group_id = H5Gopen( file_id, "/Data", H5P_DEFAULT );
140 
141  for ( int irrep = 0; irrep < n_irreps; irrep++ ){
142 
143  std::stringstream irrepname;
144  irrepname << "irrep_" << irrep;
145 
146  hid_t dataset_id = H5Dopen( group_id, irrepname.str().c_str(), H5P_DEFAULT );
147  H5Dread( dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, storage[ irrep ] );
148 
149  H5Dclose( dataset_id );
150 
151  }
152 
153  H5Gclose( group_id );
154  H5Fclose( file_id );
155 
156 }
157 
158 #ifdef CHEMPS2_MPI_COMPILATION
159 void CheMPS2::DMRGSCFmatrix::broadcast( const int ROOT ){
160 
161  for ( int irrep = 0; irrep < num_irreps; irrep++ ){
162  const int NORB = iHandler->getNORB( irrep );
163  const int size = NORB * NORB;
164  if ( size > 0 ){
165  MPIchemps2::broadcast_array_double( entries[ irrep ], size, ROOT );
166  }
167  }
168 
169 }
170 #endif
171 
int getNORB(const int irrep) const
Get the number of orbitals for an irrep.
double * getBlock(const int irrep)
Get a matrix block.
const DMRGSCFindices * iHandler
The information on the occupied, active, and virtual spaces.
Definition: DMRGSCFmatrix.h:94
static void read(const string filename, const int n_irreps, double **storage)
Read the DMRGSCFmatrix from disk.
void set(const int irrep, const int p, const int q, const double val)
Set an element.
double ** entries
The matrix entries.
Definition: DMRGSCFmatrix.h:97
static void write(const string filename, const DMRGSCFindices *idx, double **storage)
Write a DMRGSCFmatrix to disk.
double rms_deviation(const DMRGSCFmatrix *other) const
Get the RMS deviation with another DMRGSCFmatrix.
double get(const int irrep, const int p, const int q) const
Get an element.
DMRGSCFmatrix(const DMRGSCFindices *iHandler)
Constructor.
int num_irreps
The number of irreps.
void identity()
Make this matrix the identity matrix.
void clear()
Clear the matrix.
virtual ~DMRGSCFmatrix()
Destructor.
int getNirreps() const
Get the number of irreps.