Loading...
Searching...
No Matches
cell.cpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#include<iostream>
26
27#include "bitpit_common.hpp"
28
29#include "vertex.hpp"
30#include "cell.hpp"
31
32namespace bitpit {
33
42IBinaryStream& operator>>(IBinaryStream &buffer, Cell &cell)
43{
44 // Write connectivity data ---------------------------------------------- //
45 Element &element(cell);
46 buffer >> element;
47
48 // Write interface data ------------------------------------------------- //
49 buffer >> cell.m_interfaces;
50
51 // Write adjacencies data ----------------------------------------------- //
52 buffer >> cell.m_adjacencies;
53
54 return buffer;
55}
56
65OBinaryStream& operator<<(OBinaryStream &buffer, const Cell &cell)
66{
67 // Write connectivity data ---------------------------------------------- //
68 const Element &element(cell);
69 buffer << element;
70
71 // Write interface data ------------------------------------------------- //
72 buffer << cell.m_interfaces;
73
74 // Write adjacencies data ----------------------------------------------- //
75 buffer << cell.m_adjacencies;
76
77 return buffer;
78}
79
99bitpit::FlatVector2D<long> Cell::createNeighbourhoodStorage(bool storeNeighbourhood)
100{
101 ElementType type = getType();
102 if (!storeNeighbourhood || type == ElementType::UNDEFINED) {
103 return bitpit::FlatVector2D<long>(false);
104 }
105
106 int nFaces = getFaceCount();
107 if (nFaces <= 0) {
108 return bitpit::FlatVector2D<long>(false);
109 }
110
111 return bitpit::FlatVector2D<long>(nFaces, 0);
112}
113
118 : Element(),
119 m_interfaces(createNeighbourhoodStorage(false)),
120 m_adjacencies(createNeighbourhoodStorage(false)),
121 m_interior(true)
122{
123
124}
125
135Cell::Cell(long id, ElementType type, bool interior, bool storeNeighbourhood)
136 : Cell(id, type, interior, storeNeighbourhood, storeNeighbourhood)
137{
138}
139
151Cell::Cell(long id, ElementType type, int connectSize, bool interior, bool storeNeighbourhood)
152 : Cell(id, type, connectSize, interior, storeNeighbourhood, storeNeighbourhood)
153{
154}
155
167Cell::Cell(long id, ElementType type, std::unique_ptr<long[]> &&connectStorage, bool interior, bool storeNeighbourhood)
168 : Cell(id, type, std::move(connectStorage), interior, storeNeighbourhood, storeNeighbourhood)
169{
170}
171
183Cell::Cell(long id, ElementType type, bool interior, bool storeInterfaces, bool storeAdjacencies)
184 : Element(id, type),
185 m_interfaces(createNeighbourhoodStorage(storeInterfaces)),
186 m_adjacencies(createNeighbourhoodStorage(storeAdjacencies))
187{
188 _initialize(interior, false, false, false, false);
189}
190
204Cell::Cell(long id, ElementType type, int connectSize, bool interior, bool storeInterfaces, bool storeAdjacencies)
205 : Element(id, type, connectSize),
206 m_interfaces(createNeighbourhoodStorage(storeInterfaces)),
207 m_adjacencies(createNeighbourhoodStorage(storeAdjacencies))
208{
209 _initialize(interior, false, false, false, false);
210}
211
225Cell::Cell(long id, ElementType type, std::unique_ptr<long[]> &&connectStorage, bool interior, bool storeInterfaces, bool storeAdjacencies)
226 : Element(id, type, std::move(connectStorage)),
227 m_interfaces(createNeighbourhoodStorage(storeInterfaces)),
228 m_adjacencies(createNeighbourhoodStorage(storeAdjacencies))
229{
230 _initialize(interior, false, false, false, false);
231}
232
238void Cell::swap(Cell &other) noexcept
239{
240 Element::swap(other);
241
242 std::swap(other.m_interior, m_interior);
243
244 other.m_interfaces.swap(m_interfaces);
245 other.m_adjacencies.swap(m_adjacencies);
246}
247
257void Cell::initialize(long id, ElementType type, bool interior, bool storeNeighbourhood)
258{
259 initialize(id, type, interior, storeNeighbourhood, storeNeighbourhood);
260}
261
273void Cell::initialize(long id, ElementType type, int connectSize, bool interior, bool storeNeighbourhood)
274{
275 initialize(id, type, connectSize, interior, storeNeighbourhood, storeNeighbourhood);
276}
277
289void Cell::initialize(long id, ElementType type, std::unique_ptr<long[]> &&connectStorage, bool interior, bool storeNeighbourhood)
290{
291 initialize(id, type, std::move(connectStorage), interior, storeNeighbourhood, storeNeighbourhood);
292}
293
305void Cell::initialize(long id, ElementType type, bool interior, bool storeInterfaces, bool storeAdjacencies)
306{
307 Element::initialize(id, type);
308
309 _initialize(interior, true, storeInterfaces, true, storeAdjacencies);
310}
311
325void Cell::initialize(long id, ElementType type, int connectSize, bool interior, bool storeInterfaces, bool storeAdjacencies)
326{
327 Element::initialize(id, type, connectSize);
328
329 _initialize(interior, true, storeInterfaces, true, storeAdjacencies);
330}
331
345void Cell::initialize(long id, ElementType type, std::unique_ptr<long[]> &&connectStorage, bool interior, bool storeInterfaces, bool storeAdjacencies)
346{
347 Element::initialize(id, type, std::move(connectStorage));
348
349 _initialize(interior, true, storeInterfaces, true, storeAdjacencies);
350}
351
365void Cell::_initialize(bool interior, bool initializeInterfaces, bool storeInterfaces, bool initializeAdjacency, bool storeAdjacencies)
366{
367 setInterior(interior);
368
369 // Interface information
370 if (initializeInterfaces) {
371 resetInterfaces(storeInterfaces);
372 }
373
374 // Interface information
375 if (initializeAdjacency) {
376 resetAdjacencies(storeAdjacencies);
377 }
378}
379
385void Cell::setInterior(bool interior)
386{
387 m_interior = interior;
388}
389
397{
398 return m_interior;
399}
400
405{
406 resetInterfaces(false);
407}
408
425void Cell::resetInterfaces(bool storeInterfaces)
426{
427 // Early return if no interfaces should be stored
428 ElementType type = getType();
429 if (!storeInterfaces || type == ElementType::UNDEFINED) {
430 m_interfaces.destroy();
431 return;
432 }
433
434 // Early return if not element defines no faces
435 int nFaces = getFaceCount();
436 if (nFaces <= 0) {
437 m_interfaces.destroy();
438 return;
439 }
440
441 // Initialize the interface storage
442 m_interfaces.initialize(nFaces, 0);
443}
444
450void Cell::setInterfaces(const std::vector<std::vector<long>> &interfaces)
451{
452 if (getType() == ElementType::UNDEFINED) {
453 return;
454 }
455
456 assert((int) interfaces.size() == getFaceCount());
457 assert((int) m_interfaces.size() == getFaceCount());
458 m_interfaces.initialize(interfaces);
459}
460
467{
468 if (getType() == ElementType::UNDEFINED) {
469 return;
470 }
471
472 assert((int) m_interfaces.size() == getFaceCount());
473 assert((int) interfaces.size() == getFaceCount());
474 m_interfaces.swap(interfaces);
475}
476
484void Cell::setInterface(int face, int index, long interface)
485{
486 m_interfaces.setItem(face, index, interface);
487}
488
501bool Cell::pushInterface(int face, long interface)
502{
503 // Do not push an existing interface
504 if (findInterface(face, interface) >= 0) {
505 return false;
506 }
507
508 // Add the interface
509 m_interfaces.pushBackItem(face, interface);
510
511 // Done
512 return true;
513}
514
522void Cell::deleteInterface(int face, int i)
523{
524 m_interfaces.eraseItem(face, i);
525}
526
536{
537 return m_interfaces.getItemCount();
538}
539
549int Cell::getInterfaceCount(int face) const
550{
551 return m_interfaces.getItemCount(face);
552}
553
561long Cell::getInterface(int face, int index) const
562{
563 return m_interfaces.getItem(face, index);
564}
565
571const long * Cell::getInterfaces() const
572{
573 if (m_interfaces.empty()) {
574 return nullptr;
575 }
576
577 return m_interfaces.get(0);
578}
579
586const long * Cell::getInterfaces(int face) const
587{
588 if (m_interfaces.empty()) {
589 return nullptr;
590 }
591
592 return m_interfaces.get(face);
593}
594
601{
602 if (m_interfaces.empty()) {
603 return nullptr;
604 }
605
606 return m_interfaces.get(0);
607}
608
615long * Cell::getInterfaces(int face)
616{
617 if (m_interfaces.empty()) {
618 return nullptr;
619 }
620
621 return m_interfaces.get(face);
622}
623
635int Cell::findInterface(int face, int interface)
636{
637 long *faceInterfaces = getInterfaces(face);
638 int nFaceInterfaces = getInterfaceCount(face);
639 for (int i = 0; i < nFaceInterfaces; i++) {
640 if (faceInterfaces[i] == interface) {
641 return i;
642 }
643 }
644
645 return -1;
646}
647
658int Cell::findInterface(int interface)
659{
660 int nCellInterfaces = getInterfaceCount();
661 const long *interfaces = getInterfaces();
662 for (int i = 0; i < nCellInterfaces; i++) {
663 if (interfaces[i] == interface) {
664 return i;
665 }
666 }
667
668 return -1;
669}
670
675{
676 resetAdjacencies(false);
677}
678
695void Cell::resetAdjacencies(bool storeAdjacencies)
696{
697 // Early return if no adjacencies should be stored
698 ElementType type = getType();
699 if (!storeAdjacencies || type == ElementType::UNDEFINED) {
700 m_adjacencies.destroy();
701 return;
702 }
703
704 // Early return if not element defines no faces
705 int nFaces = getFaceCount();
706 if (nFaces <= 0) {
707 m_adjacencies.destroy();
708 return;
709 }
710
711 // Initialize the interface storage
712 m_adjacencies.initialize(nFaces, 0);
713}
714
720void Cell::setAdjacencies(const std::vector<std::vector<long>> &adjacencies)
721{
722 if (getType() == ElementType::UNDEFINED) {
723 return;
724 }
725
726 assert((int) m_adjacencies.size() == getFaceCount());
727 assert((int) adjacencies.size() == getFaceCount());
728 m_adjacencies.initialize(adjacencies);
729}
730
737{
738 if (getType() == ElementType::UNDEFINED) {
739 return;
740 }
741
742 assert((int) m_adjacencies.size() == getFaceCount());
743 assert((int) adjacencies.size() == getFaceCount());
744 m_adjacencies.swap(adjacencies);
745}
746
754void Cell::setAdjacency(int face, int index, long adjacency)
755{
756 m_adjacencies.setItem(face, index, adjacency);
757}
758
771bool Cell::pushAdjacency(int face, long adjacency)
772{
773 // Do not push an existing adjacency
774 if (findAdjacency(face, adjacency) >= 0) {
775 return false;
776 }
777
778 // Add the adjacency
779 m_adjacencies.pushBackItem(face, adjacency);
780
781 // Done
782 return true;
783}
784
792void Cell::deleteAdjacency(int face, int i)
793{
794 m_adjacencies.eraseItem(face, i);
795}
796
806{
807 return m_adjacencies.getItemCount();
808}
809
819int Cell::getAdjacencyCount(int face) const
820{
821 return m_adjacencies.getItemCount(face);
822}
823
831long Cell::getAdjacency(int face, int index) const
832{
833 return m_adjacencies.getItem(face, index);
834}
835
841const long * Cell::getAdjacencies() const
842{
843 if (m_adjacencies.empty()) {
844 return nullptr;
845 }
846
847 return m_adjacencies.get(0);
848}
849
856const long * Cell::getAdjacencies(int face) const
857{
858 if (m_adjacencies.empty()) {
859 return nullptr;
860 }
861
862 return m_adjacencies.get(face);
863}
864
871{
872 if (m_adjacencies.empty()) {
873 return nullptr;
874 }
875
876 return m_adjacencies.get(0);
877}
878
885long * Cell::getAdjacencies(int face)
886{
887 if (m_adjacencies.empty()) {
888 return nullptr;
889 }
890
891 return m_adjacencies.get(face);
892}
893
905int Cell::findAdjacency(int face, int adjacency)
906{
907 long *faceAdjacencies = getAdjacencies(face);
908 int nFaceAdjacencies = getAdjacencyCount(face);
909 for (int i = 0; i < nFaceAdjacencies; i++) {
910 if (faceAdjacencies[i] == adjacency) {
911 return i;
912 }
913 }
914
915 return -1;
916}
917
928int Cell::findAdjacency(int adjacency)
929{
930 int nCellAdjacencies = getAdjacencyCount();
931 const long *adjacencies = getAdjacencies();
932 for (int i = 0; i < nCellAdjacencies; i++) {
933 if (adjacencies[i] == adjacency) {
934 return i;
935 }
936 }
937
938 return -1;
939}
940
946bool Cell::isFaceBorder(int face) const
947{
948 return (m_adjacencies.getItemCount(face) == 0);
949}
950
958void Cell::display(std::ostream &out, unsigned short int indent) const
959{
960 // ====================================================================== //
961 // VARIABLES DECLARATION //
962 // ====================================================================== //
963
964 // Local variables
965 std::string t_s(indent, ' ');
966
967 // Counters
968 int i, j;
969 int nn;
970
971 // ====================================================================== //
972 // DISPLAY INFOS //
973 // ====================================================================== //
974 if (getType() == ElementType::UNDEFINED) {
975 out << t_s << "cell type: (unknown)" << std::endl;
976 return;
977 }
978
979 // Scope variables -------------------------------------------------- //
980 int nv = getVertexCount();
981 int nf = getFaceCount();
982
983 ConstProxyVector<long> cellVertexIds = getVertexIds();
984
985 // General info ----------------------------------------------------- //
986 out << t_s << "cell type: " << getType() << std::endl;
987 out << t_s << "ID: " << getId() << std::endl;
988 out << t_s << "is ghost: ";
989 if (m_interior) { out << "(false)"; }
990 else { out << "(true)"; }
991 out << std::endl;
992
993 // Connectivity infos --------------------------------------------------- //
994 out << t_s << "connectivity: [ ";
995 for (i = 0; i < nv-1; ++i) {
996 if (cellVertexIds[i] == Vertex::NULL_ID) out << "n.a. ";
997 else out << cellVertexIds[i] << ", ";
998 } //next i
999 if (cellVertexIds[nv-1] == Vertex::NULL_ID) out << "n.a. ";
1000 else out << cellVertexIds[nv-1] << " ]" << std::endl;
1001
1002 // neighbors infos ------------------------------------------------------ //
1003 if (m_adjacencies.size() > 0) {
1004 out << t_s << "neighbors: [ ";
1005 for (i = 0; i < nf; ++i) {
1006 nn = getAdjacencyCount(i);
1007 out << "[ ";
1008 if (nn == 0) {
1009 out << "n.a. ";
1010 } else {
1011 for (j = 0; j < nn; ++j) {
1012 out << getAdjacency(i, j);
1013 if (j != (nn - 1)) {
1014 out << ",";
1015 }
1016 out << " ";
1017 }
1018 }
1019 out << "]";
1020 if (i != (nf - 1)) {
1021 out << ",";
1022 }
1023 out << " ";
1024 }
1025 out << "]" << std::endl;
1026 }
1027
1028 // interface infos ------------------------------------------------------ //
1029 if (m_interfaces.size() > 0) {
1030 out << t_s << "interfaces: [ ";
1031 for (i = 0; i < nf; ++i) {
1032 nn = getInterfaceCount(i);
1033 out << "[ ";
1034 if (nn == 0) {
1035 out << "n.a. ";
1036 } else {
1037 for (j = 0; j < nn; ++j) {
1038 out << getInterface(i, j);
1039 if (j != (nn - 1)) {
1040 out << ",";
1041 }
1042 out << " ";
1043 }
1044 }
1045 out << "]";
1046 if (i != (nf - 1)) {
1047 out << ",";
1048 }
1049 out << " ";
1050 }
1051 out << "]" << std::endl;
1052 }
1053
1054}
1055
1061unsigned int Cell::getBinarySize() const
1062{
1063 return (Element::getBinarySize() + m_interfaces.getBinarySize() + m_adjacencies.getBinarySize());
1064}
1065
1066// Explicit instantiation of the Cell containers
1067template class PiercedVector<Cell>;
1068
1069template class QualifiedCellHalfEdge<Cell>;
1071
1072template class QualifiedCellHalfFace<Cell>;
1074
1075}
The Cell class defines the cells.
Definition cell.hpp:42
bool isInterior() const
Definition cell.cpp:396
void swap(Cell &other) noexcept
Definition cell.cpp:238
bool isFaceBorder(int face) const
Definition cell.cpp:946
void initialize(long id, ElementType type, bool interior, bool storeNeighbourhood)
Definition cell.cpp:257
bool pushAdjacency(int face, long adjacency)
Definition cell.cpp:771
void setInterior(bool interior)
Definition cell.cpp:385
void deleteInterface(int face, int i)
Definition cell.cpp:522
void setInterfaces(const std::vector< std::vector< long > > &interfaces)
Definition cell.cpp:450
void deleteAdjacencies()
Definition cell.cpp:674
int findInterface(int face, int interface)
Definition cell.cpp:635
int getInterfaceCount() const
Definition cell.cpp:535
void deleteInterfaces()
Definition cell.cpp:404
int findAdjacency(int face, int adjacency)
Definition cell.cpp:905
void resetInterfaces(bool storeInterfaces=true)
Definition cell.cpp:425
unsigned int getBinarySize() const
Definition cell.cpp:1061
void resetAdjacencies(bool storeAdjacencies=true)
Definition cell.cpp:695
long getAdjacency(int face, int index=0) const
Definition cell.cpp:831
const long * getAdjacencies() const
Definition cell.cpp:841
void setInterface(int face, int index, long interface)
Definition cell.cpp:484
void setAdjacency(int face, int index, long adjacencies)
Definition cell.cpp:754
const long * getInterfaces() const
Definition cell.cpp:571
void display(std::ostream &out, unsigned short int indent) const
Definition cell.cpp:958
void deleteAdjacency(int face, int i)
Definition cell.cpp:792
int getAdjacencyCount() const
Definition cell.cpp:805
long getInterface(int face, int index=0) const
Definition cell.cpp:561
void setAdjacencies(const std::vector< std::vector< long > > &adjacencies)
Definition cell.cpp:720
bool pushInterface(int face, long interface)
Definition cell.cpp:501
The Element class provides an interface for defining elements.
Definition element.hpp:46
long getId() const
Definition element.cpp:510
ElementType getType() const
Definition element.cpp:551
void swap(Element &other) noexcept
Definition element.cpp:399
unsigned int getBinarySize() const
Definition element.cpp:2144
ConstProxyVector< long > getVertexIds() const
Definition element.cpp:1181
void initialize(long id, ElementType type, int connectSize=0)
Definition element.cpp:415
int getFaceCount() const
Definition element.cpp:678
int getVertexCount() const
Definition element.cpp:1152
Metafunction for generation of a flattened vector of vectors.
T & getItem(std::size_t i, std::size_t j)
void swap(FlatVector2D &other) noexcept
std::size_t size() const
std::size_t getItemCount() const
void initialize(const std::vector< std::size_t > &sizes, const T &value=T())
void eraseItem(std::size_t i, std::size_t j)
const T * get(std::size_t i) const
std::size_t getBinarySize() const
void setItem(std::size_t i, std::size_t j, const T &value)
void pushBackItem(const T &value)
Metafunction for generating a pierced vector.
Metafunction for generating a list of elements that can be either stored in an external vectror or,...
The QualifiedCellHalfFace class defines cell half-faces.
Definition cell.hpp:137
Logger & operator<<(Logger &logger, LoggerManipulator< T > &&m)
Definition logger.hpp:367
--- layout: doxygen_footer ---