Go to the documentation of this file.
   77 #include "femparameters.h" 
   78 #include "heatsystem.h" 
   81 #include "libmesh/equation_systems.h" 
   82 #include "libmesh/dirichlet_boundaries.h" 
   83 #include "libmesh/system_norm.h" 
   84 #include "libmesh/numeric_vector.h" 
   85 #include "libmesh/auto_ptr.h"  
   86 #include "libmesh/enum_solver_package.h" 
   88 #include "libmesh/mesh.h" 
   89 #include "libmesh/mesh_generation.h" 
   90 #include "libmesh/mesh_refinement.h" 
   92 #include "libmesh/petsc_diff_solver.h" 
   93 #include "libmesh/steady_solver.h" 
   94 #include "libmesh/euler_solver.h" 
   95 #include "libmesh/euler2_solver.h" 
   96 #include "libmesh/newton_solver.h" 
   97 #include "libmesh/twostep_time_solver.h" 
   99 #include "libmesh/getpot.h" 
  100 #include "libmesh/tecplot_io.h" 
  101 #include "libmesh/gmv_io.h" 
  102 #include "libmesh/exodusII_io.h" 
  105 #include "libmesh/solution_history.h" 
  106 #include "libmesh/memory_solution_history.h" 
  110 #include <sys/time.h> 
  115                   std::string solution_type, 
 
  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(),
 
  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;
 
  290 int main (
int argc, 
char ** argv)
 
  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 
  
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.
 
int main(int argc, char **argv)
 
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.