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" 91 void assemble_A_and_F();
143 std::unique_ptr<FEBase> fe (FEBase::build(
dim, fe_type));
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.;
193 #endif // LIBMESH_ENABLE_CONSTRAINTS 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");
241 int 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));
345 #endif // LIBMESH_ENABLE_DIRICHLET 347 equation_systems.
init();
366 #ifdef LIBMESH_HAVE_EXODUS_API 367 std::stringstream filename;
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
OptimizationSystem & _sys
Keep a reference to the OptimizationSystem.
The FPEDisabler class puts Floating-Point Exception (FPE) trapping on hold during its lifetime...
This is the EquationSystems class.
virtual Number objective(const NumericVector< Number > &soln, OptimizationSystem &)
Evaluate the objective function.
ConstFunction that simply returns 0.
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.
Abstract base class to be used to calculate the objective function for optimization.
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
AssembleOptimization(OptimizationSystem &sys_in)
Constructor.
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...
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
virtual std::unique_ptr< NumericVector< T > > zero_clone() const =0
void resize(const unsigned int n)
Resize the vector.
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 solve() override
Solves the optimization problem.
const FEType & variable_type(const unsigned int c) const
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
NumericVector< Number > * F_vector
Vector for storing F.
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
Order default_quadrature_order() const
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.
int main(int argc, char **argv)
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
The libMesh namespace provides an interface to certain functionality in the library.
virtual T dot(const NumericVector< T > &v) const =0
const MeshBase & get_mesh() const
This is the MeshBase class.
virtual void zero()=0
Set all entries to zero.
unsigned int variable_number(std::string_view var) const
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
Add value to the element (i,j).
This class handles the numbering of degrees of freedom on a mesh.
SparseMatrix< Number > * A_matrix
Sparse matrix for storing the matrix A.
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.
This class encapsulate all functionality required for assembling the objective function, gradient, and hessian.
virtual void zero()=0
Set all entries to 0.
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.
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.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
bool is_constrained_dof(const dof_id_type dof) 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.
virtual void close()=0
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
std::unique_ptr< OptimizationSolver< Number > > optimization_solver
The OptimizationSolver that is used for performing the optimization.
Abstract base class to be used to calculate the gradient of an objective function.
virtual void hessian(const NumericVector< Number > &soln, SparseMatrix< Number > &H_f, OptimizationSystem &)
Evaluate the Hessian.
virtual void close()=0
Calls the SparseMatrix'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...
virtual void gradient(const NumericVector< Number > &soln, NumericVector< Number > &grad_f, OptimizationSystem &)
Evaluate the gradient.
Abstract base class to be used to calculate the Hessian of an objective function. ...
void resize(const unsigned int new_m, const unsigned int new_n)
Resizes the matrix to the specified size and calls zero().
This class implements specific orders of Gauss quadrature.
unsigned int mesh_dimension() const
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)
virtual void init()
Initialize all the systems.
SolverPackage
Defines an enum for various linear solver packages.
void assemble_A_and_F()
The optimization problem we consider here is: min_U 0.5*U^T A U - U^T F.
virtual void add(const numeric_index_type i, const T value)=0
Adds value to the vector entry specified by i.
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.
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 DofMap & get_dof_map() const
const SparseMatrix< Number > & get_matrix(std::string_view mat_name) const
const SparseMatrix< Number > & get_system_matrix() const
const NumericVector< Number > & get_vector(std::string_view vec_name) const