libMesh
Classes | Functions
optimization_ex1.C File Reference

Go to the source code of this file.

Classes

class  AssembleOptimization
 This class encapsulate all functionality required for assembling the objective function, gradient, and hessian. More...
 

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 241 of file optimization_ex1.C.

References AssembleOptimization::A_matrix, libMesh::DofMap::add_dirichlet_boundary(), libMesh::System::add_matrix(), libMesh::EquationSystems::add_system(), libMesh::System::add_variable(), libMesh::System::add_vector(), AssembleOptimization::assemble_A_and_F(), libMesh::MeshTools::Generation::build_square(), libMesh::SparseMatrix< T >::close(), libMesh::err, AssembleOptimization::F_vector, libMesh::System::get_dof_map(), libMesh::System::get_matrix(), libMesh::ImplicitSystem::get_system_matrix(), libMesh::System::get_vector(), libMesh::TriangleWrapper::init(), libMesh::EquationSystems::init(), libMesh::LOCAL_VARIABLE_ORDER, mesh, libMesh::MeshTools::n_elem(), libMesh::on_command_line(), libMesh::OptimizationSystem::optimization_solver, libMesh::EquationSystems::print_info(), libMesh::MeshBase::print_info(), libMesh::QUAD9, libMesh::OptimizationSystem::solve(), and libMesh::ExodusII_IO::write_equation_systems().

