deal.II version GIT relicensing-7229-gca0444b871 2026-11-28 02:50:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
tria.cc
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 1999 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
17#include <deal.II/base/mpi.templates.h>
25
28
33#include <deal.II/grid/tria.h>
40
41#include <boost/archive/text_iarchive.hpp>
42#include <boost/archive/text_oarchive.hpp>
43
44#include <algorithm>
45#include <array>
46#include <cmath>
47#include <cstdint>
48#include <fstream>
49#include <functional>
50#include <limits>
51#include <list>
52#include <map>
53#include <memory>
54#include <numeric>
55#include <vector>
56
57
59
60
61namespace internal
62{
63 namespace TriangulationImplementation
64 {
66 : n_levels(0)
67 , n_lines(0)
68 , n_active_lines(0)
69 // all other fields are
70 // default constructed
71 {}
72
73
74
75 std::size_t
77 {
78 std::size_t mem =
83 MemoryConsumption::memory_consumption(n_active_lines_level);
84
85 if (active_cell_index_partitioner)
86 mem += active_cell_index_partitioner->memory_consumption();
87
88 for (const auto &partitioner : level_cell_index_partitioners)
89 if (partitioner)
90 mem += partitioner->memory_consumption();
91
92 return mem;
93 }
94
95
97 : n_quads(0)
98 , n_active_quads(0)
99 // all other fields are
100 // default constructed
101 {}
102
103
104
105 std::size_t
114
115
116
118 : n_hexes(0)
119 , n_active_hexes(0)
120 // all other fields are
121 // default constructed
122 {}
123
124
125
126 std::size_t
135
136
137
138 template <int dim>
140 : max_children_per_cell(numbers::invalid_unsigned_int)
141 , max_faces_per_cell(numbers::invalid_unsigned_int)
142 , max_lines_per_cell(numbers::invalid_unsigned_int)
143 , max_vertices_per_cell(numbers::invalid_unsigned_int)
144 , max_children_per_face(numbers::invalid_unsigned_int)
145 , max_lines_per_face(numbers::invalid_unsigned_int)
146 {}
147
148
149
150 template <int dim>
152 const std::vector<ReferenceCell<dim>> &reference_cells)
153 : max_children_per_cell(0u)
154 , max_faces_per_cell(0u)
155 , max_lines_per_cell(0u)
156 , max_vertices_per_cell(0u)
157 , max_children_per_face(0u)
158 , max_lines_per_face(0u)
159 {
160 for (const ReferenceCell<dim> &reference_cell : reference_cells)
161 {
164 reference_cell.n_isotropic_children());
166 std::max(max_faces_per_cell, reference_cell.n_faces());
168 std::max(max_lines_per_cell, reference_cell.n_lines());
170 std::max(max_vertices_per_cell, reference_cell.n_vertices());
171
172 for (unsigned int face_no : reference_cell.face_indices())
173 {
174 const auto face_reference_cell =
175 reference_cell.face_reference_cell(face_no);
178 face_reference_cell.n_isotropic_children());
180 std::max(max_lines_per_face, face_reference_cell.n_lines());
181 }
182 }
183 }
184 } // namespace TriangulationImplementation
185
186
187 template <int dim, int spacedim>
190 : variable_size_data_stored(false)
191 {}
192
193
194 template <int dim, int spacedim>
196 void CellAttachedDataSerializer<dim, spacedim>::pack_data(
197 const std::vector<cell_relation_t> &cell_relations,
198 const std::vector<
199 typename internal::CellAttachedData<dim, spacedim>::pack_callback_t>
200 &pack_callbacks_fixed,
201 const std::vector<
202 typename internal::CellAttachedData<dim, spacedim>::pack_callback_t>
203 &pack_callbacks_variable,
204 const MPI_Comm &mpi_communicator)
205 {
206 Assert(src_data_fixed.empty(),
207 ExcMessage("Previously packed data has not been released yet!"));
208 Assert(src_sizes_variable.empty(), ExcInternalError());
209
210 const unsigned int n_callbacks_fixed = pack_callbacks_fixed.size();
211 const unsigned int n_callbacks_variable = pack_callbacks_variable.size();
212
213 // Store information that we packed variable size data in
214 // a member variable for later.
215 variable_size_data_stored = (n_callbacks_variable > 0);
216
217 // If variable transfer is scheduled, we will store the data size that
218 // each variable size callback function writes in this auxiliary
219 // container. The information will be stored by each cell in this vector
220 // temporarily.
221 std::vector<unsigned int> cell_sizes_variable_cumulative(
222 n_callbacks_variable);
223
224 // Prepare the buffer structure, in which each callback function will
225 // store its data for each active cell.
226 // The outmost shell in this container construct corresponds to the
227 // data packed per cell. The next layer resembles the data that
228 // each callback function packs on the corresponding cell. These
229 // buffers are chains of chars stored in an std::vector<char>.
230 // A visualisation of the data structure:
231 /* clang-format off */
232 // | cell_1 | | cell_2 | ...
233 // || callback_1 || callback_2 |...| || callback_1 || callback_2 |...| ...
234 // |||char|char|...|||char|char|...|...| |||char|char|...|||char|char|...|...| ...
235 /* clang-format on */
236 std::vector<std::vector<std::vector<char>>> packed_fixed_size_data(
237 cell_relations.size());
238 std::vector<std::vector<std::vector<char>>> packed_variable_size_data(
239 variable_size_data_stored ? cell_relations.size() : 0);
240
241 //
242 // --------- Pack data for fixed and variable size transfer ---------
243 //
244 // Iterate over all cells, call all callback functions on each cell,
245 // and store their data in the corresponding buffer scope.
246 {
247 auto cell_rel_it = cell_relations.cbegin();
248 auto data_cell_fixed_it = packed_fixed_size_data.begin();
249 auto data_cell_variable_it = packed_variable_size_data.begin();
250 for (; cell_rel_it != cell_relations.cend(); ++cell_rel_it)
251 {
252 const auto &dealii_cell = cell_rel_it->first;
253 const auto &cell_status = cell_rel_it->second;
254
255 // Assertions about the tree structure.
256 switch (cell_status)
257 {
260 // double check the condition that we will only ever attach
261 // data to active cells when we get here
262 Assert(dealii_cell->is_active(), ExcInternalError());
263 break;
264
266 // double check the condition that we will only ever attach
267 // data to cells with children when we get here. however, we
268 // can only tolerate one level of coarsening at a time, so
269 // check that the children are all active
270 Assert(dealii_cell->is_active() == false, ExcInternalError());
271 for (unsigned int c = 0; c < dealii_cell->n_children(); ++c)
272 Assert(dealii_cell->child(c)->is_active(),
274 break;
275
277 // do nothing on invalid cells
278 break;
279
280 default:
282 break;
283 }
284
285 // Reserve memory corresponding to the number of callback
286 // functions that will be called.
287 // If variable size transfer is scheduled, we need to leave
288 // room for an array that holds information about how many
289 // bytes each of the variable size callback functions will
290 // write.
291 // On cells flagged with CellStatus::cell_invalid, only its CellStatus
292 // will be stored.
293 const unsigned int n_fixed_size_data_sets_on_cell =
294 1 + ((cell_status == CellStatus::cell_invalid) ?
295 0 :
296 ((variable_size_data_stored ? 1 : 0) + n_callbacks_fixed));
297 data_cell_fixed_it->resize(n_fixed_size_data_sets_on_cell);
298
299 // We continue with packing all data on this specific cell.
300 auto data_fixed_it = data_cell_fixed_it->begin();
301
302 // First, we pack the CellStatus information.
303 // to get consistent data sizes on each cell for the fixed size
304 // transfer, we won't allow compression
305 *data_fixed_it =
306 Utilities::pack(cell_status, /*allow_compression=*/false);
307 ++data_fixed_it;
308
309 // Proceed with all registered callback functions.
310 // Skip cells with the CellStatus::cell_invalid flag.
311 if (cell_status != CellStatus::cell_invalid)
312 {
313 // Pack fixed size data.
314 for (auto callback_it = pack_callbacks_fixed.cbegin();
315 callback_it != pack_callbacks_fixed.cend();
316 ++callback_it, ++data_fixed_it)
317 {
318 *data_fixed_it = (*callback_it)(dealii_cell, cell_status);
319 }
320
321 // Pack variable size data.
322 // If we store variable size data, we need to transfer
323 // the sizes of each corresponding callback function
324 // via fixed size transfer as well.
325 if (variable_size_data_stored)
326 {
327 const unsigned int n_variable_size_data_sets_on_cell =
328 ((cell_status == CellStatus::cell_invalid) ?
329 0 :
330 n_callbacks_variable);
331 data_cell_variable_it->resize(
332 n_variable_size_data_sets_on_cell);
333
334 auto callback_it = pack_callbacks_variable.cbegin();
335 auto data_variable_it = data_cell_variable_it->begin();
336 auto sizes_variable_it =
337 cell_sizes_variable_cumulative.begin();
338 for (; callback_it != pack_callbacks_variable.cend();
339 ++callback_it, ++data_variable_it, ++sizes_variable_it)
340 {
341 *data_variable_it =
342 (*callback_it)(dealii_cell, cell_status);
343
344 // Store data sizes for each callback function first.
345 // Make it cumulative below.
346 *sizes_variable_it = data_variable_it->size();
347 }
348
349 // Turn size vector into its cumulative representation.
350 std::partial_sum(cell_sizes_variable_cumulative.begin(),
351 cell_sizes_variable_cumulative.end(),
352 cell_sizes_variable_cumulative.begin());
353
354 // Serialize cumulative variable size vector
355 // value-by-value. This way we can circumvent the overhead
356 // of storing the container object as a whole, since we
357 // know its size by the number of registered callback
358 // functions.
359 data_fixed_it->resize(n_callbacks_variable *
360 sizeof(unsigned int));
361 for (unsigned int i = 0; i < n_callbacks_variable; ++i)
362 std::memcpy(&(data_fixed_it->at(i * sizeof(unsigned int))),
363 &(cell_sizes_variable_cumulative.at(i)),
364 sizeof(unsigned int));
365
366 ++data_fixed_it;
367 }
368
369 // Double check that we packed everything we wanted
370 // in the fixed size buffers.
371 Assert(data_fixed_it == data_cell_fixed_it->end(),
373 }
374
375 ++data_cell_fixed_it;
376
377 // Increment the variable size data iterator
378 // only if we actually pack this kind of data
379 // to avoid getting out of bounds.
380 if (variable_size_data_stored)
381 ++data_cell_variable_it;
382 } // loop over cell_relations
383 }
384
385 //
386 // ----------- Gather data sizes for fixed size transfer ------------
387 //
388 // Generate a vector which stores the sizes of each callback function,
389 // including the packed CellStatus transfer.
390 // Find the very first cell that we wrote to with all callback
391 // functions (i.e. a cell that was not flagged with
392 // CellStatus::cell_invalid) and store the sizes of each buffer.
393 //
394 // To deal with the case that at least one of the processors does not
395 // own any cell at all, we will exchange the information about the data
396 // sizes among them later. The code in between is still well-defined,
397 // since the following loops will be skipped.
398 std::vector<unsigned int> local_sizes_fixed(
399 1 + n_callbacks_fixed + (variable_size_data_stored ? 1 : 0));
400 for (const auto &data_cell : packed_fixed_size_data)
401 {
402 if (data_cell.size() == local_sizes_fixed.size())
403 {
404 auto sizes_fixed_it = local_sizes_fixed.begin();
405 auto data_fixed_it = data_cell.cbegin();
406 for (; data_fixed_it != data_cell.cend();
407 ++data_fixed_it, ++sizes_fixed_it)
408 {
409 *sizes_fixed_it = data_fixed_it->size();
410 }
411
412 break;
413 }
414 }
415
416 // Check if all cells have valid sizes.
417 for (auto data_cell_fixed_it = packed_fixed_size_data.cbegin();
418 data_cell_fixed_it != packed_fixed_size_data.cend();
419 ++data_cell_fixed_it)
420 {
421 Assert((data_cell_fixed_it->size() == 1) ||
422 (data_cell_fixed_it->size() == local_sizes_fixed.size()),
424 }
425
426 // Share information about the packed data sizes
427 // of all callback functions across all processors, in case one
428 // of them does not own any cells at all.
429 std::vector<unsigned int> global_sizes_fixed(local_sizes_fixed.size());
430 Utilities::MPI::max(local_sizes_fixed,
431 mpi_communicator,
432 global_sizes_fixed);
433
434 // Construct cumulative sizes, since this is the only information
435 // we need from now on.
436 sizes_fixed_cumulative.resize(global_sizes_fixed.size());
437 std::partial_sum(global_sizes_fixed.begin(),
438 global_sizes_fixed.end(),
439 sizes_fixed_cumulative.begin());
440
441 //
442 // ---------- Gather data sizes for variable size transfer ----------
443 //
444 if (variable_size_data_stored)
445 {
446 src_sizes_variable.reserve(packed_variable_size_data.size());
447 for (const auto &data_cell : packed_variable_size_data)
448 {
449 int variable_data_size_on_cell = 0;
450
451 for (const auto &data : data_cell)
452 variable_data_size_on_cell += data.size();
453
454 src_sizes_variable.push_back(variable_data_size_on_cell);
455 }
456 }
457
458 //
459 // ------------------------ Build buffers ---------------------------
460 //
461 const unsigned int expected_size_fixed =
462 cell_relations.size() * sizes_fixed_cumulative.back();
463 const unsigned int expected_size_variable =
464 std::accumulate(src_sizes_variable.begin(),
465 src_sizes_variable.end(),
466 std::vector<int>::size_type(0));
467
468 // Move every piece of packed fixed size data into the consecutive
469 // buffer.
470 src_data_fixed.reserve(expected_size_fixed);
471 for (const auto &data_cell_fixed : packed_fixed_size_data)
472 {
473 // Move every fraction of packed data into the buffer
474 // reserved for this particular cell.
475 for (const auto &data_fixed : data_cell_fixed)
476 std::move(data_fixed.begin(),
477 data_fixed.end(),
478 std::back_inserter(src_data_fixed));
479
480 // If we only packed the CellStatus information
481 // (i.e. encountered a cell flagged CellStatus::cell_invalid),
482 // fill the remaining space with invalid entries.
483 // We can skip this if there is nothing else to pack.
484 if ((data_cell_fixed.size() == 1) &&
485 (sizes_fixed_cumulative.size() > 1))
486 {
487 const std::size_t bytes_skipped =
488 sizes_fixed_cumulative.back() - sizes_fixed_cumulative.front();
489
490 src_data_fixed.insert(src_data_fixed.end(),
491 bytes_skipped,
492 static_cast<char>(-1)); // invalid_char
493 }
494 }
495
496 // Move every piece of packed variable size data into the consecutive
497 // buffer.
498 if (variable_size_data_stored)
499 {
500 src_data_variable.reserve(expected_size_variable);
501 for (const auto &data_cell : packed_variable_size_data)
502 {
503 // Move every fraction of packed data into the buffer
504 // reserved for this particular cell.
505 for (const auto &data : data_cell)
506 std::move(data.begin(),
507 data.end(),
508 std::back_inserter(src_data_variable));
509 }
510 }
511
512 // Double check that we packed everything correctly.
513 Assert(src_data_fixed.size() == expected_size_fixed, ExcInternalError());
514 Assert(src_data_variable.size() == expected_size_variable,
516 }
517
518
519
520 template <int dim, int spacedim>
522 void CellAttachedDataSerializer<dim, spacedim>::unpack_cell_status(
523 std::vector<
524 typename CellAttachedDataSerializer<dim, spacedim>::cell_relation_t>
525 &cell_relations) const
526 {
527 Assert(sizes_fixed_cumulative.size() > 0,
528 ExcMessage("No data has been packed!"));
529 if (cell_relations.size() > 0)
530 {
531 Assert(dest_data_fixed.size() > 0,
532 ExcMessage("No data has been received!"));
533 }
534
535 // Size of CellStatus object that will be unpacked on each cell.
536 const unsigned int size = sizes_fixed_cumulative.front();
537
538 // Iterate over all cells and overwrite the CellStatus
539 // information from the transferred data.
540 // Proceed buffer iterator position to next cell after
541 // each iteration.
542 auto cell_rel_it = cell_relations.begin();
543 auto dest_fixed_it = dest_data_fixed.cbegin();
544 for (; cell_rel_it != cell_relations.end();
545 ++cell_rel_it, dest_fixed_it += sizes_fixed_cumulative.back())
546 {
547 cell_rel_it->second = // cell_status
548 Utilities::unpack<CellStatus>(dest_fixed_it,
549 dest_fixed_it + size,
550 /*allow_compression=*/false);
551 }
552 }
553
554
556 template <int dim, int spacedim>
558 void CellAttachedDataSerializer<dim, spacedim>::unpack_data(
559 const std::vector<
560 typename CellAttachedDataSerializer<dim, spacedim>::cell_relation_t>
561 &cell_relations,
562 const unsigned int handle,
563 const std::function<
564 void(const cell_iterator &,
565 const CellStatus &,
566 const boost::iterator_range<std::vector<char>::const_iterator> &)>
567 &unpack_callback) const
568 {
569 // We decode the handle returned by register_data_attach() back into
570 // a format we can use. All even handles belong to those callback
571 // functions which write/read variable size data, all odd handles
572 // interact with fixed size buffers.
573 const bool callback_variable_transfer = (handle % 2 == 0);
574 const unsigned int callback_index = handle / 2;
575
576 // Cells will always receive fixed size data (i.e., CellStatus
577 // information), but not necessarily variable size data (e.g., with a
578 // ParticleHandler a cell might not contain any particle at all).
579 // Thus it is sufficient to check if fixed size data has been received.
580 Assert(sizes_fixed_cumulative.size() > 0,
581 ExcMessage("No data has been packed!"));
582 if (cell_relations.size() > 0)
583 {
584 Assert(dest_data_fixed.size() > 0,
585 ExcMessage("No data has been received!"));
586 }
587
588 std::vector<char>::const_iterator dest_data_it;
589 std::vector<char>::const_iterator dest_sizes_cell_it;
590
591 // Depending on whether our callback function unpacks fixed or
592 // variable size data, we have to pursue different approaches
593 // to localize the correct fraction of the buffer from which
594 // we are allowed to read.
595 unsigned int offset = numbers::invalid_unsigned_int;
596 unsigned int size = numbers::invalid_unsigned_int;
597 unsigned int data_increment = numbers::invalid_unsigned_int;
598
599 if (callback_variable_transfer)
600 {
601 // For the variable size data, we need to extract the
602 // data size from the fixed size buffer on each cell.
603 //
604 // We packed this information last, so the last packed
605 // object in the fixed size buffer corresponds to the
606 // variable data sizes.
607 //
608 // The last entry of sizes_fixed_cumulative corresponds
609 // to the size of all fixed size data packed on the cell.
610 // To get the offset for the last packed object, we need
611 // to get the next-to-last entry.
612 const unsigned int offset_variable_data_sizes =
613 sizes_fixed_cumulative[sizes_fixed_cumulative.size() - 2];
614
615 // This iterator points to the data size that the
616 // callback_function packed for each specific cell.
617 // Adjust buffer iterator to the offset of the callback
618 // function so that we only have to advance its position
619 // to the next cell after each iteration.
620 dest_sizes_cell_it = dest_data_fixed.cbegin() +
621 offset_variable_data_sizes +
622 callback_index * sizeof(unsigned int);
623
624 // Let the data iterator point to the correct buffer.
625 dest_data_it = dest_data_variable.cbegin();
626 }
627 else
628 {
629 // For the fixed size data, we can get the information about
630 // the buffer location on each cell directly from the
631 // sizes_fixed_cumulative vector.
632 offset = sizes_fixed_cumulative[callback_index];
633 size = sizes_fixed_cumulative[callback_index + 1] - offset;
634 data_increment = sizes_fixed_cumulative.back();
635
636 // Let the data iterator point to the correct buffer.
637 // Adjust buffer iterator to the offset of the callback
638 // function so that we only have to advance its position
639 // to the next cell after each iteration.
640 if (cell_relations.begin() != cell_relations.end())
641 dest_data_it = dest_data_fixed.cbegin() + offset;
642 }
643
644 // Iterate over all cells and unpack the transferred data.
645 auto cell_rel_it = cell_relations.begin();
646 auto dest_sizes_it = dest_sizes_variable.cbegin();
647 for (; cell_rel_it != cell_relations.end(); ++cell_rel_it)
648 {
649 const auto &dealii_cell = cell_rel_it->first;
650 const auto &cell_status = cell_rel_it->second;
651
652 if (callback_variable_transfer)
653 {
654 // Update the increment according to the whole data size
655 // of the current cell.
656 data_increment = *dest_sizes_it;
657
658 if (cell_status != CellStatus::cell_invalid)
659 {
660 // Extract the corresponding values for offset and size from
661 // the cumulative sizes array stored in the fixed size
662 // buffer.
663 if (callback_index == 0)
664 offset = 0;
665 else
666 std::memcpy(&offset,
667 &(*(dest_sizes_cell_it - sizeof(unsigned int))),
668 sizeof(unsigned int));
669
670 std::memcpy(&size,
671 &(*dest_sizes_cell_it),
672 sizeof(unsigned int));
673
674 size -= offset;
675
676 // Move the data iterator to the corresponding position
677 // of the callback function and adjust the increment
678 // accordingly.
679 dest_data_it += offset;
680 data_increment -= offset;
681 }
682
683 // Advance data size iterators to the next cell, avoid iterating
684 // past the end of dest_sizes_cell_it
685 if (cell_rel_it != cell_relations.end() - 1)
686 dest_sizes_cell_it += sizes_fixed_cumulative.back();
687 ++dest_sizes_it;
688 }
689
690 switch (cell_status)
691 {
694 unpack_callback(dealii_cell,
695 cell_status,
696 boost::make_iterator_range(dest_data_it,
697 dest_data_it + size));
698 break;
699
701 unpack_callback(dealii_cell->parent(),
702 cell_status,
703 boost::make_iterator_range(dest_data_it,
704 dest_data_it + size));
705 break;
706
708 // Skip this cell.
709 break;
710
711 default:
713 break;
714 }
715
716 if (cell_rel_it != cell_relations.end() - 1)
717 dest_data_it += data_increment;
718 }
719 }
720
721
722
723 template <int dim, int spacedim>
725 void CellAttachedDataSerializer<dim, spacedim>::save(
726 const unsigned int global_first_cell,
727 const unsigned int global_num_cells,
728 const std::string &file_basename,
729 const MPI_Comm &mpi_communicator) const
730 {
731 Assert(sizes_fixed_cumulative.size() > 0,
732 ExcMessage("No data has been packed!"));
733
734#ifdef DEAL_II_WITH_MPI
735 // Large fractions of this function have been copied from
736 // DataOutInterface::write_vtu_in_parallel.
737 // TODO: Write general MPIIO interface.
738
739 const unsigned int myrank =
740 Utilities::MPI::this_mpi_process(mpi_communicator);
741 const unsigned int mpisize =
742 Utilities::MPI::n_mpi_processes(mpi_communicator);
743
744 if (mpisize > 1)
745 {
746 const unsigned int bytes_per_cell = sizes_fixed_cumulative.back();
747
748 //
749 // ---------- Fixed size data ----------
750 //
751 {
752 const std::string fname_fixed =
753 std::string(file_basename) + "_fixed.data";
754
755 MPI_Info info;
756 int ierr = MPI_Info_create(&info);
757 AssertThrowMPI(ierr);
758
759 MPI_File fh;
760 ierr = MPI_File_open(mpi_communicator,
761 fname_fixed.c_str(),
762 MPI_MODE_CREATE | MPI_MODE_WRONLY,
763 info,
764 &fh);
765 AssertThrowMPI(ierr);
766
767 ierr = MPI_File_set_size(fh, 0); // delete the file contents
768 AssertThrowMPI(ierr);
769 // this barrier is necessary, because otherwise others might already
770 // write while one core is still setting the size to zero.
771 ierr = MPI_Barrier(mpi_communicator);
772 AssertThrowMPI(ierr);
773 ierr = MPI_Info_free(&info);
774 AssertThrowMPI(ierr);
775 // ------------------
776
777 // Write cumulative sizes to file.
778 // Since each processor owns the same information about the data
779 // sizes, it is sufficient to let only the first processor perform
780 // this task.
781 if (myrank == 0)
782 {
784 fh,
785 0,
786 sizes_fixed_cumulative.data(),
787 sizes_fixed_cumulative.size(),
788 MPI_UNSIGNED,
789 MPI_STATUS_IGNORE);
790 AssertThrowMPI(ierr);
791 }
792
793 // Write packed data to file simultaneously.
794 const MPI_Offset size_header =
795 sizes_fixed_cumulative.size() * sizeof(unsigned int);
796
797 // Make sure we do the following computation in 64bit integers to be
798 // able to handle 4GB+ files:
799 const MPI_Offset my_global_file_position =
800 size_header +
801 static_cast<MPI_Offset>(global_first_cell) * bytes_per_cell;
802
803 ierr =
805 my_global_file_position,
806 src_data_fixed.data(),
807 src_data_fixed.size(),
808 MPI_BYTE,
809 MPI_STATUS_IGNORE);
810 AssertThrowMPI(ierr);
811
812 ierr = MPI_File_close(&fh);
813 AssertThrowMPI(ierr);
814 }
815
816
817
818 //
819 // ---------- Variable size data ----------
820 //
821 if (variable_size_data_stored)
822 {
823 const std::string fname_variable =
824 std::string(file_basename) + "_variable.data";
825
826 MPI_Info info;
827 int ierr = MPI_Info_create(&info);
828 AssertThrowMPI(ierr);
829
830 MPI_File fh;
831 ierr = MPI_File_open(mpi_communicator,
832 fname_variable.c_str(),
833 MPI_MODE_CREATE | MPI_MODE_WRONLY,
834 info,
835 &fh);
836 AssertThrowMPI(ierr);
837
838 ierr = MPI_File_set_size(fh, 0); // delete the file contents
839 AssertThrowMPI(ierr);
840 // this barrier is necessary, because otherwise others might already
841 // write while one core is still setting the size to zero.
842 ierr = MPI_Barrier(mpi_communicator);
843 AssertThrowMPI(ierr);
844 ierr = MPI_Info_free(&info);
845 AssertThrowMPI(ierr);
846
847 // Write sizes of each cell into file simultaneously.
848 {
849 const MPI_Offset my_global_file_position =
850 static_cast<MPI_Offset>(global_first_cell) *
851 sizeof(unsigned int);
852
853 // It is very unlikely that a single process has more than
854 // 2 billion cells, but we might as well check.
855 AssertThrow(src_sizes_variable.size() <
856 static_cast<std::size_t>(
857 std::numeric_limits<int>::max()),
859
861 fh,
862 my_global_file_position,
863 src_sizes_variable.data(),
864 src_sizes_variable.size(),
865 MPI_INT,
866 MPI_STATUS_IGNORE);
867 AssertThrowMPI(ierr);
868 }
869
870 // Gather size of data in bytes we want to store from this
871 // processor and compute the prefix sum. We do this in 64 bit
872 // to avoid overflow for files larger than 4GB:
873 const std::uint64_t size_on_proc = src_data_variable.size();
874 std::uint64_t prefix_sum = 0;
875 ierr = MPI_Exscan(&size_on_proc,
876 &prefix_sum,
877 1,
878 MPI_UINT64_T,
879 MPI_SUM,
880 mpi_communicator);
881 AssertThrowMPI(ierr);
882
883 const MPI_Offset my_global_file_position =
884 static_cast<MPI_Offset>(global_num_cells) * sizeof(unsigned int) +
885 prefix_sum;
886
887 // Write data consecutively into file.
889 fh,
890 my_global_file_position,
891 src_data_variable.data(),
892 src_data_variable.size(),
893 MPI_BYTE,
894 MPI_STATUS_IGNORE);
895 AssertThrowMPI(ierr);
896
897
898 ierr = MPI_File_close(&fh);
899 AssertThrowMPI(ierr);
900 }
901 } // if (mpisize > 1)
902 else
903#endif
904 {
905 (void)global_first_cell;
906 (void)global_num_cells;
907 (void)mpi_communicator;
908
909 //
910 // ---------- Fixed size data ----------
911 //
912 {
913 const std::string fname_fixed =
914 std::string(file_basename) + "_fixed.data";
915
916 std::ofstream file(fname_fixed, std::ios::binary | std::ios::out);
917 AssertThrow(file.fail() == false, ExcIO());
918
919 // Write header data.
920 file.write(reinterpret_cast<const char *>(
921 sizes_fixed_cumulative.data()),
922 sizes_fixed_cumulative.size() * sizeof(unsigned int));
923
924 // Write packed data.
925 file.write(reinterpret_cast<const char *>(src_data_fixed.data()),
926 src_data_fixed.size() * sizeof(char));
927 }
928
929 //
930 // ---------- Variable size data ----------
931 //
932 if (variable_size_data_stored)
933 {
934 const std::string fname_variable =
935 std::string(file_basename) + "_variable.data";
936
937 std::ofstream file(fname_variable,
938 std::ios::binary | std::ios::out);
939 AssertThrow(file.fail() == false, ExcIO());
940
941 // Write header data.
942 file.write(reinterpret_cast<const char *>(
943 src_sizes_variable.data()),
944 src_sizes_variable.size() * sizeof(int));
945
946 // Write packed data.
947 file.write(reinterpret_cast<const char *>(src_data_variable.data()),
948 src_data_variable.size() * sizeof(char));
949 }
950 }
951 }
952
953
954 template <int dim, int spacedim>
956 void CellAttachedDataSerializer<dim, spacedim>::load(
957 const unsigned int global_first_cell,
958 const unsigned int global_num_cells,
959 const unsigned int local_num_cells,
960 const std::string &file_basename,
961 const unsigned int n_attached_deserialize_fixed,
962 const unsigned int n_attached_deserialize_variable,
963 const MPI_Comm &mpi_communicator)
964 {
965 Assert(dest_data_fixed.empty(),
966 ExcMessage("Previously loaded data has not been released yet!"));
967
968 variable_size_data_stored = (n_attached_deserialize_variable > 0);
969
970#ifdef DEAL_II_WITH_MPI
971 // Large fractions of this function have been copied from
972 // DataOutInterface::write_vtu_in_parallel.
973 // TODO: Write general MPIIO interface.
974
975 const unsigned int mpisize =
976 Utilities::MPI::n_mpi_processes(mpi_communicator);
977
978 if (mpisize > 1)
979 {
980 //
981 // ---------- Fixed size data ----------
982 //
983 {
984 const std::string fname_fixed =
985 std::string(file_basename) + "_fixed.data";
986
987 MPI_Info info;
988 int ierr = MPI_Info_create(&info);
989 AssertThrowMPI(ierr);
990
991 MPI_File fh;
992 ierr = MPI_File_open(
993 mpi_communicator, fname_fixed.c_str(), MPI_MODE_RDONLY, info, &fh);
994 AssertThrowMPI(ierr);
995
996 ierr = MPI_Info_free(&info);
997 AssertThrowMPI(ierr);
998
999 // Read cumulative sizes from file.
1000 // Since all processors need the same information about the data
1001 // sizes, let each of them retrieve it by reading from the same
1002 // location in the file.
1003 sizes_fixed_cumulative.resize(1 + n_attached_deserialize_fixed +
1004 (variable_size_data_stored ? 1 : 0));
1006 fh,
1007 0,
1008 sizes_fixed_cumulative.data(),
1009 sizes_fixed_cumulative.size(),
1010 MPI_UNSIGNED,
1011 MPI_STATUS_IGNORE);
1012 AssertThrowMPI(ierr);
1013
1014 // Allocate sufficient memory.
1015 const unsigned int bytes_per_cell = sizes_fixed_cumulative.back();
1016 dest_data_fixed.resize(static_cast<std::size_t>(local_num_cells) *
1017 bytes_per_cell);
1018
1019 // Read packed data from file simultaneously.
1020 const MPI_Offset size_header =
1021 sizes_fixed_cumulative.size() * sizeof(unsigned int);
1022
1023 // Make sure we do the following computation in 64bit integers to be
1024 // able to handle 4GB+ files:
1025 const MPI_Offset my_global_file_position =
1026 size_header +
1027 static_cast<MPI_Offset>(global_first_cell) * bytes_per_cell;
1028
1029 ierr =
1031 my_global_file_position,
1032 dest_data_fixed.data(),
1033 dest_data_fixed.size(),
1034 MPI_BYTE,
1035 MPI_STATUS_IGNORE);
1036 AssertThrowMPI(ierr);
1037
1038
1039 ierr = MPI_File_close(&fh);
1040 AssertThrowMPI(ierr);
1041 }
1042
1043 //
1044 // ---------- Variable size data ----------
1045 //
1046 if (variable_size_data_stored)
1047 {
1048 const std::string fname_variable =
1049 std::string(file_basename) + "_variable.data";
1050
1051 MPI_Info info;
1052 int ierr = MPI_Info_create(&info);
1053 AssertThrowMPI(ierr);
1054
1055 MPI_File fh;
1056 ierr = MPI_File_open(mpi_communicator,
1057 fname_variable.c_str(),
1058 MPI_MODE_RDONLY,
1059 info,
1060 &fh);
1061 AssertThrowMPI(ierr);
1062
1063 ierr = MPI_Info_free(&info);
1064 AssertThrowMPI(ierr);
1065
1066 // Read sizes of all locally owned cells.
1067 dest_sizes_variable.resize(local_num_cells);
1068
1069 const MPI_Offset my_global_file_position_sizes =
1070 static_cast<MPI_Offset>(global_first_cell) * sizeof(unsigned int);
1071
1073 fh,
1074 my_global_file_position_sizes,
1075 dest_sizes_variable.data(),
1076 dest_sizes_variable.size(),
1077 MPI_INT,
1078 MPI_STATUS_IGNORE);
1079 AssertThrowMPI(ierr);
1080
1081
1082 // Compute my data size in bytes and compute prefix sum. We do this
1083 // in 64 bit to avoid overflow for files larger than 4 GB:
1084 const std::uint64_t size_on_proc =
1085 std::accumulate(dest_sizes_variable.begin(),
1086 dest_sizes_variable.end(),
1087 0ULL);
1088
1089 std::uint64_t prefix_sum = 0;
1090 ierr = MPI_Exscan(&size_on_proc,
1091 &prefix_sum,
1092 1,
1093 MPI_UINT64_T,
1094 MPI_SUM,
1095 mpi_communicator);
1096 AssertThrowMPI(ierr);
1097
1098 const MPI_Offset my_global_file_position =
1099 static_cast<MPI_Offset>(global_num_cells) * sizeof(unsigned int) +
1100 prefix_sum;
1101
1102 dest_data_variable.resize(size_on_proc);
1103
1105 fh,
1106 my_global_file_position,
1107 dest_data_variable.data(),
1108 dest_data_variable.size(),
1109 MPI_BYTE,
1110 MPI_STATUS_IGNORE);
1111 AssertThrowMPI(ierr);
1112
1113 ierr = MPI_File_close(&fh);
1114 AssertThrowMPI(ierr);
1115 }
1116 }
1117 else // if (mpisize > 1)
1118#endif
1119 {
1120 (void)mpi_communicator;
1121 (void)global_first_cell;
1122 (void)global_num_cells;
1123
1124 //
1125 // ---------- Fixed size data ----------
1126 //
1127 {
1128 const std::string fname_fixed =
1129 std::string(file_basename) + "_fixed.data";
1130
1131 std::ifstream file(fname_fixed, std::ios::binary | std::ios::in);
1132 AssertThrow(file.fail() == false, ExcIO());
1133
1134 sizes_fixed_cumulative.resize(1 + n_attached_deserialize_fixed +
1135 (variable_size_data_stored ? 1 : 0));
1136 // Read header data.
1137 file.read(reinterpret_cast<char *>(sizes_fixed_cumulative.data()),
1138 sizes_fixed_cumulative.size() * sizeof(unsigned int));
1139
1140 const unsigned int bytes_per_cell = sizes_fixed_cumulative.back();
1141 dest_data_fixed.resize(static_cast<std::size_t>(local_num_cells) *
1142 bytes_per_cell);
1143
1144 // Read packed data.
1145 file.read(reinterpret_cast<char *>(dest_data_fixed.data()),
1146 dest_data_fixed.size() * sizeof(char));
1147 }
1148
1149 //
1150 // ---------- Variable size data ----------
1151 //
1152 if (variable_size_data_stored)
1153 {
1154 const std::string fname_variable =
1155 std::string(file_basename) + "_variable.data";
1156
1157 std::ifstream file(fname_variable, std::ios::binary | std::ios::in);
1158 AssertThrow(file.fail() == false, ExcIO());
1159
1160 // Read header data.
1161 dest_sizes_variable.resize(local_num_cells);
1162 file.read(reinterpret_cast<char *>(dest_sizes_variable.data()),
1163 dest_sizes_variable.size() * sizeof(int));
1164
1165 // Read packed data.
1166 const std::uint64_t size =
1167 std::accumulate(dest_sizes_variable.begin(),
1168 dest_sizes_variable.end(),
1169 0ULL);
1170 dest_data_variable.resize(size);
1171 file.read(reinterpret_cast<char *>(dest_data_variable.data()),
1172 dest_data_variable.size() * sizeof(char));
1173 }
1174 }
1175 }
1176
1177
1178 template <int dim, int spacedim>
1180 void CellAttachedDataSerializer<dim, spacedim>::clear()
1181 {
1182 variable_size_data_stored = false;
1183
1184 // free information about data sizes
1185 sizes_fixed_cumulative.clear();
1186 sizes_fixed_cumulative.shrink_to_fit();
1187
1188 // free fixed size transfer data
1189 src_data_fixed.clear();
1190 src_data_fixed.shrink_to_fit();
1191
1192 dest_data_fixed.clear();
1193 dest_data_fixed.shrink_to_fit();
1194
1195 // free variable size transfer data
1196 src_sizes_variable.clear();
1197 src_sizes_variable.shrink_to_fit();
1198
1199 src_data_variable.clear();
1200 src_data_variable.shrink_to_fit();
1201
1202 dest_sizes_variable.clear();
1203 dest_sizes_variable.shrink_to_fit();
1204
1205 dest_data_variable.clear();
1206 dest_data_variable.shrink_to_fit();
1207 }
1208
1209} // namespace internal
1210
1211// anonymous namespace for internal helper functions
1212namespace
1213{
1214 // return whether the given cell is
1215 // patch_level_1, i.e. determine
1216 // whether either all or none of
1217 // its children are further
1218 // refined. this function can only
1219 // be called for non-active cells.
1220 template <int dim, int spacedim>
1221 bool
1222 cell_is_patch_level_1(
1224 {
1225 Assert(cell->is_active() == false, ExcInternalError());
1226
1227 unsigned int n_active_children = 0;
1228 for (unsigned int i = 0; i < cell->n_children(); ++i)
1229 if (cell->child(i)->is_active())
1230 ++n_active_children;
1231
1232 return (n_active_children == 0) ||
1233 (n_active_children == cell->n_children());
1234 }
1235
1236
1237
1238 // return, whether a given @p cell will be
1239 // coarsened, which is the case if all
1240 // children are active and have their coarsen
1241 // flag set. In case only part of the coarsen
1242 // flags are set, remove them.
1243 template <int dim, int spacedim>
1244 bool
1245 cell_will_be_coarsened(
1247 {
1248 // only cells with children should be
1249 // considered for coarsening
1250
1251 if (cell->has_children())
1252 {
1253 unsigned int children_to_coarsen = 0;
1254 const unsigned int n_children = cell->n_children();
1255
1256 for (unsigned int c = 0; c < n_children; ++c)
1257 if (cell->child(c)->is_active() && cell->child(c)->coarsen_flag_set())
1258 ++children_to_coarsen;
1259 if (children_to_coarsen == n_children)
1260 return true;
1261 else
1262 for (unsigned int c = 0; c < n_children; ++c)
1263 if (cell->child(c)->is_active())
1264 cell->child(c)->clear_coarsen_flag();
1265 }
1266 // no children, so no coarsening
1267 // possible. however, no children also
1268 // means that this cell will be in the same
1269 // state as if it had children and was
1270 // coarsened. So, what should we return -
1271 // false or true?
1272 // make sure we do not have to do this at
1273 // all...
1274 Assert(cell->has_children(), ExcInternalError());
1275 // ... and then simply return false
1276 return false;
1277 }
1278
1279
1280 // return, whether the face @p face_no of the
1281 // given @p cell will be refined after the
1282 // current refinement step, considering
1283 // refine and coarsen flags and considering
1284 // only those refinemnts that will be caused
1285 // by the neighboring cell.
1286
1287 // this function is used on both active cells
1288 // and cells with children. on cells with
1289 // children it also of interest to know 'how'
1290 // the face will be refined. thus there is an
1291 // additional third argument @p
1292 // expected_face_ref_case returning just
1293 // that. be aware, that this variable will
1294 // only contain useful information if this
1295 // function is called for an active cell.
1296 //
1297 // thus, this is an internal function, users
1298 // should call one of the two alternatives
1299 // following below.
1300 template <int dim, int spacedim>
1301 bool
1302 face_will_be_refined_by_neighbor_internal(
1304 const unsigned int face_no,
1305 RefinementCase<dim - 1> &expected_face_ref_case)
1306 {
1307 // first of all: set the default value for
1308 // expected_face_ref_case, which is no
1309 // refinement at all
1310 expected_face_ref_case = RefinementCase<dim - 1>::no_refinement;
1311
1312 const typename Triangulation<dim, spacedim>::cell_iterator neighbor =
1313 cell->neighbor(face_no);
1314
1315 // If we are at the boundary, there is no
1316 // neighbor which could refine the face
1317 if (neighbor.state() != IteratorState::valid)
1318 return false;
1319
1320 if (neighbor->has_children())
1321 {
1322 // if the neighbor is refined, it may be
1323 // coarsened. if so, then it won't refine
1324 // the face, no matter what else happens
1325 if (cell_will_be_coarsened(neighbor))
1326 return false;
1327 else
1328 // if the neighbor is refined, then it
1329 // is also refined at our current
1330 // face. It will stay so without
1331 // coarsening, so return true in that
1332 // case.
1333 {
1334 expected_face_ref_case = cell->face(face_no)->refinement_case();
1335 return true;
1336 }
1337 }
1338
1339 // now, the neighbor is not refined, but
1340 // perhaps it will be
1341 const RefinementCase<dim> nb_ref_flag = neighbor->refine_flag_set();
1342 if (nb_ref_flag != RefinementCase<dim>::no_refinement)
1343 {
1344 // now we need to know, which of the
1345 // neighbors faces points towards us
1346 const unsigned int neighbor_neighbor = cell->neighbor_face_no(face_no);
1347 // check, whether the cell will be
1348 // refined in a way that refines our
1349 // face
1350 const RefinementCase<dim - 1> face_ref_case =
1352 nb_ref_flag,
1353 neighbor_neighbor,
1354 neighbor->face_orientation(neighbor_neighbor),
1355 neighbor->face_flip(neighbor_neighbor),
1356 neighbor->face_rotation(neighbor_neighbor));
1357 if (face_ref_case != RefinementCase<dim - 1>::no_refinement)
1358 {
1360 neighbor_face = neighbor->face(neighbor_neighbor);
1361 const int this_face_index = cell->face_index(face_no);
1362
1363 // there are still two basic
1364 // possibilities here: the neighbor
1365 // might be coarser or as coarse
1366 // as we are
1367 if (neighbor_face->index() == this_face_index)
1368 // the neighbor is as coarse as
1369 // we are and will be refined at
1370 // the face of consideration, so
1371 // return true
1372 {
1373 expected_face_ref_case = face_ref_case;
1374 return true;
1375 }
1376 else
1377 {
1378 // the neighbor is coarser.
1379 // this is the most complicated
1380 // case. It might be, that the
1381 // neighbor's face will be
1382 // refined, but that we will
1383 // not see this, as we are
1384 // refined in a similar way.
1385
1386 // so, the neighbor's face must
1387 // have children. check, if our
1388 // cell's face is one of these
1389 // (it could also be a
1390 // grand_child)
1391 for (unsigned int c = 0; c < neighbor_face->n_children(); ++c)
1392 if (neighbor_face->child_index(c) == this_face_index)
1393 {
1394 // if the flagged refine
1395 // case of the face is a
1396 // subset or the same as
1397 // the current refine case,
1398 // then the face, as seen
1399 // from our cell, won't be
1400 // refined by the neighbor
1401 if ((neighbor_face->refinement_case() | face_ref_case) ==
1402 neighbor_face->refinement_case())
1403 return false;
1404 else
1405 {
1406 // if we are active, we
1407 // must be an
1408 // anisotropic child
1409 // and the coming
1410 // face_ref_case is
1411 // isotropic. Thus,
1412 // from our cell we
1413 // will see exactly the
1414 // opposite refine case
1415 // that the face has
1416 // now...
1417 Assert(
1418 face_ref_case ==
1421 expected_face_ref_case =
1422 ~neighbor_face->refinement_case();
1423 return true;
1424 }
1425 }
1426
1427 // so, obviously we were not
1428 // one of the children, but a
1429 // grandchild. This is only
1430 // possible in 3d.
1431 Assert(dim == 3, ExcInternalError());
1432 // In that case, however, no
1433 // matter what the neighbor
1434 // does, it won't be finer
1435 // after the next refinement
1436 // step.
1437 return false;
1438 }
1439 } // if face will be refined
1440 } // if neighbor is flagged for refinement
1441
1442 // no cases left, so the neighbor will not
1443 // refine the face
1444 return false;
1445 }
1446
1447 // version of above function for both active
1448 // and non-active cells
1449 template <int dim, int spacedim>
1450 bool
1451 face_will_be_refined_by_neighbor(
1453 const unsigned int face_no)
1454 {
1455 RefinementCase<dim - 1> dummy = RefinementCase<dim - 1>::no_refinement;
1456 return face_will_be_refined_by_neighbor_internal(cell, face_no, dummy);
1457 }
1458
1459 // version of above function for active cells
1460 // only. Additionally returning the refine
1461 // case (to come) of the face under
1462 // consideration
1463 template <int dim, int spacedim>
1464 bool
1465 face_will_be_refined_by_neighbor(
1467 const unsigned int face_no,
1468 RefinementCase<dim - 1> &expected_face_ref_case)
1469 {
1470 return face_will_be_refined_by_neighbor_internal(cell,
1471 face_no,
1472 expected_face_ref_case);
1473 }
1474
1475
1476
1477 template <int dim, int spacedim>
1478 bool
1479 satisfies_level1_at_vertex_rule(
1480 const Triangulation<dim, spacedim> &triangulation)
1481 {
1482 std::vector<unsigned int> min_adjacent_cell_level(
1483 triangulation.n_vertices(), triangulation.n_levels());
1484 std::vector<unsigned int> max_adjacent_cell_level(
1485 triangulation.n_vertices(), 0);
1486
1487 for (const auto &cell : triangulation.active_cell_iterators())
1488 for (const unsigned int v : cell->vertex_indices())
1489 {
1490 min_adjacent_cell_level[cell->vertex_index(v)] =
1491 std::min<unsigned int>(
1492 min_adjacent_cell_level[cell->vertex_index(v)], cell->level());
1493 max_adjacent_cell_level[cell->vertex_index(v)] =
1494 std::max<unsigned int>(
1495 min_adjacent_cell_level[cell->vertex_index(v)], cell->level());
1496 }
1497
1498 for (unsigned int k = 0; k < triangulation.n_vertices(); ++k)
1499 if (triangulation.vertex_used(k))
1500 if (max_adjacent_cell_level[k] - min_adjacent_cell_level[k] > 1)
1501 return false;
1502 return true;
1503 }
1504
1505
1506
1524 template <int dim, int spacedim>
1525 unsigned int
1526 middle_vertex_index(
1528 {
1529 if (line->has_children())
1530 return line->child(0)->vertex_index(1);
1532 }
1533
1534
1535 template <int dim, int spacedim>
1536 unsigned int
1537 middle_vertex_index(
1539 {
1540 switch (static_cast<unsigned char>(quad->refinement_case()))
1541 {
1543 return middle_vertex_index<dim, spacedim>(quad->child(0)->line(1));
1544 break;
1546 return middle_vertex_index<dim, spacedim>(quad->child(0)->line(3));
1547 break;
1549 return quad->child(0)->vertex_index(3);
1550 break;
1551 default:
1552 break;
1553 }
1555 }
1556
1557
1558
1571 template <class TRIANGULATION>
1572 typename TRIANGULATION::DistortedCellList
1573 collect_distorted_coarse_cells(const TRIANGULATION &)
1574 {
1575 return typename TRIANGULATION::DistortedCellList();
1576 }
1577
1578
1579
1588 template <int dim>
1590 collect_distorted_coarse_cells(const Triangulation<dim, dim> &triangulation)
1591 {
1592 typename Triangulation<dim, dim>::DistortedCellList distorted_cells;
1593 for (const auto &cell : triangulation.cell_iterators_on_level(0))
1594 {
1596 for (const unsigned int i : GeometryInfo<dim>::vertex_indices())
1597 vertices[i] = cell->vertex(i);
1598
1601
1602 for (const unsigned int i : GeometryInfo<dim>::vertex_indices())
1603 if (determinants[i] <=
1604 1e-9 * Utilities::fixed_power<dim>(cell->diameter()))
1605 {
1606 distorted_cells.distorted_cells.push_back(cell);
1607 break;
1608 }
1609 }
1610
1611 return distorted_cells;
1612 }
1613
1614
1619 template <int dim, int spacedim>
1620 bool
1621 has_distorted_children(
1623 {
1624 Assert(cell->has_children(), ExcInternalError());
1625 Assert(cell->reference_cell().is_hyper_cube(), ExcNotImplemented());
1626
1627 if constexpr (dim == spacedim)
1628 {
1629 for (unsigned int c = 0; c < cell->n_children(); ++c)
1630 {
1631 // We call this function during mesh refinement, at a time
1632 // when the cell vertex cache has not yet been
1633 // re-built. As a consequence, we cannot call
1634 // cell->vertex(), or cell->diameter() (the latter
1635 // implicitly depending on the former). Instead, we have to
1636 // use a helper function to extract vertex locations into
1637 // an array that bypasses the cache.
1638
1639 const unsigned int n_vertices = cell->child(c)->n_vertices();
1641 ReferenceCells::max_n_vertices<dim>()>
1642 child_vertex_indices(n_vertices);
1643 GridTools::internal::extract_vertices_without_cache<dim, spacedim>(
1644 cell->child(c), child_vertex_indices);
1645
1647 for (unsigned int i = 0; i < n_vertices; ++i)
1648 child_vertices[i] = cell->get_triangulation()
1649 .get_vertices()[child_vertex_indices[i]];
1650
1653 determinants);
1654
1655 double child_diameter = numbers::signaling_nan<double>();
1656 if constexpr (dim == 1)
1657 child_diameter = (child_vertices[1] - child_vertices[0]).norm();
1658 else if constexpr (dim == 2)
1659 // Take the longer one of the two diagonals of the quadrilateral
1660 child_diameter =
1661 std::max({(child_vertices[3] - child_vertices[0]).norm(),
1662 (child_vertices[2] - child_vertices[1]).norm()});
1663 else if constexpr (dim == 3)
1664 // Take the longest of the four diagonals of the hexahedron
1665 child_diameter =
1666 std::max({(child_vertices[7] - child_vertices[0]).norm(),
1667 (child_vertices[6] - child_vertices[1]).norm(),
1668 (child_vertices[2] - child_vertices[5]).norm(),
1669 (child_vertices[3] - child_vertices[4]).norm()});
1670 else
1672
1673 for (const unsigned int i : GeometryInfo<dim>::vertex_indices())
1674 if (determinants[i] <=
1675 1e-9 * Utilities::fixed_power<dim>(child_diameter))
1676 return true;
1677 }
1678
1679 return false;
1680 }
1681 else
1682 // Like for collect_distorted_coarse_cells, there is nothing
1683 // that we can do in this case.
1684 return false;
1685 }
1686
1687
1688
1689 template <int dim, int spacedim>
1690 void
1691 update_periodic_face_map_recursively(
1692 const typename Triangulation<dim, spacedim>::cell_iterator &cell_1,
1693 const typename Triangulation<dim, spacedim>::cell_iterator &cell_2,
1694 unsigned int n_face_1,
1695 unsigned int n_face_2,
1696 const types::geometric_orientation orientation,
1697 typename std::map<
1699 unsigned int>,
1700 std::pair<std::pair<typename Triangulation<dim, spacedim>::cell_iterator,
1701 unsigned int>,
1702 types::geometric_orientation>> &periodic_face_map)
1703 {
1704 using FaceIterator = typename Triangulation<dim, spacedim>::face_iterator;
1705 const FaceIterator face_1 = cell_1->face(n_face_1);
1706 const FaceIterator face_2 = cell_2->face(n_face_2);
1707
1708 const auto inverse_orientation =
1709 face_1->reference_cell().get_inverse_combined_orientation(orientation);
1710
1711 if constexpr (running_in_debug_mode())
1712 {
1713 const auto [face_orientation, face_rotation, face_flip] =
1715
1716 Assert((dim != 1) || (face_orientation == true && face_flip == false &&
1717 face_rotation == false),
1718 ExcMessage("The supplied orientation "
1719 "(face_orientation, face_flip, face_rotation) "
1720 "is invalid for 1d"));
1721
1722 Assert((dim != 2) || (face_flip == false && face_rotation == false),
1723 ExcMessage("The supplied orientation "
1724 "(face_orientation, face_flip, face_rotation) "
1725 "is invalid for 2d"));
1726 }
1727
1728 Assert(face_1 != face_2, ExcMessage("face_1 and face_2 are equal!"));
1729
1730 Assert(face_1->at_boundary() && face_2->at_boundary(),
1731 ExcMessage("Periodic faces must be on the boundary"));
1732
1733 // Check if the requirement that each edge can only have at most one hanging
1734 // node, and as a consequence neighboring cells can differ by at most
1735 // one refinement level is enforced. In 1d, there are no hanging nodes and
1736 // so neighboring cells can differ by more than one refinement level.
1737 Assert(dim == 1 || std::abs(cell_1->level() - cell_2->level()) < 2,
1739
1740 // insert periodic face pair for both cells
1741 using CellFace =
1742 std::pair<typename Triangulation<dim, spacedim>::cell_iterator,
1743 unsigned int>;
1744 const CellFace cell_face_1(cell_1, n_face_1);
1745 const CellFace cell_face_2(cell_2, n_face_2);
1746 const std::pair<CellFace, types::geometric_orientation>
1747 cell_face_orientation_2(cell_face_2, orientation);
1748
1749 const std::pair<CellFace, std::pair<CellFace, types::geometric_orientation>>
1750 periodic_faces(cell_face_1, cell_face_orientation_2);
1751
1752 // Only one periodic neighbor is allowed
1753 Assert(periodic_face_map.count(cell_face_1) == 0, ExcInternalError());
1754 periodic_face_map.insert(periodic_faces);
1755
1756 if (dim == 1)
1757 {
1758 if (cell_1->has_children())
1759 {
1760 if (cell_2->has_children())
1761 {
1762 update_periodic_face_map_recursively<dim, spacedim>(
1763 cell_1->child(n_face_1),
1764 cell_2->child(n_face_2),
1765 n_face_1,
1766 n_face_2,
1767 orientation,
1768 periodic_face_map);
1769 }
1770 else // only face_1 has children
1771 {
1772 update_periodic_face_map_recursively<dim, spacedim>(
1773 cell_1->child(n_face_1),
1774 cell_2,
1775 n_face_1,
1776 n_face_2,
1777 orientation,
1778 periodic_face_map);
1779 }
1780 }
1781 }
1782 else // dim == 2 || dim == 3
1783 {
1784 if (cell_1->has_children())
1785 {
1786 if (cell_2->has_children())
1787 {
1788 // In the case that both faces have children, we loop over all
1789 // children and apply update_periodic_face_map_recursively
1790 // recursively:
1791
1792 Assert(face_1->n_children() ==
1794 face_2->n_children() ==
1797
1798 const auto reference_cell = cell_1->reference_cell();
1799
1800 for (unsigned int i = 0;
1801 i < GeometryInfo<dim>::max_children_per_face;
1802 ++i)
1803 {
1804 // Lookup the index for the second face
1805 const unsigned int j =
1806 reference_cell.standard_to_real_face_vertex(
1807 i, n_face_1, inverse_orientation);
1808
1809 // find subcell ids that belong to the subface indices
1810 unsigned int child_cell_1 =
1812 cell_1->refinement_case(),
1813 n_face_1,
1814 i,
1815 cell_1->face_orientation(n_face_1),
1816 cell_1->face_flip(n_face_1),
1817 cell_1->face_rotation(n_face_1),
1818 face_1->refinement_case());
1819 unsigned int child_cell_2 =
1821 cell_2->refinement_case(),
1822 n_face_2,
1823 j,
1824 cell_2->face_orientation(n_face_2),
1825 cell_2->face_flip(n_face_2),
1826 cell_2->face_rotation(n_face_2),
1827 face_2->refinement_case());
1828
1829 Assert(cell_1->child(child_cell_1)->face(n_face_1) ==
1830 face_1->child(i),
1832 Assert(cell_2->child(child_cell_2)->face(n_face_2) ==
1833 face_2->child(j),
1835
1836 // precondition: subcell has the same orientation as cell
1837 // (so that the face numbers coincide) recursive call
1838 update_periodic_face_map_recursively<dim, spacedim>(
1839 cell_1->child(child_cell_1),
1840 cell_2->child(child_cell_2),
1841 n_face_1,
1842 n_face_2,
1843 orientation,
1844 periodic_face_map);
1845 }
1846 }
1847 else // only face_1 has children
1848 {
1849 for (unsigned int i = 0;
1850 i < GeometryInfo<dim>::max_children_per_face;
1851 ++i)
1852 {
1853 // find subcell ids that belong to the subface indices
1854 unsigned int child_cell_1 =
1856 cell_1->refinement_case(),
1857 n_face_1,
1858 i,
1859 cell_1->face_orientation(n_face_1),
1860 cell_1->face_flip(n_face_1),
1861 cell_1->face_rotation(n_face_1),
1862 face_1->refinement_case());
1863
1864 // recursive call
1865 update_periodic_face_map_recursively<dim, spacedim>(
1866 cell_1->child(child_cell_1),
1867 cell_2,
1868 n_face_1,
1869 n_face_2,
1870 orientation,
1871 periodic_face_map);
1872 }
1873 }
1874 }
1875 }
1876 }
1877
1878 // Given the child number and parent's line orientation, return the child face
1879 // number.
1880 unsigned int
1881 child_line_index(const unsigned int child_no,
1882 const types::geometric_orientation line_orientation)
1883 {
1884 AssertIndexRange(child_no, ReferenceCells::Line.n_children());
1885 Assert(line_orientation == numbers::default_geometric_orientation ||
1886 line_orientation == numbers::reverse_line_orientation,
1888 constexpr auto D = numbers::default_geometric_orientation;
1889 if (child_no == 0)
1890 return line_orientation == D ? 0 : 1;
1891 else
1892 return line_orientation == D ? 1 : 0;
1893 }
1894} // end of anonymous namespace
1895
1896
1897namespace internal
1898{
1899 namespace TriangulationImplementation
1900 {
1901 // make sure that if in the following we
1902 // write Triangulation<dim,spacedim>
1903 // we mean the *class*
1904 // ::Triangulation, not the
1905 // enclosing namespace
1906 // internal::TriangulationImplementation
1907 using ::Triangulation;
1908
1914 int,
1915 << "Something went wrong upon construction of cell "
1916 << arg1);
1927 int,
1928 << "Cell " << arg1
1929 << " has negative measure. This typically "
1930 << "indicates some distortion in the cell, or a mistakenly "
1931 << "swapped pair of vertices in the input to "
1932 << "Triangulation::create_triangulation().");
1941 int,
1942 int,
1943 int,
1944 << "Error while creating cell " << arg1
1945 << ": the vertex index " << arg2 << " must be between 0 and "
1946 << arg3 << '.');
1953 int,
1954 int,
1956 << "The input data for creating a triangulation contained "
1957 << "information about a line with indices " << arg1 << " and " << arg2
1958 << " that is described to have boundary indicator "
1959 << static_cast<int>(arg3)
1960 << ". However, this is an internal line not located on the "
1961 << "boundary. You cannot assign a boundary indicator to it." << std::endl
1962 << std::endl
1963 << "If this happened at a place where you call "
1964 << "Triangulation::create_triangulation() yourself, you need "
1965 << "to check the SubCellData object you pass to this function."
1966 << std::endl
1967 << std::endl
1968 << "If this happened in a place where you are reading a mesh "
1969 << "from a file, then you need to investigate why such a line "
1970 << "ended up in the input file. A typical case is a geometry "
1971 << "that consisted of multiple parts and for which the mesh "
1972 << "generator program assumes that the interface between "
1973 << "two parts is a boundary when that isn't supposed to be "
1974 << "the case, or where the mesh generator simply assigns "
1975 << "'geometry indicators' to lines at the perimeter of "
1976 << "a part that are not supposed to be interpreted as "
1977 << "'boundary indicators'.");
1984 int,
1985 int,
1986 int,
1987 int,
1989 << "The input data for creating a triangulation contained "
1990 << "information about a quad with indices " << arg1 << ", " << arg2
1991 << ", " << arg3 << ", and " << arg4
1992 << " that is described to have boundary indicator "
1993 << static_cast<int>(arg5)
1994 << ". However, this is an internal quad not located on the "
1995 << "boundary. You cannot assign a boundary indicator to it." << std::endl
1996 << std::endl
1997 << "If this happened at a place where you call "
1998 << "Triangulation::create_triangulation() yourself, you need "
1999 << "to check the SubCellData object you pass to this function."
2000 << std::endl
2001 << std::endl
2002 << "If this happened in a place where you are reading a mesh "
2003 << "from a file, then you need to investigate why such a quad "
2004 << "ended up in the input file. A typical case is a geometry "
2005 << "that consisted of multiple parts and for which the mesh "
2006 << "generator program assumes that the interface between "
2007 << "two parts is a boundary when that isn't supposed to be "
2008 << "the case, or where the mesh generator simply assigns "
2009 << "'geometry indicators' to quads at the surface of "
2010 << "a part that are not supposed to be interpreted as "
2011 << "'boundary indicators'.");
2018 int,
2019 int,
2020 << "In SubCellData the line info of the line with vertex indices " << arg1
2021 << " and " << arg2 << " appears more than once. "
2022 << "This is not allowed.");
2029 int,
2030 int,
2031 std::string,
2032 << "In SubCellData the line info of the line with vertex indices " << arg1
2033 << " and " << arg2 << " appears multiple times with different (valid) "
2034 << arg3 << ". This is not allowed.");
2041 int,
2042 int,
2043 int,
2044 int,
2045 std::string,
2046 << "In SubCellData the quad info of the quad with line indices " << arg1
2047 << ", " << arg2 << ", " << arg3 << " and " << arg4
2048 << " appears multiple times with different (valid) " << arg5
2049 << ". This is not allowed.");
2050
2055 int,
2056 int,
2057 << "The containers have sizes " << arg1 << " and " << arg2
2058 << ", which is not as expected.");
2059
2065 template <int dim, int spacedim>
2066 void
2068 const unsigned int true_dimension)
2069 {
2070 Assert(2 * true_dimension * tria_level.refine_flags.size() ==
2071 tria_level.neighbors.size(),
2072 ExcMemoryInexact(tria_level.refine_flags.size(),
2073 tria_level.neighbors.size()));
2074 Assert(2 * true_dimension * tria_level.coarsen_flags.size() ==
2075 tria_level.neighbors.size(),
2076 ExcMemoryInexact(tria_level.coarsen_flags.size(),
2077 tria_level.neighbors.size()));
2078 }
2079
2080
2081
2087 void
2088 monitor_memory(const TriaObjects &tria_object, const unsigned int)
2089 {
2090 Assert(tria_object.n_objects() == tria_object.used.size(),
2091 ExcMemoryInexact(tria_object.n_objects(),
2092 tria_object.used.size()));
2093 Assert(tria_object.n_objects() == tria_object.user_flags.size(),
2094 ExcMemoryInexact(tria_object.n_objects(),
2095 tria_object.user_flags.size()));
2096 Assert(tria_object.n_objects() ==
2097 tria_object.boundary_or_material_id.size(),
2098 ExcMemoryInexact(tria_object.n_objects(),
2099 tria_object.boundary_or_material_id.size()));
2100 Assert(tria_object.n_objects() == tria_object.manifold_id.size(),
2101 ExcMemoryInexact(tria_object.n_objects(),
2102 tria_object.manifold_id.size()));
2103 Assert(tria_object.n_objects() == tria_object.user_data.size(),
2104 ExcMemoryInexact(tria_object.n_objects(),
2105 tria_object.user_data.size()));
2106
2107 if (tria_object.structdim > 0)
2108 {
2109 const unsigned int factor = tria_object.children_per_object / 2;
2110 Assert(factor * tria_object.n_objects() ==
2111 tria_object.children.size(),
2112 ExcMemoryInexact(tria_object.n_objects(),
2113 tria_object.children.size()));
2114 }
2115 }
2116 } // namespace TriangulationImplementation
2117} // namespace internal
2118
2119
2124template <int dim, int spacedim>
2126class Triangulation<dim, spacedim>::Policy
2127{
2128public:
2132 virtual ~Policy() = default;
2133
2137 virtual void
2139
2143 virtual void
2146 std::vector<unsigned int> &line_cell_count,
2147 std::vector<unsigned int> &quad_cell_count) = 0;
2148
2154 const bool check_for_distorted_cells) = 0;
2155
2159 virtual void
2161 Triangulation<dim, spacedim> &triangulation) = 0;
2162
2166 virtual void
2168 Triangulation<dim, spacedim> &triangulation) = 0;
2169
2173 virtual bool
2175 const typename Triangulation<dim, spacedim>::cell_iterator &cell) = 0;
2176
2183 virtual std::unique_ptr<Policy>
2184 clone() = 0;
2185};
2186
2187
2188namespace internal
2189{
2190 namespace TriangulationImplementation
2191 {
2192
2198 template <int dim, int spacedim, typename T>
2199 class PolicyWrapper : public Triangulation<dim, spacedim>::Policy
2200 {
2201 public:
2202 void
2204 {
2205 T::update_neighbors(tria);
2206 }
2207
2208 void
2212 std::vector<unsigned int> &line_cell_count,
2213 std::vector<unsigned int> &quad_cell_count) override
2214 {
2215 T::delete_children(tria, cell, line_cell_count, quad_cell_count);
2216 }
2217
2220 const bool check_for_distorted_cells) override
2221 {
2222 return T::execute_refinement(triangulation, check_for_distorted_cells);
2223 }
2224
2225 void
2227 Triangulation<dim, spacedim> &triangulation) override
2228 {
2229 T::prevent_distorted_boundary_cells(triangulation);
2230 }
2231
2232 void
2234 Triangulation<dim, spacedim> &triangulation) override
2235 {
2236 T::prepare_refinement_dim_dependent(triangulation);
2237 }
2238
2239 bool
2242 override
2243 {
2244 return T::template coarsening_allowed<dim, spacedim>(cell);
2245 }
2246
2247 std::unique_ptr<typename Triangulation<dim, spacedim>::Policy>
2248 clone() override
2249 {
2250 return std::make_unique<PolicyWrapper<dim, spacedim, T>>();
2251 }
2252 };
2253
2254
2260 {
2265 : offsets{0} {};
2266
2270 ArrayOfArrays(const ArrayOfArrays &) = default;
2271
2276
2280 ArrayOfArrays(const std::vector<std::size_t> &offsets,
2281 const std::vector<unsigned int> &columns)
2282 : offsets(offsets)
2283 , columns(columns)
2284 {}
2285
2290 ArrayOfArrays(std::vector<std::size_t> &&offsets,
2291 std::vector<unsigned int> &&columns)
2292 : offsets(std::move(offsets))
2293 , columns(std::move(columns))
2294 {}
2295
2300 operator=(const ArrayOfArrays &) = default;
2301
2307
2311 unsigned int
2312 size() const
2313 {
2314 return offsets.size() - 1;
2315 }
2316
2321 operator[](const unsigned int i) const
2322 {
2323 AssertIndexRange(i, offsets.size() - 1);
2325 offsets[i + 1] - offsets[i]);
2326 }
2327
2328 private:
2333 std::vector<std::size_t> offsets;
2334
2338 std::vector<unsigned int> columns;
2339 };
2340
2341
2342
2371 template <int dim>
2373 {
2375 const std::set<ReferenceCell<dim>> &all_reference_cells)
2378 {
2379 if constexpr (running_in_debug_mode())
2380 for (const auto &reference_cell : cell_types)
2381 Assert(all_reference_cells_.count(reference_cell) == 1u,
2383 }
2384
2385 const std::set<ReferenceCell<dim>> &
2387 {
2388 return all_reference_cells_;
2389 }
2390
2391 std::vector<types::geometric_orientation> &
2392 entity_orientations(const unsigned int structdim)
2393 {
2394 if (structdim == 1)
2395 return line_orientation;
2396
2397 AssertDimension(structdim, 2);
2398
2399 return quad_orientation;
2400 }
2401
2402 const std::vector<types::geometric_orientation> &
2403 entity_orientations(const unsigned int structdim) const
2404 {
2405 if (structdim == 1)
2406 return line_orientation;
2407
2408 AssertDimension(structdim, 2);
2409
2410 return quad_orientation;
2411 }
2412
2413 template <int structdim>
2414 std::vector<ReferenceCell<structdim>> &
2416 {
2417 if constexpr (structdim == dim)
2418 return cell_types;
2419 else if constexpr ((structdim == 2) && (dim == 3))
2420 {
2421 return quad_types;
2422 }
2423 else
2424 // for vertices/lines the entity types are clear
2425 // and we shouldn't need this function
2427 }
2428
2429 template <int structdim>
2430 const std::vector<ReferenceCell<structdim>> &
2432 {
2433 if constexpr (structdim == dim)
2434 return cell_types;
2435 else if constexpr ((structdim == 2) && (dim == 3))
2436 {
2437 return quad_types;
2438 }
2439 else
2440 // for vertices/lines the entity types are clear
2441 // and we shouldn't need this function
2443 }
2444
2446 entity_to_entities(const unsigned int from, const unsigned int to)
2447 {
2448 if (from == dim && to == dim)
2449 return neighbors;
2450 else if (from == dim && to == dim - 1)
2451 return cell_entities;
2452 else if (dim == 3 && from == 2 && to == 0)
2453 return quad_vertices;
2454 else if (dim == 3 && from == 2 && to == 1)
2455 return quad_lines;
2456 else if (from == 1 && to == 0)
2457 return line_vertices;
2458
2460
2461 return cell_entities;
2462 }
2463
2464 const ArrayOfArrays &
2465 entity_to_entities(const unsigned int from, const unsigned int to) const
2466 {
2467 if (from == dim && to == dim)
2468 return neighbors;
2469 else if (from == dim && to == dim - 1)
2470 return cell_entities;
2471 else if (dim == 3 && from == 2 && to == 0)
2472 return quad_vertices;
2473 else if (dim == 3 && from == 2 && to == 1)
2474 return quad_lines;
2475 else if (from == 1 && to == 0)
2476 return line_vertices;
2477
2479
2480 return cell_entities;
2481 }
2482
2483 private:
2484 std::vector<ReferenceCell<dim>> cell_types;
2485
2486 std::set<ReferenceCell<dim>> all_reference_cells_;
2487
2489
2490 std::vector<types::geometric_orientation> line_orientation;
2491
2494
2495 std::vector<types::geometric_orientation> quad_orientation;
2496
2499
2500 std::vector<ReferenceCell<2>> quad_types;
2501 };
2502
2503
2504
2513 determine_neighbors(const ArrayOfArrays &cells_to_faces)
2514 {
2515 // First determine the maximal face number used. The number of faces
2516 // is then one more (faces are counted starting at zero):
2517 unsigned int n_faces = 0;
2518 for (unsigned int cell = 0; cell < cells_to_faces.size(); ++cell)
2519 {
2520 const ArrayView<const unsigned int> face_indices =
2521 cells_to_faces[cell];
2522 n_faces = std::max(n_faces,
2523 *std::max_element(face_indices.begin(),
2524 face_indices.end()) +
2525 1);
2526 }
2527
2528 std::vector<std::size_t> cells_to_cells_offsets(cells_to_faces.size() + 1,
2529 0u);
2530 for (unsigned int cell = 0; cell < cells_to_faces.size(); ++cell)
2531 {
2532 const ArrayView<const unsigned int> face_indices =
2533 cells_to_faces[cell];
2534 cells_to_cells_offsets[cell + 1] =
2535 cells_to_cells_offsets[cell] + face_indices.size();
2536 }
2537 std::vector<unsigned int> cells_to_cells_columns(
2538 cells_to_cells_offsets.back(),
2539 /* default: cells is at the boundary in this direction */ -1);
2540
2541 std::vector<std::pair<unsigned int, unsigned int>> neighbors(
2542 n_faces,
2543 std::make_pair(numbers::invalid_unsigned_int,
2545
2546 // loop over all cells
2547 unsigned int global_face_index = 0;
2548 for (unsigned int cell = 0; cell < cells_to_faces.size(); ++cell)
2549 {
2550 // ... and all its faces
2551 const auto faces = cells_to_faces[cell];
2552 for (unsigned int f = 0; f < faces.size(); ++f, ++global_face_index)
2553 {
2554 if (neighbors[faces[f]].first == numbers::invalid_unsigned_int)
2555 {
2556 // face is visited the first time -> save the visiting cell
2557 // and the face pointer
2558 neighbors[faces[f]] = std::make_pair(cell, global_face_index);
2559 }
2560 else
2561 {
2562 // face is visited the second time -> now we know the cells
2563 // on both sides of the face and we can determine for both
2564 // cells the neighbor
2565 cells_to_cells_columns[global_face_index] =
2566 neighbors[faces[f]].first;
2567 cells_to_cells_columns[neighbors[faces[f]].second] = cell;
2568 }
2569 }
2570 }
2571
2572 return ArrayOfArrays(std::move(cells_to_cells_offsets),
2573 std::move(cells_to_cells_columns));
2574 }
2575
2576
2577
2586 template <int max_n_vertices, int face_dim, int dim, typename FU>
2587 void
2589 const std::vector<ReferenceCell<dim>> &cell_types,
2590 const ArrayOfArrays &cells_to_vertices,
2591 ArrayOfArrays &cells_to_faces, // result
2592 ArrayOfArrays &crs_0, // result
2593 std::vector<types::geometric_orientation> &orientations, // result
2594 const FU &second_key_function)
2595 {
2596 const bool compatibility_mode = true;
2597
2598 // note: we do not pre-allocate memory for these arrays because it turned
2599 // out that counting unique entities is more expensive than push_back().
2600 std::vector<std::size_t> offsets_0;
2601 std::vector<unsigned int> columns_0;
2602
2603 unsigned int n_entities = 0;
2604
2605 for (const auto &c : cell_types)
2606 {
2607 // Make sure that there are only two possibilities for
2608 // face_dim that we can cover with the ?: statement below:
2609 Assert((face_dim == dim - 1) || ((dim == 3) && (face_dim == 1)),
2611 n_entities += (face_dim == dim - 1 ? c.n_faces() : c.n_lines());
2612 }
2613
2614 // step 1: store each d-dimensional entity of a cell (described by their
2615 // vertices) into a vector and create a key for them
2616 //
2617 // note: it turned out to be more efficient to have a vector of tuples
2618 // than to have two vectors (sorting becomes inefficient)
2619 std::vector<
2620 std::tuple<std::array<unsigned int, max_n_vertices>, unsigned int>>
2621 keys; // key (sorted vertices), cell-entity index
2622
2623 std::vector<std::array<unsigned int, max_n_vertices>> ad_entity_vertices;
2624 std::vector<ReferenceCell<face_dim>> ad_entity_types;
2625 std::vector<std::array<unsigned int, max_n_vertices>> ad_compatibility;
2626
2627 keys.reserve(n_entities);
2628 ad_entity_vertices.reserve(n_entities);
2629 ad_entity_types.reserve(n_entities);
2630 ad_compatibility.reserve(n_entities);
2631
2632 std::vector<std::size_t> cells_to_faces_offsets(cell_types.size() + 1);
2633 cells_to_faces_offsets[0] = 0;
2634
2635 constexpr unsigned int offset = 1;
2636
2637 // loop over all cells
2638 for (unsigned int c = 0, counter = 0; c < cell_types.size(); ++c)
2639 {
2640 const auto &cell_type = cell_types[c];
2641
2642 // Make sure that there are only two possibilities for
2643 // face_dimensionality that we can cover with the ?: statement below:
2644 Assert((face_dim == dim - 1) || ((dim == 3) && (face_dim == 1)),
2646 const unsigned int n_face_entities =
2647 (face_dim == dim - 1 ? cell_type.n_faces() : cell_type.n_lines());
2648 cells_to_faces_offsets[c + 1] =
2649 cells_to_faces_offsets[c] + n_face_entities;
2650
2651 // ... loop over all its entities
2652 const ArrayView<const unsigned int> local_vertices =
2653 cells_to_vertices[c];
2654 for (unsigned int e = 0; e < n_face_entities; ++e)
2655 {
2656 // ... determine global entity vertices
2657 std::array<unsigned int, max_n_vertices> entity_vertices;
2658 std::fill(entity_vertices.begin(), entity_vertices.end(), 0);
2659
2660 // Same as above, make sure that there are only two possibilities
2661 // for face_dimensionality that we can cover with the ?: statement
2662 // below:
2663 Assert((face_dim == dim - 1) || ((dim == 3) && (face_dim == 1)),
2665 for (unsigned int i = 0;
2666 i < (face_dim == dim - 1 ?
2667 cell_type.face_reference_cell(e).n_vertices() :
2668 ReferenceCells::Line.n_vertices());
2669 ++i)
2670 entity_vertices[i] =
2671 local_vertices[face_dim == dim - 1 ?
2672 cell_type.face_to_cell_vertices(
2673 e,
2674 i,
2676 cell_type.line_to_cell_vertices(e, i)] +
2677 offset;
2678
2679 // ... create key
2680 std::array<unsigned int, max_n_vertices> key = entity_vertices;
2681 std::sort(key.begin(), key.end());
2682 keys.emplace_back(key, counter++);
2683
2684 ad_entity_vertices.emplace_back(entity_vertices);
2685
2686 if constexpr (face_dim == dim - 1)
2687 ad_entity_types.emplace_back(cell_type.face_reference_cell(e));
2688 else if constexpr (face_dim == dim - 2)
2689 {
2690 // Since we only deal with meshes up to dimension 3,
2691 // something that has co-dimensionality -2 must either be
2692 // a vertex or a line, regardless of what the object we are
2693 // working on actually us:
2694 if constexpr (face_dim == 0)
2695 ad_entity_types.emplace_back(ReferenceCells::Vertex);
2696 else if constexpr (face_dim == 1)
2697 ad_entity_types.emplace_back(ReferenceCells::Line);
2698 else
2699 // But it's probably useful to be conservative if someone
2700 // ever comes along to implement 4d meshes:
2702 }
2703 else
2705
2706 if (compatibility_mode)
2707 ad_compatibility.emplace_back(
2708 second_key_function(entity_vertices, cell_type, c, e));
2709 }
2710 }
2711
2712 std::vector<unsigned int> cells_to_faces_indices(keys.size());
2713 orientations.assign(keys.size(), numbers::default_geometric_orientation);
2714
2715 // step 2: sort according to key so that entities with same key can be
2716 // merged
2717 std::sort(keys.begin(), keys.end());
2718
2719 if (compatibility_mode)
2720 {
2721 unsigned int n_unique_entities = 0;
2722 unsigned int n_unique_entity_vertices = 0;
2723
2724 std::array<unsigned int, max_n_vertices> ref_key, new_key;
2725 std::fill(ref_key.begin(), ref_key.end(), 0);
2726 for (unsigned int i = 0; i < keys.size(); ++i)
2727 {
2728 const auto offset_i = std::get<1>(keys[i]);
2729
2730 if (ref_key != std::get<0>(keys[i]))
2731 {
2732 ref_key = std::get<0>(keys[i]);
2733
2734 ++n_unique_entities;
2735 n_unique_entity_vertices +=
2736 ad_entity_types[offset_i].n_vertices();
2737
2738 new_key = ad_compatibility[offset_i];
2739 }
2740
2741 std::get<0>(keys[i]) = new_key;
2742 }
2743
2744 std::sort(keys.begin(), keys.end());
2745
2746 offsets_0.reserve(n_unique_entities + 1);
2747 columns_0.reserve(n_unique_entity_vertices);
2748 }
2749
2750
2751 std::array<unsigned int, max_n_vertices> ref_key;
2752 std::array<unsigned int, max_n_vertices> ref_indices;
2753 std::fill(ref_key.begin(), ref_key.end(), 0);
2754
2755 unsigned int counter = ::numbers::invalid_unsigned_int;
2756 for (unsigned int i = 0; i < keys.size(); i++)
2757 {
2758 const auto offset_i = std::get<1>(keys[i]);
2759
2760 if (ref_key != std::get<0>(keys[i]))
2761 {
2762 // new key: default orientation is correct
2763 ++counter;
2764 ref_key = std::get<0>(keys[i]);
2765 ref_indices = ad_entity_vertices[offset_i];
2766
2767 offsets_0.push_back(columns_0.size());
2768 for (const auto j : ad_entity_vertices[offset_i])
2769 if (j != 0)
2770 columns_0.push_back(j - offset);
2771 }
2772 else
2773 {
2774 // previously seen key: set orientation relative to the first
2775 // occurrence
2776 orientations[offset_i] =
2777 ad_entity_types[offset_i]
2778 .template get_combined_orientation<unsigned int>(
2779 make_array_view(ad_entity_vertices[offset_i].begin(),
2780 ad_entity_vertices[offset_i].begin() +
2781 ad_entity_types[offset_i].n_vertices()),
2782 make_array_view(ref_indices.begin(),
2783 ref_indices.begin() +
2784 ad_entity_types[offset_i].n_vertices()));
2785 }
2786 cells_to_faces_indices[offset_i] = counter;
2787 }
2788 offsets_0.push_back(columns_0.size());
2789
2790 // Finally build the output objects:
2791 crs_0 = ArrayOfArrays(std::move(offsets_0), std::move(columns_0));
2792 cells_to_faces = ArrayOfArrays(std::move(cells_to_faces_offsets),
2793 std::move(cells_to_faces_indices));
2794 }
2795
2796
2797
2802 template <int face_dim, int dim, typename FU>
2803 void
2804 build_face_entities(const std::vector<ReferenceCell<dim>> &cell_types,
2805 const ArrayOfArrays &cells_to_vertices,
2806 ArrayOfArrays &cells_to_faces,
2807 ArrayOfArrays &crs_0,
2808 std::vector<types::geometric_orientation> &orientations,
2809 const FU &second_key_function)
2810 {
2811 unsigned int max_n_vertices = 0;
2812
2813 // If we are dealing with faces of cells, figure out how many vertices
2814 // each face may have. Otherwise, we're in 3d and are dealing with
2815 // lines, for which we know the number of vertices:
2816 for (const auto &c : cell_types)
2817 if (face_dim == dim - 1)
2818 {
2819 for (unsigned int f = 0; f < c.n_faces(); ++f)
2820 max_n_vertices =
2821 std::max(max_n_vertices, c.face_reference_cell(f).n_vertices());
2822 }
2823 else if (face_dim == 1)
2824 max_n_vertices = std::max(max_n_vertices, 2u);
2825 else
2827
2828 if (max_n_vertices == 2)
2829 build_face_entities_templated<2, face_dim>(cell_types,
2830 cells_to_vertices,
2831 cells_to_faces,
2832 crs_0,
2833 orientations,
2834 second_key_function);
2835 else if (max_n_vertices == 3)
2836 build_face_entities_templated<3, face_dim>(cell_types,
2837 cells_to_vertices,
2838 cells_to_faces,
2839 crs_0,
2840 orientations,
2841 second_key_function);
2842 else if (max_n_vertices == 4)
2843 build_face_entities_templated<4, face_dim>(cell_types,
2844 cells_to_vertices,
2845 cells_to_faces,
2846 crs_0,
2847 orientations,
2848 second_key_function);
2849 else
2851 }
2852
2853
2854
2862 template <int dim>
2863 void
2865 const std::vector<ReferenceCell<dim>> &cell_types,
2866 const ArrayOfArrays &cells_to_vertices,
2867 const ArrayOfArrays &cells_to_lines,
2868 const ArrayOfArrays &lines_to_vertices,
2869 const ArrayOfArrays &cells_to_quads,
2870 const ArrayOfArrays &quads_to_vertices,
2871 const std::vector<types::geometric_orientation> &ori_cq,
2872 ArrayOfArrays &quads_to_lines, // result
2873 std::vector<types::geometric_orientation> &ori_ql, // result
2874 std::vector<ReferenceCell<2>> &quad_t_id // result
2875 )
2876 {
2877 quad_t_id.resize(quads_to_vertices.size());
2878
2879 // Build the offsets of the compressed representation of the
2880 // quads-to-lines array by counting the number of lines of each face.
2881 // We do this by looping over all cells and querying their faces.
2882 // This encounters faces in a random order, so we first
2883 // store the number of lines of the f'th face in the (f+1)st
2884 // offset and then run a prefix sum to sum up all
2885 // previous offsets
2886 std::vector<std::size_t> quads_to_lines_offsets(quads_to_vertices.size() +
2887 1);
2888 for (unsigned int c = 0; c < cells_to_quads.size(); ++c)
2889 {
2890 // loop over faces
2891 const ArrayView<const unsigned int> faces = cells_to_quads[c];
2892 for (unsigned int f = 0; f < faces.size(); ++f)
2893 {
2894 quads_to_lines_offsets[faces[f] + 1] =
2895 cell_types[c].face_reference_cell(f).n_lines();
2896 }
2897 }
2898 quads_to_lines_offsets[0] = 0;
2899 for (unsigned int i = 0; i < quads_to_lines_offsets.size() - 1; ++i)
2900 quads_to_lines_offsets[i + 1] += quads_to_lines_offsets[i];
2901
2902 // Now that we have the offsets, allocate memory for the indices:
2903 std::vector<unsigned int> quads_to_lines_indices(
2904 quads_to_lines_offsets.back());
2905 ori_ql.assign(quads_to_lines_offsets.back(),
2907
2908 // loop over cells
2909 unsigned int global_face_index = 0;
2910 for (unsigned int c = 0; c < cells_to_quads.size(); ++c)
2911 {
2912 const ReferenceCell<dim> cell_type = cell_types[c];
2913
2914 // loop over faces
2915 const ArrayView<const unsigned int> faces = cells_to_quads[c];
2916 for (unsigned int f_index = 0; f_index < faces.size();
2917 ++f_index, ++global_face_index)
2918 {
2919 const unsigned int f = faces[f_index];
2920
2921 // only faces with default orientation have to do something
2922 if (ori_cq[global_face_index] !=
2924 continue;
2925
2926 // loop over the lines of this face
2927 quad_t_id[f] = cell_type.face_reference_cell(f_index);
2928 for (unsigned int l = 0; l < quad_t_id[f].n_lines(); ++l)
2929 {
2930 // determine global index of line
2931 const unsigned int local_line_index =
2932 cell_type.face_to_cell_lines(
2934 const unsigned int global_line_index =
2935 cells_to_lines[c][local_line_index];
2936 quads_to_lines_indices[quads_to_lines_offsets[f] + l] =
2937 global_line_index;
2938
2939 // determine orientation of line
2940 bool same = true;
2941 for (unsigned int v = 0; v < 2; ++v)
2942 if (cells_to_vertices
2943 [c][cell_type.face_and_line_to_cell_vertices(
2944 f_index,
2945 l,
2946 v,
2948 lines_to_vertices[global_line_index][v])
2949 {
2950 same = false;
2951 break;
2952 }
2953
2954 // ... comparison gives orientation
2955 ori_ql[quads_to_lines_offsets[f] + l] =
2958 }
2959 }
2960 }
2961
2962 // Finally build the output object:
2963 quads_to_lines = ArrayOfArrays(std::move(quads_to_lines_offsets),
2964 std::move(quads_to_lines_indices));
2965 }
2966
2967
2968
2977 template <int dim>
2978 Connectivity<dim>
2979 build_connectivity(const std::vector<ReferenceCell<dim>> &cell_types,
2980 const std::set<ReferenceCell<dim>> &all_reference_cells,
2981 const ArrayOfArrays &cells_to_vertices)
2982 {
2983 Connectivity<dim> connectivity(cell_types, all_reference_cells);
2984
2985 ArrayOfArrays temp1; // needed for 3d
2986
2987 if constexpr (dim == 1)
2988 connectivity.entity_to_entities(1, 0) = cells_to_vertices;
2989
2990 if constexpr (dim == 2 || dim == 3) // build lines
2991 {
2992 std::vector<types::geometric_orientation> dummy;
2993
2994 build_face_entities<1>(
2995 connectivity.template entity_types<dim>(),
2996 cells_to_vertices,
2997 dim == 2 ? connectivity.entity_to_entities(2, 1) : temp1,
2998 connectivity.entity_to_entities(1, 0),
2999 dim == 2 ? connectivity.entity_orientations(1) : dummy,
3000 [](auto key, const auto &, const auto &, const auto &) {
3001 // to ensure same enumeration as in deal.II
3002 return key;
3003 });
3004 }
3005
3006 if constexpr (dim == 3) // build quads
3007 {
3008 build_face_entities<2>(
3009 connectivity.template entity_types<3>(),
3010 cells_to_vertices,
3011 connectivity.entity_to_entities(3, 2),
3012 connectivity.entity_to_entities(2, 0),
3013 connectivity.entity_orientations(2),
3014 [&](auto key, // of type std::array<unsigned int, max_n_vertices>
3015 // but max_n_vertices is not known here
3016 const ReferenceCell<dim> &cell_type,
3017 const unsigned int &c,
3018 const unsigned int &f) {
3019 // to ensure same enumeration as in deal.II
3020 AssertIndexRange(cell_type.face_reference_cell(f).n_lines(),
3021 key.size() + 1);
3022
3023 unsigned int l = 0;
3024
3025 for (; l < cell_type.face_reference_cell(f).n_lines(); ++l)
3026 {
3027 AssertIndexRange(l, key.size());
3028 key[l] = temp1[c][cell_type.face_to_cell_lines(
3029 f, l, numbers::default_geometric_orientation)] +
3030 1 /*offset!*/;
3031 }
3032
3033 for (; l < key.size(); ++l)
3034 key[l] = 0;
3035
3036 return key;
3037 });
3038
3039 // create connectivity: quad -> line
3040 build_intersection(connectivity.template entity_types<3>(),
3041 cells_to_vertices,
3042 temp1,
3043 connectivity.entity_to_entities(1, 0),
3044 connectivity.entity_to_entities(3, 2),
3045 connectivity.entity_to_entities(2, 0),
3046 connectivity.entity_orientations(2),
3047 connectivity.entity_to_entities(2, 1),
3048 connectivity.entity_orientations(1),
3049 connectivity.template entity_types<2>());
3050 }
3051
3052 // determine neighbors
3053 connectivity.entity_to_entities(dim, dim) =
3054 determine_neighbors(connectivity.entity_to_entities(dim, dim - 1));
3055
3056 return connectivity;
3057 }
3058
3059
3060
3064 template <int dim>
3065 Connectivity<dim>
3066 build_connectivity(const std::vector<CellData<dim>> &cells)
3067 {
3068 AssertThrow(cells.size() > 0, ExcMessage("No cells have been provided!"));
3069
3070 // determine cell types and process vertices
3071 std::vector<unsigned int> cell_vertices;
3072 cell_vertices.reserve(std::accumulate(cells.begin(),
3073 cells.end(),
3074 0u,
3075 [](const unsigned int accumulator,
3076 const CellData<dim> &cell) {
3077 return accumulator +
3078 cell.vertices.size();
3079 }));
3080
3081 std::vector<std::size_t> cell_vertices_offsets;
3082 cell_vertices_offsets.reserve(cells.size() + 1);
3083 cell_vertices_offsets.push_back(0);
3084
3085 std::vector<ReferenceCell<dim>> cell_types;
3086 cell_types.reserve(cells.size());
3087
3088 // Looping over all the cells can be expensive, so we mark the kinds of
3089 // ReferenceCells we have just once
3090 std::set<ReferenceCell<dim>> all_reference_cells;
3091 // loop over cells and create CRS
3092 for (const auto &cell : cells)
3093 {
3094 if constexpr (running_in_debug_mode())
3095 {
3096 auto vertices_unique = cell.vertices;
3097 std::sort(vertices_unique.begin(), vertices_unique.end());
3098 vertices_unique.erase(std::unique(vertices_unique.begin(),
3099 vertices_unique.end()),
3100 vertices_unique.end());
3101
3102 Assert(
3103 vertices_unique.size() == cell.vertices.size(),
3104 ExcMessage(
3105 "The definition of a cell refers to the same vertex several "
3106 "times. This is not possible. A common reason is that "
3107 "CellData::vertices has a size that does not match the "
3108 "size expected from the reference cell. Please resize "
3109 "CellData::vertices or use the appropriate constructor of "
3110 "CellData."));
3111 }
3112
3113 cell_types.push_back(
3114 ReferenceCells::n_vertices_to_reference_cell<dim>(
3115 cell.vertices.size()));
3116 all_reference_cells.insert(cell_types.back());
3117
3118 // create CRS of vertices (to remove template argument dim)
3119 cell_vertices.insert(cell_vertices.end(),
3120 cell.vertices.begin(),
3121 cell.vertices.end());
3122 cell_vertices_offsets.push_back(cell_vertices.size());
3123 }
3124
3125 // do the actual work
3126 return build_connectivity(cell_types,
3127 all_reference_cells,
3128 {std::move(cell_vertices_offsets),
3129 std::move(cell_vertices)});
3130 }
3131
3132
3229 {
3241 template <int dim, int spacedim>
3242 static void
3244 const Triangulation<dim, spacedim> &triangulation,
3245 const unsigned int level_objects,
3247 {
3248 using line_iterator =
3250
3251 number_cache.n_levels = 0;
3252 if (level_objects > 0)
3253 // find the last level on which there are used cells
3254 for (unsigned int level = 0; level < level_objects; ++level)
3255 if (triangulation.begin(level) != triangulation.end(level))
3256 number_cache.n_levels = level + 1;
3257
3258 // no cells at all?
3259 Assert(number_cache.n_levels > 0, ExcInternalError());
3260
3261 //---------------------------------
3262 // update the number of lines on the different levels in the
3263 // cache
3264 number_cache.n_lines = 0;
3265 number_cache.n_active_lines = 0;
3266
3267 // for 1d, lines have levels so take count the objects per
3268 // level and globally
3269 if (dim == 1)
3270 {
3271 number_cache.n_lines_level.resize(number_cache.n_levels);
3272 number_cache.n_active_lines_level.resize(number_cache.n_levels);
3273
3274 for (unsigned int level = 0; level < number_cache.n_levels; ++level)
3275 {
3276 // count lines on this level
3277 number_cache.n_lines_level[level] = 0;
3278 number_cache.n_active_lines_level[level] = 0;
3279
3280 line_iterator line = triangulation.begin_line(level),
3281 endc =
3282 (level == number_cache.n_levels - 1 ?
3283 line_iterator(triangulation.end_line()) :
3284 triangulation.begin_line(level + 1));
3285 for (; line != endc; ++line)
3286 {
3287 ++number_cache.n_lines_level[level];
3288 if (line->has_children() == false)
3289 ++number_cache.n_active_lines_level[level];
3290 }
3291
3292 // update total number of lines
3293 number_cache.n_lines += number_cache.n_lines_level[level];
3294 number_cache.n_active_lines +=
3295 number_cache.n_active_lines_level[level];
3296 }
3297 }
3298 else
3299 {
3300 // for dim>1, there are no levels for lines
3301 number_cache.n_lines_level.clear();
3302 number_cache.n_active_lines_level.clear();
3303
3304 line_iterator line = triangulation.begin_line(),
3305 endc = triangulation.end_line();
3306 for (; line != endc; ++line)
3307 {
3308 ++number_cache.n_lines;
3309 if (line->has_children() == false)
3310 ++number_cache.n_active_lines;
3311 }
3312 }
3313 }
3314
3329 template <int dim, int spacedim>
3330 static void
3332 const Triangulation<dim, spacedim> &triangulation,
3333 const unsigned int level_objects,
3335 {
3336 // update lines and n_levels in number_cache. since we don't
3337 // access any of these numbers, we can do this in the
3338 // background
3339 Threads::Task<void> update_lines = Threads::new_task([&]() {
3340 compute_number_cache_dim(
3341 triangulation,
3342 level_objects,
3343 // Select the 1d part of the cache:
3345 &>(number_cache));
3346 });
3347
3348 using quad_iterator =
3350
3351 //---------------------------------
3352 // update the number of quads on the different levels in the
3353 // cache
3354 number_cache.n_quads = 0;
3355 number_cache.n_active_quads = 0;
3356
3357 // for 2d, quads have levels so take count the objects per
3358 // level and globally
3359 if (dim == 2)
3360 {
3361 // count the number of levels; the function we called above
3362 // on a separate Task for lines also does this and puts it into
3363 // number_cache.n_levels, but this datum may not yet be
3364 // available as we call the function on a separate task
3365 unsigned int n_levels = 0;
3366 if (level_objects > 0)
3367 // find the last level on which there are used cells
3368 for (unsigned int level = 0; level < level_objects; ++level)
3369 if (triangulation.begin(level) != triangulation.end(level))
3370 n_levels = level + 1;
3371
3372 number_cache.n_quads_level.resize(n_levels);
3373 number_cache.n_active_quads_level.resize(n_levels);
3374
3375 for (unsigned int level = 0; level < n_levels; ++level)
3376 {
3377 // count quads on this level
3378 number_cache.n_quads_level[level] = 0;
3379 number_cache.n_active_quads_level[level] = 0;
3380
3381 quad_iterator quad = triangulation.begin_quad(level),
3382 endc =
3383 (level == n_levels - 1 ?
3384 quad_iterator(triangulation.end_quad()) :
3385 triangulation.begin_quad(level + 1));
3386 for (; quad != endc; ++quad)
3387 {
3388 ++number_cache.n_quads_level[level];
3389 if (quad->has_children() == false)
3390 ++number_cache.n_active_quads_level[level];
3391 }
3392
3393 // update total number of quads
3394 number_cache.n_quads += number_cache.n_quads_level[level];
3395 number_cache.n_active_quads +=
3396 number_cache.n_active_quads_level[level];
3397 }
3398 }
3399 else
3400 {
3401 // for dim>2, there are no levels for quads
3402 number_cache.n_quads_level.clear();
3403 number_cache.n_active_quads_level.clear();
3404
3405 quad_iterator quad = triangulation.begin_quad(),
3406 endc = triangulation.end_quad();
3407 for (; quad != endc; ++quad)
3408 {
3409 ++number_cache.n_quads;
3410 if (quad->has_children() == false)
3411 ++number_cache.n_active_quads;
3412 }
3413 }
3414
3415 // wait for the background computation for lines
3416 update_lines.join();
3417 }
3418
3434 template <int dim, int spacedim>
3435 static void
3437 const Triangulation<dim, spacedim> &triangulation,
3438 const unsigned int level_objects,
3440 {
3441 // update quads, lines and n_levels in number_cache. since we
3442 // don't access any of these numbers, we can do this in the
3443 // background
3444 Threads::Task<void> update_quads_and_lines = Threads::new_task([&]() {
3445 compute_number_cache_dim(
3446 triangulation,
3447 level_objects,
3448 // Select the 1d and 2d part of the cache:
3450 &>(number_cache));
3451 });
3452
3453 using hex_iterator =
3455
3456 //---------------------------------
3457 // update the number of hexes on the different levels in the
3458 // cache
3459 number_cache.n_hexes = 0;
3460 number_cache.n_active_hexes = 0;
3461
3462 // for 3d, hexes have levels so take count the objects per
3463 // level and globally
3464 if (dim == 3)
3465 {
3466 // count the number of levels; the function we called
3467 // above on a separate Task for quads (recursively, via
3468 // the lines function) also does this and puts it into
3469 // number_cache.n_levels, but this datum may not yet be
3470 // available as we call the function on a separate task
3471 unsigned int n_levels = 0;
3472 if (level_objects > 0)
3473 // find the last level on which there are used cells
3474 for (unsigned int level = 0; level < level_objects; ++level)
3475 if (triangulation.begin(level) != triangulation.end(level))
3476 n_levels = level + 1;
3477
3478 number_cache.n_hexes_level.resize(n_levels);
3479 number_cache.n_active_hexes_level.resize(n_levels);
3480
3481 for (unsigned int level = 0; level < n_levels; ++level)
3482 {
3483 // count hexes on this level
3484 number_cache.n_hexes_level[level] = 0;
3485 number_cache.n_active_hexes_level[level] = 0;
3486
3487 hex_iterator hex = triangulation.begin_hex(level),
3488 endc = (level == n_levels - 1 ?
3489 hex_iterator(triangulation.end_hex()) :
3490 triangulation.begin_hex(level + 1));
3491 for (; hex != endc; ++hex)
3492 {
3493 ++number_cache.n_hexes_level[level];
3494 if (hex->has_children() == false)
3495 ++number_cache.n_active_hexes_level[level];
3496 }
3497
3498 // update total number of hexes
3499 number_cache.n_hexes += number_cache.n_hexes_level[level];
3500 number_cache.n_active_hexes +=
3501 number_cache.n_active_hexes_level[level];
3502 }
3503 }
3504 else
3505 {
3506 // for dim>3, there are no levels for hexes
3507 number_cache.n_hexes_level.clear();
3508 number_cache.n_active_hexes_level.clear();
3509
3510 hex_iterator hex = triangulation.begin_hex(),
3511 endc = triangulation.end_hex();
3512 for (; hex != endc; ++hex)
3513 {
3514 ++number_cache.n_hexes;
3515 if (hex->has_children() == false)
3516 ++number_cache.n_active_hexes;
3517 }
3518 }
3519
3520 // wait for the background computation for quads
3521 update_quads_and_lines.join();
3522 }
3523
3524
3525 template <int dim, int spacedim>
3526 static void
3528 const Triangulation<dim, spacedim> &triangulation,
3529 const unsigned int level_objects,
3531 {
3532 compute_number_cache_dim(triangulation, level_objects, number_cache);
3533
3534 number_cache.active_cell_index_partitioner =
3535 std::make_shared<const Utilities::MPI::Partitioner>(
3536 triangulation.n_active_cells());
3537
3538 number_cache.level_cell_index_partitioners.resize(
3539 triangulation.n_levels());
3540 for (unsigned int level = 0; level < triangulation.n_levels(); ++level)
3541 number_cache.level_cell_index_partitioners[level] =
3542 std::make_shared<const Utilities::MPI::Partitioner>(
3543 triangulation.n_cells(level));
3544 }
3545
3546
3547 template <int spacedim>
3548 static void
3551
3552
3553 template <int dim, int spacedim>
3554 static void
3556 {
3557 // each face can be neighbored on two sides by cells. according to the
3558 // face's intrinsic normal we define the left neighbor as the one for
3559 // which the face normal points outward, and store that one first; the
3560 // second one is then the right neighbor for which the face normal
3561 // points inward. This information depends on the type of cell and
3562 // local number of face for the 'standard ordering and orientation' of
3563 // faces and then on the face_orientation information for the real
3564 // mesh. Set up a table to have fast access to those offsets (0 for
3565 // left and 1 for right). Some of the values are invalid as they
3566 // reference too large face numbers, but we just leave them at a zero
3567 // value.
3568 //
3569 // Note, that in 2d for lines as faces the normal direction given in
3570 // the GeometryInfo class is not consistent. We thus define here that
3571 // the normal for a line points to the right if the line points
3572 // upwards.
3573 //
3574 // There is one more point to consider, however: if we have
3575 // dim<spacedim, then we may have cases where cells are inverted. In
3576 // effect, both cells think they are the left neighbor of an edge, for
3577 // example, which leads us to forget neighborship information (a case
3578 // that shows this is codim_one/hanging_nodes_02). We store whether a
3579 // cell is inverted using the direction_flag, so if a cell has a false
3580 // direction_flag, then we need to invert our selection whether we are
3581 // a left or right neighbor in all following computations.
3582 //
3583 // first index: dimension (minus 2)
3584 // second index: local face index
3585 // third index: face_orientation (false and true)
3586 constexpr unsigned int left_right_offset[2][6][2] = {
3587 // quadrilateral
3588 {{0, 1}, // face 0, face_orientation = false and true
3589 {1, 0}, // face 1, face_orientation = false and true
3590 {1, 0}, // face 2, face_orientation = false and true
3591 {0, 1}, // face 3, face_orientation = false and true
3592 {0, 0}, // face 4, invalid face
3593 {0, 0}}, // face 5, invalid face
3594 // hexahedron
3595 {{0, 1}, {1, 0}, {0, 1}, {1, 0}, {0, 1}, {1, 0}}};
3596
3597 // now create a vector of the two active neighbors (left and right)
3598 // for each face and fill it by looping over all cells. For cases with
3599 // anisotropic refinement and more than one cell neighboring at a
3600 // given side of the face we will automatically get the active one on
3601 // the highest level as we loop over cells from lower levels first.
3602 std::vector<typename Triangulation<dim, spacedim>::cell_iterator>
3603 adjacent_cells(2 * triangulation.n_raw_faces(), triangulation.end());
3604
3605 for (const auto &cell : triangulation.cell_iterators())
3606 for (auto f : cell->face_indices())
3607 {
3609 cell->face(f);
3610
3611 const unsigned int offset =
3612 (cell->direction_flag() ?
3613 left_right_offset[dim - 2][f][cell->face_orientation(f)] :
3614 1 -
3615 left_right_offset[dim - 2][f][cell->face_orientation(f)]);
3616
3617 adjacent_cells[2 * face->index() + offset] = cell;
3618
3619 // if this cell is not refined, but the face is, then we'll have
3620 // to set our cell as neighbor for the child faces as
3621 // well. Fortunately the normal orientation of children will be
3622 // just the same.
3623 if (dim == 2)
3624 {
3625 if (cell->is_active() && face->has_children())
3626 {
3627 adjacent_cells[2 * face->child(0)->index() + offset] =
3628 cell;
3629 adjacent_cells[2 * face->child(1)->index() + offset] =
3630 cell;
3631 }
3632 }
3633 else // -> dim == 3
3634 {
3635 // We need the same as in 2d here. Furthermore, if the face
3636 // is refined with cut_x or cut_y then those children again
3637 // in the other direction, and if this cell is refined
3638 // isotropically (along the face) then the neighbor will
3639 // (probably) be refined as cut_x or cut_y along the
3640 // face. For those neighboring children cells, their
3641 // neighbor will be the current, inactive cell, as our
3642 // children are too fine to be neighbors. Catch that case by
3643 // also acting on inactive cells with isotropic refinement
3644 // along the face. If the situation described is not
3645 // present, the data will be overwritten later on when we
3646 // visit cells on finer levels, so no harm will be done.
3647 if (face->has_children() &&
3648 (cell->is_active() ||
3650 cell->refinement_case(), f) ==
3652 {
3653 for (unsigned int c = 0; c < face->n_children(); ++c)
3654 adjacent_cells[2 * face->child(c)->index() + offset] =
3655 cell;
3656 if (face->child(0)->has_children())
3657 {
3658 adjacent_cells[2 * face->child(0)->child(0)->index() +
3659 offset] = cell;
3660 adjacent_cells[2 * face->child(0)->child(1)->index() +
3661 offset] = cell;
3662 }
3663 if (face->child(1)->has_children())
3664 {
3665 adjacent_cells[2 * face->child(1)->child(0)->index() +
3666 offset] = cell;
3667 adjacent_cells[2 * face->child(1)->child(1)->index() +
3668 offset] = cell;
3669 }
3670 } // if cell active and face refined
3671 } // else -> dim==3
3672 } // for all faces of all cells
3673
3674 // now loop again over all cells and set the corresponding neighbor
3675 // cell. Note, that we have to use the opposite of the
3676 // left_right_offset in this case as we want the offset of the
3677 // neighbor, not our own.
3678 for (const auto &cell : triangulation.cell_iterators())
3679 for (auto f : cell->face_indices())
3680 {
3681 const unsigned int offset =
3682 (cell->direction_flag() ?
3683 left_right_offset[dim - 2][f][cell->face_orientation(f)] :
3684 1 -
3685 left_right_offset[dim - 2][f][cell->face_orientation(f)]);
3686 cell->set_neighbor(
3687 f, adjacent_cells[2 * cell->face(f)->index() + 1 - offset]);
3688 }
3689 }
3690
3691
3695 template <int dim, int spacedim>
3696 static void
3697 create_triangulation(const std::vector<Point<spacedim>> &vertices,
3698 const std::vector<CellData<dim>> &cells,
3699 const SubCellData &subcelldata,
3701 {
3702 AssertThrow(vertices.size() > 0, ExcMessage("No vertices given"));
3703 AssertThrow(cells.size() > 0, ExcMessage("No cells given"));
3704
3705 // Check that all cells have positive volume.
3706#ifndef _MSC_VER
3707 // TODO: The following code does not compile with MSVC. Find a way
3708 // around it
3709 if (dim == spacedim)
3710 for (unsigned int cell_no = 0; cell_no < cells.size(); ++cell_no)
3711 {
3712 // If we should check for distorted cells, then we permit them
3713 // to exist. If a cell has negative measure, then it must be
3714 // distorted (the converse is not necessarily true); hence
3715 // throw an exception if no such cells should exist.
3717 {
3718 const double cell_measure = GridTools::cell_measure<spacedim>(
3719 vertices,
3720 ArrayView<const unsigned int>(cells[cell_no].vertices));
3721 AssertThrow(cell_measure > 0, ExcGridHasInvalidCell(cell_no));
3722 }
3723 }
3724#endif
3725
3726 // copy vertices
3727 tria.vertices = vertices;
3728 tria.vertices_used.assign(vertices.size(), true);
3729
3730 // compute connectivity
3731 const auto connectivity = build_connectivity(cells);
3732 const auto n_cells = cells.size();
3733
3734 // set up reference cells for the first time
3735 const auto all_reference_cells =
3736 Utilities::MPI::compute_set_union(connectivity.all_reference_cells(),
3737 tria.get_mpi_communicator());
3738 tria.reference_cells.assign(all_reference_cells.begin(),
3739 all_reference_cells.end());
3741 tria.reference_cells);
3742
3743 // clear old content
3744 tria.levels.clear();
3745 tria.levels.push_back(
3746 std::make_unique<
3748 tria.strides.max_children_per_cell,
3749 tria.strides.max_faces_per_cell,
3750 tria.strides.max_vertices_per_cell));
3751
3752 if (dim > 1)
3753 tria.faces = std::make_unique<
3755 tria.strides.max_children_per_face,
3756 tria.strides.max_lines_per_face);
3757
3758 const ArrayOfArrays empty;
3759 const ArrayOfArrays &lines_to_vertices =
3760 dim > 1 ? connectivity.entity_to_entities(1, 0) : empty;
3761 const ArrayOfArrays &quads_to_lines =
3762 dim == 3 ? connectivity.entity_to_entities(2, 1) : empty;
3763 const auto n_lines = lines_to_vertices.size();
3765 n_lines <= std::size_t(std::numeric_limits<int>::max()),
3766 ExcMessage("The number of lines must be less than the largest int."));
3767 const auto n_quads = quads_to_lines.size();
3769 n_quads <= std::size_t(std::numeric_limits<int>::max()),
3770 ExcMessage("The number of quads must be less than the largest int."));
3771
3772 if (dim > 1)
3773 tria.faces->allocate(n_lines, n_quads);
3774
3775 // TriaObjects: lines
3776 if (dim > 1)
3777 {
3778 auto &faces = *tria.faces;
3779 // loop over lines
3780 for (int line = 0; line < int(n_lines); ++line)
3781 {
3782 const auto vertices = lines_to_vertices[line];
3783 for (unsigned int v = 0; v < vertices.size(); ++v)
3784 faces.lines.set_bounding_object(line, v, vertices[v]);
3785 }
3786 }
3787
3788 // TriaObjects: quads
3789 if constexpr (dim == 3)
3790 {
3791 auto &faces = *tria.faces;
3792 // get connectivity between quads and lines
3793
3794 // loop over all quads -> entity type, line indices/orientations
3795 for (int q = 0, k = 0; q < int(n_quads); ++q)
3796 {
3797 // set entity type of quads
3798 const auto face_reference_cell =
3799 connectivity.template entity_types<2>()[q];
3800 faces.set_quad_type(q, face_reference_cell);
3801
3802 // loop over all its lines
3803 const auto lines = quads_to_lines[q];
3804 for (unsigned int l = 0; l < lines.size(); ++l, ++k)
3805 {
3806 AssertIndexRange(l, face_reference_cell.n_lines());
3807 // set line index
3808 faces.quads.set_bounding_object(q, l, lines[l]);
3809
3810 // set line orientations
3811 faces.set_line_orientation(
3812 q, l, connectivity.entity_orientations(1)[k]);
3813 }
3814 }
3815 }
3816
3817 // TriaObjects/TriaLevel: cell
3818 {
3819 auto &cells_0 = tria.levels[0]->cells; // data structure to be filled
3820 auto &level = *tria.levels[0];
3821
3822 // get connectivity between cells/faces and cells/cells
3823 const auto &cells_to_faces =
3824 connectivity.entity_to_entities(dim, dim - 1);
3825 const auto &cells_to_neighbor_cells =
3826 connectivity.entity_to_entities(dim, dim);
3827
3828 // in 2d optional: since in in pure QUAD meshes same line
3829 // orientations can be guaranteed
3830 bool orientation_needed = false;
3831 if (dim == 3)
3832 orientation_needed = true;
3833 else if (dim == 2)
3834 {
3835 const auto &orientations = connectivity.entity_orientations(1);
3836 orientation_needed = std::any_of(
3837 orientations.begin(), orientations.end(), [](const auto &a) {
3838 return a != numbers::default_geometric_orientation;
3839 });
3840 }
3841
3842 level.allocate(n_cells, orientation_needed);
3843 // loop over all cells
3844 unsigned int global_face_index = 0;
3845 for (unsigned int cell = 0; cell < n_cells; ++cell)
3846 {
3847 // set material ids
3848 cells_0.boundary_or_material_id[cell].material_id =
3849 cells[cell].material_id;
3850
3851 // set manifold ids
3852 cells_0.manifold_id[cell] = cells[cell].manifold_id;
3853
3854 // set entity types
3855 level.reference_cell[cell] =
3856 connectivity.template entity_types<dim>()[cell];
3857 Assert(all_reference_cells.count(level.reference_cell[cell]) ==
3858 1u,
3860
3861 // loop over faces
3862 const auto faces = cells_to_faces[cell];
3863 const auto neighbors = cells_to_neighbor_cells[cell];
3864 for (unsigned int f = 0; f < faces.size();
3865 ++f, ++global_face_index)
3866 {
3867 // set neighbor if not at boundary
3868 if (neighbors[f] != numbers::invalid_unsigned_int)
3869 level.set_neighbor(cell, f, 0, neighbors[f]);
3870
3871 // set face indices
3872 cells_0.set_bounding_object(cell, f, faces[f]);
3873
3874 // set face orientation if needed
3875 if (orientation_needed)
3876 {
3877 level.face_orientations.set_combined_orientation(
3878 cell,
3879 f,
3880 connectivity.entity_orientations(dim -
3881 1)[global_face_index]);
3882 }
3883 }
3884 }
3885 }
3886
3887 // TriaFaces: boundary id of boundary faces
3888 if (dim > 1)
3889 {
3890 auto &bids_face = dim == 3 ?
3891 tria.faces->quads.boundary_or_material_id :
3892 tria.faces->lines.boundary_or_material_id;
3893
3894 // count number of cells a face is belonging to
3895 std::vector<unsigned int> count(bids_face.size(), 0);
3896
3897 // get connectivity between cells/faces
3898 const auto &cells_to_faces =
3899 connectivity.entity_to_entities(dim, dim - 1);
3900
3901 // count how many cells are adjacent to the same face
3902 for (unsigned int cell = 0; cell < cells.size(); ++cell)
3903 for (const unsigned int face : cells_to_faces[cell])
3904 ++count[face];
3905
3906 // loop over all faces
3907 for (unsigned int face = 0; face < count.size(); ++face)
3908 {
3909 if (count[face] != 1) // inner face
3910 continue;
3911
3912 // boundary faces ...
3913 bids_face[face].boundary_id = 0;
3914
3915 if (dim != 3)
3916 continue;
3917
3918 // ... and the lines of quads in 3d
3919 const auto &quads_to_lines =
3920 connectivity.entity_to_entities(2, 1);
3921 for (const unsigned int line : quads_to_lines[face])
3922 tria.faces->lines.boundary_or_material_id[line].boundary_id =
3923 0;
3924 }
3925 }
3926 else // 1d
3927 {
3928 constexpr unsigned int t_tba = static_cast<unsigned int>(-1);
3929 constexpr unsigned int t_inner = static_cast<unsigned int>(-2);
3930
3931 std::vector<unsigned int> type(vertices.size(), t_tba);
3932
3933 const auto &cells_to_vertices =
3934 connectivity.entity_to_entities(1, 0);
3935
3936 for (unsigned int cell = 0; cell < cells.size(); ++cell)
3937 {
3938 const auto vertices = cells_to_vertices[cell];
3939 for (unsigned int v = 0; v < vertices.size(); ++v)
3940 if (type[vertices[v]] != t_inner)
3941 type[vertices[v]] =
3942 (type[vertices[v]] == t_tba ? v : t_inner);
3943 }
3944
3945 for (unsigned int face = 0; face < type.size(); ++face)
3946 {
3947 // note: we also treat manifolds here!?
3948 (*tria.vertex_to_manifold_id_map_1d)[face] =
3950 if (type[face] != t_inner && type[face] != t_tba)
3951 (*tria.vertex_to_boundary_id_map_1d)[face] = type[face];
3952 }
3953 }
3954
3955 // SubCellData: line
3956 if (dim >= 2)
3957 process_subcelldata(connectivity.entity_to_entities(1, 0),
3958 tria.faces->lines,
3959 subcelldata.boundary_lines,
3960 vertices);
3961
3962 // SubCellData: quad
3963 if (dim == 3)
3964 process_subcelldata(connectivity.entity_to_entities(2, 0),
3965 tria.faces->quads,
3966 subcelldata.boundary_quads,
3967 vertices);
3968 }
3969
3970
3971 template <int structdim, int spacedim>
3972 static void
3974 const ArrayOfArrays &crs,
3975 TriaObjects &obj,
3976 const std::vector<CellData<structdim>> &boundary_objects_in,
3977 const std::vector<Point<spacedim>> &vertex_locations)
3978 {
3979 AssertDimension(obj.structdim, structdim);
3980
3981 if (boundary_objects_in.empty())
3982 return; // empty subcelldata -> nothing to do
3983
3984 // pre-sort subcelldata
3985 auto boundary_objects = boundary_objects_in;
3986
3987 // ... sort vertices
3988 for (auto &boundary_object : boundary_objects)
3989 std::sort(boundary_object.vertices.begin(),
3990 boundary_object.vertices.end());
3991
3992 // ... sort cells
3993 std::sort(boundary_objects.begin(),
3994 boundary_objects.end(),
3995 [](const auto &a, const auto &b) {
3996 return a.vertices < b.vertices;
3997 });
3998
3999 [[maybe_unused]] unsigned int counter = 0;
4000
4002 ReferenceCells::max_n_vertices<structdim>()>
4003 key;
4004
4005 for (unsigned int o = 0; o < obj.n_objects(); ++o)
4006 {
4007 auto &boundary_id = obj.boundary_or_material_id[o].boundary_id;
4008 auto &manifold_id = obj.manifold_id[o];
4009
4010 // assert that object has not been visited yet and its value
4011 // has not been modified yet
4012 AssertThrow(boundary_id == 0 ||
4017
4018 // create key
4019 const ArrayView<const unsigned int> indices = crs[o];
4020 key.assign(indices.begin(), indices.end());
4021 // Sort the (at most four) indices with an insertion sort.
4022 // This is faster than std::sort() for such short ranges, and
4023 // it avoids a -Warray-bounds false positive of GCC 16 about the
4024 // code path std::sort() uses for long ranges.
4025 for (auto it = key.begin(); it != key.end(); ++it)
4026 std::rotate(std::upper_bound(key.begin(), it, *it),
4027 it,
4028 std::next(it));
4029
4030 // is subcelldata provided? -> binary search
4031 const auto subcell_object =
4032 std::lower_bound(boundary_objects.begin(),
4033 boundary_objects.end(),
4034 key,
4035 [&](const auto &cell, const auto &key) {
4036 return cell.vertices < key;
4037 });
4038
4039 // no subcelldata provided for this object
4040 if (subcell_object == boundary_objects.end() ||
4041 subcell_object->vertices != key)
4042 continue;
4043
4044 ++counter;
4045
4046 // set manifold id
4047 manifold_id = subcell_object->manifold_id;
4048
4049 // set boundary id
4050 if (subcell_object->boundary_id !=
4052 {
4055 ExcMessage(
4056 "The input arguments for creating a triangulation "
4057 "specified a boundary id for an internal face. This "
4058 "is not allowed."
4059 "\n\n"
4060 "The object in question has vertex indices " +
4061 [subcell_object]() {
4062 std::string s;
4063 for (const auto v : subcell_object->vertices)
4064 s += std::to_string(v) + ',';
4065 return s;
4066 }() +
4067 " which are located at coordinates " +
4068 [vertex_locations, subcell_object]() {
4069 std::ostringstream s;
4070 for (unsigned int i = 0;
4071 i < subcell_object->vertices.size();
4072 ++i)
4073 s << '('
4074 << vertex_locations[subcell_object->vertices[i]]
4075 << (i != subcell_object->vertices.size() - 1 ? "), " :
4076 ")");
4077 return s.str();
4078 }() +
4079 "."));
4080 boundary_id = subcell_object->boundary_id;
4081 }
4082 }
4083
4084 // make sure that all subcelldata entries have been processed
4085 // TODO: this is not guaranteed, why?
4086 // AssertDimension(counter, boundary_objects_in.size());
4087 }
4088
4089
4090
4106 template <int spacedim>
4107 static void
4110 std::vector<unsigned int> &,
4111 std::vector<unsigned int> &)
4112 {
4113 const unsigned int dim = 1;
4114
4115 // first we need to reset the
4116 // neighbor pointers of the
4117 // neighbors of this cell's
4118 // children to this cell. This is
4119 // different for one dimension,
4120 // since there neighbors can have a
4121 // refinement level differing from
4122 // that of this cell's children by
4123 // more than one level.
4124
4125 Assert(!cell->child(0)->has_children() &&
4126 !cell->child(1)->has_children(),
4128
4129 // first do it for the cells to the
4130 // left
4131 if (cell->neighbor(0).state() == IteratorState::valid)
4132 if (cell->neighbor(0)->has_children())
4133 {
4135 cell->neighbor(0);
4136 Assert(neighbor->level() == cell->level(), ExcInternalError());
4137
4138 // right child
4139 neighbor = neighbor->child(1);
4140 while (true)
4141 {
4142 Assert(neighbor->neighbor(1) == cell->child(0),
4144 neighbor->set_neighbor(1, cell);
4145
4146 // move on to further
4147 // children on the
4148 // boundary between this
4149 // cell and its neighbor
4150 if (neighbor->has_children())
4151 neighbor = neighbor->child(1);
4152 else
4153 break;
4154 }
4155 }
4156
4157 // now do it for the cells to the
4158 // left
4159 if (cell->neighbor(1).state() == IteratorState::valid)
4160 if (cell->neighbor(1)->has_children())
4161 {
4163 cell->neighbor(1);
4164 Assert(neighbor->level() == cell->level(), ExcInternalError());
4165
4166 // left child
4167 neighbor = neighbor->child(0);
4168 while (true)
4169 {
4170 Assert(neighbor->neighbor(0) == cell->child(1),
4172 neighbor->set_neighbor(0, cell);
4173
4174 // move on to further
4175 // children on the
4176 // boundary between this
4177 // cell and its neighbor
4178 if (neighbor->has_children())
4179 neighbor = neighbor->child(0);
4180 else
4181 break;
4182 }
4183 }
4184
4185
4186 // delete the vertex which will not
4187 // be needed anymore. This vertex
4188 // is the second of the first child
4189 triangulation.vertices_used[cell->child(0)->vertex_index(1)] = false;
4190
4191 // invalidate children. clear user
4192 // pointers, to avoid that they may
4193 // appear at unwanted places later
4194 // on...
4195 for (unsigned int child = 0; child < cell->n_children(); ++child)
4196 {
4197 cell->child(child)->clear_user_data();
4198 cell->child(child)->clear_user_flag();
4199 cell->child(child)->clear_used_flag();
4200 }
4201
4202
4203 // delete pointer to children
4204 cell->clear_children();
4205 cell->clear_user_flag();
4206 }
4207
4208
4209
4210 template <int spacedim>
4211 static void
4214 std::vector<unsigned int> &line_cell_count,
4215 std::vector<unsigned int> &)
4216 {
4217 const unsigned int dim = 2;
4218 const RefinementCase<dim> ref_case = cell->refinement_case();
4219
4220 Assert(line_cell_count.size() == triangulation.n_raw_lines(),
4222
4223 // vectors to hold all lines which
4224 // may be deleted
4225 std::vector<typename Triangulation<dim, spacedim>::line_iterator>
4226 lines_to_delete(0);
4227
4228 lines_to_delete.reserve(4 * 2 + 4);
4229
4230 // now we decrease the counters for
4231 // lines contained in the child
4232 // cells
4233 for (unsigned int c = 0; c < cell->n_children(); ++c)
4234 {
4236 cell->child(c);
4237 for (unsigned int l = 0; l < GeometryInfo<dim>::lines_per_cell; ++l)
4238 --line_cell_count[child->line_index(l)];
4239 }
4240
4241
4242 // delete the vertex which will not
4243 // be needed anymore. This vertex
4244 // is the second of the second line
4245 // of the first child, if the cell
4246 // is refined with cut_xy, else there
4247 // is no inner vertex.
4248 // additionally delete unneeded inner
4249 // lines
4250 if (ref_case == RefinementCase<dim>::cut_xy)
4251 {
4252 triangulation
4253 .vertices_used[cell->child(0)->line(1)->vertex_index(1)] = false;
4254
4255 lines_to_delete.push_back(cell->child(0)->line(1));
4256 lines_to_delete.push_back(cell->child(0)->line(3));
4257 lines_to_delete.push_back(cell->child(3)->line(0));
4258 lines_to_delete.push_back(cell->child(3)->line(2));
4259 }
4260 else
4261 {
4262 unsigned int inner_face_no =
4263 ref_case == RefinementCase<dim>::cut_x ? 1 : 3;
4264
4265 // the inner line will not be
4266 // used any more
4267 lines_to_delete.push_back(cell->child(0)->line(inner_face_no));
4268 }
4269
4270 // invalidate children
4271 for (unsigned int child = 0; child < cell->n_children(); ++child)
4272 {
4273 cell->child(child)->clear_user_data();
4274 cell->child(child)->clear_user_flag();
4275 cell->child(child)->clear_used_flag();
4276 }
4277
4278
4279 // delete pointer to children
4280 cell->clear_children();
4281 cell->clear_refinement_case();
4282 cell->clear_user_flag();
4283
4284 // look at the refinement of outer
4285 // lines. if nobody needs those
4286 // anymore we can add them to the
4287 // list of lines to be deleted.
4288 for (unsigned int line_no = 0;
4289 line_no < GeometryInfo<dim>::lines_per_cell;
4290 ++line_no)
4291 {
4293 cell->line(line_no);
4294
4295 if (line->has_children())
4296 {
4297 // if one of the cell counters is
4298 // zero, the other has to be as well
4299
4300 Assert((line_cell_count[line->child_index(0)] == 0 &&
4301 line_cell_count[line->child_index(1)] == 0) ||
4302 (line_cell_count[line->child_index(0)] > 0 &&
4303 line_cell_count[line->child_index(1)] > 0),
4305
4306 if (line_cell_count[line->child_index(0)] == 0)
4307 {
4308 for (unsigned int c = 0; c < 2; ++c)
4309 Assert(!line->child(c)->has_children(),
4311
4312 // we may delete the line's
4313 // children and the middle vertex
4314 // as no cell references them
4315 // anymore
4316 triangulation
4317 .vertices_used[line->child(0)->vertex_index(1)] = false;
4318
4319 lines_to_delete.push_back(line->child(0));
4320 lines_to_delete.push_back(line->child(1));
4321
4322 line->clear_children();
4323 }
4324 }
4325 }
4326
4327 // finally, delete unneeded lines
4328
4329 // clear user pointers, to avoid that
4330 // they may appear at unwanted places
4331 // later on...
4332 // same for user flags, then finally
4333 // delete the lines
4334 typename std::vector<
4336 line = lines_to_delete.begin(),
4337 endline = lines_to_delete.end();
4338 for (; line != endline; ++line)
4339 {
4340 (*line)->clear_user_data();
4341 (*line)->clear_user_flag();
4342 (*line)->clear_used_flag();
4343 }
4344 }
4345
4346
4347
4348 template <int spacedim>
4349 static void
4352 std::vector<unsigned int> &line_cell_count,
4353 std::vector<unsigned int> &quad_cell_count)
4354 {
4355 const unsigned int dim = 3;
4356
4357 Assert(line_cell_count.size() == triangulation.n_raw_lines(),
4359 Assert(quad_cell_count.size() == triangulation.n_raw_quads(),
4361
4362 // first of all, we store the RefineCase of
4363 // this cell
4364 const RefinementCase<dim> ref_case = cell->refinement_case();
4365 // vectors to hold all lines and quads which
4366 // may be deleted
4367 std::vector<typename Triangulation<dim, spacedim>::line_iterator>
4368 lines_to_delete(0);
4369 std::vector<typename Triangulation<dim, spacedim>::quad_iterator>
4370 quads_to_delete(0);
4371
4372 lines_to_delete.reserve(12 * 2 + 6 * 4 + 6);
4373 quads_to_delete.reserve(6 * 4 + 12);
4374
4375 // now we decrease the counters for lines and
4376 // quads contained in the child cells
4377 for (unsigned int c = 0; c < cell->n_children(); ++c)
4378 {
4380 cell->child(c);
4381 const auto line_indices = TriaAccessorImplementation::
4382 Implementation::get_line_indices_of_cell(*child);
4383 for (const unsigned int l : cell->line_indices())
4384 --line_cell_count[line_indices[l]];
4385 for (auto f : GeometryInfo<dim>::face_indices())
4386 --quad_cell_count[child->quad_index(f)];
4387 }
4388
4389 //-------------------------------------
4390 // delete interior quads and lines and the
4391 // interior vertex, depending on the
4392 // refinement case of the cell
4393 //
4394 // for append quads and lines: only append
4395 // them to the list of objects to be deleted
4396
4397 switch (ref_case)
4398 {
4400 quads_to_delete.push_back(cell->child(0)->face(1));
4401 break;
4403 quads_to_delete.push_back(cell->child(0)->face(3));
4404 break;
4406 quads_to_delete.push_back(cell->child(0)->face(5));
4407 break;
4409 quads_to_delete.push_back(cell->child(0)->face(1));
4410 quads_to_delete.push_back(cell->child(0)->face(3));
4411 quads_to_delete.push_back(cell->child(3)->face(0));
4412 quads_to_delete.push_back(cell->child(3)->face(2));
4413
4414 lines_to_delete.push_back(cell->child(0)->line(11));
4415 break;
4417 quads_to_delete.push_back(cell->child(0)->face(1));
4418 quads_to_delete.push_back(cell->child(0)->face(5));
4419 quads_to_delete.push_back(cell->child(3)->face(0));
4420 quads_to_delete.push_back(cell->child(3)->face(4));
4421
4422 lines_to_delete.push_back(cell->child(0)->line(5));
4423 break;
4425 quads_to_delete.push_back(cell->child(0)->face(3));
4426 quads_to_delete.push_back(cell->child(0)->face(5));
4427 quads_to_delete.push_back(cell->child(3)->face(2));
4428 quads_to_delete.push_back(cell->child(3)->face(4));
4429
4430 lines_to_delete.push_back(cell->child(0)->line(7));
4431 break;
4433 quads_to_delete.push_back(cell->child(0)->face(1));
4434 quads_to_delete.push_back(cell->child(2)->face(1));
4435 quads_to_delete.push_back(cell->child(4)->face(1));
4436 quads_to_delete.push_back(cell->child(6)->face(1));
4437
4438 quads_to_delete.push_back(cell->child(0)->face(3));
4439 quads_to_delete.push_back(cell->child(1)->face(3));
4440 quads_to_delete.push_back(cell->child(4)->face(3));
4441 quads_to_delete.push_back(cell->child(5)->face(3));
4442
4443 quads_to_delete.push_back(cell->child(0)->face(5));
4444 quads_to_delete.push_back(cell->child(1)->face(5));
4445 quads_to_delete.push_back(cell->child(2)->face(5));
4446 quads_to_delete.push_back(cell->child(3)->face(5));
4447
4448 lines_to_delete.push_back(cell->child(0)->line(5));
4449 lines_to_delete.push_back(cell->child(0)->line(7));
4450 lines_to_delete.push_back(cell->child(0)->line(11));
4451 lines_to_delete.push_back(cell->child(7)->line(0));
4452 lines_to_delete.push_back(cell->child(7)->line(2));
4453 lines_to_delete.push_back(cell->child(7)->line(8));
4454 // delete the vertex which will not
4455 // be needed anymore. This vertex
4456 // is the vertex at the heart of
4457 // this cell, which is the sixth of
4458 // the first child
4459 triangulation.vertices_used[cell->child(0)->vertex_index(7)] =
4460 false;
4461 break;
4462 default:
4463 // only remaining case is
4464 // no_refinement, thus an error
4466 break;
4467 }
4468
4469
4470 // invalidate children
4471 for (unsigned int child = 0; child < cell->n_children(); ++child)
4472 {
4473 cell->child(child)->clear_user_data();
4474 cell->child(child)->clear_user_flag();
4475
4476 for (auto f : GeometryInfo<dim>::face_indices())
4477 // set flags denoting deviations from standard orientation of
4478 // faces back to initialization values
4479 cell->child(child)->set_combined_face_orientation(
4481
4482 cell->child(child)->clear_used_flag();
4483 }
4484
4485
4486 // delete pointer to children
4487 cell->clear_children();
4488 cell->clear_refinement_case();
4489 cell->clear_user_flag();
4490
4491 // so far we only looked at inner quads,
4492 // lines and vertices. Now we have to
4493 // consider outer ones as well. here, we have
4494 // to check, whether there are other cells
4495 // still needing these objects. otherwise we
4496 // can delete them. first for quads (and
4497 // their inner lines).
4498
4499 for (const unsigned int quad_no : GeometryInfo<dim>::face_indices())
4500 {
4502 cell->face(quad_no);
4503
4504 Assert(
4505 (GeometryInfo<dim>::face_refinement_case(ref_case, quad_no) &&
4506 quad->has_children()) ||
4507 GeometryInfo<dim>::face_refinement_case(ref_case, quad_no) ==
4510
4511 switch (quad->refinement_case())
4512 {
4513 case RefinementCase<dim - 1>::no_refinement:
4514 // nothing to do as the quad
4515 // is not refined
4516 break;
4517 case RefinementCase<dim - 1>::cut_x:
4518 case RefinementCase<dim - 1>::cut_y:
4519 {
4520 // if one of the cell counters is
4521 // zero, the other has to be as
4522 // well
4523 Assert((quad_cell_count[quad->child_index(0)] == 0 &&
4524 quad_cell_count[quad->child_index(1)] == 0) ||
4525 (quad_cell_count[quad->child_index(0)] > 0 &&
4526 quad_cell_count[quad->child_index(1)] > 0),
4528 // it might be, that the quad is
4529 // refined twice anisotropically,
4530 // first check, whether we may
4531 // delete possible grand_children
4532 unsigned int deleted_grandchildren = 0;
4533 unsigned int number_of_child_refinements = 0;
4534
4535 for (unsigned int c = 0; c < 2; ++c)
4536 if (quad->child(c)->has_children())
4537 {
4538 ++number_of_child_refinements;
4539 // if one of the cell counters is
4540 // zero, the other has to be as
4541 // well
4542 Assert(
4543 (quad_cell_count[quad->child(c)->child_index(0)] ==
4544 0 &&
4545 quad_cell_count[quad->child(c)->child_index(1)] ==
4546 0) ||
4547 (quad_cell_count[quad->child(c)->child_index(0)] >
4548 0 &&
4549 quad_cell_count[quad->child(c)->child_index(1)] >
4550 0),
4552 if (quad_cell_count[quad->child(c)->child_index(0)] ==
4553 0)
4554 {
4555 // Assert, that the two
4556 // anisotropic
4557 // refinements add up to
4558 // isotropic refinement
4559 Assert(quad->refinement_case() +
4560 quad->child(c)->refinement_case() ==
4563 // we may delete the
4564 // quad's children and
4565 // the inner line as no
4566 // cell references them
4567 // anymore
4568 quads_to_delete.push_back(
4569 quad->child(c)->child(0));
4570 quads_to_delete.push_back(
4571 quad->child(c)->child(1));
4572 if (quad->child(c)->refinement_case() ==
4574 lines_to_delete.push_back(
4575 quad->child(c)->child(0)->line(1));
4576 else
4577 lines_to_delete.push_back(
4578 quad->child(c)->child(0)->line(3));
4579 quad->child(c)->clear_children();
4580 quad->child(c)->clear_refinement_case();
4581 ++deleted_grandchildren;
4582 }
4583 }
4584 // if no grandchildren are left, we
4585 // may as well delete the
4586 // refinement of the inner line
4587 // between our children and the
4588 // corresponding vertex
4589 if (number_of_child_refinements > 0 &&
4590 deleted_grandchildren == number_of_child_refinements)
4591 {
4593 middle_line;
4594 if (quad->refinement_case() == RefinementCase<2>::cut_x)
4595 middle_line = quad->child(0)->line(1);
4596 else
4597 middle_line = quad->child(0)->line(3);
4598
4599 lines_to_delete.push_back(middle_line->child(0));
4600 lines_to_delete.push_back(middle_line->child(1));
4601 triangulation
4602 .vertices_used[middle_vertex_index<dim, spacedim>(
4603 middle_line)] = false;
4604 middle_line->clear_children();
4605 }
4606
4607 // now consider the direct children
4608 // of the given quad
4609 if (quad_cell_count[quad->child_index(0)] == 0)
4610 {
4611 // we may delete the quad's
4612 // children and the inner line
4613 // as no cell references them
4614 // anymore
4615 quads_to_delete.push_back(quad->child(0));
4616 quads_to_delete.push_back(quad->child(1));
4617 if (quad->refinement_case() == RefinementCase<2>::cut_x)
4618 lines_to_delete.push_back(quad->child(0)->line(1));
4619 else
4620 lines_to_delete.push_back(quad->child(0)->line(3));
4621
4622 // if the counters just dropped
4623 // to zero, otherwise the
4624 // children would have been
4625 // deleted earlier, then this
4626 // cell's children must have
4627 // contained the anisotropic
4628 // quad children. thus, if
4629 // those have again anisotropic
4630 // children, which are in
4631 // effect isotropic children of
4632 // the original quad, those are
4633 // still needed by a
4634 // neighboring cell and we
4635 // cannot delete them. instead,
4636 // we have to reset this quad's
4637 // refine case to isotropic and
4638 // set the children
4639 // accordingly.
4640 if (quad->child(0)->has_children())
4641 if (quad->refinement_case() ==
4643 {
4644 // now evereything is
4645 // quite complicated. we
4646 // have the children
4647 // numbered according to
4648 //
4649 // *---*---*
4650 // |n+1|m+1|
4651 // *---*---*
4652 // | n | m |
4653 // *---*---*
4654 //
4655 // from the original
4656 // anisotropic
4657 // refinement. we have to
4658 // reorder them as
4659 //
4660 // *---*---*
4661 // | m |m+1|
4662 // *---*---*
4663 // | n |n+1|
4664 // *---*---*
4665 //
4666 // for isotropic refinement.
4667 //
4668 // this is a bit ugly, of
4669 // course: loop over all
4670 // cells on all levels
4671 // and look for faces n+1
4672 // (switch_1) and m
4673 // (switch_2).
4674 const typename Triangulation<dim, spacedim>::
4675 quad_iterator switch_1 =
4676 quad->child(0)->child(1),
4677 switch_2 =
4678 quad->child(1)->child(0);
4679
4680 Assert(!switch_1->has_children(),
4682 Assert(!switch_2->has_children(),
4684
4685 const int switch_1_index = switch_1->index();
4686 const int switch_2_index = switch_2->index();
4687 for (unsigned int l = 0;
4688 l < triangulation.levels.size();
4689 ++l)
4690 for (unsigned int h = 0;
4691 h <
4692 triangulation.levels[l]->cells.n_objects();
4693 ++h)
4694 for (const unsigned int q :
4696 {
4697 const int index =
4698 triangulation.levels[l]
4699 ->cells.get_bounding_object_indices(
4700 h)[q];
4701 if (index == switch_1_index)
4702 triangulation.levels[l]
4703 ->cells.get_bounding_object_indices(
4704 h)[q] = switch_2_index;
4705 else if (index == switch_2_index)
4706 triangulation.levels[l]
4707 ->cells.get_bounding_object_indices(
4708 h)[q] = switch_1_index;
4709 }
4710 // now we have to copy
4711 // all information of the
4712 // two quads
4713 int switch_1_lines[4], switch_2_lines[4];
4714 for (int i = 0; i < 4; ++i)
4715 {
4716 switch_1_lines[i] =
4717 switch_1->line(i)->index();
4718 switch_2_lines[i] =
4719 switch_2->line(i)->index();
4720 }
4722 switch_1_line_orientations[4] = {
4723 switch_1->line_orientation(0),
4724 switch_1->line_orientation(1),
4725 switch_1->line_orientation(2),
4726 switch_1->line_orientation(3)};
4727 const types::boundary_id switch_1_boundary_id =
4728 switch_1->boundary_id();
4729 const unsigned int switch_1_user_index =
4730 switch_1->user_index();
4731 const bool switch_1_user_flag =
4732 switch_1->user_flag_set();
4733
4734 switch_1->set_bounding_object_indices(
4735 {switch_2_lines[0],
4736 switch_2_lines[1],
4737 switch_2_lines[2],
4738 switch_2_lines[3]});
4739 switch_1->set_line_orientation(
4740 0, switch_2->line_orientation(0));
4741 switch_1->set_line_orientation(
4742 1, switch_2->line_orientation(1));
4743 switch_1->set_line_orientation(
4744 2, switch_2->line_orientation(2));
4745 switch_1->set_line_orientation(
4746 3, switch_2->line_orientation(3));
4747 switch_1->set_boundary_id_internal(
4748 switch_2->boundary_id());
4749 switch_1->set_manifold_id(
4750 switch_2->manifold_id());
4751 switch_1->set_user_index(switch_2->user_index());
4752 if (switch_2->user_flag_set())
4753 switch_1->set_user_flag();
4754 else
4755 switch_1->clear_user_flag();
4756
4757 switch_2->set_bounding_object_indices(
4758 {switch_1_lines[0],
4759 switch_1_lines[1],
4760 switch_1_lines[2],
4761 switch_1_lines[3]});
4762 switch_2->set_line_orientation(
4763 0, switch_1_line_orientations[0]);
4764 switch_2->set_line_orientation(
4765 1, switch_1_line_orientations[1]);
4766 switch_2->set_line_orientation(
4767 2, switch_1_line_orientations[2]);
4768 switch_2->set_line_orientation(
4769 3, switch_1_line_orientations[3]);
4770 switch_2->set_boundary_id_internal(
4771 switch_1_boundary_id);
4772 switch_2->set_manifold_id(
4773 switch_1->manifold_id());
4774 switch_2->set_user_index(switch_1_user_index);
4775 if (switch_1_user_flag)
4776 switch_2->set_user_flag();
4777 else
4778 switch_2->clear_user_flag();
4779
4780 const unsigned int child_0 =
4781 quad->child(0)->child_index(0);
4782 const unsigned int child_2 =
4783 quad->child(1)->child_index(0);
4784 quad->clear_children();
4785 quad->clear_refinement_case();
4786 quad->set_refinement_case(
4788 quad->set_children(0, child_0);
4789 quad->set_children(2, child_2);
4790 std::swap(quad_cell_count[child_0 + 1],
4791 quad_cell_count[child_2]);
4792 }
4793 else
4794 {
4795 // the face was refined
4796 // with cut_y, thus the
4797 // children are already
4798 // in correct order. we
4799 // only have to set them
4800 // correctly, deleting
4801 // the indirection of two
4802 // anisotropic refinement
4803 // and going directly
4804 // from the quad to
4805 // isotropic children
4806 const unsigned int child_0 =
4807 quad->child(0)->child_index(0);
4808 const unsigned int child_2 =
4809 quad->child(1)->child_index(0);
4810 quad->clear_children();
4811 quad->clear_refinement_case();
4812 quad->set_refinement_case(
4814 quad->set_children(0, child_0);
4815 quad->set_children(2, child_2);
4816 }
4817 else
4818 {
4819 quad->clear_children();
4820 quad->clear_refinement_case();
4821 }
4822 }
4823 break;
4824 }
4825 case RefinementCase<dim - 1>::cut_xy:
4826 {
4827 // if one of the cell counters is
4828 // zero, the others have to be as
4829 // well
4830
4831 Assert((quad_cell_count[quad->child_index(0)] == 0 &&
4832 quad_cell_count[quad->child_index(1)] == 0 &&
4833 quad_cell_count[quad->child_index(2)] == 0 &&
4834 quad_cell_count[quad->child_index(3)] == 0) ||
4835 (quad_cell_count[quad->child_index(0)] > 0 &&
4836 quad_cell_count[quad->child_index(1)] > 0 &&
4837 quad_cell_count[quad->child_index(2)] > 0 &&
4838 quad_cell_count[quad->child_index(3)] > 0),
4840
4841 if (quad_cell_count[quad->child_index(0)] == 0)
4842 {
4843 // we may delete the quad's
4844 // children, the inner lines
4845 // and the middle vertex as no
4846 // cell references them anymore
4847 lines_to_delete.push_back(quad->child(0)->line(1));
4848 lines_to_delete.push_back(quad->child(3)->line(0));
4849 lines_to_delete.push_back(quad->child(0)->line(3));
4850 lines_to_delete.push_back(quad->child(3)->line(2));
4851
4852 for (unsigned int child = 0; child < quad->n_children();
4853 ++child)
4854 quads_to_delete.push_back(quad->child(child));
4855
4856 triangulation
4857 .vertices_used[quad->child(0)->vertex_index(3)] =
4858 false;
4859
4860 quad->clear_children();
4861 quad->clear_refinement_case();
4862 }
4863 }
4864 break;
4865
4866 default:
4868 break;
4869 }
4870 }
4871
4872 // now we repeat a similar procedure
4873 // for the outer lines of this cell.
4874
4875 // if in debug mode: check that each
4876 // of the lines for which we consider
4877 // deleting the children in fact has
4878 // children (the bits/coarsening_3d
4879 // test tripped over this initially)
4880 for (unsigned int line_no = 0;
4881 line_no < GeometryInfo<dim>::lines_per_cell;
4882 ++line_no)
4883 {
4885 cell->line(line_no);
4886
4887 Assert(
4888 (GeometryInfo<dim>::line_refinement_case(ref_case, line_no) &&
4889 line->has_children()) ||
4890 GeometryInfo<dim>::line_refinement_case(ref_case, line_no) ==
4893
4894 if (line->has_children())
4895 {
4896 // if one of the cell counters is
4897 // zero, the other has to be as well
4898
4899 Assert((line_cell_count[line->child_index(0)] == 0 &&
4900 line_cell_count[line->child_index(1)] == 0) ||
4901 (line_cell_count[line->child_index(0)] > 0 &&
4902 line_cell_count[line->child_index(1)] > 0),
4904
4905 if (line_cell_count[line->child_index(0)] == 0)
4906 {
4907 for (unsigned int c = 0; c < 2; ++c)
4908 Assert(!line->child(c)->has_children(),
4910
4911 // we may delete the line's
4912 // children and the middle vertex
4913 // as no cell references them
4914 // anymore
4915 triangulation
4916 .vertices_used[line->child(0)->vertex_index(1)] = false;
4917
4918 lines_to_delete.push_back(line->child(0));
4919 lines_to_delete.push_back(line->child(1));
4920
4921 line->clear_children();
4922 }
4923 }
4924 }
4925
4926 // finally, delete unneeded quads and lines
4927
4928 // clear user pointers, to avoid that
4929 // they may appear at unwanted places
4930 // later on...
4931 // same for user flags, then finally
4932 // delete the quads and lines
4933 typename std::vector<
4935 line = lines_to_delete.begin(),
4936 endline = lines_to_delete.end();
4937 for (; line != endline; ++line)
4938 {
4939 (*line)->clear_user_data();
4940 (*line)->clear_user_flag();
4941 (*line)->clear_used_flag();
4942 }
4943
4944 typename std::vector<
4946 quad = quads_to_delete.begin(),
4947 endquad = quads_to_delete.end();
4948 for (; quad != endquad; ++quad)
4949 {
4950 (*quad)->clear_user_data();
4951 (*quad)->clear_children();
4952 (*quad)->clear_refinement_case();
4953 (*quad)->clear_user_flag();
4954 (*quad)->clear_used_flag();
4955 }
4956 }
4957
4958
4976 template <int spacedim>
4977 static void
4979 Triangulation<2, spacedim> &triangulation,
4980 unsigned int &next_unused_vertex,
4982 &next_unused_line,
4984 &next_unused_cell,
4985 const typename Triangulation<2, spacedim>::cell_iterator &cell)
4986 {
4987 const unsigned int dim = 2;
4988 // clear refinement flag
4989 const RefinementCase<dim> ref_case = cell->refine_flag_set();
4990 cell->clear_refine_flag();
4991
4992 /* For the refinement process: since we go the levels up from the
4993 lowest, there are (unlike above) only two possibilities: a neighbor
4994 cell is on the same level or one level up (in both cases, it may or
4995 may not be refined later on, but we don't care here).
4996
4997 First:
4998 Set up an array of the 3x3 vertices, which are distributed on the
4999 cell (the array consists of indices into the @p{vertices} std::vector
5000
5001 2--7--3
5002 | | |
5003 4--8--5
5004 | | |
5005 0--6--1
5006
5007 note: in case of cut_x or cut_y not all these vertices are needed for
5008 the new cells
5009
5010 Second:
5011 Set up an array of the new lines (the array consists of iterator
5012 pointers into the lines arrays)
5013
5014 .-6-.-7-. The directions are: .->-.->-.
5015 1 9 3 ^ ^ ^
5016 .-10.11-. .->-.->-.
5017 0 8 2 ^ ^ ^
5018 .-4-.-5-. .->-.->-.
5019
5020 cut_x:
5021 .-4-.-5-.
5022 | | |
5023 0 6 1
5024 | | |
5025 .-2-.-3-.
5026
5027 cut_y:
5028 .---5---.
5029 1 3
5030 .---6---.
5031 0 2
5032 .---4---.
5033
5034
5035 Third:
5036 Set up an array of neighbors:
5037
5038 6 7
5039 .--.--.
5040 1| | |3
5041 .--.--.
5042 0| | |2
5043 .--.--.
5044 4 5
5045
5046 We need this array for two reasons: first to get the lines which will
5047 bound the four subcells (if the neighboring cell is refined, these
5048 lines already exist), and second to update neighborship information.
5049 Since if a neighbor is not refined, its neighborship record only
5050 points to the present, unrefined, cell rather than the children we
5051 are presently creating, we only need the neighborship information
5052 if the neighbor cells are refined. In all other cases, we store
5053 the unrefined neighbor address
5054
5055 We also need for every neighbor (if refined) which number among its
5056 neighbors the present (unrefined) cell has, since that number is to
5057 be replaced and because that also is the number of the subline which
5058 will be the interface between that neighbor and the to be created
5059 cell. We will store this number (between 0 and 3) in the field
5060 @p{neighbors_neighbor}.
5061
5062 It would be sufficient to use the children of the common line to the
5063 neighbor, if we only wanted to get the new sublines and the new
5064 vertex, but because we need to update the neighborship information of
5065 the two refined subcells of the neighbor, we need to search these
5066 anyway.
5067
5068 Convention:
5069 The created children are numbered like this:
5070
5071 .--.--.
5072 |2 . 3|
5073 .--.--.
5074 |0 | 1|
5075 .--.--.
5076 */
5077 // collect the indices of the eight surrounding vertices
5078 // 2--7--3
5079 // | | |
5080 // 4--8--5
5081 // | | |
5082 // 0--6--1
5083 int new_vertices[9];
5084 for (unsigned int vertex_no = 0; vertex_no < 4; ++vertex_no)
5085 new_vertices[vertex_no] = cell->vertex_index(vertex_no);
5086 for (unsigned int line_no = 0; line_no < 4; ++line_no)
5087 if (cell->line(line_no)->has_children())
5088 new_vertices[4 + line_no] =
5089 cell->line(line_no)->child(0)->vertex_index(1);
5090
5091 if (ref_case == RefinementCase<dim>::cut_xy)
5092 {
5093 // find the next
5094 // unused vertex and
5095 // allocate it for
5096 // the new vertex we
5097 // need here
5098 while (triangulation.vertices_used[next_unused_vertex] == true)
5099 ++next_unused_vertex;
5100 Assert(next_unused_vertex < triangulation.vertices.size(),
5101 ExcMessage(
5102 "Internal error: During refinement, the triangulation "
5103 "wants to access an element of the 'vertices' array "
5104 "but it turns out that the array is not large enough."));
5105 triangulation.vertices_used[next_unused_vertex] = true;
5106
5107 new_vertices[8] = next_unused_vertex;
5108
5109 // determine middle vertex by transfinite interpolation to be
5110 // consistent with what happens to quads in a
5111 // Triangulation<3,3> when they are refined
5112 triangulation.vertices[next_unused_vertex] =
5113 cell->center(true, true);
5114 }
5115
5116
5117 // Now the lines:
5119 unsigned int lmin = 8;
5120 unsigned int lmax = 12;
5121 if (ref_case != RefinementCase<dim>::cut_xy)
5122 {
5123 lmin = 6;
5124 lmax = 7;
5125 }
5126
5127 for (unsigned int l = lmin; l < lmax; ++l)
5128 {
5129 while (next_unused_line->used() == true)
5130 ++next_unused_line;
5131 new_lines[l] = next_unused_line;
5132 ++next_unused_line;
5133
5134 AssertIsNotUsed(new_lines[l]);
5135 }
5136
5137 if (ref_case == RefinementCase<dim>::cut_xy)
5138 {
5139 // .-6-.-7-.
5140 // 1 9 3
5141 // .-10.11-.
5142 // 0 8 2
5143 // .-4-.-5-.
5144
5145 // lines 0-7 already exist, create only the four interior
5146 // lines 8-11
5147 unsigned int l = 0;
5148 for (const unsigned int face_no : GeometryInfo<dim>::face_indices())
5149 for (unsigned int c = 0; c < 2; ++c, ++l)
5150 new_lines[l] = cell->line(face_no)->child(c);
5151 Assert(l == 8, ExcInternalError());
5152
5153 new_lines[8]->set_bounding_object_indices(
5154 {new_vertices[6], new_vertices[8]});
5155 new_lines[9]->set_bounding_object_indices(
5156 {new_vertices[8], new_vertices[7]});
5157 new_lines[10]->set_bounding_object_indices(
5158 {new_vertices[4], new_vertices[8]});
5159 new_lines[11]->set_bounding_object_indices(
5160 {new_vertices[8], new_vertices[5]});
5161 }
5162 else if (ref_case == RefinementCase<dim>::cut_x)
5163 {
5164 // .-4-.-5-.
5165 // | | |
5166 // 0 6 1
5167 // | | |
5168 // .-2-.-3-.
5169 new_lines[0] = cell->line(0);
5170 new_lines[1] = cell->line(1);
5171 new_lines[2] = cell->line(2)->child(0);
5172 new_lines[3] = cell->line(2)->child(1);
5173 new_lines[4] = cell->line(3)->child(0);
5174 new_lines[5] = cell->line(3)->child(1);
5175 new_lines[6]->set_bounding_object_indices(
5176 {new_vertices[6], new_vertices[7]});
5177 }
5178 else
5179 {
5181 // .---5---.
5182 // 1 3
5183 // .---6---.
5184 // 0 2
5185 // .---4---.
5186 new_lines[0] = cell->line(0)->child(0);
5187 new_lines[1] = cell->line(0)->child(1);
5188 new_lines[2] = cell->line(1)->child(0);
5189 new_lines[3] = cell->line(1)->child(1);
5190 new_lines[4] = cell->line(2);
5191 new_lines[5] = cell->line(3);
5192 new_lines[6]->set_bounding_object_indices(
5193 {new_vertices[4], new_vertices[5]});
5194 }
5195
5196 for (unsigned int l = lmin; l < lmax; ++l)
5197 {
5198 new_lines[l]->set_used_flag();
5199 new_lines[l]->clear_user_flag();
5200 new_lines[l]->clear_user_data();
5201 new_lines[l]->clear_children();
5202 // interior line
5203 new_lines[l]->set_boundary_id_internal(
5205 new_lines[l]->set_manifold_id(cell->manifold_id());
5206 }
5207
5208 // Now add the four (two)
5209 // new cells!
5211 subcells[ReferenceCells::max_n_children<dim>()];
5212 while (next_unused_cell->used() == true)
5213 ++next_unused_cell;
5214
5215 const unsigned int n_children = GeometryInfo<dim>::n_children(ref_case);
5216 for (unsigned int i = 0; i < n_children; ++i)
5217 {
5218 AssertIsNotUsed(next_unused_cell);
5219 subcells[i] = next_unused_cell;
5220 ++next_unused_cell;
5221 if (i % 2 == 1 && i < n_children - 1)
5222 while (next_unused_cell->used() == true)
5223 ++next_unused_cell;
5224 }
5225
5226 if (ref_case == RefinementCase<dim>::cut_xy)
5227 {
5228 // children:
5229 // .--.--.
5230 // |2 . 3|
5231 // .--.--.
5232 // |0 | 1|
5233 // .--.--.
5234 // lines:
5235 // .-6-.-7-.
5236 // 1 9 3
5237 // .-10.11-.
5238 // 0 8 2
5239 // .-4-.-5-.
5240 subcells[0]->set_bounding_object_indices({new_lines[0]->index(),
5241 new_lines[8]->index(),
5242 new_lines[4]->index(),
5243 new_lines[10]->index()});
5244 subcells[1]->set_bounding_object_indices({new_lines[8]->index(),
5245 new_lines[2]->index(),
5246 new_lines[5]->index(),
5247 new_lines[11]->index()});
5248 subcells[2]->set_bounding_object_indices({new_lines[1]->index(),
5249 new_lines[9]->index(),
5250 new_lines[10]->index(),
5251 new_lines[6]->index()});
5252 subcells[3]->set_bounding_object_indices({new_lines[9]->index(),
5253 new_lines[3]->index(),
5254 new_lines[11]->index(),
5255 new_lines[7]->index()});
5256 }
5257 else if (ref_case == RefinementCase<dim>::cut_x)
5258 {
5259 // children:
5260 // .--.--.
5261 // | . |
5262 // .0 . 1.
5263 // | | |
5264 // .--.--.
5265 // lines:
5266 // .-4-.-5-.
5267 // | | |
5268 // 0 6 1
5269 // | | |
5270 // .-2-.-3-.
5271 subcells[0]->set_bounding_object_indices({new_lines[0]->index(),
5272 new_lines[6]->index(),
5273 new_lines[2]->index(),
5274 new_lines[4]->index()});
5275 subcells[1]->set_bounding_object_indices({new_lines[6]->index(),
5276 new_lines[1]->index(),
5277 new_lines[3]->index(),
5278 new_lines[5]->index()});
5279 }
5280 else
5281 {
5283 // children:
5284 // .-----.
5285 // | 1 |
5286 // .-----.
5287 // | 0 |
5288 // .-----.
5289 // lines:
5290 // .---5---.
5291 // 1 3
5292 // .---6---.
5293 // 0 2
5294 // .---4---.
5295 subcells[0]->set_bounding_object_indices({new_lines[0]->index(),
5296 new_lines[2]->index(),
5297 new_lines[4]->index(),
5298 new_lines[6]->index()});
5299 subcells[1]->set_bounding_object_indices({new_lines[1]->index(),
5300 new_lines[3]->index(),
5301 new_lines[6]->index(),
5302 new_lines[5]->index()});
5303 }
5304
5305 types::subdomain_id subdomainid = cell->subdomain_id();
5306
5307 for (unsigned int i = 0; i < n_children; ++i)
5308 {
5309 subcells[i]->set_used_flag();
5310 subcells[i]->clear_refine_flag();
5311 subcells[i]->clear_user_flag();
5312 subcells[i]->clear_user_data();
5313 subcells[i]->clear_children();
5314 // inherit material properties
5315 subcells[i]->set_material_id(cell->material_id());
5316 subcells[i]->set_manifold_id(cell->manifold_id());
5317 subcells[i]->set_subdomain_id(subdomainid);
5318
5319 // We only store the parent for every second cell. That's because
5320 // cells are created during refinement in multiples of two, and so
5321 // two successive cells always share the same parent. As a
5322 // consequence, we can save a bit of work by skipping setting parent
5323 // indices for the odd children.
5324 if (i % 2 == 0)
5325 subcells[i]->set_parent(cell->index());
5326 }
5327
5328
5329
5330 // set child index for even children i=0,2 (0)
5331 for (unsigned int i = 0; i < n_children / 2; ++i)
5332 cell->set_children(2 * i, subcells[2 * i]->index());
5333 // set the refine case
5334 cell->set_refinement_case(ref_case);
5335
5336 // note that the
5337 // refinement flag was
5338 // already cleared at the
5339 // beginning of this function
5340
5341 if (dim == spacedim - 1)
5342 for (unsigned int c = 0; c < n_children; ++c)
5343 cell->child(c)->set_direction_flag(cell->direction_flag());
5344 }
5345
5346
5347
5348 template <int spacedim>
5351 Triangulation<1, spacedim> & /*triangulation*/,
5352 const bool /*check_for_distorted_cells*/)
5353 {
5355 }
5356
5357
5358
5359 template <int spacedim>
5362 const bool check_for_distorted_cells)
5363 {
5364 constexpr int dim = 2;
5365
5366 // Check whether a new level is needed. We have to check for
5367 // this on the highest level only
5368 for (const auto &cell : triangulation.active_cell_iterators_on_level(
5369 triangulation.levels.size() - 1))
5370 if (cell->refine_flag_set())
5371 {
5372 triangulation.levels.push_back(
5373 std::make_unique<internal::TriangulationImplementation::
5375 triangulation.strides.max_children_per_cell,
5376 triangulation.strides.max_faces_per_cell,
5377 triangulation.strides.max_vertices_per_cell));
5378 break;
5379 }
5380
5382 triangulation.begin_line();
5383 line != triangulation.end_line();
5384 ++line)
5385 {
5386 line->clear_user_flag();
5387 line->clear_user_data();
5388 }
5389
5390 unsigned int n_single_lines = 0;
5391 unsigned int n_lines_in_pairs = 0;
5392 unsigned int needed_vertices = 0;
5393
5394 const bool orientation_needed =
5395 !triangulation.all_reference_cells_are_hyper_cube();
5396
5397 for (int level_no = triangulation.levels.size() - 2; level_no >= 0;
5398 --level_no)
5399 {
5400 // count number of flagged cells on this level and compute
5401 // how many new vertices and new lines will be needed
5402 std::size_t needed_cells = 0;
5403
5404 for (const auto &cell :
5405 triangulation.active_cell_iterators_on_level(level_no))
5406 if (cell->refine_flag_set())
5407 {
5408 if (cell->reference_cell() == ReferenceCells::Triangle)
5409 {
5410 needed_cells += 4;
5411 needed_vertices += 0;
5412 n_single_lines += 3;
5413 }
5414 else if (cell->reference_cell() ==
5416 {
5417 needed_cells += 4;
5418 needed_vertices += 1;
5419 n_single_lines += 4;
5420 }
5421 else
5422 {
5424 }
5425
5426 for (const auto line_no : cell->face_indices())
5427 {
5428 auto line = cell->line(line_no);
5429 if (line->has_children() == false)
5430 line->set_user_flag();
5431 }
5432 }
5433
5434 // count number of used cells on the next higher level
5435 auto &next_level = *triangulation.levels[level_no + 1];
5436 const auto used_cells = std::count(next_level.cells.used.begin(),
5437 next_level.cells.used.end(),
5438 true);
5439
5440 if (used_cells + needed_cells > next_level.size())
5441 next_level.allocate_end((used_cells + needed_cells) -
5442 next_level.size(),
5443 orientation_needed,
5444 // We are in 2d so there are no tetrahedra
5445 false);
5446
5447 // TODO: perhaps we can merge this with TriaLevel::allocate_end()
5448 next_level.cells.allocate_end(needed_cells, 0);
5449 }
5450
5451 for (auto line = triangulation.begin_line();
5452 line != triangulation.end_line();
5453 ++line)
5454 if (line->user_flag_set())
5455 {
5456 Assert(line->has_children() == false, ExcInternalError());
5457 n_lines_in_pairs += 2;
5458 needed_vertices += 1;
5459 }
5460
5461 triangulation.faces->lines.allocate_end(n_lines_in_pairs, 0);
5462
5463 needed_vertices += std::count(triangulation.vertices_used.begin(),
5464 triangulation.vertices_used.end(),
5465 true);
5466
5467 if (needed_vertices > triangulation.vertices.size())
5468 {
5469 triangulation.vertices.resize(needed_vertices, Point<spacedim>());
5470 triangulation.vertices_used.resize(needed_vertices, false);
5471 }
5472
5473 unsigned int next_unused_vertex = 0;
5474
5475 {
5477 line = triangulation.begin_active_line(),
5478 endl = triangulation.end_line();
5480 next_unused_line = triangulation.begin_raw_line();
5481
5482 for (; line != endl; ++line)
5483 if (line->user_flag_set())
5484 {
5485 // This line needs to be refined. Find the next unused vertex
5486 // and set it appropriately
5487 while (triangulation.vertices_used[next_unused_vertex] == true)
5488 ++next_unused_vertex;
5489 Assert(next_unused_vertex < triangulation.vertices.size(),
5490 ExcMessage(
5491 "Internal error: During refinement, the triangulation "
5492 "wants to access an element of the 'vertices' array "
5493 "but it turns out that the array is not large "
5494 "enough."));
5495 triangulation.vertices_used[next_unused_vertex] = true;
5496
5497 triangulation.vertices[next_unused_vertex] = line->center(true);
5498
5499 bool pair_found = false;
5500 for (; next_unused_line != endl; ++next_unused_line)
5501 if (!next_unused_line->used() &&
5502 !(++next_unused_line)->used())
5503 {
5504 --next_unused_line;
5505 pair_found = true;
5506 break;
5507 }
5508 Assert(pair_found, ExcInternalError());
5509
5510 line->set_children(0, next_unused_line->index());
5511
5513 children[2] = {next_unused_line, ++next_unused_line};
5514
5515 AssertIsNotUsed(children[0]);
5516 AssertIsNotUsed(children[1]);
5517
5518 children[0]->set_bounding_object_indices(
5519 {line->vertex_index(0), next_unused_vertex});
5520 children[1]->set_bounding_object_indices(
5521 {next_unused_vertex, line->vertex_index(1)});
5522
5523 for (auto &child : children)
5524 {
5525 child->set_used_flag();
5526 child->clear_children();
5527 child->clear_user_data();
5528 child->clear_user_flag();
5529 child->set_boundary_id_internal(line->boundary_id());
5530 child->set_manifold_id(line->manifold_id());
5531 // Line orientation is relative to the cell it is on so
5532 // those cannot be set at this point.
5533 }
5534
5535 line->clear_user_flag();
5536 }
5537 }
5538
5539 triangulation.faces->lines.allocate_end(0, n_single_lines);
5540
5541
5543 cells_with_distorted_children;
5544
5546 next_unused_line = triangulation.begin_raw_line();
5547
5548 const auto create_children = [](auto &triangulation,
5549 unsigned int &next_unused_vertex,
5550 auto &next_unused_line,
5551 auto &next_unused_cell,
5552 const auto &cell) {
5553 const auto ref_case = cell->refine_flag_set();
5554 cell->clear_refine_flag();
5555
5556 unsigned int n_new_vertices = 0;
5557 if (cell->reference_cell() == ReferenceCells::Triangle)
5558 n_new_vertices = 6;
5559 else if (cell->reference_cell() == ReferenceCells::Quadrilateral)
5560 n_new_vertices = 9;
5561 else
5563
5565 n_new_vertices, numbers::invalid_unsigned_int);
5566 for (unsigned int vertex_no = 0; vertex_no < cell->n_vertices();
5567 ++vertex_no)
5568 new_vertices[vertex_no] = cell->vertex_index(vertex_no);
5569 for (unsigned int line_no = 0; line_no < cell->n_lines(); ++line_no)
5570 if (cell->line(line_no)->has_children())
5571 new_vertices[cell->n_vertices() + line_no] =
5572 cell->line(line_no)->child(0)->vertex_index(1);
5573
5574 if (cell->reference_cell() == ReferenceCells::Quadrilateral)
5575 {
5576 while (triangulation.vertices_used[next_unused_vertex] == true)
5577 ++next_unused_vertex;
5578 Assert(
5579 next_unused_vertex < triangulation.vertices.size(),
5580 ExcMessage(
5581 "Internal error: During refinement, the triangulation wants "
5582 "to access an element of the 'vertices' array but it turns "
5583 "out that the array is not large enough."));
5584 triangulation.vertices_used[next_unused_vertex] = true;
5585
5586 new_vertices[8] = next_unused_vertex;
5587
5588 triangulation.vertices[next_unused_vertex] =
5589 cell->center(true, true);
5590 }
5591
5592 std::array<typename Triangulation<dim, spacedim>::raw_line_iterator,
5593 12>
5594 new_lines;
5595 std::array<types::geometric_orientation, 12> inherited_orientations;
5596 inherited_orientations.fill(numbers::default_geometric_orientation);
5597 unsigned int lmin = 0;
5598 unsigned int lmax = 0;
5599
5600 if (cell->reference_cell() == ReferenceCells::Triangle)
5601 {
5602 lmin = 6;
5603 lmax = 9;
5604 // For triangles, the innermost faces are always reversed for the
5605 // first three children and are in the standard orientation for
5606 // the last one.
5607 std::fill(inherited_orientations.begin() + lmin,
5608 inherited_orientations.begin() + lmax,
5610 }
5611 else if (cell->reference_cell() == ReferenceCells::Quadrilateral)
5612 {
5613 lmin = 8;
5614 lmax = 12;
5615 }
5616 else
5617 {
5619 }
5620
5621 for (unsigned int l = lmin; l < lmax; ++l)
5622 {
5623 while (next_unused_line->used() == true)
5624 ++next_unused_line;
5625 new_lines[l] = next_unused_line;
5626 ++next_unused_line;
5627
5628 AssertIsNotUsed(new_lines[l]);
5629 }
5630
5631 // set up lines which have parents:
5632 for (const unsigned int face_no : cell->face_indices())
5633 {
5634 // Check the face (line) orientation to ensure that the (six or
5635 // eight) outer lines in new_lines are indexed in the default
5636 // orientation. This way we can index into this array in the
5637 // without special casing orientations (e.g., quadrilateral child
5638 // 3 will always have lines 9, 3, 11, 7) when setting child lines.
5639 const auto combined_orientation =
5640 cell->combined_face_orientation(face_no);
5641 Assert(combined_orientation ==
5643 combined_orientation ==
5646 for (unsigned int c = 0; c < 2; ++c)
5647 {
5648 new_lines[2 * face_no + c] = cell->line(face_no)->child(c);
5649 inherited_orientations[2 * face_no + c] =
5650 cell->combined_face_orientation(face_no);
5651 }
5652 if (combined_orientation == numbers::reverse_line_orientation)
5653 std::swap(new_lines[2 * face_no], new_lines[2 * face_no + 1]);
5654 }
5655
5656 // set up lines which do not have parents:
5657 if (cell->reference_cell() == ReferenceCells::Triangle)
5658 {
5659 new_lines[6]->set_bounding_object_indices(
5660 {new_vertices[3], new_vertices[4]});
5661 new_lines[7]->set_bounding_object_indices(
5662 {new_vertices[4], new_vertices[5]});
5663 new_lines[8]->set_bounding_object_indices(
5664 {new_vertices[5], new_vertices[3]});
5665 }
5666 else if (cell->reference_cell() == ReferenceCells::Quadrilateral)
5667 {
5668 new_lines[8]->set_bounding_object_indices(
5669 {new_vertices[6], new_vertices[8]});
5670 new_lines[9]->set_bounding_object_indices(
5671 {new_vertices[8], new_vertices[7]});
5672 new_lines[10]->set_bounding_object_indices(
5673 {new_vertices[4], new_vertices[8]});
5674 new_lines[11]->set_bounding_object_indices(
5675 {new_vertices[8], new_vertices[5]});
5676 }
5677 else
5678 {
5680 }
5681
5682 for (unsigned int l = lmin; l < lmax; ++l)
5683 {
5684 new_lines[l]->set_used_flag();
5685 new_lines[l]->clear_user_flag();
5686 new_lines[l]->clear_user_data();
5687 new_lines[l]->clear_children();
5688 // new lines are always internal.
5689 new_lines[l]->set_boundary_id_internal(
5691 new_lines[l]->set_manifold_id(cell->manifold_id());
5692 }
5693
5695 subcells[ReferenceCells::max_n_children<dim>()];
5696 while (next_unused_cell->used() == true)
5697 ++next_unused_cell;
5698
5699 unsigned int n_children = 0;
5700 if (cell->reference_cell() == ReferenceCells::Triangle)
5701 n_children = 4;
5702 else if (cell->reference_cell() == ReferenceCells::Quadrilateral)
5703 n_children = 4;
5704 else
5706
5707 for (unsigned int i = 0; i < n_children; ++i)
5708 {
5709 AssertIsNotUsed(next_unused_cell);
5710 subcells[i] = next_unused_cell;
5711 ++next_unused_cell;
5712 if (i % 2 == 1 && i < n_children - 1)
5713 while (next_unused_cell->used() == true)
5714 ++next_unused_cell;
5715 }
5716
5717 // Assign lines to child cells:
5718 constexpr unsigned int X = numbers::invalid_unsigned_int;
5719 constexpr ::ndarray<unsigned int, 4, 4> tri_child_lines = {
5720 {{{0, 8, 5, X}}, //
5721 {{1, 2, 6, X}}, //
5722 {{7, 3, 4, X}}, //
5723 {{6, 7, 8, X}}}};
5724 constexpr ::ndarray<unsigned int, 4, 4> quad_child_lines = {
5725 {{{0, 8, 4, 10}},
5726 {{8, 2, 5, 11}},
5727 {{1, 9, 10, 6}},
5728 {{9, 3, 11, 7}}}};
5729 // Here and below we assume that child cells have the same reference
5730 // cell type as the parent.
5731 const auto &child_lines =
5732 cell->reference_cell() == ReferenceCells::Triangle ?
5733 tri_child_lines :
5734 quad_child_lines;
5735 for (unsigned int i = 0; i < n_children; ++i)
5736 {
5737 if (cell->reference_cell() == ReferenceCells::Triangle)
5738 subcells[i]->set_bounding_object_indices(
5739 {new_lines[child_lines[i][0]]->index(),
5740 new_lines[child_lines[i][1]]->index(),
5741 new_lines[child_lines[i][2]]->index()});
5742 else
5743 subcells[i]->set_bounding_object_indices(
5744 {new_lines[child_lines[i][0]]->index(),
5745 new_lines[child_lines[i][1]]->index(),
5746 new_lines[child_lines[i][2]]->index(),
5747 new_lines[child_lines[i][3]]->index()});
5748
5749 subcells[i]->set_used_flag();
5750 subcells[i]->clear_refine_flag();
5751 subcells[i]->clear_user_flag();
5752 subcells[i]->clear_user_data();
5753 subcells[i]->clear_children();
5754 // inherit material properties
5755 subcells[i]->set_material_id(cell->material_id());
5756 subcells[i]->set_manifold_id(cell->manifold_id());
5757 subcells[i]->set_subdomain_id(cell->subdomain_id());
5758
5759 triangulation.levels[subcells[i]->level()]
5760 ->reference_cell[subcells[i]->index()] = cell->reference_cell();
5761
5762 // Finally, now that children are marked as used, we can set
5763 // orientation flags (if needed):
5764 if (triangulation.levels[subcells[i]->level()]
5765 ->face_orientations.n_objects() > 0)
5766 for (unsigned int face_no : cell->face_indices())
5767 subcells[i]->set_combined_face_orientation(
5768 face_no, inherited_orientations[child_lines[i][face_no]]);
5769 else
5770 // It should not be possible to inherit a nonstandard
5771 // orientation when orientations are not set up
5772 for (unsigned int face_no : cell->face_indices())
5773 Assert(inherited_orientations[child_lines[i][face_no]] ==
5776
5777 // We only store the parent for every second cell. That's because
5778 // cells are created during refinement in multiples of two, and so
5779 // two successive cells always share the same parent. As a
5780 // consequence, we can save a bit of work by skipping setting
5781 // parent indices for the odd children.
5782 if (i % 2 == 0)
5783 subcells[i]->set_parent(cell->index());
5784 }
5785
5786 // Unlike the same lines on other children, the innermost triangle's
5787 // faces are all in the default orientation:
5788 if (cell->reference_cell() == ReferenceCells::Triangle)
5789 for (unsigned int face_no : cell->face_indices())
5790 subcells[3]->set_combined_face_orientation(
5792
5793 for (unsigned int i = 0; i < n_children / 2; ++i)
5794 cell->set_children(2 * i, subcells[2 * i]->index());
5795
5796 cell->set_refinement_case(ref_case);
5797
5798 if (dim == spacedim - 1)
5799 for (unsigned int c = 0; c < n_children; ++c)
5800 cell->child(c)->set_direction_flag(cell->direction_flag());
5801 };
5802
5803 for (int level = 0;
5804 level < static_cast<int>(triangulation.levels.size()) - 1;
5805 ++level)
5806 {
5808 next_unused_cell = triangulation.begin_raw(level + 1);
5809
5810 for (const auto &cell :
5812 if (cell->refine_flag_set())
5813 {
5814 create_children(triangulation,
5815 next_unused_vertex,
5816 next_unused_line,
5817 next_unused_cell,
5818 cell);
5819
5820 if (cell->reference_cell() == ReferenceCells::Quadrilateral &&
5821 check_for_distorted_cells &&
5822 has_distorted_children<dim, spacedim>(cell))
5823 cells_with_distorted_children.distorted_cells.push_back(
5824 cell);
5825
5826 triangulation.signals.post_refinement_on_cell(cell);
5827 }
5828 }
5829
5830 return cells_with_distorted_children;
5831 }
5832
5833
5834
5839 template <int spacedim>
5842 const bool /*check_for_distorted_cells*/)
5843 {
5844 const unsigned int dim = 1;
5845
5846 // Check whether a new level is needed. We have to check for
5847 // this on the highest level only
5848 for (const auto &cell : triangulation.active_cell_iterators_on_level(
5849 triangulation.levels.size() - 1))
5850 if (cell->refine_flag_set())
5851 {
5852 triangulation.levels.push_back(
5853 std::make_unique<internal::TriangulationImplementation::
5855 triangulation.strides.max_children_per_cell,
5856 triangulation.strides.max_faces_per_cell,
5857 triangulation.strides.max_vertices_per_cell));
5858 break;
5859 }
5860
5861 // check how much space is needed on every level. We need not
5862 // check the highest level since either - on the highest level
5863 // no cells are flagged for refinement - there are, but
5864 // prepare_refinement added another empty level
5865 std::size_t needed_vertices = 0;
5866 for (int level_no = triangulation.levels.size() - 2; level_no >= 0;
5867 --level_no)
5868 {
5869 std::size_t needed_cells = 0;
5870
5871 for (const auto &cell :
5872 triangulation.active_cell_iterators_on_level(level_no))
5873 if (cell->refine_flag_set())
5874 needed_cells += ReferenceCells::Line.n_isotropic_children();
5875
5876 // count number of used cells on the next higher level
5877 auto &next_level = *triangulation.levels[level_no + 1];
5878 const auto used_cells = std::count(next_level.cells.used.begin(),
5879 next_level.cells.used.end(),
5880 true);
5881
5882 if (used_cells + needed_cells > next_level.size())
5883 next_level.allocate_end((used_cells + needed_cells) -
5884 next_level.size(),
5885 // We are in 1d so there are no tetrahedra
5886 // and we do not need the orientation
5887 false,
5888 false);
5889
5890 // TODO: perhaps we can merge this with TriaLevel::allocate_end()
5891 next_level.cells.allocate_end(needed_cells, 0);
5892 needed_vertices += needed_cells / 2;
5893 }
5894
5895 // add to needed vertices how many
5896 // vertices are already in use
5897 needed_vertices += std::count(triangulation.vertices_used.begin(),
5898 triangulation.vertices_used.end(),
5899 true);
5900 // if we need more vertices: create them, if not: leave the
5901 // array as is, since shrinking is not really possible because
5902 // some of the vertices at the end may be in use
5903 if (needed_vertices > triangulation.vertices.size())
5904 {
5905 triangulation.vertices.resize(needed_vertices, Point<spacedim>());
5906 triangulation.vertices_used.resize(needed_vertices, false);
5907 }
5908
5909
5910 // Do REFINEMENT on every level; exclude highest level as
5911 // above
5912
5913 // index of next unused vertex
5914 unsigned int next_unused_vertex = 0;
5915
5916 for (int level = triangulation.levels.size() - 2; level >= 0; --level)
5917 {
5919 next_unused_cell = triangulation.begin_raw(level + 1);
5920
5921 for (const auto &cell :
5923 if (cell->refine_flag_set())
5924 {
5925 // clear refinement flag
5926 cell->clear_refine_flag();
5927
5928 // search for next unused
5929 // vertex
5930 while (triangulation.vertices_used[next_unused_vertex] ==
5931 true)
5932 ++next_unused_vertex;
5933 Assert(
5934 next_unused_vertex < triangulation.vertices.size(),
5935 ExcMessage(
5936 "Internal error: During refinement, the triangulation "
5937 "wants to access an element of the 'vertices' array "
5938 "but it turns out that the array is not large enough."));
5939
5940 // Now we always ask the cell itself where to put
5941 // the new point. The cell in turn will query the
5942 // manifold object internally.
5943 triangulation.vertices[next_unused_vertex] =
5944 cell->center(true);
5945
5946 triangulation.vertices_used[next_unused_vertex] = true;
5947
5948 // search for next two unused cell (++ takes care of
5949 // the end of the vector)
5951 first_child,
5952 second_child;
5953 while (next_unused_cell->used() == true)
5954 ++next_unused_cell;
5955 first_child = next_unused_cell;
5956 first_child->set_used_flag();
5957 first_child->clear_user_data();
5958 ++next_unused_cell;
5959 AssertIsNotUsed(next_unused_cell);
5960 second_child = next_unused_cell;
5961 second_child->set_used_flag();
5962 second_child->clear_user_data();
5963
5964 types::subdomain_id subdomainid = cell->subdomain_id();
5965
5966 // insert first child
5967 cell->set_children(0, first_child->index());
5968 first_child->clear_children();
5969 first_child->set_bounding_object_indices(
5970 {cell->vertex_index(0), next_unused_vertex});
5971 first_child->set_material_id(cell->material_id());
5972 first_child->set_manifold_id(cell->manifold_id());
5973 first_child->set_subdomain_id(subdomainid);
5974 if (dim == spacedim - 1)
5975 first_child->set_direction_flag(cell->direction_flag());
5976
5977 // We only store the parent for every second cell. That's
5978 // because cells are created during refinement in multiples of
5979 // two, and so two successive cells always share the same
5980 // parent. As a consequence, we can save a bit of work by
5981 // skipping setting parent indices for the second child.
5982 first_child->set_parent(cell->index());
5983
5984 // Set manifold id of the right face. Only do this
5985 // on the first child.
5986 first_child->face(1)->set_manifold_id(cell->manifold_id());
5987
5988 // reset neighborship info (refer to
5989 // internal::TriangulationImplementation::TriaLevel for
5990 // details)
5991 first_child->set_neighbor(1, second_child);
5992 if (cell->neighbor(0).state() != IteratorState::valid)
5993 first_child->set_neighbor(0, cell->neighbor(0));
5994 else if (cell->neighbor(0)->is_active())
5995 {
5996 // since the neighbors level is always <=level,
5997 // if the cell is active, then there are no
5998 // cells to the left which may want to know
5999 // about this new child cell.
6000 Assert(cell->neighbor(0)->level() <= cell->level(),
6002 first_child->set_neighbor(0, cell->neighbor(0));
6003 }
6004 else
6005 // left neighbor is refined
6006 {
6007 // set neighbor to cell on same level
6008 const unsigned int nbnb = cell->neighbor_of_neighbor(0);
6009 first_child->set_neighbor(0,
6010 cell->neighbor(0)->child(nbnb));
6011
6012 // reset neighbor info of all right descendant
6013 // of the left neighbor of cell
6015 left_neighbor = cell->neighbor(0);
6016 while (left_neighbor->has_children())
6017 {
6018 left_neighbor = left_neighbor->child(nbnb);
6019 left_neighbor->set_neighbor(nbnb, first_child);
6020 }
6021 }
6022
6023 // insert second child
6024 second_child->clear_children();
6025 second_child->set_bounding_object_indices(
6026 {next_unused_vertex, cell->vertex_index(1)});
6027 second_child->set_neighbor(0, first_child);
6028 second_child->set_material_id(cell->material_id());
6029 second_child->set_manifold_id(cell->manifold_id());
6030 second_child->set_subdomain_id(subdomainid);
6031 if (dim == spacedim - 1)
6032 second_child->set_direction_flag(cell->direction_flag());
6033
6034 if (cell->neighbor(1).state() != IteratorState::valid)
6035 second_child->set_neighbor(1, cell->neighbor(1));
6036 else if (cell->neighbor(1)->is_active())
6037 {
6038 Assert(cell->neighbor(1)->level() <= cell->level(),
6040 second_child->set_neighbor(1, cell->neighbor(1));
6041 }
6042 else
6043 // right neighbor is refined same as above
6044 {
6045 const unsigned int nbnb = cell->neighbor_of_neighbor(1);
6046 second_child->set_neighbor(
6047 1, cell->neighbor(1)->child(nbnb));
6048
6050 right_neighbor = cell->neighbor(1);
6051 while (right_neighbor->has_children())
6052 {
6053 right_neighbor = right_neighbor->child(nbnb);
6054 right_neighbor->set_neighbor(nbnb, second_child);
6055 }
6056 }
6057 // inform all listeners that cell refinement is done
6058 triangulation.signals.post_refinement_on_cell(cell);
6059 }
6060 }
6061
6062 // in 1d, we can not have distorted children unless the parent
6063 // was already distorted (that is because we don't use
6064 // boundary information for 1d triangulations). so return an
6065 // empty list
6067 }
6068
6069
6074 template <int spacedim>
6077 const bool check_for_distorted_cells)
6078 {
6079 const unsigned int dim = 2;
6080
6081 // First check whether we can get away with isotropic refinement, or
6082 // whether we need to run through the full anisotropic algorithm
6083 bool do_isotropic_refinement = true;
6084 for (const auto &cell : triangulation.active_cell_iterators())
6085 if (cell->refine_flag_set() == RefinementCase<dim>::cut_x ||
6086 cell->refine_flag_set() == RefinementCase<dim>::cut_y)
6087 {
6088 do_isotropic_refinement = false;
6089 break;
6090 }
6091
6092 if (do_isotropic_refinement)
6093 return execute_refinement_isotropic(triangulation,
6094 check_for_distorted_cells);
6095 else
6096 {
6097 // If we get here, we are doing anisotropic refinement.
6098
6099 // Check whether a new level is needed. We have to check for
6100 // this on the highest level only
6101 for (const auto &cell :
6102 triangulation.active_cell_iterators_on_level(
6103 triangulation.levels.size() - 1))
6104 if (cell->refine_flag_set())
6105 {
6106 triangulation.levels.push_back(
6107 std::make_unique<internal::TriangulationImplementation::
6109 triangulation.strides.max_children_per_cell,
6110 triangulation.strides.max_faces_per_cell,
6111 triangulation.strides.max_vertices_per_cell));
6112 break;
6113 }
6114
6115 // TODO[WB]: we clear user flags and pointers of lines; we're going
6116 // to use them to flag which lines need refinement
6118 triangulation.begin_line();
6119 line != triangulation.end_line();
6120 ++line)
6121 {
6122 line->clear_user_flag();
6123 line->clear_user_data();
6124 }
6125
6126 // running over all cells and lines count the number
6127 // n_single_lines of lines which can be stored as single
6128 // lines, e.g. inner lines
6129 unsigned int n_single_lines = 0;
6130
6131 // New lines to be created: number lines which are stored in
6132 // pairs (the children of lines must be stored in pairs)
6133 unsigned int n_lines_in_pairs = 0;
6134
6135 // check how much space is needed on every level. We need not
6136 // check the highest level since either - on the highest level
6137 // no cells are flagged for refinement - there are, but
6138 // prepare_refinement added another empty level
6139 unsigned int needed_vertices = 0;
6140
6141 const bool orientation_needed =
6142 !triangulation.all_reference_cells_are_hyper_cube();
6143
6144 for (int level_no = triangulation.levels.size() - 2; level_no >= 0;
6145 --level_no)
6146 {
6147 // count number of flagged cells on this level and compute
6148 // how many new vertices and new lines will be needed
6149 std::size_t needed_cells = 0;
6150
6151 for (const auto &cell :
6152 triangulation.active_cell_iterators_on_level(level_no))
6153 if (cell->refine_flag_set())
6154 {
6155 Assert(cell->reference_cell().is_hyper_cube(),
6157 if (cell->refine_flag_set() ==
6159 {
6160 needed_cells += 4;
6161
6162 // new vertex at center of cell is needed in any
6163 // case
6164 ++needed_vertices;
6165
6166 // the four inner lines can be stored as singles
6167 n_single_lines += 4;
6168 }
6169 else // cut_x || cut_y
6170 {
6171 // set the flag showing that anisotropic
6172 // refinement is used for at least one cell
6173 triangulation.anisotropic_refinement = true;
6174
6175 needed_cells += 2;
6176 // no vertex at center
6177
6178 // the inner line can be stored as single
6179 n_single_lines += 1;
6180 }
6181
6182 // mark all faces (lines) for refinement; checking
6183 // locally whether the neighbor would also like to
6184 // refine them is rather difficult for lines so we
6185 // only flag them and after visiting all cells, we
6186 // decide which lines need refinement;
6187 for (const unsigned int line_no :
6189 {
6191 cell->refine_flag_set(), line_no) ==
6193 {
6194 typename Triangulation<dim,
6195 spacedim>::line_iterator
6196 line = cell->line(line_no);
6197 if (line->has_children() == false)
6198 line->set_user_flag();
6199 }
6200 }
6201 }
6202
6203 auto &next_level = *triangulation.levels[level_no + 1];
6204 const auto used_cells =
6205 std::count(next_level.cells.used.begin(),
6206 next_level.cells.used.end(),
6207 true);
6208 if (used_cells + needed_cells > next_level.size())
6209 next_level.allocate_end(
6210 (used_cells + needed_cells) - next_level.size(),
6211 orientation_needed,
6212 // We are in 2d so there are no tetrahedra
6213 false);
6214
6215 // TODO: perhaps we can merge this with
6216 // TriaLevel::allocate_end()
6217 next_level.cells.allocate_end(needed_cells, 0);
6218 }
6219
6220 // now count the lines which were flagged for refinement
6222 triangulation.begin_line();
6223 line != triangulation.end_line();
6224 ++line)
6225 if (line->user_flag_set())
6226 {
6227 Assert(line->has_children() == false, ExcInternalError());
6228 n_lines_in_pairs += 2;
6229 needed_vertices += 1;
6230 }
6231 // reserve space for n_lines_in_pairs new lines. note, that
6232 // we can't reserve space for the single lines here as well,
6233 // as all the space reserved for lines in pairs would be
6234 // counted as unused and we would end up with too little space
6235 // to store all lines. memory reservation for n_single_lines
6236 // can only be done AFTER we refined the lines of the current
6237 // cells
6238 triangulation.faces->lines.allocate_end(n_lines_in_pairs, 0);
6239
6240 // add to needed vertices how many vertices are already in use
6241 needed_vertices += std::count(triangulation.vertices_used.begin(),
6242 triangulation.vertices_used.end(),
6243 true);
6244 // if we need more vertices: create them, if not: leave the
6245 // array as is, since shrinking is not really possible because
6246 // some of the vertices at the end may be in use
6247 if (needed_vertices > triangulation.vertices.size())
6248 {
6249 triangulation.vertices.resize(needed_vertices,
6250 Point<spacedim>());
6251 triangulation.vertices_used.resize(needed_vertices, false);
6252 }
6253
6254
6255 // Do REFINEMENT on every level; exclude highest level as
6256 // above
6257
6258 // index of next unused vertex
6259 unsigned int next_unused_vertex = 0;
6260
6261 // first the refinement of lines. children are stored
6262 // pairwise
6263 {
6264 // only active objects can be refined further
6266 line = triangulation.begin_active_line(),
6267 endl = triangulation.end_line();
6269 next_unused_line = triangulation.begin_raw_line();
6270
6271 for (; line != endl; ++line)
6272 if (line->user_flag_set())
6273 {
6274 // this line needs to be refined
6275
6276 // find the next unused vertex and set it
6277 // appropriately
6278 while (triangulation.vertices_used[next_unused_vertex] ==
6279 true)
6280 ++next_unused_vertex;
6281 Assert(
6282 next_unused_vertex < triangulation.vertices.size(),
6283 ExcMessage(
6284 "Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
6285 triangulation.vertices_used[next_unused_vertex] = true;
6286
6287 triangulation.vertices[next_unused_vertex] =
6288 line->center(true);
6289
6290 // now that we created the right point, make up the
6291 // two child lines. To this end, find a pair of
6292 // unused lines
6293 bool pair_found = false;
6294 for (; next_unused_line != endl; ++next_unused_line)
6295 if (!next_unused_line->used() &&
6296 !(++next_unused_line)->used())
6297 {
6298 // go back to the first of the two unused
6299 // lines
6300 --next_unused_line;
6301 pair_found = true;
6302 break;
6303 }
6304 Assert(pair_found, ExcInternalError());
6305
6306 // there are now two consecutive unused lines, such
6307 // that the children of a line will be consecutive.
6308 // then set the child pointer of the present line
6309 line->set_children(0, next_unused_line->index());
6310
6311 // set the two new lines
6312 const typename Triangulation<dim,
6313 spacedim>::raw_line_iterator
6314 children[2] = {next_unused_line, ++next_unused_line};
6315 // some tests; if any of the iterators should be
6316 // invalid, then already dereferencing will fail
6317 AssertIsNotUsed(children[0]);
6318 AssertIsNotUsed(children[1]);
6319
6320 children[0]->set_bounding_object_indices(
6321 {line->vertex_index(0), next_unused_vertex});
6322 children[1]->set_bounding_object_indices(
6323 {next_unused_vertex, line->vertex_index(1)});
6324
6325 children[0]->set_used_flag();
6326 children[1]->set_used_flag();
6327 children[0]->clear_children();
6328 children[1]->clear_children();
6329 children[0]->clear_user_data();
6330 children[1]->clear_user_data();
6331 children[0]->clear_user_flag();
6332 children[1]->clear_user_flag();
6333
6334
6335 children[0]->set_boundary_id_internal(line->boundary_id());
6336 children[1]->set_boundary_id_internal(line->boundary_id());
6337
6338 children[0]->set_manifold_id(line->manifold_id());
6339 children[1]->set_manifold_id(line->manifold_id());
6340
6341 // finally clear flag indicating the need for
6342 // refinement
6343 line->clear_user_flag();
6344 }
6345 }
6346
6347
6348 // Now set up the new cells
6349
6350 // reserve space for inner lines (can be stored as single
6351 // lines)
6352 triangulation.faces->lines.allocate_end(0, n_single_lines);
6353
6355 cells_with_distorted_children;
6356
6357 // reset next_unused_line, as now also single empty places in
6358 // the vector can be used
6360 next_unused_line = triangulation.begin_raw_line();
6361
6362 for (int level = 0;
6363 level < static_cast<int>(triangulation.levels.size()) - 1;
6364 ++level)
6365 {
6367 next_unused_cell = triangulation.begin_raw(level + 1);
6368
6369 for (const auto &cell :
6371 if (cell->refine_flag_set())
6372 {
6373 // actually set up the children and update neighbor
6374 // information
6375 create_children(triangulation,
6376 next_unused_vertex,
6377 next_unused_line,
6378 next_unused_cell,
6379 cell);
6380
6381 if (check_for_distorted_cells &&
6382 has_distorted_children<dim, spacedim>(cell))
6383 cells_with_distorted_children.distorted_cells.push_back(
6384 cell);
6385 // inform all listeners that cell refinement is done
6386 triangulation.signals.post_refinement_on_cell(cell);
6387 }
6388 }
6389
6390 return cells_with_distorted_children;
6391 }
6392 }
6393
6394
6395 template <int spacedim>
6398 const bool check_for_distorted_cells)
6399 {
6400 constexpr int dim = 3;
6401 constexpr unsigned int X = numbers::invalid_unsigned_int;
6402 using raw_line_iterator =
6404 using raw_quad_iterator =
6406
6407 Assert(spacedim == 3, ExcNotImplemented());
6408
6409 Assert(triangulation.vertices.size() ==
6410 triangulation.vertices_used.size(),
6412
6413 // Check whether a new level is needed. We have to check for
6414 // this on the highest level only
6415 for (const auto &cell : triangulation.active_cell_iterators_on_level(
6416 triangulation.levels.size() - 1))
6417 if (cell->refine_flag_set())
6418 {
6420 triangulation.levels.size() < numbers::max_n_levels,
6422 triangulation.levels.push_back(
6423 std::make_unique<internal::TriangulationImplementation::
6425 triangulation.strides.max_children_per_cell,
6426 triangulation.strides.max_faces_per_cell,
6427 triangulation.strides.max_vertices_per_cell));
6428 break;
6429 }
6430
6431 // first clear user flags for quads and lines; we're going to
6432 // use them to flag which lines and quads need refinement
6433 triangulation.faces->quads.clear_user_data();
6434 triangulation.faces->lines.clear_user_flags();
6435 triangulation.faces->quads.clear_user_flags();
6436
6437 // check how much space is needed on every level. We need not
6438 // check the highest level since either
6439 // - on the highest level no cells are flagged for refinement
6440 // - there are, but prepare_refinement added another empty
6441 // level which then is the highest level
6442
6443 // Variables to hold the number of newly to be created
6444 // vertices, lines, and faces. As these are stored globally,
6445 // declare them outside the loop over all levels. We need lines
6446 // and faces in pairs for refinement of old lines/face. And lines and
6447 // faces stored individually for the ones created in the interior
6448 // of an existing cell
6449 { // STORAGE
6450 unsigned int needed_vertices = 0;
6451 unsigned int needed_lines_single = 0;
6452 unsigned int needed_faces_single = 0;
6453 unsigned int needed_lines_pair = 0;
6454 unsigned int needed_faces_pair = 0;
6455 bool contains_pyramid = false;
6456 for (int level_no = triangulation.levels.size() - 2; level_no >= 0;
6457 --level_no)
6458 {
6459 std::size_t needed_cells = 0;
6460
6461 for (const auto &cell :
6462 triangulation.active_cell_iterators_on_level(level_no))
6463 if (cell->refine_flag_set())
6464 {
6465 // Only support isotropic refinement
6466 Assert(cell->refine_flag_set() ==
6469
6470 // get reference cell type of current cell
6471 const auto cell_reference_cell = cell->reference_cell();
6472
6473 // Now count up how many new cells, faces, edges, and
6474 // vertices we will need to allocate to do this refinement.
6475 needed_cells += cell_reference_cell.n_isotropic_children();
6476
6477 switch (cell_reference_cell)
6478 {
6480 needed_lines_single += 1;
6481 needed_faces_single += 8;
6482 break;
6483
6485 // TODO: Pyramid
6486 // - No vertices (all provided by face refinement)
6487 // - 5 faces for center pyramid
6488 // - 4 times 2 for the remaining tets
6489 needed_lines_single += 4;
6490 needed_faces_single += 13;
6491 contains_pyramid = true;
6492 break;
6493
6495 // - We won't need vertices (all required vertices
6496 // will be provided by the refined faces)
6497 // - 3 lines will be required to build up the
6498 // "refined" middle triangle
6499 // - 4 faces for the new refined middle triangle + 6
6500 // faces for the quads building the wedges in
6501 // z-direction. Rest will be provided via refinement
6502 // of faces. Store them all as singles. See also
6503 // https://link.springer.com/article/10.1007/BF01221213
6504 needed_lines_single += 3;
6505 needed_faces_single += 10;
6506 break;
6507
6509 // One vertex in the center of the parent
6510 ++needed_vertices;
6511 needed_lines_single += 6;
6512 needed_faces_single += 12;
6513 break;
6514
6515 default:
6517 }
6518
6519 // Also check whether we have to refine any of the faces and
6520 // edges that bound this cell. They may of course already be
6521 // refined, so we only *mark* them for refinement by setting
6522 // the user flags
6523
6524 // Faces
6525 for (const auto face : cell->face_indices())
6526 if (cell->face(face)->n_children() == 0)
6527 cell->face(face)->set_user_flag();
6528 else
6529 // Check that the child of the cell has enough children
6530 Assert(cell->face(face)->n_children() ==
6531 cell_reference_cell.face_reference_cell(face)
6532 .n_isotropic_children(),
6534
6535 // Edges
6536 for (const auto line : cell->line_indices())
6537 if (cell->line(line)->has_children() == false)
6538 cell->line(line)->set_user_flag();
6539 else
6540 Assert(cell->line(line)->n_children() == 2,
6542 }
6543
6544 auto &next_level = *triangulation.levels[level_no + 1];
6545 const auto used_cells = std::count(next_level.cells.used.begin(),
6546 next_level.cells.used.end(),
6547 true);
6548 if (used_cells + needed_cells > next_level.size())
6549 next_level.allocate_end(
6550 (used_cells + needed_cells) - next_level.size(),
6551 // We unconditionally store the orientation in 3d
6552 true,
6553 // TODO: we can get rid of this check (i.e., we don't need to
6554 // store this value for purely wedge meshes). This will be
6555 // simpler to do once we merge IsotropicRefinementChoice and
6556 // RefinementCase.
6557 !triangulation.all_reference_cells_are_hyper_cube());
6558
6559 // TODO: perhaps we can merge this with TriaLevel::allocate_end()
6560 next_level.cells.allocate_end(needed_cells, 0);
6561 }
6562
6563 // In case a pyramid is going to be refined we will get additional
6564 // tets in the refined triangulation, hence we need to inform the
6565 // triangulation that this happens. Note: if coarsening gets
6566 // implemented this step needs to be reverted. Maybe switch to a
6567 // refinement counter for pyramids and track its value to revert this
6568 // change.
6569 if (contains_pyramid)
6570 // Prevent multiple occurrences of tet inside the vector
6571 if (std::find(triangulation.reference_cells.begin(),
6572 triangulation.reference_cells.end(),
6574 triangulation.reference_cells.end())
6575 // Add it to start of vector to keep sorting intact.
6576 triangulation.reference_cells.insert(
6577 triangulation.reference_cells.begin(),
6579
6580 // now count the faces and lines which were flagged for
6581 // refinement
6583 triangulation.begin_quad();
6584 quad != triangulation.end_quad();
6585 ++quad)
6586 {
6587 if (quad->user_flag_set() == false)
6588 continue;
6589
6590 if (quad->reference_cell() == ReferenceCells::Quadrilateral)
6591 {
6592 needed_faces_pair += 4;
6593 needed_lines_pair += 4;
6594 needed_vertices += 1;
6595 }
6596 else if (quad->reference_cell() == ReferenceCells::Triangle)
6597 {
6598 needed_faces_pair += 4;
6599 needed_lines_single += 3;
6600 }
6601 else
6602 {
6604 }
6605 }
6606
6608 triangulation.begin_line();
6609 line != triangulation.end_line();
6610 ++line)
6611 {
6612 if (line->user_flag_set() == false)
6613 continue;
6614
6615 needed_lines_pair += 2;
6616 needed_vertices += 1;
6617 }
6618
6619 triangulation.faces->allocate_end(needed_faces_pair,
6620 needed_faces_single);
6621 triangulation.faces->lines.allocate_end(needed_lines_pair,
6622 needed_lines_single);
6623
6624 // add to needed vertices how many vertices are already in use
6625 needed_vertices += std::count(triangulation.vertices_used.begin(),
6626 triangulation.vertices_used.end(),
6627 true);
6628
6629 if (needed_vertices > triangulation.vertices.size())
6630 {
6631 triangulation.vertices.resize(needed_vertices, Point<spacedim>());
6632 triangulation.vertices_used.resize(needed_vertices, false);
6633 }
6634 } // STORAGE
6635
6636 //-----------------------------------------
6637 // Before we start with the actual refinement, we do some
6638 // sanity checks if in debug mode. especially, we try to catch
6639 // the notorious problem with lines being twice refined,
6640 // i.e. there are cells adjacent at one line ("around the
6641 // edge", but not at a face), with two cells differing by more
6642 // than one refinement level
6643 //
6644 // this check is very simple to implement here, since we have
6645 // all lines flagged if they shall be refined
6646 if constexpr (running_in_debug_mode())
6647 {
6648 for (const auto &cell : triangulation.active_cell_iterators())
6649 if (!cell->refine_flag_set())
6650 for (unsigned int line_n = 0; line_n < cell->n_lines();
6651 ++line_n)
6652 if (cell->line(line_n)->has_children())
6653 for (unsigned int c = 0; c < 2; ++c)
6654 Assert(cell->line(line_n)->child(c)->user_flag_set() ==
6655 false,
6657 }
6658
6659 unsigned int current_vertex = 0;
6660
6661 // helper function - find the next available vertex number and mark it
6662 // as used.
6663 auto get_next_unused_vertex = [](const unsigned int current_vertex,
6664 std::vector<bool> &vertices_used) {
6665 unsigned int next_vertex = current_vertex;
6666 while (next_vertex < vertices_used.size() &&
6667 vertices_used[next_vertex] == true)
6668 ++next_vertex;
6669 Assert(next_vertex < vertices_used.size(), ExcInternalError());
6670 vertices_used[next_vertex] = true;
6671
6672 return next_vertex;
6673 };
6674
6675
6676 { // LINES
6678 line = triangulation.begin_active_line(),
6679 endl = triangulation.end_line();
6680 raw_line_iterator next_unused_line = triangulation.begin_raw_line();
6681
6682 for (; line != endl; ++line)
6683 {
6684 if (line->user_flag_set() == false)
6685 continue;
6686
6687 next_unused_line =
6688 triangulation.faces->lines.template next_free_pair_object<1>(
6689 triangulation);
6690 Assert(next_unused_line.state() == IteratorState::valid,
6692
6693 // now we found two consecutive unused lines, such
6694 // that the children of a line will be consecutive.
6695 // then set the child pointer of the present line
6696 line->set_children(0, next_unused_line->index());
6697
6698 const std::array<raw_line_iterator, 2> children{
6699 {next_unused_line, ++next_unused_line}};
6700
6701 AssertIsNotUsed(children[0]);
6702 AssertIsNotUsed(children[1]);
6703
6704 current_vertex =
6705 get_next_unused_vertex(current_vertex,
6706 triangulation.vertices_used);
6707 triangulation.vertices[current_vertex] = line->center(true);
6708
6709 children[0]->set_bounding_object_indices(
6710 {line->vertex_index(0), current_vertex});
6711 children[1]->set_bounding_object_indices(
6712 {current_vertex, line->vertex_index(1)});
6713
6714 const auto manifold_id = line->manifold_id();
6715 const auto boundary_id = line->boundary_id();
6716 for (const auto &child : children)
6717 {
6718 child->set_used_flag();
6719 child->clear_children();
6720 child->clear_user_data();
6721 child->clear_user_flag();
6722 child->set_boundary_id_internal(boundary_id);
6723 child->set_manifold_id(manifold_id);
6724 }
6725
6726 line->clear_user_flag();
6727 }
6728 }
6729
6730 { // FACES
6731 // (i.e., quads or triangles, or both)
6733 face = triangulation.begin_face(),
6734 endf = triangulation.end_face();
6735
6736 for (; face != endf; ++face)
6737 {
6738 if (face->user_flag_set() == false)
6739 continue;
6740
6741 const auto reference_face_type = face->reference_cell();
6742 const auto n_lines_per_face = reference_face_type.n_lines();
6743
6744 // 1) Create new lines (property is set later).
6745 // Maximum of 4 new lines (4 quadrilateral, 3 triangle).
6746 std::array<raw_line_iterator, 4> new_lines;
6747 switch (reference_face_type)
6748 {
6750 for (unsigned int l = 0; l < 3; ++l)
6751 new_lines[l] =
6752 triangulation.faces->lines
6753 .template next_free_single_object<1>(triangulation);
6754 break;
6755
6757 for (unsigned int l = 0; l < 2; ++l)
6758 {
6759 auto next_unused_line =
6760 triangulation.faces->lines
6761 .template next_free_pair_object<1>(triangulation);
6762 new_lines[2 * l] = next_unused_line;
6763 new_lines[2 * l + 1] = ++next_unused_line;
6764 }
6765 break;
6766
6767 default:
6769 }
6770
6771 if constexpr (running_in_debug_mode())
6772 {
6773 for (const unsigned int line : face->line_indices())
6774 AssertIsNotUsed(new_lines[line]);
6775 }
6776
6777 // 2) Create new face (properties are set below).
6778 // Both triangles and quads are divided in four. (For historical
6779 // reasons, we only have `raw_quad_iterator`, not
6780 // `raw_face_iterator`, but the former also works if a face is
6781 // actually a triangle.)
6782 std::array<raw_quad_iterator, 4> new_faces;
6783 for (unsigned int f = 0; f < 2; ++f)
6784 {
6785 auto next_unused_quad =
6786 triangulation.faces->quads
6787 .template next_free_pair_object<2>(triangulation);
6788
6789 new_faces[2 * f] = next_unused_quad;
6790 new_faces[2 * f + 1] = ++next_unused_quad;
6791
6792 face->set_children(2 * f, new_faces[2 * f]->index());
6793 }
6794 // Tell the original face that is has been isotropically refined.
6795 face->set_refinement_case(RefinementCase<2>::cut_xy);
6796
6797 if constexpr (running_in_debug_mode())
6798 {
6799 for (const auto &quad : new_faces)
6800 AssertIsNotUsed(quad);
6801 }
6802
6803 // 3) Create an ordered list of vertices.
6804 // The order is determined by the physical position of the
6805 // vertices within the face reference cell, enabling easy
6806 // identification/lookup by the vertex's index in this list.
6807 // Additionally the center vertex required for quadrilaterals is
6808 // created.
6809
6810 // Maximum of 9 vertices per refined face (9 for Quadrilateral, 6
6811 // for Triangle)
6812 std::array<unsigned int, 9> vertex_indices = {};
6813 unsigned int k = 0;
6814 for (const auto i : face->vertex_indices())
6815 vertex_indices[k++] = face->vertex_index(i);
6816
6817 for (unsigned int l = 0; l < n_lines_per_face; ++l)
6818 vertex_indices[k++] = face->line(l)->child(0)->vertex_index(1);
6819
6820 if (reference_face_type == ReferenceCells::Quadrilateral)
6821 {
6822 current_vertex =
6823 get_next_unused_vertex(current_vertex,
6824 triangulation.vertices_used);
6825 vertex_indices[k++] = current_vertex;
6826
6827 triangulation.vertices[current_vertex] =
6828 face->center(true, true);
6829 }
6830
6831 // 4) Set new lines on these faces and their properties.
6832 // 'magic numbers' in `line_vertices_X` are indices in the list
6833 // created in step 3).
6834 std::array<raw_line_iterator, 12> lines;
6835 unsigned int n_lines = 0;
6836 for (unsigned int l = 0; l < n_lines_per_face; ++l)
6837 for (unsigned int c = 0; c < 2; ++c)
6838 lines[n_lines++] = face->line(l)->child(
6839 child_line_index(c, face->line_orientation(l)));
6840
6841 for (unsigned int l = 0; l < n_lines_per_face; ++l)
6842 lines[n_lines++] = new_lines[l];
6843
6844 std::array<int, 12> line_indices;
6845 for (unsigned int i = 0; i < n_lines; ++i)
6846 line_indices[i] = lines[i]->index();
6847
6848 // 2---7---3 .-6-.-7-. .---.---.
6849 // | | | 1 9 3 | 2 | 3 |
6850 // 4---8---5 .-10.11-. .---.---.
6851 // | | | 0 8 2 | 0 | 1 |
6852 // 0---6---1 .-4-.-5-. .---.---.
6853 constexpr ::ndarray<unsigned int, 12, 2> line_vertices_quad{
6854 {{{0, 4}},
6855 {{4, 2}},
6856 {{1, 5}},
6857 {{5, 3}},
6858 {{0, 6}},
6859 {{6, 1}},
6860 {{2, 7}},
6861 {{7, 3}},
6862 {{6, 8}},
6863 {{8, 7}},
6864 {{4, 8}},
6865 {{8, 5}}}};
6866
6867 constexpr ::ndarray<unsigned int, 4, 4> quad_lines_quad{
6868 {{{0, 8, 4, 10}},
6869 {{8, 2, 5, 11}},
6870 {{1, 9, 10, 6}},
6871 {{9, 3, 11, 7}}}};
6872
6873 // 2 * * .
6874 // |\ |\ |\ .
6875 // v ^ 4 3 | \ .
6876 // | \ | \ | 2 \ .
6877 // 5--<--4 *--7--* *-----* .
6878 // |\ |\ |\ |\ |\ 3 |\ .
6879 // v ^ ^ ^ 5 8 6 2 | \ | \ .
6880 // | \| \ | \| \ | 0 \| 1 \ .
6881 // 0-->--3-->--1 *--0--*--1--* *-----*-----* .
6882 constexpr ::ndarray<unsigned int, 12, 2> line_vertices_tri{
6883 {{{0, 3}},
6884 {{3, 1}},
6885 {{1, 4}},
6886 {{4, 2}},
6887 {{2, 5}},
6888 {{5, 0}},
6889 {{3, 4}},
6890 {{4, 5}},
6891 {{3, 5}},
6892 {{X, X}},
6893 {{X, X}},
6894 {{X, X}}}};
6895
6896 constexpr ::ndarray<unsigned int, 4, 4> tri_lines_tri{
6897 {{{0, 8, 5, X}},
6898 {{1, 2, 6, X}},
6899 {{7, 3, 4, X}},
6900 {{6, 7, 8, X}}}};
6901
6902 // The defined lines in `line_vertices_tri` do not satisfy the
6903 // expected orientations of all the children's reference cells.
6904 // This table specifies the expected line orientations (i.e.
6905 // vertex order) necessary for `set_line_orientation(...)`.
6906 constexpr ::ndarray<unsigned int, 4, 4, 2>
6907 tri_line_vertices_tri{
6908 {{{{{0, 3}}, {{3, 5}}, {{5, 0}}, {{X, X}}}},
6909 {{{{3, 1}}, {{1, 4}}, {{4, 3}}, {{X, X}}}},
6910 {{{{5, 4}}, {{4, 2}}, {{2, 5}}, {{X, X}}}},
6911 {{{{3, 4}}, {{4, 5}}, {{5, 3}}, {{X, X}}}}}};
6912
6913 // Select lookup table according to reference cell of parent.
6914 const auto &line_vertices =
6915 (reference_face_type == ReferenceCells::Quadrilateral) ?
6916 line_vertices_quad :
6917 line_vertices_tri;
6918 const auto &face_lines =
6919 (reference_face_type == ReferenceCells::Quadrilateral) ?
6920 quad_lines_quad :
6921 tri_lines_tri;
6922
6923 // The first 2*face->n_lines() lines are already created by
6924 // refining the original lines on the face. Therefore, only the
6925 // subsequent face->n_lines() lines need to be processed now.
6926 for (unsigned int i = 0, j = 2 * n_lines_per_face;
6927 i < n_lines_per_face;
6928 ++i, ++j)
6929 {
6930 auto &new_line = new_lines[i];
6931 new_line->set_bounding_object_indices(
6932 {vertex_indices[line_vertices[j][0]],
6933 vertex_indices[line_vertices[j][1]]});
6934 new_line->set_used_flag();
6935 new_line->clear_user_flag();
6936 new_line->clear_user_data();
6937 new_line->clear_children();
6938 new_line->set_boundary_id_internal(face->boundary_id());
6939 new_line->set_manifold_id(face->manifold_id());
6940 }
6941
6942 // 5) Set properties of faces
6943 for (unsigned int i = 0; i < new_faces.size(); ++i)
6944 {
6945 // 5.a) Create faces
6946 auto &new_face = new_faces[i];
6947
6948 // We assume here that all children have the same type as the
6949 // parent face.
6950 triangulation.faces->set_quad_type(new_face->index(),
6951 reference_face_type);
6952
6953 switch (reference_face_type)
6954 {
6956 new_face->set_bounding_object_indices(
6957 {line_indices[face_lines[i][0]],
6958 line_indices[face_lines[i][1]],
6959 line_indices[face_lines[i][2]]});
6960 break;
6961
6963 new_face->set_bounding_object_indices(
6964 {line_indices[face_lines[i][0]],
6965 line_indices[face_lines[i][1]],
6966 line_indices[face_lines[i][2]],
6967 line_indices[face_lines[i][3]]});
6968 break;
6969
6970 default:
6972 }
6973
6974 new_face->set_used_flag();
6975 new_face->clear_user_flag();
6976 new_face->clear_user_data();
6977 new_face->clear_children();
6978 new_face->set_boundary_id_internal(face->boundary_id());
6979 new_face->set_manifold_id(face->manifold_id());
6980
6981 [[maybe_unused]] std::set<unsigned int> s;
6982
6983 // 5.b.I) Fix orientation of lines of face
6984 // For triangles an expensive algorithm is used,
6985 // quadrilaterals are treated a few lines below by a cheaper
6986 // algorithm
6987 if (reference_face_type == ReferenceCells::Triangle)
6988 {
6989 for (const auto f : new_face->line_indices())
6990 {
6991 const std::array<unsigned int, 2> vertices_0 = {
6992 {lines[face_lines[i][f]]->vertex_index(0),
6993 lines[face_lines[i][f]]->vertex_index(1)}};
6994
6995 const std::array<unsigned int, 2> vertices_1 = {
6996 {vertex_indices[tri_line_vertices_tri[i][f][0]],
6997 vertex_indices[tri_line_vertices_tri[i][f][1]]}};
6998
6999 const auto orientation =
7000 ReferenceCells::Line.get_combined_orientation(
7001 make_array_view(vertices_0),
7002 make_array_view(vertices_1));
7003
7004 if constexpr (library_build_mode ==
7006 {
7007 for (const auto i : vertices_0)
7008 s.insert(i);
7009 for (const auto i : vertices_1)
7010 s.insert(i);
7011 }
7012
7013 new_face->set_line_orientation(f, orientation);
7014 }
7015 if constexpr (library_build_mode ==
7017 {
7018 AssertDimension(s.size(), 3);
7019 }
7020 }
7021 }
7022
7023 // 5.b.II) Fix orientation of lines of faces for quadrilaterals
7024 // with cheap algorithm.
7025 if (reference_face_type == ReferenceCells::Quadrilateral)
7026 {
7027 constexpr ::ndarray<unsigned int, 4, 2>
7028 quad_child_line_vertices{{{{0, 2}}, //
7029 {{1, 3}}, //
7030 {{0, 1}}, //
7031 {{2, 3}}}};
7032
7033 for (unsigned int i = 0; i < 4; ++i)
7034 for (unsigned int j = 0; j < 2; ++j)
7035 new_faces[quad_child_line_vertices[i][j]]
7036 ->set_line_orientation(i, face->line_orientation(i));
7037 }
7038
7039 face->clear_user_flag();
7040 }
7041 } // FACES
7042
7043 // Cells
7045 cells_with_distorted_children;
7046
7048 triangulation.begin_active(0);
7049 for (unsigned int level = 0; level != triangulation.levels.size() - 1;
7050 ++level)
7051 {
7053 next_unused_cell = triangulation.begin_raw(level + 1);
7054 Assert(cell == triangulation.end() ||
7055 cell->level() >= static_cast<int>(level),
7057
7058 // Iterate over all active cells in the current level
7059 for (; cell != triangulation.end() &&
7060 cell->level() == static_cast<int>(level);
7061 ++cell)
7062 {
7063 if (cell->refine_flag_set() ==
7065 continue;
7066
7067 // Copy the requested refinement case to the cell's actual
7068 // refinement flag, informing the cell how it has been refined.
7069 const RefinementCase<dim> ref_case = cell->refine_flag_set();
7070 cell->clear_refine_flag();
7071 cell->set_refinement_case(ref_case);
7072
7073 const auto cell_reference_cell = cell->reference_cell();
7074
7075 unsigned int n_new_lines = 0;
7076 unsigned int n_new_faces = 0;
7077 const unsigned int n_new_cells =
7078 cell_reference_cell.n_isotropic_children();
7079
7080 // Must match the information used close the beginning of this
7081 // function.
7082 switch (cell_reference_cell)
7083 {
7085 n_new_lines = 1;
7086 n_new_faces = 8;
7087 break;
7088
7090 n_new_lines = 4;
7091 n_new_faces = 13;
7092 break;
7093
7095 n_new_lines = 3;
7096 n_new_faces = 10;
7097 break;
7098
7100 n_new_lines = 6;
7101 n_new_faces = 12;
7102 break;
7103
7104 default:
7106 }
7107
7108 // the following numbers should be the max numbers of the above.
7109 // They are used for sizing of arrays.
7110 constexpr unsigned int max_n_new_lines = 6;
7111 constexpr unsigned int max_n_new_faces = 13;
7112 constexpr unsigned int max_n_new_children = 10;
7113 constexpr unsigned int max_relevant_vertices = 27;
7114 constexpr unsigned int max_relevant_lines = 30;
7115 constexpr unsigned int max_face_indices = 36;
7116
7117 std::array<raw_line_iterator, max_n_new_lines> new_lines;
7118 for (unsigned int i = 0; i < n_new_lines; ++i)
7119 {
7120 new_lines[i] =
7121 triangulation.faces->lines
7122 .template next_free_single_object<1>(triangulation);
7123
7124 AssertIsNotUsed(new_lines[i]);
7125 new_lines[i]->set_used_flag();
7126 new_lines[i]->clear_user_flag();
7127 new_lines[i]->clear_user_data();
7128 new_lines[i]->clear_children();
7129 new_lines[i]->set_boundary_id_internal(
7131 new_lines[i]->set_manifold_id(cell->manifold_id());
7132 }
7133
7134 std::array<raw_quad_iterator, max_n_new_faces> new_faces;
7135 for (unsigned int i = 0; i < n_new_faces; ++i)
7136 {
7137 new_faces[i] =
7138 triangulation.faces->quads
7139 .template next_free_single_object<2>(triangulation);
7140
7141 auto &new_face = new_faces[i];
7142
7143 AssertIsNotUsed(new_face);
7144 new_face->set_used_flag();
7145 new_face->clear_user_flag();
7146 new_face->clear_user_data();
7147 new_face->clear_children();
7148 new_face->set_boundary_id_internal(
7150 new_face->set_manifold_id(cell->manifold_id());
7151
7152 // At this point the face doesn't have its ReferenceCell
7153 // set, so rely on lower-level functionality to reset
7154 // per-line data
7155 for (unsigned int j = 0;
7156 j < triangulation.faces->lines_per_quad;
7157 ++j)
7158 new_face->set_line_orientation(
7160 }
7161
7162
7163 { // CREATE_CELLS
7164 // Build a list of all relevant vertices required for
7165 // - The creation of additional faces not generated by the
7166 // refinement of the parent faces.
7167 // - The definition of the child cells.
7168 //
7169 // Number of meaningful entries in this list is
7170 // 10 Tetrahedron 18 Wedge
7171 // 14 Pyramid 27 Hexahedron
7172 // In case the maximum changes, edit `max_relevant_vertices` a
7173 // few lines above (or in case it was moved to ReferenceCell,
7174 // update it there).
7175 //
7176 // The following algorithm is used to get the vertices.
7177 // 1. Get vertices of the parent as provided.
7178 // https://dealii.org/developer/doxygen/deal.II/group__simplex.html#simplex_reference_cells.
7179 //
7180 // 2. Get the center vertices of the (refined) bounding lines
7181 // of the parent's faces (quad and tet (i.e. all) faces)
7182 //
7183 // .---3---. .
7184 // | | /|
7185 // 0 1 or 2 1
7186 // | | / |
7187 // .---2---. .-0-. n: vertices retrieved
7188 //
7189 // 3. Get the remaining new vertices that were created in the
7190 // center of the parent's bounding faces during refinement
7191 // (quad faces)
7192 //
7193 // .-------.
7194 // | |
7195 // .---O |
7196 // | | |
7197 // .---.---. O: vertex retrieved
7198 //
7199 // 4. Get (or, better yet, create) the newly required
7200 // vertices. (i.e. the new vertex in the middle of a hex,
7201 // only relevant for hexes)
7202 //
7203 // .-------.
7204 // /| |
7205 // . | / |
7206 // | - -O |
7207 // | .--:----.
7208 // |/ : /
7209 // .-------.
7210
7211 std::array<unsigned int, max_relevant_vertices>
7212 vertex_indices = {};
7213
7214 { // GET_VERTICES
7215 // continuous counter variable
7216 unsigned int k = 0;
7217
7218 // avoid a compiler warning by fixing max number of
7219 // iterations to 8.
7220 const unsigned int n_vertices =
7221 std::min(cell->n_vertices(), 8u);
7222
7223 // Step 1 Get vertices of the parent
7224 for (unsigned int i = 0; i < n_vertices; ++i)
7225 vertex_indices[k++] = cell->vertex_index(i);
7226
7227 // Step 2 (relevant for all faces)
7228
7229 // Avoid a compiler warning by fixing the max number of loop
7230 // iterations to 12 (max number of new lines)
7231 const unsigned int n_lines = std::min(cell->n_lines(), 12u);
7232
7233 const std::array<unsigned int, 12> line_indices =
7234 TriaAccessorImplementation::Implementation::
7235 get_line_indices_of_cell(*cell);
7236
7237 for (unsigned int l = 0; l < n_lines; ++l)
7238 {
7239 raw_line_iterator line(&triangulation,
7240 0,
7241 line_indices[l]);
7242 vertex_indices[k++] = line->child(0)->vertex_index(1);
7243 }
7244
7245 // Step 3 (depends on the reference cell type of face, only
7246 // quads affected)
7247 for (auto quad_face_index :
7248 cell_reference_cell.face_indices_by_type(
7250 {
7251 vertex_indices[k++] = cell->face(quad_face_index)
7252 ->child(0)
7253 ->vertex_index(3);
7254 }
7255
7256 // Step 4 Create the center vertex for hex cells
7257 if (cell_reference_cell == ReferenceCells::Hexahedron)
7258 {
7259 // Set single new vertex in the center
7260 current_vertex =
7261 get_next_unused_vertex(current_vertex,
7262 triangulation.vertices_used);
7263 vertex_indices[k++] = current_vertex;
7264
7265 triangulation.vertices[current_vertex] =
7266 cell->center(true, true);
7267 }
7268 } // GET_VERTICES
7269
7270
7271 unsigned int chosen_line_tetrahedron = 0;
7272
7273 // set up new lines
7274 switch (cell_reference_cell)
7275 {
7277 {
7278 // in the tetrahedron case, we have the three
7279 // possibilities (6,8), (5,7), (4,9) -> pick the
7280 // shortest line to guarantee the best possible aspect
7281 // ratios
7282 constexpr ::ndarray<unsigned int, 3, 2>
7283 new_line_vertices = {{{{6, 8}}, //
7284 {{5, 7}}, //
7285 {{4, 9}}}};
7286
7287 // choose line to cut either by refinement case or by
7288 // shortest distance between edge midpoints
7289 std::uint8_t refinement_choice =
7290 cell->refine_choice();
7291 if (refinement_choice ==
7292 static_cast<char>(IsotropicRefinementChoice::
7294 {
7295 const auto &vertices =
7296 triangulation.get_vertices();
7297 double min_distance =
7298 std::numeric_limits<double>::infinity();
7299 for (unsigned int i = 0;
7300 i < new_line_vertices.size();
7301 ++i)
7302 {
7303 const double current_distance =
7304 vertices[vertex_indices
7305 [new_line_vertices[i][0]]]
7306 .distance(
7307 vertices[vertex_indices
7308 [new_line_vertices[i][1]]]);
7309 if (current_distance < min_distance)
7310 {
7311 chosen_line_tetrahedron = i;
7312 min_distance = current_distance;
7313 }
7314 }
7315 }
7316 else if (refinement_choice ==
7317 static_cast<char>(
7319 chosen_line_tetrahedron = 0;
7320 else if (refinement_choice ==
7321 static_cast<char>(
7323 chosen_line_tetrahedron = 1;
7324 else if (refinement_choice ==
7325 static_cast<char>(
7327 chosen_line_tetrahedron = 2;
7328 else
7330
7331 cell->set_refinement_case(
7332 RefinementCase<dim>(chosen_line_tetrahedron + 1));
7333
7334 new_lines[0]->set_bounding_object_indices(
7336 [new_line_vertices[chosen_line_tetrahedron][0]],
7337 vertex_indices[new_line_vertices
7338 [chosen_line_tetrahedron][1]]});
7339 }
7340 break;
7341
7343 {
7344 // Directions so that middle bottom pyramids all have
7345 // lines with default orientation. Order as children
7346 // of refined bottom quad.
7347 static constexpr ::ndarray<unsigned int, 4, 2>
7348 new_line_vertices = {{{{13, 9}}, //
7349 {{13, 10}}, //
7350 {{13, 11}}, //
7351 {{13, 12}}}};
7352
7353 for (unsigned int i = 0; i < n_new_lines; ++i)
7354 new_lines[i]->set_bounding_object_indices(
7355 {vertex_indices[new_line_vertices[i][0]],
7356 vertex_indices[new_line_vertices[i][1]]});
7357 }
7358 break;
7359
7361 {
7362 // Directions so that middle tri looks like a 'normal'
7363 // refined tri (refined quads define outer lines)
7364 constexpr ::ndarray<unsigned int, 3, 2>
7365 new_line_vertices = {{{{15, 16}}, //
7366 {{16, 17}}, //
7367 {{15, 17}}}};
7368
7369 for (unsigned int i = 0; i < n_new_lines; ++i)
7370 new_lines[i]->set_bounding_object_indices(
7371 {vertex_indices[new_line_vertices[i][0]],
7372 vertex_indices[new_line_vertices[i][1]]});
7373 }
7374 break;
7375
7377 {
7378 constexpr ::ndarray<unsigned int, 6, 2>
7379 new_line_vertices = {{{{22, 26}},
7380 {{26, 23}},
7381 {{20, 26}},
7382 {{26, 21}},
7383 {{24, 26}},
7384 {{26, 25}}}};
7385 for (unsigned int i = 0; i < n_new_lines; ++i)
7386 new_lines[i]->set_bounding_object_indices(
7387 {vertex_indices[new_line_vertices[i][0]],
7388 vertex_indices[new_line_vertices[i][1]]});
7389 }
7390 break;
7391
7392 default:
7394 }
7395
7396 // Get the corrected orientation of the child indices of
7397 // `child` for a refined face, given the `face_ref_cell_type`
7398 // and the `face_orientation` as stored in the cell it bounds.
7399 auto standard_to_real_quad_child_index =
7400 [](const unsigned int child,
7401 const types::geometric_orientation face_orientation,
7402 const ReferenceCell<dim - 1> face_ref_cell_type) {
7403 // The parent vertex indices correlate nicely with the
7404 // child indices of a tri and quad, as shown in the
7405 // drawing for 2D face refinement (see the curly brackets
7406 // tagged {} // FACES). We can thus apply the existing
7407 // algorithm for orientation correction of vertex indices
7408 // and reinterpret the output. The only problem is child 3
7409 // in triangles. However it is always in the center so no
7410 // correction is necessary at all.
7411 switch (face_ref_cell_type)
7412 {
7414 // A complicated way to query the private attribute
7415 // `ReferenceCell::triangle_vertex_permutations[face_orientation][vertex]`
7416 if (child != 3)
7419 0,
7420 face_orientation);
7421 else
7422 return 3u;
7423
7425 // `ReferenceCell::quadrilateral_vertex_permutations[face_orientation][vertex]`.
7428 0,
7429 face_orientation);
7430 default:
7432 }
7433 };
7434
7435 // set up new faces
7436 {
7437 // Similar to `vertex_indices` above the `relevant_lines`
7438 // are filled in multiple steps. The order is once again
7439 // determined by the reference cell and the refinement
7440 // algorithms used by the faces. (For Tetrahedron
7441 // `chosen_line_tetrahedron` is also relevant).
7442 // 1. Get the lines from the faces that are Quadrilaterals.
7443 //
7444 // .- -.- -.
7445 // : 1 :
7446 // .-2-.-3-.
7447 // : 0 :
7448 // .- - - -. n: extracted lines
7449 //
7450 // 2. Get the lines from the faces that are Triangles.
7451 //
7452 // | \ .
7453 // *--1--* .
7454 // |\ |\ .
7455 // | 2 0 \ .
7456 // | \| .
7457 // *-----*--- .
7458 //
7459 // 3. Newly created lines (see above)
7460 // 4. Load indices for the extracted lines
7461 //
7462 // Number of meaningful entries in this list is
7463 // 13 Tetrahedron 21 Wedge
7464 // 20 Pyramid 30 Hexahedron
7465 // In case the maximum changes, edit `max_relevant_lines` a
7466 // few lines above (or in case it was moved to
7467 // ReferenceCell, update it there).
7468
7469 std::array<raw_line_iterator, max_relevant_lines>
7470 relevant_lines;
7471 std::array<int, max_relevant_lines> relevant_line_indices;
7472 unsigned int relevant_lines_counter = 0;
7473 // 1. Get relevant lines from quads
7474 for (auto f : cell_reference_cell.face_indices_by_type(
7476 {
7477 // To retrieve the newly created lines within a quad, it
7478 // is sufficient to query child 0 and 3 (or 1 and 2), as
7479 // they border all relevant lines. Specifically, we are
7480 // interested in lines 1 and 3 of child 0, and lines
7481 // 0 and 2 of child 3. This is currently implemented in
7482 // `quad_relevant_children_and_lines` below (where the
7483 // first index is the child and the second is the
7484 // child's line index). A more readable implementation
7485 // is provided right below. However, it cannot be
7486 // (nicely) used to mimic the existing table's behavior.
7487 // Modifications to this behavior would result in
7488 // extensive rewriting of tables in `ReferenceCell`.
7489 //
7490 // constexpr std::
7491 // pair<unsigned int, std::array<unsigned int, 2>>
7492 // quad_relevant_children_and_lines = {{0, {1, 3}},
7493 // {3, {0, 2}}};
7494 constexpr ::ndarray<unsigned int, 4, 2>
7495 quad_relevant_children_and_lines = {{{{0, 1}}, //
7496 {{3, 0}}, //
7497 {{0, 3}}, //
7498 {{3, 2}}}};
7499 for (unsigned int l = 0; l < 4;
7500 ++l, ++relevant_lines_counter)
7501 {
7502 // Use previously defined helper function to find
7503 // children in an orientation-corrected way.
7504 const unsigned int relevant_child =
7505 standard_to_real_quad_child_index(
7506 quad_relevant_children_and_lines[l][0],
7507 cell->combined_face_orientation(f),
7509
7510 // Get the orientation corrected line index of the
7511 // child while using the fact that quad children
7512 // share their physical orientation with the parent.
7513 const unsigned int relevant_child_line =
7515 .standard_to_real_face_line(
7516 quad_relevant_children_and_lines[l][1],
7517 // face irrelevant -> function doesn't use it.
7518 0,
7519 cell->combined_face_orientation(f));
7520
7521
7522 // extract and store line
7523 relevant_lines[relevant_lines_counter] =
7524 cell->face(f)
7525 ->isotropic_child(relevant_child)
7526 ->line(relevant_child_line);
7527 }
7528 }
7529
7530 // 2. Get lines from tris
7531 for (auto f : cell_reference_cell.face_indices_by_type(
7533 {
7534 for (unsigned int l = 0; l < 3;
7535 ++l, ++relevant_lines_counter)
7536 {
7537 // Permutations of reference cell indices against
7538 // real world indices given the real world
7539 // orientation of the face.
7540 const std::array<std::array<unsigned int, 3>, 6>
7541 // Extract newly created lines surrounding the
7542 // center child (child 3) of the refined
7543 // triangular faces in a predictable manner (i.e.,
7544 // the lines always appear at the same location
7545 // within the to be refined 3d reference cell),
7546 // the orientation of the face must be considered.
7547 // This orientation correction is implemented via
7548 // the table below relating the lines of the
7549 // oriented face to their counterparts on the
7550 // reference cell face.
7551 tri_line_perm = {{{{0, 1, 2}}, // 0
7552 {{1, 0, 2}},
7553 {{2, 0, 1}}, // 2
7554 {{0, 2, 1}},
7555 {{1, 2, 0}}, // 4
7556 {{2, 1, 0}}}};
7557
7558 const auto combined_orientation =
7559 cell->combined_face_orientation(f);
7560 relevant_lines[relevant_lines_counter] =
7561 cell->face(f)
7562 ->child(3 /*center triangle*/)
7563 ->line(tri_line_perm[combined_orientation][l]);
7564 }
7565 }
7566
7567 // 3. Fill end of `relevant_lines` with `new_lines`.
7568 for (unsigned int i = 0; i < n_new_lines;
7569 ++i, ++relevant_lines_counter)
7570 relevant_lines[relevant_lines_counter] = new_lines[i];
7571
7572
7573 // 4. Extract and store indices of the relevant lines.
7574 for (unsigned int i = 0; i < relevant_lines_counter; ++i)
7575 relevant_line_indices[i] = relevant_lines[i]->index();
7576
7577
7578 // New faces (internal to the parent) are required for the
7579 // creation of children. The lines bounding these new faces
7580 // are stored in `relevant_lines`. The exact assignment of
7581 // these lines is then determined using the lookup table
7582 // retrieved below, by providing the appropriate indices
7583 // into `relevant_lines`.
7584 const auto &new_face_lines =
7585 cell_reference_cell.new_isotropic_child_face_lines(
7586 chosen_line_tetrahedron);
7587
7588 // The created lines might not be oriented according to the
7589 // expectation of the face's reference cell. Consequently,
7590 // an additional lookup table is necessary to define the
7591 // correct line connectivity desired by the reference cell.
7592 // This is very similar to the refinement of triangles in 2d
7593 // where the same problem occurred.
7594 const auto &new_face_lines_vert =
7595 cell_reference_cell
7596 .new_isotropic_child_face_line_vertices(
7597 chosen_line_tetrahedron);
7598
7599 // When fixing the orientation of lines for hexes the
7600 // calculated orientation of one line can be used for some
7601 // other lines as well. This connection is described in this
7602 // table.
7603 constexpr ::ndarray<unsigned int, 4, 2>
7604 representative_lines{{{{0, 2}}, //
7605 {{2, 0}}, //
7606 {{3, 3}}, //
7607 {{1, 1}}}};
7608
7609 for (unsigned int q = 0; q < n_new_faces; ++q)
7610 {
7611 auto &new_face = new_faces[q];
7612
7613 if (new_face_lines[q][3] == X)
7614 {
7615 // Set the face type implicitly, based on whether
7616 // the fourth line is `X`.
7617 triangulation.faces->set_quad_type(
7618 new_face->index(), ReferenceCells::Triangle);
7619
7620 new_face->set_bounding_object_indices(
7621 {relevant_line_indices[new_face_lines[q][0]],
7622 relevant_line_indices[new_face_lines[q][1]],
7623 relevant_line_indices[new_face_lines[q][2]]});
7624 }
7625 else
7626 {
7627 triangulation.faces->set_quad_type(
7628 new_face->index(), ReferenceCells::Quadrilateral);
7629
7630 new_face->set_bounding_object_indices(
7631 {relevant_line_indices[new_face_lines[q][0]],
7632 relevant_line_indices[new_face_lines[q][1]],
7633 relevant_line_indices[new_face_lines[q][2]],
7634 relevant_line_indices[new_face_lines[q][3]]});
7635 }
7636
7637 // On hexes, we must only determine a single line
7638 // according to the representative_lines array above
7639 // (this saves expensive operations), for tets we do
7640 // all lines manually
7641 const unsigned int n_compute_lines =
7642 cell_reference_cell == ReferenceCells::Hexahedron ?
7643 1 :
7644 new_face->n_lines();
7645 for (unsigned int line = 0; line < n_compute_lines;
7646 ++line)
7647 {
7648 const unsigned int l =
7649 (cell_reference_cell ==
7651 representative_lines[q % 4][0] :
7652 line;
7653
7654 const std::array<unsigned int, 2> vertices_0 = {
7655 {relevant_lines[new_face_lines[q][l]]
7656 ->vertex_index(0),
7657 relevant_lines[new_face_lines[q][l]]
7658 ->vertex_index(1)}};
7659
7660 const std::array<unsigned int, 2> vertices_1 = {
7661 {vertex_indices[new_face_lines_vert[q][l][0]],
7662 vertex_indices[new_face_lines_vert[q][l][1]]}};
7663
7664 const auto orientation =
7665 ReferenceCells::Line.get_combined_orientation(
7666 make_array_view(vertices_0),
7667 make_array_view(vertices_1));
7668
7669 new_face->set_line_orientation(l, orientation);
7670
7671 // on a hex, inject the status of the current line
7672 // also to the line on the other quad along the
7673 // same direction
7674 if (cell_reference_cell ==
7676 new_faces[representative_lines[q % 4][1] + q -
7677 (q % 4)]
7678 ->set_line_orientation(l, orientation);
7679 }
7680 }
7681 }
7682
7683 // set up new cell
7684 {
7685 // Similar to the steps before we now use an algorithm to
7686 // extract all the faces.
7687 // 1. Add newly created faces to the beginning.
7688 // 2. Get children of refined quad faces.
7689 // for numbering see {} // FACE.
7690 // 3. Get children of refined tri faces.
7691 // for numbering see {} // FACE.
7692 //
7693 // Number of meaningful entries in this list is
7694 // 24 Tetrahedron 30 Wedge
7695 // 33 Pyramid 36 Hexahedron
7696 // In case the maximum changes, edit `max_face_indices` a
7697 // few lines above (or in case it was moved to
7698 // ReferenceCell, update it there).
7699
7700 std::array<int, max_face_indices> face_indices;
7701
7702 unsigned int face_indices_counter = 0;
7703
7704 // 1. Get new face's ids.
7705 for (; face_indices_counter < n_new_faces;
7706 ++face_indices_counter)
7707 face_indices[face_indices_counter] =
7708 new_faces[face_indices_counter]->index();
7709
7710 // 2. Now the faces of the parent's refined quads.
7711 for (auto f_q : cell_reference_cell.face_indices_by_type(
7713 {
7714 for (unsigned int c = 0; c < 4;
7715 ++c, ++face_indices_counter)
7716 {
7717 const auto combined_orientation =
7718 cell->combined_face_orientation(f_q);
7719
7720 // Correct once again for orientation.
7721 face_indices[face_indices_counter] =
7722 cell->face(f_q)->isotropic_child_index(
7723 standard_to_real_quad_child_index(
7724 c,
7725 combined_orientation,
7727 }
7728 }
7729
7730 // 3. Now the faces of the parent's refined tris
7731 for (auto f_t : cell_reference_cell.face_indices_by_type(
7733 {
7734 for (unsigned int c = 0; c < 4;
7735 ++c, ++face_indices_counter)
7736 {
7737 const auto combined_orientation =
7738 cell->combined_face_orientation(f_t);
7739
7740 // Correct once again for orientation.
7741 face_indices[face_indices_counter] =
7742 cell->face(f_t)->child_index(
7743 standard_to_real_quad_child_index(
7744 c,
7745 combined_orientation,
7747 }
7748 }
7749
7750 // Similar to the faces described above, we need to identify
7751 // which faces (specified by their indices in
7752 // `face_indices`) bound the new cells. This information is
7753 // stored in the ReferenceCell and retrieved here.
7754 const auto cell_faces =
7755 cell_reference_cell.new_isotropic_child_cell_faces(
7756 chosen_line_tetrahedron);
7757
7758 // We also need to account for potential orientation
7759 // mismatches between the face and the cell's ReferenceCell.
7760 // (See the discussion above in new face creation for more
7761 // details.)
7762 const auto new_cells_vertices =
7763 cell_reference_cell.new_isotropic_child_cell_vertices(
7764 chosen_line_tetrahedron);
7765
7766 // 1. Preserve data that can only be extracted from active
7767 // cells.
7768 // Parent becomes inactive after setting the first child!
7769 const ::types::subdomain_id cell_subdomain_id =
7770 cell->subdomain_id();
7771
7772 // Create the new cells
7773 std::array<
7775 max_n_new_children>
7776 new_cells;
7777 for (unsigned int c = 0; c < n_new_cells; ++c)
7778 {
7779 // Since we search for pairs of hexes (next_free_hex
7780 // returns a pair) we need to search only every other
7781 // time.
7782 if (c % 2 == 0)
7783 next_unused_cell =
7784 triangulation.levels[level + 1]
7785 ->cells.next_free_hex(triangulation, level + 1);
7786 else
7787 ++next_unused_cell;
7788
7789 new_cells[c] = next_unused_cell;
7790
7791 auto &new_cell = new_cells[c];
7792
7793 AssertIsNotUsed(new_cell);
7794
7795 // 2. Copy parent data to child.
7796 new_cell->set_used_flag();
7797 new_cell->clear_user_flag();
7798 new_cell->clear_user_data();
7799 new_cell->clear_children();
7800 new_cell->set_material_id(cell->material_id());
7801 new_cell->set_manifold_id(cell->manifold_id());
7802 new_cell->set_subdomain_id(cell_subdomain_id);
7803
7804 // 3. Setup child parent relation (can only happen after
7805 // call to `set_used_flag()`)
7806 if (c % 2 == 0)
7807 {
7808 new_cell->set_parent(cell->index());
7809 cell->set_children(c, new_cell->index());
7810 }
7811
7812 // 4.a Determine the cell reference cell using the
7813 // number of vertices it has according to
7814 // `new_cells_vertices`.
7815 const ReferenceCell child_reference_cell =
7816 (new_cells_vertices[c][4] == X) ? ReferenceCells::Tetrahedron : // Tet
7817 (new_cells_vertices[c][5] == X) ? ReferenceCells::Pyramid : // Pyramid
7818 (new_cells_vertices[c][6] == X) ? ReferenceCells::Wedge : // Wedge
7820
7821 // 4.b Set the reference cell
7822 triangulation.levels[new_cell->level()]
7823 ->reference_cell[new_cell->index()] =
7824 child_reference_cell;
7825
7826 // 5. Set the bounding faces of the new cells
7827 // This seems to be the only option since
7828 // set_bounding_object_indices() takes an
7829 // std::initializer_list as argument
7830 switch (child_reference_cell)
7831 {
7833 new_cell->set_bounding_object_indices(
7834 {face_indices[cell_faces[c][0]],
7835 face_indices[cell_faces[c][1]],
7836 face_indices[cell_faces[c][2]],
7837 face_indices[cell_faces[c][3]]});
7838 break;
7839
7840 // Pyramids and Wedges share same number of faces
7843 new_cell->set_bounding_object_indices(
7844 {face_indices[cell_faces[c][0]],
7845 face_indices[cell_faces[c][1]],
7846 face_indices[cell_faces[c][2]],
7847 face_indices[cell_faces[c][3]],
7848 face_indices[cell_faces[c][4]]});
7849 break;
7850
7852 new_cell->set_bounding_object_indices(
7853 {face_indices[cell_faces[c][0]],
7854 face_indices[cell_faces[c][1]],
7855 face_indices[cell_faces[c][2]],
7856 face_indices[cell_faces[c][3]],
7857 face_indices[cell_faces[c][4]],
7858 face_indices[cell_faces[c][5]]});
7859 break;
7860
7861 default:
7863 }
7864
7865 // 6. Fix orientation of faces.
7866 // For all reference cells except for Hexes, we need to
7867 // go through the faces and figure the orientation out
7868 // the hard way
7869
7870 // Set the orientation flag to its default state for
7871 // all faces initially. later on go the other way
7872 // round and reset faces that are at the boundary of
7873 // the mother cube.
7874 // TODO: This might only be necessary for Hexes. For
7875 // all other cells the orientation is calculated for
7876 // all faces one by one. (Only a guess though).
7877 for (const auto f : new_cell->face_indices())
7878 new_cell->set_combined_face_orientation(
7880
7881 if (child_reference_cell != ReferenceCells::Hexahedron)
7882 {
7883 for (const auto f : new_cell->face_indices())
7884 {
7885 const auto &face = new_cell->face(f);
7886
7887 // load correct vertices of cell
7888 auto new_cell_vertices = new_cells_vertices[c];
7889
7890 // Load correct indices of vertices of face `f`
7891 std::array<unsigned int, 4> vertices_0,
7892 vertices_1;
7893 vertices_0.fill(numbers::invalid_unsigned_int);
7894 vertices_1.fill(numbers::invalid_unsigned_int);
7895
7896 for (unsigned int face_vertex_no :
7897 face->vertex_indices())
7898 {
7899 // Calculate index for vertex
7900 // `face_vertex_no` of face `f` as an index
7901 // in the cells reference cells indices.
7902 const auto cell_vertex_no =
7903 child_reference_cell
7905 f,
7906 face_vertex_no,
7907 numbers::
7908 default_geometric_orientation);
7909
7910 // Get according index from
7911 // `vertex_indices`. Use `cell_vertex_no` as
7912 // the index in the table describing which
7913 // vertices belong to which cell and then
7914 // get that index. Use `cell_vertex_no` to
7915 // look up the specific vertex (more
7916 // precisely the index in `vertex_indices`)
7917 // within the cell's connectivity table
7918 // `new_cell_vertices` and retrieve the
7919 // corresponding vertex (index) from
7920 // `vertex_indices`.
7921 vertices_0[face_vertex_no] = vertex_indices
7922 [new_cell_vertices[cell_vertex_no]];
7923 }
7924
7925 // max 4 vertices (if face is quad)
7926 for (const auto i : face->vertex_indices())
7927 vertices_1[i] = face->vertex_index(i);
7928
7929 // Calculate combined orientation as permutation
7930 // of desired face vertex indices an actual
7931 // vertex indices of the face.
7932 new_cell->set_combined_face_orientation(
7933 f,
7934 face->reference_cell()
7935 .get_combined_orientation(
7936 make_array_view(vertices_0.cbegin(),
7937 vertices_0.cbegin() +
7938 face->n_vertices()),
7939 make_array_view(vertices_1.cbegin(),
7940 vertices_1.cbegin() +
7941 face->n_vertices())));
7942 }
7943 }
7944 }
7945
7946 // For hexes, we can simply inherit the orientation values
7947 // from the parent on the outer faces; the inner faces can
7948 // be skipped as their orientation is always the default
7949 // one set above.
7950 constexpr ::ndarray<unsigned int, 6, 4>
7951 face_to_child_indices_hex{{{{0, 2, 4, 6}},
7952 {{1, 3, 5, 7}},
7953 {{0, 1, 4, 5}},
7954 {{2, 3, 6, 7}},
7955 {{0, 1, 2, 3}},
7956 {{4, 5, 6, 7}}}};
7957 if (cell_reference_cell == ReferenceCells::Hexahedron)
7958 for (const auto f : cell->face_indices())
7959 {
7960 const auto combined_orientation =
7961 cell->combined_face_orientation(f);
7962 for (unsigned int c = 0; c < 4; ++c)
7963 new_cells[face_to_child_indices_hex[f][c]]
7964 ->set_combined_face_orientation(
7965 f, combined_orientation);
7966 }
7967 }
7968 } // CREATE_CELLS
7969
7970 // Check for distorted children
7971 if (check_for_distorted_cells &&
7972 has_distorted_children<dim, spacedim>(cell))
7973 cells_with_distorted_children.distorted_cells.push_back(cell);
7974
7975 triangulation.signals.post_refinement_on_cell(cell);
7976 }
7977 }
7978
7979 triangulation.faces->quads.clear_user_data();
7980
7981 return cells_with_distorted_children;
7982 }
7983
7988 template <int spacedim>
7991 const bool check_for_distorted_cells)
7992 {
7993 const unsigned int dim = 3;
7994
7995 {
7996 bool flag_isotropic_mesh = true;
7998 cell = triangulation.begin(),
7999 endc = triangulation.end();
8000 for (; cell != endc; ++cell)
8001 if (cell->used())
8002 if (triangulation.get_anisotropic_refinement_flag() ||
8003 cell->refine_flag_set() == RefinementCase<dim>::cut_x ||
8004 cell->refine_flag_set() == RefinementCase<dim>::cut_y ||
8005 cell->refine_flag_set() == RefinementCase<dim>::cut_z ||
8006 cell->refine_flag_set() == RefinementCase<dim>::cut_xy ||
8007 cell->refine_flag_set() == RefinementCase<dim>::cut_xz ||
8008 cell->refine_flag_set() == RefinementCase<dim>::cut_yz)
8009 {
8010 flag_isotropic_mesh = false;
8011 break;
8012 }
8013
8014 if (flag_isotropic_mesh)
8015 return execute_refinement_isotropic(triangulation,
8016 check_for_distorted_cells);
8017 }
8018
8019 // this function probably also works for spacedim>3 but it
8020 // isn't tested. it will probably be necessary to pull new
8021 // vertices onto the manifold just as we do for the other
8022 // functions above.
8023 Assert(spacedim == 3, ExcNotImplemented());
8024
8025 // Check whether a new level is needed. We have to check for
8026 // this on the highest level only
8027 for (const auto &cell : triangulation.active_cell_iterators_on_level(
8028 triangulation.levels.size() - 1))
8029 if (cell->refine_flag_set())
8030 {
8032 triangulation.levels.size() < numbers::max_n_levels,
8034 triangulation.levels.push_back(
8035 std::make_unique<internal::TriangulationImplementation::
8037 triangulation.strides.max_children_per_cell,
8038 triangulation.strides.max_faces_per_cell,
8039 triangulation.strides.max_vertices_per_cell));
8040 break;
8041 }
8042
8043
8044 // first clear user flags for quads and lines; we're going to
8045 // use them to flag which lines and quads need refinement
8046 triangulation.faces->quads.clear_user_data();
8047
8049 triangulation.begin_line();
8050 line != triangulation.end_line();
8051 ++line)
8052 line->clear_user_flag();
8054 triangulation.begin_quad();
8055 quad != triangulation.end_quad();
8056 ++quad)
8057 quad->clear_user_flag();
8058
8059 // create an array of face refine cases. User indices of faces
8060 // will be set to values corresponding with indices in this
8061 // array.
8062 const RefinementCase<dim - 1> face_refinement_cases[4] = {
8063 RefinementCase<dim - 1>::no_refinement,
8064 RefinementCase<dim - 1>::cut_x,
8065 RefinementCase<dim - 1>::cut_y,
8066 RefinementCase<dim - 1>::cut_xy};
8067
8068 // check how much space is needed on every level. We need not
8069 // check the highest level since either
8070 // - on the highest level no cells are flagged for refinement
8071 // - there are, but prepare_refinement added another empty
8072 // level which then is the highest level
8073
8074 // variables to hold the number of newly to be created
8075 // vertices, lines and quads. as these are stored globally,
8076 // declare them outside the loop over al levels. we need lines
8077 // and quads in pairs for refinement of old ones and lines and
8078 // quads, that can be stored as single ones, as they are newly
8079 // created in the inside of an existing cell
8080 unsigned int needed_vertices = 0;
8081 unsigned int needed_lines_single = 0;
8082 unsigned int needed_quads_single = 0;
8083 unsigned int needed_lines_pair = 0;
8084 unsigned int needed_quads_pair = 0;
8085 for (int level_no = triangulation.levels.size() - 2; level_no >= 0;
8086 --level_no)
8087 {
8088 // count number of flagged cells on this level and compute
8089 // how many new vertices and new lines will be needed
8090 std::size_t needed_cells = 0;
8091
8092 for (const auto &acell :
8093 triangulation.active_cell_iterators_on_level(level_no))
8094 if (acell->refine_flag_set())
8095 {
8096 RefinementCase<dim> ref_case = acell->refine_flag_set();
8097
8098 // now for interior vertices, lines and quads, which
8099 // are needed in any case
8100 if (ref_case == RefinementCase<dim>::cut_x ||
8101 ref_case == RefinementCase<dim>::cut_y ||
8102 ref_case == RefinementCase<dim>::cut_z)
8103 {
8104 ++needed_quads_single;
8105 needed_cells += 2;
8106 triangulation.anisotropic_refinement = true;
8107 }
8108 else if (ref_case == RefinementCase<dim>::cut_xy ||
8109 ref_case == RefinementCase<dim>::cut_xz ||
8110 ref_case == RefinementCase<dim>::cut_yz)
8111 {
8112 ++needed_lines_single;
8113 needed_quads_single += 4;
8114 needed_cells += 4;
8115 triangulation.anisotropic_refinement = true;
8116 }
8117 else if (ref_case == RefinementCase<dim>::cut_xyz)
8118 {
8119 ++needed_vertices;
8120 needed_lines_single += 6;
8121 needed_quads_single += 12;
8122 needed_cells += 8;
8123 }
8124 else
8125 {
8126 // we should never get here
8128 }
8129
8130 // mark all faces for refinement; checking locally
8131 // if and how the neighbor would like to refine
8132 // these is difficult so we only flag them and after
8133 // visiting all cells, we decide which faces need
8134 // which refinement;
8135 for (const unsigned int face :
8137 {
8139 aface = acell->face(face);
8140 // get the RefineCase this faces has for the
8141 // given RefineCase of the cell
8142 RefinementCase<dim - 1> face_ref_case =
8144 ref_case,
8145 face,
8146 acell->face_orientation(face),
8147 acell->face_flip(face),
8148 acell->face_rotation(face));
8149 // only do something, if this face has to be
8150 // refined
8151 if (face_ref_case)
8152 {
8153 if (face_ref_case ==
8155 {
8156 if (aface->n_active_descendants() < 4)
8157 // we use user_flags to denote needed
8158 // isotropic refinement
8159 aface->set_user_flag();
8160 }
8161 else if (aface->refinement_case() != face_ref_case)
8162 // we use user_indices to denote needed
8163 // anisotropic refinement. note, that we
8164 // can have at most one anisotropic
8165 // refinement case for this face, as
8166 // otherwise prepare_refinement() would
8167 // have changed one of the cells to yield
8168 // isotropic refinement at this
8169 // face. therefore we set the user_index
8170 // uniquely
8171 {
8172 Assert(aface->refinement_case() ==
8174 dim - 1>::isotropic_refinement ||
8175 aface->refinement_case() ==
8178 aface->set_user_index(face_ref_case);
8179 }
8180 }
8181 } // for all faces
8182
8183 // flag all lines, that have to be refined
8184 for (unsigned int line = 0;
8185 line < GeometryInfo<dim>::lines_per_cell;
8186 ++line)
8188 line) &&
8189 !acell->line(line)->has_children())
8190 acell->line(line)->set_user_flag();
8191
8192 } // if refine_flag set and for all cells on this level
8193
8194 // count number of used cells on the next higher level
8195 auto &next_level = *triangulation.levels[level_no + 1];
8196 const auto used_cells = std::count(next_level.cells.used.begin(),
8197 next_level.cells.used.end(),
8198 true);
8199 if (used_cells + needed_cells > next_level.size())
8200 next_level.allocate_end(
8201 (used_cells + needed_cells) - next_level.size(),
8202 // We unconditionally store the orientation in 3d
8203 true,
8204 // TODO: we can get rid of this check (i.e., we don't need to
8205 // store this value for purely wedge meshes). This will be
8206 // simpler to do once we merge IsotropicRefinementChoice and
8207 // RefinementCase.
8208 !triangulation.all_reference_cells_are_hyper_cube());
8209
8210 // TODO: perhaps we can merge this with TriaLevel::allocate_end()
8211 next_level.cells.allocate_end(needed_cells, 0);
8212 } // for all levels
8213 // now count the quads and lines which were flagged for
8214 // refinement
8216 triangulation.begin_quad();
8217 quad != triangulation.end_quad();
8218 ++quad)
8219 {
8220 if (quad->user_flag_set())
8221 {
8222 // isotropic refinement: 1 interior vertex, 4 quads
8223 // and 4 interior lines. we store the interior lines
8224 // in pairs in case the face is already or will be
8225 // refined anisotropically
8226 needed_quads_pair += 4;
8227 needed_lines_pair += 4;
8228 needed_vertices += 1;
8229 }
8230 if (quad->user_index())
8231 {
8232 // anisotropic refinement: 1 interior
8233 // line and two quads
8234 needed_quads_pair += 2;
8235 needed_lines_single += 1;
8236 // there is a kind of complicated situation here which
8237 // requires our attention. if the quad is refined
8238 // isotropcally, two of the interior lines will get a
8239 // new mother line - the interior line of our
8240 // anisotropically refined quad. if those two lines
8241 // are not consecutive, we cannot do so and have to
8242 // replace them by two lines that are consecutive. we
8243 // try to avoid that situation, but it may happen
8244 // nevertheless through repeated refinement and
8245 // coarsening. thus we have to check here, as we will
8246 // need some additional space to store those new lines
8247 // in case we need them...
8248 if (quad->has_children())
8249 {
8250 Assert(quad->refinement_case() ==
8253 if ((face_refinement_cases[quad->user_index()] ==
8255 (quad->child(0)->line_index(1) + 1 !=
8256 quad->child(2)->line_index(1))) ||
8257 (face_refinement_cases[quad->user_index()] ==
8259 (quad->child(0)->line_index(3) + 1 !=
8260 quad->child(1)->line_index(3))))
8261 needed_lines_pair += 2;
8262 }
8263 }
8264 }
8265
8267 triangulation.begin_line();
8268 line != triangulation.end_line();
8269 ++line)
8270 if (line->user_flag_set())
8271 {
8272 needed_lines_pair += 2;
8273 needed_vertices += 1;
8274 }
8275
8276 // reserve space for needed_lines new lines stored in pairs
8277 triangulation.faces->lines.allocate_end(needed_lines_pair,
8278 needed_lines_single);
8279
8280 // reserve space for needed_quads new quads stored in pairs
8281 triangulation.faces->allocate_end(needed_quads_pair,
8282 needed_quads_single);
8283
8284 // add to needed vertices how many vertices are already in use
8285 needed_vertices += std::count(triangulation.vertices_used.begin(),
8286 triangulation.vertices_used.end(),
8287 true);
8288 // if we need more vertices: create them, if not: leave the
8289 // array as is, since shrinking is not really possible because
8290 // some of the vertices at the end may be in use
8291 if (needed_vertices > triangulation.vertices.size())
8292 {
8293 triangulation.vertices.resize(needed_vertices, Point<spacedim>());
8294 triangulation.vertices_used.resize(needed_vertices, false);
8295 }
8296
8297
8298 //-----------------------------------------
8299 // Before we start with the actual refinement, we do some
8300 // sanity checks if in debug mode. especially, we try to catch
8301 // the notorious problem with lines being twice refined,
8302 // i.e. there are cells adjacent at one line ("around the
8303 // edge", but not at a face), with two cells differing by more
8304 // than one refinement level
8305 //
8306 // this check is very simple to implement here, since we have
8307 // all lines flagged if they shall be refined
8308 if constexpr (running_in_debug_mode())
8309 {
8310 for (const auto &cell : triangulation.active_cell_iterators())
8311 if (!cell->refine_flag_set())
8312 for (unsigned int line = 0;
8313 line < GeometryInfo<dim>::lines_per_cell;
8314 ++line)
8315 if (cell->line(line)->has_children())
8316 for (unsigned int c = 0; c < 2; ++c)
8317 Assert(cell->line(line)->child(c)->user_flag_set() ==
8318 false,
8320 }
8321
8322 //-----------------------------------------
8323 // Do refinement on every level
8324 //
8325 // To make life a bit easier, we first refine those lines and
8326 // quads that were flagged for refinement and then compose the
8327 // newly to be created cells.
8328 //
8329 // index of next unused vertex
8330 unsigned int next_unused_vertex = 0;
8331
8332 // first for lines
8333 {
8334 // only active objects can be refined further
8336 line = triangulation.begin_active_line(),
8337 endl = triangulation.end_line();
8339 next_unused_line = triangulation.begin_raw_line();
8340
8341 for (; line != endl; ++line)
8342 if (line->user_flag_set())
8343 {
8344 // this line needs to be refined
8345
8346 // find the next unused vertex and set it
8347 // appropriately
8348 while (triangulation.vertices_used[next_unused_vertex] == true)
8349 ++next_unused_vertex;
8350 Assert(
8351 next_unused_vertex < triangulation.vertices.size(),
8352 ExcMessage(
8353 "Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
8354 triangulation.vertices_used[next_unused_vertex] = true;
8355
8356 triangulation.vertices[next_unused_vertex] = line->center(true);
8357
8358 // now that we created the right point, make up the
8359 // two child lines (++ takes care of the end of the
8360 // vector)
8361 next_unused_line =
8362 triangulation.faces->lines.template next_free_pair_object<1>(
8363 triangulation);
8364 Assert(next_unused_line.state() == IteratorState::valid,
8366
8367 // now we found two consecutive unused lines, such
8368 // that the children of a line will be consecutive.
8369 // then set the child pointer of the present line
8370 line->set_children(0, next_unused_line->index());
8371
8372 // set the two new lines
8374 children[2] = {next_unused_line, ++next_unused_line};
8375
8376 // some tests; if any of the iterators should be
8377 // invalid, then already dereferencing will fail
8378 AssertIsNotUsed(children[0]);
8379 AssertIsNotUsed(children[1]);
8380
8381 children[0]->set_bounding_object_indices(
8382 {line->vertex_index(0), next_unused_vertex});
8383 children[1]->set_bounding_object_indices(
8384 {next_unused_vertex, line->vertex_index(1)});
8385
8386 children[0]->set_used_flag();
8387 children[1]->set_used_flag();
8388 children[0]->clear_children();
8389 children[1]->clear_children();
8390 children[0]->clear_user_data();
8391 children[1]->clear_user_data();
8392 children[0]->clear_user_flag();
8393 children[1]->clear_user_flag();
8394
8395 children[0]->set_boundary_id_internal(line->boundary_id());
8396 children[1]->set_boundary_id_internal(line->boundary_id());
8397
8398 children[0]->set_manifold_id(line->manifold_id());
8399 children[1]->set_manifold_id(line->manifold_id());
8400
8401 // finally clear flag
8402 // indicating the need
8403 // for refinement
8404 line->clear_user_flag();
8405 }
8406 }
8407
8408
8409 //-------------------------------------
8410 // now refine marked quads
8411 //-------------------------------------
8412
8413 // here we encounter several cases:
8414
8415 // a) the quad is unrefined and shall be refined isotropically
8416
8417 // b) the quad is unrefined and shall be refined
8418 // anisotropically
8419
8420 // c) the quad is unrefined and shall be refined both
8421 // anisotropically and isotropically (this is reduced to case
8422 // b) and then case b) for the children again)
8423
8424 // d) the quad is refined anisotropically and shall be refined
8425 // isotropically (this is reduced to case b) for the
8426 // anisotropic children)
8427
8428 // e) the quad is refined isotropically and shall be refined
8429 // anisotropically (this is transformed to case c), however we
8430 // might have to renumber/rename children...)
8431
8432 // we need a loop in cases c) and d), as the anisotropic
8433 // children might have a lower index than the mother quad
8434 for (unsigned int loop = 0; loop < 2; ++loop)
8435 {
8436 // usually, only active objects can be refined
8437 // further. however, in cases d) and e) that is not true,
8438 // so we have to use 'normal' iterators here
8440 quad = triangulation.begin_quad(),
8441 endq = triangulation.end_quad();
8443 next_unused_line = triangulation.begin_raw_line();
8445 next_unused_quad = triangulation.begin_raw_quad();
8446
8447 for (; quad != endq; ++quad)
8448 {
8449 if (quad->user_index())
8450 {
8451 RefinementCase<dim - 1> aniso_quad_ref_case =
8452 face_refinement_cases[quad->user_index()];
8453 // there is one unlikely event here, where we
8454 // already have refind the face: if the face was
8455 // refined anisotropically and we want to refine
8456 // it isotropically, both children are flagged for
8457 // anisotropic refinement. however, if those
8458 // children were already flagged for anisotropic
8459 // refinement, they might already be processed and
8460 // refined.
8461 if (aniso_quad_ref_case == quad->refinement_case())
8462 continue;
8463
8464 Assert(quad->refinement_case() ==
8466 quad->refinement_case() ==
8469
8470 // this quad needs to be refined anisotropically
8471 Assert(quad->user_index() ==
8473 quad->user_index() ==
8476
8477 // make the new line interior to the quad
8479 new_line;
8480
8481 new_line =
8482 triangulation.faces->lines
8483 .template next_free_single_object<1>(triangulation);
8484 AssertIsNotUsed(new_line);
8485
8486 // first collect the
8487 // indices of the vertices:
8488 // *--1--*
8489 // | | |
8490 // | | | cut_x
8491 // | | |
8492 // *--0--*
8493 //
8494 // *-----*
8495 // | |
8496 // 0-----1 cut_y
8497 // | |
8498 // *-----*
8499 unsigned int vertex_indices[2];
8500 if (aniso_quad_ref_case == RefinementCase<dim - 1>::cut_x)
8501 {
8502 vertex_indices[0] =
8503 quad->line(2)->child(0)->vertex_index(1);
8504 vertex_indices[1] =
8505 quad->line(3)->child(0)->vertex_index(1);
8506 }
8507 else
8508 {
8509 vertex_indices[0] =
8510 quad->line(0)->child(0)->vertex_index(1);
8511 vertex_indices[1] =
8512 quad->line(1)->child(0)->vertex_index(1);
8513 }
8514
8515 new_line->set_bounding_object_indices(
8517 new_line->set_used_flag();
8518 new_line->clear_user_flag();
8519 new_line->clear_user_data();
8520 new_line->clear_children();
8521 new_line->set_boundary_id_internal(quad->boundary_id());
8522 new_line->set_manifold_id(quad->manifold_id());
8523
8524 // find some space (consecutive) for the two newly
8525 // to be created quads.
8527 new_quads[2];
8528
8529 next_unused_quad =
8530 triangulation.faces->quads
8531 .template next_free_pair_object<2>(triangulation);
8532 new_quads[0] = next_unused_quad;
8533 AssertIsNotUsed(new_quads[0]);
8534
8535 ++next_unused_quad;
8536 new_quads[1] = next_unused_quad;
8537 AssertIsNotUsed(new_quads[1]);
8538
8539 if (aniso_quad_ref_case == RefinementCase<dim - 1>::cut_x)
8540 {
8541 new_quads[0]->set_bounding_object_indices(
8542 {static_cast<int>(quad->line_index(0)),
8543 new_line->index(),
8544 quad->line(2)
8545 ->child(
8546 child_line_index(0, quad->line_orientation(2)))
8547 ->index(),
8548 quad->line(3)
8549 ->child(
8550 child_line_index(0, quad->line_orientation(3)))
8551 ->index()});
8552 new_quads[1]->set_bounding_object_indices(
8553 {new_line->index(),
8554 static_cast<int>(quad->line_index(1)),
8555 quad->line(2)
8556 ->child(
8557 child_line_index(1, quad->line_orientation(2)))
8558 ->index(),
8559 quad->line(3)
8560 ->child(
8561 child_line_index(1, quad->line_orientation(3)))
8562 ->index()});
8563 }
8564 else
8565 {
8566 new_quads[0]->set_bounding_object_indices(
8567 {quad->line(0)
8568 ->child(
8569 child_line_index(0, quad->line_orientation(0)))
8570 ->index(),
8571 quad->line(1)
8572 ->child(
8573 child_line_index(0, quad->line_orientation(1)))
8574 ->index(),
8575 static_cast<int>(quad->line_index(2)),
8576 new_line->index()});
8577 new_quads[1]->set_bounding_object_indices(
8578 {quad->line(0)
8579 ->child(
8580 child_line_index(1, quad->line_orientation(0)))
8581 ->index(),
8582 quad->line(1)
8583 ->child(
8584 child_line_index(1, quad->line_orientation(1)))
8585 ->index(),
8586 new_line->index(),
8587 static_cast<int>(quad->line_index(3))});
8588 }
8589
8590 for (const auto &new_quad : new_quads)
8591 {
8592 new_quad->set_used_flag();
8593 new_quad->clear_user_flag();
8594 new_quad->clear_user_data();
8595 new_quad->clear_children();
8596 new_quad->set_boundary_id_internal(quad->boundary_id());
8597 new_quad->set_manifold_id(quad->manifold_id());
8598 // set all line orientations to true, change
8599 // this after the loop, as we have to consider
8600 // different lines for each child
8601 for (unsigned int j = 0;
8602 j < GeometryInfo<dim>::lines_per_face;
8603 ++j)
8604 new_quad->set_line_orientation(
8606 }
8607 // now set the line orientation of children of
8608 // outer lines correctly, the lines in the
8609 // interior of the refined quad are automatically
8610 // oriented conforming to the standard
8611 new_quads[0]->set_line_orientation(
8612 0, quad->line_orientation(0));
8613 new_quads[0]->set_line_orientation(
8614 2, quad->line_orientation(2));
8615 new_quads[1]->set_line_orientation(
8616 1, quad->line_orientation(1));
8617 new_quads[1]->set_line_orientation(
8618 3, quad->line_orientation(3));
8619 if (aniso_quad_ref_case == RefinementCase<dim - 1>::cut_x)
8620 {
8621 new_quads[0]->set_line_orientation(
8622 3, quad->line_orientation(3));
8623 new_quads[1]->set_line_orientation(
8624 2, quad->line_orientation(2));
8625 }
8626 else
8627 {
8628 new_quads[0]->set_line_orientation(
8629 1, quad->line_orientation(1));
8630 new_quads[1]->set_line_orientation(
8631 0, quad->line_orientation(0));
8632 }
8633
8634 // test, whether this face is refined
8635 // isotropically already. if so, set the correct
8636 // children pointers.
8637 if (quad->refinement_case() ==
8638 RefinementCase<dim - 1>::cut_xy)
8639 {
8640 // we will put a new refinemnt level of
8641 // anisotropic refinement between the
8642 // unrefined and isotropically refined quad
8643 // ending up with the same fine quads but
8644 // introducing anisotropically refined ones as
8645 // children of the unrefined quad and mother
8646 // cells of the original fine ones.
8647
8648 // this process includes the creation of a new
8649 // middle line which we will assign as the
8650 // mother line of two of the existing inner
8651 // lines. If those inner lines are not
8652 // consecutive in memory, we won't find them
8653 // later on, so we have to create new ones
8654 // instead and replace all occurrences of the
8655 // old ones with those new ones. As this is
8656 // kind of ugly, we hope we don't have to do
8657 // it often...
8659 old_child[2];
8660 if (aniso_quad_ref_case ==
8662 {
8663 old_child[0] = quad->child(0)->line(1);
8664 old_child[1] = quad->child(2)->line(1);
8665 }
8666 else
8667 {
8668 Assert(aniso_quad_ref_case ==
8671
8672 old_child[0] = quad->child(0)->line(3);
8673 old_child[1] = quad->child(1)->line(3);
8674 }
8675
8676 if (old_child[0]->index() + 1 != old_child[1]->index())
8677 {
8678 // this is exactly the ugly case we talked
8679 // about. so, no complaining, lets get
8680 // two new lines and copy all info
8681 typename Triangulation<dim,
8682 spacedim>::raw_line_iterator
8683 new_child[2];
8684
8685 new_child[0] = new_child[1] =
8686 triangulation.faces->lines
8687 .template next_free_pair_object<1>(
8688 triangulation);
8689 ++new_child[1];
8690
8691 new_child[0]->set_used_flag();
8692 new_child[1]->set_used_flag();
8693
8694 const int old_index_0 = old_child[0]->index(),
8695 old_index_1 = old_child[1]->index(),
8696 new_index_0 = new_child[0]->index(),
8697 new_index_1 = new_child[1]->index();
8698
8699 // loop over all quads and replace the old
8700 // lines
8701 for (unsigned int q = 0;
8702 q < triangulation.faces->quads.n_objects();
8703 ++q)
8704 for (unsigned int l = 0;
8705 l < GeometryInfo<dim>::lines_per_face;
8706 ++l)
8707 {
8708 const int this_index =
8709 triangulation.faces->quads
8710 .get_bounding_object_indices(q)[l];
8711 if (this_index == old_index_0)
8712 triangulation.faces->quads
8713 .get_bounding_object_indices(q)[l] =
8714 new_index_0;
8715 else if (this_index == old_index_1)
8716 triangulation.faces->quads
8717 .get_bounding_object_indices(q)[l] =
8718 new_index_1;
8719 }
8720 // now we have to copy all information of
8721 // the two lines
8722 for (unsigned int i = 0; i < 2; ++i)
8723 {
8724 Assert(!old_child[i]->has_children(),
8726
8727 new_child[i]->set_bounding_object_indices(
8728 {old_child[i]->vertex_index(0),
8729 old_child[i]->vertex_index(1)});
8730 new_child[i]->set_boundary_id_internal(
8731 old_child[i]->boundary_id());
8732 new_child[i]->set_manifold_id(
8733 old_child[i]->manifold_id());
8734 new_child[i]->set_user_index(
8735 old_child[i]->user_index());
8736 if (old_child[i]->user_flag_set())
8737 new_child[i]->set_user_flag();
8738 else
8739 new_child[i]->clear_user_flag();
8740
8741 new_child[i]->clear_children();
8742
8743 old_child[i]->clear_user_flag();
8744 old_child[i]->clear_user_index();
8745 old_child[i]->clear_used_flag();
8746 }
8747 }
8748 // now that we cared about the lines, go on
8749 // with the quads themselves, where we might
8750 // encounter similar situations...
8751 if (aniso_quad_ref_case ==
8753 {
8754 new_line->set_children(
8755 0, quad->child(0)->line_index(1));
8756 Assert(new_line->child(1) ==
8757 quad->child(2)->line(1),
8759 // now evereything is quite
8760 // complicated. we have the children
8761 // numbered according to
8762 //
8763 // *---*---*
8764 // |n+2|n+3|
8765 // *---*---*
8766 // | n |n+1|
8767 // *---*---*
8768 //
8769 // from the original isotropic
8770 // refinement. we have to reorder them as
8771 //
8772 // *---*---*
8773 // |n+1|n+3|
8774 // *---*---*
8775 // | n |n+2|
8776 // *---*---*
8777 //
8778 // such that n and n+1 are consecutive
8779 // children of m and n+2 and n+3 are
8780 // consecutive children of m+1, where m
8781 // and m+1 are given as in
8782 //
8783 // *---*---*
8784 // | | |
8785 // | m |m+1|
8786 // | | |
8787 // *---*---*
8788 //
8789 // this is a bit ugly, of course: loop
8790 // over all cells on all levels and look
8791 // for faces n+1 (switch_1) and n+2
8792 // (switch_2).
8793 const typename Triangulation<dim, spacedim>::
8794 quad_iterator switch_1 = quad->child(1),
8795 switch_2 = quad->child(2);
8796 const int switch_1_index = switch_1->index();
8797 const int switch_2_index = switch_2->index();
8798 for (unsigned int l = 0;
8799 l < triangulation.levels.size();
8800 ++l)
8801 for (unsigned int h = 0;
8802 h <
8803 triangulation.levels[l]->cells.n_objects();
8804 ++h)
8805 for (const unsigned int q :
8807 {
8808 const int face_index =
8809 triangulation.levels[l]
8810 ->cells.get_bounding_object_indices(
8811 h)[q];
8812 if (face_index == switch_1_index)
8813 triangulation.levels[l]
8814 ->cells.get_bounding_object_indices(
8815 h)[q] = switch_2_index;
8816 else if (face_index == switch_2_index)
8817 triangulation.levels[l]
8818 ->cells.get_bounding_object_indices(
8819 h)[q] = switch_1_index;
8820 }
8821 // now we have to copy all information of
8822 // the two quads
8823 int switch_1_lines[4], switch_2_lines[4];
8824 for (int i = 0; i < 4; ++i)
8825 {
8826 switch_1_lines[i] = switch_1->line(i)->index();
8827 switch_2_lines[i] = switch_2->line(i)->index();
8828 }
8830 switch_1_line_orientations[4] = {
8831 switch_1->line_orientation(0),
8832 switch_1->line_orientation(1),
8833 switch_1->line_orientation(2),
8834 switch_1->line_orientation(3)};
8835 const types::boundary_id switch_1_boundary_id =
8836 switch_1->boundary_id();
8837 const unsigned int switch_1_user_index =
8838 switch_1->user_index();
8839 const bool switch_1_user_flag =
8840 switch_1->user_flag_set();
8841 const RefinementCase<dim - 1>
8842 switch_1_refinement_case =
8843 switch_1->refinement_case();
8844 const int switch_1_first_child_pair =
8845 (switch_1_refinement_case ?
8846 switch_1->child_index(0) :
8847 -1);
8848 const int switch_1_second_child_pair =
8849 (switch_1_refinement_case ==
8850 RefinementCase<dim - 1>::cut_xy ?
8851 switch_1->child_index(2) :
8852 -1);
8853
8854 switch_1->set_bounding_object_indices(
8855 {switch_2_lines[0],
8856 switch_2_lines[1],
8857 switch_2_lines[2],
8858 switch_2_lines[3]});
8859 switch_1->set_line_orientation(
8860 0, switch_2->line_orientation(0));
8861 switch_1->set_line_orientation(
8862 1, switch_2->line_orientation(1));
8863 switch_1->set_line_orientation(
8864 2, switch_2->line_orientation(2));
8865 switch_1->set_line_orientation(
8866 3, switch_2->line_orientation(3));
8867 switch_1->set_boundary_id_internal(
8868 switch_2->boundary_id());
8869 switch_1->set_manifold_id(switch_2->manifold_id());
8870 switch_1->set_user_index(switch_2->user_index());
8871 if (switch_2->user_flag_set())
8872 switch_1->set_user_flag();
8873 else
8874 switch_1->clear_user_flag();
8875 switch_1->clear_refinement_case();
8876 switch_1->set_refinement_case(
8877 switch_2->refinement_case());
8878 switch_1->clear_children();
8879 if (switch_2->refinement_case())
8880 switch_1->set_children(0,
8881 switch_2->child_index(0));
8882 if (switch_2->refinement_case() ==
8883 RefinementCase<dim - 1>::cut_xy)
8884 switch_1->set_children(2,
8885 switch_2->child_index(2));
8886
8887 switch_2->set_bounding_object_indices(
8888 {switch_1_lines[0],
8889 switch_1_lines[1],
8890 switch_1_lines[2],
8891 switch_1_lines[3]});
8892 switch_2->set_line_orientation(
8893 0, switch_1_line_orientations[0]);
8894 switch_2->set_line_orientation(
8895 1, switch_1_line_orientations[1]);
8896 switch_2->set_line_orientation(
8897 2, switch_1_line_orientations[2]);
8898 switch_2->set_line_orientation(
8899 3, switch_1_line_orientations[3]);
8900 switch_2->set_boundary_id_internal(
8901 switch_1_boundary_id);
8902 switch_2->set_manifold_id(switch_1->manifold_id());
8903 switch_2->set_user_index(switch_1_user_index);
8904 if (switch_1_user_flag)
8905 switch_2->set_user_flag();
8906 else
8907 switch_2->clear_user_flag();
8908 switch_2->clear_refinement_case();
8909 switch_2->set_refinement_case(
8910 switch_1_refinement_case);
8911 switch_2->clear_children();
8912 switch_2->set_children(0,
8913 switch_1_first_child_pair);
8914 switch_2->set_children(2,
8915 switch_1_second_child_pair);
8916
8917 new_quads[0]->set_refinement_case(
8919 new_quads[0]->set_children(0, quad->child_index(0));
8920 new_quads[1]->set_refinement_case(
8922 new_quads[1]->set_children(0, quad->child_index(2));
8923 }
8924 else
8925 {
8926 new_quads[0]->set_refinement_case(
8928 new_quads[0]->set_children(0, quad->child_index(0));
8929 new_quads[1]->set_refinement_case(
8931 new_quads[1]->set_children(0, quad->child_index(2));
8932 new_line->set_children(
8933 0, quad->child(0)->line_index(3));
8934 Assert(new_line->child(1) ==
8935 quad->child(1)->line(3),
8937 }
8938 quad->clear_children();
8939 }
8940
8941 // note these quads as children to the present one
8942 quad->set_children(0, new_quads[0]->index());
8943
8944 quad->set_refinement_case(aniso_quad_ref_case);
8945
8946 // finally clear flag indicating the need for
8947 // refinement
8948 quad->clear_user_data();
8949 } // if (anisotropic refinement)
8950
8951 if (quad->user_flag_set())
8952 {
8953 // this quad needs to be refined isotropically
8954
8955 // first of all: we only get here in the first run
8956 // of the loop
8957 Assert(loop == 0, ExcInternalError());
8958
8959 // find the next unused vertex. we'll need this in
8960 // any case
8961 while (triangulation.vertices_used[next_unused_vertex] ==
8962 true)
8963 ++next_unused_vertex;
8964 Assert(
8965 next_unused_vertex < triangulation.vertices.size(),
8966 ExcMessage(
8967 "Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
8968
8969 // now: if the quad is refined anisotropically
8970 // already, set the anisotropic refinement flag
8971 // for both children. Additionally, we have to
8972 // refine the inner line, as it is an outer line
8973 // of the two (anisotropic) children
8974 const RefinementCase<dim - 1> quad_ref_case =
8975 quad->refinement_case();
8976
8977 if (quad_ref_case == RefinementCase<dim - 1>::cut_x ||
8978 quad_ref_case == RefinementCase<dim - 1>::cut_y)
8979 {
8980 // set the 'opposite' refine case for children
8981 quad->child(0)->set_user_index(
8982 RefinementCase<dim - 1>::cut_xy - quad_ref_case);
8983 quad->child(1)->set_user_index(
8984 RefinementCase<dim - 1>::cut_xy - quad_ref_case);
8985 // refine the inner line
8987 middle_line;
8988 if (quad_ref_case == RefinementCase<dim - 1>::cut_x)
8989 middle_line = quad->child(0)->line(1);
8990 else
8991 middle_line = quad->child(0)->line(3);
8992
8993 // if the face has been refined
8994 // anisotropically in the last refinement step
8995 // it might be, that it is flagged already and
8996 // that the middle line is thus refined
8997 // already. if not create children.
8998 if (!middle_line->has_children())
8999 {
9000 // set the middle vertex
9001 // appropriately. double refinement of
9002 // quads can only happen in the interior
9003 // of the domain, so we need not care
9004 // about boundary quads here
9005 triangulation.vertices[next_unused_vertex] =
9006 middle_line->center(true);
9007 triangulation.vertices_used[next_unused_vertex] =
9008 true;
9009
9010 // now search a slot for the two
9011 // child lines
9012 next_unused_line =
9013 triangulation.faces->lines
9014 .template next_free_pair_object<1>(
9015 triangulation);
9016
9017 // set the child pointer of the present
9018 // line
9019 middle_line->set_children(
9020 0, next_unused_line->index());
9021
9022 // set the two new lines
9023 const typename Triangulation<dim, spacedim>::
9024 raw_line_iterator children[2] = {
9025 next_unused_line, ++next_unused_line};
9026
9027 // some tests; if any of the iterators
9028 // should be invalid, then already
9029 // dereferencing will fail
9030 AssertIsNotUsed(children[0]);
9031 AssertIsNotUsed(children[1]);
9032
9033 children[0]->set_bounding_object_indices(
9034 {middle_line->vertex_index(0),
9035 next_unused_vertex});
9036 children[1]->set_bounding_object_indices(
9037 {next_unused_vertex,
9038 middle_line->vertex_index(1)});
9039
9040 children[0]->set_used_flag();
9041 children[1]->set_used_flag();
9042 children[0]->clear_children();
9043 children[1]->clear_children();
9044 children[0]->clear_user_data();
9045 children[1]->clear_user_data();
9046 children[0]->clear_user_flag();
9047 children[1]->clear_user_flag();
9048
9049 children[0]->set_boundary_id_internal(
9050 middle_line->boundary_id());
9051 children[1]->set_boundary_id_internal(
9052 middle_line->boundary_id());
9053
9054 children[0]->set_manifold_id(
9055 middle_line->manifold_id());
9056 children[1]->set_manifold_id(
9057 middle_line->manifold_id());
9058 }
9059 // now remove the flag from the quad and go to
9060 // the next quad, the actual refinement of the
9061 // quad takes place later on in this pass of
9062 // the loop or in the next one
9063 quad->clear_user_flag();
9064 continue;
9065 } // if (several refinement cases)
9066
9067 // if we got here, we have an unrefined quad and
9068 // have to do the usual work like in an purely
9069 // isotropic refinement
9070 Assert(quad_ref_case ==
9073
9074 // set the middle vertex appropriately: it might be that
9075 // the quad itself is not at the boundary, but that one of
9076 // its lines actually is. in this case, the newly created
9077 // vertices at the centers of the lines are not
9078 // necessarily the mean values of the adjacent vertices,
9079 // so do not compute the new vertex as the mean value of
9080 // the 4 vertices of the face, but rather as a weighted
9081 // mean value of the 8 vertices which we already have (the
9082 // four old ones, and the four ones inserted as middle
9083 // points for the four lines). summing up some more points
9084 // is generally cheaper than first asking whether one of
9085 // the lines is at the boundary
9086 //
9087 // note that the exact weights are chosen such as to
9088 // minimize the distortion of the four new quads from the
9089 // optimal shape. their description uses the formulas
9090 // underlying the TransfiniteInterpolationManifold
9091 // implementation
9092 triangulation.vertices[next_unused_vertex] =
9093 quad->center(true, true);
9094 triangulation.vertices_used[next_unused_vertex] = true;
9095
9096 // now that we created the right point, make up
9097 // the four lines interior to the quad (++ takes
9098 // care of the end of the vector)
9100 new_lines[4];
9101
9102 for (unsigned int i = 0; i < 4; ++i)
9103 {
9104 if (i % 2 == 0)
9105 // search a free pair of lines for 0. and
9106 // 2. line, so that two of them end up
9107 // together, which is necessary if later on
9108 // we want to refine the quad
9109 // anisotropically and the two lines end up
9110 // as children of new line
9111 next_unused_line =
9112 triangulation.faces->lines
9113 .template next_free_pair_object<1>(triangulation);
9114
9115 new_lines[i] = next_unused_line;
9116 ++next_unused_line;
9117
9118 AssertIsNotUsed(new_lines[i]);
9119 }
9120
9121 // set the data of the four lines. first collect
9122 // the indices of the five vertices:
9123 //
9124 // *--3--*
9125 // | | |
9126 // 0--4--1
9127 // | | |
9128 // *--2--*
9129 //
9130 // the lines are numbered as follows:
9131 //
9132 // *--*--*
9133 // | 1 |
9134 // *2-*-3*
9135 // | 0 |
9136 // *--*--*
9137
9138 const unsigned int vertex_indices[5] = {
9139 quad->line(0)->child(0)->vertex_index(1),
9140 quad->line(1)->child(0)->vertex_index(1),
9141 quad->line(2)->child(0)->vertex_index(1),
9142 quad->line(3)->child(0)->vertex_index(1),
9143 next_unused_vertex};
9144
9145 new_lines[0]->set_bounding_object_indices(
9147 new_lines[1]->set_bounding_object_indices(
9149 new_lines[2]->set_bounding_object_indices(
9151 new_lines[3]->set_bounding_object_indices(
9153
9154 for (const auto &new_line : new_lines)
9155 {
9156 new_line->set_used_flag();
9157 new_line->clear_user_flag();
9158 new_line->clear_user_data();
9159 new_line->clear_children();
9160 new_line->set_boundary_id_internal(quad->boundary_id());
9161 new_line->set_manifold_id(quad->manifold_id());
9162 }
9163
9164 // now for the quads. again, first collect some
9165 // data about the indices of the lines, with the
9166 // following numbering:
9167 //
9168 // .-6-.-7-.
9169 // 1 9 3
9170 // .-10.11-.
9171 // 0 8 2
9172 // .-4-.-5-.
9173
9174 const int line_indices[12] = {
9175 quad->line(0)
9176 ->child(child_line_index(0, quad->line_orientation(0)))
9177 ->index(),
9178 quad->line(0)
9179 ->child(child_line_index(1, quad->line_orientation(0)))
9180 ->index(),
9181 quad->line(1)
9182 ->child(child_line_index(0, quad->line_orientation(1)))
9183 ->index(),
9184 quad->line(1)
9185 ->child(child_line_index(1, quad->line_orientation(1)))
9186 ->index(),
9187 quad->line(2)
9188 ->child(child_line_index(0, quad->line_orientation(2)))
9189 ->index(),
9190 quad->line(2)
9191 ->child(child_line_index(1, quad->line_orientation(2)))
9192 ->index(),
9193 quad->line(3)
9194 ->child(child_line_index(0, quad->line_orientation(3)))
9195 ->index(),
9196 quad->line(3)
9197 ->child(child_line_index(1, quad->line_orientation(3)))
9198 ->index(),
9199 new_lines[0]->index(),
9200 new_lines[1]->index(),
9201 new_lines[2]->index(),
9202 new_lines[3]->index()};
9203
9204 // find some space (consecutive)
9205 // for the first two newly to be
9206 // created quads.
9208 new_quads[4];
9209
9210 next_unused_quad =
9211 triangulation.faces->quads
9212 .template next_free_pair_object<2>(triangulation);
9213
9214 new_quads[0] = next_unused_quad;
9215 AssertIsNotUsed(new_quads[0]);
9216
9217 ++next_unused_quad;
9218 new_quads[1] = next_unused_quad;
9219 AssertIsNotUsed(new_quads[1]);
9220
9221 next_unused_quad =
9222 triangulation.faces->quads
9223 .template next_free_pair_object<2>(triangulation);
9224 new_quads[2] = next_unused_quad;
9225 AssertIsNotUsed(new_quads[2]);
9226
9227 ++next_unused_quad;
9228 new_quads[3] = next_unused_quad;
9229 AssertIsNotUsed(new_quads[3]);
9230
9231 // note these quads as children to the present one
9232 quad->set_children(0, new_quads[0]->index());
9233 quad->set_children(2, new_quads[2]->index());
9234 quad->set_refinement_case(RefinementCase<2>::cut_xy);
9235
9236 new_quads[0]->set_bounding_object_indices(
9237 {line_indices[0],
9238 line_indices[8],
9239 line_indices[4],
9240 line_indices[10]});
9241 new_quads[1]->set_bounding_object_indices(
9242 {line_indices[8],
9243 line_indices[2],
9244 line_indices[5],
9245 line_indices[11]});
9246 new_quads[2]->set_bounding_object_indices(
9247 {line_indices[1],
9248 line_indices[9],
9249 line_indices[10],
9250 line_indices[6]});
9251 new_quads[3]->set_bounding_object_indices(
9252 {line_indices[9],
9253 line_indices[3],
9254 line_indices[11],
9255 line_indices[7]});
9256 for (const auto &new_quad : new_quads)
9257 {
9258 new_quad->set_used_flag();
9259 new_quad->clear_user_flag();
9260 new_quad->clear_user_data();
9261 new_quad->clear_children();
9262 new_quad->set_boundary_id_internal(quad->boundary_id());
9263 new_quad->set_manifold_id(quad->manifold_id());
9264 // set all line orientations to true, change
9265 // this after the loop, as we have to consider
9266 // different lines for each child
9267 for (unsigned int j = 0;
9268 j < GeometryInfo<dim>::lines_per_face;
9269 ++j)
9270 new_quad->set_line_orientation(
9272 }
9273 // now set the line orientation of children of
9274 // outer lines correctly, the lines in the
9275 // interior of the refined quad are automatically
9276 // oriented conforming to the standard
9277 new_quads[0]->set_line_orientation(
9278 0, quad->line_orientation(0));
9279 new_quads[0]->set_line_orientation(
9280 2, quad->line_orientation(2));
9281 new_quads[1]->set_line_orientation(
9282 1, quad->line_orientation(1));
9283 new_quads[1]->set_line_orientation(
9284 2, quad->line_orientation(2));
9285 new_quads[2]->set_line_orientation(
9286 0, quad->line_orientation(0));
9287 new_quads[2]->set_line_orientation(
9288 3, quad->line_orientation(3));
9289 new_quads[3]->set_line_orientation(
9290 1, quad->line_orientation(1));
9291 new_quads[3]->set_line_orientation(
9292 3, quad->line_orientation(3));
9293
9294 // finally clear flag indicating the need for
9295 // refinement
9296 quad->clear_user_flag();
9297 } // if (isotropic refinement)
9298 } // for all quads
9299 } // looped two times over all quads, all quads refined now
9300
9301 //---------------------------------
9302 // Now, finally, set up the new
9303 // cells
9304 //---------------------------------
9305
9307 cells_with_distorted_children;
9308
9309 for (unsigned int level = 0; level != triangulation.levels.size() - 1;
9310 ++level)
9311 {
9312 // only active objects can be refined further; remember
9313 // that we won't operate on the finest level, so
9314 // triangulation.begin_*(level+1) is allowed
9316 hex = triangulation.begin_active_hex(level),
9317 endh = triangulation.begin_active_hex(level + 1);
9319 next_unused_hex = triangulation.begin_raw_hex(level + 1);
9320
9321 for (; hex != endh; ++hex)
9322 if (hex->refine_flag_set())
9323 {
9324 // this hex needs to be refined
9325
9326 // clear flag indicating the need for refinement. do
9327 // it here already, since we can't do it anymore
9328 // once the cell has children
9329 const RefinementCase<dim> ref_case = hex->refine_flag_set();
9330 hex->clear_refine_flag();
9331 hex->set_refinement_case(ref_case);
9332
9333 // depending on the refine case we might have to
9334 // create additional vertices, lines and quads
9335 // interior of the hex before the actual children
9336 // can be set up.
9337
9338 // in a first step: reserve the needed space for
9339 // lines, quads and hexes and initialize them
9340 // correctly
9341
9342 unsigned int n_new_lines = 0;
9343 unsigned int n_new_quads = 0;
9344 unsigned int n_new_hexes = 0;
9345 switch (ref_case)
9346 {
9350 n_new_lines = 0;
9351 n_new_quads = 1;
9352 n_new_hexes = 2;
9353 break;
9357 n_new_lines = 1;
9358 n_new_quads = 4;
9359 n_new_hexes = 4;
9360 break;
9362 n_new_lines = 6;
9363 n_new_quads = 12;
9364 n_new_hexes = 8;
9365 break;
9366 default:
9368 break;
9369 }
9370
9371 // find some space for the newly to be created
9372 // interior lines and initialize them.
9373 std::vector<
9375 new_lines(n_new_lines);
9376 for (unsigned int i = 0; i < n_new_lines; ++i)
9377 {
9378 new_lines[i] =
9379 triangulation.faces->lines
9380 .template next_free_single_object<1>(triangulation);
9381
9382 AssertIsNotUsed(new_lines[i]);
9383 new_lines[i]->set_used_flag();
9384 new_lines[i]->clear_user_flag();
9385 new_lines[i]->clear_user_data();
9386 new_lines[i]->clear_children();
9387 // interior line
9388 new_lines[i]->set_boundary_id_internal(
9390 // they inherit geometry description of the hex they
9391 // belong to
9392 new_lines[i]->set_manifold_id(hex->manifold_id());
9393 }
9394
9395 // find some space for the newly to be created
9396 // interior quads and initialize them.
9397 std::vector<
9399 new_quads(n_new_quads);
9400 for (unsigned int i = 0; i < n_new_quads; ++i)
9401 {
9402 new_quads[i] =
9403 triangulation.faces->quads
9404 .template next_free_single_object<2>(triangulation);
9405
9406 AssertIsNotUsed(new_quads[i]);
9407 new_quads[i]->set_used_flag();
9408 new_quads[i]->clear_user_flag();
9409 new_quads[i]->clear_user_data();
9410 new_quads[i]->clear_children();
9411 // interior quad
9412 new_quads[i]->set_boundary_id_internal(
9414 // they inherit geometry description of the hex they
9415 // belong to
9416 new_quads[i]->set_manifold_id(hex->manifold_id());
9417 // set all line orientation flags to true by
9418 // default, change this afterwards, if necessary
9419 for (unsigned int j = 0;
9420 j < GeometryInfo<dim>::lines_per_face;
9421 ++j)
9422 new_quads[i]->set_line_orientation(
9424 }
9425
9426 types::subdomain_id subdomainid = hex->subdomain_id();
9427
9428 // find some space for the newly to be created hexes
9429 // and initialize them.
9430 std::vector<
9432 new_hexes(n_new_hexes);
9433 for (unsigned int i = 0; i < n_new_hexes; ++i)
9434 {
9435 if (i % 2 == 0)
9436 next_unused_hex =
9437 triangulation.levels[level + 1]->cells.next_free_hex(
9438 triangulation, level + 1);
9439 else
9440 ++next_unused_hex;
9441
9442 new_hexes[i] = next_unused_hex;
9443
9444 AssertIsNotUsed(new_hexes[i]);
9445 new_hexes[i]->set_used_flag();
9446 new_hexes[i]->clear_user_flag();
9447 new_hexes[i]->clear_user_data();
9448 new_hexes[i]->clear_children();
9449 // inherit material
9450 // properties
9451 new_hexes[i]->set_material_id(hex->material_id());
9452 new_hexes[i]->set_manifold_id(hex->manifold_id());
9453 new_hexes[i]->set_subdomain_id(subdomainid);
9454
9455 // We only store the parent for every second cell. That's
9456 // because cells are created during refinement in
9457 // multiples of two, and so two successive cells always
9458 // share the same parent.
9459 if (i % 2 == 0)
9460 new_hexes[i]->set_parent(hex->index());
9461
9462 // set the face_orientation flag to true for all
9463 // faces initially, as this is the default value
9464 // which is true for all faces interior to the
9465 // hex. later on go the other way round and
9466 // reset faces that are at the boundary of the
9467 // mother cube
9468 //
9469 // the same is true for the face_flip and
9470 // face_rotation flags. however, the latter two
9471 // are set to false by default as this is the
9472 // standard value
9473 for (const unsigned int f :
9475 new_hexes[i]->set_combined_face_orientation(
9477 }
9478 // note these hexes as children to the present cell
9479 for (unsigned int i = 0; i < n_new_hexes / 2; ++i)
9480 hex->set_children(2 * i, new_hexes[2 * i]->index());
9481
9482 // we have to take into account whether the
9483 // different faces are oriented correctly or in the
9484 // opposite direction, so store that up front
9485
9486 // face_orientation
9487 const bool f_or[6] = {hex->face_orientation(0),
9488 hex->face_orientation(1),
9489 hex->face_orientation(2),
9490 hex->face_orientation(3),
9491 hex->face_orientation(4),
9492 hex->face_orientation(5)};
9493
9494 // face_flip
9495 const bool f_fl[6] = {hex->face_flip(0),
9496 hex->face_flip(1),
9497 hex->face_flip(2),
9498 hex->face_flip(3),
9499 hex->face_flip(4),
9500 hex->face_flip(5)};
9501
9502 // face_rotation
9503 const bool f_ro[6] = {hex->face_rotation(0),
9504 hex->face_rotation(1),
9505 hex->face_rotation(2),
9506 hex->face_rotation(3),
9507 hex->face_rotation(4),
9508 hex->face_rotation(5)};
9509
9510 // combined orientation
9511 const types::geometric_orientation f_co[6] = {
9512 hex->combined_face_orientation(0),
9513 hex->combined_face_orientation(1),
9514 hex->combined_face_orientation(2),
9515 hex->combined_face_orientation(3),
9516 hex->combined_face_orientation(4),
9517 hex->combined_face_orientation(5)};
9518
9519 // little helper table, indicating, whether the
9520 // child with index 0 or with index 1 can be found
9521 // at the standard origin of an anisotropically
9522 // refined quads in real orientation index 1:
9523 // (RefineCase - 1) index 2: face_flip
9524
9525 // index 3: face rotation
9526 // note: face orientation has no influence
9527 const unsigned int child_at_origin[2][2][2] = {
9528 {{0, 0}, // RefinementCase<dim>::cut_x, face_flip=false,
9529 // face_rotation=false and true
9530 {1, 1}}, // RefinementCase<dim>::cut_x, face_flip=true,
9531 // face_rotation=false and true
9532 {{0, 1}, // RefinementCase<dim>::cut_y, face_flip=false,
9533 // face_rotation=false and true
9534 {1, 0}}}; // RefinementCase<dim>::cut_y, face_flip=true,
9535 // face_rotation=false and true
9536
9537 //-------------------------------------
9538 //
9539 // in the following we will do the same thing for
9540 // each refinement case: create a new vertex (if
9541 // needed), create new interior lines (if needed),
9542 // create new interior quads and afterwards build
9543 // the children hexes out of these and the existing
9544 // subfaces of the outer quads (which have been
9545 // created above). However, even if the steps are
9546 // quite similar, the actual work strongly depends
9547 // on the actual refinement case. therefore, we use
9548 // separate blocks of code for each of these cases,
9549 // which hopefully increases the readability to some
9550 // extend.
9551
9552 switch (ref_case)
9553 {
9555 {
9556 //----------------------------
9557 //
9558 // RefinementCase<dim>::cut_x
9559 //
9560 // the refined cube will look
9561 // like this:
9562 //
9563 // *----*----*
9564 // / / /|
9565 // / / / |
9566 // / / / |
9567 // *----*----* |
9568 // | | | |
9569 // | | | *
9570 // | | | /
9571 // | | | /
9572 // | | |/
9573 // *----*----*
9574 //
9575 // again, first collect some data about the
9576 // indices of the lines, with the following
9577 // numbering:
9578
9579 // face 2: front plane
9580 // (note: x,y exchanged)
9581 // *---*---*
9582 // | | |
9583 // | 0 |
9584 // | | |
9585 // *---*---*
9586 // m0
9587 // face 3: back plane
9588 // (note: x,y exchanged)
9589 // m1
9590 // *---*---*
9591 // | | |
9592 // | 1 |
9593 // | | |
9594 // *---*---*
9595 // face 4: bottom plane
9596 // *---*---*
9597 // / / /
9598 // / 2 /
9599 // / / /
9600 // *---*---*
9601 // m0
9602 // face 5: top plane
9603 // m1
9604 // *---*---*
9605 // / / /
9606 // / 3 /
9607 // / / /
9608 // *---*---*
9609
9610 // set up a list of line iterators first. from
9611 // this, construct lists of line_indices and
9612 // line orientations later on
9613 const typename Triangulation<dim, spacedim>::
9614 raw_line_iterator lines[4] = {
9615 hex->face(2)->child(0)->line(
9616 (hex->face(2)->refinement_case() ==
9618 1 :
9619 3), // 0
9620 hex->face(3)->child(0)->line(
9621 (hex->face(3)->refinement_case() ==
9623 1 :
9624 3), // 1
9625 hex->face(4)->child(0)->line(
9626 (hex->face(4)->refinement_case() ==
9628 1 :
9629 3), // 2
9630 hex->face(5)->child(0)->line(
9631 (hex->face(5)->refinement_case() ==
9633 1 :
9634 3) // 3
9635 };
9636
9637 int line_indices[4];
9638 for (unsigned int i = 0; i < 4; ++i)
9639 line_indices[i] = lines[i]->index();
9640
9641 // the orientation of lines for the inner quads
9642 // is quite tricky. as these lines are newly
9643 // created ones and thus have no parents, they
9644 // cannot inherit this property. set up an array
9645 // and fill it with the respective values
9646 types::geometric_orientation line_orientation[4]{};
9647
9648 // the middle vertex marked as m0 above is the
9649 // start vertex for lines 0 and 2 in standard
9650 // orientation, whereas m1 is the end vertex of
9651 // lines 1 and 3 in standard orientation
9652 const unsigned int middle_vertices[2] = {
9653 hex->line(2)->child(0)->vertex_index(1),
9654 hex->line(7)->child(0)->vertex_index(1)};
9655
9656 for (unsigned int i = 0; i < 4; ++i)
9657 if (lines[i]->vertex_index(i % 2) ==
9658 middle_vertices[i % 2])
9659 line_orientation[i] =
9661 else
9662 {
9663 // it must be the other way round then
9664 Assert(lines[i]->vertex_index((i + 1) % 2) ==
9665 middle_vertices[i % 2],
9667 line_orientation[i] =
9669 }
9670
9671 // set up the new quad, line numbering is as
9672 // indicated above
9673 new_quads[0]->set_bounding_object_indices(
9674 {line_indices[0],
9675 line_indices[1],
9676 line_indices[2],
9677 line_indices[3]});
9678
9679 new_quads[0]->set_line_orientation(
9680 0, line_orientation[0]);
9681 new_quads[0]->set_line_orientation(
9682 1, line_orientation[1]);
9683 new_quads[0]->set_line_orientation(
9684 2, line_orientation[2]);
9685 new_quads[0]->set_line_orientation(
9686 3, line_orientation[3]);
9687
9688 // the quads are numbered as follows:
9689 //
9690 // planes in the interior of the old hex:
9691 //
9692 // *
9693 // /|
9694 // / | x
9695 // / | *-------* *---------*
9696 // * | | | / /
9697 // | 0 | | | / /
9698 // | * | | / /
9699 // | / *-------*y *---------*x
9700 // | /
9701 // |/
9702 // *
9703 //
9704 // children of the faces of the old hex
9705 //
9706 // *---*---* *---*---*
9707 // /| | | / / /|
9708 // / | | | / 9 / 10/ |
9709 // / | 5 | 6 | / / / |
9710 // * | | | *---*---* |
9711 // | 1 *---*---* | | | 2 *
9712 // | / / / | | | /
9713 // | / 7 / 8 / | 3 | 4 | /
9714 // |/ / / | | |/
9715 // *---*---* *---*---*
9716 //
9717 // note that we have to take care of the
9718 // orientation of faces.
9719 const int quad_indices[11] = {
9720 new_quads[0]->index(), // 0
9721
9722 hex->face(0)->index(), // 1
9723
9724 hex->face(1)->index(), // 2
9725
9726 hex->face(2)->child_index(
9727 child_at_origin[hex->face(2)->refinement_case() -
9728 1][f_fl[2]][f_ro[2]]), // 3
9729 hex->face(2)->child_index(
9730 1 -
9731 child_at_origin[hex->face(2)->refinement_case() -
9732 1][f_fl[2]][f_ro[2]]),
9733
9734 hex->face(3)->child_index(
9735 child_at_origin[hex->face(3)->refinement_case() -
9736 1][f_fl[3]][f_ro[3]]), // 5
9737 hex->face(3)->child_index(
9738 1 -
9739 child_at_origin[hex->face(3)->refinement_case() -
9740 1][f_fl[3]][f_ro[3]]),
9741
9742 hex->face(4)->child_index(
9743 child_at_origin[hex->face(4)->refinement_case() -
9744 1][f_fl[4]][f_ro[4]]), // 7
9745 hex->face(4)->child_index(
9746 1 -
9747 child_at_origin[hex->face(4)->refinement_case() -
9748 1][f_fl[4]][f_ro[4]]),
9749
9750 hex->face(5)->child_index(
9751 child_at_origin[hex->face(5)->refinement_case() -
9752 1][f_fl[5]][f_ro[5]]), // 9
9753 hex->face(5)->child_index(
9754 1 -
9755 child_at_origin[hex->face(5)->refinement_case() -
9756 1][f_fl[5]][f_ro[5]])
9757
9758 };
9759
9760 new_hexes[0]->set_bounding_object_indices(
9761 {quad_indices[1],
9762 quad_indices[0],
9763 quad_indices[3],
9764 quad_indices[5],
9765 quad_indices[7],
9766 quad_indices[9]});
9767 new_hexes[1]->set_bounding_object_indices(
9768 {quad_indices[0],
9769 quad_indices[2],
9770 quad_indices[4],
9771 quad_indices[6],
9772 quad_indices[8],
9773 quad_indices[10]});
9774 break;
9775 }
9776
9778 {
9779 //----------------------------
9780 //
9781 // RefinementCase<dim>::cut_y
9782 //
9783 // the refined cube will look like this:
9784 //
9785 // *---------*
9786 // / /|
9787 // *---------* |
9788 // / /| |
9789 // *---------* | |
9790 // | | | |
9791 // | | | *
9792 // | | |/
9793 // | | *
9794 // | |/
9795 // *---------*
9796 //
9797 // again, first collect some data about the
9798 // indices of the lines, with the following
9799 // numbering:
9800
9801 // face 0: left plane
9802 // *
9803 // /|
9804 // * |
9805 // /| |
9806 // * | |
9807 // | 0 |
9808 // | | *
9809 // | |/
9810 // | *m0
9811 // |/
9812 // *
9813 // face 1: right plane
9814 // *
9815 // /|
9816 // m1* |
9817 // /| |
9818 // * | |
9819 // | 1 |
9820 // | | *
9821 // | |/
9822 // | *
9823 // |/
9824 // *
9825 // face 4: bottom plane
9826 // *-------*
9827 // / /
9828 // m0*---2---*
9829 // / /
9830 // *-------*
9831 // face 5: top plane
9832 // *-------*
9833 // / /
9834 // *---3---*m1
9835 // / /
9836 // *-------*
9837
9838 // set up a list of line iterators first. from
9839 // this, construct lists of line_indices and
9840 // line orientations later on
9841 const typename Triangulation<dim, spacedim>::
9842 raw_line_iterator lines[4] = {
9843 hex->face(0)->child(0)->line(
9844 (hex->face(0)->refinement_case() ==
9846 1 :
9847 3), // 0
9848 hex->face(1)->child(0)->line(
9849 (hex->face(1)->refinement_case() ==
9851 1 :
9852 3), // 1
9853 hex->face(4)->child(0)->line(
9854 (hex->face(4)->refinement_case() ==
9856 1 :
9857 3), // 2
9858 hex->face(5)->child(0)->line(
9859 (hex->face(5)->refinement_case() ==
9861 1 :
9862 3) // 3
9863 };
9864
9865 int line_indices[4];
9866 for (unsigned int i = 0; i < 4; ++i)
9867 line_indices[i] = lines[i]->index();
9868
9869 // the orientation of lines for the inner quads
9870 // is quite tricky. as these lines are newly
9871 // created ones and thus have no parents, they
9872 // cannot inherit this property. set up an array
9873 // and fill it with the respective values
9874 types::geometric_orientation line_orientation[4]{};
9875
9876 // the middle vertex marked as m0 above is the
9877 // start vertex for lines 0 and 2 in standard
9878 // orientation, whereas m1 is the end vertex of
9879 // lines 1 and 3 in standard orientation
9880 const unsigned int middle_vertices[2] = {
9881 hex->line(0)->child(0)->vertex_index(1),
9882 hex->line(5)->child(0)->vertex_index(1)};
9883
9884 for (unsigned int i = 0; i < 4; ++i)
9885 if (lines[i]->vertex_index(i % 2) ==
9886 middle_vertices[i % 2])
9887 line_orientation[i] =
9889 else
9890 {
9891 // it must be the other way round then
9892 Assert(lines[i]->vertex_index((i + 1) % 2) ==
9893 middle_vertices[i % 2],
9895 line_orientation[i] =
9897 }
9898
9899 // set up the new quad, line numbering is as
9900 // indicated above
9901 new_quads[0]->set_bounding_object_indices(
9902 {line_indices[2],
9903 line_indices[3],
9904 line_indices[0],
9905 line_indices[1]});
9906
9907 new_quads[0]->set_line_orientation(
9908 0, line_orientation[2]);
9909 new_quads[0]->set_line_orientation(
9910 1, line_orientation[3]);
9911 new_quads[0]->set_line_orientation(
9912 2, line_orientation[0]);
9913 new_quads[0]->set_line_orientation(
9914 3, line_orientation[1]);
9915
9916 // the quads are numbered as follows:
9917 //
9918 // planes in the interior of the old hex:
9919 //
9920 // *
9921 // /|
9922 // / | x
9923 // / | *-------* *---------*
9924 // * | | | / /
9925 // | | | 0 | / /
9926 // | * | | / /
9927 // | / *-------*y *---------*x
9928 // | /
9929 // |/
9930 // *
9931 //
9932 // children of the faces of the old hex
9933 //
9934 // *-------* *-------*
9935 // /| | / 10 /|
9936 // * | | *-------* |
9937 // /| | 6 | / 9 /| |
9938 // * |2| | *-------* |4|
9939 // | | *-------* | | | *
9940 // |1|/ 8 / | |3|/
9941 // | *-------* | 5 | *
9942 // |/ 7 / | |/
9943 // *-------* *-------*
9944 //
9945 // note that we have to take care of the
9946 // orientation of faces.
9947 const int quad_indices[11] = {
9948 new_quads[0]->index(), // 0
9949
9950 hex->face(0)->child_index(
9951 child_at_origin[hex->face(0)->refinement_case() -
9952 1][f_fl[0]][f_ro[0]]), // 1
9953 hex->face(0)->child_index(
9954 1 -
9955 child_at_origin[hex->face(0)->refinement_case() -
9956 1][f_fl[0]][f_ro[0]]),
9957
9958 hex->face(1)->child_index(
9959 child_at_origin[hex->face(1)->refinement_case() -
9960 1][f_fl[1]][f_ro[1]]), // 3
9961 hex->face(1)->child_index(
9962 1 -
9963 child_at_origin[hex->face(1)->refinement_case() -
9964 1][f_fl[1]][f_ro[1]]),
9965
9966 hex->face(2)->index(), // 5
9967
9968 hex->face(3)->index(), // 6
9969
9970 hex->face(4)->child_index(
9971 child_at_origin[hex->face(4)->refinement_case() -
9972 1][f_fl[4]][f_ro[4]]), // 7
9973 hex->face(4)->child_index(
9974 1 -
9975 child_at_origin[hex->face(4)->refinement_case() -
9976 1][f_fl[4]][f_ro[4]]),
9977
9978 hex->face(5)->child_index(
9979 child_at_origin[hex->face(5)->refinement_case() -
9980 1][f_fl[5]][f_ro[5]]), // 9
9981 hex->face(5)->child_index(
9982 1 -
9983 child_at_origin[hex->face(5)->refinement_case() -
9984 1][f_fl[5]][f_ro[5]])
9985
9986 };
9987
9988 new_hexes[0]->set_bounding_object_indices(
9989 {quad_indices[1],
9990 quad_indices[3],
9991 quad_indices[5],
9992 quad_indices[0],
9993 quad_indices[7],
9994 quad_indices[9]});
9995 new_hexes[1]->set_bounding_object_indices(
9996 {quad_indices[2],
9997 quad_indices[4],
9998 quad_indices[0],
9999 quad_indices[6],
10000 quad_indices[8],
10001 quad_indices[10]});
10002 break;
10003 }
10004
10006 {
10007 //----------------------------
10008 //
10009 // RefinementCase<dim>::cut_z
10010 //
10011 // the refined cube will look like this:
10012 //
10013 // *---------*
10014 // / /|
10015 // / / |
10016 // / / *
10017 // *---------* /|
10018 // | | / |
10019 // | |/ *
10020 // *---------* /
10021 // | | /
10022 // | |/
10023 // *---------*
10024 //
10025 // again, first collect some data about the
10026 // indices of the lines, with the following
10027 // numbering:
10028
10029 // face 0: left plane
10030 // *
10031 // /|
10032 // / |
10033 // / *
10034 // * /|
10035 // | 0 |
10036 // |/ *
10037 // m0* /
10038 // | /
10039 // |/
10040 // *
10041 // face 1: right plane
10042 // *
10043 // /|
10044 // / |
10045 // / *m1
10046 // * /|
10047 // | 1 |
10048 // |/ *
10049 // * /
10050 // | /
10051 // |/
10052 // *
10053 // face 2: front plane
10054 // (note: x,y exchanged)
10055 // *-------*
10056 // | |
10057 // m0*---2---*
10058 // | |
10059 // *-------*
10060 // face 3: back plane
10061 // (note: x,y exchanged)
10062 // *-------*
10063 // | |
10064 // *---3---*m1
10065 // | |
10066 // *-------*
10067
10068 // set up a list of line iterators first. from
10069 // this, construct lists of line_indices and
10070 // line orientations later on
10071 const typename Triangulation<dim, spacedim>::
10072 raw_line_iterator lines[4] = {
10073 hex->face(0)->child(0)->line(
10074 (hex->face(0)->refinement_case() ==
10076 1 :
10077 3), // 0
10078 hex->face(1)->child(0)->line(
10079 (hex->face(1)->refinement_case() ==
10081 1 :
10082 3), // 1
10083 hex->face(2)->child(0)->line(
10084 (hex->face(2)->refinement_case() ==
10086 1 :
10087 3), // 2
10088 hex->face(3)->child(0)->line(
10089 (hex->face(3)->refinement_case() ==
10091 1 :
10092 3) // 3
10093 };
10094
10095 int line_indices[4];
10096 for (unsigned int i = 0; i < 4; ++i)
10097 line_indices[i] = lines[i]->index();
10098
10099 // the orientation of lines for the inner quads
10100 // is quite tricky. as these lines are newly
10101 // created ones and thus have no parents, they
10102 // cannot inherit this property. set up an array
10103 // and fill it with the respective values
10104 types::geometric_orientation line_orientation[4]{};
10105
10106 // the middle vertex marked as m0 above is the
10107 // start vertex for lines 0 and 2 in standard
10108 // orientation, whereas m1 is the end vertex of
10109 // lines 1 and 3 in standard orientation
10110 const unsigned int middle_vertices[2] = {
10111 middle_vertex_index<dim, spacedim>(hex->line(8)),
10112 middle_vertex_index<dim, spacedim>(hex->line(11))};
10113
10114 for (unsigned int i = 0; i < 4; ++i)
10115 if (lines[i]->vertex_index(i % 2) ==
10116 middle_vertices[i % 2])
10117 line_orientation[i] =
10119 else
10120 {
10121 // it must be the other way round then
10122 Assert(lines[i]->vertex_index((i + 1) % 2) ==
10123 middle_vertices[i % 2],
10125 line_orientation[i] =
10127 }
10128
10129 // set up the new quad, line numbering is as
10130 // indicated above
10131 new_quads[0]->set_bounding_object_indices(
10132 {line_indices[0],
10133 line_indices[1],
10134 line_indices[2],
10135 line_indices[3]});
10136
10137 new_quads[0]->set_line_orientation(
10138 0, line_orientation[0]);
10139 new_quads[0]->set_line_orientation(
10140 1, line_orientation[1]);
10141 new_quads[0]->set_line_orientation(
10142 2, line_orientation[2]);
10143 new_quads[0]->set_line_orientation(
10144 3, line_orientation[3]);
10145
10146 // the quads are numbered as follows:
10147 //
10148 // planes in the interior of the old hex:
10149 //
10150 // *
10151 // /|
10152 // / | x
10153 // / | *-------* *---------*
10154 // * | | | / /
10155 // | | | | / 0 /
10156 // | * | | / /
10157 // | / *-------*y *---------*x
10158 // | /
10159 // |/
10160 // *
10161 //
10162 // children of the faces of the old hex
10163 //
10164 // *---*---* *-------*
10165 // /| 8 | / /|
10166 // / | | / 10 / |
10167 // / *-------* / / *
10168 // * 2/| | *-------* 4/|
10169 // | / | 7 | | 6 | / |
10170 // |/1 *-------* | |/3 *
10171 // * / / *-------* /
10172 // | / 9 / | | /
10173 // |/ / | 5 |/
10174 // *-------* *---*---*
10175 //
10176 // note that we have to take care of the
10177 // orientation of faces.
10178 const int quad_indices[11] = {
10179 new_quads[0]->index(), // 0
10180
10181 hex->face(0)->child_index(
10182 child_at_origin[hex->face(0)->refinement_case() -
10183 1][f_fl[0]][f_ro[0]]), // 1
10184 hex->face(0)->child_index(
10185 1 -
10186 child_at_origin[hex->face(0)->refinement_case() -
10187 1][f_fl[0]][f_ro[0]]),
10188
10189 hex->face(1)->child_index(
10190 child_at_origin[hex->face(1)->refinement_case() -
10191 1][f_fl[1]][f_ro[1]]), // 3
10192 hex->face(1)->child_index(
10193 1 -
10194 child_at_origin[hex->face(1)->refinement_case() -
10195 1][f_fl[1]][f_ro[1]]),
10196
10197 hex->face(2)->child_index(
10198 child_at_origin[hex->face(2)->refinement_case() -
10199 1][f_fl[2]][f_ro[2]]), // 5
10200 hex->face(2)->child_index(
10201 1 -
10202 child_at_origin[hex->face(2)->refinement_case() -
10203 1][f_fl[2]][f_ro[2]]),
10204
10205 hex->face(3)->child_index(
10206 child_at_origin[hex->face(3)->refinement_case() -
10207 1][f_fl[3]][f_ro[3]]), // 7
10208 hex->face(3)->child_index(
10209 1 -
10210 child_at_origin[hex->face(3)->refinement_case() -
10211 1][f_fl[3]][f_ro[3]]),
10212
10213 hex->face(4)->index(), // 9
10214
10215 hex->face(5)->index() // 10
10216 };
10217
10218 new_hexes[0]->set_bounding_object_indices(
10219 {quad_indices[1],
10220 quad_indices[3],
10221 quad_indices[5],
10222 quad_indices[7],
10223 quad_indices[9],
10224 quad_indices[0]});
10225 new_hexes[1]->set_bounding_object_indices(
10226 {quad_indices[2],
10227 quad_indices[4],
10228 quad_indices[6],
10229 quad_indices[8],
10230 quad_indices[0],
10231 quad_indices[10]});
10232 break;
10233 }
10234
10236 {
10237 //----------------------------
10238 //
10239 // RefinementCase<dim>::cut_xy
10240 //
10241 // the refined cube will look like this:
10242 //
10243 // *----*----*
10244 // / / /|
10245 // *----*----* |
10246 // / / /| |
10247 // *----*----* | |
10248 // | | | | |
10249 // | | | | *
10250 // | | | |/
10251 // | | | *
10252 // | | |/
10253 // *----*----*
10254 //
10255
10256 // first, create the new internal line
10257 new_lines[0]->set_bounding_object_indices(
10258 {middle_vertex_index<dim, spacedim>(hex->face(4)),
10259 middle_vertex_index<dim, spacedim>(hex->face(5))});
10260
10261 // again, first collect some data about the
10262 // indices of the lines, with the following
10263 // numbering:
10264
10265 // face 0: left plane
10266 // *
10267 // /|
10268 // * |
10269 // /| |
10270 // * | |
10271 // | 0 |
10272 // | | *
10273 // | |/
10274 // | *
10275 // |/
10276 // *
10277 // face 1: right plane
10278 // *
10279 // /|
10280 // * |
10281 // /| |
10282 // * | |
10283 // | 1 |
10284 // | | *
10285 // | |/
10286 // | *
10287 // |/
10288 // *
10289 // face 2: front plane
10290 // (note: x,y exchanged)
10291 // *---*---*
10292 // | | |
10293 // | 2 |
10294 // | | |
10295 // *-------*
10296 // face 3: back plane
10297 // (note: x,y exchanged)
10298 // *---*---*
10299 // | | |
10300 // | 3 |
10301 // | | |
10302 // *---*---*
10303 // face 4: bottom plane
10304 // *---*---*
10305 // / 5 /
10306 // *-6-*-7-*
10307 // / 4 /
10308 // *---*---*
10309 // face 5: top plane
10310 // *---*---*
10311 // / 9 /
10312 // *10-*-11*
10313 // / 8 /
10314 // *---*---*
10315 // middle planes
10316 // *-------* *---*---*
10317 // / / | | |
10318 // / / | 12 |
10319 // / / | | |
10320 // *-------* *---*---*
10321
10322 // set up a list of line iterators first. from
10323 // this, construct lists of line_indices and
10324 // line orientations later on
10325 const typename Triangulation<
10326 dim,
10327 spacedim>::raw_line_iterator lines[13] = {
10328 hex->face(0)->child(0)->line(
10329 (hex->face(0)->refinement_case() ==
10331 1 :
10332 3), // 0
10333 hex->face(1)->child(0)->line(
10334 (hex->face(1)->refinement_case() ==
10336 1 :
10337 3), // 1
10338 hex->face(2)->child(0)->line(
10339 (hex->face(2)->refinement_case() ==
10341 1 :
10342 3), // 2
10343 hex->face(3)->child(0)->line(
10344 (hex->face(3)->refinement_case() ==
10346 1 :
10347 3), // 3
10348
10349 hex->face(4)
10350 ->isotropic_child(
10352 0, f_or[4], f_fl[4], f_ro[4]))
10353 ->line(
10355 1, f_or[4], f_fl[4], f_ro[4])), // 4
10356 hex->face(4)
10357 ->isotropic_child(
10359 3, f_or[4], f_fl[4], f_ro[4]))
10360 ->line(
10362 0, f_or[4], f_fl[4], f_ro[4])), // 5
10363 hex->face(4)
10364 ->isotropic_child(
10366 0, f_or[4], f_fl[4], f_ro[4]))
10367 ->line(
10369 3, f_or[4], f_fl[4], f_ro[4])), // 6
10370 hex->face(4)
10371 ->isotropic_child(
10373 3, f_or[4], f_fl[4], f_ro[4]))
10374 ->line(
10376 2, f_or[4], f_fl[4], f_ro[4])), // 7
10377
10378 hex->face(5)
10379 ->isotropic_child(
10381 0, f_or[5], f_fl[5], f_ro[5]))
10382 ->line(
10384 1, f_or[5], f_fl[5], f_ro[5])), // 8
10385 hex->face(5)
10386 ->isotropic_child(
10388 3, f_or[5], f_fl[5], f_ro[5]))
10389 ->line(
10391 0, f_or[5], f_fl[5], f_ro[5])), // 9
10392 hex->face(5)
10393 ->isotropic_child(
10395 0, f_or[5], f_fl[5], f_ro[5]))
10396 ->line(
10398 3, f_or[5], f_fl[5], f_ro[5])), // 10
10399 hex->face(5)
10400 ->isotropic_child(
10402 3, f_or[5], f_fl[5], f_ro[5]))
10403 ->line(
10405 2, f_or[5], f_fl[5], f_ro[5])), // 11
10406
10407 new_lines[0] // 12
10408 };
10409
10410 int line_indices[13];
10411 for (unsigned int i = 0; i < 13; ++i)
10412 line_indices[i] = lines[i]->index();
10413
10414 // the orientation of lines for the inner quads
10415 // is quite tricky. as these lines are newly
10416 // created ones and thus have no parents, they
10417 // cannot inherit this property. set up an array
10418 // and fill it with the respective values
10419 types::geometric_orientation line_orientation[13]{};
10420
10421 // the middle vertices of the lines of our
10422 // bottom face
10423 const unsigned int middle_vertices[4] = {
10424 hex->line(0)->child(0)->vertex_index(1),
10425 hex->line(1)->child(0)->vertex_index(1),
10426 hex->line(2)->child(0)->vertex_index(1),
10427 hex->line(3)->child(0)->vertex_index(1),
10428 };
10429
10430 // note: for lines 0 to 3 the orientation of the
10431 // line is 'true', if vertex 0 is on the bottom
10432 // face
10433 for (unsigned int i = 0; i < 4; ++i)
10434 if (lines[i]->vertex_index(0) == middle_vertices[i])
10435 line_orientation[i] =
10437 else
10438 {
10439 // it must be the other way round then
10440 Assert(lines[i]->vertex_index(1) ==
10441 middle_vertices[i],
10443 line_orientation[i] =
10445 }
10446
10447 // note: for lines 4 to 11 (inner lines of the
10448 // outer quads) the following holds: the second
10449 // vertex of the even lines in standard
10450 // orientation is the vertex in the middle of
10451 // the quad, whereas for odd lines the first
10452 // vertex is the same middle vertex.
10453 for (unsigned int i = 4; i < 12; ++i)
10454 if (lines[i]->vertex_index((i + 1) % 2) ==
10455 middle_vertex_index<dim, spacedim>(
10456 hex->face(3 + i / 4)))
10457 line_orientation[i] =
10459 else
10460 {
10461 // it must be the other way round then
10462 Assert(lines[i]->vertex_index(i % 2) ==
10463 (middle_vertex_index<dim, spacedim>(
10464 hex->face(3 + i / 4))),
10466 line_orientation[i] =
10468 }
10469 // for the last line the line orientation is
10470 // always true, since it was just constructed
10471 // that way
10472 line_orientation[12] =
10474
10475 // set up the 4 quads, numbered as follows (left
10476 // quad numbering, right line numbering
10477 // extracted from above)
10478 //
10479 // * *
10480 // /| 9|
10481 // * | * |
10482 // y/| | 8| 3
10483 // * |1| * | |
10484 // | | |x | 12|
10485 // |0| * | | *
10486 // | |/ 2 |5
10487 // | * | *
10488 // |/ |4
10489 // * *
10490 //
10491 // x
10492 // *---*---* *10-*-11*
10493 // | | | | | |
10494 // | 2 | 3 | 0 12 1
10495 // | | | | | |
10496 // *---*---*y *-6-*-7-*
10497
10498 new_quads[0]->set_bounding_object_indices(
10499 {line_indices[2],
10500 line_indices[12],
10501 line_indices[4],
10502 line_indices[8]});
10503 new_quads[1]->set_bounding_object_indices(
10504 {line_indices[12],
10505 line_indices[3],
10506 line_indices[5],
10507 line_indices[9]});
10508 new_quads[2]->set_bounding_object_indices(
10509 {line_indices[6],
10510 line_indices[10],
10511 line_indices[0],
10512 line_indices[12]});
10513 new_quads[3]->set_bounding_object_indices(
10514 {line_indices[7],
10515 line_indices[11],
10516 line_indices[12],
10517 line_indices[1]});
10518
10519 new_quads[0]->set_line_orientation(
10520 0, line_orientation[2]);
10521 new_quads[0]->set_line_orientation(
10522 2, line_orientation[4]);
10523 new_quads[0]->set_line_orientation(
10524 3, line_orientation[8]);
10525
10526 new_quads[1]->set_line_orientation(
10527 1, line_orientation[3]);
10528 new_quads[1]->set_line_orientation(
10529 2, line_orientation[5]);
10530 new_quads[1]->set_line_orientation(
10531 3, line_orientation[9]);
10532
10533 new_quads[2]->set_line_orientation(
10534 0, line_orientation[6]);
10535 new_quads[2]->set_line_orientation(
10536 1, line_orientation[10]);
10537 new_quads[2]->set_line_orientation(
10538 2, line_orientation[0]);
10539
10540 new_quads[3]->set_line_orientation(
10541 0, line_orientation[7]);
10542 new_quads[3]->set_line_orientation(
10543 1, line_orientation[11]);
10544 new_quads[3]->set_line_orientation(
10545 3, line_orientation[1]);
10546
10547 // the quads are numbered as follows:
10548 //
10549 // planes in the interior of the old hex:
10550 //
10551 // *
10552 // /|
10553 // * | x
10554 // /| | *---*---* *---------*
10555 // * |1| | | | / /
10556 // | | | | 2 | 3 | / /
10557 // |0| * | | | / /
10558 // | |/ *---*---*y *---------*x
10559 // | *
10560 // |/
10561 // *
10562 //
10563 // children of the faces of the old hex
10564 //
10565 // *---*---* *---*---*
10566 // /| | | /18 / 19/|
10567 // * |10 | 11| /---/---* |
10568 // /| | | | /16 / 17/| |
10569 // * |5| | | *---*---* |7|
10570 // | | *---*---* | | | | *
10571 // |4|/14 / 15/ | | |6|/
10572 // | *---/---/ | 8 | 9 | *
10573 // |/12 / 13/ | | |/
10574 // *---*---* *---*---*
10575 //
10576 // note that we have to take care of the
10577 // orientation of faces.
10578 const int quad_indices[20] = {
10579 new_quads[0]->index(), // 0
10580 new_quads[1]->index(),
10581 new_quads[2]->index(),
10582 new_quads[3]->index(),
10583
10584 hex->face(0)->child_index(
10585 child_at_origin[hex->face(0)->refinement_case() -
10586 1][f_fl[0]][f_ro[0]]), // 4
10587 hex->face(0)->child_index(
10588 1 -
10589 child_at_origin[hex->face(0)->refinement_case() -
10590 1][f_fl[0]][f_ro[0]]),
10591
10592 hex->face(1)->child_index(
10593 child_at_origin[hex->face(1)->refinement_case() -
10594 1][f_fl[1]][f_ro[1]]), // 6
10595 hex->face(1)->child_index(
10596 1 -
10597 child_at_origin[hex->face(1)->refinement_case() -
10598 1][f_fl[1]][f_ro[1]]),
10599
10600 hex->face(2)->child_index(
10601 child_at_origin[hex->face(2)->refinement_case() -
10602 1][f_fl[2]][f_ro[2]]), // 8
10603 hex->face(2)->child_index(
10604 1 -
10605 child_at_origin[hex->face(2)->refinement_case() -
10606 1][f_fl[2]][f_ro[2]]),
10607
10608 hex->face(3)->child_index(
10609 child_at_origin[hex->face(3)->refinement_case() -
10610 1][f_fl[3]][f_ro[3]]), // 10
10611 hex->face(3)->child_index(
10612 1 -
10613 child_at_origin[hex->face(3)->refinement_case() -
10614 1][f_fl[3]][f_ro[3]]),
10615
10616 hex->face(4)->isotropic_child_index(
10618 0, f_or[4], f_fl[4], f_ro[4])), // 12
10619 hex->face(4)->isotropic_child_index(
10621 1, f_or[4], f_fl[4], f_ro[4])),
10622 hex->face(4)->isotropic_child_index(
10624 2, f_or[4], f_fl[4], f_ro[4])),
10625 hex->face(4)->isotropic_child_index(
10627 3, f_or[4], f_fl[4], f_ro[4])),
10628
10629 hex->face(5)->isotropic_child_index(
10631 0, f_or[5], f_fl[5], f_ro[5])), // 16
10632 hex->face(5)->isotropic_child_index(
10634 1, f_or[5], f_fl[5], f_ro[5])),
10635 hex->face(5)->isotropic_child_index(
10637 2, f_or[5], f_fl[5], f_ro[5])),
10638 hex->face(5)->isotropic_child_index(
10640 3, f_or[5], f_fl[5], f_ro[5]))};
10641
10642 new_hexes[0]->set_bounding_object_indices(
10643 {quad_indices[4],
10644 quad_indices[0],
10645 quad_indices[8],
10646 quad_indices[2],
10647 quad_indices[12],
10648 quad_indices[16]});
10649 new_hexes[1]->set_bounding_object_indices(
10650 {quad_indices[0],
10651 quad_indices[6],
10652 quad_indices[9],
10653 quad_indices[3],
10654 quad_indices[13],
10655 quad_indices[17]});
10656 new_hexes[2]->set_bounding_object_indices(
10657 {quad_indices[5],
10658 quad_indices[1],
10659 quad_indices[2],
10660 quad_indices[10],
10661 quad_indices[14],
10662 quad_indices[18]});
10663 new_hexes[3]->set_bounding_object_indices(
10664 {quad_indices[1],
10665 quad_indices[7],
10666 quad_indices[3],
10667 quad_indices[11],
10668 quad_indices[15],
10669 quad_indices[19]});
10670 break;
10671 }
10672
10674 {
10675 //----------------------------
10676 //
10677 // RefinementCase<dim>::cut_xz
10678 //
10679 // the refined cube will look like this:
10680 //
10681 // *----*----*
10682 // / / /|
10683 // / / / |
10684 // / / / *
10685 // *----*----* /|
10686 // | | | / |
10687 // | | |/ *
10688 // *----*----* /
10689 // | | | /
10690 // | | |/
10691 // *----*----*
10692 //
10693
10694 // first, create the new internal line
10695 new_lines[0]->set_bounding_object_indices(
10696 {middle_vertex_index<dim, spacedim>(hex->face(2)),
10697 middle_vertex_index<dim, spacedim>(hex->face(3))});
10698
10699 // again, first collect some data about the
10700 // indices of the lines, with the following
10701 // numbering:
10702
10703 // face 0: left plane
10704 // *
10705 // /|
10706 // / |
10707 // / *
10708 // * /|
10709 // | 0 |
10710 // |/ *
10711 // * /
10712 // | /
10713 // |/
10714 // *
10715 // face 1: right plane
10716 // *
10717 // /|
10718 // / |
10719 // / *
10720 // * /|
10721 // | 1 |
10722 // |/ *
10723 // * /
10724 // | /
10725 // |/
10726 // *
10727 // face 2: front plane
10728 // (note: x,y exchanged)
10729 // *---*---*
10730 // | 5 |
10731 // *-6-*-7-*
10732 // | 4 |
10733 // *---*---*
10734 // face 3: back plane
10735 // (note: x,y exchanged)
10736 // *---*---*
10737 // | 9 |
10738 // *10-*-11*
10739 // | 8 |
10740 // *---*---*
10741 // face 4: bottom plane
10742 // *---*---*
10743 // / / /
10744 // / 2 /
10745 // / / /
10746 // *---*---*
10747 // face 5: top plane
10748 // *---*---*
10749 // / / /
10750 // / 3 /
10751 // / / /
10752 // *---*---*
10753 // middle planes
10754 // *---*---* *-------*
10755 // / / / | |
10756 // / 12 / | |
10757 // / / / | |
10758 // *---*---* *-------*
10759
10760 // set up a list of line iterators first. from
10761 // this, construct lists of line_indices and
10762 // line orientations later on
10763 const typename Triangulation<
10764 dim,
10765 spacedim>::raw_line_iterator lines[13] = {
10766 hex->face(0)->child(0)->line(
10767 (hex->face(0)->refinement_case() ==
10769 1 :
10770 3), // 0
10771 hex->face(1)->child(0)->line(
10772 (hex->face(1)->refinement_case() ==
10774 1 :
10775 3), // 1
10776 hex->face(4)->child(0)->line(
10777 (hex->face(4)->refinement_case() ==
10779 1 :
10780 3), // 2
10781 hex->face(5)->child(0)->line(
10782 (hex->face(5)->refinement_case() ==
10784 1 :
10785 3), // 3
10786
10787 hex->face(2)
10788 ->isotropic_child(
10790 0, f_or[2], f_fl[2], f_ro[2]))
10791 ->line(
10793 3, f_or[2], f_fl[2], f_ro[2])), // 4
10794 hex->face(2)
10795 ->isotropic_child(
10797 3, f_or[2], f_fl[2], f_ro[2]))
10798 ->line(
10800 2, f_or[2], f_fl[2], f_ro[2])), // 5
10801 hex->face(2)
10802 ->isotropic_child(
10804 0, f_or[2], f_fl[2], f_ro[2]))
10805 ->line(
10807 1, f_or[2], f_fl[2], f_ro[2])), // 6
10808 hex->face(2)
10809 ->isotropic_child(
10811 3, f_or[2], f_fl[2], f_ro[2]))
10812 ->line(
10814 0, f_or[2], f_fl[2], f_ro[2])), // 7
10815
10816 hex->face(3)
10817 ->isotropic_child(
10819 0, f_or[3], f_fl[3], f_ro[3]))
10820 ->line(
10822 3, f_or[3], f_fl[3], f_ro[3])), // 8
10823 hex->face(3)
10824 ->isotropic_child(
10826 3, f_or[3], f_fl[3], f_ro[3]))
10827 ->line(
10829 2, f_or[3], f_fl[3], f_ro[3])), // 9
10830 hex->face(3)
10831 ->isotropic_child(
10833 0, f_or[3], f_fl[3], f_ro[3]))
10834 ->line(
10836 1, f_or[3], f_fl[3], f_ro[3])), // 10
10837 hex->face(3)
10838 ->isotropic_child(
10840 3, f_or[3], f_fl[3], f_ro[3]))
10841 ->line(
10843 0, f_or[3], f_fl[3], f_ro[3])), // 11
10844
10845 new_lines[0] // 12
10846 };
10847
10848 int line_indices[13];
10849 for (unsigned int i = 0; i < 13; ++i)
10850 line_indices[i] = lines[i]->index();
10851
10852 // the orientation of lines for the inner quads
10853 // is quite tricky. as these lines are newly
10854 // created ones and thus have no parents, they
10855 // cannot inherit this property. set up an array
10856 // and fill it with the respective values
10857 types::geometric_orientation line_orientation[13]{};
10858
10859 // the middle vertices of the
10860 // lines of our front face
10861 const unsigned int middle_vertices[4] = {
10862 hex->line(8)->child(0)->vertex_index(1),
10863 hex->line(9)->child(0)->vertex_index(1),
10864 hex->line(2)->child(0)->vertex_index(1),
10865 hex->line(6)->child(0)->vertex_index(1),
10866 };
10867
10868 // note: for lines 0 to 3 the orientation of the
10869 // line is 'true', if vertex 0 is on the front
10870 for (unsigned int i = 0; i < 4; ++i)
10871 if (lines[i]->vertex_index(0) == middle_vertices[i])
10872 line_orientation[i] =
10874 else
10875 {
10876 // it must be the other way round then
10877 Assert(lines[i]->vertex_index(1) ==
10878 middle_vertices[i],
10880 line_orientation[i] =
10882 }
10883
10884 // note: for lines 4 to 11 (inner lines of the
10885 // outer quads) the following holds: the second
10886 // vertex of the even lines in standard
10887 // orientation is the vertex in the middle of
10888 // the quad, whereas for odd lines the first
10889 // vertex is the same middle vertex.
10890 for (unsigned int i = 4; i < 12; ++i)
10891 if (lines[i]->vertex_index((i + 1) % 2) ==
10892 middle_vertex_index<dim, spacedim>(
10893 hex->face(1 + i / 4)))
10894 line_orientation[i] =
10896 else
10897 {
10898 // it must be the other way
10899 // round then
10900 Assert(lines[i]->vertex_index(i % 2) ==
10901 (middle_vertex_index<dim, spacedim>(
10902 hex->face(1 + i / 4))),
10904 line_orientation[i] =
10906 }
10907 // for the last line the line orientation is
10908 // always true, since it was just constructed
10909 // that way
10910 line_orientation[12] =
10912
10913 // set up the 4 quads, numbered as follows (left
10914 // quad numbering, right line numbering
10915 // extracted from above), the drawings denote
10916 // middle planes
10917 //
10918 // * *
10919 // /| /|
10920 // / | 3 9
10921 // y/ * / *
10922 // * 3/| * /|
10923 // | / |x 5 12|8
10924 // |/ * |/ *
10925 // * 2/ * /
10926 // | / 4 2
10927 // |/ |/
10928 // * *
10929 //
10930 // y
10931 // *----*----* *-10-*-11-*
10932 // / / / / / /
10933 // / 0 / 1 / 0 12 1
10934 // / / / / / /
10935 // *----*----*x *--6-*--7-*
10936
10937 new_quads[0]->set_bounding_object_indices(
10938 {line_indices[0],
10939 line_indices[12],
10940 line_indices[6],
10941 line_indices[10]});
10942 new_quads[1]->set_bounding_object_indices(
10943 {line_indices[12],
10944 line_indices[1],
10945 line_indices[7],
10946 line_indices[11]});
10947 new_quads[2]->set_bounding_object_indices(
10948 {line_indices[4],
10949 line_indices[8],
10950 line_indices[2],
10951 line_indices[12]});
10952 new_quads[3]->set_bounding_object_indices(
10953 {line_indices[5],
10954 line_indices[9],
10955 line_indices[12],
10956 line_indices[3]});
10957
10958 new_quads[0]->set_line_orientation(
10959 0, line_orientation[0]);
10960 new_quads[0]->set_line_orientation(
10961 2, line_orientation[6]);
10962 new_quads[0]->set_line_orientation(
10963 3, line_orientation[10]);
10964
10965 new_quads[1]->set_line_orientation(
10966 1, line_orientation[1]);
10967 new_quads[1]->set_line_orientation(
10968 2, line_orientation[7]);
10969 new_quads[1]->set_line_orientation(
10970 3, line_orientation[11]);
10971
10972 new_quads[2]->set_line_orientation(
10973 0, line_orientation[4]);
10974 new_quads[2]->set_line_orientation(
10975 1, line_orientation[8]);
10976 new_quads[2]->set_line_orientation(
10977 2, line_orientation[2]);
10978
10979 new_quads[3]->set_line_orientation(
10980 0, line_orientation[5]);
10981 new_quads[3]->set_line_orientation(
10982 1, line_orientation[9]);
10983 new_quads[3]->set_line_orientation(
10984 3, line_orientation[3]);
10985
10986 // the quads are numbered as follows:
10987 //
10988 // planes in the interior of the old hex:
10989 //
10990 // *
10991 // /|
10992 // / | x
10993 // /3 * *-------* *----*----*
10994 // * /| | | / / /
10995 // | / | | | / 0 / 1 /
10996 // |/ * | | / / /
10997 // * 2/ *-------*y *----*----*x
10998 // | /
10999 // |/
11000 // *
11001 //
11002 // children of the faces
11003 // of the old hex
11004 // *---*---* *---*---*
11005 // /|13 | 15| / / /|
11006 // / | | | /18 / 19/ |
11007 // / *---*---* / / / *
11008 // * 5/| | | *---*---* 7/|
11009 // | / |12 | 14| | 9 | 11| / |
11010 // |/4 *---*---* | | |/6 *
11011 // * / / / *---*---* /
11012 // | /16 / 17/ | | | /
11013 // |/ / / | 8 | 10|/
11014 // *---*---* *---*---*
11015 //
11016 // note that we have to take care of the
11017 // orientation of faces.
11018 const int quad_indices[20] = {
11019 new_quads[0]->index(), // 0
11020 new_quads[1]->index(),
11021 new_quads[2]->index(),
11022 new_quads[3]->index(),
11023
11024 hex->face(0)->child_index(
11025 child_at_origin[hex->face(0)->refinement_case() -
11026 1][f_fl[0]][f_ro[0]]), // 4
11027 hex->face(0)->child_index(
11028 1 -
11029 child_at_origin[hex->face(0)->refinement_case() -
11030 1][f_fl[0]][f_ro[0]]),
11031
11032 hex->face(1)->child_index(
11033 child_at_origin[hex->face(1)->refinement_case() -
11034 1][f_fl[1]][f_ro[1]]), // 6
11035 hex->face(1)->child_index(
11036 1 -
11037 child_at_origin[hex->face(1)->refinement_case() -
11038 1][f_fl[1]][f_ro[1]]),
11039
11040 hex->face(2)->isotropic_child_index(
11042 0, f_or[2], f_fl[2], f_ro[2])), // 8
11043 hex->face(2)->isotropic_child_index(
11045 1, f_or[2], f_fl[2], f_ro[2])),
11046 hex->face(2)->isotropic_child_index(
11048 2, f_or[2], f_fl[2], f_ro[2])),
11049 hex->face(2)->isotropic_child_index(
11051 3, f_or[2], f_fl[2], f_ro[2])),
11052
11053 hex->face(3)->isotropic_child_index(
11055 0, f_or[3], f_fl[3], f_ro[3])), // 12
11056 hex->face(3)->isotropic_child_index(
11058 1, f_or[3], f_fl[3], f_ro[3])),
11059 hex->face(3)->isotropic_child_index(
11061 2, f_or[3], f_fl[3], f_ro[3])),
11062 hex->face(3)->isotropic_child_index(
11064 3, f_or[3], f_fl[3], f_ro[3])),
11065
11066 hex->face(4)->child_index(
11067 child_at_origin[hex->face(4)->refinement_case() -
11068 1][f_fl[4]][f_ro[4]]), // 16
11069 hex->face(4)->child_index(
11070 1 -
11071 child_at_origin[hex->face(4)->refinement_case() -
11072 1][f_fl[4]][f_ro[4]]),
11073
11074 hex->face(5)->child_index(
11075 child_at_origin[hex->face(5)->refinement_case() -
11076 1][f_fl[5]][f_ro[5]]), // 18
11077 hex->face(5)->child_index(
11078 1 -
11079 child_at_origin[hex->face(5)->refinement_case() -
11080 1][f_fl[5]][f_ro[5]])};
11081
11082 // due to the exchange of x and y for the front
11083 // and back face, we order the children
11084 // according to
11085 //
11086 // *---*---*
11087 // | 1 | 3 |
11088 // *---*---*
11089 // | 0 | 2 |
11090 // *---*---*
11091 new_hexes[0]->set_bounding_object_indices(
11092 {quad_indices[4],
11093 quad_indices[2],
11094 quad_indices[8],
11095 quad_indices[12],
11096 quad_indices[16],
11097 quad_indices[0]});
11098 new_hexes[1]->set_bounding_object_indices(
11099 {quad_indices[5],
11100 quad_indices[3],
11101 quad_indices[9],
11102 quad_indices[13],
11103 quad_indices[0],
11104 quad_indices[18]});
11105 new_hexes[2]->set_bounding_object_indices(
11106 {quad_indices[2],
11107 quad_indices[6],
11108 quad_indices[10],
11109 quad_indices[14],
11110 quad_indices[17],
11111 quad_indices[1]});
11112 new_hexes[3]->set_bounding_object_indices(
11113 {quad_indices[3],
11114 quad_indices[7],
11115 quad_indices[11],
11116 quad_indices[15],
11117 quad_indices[1],
11118 quad_indices[19]});
11119 break;
11120 }
11121
11123 {
11124 //----------------------------
11125 //
11126 // RefinementCase<dim>::cut_yz
11127 //
11128 // the refined cube will look like this:
11129 //
11130 // *---------*
11131 // / /|
11132 // *---------* |
11133 // / /| |
11134 // *---------* |/|
11135 // | | * |
11136 // | |/| *
11137 // *---------* |/
11138 // | | *
11139 // | |/
11140 // *---------*
11141 //
11142
11143 // first, create the new
11144 // internal line
11145 new_lines[0]->set_bounding_object_indices(
11146
11147 {middle_vertex_index<dim, spacedim>(hex->face(0)),
11148 middle_vertex_index<dim, spacedim>(hex->face(1))});
11149
11150 // again, first collect some data about the
11151 // indices of the lines, with the following
11152 // numbering: (note that face 0 and 1 each are
11153 // shown twice for better readability)
11154
11155 // face 0: left plane
11156 // * *
11157 // /| /|
11158 // * | * |
11159 // /| * /| *
11160 // * 5/| * |7|
11161 // | * | | * |
11162 // |/| * |6| *
11163 // * 4/ * |/
11164 // | * | *
11165 // |/ |/
11166 // * *
11167 // face 1: right plane
11168 // * *
11169 // /| /|
11170 // * | * |
11171 // /| * /| *
11172 // * 9/| * |11
11173 // | * | | * |
11174 // |/| * |10 *
11175 // * 8/ * |/
11176 // | * | *
11177 // |/ |/
11178 // * *
11179 // face 2: front plane
11180 // (note: x,y exchanged)
11181 // *-------*
11182 // | |
11183 // *---0---*
11184 // | |
11185 // *-------*
11186 // face 3: back plane
11187 // (note: x,y exchanged)
11188 // *-------*
11189 // | |
11190 // *---1---*
11191 // | |
11192 // *-------*
11193 // face 4: bottom plane
11194 // *-------*
11195 // / /
11196 // *---2---*
11197 // / /
11198 // *-------*
11199 // face 5: top plane
11200 // *-------*
11201 // / /
11202 // *---3---*
11203 // / /
11204 // *-------*
11205 // middle planes
11206 // *-------* *-------*
11207 // / / | |
11208 // *---12--* | |
11209 // / / | |
11210 // *-------* *-------*
11211
11212 // set up a list of line iterators first. from
11213 // this, construct lists of line_indices and
11214 // line orientations later on
11215 const typename Triangulation<
11216 dim,
11217 spacedim>::raw_line_iterator lines[13] = {
11218 hex->face(2)->child(0)->line(
11219 (hex->face(2)->refinement_case() ==
11221 1 :
11222 3), // 0
11223 hex->face(3)->child(0)->line(
11224 (hex->face(3)->refinement_case() ==
11226 1 :
11227 3), // 1
11228 hex->face(4)->child(0)->line(
11229 (hex->face(4)->refinement_case() ==
11231 1 :
11232 3), // 2
11233 hex->face(5)->child(0)->line(
11234 (hex->face(5)->refinement_case() ==
11236 1 :
11237 3), // 3
11238
11239 hex->face(0)
11240 ->isotropic_child(
11242 0, f_or[0], f_fl[0], f_ro[0]))
11243 ->line(
11245 1, f_or[0], f_fl[0], f_ro[0])), // 4
11246 hex->face(0)
11247 ->isotropic_child(
11249 3, f_or[0], f_fl[0], f_ro[0]))
11250 ->line(
11252 0, f_or[0], f_fl[0], f_ro[0])), // 5
11253 hex->face(0)
11254 ->isotropic_child(
11256 0, f_or[0], f_fl[0], f_ro[0]))
11257 ->line(
11259 3, f_or[0], f_fl[0], f_ro[0])), // 6
11260 hex->face(0)
11261 ->isotropic_child(
11263 3, f_or[0], f_fl[0], f_ro[0]))
11264 ->line(
11266 2, f_or[0], f_fl[0], f_ro[0])), // 7
11267
11268 hex->face(1)
11269 ->isotropic_child(
11271 0, f_or[1], f_fl[1], f_ro[1]))
11272 ->line(
11274 1, f_or[1], f_fl[1], f_ro[1])), // 8
11275 hex->face(1)
11276 ->isotropic_child(
11278 3, f_or[1], f_fl[1], f_ro[1]))
11279 ->line(
11281 0, f_or[1], f_fl[1], f_ro[1])), // 9
11282 hex->face(1)
11283 ->isotropic_child(
11285 0, f_or[1], f_fl[1], f_ro[1]))
11286 ->line(
11288 3, f_or[1], f_fl[1], f_ro[1])), // 10
11289 hex->face(1)
11290 ->isotropic_child(
11292 3, f_or[1], f_fl[1], f_ro[1]))
11293 ->line(
11295 2, f_or[1], f_fl[1], f_ro[1])), // 11
11296
11297 new_lines[0] // 12
11298 };
11299
11300 int line_indices[13];
11301 for (unsigned int i = 0; i < 13; ++i)
11302 line_indices[i] = lines[i]->index();
11303
11304 // the orientation of lines for the inner quads
11305 // is quite tricky. as these lines are newly
11306 // created ones and thus have no parents, they
11307 // cannot inherit this property. set up an array
11308 // and fill it with the respective values
11309 types::geometric_orientation line_orientation[13]{};
11310
11311 // the middle vertices of the lines of our front
11312 // face
11313 const unsigned int middle_vertices[4] = {
11314 hex->line(8)->child(0)->vertex_index(1),
11315 hex->line(10)->child(0)->vertex_index(1),
11316 hex->line(0)->child(0)->vertex_index(1),
11317 hex->line(4)->child(0)->vertex_index(1),
11318 };
11319
11320 // note: for lines 0 to 3 the orientation of the
11321 // line is 'true', if vertex 0 is on the front
11322 for (unsigned int i = 0; i < 4; ++i)
11323 if (lines[i]->vertex_index(0) == middle_vertices[i])
11324 line_orientation[i] =
11326 else
11327 {
11328 // it must be the other way round then
11329 Assert(lines[i]->vertex_index(1) ==
11330 middle_vertices[i],
11332 line_orientation[i] =
11334 }
11335
11336 // note: for lines 4 to 11 (inner lines of the
11337 // outer quads) the following holds: the second
11338 // vertex of the even lines in standard
11339 // orientation is the vertex in the middle of
11340 // the quad, whereas for odd lines the first
11341 // vertex is the same middle vertex.
11342 for (unsigned int i = 4; i < 12; ++i)
11343 if (lines[i]->vertex_index((i + 1) % 2) ==
11344 middle_vertex_index<dim, spacedim>(
11345 hex->face(i / 4 - 1)))
11346 line_orientation[i] =
11348 else
11349 {
11350 // it must be the other way round then
11351 Assert(lines[i]->vertex_index(i % 2) ==
11352 (middle_vertex_index<dim, spacedim>(
11353 hex->face(i / 4 - 1))),
11355 line_orientation[i] =
11357 }
11358 // for the last line the line orientation is always
11359 // the default, since it was just constructed that way
11360 line_orientation[12] =
11362
11363 // set up the 4 quads, numbered as follows (left
11364 // quad numbering, right line numbering
11365 // extracted from above)
11366 //
11367 // x
11368 // *-------* *---3---*
11369 // | 3 | 5 9
11370 // *-------* *---12--*
11371 // | 2 | 4 8
11372 // *-------*y *---2---*
11373 //
11374 // y
11375 // *---------* *----1----*
11376 // / 1 / 7 11
11377 // *---------* *----12---*
11378 // / 0 / 6 10
11379 // *---------*x *----0----*
11380
11381 new_quads[0]->set_bounding_object_indices(
11382 {line_indices[6],
11383 line_indices[10],
11384 line_indices[0],
11385 line_indices[12]});
11386 new_quads[1]->set_bounding_object_indices(
11387 {line_indices[7],
11388 line_indices[11],
11389 line_indices[12],
11390 line_indices[1]});
11391 new_quads[2]->set_bounding_object_indices(
11392 {line_indices[2],
11393 line_indices[12],
11394 line_indices[4],
11395 line_indices[8]});
11396 new_quads[3]->set_bounding_object_indices(
11397 {line_indices[12],
11398 line_indices[3],
11399 line_indices[5],
11400 line_indices[9]});
11401
11402 new_quads[0]->set_line_orientation(
11403 0, line_orientation[6]);
11404 new_quads[0]->set_line_orientation(
11405 1, line_orientation[10]);
11406 new_quads[0]->set_line_orientation(
11407 2, line_orientation[0]);
11408
11409 new_quads[1]->set_line_orientation(
11410 0, line_orientation[7]);
11411 new_quads[1]->set_line_orientation(
11412 1, line_orientation[11]);
11413 new_quads[1]->set_line_orientation(
11414 3, line_orientation[1]);
11415
11416 new_quads[2]->set_line_orientation(
11417 0, line_orientation[2]);
11418 new_quads[2]->set_line_orientation(
11419 2, line_orientation[4]);
11420 new_quads[2]->set_line_orientation(
11421 3, line_orientation[8]);
11422
11423 new_quads[3]->set_line_orientation(
11424 1, line_orientation[3]);
11425 new_quads[3]->set_line_orientation(
11426 2, line_orientation[5]);
11427 new_quads[3]->set_line_orientation(
11428 3, line_orientation[9]);
11429
11430 // the quads are numbered as follows:
11431 //
11432 // planes in the interior of the old hex:
11433 //
11434 // *
11435 // /|
11436 // / | x
11437 // / | *-------* *---------*
11438 // * | | 3 | / 1 /
11439 // | | *-------* *---------*
11440 // | * | 2 | / 0 /
11441 // | / *-------*y *---------*x
11442 // | /
11443 // |/
11444 // *
11445 //
11446 // children of the faces
11447 // of the old hex
11448 // *-------* *-------*
11449 // /| | / 19 /|
11450 // * | 15 | *-------* |
11451 // /|7*-------* / 18 /|11
11452 // * |/| | *-------* |/|
11453 // |6* | 14 | | 10* |
11454 // |/|5*-------* | 13 |/|9*
11455 // * |/ 17 / *-------* |/
11456 // |4*-------* | |8*
11457 // |/ 16 / | 12 |/
11458 // *-------* *-------*
11459 //
11460 // note that we have to take care of the
11461 // orientation of faces.
11462 const int quad_indices[20] = {
11463 new_quads[0]->index(), // 0
11464 new_quads[1]->index(),
11465 new_quads[2]->index(),
11466 new_quads[3]->index(),
11467
11468 hex->face(0)->isotropic_child_index(
11470 0, f_or[0], f_fl[0], f_ro[0])), // 4
11471 hex->face(0)->isotropic_child_index(
11473 1, f_or[0], f_fl[0], f_ro[0])),
11474 hex->face(0)->isotropic_child_index(
11476 2, f_or[0], f_fl[0], f_ro[0])),
11477 hex->face(0)->isotropic_child_index(
11479 3, f_or[0], f_fl[0], f_ro[0])),
11480
11481 hex->face(1)->isotropic_child_index(
11483 0, f_or[1], f_fl[1], f_ro[1])), // 8
11484 hex->face(1)->isotropic_child_index(
11486 1, f_or[1], f_fl[1], f_ro[1])),
11487 hex->face(1)->isotropic_child_index(
11489 2, f_or[1], f_fl[1], f_ro[1])),
11490 hex->face(1)->isotropic_child_index(
11492 3, f_or[1], f_fl[1], f_ro[1])),
11493
11494 hex->face(2)->child_index(
11495 child_at_origin[hex->face(2)->refinement_case() -
11496 1][f_fl[2]][f_ro[2]]), // 12
11497 hex->face(2)->child_index(
11498 1 -
11499 child_at_origin[hex->face(2)->refinement_case() -
11500 1][f_fl[2]][f_ro[2]]),
11501
11502 hex->face(3)->child_index(
11503 child_at_origin[hex->face(3)->refinement_case() -
11504 1][f_fl[3]][f_ro[3]]), // 14
11505 hex->face(3)->child_index(
11506 1 -
11507 child_at_origin[hex->face(3)->refinement_case() -
11508 1][f_fl[3]][f_ro[3]]),
11509
11510 hex->face(4)->child_index(
11511 child_at_origin[hex->face(4)->refinement_case() -
11512 1][f_fl[4]][f_ro[4]]), // 16
11513 hex->face(4)->child_index(
11514 1 -
11515 child_at_origin[hex->face(4)->refinement_case() -
11516 1][f_fl[4]][f_ro[4]]),
11517
11518 hex->face(5)->child_index(
11519 child_at_origin[hex->face(5)->refinement_case() -
11520 1][f_fl[5]][f_ro[5]]), // 18
11521 hex->face(5)->child_index(
11522 1 -
11523 child_at_origin[hex->face(5)->refinement_case() -
11524 1][f_fl[5]][f_ro[5]])};
11525
11526 new_hexes[0]->set_bounding_object_indices(
11527 {quad_indices[4],
11528 quad_indices[8],
11529 quad_indices[12],
11530 quad_indices[2],
11531 quad_indices[16],
11532 quad_indices[0]});
11533 new_hexes[1]->set_bounding_object_indices(
11534 {quad_indices[5],
11535 quad_indices[9],
11536 quad_indices[2],
11537 quad_indices[14],
11538 quad_indices[17],
11539 quad_indices[1]});
11540 new_hexes[2]->set_bounding_object_indices(
11541 {quad_indices[6],
11542 quad_indices[10],
11543 quad_indices[13],
11544 quad_indices[3],
11545 quad_indices[0],
11546 quad_indices[18]});
11547 new_hexes[3]->set_bounding_object_indices(
11548 {quad_indices[7],
11549 quad_indices[11],
11550 quad_indices[3],
11551 quad_indices[15],
11552 quad_indices[1],
11553 quad_indices[19]});
11554 break;
11555 }
11556
11558 {
11559 //----------------------------
11560 //
11561 // RefinementCase<dim>::cut_xyz
11562 // isotropic refinement
11563 //
11564 // the refined cube will look
11565 // like this:
11566 //
11567 // *----*----*
11568 // / / /|
11569 // *----*----* |
11570 // / / /| *
11571 // *----*----* |/|
11572 // | | | * |
11573 // | | |/| *
11574 // *----*----* |/
11575 // | | | *
11576 // | | |/
11577 // *----*----*
11578 //
11579
11580 // find the next unused vertex and set it
11581 // appropriately
11582 while (
11583 triangulation.vertices_used[next_unused_vertex] ==
11584 true)
11585 ++next_unused_vertex;
11586 Assert(
11587 next_unused_vertex < triangulation.vertices.size(),
11588 ExcMessage(
11589 "Internal error: During refinement, the triangulation wants to access an element of the 'vertices' array but it turns out that the array is not large enough."));
11590 triangulation.vertices_used[next_unused_vertex] =
11591 true;
11592
11593 // the new vertex is definitely in the interior,
11594 // so we need not worry about the
11595 // boundary. However we need to worry about
11596 // Manifolds. Let the cell compute its own
11597 // center, by querying the underlying manifold
11598 // object.
11599 triangulation.vertices[next_unused_vertex] =
11600 hex->center(true, true);
11601
11602 // set the data of the six lines. first collect
11603 // the indices of the seven vertices (consider
11604 // the two planes to be crossed to form the
11605 // planes cutting the hex in two vertically and
11606 // horizontally)
11607 //
11608 // *--3--* *--5--*
11609 // / / / | | |
11610 // 0--6--1 0--6--1
11611 // / / / | | |
11612 // *--2--* *--4--*
11613 // the lines are numbered
11614 // as follows:
11615 // *--*--* *--*--*
11616 // / 1 / | 5 |
11617 // *2-*-3* *2-*-3*
11618 // / 0 / | 4 |
11619 // *--*--* *--*--*
11620 //
11621 const unsigned int vertex_indices[7] = {
11622 middle_vertex_index<dim, spacedim>(hex->face(0)),
11623 middle_vertex_index<dim, spacedim>(hex->face(1)),
11624 middle_vertex_index<dim, spacedim>(hex->face(2)),
11625 middle_vertex_index<dim, spacedim>(hex->face(3)),
11626 middle_vertex_index<dim, spacedim>(hex->face(4)),
11627 middle_vertex_index<dim, spacedim>(hex->face(5)),
11628 next_unused_vertex};
11629
11630 new_lines[0]->set_bounding_object_indices(
11632 new_lines[1]->set_bounding_object_indices(
11634 new_lines[2]->set_bounding_object_indices(
11636 new_lines[3]->set_bounding_object_indices(
11638 new_lines[4]->set_bounding_object_indices(
11640 new_lines[5]->set_bounding_object_indices(
11642
11643 // again, first collect some data about the
11644 // indices of the lines, with the following
11645 // numbering: (note that face 0 and 1 each are
11646 // shown twice for better readability)
11647
11648 // face 0: left plane
11649 // * *
11650 // /| /|
11651 // * | * |
11652 // /| * /| *
11653 // * 1/| * |3|
11654 // | * | | * |
11655 // |/| * |2| *
11656 // * 0/ * |/
11657 // | * | *
11658 // |/ |/
11659 // * *
11660 // face 1: right plane
11661 // * *
11662 // /| /|
11663 // * | * |
11664 // /| * /| *
11665 // * 5/| * |7|
11666 // | * | | * |
11667 // |/| * |6| *
11668 // * 4/ * |/
11669 // | * | *
11670 // |/ |/
11671 // * *
11672 // face 2: front plane
11673 // (note: x,y exchanged)
11674 // *---*---*
11675 // | 11 |
11676 // *-8-*-9-*
11677 // | 10 |
11678 // *---*---*
11679 // face 3: back plane
11680 // (note: x,y exchanged)
11681 // *---*---*
11682 // | 15 |
11683 // *12-*-13*
11684 // | 14 |
11685 // *---*---*
11686 // face 4: bottom plane
11687 // *---*---*
11688 // / 17 /
11689 // *18-*-19*
11690 // / 16 /
11691 // *---*---*
11692 // face 5: top plane
11693 // *---*---*
11694 // / 21 /
11695 // *22-*-23*
11696 // / 20 /
11697 // *---*---*
11698 // middle planes
11699 // *---*---* *---*---*
11700 // / 25 / | 29 |
11701 // *26-*-27* *26-*-27*
11702 // / 24 / | 28 |
11703 // *---*---* *---*---*
11704
11705 // set up a list of line iterators first. from
11706 // this, construct lists of line_indices and
11707 // line orientations later on
11708 const typename Triangulation<
11709 dim,
11710 spacedim>::raw_line_iterator lines[30] = {
11711 hex->face(0)
11712 ->isotropic_child(
11714 0, f_or[0], f_fl[0], f_ro[0]))
11715 ->line(
11717 1, f_or[0], f_fl[0], f_ro[0])), // 0
11718 hex->face(0)
11719 ->isotropic_child(
11721 3, f_or[0], f_fl[0], f_ro[0]))
11722 ->line(
11724 0, f_or[0], f_fl[0], f_ro[0])), // 1
11725 hex->face(0)
11726 ->isotropic_child(
11728 0, f_or[0], f_fl[0], f_ro[0]))
11729 ->line(
11731 3, f_or[0], f_fl[0], f_ro[0])), // 2
11732 hex->face(0)
11733 ->isotropic_child(
11735 3, f_or[0], f_fl[0], f_ro[0]))
11736 ->line(
11738 2, f_or[0], f_fl[0], f_ro[0])), // 3
11739
11740 hex->face(1)
11741 ->isotropic_child(
11743 0, f_or[1], f_fl[1], f_ro[1]))
11744 ->line(
11746 1, f_or[1], f_fl[1], f_ro[1])), // 4
11747 hex->face(1)
11748 ->isotropic_child(
11750 3, f_or[1], f_fl[1], f_ro[1]))
11751 ->line(
11753 0, f_or[1], f_fl[1], f_ro[1])), // 5
11754 hex->face(1)
11755 ->isotropic_child(
11757 0, f_or[1], f_fl[1], f_ro[1]))
11758 ->line(
11760 3, f_or[1], f_fl[1], f_ro[1])), // 6
11761 hex->face(1)
11762 ->isotropic_child(
11764 3, f_or[1], f_fl[1], f_ro[1]))
11765 ->line(
11767 2, f_or[1], f_fl[1], f_ro[1])), // 7
11768
11769 hex->face(2)
11770 ->isotropic_child(
11772 0, f_or[2], f_fl[2], f_ro[2]))
11773 ->line(
11775 1, f_or[2], f_fl[2], f_ro[2])), // 8
11776 hex->face(2)
11777 ->isotropic_child(
11779 3, f_or[2], f_fl[2], f_ro[2]))
11780 ->line(
11782 0, f_or[2], f_fl[2], f_ro[2])), // 9
11783 hex->face(2)
11784 ->isotropic_child(
11786 0, f_or[2], f_fl[2], f_ro[2]))
11787 ->line(
11789 3, f_or[2], f_fl[2], f_ro[2])), // 10
11790 hex->face(2)
11791 ->isotropic_child(
11793 3, f_or[2], f_fl[2], f_ro[2]))
11794 ->line(
11796 2, f_or[2], f_fl[2], f_ro[2])), // 11
11797
11798 hex->face(3)
11799 ->isotropic_child(
11801 0, f_or[3], f_fl[3], f_ro[3]))
11802 ->line(
11804 1, f_or[3], f_fl[3], f_ro[3])), // 12
11805 hex->face(3)
11806 ->isotropic_child(
11808 3, f_or[3], f_fl[3], f_ro[3]))
11809 ->line(
11811 0, f_or[3], f_fl[3], f_ro[3])), // 13
11812 hex->face(3)
11813 ->isotropic_child(
11815 0, f_or[3], f_fl[3], f_ro[3]))
11816 ->line(
11818 3, f_or[3], f_fl[3], f_ro[3])), // 14
11819 hex->face(3)
11820 ->isotropic_child(
11822 3, f_or[3], f_fl[3], f_ro[3]))
11823 ->line(
11825 2, f_or[3], f_fl[3], f_ro[3])), // 15
11826
11827 hex->face(4)
11828 ->isotropic_child(
11830 0, f_or[4], f_fl[4], f_ro[4]))
11831 ->line(
11833 1, f_or[4], f_fl[4], f_ro[4])), // 16
11834 hex->face(4)
11835 ->isotropic_child(
11837 3, f_or[4], f_fl[4], f_ro[4]))
11838 ->line(
11840 0, f_or[4], f_fl[4], f_ro[4])), // 17
11841 hex->face(4)
11842 ->isotropic_child(
11844 0, f_or[4], f_fl[4], f_ro[4]))
11845 ->line(
11847 3, f_or[4], f_fl[4], f_ro[4])), // 18
11848 hex->face(4)
11849 ->isotropic_child(
11851 3, f_or[4], f_fl[4], f_ro[4]))
11852 ->line(
11854 2, f_or[4], f_fl[4], f_ro[4])), // 19
11855
11856 hex->face(5)
11857 ->isotropic_child(
11859 0, f_or[5], f_fl[5], f_ro[5]))
11860 ->line(
11862 1, f_or[5], f_fl[5], f_ro[5])), // 20
11863 hex->face(5)
11864 ->isotropic_child(
11866 3, f_or[5], f_fl[5], f_ro[5]))
11867 ->line(
11869 0, f_or[5], f_fl[5], f_ro[5])), // 21
11870 hex->face(5)
11871 ->isotropic_child(
11873 0, f_or[5], f_fl[5], f_ro[5]))
11874 ->line(
11876 3, f_or[5], f_fl[5], f_ro[5])), // 22
11877 hex->face(5)
11878 ->isotropic_child(
11880 3, f_or[5], f_fl[5], f_ro[5]))
11881 ->line(
11883 2, f_or[5], f_fl[5], f_ro[5])), // 23
11884
11885 new_lines[0], // 24
11886 new_lines[1], // 25
11887 new_lines[2], // 26
11888 new_lines[3], // 27
11889 new_lines[4], // 28
11890 new_lines[5] // 29
11891 };
11892
11893 int line_indices[30];
11894 for (unsigned int i = 0; i < 30; ++i)
11895 line_indices[i] = lines[i]->index();
11896
11897 // the orientation of lines for the inner quads
11898 // is quite tricky. as these lines are newly
11899 // created ones and thus have no parents, they
11900 // cannot inherit this property. set up an array
11901 // and fill it with the respective values
11902 types::geometric_orientation line_orientation[30]{};
11903
11904 // note: for the first 24 lines (inner lines of
11905 // the outer quads) the following holds: the
11906 // second vertex of the even lines in standard
11907 // orientation is the vertex in the middle of
11908 // the quad, whereas for odd lines the first
11909 // vertex is the same middle vertex.
11910 for (unsigned int i = 0; i < 24; ++i)
11911 if (lines[i]->vertex_index((i + 1) % 2) ==
11912 vertex_indices[i / 4])
11913 line_orientation[i] =
11915 else
11916 {
11917 // it must be the other way
11918 // round then
11919 Assert(lines[i]->vertex_index(i % 2) ==
11920 vertex_indices[i / 4],
11922 line_orientation[i] =
11924 }
11925 // for the last 6 lines the line orientation is
11926 // always true, since they were just constructed
11927 // that way
11928 for (unsigned int i = 24; i < 30; ++i)
11929 line_orientation[i] =
11931
11932 // set up the 12 quads, numbered as follows
11933 // (left quad numbering, right line numbering
11934 // extracted from above)
11935 //
11936 // * *
11937 // /| 21|
11938 // * | * 15
11939 // y/|3* 20| *
11940 // * |/| * |/|
11941 // |2* |x 11 * 14
11942 // |/|1* |/| *
11943 // * |/ * |17
11944 // |0* 10 *
11945 // |/ |16
11946 // * *
11947 //
11948 // x
11949 // *---*---* *22-*-23*
11950 // | 5 | 7 | 1 29 5
11951 // *---*---* *26-*-27*
11952 // | 4 | 6 | 0 28 4
11953 // *---*---*y *18-*-19*
11954 //
11955 // y
11956 // *----*----* *-12-*-13-*
11957 // / 10 / 11 / 3 25 7
11958 // *----*----* *-26-*-27-*
11959 // / 8 / 9 / 2 24 6
11960 // *----*----*x *--8-*--9-*
11961
11962 new_quads[0]->set_bounding_object_indices(
11963 {line_indices[10],
11964 line_indices[28],
11965 line_indices[16],
11966 line_indices[24]});
11967 new_quads[1]->set_bounding_object_indices(
11968 {line_indices[28],
11969 line_indices[14],
11970 line_indices[17],
11971 line_indices[25]});
11972 new_quads[2]->set_bounding_object_indices(
11973 {line_indices[11],
11974 line_indices[29],
11975 line_indices[24],
11976 line_indices[20]});
11977 new_quads[3]->set_bounding_object_indices(
11978 {line_indices[29],
11979 line_indices[15],
11980 line_indices[25],
11981 line_indices[21]});
11982 new_quads[4]->set_bounding_object_indices(
11983 {line_indices[18],
11984 line_indices[26],
11985 line_indices[0],
11986 line_indices[28]});
11987 new_quads[5]->set_bounding_object_indices(
11988 {line_indices[26],
11989 line_indices[22],
11990 line_indices[1],
11991 line_indices[29]});
11992 new_quads[6]->set_bounding_object_indices(
11993 {line_indices[19],
11994 line_indices[27],
11995 line_indices[28],
11996 line_indices[4]});
11997 new_quads[7]->set_bounding_object_indices(
11998 {line_indices[27],
11999 line_indices[23],
12000 line_indices[29],
12001 line_indices[5]});
12002 new_quads[8]->set_bounding_object_indices(
12003 {line_indices[2],
12004 line_indices[24],
12005 line_indices[8],
12006 line_indices[26]});
12007 new_quads[9]->set_bounding_object_indices(
12008 {line_indices[24],
12009 line_indices[6],
12010 line_indices[9],
12011 line_indices[27]});
12012 new_quads[10]->set_bounding_object_indices(
12013 {line_indices[3],
12014 line_indices[25],
12015 line_indices[26],
12016 line_indices[12]});
12017 new_quads[11]->set_bounding_object_indices(
12018 {line_indices[25],
12019 line_indices[7],
12020 line_indices[27],
12021 line_indices[13]});
12022
12023 // now reset the line_orientation flags of outer
12024 // lines as they cannot be set in a loop (at
12025 // least not easily)
12026 new_quads[0]->set_line_orientation(
12027 0, line_orientation[10]);
12028 new_quads[0]->set_line_orientation(
12029 2, line_orientation[16]);
12030
12031 new_quads[1]->set_line_orientation(
12032 1, line_orientation[14]);
12033 new_quads[1]->set_line_orientation(
12034 2, line_orientation[17]);
12035
12036 new_quads[2]->set_line_orientation(
12037 0, line_orientation[11]);
12038 new_quads[2]->set_line_orientation(
12039 3, line_orientation[20]);
12040
12041 new_quads[3]->set_line_orientation(
12042 1, line_orientation[15]);
12043 new_quads[3]->set_line_orientation(
12044 3, line_orientation[21]);
12045
12046 new_quads[4]->set_line_orientation(
12047 0, line_orientation[18]);
12048 new_quads[4]->set_line_orientation(
12049 2, line_orientation[0]);
12050
12051 new_quads[5]->set_line_orientation(
12052 1, line_orientation[22]);
12053 new_quads[5]->set_line_orientation(
12054 2, line_orientation[1]);
12055
12056 new_quads[6]->set_line_orientation(
12057 0, line_orientation[19]);
12058 new_quads[6]->set_line_orientation(
12059 3, line_orientation[4]);
12060
12061 new_quads[7]->set_line_orientation(
12062 1, line_orientation[23]);
12063 new_quads[7]->set_line_orientation(
12064 3, line_orientation[5]);
12065
12066 new_quads[8]->set_line_orientation(
12067 0, line_orientation[2]);
12068 new_quads[8]->set_line_orientation(
12069 2, line_orientation[8]);
12070
12071 new_quads[9]->set_line_orientation(
12072 1, line_orientation[6]);
12073 new_quads[9]->set_line_orientation(
12074 2, line_orientation[9]);
12075
12076 new_quads[10]->set_line_orientation(
12077 0, line_orientation[3]);
12078 new_quads[10]->set_line_orientation(
12079 3, line_orientation[12]);
12080
12081 new_quads[11]->set_line_orientation(
12082 1, line_orientation[7]);
12083 new_quads[11]->set_line_orientation(
12084 3, line_orientation[13]);
12085
12086 //-------------------------------
12087 // create the eight new hexes
12088 //
12089 // again first collect some data. here, we need
12090 // the indices of a whole lotta quads.
12091
12092 // the quads are numbered as follows:
12093 //
12094 // planes in the interior of the old hex:
12095 //
12096 // *
12097 // /|
12098 // * |
12099 // /|3* *---*---* *----*----*
12100 // * |/| | 5 | 7 | / 10 / 11 /
12101 // |2* | *---*---* *----*----*
12102 // |/|1* | 4 | 6 | / 8 / 9 /
12103 // * |/ *---*---*y *----*----*x
12104 // |0*
12105 // |/
12106 // *
12107 //
12108 // children of the faces
12109 // of the old hex
12110 // *-------* *-------*
12111 // /|25 27| /34 35/|
12112 // 15| | / /19
12113 // / | | /32 33/ |
12114 // * |24 26| *-------*18 |
12115 // 1413*-------* |21 23| 17*
12116 // | /30 31/ | | /
12117 // 12/ / | |16
12118 // |/28 29/ |20 22|/
12119 // *-------* *-------*
12120 //
12121 // note that we have to
12122 // take care of the
12123 // orientation of
12124 // faces.
12125 const int quad_indices[36] = {
12126 new_quads[0]->index(), // 0
12127 new_quads[1]->index(),
12128 new_quads[2]->index(),
12129 new_quads[3]->index(),
12130 new_quads[4]->index(),
12131 new_quads[5]->index(),
12132 new_quads[6]->index(),
12133 new_quads[7]->index(),
12134 new_quads[8]->index(),
12135 new_quads[9]->index(),
12136 new_quads[10]->index(),
12137 new_quads[11]->index(), // 11
12138
12139 hex->face(0)->isotropic_child_index(
12141 0, f_or[0], f_fl[0], f_ro[0])), // 12
12142 hex->face(0)->isotropic_child_index(
12144 1, f_or[0], f_fl[0], f_ro[0])),
12145 hex->face(0)->isotropic_child_index(
12147 2, f_or[0], f_fl[0], f_ro[0])),
12148 hex->face(0)->isotropic_child_index(
12150 3, f_or[0], f_fl[0], f_ro[0])),
12151
12152 hex->face(1)->isotropic_child_index(
12154 0, f_or[1], f_fl[1], f_ro[1])), // 16
12155 hex->face(1)->isotropic_child_index(
12157 1, f_or[1], f_fl[1], f_ro[1])),
12158 hex->face(1)->isotropic_child_index(
12160 2, f_or[1], f_fl[1], f_ro[1])),
12161 hex->face(1)->isotropic_child_index(
12163 3, f_or[1], f_fl[1], f_ro[1])),
12164
12165 hex->face(2)->isotropic_child_index(
12167 0, f_or[2], f_fl[2], f_ro[2])), // 20
12168 hex->face(2)->isotropic_child_index(
12170 1, f_or[2], f_fl[2], f_ro[2])),
12171 hex->face(2)->isotropic_child_index(
12173 2, f_or[2], f_fl[2], f_ro[2])),
12174 hex->face(2)->isotropic_child_index(
12176 3, f_or[2], f_fl[2], f_ro[2])),
12177
12178 hex->face(3)->isotropic_child_index(
12180 0, f_or[3], f_fl[3], f_ro[3])), // 24
12181 hex->face(3)->isotropic_child_index(
12183 1, f_or[3], f_fl[3], f_ro[3])),
12184 hex->face(3)->isotropic_child_index(
12186 2, f_or[3], f_fl[3], f_ro[3])),
12187 hex->face(3)->isotropic_child_index(
12189 3, f_or[3], f_fl[3], f_ro[3])),
12190
12191 hex->face(4)->isotropic_child_index(
12193 0, f_or[4], f_fl[4], f_ro[4])), // 28
12194 hex->face(4)->isotropic_child_index(
12196 1, f_or[4], f_fl[4], f_ro[4])),
12197 hex->face(4)->isotropic_child_index(
12199 2, f_or[4], f_fl[4], f_ro[4])),
12200 hex->face(4)->isotropic_child_index(
12202 3, f_or[4], f_fl[4], f_ro[4])),
12203
12204 hex->face(5)->isotropic_child_index(
12206 0, f_or[5], f_fl[5], f_ro[5])), // 32
12207 hex->face(5)->isotropic_child_index(
12209 1, f_or[5], f_fl[5], f_ro[5])),
12210 hex->face(5)->isotropic_child_index(
12212 2, f_or[5], f_fl[5], f_ro[5])),
12213 hex->face(5)->isotropic_child_index(
12215 3, f_or[5], f_fl[5], f_ro[5]))};
12216
12217 // bottom children
12218 new_hexes[0]->set_bounding_object_indices(
12219 {quad_indices[12],
12220 quad_indices[0],
12221 quad_indices[20],
12222 quad_indices[4],
12223 quad_indices[28],
12224 quad_indices[8]});
12225 new_hexes[1]->set_bounding_object_indices(
12226 {quad_indices[0],
12227 quad_indices[16],
12228 quad_indices[22],
12229 quad_indices[6],
12230 quad_indices[29],
12231 quad_indices[9]});
12232 new_hexes[2]->set_bounding_object_indices(
12233 {quad_indices[13],
12234 quad_indices[1],
12235 quad_indices[4],
12236 quad_indices[24],
12237 quad_indices[30],
12238 quad_indices[10]});
12239 new_hexes[3]->set_bounding_object_indices(
12240 {quad_indices[1],
12241 quad_indices[17],
12242 quad_indices[6],
12243 quad_indices[26],
12244 quad_indices[31],
12245 quad_indices[11]});
12246
12247 // top children
12248 new_hexes[4]->set_bounding_object_indices(
12249 {quad_indices[14],
12250 quad_indices[2],
12251 quad_indices[21],
12252 quad_indices[5],
12253 quad_indices[8],
12254 quad_indices[32]});
12255 new_hexes[5]->set_bounding_object_indices(
12256 {quad_indices[2],
12257 quad_indices[18],
12258 quad_indices[23],
12259 quad_indices[7],
12260 quad_indices[9],
12261 quad_indices[33]});
12262 new_hexes[6]->set_bounding_object_indices(
12263 {quad_indices[15],
12264 quad_indices[3],
12265 quad_indices[5],
12266 quad_indices[25],
12267 quad_indices[10],
12268 quad_indices[34]});
12269 new_hexes[7]->set_bounding_object_indices(
12270 {quad_indices[3],
12271 quad_indices[19],
12272 quad_indices[7],
12273 quad_indices[27],
12274 quad_indices[11],
12275 quad_indices[35]});
12276 break;
12277 }
12278 default:
12279 // all refinement cases have been treated, there
12280 // only remains
12281 // RefinementCase<dim>::no_refinement as
12282 // untreated enumeration value. However, in that
12283 // case we should have aborted much
12284 // earlier. thus we should never get here
12286 break;
12287 } // switch (ref_case)
12288
12289 // and set face orientation flags. note that new
12290 // faces in the interior of the mother cell always
12291 // have a correctly oriented face, but the ones on
12292 // the outer faces will inherit this flag
12293 //
12294 // the flag have been set to true for all faces
12295 // initially, now go the other way round and reset
12296 // faces that are at the boundary of the mother cube
12297 //
12298 // the same is true for the face_flip and
12299 // face_rotation flags. however, the latter two are
12300 // set to false by default as this is the standard
12301 // value
12302
12303 // loop over all faces and all (relevant) subfaces
12304 // of that in order to set the correct values for
12305 // face_orientation, face_flip and face_rotation,
12306 // which are inherited from the corresponding face
12307 // of the mother cube
12308 for (const unsigned int f : GeometryInfo<dim>::face_indices())
12309 for (unsigned int s = 0;
12312 ref_case, f)),
12313 1U);
12314 ++s)
12315 {
12316 const unsigned int current_child =
12318 ref_case,
12319 f,
12320 s,
12321 f_or[f],
12322 f_fl[f],
12323 f_ro[f],
12325 ref_case, f, f_or[f], f_fl[f], f_ro[f]));
12326 new_hexes[current_child]->set_combined_face_orientation(
12327 f, f_co[f]);
12328 }
12329
12330 // now see if we have created cells that are
12331 // distorted and if so add them to our list
12332 if (check_for_distorted_cells &&
12333 has_distorted_children<dim, spacedim>(hex))
12334 cells_with_distorted_children.distorted_cells.push_back(
12335 hex);
12336
12337 // note that the refinement flag was already cleared
12338 // at the beginning of this loop
12339
12340 // inform all listeners that cell refinement is done
12341 triangulation.signals.post_refinement_on_cell(hex);
12342 }
12343 }
12344
12345 // clear user data on quads. we used some of this data to
12346 // indicate anisotropic refinemnt cases on faces. all data
12347 // should be cleared by now, but the information whether we
12348 // used indices or pointers is still present. reset it now to
12349 // enable the user to use whichever they like later on.
12350 triangulation.faces->quads.clear_user_data();
12351
12352 // return the list with distorted children
12353 return cells_with_distorted_children;
12354 }
12355
12356
12369 template <int spacedim>
12370 static void
12373
12374
12375
12376 template <int dim, int spacedim>
12377 static void
12379 Triangulation<dim, spacedim> &triangulation)
12380 {
12381 // If the codimension is one, we cannot perform this check
12382 // yet.
12383 if (spacedim > dim)
12384 return;
12385
12386 for (const auto &cell : triangulation.cell_iterators())
12387 if (cell->at_boundary() && cell->refine_flag_set() &&
12388 cell->refine_flag_set() !=
12390 {
12391 // The cell is at the boundary and it is flagged for
12392 // anisotropic refinement. Therefore, we have a closer
12393 // look
12394 const RefinementCase<dim> ref_case = cell->refine_flag_set();
12395 for (const unsigned int face_no :
12397 if (cell->face(face_no)->at_boundary())
12398 {
12399 // this is the critical face at the boundary.
12401 face_no) !=
12403 {
12404 // up to now, we do not want to refine this
12405 // cell along the face under consideration
12406 // here.
12407 const typename Triangulation<dim,
12408 spacedim>::face_iterator
12409 face = cell->face(face_no);
12410 // the new point on the boundary would be this
12411 // one.
12412 const Point<spacedim> new_bound = face->center(true);
12413 // to check it, transform to the unit cell
12414 // with a linear mapping
12415 const Point<dim> new_unit =
12416 cell->reference_cell()
12417 .template get_default_linear_mapping<spacedim>()
12418 .transform_real_to_unit_cell(cell, new_bound);
12419
12420 // Now, we have to calculate the distance from
12421 // the face in the unit cell.
12422
12423 // take the correct coordinate direction (0
12424 // for faces 0 and 1, 1 for faces 2 and 3, 2
12425 // for faces 4 and 5) and subtract the correct
12426 // boundary value of the face (0 for faces 0,
12427 // 2, and 4; 1 for faces 1, 3 and 5)
12428 const double dist =
12429 std::fabs(new_unit[face_no / 2] - face_no % 2);
12430
12431 // compare this with the empirical value
12432 // allowed. if it is too big, flag the face
12433 // for isotropic refinement
12434 const double allowed = 0.25;
12435
12436 if (dist > allowed)
12437 cell->flag_for_face_refinement(face_no);
12438 } // if flagged for anistropic refinement
12439 } // if (cell->face(face)->at_boundary())
12440 } // for all cells
12441 }
12442
12443
12456 template <int dim, int spacedim>
12457 static void
12459 {
12460 Assert(dim < 3,
12461 ExcMessage("Wrong function called -- there should "
12462 "be a specialization."));
12463 }
12464
12465
12466 template <int spacedim>
12467 static void
12469 Triangulation<3, spacedim> &triangulation)
12470 {
12471 const unsigned int dim = 3;
12472 using raw_line_iterator =
12474
12475 // variable to store whether the mesh was changed in the
12476 // present loop and in the whole process
12477 bool mesh_changed = false;
12478
12479 do
12480 {
12481 mesh_changed = false;
12482
12483 // for this following, we need to know which cells are
12484 // going to be coarsened, if we had to make a
12485 // decision. the following function sets these flags:
12486 triangulation.fix_coarsen_flags();
12487
12488 // first clear flags on lines, since we need them to determine
12489 // which lines will be refined
12490 triangulation.clear_user_flags_line();
12491
12492 // flag those lines that are refined and will not be
12493 // coarsened and those that will be refined
12494 for (const auto &cell : triangulation.cell_iterators())
12495 if (cell->refine_flag_set())
12496 {
12497 const std::array<unsigned int, 12> line_indices =
12498 TriaAccessorImplementation::Implementation::
12499 get_line_indices_of_cell(*cell);
12500 for (unsigned int l = 0; l < cell->n_lines(); ++l)
12502 cell->refine_flag_set(), l) ==
12504 {
12505 raw_line_iterator line(&triangulation,
12506 0,
12507 line_indices[l]);
12508 // flag a line, that will be refined
12509 line->set_user_flag();
12510 }
12511 }
12512 else if (cell->has_children() &&
12513 !cell->child(0)->coarsen_flag_set())
12514 {
12515 const std::array<unsigned int, 12> line_indices =
12516 TriaAccessorImplementation::Implementation::
12517 get_line_indices_of_cell(*cell);
12518 for (unsigned int l = 0; l < cell->n_lines(); ++l)
12520 cell->refinement_case(), l) ==
12522 {
12523 raw_line_iterator line(&triangulation,
12524 0,
12525 line_indices[l]);
12526 // flag a line, that is refined and will stay so
12527 line->set_user_flag();
12528 }
12529 }
12530 else if (cell->has_children() &&
12531 cell->child(0)->coarsen_flag_set())
12532 cell->set_user_flag();
12533
12534
12535 // now check whether there are cells with lines that are
12536 // more than once refined or that will be more than once
12537 // refined. The first thing should never be the case, in
12538 // the second case we flag the cell for refinement
12540 cell = triangulation.last_active();
12541 cell != triangulation.end();
12542 --cell)
12543 {
12544 const std::array<unsigned int, 12> line_indices =
12545 TriaAccessorImplementation::Implementation::
12546 get_line_indices_of_cell(*cell);
12547 for (unsigned int l = 0; l < cell->n_lines(); ++l)
12548 {
12549 raw_line_iterator line(&triangulation, 0, line_indices[l]);
12550 if (line->has_children())
12551 {
12552 // if this line is refined, its children should
12553 // not have further children
12554 //
12555 // however, if any of the children is flagged
12556 // for further refinement, we need to refine
12557 // this cell also (at least, if the cell is not
12558 // already flagged)
12559 bool offending_line_found = false;
12560
12561 for (unsigned int c = 0; c < 2; ++c)
12562 {
12563 Assert(line->child(c)->has_children() == false,
12565
12566 if (line->child(c)->user_flag_set() &&
12568 cell->refine_flag_set(), l) ==
12570 {
12571 // tag this cell for refinement
12572 cell->clear_coarsen_flag();
12573 // if anisotropic coarsening is allowed:
12574 // extend the refine_flag in the needed
12575 // direction, else set refine_flag
12576 // (isotropic)
12577 if (triangulation.smooth_grid &
12579 allow_anisotropic_smoothing)
12580 cell->flag_for_line_refinement(l);
12581 else
12582 cell->set_refine_flag();
12583
12584 for (unsigned int k = 0; k < cell->n_lines();
12585 ++k)
12587 cell->refine_flag_set(), l) ==
12589 // flag a line, that will be refined
12590 raw_line_iterator(&triangulation,
12591 0,
12592 line_indices[k])
12593 ->set_user_flag();
12594
12595 // note that we have changed the grid
12596 offending_line_found = true;
12597
12598 // it may save us several loop
12599 // iterations if we flag all lines of
12600 // this cell now (and not at the outset
12601 // of the next iteration) for refinement
12602 for (unsigned int k = 0; k < cell->n_lines();
12603 ++k)
12604 {
12605 const auto line =
12606 raw_line_iterator(&triangulation,
12607 0,
12608 line_indices[k]);
12609 if (!line->has_children() &&
12611 line_refinement_case(
12612 cell->refine_flag_set(), k) !=
12614 line->set_user_flag();
12615 }
12616
12617 break;
12618 }
12619 }
12620
12621 if (offending_line_found)
12622 {
12623 mesh_changed = true;
12624 break;
12625 }
12626 }
12627 }
12628 }
12629
12630
12631 // there is another thing here: if any of the lines will
12632 // be refined, then we may not coarsen the present cell
12633 // similarly, if any of the lines *is* already refined, we
12634 // may not coarsen the current cell. however, there's a
12635 // catch: if the line is refined, but the cell behind it
12636 // is going to be coarsened, then the situation
12637 // changes. if we forget this second condition, the
12638 // refine_and_coarsen_3d test will start to fail. note
12639 // that to know which cells are going to be coarsened, the
12640 // call for fix_coarsen_flags above is necessary
12642 triangulation.last();
12643 cell != triangulation.end();
12644 --cell)
12645 if (cell->user_flag_set())
12646 {
12647 const std::array<unsigned int, 12> line_indices =
12648 TriaAccessorImplementation::Implementation::
12649 get_line_indices_of_cell(*cell);
12650 for (unsigned int l = 0; l < cell->n_lines(); ++l)
12651 {
12652 raw_line_iterator line(&triangulation,
12653 0,
12654 line_indices[l]);
12655 if (line->has_children() &&
12656 (line->child(0)->user_flag_set() ||
12657 line->child(1)->user_flag_set()))
12658 {
12659 for (unsigned int c = 0; c < cell->n_children(); ++c)
12660 cell->child(c)->clear_coarsen_flag();
12661 cell->clear_user_flag();
12662 for (unsigned int k = 0; k < cell->n_lines(); ++k)
12664 cell->refinement_case(), k) ==
12666 // flag a line, that is refined and will
12667 // stay so
12668 raw_line_iterator(&triangulation,
12669 0,
12670 line_indices[k])
12671 ->set_user_flag();
12672 mesh_changed = true;
12673 break;
12674 }
12675 }
12676 }
12677 }
12678 while (mesh_changed == true);
12679 }
12680
12681
12682
12689 template <int dim, int spacedim>
12690 static bool
12693 {
12694 // in 1d, coarsening is always allowed since we don't enforce
12695 // the 2:1 constraint there
12696 if (dim == 1)
12697 return true;
12698
12699 const RefinementCase<dim> ref_case = cell->refinement_case();
12700 for (const unsigned int n : GeometryInfo<dim>::face_indices())
12701 {
12702 // if the cell is not refined along that face, coarsening
12703 // will not change anything, so do nothing. the same
12704 // applies, if the face is at the boundary
12705 const RefinementCase<dim - 1> face_ref_case =
12706 GeometryInfo<dim>::face_refinement_case(cell->refinement_case(),
12707 n);
12708
12709 const unsigned int n_subfaces =
12710 GeometryInfo<dim - 1>::n_children(face_ref_case);
12711
12712 if (n_subfaces == 0 || cell->at_boundary(n))
12713 continue;
12714 for (unsigned int c = 0; c < n_subfaces; ++c)
12715 {
12717 child = cell->child(
12719
12721 child_neighbor = child->neighbor(n);
12722 if (!child->neighbor_is_coarser(n))
12723 {
12724 // in 2d, if the child's neighbor is coarser, then it has
12725 // no children. however, in 3d it might be
12726 // otherwise. consider for example, that our face might be
12727 // refined with cut_x, but the neighbor is refined with
12728 // cut_xy at that face. then the neighbor pointers of the
12729 // children of our cell will point to the common neighbor
12730 // cell, not to its children. what we really want to know
12731 // in the following is, whether the neighbor cell is
12732 // refined twice with reference to our cell. that only
12733 // has to be asked, if the child's neighbor is not a
12734 // coarser one. we check whether some of the children on
12735 // the neighbor are not flagged for coarsening, in that
12736 // case we may not coarsen. it is enough to check the
12737 // first child because we have already fixed the coarsen
12738 // flags on finer levels
12739 if (child_neighbor->has_children() &&
12740 !(child_neighbor->child(0)->is_active() &&
12741 child_neighbor->child(0)->coarsen_flag_set()))
12742 return false;
12743
12744 // the same applies, if the neighbors children are not
12745 // refined but will be after refinement
12746 if (child_neighbor->refine_flag_set())
12747 return false;
12748 }
12749 }
12750 }
12751 return true;
12752 }
12753 };
12754
12755
12760 {
12761 template <int spacedim>
12762 static void
12765
12766 template <int dim, int spacedim>
12767 static void
12769 {
12770 std::vector<std::pair<int, int>> adjacent_cells(
12771 2 * triangulation.n_raw_faces(), {-1, -1});
12772
12773 const auto set_entry = [&](const auto &face_index, const auto &cell) {
12774 const std::pair<int, int> cell_pair = {cell->level(), cell->index()};
12775
12777 if (adjacent_cells[2 * face_index].first == -1 &&
12778 adjacent_cells[2 * face_index].second == -1)
12779 {
12780 index = 2 * face_index + 0;
12781 }
12782 else
12783 {
12784 Assert(((adjacent_cells[2 * face_index + 1].first == -1) &&
12785 (adjacent_cells[2 * face_index + 1].second == -1)),
12787 index = 2 * face_index + 1;
12788 }
12789
12790 adjacent_cells[index] = cell_pair;
12791 };
12792
12793 const auto get_entry =
12794 [&](const auto &face_index,
12795 const auto &cell) -> TriaIterator<CellAccessor<dim, spacedim>> {
12796 auto test = adjacent_cells[2 * face_index];
12797
12798 if (test == std::make_pair(cell->level(), cell->index()))
12799 test = adjacent_cells[2 * face_index + 1];
12800
12801 return TriaIterator<CellAccessor<dim, spacedim>>(&triangulation,
12802 test.first,
12803 test.second);
12804 };
12805
12806 for (const auto &cell : triangulation.cell_iterators())
12807 for (const auto &face : cell->face_iterators())
12808 {
12809 set_entry(face->index(), cell);
12810
12811 if (cell->is_active() && face->has_children())
12812 for (unsigned int c = 0; c < face->n_children(); ++c)
12813 set_entry(face->child(c)->index(), cell);
12814 }
12815
12816 for (const auto &cell : triangulation.cell_iterators())
12817 for (auto f : cell->face_indices())
12818 cell->set_neighbor(f, get_entry(cell->face(f)->index(), cell));
12819 }
12820
12821 template <int dim, int spacedim>
12822 static void
12824 Triangulation<dim, spacedim> & /*triangulation*/,
12826 std::vector<unsigned int> & /*line_cell_count*/,
12827 std::vector<unsigned int> & /*quad_cell_count*/)
12828 {
12830 }
12831
12832 template <int dim, int spacedim>
12835 const bool check_for_distorted_cells)
12836 {
12837 return Implementation::execute_refinement_isotropic(
12838 triangulation, check_for_distorted_cells);
12839 }
12840
12841 template <int dim, int spacedim>
12842 static void
12844 Triangulation<dim, spacedim> & /*triangulation*/)
12845 {
12846 // nothing to do since anisotropy is not supported
12847 }
12848
12849 template <int dim, int spacedim>
12850 static void
12852 Triangulation<dim, spacedim> &triangulation)
12853 {
12854 Implementation::prepare_refinement_dim_dependent(triangulation);
12855 }
12856
12857 template <int dim, int spacedim>
12858 static bool
12861 {
12863
12864 return false;
12865 }
12866 };
12867 } // namespace TriangulationImplementation
12868} // namespace internal
12869
12870#ifndef DOXYGEN
12871
12872template <int dim, int spacedim>
12875
12876
12877
12878template <int dim, int spacedim>
12881 const MeshSmoothing smooth_grid,
12882 const bool check_for_distorted_cells)
12883 : cell_attached_data({0, 0, {}, {}})
12884 , smooth_grid(smooth_grid)
12885 , default_flat_manifold(std::make_unique<const FlatManifold<dim, spacedim>>())
12886 , anisotropic_refinement(false)
12887 , check_for_distorted_cells(check_for_distorted_cells)
12888{
12889 if (dim == 1)
12890 {
12891 vertex_to_boundary_id_map_1d =
12892 std::make_unique<std::map<unsigned int, types::boundary_id>>();
12893 vertex_to_manifold_id_map_1d =
12894 std::make_unique<std::map<unsigned int, types::manifold_id>>();
12895 }
12896
12897 // connect the any_change signal to the other top level signals
12898 signals.create.connect(signals.any_change);
12899 signals.post_refinement.connect(signals.any_change);
12900 signals.clear.connect(signals.any_change);
12901 signals.mesh_movement.connect(signals.any_change);
12902}
12903
12904
12905
12906template <int dim, int spacedim>
12909 Triangulation<dim, spacedim> &&tria) noexcept
12910 : EnableObserverPointer(std::move(tria))
12911 , cell_attached_data(std::move(tria.cell_attached_data))
12912 , smooth_grid(tria.smooth_grid)
12913 , reference_cells(std::move(tria.reference_cells))
12914 , strides(tria.strides)
12915 , periodic_face_pairs_level_0(std::move(tria.periodic_face_pairs_level_0))
12916 , periodic_face_map(std::move(tria.periodic_face_map))
12917 , levels(std::move(tria.levels))
12918 , faces(std::move(tria.faces))
12919 , vertices(std::move(tria.vertices))
12920 , vertices_used(std::move(tria.vertices_used))
12921 , manifolds(std::move(tria.manifolds))
12922 , default_flat_manifold(std::make_unique<const FlatManifold<dim, spacedim>>())
12923 , anisotropic_refinement(tria.anisotropic_refinement)
12924 , check_for_distorted_cells(tria.check_for_distorted_cells)
12925 , number_cache(std::move(tria.number_cache))
12926 , vertex_to_boundary_id_map_1d(std::move(tria.vertex_to_boundary_id_map_1d))
12927 , vertex_to_manifold_id_map_1d(std::move(tria.vertex_to_manifold_id_map_1d))
12928{
12930 tria.strides = {};
12931
12932 if (tria.policy)
12933 this->policy = tria.policy->clone();
12934}
12935
12936
12937template <int dim, int spacedim>
12940 Triangulation<dim, spacedim> &&tria) noexcept
12941{
12942 EnableObserverPointer::operator=(std::move(tria));
12943
12944 cell_attached_data = std::move(tria.cell_attached_data);
12945 smooth_grid = tria.smooth_grid;
12946 reference_cells = std::move(tria.reference_cells);
12947 strides = tria.strides;
12948 periodic_face_pairs_level_0 = std::move(tria.periodic_face_pairs_level_0);
12949 periodic_face_map = std::move(tria.periodic_face_map);
12950 levels = std::move(tria.levels);
12951 faces = std::move(tria.faces);
12952 vertices = std::move(tria.vertices);
12953 vertices_used = std::move(tria.vertices_used);
12954 manifolds = std::move(tria.manifolds);
12955 anisotropic_refinement = tria.anisotropic_refinement;
12956 number_cache = tria.number_cache;
12957 vertex_to_boundary_id_map_1d = std::move(tria.vertex_to_boundary_id_map_1d);
12958 vertex_to_manifold_id_map_1d = std::move(tria.vertex_to_manifold_id_map_1d);
12959
12961 tria.strides = {};
12962
12963 if (tria.policy)
12964 this->policy = tria.policy->clone();
12965
12966 return *this;
12967}
12968
12969
12970
12971template <int dim, int spacedim>
12974{
12975 // notify listeners that the triangulation is going down...
12976 try
12977 {
12978 signals.clear();
12979 }
12980 catch (...)
12981 {}
12982
12983 levels.clear();
12984
12985 // the vertex_to_boundary_id_map_1d field should be unused except in
12986 // 1d. double check this here, as destruction is a good place to
12987 // ensure that what we've done over the course of the lifetime of
12988 // this object makes sense
12989 AssertNothrow((dim == 1) || (vertex_to_boundary_id_map_1d == nullptr),
12991
12992 // the vertex_to_manifold_id_map_1d field should be also unused
12993 // except in 1d. check this as well
12994 AssertNothrow((dim == 1) || (vertex_to_manifold_id_map_1d == nullptr),
12996}
12997
12998
12999
13000template <int dim, int spacedim>
13003{
13004 // notify listeners that the triangulation is going down...
13005 signals.clear();
13006
13007 // ...and then actually clear all content of it
13008 clear_despite_subscriptions();
13009 periodic_face_pairs_level_0.clear();
13010 periodic_face_map.clear();
13011 reference_cells.clear();
13012
13013 cell_attached_data = {0, 0, {}, {}};
13014 data_serializer.clear();
13015}
13016
13017template <int dim, int spacedim>
13020{
13021 return MPI_COMM_SELF;
13022}
13023
13024
13025
13026template <int dim, int spacedim>
13028std::weak_ptr<const Utilities::MPI::Partitioner> Triangulation<dim, spacedim>::
13030{
13031 return number_cache.active_cell_index_partitioner;
13032}
13033
13034
13035
13036template <int dim, int spacedim>
13038std::weak_ptr<const Utilities::MPI::Partitioner> Triangulation<dim, spacedim>::
13039 global_level_cell_index_partitioner(const unsigned int level) const
13040{
13041 AssertIndexRange(level, this->n_levels());
13042
13043 return number_cache.level_cell_index_partitioners[level];
13044}
13045
13046
13047
13048template <int dim, int spacedim>
13051 const MeshSmoothing mesh_smoothing)
13052{
13053 smooth_grid = mesh_smoothing;
13054}
13055
13056
13057
13058template <int dim, int spacedim>
13062{
13063 return smooth_grid;
13064}
13065
13066
13067
13068template <int dim, int spacedim>
13071 const types::manifold_id m_number,
13072 const Manifold<dim, spacedim> &manifold_object)
13073{
13075
13076 manifolds[m_number] = manifold_object.clone();
13077}
13078
13079
13080
13081template <int dim, int spacedim>
13084 const types::manifold_id m_number)
13085{
13087
13088 // delete the entry located at number.
13089 manifolds[m_number] = default_flat_manifold->clone();
13090}
13091
13092
13093template <int dim, int spacedim>
13096{
13097 for (auto &m : manifolds)
13098 m.second = default_flat_manifold->clone();
13099}
13100
13101
13102template <int dim, int spacedim>
13105 const types::manifold_id m_number)
13106{
13107 Assert(
13108 n_cells() > 0,
13109 ExcMessage(
13110 "Error: set_all_manifold_ids() can not be called on an empty Triangulation."));
13111
13112 for (const auto &cell : this->active_cell_iterators())
13113 cell->set_all_manifold_ids(m_number);
13114}
13115
13116
13117template <int dim, int spacedim>
13120 const types::manifold_id m_number)
13121{
13122 Assert(
13123 n_cells() > 0,
13124 ExcMessage(
13125 "Error: set_all_manifold_ids_on_boundary() can not be called on an empty Triangulation."));
13126
13127 for (const auto &cell : this->active_cell_iterators())
13128 for (auto f : GeometryInfo<dim>::face_indices())
13129 if (cell->face(f)->at_boundary())
13130 cell->face(f)->set_all_manifold_ids(m_number);
13131}
13132
13133
13134template <int dim, int spacedim>
13137 const types::boundary_id b_id,
13138 const types::manifold_id m_number)
13139{
13140 Assert(
13141 n_cells() > 0,
13142 ExcMessage(
13143 "Error: set_all_manifold_ids_on_boundary() can not be called on an empty Triangulation."));
13144
13145 bool boundary_found = false;
13146 for (const auto &cell : this->active_cell_iterators())
13147 {
13148 // loop on faces
13149 for (auto f : GeometryInfo<dim>::face_indices())
13150 if (cell->face(f)->at_boundary() &&
13151 cell->face(f)->boundary_id() == b_id)
13152 {
13153 boundary_found = true;
13154 cell->face(f)->set_manifold_id(m_number);
13155 }
13156
13157 // loop on edges if dim >= 3
13158 if (dim >= 3)
13159 for (unsigned int e = 0; e < GeometryInfo<dim>::lines_per_cell; ++e)
13160 if (cell->line(e)->at_boundary() &&
13161 cell->line(e)->boundary_id() == b_id)
13162 {
13163 boundary_found = true;
13164 cell->line(e)->set_manifold_id(m_number);
13165 }
13166 }
13167
13168 Assert(boundary_found, ExcBoundaryIdNotFound(b_id));
13169}
13170
13171
13172
13173template <int dim, int spacedim>
13176 const types::manifold_id m_number) const
13177{
13178 // check if flat manifold has been queried
13179 if (m_number == numbers::flat_manifold_id)
13180 return *default_flat_manifold;
13181
13182 // look, if there is a manifold stored at
13183 // manifold_id number.
13184 const auto it = manifolds.find(m_number);
13185
13186 if (it != manifolds.end())
13187 {
13188 // if we have found an entry, return it
13189 return *(it->second);
13190 }
13191
13192 Assert(
13193 false,
13194 ExcMessage(
13195 "No manifold of the manifold id " + std::to_string(m_number) +
13196 " has been attached to the triangulation. "
13197 "Please attach the right manifold with Triangulation::set_manifold()."));
13198
13199 return *default_flat_manifold; // never reached
13200}
13201
13202
13203
13204template <int dim, int spacedim>
13206std::vector<types::boundary_id> Triangulation<dim, spacedim>::get_boundary_ids()
13207 const
13208{
13209 std::set<types::boundary_id> boundary_ids;
13210 for (const auto &cell : active_cell_iterators())
13211 if (cell->is_locally_owned())
13212 for (const auto &face : cell->face_indices())
13213 if (cell->at_boundary(face))
13214 boundary_ids.insert(cell->face(face)->boundary_id());
13215
13216 return {boundary_ids.begin(), boundary_ids.end()};
13217}
13218
13219
13220
13221template <int dim, int spacedim>
13223std::vector<types::manifold_id> Triangulation<dim, spacedim>::get_manifold_ids()
13224 const
13225{
13226 std::set<types::manifold_id> m_ids;
13227 for (const auto &cell : active_cell_iterators())
13228 if (cell->is_locally_owned())
13229 {
13230 m_ids.insert(cell->manifold_id());
13231 for (const auto &face : cell->face_iterators())
13232 m_ids.insert(face->manifold_id());
13233 if (dim == 3)
13234 {
13235 const auto line_indices = internal::TriaAccessorImplementation::
13236 Implementation::get_line_indices_of_cell(*cell);
13237 for (unsigned int l = 0; l < cell->n_lines(); ++l)
13238 {
13239 raw_line_iterator line(this, 0, line_indices[l]);
13240 m_ids.insert(line->manifold_id());
13241 }
13242 }
13243 }
13244 return {m_ids.begin(), m_ids.end()};
13245}
13246
13247#endif
13248/*-----------------------------------------------------------------*/
13249
13250#ifndef DOXYGEN
13251
13252template <int dim, int spacedim>
13255 const Triangulation<dim, spacedim> &other_tria)
13256{
13257 Assert((vertices.empty()) && (levels.empty()) && (faces == nullptr),
13258 ExcTriangulationNotEmpty(vertices.size(), levels.size()));
13259 Assert((other_tria.levels.size() != 0) && (other_tria.vertices.size() != 0) &&
13260 (dim == 1 || other_tria.faces != nullptr),
13261 ExcMessage(
13262 "When calling Triangulation::copy_triangulation(), "
13263 "the target triangulation must be empty but the source "
13264 "triangulation (the argument to this function) must contain "
13265 "something. Here, it seems like the source does not "
13266 "contain anything at all."));
13267
13268
13269 // copy normal elements
13270 vertices = other_tria.vertices;
13271 vertices_used = other_tria.vertices_used;
13272 anisotropic_refinement = other_tria.anisotropic_refinement;
13273 smooth_grid = other_tria.smooth_grid;
13274 reference_cells = other_tria.reference_cells;
13275 strides = other_tria.strides;
13276
13277 if (dim > 1)
13278 faces =
13279 std::make_unique<internal::TriangulationImplementation::TriaFaces<dim>>(
13280 *other_tria.faces);
13281
13282 for (const auto &p : other_tria.manifolds)
13283 set_manifold(p.first, *p.second);
13284
13285
13286 Assert(other_tria.levels.size() <= numbers::max_n_levels, ExcInternalError());
13287 levels.reserve(other_tria.levels.size());
13288 for (const auto &level : other_tria.levels)
13289 levels.push_back(
13290 std::make_unique<
13291 internal::TriangulationImplementation::TriaLevel<dim, spacedim>>(
13292 *level));
13293
13294 number_cache = other_tria.number_cache;
13295
13296 if (dim == 1)
13297 {
13298 vertex_to_boundary_id_map_1d =
13299 std::make_unique<std::map<unsigned int, types::boundary_id>>(
13300 *other_tria.vertex_to_boundary_id_map_1d);
13301
13302 vertex_to_manifold_id_map_1d =
13303 std::make_unique<std::map<unsigned int, types::manifold_id>>(
13304 *other_tria.vertex_to_manifold_id_map_1d);
13305 }
13306
13307 if (other_tria.policy)
13308 this->policy = other_tria.policy->clone();
13309
13310 // periodic faces
13311 this->periodic_face_pairs_level_0.reserve(
13312 other_tria.periodic_face_pairs_level_0.size());
13313
13314 for (const auto &other_entry : other_tria.periodic_face_pairs_level_0)
13315 {
13316 auto entry = other_entry;
13317 entry.cell[0] =
13318 cell_iterator(this, entry.cell[0]->level(), entry.cell[0]->index());
13319 entry.cell[1] =
13320 cell_iterator(this, entry.cell[1]->level(), entry.cell[1]->index());
13321 periodic_face_pairs_level_0.emplace_back(entry);
13322 }
13323
13324 for (auto [first_cell_, second_cell_and_orientation] :
13325 other_tria.periodic_face_map)
13326 {
13327 auto first_cell = first_cell_; // make copy since key is const
13328 first_cell.first = cell_iterator(this,
13329 first_cell.first->level(),
13330 first_cell.first->index());
13331 second_cell_and_orientation.first.first =
13332 cell_iterator(this,
13333 second_cell_and_orientation.first.first->level(),
13334 second_cell_and_orientation.first.first->index());
13335
13336 this->periodic_face_map[first_cell] = second_cell_and_orientation;
13337 }
13338
13339 // inform those who are listening on other_tria of the copy operation
13340 other_tria.signals.copy(*this);
13341 // also inform all listeners of the current triangulation that the
13342 // triangulation has been created
13343 signals.create();
13344
13345 // note that we need not copy the
13346 // subscriptor!
13347}
13348
13349
13350
13351template <int dim, int spacedim>
13354{
13355 if (this->all_reference_cells_are_hyper_cube())
13356 {
13357 this->policy =
13359 dim,
13360 spacedim,
13362 }
13363 else
13364 {
13365 this->policy =
13367 dim,
13368 spacedim,
13370 }
13371}
13372
13373
13374
13375template <int dim, int spacedim>
13378 const std::vector<Point<spacedim>> &v,
13379 const std::vector<CellData<dim>> &cells,
13380 const SubCellData &subcelldata)
13381{
13382 Assert((vertices.empty()) && (levels.empty()) && (faces == nullptr),
13383 ExcTriangulationNotEmpty(vertices.size(), levels.size()));
13384 // check that no forbidden arrays
13385 // are used
13386 Assert(subcelldata.check_consistency(dim), ExcInternalError());
13387
13388 // try to create a triangulation; if this fails, we still want to
13389 // throw an exception but if we just do so we'll get into trouble
13390 // because sometimes other objects are already attached to it:
13391 try
13392 {
13394 create_triangulation(v, cells, subcelldata, *this);
13395 }
13396 catch (...)
13397 {
13398 clear_despite_subscriptions();
13399 throw;
13400 }
13401
13402 reset_policy();
13403
13404 // update our counts of the various elements of a triangulation, and set
13405 // active_cell_indices of all cells
13406 reset_cell_vertex_indices_cache();
13408 *this, levels.size(), number_cache);
13409 reset_active_cell_indices();
13410 reset_global_cell_indices();
13411
13412 // now verify that there are indeed no distorted cells. as per the
13413 // documentation of this class, we first collect all distorted cells
13414 // and then throw an exception if there are any
13415 if (check_for_distorted_cells)
13416 {
13417 DistortedCellList distorted_cells = collect_distorted_coarse_cells(*this);
13418 // throw the array (and fill the various location fields) if
13419 // there are distorted cells. otherwise, just fall off the end
13420 // of the function
13421 AssertThrow(distorted_cells.distorted_cells.empty(), distorted_cells);
13422 }
13423
13424
13425 /*
13426 When the triangulation is a manifold (dim < spacedim) and made of
13427 quadrilaterals, the normal field provided from the map class depends on
13428 the order of the vertices. It may happen that this normal field is
13429 discontinuous. The following code takes care that this is not the case by
13430 setting the cell direction flag on those cell that produce the wrong
13431 orientation.
13432
13433 To determine if 2 neighbors have the same or opposite orientation we use
13434 a truth table. Its entries are indexed by the local indices of the
13435 common face. For example if two elements share a face, and this face is
13436 face 0 for element 0 and face 1 for element 1, then table(0,1) will tell
13437 whether the orientation are the same (true) or opposite (false).
13438
13439 Even though there may be a combinatorial/graph theory argument to get this
13440 table in any dimension, I tested by hand all the different possible cases
13441 in 1D and 2D to generate the table.
13442
13443 Assuming that a surface respects the standard orientation for 2d meshes,
13444 the truth tables are symmetric and their true values are the following
13445
13446 - 1D curves: (0,1)
13447 - 2D surface: (0,1),(0,2),(1,3),(2,3)
13448
13449 We store this data using an n_faces x n_faces full matrix, which is
13450 actually much bigger than the minimal data required, but it makes the code
13451 more readable.
13452
13453 */
13454 if ((dim == spacedim - 1) && all_reference_cells_are_hyper_cube())
13455 {
13458 switch (dim)
13459 {
13460 case 1:
13461 {
13462 const bool values[][2] = {{false, true}, {true, false}};
13463 for (const unsigned int i : GeometryInfo<dim>::face_indices())
13464 for (const unsigned int j : GeometryInfo<dim>::face_indices())
13465 correct(i, j) = values[i][j];
13466 break;
13467 }
13468 case 2:
13469 {
13470 const bool values[][4] = {{false, true, true, false},
13471 {true, false, false, true},
13472 {true, false, false, true},
13473 {false, true, true, false}};
13474 for (const unsigned int i : GeometryInfo<dim>::face_indices())
13475 for (const unsigned int j : GeometryInfo<dim>::face_indices())
13476 correct(i, j) = (values[i][j]);
13477 break;
13478 }
13479 default:
13481 }
13482
13483
13484 std::list<active_cell_iterator> this_round, next_round;
13485 active_cell_iterator neighbor;
13486
13487 // Start with the first cell and (arbitrarily) decide that its
13488 // direction flag should be 'true':
13489 this_round.push_back(begin_active());
13490 begin_active()->set_direction_flag(true);
13491 begin_active()->set_user_flag();
13492
13493 while (this_round.size() > 0)
13494 {
13495 for (const auto &cell : this_round)
13496 {
13497 for (const unsigned int i : cell->face_indices())
13498 {
13499 if (cell->face(i)->at_boundary() == false)
13500 {
13501 // Consider the i'th neighbor of a cell for
13502 // which we have already set the direction:
13503 neighbor = cell->neighbor(i);
13504
13505 const unsigned int nb_of_nb =
13506 cell->neighbor_of_neighbor(i);
13507
13508 // If we already saw this neighboring cell,
13509 // check that everything is fine:
13510 if (neighbor->user_flag_set())
13511 {
13512 Assert(
13513 !(correct(i, nb_of_nb) ^
13514 (neighbor->direction_flag() ==
13515 cell->direction_flag())),
13516 ExcMessage(
13517 "The triangulation you are trying to create is not orientable."));
13518 }
13519 else
13520 {
13521 // We had not seen this cell yet. Set its
13522 // orientation flag (if necessary), mark it
13523 // as treated via the user flag, and push it
13524 // onto the list of cells to start work from
13525 // the next time around:
13526 if (correct(i, nb_of_nb) ^
13527 (neighbor->direction_flag() ==
13528 cell->direction_flag()))
13529 neighbor->set_direction_flag(
13530 !neighbor->direction_flag());
13531 neighbor->set_user_flag();
13532 next_round.push_back(neighbor);
13533 }
13534 }
13535 }
13536 }
13537
13538 // Before we quit let's check that if the triangulation is
13539 // disconnected that we still get all cells by starting
13540 // again from the first cell we haven't treated yet -- that
13541 // is, the first cell of the next disconnected component we
13542 // had not yet touched.
13543 if (next_round.empty())
13544 for (const auto &cell : this->active_cell_iterators())
13545 if (cell->user_flag_set() == false)
13546 {
13547 next_round.push_back(cell);
13548 cell->set_direction_flag(true);
13549 cell->set_user_flag();
13550 break;
13551 }
13552
13553 // Go on to the next round:
13554 next_round.swap(this_round);
13555 next_round.clear();
13556 }
13557 clear_user_flags();
13558 }
13559
13560 this->update_cell_relations();
13561
13562 // inform all listeners that the triangulation has been created
13563 signals.create();
13564}
13565
13566
13567
13568template <int dim, int spacedim>
13572{
13573 // 1) create coarse grid
13575 construction_data.coarse_cells,
13576 SubCellData());
13577
13578 // sort cell_infos on each level separately by coarse-cell index and
13579 // CellId
13580 const auto cell_infos = get_sorted_cell_infos(construction_data.cell_infos);
13581
13582 // 2) create all levels via a sequence of refinements. note that
13583 // we must make sure that we actually have cells on this level,
13584 // which is not clear in a parallel context for some processes
13585 for (unsigned int level = 0;
13586 level < cell_infos.size() && !cell_infos[level].empty();
13587 ++level)
13588 {
13589 // a) set material and manifold ids here (because new vertices
13590 // have to be positioned correctly during each refinement step)
13591 {
13592 auto cell = this->begin(level);
13593 auto cell_info = cell_infos[level].begin();
13594 for (; cell_info != cell_infos[level].end(); ++cell_info)
13595 {
13596 while (cell_info->id != cell->id().template to_binary<dim>())
13597 ++cell;
13598
13599 if (level == 0)
13600 {
13601 Assert(cell->material_id() == cell_info->material_id,
13602 ExcMessage(
13603 "The coarsest level material_ids must match."));
13604 Assert(cell->manifold_id() == cell_info->manifold_id,
13605 ExcMessage(
13606 "The coarsest level manifold_ids must match."));
13607 }
13608
13609 cell->set_material_id(cell_info->material_id);
13610 if (dim == 2)
13611 for (const auto face : cell->face_indices())
13612 cell->face(face)->set_manifold_id(
13613 cell_info->manifold_line_ids[face]);
13614 else if (dim == 3)
13615 {
13616 for (const auto face : cell->face_indices())
13617 cell->face(face)->set_manifold_id(
13618 cell_info->manifold_quad_ids[face]);
13619
13620 const auto line_indices = internal::TriaAccessorImplementation::
13621 Implementation::get_line_indices_of_cell(*cell);
13622 for (unsigned int l = 0; l < cell->n_lines(); ++l)
13623 {
13624 raw_line_iterator line(this, 0, line_indices[l]);
13625 line->set_manifold_id(cell_info->manifold_line_ids[l]);
13626 }
13627 }
13628
13629 cell->set_manifold_id(cell_info->manifold_id);
13630 }
13631 }
13632
13633 // b) perform refinement on all levels but on the finest
13634 if (level + 1 != cell_infos.size())
13635 {
13636 // find cells that should have children and mark them for
13637 // refinement
13638 auto coarse_cell = this->begin(level);
13639 auto fine_cell_info = cell_infos[level + 1].begin();
13640
13641 // loop over all cells on the next level
13642 for (; fine_cell_info != cell_infos[level + 1].end();
13643 ++fine_cell_info)
13644 {
13645 // find the parent of that cell
13646 while (
13647 !coarse_cell->id().is_parent_of(CellId(fine_cell_info->id)))
13648 ++coarse_cell;
13649
13650 // set parent for refinement
13651 coarse_cell->set_refine_flag();
13652 }
13653
13654 // execute refinement
13655 ::Triangulation<dim,
13656 spacedim>::execute_coarsening_and_refinement();
13657 }
13658 }
13659
13660 // 3) set boundary ids
13661 for (unsigned int level = 0;
13662 level < cell_infos.size() && !cell_infos[level].empty();
13663 ++level)
13664 {
13665 auto cell = this->begin(level);
13666 auto cell_info = cell_infos[level].begin();
13667 for (; cell_info != cell_infos[level].end(); ++cell_info)
13668 {
13669 // find cell that has the correct cell
13670 while (cell_info->id != cell->id().template to_binary<dim>())
13671 ++cell;
13672
13673 // boundary ids
13674 for (auto pair : cell_info->boundary_ids)
13675 if (cell->face(pair.first)->at_boundary())
13676 cell->face(pair.first)->set_boundary_id(pair.second);
13677 }
13678 }
13679
13680 // inform all listeners that the triangulation has been created
13681 signals.create();
13682}
13683
13684
13685template <int dim, int spacedim>
13688{
13689 AssertThrow(dim + 1 == spacedim,
13690 ExcMessage(
13691 "This function can only be called if dim == spacedim-1."));
13692 for (const auto &cell : this->active_cell_iterators())
13693 cell->set_direction_flag(!cell->direction_flag());
13694}
13695
13696
13697
13698template <int dim, int spacedim>
13701{
13702 if constexpr (dim == 3)
13703 {
13704 // Compute the vertex to cell map
13705 const std::vector<std::set<active_cell_iterator>> vertex_to_cell =
13707
13708 // Compute the line-neighbors
13709 if (this->line_to_adjacent_cells_map)
13710 // If the std::optional ready has a value attached to it, just change
13711 // the size of the table stored inside it.
13712 this->line_to_adjacent_cells_map->reinit(
13713 this->n_active_cells(),
13714 (is_mixed_mesh() ?
13715 GeometryInfo<dim>::lines_per_cell : // err on the safe side: choose
13716 // the largest number of lines
13717 // per cell
13718 get_reference_cells()[0]
13719 .n_lines()) // choose the right number of lines per cell for the
13720 // mesh type used
13721 );
13722 else
13723 // Else if the std::optional has no value attached to it, create a table
13724 // of the corresponding size and store it inside the std::optional.
13725 this->line_to_adjacent_cells_map =
13726 std::make_optional<Table<2, std::set<active_cell_iterator>>>(
13728 this->n_active_cells(),
13729 (is_mixed_mesh() ? GeometryInfo<dim>::lines_per_cell :
13730 get_reference_cells()[0].n_lines())));
13731
13732
13733 // Loop over all cells -> lines -> vertices
13734 for (const auto &cell : this->active_cell_iterators())
13735 for (unsigned int line : cell->line_indices())
13736 {
13737 const unsigned int vertex_0 = cell->vertex_index(
13739 const unsigned int vertex_1 = cell->vertex_index(
13741 const std::set<
13743 &adjacent_cells_to_vertex_0 = vertex_to_cell[vertex_0];
13744 const std::set<
13746 &adjacent_cells_to_vertex_1 = vertex_to_cell[vertex_1];
13747
13748 // add all cells that are adjacent to vertex_0 and vertex_1
13749 std::set_intersection(
13750 adjacent_cells_to_vertex_0.begin(),
13751 adjacent_cells_to_vertex_0.end(),
13752 adjacent_cells_to_vertex_1.begin(),
13753 adjacent_cells_to_vertex_1.end(),
13754 std::inserter(line_to_adjacent_cells_map
13755 .value()[cell->active_cell_index()][line],
13756 line_to_adjacent_cells_map
13757 .value()[cell->active_cell_index()][line]
13758 .begin()));
13759 }
13760 }
13761}
13762
13763
13764
13765template <int dim, int spacedim>
13768{
13769 Assert(n_cells() > 0,
13770 ExcMessage("Error: An empty Triangulation can not be refined."));
13771
13772 for (const auto &cell : this->active_cell_iterators())
13773 {
13774 cell->clear_coarsen_flag();
13775 cell->set_refine_flag();
13776 cell->set_refine_choice();
13777 }
13778}
13779
13780
13781
13782template <int dim, int spacedim>
13784void Triangulation<dim, spacedim>::refine_global(const unsigned int times)
13785{
13786 Assert(n_cells() > 0,
13787 ExcMessage("Error: An empty Triangulation can not be refined."));
13788
13789 for (unsigned int i = 0; i < times; ++i)
13790 {
13791 set_all_refine_flags();
13792 execute_coarsening_and_refinement();
13793 }
13794}
13795
13796
13797
13798template <int dim, int spacedim>
13800void Triangulation<dim, spacedim>::coarsen_global(const unsigned int times)
13801{
13802 for (unsigned int i = 0; i < times; ++i)
13803 {
13804 for (const auto &cell : this->active_cell_iterators())
13805 {
13806 cell->clear_refine_flag();
13807 cell->set_coarsen_flag();
13808 }
13809 execute_coarsening_and_refinement();
13810 }
13811}
13812
13813
13814#endif
13815/*-------------------- refine/coarsen flags -------------------------*/
13816
13817#ifndef DOXYGEN
13818
13819template <int dim, int spacedim>
13821void Triangulation<dim, spacedim>::save_refine_flags(std::vector<bool> &v) const
13822{
13823 v.resize(dim * n_active_cells(), false);
13824 std::vector<bool>::iterator i = v.begin();
13825
13826 for (const auto &cell : this->active_cell_iterators())
13827 for (unsigned int j = 0; j < dim; ++j, ++i)
13828 if (cell->refine_flag_set() & (1 << j))
13829 *i = true;
13830
13831 Assert(i == v.end(), ExcInternalError());
13832}
13833
13834
13835
13836template <int dim, int spacedim>
13838void Triangulation<dim, spacedim>::save_refine_flags(std::ostream &out) const
13839{
13840 std::vector<bool> v;
13841 save_refine_flags(v);
13842 write_bool_vector(mn_tria_refine_flags_begin,
13843 v,
13845 out);
13846}
13847
13848
13849
13850template <int dim, int spacedim>
13853{
13854 std::vector<bool> v;
13855 read_bool_vector(mn_tria_refine_flags_begin, v, mn_tria_refine_flags_end, in);
13856 load_refine_flags(v);
13857}
13858
13859
13860
13861template <int dim, int spacedim>
13863void Triangulation<dim, spacedim>::load_refine_flags(const std::vector<bool> &v)
13864{
13865 AssertThrow(v.size() == dim * n_active_cells(), ExcGridReadError());
13866
13867 std::vector<bool>::const_iterator i = v.begin();
13868 for (const auto &cell : this->active_cell_iterators())
13869 {
13870 unsigned int ref_case = 0;
13871
13872 for (unsigned int j = 0; j < dim; ++j, ++i)
13873 if (*i == true)
13874 ref_case += 1 << j;
13876 ExcGridReadError());
13877 if (ref_case > 0)
13878 cell->set_refine_flag(RefinementCase<dim>(ref_case));
13879 else
13880 cell->clear_refine_flag();
13881 }
13882
13883 Assert(i == v.end(), ExcInternalError());
13884}
13885
13886
13887
13888template <int dim, int spacedim>
13891 std::vector<bool> &v) const
13892{
13893 v.resize(n_active_cells(), false);
13894 std::vector<bool>::iterator i = v.begin();
13895 for (const auto &cell : this->active_cell_iterators())
13896 {
13897 *i = cell->coarsen_flag_set();
13898 ++i;
13899 }
13900
13901 Assert(i == v.end(), ExcInternalError());
13902}
13903
13904
13905
13906template <int dim, int spacedim>
13908void Triangulation<dim, spacedim>::save_coarsen_flags(std::ostream &out) const
13909{
13910 std::vector<bool> v;
13911 save_coarsen_flags(v);
13912 write_bool_vector(mn_tria_coarsen_flags_begin,
13913 v,
13915 out);
13916}
13917
13918
13919
13920template <int dim, int spacedim>
13923{
13924 std::vector<bool> v;
13925 read_bool_vector(mn_tria_coarsen_flags_begin,
13926 v,
13928 in);
13929 load_coarsen_flags(v);
13930}
13931
13932
13933
13934template <int dim, int spacedim>
13937 const std::vector<bool> &v)
13938{
13939 Assert(v.size() == n_active_cells(), ExcGridReadError());
13940
13941 std::vector<bool>::const_iterator i = v.begin();
13942 for (const auto &cell : this->active_cell_iterators())
13943 {
13944 if (*i == true)
13945 cell->set_coarsen_flag();
13946 else
13947 cell->clear_coarsen_flag();
13948 ++i;
13949 }
13950
13951 Assert(i == v.end(), ExcInternalError());
13952}
13953
13954
13955template <int dim, int spacedim>
13958{
13959 return anisotropic_refinement;
13960}
13961
13962
13963#endif
13964
13965namespace internal
13966{
13967 namespace
13968 {
13969 template <int dim, int spacedim>
13970 std::vector<std::vector<bool>>
13971 extract_raw_coarsen_flags(
13972 const std::vector<
13973 std::unique_ptr<::internal::TriangulationImplementation::
13974 TriaLevel<dim, spacedim>>> &levels)
13975 {
13976 std::vector<std::vector<bool>> coarsen_flags(levels.size());
13977 for (unsigned int level = 0; level < levels.size(); ++level)
13978 coarsen_flags[level] = levels[level]->coarsen_flags;
13979 return coarsen_flags;
13980 }
13981
13982 template <int dim, int spacedim>
13983 std::vector<std::vector<std::uint8_t>>
13984 extract_raw_refine_flags(
13985 const std::vector<
13986 std::unique_ptr<::internal::TriangulationImplementation::
13987 TriaLevel<dim, spacedim>>> &levels)
13988 {
13989 std::vector<std::vector<std::uint8_t>> refine_flags(levels.size());
13990 for (unsigned int level = 0; level < levels.size(); ++level)
13991 refine_flags[level] = levels[level]->refine_flags;
13992 return refine_flags;
13993 }
13994 } // namespace
13995} // namespace internal
13996
13997
13998/*-------------------- user data/flags -------------------------*/
13999
14000
14001namespace
14002{
14003 // clear user data of cells
14004 template <int dim, int spacedim>
14005 void
14006 clear_user_data(
14007 std::vector<std::unique_ptr<
14009 {
14010 for (auto &level : levels)
14011 level->cells.clear_user_data();
14012 }
14013
14014
14015 // clear user data of faces
14016 template <int dim>
14017 void
14019 {
14020 if (dim == 2)
14021 {
14022 faces->lines.clear_user_data();
14023 }
14024
14025
14026 if (dim == 3)
14027 {
14028 faces->lines.clear_user_data();
14029 faces->quads.clear_user_data();
14030 }
14031 }
14032} // namespace
14033
14034#ifndef DOXYGEN
14035
14036template <int dim, int spacedim>
14039{
14040 // let functions in anonymous namespace do their work
14041 ::clear_user_data(levels);
14042 if (dim > 1)
14043 ::clear_user_data(faces.get());
14044}
14045
14046
14047
14048namespace
14049{
14050 template <int dim, int spacedim>
14051 void
14052 clear_user_flags_line(
14053 std::vector<std::unique_ptr<
14056 {
14057 if (dim == 1)
14058 {
14059 for (const auto &level : levels)
14060 level->cells.clear_user_flags();
14061 }
14062 else if (dim == 2 || dim == 3)
14063 {
14064 faces->lines.clear_user_flags();
14065 }
14066 else
14067 {
14069 }
14070 }
14071} // namespace
14072
14073
14074template <int dim, int spacedim>
14077{
14078 ::clear_user_flags_line(levels, faces.get());
14079}
14080
14081
14082
14083namespace
14084{
14085 template <int dim, int spacedim>
14086 void
14087 clear_user_flags_quad(
14088 std::vector<std::unique_ptr<
14091 {
14092 if (dim == 1)
14093 {
14094 // nothing to do in 1d
14095 }
14096 else if (dim == 2)
14097 {
14098 for (const auto &level : levels)
14099 level->cells.clear_user_flags();
14100 }
14101 else if (dim == 3)
14102 {
14103 faces->quads.clear_user_flags();
14104 }
14105 else
14106 {
14108 }
14109 }
14110} // namespace
14111
14112
14113template <int dim, int spacedim>
14116{
14117 ::clear_user_flags_quad(levels, faces.get());
14118}
14119
14120
14121
14122namespace
14123{
14124 template <int dim, int spacedim>
14125 void
14126 clear_user_flags_hex(
14127 std::vector<std::unique_ptr<
14130 {
14131 if (dim == 1)
14132 {
14133 // nothing to do in 1d
14134 }
14135 else if (dim == 2)
14136 {
14137 // nothing to do in 2d
14138 }
14139 else if (dim == 3)
14140 {
14141 for (const auto &level : levels)
14142 level->cells.clear_user_flags();
14143 }
14144 else
14145 {
14147 }
14148 }
14149} // namespace
14150
14151
14152template <int dim, int spacedim>
14155{
14156 ::clear_user_flags_hex(levels, faces.get());
14157}
14158
14159
14160
14161template <int dim, int spacedim>
14164{
14165 clear_user_flags_line();
14166 clear_user_flags_quad();
14167 clear_user_flags_hex();
14168}
14169
14170
14171
14172template <int dim, int spacedim>
14174void Triangulation<dim, spacedim>::save_user_flags(std::ostream &out) const
14175{
14176 save_user_flags_line(out);
14177
14178 if (dim >= 2)
14179 save_user_flags_quad(out);
14180
14181 if (dim >= 3)
14182 save_user_flags_hex(out);
14183
14184 if (dim >= 4)
14186}
14187
14188
14189
14190template <int dim, int spacedim>
14192void Triangulation<dim, spacedim>::save_user_flags(std::vector<bool> &v) const
14193{
14194 // clear vector and append
14195 // all the stuff later on
14196 v.clear();
14197
14198 std::vector<bool> tmp;
14199
14200 save_user_flags_line(tmp);
14201 v.insert(v.end(), tmp.begin(), tmp.end());
14202
14203 if (dim >= 2)
14204 {
14205 save_user_flags_quad(tmp);
14206 v.insert(v.end(), tmp.begin(), tmp.end());
14207 }
14208
14209 if (dim >= 3)
14210 {
14211 save_user_flags_hex(tmp);
14212 v.insert(v.end(), tmp.begin(), tmp.end());
14213 }
14214
14215 if (dim >= 4)
14217}
14218
14219
14220
14221template <int dim, int spacedim>
14224{
14225 load_user_flags_line(in);
14226
14227 if (dim >= 2)
14228 load_user_flags_quad(in);
14229
14230 if (dim >= 3)
14231 load_user_flags_hex(in);
14232
14233 if (dim >= 4)
14235}
14236
14237
14238
14239template <int dim, int spacedim>
14241void Triangulation<dim, spacedim>::load_user_flags(const std::vector<bool> &v)
14242{
14243 Assert(v.size() == n_lines() + n_quads() + n_hexs(), ExcInternalError());
14244 std::vector<bool> tmp;
14245
14246 // first extract the flags
14247 // belonging to lines
14248 tmp.insert(tmp.end(), v.begin(), v.begin() + n_lines());
14249 // and set the lines
14250 load_user_flags_line(tmp);
14251
14252 if (dim >= 2)
14253 {
14254 tmp.clear();
14255 tmp.insert(tmp.end(),
14256 v.begin() + n_lines(),
14257 v.begin() + n_lines() + n_quads());
14258 load_user_flags_quad(tmp);
14259 }
14260
14261 if (dim >= 3)
14262 {
14263 tmp.clear();
14264 tmp.insert(tmp.end(),
14265 v.begin() + n_lines() + n_quads(),
14266 v.begin() + n_lines() + n_quads() + n_hexs());
14267 load_user_flags_hex(tmp);
14268 }
14269
14270 if (dim >= 4)
14272}
14273
14274
14275
14276template <int dim, int spacedim>
14279 std::vector<bool> &v) const
14280{
14281 v.resize(n_lines(), false);
14282 std::vector<bool>::iterator i = v.begin();
14283 line_iterator line = begin_line(), endl = end_line();
14284 for (; line != endl; ++line, ++i)
14285 *i = line->user_flag_set();
14286
14287 Assert(i == v.end(), ExcInternalError());
14288}
14289
14290
14291
14292template <int dim, int spacedim>
14294void Triangulation<dim, spacedim>::save_user_flags_line(std::ostream &out) const
14295{
14296 std::vector<bool> v;
14297 save_user_flags_line(v);
14298 write_bool_vector(mn_tria_line_user_flags_begin,
14299 v,
14301 out);
14302}
14303
14304
14305
14306template <int dim, int spacedim>
14309{
14310 std::vector<bool> v;
14311 read_bool_vector(mn_tria_line_user_flags_begin,
14312 v,
14314 in);
14315 load_user_flags_line(v);
14316}
14317
14318
14319
14320template <int dim, int spacedim>
14323 const std::vector<bool> &v)
14324{
14325 Assert(v.size() == n_lines(), ExcGridReadError());
14326
14327 line_iterator line = begin_line(), endl = end_line();
14328 std::vector<bool>::const_iterator i = v.begin();
14329 for (; line != endl; ++line, ++i)
14330 if (*i == true)
14331 line->set_user_flag();
14332 else
14333 line->clear_user_flag();
14334
14335 Assert(i == v.end(), ExcInternalError());
14336}
14337
14338#endif
14339
14340namespace
14341{
14342 template <typename Iterator>
14343 bool
14344 get_user_flag(const Iterator &i)
14345 {
14346 return i->user_flag_set();
14347 }
14348
14349
14350
14351 template <int structdim, int dim, int spacedim>
14352 bool
14354 {
14356 return false;
14357 }
14358
14359
14360
14361 template <typename Iterator>
14362 void
14363 set_user_flag(const Iterator &i)
14364 {
14365 i->set_user_flag();
14366 }
14367
14368
14369
14370 template <int structdim, int dim, int spacedim>
14371 void
14373 {
14375 }
14376
14377
14378
14379 template <typename Iterator>
14380 void
14381 clear_user_flag(const Iterator &i)
14382 {
14383 i->clear_user_flag();
14384 }
14385
14386
14387
14388 template <int structdim, int dim, int spacedim>
14389 void
14390 clear_user_flag(
14392 {
14394 }
14395} // namespace
14396
14397#ifndef DOXYGEN
14398
14399template <int dim, int spacedim>
14402 std::vector<bool> &v) const
14403{
14404 v.resize(n_quads(), false);
14405
14406 if (dim >= 2)
14407 {
14408 std::vector<bool>::iterator i = v.begin();
14409 quad_iterator quad = begin_quad(), endq = end_quad();
14410 for (; quad != endq; ++quad, ++i)
14411 *i = get_user_flag(quad);
14412
14413 Assert(i == v.end(), ExcInternalError());
14414 }
14415}
14416
14417
14418
14419template <int dim, int spacedim>
14421void Triangulation<dim, spacedim>::save_user_flags_quad(std::ostream &out) const
14422{
14423 std::vector<bool> v;
14424 save_user_flags_quad(v);
14425 write_bool_vector(mn_tria_quad_user_flags_begin,
14426 v,
14428 out);
14429}
14430
14431
14432
14433template <int dim, int spacedim>
14436{
14437 std::vector<bool> v;
14438 read_bool_vector(mn_tria_quad_user_flags_begin,
14439 v,
14441 in);
14442 load_user_flags_quad(v);
14443}
14444
14445
14446
14447template <int dim, int spacedim>
14450 const std::vector<bool> &v)
14451{
14452 Assert(v.size() == n_quads(), ExcGridReadError());
14453
14454 if (dim >= 2)
14455 {
14456 quad_iterator quad = begin_quad(), endq = end_quad();
14457 std::vector<bool>::const_iterator i = v.begin();
14458 for (; quad != endq; ++quad, ++i)
14459 if (*i == true)
14460 set_user_flag(quad);
14461 else
14462 clear_user_flag(quad);
14463
14464 Assert(i == v.end(), ExcInternalError());
14465 }
14466}
14467
14468
14469
14470template <int dim, int spacedim>
14473 std::vector<bool> &v) const
14474{
14475 v.resize(n_hexs(), false);
14476
14477 if (dim >= 3)
14478 {
14479 std::vector<bool>::iterator i = v.begin();
14480 hex_iterator hex = begin_hex(), endh = end_hex();
14481 for (; hex != endh; ++hex, ++i)
14482 *i = get_user_flag(hex);
14483
14484 Assert(i == v.end(), ExcInternalError());
14485 }
14486}
14487
14488
14489
14490template <int dim, int spacedim>
14492void Triangulation<dim, spacedim>::save_user_flags_hex(std::ostream &out) const
14493{
14494 std::vector<bool> v;
14495 save_user_flags_hex(v);
14496 write_bool_vector(mn_tria_hex_user_flags_begin,
14497 v,
14499 out);
14500}
14501
14502
14503
14504template <int dim, int spacedim>
14507{
14508 std::vector<bool> v;
14509 read_bool_vector(mn_tria_hex_user_flags_begin,
14510 v,
14512 in);
14513 load_user_flags_hex(v);
14514}
14515
14516
14517
14518template <int dim, int spacedim>
14521 const std::vector<bool> &v)
14522{
14523 Assert(v.size() == n_hexs(), ExcGridReadError());
14524
14525 if (dim >= 3)
14526 {
14527 hex_iterator hex = begin_hex(), endh = end_hex();
14528 std::vector<bool>::const_iterator i = v.begin();
14529 for (; hex != endh; ++hex, ++i)
14530 if (*i == true)
14531 set_user_flag(hex);
14532 else
14533 clear_user_flag(hex);
14534
14535 Assert(i == v.end(), ExcInternalError());
14536 }
14537}
14538
14539
14540
14541template <int dim, int spacedim>
14544 std::vector<unsigned int> &v) const
14545{
14546 // clear vector and append all the
14547 // stuff later on
14548 v.clear();
14549
14550 std::vector<unsigned int> tmp;
14551
14552 save_user_indices_line(tmp);
14553 v.insert(v.end(), tmp.begin(), tmp.end());
14554
14555 if (dim >= 2)
14556 {
14557 save_user_indices_quad(tmp);
14558 v.insert(v.end(), tmp.begin(), tmp.end());
14559 }
14560
14561 if (dim >= 3)
14562 {
14563 save_user_indices_hex(tmp);
14564 v.insert(v.end(), tmp.begin(), tmp.end());
14565 }
14566
14567 if (dim >= 4)
14569}
14570
14571
14572
14573template <int dim, int spacedim>
14576 const std::vector<unsigned int> &v)
14577{
14578 Assert(v.size() == n_lines() + n_quads() + n_hexs(), ExcInternalError());
14579 std::vector<unsigned int> tmp;
14580
14581 // first extract the indices
14582 // belonging to lines
14583 tmp.insert(tmp.end(), v.begin(), v.begin() + n_lines());
14584 // and set the lines
14585 load_user_indices_line(tmp);
14586
14587 if (dim >= 2)
14588 {
14589 tmp.clear();
14590 tmp.insert(tmp.end(),
14591 v.begin() + n_lines(),
14592 v.begin() + n_lines() + n_quads());
14593 load_user_indices_quad(tmp);
14594 }
14595
14596 if (dim >= 3)
14597 {
14598 tmp.clear();
14599 tmp.insert(tmp.end(),
14600 v.begin() + n_lines() + n_quads(),
14601 v.begin() + n_lines() + n_quads() + n_hexs());
14602 load_user_indices_hex(tmp);
14603 }
14604
14605 if (dim >= 4)
14607}
14608
14609
14610
14611template <int dim, int spacedim>
14613void Triangulation<dim, spacedim>::save(const std::string &file_basename) const
14614{
14615 // Save triangulation information.
14616 {
14617 std::ofstream ofs_tria(file_basename + "_triangulation.data");
14618 AssertThrow(ofs_tria.fail() == false, ExcIO());
14619
14620 boost::archive::text_oarchive oa(ofs_tria, boost::archive::no_header);
14621 save(oa,
14623 }
14624
14625 // Save attached data.
14626 {
14627 std::ofstream ofs_info(file_basename + ".info");
14628 ofs_info
14629 << "version nproc n_attached_fixed_size_objs n_attached_variable_size_objs n_active_cells"
14630 << std::endl
14632 << " " << 1 << " " << this->cell_attached_data.pack_callbacks_fixed.size()
14633 << " " << this->cell_attached_data.pack_callbacks_variable.size() << " "
14634 << this->n_global_active_cells() << std::endl;
14635 }
14636
14637 this->save_attached_data(0, this->n_global_active_cells(), file_basename);
14638}
14639
14640
14641
14642template <int dim, int spacedim>
14644void Triangulation<dim, spacedim>::load(const std::string &file_basename)
14645{
14646 // It's probably prudent to first get rid of any all content of the
14647 // triangulation, rather than hope that the deserialization below
14648 // overwrites everything:
14649 clear();
14650
14651 // Load triangulation information.
14652 {
14653 std::ifstream ifs_tria(file_basename + "_triangulation.data");
14654 AssertThrow(ifs_tria.fail() == false, ExcIO());
14655
14656 boost::archive::text_iarchive ia(ifs_tria, boost::archive::no_header);
14657 load(ia,
14659 }
14660
14661 // Load attached data.
14662 unsigned int version, numcpus, attached_count_fixed, attached_count_variable,
14663 n_global_active_cells;
14664 {
14665 std::ifstream ifs_info(std::string(file_basename) + ".info");
14666 AssertThrow(ifs_info.fail() == false, ExcIO());
14667 std::string firstline;
14668 std::getline(ifs_info, firstline);
14669 ifs_info >> version >> numcpus >> attached_count_fixed >>
14670 attached_count_variable >> n_global_active_cells;
14671 }
14672
14673 AssertThrow(numcpus == 1,
14674 ExcMessage("Incompatible number of CPUs found in .info file."));
14675
14676 const auto expected_version =
14678 spacedim>::version_number;
14679 AssertThrow(version == expected_version,
14680 ExcMessage(
14681 "The information saved in the file you are trying "
14682 "to read the triangulation from was written with an "
14683 "incompatible file format version and cannot be read."));
14684 Assert(this->n_global_active_cells() == n_global_active_cells,
14685 ExcMessage("The number of cells of the triangulation differs "
14686 "from the number of cells written into the .info file."));
14687
14688 // Clear all of the callback data, as explained in the documentation of
14689 // register_data_attach().
14690 this->cell_attached_data.n_attached_data_sets = 0;
14691 this->cell_attached_data.n_attached_deserialize =
14692 attached_count_fixed + attached_count_variable;
14693
14694 this->load_attached_data(0,
14695 this->n_global_active_cells(),
14696 this->n_active_cells(),
14697 file_basename,
14698 attached_count_fixed,
14699 attached_count_variable);
14700
14701 this->update_cell_relations();
14702}
14703
14704#endif
14705namespace
14706{
14707 template <typename Iterator>
14708 unsigned int
14709 get_user_index(const Iterator &i)
14710 {
14711 return i->user_index();
14712 }
14713
14714
14715
14716 template <int structdim, int dim, int spacedim>
14717 unsigned int
14718 get_user_index(
14720 {
14723 }
14724
14725
14726
14727 template <typename Iterator>
14728 void
14729 set_user_index(const Iterator &i, const unsigned int x)
14730 {
14731 i->set_user_index(x);
14732 }
14733
14734
14735
14736 template <int structdim, int dim, int spacedim>
14737 void
14738 set_user_index(
14740 const unsigned int)
14741 {
14743 }
14744} // namespace
14745
14746#ifndef DOXYGEN
14747
14748template <int dim, int spacedim>
14751 std::vector<unsigned int> &v) const
14752{
14753 v.resize(n_lines(), 0);
14754 std::vector<unsigned int>::iterator i = v.begin();
14755 line_iterator line = begin_line(), endl = end_line();
14756 for (; line != endl; ++line, ++i)
14757 *i = line->user_index();
14758}
14759
14760
14761
14762template <int dim, int spacedim>
14765 const std::vector<unsigned int> &v)
14766{
14767 Assert(v.size() == n_lines(), ExcGridReadError());
14768
14769 line_iterator line = begin_line(), endl = end_line();
14770 std::vector<unsigned int>::const_iterator i = v.begin();
14771 for (; line != endl; ++line, ++i)
14772 line->set_user_index(*i);
14773}
14774
14775
14776template <int dim, int spacedim>
14779 std::vector<unsigned int> &v) const
14780{
14781 v.resize(n_quads(), 0);
14782
14783 if (dim >= 2)
14784 {
14785 std::vector<unsigned int>::iterator i = v.begin();
14786 quad_iterator quad = begin_quad(), endq = end_quad();
14787 for (; quad != endq; ++quad, ++i)
14788 *i = get_user_index(quad);
14789 }
14790}
14791
14792
14793
14794template <int dim, int spacedim>
14797 const std::vector<unsigned int> &v)
14798{
14799 Assert(v.size() == n_quads(), ExcGridReadError());
14800
14801 if (dim >= 2)
14802 {
14803 quad_iterator quad = begin_quad(), endq = end_quad();
14804 std::vector<unsigned int>::const_iterator i = v.begin();
14805 for (; quad != endq; ++quad, ++i)
14806 set_user_index(quad, *i);
14807 }
14808}
14809
14810
14811template <int dim, int spacedim>
14814 std::vector<unsigned int> &v) const
14815{
14816 v.resize(n_hexs(), 0);
14817
14818 if (dim >= 3)
14819 {
14820 std::vector<unsigned int>::iterator i = v.begin();
14821 hex_iterator hex = begin_hex(), endh = end_hex();
14822 for (; hex != endh; ++hex, ++i)
14823 *i = get_user_index(hex);
14824 }
14825}
14826
14827
14828
14829template <int dim, int spacedim>
14832 const std::vector<unsigned int> &v)
14833{
14834 Assert(v.size() == n_hexs(), ExcGridReadError());
14835
14836 if (dim >= 3)
14837 {
14838 hex_iterator hex = begin_hex(), endh = end_hex();
14839 std::vector<unsigned int>::const_iterator i = v.begin();
14840 for (; hex != endh; ++hex, ++i)
14841 set_user_index(hex, *i);
14842 }
14843}
14844
14845#endif
14846
14847
14848//---------------- user pointers ----------------------------------------//
14849
14850
14851namespace
14852{
14853 template <typename Iterator>
14854 void *
14855 get_user_pointer(const Iterator &i)
14856 {
14857 return i->user_pointer();
14858 }
14859
14860
14861
14862 template <int structdim, int dim, int spacedim>
14863 void *
14864 get_user_pointer(
14866 {
14868 return nullptr;
14869 }
14870
14871
14872
14873 template <typename Iterator>
14874 void
14875 set_user_pointer(const Iterator &i, void *x)
14876 {
14877 i->set_user_pointer(x);
14878 }
14879
14880
14881
14882 template <int structdim, int dim, int spacedim>
14883 void
14884 set_user_pointer(
14886 void *)
14887 {
14889 }
14890} // namespace
14891
14892#ifndef DOXYGEN
14893
14894template <int dim, int spacedim>
14897 std::vector<void *> &v) const
14898{
14899 // clear vector and append all the
14900 // stuff later on
14901 v.clear();
14902
14903 std::vector<void *> tmp;
14904
14905 save_user_pointers_line(tmp);
14906 v.insert(v.end(), tmp.begin(), tmp.end());
14907
14908 if (dim >= 2)
14909 {
14910 save_user_pointers_quad(tmp);
14911 v.insert(v.end(), tmp.begin(), tmp.end());
14912 }
14913
14914 if (dim >= 3)
14915 {
14916 save_user_pointers_hex(tmp);
14917 v.insert(v.end(), tmp.begin(), tmp.end());
14918 }
14919
14920 if (dim >= 4)
14922}
14923
14924
14925
14926template <int dim, int spacedim>
14929 const std::vector<void *> &v)
14930{
14931 Assert(v.size() == n_lines() + n_quads() + n_hexs(), ExcInternalError());
14932 std::vector<void *> tmp;
14933
14934 // first extract the pointers
14935 // belonging to lines
14936 tmp.insert(tmp.end(), v.begin(), v.begin() + n_lines());
14937 // and set the lines
14938 load_user_pointers_line(tmp);
14939
14940 if (dim >= 2)
14941 {
14942 tmp.clear();
14943 tmp.insert(tmp.end(),
14944 v.begin() + n_lines(),
14945 v.begin() + n_lines() + n_quads());
14946 load_user_pointers_quad(tmp);
14947 }
14948
14949 if (dim >= 3)
14950 {
14951 tmp.clear();
14952 tmp.insert(tmp.end(),
14953 v.begin() + n_lines() + n_quads(),
14954 v.begin() + n_lines() + n_quads() + n_hexs());
14955 load_user_pointers_hex(tmp);
14956 }
14957
14958 if (dim >= 4)
14960}
14961
14962
14963
14964template <int dim, int spacedim>
14967 std::vector<void *> &v) const
14968{
14969 v.resize(n_lines(), nullptr);
14970 std::vector<void *>::iterator i = v.begin();
14971 line_iterator line = begin_line(), endl = end_line();
14972 for (; line != endl; ++line, ++i)
14973 *i = line->user_pointer();
14974}
14975
14976
14977
14978template <int dim, int spacedim>
14981 const std::vector<void *> &v)
14982{
14983 Assert(v.size() == n_lines(), ExcGridReadError());
14984
14985 line_iterator line = begin_line(), endl = end_line();
14986 std::vector<void *>::const_iterator i = v.begin();
14987 for (; line != endl; ++line, ++i)
14988 line->set_user_pointer(*i);
14989}
14990
14991
14992
14993template <int dim, int spacedim>
14996 std::vector<void *> &v) const
14997{
14998 v.resize(n_quads(), nullptr);
14999
15000 if (dim >= 2)
15001 {
15002 std::vector<void *>::iterator i = v.begin();
15003 quad_iterator quad = begin_quad(), endq = end_quad();
15004 for (; quad != endq; ++quad, ++i)
15005 *i = get_user_pointer(quad);
15006 }
15007}
15008
15009
15010
15011template <int dim, int spacedim>
15014 const std::vector<void *> &v)
15015{
15016 Assert(v.size() == n_quads(), ExcGridReadError());
15017
15018 if (dim >= 2)
15019 {
15020 quad_iterator quad = begin_quad(), endq = end_quad();
15021 std::vector<void *>::const_iterator i = v.begin();
15022 for (; quad != endq; ++quad, ++i)
15023 set_user_pointer(quad, *i);
15024 }
15025}
15026
15027
15028template <int dim, int spacedim>
15031 std::vector<void *> &v) const
15032{
15033 v.resize(n_hexs(), nullptr);
15034
15035 if (dim >= 3)
15036 {
15037 std::vector<void *>::iterator i = v.begin();
15038 hex_iterator hex = begin_hex(), endh = end_hex();
15039 for (; hex != endh; ++hex, ++i)
15040 *i = get_user_pointer(hex);
15041 }
15042}
15043
15044
15045
15046template <int dim, int spacedim>
15049 const std::vector<void *> &v)
15050{
15051 Assert(v.size() == n_hexs(), ExcGridReadError());
15052
15053 if (dim >= 3)
15054 {
15055 hex_iterator hex = begin_hex(), endh = end_hex();
15056 std::vector<void *>::const_iterator i = v.begin();
15057 for (; hex != endh; ++hex, ++i)
15058 set_user_pointer(hex, *i);
15059 }
15060}
15061
15062#endif
15063
15064/*------------------------ Cell iterator functions ------------------------*/
15065
15066#ifndef DOXYGEN
15067
15068template <int dim, int spacedim>
15071 Triangulation<dim, spacedim>::begin_raw(const unsigned int level) const
15072{
15073 switch (dim)
15074 {
15075 case 1:
15076 return begin_raw_line(level);
15077 case 2:
15078 return begin_raw_quad(level);
15079 case 3:
15080 return begin_raw_hex(level);
15081 default:
15083 return raw_cell_iterator();
15084 }
15085}
15086
15087
15088
15089template <int dim, int spacedim>
15092 Triangulation<dim, spacedim>::begin(const unsigned int level) const
15093{
15094 switch (dim)
15095 {
15096 case 1:
15097 return begin_line(level);
15098 case 2:
15099 return begin_quad(level);
15100 case 3:
15101 return begin_hex(level);
15102 default:
15103 Assert(false, ExcImpossibleInDim(dim));
15104 return cell_iterator();
15105 }
15106}
15107
15108
15109
15110template <int dim, int spacedim>
15113 Triangulation<dim, spacedim>::begin_active(const unsigned int level) const
15114{
15115 switch (dim)
15116 {
15117 case 1:
15118 return begin_active_line(level);
15119 case 2:
15120 return begin_active_quad(level);
15121 case 3:
15122 return begin_active_hex(level);
15123 default:
15125 return active_cell_iterator();
15126 }
15127}
15128
15129
15130
15131template <int dim, int spacedim>
15135{
15136 const unsigned int level = levels.size() - 1;
15137 if (levels[level]->cells.n_objects() == 0)
15138 return end(level);
15139
15140 // find the last raw iterator on
15141 // this level
15142 raw_cell_iterator ri(const_cast<Triangulation<dim, spacedim> *>(this),
15143 level,
15144 levels[level]->cells.n_objects() - 1);
15145
15146 // then move to the last used one
15147 if (ri->used() == true)
15148 return ri;
15149 while ((--ri).state() == IteratorState::valid)
15150 if (ri->used() == true)
15151 return ri;
15152 return ri;
15153}
15154
15155
15156
15157template <int dim, int spacedim>
15161{
15162 // get the last used cell
15163 cell_iterator cell = last();
15164
15165 if (cell != end())
15166 {
15167 // then move to the last active one
15168 if (cell->is_active() == true)
15169 return cell;
15170 while ((--cell).state() == IteratorState::valid)
15171 if (cell->is_active() == true)
15172 return cell;
15173 }
15174 return cell;
15175}
15176
15177
15178
15179template <int dim, int spacedim>
15183 const CellId &cell_id) const
15184{
15185 Assert(
15186 this->contains_cell(cell_id),
15187 ExcMessage(
15188 "CellId is invalid for this triangulation.\n"
15189 "Either the provided CellId does not correspond to a cell in this "
15190 "triangulation object, or, in case you are using a parallel "
15191 "triangulation, may correspond to an artificial cell that is less "
15192 "refined on this processor. In the case of "
15193 "parallel::fullydistributed::Triangulation, the corresponding coarse "
15194 "cell might not be accessible by the current process."));
15195
15196 cell_iterator cell(
15197 this, 0, coarse_cell_id_to_coarse_cell_index(cell_id.get_coarse_cell_id()));
15198
15199 for (const auto &child_index : cell_id.get_child_indices())
15200 cell = cell->child(static_cast<unsigned int>(child_index));
15201
15202 return cell;
15203}
15204
15205
15206
15207template <int dim, int spacedim>
15209bool Triangulation<dim, spacedim>::contains_cell(const CellId &cell_id) const
15210{
15211 const auto coarse_cell_index =
15212 coarse_cell_id_to_coarse_cell_index(cell_id.get_coarse_cell_id());
15213
15214 if (coarse_cell_index == numbers::invalid_unsigned_int)
15215 return false;
15216
15217 cell_iterator cell(this, 0, coarse_cell_index);
15218
15219 for (const auto &child_index : cell_id.get_child_indices())
15220 {
15221 if (cell->has_children() == false)
15222 return false;
15223 cell = cell->child(static_cast<unsigned int>(child_index));
15224 }
15225
15226 return true;
15227}
15228
15229
15230
15231template <int dim, int spacedim>
15235{
15236 return cell_iterator(const_cast<Triangulation<dim, spacedim> *>(this),
15237 -1,
15238 -1);
15239}
15240
15241
15242
15243template <int dim, int spacedim>
15246 Triangulation<dim, spacedim>::end_raw(const unsigned int level) const
15247{
15248 // This function may be called on parallel triangulations on levels
15249 // that exist globally, but not on the local portion of the
15250 // triangulation. In that case, just return the end iterator.
15251 //
15252 // We need to use levels.size() instead of n_levels() because the
15253 // latter function uses the cache, but we need to be able to call
15254 // this function at a time when the cache is not currently up to
15255 // date.
15256 if (level >= levels.size())
15257 {
15258 Assert(level < n_global_levels(),
15259 ExcInvalidLevel(level, n_global_levels()));
15260 return end();
15261 }
15262
15263 // Query whether the given level is valid for the local portion of the
15264 // triangulation.
15265 Assert(level < levels.size(), ExcInvalidLevel(level, levels.size()));
15266 if (level < levels.size() - 1)
15267 return begin_raw(level + 1);
15268 else
15269 return end();
15270}
15271
15272
15273template <int dim, int spacedim>
15276 Triangulation<dim, spacedim>::end(const unsigned int level) const
15277{
15278 // This function may be called on parallel triangulations on levels
15279 // that exist globally, but not on the local portion of the
15280 // triangulation. In that case, just return the end iterator.
15281 //
15282 // We need to use levels.size() instead of n_levels() because the
15283 // latter function uses the cache, but we need to be able to call
15284 // this function at a time when the cache is not currently up to
15285 // date.
15286 if (level >= levels.size())
15287 {
15288 Assert(level < n_global_levels(),
15289 ExcInvalidLevel(level, n_global_levels()));
15290 return end();
15291 }
15292
15293 // Query whether the given level is valid for the local portion of the
15294 // triangulation.
15295 Assert(level < levels.size(), ExcInvalidLevel(level, levels.size()));
15296 if (level < levels.size() - 1)
15297 return begin(level + 1);
15298 else
15299 return end();
15300}
15301
15302
15303template <int dim, int spacedim>
15306 Triangulation<dim, spacedim>::end_active(const unsigned int level) const
15307{
15308 // This function may be called on parallel triangulations on levels
15309 // that exist globally, but not on the local portion of the
15310 // triangulation. In that case, just return the end iterator.
15311 //
15312 // We need to use levels.size() instead of n_levels() because the
15313 // latter function uses the cache, but we need to be able to call
15314 // this function at a time when the cache is not currently up to
15315 // date.
15316 if (level >= levels.size())
15317 {
15318 Assert(level < n_global_levels(),
15319 ExcInvalidLevel(level, n_global_levels()));
15320 return end();
15321 }
15322
15323 // Query whether the given level is valid for the local portion of the
15324 // triangulation.
15325 Assert(level < levels.size(), ExcInvalidLevel(level, levels.size()));
15326 return (level >= levels.size() - 1 ? active_cell_iterator(end()) :
15327 begin_active(level + 1));
15328}
15329
15330
15331
15332template <int dim, int spacedim>
15336 const
15337{
15339 begin(), end());
15340}
15341
15342
15343template <int dim, int spacedim>
15346 active_cell_iterator> Triangulation<dim, spacedim>::
15348{
15349 return IteratorRange<
15351 end());
15352}
15353
15354
15355
15356template <int dim, int spacedim>
15359 cell_iterator> Triangulation<dim, spacedim>::
15360 cell_iterators_on_level(const unsigned int level) const
15361{
15363 begin(level), end(level));
15364}
15365
15366
15367
15368template <int dim, int spacedim>
15371 active_cell_iterator> Triangulation<dim, spacedim>::
15372 active_cell_iterators_on_level(const unsigned int level) const
15373{
15374 return IteratorRange<
15376 begin_active(level), end_active(level));
15377}
15378#endif
15379
15380/*------------------------ Face iterator functions ------------------------*/
15381
15382#ifndef DOXYGEN
15383
15384template <int dim, int spacedim>
15388{
15389 switch (dim)
15390 {
15391 case 1:
15392 Assert(false, ExcImpossibleInDim(1));
15393 return raw_face_iterator();
15394 case 2:
15395 return begin_line();
15396 case 3:
15397 return begin_quad();
15398 default:
15400 return face_iterator();
15401 }
15402}
15403
15404
15405
15406template <int dim, int spacedim>
15410{
15411 switch (dim)
15412 {
15413 case 1:
15414 Assert(false, ExcImpossibleInDim(1));
15415 return raw_face_iterator();
15416 case 2:
15417 return begin_active_line();
15418 case 3:
15419 return begin_active_quad();
15420 default:
15422 return active_face_iterator();
15423 }
15424}
15425
15426
15427
15428template <int dim, int spacedim>
15432{
15433 switch (dim)
15434 {
15435 case 1:
15436 Assert(false, ExcImpossibleInDim(1));
15437 return raw_face_iterator();
15438 case 2:
15439 return end_line();
15440 case 3:
15441 return end_quad();
15442 default:
15444 return raw_face_iterator();
15445 }
15446}
15447
15448
15449
15450template <int dim, int spacedim>
15453 active_face_iterator> Triangulation<dim, spacedim>::
15455{
15456 return IteratorRange<
15458 begin_active_face(), end_face());
15459}
15460
15461/*------------------------ Vertex iterator functions ------------------------*/
15462
15463
15464template <int dim, int spacedim>
15468{
15469 vertex_iterator i =
15470 raw_vertex_iterator(const_cast<Triangulation<dim, spacedim> *>(this), 0, 0);
15471 if (i.state() != IteratorState::valid)
15472 return i;
15473 // This loop will end because every triangulation has used vertices.
15474 while (i->used() == false)
15475 if ((++i).state() != IteratorState::valid)
15476 return i;
15477 return i;
15478}
15479
15480
15481
15482template <int dim, int spacedim>
15486{
15487 // every vertex is active
15488 return begin_vertex();
15489}
15490
15491
15492
15493template <int dim, int spacedim>
15497{
15498 return raw_vertex_iterator(const_cast<Triangulation<dim, spacedim> *>(this),
15499 -1,
15501}
15502
15503#endif
15504
15505
15506/*------------------------ Line iterator functions ------------------------*/
15507
15508#ifndef DOXYGEN
15509
15510template <int dim, int spacedim>
15513 Triangulation<dim, spacedim>::begin_raw_line(const unsigned int level) const
15514{
15515 // This function may be called on parallel triangulations on levels
15516 // that exist globally, but not on the local portion of the
15517 // triangulation. In that case, just return the end iterator.
15518 //
15519 // We need to use levels.size() instead of n_levels() because the
15520 // latter function uses the cache, but we need to be able to call
15521 // this function at a time when the cache is not currently up to
15522 // date.
15523 if (level >= levels.size())
15524 {
15525 Assert(level < n_global_levels(),
15526 ExcInvalidLevel(level, n_global_levels()));
15527 return end_line();
15528 }
15529
15530 switch (dim)
15531 {
15532 case 1:
15533 // Query whether the given level is valid for the local portion of the
15534 // triangulation.
15535 Assert(level < levels.size(), ExcInvalidLevel(level, levels.size()));
15536
15537 if (level >= levels.size() || levels[level]->cells.n_objects() == 0)
15538 return end_line();
15539
15540 return raw_line_iterator(
15541 const_cast<Triangulation<dim, spacedim> *>(this), level, 0);
15542
15543 default:
15544 Assert(level == 0, ExcFacesHaveNoLevel());
15545 return raw_line_iterator(
15546 const_cast<Triangulation<dim, spacedim> *>(this), 0, 0);
15547 }
15548}
15549
15550
15551template <int dim, int spacedim>
15554 Triangulation<dim, spacedim>::begin_line(const unsigned int level) const
15555{
15556 // level is checked in begin_raw
15557 raw_line_iterator ri = begin_raw_line(level);
15558 if (ri.state() != IteratorState::valid)
15559 return ri;
15560 while (ri->used() == false)
15561 if ((++ri).state() != IteratorState::valid)
15562 return ri;
15563 return ri;
15564}
15565
15566
15567
15568template <int dim, int spacedim>
15572 const unsigned int level) const
15573{
15574 // level is checked in begin_raw
15575 line_iterator i = begin_line(level);
15576 if (i.state() != IteratorState::valid)
15577 return i;
15578 while (i->has_children())
15579 if ((++i).state() != IteratorState::valid)
15580 return i;
15581 return i;
15582}
15583
15584
15585
15586template <int dim, int spacedim>
15590{
15591 return raw_line_iterator(const_cast<Triangulation<dim, spacedim> *>(this),
15592 -1,
15593 -1);
15594}
15595
15596#endif
15597
15598/*------------------------ Quad iterator functions ------------------------*/
15599
15600#ifndef DOXYGEN
15601
15602template <int dim, int spacedim>
15605 Triangulation<dim, spacedim>::begin_raw_quad(const unsigned int level) const
15606{
15607 // This function may be called on parallel triangulations on levels
15608 // that exist globally, but not on the local portion of the
15609 // triangulation. In that case, just return the end iterator.
15610 //
15611 // We need to use levels.size() instead of n_levels() because the
15612 // latter function uses the cache, but we need to be able to call
15613 // this function at a time when the cache is not currently up to
15614 // date.
15615 if (level >= levels.size())
15616 {
15617 Assert(level < n_global_levels(),
15618 ExcInvalidLevel(level, n_global_levels()));
15619 return end_quad();
15620 }
15621
15622 switch (dim)
15623 {
15624 case 1:
15625 Assert(false, ExcImpossibleInDim(1));
15626 return raw_hex_iterator();
15627 case 2:
15628 {
15629 // Query whether the given level is valid for the local portion of the
15630 // triangulation.
15631 Assert(level < levels.size(), ExcInvalidLevel(level, levels.size()));
15632
15633 if (level >= levels.size() || levels[level]->cells.n_objects() == 0)
15634 return end_quad();
15635
15636 return raw_quad_iterator(
15637 const_cast<Triangulation<dim, spacedim> *>(this), level, 0);
15638 }
15639
15640 case 3:
15641 {
15642 Assert(level == 0, ExcFacesHaveNoLevel());
15643
15644 return raw_quad_iterator(
15645 const_cast<Triangulation<dim, spacedim> *>(this), 0, 0);
15646 }
15647
15648
15649 default:
15651 return raw_hex_iterator();
15652 }
15653}
15654
15655
15656
15657template <int dim, int spacedim>
15660 Triangulation<dim, spacedim>::begin_quad(const unsigned int level) const
15661{
15662 // level is checked in begin_raw
15663 raw_quad_iterator ri = begin_raw_quad(level);
15664 if (ri.state() != IteratorState::valid)
15665 return ri;
15666 while (ri->used() == false)
15667 if ((++ri).state() != IteratorState::valid)
15668 return ri;
15669 return ri;
15670}
15671
15672
15673
15674template <int dim, int spacedim>
15678 const unsigned int level) const
15679{
15680 // level is checked in begin_raw
15681 quad_iterator i = begin_quad(level);
15682 if (i.state() != IteratorState::valid)
15683 return i;
15684 while (i->has_children())
15685 if ((++i).state() != IteratorState::valid)
15686 return i;
15687 return i;
15688}
15689
15690
15691
15692template <int dim, int spacedim>
15696{
15697 return raw_quad_iterator(const_cast<Triangulation<dim, spacedim> *>(this),
15698 -1,
15699 -1);
15700}
15701
15702#endif
15703
15704/*------------------------ Hex iterator functions ------------------------*/
15705
15706#ifndef DOXYGEN
15707
15708template <int dim, int spacedim>
15711 Triangulation<dim, spacedim>::begin_raw_hex(const unsigned int level) const
15712{
15713 // This function may be called on parallel triangulations on levels
15714 // that exist globally, but not on the local portion of the
15715 // triangulation. In that case, just return the end iterator.
15716 //
15717 // We need to use levels.size() instead of n_levels() because the
15718 // latter function uses the cache, but we need to be able to call
15719 // this function at a time when the cache is not currently up to
15720 // date.
15721 if (level >= levels.size())
15722 {
15723 Assert(level < n_global_levels(),
15724 ExcInvalidLevel(level, n_global_levels()));
15725 return end_hex();
15726 }
15727
15728 switch (dim)
15729 {
15730 case 1:
15731 case 2:
15732 Assert(false, ExcImpossibleInDim(1));
15733 return raw_hex_iterator();
15734 case 3:
15735 {
15736 // Query whether the given level is valid for the local portion of the
15737 // triangulation.
15738 Assert(level < levels.size(), ExcInvalidLevel(level, levels.size()));
15739
15740 if (level >= levels.size() || levels[level]->cells.n_objects() == 0)
15741 return end_hex();
15742
15743 return raw_hex_iterator(
15744 const_cast<Triangulation<dim, spacedim> *>(this), level, 0);
15745 }
15746
15747 default:
15749 return raw_hex_iterator();
15750 }
15751}
15752
15753
15754
15755template <int dim, int spacedim>
15758 Triangulation<dim, spacedim>::begin_hex(const unsigned int level) const
15759{
15760 // level is checked in begin_raw
15761 raw_hex_iterator ri = begin_raw_hex(level);
15762 if (ri.state() != IteratorState::valid)
15763 return ri;
15764 while (ri->used() == false)
15765 if ((++ri).state() != IteratorState::valid)
15766 return ri;
15767 return ri;
15768}
15769
15770
15771
15772template <int dim, int spacedim>
15776{
15777 // level is checked in begin_raw
15778 hex_iterator i = begin_hex(level);
15779 if (i.state() != IteratorState::valid)
15780 return i;
15781 while (i->has_children())
15782 if ((++i).state() != IteratorState::valid)
15783 return i;
15784 return i;
15785}
15786
15787
15788
15789template <int dim, int spacedim>
15793{
15794 return raw_hex_iterator(const_cast<Triangulation<dim, spacedim> *>(this),
15795 -1,
15796 -1);
15797}
15798
15799#endif
15800
15801// -------------------------------- number of cells etc ---------------
15802
15803
15804namespace internal
15805{
15806 namespace TriangulationImplementation
15807 {
15808 unsigned int
15810 {
15811 return c.n_lines;
15812 }
15813
15814
15815 unsigned int
15818 {
15819 return c.n_active_lines;
15820 }
15821
15822
15823 unsigned int
15825 {
15826 return c.n_quads;
15827 }
15828
15829
15830 unsigned int
15833 {
15834 return c.n_active_quads;
15835 }
15836
15837
15838 unsigned int
15840 {
15841 return c.n_hexes;
15842 }
15843
15844
15845 unsigned int
15848 {
15849 return c.n_active_hexes;
15850 }
15851 } // namespace TriangulationImplementation
15852} // namespace internal
15853
15854#ifndef DOXYGEN
15855
15856template <int dim, int spacedim>
15858unsigned int Triangulation<dim, spacedim>::n_cells() const
15859{
15861}
15862
15863
15864template <int dim, int spacedim>
15867{
15869}
15870
15871template <int dim, int spacedim>
15875{
15876 return n_active_cells();
15877}
15878
15879template <int dim, int spacedim>
15883{
15884 return n_cells(0);
15885}
15886
15887template <int dim, int spacedim>
15889unsigned int Triangulation<dim, spacedim>::n_faces() const
15890{
15891 switch (dim)
15892 {
15893 case 1:
15894 return n_used_vertices();
15895 case 2:
15896 return n_lines();
15897 case 3:
15898 return n_quads();
15899 default:
15901 }
15902 return 0;
15903}
15904
15905
15906template <int dim, int spacedim>
15909{
15910 switch (dim)
15911 {
15912 case 1:
15913 return n_vertices();
15914 case 2:
15915 return n_raw_lines();
15916 case 3:
15917 return n_raw_quads();
15918 default:
15920 }
15921 return 0;
15922}
15923
15924
15925template <int dim, int spacedim>
15928{
15929 switch (dim)
15930 {
15931 case 1:
15932 return n_used_vertices();
15933 case 2:
15934 return n_active_lines();
15935 case 3:
15936 return n_active_quads();
15937 default:
15939 }
15940 return 0;
15941}
15942
15943
15944template <int dim, int spacedim>
15947 const unsigned int level) const
15948{
15949 switch (dim)
15950 {
15951 case 1:
15952 return n_raw_lines(level);
15953 case 2:
15954 return n_raw_quads(level);
15955 case 3:
15956 return n_raw_hexs(level);
15957 default:
15959 }
15960 return 0;
15961}
15962
15963
15964
15965template <int dim, int spacedim>
15968 const unsigned int level) const
15969{
15970 switch (dim)
15971 {
15972 case 1:
15973 return n_lines(level);
15974 case 2:
15975 return n_quads(level);
15976 case 3:
15977 return n_hexs(level);
15978 default:
15980 }
15981 return 0;
15982}
15983
15984
15985
15986template <int dim, int spacedim>
15989 const unsigned int level) const
15990{
15991 switch (dim)
15992 {
15993 case 1:
15994 return n_active_lines(level);
15995 case 2:
15996 return n_active_quads(level);
15997 case 3:
15998 return n_active_hexs(level);
15999 default:
16001 }
16002 return 0;
16003}
16004
16005
16006template <int dim, int spacedim>
16009{
16010 if (anisotropic_refinement == false)
16011 {
16012 for (unsigned int lvl = 0; lvl < n_global_levels() - 1; ++lvl)
16013 if (n_active_cells(lvl) != 0)
16014 return true;
16015 }
16016 else
16017 {
16018 for (const auto &cell : active_cell_iterators())
16019 for (const auto &i : cell->face_indices())
16020 if (cell->face(i)->has_children())
16021 return true;
16022 }
16023 return false;
16024}
16025
16026
16027template <int dim, int spacedim>
16029unsigned int Triangulation<dim, spacedim>::n_lines() const
16030{
16031 return number_cache.n_lines;
16032}
16033
16034
16035
16036template <int dim, int spacedim>
16039 const unsigned int level) const
16040{
16041 if (dim == 1)
16042 {
16043 AssertIndexRange(level, n_levels());
16044 return levels[level]->cells.n_objects();
16045 }
16046
16047 Assert(false, ExcFacesHaveNoLevel());
16048 return 0;
16049}
16050
16051
16052template <int dim, int spacedim>
16055{
16056 if (dim == 1)
16057 {
16059 return 0;
16060 }
16061
16062 return faces->lines.n_objects();
16063}
16064
16065
16066template <int dim, int spacedim>
16069 const unsigned int level) const
16070{
16071 AssertIndexRange(level, number_cache.n_lines_level.size());
16072 Assert(dim == 1, ExcFacesHaveNoLevel());
16073 return number_cache.n_lines_level[level];
16074}
16075
16076
16077template <int dim, int spacedim>
16080{
16081 return number_cache.n_active_lines;
16082}
16083
16084
16085template <int dim, int spacedim>
16088 const unsigned int level) const
16089{
16090 AssertIndexRange(level, number_cache.n_lines_level.size());
16091 Assert(dim == 1, ExcFacesHaveNoLevel());
16092
16093 return number_cache.n_active_lines_level[level];
16094}
16095
16096
16097
16098template <int dim, int spacedim>
16100unsigned int Triangulation<dim, spacedim>::n_quads() const
16101{
16102 if constexpr (dim == 1)
16103 return 0;
16104 else
16105 return number_cache.n_quads;
16106}
16107
16108
16109
16110template <int dim, int spacedim>
16113 const unsigned int level) const
16114{
16115 (void)level;
16116 if constexpr (dim == 1)
16117 return 0;
16118 else if constexpr (dim == 2)
16119 {
16120 AssertIndexRange(level, number_cache.n_quads_level.size());
16121 return number_cache.n_quads_level[level];
16122 }
16123 else if constexpr (dim == 3)
16124 Assert(dim < 3, ExcFacesHaveNoLevel());
16125 else
16127
16129}
16130
16131
16132
16133template <int dim, int spacedim>
16136{
16137 if constexpr (dim == 1)
16138 return 0;
16139 else if constexpr (dim == 2)
16141 else if constexpr (dim == 3)
16142 return faces->quads.n_objects();
16143 else
16145
16147}
16148
16149
16150
16151template <int dim, int spacedim>
16154 const unsigned int level) const
16155{
16156 (void)level;
16157 if constexpr (dim == 1)
16158 return 0;
16159 else if constexpr (dim == 2)
16160 {
16161 AssertIndexRange(level, n_levels());
16162 return levels[level]->cells.n_objects();
16163 }
16164 else if constexpr (dim == 3)
16165 Assert(dim < 3, ExcFacesHaveNoLevel());
16166 else
16168
16170}
16171
16172
16173
16174template <int dim, int spacedim>
16177{
16178 if constexpr (dim == 1)
16179 return 0;
16180 else
16181 return number_cache.n_active_quads;
16182}
16183
16184
16185
16186template <int dim, int spacedim>
16189 const unsigned int level) const
16190{
16191 (void)level;
16192 if constexpr (dim == 1)
16193 return 0;
16194 else if constexpr (dim == 2)
16195 {
16196 AssertIndexRange(level, number_cache.n_quads_level.size());
16197 return number_cache.n_active_quads_level[level];
16198 }
16199 else if constexpr (dim == 3)
16200 Assert(dim < 3, ExcFacesHaveNoLevel());
16201 else
16203
16205}
16206
16207
16208
16209template <int dim, int spacedim>
16211unsigned int Triangulation<dim, spacedim>::n_hexs() const
16212{
16213 if constexpr (dim == 3)
16214 return number_cache.n_hexes;
16215 else
16216 return 0;
16217}
16218
16219
16220
16221template <int dim, int spacedim>
16224 const unsigned int level) const
16225{
16226 (void)level;
16227 if constexpr (dim == 3)
16228 {
16229 AssertIndexRange(level, number_cache.n_hexes_level.size());
16230 return number_cache.n_hexes_level[level];
16231 }
16232 else
16233 return 0;
16234}
16235
16236
16237
16238template <int dim, int spacedim>
16241 const unsigned int level) const
16242{
16243 (void)level;
16244 if constexpr (dim == 3)
16245 {
16246 AssertIndexRange(level, n_levels());
16247 return levels[level]->cells.n_objects();
16248 }
16249 else
16250 return 0;
16251}
16252
16253
16254template <int dim, int spacedim>
16257{
16258 if constexpr (dim == 3)
16259 return number_cache.n_active_hexes;
16260 else
16261 return 0;
16262}
16263
16264
16265
16266template <int dim, int spacedim>
16269 const unsigned int level) const
16270{
16271 (void)level;
16272 if constexpr (dim == 3)
16273 {
16274 AssertIndexRange(level, number_cache.n_hexes_level.size());
16275 return number_cache.n_active_hexes_level[level];
16276 }
16277 else
16278 return 0;
16279}
16280
16281
16282
16283template <int dim, int spacedim>
16286{
16287 return std::count(vertices_used.begin(), vertices_used.end(), true);
16288}
16289
16290
16291
16292template <int dim, int spacedim>
16294const std::vector<bool> &Triangulation<dim, spacedim>::get_used_vertices() const
16295{
16296 return vertices_used;
16297}
16298
16299#endif
16300
16301template <>
16302unsigned int
16304{
16305 return 2;
16306}
16307
16308
16309
16310template <>
16311unsigned int
16313{
16314 return 2;
16315}
16316
16317
16318template <>
16319unsigned int
16321{
16322 return 2;
16323}
16324
16325#ifndef DOXYGEN
16326
16327template <int dim, int spacedim>
16330{
16331 cell_iterator cell = begin(0),
16332 endc = (n_levels() > 1 ? begin(1) : cell_iterator(end()));
16333 // store the largest index of the
16334 // vertices used on level 0
16335 unsigned int max_vertex_index = 0;
16336 for (; cell != endc; ++cell)
16337 for (const unsigned int vertex : GeometryInfo<dim>::vertex_indices())
16338 if (cell->vertex_index(vertex) > max_vertex_index)
16339 max_vertex_index = cell->vertex_index(vertex);
16340
16341 // store the number of times a cell
16342 // touches a vertex. An unsigned
16343 // int should suffice, even for
16344 // larger dimensions
16345 std::vector<unsigned short int> usage_count(max_vertex_index + 1, 0);
16346 // touch a vertex's usage count
16347 // every time we find an adjacent
16348 // element
16349 for (cell = begin(); cell != endc; ++cell)
16350 for (const unsigned int vertex : GeometryInfo<dim>::vertex_indices())
16351 ++usage_count[cell->vertex_index(vertex)];
16352
16354 static_cast<unsigned int>(
16355 *std::max_element(usage_count.begin(), usage_count.end())));
16356}
16357
16358
16359
16360template <int dim, int spacedim>
16364{
16366}
16367
16368
16369
16370template <int dim, int spacedim>
16373{
16374 return *this;
16375}
16376
16377
16378
16379template <int dim, int spacedim>
16383{
16384 return *this;
16385}
16386
16387
16388
16389template <int dim, int spacedim>
16393 &periodicity_vector)
16394{
16395 periodic_face_pairs_level_0.insert(periodic_face_pairs_level_0.end(),
16396 periodicity_vector.begin(),
16397 periodicity_vector.end());
16398
16399 // Now initialize periodic_face_map
16400 update_periodic_face_map();
16401}
16402
16403
16404
16405template <int dim, int spacedim>
16407const typename std::map<
16408 std::pair<typename Triangulation<dim, spacedim>::cell_iterator, unsigned int>,
16409 std::pair<std::pair<typename Triangulation<dim, spacedim>::cell_iterator,
16410 unsigned int>,
16413{
16414 return periodic_face_map;
16415}
16416
16417
16418template <int dim, int spacedim>
16421{
16422 // We only update the cell relations here for serial triangulations.
16423 // For other triangulations, this is done at other stages of
16424 // mesh creation and mesh refinement.
16426 this))
16427 return;
16428
16429 this->local_cell_relations.clear();
16430 this->local_cell_relations.reserve(this->n_active_cells());
16431
16432 for (const auto &cell : this->active_cell_iterators())
16433 this->local_cell_relations.emplace_back(
16434 cell, ::CellStatus::cell_will_persist);
16435}
16436
16437
16438
16439template <int dim, int spacedim>
16442{
16444 this))
16445 return;
16446
16447 // pack data before triangulation gets updated
16448 if (this->cell_attached_data.n_attached_data_sets > 0)
16449 {
16450 this->local_cell_relations.clear();
16451
16452 for (const auto &cell : this->active_cell_iterators())
16453 {
16454 const bool children_will_be_coarsened =
16455 (cell->level() > 0) && (cell->coarsen_flag_set());
16456
16457 if (children_will_be_coarsened == false)
16458 {
16459 ::CellStatus status =
16460 cell->refine_flag_set() ?
16463
16464 local_cell_relations.emplace_back(cell, status);
16465 }
16466 else if (cell->parent()->child_iterator_to_index(cell) == 0)
16467 {
16468 local_cell_relations.emplace_back(
16469 cell->parent(),
16471
16472 for (unsigned int i = 1; i < cell->parent()->n_children(); ++i)
16473 local_cell_relations.emplace_back(
16474 cell->parent(), ::CellStatus::cell_invalid);
16475 }
16476 }
16477
16478 // pack data
16479 this->data_serializer.pack_data(
16480 this->local_cell_relations,
16481 this->cell_attached_data.pack_callbacks_fixed,
16482 this->cell_attached_data.pack_callbacks_variable,
16483 this->get_mpi_communicator());
16484
16485 // dummy copy of data
16486 this->data_serializer.dest_data_fixed =
16487 this->data_serializer.src_data_fixed;
16488 this->data_serializer.dest_data_variable =
16489 this->data_serializer.src_data_variable;
16490 this->data_serializer.dest_sizes_variable =
16491 this->data_serializer.src_sizes_variable;
16492 }
16493}
16494
16495
16496
16497template <int dim, int spacedim>
16500{
16502 this))
16503 return;
16504
16505 // transfer data after triangulation got updated
16506 if (this->cell_attached_data.n_attached_data_sets > 0)
16507 {
16508 std::vector<typename internal::CellAttachedDataSerializer<dim, spacedim>::
16509 cell_relation_t>
16510 temp;
16511
16512 for (const auto &cell : local_cell_relations)
16513 {
16514 if (cell.first->has_children())
16515 {
16518
16519 temp.emplace_back(cell.first->child(0),
16521 }
16522 else
16523 temp.push_back(cell);
16524 }
16525
16526 this->local_cell_relations = std::move(temp);
16527 }
16528}
16529
16530
16531
16532template <int dim, int spacedim>
16535{
16536 // Call our version of prepare_coarsening_and_refinement() even if a derived
16537 // class like parallel::distributed::Triangulation overrides it. Their
16538 // function will be called in their execute_coarsening_and_refinement()
16539 // function. Even in a distributed computation our job here is to reconstruct
16540 // the local part of the mesh and as such checking our flags is enough.
16542
16543 // verify a case with which we have had
16544 // some difficulty in the past (see the
16545 // deal.II/coarsening_* tests)
16546 if (smooth_grid & limit_level_difference_at_vertices)
16547 Assert(satisfies_level1_at_vertex_rule(*this), ExcInternalError());
16548
16549 // Inform all listeners about beginning of refinement.
16550 signals.pre_refinement();
16551
16552 this->pack_data_serial();
16553
16554 execute_coarsening();
16555
16556 const DistortedCellList cells_with_distorted_children = execute_refinement();
16557
16558 // We need to update the cell relations in order to be able to
16559 // deserialize data. Later on, update_cell_relations is called to mark all
16560 // active cells with the cell_will_persist status.
16561 this->unpack_data_serial();
16562
16563 reset_cell_vertex_indices_cache();
16564
16565 // If the line_to_adjacent_cell_map is populated, clear it, as it must
16566 // be recomputed every time the mesh changes.
16567 line_to_adjacent_cells_map.reset();
16568
16569 // verify a case with which we have had
16570 // some difficulty in the past (see the
16571 // deal.II/coarsening_* tests)
16572 if (smooth_grid & limit_level_difference_at_vertices)
16573 Assert(satisfies_level1_at_vertex_rule(*this) == true, ExcInternalError());
16574
16575 // finally build up neighbor connectivity information, and set
16576 // active cell indices
16577 this->policy->update_neighbors(*this);
16578 reset_active_cell_indices();
16579
16580 reset_global_cell_indices(); // TODO: better place?
16581
16582 // Inform all listeners about end of refinement.
16583 signals.post_refinement();
16584
16585 AssertThrow(cells_with_distorted_children.distorted_cells.empty(),
16586 cells_with_distorted_children);
16587
16588 update_periodic_face_map();
16589
16590 if (this->cell_attached_data.n_attached_data_sets == 0)
16591 this->update_cell_relations();
16592
16593 if constexpr (running_in_debug_mode())
16594 {
16595 // In debug mode, we want to check for some consistency of the
16596 // result of this function. Because there are multiple exit
16597 // paths, put this check into a ScopeExit object that is
16598 // executed on each of the exit paths.
16599 //
16600 // Specifically, check on exit of this function that if a quad
16601 // cell has been refined, all of its children have neighbors
16602 // in all directions in which the parent cell has neighbors as
16603 // well. The children's neighbors are either the parent
16604 // neighbor or the parent neighbor's children, or simply one of
16605 // the other children of the current cell. This check is
16606 // useful because if one creates a triangulation with an
16607 // inconsistently ordered set of cells (e.g., because one has
16608 // forgotten to call GridTools::consistently_order_cells()),
16609 // then this relatively simple invariant is violated -- so the
16610 // check here can be used to catch that case, at least
16611 // sometimes.
16612 //
16613 // In 1d, this situation cannot happen. In 3d, we have explicit
16614 // orientation flags to ensure that it is not necessary to re-orient
16615 // cells at the beginning. But in both cases, the invariant should
16616 // still hold as long as the cell is a hypercube.
16617 for (const auto &cell : cell_iterators())
16618 {
16619 if (cell->has_children() && cell->reference_cell().is_hyper_cube())
16620 for (const unsigned int f : cell->face_indices())
16621 if (cell->at_boundary(f) == false)
16622 {
16623 for (const auto &child : cell->child_iterators())
16624 {
16625 Assert(
16626 child->at_boundary(f) == false,
16627 ExcMessage(
16628 "We ended up with a triangulation whose child cells "
16629 "are not connected to their neighbors as expected. "
16630 "When you created the triangulation, did you forget "
16631 "to call GridTools::consistently_order_cells() "
16632 "before calling Triangulation::create_triangulation()?"));
16633 }
16634 }
16635 }
16636 }
16637}
16638
16639
16640
16641template <int dim, int spacedim>
16644{
16645 unsigned int active_cell_index = 0;
16646 for (raw_cell_iterator cell = begin_raw(); cell != end(); ++cell)
16647 if ((cell->used() == false) || cell->has_children())
16648 cell->set_active_cell_index(numbers::invalid_unsigned_int);
16649 else
16650 {
16651 cell->set_active_cell_index(active_cell_index);
16652 ++active_cell_index;
16653 }
16654
16655 Assert(active_cell_index == n_active_cells(), ExcInternalError());
16656}
16657
16658
16659
16660template <int dim, int spacedim>
16663{
16664 types::global_cell_index active_cell_index = 0;
16665 for (unsigned int l = 0; l < levels.size(); ++l)
16666 {
16667 types::global_cell_index level_cell_index = 0;
16668 for (const auto &cell : cell_iterators_on_level(l))
16669 {
16670 cell->set_global_level_cell_index(level_cell_index++);
16671 if (cell->is_active())
16672 cell->set_global_active_cell_index(active_cell_index++);
16673 }
16674 }
16675 AssertDimension(active_cell_index, this->n_active_cells());
16676}
16677
16678
16679
16680template <int dim, int spacedim>
16683{
16684 for (unsigned int l = 0; l < levels.size(); ++l)
16685 for (const auto &cell : cell_iterators_on_level(l))
16686 {
16687 const unsigned int n_vertices = cell->n_vertices();
16688
16690 ReferenceCells::max_n_vertices<dim>()>
16691 cell_vertices(n_vertices);
16692 if constexpr (running_in_debug_mode())
16693 std::fill(cell_vertices.begin(),
16694 cell_vertices.end(),
16696
16697 GridTools::internal::extract_vertices_without_cache<dim, spacedim>(
16698 cell, cell_vertices);
16699 for (unsigned int vertex_no = 0; vertex_no < n_vertices; ++vertex_no)
16700 levels[l]->set_cached_vertex_index(cell->index(),
16701 vertex_no,
16702 cell_vertices[vertex_no]);
16703 }
16704}
16705
16706
16707
16708template <int dim, int spacedim>
16711{
16712 // first empty the currently stored objects
16713 periodic_face_map.clear();
16714
16715 typename std::vector<
16717 for (it = periodic_face_pairs_level_0.begin();
16718 it != periodic_face_pairs_level_0.end();
16719 ++it)
16720 {
16721 update_periodic_face_map_recursively<dim, spacedim>(it->cell[0],
16722 it->cell[1],
16723 it->face_idx[0],
16724 it->face_idx[1],
16725 it->orientation,
16726 periodic_face_map);
16727
16728 const auto face_reference_cell =
16729 it->cell[0]->reference_cell().face_reference_cell(it->face_idx[0]);
16730 // for the other way, we need to invert the orientation
16731 update_periodic_face_map_recursively<dim, spacedim>(
16732 it->cell[1],
16733 it->cell[0],
16734 it->face_idx[1],
16735 it->face_idx[0],
16736 face_reference_cell.get_inverse_combined_orientation(it->orientation),
16737 periodic_face_map);
16738 }
16739
16740 // check consistency
16741 typename std::map<std::pair<cell_iterator, unsigned int>,
16742 std::pair<std::pair<cell_iterator, unsigned int>,
16743 types::geometric_orientation>>::const_iterator
16744 it_test;
16745 for (it_test = periodic_face_map.begin(); it_test != periodic_face_map.end();
16746 ++it_test)
16747 {
16749 it_test->first.first;
16751 it_test->second.first.first;
16752 if (cell_1->level() == cell_2->level())
16753 {
16754 // if both cells have the same neighbor, then the same pair
16755 // order swapped has to be in the map
16756 Assert(periodic_face_map[it_test->second.first].first ==
16757 it_test->first,
16759 }
16760 }
16761}
16762
16763
16764
16765template <int dim, int spacedim>
16767const std::vector<ReferenceCell<dim>>
16769{
16770 return this->reference_cells;
16771}
16772
16773
16774
16775template <int dim, int spacedim>
16778{
16779 Assert(this->reference_cells.size() > 0,
16780 ExcMessage("You can't ask about the kinds of reference "
16781 "cells used by this triangulation if the "
16782 "triangulation doesn't yet have any cells in it."));
16783 return (this->reference_cells.size() == 1 &&
16784 this->reference_cells[0].is_hyper_cube());
16785}
16786
16787
16788
16789template <int dim, int spacedim>
16792{
16793 Assert(this->reference_cells.size() > 0,
16794 ExcMessage("You can't ask about the kinds of reference "
16795 "cells used by this triangulation if the "
16796 "triangulation doesn't yet have any cells in it."));
16797 return (this->reference_cells.size() == 1 &&
16798 this->reference_cells[0].is_simplex());
16799}
16800
16801
16802
16803template <int dim, int spacedim>
16806{
16807 Assert(this->reference_cells.size() > 0,
16808 ExcMessage("You can't ask about the kinds of reference "
16809 "cells used by this triangulation if the "
16810 "triangulation doesn't yet have any cells in it."));
16811 return reference_cells.size() > 1 ||
16812 ((reference_cells[0].is_hyper_cube() == false) &&
16813 (reference_cells[0].is_simplex() == false));
16814}
16815
16816
16817
16818template <int dim, int spacedim>
16821 const std::function<std::vector<char>(const cell_iterator &,
16822 const ::CellStatus)>
16823 &pack_callback,
16824 const bool returns_variable_size_data)
16825{
16826 unsigned int handle = numbers::invalid_unsigned_int;
16827
16828 // Add new callback function to the corresponding register.
16829 // Encode handles according to returns_variable_size_data.
16830 if (returns_variable_size_data)
16831 {
16832 handle = 2 * this->cell_attached_data.pack_callbacks_variable.size();
16833 this->cell_attached_data.pack_callbacks_variable.push_back(pack_callback);
16834 }
16835 else
16836 {
16837 handle = 2 * this->cell_attached_data.pack_callbacks_fixed.size() + 1;
16838 this->cell_attached_data.pack_callbacks_fixed.push_back(pack_callback);
16839 }
16840
16841 // Increase overall counter.
16842 ++this->cell_attached_data.n_attached_data_sets;
16843
16844 return handle;
16845}
16846
16847
16848
16849template <int dim, int spacedim>
16852 const unsigned int handle,
16853 const std::function<
16854 void(const cell_iterator &,
16855 const ::CellStatus,
16856 const boost::iterator_range<std::vector<char>::const_iterator> &)>
16857 &unpack_callback)
16858{
16859 // perform unpacking
16860 this->data_serializer.unpack_data(this->local_cell_relations,
16861 handle,
16862 unpack_callback);
16863
16864 // decrease counters
16865 --this->cell_attached_data.n_attached_data_sets;
16866 if (this->cell_attached_data.n_attached_deserialize > 0)
16867 --this->cell_attached_data.n_attached_deserialize;
16868
16869 // important: only remove data if we are not in the deserialization
16870 // process. There, each SolutionTransfer registers and unpacks before
16871 // the next one does this, so n_attached_data_sets is only 1 here. This
16872 // would destroy the saved data before the second SolutionTransfer can
16873 // get it. This created a bug that is documented in
16874 // tests/mpi/p4est_save_03 with more than one SolutionTransfer.
16875
16876 if (this->cell_attached_data.n_attached_data_sets == 0 &&
16877 this->cell_attached_data.n_attached_deserialize == 0)
16878 {
16879 // everybody got their data, time for cleanup!
16880 this->cell_attached_data.pack_callbacks_fixed.clear();
16881 this->cell_attached_data.pack_callbacks_variable.clear();
16882 this->data_serializer.clear();
16883
16884 // reset all cell_status entries after coarsening/refinement
16885 for (auto &cell_rel : this->local_cell_relations)
16886 cell_rel.second = ::CellStatus::cell_will_persist;
16887 }
16888}
16889
16890
16891
16892template <int dim, int spacedim>
16895 const unsigned int global_first_cell,
16896 const unsigned int global_num_cells,
16897 const std::string &file_basename) const
16898{
16899 // cast away constness
16900 auto tria = const_cast<Triangulation<dim, spacedim> *>(this);
16901
16902 // each cell should have been flagged `CellStatus::cell_will_persist`
16903 for (const auto &cell_rel : this->local_cell_relations)
16904 {
16905 (void)cell_rel;
16906 Assert((cell_rel.second == // cell_status
16909 }
16910
16911 if (this->cell_attached_data.n_attached_data_sets > 0)
16912 {
16913 // pack attached data first
16914 tria->data_serializer.pack_data(
16915 tria->local_cell_relations,
16916 tria->cell_attached_data.pack_callbacks_fixed,
16917 tria->cell_attached_data.pack_callbacks_variable,
16918 this->get_mpi_communicator());
16919
16920 // then store buffers in file
16921 tria->data_serializer.save(global_first_cell,
16922 global_num_cells,
16923 file_basename,
16924 this->get_mpi_communicator());
16925
16926 // and release the memory afterwards
16927 tria->data_serializer.clear();
16928 }
16929
16930 // clear all of the callback data, as explained in the documentation of
16931 // register_data_attach()
16932 {
16933 tria->cell_attached_data.n_attached_data_sets = 0;
16934 tria->cell_attached_data.pack_callbacks_fixed.clear();
16935 tria->cell_attached_data.pack_callbacks_variable.clear();
16936 }
16937}
16938
16939
16940template <int dim, int spacedim>
16943 const unsigned int global_first_cell,
16944 const unsigned int global_num_cells,
16945 const unsigned int local_num_cells,
16946 const std::string &file_basename,
16947 const unsigned int n_attached_deserialize_fixed,
16948 const unsigned int n_attached_deserialize_variable)
16949{
16950 // load saved data, if any was stored
16951 if (this->cell_attached_data.n_attached_deserialize > 0)
16952 {
16953 this->data_serializer.load(global_first_cell,
16954 global_num_cells,
16955 local_num_cells,
16956 file_basename,
16957 n_attached_deserialize_fixed,
16958 n_attached_deserialize_variable,
16959 this->get_mpi_communicator());
16960
16961 this->data_serializer.unpack_cell_status(this->local_cell_relations);
16962
16963 if constexpr (running_in_debug_mode())
16964 {
16965 // the CellStatus of all stored cells should always be
16966 // CellStatus::cell_will_persist.
16967 for (const auto &cell_rel : this->local_cell_relations)
16968 {
16969 Assert((cell_rel.second == // cell_status
16972 }
16973 }
16974 }
16975}
16976
16977
16978template <int dim, int spacedim>
16981{
16982 levels.clear();
16983 faces.reset();
16984
16985 vertices.clear();
16986 vertices_used.clear();
16987
16988 manifolds.clear();
16989
16990 // In 1d, also reset vertex-to-(boundary|manifold) maps to empty maps
16991 if (dim == 1)
16992 {
16993 Assert(vertex_to_boundary_id_map_1d != nullptr, ExcInternalError());
16994 vertex_to_boundary_id_map_1d->clear();
16995
16996 Assert(vertex_to_manifold_id_map_1d != nullptr, ExcInternalError());
16997 vertex_to_manifold_id_map_1d->clear();
16998 }
16999 else
17000 {
17001 // For dim>1, these maps should simply not exist.
17002 Assert(vertex_to_boundary_id_map_1d == nullptr, ExcInternalError());
17003 Assert(vertex_to_manifold_id_map_1d == nullptr, ExcInternalError());
17004 }
17005
17006
17008}
17009
17010
17011
17012template <int dim, int spacedim>
17016{
17017 const DistortedCellList cells_with_distorted_children =
17018 this->policy->execute_refinement(*this, check_for_distorted_cells);
17019
17020
17021
17022 // re-compute number of lines
17024 *this, levels.size(), number_cache);
17025
17026 if constexpr (running_in_debug_mode())
17027 {
17028 for (const auto &level : levels)
17029 monitor_memory(level->cells, dim);
17030
17031 // check whether really all refinement flags are reset (also of
17032 // previously non-active cells which we may not have touched. If the
17033 // refinement flag of a non-active cell is set, something went wrong
17034 // since the cell-accessors should have caught this)
17035 for (const auto &cell : this->cell_iterators())
17036 Assert(!cell->refine_flag_set(), ExcInternalError());
17037 }
17038
17039 return cells_with_distorted_children;
17040}
17041
17042
17043
17044template <int dim, int spacedim>
17047{
17048 // first find out if there are any cells at all to be coarsened in the
17049 // loop below
17050 const cell_iterator endc = end();
17051 bool do_coarsen = false;
17052 if (levels.size() >= 2)
17053 for (cell_iterator cell = begin(n_levels() - 1); cell != endc; --cell)
17054 if (!cell->is_active() && cell->child(0)->coarsen_flag_set())
17055 {
17056 do_coarsen = true;
17057 break;
17058 }
17059
17060 if (!do_coarsen)
17061 return;
17062
17063 // create a vector counting for each line and quads how many cells contain
17064 // the respective object. this is used later to decide which lines can be
17065 // deleted after coarsening a cell.
17066 std::vector<unsigned int> line_cell_count(dim > 1 ? this->n_raw_lines() : 0);
17067 std::vector<unsigned int> quad_cell_count(dim > 2 ? this->n_raw_quads() : 0);
17068 if (dim > 1)
17069 for (const auto &cell : this->cell_iterators())
17070 {
17071 if (dim > 2)
17072 {
17073 const auto line_indices = internal::TriaAccessorImplementation::
17074 Implementation::get_line_indices_of_cell(*cell);
17075 // avoid a compiler warning by fixing the max number of
17076 // loop iterations to 12
17077 const unsigned int n_lines = std::min(cell->n_lines(), 12u);
17078 for (unsigned int l = 0; l < n_lines; ++l)
17079 ++line_cell_count[line_indices[l]];
17080 for (const unsigned int q : cell->face_indices())
17081 ++quad_cell_count[cell->face_index(q)];
17082 }
17083 else
17084 for (unsigned int l = 0; l < cell->n_lines(); ++l)
17085 ++line_cell_count[cell->line(l)->index()];
17086 }
17087
17088 // Since the loop goes over used cells we only need not worry about
17089 // deleting some cells since the ++operator will then just hop over them
17090 // if we should hit one. Do the loop in the reverse way since we may
17091 // only delete some cells if their neighbors have already been deleted
17092 // (if the latter are on a higher level for example). In effect, only
17093 // those cells are deleted of which originally all children were flagged
17094 // and for which all children are on the same refinement level. Note
17095 // that because of the effects of
17096 // @p{fix_coarsen_flags}, of a cell either all or no children must be
17097 // flagged for coarsening, so it is ok to only check the first child
17098 //
17099 // since we delete the *children* of cells, we can ignore cells on the
17100 // highest level, i.e., level must be less than or equal to
17101 // n_levels()-2.
17102 if (levels.size() >= 2)
17103 for (cell_iterator cell = begin(n_levels() - 1); cell != endc; --cell)
17104 if (!cell->is_active() && cell->child(0)->coarsen_flag_set())
17105 {
17106 for (unsigned int child = 0; child < cell->n_children(); ++child)
17107 {
17108 Assert(cell->child(child)->coarsen_flag_set(),
17110 cell->child(child)->clear_coarsen_flag();
17111 }
17112 // inform all listeners that cell coarsening is going to happen
17113 signals.pre_coarsening_on_cell(cell);
17114 // use a separate function, since this is dimension specific
17115 this->policy->delete_children(*this,
17116 cell,
17117 line_cell_count,
17118 quad_cell_count);
17119 }
17120
17121 // re-compute number of lines and quads
17123 *this, levels.size(), number_cache);
17124}
17125
17126
17127
17128template <int dim, int spacedim>
17131{
17132 // copy a piece of code from prepare_coarsening_and_refinement that
17133 // ensures that the level difference at vertices is limited if so
17134 // desired. we need this code here since at least in 1d we don't
17135 // call the dimension-independent version of
17136 // prepare_coarsening_and_refinement function. in 2d and 3d, having
17137 // this hunk here makes our lives a bit easier as well as it takes
17138 // care of these cases earlier than it would otherwise happen.
17139 //
17140 // the main difference to the code in p_c_and_r is that here we
17141 // absolutely have to make sure that we get things right, i.e. that
17142 // in particular we set flags right if
17143 // limit_level_difference_at_vertices is set. to do so we iterate
17144 // until the flags don't change any more
17145 auto previous_coarsen_flags = internal::extract_raw_coarsen_flags(levels);
17146
17147 bool continue_iterating = true;
17148
17149 do
17150 {
17151 if (smooth_grid & limit_level_difference_at_vertices)
17152 {
17153 Assert(!anisotropic_refinement,
17154 ExcMessage("In case of anisotropic refinement the "
17155 "limit_level_difference_at_vertices flag for "
17156 "mesh smoothing must not be set!"));
17157
17158 // store highest level one of the cells adjacent to a vertex
17159 // belongs to
17160 std::vector<int> vertex_level(vertices.size(), 0);
17161 for (const auto &cell : this->active_cell_iterators())
17162 {
17163 if (cell->refine_flag_set())
17164 for (const unsigned int vertex : cell->vertex_indices())
17165 vertex_level[cell->vertex_index(vertex)] =
17166 std::max(vertex_level[cell->vertex_index(vertex)],
17167 cell->level() + 1);
17168 else if (!cell->coarsen_flag_set())
17169 for (const unsigned int vertex : cell->vertex_indices())
17170 vertex_level[cell->vertex_index(vertex)] =
17171 std::max(vertex_level[cell->vertex_index(vertex)],
17172 cell->level());
17173 else
17174 {
17175 // if coarsen flag is set then tentatively assume
17176 // that the cell will be coarsened. this isn't
17177 // always true (the coarsen flag could be removed
17178 // again) and so we may make an error here. we try
17179 // to correct this by iterating over the entire
17180 // process until we are converged
17181 Assert(cell->coarsen_flag_set(), ExcInternalError());
17182 for (const unsigned int vertex : cell->vertex_indices())
17183 vertex_level[cell->vertex_index(vertex)] =
17184 std::max(vertex_level[cell->vertex_index(vertex)],
17185 cell->level() - 1);
17186 }
17187 }
17188
17189
17190 // loop over all cells in reverse order. do so because we
17191 // can then update the vertex levels on the adjacent
17192 // vertices and maybe already flag additional cells in this
17193 // loop
17194 //
17195 // note that not only may we have to add additional
17196 // refinement flags, but we will also have to remove
17197 // coarsening flags on cells adjacent to vertices that will
17198 // see refinement
17199 active_cell_iterator endc = end();
17200 for (active_cell_iterator cell = last_active(); cell != endc; --cell)
17201 if (cell->refine_flag_set() == false)
17202 {
17203 for (const unsigned int vertex : cell->vertex_indices())
17204 if (vertex_level[cell->vertex_index(vertex)] >=
17205 cell->level() + 1)
17206 {
17207 // remove coarsen flag...
17208 cell->clear_coarsen_flag();
17209
17210 // ...and if necessary also refine the current
17211 // cell, at the same time updating the level
17212 // information about vertices
17213 if (vertex_level[cell->vertex_index(vertex)] >
17214 cell->level() + 1)
17215 {
17216 cell->set_refine_flag();
17217
17218 for (const unsigned int v : cell->vertex_indices())
17219 vertex_level[cell->vertex_index(v)] =
17220 std::max(vertex_level[cell->vertex_index(v)],
17221 cell->level() + 1);
17222 }
17223
17224 // continue and see whether we may, for example,
17225 // go into the inner 'if' above based on a
17226 // different vertex
17227 }
17228 }
17229 }
17230
17231 // loop over all cells and remove the coarsen flags for those cells that
17232 // have sister cells not marked for coarsening, or where some neighbors
17233 // are more refined.
17234
17235 // Coarsen flags of cells with no mother cell, i.e. on the
17236 // coarsest level, are deleted explicitly.
17237 for (const auto &acell : this->active_cell_iterators_on_level(0))
17238 acell->clear_coarsen_flag();
17239
17240 const cell_iterator endc = end();
17241 for (cell_iterator cell = begin(n_levels() - 1); cell != endc; --cell)
17242 {
17243 // nothing to do if we are already on the finest level
17244 if (cell->is_active())
17245 continue;
17246
17247 const unsigned int n_children = cell->n_children();
17248 unsigned int flagged_children = 0;
17249 for (unsigned int child = 0; child < n_children; ++child)
17250 {
17251 const auto child_cell = cell->child(child);
17252 if (child_cell->is_active() && child_cell->coarsen_flag_set())
17253 {
17254 ++flagged_children;
17255 // clear flag since we don't need it anymore
17256 child_cell->clear_coarsen_flag();
17257 }
17258 }
17259
17260 // flag the children for coarsening again if all children were
17261 // flagged and if the policy allows it
17262 if (flagged_children == n_children &&
17263 this->policy->coarsening_allowed(cell))
17264 for (unsigned int c = 0; c < n_children; ++c)
17265 {
17266 Assert(cell->child(c)->refine_flag_set() == false,
17268
17269 cell->child(c)->set_coarsen_flag();
17270 }
17271 }
17272
17273 // now see if anything has changed in the last iteration of this
17274 // function
17275 auto current_coarsen_flags = internal::extract_raw_coarsen_flags(levels);
17276
17277 continue_iterating = (current_coarsen_flags != previous_coarsen_flags);
17278 previous_coarsen_flags.swap(current_coarsen_flags);
17279 }
17280 while (continue_iterating == true);
17281}
17282
17283#endif
17284
17285// TODO: merge the following 3 functions since they are the same
17286template <>
17287bool
17289{
17290 // save the flags to determine whether something was changed in the
17291 // course of this function
17292 const auto flags_before = internal::extract_raw_coarsen_flags(levels);
17293
17294 // do nothing in 1d, except setting the coarsening flags correctly
17295 fix_coarsen_flags();
17296
17297 const auto flags_after = internal::extract_raw_coarsen_flags(levels);
17298
17299 return (flags_before != flags_after);
17300}
17301
17302
17303
17304template <>
17305bool
17307{
17308 // save the flags to determine whether something was changed in the
17309 // course of this function
17310 const auto flags_before = internal::extract_raw_coarsen_flags(levels);
17311
17312 // do nothing in 1d, except setting the coarsening flags correctly
17313 fix_coarsen_flags();
17314
17315 const auto flags_after = internal::extract_raw_coarsen_flags(levels);
17316
17317 return (flags_before != flags_after);
17318}
17319
17320
17321
17322template <>
17323bool
17325{
17326 // save the flags to determine whether something was changed in the
17327 // course of this function
17328 const auto flags_before = internal::extract_raw_coarsen_flags(levels);
17329
17330 // do nothing in 1d, except setting the coarsening flags correctly
17331 fix_coarsen_flags();
17332
17333 const auto flags_after = internal::extract_raw_coarsen_flags(levels);
17334
17335 return (flags_before != flags_after);
17336}
17337
17338
17339
17340namespace
17341{
17342 // check if the given @param cell marked for coarsening would
17343 // produce an unrefined island. To break up long chains of these
17344 // cells we recursively check our neighbors in case we change this
17345 // cell. This reduces the number of outer iterations dramatically.
17346 template <int dim, int spacedim>
17347 void
17348 possibly_do_not_produce_unrefined_islands(
17350 {
17351 Assert(cell->has_children(), ExcInternalError());
17352
17353 unsigned int n_neighbors = 0;
17354 // count all neighbors that will be refined along the face of our
17355 // cell after the next step
17356 unsigned int count = 0;
17357 for (const unsigned int n : GeometryInfo<dim>::face_indices())
17358 {
17359 const typename Triangulation<dim, spacedim>::cell_iterator neighbor =
17360 cell->neighbor(n);
17361 if (neighbor.state() == IteratorState::valid)
17362 {
17363 ++n_neighbors;
17364 if (face_will_be_refined_by_neighbor(cell, n))
17365 ++count;
17366 }
17367 }
17368 // clear coarsen flags if either all existing neighbors will be
17369 // refined or all but one will be and the cell is in the interior
17370 // of the domain
17371 if (count == n_neighbors ||
17372 (count >= n_neighbors - 1 &&
17373 n_neighbors == GeometryInfo<dim>::faces_per_cell))
17374 {
17375 for (unsigned int c = 0; c < cell->n_children(); ++c)
17376 cell->child(c)->clear_coarsen_flag();
17377
17378 for (const unsigned int face : GeometryInfo<dim>::face_indices())
17379 if (!cell->at_boundary(face) &&
17380 (!cell->neighbor(face)->is_active()) &&
17381 (cell_will_be_coarsened(cell->neighbor(face))))
17382 possibly_do_not_produce_unrefined_islands<dim, spacedim>(
17383 cell->neighbor(face));
17384 }
17385 }
17386
17387
17388 // see if the current cell needs to be refined to avoid unrefined
17389 // islands.
17390 //
17391 // there are sometimes chains of cells that induce refinement of
17392 // each other. to avoid running the loop in
17393 // prepare_coarsening_and_refinement over and over again for each
17394 // one of them, at least for the isotropic refinement case we seek
17395 // to flag neighboring elements as well as necessary. this takes
17396 // care of (slightly pathological) cases like
17397 // deal.II/mesh_smoothing_03
17398 template <int dim, int spacedim>
17399 void
17400 possibly_refine_unrefined_island(
17402 const bool allow_anisotropic_smoothing)
17403 {
17404 Assert(cell->is_active(), ExcInternalError());
17405
17406 if constexpr (running_in_debug_mode())
17407 {
17408 // If this is not a parallel::distributed::Triangulation, then we really
17409 // should only get here if the cell is marked for refinement:
17410 if (dynamic_cast<
17412 &cell->get_triangulation()) == nullptr)
17413 Assert(cell->refine_flag_set() == false, ExcInternalError());
17414 else
17415 // But if this is a p::d::Triangulation, then we don't have that
17416 // much control and we can get here because mesh smoothing is
17417 // requested but can not be honored because p4est controls
17418 // what gets refined. In that case, we can at least provide
17419 // a better error message.
17420 Assert(
17421 cell->refine_flag_set() == false,
17422 ExcMessage(
17423 "The triangulation is trying to avoid unrefined islands "
17424 "during mesh refinement/coarsening, as you had requested "
17425 " by passing the appropriate 'smoothing flags' to the "
17426 "constructor of the triangulation. However, for objects "
17427 "of type parallel::distributed::Triangulation, control "
17428 "over which cells get refined rests with p4est, not the "
17429 "deal.II triangulation, and consequently it is not "
17430 "always possible to avoid unrefined islands in the mesh. "
17431 "Please remove the constructor argument to the triangulation "
17432 "object that requests mesh smoothing."));
17433 }
17434
17435 // now we provide two algorithms. the first one is the standard
17436 // one, coming from the time, where only isotropic refinement was
17437 // possible. it simply counts the neighbors that are or will be
17438 // refined and compares to the number of other ones. the second
17439 // one does this check independently for each direction: if all
17440 // neighbors in one direction (normally two, at the boundary only
17441 // one) are refined, the current cell is flagged to be refined in
17442 // an according direction.
17443
17444 if (allow_anisotropic_smoothing == false)
17445 {
17446 // use first algorithm
17447 unsigned int refined_neighbors = 0, unrefined_neighbors = 0;
17448 for (const unsigned int face : GeometryInfo<dim>::face_indices())
17449 if (!cell->at_boundary(face))
17450 {
17451 if (face_will_be_refined_by_neighbor(cell, face))
17452 ++refined_neighbors;
17453 else
17454 ++unrefined_neighbors;
17455 }
17456
17457 if (unrefined_neighbors < refined_neighbors)
17458 {
17459 cell->clear_coarsen_flag();
17460 cell->set_refine_flag();
17461
17462 // ok, so now we have flagged this cell. if we know that
17463 // there were any unrefined neighbors at all, see if any
17464 // of those will have to be refined as well
17465 if (unrefined_neighbors > 0)
17466 for (const unsigned int face : GeometryInfo<dim>::face_indices())
17467 if (!cell->at_boundary(face) &&
17468 (face_will_be_refined_by_neighbor(cell, face) == false) &&
17469 (cell->neighbor(face)->has_children() == false) &&
17470 (cell->neighbor(face)->refine_flag_set() == false))
17471 possibly_refine_unrefined_island<dim, spacedim>(
17472 cell->neighbor(face), allow_anisotropic_smoothing);
17473 }
17474 }
17475 else
17476 {
17477 // variable to store the cell refine case needed to fulfill
17478 // all smoothing requirements
17479 RefinementCase<dim> smoothing_cell_refinement_case =
17481
17482 // use second algorithm, do the check individually for each
17483 // direction
17484 for (unsigned int face_pair = 0;
17485 face_pair < GeometryInfo<dim>::faces_per_cell / 2;
17486 ++face_pair)
17487 {
17488 // variable to store the cell refine case needed to refine
17489 // at the current face pair in the same way as the
17490 // neighbors do...
17491 RefinementCase<dim> directional_cell_refinement_case =
17493
17494 for (unsigned int face_index = 0; face_index < 2; ++face_index)
17495 {
17496 unsigned int face = 2 * face_pair + face_index;
17497 // variable to store the refine case (to come) of the
17498 // face under consideration
17499 RefinementCase<dim - 1> expected_face_ref_case =
17500 RefinementCase<dim - 1>::no_refinement;
17501
17502 if (cell->neighbor(face).state() == IteratorState::valid)
17503 face_will_be_refined_by_neighbor<dim, spacedim>(
17504 cell, face, expected_face_ref_case);
17505 // now extract which refine case would be necessary to
17506 // achieve the same face refinement. set the
17507 // intersection with other requirements for the same
17508 // direction.
17509
17510 // note: using the intersection is not an obvious
17511 // decision, we could also argue that it is more
17512 // natural to use the union. however, intersection is
17513 // the less aggressive tactic and favours a smaller
17514 // number of refined cells over an intensive
17515 // smoothing. this way we try not to lose too much of
17516 // the effort we put in anisotropic refinement
17517 // indicators due to overly aggressive smoothing...
17518 directional_cell_refinement_case =
17519 (directional_cell_refinement_case &
17522 expected_face_ref_case,
17523 face,
17524 cell->face_orientation(face),
17525 cell->face_flip(face),
17526 cell->face_rotation(face)));
17527 } // for both face indices
17528 // if both requirements sum up to something useful, add
17529 // this to the refine case for smoothing. note: if
17530 // directional_cell_refinement_case is isotropic still,
17531 // then something went wrong...
17532 Assert(directional_cell_refinement_case <
17535 smoothing_cell_refinement_case =
17536 smoothing_cell_refinement_case | directional_cell_refinement_case;
17537 } // for all face_pairs
17538 // no we collected contributions from all directions. combine
17539 // the new flags with the existing refine case, but only if
17540 // smoothing is required
17541 if (smoothing_cell_refinement_case)
17542 {
17543 cell->clear_coarsen_flag();
17544 cell->set_refine_flag(cell->refine_flag_set() |
17545 smoothing_cell_refinement_case);
17546 }
17547 }
17548 }
17549} // namespace
17550
17551#ifndef DOXYGEN
17552template <int dim, int spacedim>
17555{
17556 // save the flags to determine whether something was changed in the
17557 // course of this function
17558 const auto coarsen_flags_before = internal::extract_raw_coarsen_flags(levels);
17559 const auto refine_flags_before = internal::extract_raw_refine_flags(levels);
17560
17561 // save the flags at the outset of each loop. we do so in order to
17562 // find out whether something was changed in the present loop, in
17563 // which case we would have to re-run the loop. the other
17564 // possibility to find this out would be to set a flag
17565 // @p{something_changed} to true each time we change something.
17566 // however, sometimes one change in one of the parts of the loop is
17567 // undone by another one, so we might end up in an endless loop. we
17568 // could be tempted to break this loop at an arbitrary number of
17569 // runs, but that would not be a clean solution, since we would
17570 // either have to 1/ break the loop too early, in which case the
17571 // promise that a second call to this function immediately after the
17572 // first one does not change anything, would be broken, or 2/ we do
17573 // as many loops as there are levels. we know that information is
17574 // transported over one level in each run of the loop, so this is
17575 // enough. Unfortunately, each loop is rather expensive, so we chose
17576 // the way presented here
17577 auto coarsen_flags_before_loop = coarsen_flags_before;
17578 auto refine_flags_before_loop = refine_flags_before;
17579
17580 // now for what is done in each loop: we have to fulfill several
17581 // tasks at the same time, namely several mesh smoothing algorithms
17582 // and mesh regularization, by which we mean that the next mesh
17583 // fulfills several requirements such as no double refinement at
17584 // each face or line, etc.
17585 //
17586 // since doing these things at once seems almost impossible (in the
17587 // first year of this library, they were done in two functions, one
17588 // for refinement and one for coarsening, and most things within
17589 // these were done at once, so the code was rather impossible to
17590 // join into this, only, function), we do them one after each
17591 // other. the order in which we do them is such that the important
17592 // tasks, namely regularization, are done last and the least
17593 // important things are done the first. the following order is
17594 // chosen:
17595 //
17596 // 0/ Only if coarsest_level_1 or patch_level_1 is set: clear all
17597 // coarsen flags on level 1 to avoid level 0 cells being created
17598 // by coarsening. As coarsen flags will never be added, this can
17599 // be done once and for all before the actual loop starts.
17600 //
17601 // 1/ do not coarsen a cell if 'most of the neighbors' will be
17602 // refined after the step. This is to prevent occurrence of
17603 // unrefined islands.
17604 //
17605 // 2/ eliminate refined islands in the interior and at the
17606 // boundary. since they don't do much harm besides increasing the
17607 // number of degrees of freedom, doing this has a rather low
17608 // priority.
17609 //
17610 // 3/ limit the level difference of neighboring cells at each
17611 // vertex.
17612 //
17613 // 4/ eliminate unrefined islands. this has higher priority since
17614 // this diminishes the approximation properties not only of the
17615 // unrefined island, but also of the surrounding patch.
17616 //
17617 // 5/ ensure patch level 1. Then the triangulation consists of
17618 // patches, i.e. of cells that are refined once. It follows that
17619 // if at least one of the children of a cell is or will be
17620 // refined than all children need to be refined. This step only
17621 // sets refinement flags and does not set coarsening flags. If
17622 // the patch_level_1 flag is set, then
17623 // eliminate_unrefined_islands, eliminate_refined_inner_islands
17624 // and eliminate_refined_boundary_islands will be fulfilled
17625 // automatically and do not need to be enforced separately.
17626 //
17627 // 6/ take care of the requirement that no double refinement is done
17628 // at each face
17629 //
17630 // 7/ take care that no double refinement is done at each line in 3d
17631 // or higher dimensions.
17632 //
17633 // 8/ make sure that all children of each cell are either flagged
17634 // for coarsening or none of the children is
17635 //
17636 // For some of these steps, it is known that they interact. Namely,
17637 // it is not possible to guarantee that after step 6 another step 5
17638 // would have no effect; the same holds for the opposite order and
17639 // also when taking into account step 7. however, it is important to
17640 // guarantee that step five or six do not undo something that step 5
17641 // did, and step 7 not something of step 6, otherwise the
17642 // requirements will not be satisfied even if the loop
17643 // terminates. this is accomplished by the fact that steps 5 and 6
17644 // only *add* refinement flags and delete coarsening flags
17645 // (therefore, step 6 can't undo something that step 4 already did),
17646 // and step 7 only deletes coarsening flags, never adds some. step 7
17647 // needs also take care that it won't tag cells for refinement for
17648 // which some neighbors are more refined or will be refined.
17649
17650 //------------------------------------
17651 // STEP 0:
17652 // Only if coarsest_level_1 or patch_level_1 is set: clear all
17653 // coarsen flags on level 1 to avoid level 0 cells being created
17654 // by coarsening.
17655 if (((smooth_grid & coarsest_level_1) || (smooth_grid & patch_level_1)) &&
17656 n_levels() >= 2)
17657 {
17658 for (const auto &cell : active_cell_iterators_on_level(1))
17659 cell->clear_coarsen_flag();
17660 }
17661
17662 bool mesh_changed_in_this_loop = false;
17663 do
17664 {
17665 //------------------------------------
17666 // STEP 1:
17667 // do not coarsen a cell if 'most of the neighbors' will be
17668 // refined after the step. This is to prevent the occurrence
17669 // of unrefined islands. If patch_level_1 is set, this will
17670 // be automatically fulfilled.
17671 if (smooth_grid & do_not_produce_unrefined_islands &&
17672 !(smooth_grid & patch_level_1))
17673 {
17674 for (const auto &cell : cell_iterators())
17675 {
17676 // only do something if this
17677 // cell will be coarsened
17678 if (!cell->is_active() && cell_will_be_coarsened(cell))
17679 possibly_do_not_produce_unrefined_islands<dim, spacedim>(cell);
17680 }
17681 }
17682
17683
17684 //------------------------------------
17685 // STEP 2:
17686 // eliminate refined islands in the interior and at the
17687 // boundary. since they don't do much harm besides increasing
17688 // the number of degrees of freedom, doing this has a rather
17689 // low priority. If patch_level_1 is set, this will be
17690 // automatically fulfilled.
17691 //
17692 // there is one corner case to consider: if this is a
17693 // distributed triangulation, there may be refined islands on
17694 // the boundary of which we own only part (e.g. a single cell
17695 // in the corner of a domain). the rest of the island is
17696 // ghost cells and it *looks* like the area around it
17697 // (artificial cells) are coarser but this is only because
17698 // they may actually be equally fine on other
17699 // processors. it's hard to detect this case but we can do
17700 // the following: only set coarsen flags to remove this
17701 // refined island if all cells we want to set flags on are
17702 // locally owned
17703 if (smooth_grid & (eliminate_refined_inner_islands |
17704 eliminate_refined_boundary_islands) &&
17705 !(smooth_grid & patch_level_1))
17706 {
17707 for (const auto &cell : cell_iterators())
17708 if (!cell->is_active() ||
17709 (cell->is_active() && cell->refine_flag_set() &&
17710 cell->is_locally_owned()))
17711 {
17712 // check whether all children are active, i.e. not
17713 // refined themselves. This is a precondition that the
17714 // children may be coarsened away. If the cell is only
17715 // flagged for refinement, then all future children
17716 // will be active
17717 bool all_children_active = true;
17718 if (!cell->is_active())
17719 for (unsigned int c = 0; c < cell->n_children(); ++c)
17720 if (!cell->child(c)->is_active() ||
17721 cell->child(c)->is_ghost() ||
17722 cell->child(c)->is_artificial())
17723 {
17724 all_children_active = false;
17725 break;
17726 }
17727
17728 if (all_children_active)
17729 {
17730 // count number of refined and unrefined neighbors
17731 // of cell. neighbors on lower levels are counted
17732 // as unrefined since they can only get to the
17733 // same level as this cell by the next refinement
17734 // cycle
17735 unsigned int unrefined_neighbors = 0, total_neighbors = 0;
17736
17737 // Keep track if this cell is at a periodic
17738 // boundary or not. TODO: We do not currently run
17739 // the algorithm for inner islands at a periodic
17740 // boundary (remains to be implemented), but we
17741 // also don't want to consider them
17742 // boundary_island cells as this can interfere
17743 // with 2:1 refinement across periodic faces.
17744 // Instead: just ignore those cells for this
17745 // smoothing operation below.
17746 bool at_periodic_boundary = false;
17747
17748 for (const unsigned int n : cell->face_indices())
17749 {
17750 const cell_iterator neighbor = cell->neighbor(n);
17751 if (neighbor.state() == IteratorState::valid)
17752 {
17753 ++total_neighbors;
17754
17755 if (!face_will_be_refined_by_neighbor(cell, n))
17756 ++unrefined_neighbors;
17757 }
17758 else if (cell->has_periodic_neighbor(n))
17759 {
17760 ++total_neighbors;
17761 at_periodic_boundary = true;
17762 }
17763 }
17764
17765 // if all neighbors unrefined: mark this cell for
17766 // coarsening or don't refine if marked for that
17767 //
17768 // also do the distinction between the two
17769 // versions of the eliminate_refined_*_islands
17770 // flag
17771 //
17772 // the last check is whether there are any
17773 // neighbors at all. if not so, then we are (e.g.)
17774 // on the coarsest grid with one cell, for which,
17775 // of course, we do not remove the refine flag.
17776 if ((unrefined_neighbors == total_neighbors) &&
17777 ((!cell->at_boundary() &&
17778 (smooth_grid & eliminate_refined_inner_islands)) ||
17779 (cell->at_boundary() && !at_periodic_boundary &&
17780 (smooth_grid &
17781 eliminate_refined_boundary_islands))) &&
17782 (total_neighbors != 0))
17783 {
17784 if (!cell->is_active())
17785 for (unsigned int c = 0; c < cell->n_children(); ++c)
17786 {
17787 cell->child(c)->clear_refine_flag();
17788 cell->child(c)->set_coarsen_flag();
17789 }
17790 else
17791 cell->clear_refine_flag();
17792 }
17793 }
17794 }
17795 }
17796
17797 //------------------------------------
17798 // STEP 3:
17799 // limit the level difference of neighboring cells at each
17800 // vertex.
17801 //
17802 // in case of anisotropic refinement this does not make
17803 // sense. as soon as one cell is anisotropically refined, an
17804 // Assertion is thrown. therefore we can ignore this problem
17805 // later on
17806 if (smooth_grid & limit_level_difference_at_vertices)
17807 {
17808 Assert(!anisotropic_refinement,
17809 ExcMessage("In case of anisotropic refinement the "
17810 "limit_level_difference_at_vertices flag for "
17811 "mesh smoothing must not be set!"));
17812
17813 // store highest level one of the cells adjacent to a vertex
17814 // belongs to
17815 std::vector<int> vertex_level(vertices.size(), 0);
17816 for (const auto &cell : active_cell_iterators())
17817 {
17818 if (cell->refine_flag_set())
17819 for (const unsigned int vertex : cell->vertex_indices())
17820 vertex_level[cell->vertex_index(vertex)] =
17821 std::max(vertex_level[cell->vertex_index(vertex)],
17822 cell->level() + 1);
17823 else if (!cell->coarsen_flag_set())
17824 for (const unsigned int vertex : cell->vertex_indices())
17825 vertex_level[cell->vertex_index(vertex)] =
17826 std::max(vertex_level[cell->vertex_index(vertex)],
17827 cell->level());
17828 else
17829 {
17830 // if coarsen flag is set then tentatively assume
17831 // that the cell will be coarsened. this isn't
17832 // always true (the coarsen flag could be removed
17833 // again) and so we may make an error here
17834 Assert(cell->coarsen_flag_set(), ExcInternalError());
17835 for (const unsigned int vertex : cell->vertex_indices())
17836 vertex_level[cell->vertex_index(vertex)] =
17837 std::max(vertex_level[cell->vertex_index(vertex)],
17838 cell->level() - 1);
17839 }
17840 }
17841
17842
17843 // loop over all cells in reverse order. do so because we
17844 // can then update the vertex levels on the adjacent
17845 // vertices and maybe already flag additional cells in this
17846 // loop
17847 //
17848 // note that not only may we have to add additional
17849 // refinement flags, but we will also have to remove
17850 // coarsening flags on cells adjacent to vertices that will
17851 // see refinement
17852 for (active_cell_iterator cell = last_active(); cell != end(); --cell)
17853 if (cell->refine_flag_set() == false)
17854 {
17855 for (const unsigned int vertex : cell->vertex_indices())
17856 if (vertex_level[cell->vertex_index(vertex)] >=
17857 cell->level() + 1)
17858 {
17859 // remove coarsen flag...
17860 cell->clear_coarsen_flag();
17861
17862 // ...and if necessary also refine the current
17863 // cell, at the same time updating the level
17864 // information about vertices
17865 if (vertex_level[cell->vertex_index(vertex)] >
17866 cell->level() + 1)
17867 {
17868 cell->set_refine_flag();
17869
17870 for (const unsigned int v : cell->vertex_indices())
17871 vertex_level[cell->vertex_index(v)] =
17872 std::max(vertex_level[cell->vertex_index(v)],
17873 cell->level() + 1);
17874 }
17875
17876 // continue and see whether we may, for example,
17877 // go into the inner'if'
17878 // above based on a
17879 // different vertex
17880 }
17881 }
17882 }
17883
17884 //-----------------------------------
17885 // STEP 4:
17886 // eliminate unrefined islands. this has higher priority
17887 // since this diminishes the approximation properties not
17888 // only of the unrefined island, but also of the surrounding
17889 // patch.
17890 //
17891 // do the loop from finest to coarsest cells since we may
17892 // trigger a cascade by marking cells for refinement which
17893 // may trigger more cells further down below
17894 if (smooth_grid & eliminate_unrefined_islands)
17895 {
17896 for (active_cell_iterator cell = last_active(); cell != end(); --cell)
17897 // only do something if cell is not already flagged for
17898 // (isotropic) refinement
17899 if (cell->refine_flag_set() !=
17901 possibly_refine_unrefined_island<dim, spacedim>(
17902 cell, (smooth_grid & allow_anisotropic_smoothing) != 0);
17903 }
17904
17905 //-------------------------------
17906 // STEP 5:
17907 // ensure patch level 1.
17908 //
17909 // Introduce some terminology:
17910 // - a cell that is refined
17911 // once is a patch of
17912 // level 1 simply called patch.
17913 // - a cell that is globally
17914 // refined twice is called
17915 // a patch of level 2.
17916 // - patch level n says that
17917 // the triangulation consists
17918 // of patches of level n.
17919 // This makes sense only
17920 // if the grid is already at
17921 // least n times globally
17922 // refined.
17923 //
17924 // E.g. from patch level 1 follows: if at least one of the
17925 // children of a cell is or will be refined than enforce all
17926 // children to be refined.
17927
17928 // This step 4 only sets refinement flags and does not set
17929 // coarsening flags.
17930 if (smooth_grid & patch_level_1)
17931 {
17932 // An important assumption (A) is that before calling this
17933 // function the grid was already of patch level 1.
17934
17935 // loop over all cells whose children are all active. (By
17936 // assumption (A) either all or none of the children are
17937 // active). If the refine flag of at least one of the
17938 // children is set then set_refine_flag and
17939 // clear_coarsen_flag of all children.
17940 for (const auto &cell : cell_iterators())
17941 if (!cell->is_active())
17942 {
17943 // ensure the invariant. we can then check whether all
17944 // of its children are further refined or not by
17945 // simply looking at the first child
17946 Assert(cell_is_patch_level_1(cell), ExcInternalError());
17947 if (cell->child(0)->has_children() == true)
17948 continue;
17949
17950 // cell is found to be a patch. combine the refine
17951 // cases of all children
17952 RefinementCase<dim> combined_ref_case =
17954 for (unsigned int i = 0; i < cell->n_children(); ++i)
17955 combined_ref_case =
17956 combined_ref_case | cell->child(i)->refine_flag_set();
17957 if (combined_ref_case != RefinementCase<dim>::no_refinement)
17958 for (unsigned int i = 0; i < cell->n_children(); ++i)
17959 {
17960 cell_iterator child = cell->child(i);
17961
17962 child->clear_coarsen_flag();
17963 child->set_refine_flag(combined_ref_case);
17964 }
17965 }
17966
17967 // The code above dealt with the case where we may get a
17968 // non-patch_level_1 mesh from refinement. Now also deal
17969 // with the case where we could get such a mesh by
17970 // coarsening. Coarsen the children (and remove the
17971 // grandchildren) only if all cell->grandchild(i)
17972 // ->coarsen_flag_set() are set.
17973 //
17974 // for a case where this is a bit tricky, take a look at the
17975 // mesh_smoothing_0[12] testcases
17976 for (const auto &cell : cell_iterators())
17977 {
17978 // check if this cell has active grandchildren. note
17979 // that we know that it is patch_level_1, i.e. if one of
17980 // its children is active then so are all, and it isn't
17981 // going to have any grandchildren at all:
17982 if (cell->is_active() || cell->child(0)->is_active())
17983 continue;
17984
17985 // cell is not active, and so are none of its
17986 // children. check the grandchildren. note that the
17987 // children are also patch_level_1, and so we only ever
17988 // need to check their first child
17989 const unsigned int n_children = cell->n_children();
17990 bool has_active_grandchildren = false;
17991
17992 for (unsigned int i = 0; i < n_children; ++i)
17993 if (cell->child(i)->child(0)->is_active())
17994 {
17995 has_active_grandchildren = true;
17996 break;
17997 }
17998
17999 if (has_active_grandchildren == false)
18000 continue;
18001
18002
18003 // ok, there are active grandchildren. see if either all
18004 // or none of them are flagged for coarsening
18005 unsigned int n_grandchildren = 0;
18006
18007 // count all coarsen flags of the grandchildren.
18008 unsigned int n_coarsen_flags = 0;
18009
18010 // cell is not a patch (of level 1) as it has a
18011 // grandchild. Is cell a patch of level 2?? Therefore:
18012 // find out whether all cell->child(i) are patches
18013 for (unsigned int c = 0; c < n_children; ++c)
18014 {
18015 // get at the child. by assumption (A), and the
18016 // check by which we got here, the child is not
18017 // active
18018 cell_iterator child = cell->child(c);
18019
18020 const unsigned int nn_children = child->n_children();
18021 n_grandchildren += nn_children;
18022
18023 // if child is found to be a patch of active cells
18024 // itself, then add up how many of its children are
18025 // supposed to be coarsened
18026 if (child->child(0)->is_active())
18027 for (unsigned int cc = 0; cc < nn_children; ++cc)
18028 if (child->child(cc)->coarsen_flag_set())
18029 ++n_coarsen_flags;
18030 }
18031
18032 // if not all grandchildren are supposed to be coarsened
18033 // (e.g. because some simply don't have the flag set, or
18034 // because they are not active and therefore cannot
18035 // carry the flag), then remove the coarsen flag from
18036 // all of the active grandchildren. note that there may
18037 // be coarsen flags on the grandgrandchildren -- we
18038 // don't clear them here, but we'll get to them in later
18039 // iterations if necessary
18040 //
18041 // there is nothing we have to do if no coarsen flags
18042 // have been set at all
18043 if ((n_coarsen_flags != n_grandchildren) && (n_coarsen_flags > 0))
18044 for (unsigned int c = 0; c < n_children; ++c)
18045 {
18046 const cell_iterator child = cell->child(c);
18047 if (child->child(0)->is_active())
18048 for (unsigned int cc = 0; cc < child->n_children(); ++cc)
18049 child->child(cc)->clear_coarsen_flag();
18050 }
18051 }
18052 }
18053
18054 //--------------------------------
18055 //
18056 // at the boundary we could end up with cells with negative
18057 // volume or at least with a part, that is negative, if the
18058 // cell is refined anisotropically. we have to check, whether
18059 // that can happen
18060 this->policy->prevent_distorted_boundary_cells(*this);
18061
18062 //-------------------------------
18063 // STEP 6:
18064 // take care of the requirement that no
18065 // double refinement is done at each face
18066 //
18067 // in case of anisotropic refinement it is only likely, but
18068 // not sure, that the cells, which are more refined along a
18069 // certain face common to two cells are on a higher
18070 // level. therefore we cannot be sure, that the requirement
18071 // of no double refinement is fulfilled after a single pass
18072 // of the following actions. We could just wait for the next
18073 // global loop. when this function terminates, the
18074 // requirement will be fulfilled. However, it might be faster
18075 // to insert an inner loop here.
18076 bool changed = true;
18077 while (changed)
18078 {
18079 changed = false;
18080 active_cell_iterator cell = last_active(), endc = end();
18081
18082 for (; cell != endc; --cell)
18083 if (cell->refine_flag_set())
18084 {
18085 // loop over neighbors of cell
18086 for (const auto i : cell->face_indices())
18087 {
18088 // only do something if the face is not at the
18089 // boundary and if the face will be refined with
18090 // the RefineCase currently flagged for
18091 const bool has_periodic_neighbor =
18092 cell->has_periodic_neighbor(i);
18093 const bool has_neighbor_or_periodic_neighbor =
18094 !cell->at_boundary(i) || has_periodic_neighbor;
18095 if (has_neighbor_or_periodic_neighbor &&
18097 cell->refine_flag_set(), i) !=
18099 {
18100 // 1) if the neighbor has children: nothing to
18101 // worry about. 2) if the neighbor is active
18102 // and a coarser one, ensure, that its
18103 // refine_flag is set 3) if the neighbor is
18104 // active and as refined along the face as our
18105 // current cell, make sure, that no
18106 // coarsen_flag is set. if we remove the
18107 // coarsen flag of our neighbor,
18108 // fix_coarsen_flags() makes sure, that the
18109 // mother cell will not be coarsened
18110 if (cell->neighbor_or_periodic_neighbor(i)->is_active())
18111 {
18112 if ((!has_periodic_neighbor &&
18113 cell->neighbor_is_coarser(i)) ||
18114 (has_periodic_neighbor &&
18115 cell->periodic_neighbor_is_coarser(i)))
18116 {
18117 if (cell->neighbor_or_periodic_neighbor(i)
18118 ->coarsen_flag_set())
18119 cell->neighbor_or_periodic_neighbor(i)
18120 ->clear_coarsen_flag();
18121 // we'll set the refine flag for this
18122 // neighbor below. we note, that we
18123 // have changed something by setting
18124 // the changed flag to true. We do not
18125 // need to do so, if we just removed
18126 // the coarsen flag, as the changed
18127 // flag only indicates the need to
18128 // re-run the inner loop. however, we
18129 // only loop over cells flagged for
18130 // refinement here, so nothing to
18131 // worry about if we remove coarsen
18132 // flags
18133
18134 if (dim == 2)
18135 {
18136 if (smooth_grid &
18137 allow_anisotropic_smoothing)
18138 changed =
18139 has_periodic_neighbor ?
18140 cell->periodic_neighbor(i)
18141 ->flag_for_face_refinement(
18142 cell
18143 ->periodic_neighbor_of_coarser_periodic_neighbor(
18144 i)
18145 .first,
18147 cell->neighbor(i)
18148 ->flag_for_face_refinement(
18149 cell
18150 ->neighbor_of_coarser_neighbor(
18151 i)
18152 .first,
18154 else
18155 {
18156 if (!cell
18157 ->neighbor_or_periodic_neighbor(
18158 i)
18159 ->refine_flag_set())
18160 changed = true;
18161 cell->neighbor_or_periodic_neighbor(i)
18162 ->set_refine_flag();
18163 }
18164 }
18165 else // i.e. if (dim==3)
18166 {
18167 // ugly situations might arise here,
18168 // consider the following situation, which
18169 // shows neighboring cells at the common
18170 // face, where the upper right element is
18171 // coarser at the given face. Now the upper
18172 // child element of the lower left wants to
18173 // refine according to cut_z, such that
18174 // there is a 'horizontal' refinement of the
18175 // face marked with #####
18176 //
18177 // / /
18178 // / /
18179 // *---------------*
18180 // | |
18181 // | |
18182 // | |
18183 // | |
18184 // | |
18185 // | | /
18186 // | |/
18187 // *---------------*
18188 //
18189 //
18190 // *---------------*
18191 // /| /|
18192 // / | ##### / |
18193 // | |
18194 // *---------------*
18195 // /| /|
18196 // / | / |
18197 // | |
18198 // *---------------*
18199 // / /
18200 // / /
18201 //
18202 // this introduces too many hanging nodes
18203 // and the neighboring (coarser) cell (upper
18204 // right) has to be refined. If it is only
18205 // refined according to cut_z, then
18206 // everything is ok:
18207 //
18208 // / /
18209 // / /
18210 // *---------------*
18211 // | |
18212 // | | /
18213 // | |/
18214 // *---------------*
18215 // | |
18216 // | | /
18217 // | |/
18218 // *---------------*
18219 //
18220 //
18221 // *---------------*
18222 // /| /|
18223 // / *---------------*
18224 // /| /|
18225 // *---------------*
18226 // /| /|
18227 // / | / |
18228 // | |
18229 // *---------------*
18230 // / /
18231 // / /
18232 //
18233 // if however the cell wants to refine
18234 // itself in an other way, or if we disallow
18235 // anisotropic smoothing, then simply
18236 // refining the neighbor isotropically is
18237 // not going to work, since this introduces
18238 // a refinement of face ##### with both
18239 // cut_x and cut_y, which is not possible:
18240 //
18241 // / / /
18242 // / / /
18243 // *-------*-------*
18244 // | | |
18245 // | | | /
18246 // | | |/
18247 // *-------*-------*
18248 // | | |
18249 // | | | /
18250 // | | |/
18251 // *-------*-------*
18252 //
18253 //
18254 // *---------------*
18255 // /| /|
18256 // / *---------------*
18257 // /| /|
18258 // *---------------*
18259 // /| /|
18260 // / | / |
18261 // | |
18262 // *---------------*
18263 // / /
18264 // / /
18265 //
18266 // thus, in this case we also need to refine
18267 // our current cell in the new direction:
18268 //
18269 // / / /
18270 // / / /
18271 // *-------*-------*
18272 // | | |
18273 // | | | /
18274 // | | |/
18275 // *-------*-------*
18276 // | | |
18277 // | | | /
18278 // | | |/
18279 // *-------*-------*
18280 //
18281 //
18282 // *-------*-------*
18283 // /| /| /|
18284 // / *-------*-------*
18285 // /| /| /|
18286 // *-------*-------*
18287 // /| / /|
18288 // / | / |
18289 // | |
18290 // *---------------*
18291 // / /
18292 // / /
18293
18294 std::pair<unsigned int, unsigned int>
18295 nb_indices =
18296 has_periodic_neighbor ?
18297 cell
18298 ->periodic_neighbor_of_coarser_periodic_neighbor(
18299 i) :
18300 cell->neighbor_of_coarser_neighbor(i);
18301 unsigned int refined_along_x = 0,
18302 refined_along_y = 0,
18303 to_be_refined_along_x = 0,
18304 to_be_refined_along_y = 0;
18305
18306 const int this_face_index =
18307 cell->face_index(i);
18308
18309 // step 1: detect, along which axis the face
18310 // is currently refined
18311
18312 // first, we need an iterator pointing to
18313 // the parent face. This requires a slight
18314 // detour in case the neighbor is behind a
18315 // periodic face.
18316 const auto parent_face = [&]() {
18317 if (has_periodic_neighbor)
18318 {
18319 const auto neighbor =
18320 cell->periodic_neighbor(i);
18321 const auto parent_face_no =
18322 neighbor
18323 ->periodic_neighbor_of_periodic_neighbor(
18324 nb_indices.first);
18325 auto parent =
18326 neighbor->periodic_neighbor(
18327 nb_indices.first);
18328 return parent->face(parent_face_no);
18329 }
18330 else
18331 return cell->neighbor(i)->face(
18332 nb_indices.first);
18333 }();
18334
18335 if ((this_face_index ==
18336 parent_face->child_index(0)) ||
18337 (this_face_index ==
18338 parent_face->child_index(1)))
18339 {
18340 // this might be an
18341 // anisotropic child. get the
18342 // face refine case of the
18343 // neighbors face and count
18344 // refinements in x and y
18345 // direction.
18346 RefinementCase<dim - 1> frc =
18347 parent_face->refinement_case();
18349 ++refined_along_x;
18351 ++refined_along_y;
18352 }
18353 else
18354 // this has to be an isotropic
18355 // child
18356 {
18357 ++refined_along_x;
18358 ++refined_along_y;
18359 }
18360 // step 2: detect, along which axis the face
18361 // has to be refined given the current
18362 // refine flag
18363 RefinementCase<dim - 1> flagged_frc =
18365 cell->refine_flag_set(),
18366 i,
18367 cell->face_orientation(i),
18368 cell->face_flip(i),
18369 cell->face_rotation(i));
18370 if (flagged_frc &
18372 ++to_be_refined_along_x;
18373 if (flagged_frc &
18375 ++to_be_refined_along_y;
18376
18377 // step 3: set the refine flag of the
18378 // (coarser and active) neighbor.
18379 if ((smooth_grid &
18380 allow_anisotropic_smoothing) ||
18381 cell->neighbor_or_periodic_neighbor(i)
18382 ->refine_flag_set())
18383 {
18384 if (refined_along_x +
18385 to_be_refined_along_x >
18386 1)
18387 changed |=
18388 cell
18389 ->neighbor_or_periodic_neighbor(i)
18390 ->flag_for_face_refinement(
18391 nb_indices.first,
18392 RefinementCase<dim -
18393 1>::cut_axis(0));
18394 if (refined_along_y +
18395 to_be_refined_along_y >
18396 1)
18397 changed |=
18398 cell
18399 ->neighbor_or_periodic_neighbor(i)
18400 ->flag_for_face_refinement(
18401 nb_indices.first,
18402 RefinementCase<dim -
18403 1>::cut_axis(1));
18404 }
18405 else
18406 {
18407 if (cell
18408 ->neighbor_or_periodic_neighbor(i)
18409 ->refine_flag_set() !=
18412 changed = true;
18413 cell->neighbor_or_periodic_neighbor(i)
18414 ->set_refine_flag();
18415 }
18416
18417 // step 4: if necessary (see above) add to
18418 // the refine flag of the current cell
18419 cell_iterator nb =
18420 cell->neighbor_or_periodic_neighbor(i);
18421 RefinementCase<dim - 1> nb_frc =
18423 nb->refine_flag_set(),
18424 nb_indices.first,
18425 nb->face_orientation(nb_indices.first),
18426 nb->face_flip(nb_indices.first),
18427 nb->face_rotation(nb_indices.first));
18428 if ((nb_frc & RefinementCase<dim>::cut_x) &&
18429 !((refined_along_x != 0u) ||
18430 (to_be_refined_along_x != 0u)))
18431 changed |= cell->flag_for_face_refinement(
18432 i,
18434 if ((nb_frc & RefinementCase<dim>::cut_y) &&
18435 !((refined_along_y != 0u) ||
18436 (to_be_refined_along_y != 0u)))
18437 changed |= cell->flag_for_face_refinement(
18438 i,
18440 }
18441 } // if neighbor is coarser
18442 else // -> now the neighbor is not coarser
18443 {
18444 cell->neighbor_or_periodic_neighbor(i)
18445 ->clear_coarsen_flag();
18446 const unsigned int nb_nb =
18447 has_periodic_neighbor ?
18448 cell
18449 ->periodic_neighbor_of_periodic_neighbor(
18450 i) :
18451 cell->neighbor_of_neighbor(i);
18452 const cell_iterator neighbor =
18453 cell->neighbor_or_periodic_neighbor(i);
18454 RefinementCase<dim - 1> face_ref_case =
18456 neighbor->refine_flag_set(),
18457 nb_nb,
18458 neighbor->face_orientation(nb_nb),
18459 neighbor->face_flip(nb_nb),
18460 neighbor->face_rotation(nb_nb));
18461 RefinementCase<dim - 1> needed_face_ref_case =
18463 cell->refine_flag_set(),
18464 i,
18465 cell->face_orientation(i),
18466 cell->face_flip(i),
18467 cell->face_rotation(i));
18468 // if the neighbor wants to refine the
18469 // face with cut_x and we want cut_y
18470 // or vice versa, we have to refine
18471 // isotropically at the given face
18472 if ((face_ref_case ==
18474 needed_face_ref_case ==
18476 (face_ref_case ==
18478 needed_face_ref_case ==
18480 {
18481 changed = cell->flag_for_face_refinement(
18482 i, face_ref_case);
18483 neighbor->flag_for_face_refinement(
18484 nb_nb, needed_face_ref_case);
18485 }
18486 }
18487 }
18488 else //-> the neighbor is not active
18489 {
18490 RefinementCase<dim - 1>
18491 face_ref_case = cell->face(i)->refinement_case(),
18492 needed_face_ref_case =
18494 cell->refine_flag_set(),
18495 i,
18496 cell->face_orientation(i),
18497 cell->face_flip(i),
18498 cell->face_rotation(i));
18499 // if the face is refined with cut_x and
18500 // we want cut_y or vice versa, we have to
18501 // refine isotropically at the given face
18502 if ((face_ref_case == RefinementCase<dim>::cut_x &&
18503 needed_face_ref_case ==
18505 (face_ref_case == RefinementCase<dim>::cut_y &&
18506 needed_face_ref_case ==
18508 changed =
18509 cell->flag_for_face_refinement(i,
18510 face_ref_case);
18511 }
18512 }
18513 }
18514 }
18515 }
18516
18517 //------------------------------------
18518 // STEP 7:
18519 // take care that no double refinement is done at each line in 3d or
18520 // higher dimensions.
18521 this->policy->prepare_refinement_dim_dependent(*this);
18522
18523 //------------------------------------
18524 // STEP 8:
18525 // make sure that all children of each cell are either flagged for
18526 // coarsening or none of the children is
18527 fix_coarsen_flags();
18528
18529 //------------------------------------
18530 // Match refinement and coarsening decisions across periodic
18531 // faces if requested. In a distributed triangulation, only
18532 // modify locally owned cells; changes to ghost cells are made
18533 // by their owners after flags are exchanged.
18534 if (smooth_grid & match_refinement_across_periodic_boundaries)
18535 for (const auto &cell : active_cell_iterators())
18536 if (cell->is_locally_owned())
18537 for (const unsigned int face : cell->face_indices())
18538 if (cell->has_periodic_neighbor(face))
18539 {
18540 const auto periodic_neighbor = cell->periodic_neighbor(face);
18541
18542 const int periodic_neighbor_future_level =
18543 periodic_neighbor->level() +
18544 (periodic_neighbor->refine_flag_set() ? 1 : 0) -
18545 (periodic_neighbor->coarsen_flag_set() ? 1 : 0);
18546
18547 if (cell->level() < periodic_neighbor_future_level)
18548 {
18549 if (cell->coarsen_flag_set())
18550 cell->clear_coarsen_flag();
18551 else if (!cell->refine_flag_set())
18552 cell->set_refine_flag();
18553 }
18554 else if (cell->level() == periodic_neighbor_future_level &&
18555 cell->coarsen_flag_set())
18556 cell->clear_coarsen_flag();
18557 }
18558
18559 // get the refinement and coarsening flags
18560 auto coarsen_flags_after_loop =
18561 internal::extract_raw_coarsen_flags(levels);
18562 auto refine_flags_after_loop = internal::extract_raw_refine_flags(levels);
18563
18564 // find out whether something was changed in this loop
18565 mesh_changed_in_this_loop =
18566 ((coarsen_flags_before_loop != coarsen_flags_after_loop) ||
18567 (refine_flags_before_loop != refine_flags_after_loop));
18568
18569 // set the flags for the next loop already
18570 coarsen_flags_before_loop.swap(coarsen_flags_after_loop);
18571 refine_flags_before_loop.swap(refine_flags_after_loop);
18572 }
18573 while (mesh_changed_in_this_loop);
18574
18575
18576 // find out whether something was really changed in this
18577 // function. Note that @p{..._flags_before_loop} represents the state
18578 // after the last loop, i.e., the present state
18579 return ((coarsen_flags_before != coarsen_flags_before_loop) ||
18580 (refine_flags_before != refine_flags_before_loop));
18581}
18582
18583
18584
18585template <int dim, int spacedim>
18587std::vector<std::vector<
18590 const std::vector<std::vector<TriangulationDescription::CellData<dim>>>
18591 &cell_infos) const
18592{
18593 std::vector<std::vector<TriangulationDescription::CellData<dim>>>
18594 sorted_cell_infos(cell_infos.size());
18595
18596 // compute sort keys once per cell rather than for each comparison
18597 std::vector<std::pair<CellId::binary_type, unsigned int>> keys;
18598 for (unsigned int level = 0; level < cell_infos.size(); ++level)
18599 {
18600 const std::vector<TriangulationDescription::CellData<dim>> &level_infos =
18602 keys.resize(level_infos.size());
18603
18604 // consecutive cells typically share the same coarse cell, so
18605 // cache the result of the last lookup
18606 types::coarse_cell_id last_coarse_cell_id =
18608 unsigned int last_coarse_cell_index = numbers::invalid_unsigned_int;
18609 bool is_sorted = true;
18610 for (unsigned int i = 0; i < level_infos.size(); ++i)
18611 {
18612 const CellId::binary_type &id = level_infos[i].id;
18613 if (id[0] != last_coarse_cell_id || i == 0)
18614 {
18615 last_coarse_cell_id = id[0];
18616 last_coarse_cell_index = coarse_cell_id_to_coarse_cell_index(
18617 static_cast<types::coarse_cell_id>(id[0]));
18618 }
18619 keys[i].first = id;
18620 keys[i].first[0] = last_coarse_cell_index;
18621 keys[i].second = i;
18622 if (i > 0 && keys[i].first < keys[i - 1].first)
18623 is_sorted = false;
18624 }
18625
18626 if (is_sorted)
18627 sorted_cell_infos[level] = level_infos;
18628 else
18629 {
18630 std::sort(keys.begin(), keys.end(), [](const auto &a, const auto &b) {
18631 return a.first < b.first;
18632 });
18633 sorted_cell_infos[level].reserve(level_infos.size());
18634 for (const auto &key : keys)
18635 sorted_cell_infos[level].push_back(level_infos[key.second]);
18636 }
18637 }
18638
18639 return sorted_cell_infos;
18640}
18641
18642
18643
18644template <int dim, int spacedim>
18647 const unsigned int magic_number1,
18648 const std::vector<bool> &v,
18649 const unsigned int magic_number2,
18650 std::ostream &out)
18651{
18652 const unsigned int N = v.size();
18653 unsigned char *flags = new unsigned char[N / 8 + 1];
18654 for (unsigned int i = 0; i < N / 8 + 1; ++i)
18655 flags[i] = 0;
18656
18657 for (unsigned int position = 0; position < N; ++position)
18658 flags[position / 8] |= (v[position] ? (1 << (position % 8)) : 0);
18659
18660 AssertThrow(out.fail() == false, ExcIO());
18661
18662 // format:
18663 // 0. magic number
18664 // 1. number of flags
18665 // 2. the flags
18666 // 3. magic number
18667 out << magic_number1 << ' ' << N << std::endl;
18668 for (unsigned int i = 0; i < N / 8 + 1; ++i)
18669 out << static_cast<unsigned int>(flags[i]) << ' ';
18670
18671 out << std::endl << magic_number2 << std::endl;
18672
18673 delete[] flags;
18674
18675 AssertThrow(out.fail() == false, ExcIO());
18676}
18677
18678
18679template <int dim, int spacedim>
18682 const unsigned int magic_number1,
18683 std::vector<bool> &v,
18684 const unsigned int magic_number2,
18685 std::istream &in)
18686{
18687 AssertThrow(in.fail() == false, ExcIO());
18688
18689 unsigned int magic_number;
18690 in >> magic_number;
18691 AssertThrow(magic_number == magic_number1, ExcGridReadError());
18692
18693 unsigned int N;
18694 in >> N;
18695 v.resize(N);
18696
18697 unsigned char *flags = new unsigned char[N / 8 + 1];
18698 unsigned short int tmp;
18699 for (unsigned int i = 0; i < N / 8 + 1; ++i)
18700 {
18701 in >> tmp;
18702 flags[i] = tmp;
18703 }
18704
18705 for (unsigned int position = 0; position != N; ++position)
18706 v[position] = ((flags[position / 8] & (1 << (position % 8))) != 0);
18707
18708 in >> magic_number;
18709 AssertThrow(magic_number == magic_number2, ExcGridReadError());
18710
18711 delete[] flags;
18712
18713 AssertThrow(in.fail() == false, ExcIO());
18714}
18715
18716
18717
18718template <int dim, int spacedim>
18721{
18722 std::size_t mem = 0;
18723 mem += sizeof(MeshSmoothing);
18724 mem += MemoryConsumption::memory_consumption(reference_cells);
18725 mem += MemoryConsumption::memory_consumption(periodic_face_pairs_level_0);
18727 for (const auto &level : levels)
18730 mem += MemoryConsumption::memory_consumption(vertices_used);
18731 mem += sizeof(default_flat_manifold) + sizeof(*default_flat_manifold);
18732 mem += sizeof(manifolds);
18733 mem += sizeof(smooth_grid);
18734 mem += MemoryConsumption::memory_consumption(number_cache);
18735 mem += sizeof(faces);
18736 if (faces)
18738
18739 return mem;
18740}
18741
18742
18743
18744template <int dim, int spacedim>
18747 default;
18748
18749#endif
18750
18751// explicit instantiations
18752#include "grid/tria.inst"
18753
*  iterator end()
*  *  for(const auto &cell :triangulation.active_cell_iterators())
*  *  iterator begin()
*  *  iterator()=default
***mech_lbc_system increment_interpolation_handlers push_back(scale_z_handler)
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
CellStatus
Definition cell_status.h:29
@ cell_will_be_refined
@ children_will_be_coarsened
iterator begin() const
Definition array_view.h:755
iterator end() const
Definition array_view.h:764
std::size_t size() const
Definition array_view.h:737
types::coarse_cell_id get_coarse_cell_id() const
Definition cell_id.h:385
std::array< std::uint64_t, 3 > binary_type
Definition cell_id.h:71
EnableObserverPointer & operator=(const EnableObserverPointer &)
virtual std::unique_ptr< Manifold< dim, spacedim > > clone() const =0
Definition point.h:111
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
unsigned int face_to_cell_lines(const unsigned int face, const unsigned int line, const types::geometric_orientation face_orientation) const
constexpr ReferenceCell< dim - 1 > face_reference_cell(const unsigned int face_index) const
unsigned int standard_to_real_face_vertex(const unsigned int vertex, const unsigned int face, const types::geometric_orientation face_orientation) const
unsigned int face_to_cell_vertices(const unsigned int face, const unsigned int vertex, const types::geometric_orientation face_orientation) const
unsigned int face_and_line_to_cell_vertices(const unsigned int face, const unsigned int line, const unsigned int vertex, const types::geometric_orientation face_orientation) const
constexpr void clear()
void join() const
IteratorState::IteratorStates state() const
virtual bool coarsening_allowed(const typename Triangulation< dim, spacedim >::cell_iterator &cell)=0
virtual ~Policy()=default
virtual Triangulation< dim, spacedim >::DistortedCellList execute_refinement(Triangulation< dim, spacedim > &triangulation, const bool check_for_distorted_cells)=0
virtual void prevent_distorted_boundary_cells(Triangulation< dim, spacedim > &triangulation)=0
virtual std::unique_ptr< Policy > clone()=0
virtual void delete_children(Triangulation< dim, spacedim > &triangulation, typename Triangulation< dim, spacedim >::cell_iterator &cell, std::vector< unsigned int > &line_cell_count, std::vector< unsigned int > &quad_cell_count)=0
virtual void prepare_refinement_dim_dependent(Triangulation< dim, spacedim > &triangulation)=0
virtual void update_neighbors(Triangulation< dim, spacedim > &tria)=0
virtual void add_periodicity(const std::vector< GridTools::PeriodicFacePair< cell_iterator > > &)
virtual types::global_cell_index n_global_active_cells() const
quad_iterator begin_quad(const unsigned int level=0) const
typename IteratorSelector::raw_line_iterator raw_line_iterator
Definition tria.h:4268
active_vertex_iterator begin_active_vertex() const
void load_user_indices_quad(const std::vector< unsigned int > &v)
unsigned int n_quads() const
Triangulation & operator=(Triangulation< dim, spacedim > &&tria) noexcept
void load_user_indices(const std::vector< unsigned int > &v)
std::vector< bool > vertices_used
Definition tria.h:4644
virtual void clear()
bool anisotropic_refinement
Definition tria.h:4663
active_quad_iterator begin_active_quad(const unsigned int level=0) const
bool get_anisotropic_refinement_flag() const
virtual const MeshSmoothing & get_mesh_smoothing() const
virtual void copy_triangulation(const Triangulation< dim, spacedim > &other_tria)
virtual types::coarse_cell_id n_global_coarse_cells() const
std::unique_ptr< std::map< unsigned int, types::manifold_id > > vertex_to_manifold_id_map_1d
Definition tria.h:4721
void save_user_pointers_quad(std::vector< void * > &v) const
void save_user_flags_hex(std::ostream &out) const
void clear_user_flags_quad()
unsigned int n_faces() const
active_hex_iterator begin_active_hex(const unsigned int level=0) const
static void read_bool_vector(const unsigned int magic_number1, std::vector< bool > &v, const unsigned int magic_number2, std::istream &in)
virtual std::weak_ptr< const Utilities::MPI::Partitioner > global_active_cell_index_partitioner() const
bool all_reference_cells_are_hyper_cube() const
void load_user_flags_line(std::istream &in)
void clear_user_data()
raw_hex_iterator begin_raw_hex(const unsigned int level=0) const
void save_user_flags_line(std::ostream &out) const
active_cell_iterator last_active() const
void save(Archive &ar, const unsigned int version) const
void reset_global_cell_indices()
std::unique_ptr<::internal::TriangulationImplementation::TriaFaces< dim > > faces
Definition tria.h:4633
face_iterator end_face() const
void reset_active_cell_indices()
cell_iterator create_cell_iterator(const CellId &cell_id) const
cell_iterator begin(const unsigned int level=0) const
void fix_coarsen_flags()
virtual MPI_Comm get_mpi_communicator() const
void save_user_pointers_line(std::vector< void * > &v) const
void load_refine_flags(std::istream &in)
void save_user_indices_line(std::vector< unsigned int > &v) const
raw_cell_iterator begin_raw(const unsigned int level=0) const
unsigned int n_lines() const
virtual void set_mesh_smoothing(const MeshSmoothing mesh_smoothing)
unsigned int n_raw_lines() const
virtual std::size_t memory_consumption() const
std::vector< Point< spacedim > > vertices
Definition tria.h:4639
raw_quad_iterator begin_raw_quad(const unsigned int level=0) const
virtual types::subdomain_id locally_owned_subdomain() const
unsigned int n_raw_faces() const
unsigned int n_active_faces() const
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
const bool check_for_distorted_cells
Definition tria.h:4670
raw_cell_iterator end_raw(const unsigned int level) const
line_iterator end_line() const
std::unique_ptr< std::map< unsigned int, types::boundary_id > > vertex_to_boundary_id_map_1d
Definition tria.h:4698
void load_user_flags_quad(std::istream &in)
unsigned int n_active_cells() const
void update_periodic_face_map()
void clear_despite_subscriptions()
void coarsen_global(const unsigned int times=1)
Triangulation(const MeshSmoothing smooth_grid=none, const bool check_for_distorted_cells=false)
std::vector< ReferenceCell< dim > > reference_cells
Definition tria.h:4163
void save_user_flags(std::ostream &out) const
void refine_global(const unsigned int times=1)
std::vector< std::unique_ptr< ::internal::TriangulationImplementation::TriaLevel< dim, spacedim > > > levels
Definition tria.h:4625
virtual std::weak_ptr< const Utilities::MPI::Partitioner > global_level_cell_index_partitioner(const unsigned int level) const
void load_user_flags_hex(std::istream &in)
const std::vector< Point< spacedim > > & get_vertices() const
void load_user_pointers_quad(const std::vector< void * > &v)
unsigned int n_used_vertices() const
void reset_cell_vertex_indices_cache()
unsigned int n_active_lines() const
unsigned int n_levels() const
void load_user_indices_line(const std::vector< unsigned int > &v)
void clear_user_flags_hex()
void save_user_pointers_hex(std::vector< void * > &v) const
typename IteratorSelector::raw_quad_iterator raw_quad_iterator
Definition tria.h:4269
void load_user_pointers(const std::vector< void * > &v)
unsigned int register_data_attach(const std::function< std::vector< char >(const cell_iterator &, const ::CellStatus)> &pack_callback, const bool returns_variable_size_data)
::internal::TriangulationImplementation::NumberCache< dim > number_cache
Definition tria.h:4681
void save_attached_data(const unsigned int global_first_cell, const unsigned int global_num_cells, const std::string &file_basename) const
void save_user_indices_hex(std::vector< unsigned int > &v) const
DistortedCellList execute_refinement()
void update_cell_relations()
active_line_iterator begin_active_line(const unsigned int level=0) const
void save_user_indices_quad(std::vector< unsigned int > &v) const
void load_user_pointers_hex(const std::vector< void * > &v)
void pack_data_serial()
cell_iterator end() const
virtual bool has_hanging_nodes() const
std::vector< GridTools::PeriodicFacePair< cell_iterator > > periodic_face_pairs_level_0
Definition tria.h:4244
unsigned int n_raw_cells(const unsigned int level) const
bool contains_cell(const CellId &cell_id) const
std::vector< std::vector< TriangulationDescription::CellData< dim > > > get_sorted_cell_infos(const std::vector< std::vector< TriangulationDescription::CellData< dim > > > &cell_infos) const
void load_attached_data(const unsigned int global_first_cell, const unsigned int global_num_cells, const unsigned int local_num_cells, const std::string &file_basename, const unsigned int n_attached_deserialize_fixed, const unsigned int n_attached_deserialize_variable)
void load_coarsen_flags(std::istream &out)
quad_iterator end_quad() const
line_iterator begin_line(const unsigned int level=0) const
unsigned int max_adjacent_cells() const
vertex_iterator begin_vertex() const
void clear_user_flags()
unsigned int n_hexs() const
vertex_iterator end_vertex() const
bool vertex_used(const unsigned int index) const
void load_user_pointers_line(const std::vector< void * > &v)
hex_iterator end_hex() const
hex_iterator begin_hex(const unsigned int level=0) const
virtual void execute_coarsening_and_refinement()
active_cell_iterator end_active(const unsigned int level) const
bool is_mixed_mesh() const
cell_iterator last() const
unsigned int n_active_quads() const
void load_user_indices_hex(const std::vector< unsigned int > &v)
unsigned int n_raw_quads() const
void save_user_pointers(std::vector< void * > &v) const
face_iterator begin_face() const
unsigned int n_cells() const
virtual bool prepare_coarsening_and_refinement()
void unpack_data_serial()
const std::vector< bool > & get_used_vertices() const
typename IteratorSelector::raw_hex_iterator raw_hex_iterator
Definition tria.h:4270
MeshSmoothing smooth_grid
Definition tria.h:4157
void save_refine_flags(std::ostream &out) const
Triangulation< dim, spacedim > & get_triangulation()
std::unique_ptr< Policy > policy
Definition tria.h:4235
void save_user_flags_quad(std::ostream &out) const
void compute_line_to_adjacent_cells_map()
Signals signals
Definition tria.h:2606
virtual ~Triangulation() override
internal::TriangulationImplementation::Strides< dim > strides
Definition tria.h:4168
unsigned int n_vertices() const
void load(Archive &ar, const unsigned int version)
void save_user_indices(std::vector< unsigned int > &v) const
void notify_ready_to_unpack(const unsigned int handle, const std::function< void(const cell_iterator &, const ::CellStatus, const boost::iterator_range< std::vector< char >::const_iterator > &)> &unpack_callback)
bool all_reference_cells_are_simplex() const
unsigned int n_raw_hexs(const unsigned int level) const
void set_all_refine_flags()
const std::map< std::pair< cell_iterator, unsigned int >, std::pair< std::pair< cell_iterator, unsigned int >, types::geometric_orientation > > & get_periodic_face_map() const
unsigned int n_active_hexs() const
virtual std::vector< types::boundary_id > get_boundary_ids() const
const std::vector< ReferenceCell< dim > > & get_reference_cells() const
void load_user_flags(std::istream &in)
void reset_policy()
void save_coarsen_flags(std::ostream &out) const
active_face_iterator begin_active_face() const
void clear_user_flags_line()
raw_line_iterator begin_raw_line(const unsigned int level=0) const
static void write_bool_vector(const unsigned int magic_number1, const std::vector< bool > &v, const unsigned int magic_number2, std::ostream &out)
void flip_all_direction_flags()
active_cell_iterator begin_active(const unsigned int level=0) const
void execute_coarsening()
typename std::pair< cell_iterator, CellStatus > cell_relation_t
Definition tria.h:461
std::unique_ptr< typename Triangulation< dim, spacedim >::Policy > clone() override
Definition tria.cc:2248
void prevent_distorted_boundary_cells(Triangulation< dim, spacedim > &triangulation) override
Definition tria.cc:2226
void prepare_refinement_dim_dependent(Triangulation< dim, spacedim > &triangulation) override
Definition tria.cc:2233
void delete_children(Triangulation< dim, spacedim > &tria, typename Triangulation< dim, spacedim >::cell_iterator &cell, std::vector< unsigned int > &line_cell_count, std::vector< unsigned int > &quad_cell_count) override
Definition tria.cc:2209
void update_neighbors(Triangulation< dim, spacedim > &tria) override
Definition tria.cc:2203
bool coarsening_allowed(const typename Triangulation< dim, spacedim >::cell_iterator &cell) override
Definition tria.cc:2240
Triangulation< dim, spacedim >::DistortedCellList execute_refinement(Triangulation< dim, spacedim > &triangulation, const bool check_for_distorted_cells) override
Definition tria.cc:2219
std::vector< types::manifold_id > manifold_id
std::vector< BoundaryOrMaterialId > boundary_or_material_id
constexpr iterator end() noexcept
constexpr size_type size() const noexcept
constexpr iterator begin() noexcept
constexpr LibraryBuildMode library_build_mode
Definition config.h:66
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
constexpr bool running_in_debug_mode()
Definition config.h:76
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define AssertIsNotUsed(obj)
#define DEAL_II_ASSERT_UNREACHABLE()
#define DEAL_II_NOT_IMPLEMENTED()
Point< 2 > second
Definition grid_out.cc:4640
Point< 2 > first
Definition grid_out.cc:4639
unsigned int level
Definition grid_out.cc:4642
AdjacentCell adjacent_cells[2]
unsigned int vertex_indices[2]
IteratorRange< active_cell_iterator > active_cell_iterators_on_level(const unsigned int level) const
IteratorRange< active_face_iterator > active_face_iterators() const
IteratorRange< active_cell_iterator > active_cell_iterators() const
IteratorRange< cell_iterator > cell_iterators_on_level(const unsigned int level) const
IteratorRange< cell_iterator > cell_iterators() const
static ::ExceptionBase & ExcInternalErrorOnCell(int arg1)
static ::ExceptionBase & ExcIO()
static ::ExceptionBase & ExcInteriorQuadCantBeBoundary(int arg1, int arg2, int arg3, int arg4, types::boundary_id arg5)
static ::ExceptionBase & ExcNotImplemented()
static ::ExceptionBase & ExcInconsistentLineInfoOfLine(int arg1, int arg2, std::string arg3)
static ::ExceptionBase & ExcCellHasNegativeMeasure(int arg1)
#define Assert(cond, exc)
static ::ExceptionBase & ExcImpossibleInDim(int arg1)
static ::ExceptionBase & ExcMemoryInexact(int arg1, int arg2)
#define DeclException2(Exception2, type1, type2, outsequence)
#define AssertDimension(dim1, dim2)
#define AssertThrowMPI(error_code)
static ::ExceptionBase & ExcGridHasInvalidCell(int arg1)
static ::ExceptionBase & ExcMultiplySetLineInfoOfLine(int arg1, int arg2)
#define AssertNothrow(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcInteriorLineCantBeBoundary(int arg1, int arg2, types::boundary_id arg3)
#define DeclException3(Exception3, type1, type2, type3, outsequence)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcInvalidVertexIndex(int arg1, int arg2, int arg3)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define DeclException5( Exception5, type1, type2, type3, type4, type5, outsequence)
#define AssertThrow(cond, exc)
static ::ExceptionBase & ExcInconsistentQuadInfoOfQuad(int arg1, int arg2, int arg3, int arg4, std::string arg5)
typename IteratorSelector::hex_iterator hex_iterator
Definition tria.h:1774
typename IteratorSelector::active_quad_iterator active_quad_iterator
Definition tria.h:1765
typename IteratorSelector::active_hex_iterator active_hex_iterator
Definition tria.h:1785
typename IteratorSelector::quad_iterator quad_iterator
Definition tria.h:1750
typename IteratorSelector::line_iterator line_iterator
Definition tria.h:1726
typename IteratorSelector::active_line_iterator active_line_iterator
Definition tria.h:1741
void set_all_manifold_ids_on_boundary(const types::manifold_id number)
const Manifold< dim, spacedim > & get_manifold(const types::manifold_id number) const
virtual std::vector< types::manifold_id > get_manifold_ids() const
void reset_manifold(const types::manifold_id manifold_number)
void set_manifold(const types::manifold_id number, const Manifold< dim, spacedim > &manifold_object)
void reset_all_manifolds()
void set_all_manifold_ids(const types::manifold_id number)
Task< RT > new_task(const std::function< RT()> &function)
const unsigned int mn_tria_refine_flags_end
const unsigned int mn_tria_coarsen_flags_end
const unsigned int mn_tria_refine_flags_begin
const unsigned int mn_tria_hex_user_flags_end
const unsigned int mn_tria_line_user_flags_begin
const unsigned int mn_tria_line_user_flags_end
const unsigned int mn_tria_quad_user_flags_end
const unsigned int mn_tria_coarsen_flags_begin
const unsigned int mn_tria_hex_user_flags_begin
const unsigned int mn_tria_quad_user_flags_begin
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733
void create_triangulation(Triangulation< dim, dim > &tria, const AdditionalData &additional_data=AdditionalData())
void reference_cell(Triangulation< dim, spacedim > &tria, const ReferenceCell< dim > &reference_cell)
std::vector< std::set< typename Triangulation< dim, spacedim >::active_cell_iterator > > vertex_to_cell_map(const Triangulation< dim, spacedim > &triangulation)
double diameter(const Triangulation< dim, spacedim > &tria)
@ valid
Iterator points to a valid object.
constexpr char N
double norm(const FEValuesBase< dim > &fe, const ArrayView< const std::vector< Tensor< 1, dim > > > &Du)
Definition divergence.h:469
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
Tensor< 2, dim, Number > l(const Tensor< 2, dim, Number > &F, const Tensor< 2, dim, Number > &dF_dt)
*  *  if(update_pressure &update_flags) *  compute_pressure(constitutive_request
*  *  *  *  std::vector< Number > ThermoPlasticMaterial< dim, ViscoplasticYieldLaw, Number >::get_state_parameters   const
constexpr ReferenceCell< 3 > Hexahedron
constexpr ReferenceCell< 2 > Quadrilateral
constexpr ReferenceCell< 1 > Line
constexpr ReferenceCell< 2 > Triangle
constexpr ReferenceCell< 3 > Tetrahedron
constexpr ReferenceCell< 3 > Pyramid
constexpr ReferenceCell< 3 > Wedge
constexpr ReferenceCell< 0 > Vertex
int File_write_at_c(MPI_File fh, MPI_Offset offset, const void *buf, MPI_Count count, MPI_Datatype datatype, MPI_Status *status)
int File_read_at_c(MPI_File fh, MPI_Offset offset, void *buf, MPI_Count count, MPI_Datatype datatype, MPI_Status *status)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
Definition mpi.cc:103
T max(const T &t, const MPI_Comm mpi_communicator)
std::vector< T > compute_set_union(const std::vector< T > &vec, const MPI_Comm comm)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:118
std::size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
Definition utilities.h:1352
constexpr T fixed_power(const T t)
Definition utilities.h:942
void load(Archive &ar, ::std_cxx26::inplace_vector< T, N > &vec, const unsigned int)
void save(Archive &ar, const ::std_cxx26::inplace_vector< T, N > &vec, const unsigned int)
void build_face_entities_templated(const std::vector< ReferenceCell< dim > > &cell_types, const ArrayOfArrays &cells_to_vertices, ArrayOfArrays &cells_to_faces, ArrayOfArrays &crs_0, std::vector< types::geometric_orientation > &orientations, const FU &second_key_function)
Definition tria.cc:2588
ArrayOfArrays determine_neighbors(const ArrayOfArrays &cells_to_faces)
Definition tria.cc:2513
Connectivity< dim > build_connectivity(const std::vector< ReferenceCell< dim > > &cell_types, const std::set< ReferenceCell< dim > > &all_reference_cells, const ArrayOfArrays &cells_to_vertices)
Definition tria.cc:2979
unsigned int n_active_cells(const internal::TriangulationImplementation::NumberCache< 1 > &c)
Definition tria.cc:15816
void build_face_entities(const std::vector< ReferenceCell< dim > > &cell_types, const ArrayOfArrays &cells_to_vertices, ArrayOfArrays &cells_to_faces, ArrayOfArrays &crs_0, std::vector< types::geometric_orientation > &orientations, const FU &second_key_function)
Definition tria.cc:2804
void build_intersection(const std::vector< ReferenceCell< dim > > &cell_types, const ArrayOfArrays &cells_to_vertices, const ArrayOfArrays &cells_to_lines, const ArrayOfArrays &lines_to_vertices, const ArrayOfArrays &cells_to_quads, const ArrayOfArrays &quads_to_vertices, const std::vector< types::geometric_orientation > &ori_cq, ArrayOfArrays &quads_to_lines, std::vector< types::geometric_orientation > &ori_ql, std::vector< ReferenceCell< 2 > > &quad_t_id)
Definition tria.cc:2864
unsigned int n_cells(const internal::TriangulationImplementation::NumberCache< 1 > &c)
Definition tria.cc:15809
void monitor_memory(const TriaLevel< dim, spacedim > &tria_level, const unsigned int true_dimension)
Definition tria.cc:2067
std::tuple< bool, bool, bool > split_face_orientation(const types::geometric_orientation combined_orientation)
constexpr std::uint8_t max_n_levels
Definition types.h:415
constexpr unsigned int invalid_unsigned_int
Definition types.h:228
constexpr types::boundary_id internal_face_boundary_id
Definition types.h:319
constexpr types::manifold_id flat_manifold_id
Definition types.h:332
constexpr types::geometric_orientation reverse_line_orientation
Definition types.h:355
constexpr types::subdomain_id invalid_subdomain_id
Definition types.h:385
constexpr types::coarse_cell_id invalid_coarse_cell_id
Definition types.h:272
constexpr types::geometric_orientation default_geometric_orientation
Definition types.h:342
STL namespace.
::VectorizedArray< Number, width > min(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
unsigned int manifold_id
Definition types.h:171
unsigned int boundary_id
Definition types.h:159
std::uint8_t geometric_orientation
Definition types.h:38
IsotropicRefinementChoice
std_cxx26::inplace_vector< unsigned int, ReferenceCells::max_n_vertices< structdim >()> vertices
Definition cell_data.h:84
static unsigned int child_cell_on_face(const RefinementCase< dim > &ref_case, const unsigned int face, const unsigned int subface, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false, const RefinementCase< dim - 1 > &face_refinement_case=RefinementCase< dim - 1 >::isotropic_refinement)
static RefinementCase< dim - 1 > face_refinement_case(const RefinementCase< dim > &cell_refinement_case, const unsigned int face_no, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static std_cxx20::ranges::iota_view< unsigned int, unsigned int > face_indices()
static RefinementCase< dim > min_cell_refinement_case_for_face_refinement(const RefinementCase< dim - 1 > &face_refinement_case, const unsigned int face_no, const bool face_orientation=true, const bool face_flip=false, const bool face_rotation=false)
static unsigned int n_children(const RefinementCase< dim > &refinement_case)
static void alternating_form_at_vertices(const Point< spacedim >(&vertices)[vertices_per_cell], Tensor< spacedim - dim, spacedim >(&forms)[vertices_per_cell])
std::vector< CellData< 2 > > boundary_quads
Definition cell_data.h:247
bool check_consistency(const unsigned int dim) const
std::vector< CellData< 1 > > boundary_lines
Definition cell_data.h:231
std::vector< std::vector< CellData< dim > > > cell_infos
std::vector<::CellData< dim > > coarse_cells
std::vector< Point< spacedim > > coarse_cell_vertices
virtual ~DistortedCellList() noexcept override
std::list< typename Triangulation< dim, spacedim >::cell_iterator > distorted_cells
Definition tria.h:1820
boost::signals2::signal< void(const Triangulation< dim, spacedim > &destination_tria)> copy
Definition tria.h:2452
boost::signals2::signal< void(const typename Triangulation< dim, spacedim >::cell_iterator &cell)> post_refinement_on_cell
Definition tria.h:2442
ArrayView< const unsigned int > operator[](const unsigned int i) const
Definition tria.cc:2321
ArrayOfArrays & operator=(ArrayOfArrays &&)=default
ArrayOfArrays(const std::vector< std::size_t > &offsets, const std::vector< unsigned int > &columns)
Definition tria.cc:2280
ArrayOfArrays & operator=(const ArrayOfArrays &)=default
ArrayOfArrays(std::vector< std::size_t > &&offsets, std::vector< unsigned int > &&columns)
Definition tria.cc:2290
const ArrayOfArrays & entity_to_entities(const unsigned int from, const unsigned int to) const
Definition tria.cc:2465
Connectivity(const std::vector< ReferenceCell< dim > > &cell_types, const std::set< ReferenceCell< dim > > &all_reference_cells)
Definition tria.cc:2374
std::vector< ReferenceCell< structdim > > & entity_types()
Definition tria.cc:2415
std::set< ReferenceCell< dim > > all_reference_cells_
Definition tria.cc:2486
std::vector< types::geometric_orientation > line_orientation
Definition tria.cc:2490
const std::vector< ReferenceCell< structdim > > & entity_types() const
Definition tria.cc:2431
std::vector< ReferenceCell< 2 > > quad_types
Definition tria.cc:2500
ArrayOfArrays & entity_to_entities(const unsigned int from, const unsigned int to)
Definition tria.cc:2446
const std::set< ReferenceCell< dim > > & all_reference_cells() const
Definition tria.cc:2386
std::vector< types::geometric_orientation > & entity_orientations(const unsigned int structdim)
Definition tria.cc:2392
const std::vector< types::geometric_orientation > & entity_orientations(const unsigned int structdim) const
Definition tria.cc:2403
std::vector< types::geometric_orientation > quad_orientation
Definition tria.cc:2495
std::vector< ReferenceCell< dim > > cell_types
Definition tria.cc:2484
static void update_neighbors(Triangulation< 1, spacedim > &)
Definition tria.cc:12763
static void prevent_distorted_boundary_cells(Triangulation< dim, spacedim > &)
Definition tria.cc:12843
static Triangulation< dim, spacedim >::DistortedCellList execute_refinement(Triangulation< dim, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition tria.cc:12834
static bool coarsening_allowed(const typename Triangulation< dim, spacedim >::cell_iterator &)
Definition tria.cc:12859
static void update_neighbors(Triangulation< dim, spacedim > &triangulation)
Definition tria.cc:12768
static void prepare_refinement_dim_dependent(Triangulation< dim, spacedim > &triangulation)
Definition tria.cc:12851
static void delete_children(Triangulation< dim, spacedim > &, typename Triangulation< dim, spacedim >::cell_iterator &, std::vector< unsigned int > &, std::vector< unsigned int > &)
Definition tria.cc:12823
static void compute_number_cache_dim(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< 2 > &number_cache)
Definition tria.cc:3331
static void prevent_distorted_boundary_cells(Triangulation< 1, spacedim > &)
Definition tria.cc:12371
static void process_subcelldata(const ArrayOfArrays &crs, TriaObjects &obj, const std::vector< CellData< structdim > > &boundary_objects_in, const std::vector< Point< spacedim > > &vertex_locations)
Definition tria.cc:3973
static void update_neighbors(Triangulation< dim, spacedim > &triangulation)
Definition tria.cc:3555
static void prepare_refinement_dim_dependent(const Triangulation< dim, spacedim > &)
Definition tria.cc:12458
static void delete_children(Triangulation< 3, spacedim > &triangulation, typename Triangulation< 3, spacedim >::cell_iterator &cell, std::vector< unsigned int > &line_cell_count, std::vector< unsigned int > &quad_cell_count)
Definition tria.cc:4350
static void update_neighbors(Triangulation< 1, spacedim > &)
Definition tria.cc:3549
static Triangulation< 3, spacedim >::DistortedCellList execute_refinement(Triangulation< 3, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition tria.cc:7990
static void compute_number_cache(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< dim > &number_cache)
Definition tria.cc:3527
static void create_children(Triangulation< 2, spacedim > &triangulation, unsigned int &next_unused_vertex, typename Triangulation< 2, spacedim >::raw_line_iterator &next_unused_line, typename Triangulation< 2, spacedim >::raw_cell_iterator &next_unused_cell, const typename Triangulation< 2, spacedim >::cell_iterator &cell)
Definition tria.cc:4978
static void prevent_distorted_boundary_cells(Triangulation< dim, spacedim > &triangulation)
Definition tria.cc:12378
static Triangulation< 1, spacedim >::DistortedCellList execute_refinement_isotropic(Triangulation< 1, spacedim > &, const bool)
Definition tria.cc:5350
static bool coarsening_allowed(const typename Triangulation< dim, spacedim >::cell_iterator &cell)
Definition tria.cc:12691
static void compute_number_cache_dim(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< 3 > &number_cache)
Definition tria.cc:3436
static Triangulation< 2, spacedim >::DistortedCellList execute_refinement_isotropic(Triangulation< 2, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition tria.cc:5361
static void delete_children(Triangulation< 1, spacedim > &triangulation, typename Triangulation< 1, spacedim >::cell_iterator &cell, std::vector< unsigned int > &, std::vector< unsigned int > &)
Definition tria.cc:4108
static void prepare_refinement_dim_dependent(Triangulation< 3, spacedim > &triangulation)
Definition tria.cc:12468
static void delete_children(Triangulation< 2, spacedim > &triangulation, typename Triangulation< 2, spacedim >::cell_iterator &cell, std::vector< unsigned int > &line_cell_count, std::vector< unsigned int > &)
Definition tria.cc:4212
static void compute_number_cache_dim(const Triangulation< dim, spacedim > &triangulation, const unsigned int level_objects, internal::TriangulationImplementation::NumberCache< 1 > &number_cache)
Definition tria.cc:3243
static Triangulation< 1, spacedim >::DistortedCellList execute_refinement(Triangulation< 1, spacedim > &triangulation, const bool)
Definition tria.cc:5841
static Triangulation< 3, spacedim >::DistortedCellList execute_refinement_isotropic(Triangulation< 3, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition tria.cc:6397
static void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata, Triangulation< dim, spacedim > &tria)
Definition tria.cc:3697
static Triangulation< 2, spacedim >::DistortedCellList execute_refinement(Triangulation< 2, spacedim > &triangulation, const bool check_for_distorted_cells)
Definition tria.cc:6076
Strides of various arrays in a Triangulation.
Definition tria.h:356
std::vector< std::vector< CellData< dim > > > cell_infos