-
Notifications
You must be signed in to change notification settings - Fork 0
/
plequil.c
1467 lines (1453 loc) · 52.4 KB
/
plequil.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 "include.h"
#ifdef IRIS
#include <device.h>
#endif
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * P T R E L A X ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
* T O B E M O D I F I E D ! ! ! !
*
* Subroutine: ptrelax(short i, REAL *dx1, *dy1)
*
* Arguments: i = vertex to be relaxed
* (*dx1, *dy1) = suggested displacement to be added to
* vertex `i' in order to move it towards an
* equilibrium position
*
* Return value: none
*
* Action: This subroutine carries out a fundamental step in
* relaxing a vertex so that it obeys the wet foam
* equilibrium conditions. It `suggests' that the vertex
* `i' be displaced by the amount (*dx1, *dy1), in order
* come closer to an equilibrium configuration but it
* does not actually move the vertex (that is taken care
* of in the separate routine `vincrement()' called later
* on).
*
* The magnitude of the suggested displacement is highly
* unreliable however it invariably tends to point in the
* right direction. For this reason, the routine
* `vincrement()', which later actually moves the vertex,
* tends to limit the size of the suggested increment
* (*dx1, *dy1) while keeping the same direction.
*
* ALGORITHM:
* This routine tries to estimate a displacement of the
* vertex `i' which would place it in a position where the
* two border arcs incident at `i' line up smoothly with
* the cell-cell arc incident at `i'.
*
*
*****************************************************************************/
void ptrelax(i,dx1,dy1)
short i;
REAL *dx1, *dy1;
{
short c1, c2, ii, j, k;
REAL del, del2, x1, y1, x2, y2, x11, y11, x12, y12, p1, p2, pb, delp2,
dplus, dminus, xc1, yc1, xc2, yc2, rx1, ry1, rx12, ry12, alpha1, alpha2,
r12, rdot;
REAL bcangle1(), bcangle2(), barcangle(), v[2], m[2][2], linlen();
boolean la11, la12, larc(), arccentre(), arcbrk;
void matinv2(), vnbrxy(), bordpop();
del=lengthdelta; del2=del/2.0; delp2=pressuredelta/2.0;
x1=vx[i]; y1=vy[i];
vnbrxy(i,0,&x2,&y2); vnbrxy(i,1,&x11,&y11); vnbrxy(i,2,&x12,&y12);
c1=cadj[i][2]; c2=cadj[i][1];
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
la11=larc(i,1); la12=larc(i,2);
/* Now set up the vector angle discrepancies */
v[0]= PI-bcangle1(x12,y12,x1,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
v[0]= PI-bcangle1(x12,y12,x1,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
}
v[1]= PI-bcangle2(x11,y11,x1,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
v[1]= PI-bcangle2(x11,y11,x1,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
}
if (v[1]>v[0]) {
alpha1=barcangle(x1,y1,x11,y11,p1,pb,la11,&arcbrk);
alpha2=barcangle(x1,y1,x12,y12,pb,p2,la12,&arcbrk);
arccentre(x1,y1,x11,y11,alpha1,&xc1,&yc1);
arccentre(x1,y1,x12,y12,alpha2,&xc2,&yc2);
rx1=x1-xc1; ry1=y1-yc1;
rx12=xc2-xc1; ry12=yc2-yc1; r12=sqrt(rx12*rx12+ry12*ry12);
rx12 /= r12; ry12 /= r12;
rdot=rx1*rx12+ry1*ry12;
vx[i] += (rdot*rx12+xc1)-x1; vy[i] += (rdot*ry12+yc1)-y1;
x1=vx[i]; y1=vy[i];
v[0]= PI-bcangle1(x12,y12,x1,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
v[0]= PI-bcangle1(x12,y12,x1,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
}
v[1]= PI-bcangle2(x11,y11,x1,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
v[1]= PI-bcangle2(x11,y11,x1,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
}
}
if (linlen(x1,y1,x2,y2)<minvvlen) {
*dx1= *dy1=0.0; return;
}
dplus=bcangle1(x12,y12,x1+del2,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dplus=bcangle1(x12,y12,x1+del2,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
}
dminus=bcangle1(x12,y12,x1-del2,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dminus=bcangle1(x12,y12,x1-del2,y1,x2,y2,p2,p1,pb,la12,&arcbrk);
}
m[0][0]=(dplus-dminus)/del;
dplus=bcangle2(x11,y11,x1+del2,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dplus=bcangle2(x11,y11,x1+del2,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
}
dminus=bcangle2(x11,y11,x1-del2,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dminus=bcangle2(x11,y11,x1-del2,y1,x2,y2,p1,p2,pb,la11,&arcbrk);
}
m[1][0]=(dplus-dminus)/del;
dplus=bcangle1(x12,y12,x1,y1+del2,x2,y2,p2,p1,pb,la12,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dplus=bcangle1(x12,y12,x1,y1+del2,x2,y2,p2,p1,pb,la12,&arcbrk);
}
dminus=bcangle1(x12,y12,x1,y1-del2,x2,y2,p2,p1,pb,la12,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dminus=bcangle1(x12,y12,x1,y1-del2,x2,y2,p2,p1,pb,la12,&arcbrk);
}
m[0][1]=(dplus-dminus)/del;
dplus=bcangle2(x11,y11,x1,y1+del2,x2,y2,p1,p2,pb,la11,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dplus=bcangle2(x11,y11,x1,y1+del2,x2,y2,p1,p2,pb,la11,&arcbrk);
}
dminus=bcangle2(x11,y11,x1,y1-del2,x2,y2,p1,p2,pb,la11,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); la11=larc(i,1); la12=larc(i,2);
p1=cp[c1]; p2=cp[c2]; pb=bp[cadj[i][0]];
dminus=bcangle2(x11,y11,x1,y1-del2,x2,y2,p1,p2,pb,la11,&arcbrk);
}
m[1][1]=(dplus-dminus)/del;
matinv2(m);
*dx1=m[0][0]*v[0]+m[0][1]*v[1];
*dy1=m[1][0]*v[0]+m[1][1]*v[1];
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * M A T I N V ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: matinv2(REAL m[2][2])
*
* Arguments: m[2][2] = a 2x2 real matrix
*
* Return value: none
*
* Action: Inverts the given 2x2 real matrix (or returns an error
* if the matrix is uninvertible).
* Used by the subroutine `ptrelax()'.
*
*****************************************************************************/
void matinv2(m)
REAL m[2][2];
{
REAL x;
void plerror();
x=m[0][0]; m[0][0]=m[1][1]; m[1][1]=x;
m[0][1]= -m[0][1]; m[1][0]= -m[1][0];
x=m[0][0]*m[1][1]-m[0][1]*m[1][0];
if (x==0.0) {
plerror("uninvertible 2x2 matrix");
m[0][0]=0.0; m[0][1]=0.0; m[1][0]=0.0; m[1][1]=0.0;
}
else {
m[0][0] /= x; m[0][1] /= x; m[1][0] /= x; m[1][1] /= x;
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * E Q U I L ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: REAL equil(boolean adjustflag)
*
* Arguments: adjustflag = when set to TRUE, an `adjustment' is
* made to vertices instead of a full
* equilibration (usually passed as FALSE).
*
* Return value: Returns `sup' which is the largest increment given to
* any vertex during this equilibration step (thus it can
* be used to estimate how close the network has got to
* equilibrium).
*
* Action: This subroutine does one iteration of a froth
* equilibration. Here equilibrium is defined by two
* conditions:
* (i) The area of each cell `k' must match up with the
* target area `carea[k]+darea[k]'
* (ii) The slopes of all the arcs meeting at a point
* must be equal.
*
* The program also has an optional effect, selected by
* `adjustflag = TRUE', whereby the vertices will all be
* shifted by the amounts given in (dvx[], dvy[]) and
* some housekeeping tasks will be done. No equilibration
* is done in this mode.
*
* Usually the subroutine is called as `equil(0)' and the
* equilibration algorithms are also carried out. The
* algorithm carries out the following steps:
* (i) Area equilibration: Firstly the areas of the
* cells are equilibrated so that they equal
* `carea[i]+darea[i]' to a good degree of accuracy.
* This is done primarily by adjusting the pressures
* `cp[i]'. However, when a froth is very dry it is also
* necessary to adjust the pressures of the Plateau
* borders `bp[j]' -- the case of a dry froth is
* signalled by the flag `foamlike'.
* (ii) Fix the new Plateau border areas to be equal to
* their actual values
* (iii) Vertex equilibration: A *single* increment is
* worked out as a suggested change in position of the
* vertices (note that unlike `area equilibration' this is
* not iterated within this subroutine).
* (iv) Edge loss detection: It is worked out if the
* suggested movements of the vertices will give rise
* to any edges being lost -- these edges are recorded in
* `iel[]'.
* (v) Vertex incrementation: Then increments are given
* to the vertices by calling the subroutine
* `vincrement()'. The reason for being circumspect
* about this is that you can break the network by moving
* vertices.
* (vi) Border pinching: Now we deal with the first kind
* of topological change. If a Plateau border has more
* than three sides then two of its arcs can
* `pinch' together, thereby splitting off a new Plateau
* border. The subroutine `bpinch()' is called here both
* to test for and implement this change.
* (vii) Edge loss: The list of edges which were
* recorded in `iel[]' are now deleted. The nett effect
* of this is that Plateau borders will coalesce.
* (viii) Cell area update: The new geometrical areas
* of all the cells are recalculated and stored in
* `carea[]' (note that the target cell areas =
* `carea[i]+darea[i]' remain the same).
*
*
*****************************************************************************/
REAL equil(adjustflag)
boolean adjustflag;
{
/* This drives the equilibration and topological routines, doing
a single pass of the whole network. The value returned is a figure to
indicate how converged the network is.
The convergence error used is the supremum of the increments to the
vertices. */
short i, i1, i2, k, j, j1, k1, c1, c2, b, ii, icount;
REAL x1,y1,x11,y11,x12,y12,x2,y2,x21,y21,x22,y22,p1,p2,da1,da2,dp1,dp2,sup,xc,yc;
REAL prate, vrate, vvrate, maxdcp, maxcp, dcp1, delp, delp2, dadp, maxdafrac,
bmaxdafrac, daconverge, omaxdafrac=0.0, f, ca, cl;
REAL a1, a2, a3, dx, dy, lim, d, d1;
boolean la11, la12, la21, la22, arcbrk, stuck;
boolean found[MBORD];
void vnbrxy(), trans(), erelax(), elose(), bpinch(), normalizep(),
normalizeba();
void plerror(), ptrelax(), careaperim(), dvlimit(), cellpop(), bordpop();
short perconcat();
REAL cellarea(), bordarea(), fsign(), linlen();
boolean larc(), elost(), vincrement();
void foamplot();
/*
* Initialise:
*
* nel = number of edges to lost due to topological changes
* sup = size of largest suggested increment to a vertex
*/
nel=0; sup=0.0;
/*
* When the flag `adjustflag == TRUE' then the routine does *not* do
* a full equilibration. Instead it just increments the vertices by
* the amounts given in (dvx[], dvy[]) and does some housekeeping to
* make sure that the topology/geometry is not broken by these changes.
*
* This option is useful, for example, when you want to subject the whole
* network to Hencky strain -- the vertices of the network can be subjected
* to a linear transformation before the equilibration is begun.
*/
if (adjustflag) goto ADJUST;
/*
* Shorthand for a `small' amount of pressure delta p...
* used for calculating numerical derivatives w.r.t pressure etc.
*/
delp=pressuredelta; delp2=delp/2.0;
icount=0; maxdafrac=0.0; daconverge=0.0; prate=spdamp;
/*
* Adjust the total area of the network so that it fits within the given
* periodic box. This is just a housekeeping task in case this total
* area has drifted due to cumulative numerical error.
*/
normalizeba();
/*
* `do {} while' Loop for AREA EQUILIBRATION...
*
* The first step of equilibration is to adjust the pressures of all
* the cells and Plateau borders until all of the cell areas are
* equal to their target areas. This condition is imposed to a fairly high
* degree of accuracy, as experience shows it needs to be.
*/
do {
/*
* This loop can get stuck if the suggested pressure increments
* get too small. Use this flag to avoid getting trapped within this
* loop forever ! ...
*/
stuck=TRUE;
bmaxdafrac=0.0;
/*
* A froth is defined to be `foamlike' (actually, what is meant is `like
* a dry foam') if the Plateau borders are very small. The exact
* definition of `foamlike' is given in the routine `setconstants()'.
*
* When the Plateau borders are relatively small, the simulation behaves
* somewhat differently -- in particular here it is necessary to
* individually equilibrate Plateau borders (which is something we don't
* bother with when the borders are large).
*/
if (foamlike) {
for (i=0; i<onb; i++) found[i]=FALSE;
/*
* Loop around all Plateau borders and do:
* Numerically calculate the derivative
* dadp = d{area of Plateau border} / d{pressure of Plateau border}
* ...and use this to estimate an increment to border pressure via
* suggested dp = ({ideal border area} - {actual border area}) / dadp
*
* Note however, that {ideal border area} is not absolutely fixed (that
* would be physically unrealistic) but is reset to its actual value
* later on in this subroutine.
* Also calculate `bmaxdafrac', the maximum fractional change in area of
* a Plateau border, as a convergence criterion for area equilibration.
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
if (!found[b=cadj[i][0]]) {
found[b]=TRUE;
bp[b] += delp2;
a3=bordarea(i,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); a3=bordarea(i,&arcbrk);
}
bp[b] -= delp;
a1=bordarea(i,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); a1=bordarea(i,&arcbrk);
}
bp[b] += delp2;
a2=bordarea(i,&arcbrk);
if (arcbrk) {
bordpop(i,delp2); a2=bordarea(i,&arcbrk);
}
dadp=(a3-a1)/delp;
dp1=0.5*sbpdamp*(barea[b]-a2)/dadp;
bmaxdafrac=
(bmaxdafrac>(f=fabs((a2-barea[b])/barea[b])))?bmaxdafrac : f;
if (fabs(dp1)>delp) {
stuck=FALSE;
/* Restrict increments to 0.1 of 'bpav' */
if (fabs(dp1/bpav)>0.1) dp1= fsign(dp1)*0.1*fabs(bpav);
bp[b] += dp1;
}
}
}
}
/*
* Clear array `found[]' *and* array `dcp[]' which will store
* suggested changes in cell pressure (note that the suggested
* changes to a cell's pressure will come not just from the cell itself
* but also from its immediate neighbours).
*/
for (i=0; i<onc; i++) { found[i]=FALSE; dcp[i]=0.0; }
/*
* Loop around all the cells:
*
* Calculate the numerical derivative of each cell
* dadp = d{area of cell} / d{pressure of cell}
* ...and then estimate the amount by which to change pressure
* suggested dcp = 0.5*({target area} - {actual area})/dadp
* The factor of `0.5' comes in because the cell itself has its
* pressure *increased* by half the amount, while its neighbours
* have their pressure *decreased* so as to account for the rest
* (roughly).
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
for (k=1; k<3; k++) {
if (!found[c1=cadj[i][k]]) {
found[c1]=TRUE;
a2=cellarea(i,k,&arcbrk);
if (arcbrk) {
cellpop(i,k,delp2); a2=cellarea(i,k,&arcbrk);
}
darea[c1]=carea[c1]+darea[c1]-a2;
carea[c1]=a2;
/* a2=carea[c1]; */
cp[c1] += delp2;
a3=cellarea(i,k,&arcbrk);
if (arcbrk) {
cellpop(i,k,delp2); a3=cellarea(i,k,&arcbrk);
}
cp[c1] -= delp;
a1=cellarea(i,k,&arcbrk);
if (arcbrk) {
cellpop(i,k,delp2); a1=cellarea(i,k,&arcbrk);
}
cp[c1] += delp2;
dadp=(a3-a1)/delp;
if (dadp<0.0) {
/* plerror("negative area derivative in routine equil"); */
/* this is actually correct in some cases where a pb arc */
/* has popped. */
}
dp1=0.5*darea[c1]/dadp;
/* the factor of 0.5 comes in here because it is assumed that, */
/* on average, its nbrs provide the other half of the required */
/* change in pressure difference. */
if (/* (-cp[c1]/bpav)<0.1 && */ fabs(dp1)>delp) {
stuck=FALSE;
dcp[c1] += dp1;
/* Now change the pressure of its nbrs by a complementary amount */
dp1= -dp1/((REAL) ncsides[c1]);
if (k==2) { i=vnbr[i][0]; k=1; }
j=i;
}
}
}
}
/* Now to determine what fraction of the calculated increments ought */
/* to be applied */
/*
* First get the largest cell pressure increment...
*/
maxdcp=0.0;
for (ii=0; ii<nc; ii++) {
i=clist[ii];
maxdcp= (maxdcp > (dcp1=fabs(dcp[i]))) ? maxdcp : dcp1;
}
/*
* ... then restrict the largest pressure change to 0.01 of 'bpav'
*/
if (fabs(maxdcp/bpav)<0.01) prate=spdamp;
else {
prate=spdamp*fabs(0.01*bpav/maxdcp);
}
/*
* Now decrease the pressure increments by 'prate' and add them
* to the cell pressures `cp[]'
*/
maxdcp *= prate;
for (ii=0; ii<nc; ii++) {
i=clist[ii];
dcp[i] *= prate;
cp[i] += dcp[i];
}
/*
* Now correct the areas of the cells...
*
* ...and calculate `maxdafrac', the maximum fractional change in area,
* as a convergence criterion for the area equilibration.
*/
omaxdafrac=maxdafrac; maxdafrac=0.0; maxcp=0.0;
for (i=0; i<onc; i++) found[i]=FALSE;
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
for (k=1; k<3; k++) {
if (!found[c1=cadj[i][k]]) {
found[c1]=TRUE;
da1=cellarea(i,k,&arcbrk);
if (arcbrk) { cellpop(i,k,delp2); da1=cellarea(i,k,&arcbrk); }
da1 -= carea[c1];
carea[c1] += da1; darea[c1] -= da1;
maxdafrac= (maxdafrac>(f=fabs(darea[c1]/carea[c1]))) ? maxdafrac : f;
maxcp= (maxcp>(p1=cp[c1])) ? maxcp : p1;
}
}
}
daconverge= (omaxdafrac==0.0) ? 0.0 : maxdafrac/omaxdafrac;
icount++;
// foamplot(0,0);
} while (((maxdafrac>areasup) || (bmaxdafrac>bareasup)) && !stuck);
/*
* ...finished AREA EQUILIBRATION.
*
* Now shift all pressures in the system so that the average cell area
* works out to be zero (just a handy convention).
*/
normalizep();
/*
* Fix the new border areas at the present values
* (as we promised we would earlier on)
*/
for (i=0; i<onb; i++) found[i]=FALSE;
for (ii=0; ii<nb; ii++) {
i=vlist[ii];
if (!found[b=cadj[i][0]]) {
found[b]=TRUE;
da1=bordarea(i,&arcbrk);
if (arcbrk) { bordpop(i,delp2); da1=bordarea(i,&arcbrk); }
barea[b]=da1;
}
}
/*
* Now do VERTEX EQUILIBRATION...
*
* The key task is performed by `ptrelax()' which suggests
* the increments (dvx[], dvy[]) which should be made to
* relax a vertex.
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
ptrelax(i,&dvx[i],&dvy[i]);
}
ADJUST : ;
if (adjustflag) { vrate=1.0; vvrate=1.0; }
else if (foamlike) { vrate=0.5*svdamp; vvrate=0.5*svvdamp; }
else { vrate = vvrate = 0.5*svvdamp; }
/* the factor of 0.5 anticipates the fact that the increments */
/* tend to overshoot */
/*
* Calculate the convergence criterion `sup'...
* (which is the largest suggested increment to a vertex)
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
sup= (sup>fabs(dvx[i])) ? sup : fabs(dvx[i]);
sup= (sup>fabs(dvy[i])) ? sup : fabs(dvy[i]);
}
if (adjustflag) goto ADJUST1;
for (i=0; i<onb; i++) found[i]=FALSE;
/*
* Turn the suggested increments (dvx[], dvy[]) into reasonable
* sized increments. As was noted in the routine `ptrelax()', the
* increments which are returned are often unreasonably large
* (e.g. when the vertex is far from equilibrium) although they tend
* to point in the right direction. Therefore, we limit the size of
* the increments here...
*
* In particular, `vvrate' is the usual rate of damping of a vertex
* increment. Additionally, an absolute upper limit to the increment
* size is imposed by the subroutine `dvlimit()'.
*
* Note that three-sided Plateau borders get special treatment. There
* are two rates of damping used -- `vrate', gentle damping, for the
* motion of the centre of mass of the 3-border; and `vvrate', strong
* damping, used for the relative motion of the vertices within the
* 3-border. This allows networks with small 3-sided Plateau borders
* to equilibrate more rapidly.
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
if (!found[b=cadj[i][0]]) {
found[b]=TRUE;
if (nbsides[b]>3) {
dvx[i] *= vvrate; dvy[i] *= vvrate;
dvlimit(maxdvv,i,dvx,dvy);
}
else { /* 3-sided borders get special treatment! */
/* Essentially, the average motion of the 3-border is damped */
/* by 'vrate', while the distortion of the 3-border is */
/* damped by 'vvrate'. */
i1=vnbr[i][1]; i2=vnbr[i][2];
dx=(dvx[i]+dvx[i1]+dvx[i2])/3.0;
dy=(dvy[i]+dvy[i1]+dvy[i2])/3.0;
dvx[i] -= dx; dvx[i1] -= dx; dvx[i2] -= dx;
dvy[i] -= dy; dvy[i1] -= dy; dvy[i2] -= dy;
dx *= vrate; dy *= vrate;
lim=maxdv;
if (fabs(dx)>fabs(dy)) {
if (fabs(dx)>lim) {
dx = fsign(dx)*lim;
dy *= lim/fabs(dx);
}
}
else {
if (fabs(dy)>lim) {
dx *= lim/fabs(dy);
dy = fsign(dy)*lim;
}
}
dvx[i] *= vrate; dvx[i1] *= vrate; dvx[i2] *= vrate;
dvy[i] *= vrate; dvy[i1] *= vrate; dvy[i2] *= vrate;
dvlimit(maxdvv,i,dvx,dvy);
dvx[i] += dx; dvx[i1] += dx; dvx[i2] += dx;
dvy[i] += dy; dvy[i1] += dy; dvy[i2] += dy;
}
}
}
#ifndef BACKGROUND
if (sup>0.2) {
/* printf("warning: very large increment occurred\n"); */
}
#endif
ADJUST1 : ;
/*
* Now calculate a suitable increment for the vertices of a one-sided cell.
* They are moved more or less in parallel to the their neighbouring
* vertices (ensuring that the one-sided cell remains attatched to its
* neighbour).
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
if (ncsides[cadj[i][1]]==1) {
j=vnbr[i][0]; i1=vnbr[i][1]; j1=vnbr[j][2];
x1=vx[i]; y1=vy[i]; vnbrxy(i,1,&x11,&y11);
vnbrxy(i,0,&x2,&y2);
k=perconcat(vper[i][0],vper[j][2]);
trans(vx[j1],vy[j1],k,&x22,&y22);
dx=dvx[j1]-dvx[i1]; dy=dvy[j1]-dvy[i1];
dvx[i]=dvx[j]=dvx[i1]; dvy[i]=dvy[j]=dvy[i1];
d=linlen(x11,y11,x22,y22); d1=linlen(x11,y11,x1,y1);
f=d1/d;
dvx[i] += f*dx; dvy[i] += f*dy;
d1=linlen(x11,y11,x2,y2);
f=d1/d;
dvx[j] += f*dx; dvy[j] += f*dy;
}
}
/*
* Now test for lost edges
*/
nel=0;
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
j=vnbr[i][0];
if (j>i) {
/* avoids double counting */
x1=vx[i]; y1=vy[i]; vnbrxy(i,0,&x2,&y2);
if (elost(x2-x1,y2-y1,dvx[j]-dvx[i],dvy[j]-dvy[i])) {
/*
* Add it to the list of cell edges to be deleted...
*/
iel[nel]=i; nel++;
}
}
}
/*
* At last! Add the increments to the vertices...
*/
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
/* takes care of some knotty eventualities, even calling itself. */
vincrement(i,dvx[i],dvy[i]);
}
/*
* Loop over all Plateau borders, calling routine `bpinch()' for each
* one. Routine `bpinch()' tests for the second kind of topological
* change, where two arcs of a Plateau border `pinch' together, thus
* splitting off a piece of the Plateau border.
*/
if (!notopol) {
for (b=0; b<onb; b++) found[b]=FALSE;
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
b=cadj[i][0];
if (!found[b]) {
found[b]=TRUE;
if (nbsides[b]>3) bpinch(i);
}
}
}
/*
* Now implement the lost edges.
* NB: a maximum of one edge adjacent to a given cell is lost,
* since this aids stability
*/
if (!notopol) {
for (i=0; i<onc; i++) found[i]=FALSE;
for (ii=0; ii<nel; ii++) {
i=iel[ii];
if (!found[cadj[i][1]] && !found[cadj[i][2]]) {
found[cadj[i][1]]=TRUE; found[cadj[i][2]]=TRUE;
elose(i);
}
}
}
/*
* Now recalculate the cell areas and calculate the network energy
* by adding up the total perimeter length of the froth...
*/
maxdafrac=0.0; netenergy=0.0; minenergy=0.0;
for (i=0; i<onc; i++) found[i]=FALSE;
for (ii=0; ii<nv; ii++) {
i=vlist[ii];
for (k=1; k<3; k++) {
if (!found[c1=cadj[i][k]]) {
found[c1]=TRUE;
careaperim(i,k,TRUE,TRUE,FALSE,&ca,&cl,&xc,&yc,&arcbrk);
if (arcbrk) {
cellpop(i,k,delp2); careaperim(i,k,TRUE,TRUE,FALSE,&ca,&cl,&xc,&yc,&arcbrk);
}
netenergy += cl;
da1=ca-carea[c1];
carea[c1] += da1; darea[c1] -= da1;
minenergy += 2.0*sqrt(M_PI*carea[c1]);
maxdafrac= (maxdafrac>(f=fabs(darea[c1]/carea[c1]))) ? maxdafrac : f;
}
}
}
return sup;
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * D V L I M I T ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: dvlimit(REAL lim, short i, REAL dvx[], dvy[])
*
* Arguments: lim = size of maximum allowed vertex increment
* i = index of vertex lying on the particular
* Plateau border in question
* (dvx[], dvy[]) = array of vertex increments
*
* Return value: none
*
* Action: Limits the maximum size of a vertex increment to `lim'.
* In particular, it takes the Plateau border selected
* via vertex index `i'; runs around the vertices
* lying on the Plateau border and finds the size of the
* largest of all the increments; it then proportionally
* reduces the increments to all of the vertices so
* that the largest becomes equal to `lim'.
*
* The reason for treating a whole Plateau border all in
* one go, instead of individually limiting increments
* to vertices, is that we want to avoid dramatically
* distorting a Plateau border.
*
*****************************************************************************/
void dvlimit(lim,i,dvx,dvy)
short i;
REAL lim, dvx[], dvy[];
{
short j, je;
REAL sup, m, f, fsign();
/* First of all find the largest increment: */
sup=0.0;
je=j=i;
do {
m=max(fabs(dvx[j]),fabs(dvy[j]));
sup=max(m,sup);
j=vnbr[j][1];
} while (j != je);
/* Then impose 'lim' as the largest increment, shrinking all increments */
/* in proportion */
if (sup>lim) {
f=lim/sup;
je=j=i;
do {
dvx[j] *= f; dvy[j] *= f;
j=vnbr[j][1];
} while (j != je);
}
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * E L O S T ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: elost(REAL rx, ry, dx, dy)
*
* Arguments: (rx, ry) = the vector joining the endpoints of edge
* (dx, dy) = proposed total increment to edge vector
*
* Return value: TRUE if the edge is to be lost, else FALSE
*
* Action: Test whether the given edge disappears once the
* proposed vertex increments are made. If the component
* of the increment vector lying along the edge vector is
* both negative and bigger than the edge vector, then
* the edge is lost. Alternatively if the edge has
* simply got very small (less than `minvvlen') then it
* is also lost.
*
*****************************************************************************/
boolean elost(rx,ry,dx,dy)
REAL rx,ry,dx,dy;
{
REAL rr;
REAL minvv2=0.0;
minvv2=minvvlen*minvvlen;
rr=rx*rx+ry*ry;
return (rr< -(rx*dx+ry*dy) || rr<minvv2);
}
/*****************************************************************************
*** * * * * * * * * * * * * ***
*** * * * * * * E L O S E ( ) * * * * * * ***
*** * * * * * * * * ***
*****************************************************************************
*
* Subroutine: elose(short k)
*
* Arguments: k = index of one of the vertices on the edge
*
* Return value: none
*
* Action: First of all the two merging borders are moved
* together so as to close the small gap along the
* disappearing edge. The algorithm then carries out the
* necessary bookkeeping to make the edge disappear.
*
* There are two special cases which arise. At the outset
* the routine checks if, by removing this edge, a
* percolating Plateau border would be created. The
* program stops if this is the case. The other special
* case is when either of the cells adjacent to the edge
* are single-sided. In that case the edge is left alone
* since we do not want to cut off the cell completely
* from the network (which would oblige us to deal with
* a new object, the isolated bubble).
*
*****************************************************************************/
void elose(k)
short k;
{
short i, ii, j, jj, kk, i1, j1, b, b1, c1, c2, ns, iseed, iseed1, iseed2;
REAL dx, dy, x1, y1, x2, y2, p, pb, alpha, f, d, r;
boolean la, la1, arcbrk;
char s[80];
void cforgetindex(), trans(), setlarc(), vnbrxy(), plerror(), bforgetindex(),
vforgetindex(), setconstants(), bublistindex(), cellpop(), bordpop(),
clusterplot(), foamout(), infoclose();
short perconcat(), bgetindex();
REAL barcangle(), linlen(), bordarea();
boolean vincrement(), larc(), perculates();
/*
* The first special case:
*
* One-sided cells do not have their last edge removed,
* and this is because we do not want to deal with isolated
* bubbles. So just return...
*/
if (ncsides[cadj[k][1]]==1 || ncsides[cadj[k][2]]==1) return;
/*
* Set up the basic objects we are dealing with, for example
* (x1, y1) and (x2, y2) are the endpoints of the edge to be removed
*/
iseed1= -1; iseed2= -1;
kk=vnbr[k][0];
x1=vx[k]; y1=vy[k]; vnbrxy(k,0,&x2,&y2);
b=cadj[k][0]; b1=cadj[kk][0];
c1=cadj[k][1]; c2=cadj[k][2];
pb=0.5*(bp[b]+bp[b1]);
/*
* This is the second special case:
*
* If the two Plateau borders which are about to merge are,
* in fact, one and the same Plateau border it indicates that
* the froth is about to be broken apart by this percolating
* Plateau border.
*/
if (b==b1) { /* implies that the foam is breaking off a piece */
if (perculates(k)) {
sprintf(s,"perculation occured next to vertex %d",k); plerror(s);
fflush(stdout);
clusterplot(k);
foamout("fmperc.pb");
#ifdef IRIS
sleep(30);
#endif
infoclose();
exit(0);
}
else return;
}
/*
* The usual case:
*
* Start by moving the two Plateau borders up alongside
* each other so that we lose the edge gently. Note that
* the *entire* Plateau borders are moved so that the endpoints
* lie on top of each other.
*/
i=k; j=kk;
dx=0.5*(x2-x1)-dvx[i]; dy=0.5*(y2-y1)-dvy[i];
i1=i;
do {
dvx[i] += dx; dvy[i] += dy;
i=vnbr[i][1];
} while (i != i1);
dx=0.5*(x1-x2)-dvx[j]; dy=0.5*(y1-y2)-dvy[j];
j1=j;
do {
dvx[j] += dx; dvy[j] += dy;
j=vnbr[j][1];
} while (j != j1);
/*
* Deal with topological changes on the c1 side.
*
* This involves some straightforward bookkeeping to
* join two Plateau border arcs together.
*/
if (vnbr[k][2]==kk) {
/* Do nothing! */ ;
/*
* This is where an isolated bubble would be created
* if we were interested in making them!
*/
}
else {
iseed=iseed1=i=vnbr[k][2]; ii=vnbr[kk][1];
la=larc(i,1); la1=larc(kk,1);
p=cp[c1];
x1=vx[i]; y1=vy[i];
i1=vper[i][1]; trans(vx[k],vy[k],i1,&x2,&y2);
alpha=barcangle(x1,y1,x2,y2,p,pb,la,&arcbrk);
if (arcbrk) {
cellpop(k,1,pressuredelta/2.0);
alpha=barcangle(x1,y1,x2,y2,p,pb,la,&arcbrk);
}
x1=x2; y1=y2;
i1=perconcat(i1,vper[k][0]); i1=perconcat(i1,vper[kk][1]);
trans(vx[ii],vy[ii],i1,&x2,&y2);
alpha += barcangle(x1,y1,x2,y2,p,pb,la1,&arcbrk);
if (arcbrk) {
cellpop(k,1,pressuredelta/2.0);
alpha += barcangle(x1,y1,x2,y2,p,pb,la1,&arcbrk);
}
/* Now do updates */
vnbr[i][1]=ii; vnbr[ii][2]=i;
vper[i][1]=i1;
vper[ii][2]=PERFN(-PERX(i1),-PERY(i1));
setlarc(&vper[i][1],(alpha>PI));
setlarc(&vper[ii][2],(alpha>PI));
x1=vx[i]; y1=vy[i];
vnbrxy(i,1,&x2,&y2);
d=linlen(x1,y1,x2,y2); r=BRADIUS(p,pb);
if (2.0*r*cosminang<d) cellpop(i,2,pressuredelta/2.0);
}
/*
* Deal with topological changes on the c2 side.
*
* This involves some straightforward bookkeeping to
* join two Plateau border arcs together.
*/
if (vnbr[k][1]==kk) {
/* Do nothing! */ ;
/*
* This is where an isolated bubble would be created
* if we were interested in making them!
*/
}
else {
iseed=iseed2=j=vnbr[kk][2]; jj=vnbr[k][1];
la=larc(j,1); la1=larc(k,1);
p=cp[c2];
x1=vx[j]; y1=vy[j];
j1=vper[j][1]; trans(vx[kk],vy[kk], j1,&x2,&y2);
alpha=barcangle(x1,y1,x2,y2,p,pb,la,&arcbrk);
if (arcbrk) {
cellpop(k,2,pressuredelta/2.0);
alpha=barcangle(x1,y1,x2,y2,p,pb,la,&arcbrk);
}
x1=x2; y1=y2;
j1=perconcat(j1,vper[kk][0]); j1=perconcat(j1,vper[k][1]);
trans(vx[jj],vy[jj],j1,&x2,&y2);
alpha += barcangle(x1,y1,x2,y2,p,pb,la1,&arcbrk);
if (arcbrk) {
cellpop(k,2,pressuredelta/2.0);
alpha += barcangle(x1,y1,x2,y2,p,pb,la1,&arcbrk);
}
/* Now do updates */
vnbr[j][1]=jj; vnbr[jj][2]=j;
vper[j][1]=j1;