File: ReflectionColor.h

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 (386 lines) | stat: -rw-r--r-- 11,086 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
Copyright (c) 2009, UT-Battelle, LLC
All rights reserved

[DMRG++, Version 2.0.0]
[by G.A., Oak Ridge National Laboratory]

UT Battelle Open Source Software License 11242008

OPEN SOURCE LICENSE

Subject to the conditions of this License, each
contributor to this software hereby grants, free of
charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), a
perpetual, worldwide, non-exclusive, no-charge,
royalty-free, irrevocable copyright license to use, copy,
modify, merge, publish, distribute, and/or sublicense
copies of the Software.

1. Redistributions of Software must retain the above
copyright and license notices, this list of conditions,
and the following disclaimer.  Changes or modifications
to, or derivative works of, the Software should be noted
with comments and the contributor and organization's
name.

2. Neither the names of UT-Battelle, LLC or the
Department of Energy nor the names of the Software
contributors may be used to endorse or promote products
derived from this software without specific prior written
permission of UT-Battelle.

3. The software and the end-user documentation included
with the redistribution, with or without modification,
must include the following acknowledgment:

"This product includes software produced by UT-Battelle,
LLC under Contract No. DE-AC05-00OR22725  with the
Department of Energy."

*********************************************************
DISCLAIMER

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. IN NO EVENT SHALL THE
COPYRIGHT OWNER, CONTRIBUTORS, UNITED STATES GOVERNMENT,
OR THE UNITED STATES DEPARTMENT OF ENERGY BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

NEITHER THE UNITED STATES GOVERNMENT, NOR THE UNITED
STATES DEPARTMENT OF ENERGY, NOR THE COPYRIGHT OWNER, NOR
ANY OF THEIR EMPLOYEES, REPRESENTS THAT THE USE OF ANY
INFORMATION, DATA, APPARATUS, PRODUCT, OR PROCESS
DISCLOSED WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS.

*********************************************************


*/

/** \ingroup DMRG */
/*@{*/

/*! \file ReflectionColor
 *
 *  Critical problems:
 *
 *  - getting a l.i. set in an efficient way
 *  - support for fermionic reflections
 *
 *  Low priority work that needs to be done:
 *
 *  - support for bases that change from site to site
 *
 */
#ifndef REFLECTION_COLOR_H
#define REFLECTION_COLOR_H

#include "LAPACK.h"
#include "Matrix.h"
#include "PackIndices.h" // in PsimagLite
#include "ProgressIndicator.h"
#include "Sort.h"
#include "SparseVector.h"

namespace Dmrg
{

// FIXME: MOVE ELSEWHERE:
template <typename RealType>
bool isAlmostZero(const RealType& x, RealType eps = 1e-20)
{
	return (fabs(x) < eps);
}

// FIXME: MOVE ELSEWHERE:
template <typename RealType>
bool isAlmostZero(const std::complex<RealType>& x, RealType eps = 1e-20)
{
	return (fabs(real(x) * real(x) + imag(x) * imag(x)) < eps);
}

template <typename RealType, typename SparseMatrixType>
class ReflectionColor
{

	typedef PsimagLite::PackIndices PackIndicesType;
	typedef typename SparseMatrixType::value_type ComplexOrRealType;
	typedef typename PsimagLite::Vector<ComplexOrRealType>::Type VectorType;
	typedef SparseVector<typename VectorType::value_type> SparseVectorType;

	enum { AVAILABLE,
		NOT_AVAILABLE,
		COLOR };

public:

	ReflectionColor(const SparseMatrixType& reflection, bool idebug)
	    : reflection_(reflection)
	    , idebug_(idebug)
	{
		//		printFullMatrix(reflection,"reflection");
		findIsolated();

		printInfo();
		//		std::cerr<<"Isolated nodes="<<ipIsolated_.size()<<"\n";
		//		std::cerr<<"Connected nodes="<<ipConnected_.size()<<"\n";

		SizeType firstColor = 2;
		// SparseMatrixType A;
		// generateAmatrix(A,ipConnected_);
		SizeType ncolor = gencolorLabel(reflection, firstColor);

		//		for (SizeType i=0ilabel(ip_isolated) = 0;

		//		% print statistics
		//		% ----------------
		printInfo2(ncolor, firstColor);

		// gencolorCheck(ilabel_,ncolor);

		// gencolorPerm(ilabel_,firstColor,ncolor);

		for (SizeType i = 0; i < ipIsolated_.size(); i++)
			ilabel_[ipIsolated_[i]] = AVAILABLE;

		for (SizeType i = 0; i < ilabel_.size(); i++)
			if (ilabel_[i] == firstColor)
				ipcolor_.push_back(i);
	}

