-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathellipticalFoam.c
2705 lines (2300 loc) · 82.7 KB
/
ellipticalFoam.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXACTIVE 10
#define MAXTRANSITIONS 100
#define ENGULFPACKET 64
/* structure definitions... */
struct xyzBuffer {
double *abscissas; /* the point values in either x,y, or z... */
int *indices; /* the indices associated with the sorted arrays of
abscissas */
int *reverseIndices; /* reverseIndices[i] is the index of i in indices... */
};
struct ellipsoid {
double axisLengths[3]; /* the three axis lengths... */
double axes[3][3]; /* the three axes, as unit vectors.
axes[0][*] corresponds to axisLength[0],
etc... */
};
struct occupant{
int id; /* the id of the point contained... */
double eFunc; /* the ellipsoid function of point id in the current
ellipsoid... */
double offset[3]; /* the offset required to bring point id into the current
ellipsoid... */
};
struct occupantList {
struct occupant *theOccupant;
struct occupantList *next;
};
struct hash{
int basis; /* the hash function is index % basis... */
int **hash; /* hash[i][0] is the index that hashes to the i'th location,
hash[i][1] is the location in the target array... */
};
struct foamCell {
int id; /* the index leading to this particular foamCell... */
int actives; /* the number of enclosing cells that are active... */
int activeEngulfers[MAXACTIVE]; /* the indices of the enclosing cells... */
double point[3]; /* the center of the ellipsoid... */
struct ellipsoid e; /* the desired ellipsoidal cell... */
int nContained; /* the number of points my ellipsoid contains... */
struct occupant **containedIn; /* the points that my ellipse contains,
along with their ellipsoidFunctions... */
struct hash *containedInHash; /* a hash to allow index containedIn lookup
easily... */
int nEngulfers; /* the number of foamCells whose ellipsoids contain this
point... */
int *engulfers; /* the indices of the foamCells containing this
foamCell... */
struct hash *engulferHash; /* a hash to allow engulfer index lookup
easily... */
double orientation[3]; /* the desired orientation, in bunge notation... */
};
struct bbox{
double ranges[3][2]; /* the range in x,y,z...
xmin = ranges[0][0], xmax = ranges[0][1],
ymin = ranges[1][0], ymax = ranges[1][1],
zmin = ranges[2][0], zmax = ranges[2][1] */
};
/* a few convenient globals... */
long ranseed = 0l;
int transitionCount = 0;
double consumeAward = 1.0;
double overlapEncouragement = 1.0;
double zeroPenalty = 0.95;
double systemState = 0.0;
int maxTransitions;
double sumTransitions=0.0;
int currentOperation;
struct bbox *theBbox;
double paddingFraction=0.5;
int metaInformation=0;
int *immortals;
FILE *logFile;
/* function prototypes... */
double ellipsoidFunction(double *point, struct foamCell *theCell);
double addCost(int index, struct foamCell **theCells,int *activeList);
double subtractCost(int index, struct foamCell **theCells,int *activeList);
double swapCost(int markedForDeath,int reincarnated,
struct foamCell **theCells,int *activeList);
void commitAdd(int index,struct foamCell **theCells,int *activeList);
void commitSubtract(int index,struct foamCell **theCells,int *activeList);
void commitSwap(int markedForDeath,int reincarnated,
struct foamCell **theCells, int *activeList);
void updateActives(struct foamCell **theCells,int index,int *activeList);
double findEFunc(struct foamCell *me, int index, int *found);
void add(int nCells,struct foamCell **theCells,int *activeList);
void swap(int nCells,struct foamCell **theCells,int *activeList);
void jog(int nCells,struct foamCell **theCells,int *activeList);
void delete(int nCells,struct foamCell **theCells,int *activeList);
int oracle(double cost);
double annealingSchedule(double cost);
void genrandom(long *ranseed,int count,double *ranvalues);
void logState(double cost);
void reorderAxes(struct foamCell *theCell);
void sortBuffer(int count,struct xyzBuffer *buffer);
void findContained(int count,struct bbox *theBbox,
struct xyzBuffer *xBuffer,
struct xyzBuffer *yBuffer,
struct xyzBuffer *zBuffer,
int index,
struct foamCell **theCells);
int bisectFind(int count, double value, struct xyzBuffer *theBuffer);
int intervalCount(int nsegs,int *intervals);
int inInterval(int probe,int nsegs,int *intervals);
void findThisInterval(int nCells,
struct bbox *theBbox,
int theCoord,
struct xyzBuffer *theBuffer,
double vmin,
double vmax,
int *nsegs,int *breakPoints,
double *offsets);
void updateEngulfers(int id,struct foamCell **theCells);
void buildHashes(int id, struct foamCell **theCells);
void readPoints(char *fnam,int *nCells,struct foamCell ***theCells);
void repackEngulfers(struct foamCell *me);
void twiddle(int nCells,struct foamCell **theCells,int *activeList);
double overlap(double EFunc);
void outputCell(FILE *outfile,int noOffset,struct foamCell *theCell,
struct foamCell **theCells,struct bbox *theBbox);
void genNewCells(struct foamCell **newCells,int *next,
int index,struct foamCell **oldCells,int *replacements);
void output(int nCells,struct foamCell **theCells,int *activeList,
struct bbox *theBbox);
void logState(double cost) {
static int initialized=0;
static FILE *logFile;
static int lastCount;
if (!initialized) {
initialized = 1;
logFile = fopen("annealing.log","w");
lastCount = 0;
}
if ((int)(transitionCount/1000) > (int)(lastCount/1000)) {
lastCount = transitionCount;
fprintf(logFile,"%d %d %f %f %f %f\n",transitionCount,currentOperation,
sumTransitions,sumTransitions/transitionCount,cost,systemState);
fflush(logFile);
}
}
double overlap(double EFunc) {
/* given the ellipsoid function of a point in two cells, calculate
the penalty or benefit of the overlap. the overlap function
is -k*EFunc^2 + q, where k and q are determined by
zeroPenalty and overlapEncouragement... */
static int initialized=0; /* have i calculated the parabolic overlap
function... */
static double k;
static double q;
if (!initialized) {
/* solve for k and q. at EFunc == zeroPenalty the overlap function
is 0. at EFunc == 1 the overlap function is -overlapEncouragement. */
initialized = 1;
q = -overlapEncouragement/(1.0 - (1.0/(zeroPenalty*zeroPenalty)));
k = q/(zeroPenalty*zeroPenalty);
}
return(-k*EFunc*EFunc + q);
}
double ellipsoidFunction(double *point, struct foamCell *theCell) {
/* returns the ellipsoid function (x/a)^2 + (y/b)^2 + (z/c)^2 of
point in the ellipsoidal cell theCell. values of < 1 mean
point is inside theCell, > 1 mean point is outside theCell, and
exactly 1 means point lies on the surface of the ellipsoid... */
int i,j; /* trusty indices... */
double retval; /* (x/a)^2 + (y/b)^2 + (z/c)^2 */
double offset[3]; /* point - center of ellipsoid... */
double local[3]; /* offset in the local ellipsoid coordinate system... */
for(i=0;i<3;i++) {
offset[i] = point[i]-theCell->point[i];
}
retval = 0.0;
for(i=0;i<3;i++) {
local[i] = 0.0;
for(j=0;j<3;j++) {
local[i] += offset[j]*(theCell->e.axes[i][j]);
}
retval += (local[i]/(theCell->e.axisLengths[i]))*
(local[i]/(theCell->e.axisLengths[i]));
}
return(retval);
}
double addCost(int index, struct foamCell **theCells,int *activeList) {
/* returns the cost to make the index'th ellipsoid active... */
int i,j; /* trusty indices... */
double retval; /* the return value... */
struct foamCell *me; /* shorthand for the index'th cell... */
int neighbor; /* the contained point... */
int theRival; /* another cell that contains the current point... */
int found; /* have i found the ellipsoid function of the point in
question...*/
me = theCells[index];
retval = 0.0;
for (i=0;i<me->nContained;i++) {
neighbor = me->containedIn[i]->id;
if (theCells[neighbor]->actives == 0) {
retval -= consumeAward;
}
else {
retval += overlap(me->containedIn[i]->eFunc);
/* and include the collateral damage on the other side... */
for (j=0;j<theCells[neighbor]->actives;j++) {
theRival = theCells[neighbor]->activeEngulfers[j];
retval += overlap(findEFunc(theCells[theRival],neighbor,&found));
if (!found) {
fprintf(stderr,"Inconsistent structure detected\n");
fprintf(stderr,"foamCell:%d, neighbor:%d\n",theRival,neighbor);
}
}
}
}
return(retval);
}
void commitAdd(int index,struct foamCell **theCells,int *activeList) {
/* adds index to the activeList, and makes theCells reflect the change... */
int i; /* trusty indices... */
struct foamCell *me;
int neighbor; /* a point contained in me... */
me = theCells[index];
activeList[index] = 1;
for(i=0;i<me->nContained;i++) {
neighbor= me->containedIn[i]->id;
updateActives(theCells,neighbor,activeList);
}
}
double subtractCost(int index, struct foamCell **theCells,
int *activeList) {
/* returns the cost to make the index'th ellipsoid inActive... */
int i,j; /* trusty indices... */
double retval; /* the return value... */
struct foamCell *me; /* shorthand for the index'th cell... */
int neighbor; /* the contained point... */
int theRival; /* another active cell that contains neighbor... */
int found; /* have i found the ellipsoid function for neighbor? */
if (!activeList[index]) {
fprintf(stderr,"subtractCost called on inactive cell...\n");
fprintf(stderr,"index:%d\n",index);
}
me = theCells[index];
retval = 0.0;
for (i=0;i<me->nContained;i++) {
neighbor = me->containedIn[i]->id;
if (theCells[neighbor]->actives == 1) {
if (theCells[neighbor]->activeEngulfers[0] == index) {
retval += consumeAward;
}
else {
fprintf(stderr,"%d thinks it is active and contains %d\n",
index,neighbor);
fprintf(stderr,"but %d thinks it is contained by %d\n",
neighbor,theCells[neighbor]->activeEngulfers[0]);
}
}
else {
if (theCells[neighbor]->actives > 1) {
retval -= overlap(me->containedIn[i]->eFunc);
/* and include the collateral damage on the other side... */
for (j=0;j<theCells[neighbor]->actives;j++) {
theRival = theCells[neighbor]->activeEngulfers[j];
if (theRival != index) {
retval -= overlap(findEFunc(theCells[theRival],neighbor,&found));
if (!found) {
fprintf(stderr,"Inconsistent structure detected\n");
fprintf(stderr,"foamCell:%d, index:%d\n",theRival,index);
}
}
}
}
else {
fprintf(stderr,"Inconsistent structure detected\n");
fprintf(stderr,"neighbor:%d doesn't know he's in active cell:%d\n",
neighbor,index);
}
}
}
return(retval);
}
void commitSubtract(int index,struct foamCell **theCells,int *activeList) {
/* removes index from the activeList, and makes theCells reflect the
change... */
int i; /* trusty index... */
struct foamCell *me;
int neighbor; /* a point contained in me... */
me = theCells[index];
activeList[index] = 0;
for(i=0;i<me->nContained;i++) {
neighbor= me->containedIn[i]->id;
updateActives(theCells,neighbor,activeList);
}
}
void updateActives(struct foamCell **theCells,int index,int *activeList) {
int i; /* trusty index... */
int next; /* the next index in activeEngulfers to fill... */
struct foamCell *me;
me = theCells[index];
for(i=0;i<MAXACTIVE;i++) {
me->activeEngulfers[i] = -1;
}
next = 0;
me->actives = 0;
for(i=0;i<me->nEngulfers;i++) {
if (activeList[me->engulfers[i]]) {
me->actives++;
me->activeEngulfers[next++] = me->engulfers[i];
}
}
}
double findEFunc(struct foamCell *me, int index, int *found) {
int probe; /* the place to start looking in the containedInHash... */
int i; /* trusty index... */
int maxVal; /* the size of the containedInHash... */
maxVal = me->containedInHash->basis;
probe = index % (me->containedInHash->basis);
for(i=probe;i<maxVal;i++) {
if (me->containedInHash->hash[i][0] == index) {
*found =me->containedInHash->hash[i][1] + 1;
return(me->containedIn[me->containedInHash->hash[i][1]]->eFunc);
}
else {
if (me->containedInHash->hash[i][0] < 0) {
*found = 0;
return(0.0);
}
}
}
/* could get here from a collision at the last element... */
for(i=0;i<probe;i++) {
if (me->containedInHash->hash[i][0] == index) {
*found = me->containedInHash->hash[i][1] + 1;
return(me->containedIn[me->containedInHash->hash[i][1]]->eFunc);
}
else {
if (me->containedInHash->hash[i][0] < 0) {
*found = 0;
return(0.0);
}
}
}
*found = 0;
return(0.0);
}
double swapCost(int markedForDeath,int reincarnated,
struct foamCell **theCells,int *activeList) {
/* the cost of killing marked for death and replacing him with
reincarnated. be careful to correct for the effect of markedForDeath
in the returned cost... */
int i,j; /* trusty indices... */
double retval; /* the return value... */
struct foamCell *me; /* shorthand for the current cell... */
int neighbor; /* the contained point... */
int theRival; /* another active cell that contains neighbor... */
int found; /* have i found the ellipsoid function for neighbor? */
if ((activeList[markedForDeath]) && (!activeList[reincarnated])) {
me = theCells[reincarnated];
retval = 0.0;
for (i=0;i<me->nContained;i++) {
neighbor = me->containedIn[i]->id;
if (theCells[neighbor]->actives == 0) {
retval -= consumeAward;
}
else {
if ((theCells[neighbor]->actives == 1) &&
(theCells[neighbor]->activeEngulfers[0] == markedForDeath)) {
retval -= consumeAward;
}
else {
retval += overlap(me->containedIn[i]->eFunc);
}
/* and include the collateral damage on the other side... */
for (j=0;j<theCells[neighbor]->actives;j++) {
theRival = theCells[neighbor]->activeEngulfers[j];
if (theRival != markedForDeath) {
retval +=
overlap(findEFunc(theCells[theRival],neighbor,&found));
if (!found) {
fprintf(stderr,"Inconsistent structure detected\n");
fprintf(stderr,"foamCell:%d, neighbor:%d\n",theRival,neighbor);
}
}
}
}
}
me = theCells[markedForDeath];
for (i=0;i<me->nContained;i++) {
neighbor = me->containedIn[i]->id;
if (theCells[neighbor]->actives == 1) {
if (theCells[neighbor]->activeEngulfers[0] == markedForDeath) {
retval += consumeAward;
}
else {
fprintf(stderr,"%d thinks it is active and contains %d\n",
markedForDeath,neighbor);
fprintf(stderr,"but %d thinks it is contained by %d\n",
neighbor,theCells[neighbor]->activeEngulfers[0]);
}
}
else {
if (theCells[neighbor]->actives > 1) {
retval -= overlap(me->containedIn[i]->eFunc);
/* and include the collateral damage on the other side... */
for (j=0;j<theCells[neighbor]->actives;j++) {
theRival = theCells[neighbor]->activeEngulfers[j];
if (theRival != markedForDeath) {
retval -=
overlap(findEFunc(theCells[theRival],neighbor,&found));
if (!found) {
fprintf(stderr,"Inconsistent structure detected\n");
fprintf(stderr,"foamCell:%d, neighbor:%d\n",theRival,neighbor);
}
}
}
}
else {
fprintf(stderr,"Inconsistent structure detected\n");
fprintf(stderr,"neighbor:%d doesn't know he's in active cell:%d\n",
neighbor,markedForDeath);
}
}
}
return(retval);
}
else {
fprintf(stderr,"Inconsistent call to swapCost:\n");
fprintf(stderr,"markedForDeath:%d, activeList[markedForDeath]:%d\n",
markedForDeath,activeList[markedForDeath]);
fprintf(stderr,"reincarnated:%d, activeList[reincarnated]:%d\n",
reincarnated,activeList[reincarnated]);
return(HUGE);
}
}
void add(int nCells,struct foamCell **theCells,
int *activeList) {
/* selects an unenclosed cell at random, evaluates the cost of adding it,
adds it if the oracle says yes, and updates the systemState... */
int next; /* the next foamCell to check... */
int theIndex; /* the place to start looking for a cell to turn on... */
double cost; /* the change in systemState from adding this cell... */
double deviate; /* a random deviate... */
int found; /* have i found a candidate to add... */
int priorIndex; /* the predecessor to the starting position... */
found = 0;
genrandom(&ranseed,1,&deviate);
theIndex = (int)(nCells*deviate);
if (theIndex != 0) {
priorIndex = theIndex-1;
}
else {
priorIndex = nCells - 1;
}
next = theIndex;
while ((!found) && (next != priorIndex)) {
if (theCells[next]->actives == 0) {
found = 1;
cost = addCost(next,theCells,activeList);
if (oracle(cost)) {
commitAdd(next,theCells,activeList);
systemState += cost;
logState(cost);
return;
}
}
next++;
if (next == nCells) {
next = 0;
}
}
}
void swap(int nCells,struct foamCell **theCells,int *activeList) {
/* selects an active and an inactive cell at random, evaluates the cost of
swapping their state, commits if the oracle says yes,
and updates the systemState... */
int next; /* the next foamCell to check... */
int theIndex; /* the place to start looking for a cell to turn on... */
double cost; /* the change in systemState from adding this cell... */
double deviate; /* a random deviate... */
int found; /* have i found a candidate to add... */
int markedForDeath; /* the cell to turn off... */
int reincarnated; /* the cell to turn on... */
int priorIndex; /* the predecessor to the starting position... */
found = 0;
genrandom(&ranseed,1,&deviate);
theIndex = (int)(nCells*deviate);
if (theIndex != 0) {
priorIndex = theIndex-1;
}
else {
priorIndex = nCells - 1;
}
next = theIndex;
while ((!found) && (next != priorIndex)) {
if ((activeList[next]) && (!immortals[next])) {
found = 1;
markedForDeath = next;
}
next++;
if (next == nCells) {
next = 0;
}
}
if (found) {
found = 0;
genrandom(&ranseed,1,&deviate);
theIndex = (int)(nCells*deviate);
if (theIndex != 0) {
priorIndex = theIndex-1;
}
else {
priorIndex = nCells - 1;
}
next = theIndex;
while ((!found) && (next != priorIndex)) {
if (!activeList[next]) {
found = 1;
reincarnated = next;
}
next++;
if (next == nCells) {
next = 0;
}
}
if (found) {
cost = swapCost(markedForDeath,reincarnated,theCells,activeList);
if (oracle(cost)) {
commitSwap(markedForDeath,reincarnated,theCells,activeList);
systemState += cost;
logState(cost);
}
}
}
}
void jog(int nCells,struct foamCell **theCells,int *activeList) {
/* selects an active cell at random, evaluates the cost of
swapping the cell with one contained in it, commits if the oracle says
yes, and updates the systemState... */
int next; /* the next foamCell to check... */
int theIndex; /* the place to start looking for a cell to turn on... */
double cost; /* the change in systemState from adding this cell... */
double deviate; /* a random deviate... */
int found; /* have i found a candidate to add... */
int markedForDeath; /* the cell to turn off... */
int reincarnated; /* the cell to turn on... */
int priorIndex; /* the predecessor to the starting position... */
found = 0;
genrandom(&ranseed,1,&deviate);
theIndex = (int)(nCells*deviate);
if (theIndex != 0) {
priorIndex = theIndex-1;
}
else {
priorIndex = nCells - 1;
}
next = theIndex;
while ((!found) && (next != priorIndex)) {
if ((activeList[next]) && (!immortals[next])) {
found = 1;
markedForDeath = next;
}
next++;
if (next == nCells) {
next = 0;
}
}
if (found) {
found = 0;
genrandom(&ranseed,1,&deviate);
theIndex = (int)((theCells[markedForDeath]->nContained)*deviate);
if (theIndex != 0) {
priorIndex = theIndex-1;
}
else {
priorIndex = (theCells[markedForDeath]->nContained) - 1;
}
next = theIndex;
while ((!found) && (next != priorIndex)) {
reincarnated = theCells[markedForDeath]->containedIn[next]->id;
if ((theCells[reincarnated]->actives == 1) &&
(theCells[reincarnated]->activeEngulfers[0] == markedForDeath) &&
(reincarnated != markedForDeath)) {
found = 1;
}
next++;
if (next == theCells[markedForDeath]->nContained) {
next = 0;
}
}
if (found) {
cost = swapCost(markedForDeath,reincarnated,theCells,activeList);
if (oracle(cost)) {
commitSwap(markedForDeath,reincarnated,theCells,activeList);
systemState += cost;
logState(cost);
}
}
}
}
void delete(int nCells,struct foamCell **theCells,int *activeList) {
/* chooses an active cell at random, calculates the cost of deleting it,
commits the delete if the oracle says yes, and updates the
systemState... */
int next; /* the next foamCell to check... */
int theIndex; /* the place to start looking for a cell to turn on... */
double cost; /* the change in systemState from adding this cell... */
double deviate; /* a random deviate... */
int found; /* have i found a candidate to add... */
int priorIndex; /* the predecessor to the starting position... */
found = 0;
genrandom(&ranseed,1,&deviate);
theIndex = (int)(nCells*deviate);
if (theIndex != 0) {
priorIndex = theIndex-1;
}
else {
priorIndex = nCells - 1;
}
next = theIndex;
while ((!found) && (next != priorIndex)) {
if ((activeList[next]) && (!immortals[next])) {
found = 1;
cost = subtractCost(next,theCells,activeList);
if (oracle(cost)) {
commitSubtract(next,theCells,activeList);
systemState += cost;
logState(cost);
return;
}
}
next++;
if (next == nCells) {
next = 0;
}
}
}
void commitSwap(int markedForDeath,int reincarnated,
struct foamCell **theCells, int *activeList) {
/* updates theCells to reflect the deletion of markedForDeath from
activeList, and the addition of reincarnated... */
commitSubtract(markedForDeath,theCells,activeList);
commitAdd(reincarnated,theCells,activeList);
}
int oracle(double cost) {
double prob; /* the acceptance probability... */
double deviate; /* a probe... */
sumTransitions += fabs(cost);
transitionCount++;
if (cost < 0.0) {
return(1);
}
else {
prob = exp(-cost/annealingSchedule(cost));
}
genrandom(&ranseed,1,&deviate);
if (deviate < prob) {
return(1);
}
else {
return(0);
}
}
double annealingSchedule(double cost) {
/* calculates an annealing schedule, which is a specification of the
how likely the program is to accept a positive transition (from a better
to a worse state). the annealing schedule is specified in terms of
a fraction of the average transition magnitude, as a function of
transitionCount. for instance, if there is no annealing.schedule file
the default annealing schedule is
0 0.4
100000 0.0001
which means that to begin with, positive transitions up to 0.4 times the
average transition magnitude have a 50% chance of being accepted, but by
100000 transitions, positive transitions of only 0.0001*the average
transition magnitude have a 50% chance of being accepted... */
static int nbreaks; /* the number of break points in the annealing
schedule... */
static int xs[MAXTRANSITIONS]; /* the x values of the annealing schedule...*/
static double ys[MAXTRANSITIONS]; /* the y values of the annealing
schedule... */
static int initialized=0; /* have i initialized xs and ys... */
double frac; /* the fraction of the average transition at which the
probability of acceptance is 50%... */
double avg; /* the average positive transition... */
int i; /* trusty index... */
int theBreak; /* the interval of the annealing schedule in which
we currently fall... */
FILE *annealingFile; /* potentially a file with an annealing schedule */
if (!initialized) {
initialized = 1;
annealingFile = fopen("annealing.schedule","r");
if (annealingFile == NULL) {
nbreaks = 2;
xs[0] = 0;
xs[1] = 100000;
ys[0] = 0.4;
ys[1] = 0.0001;
}
else {
fscanf(annealingFile,"%d",&nbreaks);
for(i=0;i<nbreaks;i++){
fscanf(annealingFile,"%d %lf",&(xs[i]),&(ys[i]));
}
}
maxTransitions = xs[nbreaks-1];
}
if (transitionCount > 0) {
avg = sumTransitions/transitionCount;
}
else {
avg = sumTransitions;
}
theBreak = 0;
if (transitionCount <= xs[0]) {
frac = ys[0];
}
else {
if (transitionCount >= xs[nbreaks-1]) {
frac = ys[nbreaks-1];
}
else {
for(i=0;i<nbreaks-1;i++) {
if ((xs[i] <= transitionCount) && (xs[i+1] > transitionCount)) {
frac = ys[i] + ((double)(transitionCount - xs[i])/
(double)(xs[i+1]-xs[i]))*(ys[i+1]-ys[i]);
break;
}
}
}
}
/* 1/2 = exp(-frac*avg/retval) => retval = (frac*avg)/0.69 */
return(frac*avg/0.69);
}
#define STOREDSTATE 97
void genrandom(long *ranseed,int count,double *ranvalues)
{
/* this function generates count random numbers between 0 and 1, shuffled
to avoid serial correlation. the generator keeps a cache of random
numbers, which it accesses and replenishes in a random order. the cache
is initialized at first call. if *ranseed is zero, reinitialize the
random number cache, and the ranseed, which is initialized from the
current time... */
static int initialized=0; /* has the history vector been initialized... */
static double history[STOREDSTATE]; /* a bunch of random doubles to choose
from... */
int i; /* an index */
double probe; /* random deviate used for calculating selected value... */
int selection; /* the index into history of the selected item... */
time_t *tloc; /* the current time value, if needed */
if ((!initialized) || (ranseed == 0)) {
initialized = 1;
if (*ranseed == 0) {
tloc = (time_t *)malloc(sizeof(time_t));
*ranseed = (long)time(tloc);
}
/* initialize the random number generator... */
srand48(*ranseed);
/* srand(*ranseed); */
/* fill the history vector... */
for (i=0;i<STOREDSTATE;i++)
history[i] = drand48();
/* history[i] = ((double)rand())/((double)RAND_MAX+1); */
}
for (i=0;i<count;i++) {
probe = drand48();
/* probe = ((double)rand())/((double)RAND_MAX+1); */
selection = (int)(STOREDSTATE*drand48());
/* selection = (int)(STOREDSTATE*((double)rand())/((double)RAND_MAX+1));*/
ranvalues[i] = history[selection];
history[selection] = drand48();
/* history[selection] = ((double)rand())/((double)RAND_MAX+1);*/
}
}
void readPoints(char *fnam,int *nCells,struct foamCell ***theCells) {
/* read the points, along with their associated ellipsoids and orientations.
allocate foamCell, fill in the ellipsoid and the orientation.
the format of the input file is:
foreach point:
point (3 doubles),
ellipsoid axis lengths (3 doubles),
ellipsoid axes (9 doubles, unit vector for
three axes),
orientation (3 doubles)
*/
int count; /* the current count of points... */
double record[18]; /* one point record... */
int i,j; /* trusty indices... */
int fullrecord; /* did we have a partial record at the end of the file? */
FILE *infile; /* the input file pointer... */
struct xyzBuffer *xBuffer; /* the sorted x coordinates of the centers... */
struct xyzBuffer *yBuffer; /* the sorted y coordinates of the centers... */
struct xyzBuffer *zBuffer; /* the sorted z coordinates of the centers... */
infile = fopen(fnam,"r");
if (infile == NULL) {
fprintf(stderr,"Input file:%s cannot be read. Aborting...\n",
fnam);
exit(1);
}
count = 0;
while(fscanf(infile,"%lf",&(record[0])) == 1) {
fullrecord = 1;
for(i=1;i<18;i++) {
if (fscanf(infile,"%lf",&(record[i])) != 1) {
fullrecord = 0;
}
}
if (fullrecord) {
count++;
}
}
fclose(infile);
infile = fopen(fnam,"r");
*nCells = count;
*theCells = (struct foamCell **)malloc(count*sizeof(struct foamCell *));
if (*theCells == NULL) {
fprintf(stderr,"Malloc failure...\n");
}
for(i=0;i<count;i++) {
for(j=0;j<18;j++) {
fscanf(infile,"%lf",&(record[j]));
}