242 {
243  LibMeshInit init (argc, argv);
244 
245 #ifndef LIBMESH_HAVE_PETSC_TAO
246 
247  libmesh_example_requires(false, "PETSc >= 3.5.0 with built-in TAO support");
248 
249 #elif !defined(LIBMESH_ENABLE_GHOSTED)
250 
251  libmesh_example_requires(false, "--enable-ghosted");
252 
253 #elif LIBMESH_USE_COMPLEX_NUMBERS
254 
255  // According to
256  // http://www.mcs.anl.gov/research/projects/tao/documentation/installation.html
257  // TAO & PETSc-complex are currently mutually exclusive
258  libmesh_example_requires(false, "PETSc >= 3.5.0 with built-in TAO support & real-numbers only");
259 
260 #endif
261 
262  // We use a 2D domain.
263  libmesh_example_requires(LIBMESH_DIM > 1, "--disable-1D-only");
264 
265  // We use Dirichlet boundary conditions here
266 #ifndef LIBMESH_ENABLE_DIRICHLET
267  libmesh_example_requires(false, "--enable-dirichlet");
268 #endif
269 
270  if (libMesh::on_command_line ("--use-eigen"))
271  {
272  libMesh::err << "This example requires an OptimizationSolver, and therefore does not "
273  << "support --use-eigen on the command line."
274  << std::endl;
275  return 0;
276  }
277 
278  GetPot infile("optimization_ex1.in");
279  infile.parse_command_line(argc, argv);
280 
281  const std::string approx_order = infile("approx_order", "FIRST");
282  const std::string fe_family = infile("fe_family", "LAGRANGE");
283  const unsigned int n_elem = infile("n_elem", 10);
284 
285  Mesh mesh(init.comm());
287  n_elem,
288  n_elem,
289  -1., 1.,
290  -1., 1.,
291  QUAD9);
292 
293  mesh.print_info();
294 
295  EquationSystems equation_systems (mesh);
296 
297  OptimizationSystem & system =
298  equation_systems.add_system<OptimizationSystem> ("Optimization");
299 
300  // The default is to use PETSc/Tao solvers, but let the user change
301  // the optimization solver package on the fly.
302  {
303  const std::string optimization_solver_type = infile("optimization_solver_type",
304  "PETSC_SOLVERS");
305  SolverPackage sp = Utility::string_to_enum<SolverPackage>(optimization_solver_type);
306  std::unique_ptr<OptimizationSolver<Number>> new_solver =
308  system.optimization_solver.reset(new_solver.release());
309  }
310 
311  // Set tolerances and maximum iteration counts directly on the optimization solver.
312  system.optimization_solver->max_objective_function_evaluations = 128;
313  system.optimization_solver->objective_function_relative_tolerance = 1.e-4;
314  system.optimization_solver->verbose = true;
315 
316  AssembleOptimization assemble_opt(system);
317 
318  system.optimization_solver->objective_object = &assemble_opt;
319  system.optimization_solver->gradient_object = &assemble_opt;
320  system.optimization_solver->hessian_object = &assemble_opt;
321 
322  // system.matrix and system.rhs are used for the gradient and Hessian,
323  // so in this case we add an extra matrix and vector to store A and F.
324  // This makes it easy to write the code for evaluating the objective,
325  // gradient, and hessian.
326  system.add_matrix("A_matrix");
327  system.add_vector("F_vector");
328  assemble_opt.A_matrix = &system.get_matrix("A_matrix");
329  assemble_opt.F_vector = &system.get_vector("F_vector");
330 
331 #ifdef LIBMESH_ENABLE_DIRICHLET
332  unsigned int u_var = system.add_variable("u",
333  Utility::string_to_enum<Order> (approx_order),
334  Utility::string_to_enum<FEFamily>(fe_family));
335 
336  // Apply Dirichlet constraints. This will be used to apply constraints
337  // to the objective function, gradient and Hessian.
338  ZeroFunction<> zf;
339 
340  // Most DirichletBoundary users will want to supply a "locally
341  // indexed" functor
342  DirichletBoundary dirichlet_bc({0,1,2,3}, {u_var}, zf,
344  system.get_dof_map().add_dirichlet_boundary(dirichlet_bc);
345 #endif // LIBMESH_ENABLE_DIRICHLET
346 
347  equation_systems.init();
348  equation_systems.print_info();
349 
350  assemble_opt.assemble_A_and_F();
351 
352  // We need to close the matrix so that we can use it to store the
353  // Hessian during the solve.
354  system.get_system_matrix().close();
355 
356  // We can get division-by-zero from ILU within the TAO solver, but
357  // they can recover from it, so if we have FPEs turned on we should
358  // turn them off.
359  FPEDisabler disable_fpes;
360 
361  system.solve();
362 
363  // Print convergence information
364  system.optimization_solver->print_converged_reason();
365 
366 #ifdef LIBMESH_HAVE_EXODUS_API
367  std::stringstream filename;
368  ExodusII_IO (mesh).write_equation_systems("optimization_soln.exo",
369  equation_systems);
370 #endif
371 
372  return 0;
373 }
OStreamProxy err
The FPEDisabler class puts Floating-Point Exception (FPE) trapping on hold during its lifetime...
Definition: fpe_disabler.h:49
This is the EquationSystems class.
ConstFunction that simply returns 0.
Definition: zero_function.h:38
dof_id_type n_elem(const MeshBase::const_element_iterator &begin, const MeshBase::const_element_iterator &end)
Count up the number of elements of a specific type (as defined by an iterator range).
Definition: mesh_tools.C:969
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
Definition: exodusII_io.h:52
virtual void solve() override
Solves the optimization problem.
MeshBase & mesh
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
NumericVector< Number > & add_vector(std::string_view vec_name, const bool projections=true, const ParallelType type=PARALLEL)
Adds the additional vector vec_name to this system.
Definition: system.C:768
void build_square(UnstructuredMesh &mesh, const unsigned int nx, const unsigned int ny, const Real xmin=0., const Real xmax=1., const Real ymin=0., const Real ymax=1., const ElemType type=INVALID_ELEM, const bool gauss_lobatto_grid=false)
A specialized build_cube() for 2D meshes.
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Definition: libmesh.h:90
This class encapsulate all functionality required for assembling the objective function, gradient, and hessian.
virtual void write_equation_systems(const std::string &fname, const EquationSystems &es, const std::set< std::string > *system_names=nullptr) override
Writes out the solution for no specific time or timestep.
Definition: exodusII_io.C:2033
This System subclass enables us to assemble an objective function, gradient, Hessian and bounds for o...
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
Definition: mesh_base.C:1562
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
unsigned int add_variable(std::string_view var, const FEType &type, const std::set< subdomain_id_type > *const active_subdomains=nullptr)
Adds the variable var to the list of variables for this system.
Definition: system.C:1357
std::unique_ptr< OptimizationSolver< Number > > optimization_solver
The OptimizationSolver that is used for performing the optimization.
virtual void close()=0
Calls the SparseMatrix&#39;s internal assembly routines, ensuring that the values are consistent across p...
This base class can be inherited from to provide interfaces to optimization solvers from different pa...
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
bool on_command_line(std::string arg)
Definition: libmesh.C:987
SolverPackage
Defines an enum for various linear solver packages.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Definition: mesh.h:50
SparseMatrix< Number > & add_matrix(std::string_view mat_name, ParallelType type=PARALLEL, MatrixBuildType mat_build_type=MatrixBuildType::AUTOMATIC)
Adds the additional matrix mat_name to this system.
Definition: system.C:1010
const DofMap & get_dof_map() const
Definition: system.h:2374
const SparseMatrix< Number > & get_matrix(std::string_view mat_name) const
Definition: system.C:1124
const SparseMatrix< Number > & get_system_matrix() const
const NumericVector< Number > & get_vector(std::string_view vec_name) const
Definition: system.C:943