	const typename PsimagLite::Vector<SizeType>::Type& ipcolor() const { return ipcolor_; }

	const typename PsimagLite::Vector<SizeType>::Type& isolated() const { return ipIsolated_; }

private:

	void printInfo() const
	{
		if (!idebug_)
			return;
		std::cout << "ipIsolated ";
		for (SizeType i = 0; i < ipIsolated_.size(); i++)
			std::cout << ipIsolated_[i] << " ";
		std::cout << "\n";
		for (SizeType i = 0; i < ipConnected_.size(); i++)
			std::cout << ipConnected_[i] << " ";
		std::cout << "\n";
	}

	void printInfo2(SizeType ncolor, SizeType firstColor) const
	{
		if (!idebug_)
			return;
		std::cout << "ncolor=" << ncolor << "\n";
		for (SizeType icolor = firstColor; icolor <= ncolor; icolor++) {
			typename PsimagLite::Vector<SizeType>::Type ilist;
			findWithLabel(ilist, ilabel_, icolor);
			std::cout << "color=" << icolor << " size=" << ilist.size() << "\n";
		}
	}

	void generateAmatrix(SparseMatrixType& A, const typename PsimagLite::Vector<SizeType>::Type& ipConnected) const
	{
		A.resize(ipConnected.size());
		SizeType counter = 0;
		typename PsimagLite::Vector<int>::Type ipConnectedInverse(reflection_.rank(), -1);
		for (SizeType i = 0; i < ipConnected.size(); i++)
			ipConnectedInverse[ipConnected[i]] = i;

		for (SizeType i = 0; i < ipConnected.size(); i++) {
			A.setRow(i, counter);
			SizeType ii = ipConnected[i];
			for (int k = reflection_.getRowPtr(ii); k < reflection_.getRowPtr(ii + 1); k++) {
				ComplexOrRealType val = reflection_.getValue(k);
				int col = ipConnectedInverse[reflection_.getCol(k)];
				if (col < 0)
					continue;
				A.pushCol(col);
				A.pushValue(val);
				counter++;
			}
		}
		A.setRow(A.rank(), counter);
		A.checkValidity();
	}

	void findIsolated()
	{
		for (SizeType i = 0; i < reflection_.rank(); i++) {
			SizeType nz = 0;
			bool hasDiagonal = false;
			for (int k = reflection_.getRowPtr(i); k < reflection_.getRowPtr(i + 1); k++) {
				ComplexOrRealType val = reflection_.getValue(k);
				SizeType col = reflection_.getCol(k);
				if (i == col) {
					val = val + 1.0;
					hasDiagonal = true;
				}
				if (isAlmostZero(val, 1e-4))
					continue;
				nz++;
			}
			if (!hasDiagonal)
				nz++;
			if (nz == 1)
				ipIsolated_.push_back(i);
			else
				ipConnected_.push_back(i);
		}
	}

	SizeType gencolorLabel(const SparseMatrixType& A, SizeType firstColor)
	{
		ilabel_.assign(A.rank(), AVAILABLE);

		SizeType ncolor = firstColor - 1;
		typename PsimagLite::Vector<SizeType>::Type ilist;
		RealType eps = 1e-3;

		// while (any( ilabel == unlabeled))
		for (SizeType ii = 0; ii < ilabel_.size(); ii++) {
			if (ilabel_[ii] != AVAILABLE)
				continue;
			ncolor++;
			SizeType icolor = ncolor;
			ilist.clear();
			findWithLabel(ilist, ilabel_, AVAILABLE);
			SizeType ifound = 0;
			for (SizeType i = 0; i < ilist.size(); i++) {
				//				if (ifound >= maxsize) break;

				SizeType ni = ilist[i];
				if (ilabel_[ni] == AVAILABLE) {
					ilabel_[ni] = icolor;
					ifound++;
					// --------------
					// mark neighbors
					// --------------
					typename PsimagLite::Vector<SizeType>::Type jlist;
					findConnected(jlist, ni, A, eps);
					//					jlist = find( A(ni,:) );
					for (SizeType j = 0; j < jlist.size(); j++) {
						SizeType nj = jlist[j];
						if (ilabel_[nj] == AVAILABLE) {
							ilabel_[nj] = NOT_AVAILABLE;
						}
					}
				}
			}
			ilist.clear();
			findWithLabel(ilist, ilabel_, NOT_AVAILABLE);
			//			ilist = find( ilabel == isseen);
			for (SizeType jj = 0; jj < ilist.size(); jj++) {
				ilabel_[ilist[jj]] = AVAILABLE;
			}
			//			if (ilist.size() >= 1) {
			//			   ilabel(ilist) = unlabeled;
			//			}
		}
		return ncolor;
	}

