-
Notifications
You must be signed in to change notification settings - Fork 3
/
dom.c
4214 lines (3466 loc) · 115 KB
/
dom.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
/*
* dom.c
* Copyright (C) 2008, Tomasz Koziara (t.koziara AT gmail.com)
* ---------------------------------------------------------------
* a domain gathers bodies and constraints
*/
/* This file is part of Solfec.
* Solfec is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Solfec 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Solfec. If not, see <http://www.gnu.org/licenses/>. */
#include <string.h>
#include <limits.h>
#include <float.h>
#include "sol.h"
#include "alg.h"
#include "msh.h"
#include "cvx.h"
#include "set.h"
#include "dom.h"
#include "goc.h"
#include "tmr.h"
#include "pck.h"
#include "err.h"
#include "dio.h"
#include "cra.h"
#include "fra.h"
#include "psc.h"
#include "lng.h"
#if MPI
#include "put.h"
#include "com.h"
#endif
#if OMP
#include <omp.h>
#include "ompu.h"
#endif
#define CONBLK 128 /* constraints memory block size */
#define MAPBLK 128 /* map items memory block size */
#define SETBLK 128 /* set items memory block size */
/* excluded surface pairs comparison */
static int pair_compare (int *a, int *b)
{
if (a[0] < b[0]) return -1;
else if (a[0] == b[0] && a[1] < b[1]) return -1;
else if (a[0] == b[0] && a[1] == b[1]) return 0;
else return 1;
}
/* create aabb data */
static AABB_DATA* aabb_create_data (void)
{
AABB_DATA *data;
ERRMEM (data = malloc (sizeof (AABB_DATA)));
data->aabb_limits [0] = 0.0;
data->aabb_counter = 0;
data->aabb_algo = 0;
return data;
}
/* free aabb data */
static void aabb_destroy_data (AABB_DATA *data)
{
free (data);
}
/* fastest box overlap algorithm */
static BOXALG aabb_algorithm (DOM *dom)
{
#if 0
AABB_DATA *data = dom->aabb_data;
double num, *tim, *lim;
int i;
if (data->aabb_counter < BOXALG_COUNT)
{
data->aabb_algo = data->aabb_counter ++; /* at first test all algorithms */
}
else /* when all have been tested */
{
tim = data->aabb_timings;
lim = data->aabb_limits;
for (i = 0; i < BOXALG_COUNT; i ++) lim [i+1] = lim [i] + tim [i]; /* sum up */
for (i = 1; i <= BOXALG_COUNT; i ++) lim [i] /= lim [BOXALG_COUNT]; /* normalize */
num = DRAND(); /* random in [0, 1] */
for (i = 0; i < BOXALG_COUNT; i ++)
{
if (num >= lim [i] && num < lim [i+1])
{
data->aabb_algo = i;
return i;
}
}
}
return data->aabb_algo;
#else
return HYBRID; /* TODO: remove when the above proves robust */
#endif
}
/* update aabb timing related data */
static void aabb_timing (DOM *dom, double timing)
{
AABB_DATA *data = dom->aabb_data;
data->aabb_timings [data->aabb_algo] = timing;
}
/* calculate orthonormal
* base storing it in column-wise manner
* in 'loc' with 'n' as the last column */
static void localbase (double *n, double *loc)
{
double len,
e [2][3] = { {1., 0., 0.},
{0., 1., 0.} };
loc [6] = n [0];
loc [7] = n [1];
loc [8] = n [2];
PRODUCT (e [0], n, loc);
if ((len = LEN (loc)) < GEOMETRIC_EPSILON)
{
PRODUCT (e [1], n, loc);
len = LEN (loc);
}
loc [0] /= len;
loc [1] /= len;
loc [2] /= len;
PRODUCT (loc, n, (loc + 3));
}
/* compare two constraints */
#define CONCMP ((SET_Compare) constraint_compare)
static int constraint_compare (CON *one, CON *two)
{
short oc = (one->kind == CONTACT), tc = (two->kind == CONTACT);
BODY *onebod [2], *twobod [2];
SGP *onesgp [2], *twosgp [2];
if (oc > tc) return -1; /* contacts are smaller than non-contacts */
else if (oc < tc) return 1; /* -||- */
else if ((oc + tc) == 0) return (one->id < two->id ? -1 : (one->id == two->id ? 0 : 1)); /* non-contacts: compare IDs */
if (one->master < one->slave) /* contacts: order pointers before comparing */
{
onebod [0] = one->master;
onesgp [0] = one->msgp;
onebod [1] = one->slave;
onesgp [1] = one->ssgp;
}
else if (one->master > one->slave)
{
onebod [0] = one->slave;
onesgp [0] = one->ssgp;
onebod [1] = one->master;
onesgp [1] = one->msgp;
}
else
{
onebod [0] = onebod [1] = one->master;
if (one->msgp < one->ssgp)
{
onesgp [0] = one->msgp;
onesgp [1] = one->ssgp;
}
else
{
onesgp [0] = one->ssgp;
onesgp [1] = one->msgp;
}
}
if (two->master < two->slave)
{
twobod [0] = two->master;
twosgp [0] = two->msgp;
twobod [1] = two->slave;
twosgp [1] = two->ssgp;
}
else if (two->master > two->slave)
{
twobod [0] = two->slave;
twosgp [0] = two->ssgp;
twobod [1] = two->master;
twosgp [1] = two->msgp;
}
else
{
twobod [0] = twobod [1] = two->master;
if (two->msgp < two->ssgp)
{
twosgp [0] = two->msgp;
twosgp [1] = two->ssgp;
}
else
{
twosgp [0] = two->ssgp;
twosgp [1] = two->msgp;
}
}
if (onebod [0] < twobod [0]) return -1; /* compare lexicographically by body pointers and sgps */
else if (onebod [0] == twobod [0])
{
if (onebod [1] < twobod [1]) return -1;
else if (onebod [1] == twobod [1])
{
if (onesgp [0] < twosgp [0]) return -1;
else if (onesgp [0] == twosgp [0])
{
if (onesgp [1] < twosgp [1]) return -1;
else if (onesgp [1] == twosgp [1]) return 0;
}
}
}
return 1;
}
/* insert a new constrait between two bodies */
static CON* insert (DOM *dom, BODY *master, BODY *slave, SGP *msgp, SGP *ssgp, short kind)
{
CON *con;
/* ensure that master pointer was passed;
* tollerate NULL slave pointers, as this indicates a single body constraint;
* note that msgp may be NULL for dummy bodies created in get_body function */
ASSERT_DEBUG (master, "At least master body pointers must be passed");
ERRMEM (con = MEM_Alloc (&dom->conmem));
con->kind = kind;
con->master = master;
con->slave = slave;
con->msgp = msgp;
con->ssgp = ssgp;
/* insert into list */
con->next = dom->con;
if (dom->con) dom->con->prev = con;
dom->con = con;
dom->ncon ++;
#if MPI
if (dom->noid == 0) /* if id generation is enabled */
{
#endif
SET *item = SET_First (dom->sparecid);
if (item)
{
con->id = (unsigned int) (long) item->data; /* use a previously freed id */
SET_Delete (&dom->setmem, &dom->sparecid, item->data, NULL);
}
else
{
con->id = dom->cid;
#if MPI
ASSERT (((unsigned long long) dom->cid) + ((unsigned long long) dom->ncpu) < UINT_MAX, ERR_DOM_TOO_MANY_CONSTRAINTS);
dom->cid += dom->ncpu; /* every ncpu number */
#else
ASSERT (((unsigned long long) dom->cid) + ((unsigned long long) 1) < UINT_MAX, ERR_DOM_TOO_MANY_CONSTRAINTS);
con->id = dom->cid ++;
#endif
}
#if MPI
}
else con->id = dom->noid; /* assign the 'noid' as it was imported with a constraint */
#endif
#if PARDEBUG
ASSERT_DEBUG (!SET_Contains (master->con, con, CONCMP), "Constraint %s with id %d already in body list", CON_Kind (con), con->id);
ASSERT_DEBUG (!slave || (slave && !SET_Contains (slave->con, con, CONCMP)), "Constraint %s with id %d already in body list", CON_Kind (con), con->id);
#endif
/* insert into body constraint adjacency */
SET_Insert (&dom->setmem, &master->con, con, CONCMP);
if (slave) SET_Insert (&dom->setmem, &slave->con, con, CONCMP);
#if PARDEBUG
ASSERT_DEBUG (SET_Contains (master->con, con, CONCMP), "Failed to insert constraint %s with id %d into body list", CON_Kind (con), con->id);
ASSERT_DEBUG (!slave || (slave && SET_Contains (slave->con, con, CONCMP)), "Failed to insert constraint %s with id %d already into body list", CON_Kind (con), con->id);
#endif
/* map by id */
MAP_Insert (&dom->mapmem, &dom->idc, (void*) (long) con->id, con, NULL);
return con;
}
/* insert a contact into the constraints set */
static CON* insert_contact (DOM *dom, BODY *master, BODY *slave, SGP *msgp, SGP *ssgp, double *mpntspa,
double *spntspa, double *normal, double area , double gap, SURFACE_MATERIAL *mat, short paircode)
{
CON *con;
con = insert (dom, master, slave, msgp, ssgp, CONTACT); /* do not insert into LOCDYN yet, only after sparsification */
COPY (mpntspa, con->point);
BODY_Ref_Point (master, msgp, mpntspa, con->mpnt); /* referential image */
BODY_Ref_Point (slave, ssgp, spntspa, con->spnt);
localbase (normal, con->base);
con->area = area;
con->gap = gap;
con->paircode = paircode;
con->state |= SURFACE_MATERIAL_Transfer (dom->time, mat, &con->mat); /* transfer surface pair data from the database to the local variable */
con->state |= CON_NEW; /* mark as newly created */
return con;
}
/* does a potential contact already exists ? */
static int contact_exists (BOX *one, BOX *two)
{
CON aux;
aux.kind = CONTACT;
aux.master = one->body;
aux.msgp = one->sgp;
aux.slave = two->body;
aux.ssgp = two->sgp;
return SET_Contains (one->body->con, &aux, CONCMP);
}
/* box overlap creation callback */
static void overlap_create (DOM *dom, BOX *one, BOX *two)
{
double onepnt [3], twopnt [3], normal [3], gap, area;
int state, spair [2], pair [2], ntri;
SURFACE_MATERIAL *mat;
short paircode;
CON *con;
TRI *tri;
if (contact_exists (one, two)) return;
state = gobjcontact (
CONTACT_DETECT, GOBJ_Pair_Code (one, two),
one->sgp->shp, one->sgp->gobj,
two->sgp->shp, two->sgp->gobj,
onepnt, twopnt, normal,
&gap, &area, spair, &tri, &ntri);
if (state)
{
#if OMP
omp_set_lock (&dom->lock);
#endif
ASSERT_DEBUG (gap <= 0, "A contact with positive gap (%g) was detected which indicates a bug in goc.c", gap);
if (gap <= dom->depth) dom->flags |= DOM_DEPTH_VIOLATED;
/* set surface pair data if there was a contact */
mat = SPSET_Find (dom->sps, spair [0], spair [1]);
if (dom->excluded)
{
if (spair [0] <= spair [1]) { pair [0] = spair [0]; pair [1] = spair [1]; }
else { pair [0] = spair [1]; pair [1] = spair [0]; }
if (SET_Contains (dom->excluded, pair, (SET_Compare) pair_compare))
{
free (tri);
return; /* exluded pair */
}
}
switch (state)
{
case 1: /* first body has outward normal => second body is the master */
{
paircode = GOBJ_Pair_Code (one, two);
con = insert_contact (dom, two->body, one->body, two->sgp, one->sgp, twopnt, onepnt, normal, area, gap, mat, paircode);
con->spair [0] = spair [0];
con->spair [1] = spair [1];
}
break;
case 2: /* second body has outward normal => first body is the master */
{
paircode = GOBJ_Pair_Code (two, one);
con = insert_contact (dom, one->body, two->body, one->sgp, two->sgp, onepnt, twopnt, normal, area, gap, mat, paircode);
con->spair [0] = spair [1];
con->spair [1] = spair [0];
}
break;
}
#if OMP
omp_unset_lock (&dom->lock);
#endif
}
if (tri) free (tri);
}
#if MPI
/* schedule remote deletion of external constraints */
static void ext_to_remove (DOM *dom, CON *con)
{
for (SET *item = SET_First (con->ext); item; item = SET_Next (item))
{
SET_Insert (&dom->setmem, &dom->dbd [(int) (long) item->data].remove, (void*) (long) con->id, NULL);
}
}
#endif
/* update contact data */
static void update_contact (DOM *dom, CON *con)
{
double mpnt [3], spnt [3], normal [3];
void *mgobj = mgobj(con),
*sgobj = sgobj(con);
SHAPE *mshp = mshp(con),
*sshp = sshp(con);
int state, ntri;
TRI *tri;
/* current spatial points and normal */
BODY_Cur_Point (con->master, con->msgp, con->mpnt, mpnt);
BODY_Cur_Point (con->slave, con->ssgp, con->spnt, spnt);
COPY (con->base+6, normal);
/* update contact data => during an update 'master' and 'slave' relation does not change */
state = gobjcontact (
CONTACT_UPDATE, con->paircode,
sshp, sgobj, mshp, mgobj, /* the slave body holds the outward normal */
spnt, mpnt, normal, &con->gap, /* 'mpnt' and 'spnt' are updated here */
&con->area, con->spair, &tri, &ntri); /* surface pair might change though */
if (state || (con->state & CON_COHESIVE))
{
if (con->state & CON_COHESIVE) /* reuse original point */
{
BODY_Cur_Point (con->master, con->msgp, con->mpnt, con->point);
localbase (normal, con->base); /* but update normal (the interface might rotate) */
}
else
{
if (con->gap <= dom->depth) dom->flags |= DOM_DEPTH_VIOLATED;
COPY (mpnt, con->point);
BODY_Ref_Point (con->master, con->msgp, mpnt, con->mpnt);
BODY_Ref_Point (con->slave, con->ssgp, spnt, con->spnt);
localbase (normal, con->base);
if (state > 1) /* surface pair has changed */
{
SURFACE_MATERIAL *mat = SPSET_Find (dom->sps, con->spair [0], con->spair [1]); /* find new surface pair description */
con->state |= SURFACE_MATERIAL_Transfer (dom->time, mat, &con->mat); /* transfer surface pair data from the database to the local variable */
}
}
}
else
{
#if MPI
ext_to_remove (dom, con); /* schedule remote deletion of external constraints */
#endif
DOM_Remove_Constraint (dom, con); /* remove from the domain */
}
if (tri) free (tri);
}
/* update fixed point data */
static void update_fixpnt (DOM *dom, CON *con)
{
BODY_Cur_Point (con->master, con->msgp, con->mpnt, con->point);
}
/* update fixed direction data */
static void update_fixdir (DOM *dom, CON *con)
{
if (con->slave)
{
double n [3];
BODY_Cur_Point (con->slave, con->ssgp, con->spnt, con->point);
BODY_Ref_Point (con->master, con->msgp, con->point, con->mpnt);
BODY_Cur_Vector (con->master, con->msgp->gobj, con->mpnt, con->Z, n);
localbase (n, con->base);
}
else BODY_Cur_Point (con->master, con->msgp, con->mpnt, con->point);
}
/* update velocity direction data */
static void update_velodir (DOM *dom, CON *con)
{
VELODIR (con->Z) = TMS_Value (con->tms[0], dom->time + dom->step);
BODY_Cur_Point (con->master, con->msgp, con->mpnt, con->point);
}
/* update velocity3 direction data */
static void update_velodir3 (DOM *dom, CON *con)
{
VELODIR0 (con->Z) = TMS_Value (con->tms[0], dom->time + dom->step);
VELODIR1 (con->Z) = TMS_Value (con->tms[1], dom->time + dom->step);
VELODIR2 (con->Z) = TMS_Value (con->tms[2], dom->time + dom->step);
BODY_Cur_Point (con->master, con->msgp, con->mpnt, con->point);
}
/* update rigid link data */
static void update_riglnk (DOM *dom, CON *con)
{
double n [3],
m [3],
s [3],
len;
if (con->master && con->slave)
{
BODY_Cur_Point (con->master, con->msgp, con->mpnt, m);
BODY_Cur_Point (con->slave, con->ssgp, con->spnt, s);
}
else /* master point to a spatial point link */
{
BODY_Cur_Point (con->master, con->msgp, con->mpnt, m);
COPY (con->spnt, s);
}
COPY (m, con->point);
SUB (m, s, RIGLNK_VEC (con->Z));
SUB (m, s, n);
len = LEN (n);
con->gap = len - RIGLNK_LEN(con->Z);
len = 1.0 / len;
SCALE (n, len);
localbase (n, con->base);
}
/* update glue data */
static void update_spring (DOM *dom, CON *con)
{
double v [3],
n [3],
m [3],
s [3],
len,
inv;
BODY_Cur_Point (con->master, con->msgp, con->mpnt, m);
BODY_Cur_Point (con->slave, con->ssgp, con->spnt, s);
COPY (m, con->point);
SUB (m, s, v);
switch (con->spair[0])
{
case SPRING_FOLLOW:
{
COPY (v, n);
len = LEN (v);
if (len != 0.0)
{
inv = 1.0 / len;
SCALE (n, inv);
}
else /* avoid singularity */
{
VECTOR (n, 0, 0, 1);
}
}
break;
case SPRING_FIXED:
{
n[0] = con->Z[4];
n[1] = con->Z[5];
n[2] = con->Z[6];
len = DOT (v, n);
}
break;
case SPRING_CONV_MASTER:
{
BODY_Cur_Vector (con->master, con->msgp->gobj, con->mpnt, con->Z+4, n);
len = DOT (v, n);
}
break;
case SPRING_CONV_SLAVE:
{
BODY_Cur_Vector (con->slave, con->ssgp->gobj, con->spnt, con->Z+4, n);
len = DOT (v, n);
}
break;
}
con->Z[3] = len; /* useful for visualization */
con->gap = len - con->Z[2];
localbase (n, con->base);
}
/* tell whether the geometric objects are topologically adjacent */
static int gobj_adjacent (short paircode, void *aobj, void *bobj)
{
switch (paircode)
{
case AABB_ELEMENT_ELEMENT: return ELEMENT_Adjacent (aobj, bobj);
case AABB_CONVEX_CONVEX: return CONVEX_Adjacent (aobj, bobj);
}
return 0;
}
#if MPI
/* return an SGP index:
* semi-positive index indicates regular surface SGP */
static int SGP_index (BODY *bod, SGP *sgp)
{
long n = sgp - bod->sgp;
ASSERT_DEBUG (n >= 0 && n < bod->nsgp, "Error in SGP index");
return n;
}
/* recreate an SGP from an index returned by SGP_index */
static SGP* SGP_from_index (DOM *dom, BODY *bod, int n)
{
SGP *sgp;
ASSERT_DEBUG (n >= 0 && n < bod->nsgp, "Error in SGP index");
sgp = &bod->sgp [n];
return sgp;
}
/* constraint weight */
static int constraint_weight (CON *con)
{
int wgt0 = 1, /* default weight */
wgt1 = 0; /* weight of local dynamics row */
DOM *dom = con->master->dom;
if (con->dia)
{
OFFB *blk;
for (blk = con->dia->adjext; blk; blk = blk->n) wgt1 ++;
for (blk = con->dia->adj; blk; blk = blk->n) wgt1 ++;
}
wgt0 += (int) (dom->weight_factor * (double) wgt1);
return wgt0;
}
/* body weight */
static int body_weight (BODY *bod)
{
return bod->dofs + bod->nsgp; /* XXX: this is meant to represent the time integration and contact detection together */
}
/* domain weight */
static int domain_weight (DOM *dom)
{
int weight = 0;
BODY *bod;
CON *con;
for (bod = dom->bod; bod; bod = bod->next)
{
weight += body_weight (bod);
}
for (con = dom->con; con; con = con->next)
{
weight += constraint_weight (con);
}
return weight;
}
#if ZOLTAN
/* number of objects for balacing */
static int obj_count (DOM *dom, int *ierr)
{
*ierr = ZOLTAN_OK;
if (dom->ncon + dom->nbod == 0) return 1; /* XXX: Zoltan fails for 0 count */
else return dom->ncon + dom->nbod;
}
/* list of object identifiers for load balancing */
static void obj_list (DOM *dom, int num_gid_entries, int num_lid_entries,
ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids, int wgt_dim, float *obj_wgts, int *ierr)
{
BODY *bod;
CON *con;
int i;
for (con = dom->con, i = 0; con; con = con->next)
{
global_ids [i * num_gid_entries] = con->id;
obj_wgts [i * wgt_dim] = constraint_weight (con);
i ++;
}
for (bod = dom->bod; bod; i ++, bod = bod->next)
{
global_ids [i * num_gid_entries] = UINT_MAX - bod->id; /* XXX: vournable */
obj_wgts [i * wgt_dim] = body_weight (bod);
}
if (i == 0) /* XXX: Zoltan workaround */
{
global_ids [0] = UINT_MAX;
obj_wgts [0] = 1.0;
}
*ierr = ZOLTAN_OK;
}
/* number of spatial dimensions */
static int dimensions (DOM *dom, int *ierr)
{
*ierr = ZOLTAN_OK;
return 3;
}
/* list of object points exploited during load balancing */
static void obj_points (DOM *dom, int num_gid_entries, int num_lid_entries, int num_obj,
ZOLTAN_ID_PTR global_ids, ZOLTAN_ID_PTR local_ids, int num_dim, double *geom_vec, int *ierr)
{
unsigned int id;
double *e, *v;
BODY *bod;
CON *con;
int i;
if (num_obj == 1 && global_ids [0] == UINT_MAX) /* XXX: Zoltan workaround */
{
SET (geom_vec, 0.0);
}
else for (i = 0; i < num_obj; i ++)
{
id = global_ids [i * num_gid_entries];
v = &geom_vec [i* num_dim];
con = MAP_Find (dom->idc, (void*) (long) id, NULL);
if (con)
{
COPY (con->point, v);
}
else
{
ASSERT_DEBUG_EXT (bod = MAP_Find (dom->idb, (void*) (long) (UINT_MAX - id), NULL), "Invalid body id");
e = bod->extents;
MID (e, e+3, v);
}
}
*ierr = ZOLTAN_OK;
}
#endif
/* create dummy body if needed */
static BODY* get_body (DOM *dom, int id)
{
BODY *bod;
bod = MAP_Find (dom->allbodies, (void*) (long) id, NULL);
if (!bod) /* create dummy */
{
ERRMEM (bod = MEM_CALLOC (sizeof (BODY)));
bod->nsgp = INT_MAX;
bod->id = id;
bod->dom = dom;
MAP_Insert (&dom->mapmem, &dom->allbodies, (void*) (long) id, bod, NULL);
}
return bod;
}
/* pack body if the child of this body was not previously on given rank */
static void pack_body (BODY *bod, int rank, int *dsize, double **d, int *doubles, int *isize, int **i, int *ints)
{
if (!SET_Contains (bod->prevchildren, (void*) (long) rank, NULL))
{
pack_int (isize, i, ints, 1);
BODY_Pack (bod, dsize, d, doubles, isize, i, ints);
}
else pack_int (isize, i, ints, 0);
}
/* unpack body in place of another body or a dummy */
static BODY* unpack_body (DOM *dom, int id, int *dpos, double *d, int doubles, int *ipos, int *i, int ints)
{
BODY *bod, *q;
MAP *item;
SET *jtem;
CON *con;
int flg;
flg = unpack_int (ipos, i, ints);
item = MAP_Find_Node (dom->allbodies, (void*) (long) id, NULL);
if (flg == 0) /* previous child is expected to exist here */
{
ASSERT_DEBUG (item, "Invalid body id");
return item->data;
}
bod = BODY_Unpack (dom->solfec, dpos, d, doubles, ipos, i, ints);
bod->dom = dom;
if (item == NULL) /* if not in the map simply insert it */
{
MAP_Insert (&dom->mapmem, &dom->allbodies, (void*) (long) bod->id, bod, NULL);
}
else /* already in the map */
{
q = item->data;
if (q->nsgp == INT_MAX) /* q is a dummy */
{
for (jtem = SET_First (q->con); jtem; jtem = SET_Next (jtem)) /* for every constraint */
{
con = jtem->data;
if (con->master == q) /* if master was the dummy */
{
if (con->slave) SET_Delete (&dom->setmem, &con->slave->con, con, CONCMP); /* remove constraint from slave's set */
con->master = bod; /* remap constraint to the new body */
con->msgp = SGP_from_index (dom, bod, con->msgp - q->sgp); /* remap SGP into the new body */
if (con->slave) SET_Insert (&dom->setmem, &con->slave->con, con, CONCMP); /* insert updated constraint into slave's set */
}
else /* slave was the dummy */
{
SET_Delete (&dom->setmem, &con->master->con, con, CONCMP); /* remove from master's set */
con->slave = bod; /* remap body */
con->ssgp = SGP_from_index (dom, bod, con->ssgp - q->sgp); /* remap SGP */
SET_Insert (&dom->setmem, &con->master->con, con, CONCMP); /* insert updated constraint into master's set */
}
SET_Insert (&dom->setmem, &bod->con, con, CONCMP); /* insert updated constraint into body set */
}
SET_Free (&dom->setmem, &q->con); /* free dummies constraint set */
free (q); /* free dummy */
item->data = bod; /* map new body */
}
else /* q is a valid body (formerly a child or a parent on this processor) */
{
BODY_Destroy (bod); /* destroy unpacked body */
bod = q; /* return q */
}
}
return bod;
}
/* pack constraint migrating out during */
static void pack_constraint (CON *con, int *dsize, double **d, int *doubles, int *isize, int **i, int *ints)
{
pack_int (isize, i, ints, con->id);
/* external ranks */
pack_int (isize, i, ints, SET_Size (con->ext));
for (SET *item = SET_First (con->ext); item; item = SET_Next (item))
pack_int (isize, i, ints, (int) (long) item->data);
pack_int (isize, i, ints, con->kind);
pack_int (isize, i, ints, con->master->id);
if (con->slave) pack_int (isize, i, ints, con->slave->id);
else pack_int (isize, i, ints, 0);
pack_int (isize, i, ints, SGP_index (con->master, con->msgp));
if (con->slave) pack_int (isize, i, ints, SGP_index (con->slave, con->ssgp));
pack_doubles (dsize, d, doubles, con->mpnt, 3);
if (con->slave) pack_doubles (dsize, d, doubles, con->spnt, 3);
pack_doubles (dsize, d, doubles, con->R, 3);
pack_doubles (dsize, d, doubles, con->U, 3);
pack_doubles (dsize, d, doubles, con->point, 3);
pack_doubles (dsize, d, doubles, con->base, 9);
pack_double (dsize, d, doubles, con->gap);
switch ((int) con->kind)
{
case CONTACT:
pack_double (dsize, d, doubles, con->area);
pack_int (isize, i, ints, con->paircode);
SURFACE_MATERIAL_Pack_State (&con->mat, dsize, d, doubles, isize, i, ints);
break;
case VELODIR:
TMS_Pack (con->tms[0], dsize, d, doubles, isize, i, ints);
pack_doubles (dsize, d, doubles, con->Z, DOM_Z_SIZE);
break;
case VELODIR3:
TMS_Pack (con->tms[0], dsize, d, doubles, isize, i, ints);
TMS_Pack (con->tms[1], dsize, d, doubles, isize, i, ints);
TMS_Pack (con->tms[2], dsize, d, doubles, isize, i, ints);
pack_doubles (dsize, d, doubles, con->Z, DOM_Z_SIZE);
break;
case FIXPNT:
case FIXDIR:
case RIGLNK:
pack_doubles (dsize, d, doubles, con->Z, DOM_Z_SIZE);
break;
case SPRING:
{
int fid = lngcallback_id (NULL, con->tms[0]);
ASSERT_TEXT (fid, "failed to obtain SPRING callback function ID");
pack_int (isize, i, ints, fid);
pack_int (isize, i, ints, con->spair[0]); /* direction update kind */
pack_doubles (dsize, d, doubles, con->Z, DOM_Z_SIZE);
}
break;
}
con->state |= CON_IDLOCK; /* prevent id deletion */
DOM_Remove_Constraint (con->master->dom, con); /* remove from the domain */
}
/* unpack constraint migrated in during load balancing */
static void unpack_constraint (DOM *dom, int *dpos, double *d, int doubles, int *ipos, int *i, int ints)
{
int kind, cid, mid, sid, n, j, k;
BODY *master, *slave;
SGP *msgp, *ssgp;
SET *ext;
CON *con;
cid = unpack_int (ipos, i, ints);
/* external ranks */
j = unpack_int (ipos, i, ints);
for (ext = NULL, n = 0; n < j; n ++)
{
k = unpack_int (ipos, i, ints);
if (k == dom->rank) /* migrated in plance of a previous external constraint */
{
ASSERT_DEBUG_EXT (con = MAP_Find (dom->conext, (void*) (long) cid, NULL), "Invalid constraint id");
DOM_Remove_Constraint (dom, con); /* ranks packing and this removal are before constraint creation not to mess up bod->con set comparisons */
}
else SET_Insert (&dom->setmem, &ext, (void*) (long) k, NULL);
}
kind = unpack_int (ipos, i, ints);
mid = unpack_int (ipos, i, ints);
sid = unpack_int (ipos, i, ints);
master = get_body (dom, mid);
if (sid) slave = get_body (dom, sid); else slave = NULL;
n = unpack_int (ipos, i, ints);
msgp = SGP_from_index (dom, master, n);
if (slave) n = unpack_int (ipos, i, ints), ssgp = SGP_from_index (dom, slave, n); else ssgp = NULL;
dom->noid = cid; /* disable constraint ids generation and use 'noid' instead */
con = insert (dom, master, slave, msgp, ssgp, kind);
dom->noid = 0; /* enable constraint ids generation */
con->ext = ext; /* external ranks */
unpack_doubles (dpos, d, doubles, con->mpnt, 3);
if (slave) unpack_doubles (dpos, d, doubles, con->spnt, 3);
unpack_doubles (dpos, d, doubles, con->R, 3);
unpack_doubles (dpos, d, doubles, con->U, 3);
unpack_doubles (dpos, d, doubles, con->point, 3);
unpack_doubles (dpos, d, doubles, con->base, 9);
con->gap = unpack_double (dpos, d, doubles);
switch (kind)
{