forked from SCOREC/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphConstraint.cc
609 lines (575 loc) · 17 KB
/
phConstraint.cc
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
#include "phBC.h"
#include <apfGeometry.h>
#include <cstdlib>
#include <pcu_util.h>
#include <lionPrint.h>
#include <iostream>
#include <sstream>
namespace ph {
struct DebugConstraint
{
int modelDim;
int modelTag;
};
}
std::ostream& operator<<(std::ostream& s, ph::DebugConstraint const& dbg)
{
s << "\nat model entity dim " << dbg.modelDim
<< " tag " << dbg.modelTag << '\n';
return s;
}
namespace ph {
struct Constraint
{
Constraint(int n)
{
degreesOfFreedom = n;
}
virtual ~Constraint() {}
int degreesOfFreedom;
virtual void write(int* iBC, double* BC) = 0;
};
static int maxComponent(apf::Vector3 const& v)
{
double max = 0;
int max_i = 0;
for (int i = 0; i < 3; ++i) {
double mag = fabs(v[i]);
if (mag > max) {
max = mag;
max_i = i;
}
}
return max_i;
}
struct PlaneConstraint : public Constraint
{
PlaneConstraint(apf::Plane const& a):
Constraint(2),
plane(a)
{
}
apf::Plane plane;
bool operator==(PlaneConstraint const& other) const
{
return apf::areClose(plane, other.plane, 0.0);
}
virtual void write(int* iBC, double* BC)
{
int bit = maxComponent(plane.normal) + 3;
*iBC |= (1<<bit);
plane.normal.toArray(BC + 3);
BC[6] = plane.radius;
}
};
struct PlaneConstraintElas : public Constraint
{
PlaneConstraintElas(apf::Plane const& a):
Constraint(2),
plane(a)
{
}
apf::Plane plane;
bool operator==(PlaneConstraintElas const& other) const
{
return apf::areClose(plane, other.plane, 0.0);
}
virtual void write(int* iBC, double* BC)
{
int bit = maxComponent(plane.normal) + 14;
*iBC |= (1<<bit);
plane.normal.toArray(BC + 16);
BC[19] = plane.radius;
}
};
static Constraint* makePlaneConstraint(double* values)
{
apf::Vector3 normal;
normal.fromArray(values + 1);
return new PlaneConstraint(
apf::Plane(normal, values[0]));
}
static Constraint* makePlaneConstraintElas(double* values)
{
apf::Vector3 normal;
normal.fromArray(values + 1);
return new PlaneConstraintElas(
apf::Plane(normal, values[0]));
}
/* due to the way constraints are written to file,
the planes forming this constraint are kept around */
struct LineConstraint : public Constraint
{
PlaneConstraint* pcs[2];
LineConstraint():
Constraint(1)
{
pcs[0] = pcs[1] = 0;
}
~LineConstraint()
{
delete pcs[0];
delete pcs[1];
}
virtual void write(int* iBC, double* BC)
{
/* okay, we have to set two bits.
basically, we have to line them up as best
we can with the two normals.
there isn't really a good answer to this. */
/* try max components */
int comp0 = maxComponent(pcs[0]->plane.normal);
int comp1 = maxComponent(pcs[1]->plane.normal);
if (comp0 == comp1) {
/* try to resolve a conflict in max components */
apf::Vector3 a = pcs[0]->plane.normal;
apf::Vector3 b = pcs[0]->plane.normal;
if (b[comp0] > a[comp0])
std::swap(a,b);
b[comp0] = 0;
int comp1 = maxComponent(b);
PCU_ALWAYS_ASSERT(comp0 != comp1);
}
int bit0 = comp0 + 3;
int bit1 = comp1 + 3;
*iBC |= (1<<bit0);
*iBC |= (1<<bit1);
pcs[0]->plane.normal.toArray(BC + 3);
BC[6] = pcs[0]->plane.radius;
pcs[1]->plane.normal.toArray(BC + 7);
BC[10] = pcs[1]->plane.radius;
}
};
struct LineConstraintElas : public Constraint
{
PlaneConstraintElas* pcs[2];
LineConstraintElas():
Constraint(1)
{
pcs[0] = pcs[1] = 0;
}
~LineConstraintElas()
{
delete pcs[0];
delete pcs[1];
}
virtual void write(int* iBC, double* BC)
{
/* okay, we have to set two bits.
basically, we have to line them up as best
we can with the two normals.
there isn't really a good answer to this. */
/* try max components */
int comp0 = maxComponent(pcs[0]->plane.normal);
int comp1 = maxComponent(pcs[1]->plane.normal);
if (comp0 == comp1) {
/* try to resolve a conflict in max components */
apf::Vector3 a = pcs[0]->plane.normal;
apf::Vector3 b = pcs[0]->plane.normal;
if (b[comp0] > a[comp0])
std::swap(a,b);
b[comp0] = 0;
int comp1 = maxComponent(b);
PCU_ALWAYS_ASSERT(comp0 != comp1);
}
int bit0 = comp0 + 14;
int bit1 = comp1 + 14;
*iBC |= (1<<bit0);
*iBC |= (1<<bit1);
pcs[0]->plane.normal.toArray(BC + 16);
BC[19] = pcs[0]->plane.radius;
pcs[1]->plane.normal.toArray(BC + 20);
BC[23] = pcs[1]->plane.radius;
}
};
struct PointConstraint : public Constraint
{
PointConstraint():Constraint(0) {}
apf::Vector3 point;
apf::Vector3 originalDirection_;
virtual void write(int* iBC, double* BC)
{
for (int i = 0; i < 3; ++i) {
int bit = i + 3;
*iBC |= (1<<bit);
}
double magnitude = point.getLength();
BC[6] = magnitude;
if (magnitude) {
apf::Vector3 direction = point.normalize();
direction.toArray(BC + 3);
} else {
originalDirection_.toArray(BC + 3);
}
}
};
struct PointConstraintElas : public Constraint
{
PointConstraintElas():Constraint(0) {}
apf::Vector3 point;
apf::Vector3 originalDirection_;
virtual void write(int* iBC, double* BC)
{
for (int i = 0; i < 3; ++i) {
int bit = i + 14;
*iBC |= (1<<bit);
}
double magnitude = point.getLength();
BC[19] = magnitude;
if (magnitude) {
apf::Vector3 direction = point.normalize();
direction.toArray(BC + 16);
} else {
originalDirection_.toArray(BC + 16);
}
}
};
struct InterfaceConstraint : public Constraint
{
};
static Constraint* makePointConstraint(double* values)
{
PointConstraint* c = new PointConstraint();
c->originalDirection_.fromArray(values + 1);
c->point = c->originalDirection_ * values[0];
/* just-in-case normalization */
c->originalDirection_ = c->originalDirection_.normalize();
return c;
}
static Constraint* makePointConstraintElas(double* values)
{
PointConstraintElas* c = new PointConstraintElas();
c->originalDirection_.fromArray(values + 1);
c->point = c->originalDirection_ * values[0];
/* just-in-case normalization */
c->originalDirection_ = c->originalDirection_.normalize();
return c;
}
static Constraint* takeFirst(Constraint* a, Constraint* b)
{
delete b;
return a;
}
/* two point constraints */
static Constraint* combinePoints(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
PointConstraint* pa = static_cast<PointConstraint*>(a);
PointConstraint* pb = static_cast<PointConstraint*>(b);
/* same points, arbitrary victory */
if (apf::areClose(pa->point, pb->point, 0.0))
return takeFirst(a, b);
double ma = pa->point.getLength();
double mb = pb->point.getLength();
/* any zero magnitude wins (no-slip wins over weaker constraints) */
if (ma == 0)
return takeFirst(a, b);
if (mb == 0)
return takeFirst(b, a);
/* multiple non-zero point constraints ? we got a problem. */
std::stringstream ss;
ss << "ph error: point overconstraint (velocity): ";
ss << pa->point << " and " << pb->point << dbg;
std::string s = ss.str();
lion_eprint(1,"%s",s.c_str());
abort();
return 0;
}
static Constraint* combinePointsElas(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
PointConstraintElas* pa = static_cast<PointConstraintElas*>(a);
PointConstraintElas* pb = static_cast<PointConstraintElas*>(b);
double ma = pa->point.getLength();
double mb = pb->point.getLength();
/* same points, arbitrary victory */
if (apf::areClose(pa->point, pb->point, ma*1e-12)) // allow machine epsilon
return takeFirst(a, b);
/* any zero magnitude wins (no-slip wins over weaker constraints) */
if (ma == 0)
return takeFirst(a, b);
if (mb == 0)
return takeFirst(b, a);
/* multiple non-zero point constraints ? we got a problem. */
std::stringstream ss;
ss << "ph error: point overconstraint (mesh-elas): ";
ss << pa->point << " and " << pb->point << dbg;
std::string s = ss.str();
lion_eprint(1,"%s",s.c_str());
abort();
return 0;
}
static Constraint* combinePlanes(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
PlaneConstraint* pa = static_cast<PlaneConstraint*>(a);
PlaneConstraint* pb = static_cast<PlaneConstraint*>(b);
if (apf::areClose(pa->plane, pb->plane, 0.0))
/* same plane, arbitrary winner */
return takeFirst(a, b);
/* the planes are different, so make sure they're not parallel */
if (apf::areParallel(pa->plane, pb->plane, 0.0)) {
std::stringstream ss;
ss << "ph error: different parallel planes (velocity)" << dbg;
std::string s = ss.str();
lion_eprint(1,"%s",s.c_str());
abort();
}
/* different intersecting planes, combine into a line constraint */
LineConstraint* c = new LineConstraint();
c->pcs[0] = pa;
c->pcs[1] = pb;
return c;
}
static Constraint* combinePlanesElas(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
PlaneConstraintElas* pa = static_cast<PlaneConstraintElas*>(a);
PlaneConstraintElas* pb = static_cast<PlaneConstraintElas*>(b);
if (apf::areClose(pa->plane, pb->plane, 0.0))
/* same plane, arbitrary winner */
return takeFirst(a, b);
/* the planes are different, so make sure they're not parallel */
if (apf::areParallel(pa->plane, pb->plane, 0.0)) {
std::stringstream ss;
ss << "ph error: different parallel planes (mesh-elas)" << dbg;
std::string s = ss.str();
lion_eprint(1,"%s",s.c_str());
abort();
}
/* different intersecting planes, combine into a line constraint */
LineConstraintElas* c = new LineConstraintElas();
c->pcs[0] = pa;
c->pcs[1] = pb;
return c;
}
static Constraint* combineLinePlane(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
LineConstraint* pa = static_cast<LineConstraint*>(a);
PlaneConstraint* pb = static_cast<PlaneConstraint*>(b);
/* first off, if the plane is the same as one of
the two that formed the line, we can leave early. */
for (int i = 0; i < 2; ++i)
if (apf::areClose(pa->pcs[i]->plane, pb->plane, 0.0))
return takeFirst(a, b); /* just keep the line */
/* okay, we really have 3 distinct planes. lets combine them.
first, we actually compute the line that has been waiting
to be evaluated until now */
apf::Line line = apf::intersect(pa->pcs[0]->plane, pa->pcs[1]->plane);
/* the line may be on the third plane */
if (apf::areClose(line, pb->plane, 0.0))
return takeFirst(a, b); /* keep the line */
/* it may never intersect */
if (apf::areParallel(line, pb->plane, 0.0)) {
std::stringstream ss;
ss << "line doesn't intersect plane (velocity)" << dbg;
std::string s = ss.str();
lion_eprint(1,"%s",s.c_str());
abort();
}
/* okay, there is a legit intersection point. find it. */
apf::Vector3 point = apf::intersect(line, pb->plane);
PointConstraint* result = new PointConstraint();
result->point = point;
result->originalDirection_ = apf::Vector3(1,0,0);
delete pa;
delete pb;
return result;
}
static Constraint* combineLinePlaneElas(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
LineConstraintElas* pa = static_cast<LineConstraintElas*>(a);
PlaneConstraintElas* pb = static_cast<PlaneConstraintElas*>(b);
/* first off, if the plane is the same as one of
the two that formed the line, we can leave early. */
for (int i = 0; i < 2; ++i)
if (apf::areClose(pa->pcs[i]->plane, pb->plane, 0.0))
return takeFirst(a, b); /* just keep the line */
/* okay, we really have 3 distinct planes. lets combine them.
first, we actually compute the line that has been waiting
to be evaluated until now */
apf::Line line = apf::intersect(pa->pcs[0]->plane, pa->pcs[1]->plane);
/* the line may be on the third plane */
if (apf::areClose(line, pb->plane, 0.0))
return takeFirst(a, b); /* keep the line */
/* it may never intersect */
if (apf::areParallel(line, pb->plane, 0.0)) {
std::stringstream ss;
ss << "line doesn't intersect plane (mesh-elas)" << dbg;
std::string s = ss.str();
lion_eprint(1,"%s",s.c_str());
abort();
}
/* okay, there is a legit intersection point. find it. */
apf::Vector3 point = apf::intersect(line, pb->plane);
PointConstraintElas* result = new PointConstraintElas();
result->point = point;
result->originalDirection_ = apf::Vector3(1,0,0);
delete pa;
delete pb;
return result;
}
/* combine linear constraints, we assume that
a is the accumulation and b is a new constraint.
also accumulation should proceed from most
to least strict constraint.
b is expected to be either a point or a plane
constraint */
static Constraint* combine(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
if (!a)
return b;
PCU_ALWAYS_ASSERT(a->degreesOfFreedom <= b->degreesOfFreedom);
if (a->degreesOfFreedom == 0) {
/* Rule #1: a point constraint beats anything else */
if (b->degreesOfFreedom != 0)
return takeFirst(a, b);
/* otherwise, compare the two points */
if (b->degreesOfFreedom == 0)
return combinePoints(a, b, dbg);
}
if (a->degreesOfFreedom == 1 &&
b->degreesOfFreedom == 2)
return combineLinePlane(a, b, dbg);
if (a->degreesOfFreedom == 2 &&
b->degreesOfFreedom == 2)
return combinePlanes(a, b, dbg);
abort();
return 0;
}
static Constraint* combineElas(Constraint* a, Constraint* b,
DebugConstraint const& dbg)
{
if (!a)
return b;
PCU_ALWAYS_ASSERT(a->degreesOfFreedom <= b->degreesOfFreedom);
if (a->degreesOfFreedom == 0) {
/* Rule #1: a point constraint beats anything else */
if (b->degreesOfFreedom != 0)
return takeFirst(a, b);
/* otherwise, compare the two points */
if (b->degreesOfFreedom == 0)
return combinePointsElas(a, b, dbg);
}
if (a->degreesOfFreedom == 1 &&
b->degreesOfFreedom == 2)
return combineLinePlaneElas(a, b, dbg);
if (a->degreesOfFreedom == 2 &&
b->degreesOfFreedom == 2)
return combinePlanesElas(a, b, dbg);
abort();
return 0;
}
typedef Constraint* (*Make)(double* values);
/* similar flow to getFirstApplied in phBC.cc, but we
have to account for all first-reachable BCs, not just
the first one we see. */
Constraint* combineAll(gmi_model* gm, FieldBCs& bcs, Make make,
gmi_ent* ge, apf::Vector3 const& x, Constraint* a)
{
double* v = getBCValue(gm, bcs, ge, x);
if (v) {
DebugConstraint dbg;
dbg.modelTag = gmi_tag(gm, ge);
dbg.modelDim = gmi_dim(gm, ge);
Constraint* b = make(v);
return combine(a, b, dbg);
}
gmi_set* up = gmi_adjacent(gm, ge, gmi_dim(gm, ge) + 1);
for (int i = 0; i < up->n; ++i)
a = combineAll(gm, bcs, make, up->e[i], x, a);
gmi_free_set(up);
return a;
}
Constraint* combineAllElas(gmi_model* gm, FieldBCs& bcs, Make make,
gmi_ent* ge, apf::Vector3 const& x, Constraint* a)
{
double* v = getBCValue(gm, bcs, ge, x);
if (v) {
DebugConstraint dbg;
dbg.modelTag = gmi_tag(gm, ge);
dbg.modelDim = gmi_dim(gm, ge);
Constraint* b = make(v);
return combineElas(a, b, dbg);
}
gmi_set* up = gmi_adjacent(gm, ge, gmi_dim(gm, ge) + 1);
for (int i = 0; i < up->n; ++i)
a = combineAllElas(gm, bcs, make, up->e[i], x, a);
gmi_free_set(up);
return a;
}
Constraint* combineInterface
(
gmi_model* gm, FieldBCs& bcs, Make make,
gmi_ent* ge, apf::Vector3 const& x, Constraint* a
)
{
double* v = getBCValue(gm, bcs, ge, x);
if (v) {
/* The interface attribute only takes an integer value now
and it is not even used. So, in order to reuse combineElas,
fake value (u) is used. It is equivalent to mag=0, direction=(1,0,0)
*/
double u[4] = {0,1,0,0};
DebugConstraint dbg;
dbg.modelTag = gmi_tag(gm, ge);
dbg.modelDim = gmi_dim(gm, ge);
Constraint* b = make(u);
return combineElas(a, b, dbg);
}
gmi_set* up = gmi_adjacent(gm, ge, gmi_dim(gm, ge) + 1);
for (int i = 0; i < up->n; ++i)
a = combineInterface(gm, bcs, make, up->e[i], x, a);
gmi_free_set(up);
return a;
}
bool applyVelocityConstaints(gmi_model* gm, BCs& bcs, gmi_ent* e,
apf::Vector3 const& x, double* BC, int* iBC)
{
Constraint* c = 0;
std::string name = "comp3";
if (haveBC(bcs, name)) {
FieldBCs& fbcs = bcs.fields[name];
c = combineAll(gm, fbcs, makePointConstraint, e, x, c);
}
name = "comp1";
if (haveBC(bcs, name)) {
FieldBCs& fbcs = bcs.fields[name];
c = combineAll(gm, fbcs, makePlaneConstraint, e, x, c);
}
if (!c)
return false;
c->write(iBC, BC);
delete c;
return true;
}
bool applyElasticConstaints(gmi_model* gm, BCs& bcs, gmi_ent* e,
apf::Vector3 const& x, double* BC, int* iBC)
{
Constraint* c = 0;
std::string name;
name = "DG interface";
if (haveBC(bcs, name)) {
FieldBCs& fbcs = bcs.fields[name];
c = combineInterface(gm, fbcs, makePointConstraintElas, e, x, c);
}
name = "comp3_elas";
if (haveBC(bcs, name)) {
FieldBCs& fbcs = bcs.fields[name];
c = combineAllElas(gm, fbcs, makePointConstraintElas, e, x, c);
}
name = "comp1_elas";
if (haveBC(bcs, name)) {
FieldBCs& fbcs = bcs.fields[name];
c = combineAllElas(gm, fbcs, makePlaneConstraintElas, e, x, c);
}
if (!c)
return false;
c->write(iBC, BC);
delete c;
return true;
}
}