Loading...
Searching...
No Matches
patchkernel_example_00001.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
36#if BITPIT_ENABLE_MPI==1
37#include <mpi.h>
38#endif
39
40#include "bitpit_patchkernel.hpp"
41
42using namespace bitpit;
43
44void printCellIds(PiercedVector<Cell> &cells)
45{
46 std::cout << std::endl << " List of cell ids:" << std::endl;
47
48 if (cells.empty()) {
49 std::cout << std::endl << " Vector is empty!" << std::endl;
50 return;
51 }
52
53 for (auto const &cell : cells) {
54 std::cout << " > id = " << cell.getId() << std::endl;
55 }
56}
57
58void fillCellList(int nCells, PiercedVector<Cell> &cells)
59{
60 for (int i = 0; i < nCells; i++) {
61 cells.emplace(i, i, ElementType::TRIANGLE);
62 }
63}
64
68void run()
69{
70 // Creating an emtpy list of cells
71 std::cout << std::endl << "::: Creating an empty PiercedVector :::" << std::endl;
72 std::cout << std::endl;
73
74 std::cout << " Pierced vector created" << std::endl;
75
77
78 // Filling the list of cells
79 std::cout << std::endl << "::: Filling the vector :::" << std::endl;
80
81 int nCells = 10;
82 fillCellList(nCells, cells);
83
84 printCellIds(cells);
85
86 // Find marker
87 std::cout << std::endl << "::: Marker :::" << std::endl;
88 std::cout << std::endl;
89
90 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
91
92 // Deleting cells
93 int id_erase;
94
95 std::cout << std::endl << "::: Deleting cells :::" << std::endl;
96 std::cout << std::endl;
97
98 nCells = cells.size();
99 for (int id_erase = 0; id_erase < nCells; id_erase += 2) {
100 if (!cells.exists(id_erase)) {
101 continue;
102 }
103
104 std::cout << " Deleting element with id = " << id_erase << std::endl;
105 cells.erase(id_erase);
106 }
107
108 printCellIds(cells);
109
110 // Find marker
111 std::cout << std::endl << "::: Marker :::" << std::endl;
112 std::cout << std::endl;
113
114 for (int i = 0; i < 10; ++i) {
115 std::cout << " Element id before which there are " << i << " elements: " << cells.getSizeMarker(i) << std::endl;
116 }
117
118 // Resizing the cell container
119 std::cout << std::endl << "::: Resizing the cell container :::" << std::endl;
120 std::cout << std::endl;
121
122 int size = 3;
123 std::cout << " Resizing the cell container to " << size << " elements" << std::endl;
124
125 cells.resize(size);
126
127 printCellIds(cells);
128
129 // Find marker
130 std::cout << std::endl << "::: Marker :::" << std::endl;
131 std::cout << std::endl;
132
133 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
134
135 // Inserting cells again
136 int id_insert;
137
138 std::cout << std::endl << "::: Inserting cells :::" << std::endl;
139 std::cout << std::endl;
140
141 id_insert = 10;
142 if (!cells.exists(id_insert)) {
143 std::cout << " Inserting (at first avilable position) element with id = " << id_insert << std::endl;
144 cells.emplace(id_insert, id_insert, ElementType::TRIANGLE);
145 }
146
147 id_insert = 13;
148 if (!cells.exists(id_insert)) {
149 std::cout << " Inserting (at the end) element with id = " << id_insert << std::endl;
150 cells.emplaceBack(id_insert, id_insert, ElementType::TRIANGLE);
151 }
152
153 id_insert = 15;
154 if (!cells.exists(id_insert)) {
155 std::cout << " Inserting (at first avilable position) element with id = " << id_insert << std::endl;
156 cells.emplace(id_insert, id_insert, ElementType::TRIANGLE);
157 }
158
159 id_insert = 17;
160 if (!cells.exists(id_insert)) {
161 std::cout << " Inserting (at the end) element with id = " << id_insert << std::endl;
162 cells.emplaceBack(id_insert, id_insert, ElementType::TRIANGLE);
163 }
164
165 id_insert = 45;
166 if (!cells.exists(id_insert)) {
167 std::cout << " Inserting (at first avilable position) element with id = " << id_insert << std::endl;
168 cells.emplace(id_insert, id_insert, ElementType::TRIANGLE);
169 }
170
171 id_insert = 102;
172 if (!cells.exists(id_insert)) {
173 std::cout << " Inserting (at the end) element with id = " << id_insert << std::endl;
174 cells.emplaceBack(id_insert, id_insert, ElementType::TRIANGLE);
175 }
176
177 printCellIds(cells);
178
179 // Find marker
180 std::cout << std::endl << "::: Marker :::" << std::endl;
181 std::cout << std::endl;
182
183 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
184
185 std::cout << std::endl << "::: Inserting cells after/before :::" << std::endl;
186 std::cout << std::endl;
187
188 long id_reference;
189
190 id_insert = 111;
191 id_reference = 13;
192 if (!cells.exists(id_insert)) {
193 std::cout << " Inserting (before the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
194 cells.emplaceBefore(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
195 }
196
197 id_insert = 123;
198 id_reference = 102;
199 if (!cells.exists(id_insert)) {
200 std::cout << " Inserting (before the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
201 cells.emplaceBefore(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
202 }
203
204
205 id_insert = 124;
206 id_reference = 10;
207 if (!cells.exists(id_insert)) {
208 std::cout << " Inserting (before the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
209 cells.emplaceBefore(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
210 }
211
212 id_insert = 125;
213 id_reference = 124;
214 if (!cells.exists(id_insert)) {
215 std::cout << " Inserting (after the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
216 cells.emplaceAfter(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
217 }
218
219 id_insert = 126;
220 id_reference = 102;
221 if (!cells.exists(id_insert)) {
222 std::cout << " Inserting (after the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
223 cells.emplaceAfter(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
224 }
225
226 id_insert = 127;
227 id_reference = 45;
228 if (!cells.exists(id_insert)) {
229 std::cout << " Inserting (after the element with id = " << id_reference << ") element with id = " << id_insert << std::endl;
230 cells.emplaceAfter(id_reference, id_insert, id_insert, ElementType::TRIANGLE);
231 }
232
233 printCellIds(cells);
234
235 // Find marker
236 std::cout << std::endl << "::: Marker :::" << std::endl;
237 std::cout << std::endl;
238
239 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
240
241 // List of ids
242 std::cout << std::endl << "::: List of cells id :::" << std::endl;
243 std::cout << std::endl;
244
245 std::cout << " List of the ids (not ordered)" << std::endl;
246 for (auto const &id : cells.getIds(false)) {
247 std::cout << " > id = " << id << std::endl;
248 }
249
250 std::cout << std::endl;
251 std::cout << " List of the ids (ordered)" << std::endl;
252 for (auto const &id : cells.getIds(true)) {
253 std::cout << " > id = " << id << std::endl;
254 }
255
256 // Sort the vector
257 std::cout << std::endl << "::: Sort cells by ids :::" << std::endl;
258
259 cells.sort();
260
261 printCellIds(cells);
262
263 // Delete all the cells
264 std::cout << std::endl << "::: Deleting all cells :::" << std::endl;
265
266 while (!cells.empty()) {
267 cells.popBack();
268 }
269
270 printCellIds(cells);
271
272 // Find marker
273 std::cout << std::endl << "::: Marker :::" << std::endl;
274 std::cout << std::endl;
275
276 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
277
278 // Filling the vector again
279 std::cout << std::endl << "::: Filling the vector :::" << std::endl;
280
281 fillCellList(nCells, cells);
282
283 printCellIds(cells);
284
285 // Find marker
286 std::cout << std::endl << "::: Marker :::" << std::endl;
287 std::cout << std::endl;
288
289 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
290
291 // Deleting cells
292 std::cout << std::endl << "::: Deleting cells :::" << std::endl;
293 std::cout << std::endl;
294
295 id_erase = 0;
296 std::cout << " Deleting element with id = " << id_erase << std::endl;
297 cells.erase(id_erase);
298
299 id_erase = 1;
300 std::cout << " Deleting element with id = " << id_erase << std::endl;
301 cells.erase(id_erase);
302
303 id_erase = 2;
304 std::cout << " Deleting element with id = " << id_erase << std::endl;
305 cells.erase(id_erase);
306
307 id_erase = 3;
308 std::cout << " Deleting element with id = " << id_erase << std::endl;
309 cells.erase(id_erase);
310
311 id_erase = 4;
312 std::cout << " Deleting element with id = " << id_erase << std::endl;
313 cells.erase(id_erase);
314
315 id_erase = 8;
316 std::cout << " Deleting element with id = " << id_erase << std::endl;
317 cells.erase(id_erase);
318
319 id_erase = 7;
320 std::cout << " Deleting element with id = " << id_erase << std::endl;
321 cells.erase(id_erase);
322
323 // Inserting cells
324 std::cout << std::endl << "::: Inserting cells :::" << std::endl;
325 std::cout << std::endl;
326
327 id_insert = 40;
328 std::cout << " Emplacing element with id = " << id_insert << std::endl;
329 cells.emplace(id_insert, id_insert, ElementType::TRIANGLE);
330
331 id_insert = 41;
332 std::cout << " Emplacing element with id = " << id_insert << std::endl;
333 cells.emplace(id_insert, id_insert, ElementType::TRIANGLE);
334
335 id_insert = 42;
336 std::cout << " Emplacing element with id = " << id_insert << std::endl;
337 cells.emplace(id_insert, id_insert, ElementType::TRIANGLE);
338
339 printCellIds(cells);
340
341 // Moving elements
342 std::cout << std::endl << "::: Moving cells :::" << std::endl;
343 std::cout << std::endl;
344
345 long id_move;
346
347 id_move = 42;
348 id_reference = 40;
349 std::cout << std::endl;
350 std::cout << " Moving element with id = " << id_move << " before element with id = " << id_reference << std::endl;
351 cells.moveBefore(id_reference, id_move);
352
353 printCellIds(cells);
354
355 id_move = 40;
356 id_reference = 41;
357 std::cout << std::endl;
358 std::cout << " Moving element with id = " << id_move << " after element with id = " << id_reference << std::endl;
359 cells.moveAfter(id_reference, id_move);
360
361 printCellIds(cells);
362
363 id_move = 5;
364 id_reference = 9;
365 std::cout << std::endl;
366 std::cout << " Moving element with id = " << id_move << " after element with id = " << id_reference << std::endl;
367 cells.moveAfter(id_reference, id_move);
368
369 printCellIds(cells);
370
371 // Find marker
372 std::cout << std::endl << "::: Marker :::" << std::endl;
373 std::cout << std::endl;
374
375 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
376
377 // Squeezee the vector
378 std::cout << std::endl << "::: Squeeze :::" << std::endl;
379 std::cout << std::endl;
380
381 std::cout << " Capacity before squeeze = " << cells.capacity() << std::endl;
382
383 cells.squeeze();
384
385 std::cout << " Capacity after squeeze = " << cells.capacity() << std::endl;
386
387 printCellIds(cells);
388
389 // Find marker
390 std::cout << std::endl << "::: Marker :::" << std::endl;
391 std::cout << std::endl;
392
393 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
394
395 // Clear the vector
396 std::cout << std::endl << "::: Clear the vector :::" << std::endl;
397
398 cells.clear();
399
400 printCellIds(cells);
401
402 // Filling the vector again
403 std::cout << std::endl << "::: Filling the vector :::" << std::endl;
404
405 fillCellList(nCells, cells);
406
407 printCellIds(cells);
408
409 // Find marker
410 std::cout << std::endl << "::: Marker :::" << std::endl;
411 std::cout << std::endl;
412
413 std::cout << " Element id before which there are 4 elements: " << cells.getSizeMarker(4) << std::endl;
414
415 // Done
416 std::cout << std::endl << "::: Done :::" << std::endl;
417 std::cout << std::endl;
418
419}
420
424int main(int argc, char *argv[])
425{
426#if BITPIT_ENABLE_MPI==1
427 MPI_Init(&argc,&argv);
428#else
429 BITPIT_UNUSED(argc);
430 BITPIT_UNUSED(argv);
431#endif
432
433 int nProcs;
434 int rank;
435#if BITPIT_ENABLE_MPI==1
436 MPI_Comm_size(MPI_COMM_WORLD, &nProcs);
437 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
438#else
439 nProcs = 1;
440 rank = 0;
441#endif
442
443 // Initialize the logger
444 log::manager().initialize(log::MODE_SEPARATE, false, nProcs, rank);
445 log::cout() << log::fileVerbosity(log::LEVEL_INFO);
447
448 // Run the example
449 try {
450 run();
451 } catch (const std::exception &exception) {
452 log::cout() << exception.what();
453 exit(1);
454 }
455
456#if BITPIT_ENABLE_MPI==1
457 MPI_Finalize();
458#endif
459}
void initialize(log::Mode mode, bool reset, int nProcesses, int rank)
Definition logger.cpp:1268
id_t getSizeMarker(std::size_t targetSize, const id_t &fallback=-1)
std::size_t capacity() const
std::size_t size() 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)
Definition compiler.hpp:63
Logger & cout(log::Level defaultSeverity, log::Visibility defaultVisibility)
Definition logger.cpp:1705
LoggerManipulator< log::Level > fileVerbosity(const log::Level &threshold)
Definition logger.cpp:2120
Logger & disableConsole(Logger &logger, const log::Level &level)
Definition logger.cpp:2165
LoggerManager & manager()
Definition logger.cpp:1685
--- layout: doxygen_footer ---