36#include "VertexMesh.hpp"
37#include "MutableMesh.hpp"
38#include "RandomNumberGenerator.hpp"
41#include "VtkMeshWriter.hpp"
42#include "NodesOnlyMesh.hpp"
44template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
47 : mpDelaunayMesh(nullptr)
54 for (
unsigned node_index = 0; node_index < nodes.size(); node_index++)
57 this->
mNodes.push_back(p_temp_node);
59 for (
unsigned elem_index = 0; elem_index < vertexElements.size(); elem_index++)
62 mElements.push_back(p_temp_vertex_element);
69 std::set<unsigned> faces_counted;
72 for (
unsigned elem_index = 0; elem_index <
mElements.size(); elem_index++)
75 for (
unsigned face_index = 0; face_index <
mElements[elem_index]->GetNumFaces(); face_index++)
78 unsigned global_index = p_face->
GetIndex();
81 if (faces_counted.find(global_index) == faces_counted.end())
84 faces_counted.insert(global_index);
91 for (
unsigned index = 0; index <
mElements.size(); index++)
95 unsigned element_index = p_element->
GetIndex();
96 unsigned num_nodes_in_element = p_element->
GetNumNodes();
98 for (
unsigned node_index = 0; node_index < num_nodes_in_element; node_index++)
100 p_element->
GetNode(node_index)->AddElement(element_index);
109template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
113 : mpDelaunayMesh(nullptr)
119 for (
unsigned node_index = 0; node_index < nodes.size(); node_index++)
122 this->
mNodes.push_back(p_temp_node);
125 for (
unsigned face_index = 0; face_index < faces.size(); face_index++)
127 VertexElement<ELEMENT_DIM - 1, SPACE_DIM>* p_temp_face = faces[face_index];
128 mFaces.push_back(p_temp_face);
131 for (
unsigned elem_index = 0; elem_index < vertexElements.size(); elem_index++)
134 mElements.push_back(p_temp_vertex_element);
138 for (
unsigned index = 0; index <
mElements.size(); index++)
141 for (
unsigned node_index = 0; node_index < p_temp_vertex_element->
GetNumNodes(); node_index++)
159 bool scaleBoundByEdgeLength,
160 double maxDelaunayEdgeLength,
161 bool offsetNewBoundaryNodes)
162 : mpDelaunayMesh(&rMesh)
175 this->
mNodes.reserve(num_nodes);
179 for (
unsigned elem_index = 0; elem_index < num_elements; elem_index++)
189 for (
unsigned i = 0; i < num_nodes; i++)
193 for (
unsigned local_index = 0; local_index < 3; local_index++)
195 unsigned elem_index =
mpDelaunayMesh->GetElement(i)->GetNodeGlobalIndex(local_index);
196 unsigned num_nodes_in_elem =
mElements[elem_index]->GetNumNodes();
197 unsigned end_index = num_nodes_in_elem > 0 ? num_nodes_in_elem - 1 : 0;
206 std::vector<Node<2> *> nodes;
211 nodes.push_back(
new Node<2>(node_iter->GetIndex(), node_iter->rGetLocation(),node_iter->IsBoundaryNode()));
222 bool bad_element =
false;
224 for (
unsigned j=0; j<3; j++)
235 for (
unsigned j=0; j<3; j++)
243 std::set<unsigned> shared_elements;
244 std::set_intersection(node_a_element_indices.begin(),
245 node_a_element_indices.end(),
246 node_b_element_indices.begin(),
247 node_b_element_indices.end(),
248 std::inserter(shared_elements, shared_elements.begin()));
251 double edge_length = norm_2(edge);
258 bool is_boundary_edge =
false;
259 double direction_of_normal = 1.0;
260 if ((edge_length < maxDelaunayEdgeLength) && (shared_elements.size() == 1))
262 is_boundary_edge =
true;
264 if (bad_element && (edge_length < maxDelaunayEdgeLength))
270 assert(!is_boundary_edge);
271 is_boundary_edge =
true;
273 direction_of_normal = -1.0;
276 if (is_boundary_edge)
278 c_vector<double,2> normal_vector;
280 normal_vector[0]= edge[1];
281 normal_vector[1]= -edge[0];
283 double dij = norm_2(normal_vector);
285 normal_vector /= dij;
287 double new_node_distance = 1.0;
289 if (scaleBoundByEdgeLength)
291 new_node_distance = edge_length;
294 int num_sections = 1;
295 for (
int section=0; section<=num_sections; section++)
298 if (offsetNewBoundaryNodes)
309 c_vector<double,2> new_node_location = direction_of_normal * new_node_distance * normal_vector + ratio*p_node_a->
rGetLocation() + (1-ratio)*p_node_b->
rGetLocation();
312 double node_clearance = 0.01;
315 nodes.push_back(
new Node<2>(new_node_index, new_node_location));
332 unsigned num_nodes = extended_mesh.GetNumAllElements();
339 for (
unsigned elem_index = 0; elem_index < num_elements; elem_index++)
349 for (
unsigned i = 0; i < num_nodes; i++)
353 for (
unsigned local_index = 0; local_index < 3; local_index++)
355 unsigned elem_index = extended_mesh.GetElement(i)->GetNodeGlobalIndex(local_index);
357 if (elem_index < num_elements)
359 unsigned num_nodes_in_elem =
mElements[elem_index]->GetNumNodes();
360 unsigned end_index = num_nodes_in_elem > 0 ? num_nodes_in_elem - 1 : 0;
369 for (
unsigned elem_index = 0; elem_index <
mElements.size(); elem_index++)
376 std::vector<std::pair<double, unsigned> > index_angle_list;
377 for (
unsigned local_index = 0; local_index <
mElements[elem_index]->GetNumNodes(); local_index++)
379 c_vector<double, 2> vectorA =
mpDelaunayMesh->GetNode(elem_index)->rGetLocation();
380 c_vector<double, 2> vectorB =
mElements[elem_index]->GetNodeLocation(local_index);
381 c_vector<double, 2> centre_to_vertex =
mpDelaunayMesh->GetVectorFromAtoB(vectorA, vectorB);
383 double angle = atan2(centre_to_vertex(1), centre_to_vertex(0));
384 unsigned global_index =
mElements[elem_index]->GetNodeGlobalIndex(local_index);
386 std::pair<double, unsigned> pair(angle, global_index);
387 index_angle_list.push_back(pair);
391 sort(index_angle_list.begin(), index_angle_list.end());
395 for (
unsigned count = 0; count < index_angle_list.size(); count++)
397 unsigned local_index = count > 1 ? count - 1 : 0;
398 p_new_element->
AddNode(
mNodes[index_angle_list[count].second], local_index);
420 : mpDelaunayMesh(&rMesh)
428 this->
mNodes.reserve(num_nodes);
433 std::map<unsigned, VertexElement<3, 3>*> index_element_map;
434 unsigned face_index = 0;
435 unsigned element_index = 0;
442 Node<3>* p_node_a = edge_iterator.GetNodeA();
443 Node<3>* p_node_b = edge_iterator.GetNodeB();
449 std::set<unsigned> edge_element_indices;
451 std::set_intersection(node_a_element_indices.begin(),
452 node_a_element_indices.end(),
453 node_b_element_indices.begin(),
454 node_b_element_indices.end(),
455 std::inserter(edge_element_indices, edge_element_indices.begin()));
457 c_vector<double, 3> edge_vector;
460 c_vector<double, 3> mid_edge;
461 mid_edge = edge_vector * 0.5 + p_node_a->
rGetLocation();
463 unsigned element0_index = *(edge_element_indices.begin());
465 c_vector<double, 3> basis_vector1;
466 basis_vector1 =
mNodes[element0_index]->rGetLocation() - mid_edge;
468 c_vector<double, 3> basis_vector2;
476 std::vector<std::pair<double, unsigned> > index_angle_list;
479 for (std::set<unsigned>::iterator index_iter = edge_element_indices.begin();
480 index_iter != edge_element_indices.end();
484 c_vector<double, 3> vertex_vector =
mNodes[*index_iter]->rGetLocation() - mid_edge;
486 double local_vertex_dot_basis_vector1 = inner_prod(vertex_vector, basis_vector1);
487 double local_vertex_dot_basis_vector2 = inner_prod(vertex_vector, basis_vector2);
489 double angle = atan2(local_vertex_dot_basis_vector2, local_vertex_dot_basis_vector1);
491 std::pair<double, unsigned> pair(angle, *index_iter);
492 index_angle_list.push_back(pair);
496 sort(index_angle_list.begin(), index_angle_list.end());
501 for (
unsigned count = 0; count < index_angle_list.size(); count++)
503 unsigned local_index = count > 1 ? count - 1 : 0;
504 p_face->
AddNode(
mNodes[index_angle_list[count].second], local_index);
513 unsigned node_a_index = p_node_a->
GetIndex();
514 if (index_element_map[node_a_index])
517 index_element_map[node_a_index]->AddFace(p_face);
526 index_element_map[node_a_index] = p_element;
531 unsigned node_b_index = p_node_b->
GetIndex();
532 if (index_element_map[node_b_index])
535 index_element_map[node_b_index]->AddFace(p_face);
544 index_element_map[node_b_index] = p_element;
551 unsigned elem_count = 0;
552 for (std::map<
unsigned,
VertexElement<3, 3>*>::iterator element_iter = index_element_map.begin();
553 element_iter != index_element_map.end();
556 mElements.push_back(element_iter->second);
568template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
573 for (
auto elem : rElements)
575 elem->SetEdgeHelper(&mEdgeHelper);
580template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
583 return mEdgeHelper.GetNumEdges();
586template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
589 return mEdgeHelper.GetEdge(index);
592template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
598template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
601 c_matrix<double, SPACE_DIM, ELEMENT_DIM> jacobian;
602 c_matrix<double, ELEMENT_DIM, SPACE_DIM> inverse_jacobian;
610 c_vector<double, SPACE_DIM + 1> circumsphere = rMesh.
GetElement(i)->CalculateCircumsphere(jacobian, inverse_jacobian);
612 c_vector<double, SPACE_DIM> circumcentre;
613 for (
unsigned j = 0; j < SPACE_DIM; j++)
615 circumcentre(j) = circumsphere(j);
623template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
626 assert(SPACE_DIM == 2);
628 std::set<unsigned> node_indices_1;
629 for (
unsigned i = 0; i < mElements[elementIndex1]->GetNumNodes(); i++)
631 node_indices_1.insert(mElements[elementIndex1]->GetNodeGlobalIndex(i));
633 std::set<unsigned> node_indices_2;
634 for (
unsigned i = 0; i < mElements[elementIndex2]->GetNumNodes(); i++)
636 node_indices_2.insert(mElements[elementIndex2]->GetNodeGlobalIndex(i));
639 std::set<unsigned> shared_nodes;
640 std::set_intersection(node_indices_1.begin(), node_indices_1.end(),
641 node_indices_2.begin(), node_indices_2.end(),
642 std::inserter(shared_nodes, shared_nodes.begin()));
644 if (shared_nodes.size() == 1)
647 EXCEPTION(
"Elements " << elementIndex1 <<
" and " << elementIndex2 <<
" share only one node.");
649 assert(shared_nodes.size() == 2);
651 unsigned index1 = *(shared_nodes.begin());
652 unsigned index2 = *(++(shared_nodes.begin()));
654 double edge_length = this->GetDistanceBetweenNodes(index1, index2);
658template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
661 assert(SPACE_DIM == 2);
663 c_vector<double, 3> moments = CalculateMomentsOfElement(index);
665 double discriminant = sqrt((moments(0) - moments(1)) * (moments(0) - moments(1)) + 4.0 * moments(2) * moments(2));
668 double largest_eigenvalue = (moments(0) + moments(1) + discriminant) * 0.5;
669 double smallest_eigenvalue = (moments(0) + moments(1) - discriminant) * 0.5;
671 double elongation_shape_factor = sqrt(largest_eigenvalue / smallest_eigenvalue);
672 return elongation_shape_factor;
675template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
678 mpDelaunayMesh =
nullptr;
679 this->mMeshChangesDuringSimulation =
false;
683template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
689template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
692 assert(index < this->mNodes.size());
696template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
699 assert(index < this->mElements.size());
703template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
711template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
716 if (mVoronoiElementIndexMap.empty())
718 node_index = elementIndex;
722 for (std::map<unsigned, unsigned>::iterator iter = mVoronoiElementIndexMap.begin();
723 iter != mVoronoiElementIndexMap.end();
726 if (iter->second == elementIndex)
728 node_index = iter->first;
737template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
742 if (mVoronoiElementIndexMap.empty())
744 element_index = nodeIndex;
748 std::map<unsigned, unsigned>::iterator iter = mVoronoiElementIndexMap.find(nodeIndex);
750 if (iter == mVoronoiElementIndexMap.end())
752 EXCEPTION(
"This index does not correspond to a VertexElement");
756 element_index = iter->second;
760 return element_index;
763template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
766 assert(SPACE_DIM == 2 || SPACE_DIM == 3);
772 unsigned rosette_rank = 0;
773 for (
unsigned node_idx = 0; node_idx < p_element->
GetNumNodes(); node_idx++)
775 unsigned num_elems_this_node = p_element->
GetNode(node_idx)->rGetContainingElementIndices().size();
777 if (num_elems_this_node > rosette_rank)
779 rosette_rank = num_elems_this_node;
787template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
791 for (
unsigned i = 0; i < mElements.size(); i++)
798 for (
unsigned i = 0; i < mFaces.size(); i++)
806 for (
unsigned i = 0; i < this->mNodes.size(); i++)
808 delete this->mNodes[i];
810 this->mNodes.clear();
813template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
816 return this->mNodes.size();
819template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
822 return mElements.size();
825template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
828 return mElements.size();
831template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
834 return mFaces.size();
837template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
840 assert(index < mElements.size());
841 return mElements[index];
844template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
847 assert(index < mFaces.size());
848 return mFaces[index];
851template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
857 c_vector<double, SPACE_DIM> centroid = zero_vector<double>(SPACE_DIM);
868 double centroid_x = 0;
869 double centroid_y = 0;
872 double element_signed_area = 0.0;
875 c_vector<double, SPACE_DIM> first_node_location = p_element->
GetNodeLocation(0);
876 c_vector<double, SPACE_DIM> pos_1 = zero_vector<double>(SPACE_DIM);
879 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
881 c_vector<double, SPACE_DIM> next_node_location = p_element->
GetNodeLocation((local_index + 1) % num_nodes);
882 c_vector<double, SPACE_DIM> pos_2 = GetVectorFromAtoB(first_node_location, next_node_location);
884 double this_x = pos_1[0];
885 double this_y = pos_1[1];
886 double next_x = pos_2[0];
887 double next_y = pos_2[1];
889 double signed_area_term = this_x * next_y - this_y * next_x;
891 centroid_x += (this_x + next_x) * signed_area_term;
892 centroid_y += (this_y + next_y) * signed_area_term;
893 element_signed_area += 0.5 * signed_area_term;
898 assert(element_signed_area != 0.0);
901 centroid = first_node_location;
902 centroid(0) += centroid_x / (6.0 * element_signed_area);
903 centroid(1) += centroid_y / (6.0 * element_signed_area);
909 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
913 centroid /= ((
double)num_nodes);
922template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
926 std::set<unsigned> neighbouring_node_indices;
929 std::set<unsigned> containing_elem_indices = this->GetNode(nodeIndex)->rGetContainingElementIndices();
932 for (std::set<unsigned>::iterator elem_iter = containing_elem_indices.begin();
933 elem_iter != containing_elem_indices.end();
937 unsigned local_index = GetElement(*elem_iter)->GetNodeLocalIndex(nodeIndex);
940 unsigned num_nodes = GetElement(*elem_iter)->GetNumNodes();
941 unsigned previous_local_index = (local_index + num_nodes - 1) % num_nodes;
942 unsigned next_local_index = (local_index + 1) % num_nodes;
945 neighbouring_node_indices.insert(GetElement(*elem_iter)->GetNodeGlobalIndex(previous_local_index));
946 neighbouring_node_indices.insert(GetElement(*elem_iter)->GetNodeGlobalIndex(next_local_index));
949 return neighbouring_node_indices;
952template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
959 std::set<unsigned> node_indices_in_this_element;
960 for (
unsigned local_index = 0; local_index < p_element->
GetNumNodes(); local_index++)
963 node_indices_in_this_element.insert(global_index);
967 if (node_indices_in_this_element.find(nodeIndex) == node_indices_in_this_element.end())
969 EXCEPTION(
"The given node is not contained in the given element.");
973 std::set<unsigned> neighbouring_node_indices_not_in_this_element;
976 std::set<unsigned> node_neighbours = GetNeighbouringNodeIndices(nodeIndex);
979 for (std::set<unsigned>::iterator iter = node_neighbours.begin();
980 iter != node_neighbours.end();
983 if (node_indices_in_this_element.find(*iter) == node_indices_in_this_element.end())
985 neighbouring_node_indices_not_in_this_element.insert(*iter);
989 return neighbouring_node_indices_not_in_this_element;
992template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
999 std::set<unsigned> neighbouring_element_indices;
1002 for (
unsigned local_index = 0; local_index < p_element->
GetNumNodes(); local_index++)
1011 std::set<unsigned> all_elements;
1012 std::set_union(neighbouring_element_indices.begin(), neighbouring_element_indices.end(),
1013 containing_elem_indices.begin(), containing_elem_indices.end(),
1014 std::inserter(all_elements, all_elements.begin()));
1017 neighbouring_element_indices = all_elements;
1021 neighbouring_element_indices.erase(elementIndex);
1023 return neighbouring_element_indices;
1026template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1032template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1035 bool node_near =
false;
1037 for (
unsigned i=0; i<nodesToCheck.size(); i++)
1039 double distance = norm_2(mpDelaunayMesh->GetVectorFromAtoB(nodesToCheck[i]->rGetLocation(), newNodeLocation));
1040 if (distance < minClearance)
1055 EXCEPTION(
"VertexMesh<1,1>::ConstructFromMeshReader() is not implemented");
1063 EXCEPTION(
"VertexMesh<1,2>::ConstructFromMeshReader() is not implemented");
1071 EXCEPTION(
"VertexMesh<1,3>::ConstructFromMeshReader() is not implemented");
1079 EXCEPTION(
"VertexMesh<2,3>::ConstructFromMeshReader() is not implemented");
1093 this->mNodes.reserve(num_nodes);
1095 rMeshReader.
Reset();
1098 std::vector<double> node_data;
1099 for (
unsigned i = 0; i < num_nodes; i++)
1102 unsigned is_boundary_node = (
bool)node_data[2];
1103 node_data.pop_back();
1104 this->mNodes.push_back(
new Node<2>(i, node_data, is_boundary_node));
1107 rMeshReader.
Reset();
1113 for (
unsigned elem_index = 0; elem_index < num_elements; elem_index++)
1119 std::vector<Node<2>*> nodes;
1120 unsigned num_nodes_in_element = element_data.
NodeIndices.size();
1121 for (
unsigned j = 0; j < num_nodes_in_element; j++)
1123 assert(element_data.
NodeIndices[j] < this->mNodes.size());
1124 nodes.push_back(this->mNodes[element_data.
NodeIndices[j]]);
1129 mElements.push_back(p_element);
1138 GenerateEdgesFromElements(mElements);
1153 this->mNodes.reserve(num_nodes);
1155 rMeshReader.
Reset();
1158 std::vector<double> node_data;
1159 for (
unsigned i = 0; i < num_nodes; i++)
1162 unsigned is_boundary_node = (
bool)node_data[3];
1163 node_data.pop_back();
1164 this->mNodes.push_back(
new Node<3>(i, node_data, is_boundary_node));
1167 rMeshReader.
Reset();
1173 std::set<unsigned> faces_counted;
1176 for (
unsigned elem_index = 0; elem_index < num_elements; elem_index++)
1180 assert(
dynamic_cast<VERTEX_MESH_READER*
>(&rMeshReader) !=
nullptr);
1183 VertexElementData element_data =
static_cast<VERTEX_MESH_READER*
>(&rMeshReader)->GetNextElementDataWithFaces();
1186 std::vector<Node<3>*> nodes;
1187 unsigned num_nodes_in_element = element_data.
NodeIndices.size();
1188 for (
unsigned j = 0; j < num_nodes_in_element; j++)
1190 assert(element_data.
NodeIndices[j] < this->mNodes.size());
1191 nodes.push_back(this->mNodes[element_data.
NodeIndices[j]]);
1195 std::vector<VertexElement<2, 3>*> faces;
1196 unsigned num_faces_in_element = element_data.
Faces.size();
1197 for (
unsigned i = 0; i < num_faces_in_element; i++)
1206 std::vector<Node<3>*> nodes_in_face;
1207 unsigned num_nodes_in_face = face_data.NodeIndices.size();
1208 for (
unsigned j = 0; j < num_nodes_in_face; j++)
1210 assert(face_data.NodeIndices[j] < this->mNodes.size());
1211 nodes_in_face.push_back(this->mNodes[face_data.NodeIndices[j]]);
1215 if (faces_counted.find(face_index) == faces_counted.end())
1219 mFaces.push_back(p_face);
1220 faces_counted.insert(face_index);
1221 faces.push_back(p_face);
1226 bool face_added =
false;
1227 for (
unsigned k = 0; k < mFaces.size(); k++)
1229 if (mFaces[k]->GetIndex() == face_index)
1231 faces.push_back(mFaces[k]);
1237 assert(face_added ==
true);
1242 std::vector<bool> orientations = std::vector<bool>(num_faces_in_element,
true);
1246 mElements.push_back(p_element);
1257template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1259 const c_vector<double, SPACE_DIM>& rLocationA,
const c_vector<double, SPACE_DIM>& rLocationB)
1261 c_vector<double, SPACE_DIM> vector {};
1264 vector = mpDelaunayMesh->GetVectorFromAtoB(rLocationA, rLocationB);
1273template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1276 assert(SPACE_DIM == 2 || SPACE_DIM == 3);
1281 double element_volume = 0.0;
1285 c_vector<double, SPACE_DIM> first_node_location = p_element->
GetNodeLocation(0);
1286 c_vector<double, SPACE_DIM> pos_1 = zero_vector<double>(SPACE_DIM);
1289 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
1291 c_vector<double, SPACE_DIM> next_node_location = p_element->
GetNodeLocation((local_index + 1) % num_nodes);
1292 c_vector<double, SPACE_DIM> pos_2 = GetVectorFromAtoB(first_node_location, next_node_location);
1294 double this_x = pos_1[0];
1295 double this_y = pos_1[1];
1296 double next_x = pos_2[0];
1297 double next_y = pos_2[1];
1299 element_volume += 0.5 * (this_x * next_y - next_x * this_y);
1307 c_vector<double, SPACE_DIM> pyramid_apex = p_element->
GetNodeLocation(0);
1308 for (
unsigned face_index = 0; face_index < p_element->
GetNumFaces(); face_index++)
1314 c_vector<double, SPACE_DIM> unit_normal;
1315 double face_area = CalculateUnitNormalToFaceWithArea(p_face, unit_normal);
1318 c_vector<double, SPACE_DIM> base_to_apex = GetVectorFromAtoB(p_face->GetNodeLocation(0), pyramid_apex);
1319 double perpendicular_distance = fabs(inner_prod(base_to_apex, unit_normal));
1322 element_volume += face_area * perpendicular_distance / 3;
1327 return fabs(element_volume);
1330template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1333 assert(SPACE_DIM == 2 || SPACE_DIM == 3);
1338 double surface_area = 0.0;
1343 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
1345 unsigned next_node_index = p_element->
GetNodeGlobalIndex((local_index + 1) % num_nodes);
1347 surface_area += this->GetDistanceBetweenNodes(this_node_index, next_node_index);
1348 this_node_index = next_node_index;
1354 for (
unsigned face_index = 0; face_index < p_element->
GetNumFaces(); face_index++)
1356 surface_area += CalculateAreaOfFace(p_element->
GetFace(face_index));
1359 return surface_area;
1365template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1367 [[maybe_unused]]
const c_vector<double, SPACE_DIM>& rTestPoint, [[maybe_unused]]
unsigned elementIndex)
1369 if constexpr (ELEMENT_DIM == 2 && SPACE_DIM == 2)
1373 const unsigned num_nodes = p_element->
GetNumNodes();
1376 bool element_includes_point =
false;
1436 const c_vector<double, 2> first_vertex = p_element->
GetNodeLocation(0);
1437 c_vector<double, 2> test_point = GetVectorFromAtoB(first_vertex, rTestPoint);
1440 c_vector<double, 2> vertexA = zero_vector<double>(2);
1441 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
1444 c_vector<double, 2> vector_a_to_point = GetVectorFromAtoB(vertexA, test_point);
1448 if (norm_2(vector_a_to_point) < DBL_EPSILON)
1453 c_vector<double, 2> vertexB = GetVectorFromAtoB(first_vertex, p_element->
GetNodeLocation((local_index + 1) % num_nodes));
1454 c_vector<double, 2> vector_b_to_point = GetVectorFromAtoB(vertexB, test_point);
1455 c_vector<double, 2> vector_a_to_b = GetVectorFromAtoB(vertexA, vertexB);
1458 if ((fabs(vector_a_to_b[1]) < DBL_EPSILON) && (fabs(vector_a_to_point[1]) < DBL_EPSILON) && (fabs(vector_b_to_point[1]) < DBL_EPSILON))
1460 if ((vector_a_to_point[0] > 0) != (vector_b_to_point[0] > 0))
1468 if ((vertexA[1] > test_point[1]) != (vertexB[1] > test_point[1]))
1471 if (test_point[0] < vertexA[0] + vector_a_to_b[0] * vector_a_to_point[1] / vector_a_to_b[1])
1473 element_includes_point = !element_includes_point;
1479 return element_includes_point;
1487template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1489 [[maybe_unused]]
const c_vector<double, SPACE_DIM>& rTestPoint, [[maybe_unused]]
unsigned elementIndex)
1491 if constexpr (ELEMENT_DIM == 2 && SPACE_DIM == 2)
1495 const unsigned num_nodes = p_element->
GetNumNodes();
1497 double min_squared_normal_distance = DBL_MAX;
1498 unsigned min_distance_edge_index = UINT_MAX;
1501 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
1504 const c_vector<double, 2> vertexA = p_element->
GetNodeLocation(local_index);
1505 const c_vector<double, 2> vertexB = p_element->
GetNodeLocation((local_index + 1) % num_nodes);
1507 const c_vector<double, 2> vector_a_to_point = this->GetVectorFromAtoB(vertexA, rTestPoint);
1508 const c_vector<double, 2> vector_a_to_b = this->GetVectorFromAtoB(vertexA, vertexB);
1509 const double distance_a_to_b = norm_2(vector_a_to_b);
1511 const c_vector<double, 2> edge_ab_unit_vector = vector_a_to_b / norm_2(vector_a_to_b);
1512 double distance_parallel_to_edge = inner_prod(vector_a_to_point, edge_ab_unit_vector);
1514 double squared_distance_normal_to_edge = SmallPow(norm_2(vector_a_to_point), 2) - SmallPow(distance_parallel_to_edge, 2);
1522 if (squared_distance_normal_to_edge < DBL_EPSILON)
1524 squared_distance_normal_to_edge = 0.0;
1527 if (fabs(distance_parallel_to_edge) < DBL_EPSILON)
1529 distance_parallel_to_edge = 0.0;
1531 else if (fabs(distance_parallel_to_edge - distance_a_to_b) < DBL_EPSILON)
1533 distance_parallel_to_edge = distance_a_to_b;
1537 if (squared_distance_normal_to_edge < min_squared_normal_distance && distance_parallel_to_edge >= 0 && distance_parallel_to_edge <= distance_a_to_b)
1539 min_squared_normal_distance = squared_distance_normal_to_edge;
1540 min_distance_edge_index = local_index;
1544 assert(min_distance_edge_index < num_nodes);
1545 return min_distance_edge_index;
1553template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1556 if constexpr (SPACE_DIM == 2)
1561 c_vector<double, 3> moments = zero_vector<double>(3);
1564 c_vector<double, SPACE_DIM> centroid = GetCentroidOfElement(index);
1566 c_vector<double, SPACE_DIM> this_node_location = p_element->
GetNodeLocation(0);
1567 c_vector<double, SPACE_DIM> pos_1 = this->GetVectorFromAtoB(centroid, this_node_location);
1569 for (
unsigned local_index = 0; local_index < num_nodes; local_index++)
1571 unsigned next_index = (local_index + 1) % num_nodes;
1572 c_vector<double, SPACE_DIM> next_node_location = p_element->
GetNodeLocation(next_index);
1573 c_vector<double, SPACE_DIM> pos_2 = this->GetVectorFromAtoB(centroid, next_node_location);
1575 double signed_area_term = pos_1(0) * pos_2(1) - pos_2(0) * pos_1(1);
1577 moments(0) += (pos_1(1) * pos_1(1) + pos_1(1) * pos_2(1) + pos_2(1) * pos_2(1)) * signed_area_term;
1580 moments(1) += (pos_1(0) * pos_1(0) + pos_1(0) * pos_2(0) + pos_2(0) * pos_2(0)) * signed_area_term;
1583 moments(2) += (pos_1(0) * pos_2(1) + 2 * pos_1(0) * pos_1(1) + 2 * pos_2(0) * pos_2(1) + pos_2(0) * pos_1(1)) * signed_area_term;
1598 if (moments(0) < 0.0)
1600 moments(0) = -moments(0);
1601 moments(1) = -moments(1);
1602 moments(2) = -moments(2);
1612template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1615 if constexpr (SPACE_DIM == 2)
1618 c_vector<double, SPACE_DIM> short_axis = zero_vector<double>(SPACE_DIM);
1621 c_vector<double, 3> moments = CalculateMomentsOfElement(index);
1624 moments /= norm_2(moments);
1627 double discriminant = (moments(0) - moments(1)) * (moments(0) - moments(1)) + 4.0 * moments(2) * moments(2);
1628 if (fabs(discriminant) < DBL_EPSILON)
1632 short_axis(1) = sqrt(1.0 - short_axis(0) * short_axis(0));
1637 if (fabs(moments(2)) < DBL_EPSILON)
1639 if (moments(0) < moments(1))
1641 short_axis(0) = 0.0;
1642 short_axis(1) = 1.0;
1646 short_axis(0) = 1.0;
1647 short_axis(1) = 0.0;
1653 double lambda = 0.5 * (moments(0) + moments(1) + sqrt(discriminant));
1655 short_axis(0) = 1.0;
1656 short_axis(1) = (moments(0) - lambda) / moments(2);
1659 short_axis /= norm_2(short_axis);
1671template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1675 if constexpr (SPACE_DIM == 2)
1677 unsigned num_nodes_in_element = pElement->GetNumNodes();
1678 unsigned next_local_index = (localIndex + 1) % num_nodes_in_element;
1681 unsigned previous_local_index = (num_nodes_in_element + localIndex - 1) % num_nodes_in_element;
1683 c_vector<double, SPACE_DIM> previous_node_location = pElement->GetNodeLocation(previous_local_index);
1684 c_vector<double, SPACE_DIM> next_node_location = pElement->GetNodeLocation(next_local_index);
1685 c_vector<double, SPACE_DIM> difference_vector = this->GetVectorFromAtoB(previous_node_location, next_node_location);
1687 c_vector<double, SPACE_DIM> area_gradient;
1689 area_gradient[0] = 0.5 * difference_vector[1];
1690 area_gradient[1] = -0.5 * difference_vector[0];
1692 return area_gradient;
1700template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1704 if constexpr (SPACE_DIM == 2)
1706 assert(SPACE_DIM == 2);
1708 const unsigned num_nodes_in_element = pElement->GetNumNodes();
1711 const unsigned previous_local_index = (num_nodes_in_element + localIndex - 1) % num_nodes_in_element;
1713 const unsigned this_global_index = pElement->GetNodeGlobalIndex(localIndex);
1714 const unsigned previous_global_index = pElement->GetNodeGlobalIndex(previous_local_index);
1716 const double previous_edge_length = this->GetDistanceBetweenNodes(this_global_index, previous_global_index);
1717 assert(previous_edge_length > DBL_EPSILON);
1719 const c_vector<double, SPACE_DIM> previous_edge_gradient = this->GetVectorFromAtoB(pElement->GetNodeLocation(previous_local_index), pElement->GetNodeLocation(localIndex)) / previous_edge_length;
1721 return previous_edge_gradient;
1729template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1733 if constexpr (SPACE_DIM == 2)
1735 const unsigned next_local_index = (localIndex + 1) % (pElement->GetNumNodes());
1737 const unsigned this_global_index = pElement->GetNodeGlobalIndex(localIndex);
1738 const unsigned next_global_index = pElement->GetNodeGlobalIndex(next_local_index);
1740 const double next_edge_length = this->GetDistanceBetweenNodes(this_global_index, next_global_index);
1741 assert(next_edge_length > DBL_EPSILON);
1743 const c_vector<double, SPACE_DIM> next_edge_gradient = this->GetVectorFromAtoB(pElement->GetNodeLocation(next_local_index), pElement->GetNodeLocation(localIndex)) / next_edge_length;
1745 return next_edge_gradient;
1753template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1757 if constexpr (SPACE_DIM == 2)
1760 c_vector<double, SPACE_DIM> previous_edge_gradient = GetPreviousEdgeGradientOfElementAtNode(pElement, localIndex);
1761 c_vector<double, SPACE_DIM> next_edge_gradient = GetNextEdgeGradientOfElementAtNode(pElement, localIndex);
1763 return previous_edge_gradient + next_edge_gradient;
1775template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1778 [[maybe_unused]] c_vector<double, SPACE_DIM>& rNormal)
1780 if constexpr (SPACE_DIM == 3)
1782 assert(SPACE_DIM == 3);
1785 assert(pFace->GetNumNodes() >= 3u);
1788 rNormal = zero_vector<double>(SPACE_DIM);
1790 c_vector<double, SPACE_DIM> v_minus_v0 = this->GetVectorFromAtoB(pFace->GetNode(0)->rGetLocation(), pFace->GetNode(1)->rGetLocation());
1791 for (
unsigned local_index = 2; local_index < pFace->GetNumNodes(); local_index++)
1793 c_vector<double, SPACE_DIM> vnext_minus_v0 = this->GetVectorFromAtoB(pFace->GetNode(0)->rGetLocation(), pFace->GetNode(local_index)->rGetLocation());
1795 v_minus_v0 = vnext_minus_v0;
1797 double magnitude = norm_2(rNormal);
1798 if (magnitude != 0.0)
1801 rNormal /= magnitude;
1805 return magnitude / 2.0;
1813template <
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
1817 if constexpr (SPACE_DIM == 3)
1820 c_vector<double, SPACE_DIM> unit_normal;
1821 return CalculateUnitNormalToFaceWithArea(pFace, unit_normal);
#define EXCEPTION(message)
const unsigned UNSIGNED_UNSET
#define EXPORT_TEMPLATE_CLASS_ALL_DIMS(CLASS)
c_vector< T, 1 > VectorProduct(const c_vector< T, 1 > &rA, const c_vector< T, 1 > &rB)
void SetAttribute(double attribute)
Node< SPACE_DIM > * GetNode(unsigned localIndex) const
double GetNodeLocation(unsigned localIndex, unsigned dimension) const
unsigned GetNumNodes() const
unsigned GetNodeGlobalIndex(unsigned localIndex) const
unsigned GetIndex() const
virtual unsigned GetNumElements() const =0
virtual ElementData GetNextElementData()=0
virtual unsigned GetNumElementAttributes() const
virtual std::vector< double > GetNextNode()=0
virtual bool HasNodePermutation()
virtual unsigned GetNumNodes() const =0
bool mMeshChangesDuringSimulation
virtual c_vector< double, SPACE_DIM > GetVectorFromAtoB(const c_vector< double, SPACE_DIM > &rLocationA, const c_vector< double, SPACE_DIM > &rLocationB)
std::vector< Node< SPACE_DIM > * > mNodes
Element< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
virtual unsigned GetNumElements() const
void AddNode(Node< SPACE_DIM > *pNode, const unsigned &rIndex)
std::set< unsigned > & rGetContainingElementIndices()
void AddElement(unsigned index)
const c_vector< double, SPACE_DIM > & rGetLocation() const
bool IsBoundaryNode() const
unsigned GetIndex() const
static RandomNumberGenerator * Instance()
virtual void GetInverseJacobianForElement(unsigned elementIndex, c_matrix< double, SPACE_DIM, ELEMENT_DIM > &rJacobian, double &rJacobianDeterminant, c_matrix< double, ELEMENT_DIM, SPACE_DIM > &rInverseJacobian) const
VertexElement< ELEMENT_DIM-1, SPACE_DIM > * GetFace(unsigned index) const
void AddFace(VertexElement< ELEMENT_DIM-1, SPACE_DIM > *pFace)
unsigned GetNumFaces() const
std::vector< VertexElement< ELEMENT_DIM - 1, SPACE_DIM > * > mFaces
virtual c_vector< double, SPACE_DIM > GetVectorFromAtoB(const c_vector< double, SPACE_DIM > &rLocationA, const c_vector< double, SPACE_DIM > &rLocationB)
double CalculateUnitNormalToFaceWithArea(VertexElement< ELEMENT_DIM - 1, SPACE_DIM > *pFace, c_vector< double, SPACE_DIM > &rNormal)
VertexElement< ELEMENT_DIM - 1, SPACE_DIM > * GetFace(unsigned index) const
virtual unsigned GetNumNodes() const
virtual double GetSurfaceAreaOfElement(unsigned index)
std::set< unsigned > GetNeighbouringNodeIndices(unsigned nodeIndex)
std::set< unsigned > GetNeighbouringNodeNotAlsoInElement(unsigned nodeIndex, unsigned elemIndex)
void GenerateEdgesFromElements(std::vector< VertexElement< ELEMENT_DIM, SPACE_DIM > * > &rElements)
virtual double CalculateAreaOfFace(VertexElement< ELEMENT_DIM - 1, SPACE_DIM > *pFace)
const EdgeHelper< SPACE_DIM > & rGetEdgeHelper() const
c_vector< double, SPACE_DIM > GetNextEdgeGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
virtual unsigned GetNumFaces() const
VertexElement< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
unsigned GetLocalIndexForElementEdgeClosestToPoint(const c_vector< double, SPACE_DIM > &rTestPoint, unsigned elementIndex)
double GetElongationShapeFactorOfElement(unsigned elementIndex)
unsigned GetNumAllElements() const
unsigned SolveNodeMapping(unsigned index) const
TetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpDelaunayMesh
bool ElementIncludesPoint(const c_vector< double, SPACE_DIM > &rTestPoint, unsigned elementIndex)
c_vector< double, SPACE_DIM > GetAreaGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
bool IsNearExistingNodes(c_vector< double, SPACE_DIM > newNodeLocation, std::vector< Node< SPACE_DIM > * > nodesToCheck, double minClearance)
std::set< unsigned > GetNeighbouringElementIndices(unsigned elementIndex)
c_vector< double, SPACE_DIM > GetPerimeterGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
double GetEdgeLength(unsigned elementIndex1, unsigned elementIndex2)
c_vector< double, SPACE_DIM > GetShortAxisOfElement(unsigned index)
unsigned GetRosetteRankOfElement(unsigned index)
Edge< SPACE_DIM > * GetEdge(unsigned index) const
unsigned SolveBoundaryElementMapping(unsigned index) const
unsigned SolveElementMapping(unsigned index) const
unsigned GetVoronoiElementIndexCorrespondingToDelaunayNodeIndex(unsigned nodeIndex)
virtual c_vector< double, SPACE_DIM > GetCentroidOfElement(unsigned index)
std::map< unsigned, unsigned > mVoronoiElementIndexMap
void ConstructFromMeshReader(AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > &rMeshReader)
unsigned GetDelaunayNodeIndexCorrespondingToVoronoiElementIndex(unsigned elementIndex)
unsigned GetNumEdges() const
c_vector< double, SPACE_DIM > GetPreviousEdgeGradientOfElementAtNode(VertexElement< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localIndex)
std::vector< VertexElement< ELEMENT_DIM, SPACE_DIM > * > mElements
virtual c_vector< double, 3 > CalculateMomentsOfElement(unsigned index)
virtual VertexMesh< ELEMENT_DIM, SPACE_DIM > * GetMeshForVtk()
virtual double GetVolumeOfElement(unsigned index)
virtual unsigned GetNumElements() const
void GenerateVerticesFromElementCircumcentres(TetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
std::vector< unsigned > NodeIndices
std::vector< ElementData > Faces
std::vector< unsigned > NodeIndices