Creation and manipulation a list of cells.
Creation and manipulation a list of cellsThis example shows how to use the pierced vector to create and manipulate a list of cells.
#if BITPIT_ENABLE_MPI==1
#include <mpi.h>
#endif
#include "bitpit_patchkernel.hpp"
using namespace bitpit;
{
std::cout << std::endl << " List of cell ids:" << std::endl;
std::cout << std::endl << " Vector is empty!" << std::endl;
return;
}
for (auto const &cell : cells) {
std::cout << " > id = " << cell.getId() << std::endl;
}
}
{
for (int i = 0; i < nCells; i++) {
cells.
emplace(i, i, ElementType::TRIANGLE);
}
}
void run()
{
std::cout << std::endl << "::: Creating an empty PiercedVector :::" << std::endl;
std::cout << std::endl;
std::cout << " Pierced vector created" << std::endl;
std::cout << std::endl << "::: Filling the vector :::" << std::endl;
int nCells = 10;
fillCellList(nCells, cells);
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
int id_erase;
std::cout << std::endl << "::: Deleting cells :::" << std::endl;
std::cout << std::endl;
for (int id_erase = 0; id_erase < nCells; id_erase += 2) {
if (!cells.
exists(id_erase)) {
continue;
}
std::cout << " Deleting element with id = " << id_erase << std::endl;
}
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
for (int i = 0; i < 10; ++i) {
std::cout <<
" Element id before which there are " << i <<
" elements: " << cells.
getSizeMarker(i) << std::endl;
}
std::cout << std::endl << "::: Resizing the cell container :::" << std::endl;
std::cout << std::endl;
int size = 3;
std::cout << " Resizing the cell container to " << size << " elements" << std::endl;
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
int id_insert;
std::cout << std::endl << "::: Inserting cells :::" << std::endl;
std::cout << std::endl;
id_insert = 10;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (at first avilable position) element with id = " << id_insert << std::endl;
cells.
emplace(id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 13;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (at the end) element with id = " << id_insert << std::endl;
cells.
emplaceBack(id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 15;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (at first avilable position) element with id = " << id_insert << std::endl;
cells.
emplace(id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 17;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (at the end) element with id = " << id_insert << std::endl;
cells.
emplaceBack(id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 45;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (at first avilable position) element with id = " << id_insert << std::endl;
cells.
emplace(id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 102;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (at the end) element with id = " << id_insert << std::endl;
cells.
emplaceBack(id_insert, id_insert, ElementType::TRIANGLE);
}
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: Inserting cells after/before :::" << std::endl;
std::cout << std::endl;
long id_reference;
id_insert = 111;
id_reference = 13;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (before the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
cells.
emplaceBefore(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 123;
id_reference = 102;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (before the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
cells.
emplaceBefore(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 124;
id_reference = 10;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (before the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
cells.
emplaceBefore(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 125;
id_reference = 124;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (after the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
cells.
emplaceAfter(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 126;
id_reference = 102;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (after the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
cells.
emplaceAfter(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
}
id_insert = 127;
id_reference = 45;
if (!cells.
exists(id_insert)) {
std::cout << " Inserting (after the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
cells.
emplaceAfter(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
}
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: List of cells id :::" << std::endl;
std::cout << std::endl;
std::cout << " List of the ids (not ordered)" << std::endl;
for (auto const &id : cells.getIds(false)) {
std::cout << " > id = " << id << std::endl;
}
std::cout << std::endl;
std::cout << " List of the ids (ordered)" << std::endl;
for (auto const &id : cells.getIds(true)) {
std::cout << " > id = " << id << std::endl;
}
std::cout << std::endl << "::: Sort cells by ids :::" << std::endl;
printCellIds(cells);
std::cout << std::endl << "::: Deleting all cells :::" << std::endl;
}
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: Filling the vector :::" << std::endl;
fillCellList(nCells, cells);
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: Deleting cells :::" << std::endl;
std::cout << std::endl;
id_erase = 0;
std::cout << " Deleting element with id = " << id_erase << std::endl;
id_erase = 1;
std::cout << " Deleting element with id = " << id_erase << std::endl;
id_erase = 2;
std::cout << " Deleting element with id = " << id_erase << std::endl;
id_erase = 3;
std::cout << " Deleting element with id = " << id_erase << std::endl;
id_erase = 4;
std::cout << " Deleting element with id = " << id_erase << std::endl;
id_erase = 8;
std::cout << " Deleting element with id = " << id_erase << std::endl;
id_erase = 7;
std::cout << " Deleting element with id = " << id_erase << std::endl;
std::cout << std::endl << "::: Inserting cells :::" << std::endl;
std::cout << std::endl;
id_insert = 40;
std::cout << " Emplacing element with id = " << id_insert << std::endl;
cells.
emplace(id_insert, id_insert, ElementType::TRIANGLE);
id_insert = 41;
std::cout << " Emplacing element with id = " << id_insert << std::endl;
cells.
emplace(id_insert, id_insert, ElementType::TRIANGLE);
id_insert = 42;
std::cout << " Emplacing element with id = " << id_insert << std::endl;
cells.
emplace(id_insert, id_insert, ElementType::TRIANGLE);
printCellIds(cells);
std::cout << std::endl << "::: Moving cells :::" << std::endl;
std::cout << std::endl;
long id_move;
id_move = 42;
id_reference = 40;
std::cout << std::endl;
std::cout << " Moving element with id = " << id_move << " before element with id = " << id_reference << std::endl;
printCellIds(cells);
id_move = 40;
id_reference = 41;
std::cout << std::endl;
std::cout << " Moving element with id = " << id_move << " after element with id = " << id_reference << std::endl;
printCellIds(cells);
id_move = 5;
id_reference = 9;
std::cout << std::endl;
std::cout << " Moving element with id = " << id_move << " after element with id = " << id_reference << std::endl;
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: Squeeze :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Capacity before squeeze = " << cells.
capacity() << std::endl;
std::cout <<
" Capacity after squeeze = " << cells.
capacity() << std::endl;
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: Clear the vector :::" << std::endl;
printCellIds(cells);
std::cout << std::endl << "::: Filling the vector :::" << std::endl;
fillCellList(nCells, cells);
printCellIds(cells);
std::cout << std::endl << "::: Marker :::" << std::endl;
std::cout << std::endl;
std::cout <<
" Element id before which there are 4 elements: " << cells.
getSizeMarker(4) << std::endl;
std::cout << std::endl << "::: Done :::" << std::endl;
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
#if BITPIT_ENABLE_MPI==1
MPI_Init(&argc,&argv);
#else
#endif
int nProcs;
int rank;
#if BITPIT_ENABLE_MPI==1
MPI_Comm_size(MPI_COMM_WORLD, &nProcs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#else
nProcs = 1;
rank = 0;
#endif
log::manager().initialize(log::MODE_SEPARATE, false, nProcs, rank);
log::cout() << log::fileVerbosity(log::LEVEL_INFO);
log::cout() << log::disableConsole();
try {
run();
} catch (const std::exception &exception) {
log::cout() << exception.what();
exit(1);
}
#if BITPIT_ENABLE_MPI==1
MPI_Finalize();
#endif
}
id_t getSizeMarker(std::size_t targetSize, const id_t &fallback=-1)
std::size_t capacity() const
bool exists(id_t id) const
Metafunction for generating a pierced vector.
iterator emplaceBefore(const id_t &referenceId, id_t id, Args &&... args)
iterator emplace(id_t id, Args &&... args)
void clear(bool release=true)
iterator erase(id_t id, bool delayed=false)
iterator moveAfter(const id_t &referenceId, id_t id, bool delayed=false)
void emplaceBack(id_t id, Args &&... args)
void resize(std::size_t n)
iterator moveBefore(const id_t &referenceId, id_t id, bool delayed=false)
iterator emplaceAfter(const id_t &referenceId, id_t id, Args &&... args)
#define BITPIT_UNUSED(variable)