Go to the documentation of this file.
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/boundary_info.h"
51 #include "libmesh/string_to_enum.h"
52 #include "libmesh/getpot.h"
53 #include "libmesh/zero_function.h"
54 #include "libmesh/dirichlet_boundaries.h"
55 #include "libmesh/optimization_system.h"
56 #include "libmesh/optimization_solver.h"
90 void assemble_A_and_F();
142 std::unique_ptr<FEBase> fe (FEBase::build(
dim, fe_type));
144 fe->attach_quadrature_rule (&qrule);
146 const std::vector<Real> & JxW = fe->get_JxW();
147 const std::vector<std::vector<Real>> & phi = fe->get_phi();
148 const std::vector<std::vector<RealGradient>> & dphi = fe->get_dphi();
150 std::vector<dof_id_type> dof_indices;
159 const unsigned int n_dofs = dof_indices.size();
163 Ke.
resize (n_dofs, n_dofs);
166 for (
unsigned int qp=0; qp<qrule.n_points(); qp++)
168 for (
unsigned int dof_i=0; dof_i<n_dofs; dof_i++)
170 for (
unsigned int dof_j=0; dof_j<n_dofs; dof_j++)
172 Ke(dof_i, dof_j) += JxW[qp] * (dphi[dof_j][qp]* dphi[dof_i][qp]);
174 Fe(dof_i) += JxW[qp] * phi[dof_i][qp];
178 #ifdef LIBMESH_ENABLE_CONSTRAINTS
184 for (
unsigned int local_dof_index=0; local_dof_index<n_dofs; local_dof_index++)
186 dof_id_type global_dof_index = dof_indices[local_dof_index];
189 Ke(local_dof_index, local_dof_index) = 0.;
192 #endif // LIBMESH_ENABLE_CONSTRAINTS
205 std::unique_ptr<NumericVector<Number>> AxU = soln.
zero_clone();
210 UTxAxU = AxU->dot(soln),
213 return 0.5 * UTxAxU - UTxF;
239 int main (
int argc,
char ** argv)
243 #ifndef LIBMESH_HAVE_PETSC_TAO
245 libmesh_example_requires(
false,
"PETSc >= 3.5.0 with built-in TAO support");
247 #elif !defined(LIBMESH_ENABLE_GHOSTED)
249 libmesh_example_requires(
false,
"--enable-ghosted");
251 #elif LIBMESH_USE_COMPLEX_NUMBERS
256 libmesh_example_requires(
false,
"PETSc >= 3.5.0 with built-in TAO support & real-numbers only");
261 libmesh_example_requires(LIBMESH_DIM > 1,
"--disable-1D-only");
264 #ifndef LIBMESH_ENABLE_DIRICHLET
265 libmesh_example_requires(
false,
"--enable-dirichlet");
270 libMesh::err <<
"This example requires an OptimizationSolver, and therefore does not "
271 <<
"support --use-eigen on the command line."
276 GetPot infile(
"optimization_ex1.in");
277 const std::string approx_order = infile(
"approx_order",
"FIRST");
278 const std::string fe_family = infile(
"fe_family",
"LAGRANGE");
279 const unsigned int n_elem = infile(
"n_elem", 10);
299 const std::string optimization_solver_type = infile(
"optimization_solver_type",
301 SolverPackage sp = Utility::string_to_enum<SolverPackage>(optimization_solver_type);
302 std::unique_ptr<OptimizationSolver<Number>> new_solver =
327 #ifdef LIBMESH_ENABLE_DIRICHLET
329 Utility::string_to_enum<Order> (approx_order),
330 Utility::string_to_enum<FEFamily>(fe_family));
334 std::set<boundary_id_type> boundary_ids;
335 boundary_ids.insert(0);
336 boundary_ids.insert(1);
337 boundary_ids.insert(2);
338 boundary_ids.insert(3);
339 std::vector<unsigned int> variables;
340 variables.push_back(u_var);
348 #endif // LIBMESH_ENABLE_DIRICHLET
350 equation_systems.
init();
363 #ifdef LIBMESH_HAVE_EXODUS_API
364 std::stringstream filename;
This System subclass enables us to assemble an objective function, gradient, Hessian and bounds for o...
virtual void close()=0
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
virtual void zero()=0
Set all entries to zero.
SolverPackage
Defines an enum for various linear solver packages.
virtual void add(const numeric_index_type i, const T value)=0
Adds value to each entry of the vector.
The Mesh class is a thin wrapper, around the ReplicatedMesh class by default.
virtual void gradient(const NumericVector< Number > &soln, NumericVector< Number > &grad_f, OptimizationSystem &)
Evaluate the gradient.
int main(int argc, char **argv)
virtual System & add_system(const std::string &system_type, const std::string &name)
Add the system of type system_type named name to the systems array.
This class implements specific orders of Gauss quadrature.
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Fills the vector di with the global degree of freedom indices for the element.
AssembleOptimization(OptimizationSystem &sys_in)
Constructor.
void add_dirichlet_boundary(const DirichletBoundary &dirichlet_boundary)
Adds a copy of the specified Dirichlet boundary to the system.
OptimizationSystem & _sys
Keep a reference to the OptimizationSystem.
virtual SimpleRange< element_iterator > active_local_element_ptr_range()=0
virtual void solve() override
Solves the optimization problem.
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 libMesh namespace provides an interface to certain functionality in the library.
virtual void close()=0
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
SparseMatrix< Number > & add_matrix(const std::string &mat_name)
Adds the additional matrix mat_name to this system.
This class encapsulate all functionality required for assembling the objective function,...
Abstract base class to be used to calculate the Hessian of an objective function.
unsigned int mesh_dimension() const
The ExodusII_IO class implements reading meshes in the ExodusII file format from Sandia National Labs...
bool is_constrained_dof(const dof_id_type dof) const
void resize(const unsigned int new_m, const unsigned int new_n)
Resize the matrix.
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
NumericVector< Number > * F_vector
Vector for storing F.
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].
This is the MeshBase class.
virtual void hessian(const NumericVector< Number > &soln, SparseMatrix< Number > &H_f, OptimizationSystem &)
Evaluate the Hessian.
This base class can be inherited from to provide interfaces to optimization solvers from different pa...
void assemble_A_and_F()
The optimization problem we consider here is: min_U 0.5*U^T A U - U^T F.
const MeshBase & get_mesh() const
std::unique_ptr< OptimizationSolver< Number > > optimization_solver
The OptimizationSolver that is used for performing the optimization.
unsigned int add_variable(const std::string &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 init()
Initialize all the systems.
SparseMatrix< Number > * matrix
The system matrix.
const FEType & variable_type(const unsigned int c) const
The LibMeshInit class, when constructed, initializes the dependent libraries (e.g.
Abstract base class to be used to calculate the objective function for optimization.
Abstract base class to be used to calculate the gradient of an objective function.
ConstFunction that simply returns 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).
void print_info(std::ostream &os=libMesh::out) const
Prints information about the equation systems, by default to libMesh::out.
This is the EquationSystems class.
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.
virtual void write_equation_systems(const std::string &, const EquationSystems &, const std::set< std::string > *system_names=nullptr)
This method implements writing a mesh with data to a specified file where the data is taken from the ...
void resize(const unsigned int n)
Resize the vector.
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.
class FEType hides (possibly multiple) FEFamily and approximation orders, thereby enabling specialize...
This class handles the numbering of degrees of freedom on a mesh.
void print_info(std::ostream &os=libMesh::out) const
Prints relevant information about the mesh.
virtual Number objective(const NumericVector< Number > &soln, OptimizationSystem &)
Evaluate the objective function.
This class allows one to associate Dirichlet boundary values with a given set of mesh boundary ids an...
unsigned short int variable_number(const std::string &var) const
bool on_command_line(std::string arg)
SparseMatrix< Number > * A_matrix
Sparse matrix for storing the matrix A.
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.
const DofMap & get_dof_map() const
Order default_quadrature_order() const
const SparseMatrix< Number > & get_matrix(const std::string &mat_name) const
const NumericVector< Number > & get_vector(const std::string &vec_name) const
virtual void zero()=0
Set all entries to 0.
virtual std::unique_ptr< NumericVector< T > > zero_clone() const =0
virtual T dot(const NumericVector< T > &v) const =0