hamiltonian_heisenberg.cpp 6.56 KB
Newer Older
1
#include "hamiltonian_heisenberg.h"
2

3
heisenberg_hamiltonian::heisenberg_hamiltonian(int num_spin, bool periodic, double Jx, double Jy, double Jz, double hx, double hy, double hz, double hstx, double hsty, double hstz) :  heisenberg_parameters(periodic, Jx, Jy, Jz, hx, hy, hz, hstx, hsty, hstz) 
4
5
6
7
8
{
	N = num_spin;
	dim = std::pow(2,N);
}

9
10
heisenberg_parameters::heisenberg_parameters()
{
11
12
13
14
	set_periodic(true);
	set_J(1.0, 1.0, 1.0);
	set_h(0.0, 0.0, 0.0);
	set_hstaggered(0.0, 0.0, 0.0);
15
16
17
18
}

heisenberg_parameters::heisenberg_parameters(bool periodic, double Jx, double Jy, double Jz, double hx, double hy, double hz, double hstx, double hsty, double hstz)
{
19
	set_periodic(periodic);
20
21
22
	set_J(Jx, Jy, Jz);
	set_h(hx, hy, hz);
	set_hstaggered(hstx, hsty, hstz);
23
24
}

25
void heisenberg_parameters::set_periodic(double periodic)
26
27
28
29
{
	periodic_bc = periodic;
}

30
void heisenberg_parameters::set_J(double Jx, double Jy, double Jz)
31
32
33
34
35
36
{
	J_x = Jx;
	J_y = Jy;
	J_z = Jz;
}

37
void heisenberg_parameters::set_h(double hx, double hy, double hz)
38
39
40
41
42
43
{
	h_x = hx;
	h_y = hy;
	h_z = hz;
}

44
void heisenberg_parameters::set_hstaggered(double hstx, double hsty, double hstz)
45
46
47
48
49
50
{
	hst_x = hstx;
	hst_y = hsty;
	hst_z = hstz;
}

51
52
53
54
55
56
57
void heisenberg_parameters::dump_values()
{
	std::cout << "J = [ " << J_x << ", " << J_y << ", " << J_z << "]" << std::endl;
	std::cout << "h = [ " << h_x << ", " << h_y << ", " << h_z << "]" << std::endl;
	std::cout << "h_st = [ " << hst_x << ", " << hst_y << ", " << hst_z << "]" << std::endl;
	std::cout << "Periodic = " << periodic_bc << std::endl;
}
58

59
heisenberg_system::heisenberg_system(int num_spin, bool periodic, double Jx, double Jy, double Jz, double hx, double hy, double hz, double hstx, double hsty, double hstz) : heisenberg_hamiltonian(num_spin, periodic, Jx, Jy, Jz, hx, hy, hz, hstx, hsty, hstz) //, spin_system(num_spin)
60
61
62
{
}

63
heisenberg_system::~heisenberg_system()
64
65
66
67
{

}

68
int heisenberg_system::initialize()
69
70
71
72
73
74
75
{
	if(N < 2 || dim < 4)
		throw std::out_of_range("Minimum number of spins is 2.");

	if(hst_y == 0.0 && h_y == 0.0)
	{
		build();
76
		diagonalize_real();
77
78
79
	}
	else
	{
80
81
		build();
		diagonalize_complex();
82
83
84
85
86
	}

	_initialized = true;
	return 0;
}
87

88
int heisenberg_hamiltonian::build()
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
{
	if(N < 1 || dim < 2)
	{
		std::cerr << "Error: dimension of spin system missmatch! Possibly unitialized?" << std::endl;
		std::cerr << "N = " << N << "; dim = " << dim << std::endl;
		return -1;
	}
	// Allocate memory for matrix
	_matrix.resize(dim*dim);

	for(unsigned long k=0;k < dim;k++) //columns
	{
		for(unsigned long l=0;l < dim;l++) //rows
		{
			for(unsigned int m=0; m < N-1;m++)
			{
				
				_matrix.at(k*dim+l) += J_x*S_xS_x(m, m+1, k, l);
				_matrix.at(k*dim+l) += h_x*S_x(m,k,l);
				_matrix.at(k*dim+l) += J_y*S_yS_y(m, m+1, k, l);
				_matrix.at(k*dim+l) += h_y*S_y(m,k,l);
				if(k == l)
				{
					_matrix.at(k*dim+l) += J_z*S_zS_z(m, m+1, k);
					_matrix.at(k*dim+l) += h_z*S_z(m, k);
					// Staggered magnetization
					if(m % 2 == 0)
					{
						_matrix.at(k*dim+l) += hst_z*S_z(m, k);
					}
					else
					{
						_matrix.at(k*dim+l) -= hst_z*S_z(m, k);
					}
				}

			}
			// Magnetization for last spin
			_matrix.at(k*dim+l) += h_x*S_x(N-1, k,l);
			_matrix.at(k*dim+l) += h_y*S_y(N-1, k,l);
			if(k == l)
			{
				_matrix.at(k*dim+l) += h_z*S_z(N-1, k);
				// Staggered magnetization
				if((N - 1) % 2 == 0)
				{
					_matrix.at(k*dim+l) += hst_z*S_z(N-1, k);
				}
				else
				{
					_matrix.at(k*dim+l) -= hst_z*S_z(N-1, k);
				}
			}

			// If Hamiltonian with periodic boundary conditions <==> spin ring
			if(periodic_bc)
			{
				_matrix.at(k*dim+l) += J_x*S_xS_x(N-1, 0, k, l);
				_matrix.at(k*dim+l) += J_y*S_yS_y(N-1, 0, k, l);
				if(k == l)
				{
					_matrix.at(k*dim+l) += J_z*S_zS_z(N-1, 0, k);
				}
			}
		}
	}
155
	_hamiltonian_constructed = true;
156
157
158
159

	return 0;
}

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
heisenberg_hamiltonian_local_field::heisenberg_hamiltonian_local_field(int num_spin, bool periodic, double Jx, double Jy, double Jz) : heisenberg_parameters(periodic, Jx, Jy, Jz, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0) 
{
	N = num_spin;
	dim = std::pow(2,N);

}

