235 * #include <deal.II/base/function_parser.h>
236 * #include <deal.II/base/index_set.h>
237 * #include <deal.II/base/parameter_handler.h>
238 * #include <deal.II/base/quadrature_lib.h>
239 * #include <deal.II/base/utilities.h>
241 * #include <deal.II/dofs/dof_handler.h>
242 * #include <deal.II/dofs/dof_tools.h>
244 * #include <deal.II/fe/fe_nedelec.h>
245 * #include <deal.II/fe/fe_series.h>
246 * #include <deal.II/fe/fe_values.h>
248 * #include <deal.II/grid/
tria.h>
249 * #include <deal.II/grid/tria_iterator.h>
251 * #include <deal.II/lac/affine_constraints.h>
252 * #include <deal.II/lac/full_matrix.h>
253 * #include <deal.II/lac/petsc_precondition.h>
254 * #include <deal.II/lac/petsc_sparse_matrix.h>
255 * #include <deal.II/lac/petsc_vector.h>
256 * #include <deal.II/lac/slepc_solver.h>
258 * #include <deal.II/numerics/data_out.h>
259 * #include <deal.II/numerics/vector_tools.h>
263 * For parallelization (
using WorkStream and Intel TBB)
266 * #include <deal.II/base/multithread_info.h>
267 * #include <deal.II/base/work_stream.h>
269 * #include
"petscpc.h"
273 * For Error Estimation/Indication and Smoothness Indication
276 * #include <deal.II/fe/fe_tools.h>
278 * #include <deal.II/numerics/error_estimator.h>
279 * #include <deal.II/numerics/smoothness_estimator.h>
285 * #include <deal.II/grid/grid_refinement.h>
288 * #include <iostream>
291 *
namespace Operations
298 * curlcurl(const ::FEValues<2> &fe_values,
299 *
const unsigned int & i,
300 *
const unsigned int & j,
301 *
const unsigned int & q_point)
303 *
auto gradu1_x1x2 = fe_values.shape_grad_component(i, q_point, 0);
304 *
auto gradu2_x1x2 = fe_values.shape_grad_component(i, q_point, 1);
306 *
auto gradv1_x1x2 = fe_values.shape_grad_component(j, q_point, 0);
307 *
auto gradv2_x1x2 = fe_values.shape_grad_component(j, q_point, 1);
308 *
return (gradu2_x1x2[0] - gradu1_x1x2[1]) *
309 * (gradv2_x1x2[0] - gradv1_x1x2[1]);
317 * dot_term(const ::FEValues<dim> &fe_values,
318 *
const unsigned int & i,
319 *
const unsigned int & j,
320 *
const unsigned int & q_point)
322 *
double output = 0.0;
323 *
for (
unsigned int comp = 0; comp < dim; ++comp)
325 * output += fe_values.shape_value_component(i, q_point, comp) *
326 * fe_values.shape_value_component(j, q_point, comp);
336 *
namespace Structures
343 *
const unsigned int dim = 2;
345 *
const std::vector<Point<2>>
vertices = {{scaling * 0.0, scaling * 0.0},
346 * {scaling * 0.5, scaling * 0.0},
347 * {scaling * 0.0, scaling * 0.5},
348 * {scaling * 0.5, scaling * 0.5},
349 * {scaling * 0.0, scaling * 1.0},
350 * {scaling * 0.5, scaling * 1.0},
351 * {scaling * 1.0, scaling * 0.5},
352 * {scaling * 1.0, scaling * 1.0}};
354 *
const std::vector<std::array<int, GeometryInfo<dim>::vertices_per_cell>>
355 * cell_vertices = {{{0, 1, 2, 3}}, {{2, 3, 4, 5}}, {{3, 6, 5, 7}}};
356 *
const unsigned int n_cells = cell_vertices.size();
357 * std::vector<CellData<dim>> cells(n_cells,
CellData<dim>());
358 *
for (
unsigned int i = 0; i <
n_cells; ++i)
360 *
for (
unsigned int j = 0; j < cell_vertices[i].size(); ++j)
361 * cells[i].
vertices[j] = cell_vertices[i][j];
362 * cells[i].material_id = 0;
371 *
const double & scaling)
373 *
const unsigned int dim = 2;
375 *
const std::vector<Point<2>>
vertices = {{scaling * 0.0, scaling * 0.0},
376 * {scaling * 0.6, scaling * 0.0},
377 * {scaling * 0.0, scaling * 0.3},
378 * {scaling * 0.6, scaling * 0.3}};
380 *
const std::vector<std::array<int, GeometryInfo<dim>::vertices_per_cell>>
381 * cell_vertices = {{{0, 1, 2, 3}}};
382 *
const unsigned int n_cells = cell_vertices.size();
383 * std::vector<CellData<dim>> cells(n_cells,
CellData<dim>());
384 *
for (
unsigned int i = 0; i <
n_cells; ++i)
386 *
for (
unsigned int j = 0; j < cell_vertices[i].size(); ++j)
387 * cells[i].
vertices[j] = cell_vertices[i][j];
388 * cells[i].material_id = 0;
418 *
virtual unsigned int
419 * solve_problem() = 0;
421 * set_refinement_cycle(
const unsigned int cycle);
424 * output_solution() = 0;
429 *
unsigned int refinement_cycle = 0;
430 * std::unique_ptr<ParameterHandler> parameters;
431 *
unsigned int n_eigenpairs = 1;
432 *
double target = 0.0;
433 *
unsigned int eigenpair_selection_scheme;
434 *
unsigned int max_cycles = 0;
435 * ompi_communicator_t * mpi_communicator = PETSC_COMM_SELF;
444 * , parameters(std::make_unique<ParameterHandler>())
446 * parameters->declare_entry(
447 *
"Eigenpair selection scheme",
450 *
"The type of eigenpairs to find (0 - smallest, 1 - target)");
451 * parameters->declare_entry(
"Number of eigenvalues/eigenfunctions",
454 *
"The number of eigenvalues/eigenfunctions "
455 *
"to be computed.");
456 * parameters->declare_entry(
"Target eigenvalue",
459 *
"The target eigenvalue (if scheme == 1)");
461 * parameters->declare_entry(
"Cycles number",
464 *
"The number of cycles in refinement");
465 * parameters->parse_input(prm_file);
467 * eigenpair_selection_scheme =
468 * parameters->get_integer(
"Eigenpair selection scheme");
472 * The
project currently only supports selection by a target eigenvalue.
473 * Furthermore, only one eigenpair can be computed at a time.
476 * assert(eigenpair_selection_scheme == 1 &&
477 *
"Selection by a target is the only currently supported option!");
479 * parameters->get_integer(
"Number of eigenvalues/eigenfunctions");
481 * n_eigenpairs == 1 &&
482 *
"Only the computation of a single eigenpair is currently supported!");
484 * target = parameters->get_double(
"Target eigenvalue");
485 * max_cycles = parameters->get_integer(
"Cycles number");
486 *
if (eigenpair_selection_scheme == 1)
492 * Base<dim>::set_refinement_cycle(
const unsigned int cycle)
494 * refinement_cycle = cycle;
503 *
class EigenSolver :
public virtual Base<dim>
506 * EigenSolver(
const std::string & prm_file,
508 *
const unsigned int &minimum_degree,
509 *
const unsigned int &maximum_degree,
510 *
const unsigned int &starting_degree);
512 *
virtual unsigned int
513 * solve_problem()
override;
515 *
virtual unsigned int
518 *
template <
class SolverType>
520 * initialize_eigensolver(SolverType &eigensolver);
529 *
const std::unique_ptr<hp::FECollection<dim>> fe_collection;
530 * std::unique_ptr<hp::QCollection<dim>> quadrature_collection;
531 * std::unique_ptr<
hp::QCollection<dim - 1>> face_quadrature_collection;
533 *
const unsigned int max_degree, min_degree;
536 *
for the actual solution
539 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> eigenfunctions;
540 * std::unique_ptr<std::vector<double>>
eigenvalues;
550 * convert_solution();
562 * EigenSolver<dim>::EigenSolver(
const std::string & prm_file,
564 *
const unsigned int &minimum_degree,
565 *
const unsigned int &maximum_degree,
566 *
const unsigned int &starting_degree)
572 * , max_degree(maximum_degree)
573 * , min_degree(minimum_degree)
575 * std::make_unique<std::vector<PETScWrappers::MPI::Vector>>())
576 * ,
eigenvalues(std::make_unique<std::vector<double>>())
578 *
for (
unsigned int degree = min_degree; degree <= max_degree; ++degree)
583 * Generate quadrature collection with
sorted quadrature weights
588 * quadrature_collection->push_back(sorted_quadrature);
590 *
const QGauss<dim - 1> face_quadrature(degree + 1);
591 *
const QSorted<dim - 1> sorted_face_quadrature(face_quadrature);
592 * face_quadrature_collection->push_back(sorted_face_quadrature);
596 * adjust the discretization
599 *
if (starting_degree > min_degree && starting_degree <= max_degree)
601 *
const unsigned int start_diff = starting_degree - min_degree;
603 * cell1 = dof_handler.begin_active(),
604 * endc1 = dof_handler.end();
605 *
for (; cell1 < endc1; ++cell1)
607 * cell1->set_active_fe_index(start_diff);
618 * EigenSolver<dim>::get_lambda_h()
620 *
return &(*eigenvalues)[0];
629 * EigenSolver<dim>::get_solution()
639 * EigenSolver<dim>::convert_solution()
641 * solution.
reinit((*eigenfunctions)[0].size());
642 *
for (
unsigned int i = 0; i < solution.size(); ++i)
643 * solution[i] = (*eigenfunctions)[0][i];
653 *
template <
class SolverType>
655 * EigenSolver<dim>::initialize_eigensolver(SolverType &eigensolver)
659 * From the parameters
class, initialize the eigensolver...
662 *
switch (this->eigenpair_selection_scheme)
665 * eigensolver.set_which_eigenpairs(EPS_TARGET_MAGNITUDE);
668 * eigensolver.set_target_eigenvalue(this->target);
673 * eigensolver.set_which_eigenpairs(EPS_SMALLEST_MAGNITUDE);
677 * eigensolver.set_problem_type(EPS_GHEP);
680 *
apply a Shift-Invert spectrum transformation
686 *
double shift_scalar = this->parameters->get_double(
"Target eigenvalue");
695 * this->mpi_communicator, additional_data);
697 * eigensolver.set_transformation(spectral_transformation);
698 * eigensolver.set_target_eigenvalue(this->target);
707 * EigenSolver<dim>::solve_problem()
717 * this->mpi_communicator);
719 * initialize_eigensolver(eigensolver);
726 * eigensolver.solve(stiffness_matrix,
730 * eigenfunctions->size());
731 *
for (
auto &entry : *eigenfunctions)
733 * constraints.distribute(entry);
735 * convert_solution();
737 *
return solver_control.last_step();
742 * EigenSolver<dim>::n_dofs() const
744 *
return dof_handler.n_dofs();
754 * EigenSolver<dim>::setup_system()
756 * dof_handler.distribute_dofs(*fe_collection);
757 * constraints.clear();
760 * constraints.close();
762 * eigenfunctions->resize(this->n_eigenpairs);
765 *
IndexSet eigenfunction_index_set = dof_handler.locally_owned_dofs();
767 *
for (
auto &entry : *eigenfunctions)
769 * entry.
reinit(eigenfunction_index_set, MPI_COMM_WORLD);
778 * EigenSolver<dim>::assemble_system()
781 * *quadrature_collection,
787 * Prep the system matrices
for the solution
790 * stiffness_matrix.reinit(dof_handler.n_dofs(),
791 * dof_handler.n_dofs(),
792 * dof_handler.max_couplings_between_dofs());
794 * dof_handler.n_dofs(),
795 * dof_handler.max_couplings_between_dofs());
798 * std::vector<types::global_dof_index> local_dof_indices;
800 *
for (
const auto &cell : dof_handler.active_cell_iterators())
802 * const unsigned
int dofs_per_cell = cell->get_fe().dofs_per_cell;
804 * cell_stiffness_matrix.reinit(dofs_per_cell, dofs_per_cell);
805 * cell_stiffness_matrix = 0;
807 * cell_mass_matrix.reinit(dofs_per_cell, dofs_per_cell);
808 * cell_mass_matrix = 0;
810 * hp_fe_values.reinit(cell);
814 *
for (
unsigned int q_point = 0; q_point < fe_values.n_quadrature_points;
817 *
for (
unsigned int i = 0; i < dofs_per_cell; ++i)
819 *
for (
unsigned int j = 0; j < dofs_per_cell; ++j)
823 * Note that (in general) the Nedelec element is not
824 * primitive, namely that the shape
functions are vectorial
825 * with components in more than one direction
831 * cell_stiffness_matrix(i, j) +=
832 * Operations::curlcurl(fe_values, i, j, q_point) *
833 * fe_values.JxW(q_point);
835 * cell_mass_matrix(i, j) +=
836 * (Operations::dot_term(fe_values, i, j, q_point)) *
837 * fe_values.JxW(q_point);
840 * local_dof_indices.resize(dofs_per_cell);
841 * cell->get_dof_indices(local_dof_indices);
844 * constraints.distribute_local_to_global(cell_stiffness_matrix,
847 * constraints.distribute_local_to_global(cell_mass_matrix,
854 *
for (
unsigned int i = 0; i < dof_handler.n_dofs(); ++i)
855 *
if (constraints.is_constrained(i))
857 * stiffness_matrix.set(i, i, 10000.0);
862 * since we have just
set individual elements, we need the following
874 *
class PrimalSolver :
public EigenSolver<dim>
877 * PrimalSolver(
const std::string & prm_file,
879 *
const unsigned int &min_degree,
880 *
const unsigned int &max_degree,
881 *
const unsigned int &starting_degree);
886 *
virtual unsigned int
887 * n_dofs()
const override;
891 * PrimalSolver<dim>::PrimalSolver(
const std::string & prm_file,
893 *
const unsigned int &min_degree,
894 *
const unsigned int &max_degree,
895 *
const unsigned int &starting_degree)
897 * , EigenSolver<dim>(prm_file,
910 * PrimalSolver<dim>::output_solution()
915 *
for (
const auto &cell : this->dof_handler.active_cell_iterators())
916 * fe_degrees(cell->active_cell_index()) =
917 * (*this->fe_collection)[cell->active_fe_index()].degree;
918 * data_out.add_data_vector(fe_degrees,
"fe_degree");
919 * data_out.add_data_vector((*this->eigenfunctions)[0],
920 * std::string(
"eigenfunction_no_") +
923 * std::cout <<
"Eigenvalue: " << (*this->
eigenvalues)[0]
924 * <<
" NDoFs: " << this->dof_handler.n_dofs() << std::endl;
925 * std::ofstream eigenvalues_out(
926 *
"eigenvalues-" + std::to_string(this->refinement_cycle) +
".txt");
928 * eigenvalues_out << std::setprecision(20) << (*this->
eigenvalues)[0] <<
" "
929 * << this->dof_handler.n_dofs() << std::endl;
931 * eigenvalues_out.close();
934 * data_out.build_patches();
935 * std::ofstream output(
"eigenvectors-" +
936 * std::to_string(this->refinement_cycle) +
".vtu");
937 * data_out.write_vtu(output);
942 * PrimalSolver<dim>::n_dofs() const
944 *
return EigenSolver<dim>::n_dofs();
949 * Note, that at least
for the demonstrated problem (i.e., a Hermitian problem
950 * and eigenvalue QoI), the dual problem is identical to the primal problem;
951 * however, it is convenient to separate them in
this manner (
e.g.,
for
952 * considering functionals of the eigenfunction).
956 *
class DualSolver :
public EigenSolver<dim>
959 * DualSolver(
const std::string & prm_file,
961 *
const unsigned int &min_degree,
962 *
const unsigned int &max_degree,
963 *
const unsigned int &starting_degree);
967 * DualSolver<dim>::DualSolver(
const std::string & prm_file,
969 *
const unsigned int &min_degree,
970 *
const unsigned int &max_degree,
971 *
const unsigned int &starting_degree)
973 * , EigenSolver<dim>(prm_file,
985 *
namespace ErrorIndicators
987 *
using namespace Maxwell;
994 *
template <
int dim,
bool report_dual>
995 *
class DualWeightedResidual :
public PrimalSolver<dim>,
public DualSolver<dim>
999 * output_eigenvalue_data(std::ofstream &os);
1001 * output_qoi_error_estimates(std::ofstream &os);
1008 * DualWeightedResidual(
const std::string & prm_file,
1010 *
const unsigned int &min_primal_degree,
1011 *
const unsigned int &max_primal_degree,
1012 *
const unsigned int &starting_primal_degree);
1014 *
virtual unsigned int
1015 * solve_problem()
override;
1018 * output_solution()
override;
1020 *
virtual unsigned int
1021 * n_dofs()
const override;
1030 * get_primal_DoFHandler();
1033 * get_dual_DoFHandler();
1036 * get_FECollection();
1039 * get_primal_FECollection();
1041 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1042 * get_eigenfunctions();
1044 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1045 * get_primal_eigenfunctions();
1047 * std::unique_ptr<std::vector<double>> &
1048 * get_primal_eigenvalues();
1050 * std::unique_ptr<std::vector<double>> &
1051 * get_dual_eigenvalues();
1054 * synchronize_discretization();
1059 *
return PrimalSolver<dim>::fe_collection->max_degree();
1061 *
double qoi_error_estimate = 0;
1082 * std::unique_ptr<hp::FEValues<dim>> cell_hp_fe_values;
1083 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values;
1084 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values_neighbor;
1085 * std::unique_ptr<hp::FESubfaceValues<dim>> subface_hp_fe_values;
1087 * std::unique_ptr<hp::FEValues<dim>> cell_hp_fe_values_forward;
1088 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values_forward;
1089 * std::unique_ptr<hp::FEFaceValues<dim>> face_hp_fe_values_neighbor_forward;
1090 * std::unique_ptr<hp::FESubfaceValues<dim>> subface_hp_fe_values_forward;
1091 *
using FaceIntegrals =
1092 *
typename std::map<typename DoFHandler<dim>::face_iterator,
double>;
1095 * solve_primal_problem();
1098 * solve_dual_problem();
1109 * initialize_error_estimation_data();
1112 * estimate_on_one_cell(
1116 *
const double & lambda_h,
1118 * FaceIntegrals & face_integrals);
1121 * integrate_over_cell(
1125 *
const double & lambda_h,
1129 * integrate_over_regular_face(
1131 *
const unsigned int & face_no,
1134 * FaceIntegrals & face_integrals);
1137 * integrate_over_irregular_face(
1139 *
const unsigned int & face_no,
1142 * FaceIntegrals & face_integrals);
1149 *
template <
int dim,
bool report_dual>
1150 * DualWeightedResidual<dim, report_dual>::DualWeightedResidual(
1151 *
const std::string & prm_file,
1153 *
const unsigned int &min_primal_degree,
1154 *
const unsigned int &max_primal_degree,
1155 *
const unsigned int &starting_primal_degree)
1157 * , PrimalSolver<dim>(prm_file,
1159 * min_primal_degree,
1160 * max_primal_degree,
1161 * starting_primal_degree)
1162 * , DualSolver<dim>(prm_file,
1164 * min_primal_degree + 1,
1165 * max_primal_degree + 1,
1166 * starting_primal_degree + 1)
1168 * initialize_error_estimation_data();
1175 *
template <
int dim,
bool report_dual>
1177 * DualWeightedResidual<dim, report_dual>::get_DoFHandler()
1180 *
return &(PrimalSolver<dim>::dof_handler);
1182 *
return &(DualSolver<dim>::dof_handler);
1187 * See above function, but to specifically output the primal
DoFHandler...
1190 *
template <
int dim,
bool report_dual>
1192 * DualWeightedResidual<dim, report_dual>::get_primal_DoFHandler()
1194 *
return &(PrimalSolver<dim>::dof_handler);
1199 * See above function, but
for the FECollection
1202 *
template <
int dim,
bool report_dual>
1204 * DualWeightedResidual<dim, report_dual>::get_FECollection()
1207 *
return &*(PrimalSolver<dim>::fe_collection);
1209 *
return &*(DualSolver<dim>::fe_collection);
1214 * See above function, but
for the primal FECollection
1217 *
template <
int dim,
bool report_dual>
1219 * DualWeightedResidual<dim, report_dual>::get_primal_FECollection()
1221 *
return &*(PrimalSolver<dim>::fe_collection);
1224 *
template <
int dim,
bool report_dual>
1226 * DualWeightedResidual<dim, report_dual>::get_dual_DoFHandler()
1228 *
return &(DualSolver<dim>::dof_handler);
1232 *
template <
int dim,
bool report_dual>
1233 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1234 * DualWeightedResidual<dim, report_dual>::get_eigenfunctions()
1237 *
return (PrimalSolver<dim>::eigenfunctions);
1239 *
return (DualSolver<dim>::eigenfunctions);
1243 *
template <
int dim,
bool report_dual>
1244 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
1245 * DualWeightedResidual<dim, report_dual>::get_primal_eigenfunctions()
1247 *
return (PrimalSolver<dim>::eigenfunctions);
1251 *
template <
int dim,
bool report_dual>
1252 * std::unique_ptr<std::vector<double>> &
1253 * DualWeightedResidual<dim, report_dual>::get_primal_eigenvalues()
1255 *
return PrimalSolver<dim>::eigenvalues;
1259 *
template <
int dim,
bool report_dual>
1260 * std::unique_ptr<std::vector<double>> &
1261 * DualWeightedResidual<dim, report_dual>::get_dual_eigenvalues()
1263 *
return DualSolver<dim>::eigenvalues;
1266 *
template <
int dim,
bool report_dual>
1268 * DualWeightedResidual<dim, report_dual>::output_solution()
1270 * PrimalSolver<dim>::output_solution();
1275 * Solves the primal problem
1278 *
template <
int dim,
bool report_dual>
1280 * DualWeightedResidual<dim, report_dual>::solve_primal_problem()
1282 *
return PrimalSolver<dim>::solve_problem();
1287 * Solves the dual problem
1290 *
template <
int dim,
bool report_dual>
1292 * DualWeightedResidual<dim, report_dual>::solve_dual_problem()
1294 *
return DualSolver<dim>::solve_problem();
1301 *
template <
int dim,
bool report_dual>
1303 * DualWeightedResidual<dim, report_dual>::solve_problem()
1305 * DualWeightedResidual<dim, report_dual>::solve_primal_problem();
1306 *
return DualWeightedResidual<dim, report_dual>::solve_dual_problem();
1312 *
template <
int dim,
bool report_dual>
1314 * DualWeightedResidual<dim, report_dual>::n_dofs() const
1316 *
return PrimalSolver<dim>::n_dofs();
1325 *
template <
int dim,
bool report_dual>
1327 * DualWeightedResidual<dim, report_dual>::synchronize_discretization()
1341 * In
this case, we have modified the polynomial orders
for the dual;
1342 * need to update the primal
1345 * dof1 = &(DualSolver<dim>::dof_handler);
1346 * dof2 = &(PrimalSolver<dim>::dof_handler);
1349 * endc1 = dof1->end();
1351 *
for (; cell1 < endc1; ++cell1, ++cell2)
1353 * cell2->set_active_fe_index(cell1->active_fe_index());
1361 *
template <
int dim,
bool report_dual>
1363 * DualWeightedResidual<dim, report_dual>::initialize_error_estimation_data()
1367 * initialize the cell fe_values...
1370 * cell_hp_fe_values = std::make_unique<hp::FEValues<dim>>(
1371 * *DualSolver<dim>::fe_collection,
1372 * *DualSolver<dim>::quadrature_collection,
1375 * face_hp_fe_values = std::make_unique<hp::FEFaceValues<dim>>(
1376 * *DualSolver<dim>::fe_collection,
1377 * *DualSolver<dim>::face_quadrature_collection,
1380 * face_hp_fe_values_neighbor = std::make_unique<hp::FEFaceValues<dim>>(
1381 * *DualSolver<dim>::fe_collection,
1382 * *DualSolver<dim>::face_quadrature_collection,
1385 * subface_hp_fe_values = std::make_unique<hp::FESubfaceValues<dim>>(
1386 * *DualSolver<dim>::fe_collection,
1387 * *DualSolver<dim>::face_quadrature_collection,
1396 *
template <
int dim,
bool report_dual>
1398 * DualWeightedResidual<dim, report_dual>::normalize_solutions(
1402 *
double sum_primal = 0.0, sum_dual = 0.0;
1403 *
for (
const auto &cell :
1404 * DualSolver<dim>::dof_handler.active_cell_iterators())
1406 * cell_hp_fe_values->
reinit(cell);
1410 * grab the fe_values
object
1416 * std::vector<Vector<double>> cell_primal_values(
1418 * cell_dual_values(fe_values.n_quadrature_points,
Vector<double>(dim));
1419 * fe_values.get_function_values(primal_solution, cell_primal_values);
1420 * fe_values.get_function_values(dual_weights, cell_dual_values);
1423 *
for (
unsigned int p = 0; p < fe_values.n_quadrature_points; ++p)
1426 * cell_primal_values[p] * cell_primal_values[p] * fe_values.JxW(p);
1428 * cell_dual_values[p] * cell_dual_values[p] * fe_values.JxW(p);
1432 * primal_solution /=
sqrt(sum_primal);
1433 * dual_weights /=
sqrt(sum_dual);
1440 *
template <
int dim,
bool report_dual>
1442 * DualWeightedResidual<dim, report_dual>::estimate_error(
1447 * The constraints could be grabbed directly, but
this is simple
1452 * primal_hanging_node_constraints);
1453 * primal_hanging_node_constraints.close();
1457 * dual_hanging_node_constraints);
1458 * dual_hanging_node_constraints.close();
1462 * First map the primal solution to the space of the dual solution
1463 * This allows us to use just one
set of
FEValues objects (rather than one
1464 * set
for the primal, one
for dual)
1470 *
Vector<double> primal_solution(DualSolver<dim>::dof_handler.n_dofs());
1472 * embed(PrimalSolver<dim>::dof_handler,
1473 * DualSolver<dim>::dof_handler,
1474 * dual_hanging_node_constraints,
1475 * *(PrimalSolver<dim>::get_solution()),
1478 *
Vector<double> &dual_solution = *(DualSolver<dim>::get_solution());
1480 * normalize_solutions(primal_solution, dual_solution);
1482 *
Vector<double> dual_weights(DualSolver<dim>::dof_handler.n_dofs()),
1483 * dual_weights_interm(PrimalSolver<dim>::dof_handler.n_dofs());
1487 * First
extract the dual solution to the space of the primal
1490 *
extract(DualSolver<dim>::dof_handler,
1491 * PrimalSolver<dim>::dof_handler,
1492 * primal_hanging_node_constraints,
1493 * *(DualSolver<dim>::get_solution()),
1494 * dual_weights_interm);
1498 * Now embed
this back to the space of the dual solution
1501 * embed(PrimalSolver<dim>::dof_handler,
1502 * DualSolver<dim>::dof_handler,
1503 * dual_hanging_node_constraints,
1504 * dual_weights_interm,
1510 * Subtract
this from the full dual solution
1513 * dual_weights -= *(DualSolver<dim>::get_solution());
1514 * dual_weights *= -1.0;
1516 * *(DualSolver<dim>::get_solution()) -= primal_solution;
1518 * FaceIntegrals face_integrals;
1519 *
for (
const auto &cell :
1520 * DualSolver<dim>::dof_handler.active_cell_iterators())
1521 * for (const auto &face : cell->face_iterators())
1522 * face_integrals[face] = -1e20;
1525 *
for (
const auto &cell :
1526 * DualSolver<dim>::dof_handler.active_cell_iterators())
1528 * estimate_on_one_cell(cell,
1531 * *(PrimalSolver<dim>::get_lambda_h()),
1535 *
unsigned int present_cell = 0;
1536 *
for (
const auto &cell :
1537 * DualSolver<dim>::dof_handler.active_cell_iterators())
1539 * for (const auto &face : cell->face_iterators())
1541 *
Assert(face_integrals.find(face) != face_integrals.
end(),
1542 * ExcInternalError());
1543 * error_indicators(present_cell) -= 0.5 * face_integrals[face];
1550 * Now, with the error indicators computed, let us produce the
1551 * estimate of the QoI error
1554 * this->qoi_error_estimate =
1555 * this->get_global_QoI_error(*(DualSolver<dim>::get_solution()),
1556 * error_indicators);
1557 * std::cout <<
"Estimated QoI error: " << std::setprecision(20)
1558 * << qoi_error_estimate << std::endl;
1565 *
template <
int dim,
bool report_dual>
1567 * DualWeightedResidual<dim, report_dual>::estimate_on_one_cell(
1571 *
const double & lambda_h,
1573 * FaceIntegrals & face_integrals)
1575 * integrate_over_cell(
1576 * cell, primal_solution, dual_weights, lambda_h, error_indicators);
1577 *
for (
unsigned int face_no :
GeometryInfo<dim>::face_indices())
1579 * if (cell->face(face_no)->at_boundary())
1581 * face_integrals[cell->face(face_no)] = 0.0;
1584 *
if ((cell->neighbor(face_no)->has_children() ==
false) &&
1585 * (cell->neighbor(face_no)->level() == cell->level()) &&
1586 * (cell->neighbor(face_no)->index() < cell->index()))
1588 *
if (cell->at_boundary(face_no) ==
false)
1589 *
if (cell->neighbor(face_no)->level() < cell->level())
1591 *
if (cell->face(face_no)->has_children() ==
false)
1592 * integrate_over_regular_face(
1593 * cell, face_no, primal_solution, dual_weights, face_integrals);
1595 * integrate_over_irregular_face(
1596 * cell, face_no, primal_solution, dual_weights, face_integrals);
1603 *
template <
int dim,
bool report_dual>
1605 * DualWeightedResidual<dim, report_dual>::integrate_over_cell(
1609 *
const double & lambda_h,
1612 * cell_hp_fe_values->reinit(cell);
1615 * Grab the fe_values
object
1619 * std::vector<std::vector<Tensor<2, dim, double>>> cell_hessians(
1621 * std::vector<Vector<double>> cell_primal_values(
1623 * cell_dual_values(fe_values.n_quadrature_points,
Vector<double>(dim));
1624 * fe_values.get_function_values(primal_solution, cell_primal_values);
1625 * fe_values.get_function_hessians(primal_solution, cell_hessians);
1626 * fe_values.get_function_values(dual_weights, cell_dual_values);
1631 *
for (
unsigned int p = 0; p < fe_values.n_quadrature_points; ++p)
1634 * ( (cell_hessians[p][1][1][0] -
1635 * cell_hessians[p][0][1][1]) *
1636 * (cell_dual_values[p](0)) +
1638 * (cell_hessians[p][0][0][1] - cell_hessians[p][1][0][0]) *
1639 * (cell_dual_values[p](1)) -
1640 * lambda_h * (cell_primal_values[p](0) * cell_dual_values[p](0) +
1641 * cell_primal_values[p](1) * cell_dual_values[p](1))) *
1645 * error_indicators(cell->active_cell_index()) +=
sum;
1651 *
template <
int dim,
bool report_dual>
1653 * DualWeightedResidual<dim, report_dual>::integrate_over_regular_face(
1655 *
const unsigned int & face_no,
1658 * FaceIntegrals & face_integrals)
1661 * ExcInternalError());
1662 *
const unsigned int neighbor_neighbor = cell->neighbor_of_neighbor(face_no);
1663 *
const auto neighbor = cell->neighbor(face_no);
1665 *
const unsigned int quadrature_index =
1666 *
std::max(cell->active_fe_index(), neighbor->active_fe_index());
1667 * face_hp_fe_values->reinit(cell, face_no, quadrature_index);
1670 * std::vector<std::vector<Tensor<1, dim, double>>> cell_primal_grads(
1671 * fe_face_values_cell.n_quadrature_points,
1673 * neighbor_primal_grads(fe_face_values_cell.n_quadrature_points,
1675 * fe_face_values_cell.get_function_gradients(primal_solution,
1676 * cell_primal_grads);
1678 * face_hp_fe_values_neighbor->reinit(neighbor,
1679 * neighbor_neighbor,
1680 * quadrature_index);
1684 * neighbor_primal_grads);
1685 *
const unsigned int n_q_points = fe_face_values_cell.n_quadrature_points;
1686 *
double face_integral = 0.0;
1687 * std::vector<Vector<double>> cell_dual_values(n_q_points,
1689 * fe_face_values_cell.get_function_values(dual_weights, cell_dual_values);
1690 *
for (
unsigned int p = 0; p < n_q_points; ++p)
1692 *
auto face_normal = fe_face_values_cell.normal_vector(p);
1695 * (cell_primal_grads[p][1][0] - cell_primal_grads[p][0][1] -
1696 * neighbor_primal_grads[p][1][0] + neighbor_primal_grads[p][0][1]) *
1697 * (cell_dual_values[p][0] * face_normal[1] -
1698 * cell_dual_values[p][1] * face_normal[0]) *
1699 * fe_face_values_cell.JxW(p);
1701 *
Assert(face_integrals.find(cell->face(face_no)) != face_integrals.end(),
1702 * ExcInternalError());
1703 *
Assert(face_integrals[cell->face(face_no)] == -1e20, ExcInternalError());
1704 * face_integrals[cell->face(face_no)] = face_integral;
1710 *
template <
int dim,
bool report_dual>
1712 * DualWeightedResidual<dim, report_dual>::integrate_over_irregular_face(
1714 *
const unsigned int & face_no,
1717 * FaceIntegrals & face_integrals)
1721 * cell->neighbor(face_no);
1724 *
Assert(neighbor->has_children(), ExcInternalError());
1726 *
const unsigned int neighbor_neighbor = cell->neighbor_of_neighbor(face_no);
1727 *
for (
unsigned int subface_no = 0; subface_no < face->n_children();
1731 * cell->neighbor_child_on_subface(face_no, subface_no);
1732 *
Assert(neighbor_child->face(neighbor_neighbor) ==
1733 * cell->face(face_no)->child(subface_no),
1734 * ExcInternalError());
1735 *
const unsigned int quadrature_index =
1736 *
std::max(cell->active_fe_index(), neighbor_child->active_fe_index());
1739 * initialize fe_subface values_cell
1742 * subface_hp_fe_values->reinit(cell,
1745 * quadrature_index);
1748 * std::vector<std::vector<Tensor<1, dim, double>>> cell_primal_grads(
1749 * subface_fe_values_cell.n_quadrature_points,
1751 * neighbor_primal_grads(subface_fe_values_cell.n_quadrature_points,
1753 * subface_fe_values_cell.get_function_gradients(primal_solution,
1754 * cell_primal_grads);
1757 * initialize fe_face_values_neighbor
1760 * face_hp_fe_values_neighbor->reinit(neighbor_child,
1761 * neighbor_neighbor,
1762 * quadrature_index);
1766 * neighbor_primal_grads);
1767 *
const unsigned int n_q_points =
1768 * subface_fe_values_cell.n_quadrature_points;
1769 * std::vector<Vector<double>> cell_dual_values(n_q_points,
1771 * face_fe_values_neighbor.get_function_values(dual_weights,
1772 * cell_dual_values);
1774 *
double face_integral = 0.0;
1776 *
for (
unsigned int p = 0; p < n_q_points; ++p)
1778 *
auto face_normal = face_fe_values_neighbor.normal_vector(p);
1780 * (cell_primal_grads[p][0][1] - cell_primal_grads[p][1][0] +
1781 * neighbor_primal_grads[p][1][0] -
1782 * neighbor_primal_grads[p][0][1]) *
1783 * (cell_dual_values[p][0] * face_normal[1] -
1784 * cell_dual_values[p][1] * face_normal[0]) *
1785 * face_fe_values_neighbor.JxW(p);
1787 * face_integrals[neighbor_child->face(neighbor_neighbor)] = face_integral;
1790 *
for (
unsigned int subface_no = 0; subface_no < face->n_children();
1793 *
Assert(face_integrals.find(face->child(subface_no)) !=
1794 * face_integrals.end(),
1795 * ExcInternalError());
1796 *
Assert(face_integrals[face->child(subface_no)] != -1e20,
1797 * ExcInternalError());
1798 *
sum += face_integrals[face->child(subface_no)];
1800 * face_integrals[face] =
sum;
1803 *
template <
int dim,
bool report_dual>
1805 * DualWeightedResidual<dim, report_dual>::get_global_QoI_error(
1809 *
auto dual_less_primal =
1813 *
double scaling_factor = 0.0;
1814 *
for (
const auto &cell :
1815 * DualSolver<dim>::dof_handler.active_cell_iterators())
1817 * cell_hp_fe_values->
reinit(cell);
1820 * grab the fe_values
object
1826 * std::vector<Vector<double>> cell_values(fe_values.n_quadrature_points,
1828 * fe_values.get_function_values(dual_less_primal, cell_values);
1830 *
for (
unsigned int p = 0; p < fe_values.n_quadrature_points; ++p)
1833 * (cell_values[p] * cell_values[p]) * fe_values.JxW(p);
1836 *
double global_QoI_error = 0.0;
1837 *
for (
const auto &indicator : error_indicators)
1839 * global_QoI_error += indicator;
1842 * global_QoI_error /= (1 - 0.5 * scaling_factor);
1843 *
return global_QoI_error;
1847 *
template <
int dim,
bool report_dual>
1849 * DualWeightedResidual<dim, report_dual>::embed(
1856 * assert(u2.size() == dof2.n_dofs() &&
"Incorrect input vector size!");
1861 * endc1 = dof1.end();
1864 *
for (; cell1 < endc1; ++cell1, ++cell2)
1871 * assert(fe1.degree < fe2.degree &&
"Incorrect usage of embed!");
1875 * Get the embedding_dofs
1884 * std::vector<unsigned int> embedding_dofs =
1885 * fe2.get_embedding_dofs(fe1.degree);
1886 *
const unsigned int dofs_per_cell2 = fe2.n_dofs_per_cell();
1892 * local_dof_values_1.reinit(fe1.dofs_per_cell);
1893 * cell1->get_dof_values(solution, local_dof_values_1);
1895 *
for (
unsigned int i = 0; i < local_dof_values_1.size(); ++i)
1896 * local_dof_values_2[embedding_dofs[i]] = local_dof_values_1[i];
1900 * Now
set this changes to the global vector
1903 * cell2->set_dof_values(local_dof_values_2, u2);
1909 * Applies the constraints of the target finite element space
1912 * constraints.distribute(u2);
1915 *
template <
int dim,
bool report_dual>
1917 * DualWeightedResidual<dim, report_dual>::extract(
1926 * Maps from fe1 to fe2
1929 * assert(u2.size() == dof2.n_dofs() &&
"Incorrect input vector size!");
1934 * endc1 = dof1.end();
1937 *
for (; cell1 < endc1; ++cell1, ++cell2)
1944 * assert(fe1.degree > fe2.degree &&
"Incorrect usage of extract!");
1948 * Get the embedding_dofs
1951 * std::vector<unsigned int> embedding_dofs =
1952 * fe1.get_embedding_dofs(fe2.degree);
1953 *
const unsigned int dofs_per_cell2 = fe2.n_dofs_per_cell();
1959 * local_dof_values_1.reinit(fe1.dofs_per_cell);
1960 * cell1->get_dof_values(solution, local_dof_values_1);
1962 *
for (
unsigned int i = 0; i < local_dof_values_2.size(); ++i)
1963 * local_dof_values_2[i] = local_dof_values_1[embedding_dofs[i]];
1967 * Now
set this changes to the global vector
1970 * cell2->set_dof_values(local_dof_values_2, u2);
1976 * Applies the constraints of the target finite element space
1979 * constraints.distribute(u2);
1981 *
template <
int dim,
bool report_dual>
1983 * DualWeightedResidual<dim, report_dual>::output_eigenvalue_data(
1984 * std::ofstream &os)
1986 * os << (*this->get_primal_eigenvalues())[0] <<
" "
1987 * << (this->get_primal_DoFHandler())->n_dofs() <<
" "
1988 * << (*this->get_dual_eigenvalues())[0] <<
" "
1989 * << (this->get_dual_DoFHandler())->n_dofs() << std::endl;
1991 *
template <
int dim,
bool report_dual>
1993 * DualWeightedResidual<dim, report_dual>::output_qoi_error_estimates(
1994 * std::ofstream &os)
1996 * os << qoi_error_estimate << std::endl;
2003 *
template <
int dim>
2004 *
class KellyErrorIndicator :
public PrimalSolver<dim>
2013 * output_eigenvalue_data(std::ofstream &os);
2015 * output_qoi_error_estimates(std::ofstream &);
2016 * KellyErrorIndicator(
const std::string & prm_file,
2018 *
const unsigned int &min_degree,
2019 *
const unsigned int &max_degree,
2020 *
const unsigned int &starting_degree);
2022 *
virtual unsigned int
2023 * solve_problem()
override;
2026 * output_solution()
override;
2029 * get_FECollection();
2032 * get_primal_FECollection();
2034 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2035 * get_eigenfunctions();
2037 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2038 * get_primal_eigenfunctions();
2040 * std::unique_ptr<std::vector<double>> &
2041 * get_primal_eigenvalues();
2045 * synchronize_discretization();
2051 * get_primal_DoFHandler();
2056 *
return PrimalSolver<dim>::fe_collection->max_degree();
2058 *
double qoi_error_estimate = 0;
2066 * prune_eigenpairs(
const double &TOL);
2068 * std::vector<const PETScWrappers::MPI::Vector *> eigenfunction_ptrs;
2069 * std::vector<const double *> eigenvalue_ptrs;
2071 * std::vector<std::shared_ptr<Vector<float>>> errors;
2074 *
template <
int dim>
2075 * KellyErrorIndicator<dim>::KellyErrorIndicator(
2076 *
const std::string & prm_file,
2078 *
const unsigned int &min_degree,
2079 *
const unsigned int &max_degree,
2080 *
const unsigned int &starting_degree)
2081 * : Base<dim>(prm_file, coarse_grid)
2082 * , PrimalSolver<dim>(prm_file,
2089 *
template <
int dim>
2091 * KellyErrorIndicator<dim>::solve_problem()
2093 *
return PrimalSolver<dim>::solve_problem();
2096 *
template <
int dim>
2098 * KellyErrorIndicator<dim>::get_FECollection()
2100 *
return &*(PrimalSolver<dim>::fe_collection);
2103 *
template <
int dim>
2105 * KellyErrorIndicator<dim>::get_primal_FECollection()
2107 *
return &*(PrimalSolver<dim>::fe_collection);
2110 *
template <
int dim>
2111 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2112 * KellyErrorIndicator<dim>::get_eigenfunctions()
2114 *
return (PrimalSolver<dim>::eigenfunctions);
2117 *
template <
int dim>
2118 * std::unique_ptr<std::vector<double>> &
2119 * KellyErrorIndicator<dim>::get_primal_eigenvalues()
2121 *
return PrimalSolver<dim>::eigenvalues;
2124 *
template <
int dim>
2125 * std::unique_ptr<std::vector<PETScWrappers::MPI::Vector>> &
2126 * KellyErrorIndicator<dim>::get_primal_eigenfunctions()
2128 *
return (PrimalSolver<dim>::eigenfunctions);
2131 *
template <
int dim>
2133 * KellyErrorIndicator<dim>::get_DoFHandler()
2135 *
return &(PrimalSolver<dim>::dof_handler);
2138 *
template <
int dim>
2140 * KellyErrorIndicator<dim>::get_primal_DoFHandler()
2142 *
return &(PrimalSolver<dim>::dof_handler);
2145 *
template <
int dim>
2147 * KellyErrorIndicator<dim>::synchronize_discretization()
2151 * This function does
nothing for this error indicator
2157 *
template <
int dim>
2159 * KellyErrorIndicator<dim>::output_solution()
2161 * PrimalSolver<dim>::output_solution();
2164 *
template <
int dim>
2166 * KellyErrorIndicator<dim>::prune_eigenpairs(
const double &TOL)
2168 *
unsigned int count = 0;
2169 *
for (
size_t eigenpair_index = 0;
2170 * eigenpair_index < this->eigenfunctions->size();
2171 * ++eigenpair_index)
2173 *
if (count >= this->n_eigenpairs)
2175 *
if (
abs((*this->eigenvalues)[eigenpair_index]) < TOL)
2178 * eigenfunction_ptrs.push_back(&(*this->eigenfunctions)[eigenpair_index]);
2179 * eigenvalue_ptrs.push_back(&(*this->eigenvalues)[eigenpair_index]);
2183 *
template <
int dim>
2185 * KellyErrorIndicator<dim>::estimate_error(
Vector<double> &error_indicators)
2187 * std::cout <<
"Marking cells via Kelly indicator..." << std::endl;
2188 * prune_eigenpairs(1e-9);
2191 * deallocate the errors vector
2195 *
for (
size_t i = 0; i < eigenfunction_ptrs.size(); ++i)
2197 * errors.emplace_back(
2200 * std::vector<Vector<float> *> estimated_error_per_cell(
2201 * eigenfunction_ptrs.size());
2202 *
for (
size_t i = 0; i < eigenfunction_ptrs.size(); ++i)
2204 * estimated_error_per_cell[i] = errors[i].get();
2208 * *this->face_quadrature_collection,
2210 * eigenfunction_ptrs,
2211 * estimated_error_per_cell);
2213 *
for (
auto &error_vec : errors)
2215 * auto normalized_vec = *error_vec;
2216 * normalized_vec /= normalized_vec.l1_norm();
2218 *
for (
unsigned int i = 0; i < error_indicators.size(); ++i)
2219 * error_indicators(i) += double(normalized_vec(i));
2221 * std::cout <<
"...Done!" << std::endl;
2223 *
template <
int dim>
2225 * KellyErrorIndicator<dim>::output_eigenvalue_data(std::ofstream &os)
2227 * os << (*this->get_primal_eigenvalues())[0] <<
" "
2228 * << (this->get_primal_DoFHandler())->n_dofs() << std::endl;
2230 *
template <
int dim>
2232 * KellyErrorIndicator<dim>::output_qoi_error_estimates(std::ofstream &)
2242 *
namespace RegularityIndicators
2244 *
using namespace dealii;
2248 *
template <
int dim>
2249 *
class LegendreInfo
2253 *
class LegendreInfo<2>
2256 * std::unique_ptr<FESeries::Legendre<2>> legendre_u, legendre_v;
2264 * assert(fe_collection !=
nullptr && dof_handler !=
nullptr &&
2265 *
"A valid FECollection and DoFHandler must be accessible!");
2267 * legendre_u = std::make_unique<FESeries::Legendre<2>>(
2269 * legendre_v = std::make_unique<FESeries::Legendre<2>>(
2272 * legendre_u->precalculate_all_transformation_matrices();
2273 * legendre_v->precalculate_all_transformation_matrices();
2276 *
template <
class VectorType>
2278 * compute_coefficient_decay(
const VectorType & eigenfunction,
2279 * std::vector<double> &smoothness_indicators)
2283 * Compute the coefficients
for the u and v components of the solution
2288 * smoothness_v(smoothness_indicators.size());
2300 *
for (
unsigned int i = 0; i < smoothness_indicators.size(); ++i)
2302 * smoothness_indicators[i] =
std::min(smoothness_u[i], smoothness_v[i]);
2310 *
template <
int dim>
2311 *
class LegendreIndicator
2319 *
template <
class VectorType>
2321 * estimate_smoothness(
2322 *
const std::unique_ptr<std::vector<VectorType>> &eigenfunctions,
2323 *
const unsigned int & index_of_goal,
2324 * std::vector<double> & smoothness_indicators);
2330 *
template <
int dim>
2332 * LegendreIndicator<dim>::attach_FE_info_and_initialize(
2341 *
template <
int dim>
2342 *
template <
class VectorType>
2344 * LegendreIndicator<dim>::estimate_smoothness(
2345 *
const std::unique_ptr<std::vector<VectorType>> &eigenfunctions,
2346 *
const unsigned int & index_of_goal,
2347 * std::vector<double> & smoothness_indicators)
2349 * this->
legendre.compute_coefficient_decay((*eigenfunctions)[index_of_goal],
2350 * smoothness_indicators);
2358 *
namespace Refinement
2360 *
using namespace dealii;
2361 *
using namespace Maxwell;
2363 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2364 *
class Refiner :
public ErrorIndicator,
public RegularityIndicator
2367 * Refiner(
const std::string & prm_file,
2369 *
const unsigned int &min_degree,
2370 *
const unsigned int &max_degree,
2371 *
const unsigned int &starting_degree);
2374 * execute_refinement(
const double &smoothness_threshold_fraction);
2377 * output_solution()
override;
2381 * std::vector<double> smoothness_indicators;
2382 * std::ofstream eigenvalues_out;
2383 * std::ofstream error_estimate_out;
2386 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2387 * Refiner<dim, ErrorIndicator, RegularityIndicator>::Refiner(
2388 *
const std::string & prm_file,
2390 *
const unsigned int &min_degree,
2391 *
const unsigned int &max_degree,
2392 *
const unsigned int &starting_degree)
2393 * : Base<dim>(prm_file, coarse_grid)
2394 * , ErrorIndicator(prm_file,
2399 * , RegularityIndicator()
2401 *
if (ErrorIndicator::name() ==
"DWR")
2403 * error_estimate_out.open(
"error_estimate.txt");
2404 * error_estimate_out << std::setprecision(20);
2407 * eigenvalues_out.open(
"eigenvalues_" + ErrorIndicator::name() +
"_out.txt");
2408 * eigenvalues_out << std::setprecision(20);
2413 * For generating samples of the curl of the electric field
2416 *
template <
int dim>
2420 * CurlPostprocessor()
2427 * std::vector<
Vector<double>> &computed_quantities)
const override
2430 * computed_quantities.size());
2431 *
for (
unsigned int p = 0; p < input_data.solution_gradients.size(); ++p)
2433 * computed_quantities[p](0) = input_data.solution_gradients[p][1][0] -
2434 * input_data.solution_gradients[p][0][1];
2446 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2448 * Refiner<dim, ErrorIndicator, RegularityIndicator>::output_solution()
2450 * CurlPostprocessor<dim> curl_u;
2453 *
auto & output_dof = *(ErrorIndicator::get_primal_DoFHandler());
2456 *
for (
const auto &cell : output_dof.active_cell_iterators())
2457 * fe_degrees(cell->active_cell_index()) =
2458 * (*ErrorIndicator::get_primal_FECollection())[cell->active_fe_index()]
2460 * data_out.add_data_vector(fe_degrees,
"fe_degree");
2462 * data_out.add_data_vector(estimated_error_per_cell,
"error");
2464 *
for (
const auto &cell : output_dof.active_cell_iterators())
2466 * auto i = cell->active_cell_index();
2467 *
if (!cell->refine_flag_set() && !cell->coarsen_flag_set())
2468 * smoothness_out(i) = -1;
2470 * smoothness_out(i) = smoothness_indicators[i];
2472 * data_out.add_data_vector(smoothness_out,
"smoothness");
2473 * data_out.add_data_vector((*ErrorIndicator::get_primal_eigenfunctions())[0],
2474 * std::string(
"eigenfunction_no_") +
2476 * data_out.add_data_vector((*ErrorIndicator::get_primal_eigenfunctions())[0],
2479 * ErrorIndicator::output_eigenvalue_data(eigenvalues_out);
2480 * ErrorIndicator::output_qoi_error_estimates(error_estimate_out);
2482 * std::cout <<
"Number of DoFs: " << (this->get_primal_DoFHandler())->n_dofs()
2486 * data_out.build_patches();
2487 * std::ofstream output(
"eigenvectors-" + ErrorIndicator::name() +
"-" +
2488 * std::to_string(this->refinement_cycle) + +
".vtu");
2489 * data_out.write_vtu(output);
2498 *
template <
int dim,
class ErrorIndicator,
class RegularityIndicator>
2500 * Refiner<dim, ErrorIndicator, RegularityIndicator>::execute_refinement(
2501 *
const double &smoothness_threshold_fraction)
2505 * First initialize the RegularityIndicator...
2506 * Depending on the limits
set,
this may take a
while
2509 * std::cout <<
"Initializing RegularityIndicator..." << std::endl;
2511 * <<
"(This may take a while if the max expansion order is set too high)"
2513 * RegularityIndicator::attach_FE_info_and_initialize(
2514 * ErrorIndicator::get_FECollection(), ErrorIndicator::get_DoFHandler());
2515 * std::cout <<
"Done!" << std::endl <<
"Starting Refinement..." << std::endl;
2517 *
for (
unsigned int cycle = 0; cycle <= this->max_cycles; ++cycle)
2519 * this->set_refinement_cycle(cycle);
2520 * std::cout <<
"Cycle: " << cycle << std::endl;
2521 * ErrorIndicator::solve_problem();
2522 * this->estimated_error_per_cell.reinit(
2525 * ErrorIndicator::estimate_error(estimated_error_per_cell);
2529 * Depending on the source of the error estimation/indication, these
2530 *
values might be signed, so we address that with the following
2533 *
for (
double &error_indicator : estimated_error_per_cell)
2534 * error_indicator =
std::
abs(error_indicator);
2538 * *this->
triangulation, estimated_error_per_cell, 1. / 5., 0.000);
2542 * Now get regularity indicators
2543 * For those elements which must be refined,
swap to increasing @f$p@f$
2544 * depending on the regularity threshold...
2550 * smoothness_indicators =
2551 * std::vector<double>(this->
triangulation->n_active_cells(),
2552 * std::numeric_limits<double>::max());
2553 *
if (ErrorIndicator::PrimalSolver::min_degree !=
2554 * ErrorIndicator::PrimalSolver::max_degree)
2555 * RegularityIndicator::estimate_smoothness(
2556 * ErrorIndicator::get_eigenfunctions(), 0, smoothness_indicators);
2562 * this->output_solution();
2563 *
const double threshold_smoothness = smoothness_threshold_fraction;
2564 *
unsigned int num_refined = 0, num_coarsened = 0;
2565 *
if (ErrorIndicator::PrimalSolver::min_degree !=
2566 * ErrorIndicator::PrimalSolver::max_degree)
2568 *
for (
const auto &cell :
2569 * ErrorIndicator::get_DoFHandler()->active_cell_iterators())
2571 * if (cell->refine_flag_set())
2573 *
if (cell->coarsen_flag_set())
2575 *
if (cell->refine_flag_set() &&
2576 * smoothness_indicators[cell->active_cell_index()] >
2577 * threshold_smoothness &&
2578 *
static_cast<unsigned int>(cell->active_fe_index() + 1) <
2579 * ErrorIndicator::get_FECollection()->size())
2581 * cell->clear_refine_flag();
2582 * cell->set_active_fe_index(cell->active_fe_index() + 1);
2584 *
else if (cell->coarsen_flag_set() &&
2585 * smoothness_indicators[cell->active_cell_index()] <
2586 * threshold_smoothness &&
2587 * cell->active_fe_index() != 0)
2589 * cell->clear_coarsen_flag();
2591 * cell->set_active_fe_index(cell->active_fe_index() - 1);
2595 * Here we also impose a limit on how small the cells can become
2598 *
else if (cell->refine_flag_set() && cell->diameter() < 5.0e-6)
2600 * cell->clear_refine_flag();
2601 *
if (
static_cast<unsigned int>(cell->active_fe_index() + 1) <
2602 * ErrorIndicator::get_FECollection()->size())
2603 * cell->set_active_fe_index(cell->active_fe_index() + 1);
2610 * Check what the smallest
diameter is
2613 *
double min_diameter = std::numeric_limits<double>::max();
2614 *
for (
const auto &cell :
2615 * ErrorIndicator::get_DoFHandler()->active_cell_iterators())
2616 * if (cell->
diameter() < min_diameter)
2619 * std::cout <<
"Min diameter: " << min_diameter << std::endl;
2621 * ErrorIndicator::synchronize_discretization();
2623 * (this->
triangulation)->execute_coarsening_and_refinement();
2629 * main(
int argc,
char **argv)
2633 *
using namespace dealii;
2634 *
using namespace Maxwell;
2635 *
using namespace Refinement;
2636 *
using namespace ErrorIndicators;
2637 *
using namespace RegularityIndicators;
2645 * ExcMessage(
"This program can only be run in serial, use ./maxwell-hp"));
2648 * Structures::create_L_waveguide(triangulation_DWR, 2.0);
2649 * Structures::create_L_waveguide(triangulation_Kelly, 2.0);
2651 * Refiner<2, KellyErrorIndicator<2>, LegendreIndicator<2>> problem_Kelly(
2653 * triangulation_Kelly,
2658 * Refiner<2, DualWeightedResidual<2, false>, LegendreIndicator<2>>
2659 * problem_DWR(
"maxwell-hp.prm",
2660 * triangulation_DWR,
2667 * The threshold
for the
hp-decision: too small -> not enough
2668 * @f$h@f$-refinement, too large -> not enough @f$p@f$-refinement
2671 *
double smoothness_threshold = 0.75;
2673 * std::cout <<
"Executing refinement for the Kelly strategy!" << std::endl;
2674 * problem_Kelly.execute_refinement(smoothness_threshold);
2675 * std::cout <<
"...Done with Kelly refinement strategy!" << std::endl;
2676 * std::cout <<
"Executing refinement for the DWR strategy!" << std::endl;
2677 * problem_DWR.execute_refinement(smoothness_threshold);
2678 * std::cout <<
"...Done with DWR refinement strategy!" << std::endl;
2681 *
catch (std::exception &exc)
2683 * std::cerr << std::endl
2685 * <<
"----------------------------------------------------"
2687 * std::cerr <<
"Exception on processing: " << std::endl
2688 * << exc.what() << std::endl
2689 * <<
"Aborting!" << std::endl
2690 * <<
"----------------------------------------------------"
2697 * std::cerr << std::endl
2699 * <<
"----------------------------------------------------"
2701 * std::cerr <<
"Unknown exception!" << std::endl
2702 * <<
"Aborting!" << std::endl
2703 * <<
"----------------------------------------------------"
2708 * std::cout << std::endl <<
" Job done." << std::endl;
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 InputVector &fe_function, std::vector< Tensor< 1, spacedim, typename InputVector::value_type > > &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, typename InputVector::value_type > * > &neumann_bc, const InputVector &solution, Vector< float > &error, const ComponentMask &component_mask=ComponentMask(), 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)
__global__ void set(Number *val, const Number s, const size_type N)
#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_zero_boundary_constraints(const DoFHandler< dim, spacedim > &dof, const types::boundary_id boundary_id, AffineConstraints< number > &zero_boundary_constraints, const ComponentMask &component_mask=ComponentMask())
void make_hanging_node_constraints(const DoFHandler< dim, spacedim > &dof_handler, AffineConstraints< number > &constraints)
@ 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.
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)
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)
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 > &)
const ::parallel::distributed::Triangulation< dim, spacedim > * triangulation
void swap(SmartPointer< T, P > &t1, SmartPointer< T, Q > &t2)
std::array< Number, 1 > eigenvalues(const SymmetricTensor< 2, 1, Number > &T)
const ::Triangulation< dim, spacedim > & tria