Chi-Tech
volmesher_predefunpart_addcells.cc
Go to the documentation of this file.
2#include "mesh/Cell/cell.h"
4
5//###################################################################
6/**Adds a cell to the grid from a light-weight cell.*/
10 uint64_t global_id,
11 uint64_t partition_id,
12 const std::vector<chi_mesh::Vector3>& vertices)
13{
14 auto cell = std::make_unique<chi_mesh::Cell>(raw_cell.type, raw_cell.sub_type);
15 cell->centroid_ = raw_cell.centroid;
16 cell->global_id_ = global_id;
17 cell->partition_id_ = partition_id;
18 cell->material_id_ = raw_cell.material_id;
19
20 cell->vertex_ids_ = raw_cell.vertex_ids;
21
22 size_t face_counter = 0;
23 for (auto& raw_face : raw_cell.faces)
24 {
25 chi_mesh::CellFace newFace;
26
27 newFace.has_neighbor_ = raw_face.has_neighbor;
28 newFace.neighbor_id_ = raw_face.neighbor;
29
30 newFace.vertex_ids_ = raw_face.vertex_ids;
31 auto vfc = chi_mesh::Vertex(0.0, 0.0, 0.0);
32 for (auto fvid : newFace.vertex_ids_)
33 vfc = vfc + vertices[fvid];
34 newFace.centroid_ = vfc / static_cast<double>(newFace.vertex_ids_.size());
35
36 if (cell->Type() == CellType::SLAB)
37 {
38 // A slab face is very easy. If it is the first face
39 // the normal is -khat. If it is the second face then
40 // it is +khat.
41 if (face_counter == 0)
42 newFace.normal_ = chi_mesh::Vector3(0.0, 0.0, -1.0);
43 else
44 newFace.normal_ = chi_mesh::Vector3(0.0, 0.0, 1.0);
45 }
46 else if (cell->Type() == CellType::POLYGON)
47 {
48 // A polygon face is just a line so we can just grab
49 // the first vertex and form a vector with the face
50 // centroid. The normal is then just khat
51 // cross-product with this vector.
52 uint64_t fvid = newFace.vertex_ids_[0];
53 auto vec_vvc = vertices[fvid] - newFace.centroid_;
54
55 newFace.normal_ = chi_mesh::Vector3(0.0, 0.0, 1.0).Cross(vec_vvc);
56 newFace.normal_.Normalize();
57 }
58 else if (cell->Type() == CellType::POLYHEDRON)
59 {
60 // A face of a polyhedron can itself be a polygon
61 // which can be multifaceted. Here we need the
62 // average normal over all the facets computed
63 // using an area-weighted average.
64 const size_t num_face_verts = newFace.vertex_ids_.size();
65 double total_area = 0.0;
66 for (size_t fv=0; fv<num_face_verts; ++fv)
67 {
68 size_t fvp1 = (fv < (num_face_verts-1))? fv+1 : 0;
69
70 uint64_t fvid_m = newFace.vertex_ids_[fv ];
71 uint64_t fvid_p = newFace.vertex_ids_[fvp1];
72
73 auto leg_m = vertices[fvid_m] - newFace.centroid_;
74 auto leg_p = vertices[fvid_p] - newFace.centroid_;
75
76 auto vn = leg_m.Cross(leg_p);
77
78 double area = 0.5*vn.Norm();
79 total_area += area;
80
81 newFace.normal_ = newFace.normal_ + area * vn.Normalized();
82 }
83 newFace.normal_ = newFace.normal_ / total_area;
84 newFace.normal_.Normalize();
85 }
86 ++face_counter;
87
88 cell->faces_.push_back(newFace);
89 }
90
91 return cell;
92}
Normal normal_
A list of the vertices.
Definition: cell.h:39
bool has_neighbor_
Flag indicating whether face has a neighbor.
Definition: cell.h:41
uint64_t neighbor_id_
Otherwise contains boundary_id.
Definition: cell.h:42
std::vector< uint64_t > vertex_ids_
Definition: cell.h:38
Vertex centroid_
The face centroid.
Definition: cell.h:40
static std::unique_ptr< chi_mesh::Cell > MakeCell(const chi_mesh::UnpartitionedMesh::LightWeightCell &raw_cell, uint64_t global_id, uint64_t partition_id, const std::vector< chi_mesh::Vector3 > &vertices)
VectorN< 3 > Vector3
Vector3 Vertex
Definition: chi_mesh.h:20
Vector3 Cross(const Vector3 &that) const
Vector3 Normalized() const
double Norm() const