	void findConnected(typename PsimagLite::Vector<SizeType>::Type& jlist,
	    SizeType ni,
	    const SparseMatrixType& A,
	    const RealType& eps) const
	{
		bool hasDiagonal = false;
		for (int k = A.getRowPtr(ni); k < A.getRowPtr(ni + 1); k++) {
			ComplexOrRealType val = A.getValue(k);
			SizeType col = A.getCol(k);
			if (ni == col) {
				hasDiagonal = true;
				val += 1.0;
			}
			if (isAlmostZero(val, eps))
				continue;
			jlist.push_back(col);
		}
		if (!hasDiagonal)
			jlist.push_back(ni);
	}

	void gencolorCheck(const typename PsimagLite::Vector<SizeType>::Type& ilabel, SizeType ncolor) const
	{
		// ------------
		// RealType check
		// ------------
		typename PsimagLite::Vector<SizeType>::Type ilist;
		findWithLabel(ilist, ilabel, ncolor);
		//		isok = max( ilabel == ncolor);
		SizeType isok = *std::max(ilist.begin(), ilist.end());
		if (isok == 0) {
			PsimagLite::String s = "ncolor " + ttos(ncolor) + " max(ilabel) " + ttos(isok) + "\n";
			// throw PsimagLite::RuntimeError(s.c_str());
		}
	}

	void findWithLabel(typename PsimagLite::Vector<SizeType>::Type& ilist,
	    const typename PsimagLite::Vector<SizeType>::Type& ilabel,
	    SizeType icolor) const
	{
		for (SizeType i = 0; i < ilabel.size(); i++)
			if (ilabel[i] == icolor)
				ilist.push_back(i);
	}

	//	void gencolorPerm(const typename PsimagLite::Vector<SizeType>::Type& ilabel,
	//			  SizeType firstColor,
	//			  SizeType ncolor)
	//	{
	//		// find size:
	//		typename PsimagLite::Vector<SizeType>::Type ilist;
	//		SizeType ip = 0;
	//		iperm_.resize(reflection_.rank());

	//		for (SizeType icolor=firstColor;icolor<=ncolor;icolor++) {
	//			//			ilist = find( ilabel == icolor);
	//			ilist.clear();
	//			findWithLabel(ilist,ilabel,icolor);
	//			SizeType nc = ilist.size();
	//			//			nc = length(ilist);
	//			for (SizeType j=ip;j<ip+nc;j++) iperm_[j]=ilist[j-ip];

	//			// omitting check
	//			ip += nc;
	//		}
	//		if (!idebug_) return;

	//		//		% ----------------
	//		//		% print statistics
	//		//		% ----------------
	//		std::cout<<"ncolor="<<ncolor<<"\n";
	//		for (SizeType icolor=firstColor;icolor<ncolor;icolor++) {
	//			ilist.clear();
	//			findWithLabel(ilist,ilabel,icolor);
	//			std::cout<<"color="<<icolor<<" size="<<ilist.size()<<"\n";
	//		}

	//	}

	const SparseMatrixType& reflection_;
	bool idebug_;
	typename PsimagLite::Vector<SizeType>::Type ipIsolated_, ipConnected_;
	typename PsimagLite::Vector<SizeType>::Type ilabel_;
	//	typename PsimagLite::Vector<SizeType>::Type iperm_;
	typename PsimagLite::Vector<SizeType>::Type ipcolor_;
}; // class ReflectionColor

} // namespace Dmrg

/*@}*/
#endif // REFLECTION_COLOR_H