-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaux1.c
More file actions
339 lines (311 loc) · 9.42 KB
/
aux1.c
File metadata and controls
339 lines (311 loc) · 9.42 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
#include "SW.h"
/**
\section{InitialSpinConfig}
\subsection{Description}
Sets inital potts spins configuration.
\subsection{Input parameters}
\begin{itemize}
\item[N] Number of points.
\item[Q] Number of Potts spin values.
\end{itemize}
\subsection{Output parameters}
\begin{itemize}
\item[Spin] Spin[i] is the spin value ascociated with vertex i.
\end{itemize}
\subsection{file}
aux1.c
**/
void InitialSpinConfig(int N, unsigned int *Spin, int Q)
{
int i;
if ( GetParam("RandomInitialConfig") )
for(i = 0; i < N; i++) Spin[i] = IRAND(Q);
else
memset( Spin, 0, N*sizeof(unsigned int) );
return;
}
/**
\section{NewSpinConfig}
\subsection{Description}
assign each cluster a random spin value.
\subsection{Input parameters}
\begin{itemize}
\item[N] Number of points.
\item[Block] point i belongs to cluster Block[i].
\item[MBlk] number of clusters.
\item[Q] Number of Potts spin values.
\item[NewSpinValue] Previously allocated workspace. \\
Must have minimal size of N*sizeof(unsigned int).
\end{itemize}
\subsection{Output parameters}
\begin{itemize}
\item[Spin] Spin[i] is the new spin value ascociated with vertex i.
\end{itemize}
\subsection{file}
aux1.c
**/
void NewSpinConfig(
int N,
unsigned int *Spin,
unsigned int *Block,
int NBlk,
int Q,
unsigned int *NewSpinValue )
{
int nb, i;
for (nb = 0; nb < NBlk; nb++)
NewSpinValue[nb] = IRAND(Q);
for(i = 0; i < N; i++) Spin[i] = NewSpinValue[ Block[i] ];
}
/**
\section{DeletionProbabilities}
\subsection{Description}
Gives the deletion probabilities for a satisfied bond.
If bond is unsatisfied its deletion probability is 1.
\subsection{Input parameters}
\begin{itemize}
\item[T] Temperature.
\item[J] J[i][j] is the interaction between i and NK.p[i][j],
where NK is the edges array.
\end{itemize}
\subsection{Output parameters}
\begin{itemize}
\item[P] P.p[i][j] is the deletion probability of a satisfied bond
between vertices i and NK.p[i][j] when their spin values are equal.
\end{itemize}
\subsection{file}
aux1.c
**/
void DeletionProbabilities( float T, RaggedArray J, RaggedArray P )
{
int i,k;
if( T == 0.0 ) ResetRaggedArray( P );
else {
for(i = 0; i < J.n; i++)
for(k = 0; k<J.c[i]; k++){
if(J.p[i][k] != 0.0) P.p[i][k] = exp( - J.p[i][k] / T );
else P.p[i][k] = 1.0;
}
}
}
/**
\section{SetBond}
\subsection{Description}
Decides which bonds to freeze.
\subsection{Input parameters}
\begin{itemize}
\item[P] P.p[i][j] is the deletion probability of the edge
between vertices i and NK.p[i][j] if they have the same spin.
\item[Spin] Spin[i] is the current spin of vertex i.
\item[NK] nearest neighbours array. For each vertex the list of
neighbours \emph{must} be in ascending order.
\item[KN] the inverse nearest neighbours array.
\end{itemize}
\subsection{Output parameters}
\begin{itemize}
\item[Bond] Frozen bond array. Bond.p[i][j] = 1 $\longrightarrow$
bond between i and NK.p[i][j] is frozen.
Bond.p[i][j] = 0 $\longrightarrow$ the bond is deleted.
\end{itemize}
\subsection{Return value}
\begin{itemize}
\item[nb] the number of frozen bonds.
\end{itemize}
\subsection{file}
aux1.c
**/
int SetBond( RaggedArray P, unsigned int *Spin, CRaggedArray Bond,
UIRaggedArray NK, UIRaggedArray KN){
int nb = 0;
int i,k;
for(i = 0; i < Bond.n; i++)
for( k = Bond.c[i]-1; NK.p[i][k]>i && k>=0; k-- ) {
if( (Spin[i] == Spin[NK.p[i][k]] ) && (RAND(1.) > P.p[i][k]) ) {
Bond.p[i][k] = 1;
Bond.p[ NK.p[i][k] ][ KN.p[i][k] ] = 1;
nb ++;
}
else{
Bond.p[i][k] = 0;
Bond.p[ NK.p[i][k] ][ KN.p[i][k] ] = 0;
}
}
return nb;
}
/**
\section{Coarsening}
\subsection{Description}
Uses the frozen bonds to indentify the connected components (clusters).
Assign each vertex its cluster number.
\subsection{Input parameters}
\begin{itemize}
\item[Bond] Frozen bonds array. Bond.p[i][j] = 1 $\longrightarrow$
the bond between i and NK.p[i][j] is frozen.
Bond.p[i][j] = 0 $\longrightarrow$ the bond is deleted.
\item[NK] nearest neighbours array.
\item[Stack] previously allocated workspace of
size $>=$ N*sizeof(unsigned int).
\end{itemize}
\subsection{Output parameters}
\begin{itemize}
\item[Block] assignment to clusters. Spin i belongs to cluster Block[i].
\item[ClusterSize] cluster sizes. ClusterSize[k] is th size of cluster k.
\end{itemize}
\subsection{Return value}
the number of clusters.
\subsection{file}
aux1.c
**/
int Coarsening(CRaggedArray Bond, unsigned int *Block,
UIRaggedArray NK, unsigned int *ClusterSize,
unsigned int* Stack )
{
int ns,i,k,i0,N;
int nblock = -1; /* number of clusters (blocks) generated */
N = Bond.n;
for(i = 0; i < N; i++) Block[i] = UINT_MAX;
memset( ClusterSize, 0, N*sizeof(unsigned int) );
for(i = 0; i < N; i++)
if (Block[i] == UINT_MAX){ /* point i was not labeled yet */
ns = 0;
nblock ++;
Block[i] = nblock;
ClusterSize[nblock] = 1;
Stack[ns] = i;
while(ns >= 0) {
i0 = Stack[ns];
ns--;
for(k = 0; k<Bond.c[i0]; k++){
if( (Bond.p[i0][k] == 1) && (Block[ NK.p[i0][k] ] == UINT_MAX) ) {
Block[ NK.p[i0][k] ] = nblock;
ClusterSize[nblock] ++;
ns++;
Stack[ns] = NK.p[i0][k];
}
}
}
}
nblock++;
return(nblock);
}
/**
\section{OrderingClusters}
\subsection{Description}
reassign cluster numbers according to cluster sizes, so if
i $<$ j $\longrightarrow$ cluster i is larger than cluster j.
\subsection{Input parameters}
\begin{itemize}
\item[N] number of vertices.
\item[nblock] number of clusters.
\item[Block] vertex i belongs to cluster Block[i].
\item[Size] Cluster sizes. Cluster i contains Size[i] vertices.
\item[Indx] previously allocated workspace of
size $>=$ 2*N*sizeof(unsigned int).
\end{itemize}
\subsection{Output parameters}
\begin{itemize}
\item[Block] new cluster number assigned to each vertex.
\item[Size] cluster sizes, ordered in descending order, according
to their new numbers.
\end{itemize}
\subsection{file}
aux1.c
**/
void OrderingClusters(
int N,
int nblock,
unsigned int *Block,
unsigned int *Size,
unsigned int *Indx )
{
int i, j, done;
unsigned int t, *NewIndx;
if( nblock == 1 )
return;
NewIndx = Indx+N;
memset( Indx, 0, N*sizeof(unsigned int) );
/* Size[i] is the size of block i */
/* sort the indexes (linear in N) so if Size[i]>Size[j] -> Indx[i]<Indx[j] */
for( i=0; i<nblock; i++ )
Indx[ Size[i] ] ++;
NewIndx[N-1] = 0;
for( i=N-2; i>0; i-- )
NewIndx[i] = NewIndx[i+1] + Indx[i+1];
for( i=0; i<nblock; i++ ) {
Indx[i] = NewIndx[ Size[i] ];
NewIndx[ Size[i] ] ++;
}
/* order sizes according to indexes Size[i]--->Size[Indx[i]] */
for( i=0; i<nblock; i++ )
NewIndx[ Indx[i] ] = Size[ i ];
memcpy( Size, NewIndx, nblock*sizeof(unsigned int) );
/* now i<j -> Size[i]>=Size[j] */
/* and Size[Indx[i]] is the size of block i. */
/* Asign to block number Indx[i] the new number i */
for( i=0; i<N; i++ ) Block[i] = Indx[ Block[i] ];
return;
}
/**
\section{CheckParam}
\subsection{Description}
Check evironment parameters in order to prevent runtime error.
\subsection{file}
aux1.c
**/
void CheckParam()
{
int Q,N;
assure( (N=IGetParam("NumberOfPoints")) > 0 , "N<=0" );
assure( IGetParam("Dimensions") >= 0 , "D<0" );
assure( (Q=IGetParam("PottsSpins")) <= UINT_MAX , "Q too large" );
assure( Q>1, "Q too small" );
assure( IGetParam("SWCycles") < UINT_MAX , "cyc too large" );
/* if cyc too large can change unsigned int Corr -> unsigned long Corr */
assure( N<UINT_MAX, "N too large" );
assure( FGetParam( "ThresholdStep" )>0.0 ||
FGetParam( "ThresholdTheta" )>0.0, "theta is zero" );
assure( IGetParam( "ClustersReported" )<=N, "NS > N" );
}
/**
\section{CheckParam}
\subsection{Description}
Set default values to environmrnt parameters.
\subsection{file}
aux1.c
**/
void DefaultParam()
{
ISetParam( "PottsSpins", 20 );
ISetParam( "SWCycles", 2500 );
FSetParam( "SWFraction", 0.8 );
FSetParam( "ThresholdTheta", 0.5 );
SetParam( "OutFile", "SWout" );
ISetParam( "ClustersReported", 12 );
ISetParam( "SusceptColors", 4 );
}
/**
\section{Energy}
\subsection{Description}
Calculate the energy for a given configuration:
\[ E(\{s_i\})=\sum_{i,j} J_{ij}(1-\delta_{s_i s_j}) \]
\subsection{Input parameters}
\begin{itemize}
\item[Block] vertex i belongs to cluster Block[i].
\item[J] Interactions array.
\item[NK] Neighbours array.
\end{itemize}
\subsection{Return value}
the energy
\subsection{file}
aux1.c
**/
double Energy( unsigned int* Block, RaggedArray J, UIRaggedArray NK )
{
int i, k;
double e=0;
for(i = 0; i < J.n; i++)
for( k = J.c[i]-1; NK.p[i][k]>i && k>=0; k-- )
if( Block[i] != Block[NK.p[i][k]] )
e += J.p[i][k];
return e;
}