Repository navigation
Expand file tree
/
Copy pathlevel_set_user_data.h
More file actions
1263 lines (1111 loc) · 37.6 KB
/
Copy pathlevel_set_user_data.h
File metadata and controls
1263 lines (1111 loc) · 37.6 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
/*
* Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
* Author: Christian Wehner
*
* 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 __LEVEL_SET__LEVEL_SET_USER_DATA__
#define __LEVEL_SET__LEVEL_SET_USER_DATA__
#include "common/common.h"
#include "lib_grid/tools/subset_group.h"
#include "lib_disc/lib_disc.h"
#include "lib_disc/common/function_group.h"
#include "lib_disc/common/groups_util.h"
#include "lib_disc/quadrature/quadrature.h"
#include "lib_disc/local_finite_element/local_finite_element_provider.h"
#include "lib_disc/spatial_disc/user_data/user_data.h"
#include "lib_disc/reference_element/reference_mapping_provider.h"
#include "lib_disc/operator/non_linear_operator/newton_solver/newton_update_interface.h"
namespace ug{
namespace LevelSet{
template <typename TData, int dim, typename TImpl>
class LevelSetUserDataBase
: public StdUserData<LevelSetUserDataBase<TData,dim,TImpl>, TData, dim>
{
public:
virtual void operator() (TData& value,
const MathVector<dim>& globIP,
number time, int si) const
{
UG_THROW("LevelSetUserData: Need element.");
}
virtual void operator() (TData vValue[],
const MathVector<dim> vGlobIP[],
number time, int si, const size_t nip) const
{
UG_THROW("LevelSetUserData: Need element.");
}
template <int refDim>
void evaluate(TData vValue[],
const MathVector<dim> vGlobIP[],
number time, int si,
GridObject* elem,
const MathVector<dim> vCornerCoords[],
const MathVector<refDim> vLocIP[],
const size_t nip,
LocalVector* u,
const MathMatrix<refDim, dim>* vJT = NULL) const
{
getImpl().template evaluate<refDim>(vValue,vGlobIP,time,si,elem,
vCornerCoords,vLocIP,nip,u,vJT);
}
virtual void compute(LocalVector* u, GridObject* elem,
const MathVector<dim> vCornerCoords[], bool bDeriv = false)
{
const number t = this->time();
const int si = this->subset();
for(size_t s = 0; s < this->num_series(); ++s)
getImpl().template evaluate<dim>(this->values(s), this->ips(s), t, si,
elem, vCornerCoords, this->template local_ips<dim>(s),
this->num_ip(s), u);
}
virtual void compute(LocalVectorTimeSeries* u, GridObject* elem,
const MathVector<dim> vCornerCoords[], bool bDeriv = false)
{
const int si = this->subset();
for(size_t s = 0; s < this->num_series(); ++s)
getImpl().template evaluate<dim>(this->values(s), this->ips(s), this->time(s), si,
elem, vCornerCoords, this->template local_ips<dim>(s),
this->num_ip(s), &(u->solution(this->time_point(s))));
}
/// returns if provided data is continuous over geometric object boundaries
virtual bool continuous() const {return false;}
/// returns if grid function is needed for evaluation
virtual bool requires_grid_fct() const {return true;}
protected:
/// access to implementation
TImpl& getImpl() {return static_cast<TImpl&>(*this);}
/// const access to implementation
const TImpl& getImpl() const {return static_cast<const TImpl&>(*this);}
};
template <typename TGridFunction>
class LevelSetUserData
: public LevelSetUserDataBase<number, TGridFunction::dim,
LevelSetUserData<TGridFunction> >, virtual public INewtonUpdate
{
/// domain type
typedef typename TGridFunction::domain_type domain_type;
/// algebra type
typedef typename TGridFunction::algebra_type algebra_type;
/// position accessor type
typedef typename domain_type::position_accessor_type position_accessor_type;
/// world dimension
static const int dim = domain_type::dim;
/// grid type
typedef typename domain_type::grid_type grid_type;
/// element type
typedef typename TGridFunction::template dim_traits<dim>::grid_base_object elem_type;
enum eval_type {sharp,cr_ip_average};
public:
// set evaluation type, implemented so far:
// 0 sharp (compute lsf in ip and give out inside value if inside or outside value if outside)
// 1 cr_ip_average (compute averaged value in ip, if in CR-FV-Geometry from-value in ip and to-value in ip have different signs,
// harmonic average is computed using intersection position from from and to node which is computed from level set function,
// the ips given in evaluate must be the CR-FV ips)
void set_eval_type(int type){
if (type==0) m_eval_type=sharp;
if (type==1) m_eval_type=cr_ip_average;
}
eval_type m_eval_type;
private:
// level set grid function
SmartPtr<TGridFunction> m_phi;
// approximation space for level and surface grid
SmartPtr<ApproximationSpace<domain_type> > m_spApproxSpace;
// grid
grid_type* m_grid;
/* // component of function
size_t m_fct;
// local finite element id
LFEID m_lfeID; */
public:
void set_inside_data(SmartPtr<CplUserData<number, dim> > user){
m_imInsideData = user;
}
void set_inside_data(number val){
set_inside_data(make_sp(new ConstUserNumber<dim>(val)));
}
#ifdef UG_FOR_LUA
void set_inside_data(const char* fctName){
set_inside_data(LuaUserDataFactory<number, dim>::create(fctName));
}
#endif
void set_outside_data(SmartPtr<CplUserData<number, dim> > user){
m_imOutsideData = user;
}
void set_outside_data(number val){
set_outside_data(make_sp(new ConstUserNumber<dim>(val)));
}
#ifdef UG_FOR_LUA
void set_outside_data(const char* fctName){
set_outside_data(LuaUserDataFactory<number, dim>::create(fctName));
}
#endif
private:
/// Data import for inside and outside data
SmartPtr<CplUserData<number,dim> > m_imInsideData;
SmartPtr<CplUserData<number,dim> > m_imOutsideData;
public:
/// constructor
LevelSetUserData(SmartPtr<ApproximationSpace<domain_type> > approxSpace,SmartPtr<TGridFunction> spGridFct){
m_phi = spGridFct;
domain_type& domain = *m_phi->domain().get();
grid_type& grid = *domain.grid();
m_grid = &grid;
m_spApproxSpace = approxSpace;
m_eval_type = cr_ip_average;
}
virtual ~LevelSetUserData(){};
template <int refDim>
inline void evaluate(number vValue[],
const MathVector<dim> vGlobIP[],
number time, int si,
GridObject* elem,
const MathVector<dim> vCornerCoords[],
const MathVector<refDim> vLocIP[],
const size_t nip,
LocalVector* u,
const MathMatrix<refDim, dim>* vJT = NULL) const
{
UG_ASSERT(dynamic_cast<elem_type*>(elem) != NULL, "Unsupported element type");
elem_type* element = static_cast<elem_type*>(elem);
const size_t numVertices = element->num_vertices();
std::vector<Vertex*> childVertex(numVertices);
std::vector<number> phi(numVertices);
// for (size_t i=0;i<nip;i++){
// UG_LOG("co(" << i << ",:)=" << vGlobIP[i] << "\n");
// }
// find child vertices by injection
for(size_t i = 0; i < numVertices; ++i){
childVertex[i] = element->vertex(i);
};
// find out level, then get child vertices on finest level
size_t numChildren = m_grid->template num_children<elem_type>(element);
if (numChildren!=0){
size_t lowerLevel=0;
elem_type* childElem = m_grid->template get_child<elem_type>(element,0);
numChildren = m_grid->template num_children<elem_type>(childElem);
lowerLevel++;
while (numChildren>0){
childElem = m_grid->template get_child<elem_type>(childElem,0);
numChildren = m_grid->template num_children<elem_type>(childElem);
lowerLevel++;
};
for (size_t i=0;i<nip;i++){
for (size_t j=0;j<lowerLevel;j++){
childVertex[i]=m_grid->template get_child<Vertex>(childVertex[i],0);
}
}
}
// create Multiindex
std::vector<DoFIndex> ind;
for (size_t i=0;i<numVertices;i++){
m_phi->dof_indices(childVertex[i], 0, ind);
phi[i]=DoFRef(*m_phi, ind[0]);
};
bool onls=false;
bool inside=false;
for (size_t i=0;i<numVertices;i++){
if (phi[i]==0){
continue;
};
if (phi[i]<0) inside=true;
for (size_t j=i+1;j<numVertices;j++){
if (phi[i]*phi[j]<0){
onls = true;
break;
};
}
}
if (onls==false){
if (inside==false){
(*m_imInsideData)(vValue,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
} else {
(*m_imOutsideData)(vValue,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
};
return;
};
number vValueInside[max_number_of_ips];
number vValueOutside[max_number_of_ips];
(*m_imInsideData)(vValueInside,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
(*m_imOutsideData)(vValueOutside,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
if (m_eval_type == sharp){
for(size_t ip = 0; ip < nip; ++ip)
{
// reference object id
ReferenceObjectID roid = elem->reference_object_id();
// memory for shapes
std::vector<number> vShape;
// compute lsf value in ip (from Lagrange-1 shape function) and set value according to position (inside or outside)
const LocalShapeFunctionSet<refDim>& rTrialSpace =
LocalFiniteElementProvider::get<refDim>(roid, LFEID(LFEID::LAGRANGE, dim, 1));
// evaluate shapes at ip
rTrialSpace.shapes(vShape, vLocIP[ip]);
// get multiindices of element
std::vector<DoFIndex> ind;
m_phi->dof_indices(elem, 0, ind);
// compute lsf at integration point
number phiValue = 0.0;
for(size_t sh = 0; sh < vShape.size(); ++sh)
{
phiValue += phi[sh] * vShape[sh];
}
if (phiValue<0) vValue[ip] = vValueInside[ip];
else vValue[ip] = vValueOutside[ip];
// UG_LOG("vValue(" << ip << ")=" << vValueOutside[ip] << "\n");
};
}
if (m_eval_type == cr_ip_average){
// reference object id
ReferenceObjectID roid = elem->reference_object_id();
// memory for shapes
std::vector<number> vShape;
// get domain of grid function
const domain_type& domain = *m_phi->domain().get();
// get position accessor
typedef typename domain_type::position_accessor_type position_accessor_type;
const position_accessor_type& posAcc = domain.position_accessor();
//position_accessor_type aaPos = m_phi->domain()->position_accessor();
// coord and vertex array
MathVector<dim> coCoord[domain_traits<dim>::MaxNumVerticesOfElem];
Vertex* vVrt[domain_traits<dim>::MaxNumVerticesOfElem];
DimCRFVGeometry<dim> geo;
for(size_t i = 0; i < numVertices; ++i){
vVrt[i] = element->vertex(i);
coCoord[i] = posAcc[vVrt[i]];
// UG_LOG("co_coord(" << i<< "+1,:)=" << coCoord[i] << "\n");
};
// evaluate finite volume geometry
geo.update(elem, &(coCoord[0]), domain.subset_handler().get());
std::vector<number> phiSideValue(geo.num_sh());
// compute interpolated lsf values in cr dofs
for (size_t i=0;i<geo.num_scv();i++){
// compute lsf value in ip (from Lagrange-1 shape function) and set value according to position (inside or outside)
const LocalShapeFunctionSet<dim>& rTrialSpace =
LocalFiniteElementProvider::get<dim>(roid, LFEID(LFEID::LAGRANGE, dim, 1));
// evaluate shapes at ip
rTrialSpace.shapes(vShape, geo.scv(i).local_ip());
// get multiindices of element
std::vector<DoFIndex> ind;
m_phi->dof_indices(elem, 0, ind);
// compute lsf at integration point
phiSideValue[i] = 0.0;
for(size_t sh = 0; sh < vShape.size(); ++sh)
{
phiSideValue[i] += phi[sh] * vShape[sh];
}
}
for (size_t ip=0;ip<nip;ip++){
const typename DimCRFVGeometry<dim>::SCVF& scvf = geo.scvf(ip);
number phiFrom = phiSideValue[scvf.from()];
number phiTo = phiSideValue[scvf.to()];
// UG_LOG("ip=" << ip << " inside=" << vValueInside[ip] << " outside=" << vValueOutside[ip] << "\n");
if (phiFrom==0){
if (phiTo<=0){
vValue[ip] = vValueInside[ip];
} else {
vValue[ip] = vValueOutside[ip];
}
} else {
number theta;
if (phiFrom<0){
if (phiTo<=0){
vValue[ip] = vValueInside[ip];
} else {
theta = phiFrom/(phiFrom-phiTo);
vValue[ip] = vValueInside[ip]*vValueOutside[ip]/(theta*vValueInside[ip]+(1-theta)*vValueOutside[ip]);
// UG_LOG("theta=" << theta << " vValue=" << vValue[ip] << "\n");
}
} else {
if (phiTo>=0){
vValue[ip] = vValueOutside[ip];
} else {
theta = phiFrom/(phiFrom-phiTo);
vValue[ip] = vValueOutside[ip]*vValueInside[ip]/(theta*vValueOutside[ip]+(1-theta)*vValueInside[ip]);
// UG_LOG("theta=" << theta << " vValue=" << vValue[ip] << "\n");
}
}
}
}
}; // ip-cr-average
}; // evaluate
void update(){}
private:
static const size_t max_number_of_ips = 20;
};
template <typename TGridFunction>
class LevelSetUserVectorData
: public LevelSetUserDataBase<MathVector<TGridFunction::dim>, TGridFunction::dim,
LevelSetUserVectorData<TGridFunction> >, virtual public INewtonUpdate
{
/// domain type
typedef typename TGridFunction::domain_type domain_type;
/// algebra type
typedef typename TGridFunction::algebra_type algebra_type;
/// position accessor type
typedef typename domain_type::position_accessor_type position_accessor_type;
/// world dimension
static const int dim = domain_type::dim;
/// grid type
typedef typename domain_type::grid_type grid_type;
/// element type
typedef typename TGridFunction::template dim_traits<dim>::grid_base_object elem_type;
enum eval_type {sharp,cr_ip_average};
public:
// set evaluation type, implemented so far:
// 0 sharp (compute lsf in ip and give out inside value if inside or outside value if outside)
// 1 cr_ip_average (compute averaged value in ip, if in CR-FV-Geometry from-value in ip and to-value in ip have different signs,
// harmonic average is computed using intersection position from from and to node which is computed from level set function,
// the ips given in evaluate must be the CR-FV ips)
void set_eval_type(int type){
if (type==0) m_eval_type=sharp;
if (type==1) m_eval_type=cr_ip_average;
}
eval_type m_eval_type;
private:
// level set grid function
SmartPtr<TGridFunction> m_phi;
// approximation space for level and surface grid
SmartPtr<ApproximationSpace<domain_type> > m_spApproxSpace;
// grid
grid_type* m_grid;
/* // component of function
size_t m_fct;
// local finite element id
LFEID m_lfeID; */
public:
void set_inside_data(SmartPtr<CplUserData<MathVector<dim>, dim> > data)
{
m_imInsideData = data;
}
void set_inside_data(number f_x)
{
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
for (int i=0;i<dim;i++){
f->set_entry(i, f_x);
}
set_inside_data(f);
}
void set_inside_data(number f_x, number f_y)
{
if (dim!=2){
UG_THROW("NavierStokes: Setting source vector of dimension 2"
" to a Discretization for world dim " << dim);
} else {
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
f->set_entry(0, f_x);
f->set_entry(1, f_y);
set_inside_data(f);
}
}
void set_inside_data(number f_x, number f_y, number f_z)
{
if (dim<3){
UG_THROW("NavierStokes: Setting source vector of dimension 3"
" to a Discretization for world dim " << dim);
}
else
{
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
f->set_entry(0, f_x);
f->set_entry(1, f_y);
f->set_entry(2, f_z);
set_inside_data(f);
}
}
#ifdef UG_FOR_LUA
void set_inside_data(const char* fctName)
{
set_inside_data(LuaUserDataFactory<MathVector<dim>, dim>::create(fctName));
}
#endif
void set_outside_data(SmartPtr<CplUserData<MathVector<dim>, dim> > data)
{
m_imOutsideData = data;
}
void set_outside_data(number f_x)
{
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
for (int i=0;i<dim;i++){
f->set_entry(i, f_x);
}
set_outside_data(f);
}
void set_outside_data(number f_x, number f_y)
{
if (dim!=2){
UG_THROW("NavierStokes: Setting source vector of dimension 2"
" to a Discretization for world dim " << dim);
} else {
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
f->set_entry(0, f_x);
f->set_entry(1, f_y);
set_outside_data(f);
}
}
void set_outside_data(number f_x, number f_y, number f_z)
{
if (dim<3){
UG_THROW("NavierStokes: Setting source vector of dimension 3"
" to a Discretization for world dim " << dim);
}
else
{
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
f->set_entry(0, f_x);
f->set_entry(1, f_y);
f->set_entry(2, f_z);
set_outside_data(f);
}
}
#ifdef UG_FOR_LUA
void set_outside_data(const char* fctName)
{
set_outside_data(LuaUserDataFactory<MathVector<dim>, dim>::create(fctName));
}
#endif
private:
/// Data import for inside and outside data
SmartPtr<CplUserData<MathVector<dim>,dim> > m_imInsideData;
SmartPtr<CplUserData<MathVector<dim>,dim> > m_imOutsideData;
public:
/// constructor
LevelSetUserVectorData(SmartPtr<ApproximationSpace<domain_type> > approxSpace,SmartPtr<TGridFunction> spGridFct){
m_phi = spGridFct;
domain_type& domain = *m_phi->domain().get();
grid_type& grid = *domain.grid();
m_grid = &grid;
m_spApproxSpace = approxSpace;
m_eval_type = cr_ip_average;
}
virtual ~LevelSetUserVectorData(){};
template <int refDim>
inline void evaluate(MathVector<dim> vValue[],
const MathVector<dim> vGlobIP[],
number time, int si,
GridObject* elem,
const MathVector<dim> vCornerCoords[],
const MathVector<refDim> vLocIP[],
const size_t nip,
LocalVector* u,
const MathMatrix<refDim, dim>* vJT = NULL) const
{
UG_ASSERT(dynamic_cast<elem_type*>(elem) != NULL, "Unsupported element type");
elem_type* element = static_cast<elem_type*>(elem);
const size_t numVertices = element->num_vertices();
std::vector<Vertex*> childVertex(numVertices);
std::vector<number> phi(numVertices);
for (size_t i=0;i<nip;i++){
// UG_LOG("co(" << i << ",:)=" << vGlobIP[i] << "\n");
}
// find child vertices by injection
for(size_t i = 0; i < numVertices; ++i){
childVertex[i] = element->vertex(i);
};
// find out level, then get child vertices on finest level
size_t numChildren = m_grid->template num_children<elem_type>(element);
if (numChildren!=0){
size_t lowerLevel=0;
elem_type* childElem = m_grid->template get_child<elem_type>(element,0);
numChildren = m_grid->template num_children<elem_type>(childElem);
lowerLevel++;
while (numChildren>0){
childElem = m_grid->template get_child<elem_type>(childElem,0);
numChildren = m_grid->template num_children<elem_type>(childElem);
lowerLevel++;
};
for (size_t i=0;i<nip;i++){
for (size_t j=0;j<lowerLevel;j++){
childVertex[i]=m_grid->template get_child<Vertex>(childVertex[i],0);
}
}
}
// create Multiindex
std::vector<DoFIndex> ind;
for (size_t i=0;i<numVertices;i++){
m_phi->dof_indices(childVertex[i], 0, ind);
phi[i]=DoFRef(*m_phi, ind[0]);
};
bool onls=false;
bool inside=false;
for (size_t i=0;i<numVertices;i++){
if (phi[i]==0){
continue;
};
if (phi[i]<0) inside=true;
for (size_t j=i+1;j<numVertices;j++){
if (phi[i]*phi[j]<0){
onls = true;
break;
};
}
}
if (onls==false){
if (inside==false){
(*m_imInsideData)(vValue,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
} else {
(*m_imOutsideData)(vValue,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
};
return;
};
MathVector<dim> vValueInside[max_number_of_ips];
MathVector<dim> vValueOutside[max_number_of_ips];
(*m_imInsideData)(vValueInside,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
(*m_imOutsideData)(vValueOutside,
vGlobIP,
time, si,
elem,
vCornerCoords,
vLocIP,
nip,
u,
vJT);
if (m_eval_type == sharp){
for(size_t ip = 0; ip < nip; ++ip)
{
// reference object id
ReferenceObjectID roid = elem->reference_object_id();
// memory for shapes
std::vector<number> vShape;
// compute lsf value in ip (from Lagrange-1 shape function) and set value according to position (inside or outside)
const LocalShapeFunctionSet<refDim>& rTrialSpace =
LocalFiniteElementProvider::get<refDim>(roid, LFEID(LFEID::LAGRANGE, dim, 1));
// evaluate shapes at ip
rTrialSpace.shapes(vShape, vLocIP[ip]);
// get multiindices of element
std::vector<DoFIndex> ind;
m_phi->dof_indices(elem, 0, ind);
// compute lsf at integration point
number phiValue = 0.0;
for(size_t sh = 0; sh < vShape.size(); ++sh)
{
phiValue += phi[sh] * vShape[sh];
}
if (phiValue<0) vValue[ip] = vValueInside[ip];
else vValue[ip] = vValueOutside[ip];
// UG_LOG("vValue(" << ip << ")=" << vValueOutside[ip] << "\n");
};
}
if (m_eval_type == cr_ip_average){
// reference object id
ReferenceObjectID roid = elem->reference_object_id();
// memory for shapes
std::vector<number> vShape;
// get domain of grid function
const domain_type& domain = *m_phi->domain().get();
// get position accessor
typedef typename domain_type::position_accessor_type position_accessor_type;
const position_accessor_type& posAcc = domain.position_accessor();
//position_accessor_type aaPos = m_phi->domain()->position_accessor();
// coord and vertex array
MathVector<dim> coCoord[domain_traits<dim>::MaxNumVerticesOfElem];
Vertex* vVrt[domain_traits<dim>::MaxNumVerticesOfElem];
DimCRFVGeometry<dim> geo;
for(size_t i = 0; i < numVertices; ++i){
vVrt[i] = element->vertex(i);
coCoord[i] = posAcc[vVrt[i]];
// UG_LOG("co_coord(" << i<< "+1,:)=" << coCoord[i] << "\n");
};
// evaluate finite volume geometry
geo.update(elem, &(coCoord[0]), domain.subset_handler().get());
std::vector<number> phiSideValue(geo.num_sh());
// compute interpolated lsf values in cr dofs
for (size_t i=0;i<geo.num_scv();i++){
// compute lsf value in ip (from Lagrange-1 shape function) and set value according to position (inside or outside)
const LocalShapeFunctionSet<dim>& rTrialSpace =
LocalFiniteElementProvider::get<dim>(roid, LFEID(LFEID::LAGRANGE, dim, 1));
// evaluate shapes at ip
rTrialSpace.shapes(vShape, geo.scv(i).local_ip());
// get multiindices of element
std::vector<DoFIndex> ind;
m_phi->dof_indices(elem, 0, ind);
// compute lsf at integration point
phiSideValue[i] = 0.0;
for(size_t sh = 0; sh < vShape.size(); ++sh)
{
phiSideValue[i] += phi[sh] * vShape[sh];
}
}
for (size_t ip=0;ip<nip;ip++){
const typename DimCRFVGeometry<dim>::SCVF& scvf = geo.scvf(ip);
number phiFrom = phiSideValue[scvf.from()];
number phiTo = phiSideValue[scvf.to()];
// UG_LOG("ip=" << ip << " inside=" << vValueInside[ip] << " outside=" << vValueOutside[ip] << "\n");
if (phiFrom==0){
if (phiTo<=0){
vValue[ip] = vValueInside[ip];
} else {
vValue[ip] = vValueOutside[ip];
}
} else {
number theta;
if (phiFrom<0){
if (phiTo<=0){
vValue[ip] = vValueInside[ip];
} else {
theta = phiFrom/(phiFrom-phiTo);
for (int d=0;d<dim;d++)
vValue[ip][d] = vValueInside[ip][d]*vValueOutside[ip][d]/(theta*vValueInside[ip][d]+(1-theta)*vValueOutside[ip][d]);
// UG_LOG("theta=" << theta << " vValue=" << vValue[ip] << "\n");
}
} else {
if (phiTo>=0){
vValue[ip] = vValueOutside[ip];
} else {
theta = phiFrom/(phiFrom-phiTo);
for (int d=0;d<dim;d++)
vValue[ip][d] = vValueOutside[ip][d]*vValueInside[ip][d]/(theta*vValueOutside[ip][d]+(1-theta)*vValueInside[ip][d]);
// UG_LOG("theta=" << theta << " vValue=" << vValue[ip] << "\n");
}
}
}
}
}; // ip-cr-average
}; // evaluate
void update(){}
private:
static const size_t max_number_of_ips = 20;
};
template<typename TElem,typename TGrid>
void collect_finest_level_children(std::vector<TElem*>& childElemVector,TElem* elem,TGrid* grid){
size_t numChildren = grid->template num_children<TElem>(elem);
if (numChildren==0){
childElemVector.push_back(elem);
} else {
for (size_t i=0;i<numChildren;i++){
TElem* childElem = grid->template get_child<TElem>(elem,i);
size_t nc=grid->template num_children<TElem>(childElem);
if (nc==0){
childElemVector.push_back(childElem);
} else {
collect_finest_level_children<TElem,TGrid>(childElemVector,childElem,grid);
};
};
}
};
template<typename TElem,typename TPos,int dim>
void computeElemBarycenter(MathVector<dim>& bary,TElem* elem,TPos posA){
bary = 0;
size_t noc=elem->num_vertices();
for (size_t i=0;i<noc;i++){
bary+=posA[elem->vertex(i)];
}
bary/=noc;
}
template <typename TGridFunction>
class CRTwoPhaseSource
: public LevelSetUserDataBase<MathVector<TGridFunction::dim>, TGridFunction::dim,
CRTwoPhaseSource<TGridFunction> >, virtual public INewtonUpdate
{
/// domain type
typedef typename TGridFunction::domain_type domain_type;
/// algebra type
typedef typename TGridFunction::algebra_type algebra_type;
/// position accessor type
typedef typename domain_type::position_accessor_type position_accessor_type;
/// world dimension
static const int dim = domain_type::dim;
/// grid type
typedef typename domain_type::grid_type grid_type;
/// element type
typedef typename TGridFunction::template dim_traits<dim>::grid_base_object elem_type;
private:
// level set grid function
SmartPtr<TGridFunction> m_phi;
// approximation space for level and surface grid
SmartPtr<ApproximationSpace<domain_type> > m_spApproxSpace;
// grid
grid_type* m_grid;
private:
// gravitational constant
number m_gravitational_constant;
// surface tension factor
number m_sigma;
/// Data import for source
SmartPtr<CplUserData<MathVector<dim>,dim> > m_imSource;
public:
void set_gravitation(number gravityconst){
m_gravitational_constant = gravityconst;
}
void set_sigma(number sigma){
m_sigma = sigma;
}
/////////// Source
void set_source(SmartPtr<CplUserData<MathVector<dim>, dim> > data)
{
m_imSource = data;
}
void set_source(number f_x)
{
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
for (int i=0;i<dim;i++){
f->set_entry(i, f_x);
}
set_source(f);
}
void set_source(number f_x, number f_y)
{
if (dim!=2){
UG_THROW("NavierStokes: Setting source vector of dimension 2"
" to a Discretization for world dim " << dim);
} else {
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
f->set_entry(0, f_x);
f->set_entry(1, f_y);
set_source(f);
}
}
void set_source(number f_x, number f_y, number f_z)
{
if (dim<3){
UG_THROW("NavierStokes: Setting source vector of dimension 3"
" to a Discretization for world dim " << dim);
}
else
{
SmartPtr<ConstUserVector<dim> > f(new ConstUserVector<dim>());
f->set_entry(0, f_x);
f->set_entry(1, f_y);
f->set_entry(2, f_z);
set_source(f);
}
}
#ifdef UG_FOR_LUA
void set_source(const char* fctName)
{
set_source(LuaUserDataFactory<MathVector<dim>, dim>::create(fctName));
}
#endif
public:
void set_density(SmartPtr<CplUserData<number, dim> > user){
m_imDensity = user;
}
void set_density(number val){
set_density(make_sp(new ConstUserNumber<dim>(val)));
}
#ifdef UG_FOR_LUA
void set_density(const char* fctName){
set_density(LuaUserDataFactory<number, dim>::create(fctName));