heisenberg_local_field_system::heisenberg_local_field_system(int num_spin, bpy::list hz_local, bool periodic, double Jx, double Jy, double Jz) : heisenberg_hamiltonian_local_field(num_spin, periodic, Jx, Jy, Jz)
{
	_hz.resize(N);

	if(bpy::len(hz_local) != N)
	{
		std::cerr << "Error: Mismatch in number of spin sites and components in local field!" << std::endl;
		throw;
	}

	for(unsigned int k = 0; k < bpy::len(hz_local); k++)
	{
		float c = bpy::extract<float>(hz_local[k]);
		_hz.at(k) = c;
		std::cout << "Extracted hz(" << k << ") = " << c << std::endl;
	}
}

int heisenberg_local_field_system::initialize()
{
	if(N < 2 || dim < 4)
		throw std::out_of_range("Minimum number of spins is 2.");

	build();
Hylke Donker's avatar
Hylke Donker committed
191
192
193
	//diagonalize_real(); // Only magnetic field along z-direction, Hamiltonian is purely real!
	build_subsp(); 
	blockwise_diagonalise();
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

	_initialized = true;
	return 0;
}

int heisenberg_hamiltonian_local_field::build()
{
	if(N < 1 || dim < 2)
	{
		std::cerr << "Error: dimension of spin system missmatch! Possibly unitialized?" << std::endl;
		std::cerr << "N = " << N << "; dim = " << dim << std::endl;
		return -1;
	}
	if(_hz.size() != N)
	{
		std::cerr << "Error: Number of local magnetization sites do not equal the number of particles!" << std::endl;
		return -1;
	}
	// Allocate memory for matrix
	_matrix.resize(dim*dim);

	for(unsigned long k=0;k < dim;k++) //columns
	{
		for(unsigned long l=0;l < dim;l++) //rows
		{
			for(unsigned int m=0; m < N-1;m++)
			{
				
				_matrix.at(k*dim+l) += J_x*S_xS_x(m, m+1, k, l);
				_matrix.at(k*dim+l) += J_y*S_yS_y(m, m+1, k, l);
				if(k == l)
				{
					_matrix.at(k*dim+l) += J_z*S_zS_z(m, m+1, k);
					_matrix.at(k*dim+l) += _hz[m]*S_z(m, k);
				}

			}

			if(k == l)
			{
				_matrix.at(k*dim+l) += _hz[N-1]*S_z(N-1, k);
			}

			// If Hamiltonian with periodic boundary conditions <==> spin ring
			if(periodic_bc)
			{
				_matrix.at(k*dim+l) += J_x*S_xS_x(N-1, 0, k, l);
				_matrix.at(k*dim+l) += J_y*S_yS_y(N-1, 0, k, l);
				if(k == l)
				{
					_matrix.at(k*dim+l) += J_z*S_zS_z(N-1, 0, k);
				}
			}
		}
	}
249
	_hamiltonian_constructed = true;
250
251
252

	return 0;
}
253

254
heisenberg_system_subsp::heisenberg_system_subsp(int num_spin,bool periodic, double Jx, double Jy, double Jz, double hz) : heisenberg_hamiltonian(num_spin, periodic, Jx, Jy, Jz, 0.0, 0.0, hz, 0.0, 0.0, 0.0) //, spin_system(num_spin)
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
{
}

heisenberg_system_subsp::~heisenberg_system_subsp()
{

}

int heisenberg_system_subsp::initialize()
{
	if(N < 2 || dim < 4)
		throw std::out_of_range("Minimum number of spins is 2.");

	build();	
	build_subsp();
270
	blockwise_diagonalise();
271
272
273
274
275

	_initialized = true;
	return 0;
}