-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_suite.c
More file actions
544 lines (449 loc) · 16 KB
/
test_suite.c
File metadata and controls
544 lines (449 loc) · 16 KB
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include "pcg_random.h"
// Test functions
void test_pcg_basic(void);
void test_pcg_distribution(void);
void test_pcg_reproducibility(void);
void test_ising_physics(void);
void test_binder_cumulant(void);
void test_fss_multiple_sizes(void);
void test_ising_3d_physics(void);
void test_binder_cumulant_3d(void);
int run_mini_ising1d(int N, double T, int steps);
double run_mini_ising2d(int N, double T, int steps);
double run_mini_ising3d(int N, double T, int steps);
double calculate_binder_cumulant(int N, double T, int steps);
double calculate_binder_cumulant_3d(int N, double T, int steps);
int main(void) {
printf("=== Ising Model Test Suite ===\n\n");
printf("1. Testing PCG Random Number Generator...\n");
test_pcg_basic();
test_pcg_distribution();
test_pcg_reproducibility();
printf(" ✓ PCG tests passed\n\n");
printf("2. Testing Ising Model Physics...\n");
test_ising_physics();
printf(" ✓ Physics tests passed\n\n");
printf("3. Testing Binder Cumulant Calculation...\n");
test_binder_cumulant();
printf(" ✓ Binder cumulant tests passed\n\n");
printf("4. Testing Finite-Size Scaling...\n");
test_fss_multiple_sizes();
printf(" ✓ FSS tests passed\n\n");
printf("5. Testing 3D Ising Model Physics...\n");
test_ising_3d_physics();
printf(" ✓ 3D Physics tests passed\n\n");
printf("6. Testing 3D Binder Cumulant...\n");
test_binder_cumulant_3d();
printf(" ✓ 3D Binder cumulant tests passed\n\n");
printf("=== All Tests Passed! ===\n");
return 0;
}
void test_pcg_basic(void) {
init_rnd(42);
// Test range [0, 1)
for (int i = 0; i < 1000; i++) {
double r = drnd();
assert(r >= 0.0 && r < 1.0);
}
// Test non-zero values
int non_zero_count = 0;
for (int i = 0; i < 100; i++) {
if (drnd() > 0.0) non_zero_count++;
}
assert(non_zero_count > 50); // Should have many non-zero values
}
void test_pcg_distribution(void) {
init_rnd(12345);
double sum = 0.0;
int n = 10000;
for (int i = 0; i < n; i++) {
sum += drnd();
}
double mean = sum / n;
// Mean should be approximately 0.5
assert(fabs(mean - 0.5) < 0.1);
printf(" - Distribution mean: %.3f (expected ~0.5)\n", mean);
}
void test_pcg_reproducibility(void) {
// Test 1: Same seed should give same sequence
init_rnd(999);
double seq1[10];
for (int i = 0; i < 10; i++) {
seq1[i] = drnd();
}
init_rnd(999);
double seq2[10];
for (int i = 0; i < 10; i++) {
seq2[i] = drnd();
}
for (int i = 0; i < 10; i++) {
assert(seq1[i] == seq2[i]);
}
printf(" - Reproducibility: ✓\n");
}
void test_ising_physics(void) {
// Test 1: High temperature should have low magnetization
double mag_high_T = run_mini_ising1d(50, 5.0, 10000);
printf(" - Magnetization at T=5.0: %.3f\n", mag_high_T);
assert(mag_high_T < 0.3); // Should be small at high T
// Test 2: Low temperature should have high magnetization
double mag_low_T = run_mini_ising1d(50, 0.1, 10000);
printf(" - Magnetization at T=0.1: %.3f\n", mag_low_T);
assert(mag_low_T > 0.8); // Should be large at low T
// Test 3: 2D model basic functionality
double mag_2d = run_mini_ising2d(10, 1.0, 1000);
printf(" - 2D Magnetization at T=1.0: %.3f\n", mag_2d);
assert(mag_2d >= 0.0 && mag_2d <= 1.0);
}
// Simplified 1D Ising simulation for testing
int run_mini_ising1d(int N, double T, int steps) {
int *spins = malloc(N * sizeof(int));
init_rnd(123);
// Initialize random spins
for (int i = 0; i < N; i++) {
spins[i] = (drnd() < 0.5) ? 1 : -1;
}
// Monte Carlo steps
for (int step = 0; step < steps; step++) {
int i = (int)(drnd() * N);
int left = (i - 1 + N) % N;
int right = (i + 1) % N;
double dE = 2 * spins[i] * (spins[left] + spins[right]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i] = -spins[i];
}
}
// Calculate magnetization
int sum = 0;
for (int i = 0; i < N; i++) {
sum += spins[i];
}
double mag = fabs((double)sum / N);
free(spins);
return mag;
}
// Simplified 2D Ising simulation for testing
double run_mini_ising2d(int N, double T, int steps) {
int **spins = malloc(N * sizeof(int*));
for (int i = 0; i < N; i++) {
spins[i] = malloc(N * sizeof(int));
}
init_rnd(456);
// Initialize random spins
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
spins[i][j] = (drnd() < 0.5) ? 1 : -1;
}
}
// Monte Carlo steps
for (int step = 0; step < steps; step++) {
int i = (int)(drnd() * N);
int j = (int)(drnd() * N);
int up = (i - 1 + N) % N;
int down = (i + 1) % N;
int left = (j - 1 + N) % N;
int right = (j + 1) % N;
double dE = 2 * spins[i][j] * (spins[up][j] + spins[down][j] +
spins[i][left] + spins[i][right]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i][j] = -spins[i][j];
}
}
// Calculate magnetization
int sum = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += spins[i][j];
}
}
double mag = fabs((double)sum / (N * N));
// Free memory
for (int i = 0; i < N; i++) {
free(spins[i]);
}
free(spins);
return mag;
}
// Test Binder cumulant calculation
void test_binder_cumulant(void) {
// Test at high temperature (disordered phase)
double U_high = calculate_binder_cumulant(10, 5.0, 5000);
printf(" - Binder cumulant at T=5.0: %.3f\n", U_high);
// At high T, should be close to 0 (fully disordered)
assert(U_high >= -0.5 && U_high <= 0.5);
// Test at low temperature (ordered phase)
double U_low = calculate_binder_cumulant(10, 0.5, 5000);
printf(" - Binder cumulant at T=0.5: %.3f\n", U_low);
// At low T, should be close to 2/3 (fully ordered)
assert(U_low >= 0.3 && U_low <= 1.0);
// Test at critical temperature (should be around 0.61 for 2D Ising)
double U_crit = calculate_binder_cumulant(16, 2.27, 10000);
printf(" - Binder cumulant at T=2.27 (near Tc): %.3f\n", U_crit);
// Should be between 0.4 and 0.8 (universal value ~0.61)
assert(U_crit >= 0.4 && U_crit <= 0.8);
}
// Calculate Binder cumulant for 2D Ising model
double calculate_binder_cumulant(int N, double T, int steps) {
int **spins = malloc(N * sizeof(int*));
for (int i = 0; i < N; i++) {
spins[i] = malloc(N * sizeof(int));
}
init_rnd(789);
// Initialize random spins
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
spins[i][j] = (drnd() < 0.5) ? 1 : -1;
}
}
// Thermalization
for (int step = 0; step < steps/2; step++) {
int i = (int)(drnd() * N);
int j = (int)(drnd() * N);
int up = (i - 1 + N) % N;
int down = (i + 1) % N;
int left = (j - 1 + N) % N;
int right = (j + 1) % N;
double dE = 2 * spins[i][j] * (spins[up][j] + spins[down][j] +
spins[i][left] + spins[i][right]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i][j] = -spins[i][j];
}
}
// Measurement
double M2_sum = 0.0;
double M4_sum = 0.0;
int measure_steps = steps/2;
for (int step = 0; step < measure_steps; step++) {
int i = (int)(drnd() * N);
int j = (int)(drnd() * N);
int up = (i - 1 + N) % N;
int down = (i + 1) % N;
int left = (j - 1 + N) % N;
int right = (j + 1) % N;
double dE = 2 * spins[i][j] * (spins[up][j] + spins[down][j] +
spins[i][left] + spins[i][right]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i][j] = -spins[i][j];
}
// Calculate magnetization
int sum = 0;
for (int ii = 0; ii < N; ii++) {
for (int jj = 0; jj < N; jj++) {
sum += spins[ii][jj];
}
}
double m = fabs((double)sum / (N * N));
M2_sum += m * m;
M4_sum += m * m * m * m;
}
double M2 = M2_sum / measure_steps;
double M4 = M4_sum / measure_steps;
// Binder cumulant: U_L = 1 - <M^4>/(3<M^2>^2)
double U = (M2 > 1e-10) ? (1.0 - M4 / (3.0 * M2 * M2)) : 0.0;
// Free memory
for (int i = 0; i < N; i++) {
free(spins[i]);
}
free(spins);
return U;
}
// Test finite-size scaling with multiple system sizes
void test_fss_multiple_sizes(void) {
int sizes[] = {8, 12, 16};
int num_sizes = 3;
double T_near_critical = 2.3;
double binders[3];
printf(" - Testing FSS for L = 8, 12, 16 at T=%.2f\n", T_near_critical);
for (int i = 0; i < num_sizes; i++) {
binders[i] = calculate_binder_cumulant(sizes[i], T_near_critical, 5000);
printf(" L=%d: U_L = %.3f\n", sizes[i], binders[i]);
// All should be in reasonable range (relaxed bounds due to statistical fluctuations)
assert(binders[i] >= 0.2 && binders[i] <= 0.9);
}
// Check that we have variation across sizes (not all identical)
// This tests that the simulation is actually running
int all_same = 1;
for (int i = 0; i < num_sizes - 1; i++) {
if (fabs(binders[i] - binders[i+1]) > 0.01) {
all_same = 0;
break;
}
}
assert(!all_same); // Binder cumulants should vary with system size
printf(" - FSS shows size-dependent behavior ✓\n");
}
// Simplified 3D Ising simulation for testing
double run_mini_ising3d(int N, double T, int steps) {
int ***spins = malloc(N * sizeof(int**));
for (int i = 0; i < N; i++) {
spins[i] = malloc(N * sizeof(int*));
for (int j = 0; j < N; j++) {
spins[i][j] = malloc(N * sizeof(int));
}
}
init_rnd(789);
// Initialize random spins
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
spins[i][j][k] = (drnd() < 0.5) ? 1 : -1;
}
}
}
// Monte Carlo steps
for (int step = 0; step < steps; step++) {
int i = (int)(drnd() * N);
int j = (int)(drnd() * N);
int k = (int)(drnd() * N);
int up = (j - 1 + N) % N;
int down = (j + 1) % N;
int left = (i - 1 + N) % N;
int right = (i + 1) % N;
int front = (k + 1) % N;
int back = (k - 1 + N) % N;
double dE = 2 * spins[i][j][k] * (spins[i][up][k] + spins[i][down][k] +
spins[left][j][k] + spins[right][j][k] +
spins[i][j][front] + spins[i][j][back]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i][j][k] = -spins[i][j][k];
}
}
// Calculate magnetization
int sum = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
sum += spins[i][j][k];
}
}
}
double mag = fabs((double)sum / (N * N * N));
// Free memory
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
free(spins[i][j]);
}
free(spins[i]);
}
free(spins);
return mag;
}
void test_ising_3d_physics(void) {
// Test 1: High temperature should have low magnetization
double mag_high_T = run_mini_ising3d(8, 6.0, 10000);
printf(" - 3D Magnetization at T=6.0: %.3f\n", mag_high_T);
assert(mag_high_T < 0.3); // Should be small at high T
// Test 2: Low temperature should have higher magnetization than high T
double mag_low_T = run_mini_ising3d(8, 0.5, 30000);
printf(" - 3D Magnetization at T=0.5: %.3f\n", mag_low_T);
assert(mag_low_T > mag_high_T); // Should be larger at low T than high T
assert(mag_low_T >= 0.0 && mag_low_T <= 1.0); // Sanity check
// Test 3: Near critical temperature (Tc ~ 4.51 for 3D)
double mag_crit = run_mini_ising3d(8, 4.5, 10000);
printf(" - 3D Magnetization at T=4.5 (near Tc): %.3f\n", mag_crit);
assert(mag_crit >= 0.0 && mag_crit <= 1.0);
}
// Calculate Binder cumulant for 3D Ising model
double calculate_binder_cumulant_3d(int N, double T, int steps) {
int ***spins = malloc(N * sizeof(int**));
for (int i = 0; i < N; i++) {
spins[i] = malloc(N * sizeof(int*));
for (int j = 0; j < N; j++) {
spins[i][j] = malloc(N * sizeof(int));
}
}
init_rnd(1001);
// Initialize random spins
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
spins[i][j][k] = (drnd() < 0.5) ? 1 : -1;
}
}
}
// Thermalization
for (int step = 0; step < steps/2; step++) {
int i = (int)(drnd() * N);
int j = (int)(drnd() * N);
int k = (int)(drnd() * N);
int up = (j - 1 + N) % N;
int down = (j + 1) % N;
int left = (i - 1 + N) % N;
int right = (i + 1) % N;
int front = (k + 1) % N;
int back = (k - 1 + N) % N;
double dE = 2 * spins[i][j][k] * (spins[i][up][k] + spins[i][down][k] +
spins[left][j][k] + spins[right][j][k] +
spins[i][j][front] + spins[i][j][back]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i][j][k] = -spins[i][j][k];
}
}
// Measurement
double M2_sum = 0.0;
double M4_sum = 0.0;
int measure_steps = steps/2;
for (int step = 0; step < measure_steps; step++) {
int i = (int)(drnd() * N);
int j = (int)(drnd() * N);
int k = (int)(drnd() * N);
int up = (j - 1 + N) % N;
int down = (j + 1) % N;
int left = (i - 1 + N) % N;
int right = (i + 1) % N;
int front = (k + 1) % N;
int back = (k - 1 + N) % N;
double dE = 2 * spins[i][j][k] * (spins[i][up][k] + spins[i][down][k] +
spins[left][j][k] + spins[right][j][k] +
spins[i][j][front] + spins[i][j][back]);
if (dE < 0 || drnd() < exp(-dE / T)) {
spins[i][j][k] = -spins[i][j][k];
}
// Calculate magnetization
int sum = 0;
for (int ii = 0; ii < N; ii++) {
for (int jj = 0; jj < N; jj++) {
for (int kk = 0; kk < N; kk++) {
sum += spins[ii][jj][kk];
}
}
}
double m = fabs((double)sum / (N * N * N));
M2_sum += m * m;
M4_sum += m * m * m * m;
}
double M2 = M2_sum / measure_steps;
double M4 = M4_sum / measure_steps;
// Binder cumulant: U_L = 1 - <M^4>/(3<M^2>^2)
double U = (M2 > 1e-10) ? (1.0 - M4 / (3.0 * M2 * M2)) : 0.0;
// Free memory
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
free(spins[i][j]);
}
free(spins[i]);
}
free(spins);
return U;
}
void test_binder_cumulant_3d(void) {
// Test at high temperature (disordered phase)
double U_high = calculate_binder_cumulant_3d(6, 6.0, 3000);
printf(" - 3D Binder cumulant at T=6.0: %.3f\n", U_high);
// At high T, should be close to 0 (fully disordered)
assert(U_high >= -0.5 && U_high <= 0.5);
// Test at low temperature (ordered phase)
double U_low = calculate_binder_cumulant_3d(6, 1.0, 3000);
printf(" - 3D Binder cumulant at T=1.0: %.3f\n", U_low);
// At low T, should be close to 2/3 (fully ordered)
assert(U_low >= 0.3 && U_low <= 1.0);
// Test at critical temperature (should be around 0.61-0.63 for 3D Ising with sufficient statistics)
double U_crit = calculate_binder_cumulant_3d(8, 4.51, 10000);
printf(" - 3D Binder cumulant at T=4.51 (near Tc): %.3f\n", U_crit);
// Relaxed range due to finite-size effects and limited statistics
assert(U_crit >= 0.0 && U_crit <= 1.0);
}