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
|
#include "ltfat.h"
#include "ltfat_types.h"
typedef struct
{
ltfatInt *h;
ltfatInt heapsize;
ltfatInt totalheapsize;
const LTFAT_REAL *s;
}
LTFAT_NAME(heap);
LTFAT_EXTERN
void LTFAT_NAME(heap_insert)(LTFAT_NAME(heap) *h, const ltfatInt key)
{
ltfatInt pos,pos2, swap;
/*printf("Grow heap: heapsize %i, totalheapsize %i\n",h->heapsize,h->totalheapsize);*/
/* Grow heap if necessary */
if (h->totalheapsize==h->heapsize)
{
(h->totalheapsize)*=2;
h->h = ltfat_realloc_and_copy(h->h,
h->totalheapsize*sizeof*h->h/2,
h->totalheapsize*sizeof*h->h);
}
pos=h->heapsize;
h->heapsize++;
/*printf("heap: heapsize %i, pos %i\n",h->heapsize,pos);*/
h->h[h->heapsize-1]=key;
/* printf("before heapint main loop\n"); */
while (pos>0)
{
/* printf("pos %i\n",pos); */
pos2=(pos-pos%2)/2;
if (h->s[h->h[pos2]] < h->s[h->h[pos]] )
{
swap=h->h[pos2];
h->h[pos2]=h->h[pos];
h->h[pos]=swap;
pos=pos2;
}
else
{
break;
}
}
/* { */
/* printf("C INSERT heapsize: %i\n",h->heapsize); */
/* for (ii=0; ii<h->heapsize; ii++) */
/* printf(" %i\n",h->h[ii]); */
/* } */
}
LTFAT_EXTERN
ltfatInt LTFAT_NAME(heap_delete)(LTFAT_NAME(heap) *h)
{
ltfatInt pos, maxchildpos, swap, key;
LTFAT_REAL maxchildkey;
/* Extract first element */
key=h->h[0];
/* Put last element on first elements place, and make the heap smaller. */
h->h[0]=h->h[h->heapsize-1];
h->heapsize--;
/* Fix the just introduced problem. */
pos=0;
/* %%%%%%%%%%%%%%
* %
* % Is maxchildpos 0 or 1 indexed!
* %
* %
* %%%%%%%%%%%%%
*/
while (2*pos+1<h->heapsize)
{
if (2*pos+3>h->heapsize)
{
maxchildkey=h->s[h->h[2*pos+1]];
maxchildpos=1;
}
else
{
if (h->s[h->h[2*pos+1]]>=h->s[h->h[2*pos+2]])
{
maxchildkey=h->s[h->h[2*pos+1]];
maxchildpos=1;
}
else
{
maxchildkey=h->s[h->h[2*pos+2]];
maxchildpos=2;
}
}
if (maxchildkey>h->s[h->h[pos]])
{
swap=h->h[2*pos+maxchildpos];
h->h[2*pos+maxchildpos]=h->h[pos];
h->h[pos]=swap;
pos=2*pos+maxchildpos;
}
else
{
break;
}
}
/* { */
/* printf("C DELETE heapsize: %i\n",h->heapsize); */
/* for (ii=0; ii<h->heapsize; ii++) */
/* printf(" %i %f\n",h->h[ii],h->s[h->h[ii]]); */
/* } */
return key;
}
LTFAT_EXTERN
void LTFAT_NAME(heapint)(const LTFAT_REAL *s,
const LTFAT_REAL *tgrad,
const LTFAT_REAL *fgrad,
const ltfatInt a, const ltfatInt M, const ltfatInt L, const ltfatInt W,
LTFAT_REAL tol, LTFAT_REAL *phase)
{
/* Declarations */
ltfatInt b, N, ii, jj, Imax, domainloop;
ltfatInt w, w_E, w_W, w_N, w_S;
LTFAT_REAL maxs;
ltfatInt *donemask;
LTFAT_NAME(heap) h;
N=L/a;
b=L/M;
/* Main body */
/* printf("Main body a: %i, M: %i, N, %i, L: %i, W: %i\n",a,M,N,L,W); */
h.totalheapsize =(ltfatInt)(M*log((LTFAT_REAL)M));
h.h = ltfat_malloc(h.totalheapsize*sizeof*h.h);
h.s = s;
h.heapsize = 0;
donemask = ltfat_malloc(M*N*W*sizeof*donemask);
LTFAT_REAL *tgradw = ltfat_malloc(M*N*sizeof*tgradw);
LTFAT_REAL *fgradw = ltfat_malloc(M*N*sizeof*fgradw);
/* Set the phase to zero initially */
memset(phase,0,M*N*W*sizeof*phase);
//for (ii=0; ii<M*N*W; ii++)
//{
// phase[ii]=0.0;
//}
/* Rescale the partial derivatives to a torus of length L. We copy
to work arrays because the input is const. */
for (jj=0; jj<N; jj++)
{
for (ii=0; ii<M; ii++)
{
tgradw[ii+jj*M] = tgrad[ii+jj*M]*2*PI*a/L;
fgradw[ii+jj*M] = fgrad[ii+jj*M]*2*PI*b/L+2*PI*jj*a*b/L;
}
}
maxs=-1e99;
Imax=0; /* This is just included to avoid a warning from the C++ compiler */
for (ii=0; ii<M*N; ii++)
{
if (s[ii]>maxs)
{
maxs=s[ii];
Imax=ii;
}
}
/* printf("Mark\n"); */
/* Mark all the small elements as done, they get a zero phase.
* Code 5
*/
for (ii=0; ii<M*N*W; ii++)
{
if (s[ii]<tol*maxs)
donemask[ii]=5;
else
donemask[ii]=0;
}
domainloop=1;
while (domainloop)
{
/* printf("Main loop, Imax: %i\n",Imax); */
h.heapsize=0;
/* Put maximal element onto the heap and mark that it is done. It
* will get zero phase.
*/
LTFAT_NAME(heap_insert)(&h,Imax);
/* printf("after heap_insert, Imax %i, h.heapize %i\n", Imax,h.heapsize); */
donemask[Imax]=6;
/* printf("after donemask\n",h.heapsize); */
while (h.heapsize>0)
{
/* printf("Inner loop, heapsize: %i\n",h.heapsize); */
/* Extract largest element from heap and delete it. */
w = LTFAT_NAME(heap_delete)(&h);
/* Try and put the four neighbours onto the heap.
* Integration by trapezoidal rule */
/* North */
w_N=(w-1+M)%M+w-w%M;
if (!donemask[w_N])
{
/* printf("C North: %i %i\n",w,w_N); */
phase[w_N] = phase[w]+(fgradw[w]+fgradw[w_N])/2;
donemask[w_N]=1;
LTFAT_NAME(heap_insert)(&h,w_N);
}
/* South */
w_S=((w+1)%M)+w-w%M;
if (!donemask[w_S])
{
/* printf("C South: %i %i\n",w,w_S); */
phase[w_S] = phase[w]-(fgradw[w]+fgradw[w_S])/2;
donemask[w_S]=2;
LTFAT_NAME(heap_insert)(&h,w_S);
}
/* East */
w_E = (w+M)%(M*N);
if (!donemask[w_E])
{
/* printf("C East:%i %i\n",w,w_E); */
phase[w_E]=phase[w]+(tgradw[w]+tgradw[w_E])/2;
donemask[w_E]=3;
LTFAT_NAME(heap_insert)(&h,w_E);
}
/* West */
w_W = (w-M+M*N)%(M*N);
if (!donemask[w_W])
{
/* printf("C West: %i %i\n",w,w_W); */
phase[w_W]=phase[w]-(tgradw[w]+tgradw[w_W])/2;
donemask[w_W]=4;
LTFAT_NAME(heap_insert)(&h,w_W);
}
}
/* Find the new maximal element
*
* This code is currently missing
*
*/
maxs=-1e99;
domainloop=0;
for (ii=0; ii<M*N; ii++)
{
if (!donemask[ii] && s[ii]>maxs)
{
maxs=s[ii];
Imax=ii;
domainloop=1;
}
}
}
ltfat_free(tgradw);
ltfat_free(fgradw);
ltfat_free(donemask);
ltfat_free(h.h);
}
|