61 #include "libmesh/equation_systems.h" 62 #include "libmesh/error_vector.h" 63 #include "libmesh/factory.h" 64 #include "libmesh/linear_solver.h" 65 #include "libmesh/mesh.h" 66 #include "libmesh/mesh_refinement.h" 67 #include "libmesh/newton_solver.h" 68 #include "libmesh/numeric_vector.h" 69 #include "libmesh/partitioner.h" 70 #include "libmesh/petsc_diff_solver.h" 71 #include "libmesh/steady_solver.h" 72 #include "libmesh/system_norm.h" 73 #include "libmesh/enum_solver_package.h" 76 #include "libmesh/kelly_error_estimator.h" 77 #include "libmesh/patch_recovery_error_estimator.h" 80 #include "libmesh/adjoint_residual_error_estimator.h" 81 #include "libmesh/qoi_set.h" 84 #include "libmesh/getpot.h" 85 #include "libmesh/gmv_io.h" 86 #include "libmesh/exodusII_io.h" 89 #include "femparameters.h" 104 std::string solution_type,
110 #ifdef LIBMESH_HAVE_GMV 115 std::ostringstream file_name_gmv;
116 file_name_gmv << solution_type
124 (file_name_gmv.str(), es);
128 #ifdef LIBMESH_HAVE_EXODUS_API 141 std::ostringstream file_name_exodus;
143 file_name_exodus << solution_type <<
".e";
145 file_name_exodus <<
"-s" 185 system.
time_solver = std::make_unique<SteadySolver>(system);
190 #ifdef LIBMESH_HAVE_PETSC 191 system.
time_solver->diff_solver() = std::make_unique<PetscDiffSolver>(system);
193 libmesh_error_msg(
"This example requires libMesh to be compiled with PETSc support.");
198 system.
time_solver->diff_solver() = std::make_unique<NewtonSolver>(system);
199 auto solver = cast_ptr<NewtonSolver*>(system.
time_solver->diff_solver().get());
209 if (system.
time_solver->reduce_deltat_on_diffsolver_failure)
211 solver->continue_after_max_iterations =
true;
212 solver->continue_after_backtrack_failure =
true;
225 #ifdef LIBMESH_ENABLE_AMR 230 auto mesh_refinement = std::make_unique<MeshRefinement>(
mesh);
231 mesh_refinement->coarsen_by_parents() =
true;
238 return mesh_refinement;
241 #endif // LIBMESH_ENABLE_AMR 255 libMesh::out <<
"Using Kelly Error Estimator" << std::endl;
257 return std::make_unique<KellyErrorEstimator>();
261 libMesh::out <<
"Using Adjoint Residual Error Estimator with Patch Recovery Weights" << std::endl;
263 auto adjoint_residual_estimator = std::make_unique<AdjointResidualErrorEstimator>();
265 adjoint_residual_estimator->qoi_set() = qois;
266 adjoint_residual_estimator->error_plot_suffix =
"error.gmv";
268 adjoint_residual_estimator->primal_error_estimator() = std::make_unique<PatchRecoveryErrorEstimator>();
269 auto p1 = cast_ptr<PatchRecoveryErrorEstimator *>(adjoint_residual_estimator->primal_error_estimator().get());
273 adjoint_residual_estimator->dual_error_estimator() = std::make_unique<PatchRecoveryErrorEstimator>();
274 auto p2 = cast_ptr<PatchRecoveryErrorEstimator *>(adjoint_residual_estimator->dual_error_estimator().get());
278 return adjoint_residual_estimator;
281 libmesh_error_msg(
"Unknown indicator_type = " << param.
indicator_type);
285 int main (
int argc,
char ** argv)
292 "--enable-petsc, --enable-trilinos, or --enable-eigen");
295 #ifndef LIBMESH_ENABLE_EXCEPTIONS 296 libmesh_example_requires(
false,
"--enable-exceptions");
300 #ifndef LIBMESH_ENABLE_AMR 301 libmesh_example_requires(
false,
"--enable-amr");
308 std::ifstream i(
"general.in");
309 libmesh_error_msg_if(!i,
'[' <<
init.comm().rank() <<
"] Can't find general.in; exiting early.");
313 GetPot infile(
"general.in");
316 infile.parse_command_line(argc, argv);
322 libmesh_example_requires(2 <= LIBMESH_DIM,
"2D support");
330 if (param.mesh_partitioner_type !=
"Default")
332 #if !defined(LIBMESH_HAVE_RTTI) || !defined(LIBMESH_ENABLE_EXCEPTIONS) 333 libmesh_example_requires(
false,
"RTTI + exceptions support");
345 (param.mesh_partitioner_type ==
"Centroid" ||
346 param.mesh_partitioner_type ==
"Hilbert" ||
347 param.mesh_partitioner_type ==
"Morton" ||
348 param.mesh_partitioner_type ==
"SFCurves" ||
349 param.mesh_partitioner_type ==
"Metis"))
350 libmesh_example_requires(
false,
"--disable-parmesh");
357 libmesh_example_requires(
false, param.mesh_partitioner_type +
" partitioner support");
360 #endif // LIBMESH_HAVE_RTTI, LIBMESH_ENABLE_EXCEPTIONS 364 std::unique_ptr<MeshRefinement> mesh_refinement =
370 libMesh::out <<
"Reading in and building the mesh" << std::endl;
373 mesh.
read(param.domainfile.c_str());
393 equation_systems.
init ();
402 unsigned int a_step = 0;
403 for (; a_step != param.max_adaptivesteps; ++a_step)
407 if (param.global_tolerance != 0.)
408 libmesh_assert_equal_to (param.nelem_target, 0);
412 libmesh_assert_greater (param.nelem_target, 0);
420 write_output(equation_systems, a_step,
"primal", param);
454 primal_solution.
swap(dual_solution_0);
455 write_output(equation_systems, a_step,
"adjoint_0", param);
458 primal_solution.
swap(dual_solution_0);
464 primal_solution.
swap(dual_solution_1);
465 write_output(equation_systems, a_step,
"adjoint_1", param);
468 primal_solution.
swap(dual_solution_1);
474 <<
" active elements and " 489 << std::setprecision(17)
494 << std::setprecision(17)
503 std::unique_ptr<ErrorEstimator> error_estimator =
507 error_estimator->estimate_error(system, error);
514 if (param.refine_uniformly)
516 mesh_refinement->uniformly_refine(1);
519 else if (param.global_tolerance >= 0. && param.nelem_target == 0.)
521 mesh_refinement->flag_elements_by_error_tolerance (error);
523 mesh_refinement->refine_and_coarsen_elements();
530 libMesh::out <<
"We reached the target number of elements." << std::endl << std::endl;
534 mesh_refinement->flag_elements_by_nelem_target (error);
536 mesh_refinement->refine_and_coarsen_elements();
540 equation_systems.
reinit();
544 <<
" active elements and " 551 if (a_step == param.max_adaptivesteps)
556 write_output(equation_systems, a_step,
"primal", param);
561 std::vector<unsigned int> qoi_indices;
563 qoi_indices.push_back(0);
564 qoi_indices.push_back(1);
579 primal_solution.
swap(dual_solution_0);
580 write_output(equation_systems, a_step,
"adjoint_0", param);
582 primal_solution.
swap(dual_solution_0);
586 primal_solution.
swap(dual_solution_1);
587 write_output(equation_systems, a_step,
"adjoint_1", param);
589 primal_solution.
swap(dual_solution_1);
595 <<
" active elements and " 610 << std::setprecision(17)
615 << std::setprecision(17)
621 if (param.max_adaptivesteps > 5 && param.coarserefinements > 2)
623 libmesh_assert_less(
std::abs(QoI_0_computed - QoI_0_exact)/
std::abs(QoI_0_exact), 4.e-5);
624 libmesh_assert_less(
std::abs(QoI_1_computed - QoI_1_exact)/
std::abs(QoI_1_exact), 1.e-4);
630 libmesh_assert_less(
std::abs(QoI_0_computed - QoI_0_exact)/
std::abs(QoI_0_exact), 4.e-4);
631 libmesh_assert_less(
std::abs(QoI_1_computed - QoI_1_exact)/
std::abs(QoI_1_exact), 2.e-3);
637 <<
"] Completing output." 640 #endif // #ifndef LIBMESH_ENABLE_AMR unsigned int nelem_target
bool print_solution_norms
This is the EquationSystems class.
virtual void read(const std::string &name, void *mesh_data=nullptr, bool skip_renumber_nodes_and_elements=false, bool skip_find_neighbors=false)=0
Interfaces for reading/writing a mesh to/from a file.
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 write_output(EquationSystems &es, unsigned int a_step, std::string solution_type, FEMParameters ¶m)
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...
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Number & get_QoI_value(std::string type, unsigned int QoI_index)
std::string & fe_family()
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.
bool print_residual_norms
virtual std::unique_ptr< Partitioner > & partitioner()
A partitioner to use at each prepare_for_use()
libMesh::Real relative_step_tolerance
std::string indicator_type
bool & analytic_jacobians()
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
void set_system_parameters(LaplaceSystem &system, FEMParameters ¶m)
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
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
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 &...)
int main(int argc, char **argv)
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
std::unique_ptr< ErrorEstimator > build_error_estimator(const FEMParameters ¶m, const QoISet &qois)
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
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::unique_ptr< MeshRefinement > build_mesh_refinement(MeshBase &mesh, const FEMParameters ¶m)
streambufT * rdbuf() const
Get the associated stream buffer.
bool print_solutions
Set print_solutions to true to print U whenever it is used in an assembly() call. ...
libMesh::Real relative_residual_tolerance
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 write_timestep(const std::string &fname, const EquationSystems &es, const int timestep, const Real time, const std::set< std::string > *system_names=nullptr)
Writes out the solution at a specific timestep.
virtual bool is_replicated() const
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.
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 Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
processor_id_type processor_id() const
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...
static std::unique_ptr< Base > build(const std::string &name)
Builds an object of type Base identified by name.
virtual void postprocess()
Runs a postprocessing loop over all elements, and if postprocess_sides is true over all sides...
libMesh::Real coarsen_fraction
void uniformly_refine(unsigned int n=1)
Uniformly refines the mesh n times.