-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathASMbase.C
More file actions
1869 lines (1546 loc) · 48.5 KB
/
Copy pathASMbase.C
File metadata and controls
1869 lines (1546 loc) · 48.5 KB
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
// $Id$
//==============================================================================
//!
//! \file ASMbase.C
//!
//! \date Sep 20 2009
//!
//! \author Knut Morten Okstad / SINTEF
//!
//! \brief Base class for spline-based finite element (FE) assembly drivers.
//!
//==============================================================================
#include "ASMbase.h"
#include "ASMenums.h"
#include "ASM2D.h"
#include "ASM3D.h"
#include "IFEM.h"
#include "MPC.h"
#include "Tensor.h"
#include "Vec3.h"
#include "Vec3Oper.h"
#include "Function.h"
#include "Utilities.h"
#include <algorithm>
#include <functional>
#include <iomanip>
bool ASMbase::fixHomogeneousDirichlet = true;
int ASMbase::dbgElm = 0;
//! This quantitiy is used to scale the characteristic element sizes which
//! are used by residual error estimates, etc., such that they always are in
//! the range [0,1.0]. The applications have to set an appropriate value,
//! when needed.
double ASMbase::modelSize = 1.0;
int ASMbase::gEl = 0;
int ASMbase::gNod = 0;
IntMap ASMbase::xNode;
IntVec ASMbase::Empty;
ASM::CachePolicy ASM::cachePolicy = ASM::PRE_CACHE;
/*!
\brief Convenience function writing error message for non-implemented methods.
*/
static bool Aerror (const char* name)
{
std::cerr <<" *** ASMbase::"<< name
<<": Must be implemented in sub-class."<< std::endl;
return false;
}
ASMbase::ASMbase (unsigned char n_p, unsigned char n_s, unsigned char n_f)
: MLGE(myMLGE), MLGN(myMLGN), MNPC(myMNPC), shareFE(0), myActiveEls(nullptr)
{
nf = n_f;
nsd = n_s > 3 ? 3 : n_s;
ndim = n_p > nsd ? nsd : n_p;
nLag = 0;
nGauss = 0;
nel = nnod = 0;
idx = 0;
firstEl = firstIp = 0;
myElActive = nullptr;
}
ASMbase::ASMbase (const ASMbase& patch, unsigned char n_f)
: MLGE(patch.MLGE), MLGN(patch.MLGN), MNPC(patch.MNPC), shareFE('F'),
firstBp(patch.firstBp), myLMTypes(patch.myLMTypes), myLMs(patch.myLMs),
myActiveEls(nullptr), myRmaster(patch.myRmaster)
{
nf = n_f > 0 ? n_f : patch.nf;
nsd = patch.nsd;
ndim = patch.ndim;
nLag = patch.nLag;
nGauss = patch.nGauss;
nel = patch.nel;
nnod = patch.nnod;
idx = patch.idx;
firstEl = patch.firstEl;
firstIp = patch.firstIp;
// Note: Properties are _not_ copied
myElActive = nullptr; // Element activation function is not copied
}
ASMbase::ASMbase (const ASMbase& patch)
: MLGE(myMLGE), MLGN(myMLGN), MNPC(myMNPC), shareFE('S'),
BCode(patch.BCode), firstBp(patch.firstBp), myActiveEls(nullptr)
{
nf = patch.nf;
nsd = patch.nsd;
ndim = patch.ndim;
nGauss = patch.nGauss;
nel = patch.nel;
nnod = patch.nnod;
idx = patch.idx;
firstEl = patch.firstEl;
firstIp = patch.firstIp;
// Only copy the regular part of the FE data, leave out any extraordinaries
if (patch.MLGE.size() > nel)
myMLGE.insert(myMLGE.begin(),patch.MLGE.begin(),patch.MLGE.begin()+nel);
else
myMLGE = patch.MLGE;
if (patch.MLGN.size() > nnod)
myMLGN.insert(myMLGN.begin(),patch.MLGN.begin(),patch.MLGN.begin()+nnod);
else
myMLGN = patch.MLGN;
if (patch.MNPC.size() > nel)
myMNPC.insert(myMNPC.begin(),patch.MNPC.begin(),patch.MNPC.begin()+nel);
else
myMNPC = patch.MNPC;
// Can not copy pointers as it might cause problems on destruction
if (!patch.dCode.empty() || !patch.mpcs.empty())
std::cerr <<" ** ASMbase copy constructor: The copied patch has"
<<" multi-point constraints, these are not copied.\n";
myElActive = nullptr; // Element activation function is not copied
nLag = 0; // Lagrange multipliers are not copied
}
ASMbase::~ASMbase ()
{
for (MPC* mpc : mpcs)
delete mpc;
delete myElActive;
}
ASMbase* ASMbase::cloneUnShared () const
{
const ASM2D* patch2 = dynamic_cast<const ASM2D*>(this);
if (patch2) return patch2->clone();
const ASM3D* patch3 = dynamic_cast<const ASM3D*>(this);
if (patch3) return patch3->clone();
return nullptr;
}
void ASMbase::clear (bool retainGeometry)
{
if (retainGeometry)
{
// Clear all FE structures, including the elements
myMLGE.clear();
myMNPC.clear();
if (shareFE == 'F')
{
const_cast<IntVec&>(MLGE).clear();
const_cast<IntMat&>(MNPC).clear();
}
}
else // Don't erase the elements, but set them to have zero nodes
for (IntVec& mnpc : myMNPC)
mnpc.clear();
// Erase the nodes, boundary conditions and multi-point constraints
for (MPC* mpc : mpcs)
delete mpc;
myLMs.clear();
myLMTypes.clear();
myRmaster.clear();
myMLGN.clear();
if (shareFE == 'F')
const_cast<IntVec&>(MLGN).clear();
BCode.clear();
dCode.clear();
mpcs.clear();
myActiveEls = nullptr;
}
bool ASMbase::addXElms (short int, short int, size_t, IntVec&)
{
return Aerror("addXElms(short int,short int,size_t,IntVec&)");
}
bool ASMbase::addLagrangeMultipliers (size_t iel, const IntVec& mGLag,
unsigned char nnLag)
{
if (iel > MNPC.size())
{
std::cerr <<" *** ASMbase::addLagrangeMultipliers: Element index "<< iel
<<" is out of range [1,"<< MNPC.size() <<"]."<< std::endl;
return false;
}
else if (shareFE == 'F')
return false;
if (nLag == 0 || iel == 0)
nLag = nnLag;
else if (nnLag != nLag)
return false;
for (int iLag : mGLag)
{
size_t node = 1 + utl::findIndex(MLGN,iLag);
if (node == 0)
{
// Add a new Lagrange multiplier node
myMLGN.push_back(iLag);
node = myMLGN.size();
}
// Update the nodal (1-based) indices of the Lagrange multipliers
if (myLMs.empty() || node >= *myLMs.begin())
myLMs.insert(node);
else
{
std::cerr <<" *** ASMbase::addLagrangeMultipliers: Node "<< node
<<" is out of range ["<< *myLMs.begin() <<","<< *myLMs.rbegin()
<<"]."<< std::endl;
return false;
}
size_t idxLag = node - *myLMs.begin();
if (myLMTypes.size() < idxLag+1)
myLMTypes.resize(idxLag+1,0);
myLMTypes[idxLag] = iel == 0 ? 'G' : 'L';
// Extend the element connectivity table
if (iel > 0)
myMNPC[iel-1].push_back(node-1);
else for (IntVec& mnpc : myMNPC)
mnpc.push_back(node-1);
}
return true;
}
bool ASMbase::addGlobalLagrangeMultipliers (const IntVec& mGLag,
unsigned char nnLag)
{
return this->addLagrangeMultipliers(0,mGLag,nnLag);
}
void ASMbase::resetNumbering (int n)
{
gEl = 0;
gNod = n;
xNode.clear();
}
size_t ASMbase::getNodeIndex (int globalNum, bool) const
{
return 1 + utl::findIndex(MLGN,globalNum);
}
int ASMbase::getNodeID (size_t inod, bool) const
{
return inod < 1 || inod > MLGN.size() ? 0 : MLGN[inod-1];
}
char ASMbase::getLMType (size_t inod) const
{
std::set<size_t>::const_iterator firstLM = myLMs.begin();
return this->isLMn(inod) ? myLMTypes[inod-(*firstLM)] : 0;
}
size_t ASMbase::getElmIndex (int globalNum) const
{
return 1 + utl::findIndex(MLGE,globalNum);
}
int ASMbase::getElmID (size_t iel) const
{
return iel < 1 || iel > MLGE.size() ? 0 : abs(MLGE[iel-1]);
}
const IntVec& ASMbase::getElementNodes (int iel) const
{
if (iel > 0 && iel <= static_cast<int>(MNPC.size()))
return MNPC[iel-1];
static IntVec empty;
return empty;
}
unsigned char ASMbase::getNodalDOFs (size_t inod) const
{
if (this->isLMn(inod))
return nLag;
else if (this->isRMn(inod))
return nsd < 3 ? 3 : 6; // Including rotational DOFs
else
return nf;
}
char ASMbase::getNodeType (size_t inod) const
{
return this->isLMn(inod) ? this->getLMType(inod) : (inod > nnod ? 'X' : 'D');
}
size_t ASMbase::getNoNodes (int basis) const
{
if (basis > 0)
return nnod;
else if (basis < 0 && !myLMs.empty())
return *myLMs.begin() - 1;
else
return MLGN.size();
}
size_t ASMbase::getNoElms (bool includeZeroVolElms, bool includeXElms) const
{
if (includeZeroVolElms)
return includeXElms ? MLGE.size() : nel;
size_t numels = 0;
for (int iel : MLGE)
if (iel > 0 || (includeXElms && iel < 0))
numels++;
return numels;
}
int ASMbase::getMaxElmNo () const
{
return MLGE.empty() ? 0 : *std::max_element(MLGE.begin(),MLGE.end());
}
void ASMbase::getNoIntPoints (size_t& nPt, size_t& nIPt)
{
size_t nGp = 1;
if (nGauss > 0 && nGauss <= 10)
for (unsigned char d = 0; d < ndim; d++)
nGp *= nGauss;
else
{
// Use polynomial order to define number of quadrature points
int ng[3] = { 0, 0, 0 };
this->getOrder(ng[0],ng[1],ng[2]);
for (unsigned char d = 0; d < ndim && d < 3; d++)
if (nGauss > -ng[d])
nGp *= ng[d] + nGauss%10;
else
nGp = 0;
}
firstIp = nPt;
nPt += nel*nGp; // Note: Includes also the 0-span elements
// Count additional interface quadrature points
size_t nInterface = MLGE.size() - nel;
if (nInterface > 0 && nInterface != nel && nGauss > 0 && nGauss <= 10)
nIPt += nInterface*nGp/nGauss;
}
void ASMbase::getNoBouPoints (size_t& nPt, char ldim, char lindx)
{
if (ldim+1 == ndim)
lindx %= 10; // Mask off Neumann order flag
size_t nGp = 1;
if (nGauss > 0 && nGauss <= 10)
for (char d = 0; d < ldim; d++)
nGp *= nGauss;
else
{
// Use polynomial order to define number of quadrature points
int ng[3] = { 0, 0, 0 };
this->getOrder(ng[0],ng[1],ng[2]);
ng[(lindx-1)/2] = 1;
for (unsigned char d = 0; d < ndim; d++)
if (nGauss > -ng[d])
nGp *= ng[d];
else if (d != (lindx-1)/2)
nGp = 0;
}
firstBp[lindx] = nPt;
nPt += this->getNoBoundaryElms(lindx,ldim)*nGp; // Includes 0-span elements
}
void ASMbase::printNodes (std::ostream& os) const
{
Matrix X;
this->getNodalCoordinates(X);
os <<"\n\nNodal coordinates for Patch "<< idx+1;
for (size_t inod = 1; inod <= X.cols(); inod++)
{
os <<'\n'<< std::setw(4) << inod
<< ' '<< std::setw(4) << MLGN[inod-1] <<':';
for (size_t i = 1; i <= X.rows(); i++)
os <<' '<< X(i,inod);
}
os << std::endl;
}
void ASMbase::printElements (std::ostream& os) const
{
if (MNPC.empty()) return;
os <<"\n\nElement connectivities for Patch "<< idx+1;
for (size_t iel = 0; iel < MLGE.size(); iel++)
{
os <<'\n'<< std::setw(4) << iel+1
<< ' '<< std::setw(4) << MLGE[iel] <<':';
if (iel < MNPC.size())
for (int node : MNPC[iel]) os <<" "<< node;
}
for (size_t ielx = MLGE.size(); ielx < MNPC.size(); ielx++)
{
os <<'\n'<< std::setw(4) << ielx+1 <<" ----:";
for (int node : MNPC[ielx]) os <<" "<< node;
}
os << std::endl;
}
/*!
\brief A helper class used by ASMbase::isFixed().
\details The class is just an unary function that checks whether a DOF object
matches the fixed status of a given BC object.
*/
class fixed
{
int myNode; //!< The internal node number to compare with
int myDofs; //!< The local DOFs to compare with
public:
//! \brief Constructor initializing the node and local DOF index.
fixed(int node, int dof) : myNode(node), myDofs(dof) {}
//! \brief Returns \e true if the DOF has the same fixed status as \a bc.
bool operator()(const ASMbase::BC& bc)
{
if (bc.node == myNode)
for (int dof = myDofs; dof > 0; dof /= 10)
switch (dof%10)
{
case 1: return bc.CX == 0;
case 2: return bc.CY == 0;
case 3: return bc.CZ == 0;
case 4: return bc.RX == 0;
case 5: return bc.RY == 0;
case 6: return bc.RZ == 0;
}
return false;
}
};
bool ASMbase::isFixed (int node, int dof, bool all) const
{
BCVec::const_iterator bit = BCode.begin();
if (dof < 10 || !all)
bit = std::find_if(BCode.begin(),BCode.end(),fixed(node,dof));
else for (int d = dof; d > 0 && bit != BCode.end(); d /= 10)
if (d <= nf)
bit = std::find_if(BCode.begin(),BCode.end(),fixed(node,d%10));
return bit != BCode.end();
}
bool ASMbase::addMPC (MPC*& mpc, int code, bool verbose, bool overrideD)
{
if (!mpc) return true;
int sdof = mpc->getSlave().dof;
BCVec::iterator bit = std::find_if(BCode.begin(),BCode.end(),
fixed(mpc->getSlave().node,sdof));
if (bit != BCode.end())
{
if (!overrideD)
{
// Silently ignore MPCs on DOFs that already are marked as FIXED
delete mpc;
mpc = nullptr;
return true;
}
// Override the homogeneous Dirichlet condition for this DOF
if (bit->free(sdof) == 6)
BCode.erase(bit); // All DOFs in this node are free (or prescribed)
}
std::pair<MPCIter,bool> mit = mpcs.insert(mpc);
if (mit.second)
{
#if SP_DEBUG > 1
if (verbose) std::cout <<"Added constraint: "<< *mpc;
#endif
if (code > 0) dCode[mpc] = code;
return true;
}
else if (overrideD && (*mit.first)->getNoMaster() == 0)
{
// Override the existing Dirichlet condition for this DOF
#ifdef SP_DEBUG
if (verbose)
std::cout <<"Replacing constraint "<< **mit.first <<" by "<< *mpc;
#endif
dCode.erase(*mit.first);
mpcs.erase(mit.first);
delete *mit.first;
mpcs.insert(mpc);
if (code > 0) dCode[mpc] = code;
return true;
}
#ifdef SP_DEBUG
if (verbose) std::cout <<"Ignored constraint (duplicated slave): "<< *mpc;
#endif
delete mpc;
mpc = *mit.first; // This DOF is already a slave in another MPC-equation
return false;
}
bool ASMbase::add2PC (int slave, int dir, int master, int code)
{
if (dir < 1 || dir > nf) return true;
if (slave == master) return true;
MPC* cons = new MPC(slave,dir);
bool stat = this->addMPC(cons,code);
if (!cons) return stat;
cons->addMaster(master,dir);
#if SP_DEBUG > 1
std::cout <<"Added constraint: "<< *cons;
#endif
return stat;
}
bool ASMbase::add3PC (int slave, int dir, int master1, int master2, int code)
{
if (master1 == master2)
return this->add2PC(slave,dir,master1,code);
if (dir < 1 || dir > nf) return true;
MPC* cons = new MPC(slave,dir);
bool stat = this->addMPC(cons,code);
if (!cons) return stat;
if (master1 != slave) cons->addMaster(master1,dir);
if (master2 != slave) cons->addMaster(master2,dir);
#if SP_DEBUG > 1
std::cout <<"Added constraint: "<< *cons;
#endif
return stat;
}
void ASMbase::addLocal2GlobalCpl (int iSlave, int master, const Tensor& Tlg)
{
// Establish constraint equations relating the global DOFs
// of the slave node to the local DOFs of the master node.
// We here assume there are (at least) nsd unknowns per node,
// and that only the first nsd DOFs are subjected to transformation.
int fixDirs = 0;
for (unsigned char d = 1; d <= nf; d++)
if (MPC* cons = new MPC(MLGN[iSlave],d); this->addMPC(cons) && cons)
{
if (d > nsd)
{
if (!this->isFixed(master,d))
cons->addMaster(master,d);
}
else for (unsigned char c = 1; c <= nsd; c++)
{
if (!this->isFixed(master,c))
cons->addMaster(master,c,Tlg(d,c));
}
if (cons->getNoMaster() == 0)
{
// All master DOFs are fixed.
// Then the MPC-equation is not needed, fix the slave DOF instead.
mpcs.erase(cons);
delete cons;
fixDirs = d + 10*fixDirs;
}
#if SP_DEBUG > 1
else
std::cout <<"Added constraint: "<< *cons;
#endif
}
if (fixDirs > 0)
this->fix(iSlave+1,fixDirs);
}
bool ASMbase::createRgdMasterNode (int& gMaster, const Vec3& Xpt)
{
bool newNode = gMaster == 0;
if (newNode)
gMaster = ++gNod;
else if (std::find(MLGN.begin()+nnod,MLGN.end(),gMaster) != MLGN.end())
return newNode; // This node has already been created
#if SP_DEBUG > 1
std::cout <<"Adding extra-ordinary node "<< gMaster
<<" for rigid coupling in Patch "<< idx+1 << std::endl;
#endif
myMLGN.push_back(gMaster);
myRmaster[myMLGN.size()] = { Xpt.x, Xpt.y, Xpt.z };
return newNode;
}
void ASMbase::addRigidMPC (int gSlave, int gMaster, const Vec3& dX)
{
for (unsigned short int dof = 1; dof <= nf; dof++)
{
MPC* cons = new MPC(gSlave,dof);
if (this->addMPC(cons) && cons)
{
// Add one-to-one translation coupling
cons->addMaster(gMaster,dof,1.0);
// Add translation-to-rotation coupling
switch (dof) {
case 1: // u_x = dZ*theta_y - dY*theta_z
cons->addMaster(gMaster,5, dX.z);
cons->addMaster(gMaster,6,-dX.y);
break;
case 2: // u_y = dX*theta_z - dZ*theta_x
cons->addMaster(gMaster,4,-dX.z);
cons->addMaster(gMaster,6, dX.x);
break;
case 3: // u_z = dY*theta_x - dX*theta_y
cons->addMaster(gMaster,4, dX.y);
cons->addMaster(gMaster,5,-dX.x);
break;
}
#if SP_DEBUG > 1
std::cout <<"Added constraint: "<< *cons;
#endif
}
}
}
bool ASMbase::addRigidCpl (int lindx, int ldim, int basis,
int& gMaster, const Vec3& Xmaster, bool extraPt)
{
if (ldim+1 != ndim)
{
IFEM::cout <<" ** ASMbase::addRigidCpl: Not implemented for "
<< ldim <<"-dimensional boundaries (ignored)."<< std::endl;
return false;
}
if (extraPt) // The master point is not a patch node, create an extra node
extraPt = this->createRgdMasterNode(gMaster,Xmaster);
IntVec nodes;
this->getBoundaryNodes(lindx,nodes,basis,1,0,true);
this->addRigidCouplings(gMaster,Xmaster,nodes);
return extraPt;
}
void ASMbase::addRigidCouplings (int gMaster, const Vec3& Xmaster,
const IntVec& slaveNodes)
{
for (int node : slaveNodes)
{
Vec3 dX = this->getCoord(node) - Xmaster;
if (nsd == 3)
this->addRigidMPC(MLGN[node-1],gMaster,dX);
else if (nsd == 2 && nf > 1)
{
// Special for 2D problems with (at least) 2 nodal DOFs
MPC* cons = new MPC(MLGN[node-1],1);
if (this->addMPC(cons) && cons)
{
cons->addMaster(gMaster,1, 1.0);
cons->addMaster(gMaster,3,-dX.y);
}
#if SP_DEBUG > 1
std::cout <<"Added constraint: "<< *cons;
#endif
cons = new MPC(MLGN[node-1],2);
if (this->addMPC(cons) && cons)
{
cons->addMaster(gMaster,2, 1.0);
cons->addMaster(gMaster,3, dX.x);
#if SP_DEBUG > 1
std::cout <<"Added constraint: "<< *cons;
#endif
}
}
}
}
MPC* ASMbase::findMPC (int node, int dof) const
{
MPC slave(node,dof);
MPCIter cit = mpcs.find(&slave);
return cit == mpcs.end() ? nullptr : *cit;
}
bool ASMbase::addPeriodicity (size_t master, size_t slave, int dir)
{
int slaveNode = this->getNodeID(slave);
int masterNode = this->getNodeID(master);
if (slaveNode < 1 || masterNode < 1)
{
std::cerr <<" *** ASMbase::addPeriodicity: Invalid node indices "
<< master <<", "<< slave << std::endl;
return false;
}
if (this->add2PC(masterNode,dir,slaveNode) ||
this->add2PC(slaveNode,dir,masterNode))
return true;
std::cerr <<" *** ASMbase::addPeriodicity: Failed to connect nodes "
<< masterNode <<" and "<< slaveNode <<" in direction "
<< dir << std::endl;
return false;
}
void ASMbase::makePeriodic (size_t master, size_t slave, int dirs)
{
std::set<int> dofs(utl::getDigits(dirs));
if (dofs.size() == nf && *dofs.rbegin() == nf)
// If all DOFs are going to be coupled, assign a common global node number
ASMbase::collapseNodes(*this,master,*this,slave);
else for (int dof : dofs)
this->addPeriodicity(master,slave,dof);
}
void ASMbase::constrainPatch (int dof, int code)
{
if (code > 0)
std::cerr <<" ** ASMbase::constrainPatch: Projection onto the spline basis"
<<" not yet implemented!"<< std::endl;
else if (code < 0)
code = -code;
for (size_t node = 1; node <= this->getNoNodes(1); node++)
this->prescribe(node,dof,code);
}
void ASMbase::constrainNodes (const IntVec& nodes, int dof, int code,
bool overrideD)
{
if (code < 0) code = -code;
int maxNod = this->getNoNodes(1);
for (int node : nodes)
if (node > 0 && node <= maxNod)
this->prescribe(node,dof,code,overrideD);
else
std::cerr <<" ** ASMbase::constrainNodes: Node "<< node
<<" is out of range [1,"<< maxNod <<"]."<< std::endl;
}
bool ASMbase::constrainXnode (int node, int dof, int code)
{
for (size_t inod = nnod+1; inod <= MLGN.size(); inod++)
if (this->isRMn(inod) && MLGN[inod-1] == node)
{
this->prescribe(inod,dof,code);
return true;
}
return false;
}
int ASMbase::prescribe (size_t inod, int dirs, int code, bool overrideD)
{
if (code == 0 && fixHomogeneousDirichlet)
return this->fix(inod,dirs);
int node = this->getNodeID(inod);
if (node < 1 || dirs < 1) return dirs;
int ignoredDirs = 0;
for (int dof : utl::getDigits(dirs))
if (dof <= nf)
{
MPC* mpc = new MPC(node,dof,1.0);
if (!this->addMPC(mpc,code,true,overrideD))
ignoredDirs = 10*ignoredDirs + dof;
}
else
{
ignoredDirs = 10*ignoredDirs + dof;
std::cerr <<" ** ASMbase::prescribe: Ignoring invalid DOF code "<< dof
<< std::endl;
}
return ignoredDirs;
}
/*!
\brief Equality operator for BC objects comparing node numbers and DOF codes.
*/
bool operator== (const ASMbase::BC& rhs, const ASMbase::BC& lhs)
{
return (rhs.node == lhs.node &&
rhs.CX == lhs.CX && rhs.CY == lhs.CY && rhs.CZ == lhs.CZ &&
rhs.RX == lhs.RX && rhs.RY == lhs.RY && rhs.RZ == lhs.RZ);
}
/*!
\brief Equality operator for BC objects comparing node numbers only.
*/
bool operator== (const ASMbase::BC& rhs, const int& lhs)
{
return rhs.node == lhs;
}
int ASMbase::BC::free (int dof)
{
switch (dof) {
case 1: CX = 1; break;
case 2: CY = 1; break;
case 3: CZ = 1; break;
case 4: RX = 1; break;
case 5: RY = 1; break;
case 6: RZ = 1; break;
}
return (CX + CY + CZ + RX + RY + RZ);
}
int ASMbase::BC::fix (int dof)
{
switch (dof) {
case 1: CX = 0; break;
case 2: CY = 0; break;
case 3: CZ = 0; break;
case 4: RX = 0; break;
case 5: RY = 0; break;
case 6: RZ = 0; break;
}
return 6 - (CX + CY + CZ + RX + RY + RZ);
}
int ASMbase::fix (size_t inod, int dirs)
{
int node = this->getNodeID(inod);
if (node < 1 || dirs < 1) return dirs;
BCVec::iterator bit = std::find(BCode.begin(),BCode.end(),node);
if (bit == BCode.end())
{
BCode.push_back(BC(node));
bit = BCode.end()-1;
}
#if SP_DEBUG > 1
BC old = *bit;
#endif
int invalidDOFs = 0;
for (int dof : utl::getDigits(dirs))
if (dof <= nf)
bit->fix(dof);
else
{
invalidDOFs = 10*invalidDOFs + dof;
std::cerr <<" ** ASMbase::fix: Ignoring invalid DOF code "<< dof
<< std::endl;
}
#if SP_DEBUG > 1
if (!(old == *bit))
std::cout <<"\tFixed node: "<< node <<" "<< dirs << std::endl;
#endif
return invalidDOFs;
}
bool ASMbase::allDofs (int dirs) const
{
int myDof = 0;
for (int dof : utl::getDigits(dirs))
if (++myDof != dof)
return false;
return myDof == nf;
}
void ASMbase::mergeAndGetAllMPCs (const ASMVec& model, MPCSet& allMPCs)
{
// Build the set of constraint equations over all patches in the model
if (model.size() == 1)
{
// Trivial for single-patch models
allMPCs.insert(model.front()->begin_MPC(),model.front()->end_MPC());
return;
}
// In multi-patch models interface nodes may be constrained twice or more.
// Resolve this such that allMPCs only contains the set of unique MPCs.
int nmerged = 0;
int ndeleted = 0;
for (ASMbase* pch : model)
{
std::vector<MPC*> uniqueMPC;
uniqueMPC.reserve(pch->getNoMPCs());
for (MPC* mpc : pch->mpcs)
if (std::pair<MPCIter,bool> ret = allMPCs.insert(mpc); ret.second)
uniqueMPC.push_back(mpc);
else
{
// Merge multiple constraint equations with common slave definition
if ((*ret.first)->getSlave().coeff == 0.0 &&
(*ret.first)->getNoMaster() > 1 &&
(*ret.first)->merge(mpc))
{
#if SP_DEBUG > 1
std::cout <<"Merging constraint "<< *mpc;
std::cout <<"Resulting constraint "<< **ret.first;
#endif
nmerged++;
}
else
{
// The found constraint *ret.first is either a prescribed movement
// or a single-master constraint. Such MPCs are not to be merged but
// should superseed any multi-master constraints with matching slave.
#if SP_DEBUG > 1
std::cout <<"Deleted constraint "<< *mpc;
#endif
ndeleted++;
}
pch->dCode.erase(mpc);
delete mpc;
}
if (uniqueMPC.size() < pch->getNoMPCs())
{
// Compress the MPC set for this patch removing duplicated entries
pch->mpcs.clear();
pch->mpcs.insert(uniqueMPC.begin(),uniqueMPC.end());
}
}
if (nmerged > 0)
IFEM::cout <<"Merged "<< nmerged <<" MPC equations."<< std::endl;
if (ndeleted > 0)
IFEM::cout <<"Deleted "<< ndeleted <<" MPC equations."<< std::endl;
#if SP_DEBUG > 1