225 * #include <deal.II/base/function_parser.h>
226 * #include <deal.II/base/index_set.h>
227 * #include <deal.II/base/parameter_handler.h>
228 * #include <deal.II/base/quadrature_lib.h>
229 * #include <deal.II/base/utilities.h>
231 * #include <deal.II/dofs/dof_handler.h>
232 * #include <deal.II/dofs/dof_tools.h>
234 * #include <deal.II/fe/fe_nedelec.h>
235 * #include <deal.II/fe/fe_series.h>
236 * #include <deal.II/fe/fe_values.h>
238 * #include <deal.II/grid/tria.h>
239 * #include <deal.II/grid/tria_iterator.h>
241 * #include <deal.II/lac/affine_constraints.h>
242 * #include <deal.II/lac/full_matrix.h>
243 * #include <deal.II/lac/petsc_precondition.h>
244 * #include <deal.II/lac/petsc_sparse_matrix.h>
245 * #include <deal.II/lac/petsc_vector.h>
246 * #include <deal.II/lac/slepc_solver.h>
248 * #include <deal.II/numerics/data_out.h>
249 * #include <deal.II/numerics/vector_tools.h>
253 * For parallelization (
using WorkStream and Intel TBB)
256 * #include <deal.II/base/multithread_info.h>
257 * #include <deal.II/base/work_stream.h>
259 * #include
"petscpc.h"
263 * For Error Estimation/Indication and Smoothness Indication
266 * #include <deal.II/fe/fe_tools.h>
268 * #include <deal.II/numerics/error_estimator.h>
269 * #include <deal.II/numerics/smoothness_estimator.h>
275 * #include <deal.II/grid/grid_refinement.h>
278 * #include <iostream>
281 *
namespace Operations
288 * curlcurl(const ::FEValues<2> &fe_values,
289 *
const unsigned int & i,
290 *
const unsigned int & j,
291 *
const unsigned int & q_point)
293 *
auto gradu1_x1x2 = fe_values.shape_grad_component(i, q_point, 0);
294 *
auto gradu2_x1x2 = fe_values.shape_grad_component(i, q_point, 1);
296 *
auto gradv1_x1x2 = fe_values.shape_grad_component(j, q_point, 0);
297 *
auto gradv2_x1x2 = fe_values.shape_grad_component(j, q_point, 1);
298 *
return (gradu2_x1x2[0] - gradu1_x1x2[1]) *
299 * (gradv2_x1x2[0] - gradv1_x1x2[1]);
307 * dot_term(const ::FEValues<dim> &fe_values,
308 *
const unsigned int & i,
309 *
const unsigned int & j,
310 *
const unsigned int & q_point)
312 *
double output = 0.0;
313 *
for (
unsigned int comp = 0; comp < dim; ++comp)
315 * output += fe_values.shape_value_component(i, q_point, comp) *
316 * fe_values.shape_value_component(j, q_point, comp);
326 *
namespace Structures
333 *
const unsigned int dim = 2;
335 *
const std::vector<Point<2>> vertices = {{scaling * 0.0, scaling * 0.0},
336 * {scaling * 0.5, scaling * 0.0},
337 * {scaling * 0.0, scaling * 0.5},
338 * {scaling * 0.5, scaling * 0.5},
339 * {scaling * 0.0, scaling * 1.0},
340 * {scaling * 0.5, scaling * 1.0},
341 * {scaling * 1.0, scaling * 0.5},
342 * {scaling * 1.0, scaling * 1.0}};
344 *
const std::vector<std::array<int, GeometryInfo<dim>::vertices_per_cell>>
345 * cell_vertices = {{{0, 1, 2, 3}}, {{2, 3, 4, 5}}, {{3, 6, 5, 7}}};
346 *
const unsigned int n_cells = cell_vertices.size();
347 * std::vector<CellData<dim>> cells(n_cells,
CellData<dim>());
348 *
for (
unsigned int i = 0; i <
n_cells; ++i)
350 *
for (
unsigned int j = 0; j < cell_vertices[i].size(); ++j)
351 * cells[i].vertices[j] = cell_vertices[i][j];
352 * cells[i].material_id = 0;
361 *
const double & scaling)
363 *
const unsigned int dim = 2;
365 *
const std::vector<Point<2>> vertices = {{scaling * 0.0, scaling * 0.0},
366 * {scaling * 0.6, scaling * 0.0},
367 * {scaling * 0.0, scaling * 0.3},
368 * {scaling * 0.6, scaling * 0.3}};
370 *
const std::vector<std::array<int, GeometryInfo<dim>::vertices_per_cell>>
371 * cell_vertices = {{{0, 1, 2, 3}}};
372 *
const unsigned int n_cells = cell_vertices.size();
373 * std::vector<CellData<dim>> cells(n_cells,
CellData<dim>());
374 *
for (
unsigned int i = 0; i <
n_cells; ++i)
376 *
for (
unsigned int j = 0; j < cell_vertices[i].size(); ++j)
377 * cells[i].vertices[j] = cell_vertices[i][j];
378 * cells[i].material_id = 0;
408 *
virtual unsigned int
409 * solve_problem() = 0;
411 * set_refinement_cycle(
const unsigned int cycle);
414 * output_solution() = 0;
419 *
unsigned int refinement_cycle = 0;
420 * std::unique_ptr<ParameterHandler> parameters;
421 *
unsigned int n_eigenpairs = 1;
422 *
double target = 0.0;
423 *
unsigned int eigenpair_selection_scheme;
424 *
unsigned int max_cycles = 0;
425 * ompi_communicator_t * mpi_communicator = PETSC_COMM_SELF;
434 * , parameters(std::make_unique<ParameterHandler>())
436 * parameters->declare_entry(
437 *
"Eigenpair selection scheme",
440 *
"The type of eigenpairs to find (0 - smallest, 1 - target)");
441 * parameters->declare_entry(
"Number of eigenvalues/eigenfunctions",
444 *
"The number of eigenvalues/eigenfunctions "
445 *
"to be computed.");
446 * parameters->declare_entry(
"Target eigenvalue",
449 *
"The target eigenvalue (if scheme == 1)");
451 * parameters->declare_entry(
"Cycles number",
454 *
"The number of cycles in refinement");
455 * parameters->parse_input(prm_file);
457 * eigenpair_selection_scheme =
458 * parameters->get_integer(
"Eigenpair selection scheme");
462 * The
project currently only supports selection by a target eigenvalue.
463 * Furthermore, only one eigenpair can be computed at a time.
466 * assert(eigenpair_selection_scheme == 1 &&
467 *
"Selection by a target is the only currently supported option!");
469 * parameters->get_integer(
"Number of eigenvalues/eigenfunctions");
471 * n_eigenpairs == 1 &&
472 *
"Only the computation of a single eigenpair is currently supported!");
474 * target = parameters->get_double(
"Target eigenvalue");
475 * max_cycles = parameters->get_integer(
"Cycles number");
476 *
if (eigenpair_selection_scheme == 1)
482 * Base<dim>::set_refinement_cycle(
const unsigned int cycle)
484 * refinement_cycle = cycle;
493 *
class EigenSolver :
public virtual Base<dim>
496 * EigenSolver(
const std::string & prm_file,
498 *
const unsigned int &minimum_degree,
499 *
const unsigned int &maximum_degree,
500 *
const unsigned int &starting_degree);
502 *
virtual unsigned int
503 * solve_problem()
override;
505 *
virtual unsigned int
508 *
template <
class SolverType>
510 * initialize_eigensolver(SolverType &eigensolver);
519 *
const std::unique_ptr<hp::FECollection<dim>> fe_collection;
520 * std::unique_ptr<hp::QCollection<dim>> quadrature_collection;
521 * std::unique_ptr<
hp::QCollection<dim - 1>> face_quadrature_collection;
523 *
const unsigned int max_degree, min_degree;
526 *
for the actual solution
529 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> eigenfunctions;
530 * std::unique_ptr<std::vector<double>>
eigenvalues;
540 * convert_solution();
552 * EigenSolver<dim>::EigenSolver(
const std::string & prm_file,
554 *
const unsigned int &minimum_degree,
555 *
const unsigned int &maximum_degree,
556 *
const unsigned int &starting_degree)
562 * , max_degree(maximum_degree)
563 * , min_degree(minimum_degree)
565 * std::make_unique<std::vector<PETScWrappers::MPI::Vector>>())
566 * ,
eigenvalues(std::make_unique<std::vector<double>>())
568 *
for (
unsigned int degree = min_degree; degree <= max_degree; ++degree)
573 * Generate quadrature collection with
sorted quadrature weights
578 * quadrature_collection->push_back(sorted_quadrature);
580 *
const QGauss<dim - 1> face_quadrature(degree + 1);
581 *
const QSorted<dim - 1> sorted_face_quadrature(face_quadrature);
582 * face_quadrature_collection->push_back(sorted_face_quadrature);
586 * adjust the discretization
589 *
if (starting_degree > min_degree && starting_degree <= max_degree)
591 *
const unsigned int start_diff = starting_degree - min_degree;
593 * cell1 = dof_handler.begin_active(),
594 * endc1 = dof_handler.end();
595 *
for (; cell1 < endc1; ++cell1)
597 * cell1->set_active_fe_index(start_diff);
608 * EigenSolver<dim>::get_lambda_h()
610 *
return &(*eigenvalues)[0];
619 * EigenSolver<dim>::get_solution()
629 * EigenSolver<dim>::convert_solution()
631 * solution.
reinit((*eigenfunctions)[0].
size());
632 *
for (
unsigned int i = 0; i < solution.size(); ++i)
633 * solution[i] = (*eigenfunctions)[0][i];
643 *
template <
class SolverType>
645 * EigenSolver<dim>::initialize_eigensolver(SolverType &eigensolver)
649 * From the parameters
class, initialize the eigensolver...
652 *
switch (this->eigenpair_selection_scheme)
655 * eigensolver.set_which_eigenpairs(EPS_TARGET_MAGNITUDE);
658 * eigensolver.set_target_eigenvalue(this->target);
663 * eigensolver.set_which_eigenpairs(EPS_SMALLEST_MAGNITUDE);
667 * eigensolver.set_problem_type(EPS_GHEP);
670 *
apply a Shift-Invert spectrum transformation
676 *
double shift_scalar = this->parameters->get_double(
"Target eigenvalue");
685 * this->mpi_communicator, additional_data);
687 * eigensolver.set_transformation(spectral_transformation);
688 * eigensolver.set_target_eigenvalue(this->target);
697 * EigenSolver<dim>::solve_problem()
707 * this->mpi_communicator);
709 * initialize_eigensolver(eigensolver);
716 * eigensolver.solve(stiffness_matrix,
720 * eigenfunctions->size());
721 *
for (
auto &entry : *eigenfunctions)
723 * constraints.distribute(entry);
725 * convert_solution();
727 *
return solver_control.last_step();
732 * EigenSolver<dim>::n_dofs() const
734 *
return dof_handler.n_dofs();
744 * EigenSolver<dim>::setup_system()
746 * dof_handler.distribute_dofs(*fe_collection);
747 * constraints.clear();
750 * constraints.close();
752 * eigenfunctions->resize(this->n_eigenpairs);
755 *
IndexSet eigenfunction_index_set = dof_handler.locally_owned_dofs();
757 *
for (
auto &entry : *eigenfunctions)
759 * entry.
reinit(eigenfunction_index_set, MPI_COMM_WORLD);
768 * EigenSolver<dim>::assemble_system()
771 * *quadrature_collection,
777 * Prep the system matrices
for the solution
780 * stiffness_matrix.reinit(dof_handler.n_dofs(),
781 * dof_handler.n_dofs(),
782 * dof_handler.max_couplings_between_dofs());
784 * dof_handler.n_dofs(),
785 * dof_handler.max_couplings_between_dofs());
788 * std::vector<types::global_dof_index> local_dof_indices;
790 *
for (
const auto &cell : dof_handler.active_cell_iterators())
792 * const unsigned
int dofs_per_cell = cell->get_fe().dofs_per_cell;
794 * cell_stiffness_matrix.reinit(dofs_per_cell, dofs_per_cell);
795 * cell_stiffness_matrix = 0;
797 * cell_mass_matrix.reinit(dofs_per_cell, dofs_per_cell);
798 * cell_mass_matrix = 0;
800 * hp_fe_values.reinit(cell);
804 *
for (
unsigned int q_point = 0; q_point < fe_values.n_quadrature_points;
807 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
809 *
for (
unsigned int j = 0; j < dofs_per_cell; ++j)
813 * Note that (in general) the Nedelec element is not
814 * primitive, namely that the shape
functions are vectorial
815 * with components in more than one direction
821 * cell_stiffness_matrix(i, j) +=
822 * Operations::curlcurl(fe_values, i, j, q_point) *
823 * fe_values.JxW(q_point);
825 * cell_mass_matrix(i, j) +=
826 * (Operations::dot_term(fe_values, i, j, q_point)) *
827 * fe_values.JxW(q_point);
830 * local_dof_indices.resize(dofs_per_cell);
831 * cell->get_dof_indices(local_dof_indices);
834 * constraints.distribute_local_to_global(cell_stiffness_matrix,
837 * constraints.distribute_local_to_global(cell_mass_matrix,
844 *
for (
unsigned int i = 0; i < dof_handler.n_dofs(); ++i)
845 *
if (constraints.is_constrained(i))
847 * stiffness_matrix.set(i, i, 10000.0);
852 * since we have just set individual elements, we need the following
864 *
class PrimalSolver :
public EigenSolver<dim>
867 * PrimalSolver(
const std::string & prm_file,
869 *
const unsigned int &min_degree,
870 *
const unsigned int &max_degree,
871 *
const unsigned int &starting_degree);
876 *
virtual unsigned int
877 * n_dofs()
const override;
881 * PrimalSolver<dim>::PrimalSolver(
const std::string & prm_file,
883 *
const unsigned int &min_degree,
884 *
const unsigned int &max_degree,
885 *
const unsigned int &starting_degree)
887 * , EigenSolver<dim>(prm_file,
900 * PrimalSolver<dim>::output_solution()
905 *
for (
const auto &cell : this->dof_handler.active_cell_iterators())
906 * fe_degrees(cell->active_cell_index()) =
907 * (*this->fe_collection)[cell->active_fe_index()].degree;
908 * data_out.add_data_vector(fe_degrees,
"fe_degree");
909 * data_out.add_data_vector((*this->eigenfunctions)[0],
910 * std::string(
"eigenfunction_no_") +
913 * std::cout <<
"Eigenvalue: " << (*this->
eigenvalues)[0]
914 * <<
" NDoFs: " << this->dof_handler.n_dofs() << std::endl;
915 * std::ofstream eigenvalues_out(
916 *
"eigenvalues-" + std::to_string(this->refinement_cycle) +
".txt");
918 * eigenvalues_out << std::setprecision(20) << (*this->
eigenvalues)[0] <<
" "
919 * << this->dof_handler.n_dofs() << std::endl;
921 * eigenvalues_out.close();
924 * data_out.build_patches();
925 * std::ofstream output(
"eigenvectors-" +
926 * std::to_string(this->refinement_cycle) +
".vtu");
927 * data_out.write_vtu(output);
932 * PrimalSolver<dim>::n_dofs() const
934 *
return EigenSolver<dim>::n_dofs();
939 * Note, that at least
for the demonstrated problem (i.e., a Hermitian problem
940 * and eigenvalue QoI), the dual problem is identical to the primal problem;
941 * however, it is convenient to separate them in
this manner (
e.g.,
for
942 * considering functionals of the eigenfunction).
946 *
class DualSolver :
public EigenSolver<dim>
949 * DualSolver(
const std::string & prm_file,
951 *
const unsigned int &min_degree,
952 *
const unsigned int &max_degree,
953 *
const unsigned int &starting_degree);
957 * DualSolver<dim>::DualSolver(
const std::string & prm_file,
959 *
const unsigned int &min_degree,
960 *
const unsigned int &max_degree,
961 *
const unsigned int &starting_degree)
963 * , EigenSolver<dim>(prm_file,
975 *
namespace ErrorIndicators
977 *
using namespace Maxwell;
984 *
template <
int dim,
bool report_dual>
985 *
class DualWeightedResidual :
public PrimalSolver<dim>,
public DualSolver<dim>
989 * output_eigenvalue_data(std::ofstream &os);
991 * output_qoi_error_estimates(std::ofstream &os);
998 * DualWeightedResidual(
const std::string & prm_file,
1000 *
const unsigned int &min_primal_degree,
1001 *
const unsigned int &max_primal_degree,
1002 *
const unsigned int &starting_primal_degree);
1004 *
virtual unsigned int
1005 * solve_problem()
override;
1008 * output_solution()
override;
1010 *
virtual unsigned int
1011 * n_dofs()
const override;
1020 * get_primal_DoFHandler();
1023 * get_dual_DoFHandler();
1026 * get_FECollection();
1029 * get_primal_FECollection();
1031 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1032 * get_eigenfunctions();
1034 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1035 * get_primal_eigenfunctions();
1037 * std::unique_ptr<std::vector<double>> &
1038 * get_primal_eigenvalues();
1040 * std::unique_ptr<std::vector<double>> &
1041 * get_dual_eigenvalues();
1044 * synchronize_discretization();
1049 *
return PrimalSolver<dim>::fe_collection->max_degree();
1051 *
double qoi_error_estimate = 0;
1072 * std::unique_ptr<hp::FEValues<dim>> cell_hp_fe_values;
1073 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values;
1074 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values_neighbor;
1075 * std::unique_ptr<hp::FESubfaceValues<dim>> subface_hp_fe_values;
1077 * std::unique_ptr<hp::FEValues<dim>> cell_hp_fe_values_forward;
1078 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values_forward;
1079 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values_neighbor_forward;
1080 * std::unique_ptr<hp::FESubfaceValues<dim>> subface_hp_fe_values_forward;
1081 *
using FaceIntegrals =
1082 *
typename std::map<typename DoFHandler<dim>::face_iterator,
double>;
1085 * solve_primal_problem();
1088 * solve_dual_problem();
1099 * initialize_error_estimation_data();
1102 * estimate_on_one_cell(
1106 *
const double & lambda_h,
1108 * FaceIntegrals & face_integrals);
1111 * integrate_over_cell(
1115 *
const double & lambda_h,
1119 * integrate_over_regular_face(
1121 *
const unsigned int & face_no,
1124 * FaceIntegrals & face_integrals);
1127 * integrate_over_irregular_face(
1129 *
const unsigned int & face_no,
1132 * FaceIntegrals & face_integrals);
1139 *
template <
int dim,
bool report_dual>
1140 * DualWeightedResidual<dim, report_dual>::DualWeightedResidual(
1141 *
const std::string & prm_file,
1143 *
const unsigned int &min_primal_degree,
1144 *
const unsigned int &max_primal_degree,
1145 *
const unsigned int &starting_primal_degree)
1147 * , PrimalSolver<dim>(prm_file,
1149 * min_primal_degree,
1150 * max_primal_degree,
1151 * starting_primal_degree)
1152 * , DualSolver<dim>(prm_file,
1154 * min_primal_degree + 1,
1155 * max_primal_degree + 1,
1156 * starting_primal_degree + 1)
1158 * initialize_error_estimation_data();
1165 *
template <
int dim,
bool report_dual>
1167 * DualWeightedResidual<dim, report_dual>::get_DoFHandler()
1170 *
return &(PrimalSolver<dim>::dof_handler);
1172 *
return &(DualSolver<dim>::dof_handler);
1177 * See above function, but to specifically output the primal
DoFHandler...
1180 *
template <
int dim,
bool report_dual>
1182 * DualWeightedResidual<dim, report_dual>::get_primal_DoFHandler()
1184 *
return &(PrimalSolver<dim>::dof_handler);
1189 * See above function, but
for the FECollection
1192 *
template <
int dim,
bool report_dual>
1194 * DualWeightedResidual<dim, report_dual>::get_FECollection()
1197 *
return &*(PrimalSolver<dim>::fe_collection);
1199 *
return &*(DualSolver<dim>::fe_collection);
1204 * See above function, but
for the primal FECollection
1207 *
template <
int dim,
bool report_dual>
1209 * DualWeightedResidual<dim, report_dual>::get_primal_FECollection()
1211 *
return &*(PrimalSolver<dim>::fe_collection);
1214 *
template <
int dim,
bool report_dual>
1216 * DualWeightedResidual<dim, report_dual>::get_dual_DoFHandler()
1218 *
return &(DualSolver<dim>::dof_handler);
1222 *
template <
int dim,
bool report_dual>
1223 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1224 * DualWeightedResidual<dim, report_dual>::get_eigenfunctions()
1227 *
return (PrimalSolver<dim>::eigenfunctions);
1229 *
return (DualSolver<dim>::eigenfunctions);
1233 *
template <
int dim,
bool report_dual>
1234 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1235 * DualWeightedResidual<dim, report_dual>::get_primal_eigenfunctions()
1237 *
return (PrimalSolver<dim>::eigenfunctions);
1241 *
template <
int dim,
bool report_dual>
1242 * std::unique_ptr<std::vector<double>> &
1243 * DualWeightedResidual<dim, report_dual>::get_primal_eigenvalues()
1245 *
return PrimalSolver<dim>::eigenvalues;
1249 *
template <
int dim,
bool report_dual>
1250 * std::unique_ptr<std::vector<double>> &
1251 * DualWeightedResidual<dim, report_dual>::get_dual_eigenvalues()
1253 *
return DualSolver<dim>::eigenvalues;
1256 *
template <
int dim,
bool report_dual>
1258 * DualWeightedResidual<dim, report_dual>::output_solution()
1260 * PrimalSolver<dim>::output_solution();
1265 * Solves the primal problem
1268 *
template <
int dim,
bool report_dual>
1270 * DualWeightedResidual<dim, report_dual>::solve_primal_problem()
1272 *
return PrimalSolver<dim>::solve_problem();
1277 * Solves the dual problem
1280 *
template <
int dim,
bool report_dual>
1282 * DualWeightedResidual<dim, report_dual>::solve_dual_problem()
1284 *
return DualSolver<dim>::solve_problem();
1291 *
template <
int dim,
bool report_dual>
1293 * DualWeightedResidual<dim, report_dual>::solve_problem()
1295 * DualWeightedResidual<dim, report_dual>::solve_primal_problem();
1296 *
return DualWeightedResidual<dim, report_dual>::solve_dual_problem();
1302 *
template <
int dim,
bool report_dual>
1304 * DualWeightedResidual<dim, report_dual>::n_dofs() const
1306 *
return PrimalSolver<dim>::n_dofs();
1315 *
template <
int dim,
bool report_dual>
1317 * DualWeightedResidual<dim, report_dual>::synchronize_discretization()
1331 * In
this case, we have modified the polynomial orders
for the dual;
1332 * need to update the primal
1335 * dof1 = &(DualSolver<dim>::dof_handler);
1336 * dof2 = &(PrimalSolver<dim>::dof_handler);
1339 * endc1 = dof1->end();
1341 *
for (; cell1 < endc1; ++cell1, ++cell2)
1343 * cell2->set_active_fe_index(cell1->active_fe_index());
1351 *
template <
int dim,
bool report_dual>
1353 * DualWeightedResidual<dim, report_dual>::initialize_error_estimation_data()
1357 * initialize the cell fe_values...
1360 * cell_hp_fe_values = std::make_unique<hp::FEValues<dim>>(
1361 * *DualSolver<dim>::fe_collection,
1362 * *DualSolver<dim>::quadrature_collection,
1365 * face_hp_fe_values = std::make_unique<hp::FEFaceValues<dim>>(
1366 * *DualSolver<dim>::fe_collection,
1367 * *DualSolver<dim>::face_quadrature_collection,
1370 * face_hp_fe_values_neighbor = std::make_unique<hp::FEFaceValues<dim>>(
1371 * *DualSolver<dim>::fe_collection,
1372 * *DualSolver<dim>::face_quadrature_collection,
1375 * subface_hp_fe_values = std::make_unique<hp::FESubfaceValues<dim>>(
1376 * *DualSolver<dim>::fe_collection,
1377 * *DualSolver<dim>::face_quadrature_collection,
1386 *
template <
int dim,
bool report_dual>
1388 * DualWeightedResidual<dim, report_dual>::normalize_solutions(
1392 *
double sum_primal = 0.0, sum_dual = 0.0;
1393 *
for (
const auto &cell :
1394 * DualSolver<dim>::dof_handler.active_cell_iterators())
1396 * cell_hp_fe_values->
reinit(cell);
1400 * grab the fe_values
object
1406 * std::vector<Vector<double>> cell_primal_values(
1408 * cell_dual_values(fe_values.n_quadrature_points,
Vector<double>(dim));
1409 * fe_values.get_function_values(primal_solution, cell_primal_values);
1410 * fe_values.get_function_values(dual_weights, cell_dual_values);
1413 *
for (
unsigned int p = 0; p < fe_values.n_quadrature_points; ++p)
1416 * cell_primal_values[p] * cell_primal_values[p] * fe_values.JxW(p);
1418 * cell_dual_values[p] * cell_dual_values[p] * fe_values.JxW(p);
1422 * primal_solution /=
sqrt(sum_primal);
1423 * dual_weights /=
sqrt(sum_dual);
1430 *
template <
int dim,
bool report_dual>
1432 * DualWeightedResidual<dim, report_dual>::estimate_error(
1437 * The constraints could be grabbed directly, but
this is simple
1442 * primal_hanging_node_constraints);
1443 * primal_hanging_node_constraints.close();
1447 * dual_hanging_node_constraints);
1448 * dual_hanging_node_constraints.close();
1452 * First map the primal solution to the space of the dual solution
1453 * This allows us to use just one set of
FEValues objects (rather than one
1454 * set
for the primal, one
for dual)
1460 *
Vector<double> primal_solution(DualSolver<dim>::dof_handler.n_dofs());
1462 * embed(PrimalSolver<dim>::dof_handler,
1463 * DualSolver<dim>::dof_handler,
1464 * dual_hanging_node_constraints,
1465 * *(PrimalSolver<dim>::get_solution()),
1468 *
Vector<double> &dual_solution = *(DualSolver<dim>::get_solution());
1470 * normalize_solutions(primal_solution, dual_solution);
1472 *
Vector<double> dual_weights(DualSolver<dim>::dof_handler.n_dofs()),
1473 * dual_weights_interm(PrimalSolver<dim>::dof_handler.n_dofs());
1477 * First
extract the dual solution to the space of the primal
1480 *
extract(DualSolver<dim>::dof_handler,
1481 * PrimalSolver<dim>::dof_handler,
1482 * primal_hanging_node_constraints,
1483 * *(DualSolver<dim>::get_solution()),
1484 * dual_weights_interm);
1488 * Now embed
this back to the space of the dual solution
1491 * embed(PrimalSolver<dim>::dof_handler,
1492 * DualSolver<dim>::dof_handler,
1493 * dual_hanging_node_constraints,
1494 * dual_weights_interm,
1500 * Subtract
this from the full dual solution
1503 * dual_weights -= *(DualSolver<dim>::get_solution());
1504 * dual_weights *= -1.0;
1506 * *(DualSolver<dim>::get_solution()) -= primal_solution;
1508 * FaceIntegrals face_integrals;
1509 *
for (
const auto &cell :
1510 * DualSolver<dim>::dof_handler.active_cell_iterators())
1511 * for (const auto &face : cell->face_iterators())
1512 * face_integrals[face] = -1e20;
1515 *
for (
const auto &cell :
1516 * DualSolver<dim>::dof_handler.active_cell_iterators())
1518 * estimate_on_one_cell(cell,
1521 * *(PrimalSolver<dim>::get_lambda_h()),
1525 *
unsigned int present_cell = 0;
1526 *
for (
const auto &cell :
1527 * DualSolver<dim>::dof_handler.active_cell_iterators())
1529 * for (const auto &face : cell->face_iterators())
1531 *
Assert(face_integrals.find(face) != face_integrals.
end(),
1532 * ExcInternalError());
1533 * error_indicators(present_cell) -= 0.5 * face_integrals[face];
1540 * Now, with the error indicators computed, let us produce the
1541 * estimate of the QoI error
1544 * this->qoi_error_estimate =
1545 * this->get_global_QoI_error(*(DualSolver<dim>::get_solution()),
1546 * error_indicators);
1547 * std::cout <<
"Estimated QoI error: " << std::setprecision(20)
1548 * << qoi_error_estimate << std::endl;
1555 *
template <
int dim,
bool report_dual>
1557 * DualWeightedResidual<dim, report_dual>::estimate_on_one_cell(
1561 *
const double & lambda_h,
1563 * FaceIntegrals & face_integrals)
1565 * integrate_over_cell(
1566 * cell, primal_solution, dual_weights, lambda_h, error_indicators);
1567 *
for (
unsigned int face_no :
GeometryInfo<dim>::face_indices())
1569 * if (cell->face(face_no)->at_boundary())
1571 * face_integrals[cell->face(face_no)] = 0.0;
1574 *
if ((cell->neighbor(face_no)->has_children() ==
false) &&
1575 * (cell->neighbor(face_no)->level() == cell->level()) &&
1576 * (cell->neighbor(face_no)->index() < cell->index()))
1578 *
if (cell->at_boundary(face_no) ==
false)
1579 *
if (cell->neighbor(face_no)->level() < cell->level())
1581 *
if (cell->face(face_no)->has_children() ==
false)
1582 * integrate_over_regular_face(
1583 * cell, face_no, primal_solution, dual_weights, face_integrals);
1585 * integrate_over_irregular_face(
1586 * cell, face_no, primal_solution, dual_weights, face_integrals);
1593 *
template <
int dim,
bool report_dual>
1595 * DualWeightedResidual<dim, report_dual>::integrate_over_cell(
1599 *
const double & lambda_h,
1602 * cell_hp_fe_values->reinit(cell);
1605 * Grab the fe_values
object
1609 * std::vector<std::vector<Tensor<2, dim, double>>> cell_hessians(
1611 * std::vector<Vector<double>> cell_primal_values(
1613 * cell_dual_values(fe_values.n_quadrature_points,
Vector<double>(dim));
1614 * fe_values.get_function_values(primal_solution, cell_primal_values);
1615 * fe_values.get_function_hessians(primal_solution, cell_hessians);
1616 * fe_values.get_function_values(dual_weights, cell_dual_values);
1621 *
for (
unsigned int p = 0; p < fe_values.n_quadrature_points; ++p)
1624 * ( (cell_hessians[p][1][1][0] -
1625 * cell_hessians[p][0][1][1]) *
1626 * (cell_dual_values[p](0)) +
1628 * (cell_hessians[p][0][0][1] - cell_hessians[p][1][0][0]) *
1629 * (cell_dual_values[p](1)) -
1630 * lambda_h * (cell_primal_values[p](0) * cell_dual_values[p](0) +
1631 * cell_primal_values[p](1) * cell_dual_values[p](1))) *
1635 * error_indicators(cell->active_cell_index()) +=
sum;
1641 *
template <
int dim,
bool report_dual>
1643 * DualWeightedResidual<dim, report_dual>::integrate_over_regular_face(
1645 *
const unsigned int & face_no,
1648 * FaceIntegrals & face_integrals)
1651 * ExcInternalError());
1652 *
const unsigned int neighbor_neighbor = cell->neighbor_of_neighbor(face_no);
1653 *
const auto neighbor = cell->neighbor(face_no);
1655 *
const unsigned int quadrature_index =
1656 *
std::max(cell->active_fe_index(), neighbor->active_fe_index());
1657 * face_hp_fe_values->reinit(cell, face_no, quadrature_index);
1660 * std::vector<std::vector<Tensor<1, dim, double>>> cell_primal_grads(
1661 * fe_face_values_cell.n_quadrature_points,
1663 * neighbor_primal_grads(fe_face_values_cell.n_quadrature_points,
1665 * fe_face_values_cell.get_function_gradients(primal_solution,
1666 * cell_primal_grads);
1668 * face_hp_fe_values_neighbor->reinit(neighbor,
1669 * neighbor_neighbor,
1670 * quadrature_index);
1674 * neighbor_primal_grads);
1675 *
const unsigned int n_q_points = fe_face_values_cell.n_quadrature_points;
1676 *
double face_integral = 0.0;
1677 * std::vector<Vector<double>> cell_dual_values(n_q_points,
1679 * fe_face_values_cell.get_function_values(dual_weights, cell_dual_values);
1680 *
for (
unsigned int p = 0; p < n_q_points; ++p)
1682 *
auto face_normal = fe_face_values_cell.normal_vector(p);
1685 * (cell_primal_grads[p][1][0] - cell_primal_grads[p][0][1] -
1686 * neighbor_primal_grads[p][1][0] + neighbor_primal_grads[p][0][1]) *
1687 * (cell_dual_values[p][0] * face_normal[1] -
1688 * cell_dual_values[p][1] * face_normal[0]) *
1689 * fe_face_values_cell.JxW(p);
1691 *
Assert(face_integrals.find(cell->face(face_no)) != face_integrals.end(),
1692 * ExcInternalError());
1693 *
Assert(face_integrals[cell->face(face_no)] == -1e20, ExcInternalError());
1694 * face_integrals[cell->face(face_no)] = face_integral;
1700 *
template <
int dim,
bool report_dual>
1702 * DualWeightedResidual<dim, report_dual>::integrate_over_irregular_face(
1704 *
const unsigned int & face_no,
1707 * FaceIntegrals & face_integrals)
1711 * cell->neighbor(face_no);
1714 *
Assert(neighbor->has_children(), ExcInternalError());
1716 *
const unsigned int neighbor_neighbor = cell->neighbor_of_neighbor(face_no);
1717 *
for (
unsigned int subface_no = 0; subface_no < face->n_children();
1721 * cell->neighbor_child_on_subface(face_no, subface_no);
1722 *
Assert(neighbor_child->face(neighbor_neighbor) ==
1723 * cell->face(face_no)->child(subface_no),
1724 * ExcInternalError());
1725 *
const unsigned int quadrature_index =
1726 *
std::max(cell->active_fe_index(), neighbor_child->active_fe_index());
1729 * initialize fe_subface values_cell
1732 * subface_hp_fe_values->reinit(cell,
1735 * quadrature_index);
1738 * std::vector<std::vector<Tensor<1, dim, double>>> cell_primal_grads(
1739 * subface_fe_values_cell.n_quadrature_points,
1741 * neighbor_primal_grads(subface_fe_values_cell.n_quadrature_points,
1743 * subface_fe_values_cell.get_function_gradients(primal_solution,
1744 * cell_primal_grads);
1747 * initialize fe_face_values_neighbor
1750 * face_hp_fe_values_neighbor->reinit(neighbor_child,
1751 * neighbor_neighbor,
1752 * quadrature_index);
1756 * neighbor_primal_grads);
1757 *
const unsigned int n_q_points =
1758 * subface_fe_values_cell.n_quadrature_points;
1759 * std::vector<Vector<double>> cell_dual_values(n_q_points,
1761 * face_fe_values_neighbor.get_function_values(dual_weights,
1762 * cell_dual_values);
1764 *
double face_integral = 0.0;
1766 *
for (
unsigned int p = 0; p < n_q_points; ++p)
1768 *
auto face_normal = face_fe_values_neighbor.normal_vector(p);
1770 * (cell_primal_grads[p][0][1] - cell_primal_grads[p][1][0] +
1771 * neighbor_primal_grads[p][1][0] -
1772 * neighbor_primal_grads[p][0][1]) *
1773 * (cell_dual_values[p][0] * face_normal[1] -
1774 * cell_dual_values[p][1] * face_normal[0]) *
1775 * face_fe_values_neighbor.JxW(p);
1777 * face_integrals[neighbor_child->face(neighbor_neighbor)] = face_integral;
1780 *
for (
unsigned int subface_no = 0; subface_no < face->n_children();
1783 *
Assert(face_integrals.find(face->child(subface_no)) !=
1784 * face_integrals.end(),
1785 * ExcInternalError());
1786 *
Assert(face_integrals[face->child(subface_no)] != -1e20,
1787 * ExcInternalError());
1788 *
sum += face_integrals[face->child(subface_no)];
1790 * face_integrals[face] =
sum;
1793 *
template <
int dim,
bool report_dual>
1795 * DualWeightedResidual<dim, report_dual>::get_global_QoI_error(
1799 *
auto dual_less_primal =
1803 *
double scaling_factor = 0.0;
1804 *
for (
const auto &cell :
1805 * DualSolver<dim>::dof_handler.active_cell_iterators())
1807 * cell_hp_fe_values->
reinit(cell);
1810 * grab the fe_values
object
1816 * std::vector<Vector<double>> cell_values(fe_values.n_quadrature_points,
1818 * fe_values.get_function_values(dual_less_primal, cell_values);
1820 *
for (
unsigned int p = 0; p < fe_values.n_quadrature_points; ++p)
1823 * (cell_values[p] * cell_values[p]) * fe_values.JxW(p);
1826 *
double global_QoI_error = 0.0;
1827 *
for (
const auto &indicator : error_indicators)
1829 * global_QoI_error += indicator;
1832 * global_QoI_error /= (1 - 0.5 * scaling_factor);
1833 *
return global_QoI_error;
1837 *
template <
int dim,
bool report_dual>
1839 * DualWeightedResidual<dim, report_dual>::embed(
1846 * assert(u2.size() == dof2.n_dofs() &&
"Incorrect input vector size!");
1851 * endc1 = dof1.end();
1854 *
for (; cell1 < endc1; ++cell1, ++cell2)
1861 * assert(fe1.degree < fe2.degree &&
"Incorrect usage of embed!");
1865 * Get the embedding_dofs
1874 * std::vector<unsigned int> embedding_dofs =
1875 * fe2.get_embedding_dofs(fe1.degree);
1876 *
const unsigned int dofs_per_cell2 = fe2.n_dofs_per_cell();
1882 * local_dof_values_1.reinit(fe1.dofs_per_cell);
1883 * cell1->get_dof_values(solution, local_dof_values_1);
1885 *
for (
unsigned int i = 0; i < local_dof_values_1.size(); ++i)
1886 * local_dof_values_2[embedding_dofs[i]] = local_dof_values_1[i];
1890 * Now set
this changes to the global vector
1893 * cell2->set_dof_values(local_dof_values_2, u2);
1899 * Applies the constraints of the target finite element space
1902 * constraints.distribute(u2);
1905 *
template <
int dim,
bool report_dual>
1907 * DualWeightedResidual<dim, report_dual>::extract(
1916 * Maps from fe1 to fe2
1919 * assert(u2.size() == dof2.n_dofs() &&
"Incorrect input vector size!");
1924 * endc1 = dof1.end();
1927 *
for (; cell1 < endc1; ++cell1, ++cell2)
1934 * assert(fe1.degree > fe2.degree &&
"Incorrect usage of extract!");
1938 * Get the embedding_dofs
1941 * std::vector<unsigned int> embedding_dofs =
1942 * fe1.get_embedding_dofs(fe2.degree);
1943 *
const unsigned int dofs_per_cell2 = fe2.n_dofs_per_cell();
1949 * local_dof_values_1.reinit(fe1.dofs_per_cell);
1950 * cell1->get_dof_values(solution, local_dof_values_1);
1952 *
for (
unsigned int i = 0; i < local_dof_values_2.size(); ++i)
1953 * local_dof_values_2[i] = local_dof_values_1[embedding_dofs[i]];
1957 * Now set
this changes to the global vector
1960 * cell2->set_dof_values(local_dof_values_2, u2);
1966 * Applies the constraints of the target finite element space
1969 * constraints.distribute(u2);
1971 *
template <
int dim,
bool report_dual>
1973 * DualWeightedResidual<dim, report_dual>::output_eigenvalue_data(
1974 * std::ofstream &os)
1976 * os << (*this->get_primal_eigenvalues())[0] <<
" "
1977 * << (this->get_primal_DoFHandler())->n_dofs() <<
" "
1978 * << (*this->get_dual_eigenvalues())[0] <<
" "
1979 * << (this->get_dual_DoFHandler())->n_dofs() << std::endl;
1981 *
template <
int dim,
bool report_dual>
1983 * DualWeightedResidual<dim, report_dual>::output_qoi_error_estimates(
1984 * std::ofstream &os)
1986 * os << qoi_error_estimate << std::endl;
1993 *
template <
int dim>
1994 *
class KellyErrorIndicator :
public PrimalSolver<dim>
2003 * output_eigenvalue_data(std::ofstream &os);
2005 * output_qoi_error_estimates(std::ofstream &);
2006 * KellyErrorIndicator(
const std::string & prm_file,
2008 *
const unsigned int &min_degree,
2009 *
const unsigned int &max_degree,
2010 *
const unsigned int &starting_degree);
2012 *
virtual unsigned int
2013 * solve_problem()
override;
2016 * output_solution()
override;
2019 * get_FECollection();
2022 * get_primal_FECollection();
2024 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2025 * get_eigenfunctions();
2027 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2028 * get_primal_eigenfunctions();
2030 * std::unique_ptr<std::vector<double>> &
2031 * get_primal_eigenvalues();
2035 * synchronize_discretization();
2041 * get_primal_DoFHandler();
2046 *
return PrimalSolver<dim>::fe_collection->max_degree();
2048 *
double qoi_error_estimate = 0;
2056 * prune_eigenpairs(
const double &TOL);
2059 * std::vector<const ReadVector<PetscScalar> *> eigenfunction_ptrs;
2061 * std::vector<const PETScWrappers::MPI::Vector *> eigenfunction_ptrs;
2063 * std::vector<const double *> eigenvalue_ptrs;
2065 * std::vector<std::shared_ptr<Vector<float>>> errors;
2068 *
template <
int dim>
2069 * KellyErrorIndicator<dim>::KellyErrorIndicator(
2070 *
const std::string & prm_file,
2072 *
const unsigned int &min_degree,
2073 *
const unsigned int &max_degree,
2074 *
const unsigned int &starting_degree)
2075 * : Base<dim>(prm_file, coarse_grid)
2076 * , PrimalSolver<dim>(prm_file,
2083 *
template <
int dim>
2085 * KellyErrorIndicator<dim>::solve_problem()
2087 *
return PrimalSolver<dim>::solve_problem();
2090 *
template <
int dim>
2092 * KellyErrorIndicator<dim>::get_FECollection()
2094 *
return &*(PrimalSolver<dim>::fe_collection);
2097 *
template <
int dim>
2099 * KellyErrorIndicator<dim>::get_primal_FECollection()
2101 *
return &*(PrimalSolver<dim>::fe_collection);
2104 *
template <
int dim>
2105 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2106 * KellyErrorIndicator<dim>::get_eigenfunctions()
2108 *
return (PrimalSolver<dim>::eigenfunctions);
2111 *
template <
int dim>
2112 * std::unique_ptr<std::vector<double>> &
2113 * KellyErrorIndicator<dim>::get_primal_eigenvalues()
2115 *
return PrimalSolver<dim>::eigenvalues;
2118 *
template <
int dim>
2119 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2120 * KellyErrorIndicator<dim>::get_primal_eigenfunctions()
2122 *
return (PrimalSolver<dim>::eigenfunctions);
2125 *
template <
int dim>
2127 * KellyErrorIndicator<dim>::get_DoFHandler()
2129 *
return &(PrimalSolver<dim>::dof_handler);
2132 *
template <
int dim>
2134 * KellyErrorIndicator<dim>::get_primal_DoFHandler()
2136 *
return &(PrimalSolver<dim>::dof_handler);
2139 *
template <
int dim>
2141 * KellyErrorIndicator<dim>::synchronize_discretization()
2145 * This function does
nothing for this error indicator
2151 *
template <
int dim>
2153 * KellyErrorIndicator<dim>::output_solution()
2155 * PrimalSolver<dim>::output_solution();
2158 *
template <
int dim>
2160 * KellyErrorIndicator<dim>::prune_eigenpairs(
const double &TOL)
2162 *
unsigned int count = 0;
2163 *
for (
size_t eigenpair_index = 0;
2164 * eigenpair_index < this->eigenfunctions->size();
2165 * ++eigenpair_index)
2167 *
if (count >= this->n_eigenpairs)
2169 *
if (
abs((*this->eigenvalues)[eigenpair_index]) < TOL)
2172 * eigenfunction_ptrs.push_back(&(*this->eigenfunctions)[eigenpair_index]);
2173 * eigenvalue_ptrs.push_back(&(*this->eigenvalues)[eigenpair_index]);
2177 *
template <
int dim>
2179 * KellyErrorIndicator<dim>::estimate_error(
Vector<double> &error_indicators)
2181 * std::cout <<
"Marking cells via Kelly indicator..." << std::endl;
2182 * prune_eigenpairs(1e-9);
2185 * deallocate the errors vector
2189 *
for (
size_t i = 0; i < eigenfunction_ptrs.size(); ++i)
2191 * errors.emplace_back(
2194 * std::vector<Vector<float> *> estimated_error_per_cell(
2195 * eigenfunction_ptrs.size());
2196 *
for (
size_t i = 0; i < eigenfunction_ptrs.size(); ++i)
2198 * estimated_error_per_cell[i] = errors[i].get();
2205 * *this->face_quadrature_collection,
2211 * *this->face_quadrature_collection,
2213 * eigenfunction_ptrs,
2214 * estimated_error_per_cell);
2217 *
for (
auto &error_vec : errors)
2219 * auto normalized_vec = *error_vec;
2220 * normalized_vec /= normalized_vec.l1_norm();
2222 *
for (
unsigned int i = 0; i < error_indicators.size(); ++i)
2223 * error_indicators(i) += double(normalized_vec(i));
2225 * std::cout <<
"...Done!" << std::endl;
2227 *
template <
int dim>
2229 * KellyErrorIndicator<dim>::output_eigenvalue_data(std::ofstream &os)
2231 * os << (*this->get_primal_eigenvalues())[0] <<
" "
2232 * << (this->get_primal_DoFHandler())->n_dofs() << std::endl;
2234 *
template <
int dim>
2236 * KellyErrorIndicator<dim>::output_qoi_error_estimates(std::ofstream &)
2246 *
namespace RegularityIndicators
2248 *
using namespace dealii;
2252 *
template <
int dim>
2253 *
class LegendreInfo
2257 *
class LegendreInfo<2>
2260 * std::unique_ptr<FESeries::Legendre<2>> legendre_u, legendre_v;
2268 * assert(fe_collection !=
nullptr && dof_handler !=
nullptr &&
2269 *
"A valid FECollection and DoFHandler must be accessible!");
2271 * legendre_u = std::make_unique<FESeries::Legendre<2>>(
2273 * legendre_v = std::make_unique<FESeries::Legendre<2>>(
2276 * legendre_u->precalculate_all_transformation_matrices();
2277 * legendre_v->precalculate_all_transformation_matrices();
2280 *
template <
class VectorType>
2282 * compute_coefficient_decay(
const VectorType & eigenfunction,
2283 * std::vector<double> &smoothness_indicators)
2287 * Compute the coefficients
for the u and v components of the solution
2292 * smoothness_v(smoothness_indicators.size());
2304 *
for (
unsigned int i = 0; i < smoothness_indicators.size(); ++i)
2306 * smoothness_indicators[i] =
std::min(smoothness_u[i], smoothness_v[i]);
2314 *
template <
int dim>
2315 *
class LegendreIndicator
2323 *
template <
class VectorType>
2325 * estimate_smoothness(
2326 *
const std::unique_ptr<std::vector<VectorType>> &eigenfunctions,
2327 *
const unsigned int & index_of_goal,
2328 * std::vector<double> & smoothness_indicators);
2334 *
template <
int dim>
2336 * LegendreIndicator<dim>::attach_FE_info_and_initialize(
2345 *
template <
int dim>
2346 *
template <
class VectorType>
2348 * LegendreIndicator<dim>::estimate_smoothness(
2349 *
const std::unique_ptr<std::vector<VectorType>> &eigenfunctions,
2350 *
const unsigned int & index_of_goal,
2351 * std::vector<double> & smoothness_indicators)
2353 * this->
legendre.compute_coefficient_decay((*eigenfunctions)[index_of_goal],
2354 * smoothness_indicators);
2362 *
namespace Refinement
2364 *
using namespace dealii;
2365 *
using namespace Maxwell;
2367 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2368 *
class Refiner :
public ErrorIndicator,
public RegularityIndicator
2371 * Refiner(
const std::string & prm_file,
2373 *
const unsigned int &min_degree,
2374 *
const unsigned int &max_degree,
2375 *
const unsigned int &starting_degree);
2378 * execute_refinement(
const double &smoothness_threshold_fraction);
2381 * output_solution()
override;
2385 * std::vector<double> smoothness_indicators;
2386 * std::ofstream eigenvalues_out;
2387 * std::ofstream error_estimate_out;
2390 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2391 * Refiner<dim, ErrorIndicator, RegularityIndicator>::Refiner(
2392 *
const std::string & prm_file,
2394 *
const unsigned int &min_degree,
2395 *
const unsigned int &max_degree,
2396 *
const unsigned int &starting_degree)
2397 * : Base<dim>(prm_file, coarse_grid)
2398 * , ErrorIndicator(prm_file,
2403 * , RegularityIndicator()
2405 *
if (ErrorIndicator::name() ==
"DWR")
2407 * error_estimate_out.open(
"error_estimate.txt");
2408 * error_estimate_out << std::setprecision(20);
2411 * eigenvalues_out.open(
"eigenvalues_" + ErrorIndicator::name() +
"_out.txt");
2412 * eigenvalues_out << std::setprecision(20);
2417 * For generating samples of the curl of the electric field
2420 *
template <
int dim>
2424 * CurlPostprocessor()
2431 * std::vector<
Vector<double>> &computed_quantities)
const override
2434 * computed_quantities.size());
2435 *
for (
unsigned int p = 0; p < input_data.solution_gradients.size(); ++p)
2437 * computed_quantities[p](0) = input_data.solution_gradients[p][1][0] -
2438 * input_data.solution_gradients[p][0][1];
2450 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2452 * Refiner<dim, ErrorIndicator, RegularityIndicator>::output_solution()
2454 * CurlPostprocessor<dim> curl_u;
2457 *
auto & output_dof = *(ErrorIndicator::get_primal_DoFHandler());
2460 *
for (
const auto &cell : output_dof.active_cell_iterators())
2461 * fe_degrees(cell->active_cell_index()) =
2462 * (*ErrorIndicator::get_primal_FECollection())[cell->active_fe_index()]
2464 * data_out.add_data_vector(fe_degrees,
"fe_degree");
2466 * data_out.add_data_vector(estimated_error_per_cell,
"error");
2468 *
for (
const auto &cell : output_dof.active_cell_iterators())
2470 * auto i = cell->active_cell_index();
2471 *
if (!cell->refine_flag_set() && !cell->coarsen_flag_set())
2472 * smoothness_out(i) = -1;
2474 * smoothness_out(i) = smoothness_indicators[i];
2476 * data_out.add_data_vector(smoothness_out,
"smoothness");
2477 * data_out.add_data_vector((*ErrorIndicator::get_primal_eigenfunctions())[0],
2478 * std::string(
"eigenfunction_no_") +
2480 * data_out.add_data_vector((*ErrorIndicator::get_primal_eigenfunctions())[0],
2483 * ErrorIndicator::output_eigenvalue_data(eigenvalues_out);
2484 * ErrorIndicator::output_qoi_error_estimates(error_estimate_out);
2486 * std::cout <<
"Number of DoFs: " << (this->get_primal_DoFHandler())->n_dofs()
2490 * data_out.build_patches();
2491 * std::ofstream output(
"eigenvectors-" + ErrorIndicator::name() +
"-" +
2492 * std::to_string(this->refinement_cycle) + +
".vtu");
2493 * data_out.write_vtu(output);
2502 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2504 * Refiner<dim, ErrorIndicator, RegularityIndicator>::execute_refinement(
2505 *
const double &smoothness_threshold_fraction)
2509 * First initialize the RegularityIndicator...
2510 * Depending on the limits set,
this may take a
while
2513 * std::cout <<
"Initializing RegularityIndicator..." << std::endl;
2515 * <<
"(This may take a while if the max expansion order is set too high)"
2517 * RegularityIndicator::attach_FE_info_and_initialize(
2518 * ErrorIndicator::get_FECollection(), ErrorIndicator::get_DoFHandler());
2519 * std::cout <<
"Done!" << std::endl <<
"Starting Refinement..." << std::endl;
2521 *
for (
unsigned int cycle = 0; cycle <= this->max_cycles; ++cycle)
2523 * this->set_refinement_cycle(cycle);
2524 * std::cout <<
"Cycle: " << cycle << std::endl;
2525 * ErrorIndicator::solve_problem();
2526 * this->estimated_error_per_cell.reinit(
2529 * ErrorIndicator::estimate_error(estimated_error_per_cell);
2533 * Depending on the source of the error estimation/indication, these
2534 *
values might be signed, so we address that with the following
2537 *
for (
double &error_indicator : estimated_error_per_cell)
2538 * error_indicator =
std::
abs(error_indicator);
2542 * *this->
triangulation, estimated_error_per_cell, 1. / 5., 0.000);
2546 * Now get regularity indicators
2547 * For those elements which must be refined,
swap to increasing @f$p@f$
2548 * depending on the regularity threshold...
2554 * smoothness_indicators =
2555 * std::vector<double>(this->
triangulation->n_active_cells(),
2556 * std::numeric_limits<double>::max());
2557 *
if (ErrorIndicator::PrimalSolver::min_degree !=
2558 * ErrorIndicator::PrimalSolver::max_degree)
2559 * RegularityIndicator::estimate_smoothness(
2560 * ErrorIndicator::get_eigenfunctions(), 0, smoothness_indicators);
2566 * this->output_solution();
2567 *
const double threshold_smoothness = smoothness_threshold_fraction;
2568 *
unsigned int num_refined = 0, num_coarsened = 0;
2569 *
if (ErrorIndicator::PrimalSolver::min_degree !=
2570 * ErrorIndicator::PrimalSolver::max_degree)
2572 *
for (
const auto &cell :
2573 * ErrorIndicator::get_DoFHandler()->active_cell_iterators())
2575 * if (cell->refine_flag_set())
2577 *
if (cell->coarsen_flag_set())
2579 *
if (cell->refine_flag_set() &&
2580 * smoothness_indicators[cell->active_cell_index()] >
2581 * threshold_smoothness &&
2582 *
static_cast<unsigned int>(cell->active_fe_index() + 1) <
2583 * ErrorIndicator::get_FECollection()->size())
2585 * cell->clear_refine_flag();
2586 * cell->set_active_fe_index(cell->active_fe_index() + 1);
2588 *
else if (cell->coarsen_flag_set() &&
2589 * smoothness_indicators[cell->active_cell_index()] <
2590 * threshold_smoothness &&
2591 * cell->active_fe_index() != 0)
2593 * cell->clear_coarsen_flag();
2595 * cell->set_active_fe_index(cell->active_fe_index() - 1);
2599 * Here we also impose a limit on how small the cells can become
2602 *
else if (cell->refine_flag_set() && cell->diameter() < 5.0e-6)
2604 * cell->clear_refine_flag();
2605 *
if (
static_cast<unsigned int>(cell->active_fe_index() + 1) <
2606 * ErrorIndicator::get_FECollection()->size())
2607 * cell->set_active_fe_index(cell->active_fe_index() + 1);
2614 * Check what the smallest
diameter is
2617 *
double min_diameter = std::numeric_limits<double>::max();
2618 *
for (
const auto &cell :
2619 * ErrorIndicator::get_DoFHandler()->active_cell_iterators())
2620 * if (cell->
diameter() < min_diameter)
2623 * std::cout <<
"Min diameter: " << min_diameter << std::endl;
2625 * ErrorIndicator::synchronize_discretization();
2627 * (this->
triangulation)->execute_coarsening_and_refinement();
2633 * main(
int argc,
char **argv)
2637 *
using namespace dealii;
2638 *
using namespace Maxwell;
2639 *
using namespace Refinement;
2640 *
using namespace ErrorIndicators;
2641 *
using namespace RegularityIndicators;
2649 * ExcMessage(
"This program can only be run in serial, use ./maxwell-hp"));
2652 * Structures::create_L_waveguide(triangulation_DWR, 2.0);
2653 * Structures::create_L_waveguide(triangulation_Kelly, 2.0);
2655 * Refiner<2, KellyErrorIndicator<2>, LegendreIndicator<2>> problem_Kelly(
2657 * triangulation_Kelly,
2662 * Refiner<2, DualWeightedResidual<2, false>, LegendreIndicator<2>>
2663 * problem_DWR(
"maxwell-hp.prm",
2664 * triangulation_DWR,
2671 * The threshold
for the
hp-decision: too small -> not enough
2672 * @f$h@f$-refinement, too large -> not enough @f$p@f$-refinement
2675 *
double smoothness_threshold = 0.75;
2677 * std::cout <<
"Executing refinement for the Kelly strategy!" << std::endl;
2678 * problem_Kelly.execute_refinement(smoothness_threshold);
2679 * std::cout <<
"...Done with Kelly refinement strategy!" << std::endl;
2680 * std::cout <<
"Executing refinement for the DWR strategy!" << std::endl;
2681 * problem_DWR.execute_refinement(smoothness_threshold);
2682 * std::cout <<
"...Done with DWR refinement strategy!" << std::endl;
2685 *
catch (std::exception &exc)
2687 * std::cerr << std::endl
2689 * <<
"----------------------------------------------------"
2691 * std::cerr <<
"Exception on processing: " << std::endl
2692 * << exc.what() << std::endl
2693 * <<
"Aborting!" << std::endl
2694 * <<
"----------------------------------------------------"
2701 * std::cerr << std::endl
2703 * <<
"----------------------------------------------------"
2705 * std::cerr <<
"Unknown exception!" << std::endl
2706 * <<
"Aborting!" << std::endl
2707 * <<
"----------------------------------------------------"
2712 * std::cout << std::endl <<
" Job done." << std::endl;
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
void attach_dof_handler(const DoFHandler< dim, spacedim > &)
virtual void evaluate_vector_field(const DataPostprocessorInputs::Vector< dim > &input_data, std::vector< Vector< double > > &computed_quantities) const
active_cell_iterator begin_active(const unsigned int level=0) const
const FEFaceValues< dim, spacedim > & get_present_fe_values() const
const FESubfaceValues< dim, spacedim > & get_present_fe_values() const
void get_function_gradients(const ReadVector< Number > &fe_function, std::vector< Tensor< 1, spacedim, Number > > &gradients) const
const FEValues< dim, spacedim > & get_present_fe_values() const
static void estimate(const Mapping< dim, spacedim > &mapping, const DoFHandler< dim, spacedim > &dof, const Quadrature< dim - 1 > &quadrature, const std::map< types::boundary_id, const Function< spacedim, Number > * > &neumann_bc, const ReadVector< Number > &solution, Vector< float > &error, const ComponentMask &component_mask={}, const Function< spacedim > *coefficients=nullptr, const unsigned int n_threads=numbers::invalid_unsigned_int, const types::subdomain_id subdomain_id=numbers::invalid_subdomain_id, const types::material_id material_id=numbers::invalid_material_id, const Strategy strategy=cell_diameter_over_24)
virtual void reinit(const size_type N, const bool omit_zeroing_entries=false)
#define DEAL_II_VERSION_GTE(major, minor, subminor)
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertThrow(cond, exc)
typename ActiveSelector::cell_iterator cell_iterator
typename ActiveSelector::face_iterator face_iterator
typename ActiveSelector::active_cell_iterator active_cell_iterator
void make_hanging_node_constraints(const DoFHandler< dim, spacedim > &dof_handler, AffineConstraints< number > &constraints)
void make_zero_boundary_constraints(const DoFHandler< dim, spacedim > &dof, const types::boundary_id boundary_id, AffineConstraints< number > &zero_boundary_constraints, const ComponentMask &component_mask={})
@ update_hessians
Second derivatives of shape functions.
@ update_values
Shape function values.
@ update_normal_vectors
Normal vectors.
@ update_JxW_values
Transformed quadrature weights.
@ update_gradients
Shape function gradients.
@ update_quadrature_points
Transformed quadrature points.
std::vector< index_type > data
void refine_and_coarsen_fixed_number(Triangulation< dim, spacedim > &triangulation, const Vector< Number > &criteria, const double top_fraction_of_cells, const double bottom_fraction_of_cells, const unsigned int max_n_cells=std::numeric_limits< unsigned int >::max())
@ valid
Iterator points to a valid object.
void mass_matrix(FullMatrix< double > &M, const FEValuesBase< dim > &fe, const double factor=1.)
SymmetricTensor< 2, dim, Number > e(const Tensor< 2, dim, Number > &F)
void apply(const Kokkos::TeamPolicy< MemorySpace::Default::kokkos_space::execution_space >::member_type &team_member, const Kokkos::View< Number *, MemorySpace::Default::kokkos_space > shape_data, const ViewTypeIn in, ViewTypeOut out)
FESeries::Legendre< dim, spacedim > default_fe_series(const hp::FECollection< dim, spacedim > &fe_collection, const unsigned int component=numbers::invalid_unsigned_int)
void coefficient_decay(FESeries::Legendre< dim, spacedim > &fe_legendre, const DoFHandler< dim, spacedim > &dof_handler, const VectorType &solution, Vector< float > &smoothness_indicators, const VectorTools::NormType regression_strategy=VectorTools::Linfty_norm, const double smallest_abs_coefficient=1e-10, const bool only_flagged_cells=false)
constexpr ReturnType< rank, T >::value_type & extract(T &t, const ArrayType &indices)
VectorType::value_type * end(VectorType &V)
T sum(const T &t, const MPI_Comm mpi_communicator)
unsigned int n_mpi_processes(const MPI_Comm mpi_communicator)
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
unsigned int n_cells(const internal::TriangulationImplementation::NumberCache< 1 > &c)
int(&) functions(const void *v1, const void *v2)
void reinit(MatrixBlock< MatrixType > &v, const BlockSparsityPattern &p)
double legendre(unsigned int l, double x)
::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 > sqrt(const ::VectorizedArray< Number, width > &)
::VectorizedArray< Number, width > abs(const ::VectorizedArray< Number, width > &)
void swap(ObserverPointer< T, P > &t1, ObserverPointer< T, Q > &t2)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)