File: test_lan.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 (167 lines) | stat: -rw-r--r-- 4,417 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
/* 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;

typedef MatrixType::VectorType VectorType;

int print_matrix = 0;


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(int argc, char *argv[])
  try
    {

      if(argc != 2)
	{
	  printf("Usage: %s filename\n", argv[0]);
	  return EXIT_FAILURE;
	}


      // READ THE MATRIX
      char *filename = argv[1];

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

 
      MatrixWrapperErgo A(M);

      int num_eig = 1;
      vector<real> eigVal(num_eig);
      vector<VectorType> eigVec(num_eig);
      real TOL = 1e-12;
      bool use_vector_as_guess = false; 
      vector<int> num_iter(num_eig);
      int maxit = 200;

      printf("Starting Lanczos method...\n");

      MatrixWrapperErgo::lanczos_method(A, eigVal, eigVec, num_eig, TOL, use_vector_as_guess, num_iter, maxit);

      printf("Lanczos method finished...\n");
      printf("Number of iterations (first) is %d\n", num_iter[0]);


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