-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshCache_impl.hh
844 lines (731 loc) · 25.4 KB
/
MeshCache_impl.hh
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
/*
Copyright 2010-201x held jointly by LANL, ORNL, LBNL, and PNNL.
Amanzi is released under the three-clause BSD License.
The terms of use and "as is" disclaimer for this license are
provided in the top-level COPYRIGHT file.
Authors: Ethan Coon ([email protected])
Julien Loiseau ([email protected])
Rao Garimella ([email protected])
*/
//! Caches mesh information for fast repeated access.
#pragma once
#include "MeshCache_decl.hh"
#include "KokkosUtils.hh"
#include "MeshFramework.hh"
#include "Mesh_HelpersDevice_decl.hh"
#include "span.hh"
namespace Amanzi {
namespace AmanziMesh {
template<typename T, typename Func>
RaggedArray_DualView<T>
asRaggedArray_DualView(Func mesh_func, Entity_ID count)
{
RaggedArray_DualView<T> adj;
adj.rows.resize(count+1);
// do a count first, setting rows
std::vector<T> ents;
int total = 0;
for (Entity_ID i=0; i!=count; ++i) {
view<MemSpace_type::HOST>(adj.rows)[i] = total;
mesh_func(i, ents);
total += ents.size();
}
view<MemSpace_type::HOST>(adj.rows)[count] = total;
adj.entries.resize(total);
for (Entity_ID i=0; i!=count; ++i) {
mesh_func(i, ents);
Kokkos::View<T*, Kokkos::DefaultHostExecutionSpace> row_view = adj.template getRow<MemSpace_type::HOST>(i);
my_deep_copy(row_view, ents);
}
adj.rows.template modify<typename RaggedArray_DualView<Entity_ID>::host_mirror_space>();
adj.rows.template sync<typename RaggedArray_DualView<Entity_ID>::execution_space>();
adj.entries.template modify<typename RaggedArray_DualView<Entity_ID>::host_mirror_space>();
adj.entries.template sync<typename RaggedArray_DualView<Entity_ID>::execution_space>();
return adj;
}
// -----------------------------------------------------------------------------
// Constructors
// -----------------------------------------------------------------------------
MeshCacheBase::MeshCacheBase()
: is_ordered_(false),
is_logical_(false),
has_edges_(false),
has_nodes_(true),
manifold_dim_(-1),
space_dim_(-1)
{}
template<MemSpace_type MEM>
template<MemSpace_type MEM_OTHER>
MeshCache<MEM>::MeshCache(const MeshCache<MEM_OTHER>& other)
: MeshCacheBase(other) {}
// -----------------------------------------------------------------------------
// Accessors / Mutators
// -----------------------------------------------------------------------------
template<MemSpace_type MEM>
void
MeshCache<MEM>::setMeshFramework(const std::shared_ptr<MeshFramework>& framework_mesh)
{
framework_mesh_ = framework_mesh;
// always save the algorithms, so we can throw away the data
algorithms_ = framework_mesh->getAlgorithms();
has_edges_ = framework_mesh->hasEdges();
has_nodes_ = framework_mesh->hasNodes();
// comm_ = framework_mesh_->getComm();
// gm_ = framework_mesh_->getGeometricModel();
space_dim_ = framework_mesh_->getSpaceDimension();
manifold_dim_ = framework_mesh_->getManifoldDimension();
is_logical_ = framework_mesh_->isLogical();
is_ordered_ = framework_mesh_->isOrdered();
has_edges_ = framework_mesh_->hasEdges();
// bool natural_ordered_maps = plist_->get<bool>("natural map ordering", false);
// maps_.initialize(*framework_mesh_, natural_ordered_maps);
ncells_owned = framework_mesh_->getNumEntities(Entity_kind::CELL, Parallel_type::OWNED);
ncells_all = framework_mesh_->getNumEntities(Entity_kind::CELL, Parallel_type::ALL);
nfaces_owned = framework_mesh_->getNumEntities(Entity_kind::FACE, Parallel_type::OWNED);
nfaces_all = framework_mesh_->getNumEntities(Entity_kind::FACE, Parallel_type::ALL);
nboundary_faces_owned = framework_mesh_->getNumEntities(Entity_kind::BOUNDARY_FACE, Parallel_type::OWNED);
nboundary_faces_all = framework_mesh_->getNumEntities(Entity_kind::BOUNDARY_FACE, Parallel_type::ALL);
if (hasEdges()) {
nedges_owned = framework_mesh_->getNumEntities(Entity_kind::EDGE, Parallel_type::OWNED);
nedges_all = framework_mesh_->getNumEntities(Entity_kind::EDGE, Parallel_type::ALL);
}
if (hasNodes()) {
nnodes_owned = framework_mesh_->getNumEntities(Entity_kind::NODE, Parallel_type::OWNED);
nnodes_all = framework_mesh_->getNumEntities(Entity_kind::NODE, Parallel_type::ALL);
nboundary_nodes_owned = framework_mesh_->getNumEntities(Entity_kind::BOUNDARY_NODE, Parallel_type::OWNED);
nboundary_nodes_all = framework_mesh_->getNumEntities(Entity_kind::BOUNDARY_NODE, Parallel_type::ALL);
}
}
template<MemSpace_type MEM>
Entity_ID
MeshCache<MEM>::getNumEntities(const Entity_kind kind, const Parallel_type ptype) const
{
Entity_ID nowned, nall;
switch(kind) {
case (Entity_kind::CELL) :
nowned = ncells_owned; nall = ncells_all;
break;
case (Entity_kind::FACE) :
nowned = nfaces_owned; nall = nfaces_all;
break;
case (Entity_kind::EDGE) :
nowned = nedges_owned; nall = nedges_all;
break;
case (Entity_kind::NODE) :
nowned = nnodes_owned; nall = nnodes_all;
break;
case (Entity_kind::BOUNDARY_FACE) :
nowned = nboundary_faces_owned; nall = nboundary_faces_all;
break;
case (Entity_kind::BOUNDARY_NODE) :
nowned = nboundary_nodes_owned; nall = nboundary_nodes_all;
break;
default :
nowned = -1; nall = -1;
}
switch(ptype) {
case (Parallel_type::OWNED) :
return nowned;
break;
case (Parallel_type::ALL) :
return nall;
break;
case Parallel_type::GHOST :
return nall - nowned;
break;
default :
return 0;
}
}
// common error messaging
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION void MeshCache<MEM>::throwAccessError_(const std::string& func_name) const
{
Errors::Message msg;
msg << "MeshCache<MEM>" << func_name << " cannot compute this quantity -- not cached and framework does not exist.";
Exceptions::amanzi_throw(msg);
}
// -----------------------------------------------------------------------------
// Topology
// -----------------------------------------------------------------------------
//---------------------
// Downward adjacencies
//---------------------
// Get faces of a cell
//
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
size_type
MeshCache<MEM>::getCellNumFaces(const Entity_ID c) const
{
static_assert(AP != AccessPattern::COMPUTE);
static_assert(AP != AccessPattern::FRAMEWORK);
// this is where a generic function would probably help?
if constexpr(AP == AccessPattern::CACHE) {
assert(data_.cell_faces_cached);
return data_.cell_faces.size<MEM>(c);
} else {
if (data_.cell_faces_cached) return getCellNumFaces<AccessPattern::CACHE>(c);
return getCellFaces(c).size();
}
}
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
decltype(auto)
MeshCache<MEM>::getCellFaces(const Entity_ID c) const
{
MeshCache<MEM>::List<const Entity_ID> cfaces;
getCellFaces<AP>(c, cfaces);
return cfaces;
}
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
void
MeshCache<MEM>::getCellFaces(const Entity_ID c,
MeshCache<MEM>::List<const Entity_ID>& cfaces) const
{
cfaces = RaggedGetter<MEM,AP>::get(data_.cell_faces_cached,
data_.cell_faces,
framework_mesh_,
[&](const int i) {
std::vector<Entity_ID> cf;
framework_mesh_->getCellFaces(i, cf);
return cf; },
nullptr,
nullptr,
c);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
const Entity_ID& MeshCache<MEM>::getCellFace(const Entity_ID c, const size_type i) const
{
assert(data_.cell_faces_cached);
return data_.cell_faces.get<MEM>(c,i);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
decltype(auto) // Kokkos::pair<cEntity_ID_View, cEntity_Direction_View>
MeshCache<MEM>::getCellFacesAndDirections(const Entity_ID c) const
{
List<const Entity_ID> cfaces;
List<const int> dirs;
getCellFacesAndDirs(c, cfaces, &dirs);
return Kokkos::pair(cfaces, dirs);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
void
MeshCache<MEM>::getCellFacesAndDirs(const Entity_ID c,
List<const Entity_ID>& faces,
List<const int> * const dirs) const
{
if constexpr(MEM == MemSpace_type::DEVICE) {
if (data_.cell_faces_cached) {
faces = data_.cell_faces.getRow<MEM>(c);
if (dirs) *dirs = data_.cell_face_directions.getRow<MEM>(c);
return;
}
} else {
if (data_.cell_faces_cached) {
faces = asVector(data_.cell_faces.getRow<MEM>(c));
if (dirs) *dirs = asVector(data_.cell_face_directions.getRow<MEM>(c));
return;
}
if (framework_mesh_.get()) {
framework_mesh_->getCellFacesAndDirs(c, faces, dirs);
return;
}
}
throwAccessError_("getCellFacesAndDirections");
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
decltype(auto) // Kokkos::pair<cEntity_ID_View, cPoint_View>
MeshCache<MEM>::getCellFacesAndBisectors(const Entity_ID c) const
{
List<const Entity_ID> cfaces;
List<const AmanziGeometry::Point> bisectors;
getCellFacesAndBisectors(c, cfaces, &bisectors);
return Kokkos::pair(cfaces, bisectors);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
void
MeshCache<MEM>::getCellFacesAndBisectors(
const Entity_ID c,
List<const Entity_ID>& faces,
List<const AmanziGeometry::Point> * const bisectors) const
{
if constexpr(MEM == MemSpace_type::DEVICE) {
if (data_.cell_faces_cached) {
faces = data_.cell_faces.getRow<MEM>(c);
if (bisectors) *bisectors = data_.cell_face_bisectors.getRow<MEM>(c);
return;
}
} else {
if (data_.cell_faces_cached) {
faces = asVector(data_.cell_faces.getRow<MEM>(c));
if (bisectors) *bisectors = asVector(data_.cell_face_bisectors.getRow<MEM>(c));
return;
}
if (framework_mesh_.get()) {
framework_mesh_->getCellFacesAndBisectors(c, faces, bisectors);
return;
}
}
throwAccessError_("getCellFacesAndDirections");
}
// cell-face adjacencies
template<MemSpace_type MEM>
void MeshCache<MEM>::cacheCellFaces()
{
if (data_.cell_faces_cached) return;
auto lambda1 = [this](Entity_ID c, Entity_ID_List& cfaces) { this->framework_mesh_->getCellFaces(c, cfaces); };
data_.cell_faces = asRaggedArray_DualView<Entity_ID>(lambda1, ncells_all);
auto lambda2 = [this](Entity_ID c, Entity_Direction_List& dirs) { this->framework_mesh_->getCellFaceDirs(c, dirs); };
data_.cell_face_directions = asRaggedArray_DualView<int>(lambda2, ncells_all);
data_.cell_faces_cached = true;
}
//-------------------
// Upward adjacencies
//-------------------
// The cells are returned in no particular order. Also, the order of cells
// is not guaranteed to be the same for corresponding faces on different
// processors
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
size_type
MeshCache<MEM>::getFaceNumCells(const Entity_ID f, const Parallel_type ptype) const
{
static_assert(AP != AccessPattern::COMPUTE);
static_assert(AP != AccessPattern::FRAMEWORK);
// this is where a generic function would probably help?
if constexpr(AP == AccessPattern::CACHE) {
assert(data_.face_cells_cached);
if (ptype == Parallel_type::ALL) {
return data_.face_cells.size<MEM>(f);
} else {
int count = 0;
int n_all = data_.face_cells.size<MEM>(f);
for (int j=0; j!=n_all; ++j) {
if (getFaceCell(f,j) < ncells_owned) ++count;
else break;
}
return count;
}
} else {
if (data_.face_cells_cached) return getFaceNumCells<AccessPattern::CACHE>(f);
return getFaceCells(f).size();
}
}
// template<MemSpace_type MEM>
// template<class Entity_ID_View_type>
// KOKKOS_INLINE_FUNCTION
// void
// MeshCache<MEM>::getFaceCells(const Entity_ID f, const Parallel_type ptype, Entity_ID_View_type fcells) const
// {
// if (data_.face_cells_cached) {
// fcells = data_.face_cells.getRow<MEM>(f);
// if constexpr(MEM == MemSpace_type::HOST) {
// if (framework_mesh_.get()) {
// Entity_ID_List fcells;
// framework_mesh_->getFaceCells(f, ptype, fcells);
// return fcells;
// }
// }
// throwAccessError_("getFaceCells");
// }
// }
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
decltype(auto) // cEntity_ID_View
MeshCache<MEM>::getFaceCells(const Entity_ID f, const Parallel_type ptype) const
{
List<const Entity_ID> fcells;
getFaceCells(f, ptype, fcells);
return fcells;
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
const Entity_ID&
MeshCache<MEM>::getFaceCell(const Entity_ID f, const size_type i) const
{
assert(data_.face_cells_cached);
return data_.face_cells.get<MEM>(f,i);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
void
MeshCache<MEM>::getFaceCells(const Entity_ID f,
const Parallel_type ptype,
List<const Entity_ID> & fcells) const
{
if constexpr(MEM == MemSpace_type::DEVICE) {
static_assert(std::is_const_v<typename List<const Entity_ID>::value_type>);
if (data_.face_cells_cached) {
fcells = data_.face_cells.getRow<MEM>(f);
return;
}
} else {
if (data_.face_cells_cached) {
fcells = asVector(data_.face_cells.getRow<MEM>(f));
return;
}
if (framework_mesh_.get()) {
framework_mesh_->getFaceCells(f, ptype, fcells);
return;
}
}
throwAccessError_("getFaceCells");
}
// cache
template<MemSpace_type MEM>
void MeshCache<MEM>::cacheFaceCells()
{
if (data_.face_cells_cached) return;
auto lambda = [this](Entity_ID f, Entity_ID_List& fcells) { this->framework_mesh_->getFaceCells(f, Parallel_type::OWNED, fcells); };
data_.face_cells = asRaggedArray_DualView<Entity_ID>(lambda, nfaces_all);
data_.face_cells_cached = true;
}
// -----------------------------------------------------------------------------
// Geometry
// -----------------------------------------------------------------------------
// Cell Geometry
// -----------------------------------------------------------------------------
// centroids
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
decltype(auto) MeshCache<MEM>::getCellCentroid(const Entity_ID c) const
{
return Getter<MEM,AP>::get(data_.cell_geometry_cached,
data_.cell_centroids,
framework_mesh_,
[&](const int i) { return framework_mesh_->getCellCentroid(i); },
[&](const int i) {
// We need this constexpr even if we split the function into a device and a host one (like in this example)
// We can then have just one function with the constexpr inside.
// The constexpr protects the inside of the function:
// - in one case (device) the getCellNodes need to have a Kokkos::view so the
// second function (Host) cannot be instanciated.
// This can also be fixed by using functors.
if constexpr(MEM == MemSpace_type::DEVICE){
constexpr auto nnodes = MeshCacheData::static_max_nnodes_;
Entity_ID v[nnodes];
Kokkos::View<Entity_ID*, Kokkos::DefaultExecutionSpace,
Kokkos::MemoryTraits<Kokkos::Unmanaged>> nodes (v, nnodes);
getCellNodes(c,nodes);
AmanziGeometry::Point res;
for(const auto& n: nodes){
// !!! res += getnode doesnt work, it should!
res = res + getNodeCoordinate(n);
}
return res/nodes.size();
}
}, // Algo on Device
[&](const int i){
if constexpr(MEM==MemSpace_type::HOST){
constexpr auto nnodes = MeshCacheData::static_max_nnodes_;
Entity_ID_List nodes(nnodes);
getCellNodes(c,nodes);
AmanziGeometry::Point res;
for(const auto& n: nodes){
// !!! res += getnode doesnt work, it should!
res = res + getNodeCoordinate(n);
}
return res/nodes.size();
}
}, // Algo on Host
c);
}
// extent
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
decltype(auto) MeshCache<MEM>::getCellVolume(const Entity_ID c) const
{
return Getter<MEM,AP>::get(data_.cell_geometry_cached,
data_.cell_volumes,
framework_mesh_,
[&](const int i) { return framework_mesh_->getCellVolume(i); },
nullptr,
nullptr,
c);
}
template<MemSpace_type MEM>
void MeshCache<MEM>::cacheCellGeometry()
{
assert(framework_mesh_.get());
if (data_.cell_geometry_cached) return;
data_.cell_volumes.resize(ncells_all);
data_.cell_centroids.resize(ncells_all);
for (Entity_ID i=0; i!=ncells_all; ++i) {
// note this must be on host
std::tie(view<MemSpace_type::HOST>(data_.cell_volumes)[i],
view<MemSpace_type::HOST>(data_.cell_centroids)[i]) =
framework_mesh_->computeCellGeometry(i);
}
data_.cell_volumes.template modify<typename Point_DualView::host_mirror_space>();
data_.cell_volumes.template sync<typename Point_DualView::execution_space>();
data_.cell_centroids.template modify<typename Point_DualView::host_mirror_space>();
data_.cell_centroids.template sync<typename Point_DualView::execution_space>();
data_.cell_geometry_cached = true;
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
decltype(auto)
MeshCache<MEM>::getCellNodes(const Entity_ID c) const{
List<Entity_ID> nodes;
getCellNodes(c,nodes);
return nodes;
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
Entity_ID
MeshCache<MEM>::getCellNode(const Entity_ID c, const size_type i) const{
// Compute list and use only one?
List<Entity_ID> nodes;
getCellNodes(c,nodes);
return nodes[i];
}
template<MemSpace_type MEM>
template<typename ViewType> // Can be normal, unmanaged or vector
KOKKOS_INLINE_FUNCTION
void
MeshCache<MEM>::getCellNodes(const Entity_ID c, ViewType& nodes) const{
if constexpr (MEM == MemSpace_type::DEVICE){
assert(data_.cell_faces_cached);
assert(data_.face_nodes_cached);
for(int i = 0 ; i < nodes.size(); ++i)
nodes[i] = -1;
auto faces = getCellFaces(c);
int i = 0;
for(int f = 0 ; f < faces.size() ; ++f){
auto fnodes = getFaceNodes(faces[f]);
for(int n = 0; n < fnodes.size(); ++n){
if(i >= nodes.size()){
printf("Increase shared memory size\n");
assert(false);
}
if(!is_present(fnodes[n],nodes)){
nodes[i++] = fnodes[n];
}
}
}
auto tmp = Kokkos::subview(nodes,Kokkos::make_pair(0,i));
nodes = tmp;
}else{
if constexpr(std::is_same_v<ViewType,span<typename ViewType::value_type>>){
auto v = MeshAlgorithms::computeCellNodes(*this,c);
nodes = ViewType{v.data(),v.size()};
} else
nodes = MeshAlgorithms::computeCellNodes(*this,c);
}
}
// -----------------------------------------------------------------------------
// Node Geometry
// -----------------------------------------------------------------------------
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
decltype(auto)
MeshCache<MEM>::getNodeCoordinate(const Entity_ID n) const{
return Getter<MEM,AP>::get(
data_.node_coordinates_cached,
data_.node_coordinates,
framework_mesh_,
[&](const int i) { return framework_mesh_->getNodeCoordinate(i); },
nullptr,
nullptr,
n);
}
template<MemSpace_type MEM>
void
MeshCache<MEM>::cacheNodeCoordinates(){
if (data_.node_coordinates_cached) return;
data_.node_coordinates.resize(nnodes_all);
auto hv = view<MemSpace_type::HOST>(data_.node_coordinates);
for(int i = 0 ; i < nnodes_all ; ++i){
hv[i] = this->framework_mesh_->getNodeCoordinate(i);
}
Kokkos::deep_copy(data_.node_coordinates.view_device(),data_.node_coordinates.view_host());
data_.node_coordinates_cached = true;
}
// -----------------------------------------------------------------------------
// Face Geometry
// -----------------------------------------------------------------------------
// face centroids
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
decltype(auto) MeshCache<MEM>::getFaceCentroid(const Entity_ID f) const
{
return Getter<MEM,AP>::get(data_.face_geometry_cached,
data_.face_centroids,
framework_mesh_,
[&](const int i) { return framework_mesh_->getFaceCentroid(i); },
[&](const int i) { return MeshAlgorithms::getFaceCentroid(*this,i);},
[&](const int i) { return MeshAlgorithms::getFaceCentroid(*this,i);},
f);
}
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
double MeshCache<MEM>::getFaceArea(const Entity_ID f) const
{
return Getter<MEM,AP>::get(data_.face_geometry_cached,
data_.face_areas,
framework_mesh_,
[&](const int i) { return framework_mesh_->getFaceArea(i); },
nullptr,
nullptr,
f);
}
// Normal vector of a face
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
AmanziGeometry::Point MeshCache<MEM>::getFaceNormal(const Entity_ID f) const
{
return getFaceNormal<AP>(f, -1, nullptr);
}
template<MemSpace_type MEM>
template<AccessPattern AP>
KOKKOS_INLINE_FUNCTION
AmanziGeometry::Point MeshCache<MEM>::getFaceNormal(const Entity_ID f, const Entity_ID c,
int* orientation) const
{
AmanziGeometry::Point normal;
if constexpr (MEM == MemSpace_type::DEVICE){
assert(data_.face_geometry_cached);
}else {
if(!data_.face_geometry_cached)
if (framework_mesh_.get())
return framework_mesh_->getFaceNormal(f, c, orientation);
}
auto fcells = getFaceCells(f, Parallel_type::ALL);
if (orientation) *orientation = 0;
Entity_ID cc;
std::size_t i;
if (c < 0) {
cc = fcells[0];
i = 0;
} else {
cc = c;
auto ncells = fcells.size();
for (i=0; i!=ncells; ++i)
if (fcells[i] == cc) break;
}
normal = data_.face_normals.get<MEM>(f,i);
if (getSpaceDimension() == getManifoldDimension()) {
if (c < 0) {
normal *= MeshAlgorithms::getFaceDirectionInCell(*this, f, cc);
} else if (orientation) {
*orientation = MeshAlgorithms::getFaceDirectionInCell(*this, f, cc);
}
} else if (c < 0) {
Errors::Message msg("MeshFramework: asking for the natural normal of a submanifold mesh is not valid.");
Exceptions::amanzi_throw(msg);
}
return normal;
}
template<MemSpace_type MEM>
void MeshCache<MEM>::cacheFaceGeometry()
{
assert(framework_mesh_.get());
if (data_.face_geometry_cached) return;
data_.face_areas.resize(nfaces_all);
data_.face_centroids.resize(nfaces_all);
// slurp down the RaggedArray for normals using a lambda that, as a side
// effect, captures area and centroid too.
auto area_view = view<MemSpace_type::HOST>(data_.face_areas);
auto centroid_view = view<MemSpace_type::HOST>(data_.face_centroids);
auto lambda = [&,this](const Entity_ID& f, Point_List& normals) {
auto area_cent_normal = this->framework_mesh_->computeFaceGeometry(f);
area_view[f] = std::get<0>(area_cent_normal);
centroid_view[f] = std::get<1>(area_cent_normal);
normals = std::get<2>(area_cent_normal);
};
data_.face_normals = asRaggedArray_DualView<AmanziGeometry::Point>(lambda, nfaces_all);
// still must sync areas/centroids
data_.face_areas.template modify<typename Double_DualView::host_mirror_space>();
data_.face_areas.template sync<typename Double_DualView::execution_space>();
data_.face_centroids.template modify<typename Point_DualView::host_mirror_space>();
data_.face_centroids.template sync<typename Point_DualView::execution_space>();
data_.face_geometry_cached = true;
// cache normal directions -- make this a separate call? Think about
// granularity here.
auto lambda2 = [&,this](const Entity_ID& f, Entity_Direction_List& dirs) {
// This NEEDS to call the framework or be passed an host mesh to call the function on the host.
Entity_ID_List fcells;
framework_mesh_->getFaceCells(f, Parallel_type::ALL, fcells);
dirs.resize(fcells.size());
for (int i=0; i!=fcells.size(); ++i) {
this->framework_mesh_->getFaceNormal(f, fcells[i], &dirs[i]);
}
};
data_.face_normal_directions = asRaggedArray_DualView<int>(lambda2, nfaces_all);
// cache cell-face-bisectors -- make this a separate call? Think about
// granularity here.
auto lambda3 = [&,this](const Entity_ID& c, Point_List& bisectors) {
Entity_ID_List cfaces;
this->framework_mesh_->getCellFacesAndBisectors(c, cfaces, &bisectors);
};
data_.cell_face_bisectors = asRaggedArray_DualView<AmanziGeometry::Point>(lambda3, ncells_all);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
decltype(auto) // cEntity_ID_View
MeshCache<MEM>::getFaceNodes(const Entity_ID f) const
{
List<const Entity_ID> fcells;
getFaceNodes(f, fcells);
return fcells;
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
const Entity_ID&
MeshCache<MEM>::getFaceNode(const Entity_ID f, const size_type i) const
{
assert(data_.face_nodes_cached);
return data_.face_nodes.get<MEM>(f,i);
}
template<MemSpace_type MEM>
KOKKOS_INLINE_FUNCTION
void
MeshCache<MEM>::getFaceNodes(const Entity_ID f,
List<const Entity_ID> & fcells) const
{
if constexpr(MEM == MemSpace_type::DEVICE) {
static_assert(std::is_const_v<typename List<const Entity_ID>::value_type>);
if (data_.face_nodes_cached) {
fcells = data_.face_nodes.getRow<MEM>(f);
return;
}
} else {
if (data_.face_nodes_cached) {
fcells = asVector(data_.face_nodes.getRow<MEM>(f));
return;
}
if (framework_mesh_.get()) {
framework_mesh_->getFaceNodes(f, fcells);
return;
}
}
throwAccessError_("getFaceNodes");
}
// cache
template<MemSpace_type MEM>
void MeshCache<MEM>::cacheFaceNodes()
{
if (data_.face_nodes_cached) return;
auto lambda = [this](Entity_ID f, Entity_ID_List& fnodes) { this->framework_mesh_->getFaceNodes(f, fnodes); };
data_.face_nodes = asRaggedArray_DualView<Entity_ID>(lambda, nfaces_all);
data_.face_nodes_cached = true;
}
} // namespace AmanziMesh
} // namespace Amanzi