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;
218 #endif // LIBMESH_ENABLE_AMR 222 int 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;
312 for (; a_step != param.max_adaptivesteps; ++a_step)
316 if (param.global_tolerance != 0.)
317 libmesh_assert_equal_to (param.nelem_target, 0);
321 libmesh_assert_greater (param.nelem_target, 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;
409 if (!param.refine_uniformly)
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]);
419 if (param.refine_uniformly)
421 mesh_refinement->uniformly_refine(1);
424 else if (param.global_tolerance >= 0. && param.nelem_target == 0.)
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;
455 if (a_step == param.max_adaptivesteps)
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;
550 #endif // #ifndef LIBMESH_ENABLE_AMR unsigned int nelem_target
bool print_solution_norms
This is the EquationSystems class.
void set_weight(std::size_t, Real)
Set the weight for this index.
virtual dof_id_type n_active_elem() const =0
Real verify_analytic_jacobians
If verify_analytic_jacobian is equal to zero (as it is by default), no numeric jacobians will be calc...
void set_adjoint_already_solved(bool setting)
Setter for the adjoint_already_solved boolean.
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 ...
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...
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
bool print_jacobian_norms
Set print_jacobian_norms to true to print |J| whenever it is assembled.
The ErrorVector is a specialization of the StatisticsVector for error data computed on a finite eleme...
bool require_residual_reduction
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
virtual void postprocess(void)
Runs a postprocessing loop over all elements, and if postprocess_sides is true over all sides...
bool print_residual_norms
libMesh::Real relative_step_tolerance
bool postprocess_sides
If postprocess_sides is true (it is false by default), the postprocessing loop will loop over all sid...
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
The libMesh namespace provides an interface to certain functionality in the library.
This class implements writing meshes in the GMV format.
libMesh::Real refine_fraction
bool print_jacobians
Set print_jacobians to true to print J whenever it is assembled.
This is the MeshBase class.
std::vector< unsigned int > fe_order
SolverPackage default_solver_package()
libMesh::Real coarsen_threshold
bool print_solution_norms
Set print_residual_norms to true to print |U| whenever it is used in an assembly() call...
void libmesh_ignore(const Args &...)
Implements (adaptive) mesh refinement algorithms for a MeshBase.
unsigned int max_linear_iterations
bool print_residual_norms
Set print_residual_norms to true to print |F| whenever it is assembled.
double minimum_linear_tolerance
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 void reinit()
Handle any mesh changes and reinitialize all the systems on the updated mesh.
std::vector< std::string > fe_family
void write_output(EquationSystems &es, unsigned int a_step, std::string solution_type)
libMesh::Real verify_analytic_jacobians
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
bool print_residuals
Set print_residuals to true to print F whenever it is assembled.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
double initial_linear_tolerance
libMesh::Real global_tolerance
void read(GetPot &input, const std::vector< std::string > *other_variable_names=nullptr)
std::string & fe_family()
bool print_solutions
Set print_solutions to true to print U whenever it is used in an assembly() call. ...
libMesh::Real relative_residual_tolerance
std::unique_ptr< MeshRefinement > build_mesh_refinement(MeshBase &mesh, FEMParameters ¶m)
unsigned int & fe_order()
virtual LinearSolver< Number > * get_linear_solver() const override
virtual void reuse_preconditioner(bool)
Set the same_preconditioner flag, which indicates if we reuse the same preconditioner for subsequent ...
bool print_jacobian_norms
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
bool assemble_qoi_sides
If assemble_qoi_sides is true (it is false by default), the assembly loop for a quantity of interest ...
void add_indices(const std::vector< unsigned int > &indices)
Add this indices to the set to be calculated.
const MeshBase & get_mesh() const
libMesh::Real min_step_length
std::size_t n_active_dofs() const
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...
NumericVector< Number > & get_adjoint_solution(unsigned int i=0)
unsigned int max_nonlinear_iterations
double linear_tolerance_multiplier
virtual void solve() override
Invokes the solver associated with the system.
bool & analytic_jacobians()
virtual void init()
Initialize all the systems.
void set_system_parameters(PoissonSystem &system, FEMParameters ¶m)
std::unique_ptr< AdjointRefinementEstimator > build_adjoint_refinement_error_estimator(QoISet &qois)
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 Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
processor_id_type processor_id() const
int main(int argc, char **argv)
Number & get_QoI_value(std::string type, unsigned int QoI_index)
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...
libMesh::Real coarsen_fraction
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.