37#include "libmesh/libmesh.h"
38#include "libmesh/mesh.h"
39#include "libmesh/mesh_generation.h"
40#include "libmesh/exodusII_io.h"
41#include "libmesh/equation_systems.h"
42#include "libmesh/fe.h"
43#include "libmesh/quadrature_gauss.h"
44#include "libmesh/dof_map.h"
45#include "libmesh/sparse_matrix.h"
46#include "libmesh/numeric_vector.h"
47#include "libmesh/dense_matrix.h"
48#include "libmesh/dense_vector.h"
49#include "libmesh/elem.h"
50#include "libmesh/fpe_disabler.h"
51#include "libmesh/boundary_info.h"
52#include "libmesh/string_to_enum.h"
53#include "libmesh/getpot.h"
54#include "libmesh/zero_function.h"
55#include "libmesh/dirichlet_boundaries.h"
56#include "libmesh/optimization_system.h"
57#include "libmesh/optimization_solver.h"
145 fe->attach_quadrature_rule (&qrule);
147 const std::vector<Real> & JxW = fe->get_JxW();
148 const std::vector<std::vector<Real>> & phi = fe->get_phi();
149 const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
151 std::vector<dof_id_type> dof_indices;
156 for (
const auto & elem :
mesh.active_local_element_ptr_range())
160 const unsigned int n_dofs = dof_indices.size();
164 Ke.
resize (n_dofs, n_dofs);
167 for (
unsigned int qp=0; qp<qrule.
n_points(); qp++)
169 for (
unsigned int dof_i=0; dof_i<n_dofs; dof_i++)
171 for (
unsigned int dof_j=0; dof_j<n_dofs; dof_j++)
173 Ke(dof_i, dof_j) += JxW[qp] * (dphi[dof_j][qp]* dphi[dof_i][qp]);
175 Fe(dof_i) += JxW[qp] * phi[dof_i][qp];
179#ifdef LIBMESH_ENABLE_CONSTRAINTS
185 for (
unsigned int local_dof_index=0; local_dof_index<n_dofs; local_dof_index++)
187 dof_id_type global_dof_index = dof_indices[local_dof_index];
190 Ke(local_dof_index, local_dof_index) = 0.;
206 std::unique_ptr<NumericVector<Number>> AxU = soln.
zero_clone();
211 UTxAxU = AxU->dot(soln),
214 return 0.5 * UTxAxU - UTxF;
235 LOG_SCOPE(
"hessian()",
"AssembleOptimization");
241int main (
int argc,
char ** argv)
245#ifndef LIBMESH_HAVE_PETSC_TAO
247 libmesh_example_requires(
false,
"PETSc >= 3.5.0 with built-in TAO support");
249#elif !defined(LIBMESH_ENABLE_GHOSTED)
251 libmesh_example_requires(
false,
"--enable-ghosted");
253#elif LIBMESH_USE_COMPLEX_NUMBERS
258 libmesh_example_requires(
false,
"PETSc >= 3.5.0 with built-in TAO support & real-numbers only");
263 libmesh_example_requires(LIBMESH_DIM > 1,
"--disable-1D-only");
266#ifndef LIBMESH_ENABLE_DIRICHLET
267 libmesh_example_requires(
false,
"--enable-dirichlet");
272 libMesh::err <<
"This example requires an OptimizationSolver, and therefore does not "
273 <<
"support --use-eigen on the command line."
278 GetPot infile(
"optimization_ex1.in");
279 infile.parse_command_line(argc, argv);
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);
303 const std::string optimization_solver_type = infile(
"optimization_solver_type",
305 SolverPackage sp = Utility::string_to_enum<SolverPackage>(optimization_solver_type);
306 std::unique_ptr<OptimizationSolver<Number>> new_solver =
331#ifdef LIBMESH_ENABLE_DIRICHLET
333 Utility::string_to_enum<Order> (approx_order),
334 Utility::string_to_enum<FEFamily>(fe_family));
347 equation_systems.
init();
366#ifdef LIBMESH_HAVE_EXODUS_API
367 std::stringstream filename;
This class encapsulate all functionality required for assembling the objective function,...
virtual void hessian(const NumericVector< Number > &soln, SparseMatrix< Number > &H_f, OptimizationSystem &)
Evaluate the Hessian.
virtual Number objective(const NumericVector< Number > &soln, OptimizationSystem &)
Evaluate the objective function.
NumericVector< Number > * F_vector
Vector for storing F.
OptimizationSystem & _sys
Keep a reference to the OptimizationSystem.
virtual void gradient(const NumericVector< Number > &soln, NumericVector< Number > &grad_f, OptimizationSystem &)
Evaluate the gradient.
AssembleOptimization(OptimizationSystem &sys_in)
Constructor.
void assemble_A_and_F()
The optimization problem we consider here is: min_U 0.5*U^T A U - U^T F.
SparseMatrix< Number > * A_matrix
Sparse matrix for storing the matrix A.
Defines a dense matrix for use in Finite Element-type computations.
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
Defines a dense vector for use in Finite Element-type computations.
void resize(const unsigned int n)
Resize the vector.
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
This class handles the numbering of degrees of freedom on a mesh.
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
const FEType & variable_type(const unsigned int i) const
bool is_constrained_dof(const dof_id_type dof) const
void constrain_element_matrix_and_vector(DenseMatrix< Number > &matrix, DenseVector< Number > &rhs, std::vector< dof_id_type > &elem_dofs, bool asymmetric_constraint_rows=true) const
Constrains the element matrix and vector.
This is the EquationSystems class.
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
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 ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
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.
static std::unique_ptr< FEGenericBase > build(const unsigned int dim, const FEType &type)
Builds a specific finite element type.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
Order default_quadrature_order() const
const SparseMatrix< Number > & get_system_matrix() const
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
This is the MeshBase class.
unsigned int mesh_dimension() const
void print_info(std::ostream &os=libMesh::out, const unsigned int verbosity=0, const bool global=true) const
Prints relevant information about the mesh.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
virtual void close()=0
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
virtual T dot(const NumericVector< T > &v) const =0
virtual void add_vector(const T *v, const std::vector< numeric_index_type > &dof_indices)
Computes , where v is a pointer and each dof_indices[i] specifies where to add value v[i].
virtual void zero()=0
Set all entries to zero.
virtual std::unique_ptr< NumericVector< T > > zero_clone() const =0
virtual void add(const numeric_index_type i, const T value)=0
Adds value to the vector entry specified by i.
static std::unique_ptr< OptimizationSolver< T > > build(sys_type &s, const SolverPackage solver_package=libMesh::default_solver_package())
Builds an OptimizationSolver using the package specified by solver_package.
Abstract base class to be used to calculate the gradient of an objective function.
Abstract base class to be used to calculate the Hessian of an objective function.
Abstract base class to be used to calculate the objective function for optimization.
This System subclass enables us to assemble an objective function, gradient, Hessian and bounds for o...
virtual void solve() override
Solves the optimization problem.
std::unique_ptr< OptimizationSolver< Number > > optimization_solver
The OptimizationSolver that is used for performing the optimization.
unsigned int n_points() const
This class implements specific orders of Gauss quadrature.
virtual void close()=0
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const
Multiplies the matrix by the NumericVector arg and stores the result in NumericVector dest.
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols)=0
Add the full matrix dm to the SparseMatrix.
virtual void zero()=0
Set all entries to 0.
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
Add value to the element (i,j).
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.
const SparseMatrix< Number > & get_matrix(std::string_view mat_name) const
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.
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.
unsigned int variable_number(std::string_view var) const
const DofMap & get_dof_map() const
const NumericVector< Number > & get_vector(std::string_view vec_name) const
const MeshBase & get_mesh() const
ConstFunction that simply returns 0.
The libMesh namespace provides an interface to certain functionality in the library.
SolverPackage
Defines an enum for various linear solver packages.
bool on_command_line(std::string arg)
The FPEDisabler class puts Floating-Point Exception (FPE) trapping on hold during its lifetime,...