62#include "libmesh/equation_systems.h"
63#include "libmesh/linear_solver.h"
64#include "libmesh/error_vector.h"
65#include "libmesh/mesh.h"
66#include "libmesh/mesh_generation.h"
67#include "libmesh/mesh_modification.h"
68#include "libmesh/mesh_refinement.h"
69#include "libmesh/newton_solver.h"
70#include "libmesh/numeric_vector.h"
71#include "libmesh/petsc_diff_solver.h"
72#include "libmesh/steady_solver.h"
73#include "libmesh/system_norm.h"
74#include "libmesh/petsc_vector.h"
75#include "libmesh/enum_solver_package.h"
78#include "libmesh/qoi_set.h"
79#include "libmesh/adjoint_refinement_estimator.h"
82#include "libmesh/getpot.h"
83#include "libmesh/gmv_io.h"
86#include "femparameters.h"
100 std::string solution_type)
105#ifdef LIBMESH_HAVE_GMV
108 std::ostringstream file_name_gmv;
109 file_name_gmv << solution_type
117 (file_name_gmv.str(), es);
143 system.
time_solver = std::make_unique<SteadySolver>(system);
148#ifdef LIBMESH_HAVE_PETSC
149 system.
time_solver->diff_solver() = std::make_unique<PetscDiffSolver>(system);
151 libmesh_error_msg(
"This example requires libMesh to be compiled with PETSc support.");
156 system.
time_solver->diff_solver() = std::make_unique<NewtonSolver>(system);
157 auto solver = cast_ptr<NewtonSolver*>(system.
time_solver->diff_solver().get());
166 if (system.
time_solver->reduce_deltat_on_diffsolver_failure)
168 solver->continue_after_max_iterations =
true;
169 solver->continue_after_backtrack_failure =
true;
181#ifdef LIBMESH_ENABLE_AMR
187 auto mesh_refinement = std::make_unique<MeshRefinement>(
mesh);
188 mesh_refinement->coarsen_by_parents() =
true;
195 return mesh_refinement;
206 libMesh::out <<
"Computing the error estimate using the Adjoint Refinement Error Estimator\n" << std::endl;
208 auto adjoint_refinement_estimator = std::make_unique<AdjointRefinementEstimator>();
210 adjoint_refinement_estimator->qoi_set() = qois;
213 adjoint_refinement_estimator->number_h_refinements = 2;
215 return adjoint_refinement_estimator;
222int main (
int argc,
char ** argv)
229 "--enable-petsc, --enable-trilinos, or --enable-eigen");
232#ifndef LIBMESH_ENABLE_AMR
233 libmesh_example_requires(
false,
"--enable-amr");
237#ifndef LIBMESH_ENABLE_DIRICHLET
238 libmesh_example_requires(
false,
"--enable-dirichlet");
245 std::ifstream i(
"general.in");
246 libmesh_error_msg_if(!i,
'[' << init.comm().rank() <<
"] Can't find general.in; exiting early.");
250 GetPot infile(
"general.in");
253 infile.parse_command_line(argc, argv);
259 libmesh_example_requires(2 <= LIBMESH_DIM,
"2D support");
266 std::unique_ptr<MeshRefinement> mesh_refinement =
272 libMesh::out <<
"Reading in and building the mesh" << std::endl;
300 equation_systems.
init ();
311 unsigned int a_step = 0;
363 primal_solution.
swap(dual_solution_0);
367 primal_solution.
swap(dual_solution_0);
370 <<
" active elements and "
372 <<
" active dofs." << std::endl ;
382 libMesh::out <<
"The computed QoI 0 is " << std::setprecision(17)
383 << QoI_0_computed << std::endl;
384 libMesh::out <<
"The relative error in QoI 0 is " << std::setprecision(17)
385 << std::abs(QoI_0_computed - QoI_0_exact) << std::endl;
391 std::unique_ptr<AdjointRefinementEstimator> adjoint_refinement_error_estimator =
395 adjoint_refinement_error_estimator->estimate_error(system, QoI_elementwise_error);
399 libMesh::out <<
"The computed relative error in QoI 0 is " << std::setprecision(17)
400 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) << std::endl;
403 libMesh::out <<
"The effectivity index for the computed error in QoI 0 is " << std::setprecision(17)
404 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) /
405 std::abs(QoI_0_computed - QoI_0_exact) << std::endl;
410 for (std::size_t i=0; i<QoI_elementwise_error.size(); i++)
411 if (QoI_elementwise_error[i] != 0.)
412 QoI_elementwise_error[i] = std::abs(QoI_elementwise_error[i]);
421 mesh_refinement->uniformly_refine(1);
426 mesh_refinement->flag_elements_by_error_tolerance (QoI_elementwise_error);
428 mesh_refinement->refine_and_coarsen_elements();
435 libMesh::out <<
"We reached the target number of elements.\n" << std::endl;
439 mesh_refinement->flag_elements_by_nelem_target (QoI_elementwise_error);
441 mesh_refinement->refine_and_coarsen_elements();
445 equation_systems.
reinit();
449 <<
" active elements and "
451 <<
" active dofs." << std::endl;
465 std::vector<unsigned int> qoi_indices;
467 qoi_indices.push_back(0);
480 primal_solution.
swap(dual_solution_0);
483 primal_solution.
swap(dual_solution_0);
486 <<
" active elements and "
488 <<
" active dofs." << std::endl ;
497 libMesh::out <<
"The computed QoI 0 is " << std::setprecision(17)
498 << QoI_0_computed << std::endl;
499 libMesh::out <<
"The relative error in QoI 0 is " << std::setprecision(17)
500 << std::abs(QoI_0_computed - QoI_0_exact) << std::endl;
512 std::unique_ptr<AdjointRefinementEstimator> adjoint_refinement_error_estimator =
516 adjoint_refinement_error_estimator->estimate_error(system, QoI_elementwise_error);
521 libMesh::out <<
"The computed relative error in QoI 0 is " << std::setprecision(17)
522 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) << std::endl;
525 libMesh::out <<
"The effectivity index for the computed error in QoI 0 is " << std::setprecision(17)
526 << std::abs(adjoint_refinement_error_estimator->get_global_QoI_error_estimate(0)) /
527 std::abs(QoI_0_computed - QoI_0_exact) << std::endl;
548 <<
"] Completing output." << std::endl;
std::unique_ptr< AdjointRefinementEstimator > build_adjoint_refinement_error_estimator(QoISet &qois)
void set_system_parameters(PoissonSystem &system, FEMParameters ¶m)
void write_output(EquationSystems &es, unsigned int a_step, std::string solution_type)
std::unique_ptr< MeshRefinement > build_mesh_refinement(MeshBase &mesh, FEMParameters ¶m)
double minimum_linear_tolerance
double initial_linear_tolerance
unsigned int max_adaptivesteps
double linear_tolerance_multiplier
libMesh::Real refine_fraction
unsigned int coarserefinements
libMesh::Real verify_analytic_jacobians
unsigned int max_nonlinear_iterations
libMesh::Real global_tolerance
libMesh::Real min_step_length
std::vector< std::string > fe_family
libMesh::Real relative_residual_tolerance
bool print_residual_norms
bool print_jacobian_norms
void read(GetPot &input, const std::vector< std::string > *other_variable_names=nullptr)
libMesh::Real relative_step_tolerance
unsigned int max_linear_iterations
bool require_residual_reduction
libMesh::Real coarsen_fraction
unsigned int nelem_target
std::vector< unsigned int > fe_order
libMesh::Real coarsen_threshold
bool print_solution_norms
bool reuse_preconditioner
bool & analytic_jacobians()
unsigned int & fe_order()
virtual void postprocess(void)
Runs a postprocessing loop over all elements, and if postprocess_sides is true over all sides.
std::string & fe_family()
Number & get_QoI_value(std::string type, unsigned int QoI_index)
bool assemble_qoi_sides
If assemble_qoi_sides is true (it is false by default), the assembly loop for a quantity of interest ...
bool print_jacobians
Set print_jacobians to true to print J whenever it is assembled.
bool postprocess_sides
If postprocess_sides is true (it is false by default), the postprocessing loop will loop over all sid...
bool print_residuals
Set print_residuals to true to print F whenever it is assembled.
virtual void set_constrain_in_solver(bool enable)
set_constrain_in_solver to false to apply constraints only via residual terms in the systems to be so...
bool print_solution_norms
Set print_residual_norms to true to print |U| whenever it is used in an assembly() call.
virtual LinearSolver< Number > * get_linear_solver() const override
bool print_solutions
Set print_solutions to true to print U whenever it is used in an assembly() call.
bool print_residual_norms
Set print_residual_norms to true to print |F| whenever it is assembled.
virtual std::pair< unsigned int, Real > adjoint_solve(const QoISet &qoi_indices=QoISet()) override
This function sets the _is_adjoint boolean member of TimeSolver to true and then calls the adjoint_so...
bool print_jacobian_norms
Set print_jacobian_norms to true to print |J| whenever it is assembled.
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
This is the EquationSystems class.
virtual void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
std::size_t n_active_dofs() const
const MeshBase & get_mesh() const
virtual void init()
Initialize all the systems.
virtual System & add_system(std::string_view system_type, std::string_view name)
Add the system of type system_type named name to the systems array.
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
virtual void solve() override
Invokes the solver associated with the system.
Real verify_analytic_jacobians
If verify_analytic_jacobian is equal to zero (as it is by default), no numeric jacobians will be calc...
This class implements writing meshes in the GMV format.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
This base class can be inherited from to provide interfaces to linear solvers from different packages...
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
This is the MeshBase class.
void all_second_order(const bool full_ordered=true)
Calls the range-based version of this function with a range consisting of all elements in the mesh.
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
virtual dof_id_type n_active_elem() const =0
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
Implements (adaptive) mesh refinement algorithms for a MeshBase.
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
processor_id_type processor_id() const
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
void add_indices(const std::vector< unsigned int > &indices)
Add this indices to the set to be calculated.
void set_weight(std::size_t, Real)
Set the weight for this index.
void set_adjoint_already_solved(bool setting)
Setter for the adjoint_already_solved boolean.
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
NumericVector< Number > & get_adjoint_solution(unsigned int i=0)
The libMesh namespace provides an interface to certain functionality in the library.
void libmesh_ignore(const Args &...)
SolverPackage default_solver_package()