File: sparseSolverTest.cpp

package info (click to toggle)
dmrgpp 6.06-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 113,900 kB
  • sloc: cpp: 80,986; perl: 14,772; ansic: 2,923; makefile: 83; sh: 17
file content (151 lines) | stat: -rw-r--r-- 3,645 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
// BEGIN LICENSE BLOCK
/*
Copyright (c) 2009 , UT-Battelle, LLC
All rights reserved

[PsimagLite, Version 1.0.0]

*********************************************************
THE SOFTWARE IS SUPPLIED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED.

Please see full open source license included in file LICENSE.
*********************************************************

*/
// END LICENSE BLOCK

#include "CrsMatrix.h"
#include "DavidsonSolver.h"
#include "LanczosSolver.h"
#include "ParametersForSolver.h"
#include "PsimagLite.h"
#include "Random48.h"

using namespace PsimagLite;

typedef double RealType;
typedef double ComplexOrRealType;

typedef ParametersForSolver<RealType> ParametersForSolverType;
typedef Vector<ComplexOrRealType>::Type VectorType;
typedef CrsMatrix<ComplexOrRealType> SparseMatrixType;
typedef LanczosOrDavidsonBase<ParametersForSolverType, SparseMatrixType, VectorType>
    SparseSolverType;
typedef LanczosSolver<ParametersForSolverType, SparseMatrixType, VectorType>
    LanczosSolverType;
typedef DavidsonSolver<ParametersForSolverType, SparseMatrixType, VectorType>
    DavidsonSolverType;

void usage(const char* progName)
{
	std::cerr
	    << "Usage: " << progName
	    << " -n rank [-x] [-d ] [-c max_columns] [-m max_value] [-r seed]\n";
	exit(1);
}

int main(int argc, char* argv[])
{
	int opt = 0;
	bool useDavidson = false;
	SizeType n = 0;
	RealType maxValue = 0;
	SizeType maxCol = 0;
	SizeType seed = 0;
	bool lotaMemory = true;

	while ((opt = getopt(argc, argv, "n:c:m:r:dx")) != -1) {
		switch (opt) {
		case 'd':
			useDavidson = true;
			break;
		case 'n':
			n = atoi(optarg);
			break;
		case 'c':
			maxCol = atoi(optarg);
			break;
		case 'm':
			maxValue = atof(optarg);
			break;
		case 'r':
			seed = atoi(optarg);
			break;
		case 'x':
			lotaMemory = false;
			break;
		default:
			usage(argv[0]);
			return 1;
		}
	}

	// sanity checks
	if (n == 0)
		usage(argv[0]);
	if (maxCol == 0)
		maxCol = 1 + SizeType(0.1 * n);
	if (PsimagLite::norm(maxValue) < 1e-6)
		maxValue = 1.0;
	if (seed == 0)
		seed = 3443331;

	// create a random matrix:
	Random48<RealType> random(seed);
	SparseMatrixType sparse(n, n);
	Vector<bool>::Type seenThisColumn(n);
	SizeType counter = 0;
	for (SizeType i = 0; i < n; i++) {
		sparse.setRow(i, counter);
		// random vector:
		SizeType x = 1 + SizeType(random() * maxCol);
		for (SizeType j = 0; j < seenThisColumn.size(); j++)
			seenThisColumn[j] = false;
		for (SizeType j = 0; j < x; j++) {
			SizeType col = SizeType(random() * n);
			if (seenThisColumn[col])
				continue;
			seenThisColumn[col] = true;
			ComplexOrRealType val = random() * maxValue;

			sparse.pushValue(val);
			sparse.pushCol(col);
			counter++;
		}
	}
	sparse.setRow(n, counter);
	sparse.checkValidity();
	// symmetrize:
	SparseMatrixType sparse2;
	transposeConjugate(sparse2, sparse);
	sparse += sparse2;
	sparse.checkValidity();
	assert(isHermitian(sparse));

	// sparse solver setup
	ParametersForSolverType params;
	params.lotaMemory = lotaMemory;
	LanczosSolverType lanczosSolver(sparse, params);
	DavidsonSolverType davisonSolver(sparse, params);
	SparseSolverType* solver = 0;

	// select solver
	if (useDavidson)
		solver = &davisonSolver;
	else
		solver = &lanczosSolver;

	// diagonalize matrix
	RealType gsEnergy = 0;
	VectorType gsVector(n);

	VectorType initial(n);
	PsimagLite::fillRandom(initial);
	solver->computeOneState(gsEnergy, gsVector, initial, 0);

	std::cout << "Energy=" << gsEnergy << "\n";
}