-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.c
165 lines (145 loc) · 3.92 KB
/
data.c
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
/* data.c
*
* Copyright (C) 2024 L. Bertini
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "spinner.h"
spnr_data_t *
spnr_data_alloc (size_t const size)
{
spnr_data_t * const data = malloc (sizeof (spnr_data_t));
data->size = size;
data->h = malloc (data->size * sizeof (float));
data->phi = malloc (data->size * sizeof (float));
return data;
}
void
spnr_data_free (spnr_data_t * const data)
{
free (data->h);
free (data->phi);
free (data);
}
void
spnr_data_write (spnr_data_t const * const data,
char const * const fname)
{
FILE *f;
size_t i;
size_t const size = data->size;
float * const h = data->h;
float * const phi = data->phi;
f = fopen (fname, "w");
if (!f)
exit (EXIT_FAILURE);
for (i = 0; i < size; ++i)
fprintf (f, "%05ld %+.3f %+.3f\n", i, h[i], phi[i]);
fclose (f);
}
void
spnr_data_mean_calc (spnr_data_t const * const data,
float * const h_mean,
float * const phi_mean)
{
size_t i;
size_t const size = data->size;
float * const h = data->h;
float * const phi = data->phi;
float sum_h = 0;
float sum_phi = 0;
for (i = 0; i < size; ++i)
{
sum_h += h[i];
sum_phi += phi[i];
}
*h_mean = sum_h / size;
*phi_mean = sum_phi / size;
}
void
spnr_data_var_calc (spnr_data_t const * const data,
float * const h_var,
float * const phi_var)
{
size_t i;
size_t const size = data->size;
float * const h = data->h;
float * const phi = data->phi;
float sum_sq_h = 0;
float sum_sq_phi = 0;
float sum_h = 0;
float sum_phi = 0;
for (i = 0; i < size; ++i)
{
sum_sq_h += h[i] * h[i];
sum_sq_phi += phi[i] * phi[i];
sum_h += h[i];
sum_phi += phi[i];
}
*h_var = sum_sq_h / size - (sum_h * sum_h) / (size * size);
*phi_var = sum_sq_phi / size - (sum_phi * sum_phi) / (size * size);
}
static void
spnr_corr_calc (float * const corr, float const * const arr, size_t const size)
{
size_t i, j;
size_t max;
float a, b, c;
for (i = 0; i < size; ++i)
{
max = size - i;
a = 0;
b = 0;
c = 0;
for (j = 0; j < max; ++j)
{
a += arr[j] * arr[j + i];
b += arr[j];
c += arr[j + i];
}
corr[i] = (a - b * c / max) / max;
}
}
void
spnr_data_corr_calc (spnr_data_t * const corr,
spnr_data_t const * const data)
{
if (corr->size != data->size)
exit (EXIT_FAILURE);
spnr_corr_calc (corr->h, data->h, corr->size);
spnr_corr_calc (corr->phi, data->phi, corr->size);
}
void
spnr_data_run_and_probe (spnr_data_t * const data,
spnr_sys_t * const sys,
spnr_step_t const * const step,
float const temp,
size_t const n_steps_before_probe)
{
size_t i, j;
size_t const n_probes = data->size;
float const beta = 1.0 / temp;
float * const h = data->h;
float * const phi = data->phi;
h[0] = spnr_sys_calc_h (sys);
phi[0] = spnr_sys_calc_phi (sys);
for (i = 1; i < n_probes; ++i)
{
for (j = 0; j < n_steps_before_probe; ++j)
spnr_step_apply (step, sys, beta);
h[i] = spnr_sys_calc_h (sys);
phi[i] = spnr_sys_calc_phi (sys);
}
}