File: test.cc

package info (click to toggle)
ergo 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 17,044 kB
  • ctags: 6,813
  • sloc: cpp: 91,488; ansic: 15,728; sh: 6,416; makefile: 1,287; yacc: 123; lex: 108
file content (244 lines) | stat: -rw-r--r-- 6,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Ergo, version 3.5, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2016 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
 * with Linearly Scaling Computational Time and Memory Usage,
 * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
 * J. Chem. Theory Comput. 7, 340 (2011),
 * <http://dx.doi.org/10.1021/ct100611z>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */
#include "matrix_typedefs.h" // definitions of matrix types and interval type (source)
#include "realtype.h"   // definitions of types (utilities_basic)
#include "matrix_utilities.h"
#include "integral_matrix_wrappers.h"
#include "SizesAndBlocks.h"
#include "Matrix.h"
#include "Vector.h"
#include "MatrixSymmetric.h"
#include "MatrixTriangular.h"
#include "MatrixGeneral.h"
#include "VectorGeneral.h"
#include "output.h"

#include "files_dense.h"
#include "files_sparse.h"

#include <iostream>
#include <fstream>
#include <string.h>

#include "ErgoMatrix.h"

using namespace std;

typedef ergo_real real;
typedef symmMatrix MatrixType;

typedef ErgoMatrix<MatrixType> MatrixWrapperErgo;


int print_matrix = 0;
real TOL_SCALAR = 1e-12;
real TOL_EQUAL_MAT  = 1e-8;


int get_matrix_from_sparse(char *filename, MatrixType &X)
{
  vector<int> I, J;
  vector<ergo_real> val;
  int N, M;
  if(read_matrix_from_mtx(filename, I, J, val, N, M) == -1) 
    return -1;
    
  assert(N==M);

  /********** Initialization of SizesAndBlocks */
  int size = N;
  int nlevels = 5; //!!!
  std::vector<int> blockSizes(nlevels);
  blockSizes[nlevels - 1] = 1; // should always be one
    for (int ind = nlevels - 2; ind >= 0; ind--)
      blockSizes[ind] = blockSizes[ind + 1] * 10;
  mat::SizesAndBlocks rows(blockSizes, size);
  mat::SizesAndBlocks cols(blockSizes, size);
  /********************************************/  
  X.resetSizesAndBlocks(rows,cols);
  assert(X.get_nrows()*X.get_ncols() == N*N);

  X.assign_from_sparse(I, J, val);

  return 0;
}


void print_elements(MatrixType &X)
{
  int sizeX = X.get_nrows();
  cout << "rows of X = " << sizeX << endl;

  std::vector<int> row(sizeX*sizeX);
  std::vector<int> col(sizeX*sizeX);
  std::vector<real> val(sizeX*sizeX);

  X.get_all_values(row, col, val);
  assert(row.size() == col.size());

  std::vector<double> matrix(sizeX*sizeX);

  for(int i = 0; i < (int)row.size(); ++i )
    {
      matrix[row[i]*sizeX + col[i]] = val[i];
      matrix[col[i]*sizeX + row[i]] = val[i];
    }

  for (int i = 0; i < sizeX*sizeX; i++)
    {
      if(i%sizeX == 0 &&  i != 0) cout << endl;
      cout << matrix[i]<< " ";
    }
  cout << endl;
}




int main()
try{
  // READ THE MATRIX
  char filename[] = "test_files/F_1.mtx";

  MatrixType M;
  if( get_matrix_from_sparse(filename, M) == -1 )
    { 
      printf("Cannot read matrix from the file...\n");
      return EXIT_FAILURE;
    }

  if(print_matrix == 1)
    print_elements(M);

  // get rows and columns
  int n, m;
  m = M.get_ncols();
  n = M.get_nrows();
  assert(n == m);


  cout << "=================================" << endl;  
  cout << "Check constructors..." << endl;

  {
  MatrixWrapperErgo A;
  MatrixWrapperErgo B(M);
  MatrixWrapperErgo C(B);

  mat::SizesAndBlocks rows;
  mat::SizesAndBlocks cols;
  M.getRows(rows); 
  M.getCols(cols); 

  A.read_from_mtx(filename, rows, cols); // read M

  cout << "=================================" << endl;  
  cout << "Check eucledian norm of the difference..." << endl;
  real eucl = ErgoMatrix<MatrixType>::eucl_diff(A, B, 1e-12);
  assert(eucl < TOL_EQUAL_MAT);

  // check transfer
  MatrixWrapperErgo D(A);
  B.transfer_from(D); // here D is empty
  eucl = ErgoMatrix<MatrixType>::eucl_diff(A, B, 1e-12);
  assert(eucl < TOL_EQUAL_MAT);
  }


  MatrixWrapperErgo A(M);


  cout << "=================================" << endl;  
  cout << "Check gershgorin bounds..." << endl;

  real eigmin, eigmax;
  A.gershgorin(eigmin, eigmax);
  assert(abs(eigmin - (-17.120054726093)) < TOL_SCALAR);
  assert(abs(eigmax - 5.181705707709) < TOL_SCALAR);

  cout << "=================================" << endl;  
  cout << "Check square of the matrix..." << endl;
  {
    MatrixWrapperErgo B;
    A.square(B);
    MatrixType Tmp, Tmp2;
    A.get_matrix(Tmp);
    Tmp2 = (real)1.0*Tmp*Tmp;
    MatrixWrapperErgo C(Tmp2);
    real eucl_tmp = MatrixWrapperErgo::eucl_diff(C, B, 1e-12);
    assert(eucl_tmp < TOL_EQUAL_MAT);
  }

  {
    MatrixWrapperErgo B(A);
    cout << "=================================" << endl;  
    cout << "Check frobenius norm of the difference..." << endl;

    real frob = MatrixWrapperErgo::frob_diff(A, B);
    assert(frob < TOL_EQUAL_MAT);

    cout << "=================================" << endl;  
    cout << "Check mixed norm of the difference..." << endl;

    real mixed = MatrixWrapperErgo::mixed_diff(A, B, 1e-12);
    assert(mixed < TOL_EQUAL_MAT);

    cout << "=================================" << endl;  
    cout << "Check non-symm multiplication with forcing symmetry" << endl;

    MatrixWrapperErgo B2(A);
    B2.mult_force_symm(B);

    MatrixWrapperErgo C;
    A.square(C);

    real eucl_tmp = MatrixWrapperErgo::eucl_diff(B2, C, 1e-12);
    assert(eucl_tmp < TOL_EQUAL_MAT);
    
  }


  cout << "=================================" << endl;  
  cout << "Check truncation..." << endl;

  real th = A.thresh(0.0001, mat::euclNorm);
  cout << "Truncation return value th = " << th << endl;


  cout << "OK!" << endl;

  return 0;
}
 catch(char const * e)
   {
     std::cout << e << std::endl;
   }
 catch(std::exception &e)
   {
     std::cout << e.what() << std::endl;
   }