Go to the source code of this file.
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
Definition at line 290 of file adjoints_ex5.C.
293 #ifndef LIBMESH_ENABLE_AMR
295 libmesh_example_requires(
false,
"--enable-amr");
298 libmesh_example_requires(2 <= LIBMESH_DIM,
"2D support");
301 #ifndef LIBMESH_ENABLE_DIRICHLET
302 libmesh_example_requires(
false,
"--enable-dirichlet");
306 LibMeshInit
init (argc, argv);
315 std::ifstream i(
"general.in");
317 libmesh_error_msg(
'[' <<
init.comm().rank() <<
"] Can't find general.in; exiting early.");
319 GetPot infile(
"general.in");
327 Mesh
mesh(
init.comm(), cast_int<unsigned char>(param.dimension));
330 auto mesh_refinement = libmesh_make_unique<MeshRefinement>(
mesh);
333 EquationSystems equation_systems (
mesh);
340 if (param.elementtype ==
"tri" ||
341 param.elementtype ==
"unstructured")
347 param.domain_xmin, param.domain_xmin + param.domain_edge_width,
348 param.domain_ymin, param.domain_ymin + param.domain_edge_length,
360 equation_systems.init ();
363 for (
unsigned int i=0; i != param.extrarefinements; ++i)
365 mesh_refinement->uniformly_refine(1);
366 equation_systems.reinit();
369 libMesh::out <<
"Setting primal initial conditions" << std::endl;
374 equation_systems.parameters);
395 const std::string & adjoint_solution_name =
"adjoint_solution0";
406 equation_systems.print_info();
417 for (
unsigned int t_step=param.initial_timestep;
418 t_step != param.initial_timestep + param.n_timesteps; ++t_step)
438 libMesh::out <<
"Advancing timestep" << std::endl << std::endl;
442 write_output(equation_systems, t_step+1,
"primal", param);
449 libMesh::out << std::endl <<
"Solving the adjoint problem" << std::endl;
485 equation_systems.parameters,
500 write_output(equation_systems, param.n_timesteps,
"dual", param);
506 for (
unsigned int t_step=param.initial_timestep;
507 t_step != param.initial_timestep + param.n_timesteps; ++t_step)
521 libMesh::out <<
"Retrieving solutions at time t=" << system.
time << std::endl;
554 NumericVector<Number> & primal_solution = *system.
solution;
560 primal_solution.
swap(dual_solution_0);
562 write_output(equation_systems, param.n_timesteps - (t_step + 1),
"dual", param);
565 primal_solution.swap(dual_solution_0);
579 for (
unsigned int t_step=param.initial_timestep;
580 t_step != param.initial_timestep + param.n_timesteps; ++t_step)
613 dynamic_cast<HeatSystem &>(system).perturb_accumulate_residuals(dynamic_cast<HeatSystem &>(system).get_parameter_vector());
648 dynamic_cast<HeatSystem &>(system).perturb_accumulate_residuals(dynamic_cast<HeatSystem &>(system).get_parameter_vector());
652 Number sensitivity_0_0 = (dynamic_cast<HeatSystem &>(system)).compute_final_sensitivity();
655 libMesh::out <<
"Sensitivity of QoI 0 w.r.t parameter 0 is: "
663 if(
std::abs(sensitivity_0_0 - (-5.37173)) >= 2.e-4)
664 libmesh_error_msg(
"Mismatch in sensitivity gold value!");
671 <<
"] Caught exception; exiting early." << std::endl;
676 <<
"] Completing output."
682 #endif // LIBMESH_ENABLE_AMR
References std::abs(), libMesh::System::add_vector(), adjoint_initial_grad(), adjoint_initial_value(), libMesh::DifferentiableSystem::adjoint_solve(), libMesh::MeshTools::Generation::build_square(), libMesh::System::calculate_norm(), libMesh::default_solver_package(), libMesh::DifferentiableSystem::deltat, libMesh::err, finish_initialization(), libMesh::System::get_adjoint_solution(), libMesh::System::get_vector(), libMesh::GHOSTED, libMesh::H1, libMesh::TriangleWrapper::init(), initial_grad(), initial_value(), libMesh::libmesh_ignore(), mesh, libMesh::out, libMesh::PETSC_SOLVERS, libMesh::System::project_solution(), libMesh::System::project_vector(), libMesh::QUAD4, FEMParameters::read(), read_initial_parameters(), libMesh::System::set_adjoint_already_solved(), set_system_parameters(), libMesh::System::set_vector_preservation(), libMesh::System::solution, libMesh::FEMSystem::solve(), libMesh::NumericVector< T >::swap(), libMesh::System::time, libMesh::DifferentiableSystem::time_solver, libMesh::TRI3, and write_output().
◆ set_system_parameters()
Definition at line 170 of file adjoints_ex5.C.
197 UnsteadySolver *innersolver;
200 EulerSolver *eulersolver =
201 new EulerSolver(system);
204 innersolver = eulersolver;
207 libmesh_error_msg(
"This example (and unsteady adjoints in libMesh) only support Backward Euler and explicit methods.");
210 std::unique_ptr<TimeSolver>(innersolver);
213 system.
time_solver = libmesh_make_unique<SteadySolver>(system);
216 MemorySolutionHistory heatsystem_solution_history(system);
217 system.
time_solver->set_solution_history(heatsystem_solution_history);
219 system.
time_solver->reduce_deltat_on_diffsolver_failure =
223 #ifdef LIBMESH_ENABLE_DIRICHLET
226 std::map<boundary_id_type, FunctionBase<Number> *>::
233 FunctionBase<Number> *f = i->second;
234 std::set<boundary_id_type> bdys; bdys.insert(b);
240 libMesh::out <<
"Added Dirichlet boundary " << b <<
" for variables ";
245 #endif // LIBMESH_ENABLE_DIRICHLET
256 #ifdef LIBMESH_HAVE_PETSC
257 PetscDiffSolver *solver =
new PetscDiffSolver(system);
258 system.
time_solver->diff_solver() = std::unique_ptr<DiffSolver>(solver);
260 libmesh_error_msg(
"This example requires libMesh to be compiled with PETSc support.");
265 NewtonSolver *solver =
new NewtonSolver(system);
266 system.
time_solver->diff_solver() = std::unique_ptr<DiffSolver>(solver);
276 if (system.
time_solver->reduce_deltat_on_diffsolver_failure)
278 solver->continue_after_max_iterations =
true;
279 solver->continue_after_backtrack_failure =
true;
References libMesh::DofMap::add_dirichlet_boundary(), HeatSystem::analytic_jacobians(), FEMParameters::analytic_jacobians, FEMParameters::deltat, libMesh::DifferentiableSystem::deltat, FEMParameters::deltat_reductions, FEMParameters::dirichlet_condition_variables, FEMParameters::dirichlet_conditions, FEMParameters::extra_quadrature_order, libMesh::System::extra_quadrature_order, HeatSystem::fe_family(), FEMParameters::fe_family, HeatSystem::fe_order(), FEMParameters::fe_order, libMesh::System::get_dof_map(), FEMParameters::initial_linear_tolerance, FEMParameters::linear_tolerance_multiplier, FEMParameters::max_linear_iterations, FEMParameters::max_nonlinear_iterations, FEMParameters::min_step_length, FEMParameters::minimum_linear_tolerance, FEMParameters::numerical_jacobian_h, libMesh::FEMSystem::numerical_jacobian_h, libMesh::out, FEMParameters::print_element_jacobians, libMesh::DifferentiableSystem::print_element_jacobians, FEMParameters::print_element_residuals, libMesh::DifferentiableSystem::print_element_residuals, FEMParameters::print_jacobian_norms, libMesh::DifferentiableSystem::print_jacobian_norms, FEMParameters::print_jacobians, libMesh::DifferentiableSystem::print_jacobians, FEMParameters::print_residual_norms, libMesh::DifferentiableSystem::print_residual_norms, FEMParameters::print_residuals, libMesh::DifferentiableSystem::print_residuals, FEMParameters::print_solution_norms, libMesh::DifferentiableSystem::print_solution_norms, FEMParameters::print_solutions, libMesh::DifferentiableSystem::print_solutions, FEMParameters::relative_residual_tolerance, FEMParameters::relative_step_tolerance, FEMParameters::require_residual_reduction, FEMParameters::solver_quiet, FEMParameters::solver_verbose, libMesh::DifferentiableSystem::time_solver, FEMParameters::time_solver_quiet, FEMParameters::timesolver_core, FEMParameters::timesolver_theta, FEMParameters::transient, FEMParameters::use_petsc_snes, FEMParameters::verify_analytic_jacobians, and libMesh::FEMSystem::verify_analytic_jacobians.
Referenced by main().
◆ write_output()
void write_output |
( |
EquationSystems & |
es, |
|
|
unsigned int |
t_step, |
|
|
std::string |
solution_type, |
|
|
FEMParameters & |
param |
|
) |
| |
Definition at line 113 of file adjoints_ex5.C.
121 #ifdef LIBMESH_HAVE_GMV
124 MeshBase &
mesh = es.get_mesh();
126 std::ostringstream file_name_gmv;
127 file_name_gmv << solution_type
134 GMVIO(
mesh).write_equation_systems(file_name_gmv.str(), es);
138 #ifdef LIBMESH_HAVE_EXODUS_API
141 MeshBase &
mesh = es.get_mesh();
151 std::ostringstream file_name_exodus;
153 file_name_exodus << solution_type <<
".e";
155 file_name_exodus <<
"-s"
162 ExodusII_IO(
mesh).write_timestep(file_name_exodus.str(),
References libMesh::libmesh_ignore(), mesh, FEMParameters::output_exodus, and FEMParameters::output_gmv.
Referenced by main().
void set_vector_preservation(const std::string &vec_name, bool preserve)
Allows one to set the boolean controlling whether the vector identified by vec_name should be "preser...
Real deltat
For time-dependent problems, this is the amount delta t to advance the solution in time.
std::map< libMesh::boundary_id_type, libMesh::FunctionBase< libMesh::Number > * > dirichlet_conditions
virtual void solve() override
Invokes the solver associated with the system.
void project_vector(NumericVector< Number > &new_vector, FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr, int is_adjoint=-1) const
Projects arbitrary functions onto a vector of degree of freedom values for the current system.
bool print_jacobian_norms
Set print_jacobian_norms to true to print |J| whenever it is assembled.
bool print_element_jacobians
Set print_element_jacobians to true to print each J_elem contribution.
bool print_residual_norms
Set print_residual_norms to true to print |F| whenever it is assembled.
unsigned int max_nonlinear_iterations
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
virtual void swap(NumericVector< T > &v)
Swaps the contents of this with v.
bool print_solution_norms
bool print_residual_norms
void finish_initialization()
void write_output(EquationSystems &es, unsigned int t_step, std::string solution_type, FEMParameters ¶m)
bool print_residuals
Set print_residuals to true to print F whenever it is assembled.
NumericVector< Number > & get_adjoint_solution(unsigned int i=0)
int extra_quadrature_order
A member int that can be employed to indicate increased or reduced quadrature order.
bool & analytic_jacobians()
bool print_solution_norms
Set print_residual_norms to true to print |U| whenever it is used in an assembly() call.
SolverPackage default_solver_package()
double initial_linear_tolerance
std::vector< unsigned int > fe_order
std::string timesolver_core
bool require_residual_reduction
unsigned int & fe_order()
unsigned int max_linear_iterations
std::unique_ptr< TimeSolver > time_solver
A pointer to the solver object we're going to use.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
Real numerical_jacobian_h
If calculating numeric jacobians is required, the FEMSystem will perturb each solution vector entry b...
std::vector< std::string > fe_family
std::string & fe_family()
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
Number adjoint_initial_value(const Point &p, const Parameters &, const std::string &, const std::string &)
void libmesh_ignore(const Args &...)
bool print_jacobians
Set print_jacobians to true to print J whenever it is assembled.
bool print_element_jacobians
libMesh::Real relative_residual_tolerance
libMesh::Real relative_step_tolerance
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...
double minimum_linear_tolerance
void set_system_parameters(HeatSystem &system, FEMParameters ¶m)
Gradient adjoint_initial_grad(const Point &p, const Parameters &, const std::string &, const std::string &)
Real time
For time-dependent problems, this is the time t at the beginning of the current timestep.
libMesh::Real timesolver_theta
std::unique_ptr< NumericVector< Number > > solution
Data structure to hold solution values.
libMesh::Real min_step_length
void read(GetPot &input, const std::vector< std::string > *other_variable_names=nullptr)
int extra_quadrature_order
Real verify_analytic_jacobians
If verify_analytic_jacobian is equal to zero (as it is by default), no numeric jacobians will be calc...
Gradient initial_grad(const Point &, const Parameters &, const std::string &, const std::string &)
libMesh::Real numerical_jacobian_h
Real calculate_norm(const NumericVector< Number > &v, unsigned int var, FEMNormType norm_type, std::set< unsigned int > *skip_dimensions=nullptr) const
bool print_solutions
Set print_solutions to true to print U whenever it is used in an assembly() call.
NumericVector< Number > & add_vector(const std::string &vec_name, const bool projections=true, const ParallelType type=PARALLEL)
Adds the additional vector vec_name to this system.
libMesh::Real verify_analytic_jacobians
bool print_jacobian_norms
const DofMap & get_dof_map() const
unsigned int deltat_reductions
double linear_tolerance_multiplier
void project_solution(FunctionBase< Number > *f, FunctionBase< Gradient > *g=nullptr) const
Projects arbitrary functions onto the current solution.
std::map< libMesh::boundary_id_type, std::vector< unsigned int > > dirichlet_condition_variables
Number initial_value(const Point &, const Parameters &, const std::string &, const std::string &)
bool print_element_residuals
Set print_element_residuals to true to print each R_elem contribution.
const NumericVector< Number > & get_vector(const std::string &vec_name) const
void read_initial_parameters()
bool print_element_residuals
ElemType
Defines an enum for geometric element types.
void set_adjoint_already_solved(bool setting)
Setter for the adjoint_already_solved boolean.