-
Notifications
You must be signed in to change notification settings - Fork 0
/
damage_impl.h
2664 lines (2032 loc) · 83.5 KB
/
damage_impl.h
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
/*
* Copyright (c) 2019: Ruhr University Bochum
* Authors: Andreas Vogel
*
* This file is part of UG4.
*
* UG4 is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License version 3 (as published by the
* Free Software Foundation) with the following additional attribution
* requirements (according to LGPL/GPL v3 §7):
*
* (1) The following notice must be displayed in the Appropriate Legal Notices
* of covered and combined works: "Based on UG4 (www.ug4.org/license)".
*
* (2) The following notice must be displayed at a prominent place in the
* terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
*
* (3) The following bibliography is recommended for citation and must be
* preserved in all covered files:
* "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
* parallel geometric multigrid solver on hierarchically distributed grids.
* Computing and visualization in science 16, 4 (2013), 151-164"
* "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
* flexible software system for simulating pde based models on high performance
* computers. Computing and visualization in science 16, 4 (2013), 165-179"
*
* This program 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.
*/
#ifndef __SMALL_STRAIN_MECHANICS__DAMAGE_IMPL_H_
#define __SMALL_STRAIN_MECHANICS__DAMAGE_IMPL_H_
#include "damage.h"
#include "common/math/math_vector_matrix/math_vector_functions.h"
namespace ug{
namespace SmallStrainMechanics{
template <int dim>
void AveragePositions( MathVector<dim>& vCenter,
const std::vector<MathVector<dim> >& vCornerCoords)
{
vCenter = vCornerCoords[0];
for(size_t j = 1; j < vCornerCoords.size(); ++j)
{
vCenter += vCornerCoords[j];
}
vCenter *= 1./(number)( vCornerCoords.size());
}
////////////////////////////////////////////////////////////////////////////////
// Collect Surface Neighbors
////////////////////////////////////////////////////////////////////////////////
template <typename TDomain>
void CollectSurfaceNeighbors(
SmartPtr<GridFunction<TDomain, CPUAlgebra> > spF, // some dummy function
typename grid_dim_traits<TDomain::dim>::element_type* elem,
std::vector< typename grid_dim_traits<TDomain::dim>::element_type * >& vNeighbors)
{
PROFILE_BEGIN_GROUP(CollectSurfaceNeighbors, "Small Strain Mech");
static const int dim = TDomain::dim;
typedef typename TDomain::grid_type TGrid;
typedef typename grid_dim_traits<dim>::element_type TElem;
typedef typename grid_dim_traits<dim>::side_type TSide;
typedef typename contrained_dim_traits<dim>::contrained_side_type TContrainedSide;
typedef typename contrained_dim_traits<dim>::contraining_side_type TContrainingSide;
typedef typename TDomain::position_accessor_type TPositionAccessor;
// get domain
SmartPtr<TDomain> domain = spF->domain();
SmartPtr<typename TDomain::grid_type> spGrid = domain->grid();
typename TDomain::grid_type& grid = *spGrid;
// get dof distribution
SmartPtr<DoFDistribution> dd = spF->dd();
// get iterators
typename DoFDistribution::traits<TElem>::iterator iter, iterEnd;
iter = dd->begin<TElem>(SurfaceView::ALL); // SurfaceView::MG_ALL
iterEnd = dd->end<TElem>(SurfaceView::ALL);
// clear container
vNeighbors.clear();
// get all sides in order
typename TGrid::template traits<TSide>::secure_container vSide;
grid.associated_elements_sorted(vSide, elem);
// begin marking
grid.begin_marking();
// mark the elem
grid.mark(elem);
////////////////////////////////////////////////////////////////////////////
// loop all sides
////////////////////////////////////////////////////////////////////////////
for(size_t s = 0; s < vSide.size(); ++s){
// get all connected elements
typename TGrid::template traits<TElem>::secure_container vElemOfSide;
grid.associated_elements(vElemOfSide, vSide[s]);
// if no elem found: internal error
if(vElemOfSide.size() == 0)
UG_THROW("Huh, why no element? Should be at least the considered elem itself");
////////////////////////////////////////////////////////////////////////////
// handle sides with only one element
// ( if only one element for side, it must be a boundary element or contrained)
////////////////////////////////////////////////////////////////////////////
if(vElemOfSide.size() == 1){
////////////////////////////////////////////////////////////////////////////
// handle constraint edge
//
////////////////////////////////////////////////////////////////////////////
if(vSide[s]->is_constrained()){
TContrainedSide* cSide = dynamic_cast<TContrainedSide*>(vSide[s]);
TSide* constrainingSide = dynamic_cast<TSide*>(cSide->get_constraining_object());
typename TGrid::template traits<TElem>::secure_container vElemOfContrainingSide;
grid.associated_elements(vElemOfContrainingSide, constrainingSide);
if(vElemOfContrainingSide.size() != 2) UG_THROW("Huh, should be "<<2<<" at constraining side");
for(size_t nbr = 0; nbr < vElemOfContrainingSide.size(); ++nbr){
TElem* neighborElem = vElemOfContrainingSide[nbr];
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vNeighbors.push_back(neighborElem);
}
}
}
////////////////////////////////////////////////////////////////////////////
// handle hanging sides with 2 elements
// ( coupled to all fine neighbor elems )
////////////////////////////////////////////////////////////////////////////
else if(vSide[s]->is_constraining()){
TContrainingSide* cSide = dynamic_cast<TContrainingSide*>(vSide[s]);
const size_t numConstrained = cSide->template num_constrained<TSide>();
for(size_t cs = 0; cs < numConstrained; ++cs){
TSide* constrainedSide = cSide->template constrained<TSide>(cs);
// neighbor elem
typename TGrid::template traits<TElem>::secure_container vElemOfContrainedSide;
grid.associated_elements(vElemOfContrainedSide, constrainedSide);
if(vElemOfContrainedSide.size() != 1) UG_THROW("Huh, should be 1 at constrained side");
TElem* neighborElem = vElemOfContrainedSide[0];
grid.mark(neighborElem);
vNeighbors.push_back(neighborElem);
}
}
////////////////////////////////////////////////////////////////////////////
// regular refined sides
// (find all direct face neighbors)
////////////////////////////////////////////////////////////////////////////
else{
// if more than 2 elem found: internal error
if(vElemOfSide.size() != 2)
UG_THROW("Huh, why more than 2 elements of side?");
for(size_t eos = 0; eos < vElemOfSide.size(); ++eos){
// neighbor elem
TElem* neighborElem = vElemOfSide[eos];
// check
if(grid.template num_children<TElem,TElem>(neighborElem) > 0)
UG_THROW("Huh, why not on top level?");
// skip self
if(neighborElem == elem) continue;
grid.mark(neighborElem);
vNeighbors.push_back(neighborElem);
}
}
} // end loop sides
////////////////////////////////////////////////////////////////////////////
// loop all sides (FOR DIAG COUPLING)
////////////////////////////////////////////////////////////////////////////
for(size_t s = 0; s < vSide.size(); ++s){
// get all connected elements
typename TGrid::template traits<TElem>::secure_container vElemOfSide;
grid.associated_elements(vElemOfSide, vSide[s]);
// if no elem found: internal error
if(vElemOfSide.size() == 0)
UG_THROW("Huh, why no element? Should be at least the considered elem itself");
////////////////////////////////////////////////////////////////////////////
// diagonal elements (only coupled via a common vertex)
// (find all vertex coupled neighbors)
////////////////////////////////////////////////////////////////////////////
// loop vertices of side
Vertex* const* vVertex = vSide[s]->vertices();
const size_t numVertex = vSide[s]->num_vertices();
for(size_t vrt = 0; vrt < numVertex; ++vrt)
{
// collect all diag leaf-elems on same level
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, vVertex[vrt]);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vNeighbors.push_back(neighborElem);
}
// collect all diag leaf-elems on finer level
if(grid.template num_children<Vertex,Vertex>(vVertex[vrt]) > 0){
if(grid.template num_children<Vertex,Vertex>(vVertex[vrt]) != 1)
UG_THROW("Huh, why more than one vertex child?")
Vertex* fineVrt = grid.template get_child<Vertex,Vertex>(vVertex[vrt], 0);
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, fineVrt);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vNeighbors.push_back(neighborElem);
}
}
// collect all diag leaf-elems on coarser level
if(grid.get_parent(vVertex[vrt]) != 0){
Vertex* coarseVrt = dynamic_cast<Vertex*>(grid.get_parent(vVertex[vrt]));
if(coarseVrt != 0){
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, coarseVrt);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vNeighbors.push_back(neighborElem);
}
}
}
}
} // end side loop
// end marking
grid.end_marking();
}
////////////////////////////////////////////////////////////////////////////////
// CollectStencilNeighbors_NeumannZeroBND_IndexAndDistance
////////////////////////////////////////////////////////////////////////////////
template <typename TDomain>
void CollectStencilNeighbors_NeumannZeroBND_IndexAndDistance
(
std::vector< typename grid_dim_traits<TDomain::dim>::element_type* >& vElem,
std::vector<size_t>& vIndex,
std::vector< MathVector<TDomain::dim> >& vDistance,
typename grid_dim_traits<TDomain::dim>::element_type* elem,
typename TDomain::grid_type& grid,
typename TDomain::position_accessor_type& aaPos,
SmartPtr<GridFunction<TDomain, CPUAlgebra> > spF, bool fillElemSizeIntoVector
)
{
PROFILE_BEGIN_GROUP(DamageFunctionUpdater_CollectStencilNeighbors, "Small Strain Mech");
static const int dim = TDomain::dim;
typedef typename TDomain::grid_type TGrid;
typedef typename grid_dim_traits<dim>::element_type TElem;
typedef typename grid_dim_traits<dim>::side_type TSide;
typedef typename contrained_dim_traits<dim>::contrained_side_type TContrainedSide;
typedef typename contrained_dim_traits<dim>::contraining_side_type TContrainingSide;
typedef typename TDomain::position_accessor_type TPositionAccessor;
//static const int numNeighborsToFind = 2*dim + (dim * (dim-1)) / 2;
const size_t fct = 0;
vElem.clear();
vIndex.clear();
vDistance.clear();
// get vertices of element
Vertex* const* vVertex = elem->vertices();
const size_t numVertex = elem->num_vertices();
// corner coordinates
std::vector<MathVector<dim> > vCornerCoords;
vCornerCoords.resize(numVertex);
for(size_t vrt = 0; vrt < numVertex; ++vrt){
vCornerCoords[vrt] = aaPos[ vVertex[vrt] ];
}
// element midpoint
MathVector<dim> ElemMidPoint;
AveragePositions<dim>(ElemMidPoint, vCornerCoords);
// UG_LOG("############## Element with midpoint " << ElemMidPoint << " ################ \n")
if(fillElemSizeIntoVector){
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(elem, fct, ind) != 1) UG_THROW("Wrong number dofs");
const size_t i = ind[0][0];
// element volume
(*spF)[i] = ElementSize<dim>(elem->reference_object_id(), &vCornerCoords[0]);
}
// get all sides in order
typename TGrid::template traits<TSide>::secure_container vSide;
grid.associated_elements_sorted(vSide, elem);
// begin marking
grid.begin_marking();
// mark the elem
grid.mark(elem);
////////////////////////////////////////////////////////////////////////////
// loop all sides
////////////////////////////////////////////////////////////////////////////
for(size_t s = 0; s < vSide.size(); ++s){
// get all connected elements
typename TGrid::template traits<TElem>::secure_container vElemOfSide;
grid.associated_elements(vElemOfSide, vSide[s]);
// if no elem found: internal error
if(vElemOfSide.size() == 0)
UG_THROW("Huh, why no element?");
////////////////////////////////////////////////////////////////////////////
// handle sides with only one element
// ( if only one element for side, it must be a boundary element or contrained)
////////////////////////////////////////////////////////////////////////////
if(vElemOfSide.size() == 1){
////////////////////////////////////////////////////////////////////////////
// handle constraint edge
////////////////////////////////////////////////////////////////////////////
if(vSide[s]->is_constrained()){
TContrainedSide* cSide = dynamic_cast<TContrainedSide*>(vSide[s]);
TSide* constrainingSide = dynamic_cast<TSide*>(cSide->get_constraining_object());
typename TGrid::template traits<TElem>::secure_container vElemOfContrainingSide;
grid.associated_elements(vElemOfContrainingSide, constrainingSide);
if(vElemOfContrainingSide.size() != 2) UG_THROW("Huh, should be "<<2<<" at constraining side");
for(size_t nbr = 0; nbr < vElemOfContrainingSide.size(); ++nbr){
TElem* neighborElem = vElemOfContrainingSide[nbr];
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vElem.push_back(neighborElem);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
vDistance.resize(vDistance.size()+1);
CollectCornerCoordinates(vCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(vDistance.back(), vCornerCoords);
VecScaleAdd(vDistance.back(), 1.0, ElemMidPoint, -1.0, vDistance.back());
}
}
////////////////////////////////////////////////////////////////////////////
// handle mirror elements
// ( if only one element for side, it must be a boundary element)
////////////////////////////////////////////////////////////////////////////
else {
vElem.push_back(vElemOfSide[0]);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(elem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
MathVector<dim> n;
CollectCornerCoordinates(vCornerCoords, *vSide[s], aaPos);
ElementNormal<dim>(vSide[s]->reference_object_id(), n, &vCornerCoords[0]);
vDistance.resize(vDistance.size()+1);
ProjectPointToPlane(vDistance.back(), ElemMidPoint, vCornerCoords[0], n);
VecScaleAdd(vDistance.back(), 2.0, ElemMidPoint, -2.0, vDistance.back());
// UG_LOG("is boundary, with vDistance: " << vDistance.back() << "\n");
}
}
////////////////////////////////////////////////////////////////////////////
// handle hanging sides with 2 elements
// ( coupled to all fine neighbor elems )
////////////////////////////////////////////////////////////////////////////
else if(vSide[s]->is_constraining()){
TContrainingSide* cSide = dynamic_cast<TContrainingSide*>(vSide[s]);
const size_t numConstrained = cSide->template num_constrained<TSide>();
for(size_t cs = 0; cs < numConstrained; ++cs){
TSide* constrainedSide = cSide->template constrained<TSide>(cs);
// neighbor elem
typename TGrid::template traits<TElem>::secure_container vElemOfContrainedSide;
grid.associated_elements(vElemOfContrainedSide, constrainedSide);
if(vElemOfContrainedSide.size() != 1) UG_THROW("Huh, should be 1 at constrained side");
TElem* neighborElem = vElemOfContrainedSide[0];
grid.mark(neighborElem);
vElem.push_back(neighborElem);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
vDistance.resize(vDistance.size()+1);
CollectCornerCoordinates(vCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(vDistance.back(), vCornerCoords);
VecScaleAdd(vDistance.back(), 1.0, ElemMidPoint, -1.0, vDistance.back());
}
}
////////////////////////////////////////////////////////////////////////////
// regular refined sides
// (find all direct face neighbors)
////////////////////////////////////////////////////////////////////////////
else{
// if more than 2 elem found: internal error
if(vElemOfSide.size() != 2)
UG_THROW("Huh, why more than 2 elements of side?");
for(size_t eos = 0; eos < vElemOfSide.size(); ++eos){
// neighbor elem
TElem* neighborElem = vElemOfSide[eos];
// skip self
if(neighborElem == elem) continue;
// add neighbor
grid.mark(neighborElem);
vElem.push_back(neighborElem);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
vDistance.resize(vDistance.size()+1);
CollectCornerCoordinates(vCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(vDistance.back(), vCornerCoords);
VecScaleAdd(vDistance.back(), 1.0, ElemMidPoint, -1.0, vDistance.back());
// UG_LOG("has neighbor, c" << vDistance.back() << "\n");
}
}
}
////////////////////////////////////////////////////////////////////////////
// add additional elements (via vertex coupling)
////////////////////////////////////////////////////////////////////////////
for(size_t vrt = 0; vrt < numVertex; ++vrt)
{
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, vVertex[vrt]);
// collect all diag leaf-elems on same level
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vElem.push_back(neighborElem);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
vDistance.resize(vDistance.size()+1);
CollectCornerCoordinates(vCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(vDistance.back(), vCornerCoords);
VecScaleAdd(vDistance.back(), 1.0, ElemMidPoint, -1.0, vDistance.back());
}
// collect all diag leaf-elems on finer level
if(grid.template num_children<Vertex,Vertex>(vVertex[vrt]) > 0){
if(grid.template num_children<Vertex,Vertex>(vVertex[vrt]) != 1)
UG_THROW("Huh, why more than one vertex child?")
Vertex* fineVrt = grid.template get_child<Vertex,Vertex>(vVertex[vrt], 0);
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, fineVrt);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vElem.push_back(neighborElem);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
vDistance.resize(vDistance.size()+1);
CollectCornerCoordinates(vCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(vDistance.back(), vCornerCoords);
VecScaleAdd(vDistance.back(), 1.0, ElemMidPoint, -1.0, vDistance.back());
}
}
// collect all diag leaf-elems on coarser level
if(grid.get_parent(vVertex[vrt]) != 0){
Vertex* coarseVrt = dynamic_cast<Vertex*>(grid.get_parent(vVertex[vrt]));
if(coarseVrt != 0){
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, coarseVrt);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
vElem.push_back(neighborElem);
// add index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex.push_back(ind[0][0]);
// add distance
vDistance.resize(vDistance.size()+1);
CollectCornerCoordinates(vCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(vDistance.back(), vCornerCoords);
VecScaleAdd(vDistance.back(), 1.0, ElemMidPoint, -1.0, vDistance.back());
}
}
}
}
// end marking
grid.end_marking();
}
////////////////////////////////////////////////////////////////////////////////
// By partial integration
////////////////////////////////////////////////////////////////////////////////
template <typename TDomain>
void InitLaplacian_PartialIntegration(
SmartPtr<GridFunction<TDomain, CPUAlgebra> > spF, // some dummy function
std::vector< std::vector< number > >& vStencil,
std::vector< std::vector<size_t> >& vIndex,
int quadRuleType, bool fillElemSizeIntoVector)
{
PROFILE_BEGIN_GROUP(InitLaplacian_PartialIntegration, "Small Strain Mech");
static const int dim = TDomain::dim;
typedef typename TDomain::grid_type TGrid;
typedef typename grid_dim_traits<dim>::element_type TElem;
typedef typename grid_dim_traits<dim>::side_type TSide;
typedef typename contrained_dim_traits<dim>::contrained_side_type TContrainedSide;
typedef typename contrained_dim_traits<dim>::contraining_side_type TContrainingSide;
typedef typename TDomain::position_accessor_type TPositionAccessor;
if(dim == 3)
UG_THROW("This implementation is 2d only, currently. Handle vertex neighbors properly in 3d...");
const size_t fct = 0;
number sideWeight;
number vertexWeight;
if(quadRuleType == 1) // Midpoint
{
sideWeight = 1.0;
vertexWeight = 0.0;
}
else if (quadRuleType == 2) // Simpson's
{
sideWeight = 4.0/6.0;
vertexWeight = 1.0/6.0;
}
else
UG_THROW("InitLaplacian_PartialIntegration: wrong quad rule")
// get domain
SmartPtr<TDomain> domain = spF->domain();
SmartPtr<typename TDomain::grid_type> spGrid = domain->grid();
typename TDomain::grid_type& grid = *spGrid;
typename TDomain::position_accessor_type& aaPos = domain->position_accessor();
// get dof distribution
SmartPtr<DoFDistribution> dd = spF->dd();
// get iterators
typename DoFDistribution::traits<TElem>::iterator iter, iterEnd;
iter = dd->begin<TElem>(SurfaceView::ALL); // SurfaceView::MG_ALL
iterEnd = dd->end<TElem>(SurfaceView::ALL);
// resize result vectors
const size_t numIndex = spF->num_dofs();
vIndex.resize(numIndex);
vStencil.resize(numIndex);
// storage (for reuse)
std::vector<MathVector<dim> > vCornerCoords, vNbrCornerCoords;
MathVector<dim> ElemMidPoint, Normal, Distance;
// loop all vertices
for(;iter != iterEnd; ++iter)
{
// get vertex
TElem* elem = *iter;
// store index
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(elem, fct, ind) != 1) UG_THROW("Wrong number dofs");
const size_t i = ind[0][0];
// add self-coupling
vIndex[i].clear();
vIndex[i].push_back(i);
// reset stencil
vStencil[i].clear();
vStencil[i].resize(1, 0.0);
// get vertices of element
Vertex* const* vVertex = elem->vertices();
const size_t numVertex = elem->num_vertices();
// corner coordinates
vCornerCoords.resize(numVertex);
for(size_t vrt = 0; vrt < numVertex; ++vrt){
vCornerCoords[vrt] = aaPos[ vVertex[vrt] ];
}
// element volume
const number vol = ElementSize<dim>(elem->reference_object_id(), &vCornerCoords[0]);
if(fillElemSizeIntoVector)
(*spF)[i] = vol;
// element midpoint
AveragePositions<dim>(ElemMidPoint, vCornerCoords);
// get all sides in order
typename TGrid::template traits<TSide>::secure_container vSide;
grid.associated_elements_sorted(vSide, elem);
// begin marking
grid.begin_marking();
// mark the elem
grid.mark(elem);
////////////////////////////////////////////////////////////////////////////
// loop all sides
////////////////////////////////////////////////////////////////////////////
for(size_t s = 0; s < vSide.size(); ++s){
// side normal
SideNormal<dim>(elem->reference_object_id(), Normal, s, &vCornerCoords[0]);
// get all connected elements
typename TGrid::template traits<TElem>::secure_container vElemOfSide;
grid.associated_elements(vElemOfSide, vSide[s]);
// if no elem found: internal error
if(vElemOfSide.size() == 0)
UG_THROW("Huh, why no element? Should be at least the considered elem itself");
////////////////////////////////////////////////////////////////////////////
// handle sides with only one element
// ( if only one element for side, it must be a boundary element or contrained)
////////////////////////////////////////////////////////////////////////////
if(vElemOfSide.size() == 1){
////////////////////////////////////////////////////////////////////////////
// handle constraint edge
//
////////////////////////////////////////////////////////////////////////////
if(vSide[s]->is_constrained()){
TContrainedSide* cSide = dynamic_cast<TContrainedSide*>(vSide[s]);
TSide* constrainingSide = dynamic_cast<TSide*>(cSide->get_constraining_object());
typename TGrid::template traits<TElem>::secure_container vElemOfContrainingSide;
grid.associated_elements(vElemOfContrainingSide, constrainingSide);
if(vElemOfContrainingSide.size() != 2) UG_THROW("Huh, should be 2 at constraining side");
for(size_t nbr = 0; nbr < vElemOfContrainingSide.size(); ++nbr){
TElem* neighborElem = vElemOfContrainingSide[nbr];
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
grid.mark(neighborElem);
// add distance
CollectCornerCoordinates(vNbrCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(Distance, vNbrCornerCoords);
VecScaleAdd(Distance, 1.0, ElemMidPoint, -1.0, Distance);
const number cpl = (sideWeight) * VecDot(Normal, Distance) / ( VecTwoNormSq(Distance) * vol);
vStencil[i].push_back( -cpl );
vStencil[i][0] += cpl;
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex[i].push_back(ind[0][0]);
}
}
////////////////////////////////////////////////////////////////////////////
// handle mirror elements
// ( if only one element for side, it must be a boundary element)
////////////////////////////////////////////////////////////////////////////
else {
ProjectPointToPlane(Distance, ElemMidPoint, aaPos[ (vSide[s]->vertex(0)) ], Normal);
VecScaleAdd(Distance, 2.0, ElemMidPoint, -2.0, Distance);
const number cpl = VecDot(Normal, Distance) / ( VecTwoNormSq(Distance) * vol);
vStencil[i].push_back( -cpl );
vStencil[i][0] += cpl;
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(elem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex[i].push_back(ind[0][0]);
}
}
////////////////////////////////////////////////////////////////////////////
// handle hanging sides with 2 elements
// ( coupled to all fine neighbor elems )
////////////////////////////////////////////////////////////////////////////
else if(vSide[s]->is_constraining()){
TContrainingSide* cSide = dynamic_cast<TContrainingSide*>(vSide[s]);
const size_t numConstrained = cSide->template num_constrained<TSide>();
for(size_t cs = 0; cs < numConstrained; ++cs){
TSide* constrainedSide = cSide->template constrained<TSide>(cs);
// neighbor elem
typename TGrid::template traits<TElem>::secure_container vElemOfContrainedSide;
grid.associated_elements(vElemOfContrainedSide, constrainedSide);
if(vElemOfContrainedSide.size() != 1) UG_THROW("Huh, should be 1 at constrained side");
TElem* neighborElem = vElemOfContrainedSide[0];
grid.mark(neighborElem);
// add distance
CollectCornerCoordinates(vNbrCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(Distance, vNbrCornerCoords);
VecScaleAdd(Distance, 1.0, ElemMidPoint, -1.0, Distance);
const number cpl = (sideWeight / numConstrained) * VecDot(Normal, Distance) / ( VecTwoNormSq(Distance) * vol);
vStencil[i].push_back( -cpl );
vStencil[i][0] += cpl;
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex[i].push_back(ind[0][0]);
}
}
////////////////////////////////////////////////////////////////////////////
// regular refined sides
// (find all direct face neighbors)
////////////////////////////////////////////////////////////////////////////
else{
// if more than 2 elem found: internal error
if(vElemOfSide.size() != 2)
UG_THROW("Huh, why more than 2 elements of side?");
for(size_t eos = 0; eos < vElemOfSide.size(); ++eos){
// neighbor elem
TElem* neighborElem = vElemOfSide[eos];
// check
if(grid.template num_children<TElem,TElem>(neighborElem) > 0)
UG_THROW("Huh, why not on top level?");
// skip self
if(neighborElem == elem) continue;
grid.mark(neighborElem);
// add distance
CollectCornerCoordinates(vNbrCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(Distance, vNbrCornerCoords);
VecScaleAdd(Distance, 1.0, ElemMidPoint, -1.0, Distance);
const number cpl = sideWeight * VecDot(Normal, Distance) / ( VecTwoNormSq(Distance) * vol);
vStencil[i].push_back( -cpl );
vStencil[i][0] += cpl;
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
vIndex[i].push_back(ind[0][0]);
}
}
} // end loop sides
////////////////////////////////////////////////////////////////////////////
// loop all sides (FOR DIAG COUPLING)
////////////////////////////////////////////////////////////////////////////
if(vertexWeight != 0.0){
for(size_t s = 0; s < vSide.size(); ++s){
// side normal
SideNormal<dim>(elem->reference_object_id(), Normal, s, &vCornerCoords[0]);
// get all connected elements
typename TGrid::template traits<TElem>::secure_container vElemOfSide;
grid.associated_elements(vElemOfSide, vSide[s]);
// if no elem found: internal error
if(vElemOfSide.size() == 0)
UG_THROW("Huh, why no element? Should be at least the considered elem itself");
////////////////////////////////////////////////////////////////////////////
// diagonal elements (only coupled via a common vertex)
// (find all vertex coupled neighbors)
////////////////////////////////////////////////////////////////////////////
// loop vertices of side
Vertex* const* vVertex = vSide[s]->vertices();
const size_t numVertex = vSide[s]->num_vertices();
for(size_t vrt = 0; vrt < numVertex; ++vrt)
{
std::vector<TElem*> vDiagNeighbors;
// collect all diag leaf-elems on same level
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, vVertex[vrt]);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
//UG_LOG("Over Same level\n")
//grid.mark(neighborElem);
vDiagNeighbors.push_back(neighborElem);
}
// collect all diag leaf-elems on finer level
if(grid.template num_children<Vertex,Vertex>(vVertex[vrt]) > 0){
if(grid.template num_children<Vertex,Vertex>(vVertex[vrt]) != 1)
UG_THROW("Huh, why more than one vertex child?")
Vertex* fineVrt = grid.template get_child<Vertex,Vertex>(vVertex[vrt], 0);
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, fineVrt);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
//UG_LOG("Over Children\n")
//grid.mark(neighborElem);
vDiagNeighbors.push_back(neighborElem);
}
}
// collect all diag leaf-elems on coarser level
if(grid.get_parent(vVertex[vrt]) != 0){
Vertex* coarseVrt = dynamic_cast<Vertex*>(grid.get_parent(vVertex[vrt]));
if(coarseVrt != 0){
typename TGrid::template traits<TElem>::secure_container vVertexNeighbor;
grid.associated_elements(vVertexNeighbor, coarseVrt);
for(size_t eov = 0; eov < vVertexNeighbor.size(); ++eov)
{
TElem* neighborElem = vVertexNeighbor[eov];
if(grid.is_marked(neighborElem)) continue;
if(grid.template num_children<TElem,TElem>(neighborElem) > 0) continue;
//UG_LOG("Over Parent\n")
//grid.mark(neighborElem);
vDiagNeighbors.push_back(neighborElem);
}
}
}
// add contribution
const size_t numDiagElem = vDiagNeighbors.size();
for(size_t diag = 0; diag < numDiagElem; ++diag){
TElem* neighborElem = vDiagNeighbors[diag];
// add distance
CollectCornerCoordinates(vNbrCornerCoords, *neighborElem, aaPos);
AveragePositions<dim>(Distance, vNbrCornerCoords);
VecScaleAdd(Distance, 1.0, ElemMidPoint, -1.0, Distance);
//UG_LOG("Elem: "<<i<<", side: "<<s<<", vrt: "<<vrt<<", Distance: "<<Distance<<", Normal: "<<Normal<<"\n");
std::vector<DoFIndex> ind;
if(spF->inner_dof_indices(neighborElem, fct, ind) != 1) UG_THROW("Wrong number dofs");
const size_t j = ind[0][0];
size_t k = 0;
for(; k < vIndex[i].size(); ++k){
if(vIndex[i][k] == j)
break;