libMesh
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Private Attributes | List of all members
AssembleOptimization Class Reference

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

Inheritance diagram for AssembleOptimization:
[legend]

Public Member Functions

 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.
 
virtual Number objective (const NumericVector< Number > &soln, OptimizationSystem &)
 Evaluate the objective function.
 
virtual void gradient (const NumericVector< Number > &soln, NumericVector< Number > &grad_f, OptimizationSystem &)
 Evaluate the gradient.
 
virtual void hessian (const NumericVector< Number > &soln, SparseMatrix< Number > &H_f, OptimizationSystem &)
 Evaluate the Hessian.
 
 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.
 
virtual Number objective (const NumericVector< Number > &soln, OptimizationSystem &)
 Evaluate the objective function.
 
virtual void gradient (const NumericVector< Number > &soln, NumericVector< Number > &grad_f, OptimizationSystem &)
 Evaluate the gradient.
 
virtual void hessian (const NumericVector< Number > &soln, SparseMatrix< Number > &H_f, OptimizationSystem &)
 Evaluate the Hessian.
 
virtual void equality_constraints (const NumericVector< Number > &X, NumericVector< Number > &C_eq, OptimizationSystem &)
 Evaluate the equality constraints.
 
virtual void equality_constraints_jacobian (const NumericVector< Number > &X, SparseMatrix< Number > &C_eq_jac, OptimizationSystem &)
 Evaluate the equality constraints Jacobian.
 
virtual void inequality_constraints (const NumericVector< Number > &X, NumericVector< Number > &C_ineq, OptimizationSystem &)
 Evaluate the inequality constraints.
 
virtual void inequality_constraints_jacobian (const NumericVector< Number > &X, SparseMatrix< Number > &C_ineq_jac, OptimizationSystem &)
 Evaluate the inequality constraints Jacobian.
 
virtual void lower_and_upper_bounds (OptimizationSystem &sys)
 Evaluate the lower and upper bounds vectors.
 

Public Attributes

SparseMatrix< Number > * A_matrix
 Sparse matrix for storing the matrix A.
 
NumericVector< Number > * F_vector
 Vector for storing F.
 

Private Attributes

OptimizationSystem_sys
 Keep a reference to the OptimizationSystem.
 

Detailed Description

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

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

Definition at line 67 of file optimization_ex1.C.

Constructor & Destructor Documentation

◆ AssembleOptimization() [1/2]

AssembleOptimization::AssembleOptimization ( OptimizationSystem sys_in)

Constructor.

Definition at line 127 of file optimization_ex1.C.

127 :
128 _sys(sys_in)
129{}
OptimizationSystem & _sys
Keep a reference to the OptimizationSystem.

◆ AssembleOptimization() [2/2]

AssembleOptimization::AssembleOptimization ( OptimizationSystem sys_in)

Constructor.

Member Function Documentation

◆ assemble_A_and_F() [1/2]

void AssembleOptimization::assemble_A_and_F ( )

The optimization problem we consider here is: min_U 0.5*U^T A U - U^T F.

In this method, we assemble A and F.

Definition at line 131 of file optimization_ex1.C.

132{
133 A_matrix->zero();
134 F_vector->zero();
135
136 const MeshBase & mesh = _sys.get_mesh();
137
138 const unsigned int dim = mesh.mesh_dimension();
139 const unsigned int u_var = _sys.variable_number ("u");
140
141 const DofMap & dof_map = _sys.get_dof_map();
142 FEType fe_type = dof_map.variable_type(u_var);
143 std::unique_ptr<FEBase> fe (FEBase::build(dim, fe_type));
144 QGauss qrule (dim, fe_type.default_quadrature_order());
145 fe->attach_quadrature_rule (&qrule);
146
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();
150
151 std::vector<dof_id_type> dof_indices;
152
155
156 for (const auto & elem : mesh.active_local_element_ptr_range())
157 {
158 dof_map.dof_indices (elem, dof_indices);
159
160 const unsigned int n_dofs = dof_indices.size();
161
162 fe->reinit (elem);
163
164 Ke.resize (n_dofs, n_dofs);
165 Fe.resize (n_dofs);
166
167 for (unsigned int qp=0; qp<qrule.n_points(); qp++)
168 {
169 for (unsigned int dof_i=0; dof_i<n_dofs; dof_i++)
170 {
171 for (unsigned int dof_j=0; dof_j<n_dofs; dof_j++)
172 {
173 Ke(dof_i, dof_j) += JxW[qp] * (dphi[dof_j][qp]* dphi[dof_i][qp]);
174 }
175 Fe(dof_i) += JxW[qp] * phi[dof_i][qp];
176 }
177 }
178
179#ifdef LIBMESH_ENABLE_CONSTRAINTS
180 // This will zero off-diagonal entries of Ke corresponding to
181 // Dirichlet dofs.
182 dof_map.constrain_element_matrix_and_vector (Ke, Fe, dof_indices);
183
184 // We want the diagonal of constrained dofs to be zero too
185 for (unsigned int local_dof_index=0; local_dof_index<n_dofs; local_dof_index++)
186 {
187 dof_id_type global_dof_index = dof_indices[local_dof_index];
188 if (dof_map.is_constrained_dof(global_dof_index))
189 {
190 Ke(local_dof_index, local_dof_index) = 0.;
191 }
192 }
193#endif // LIBMESH_ENABLE_CONSTRAINTS
194
195 A_matrix->add_matrix (Ke, dof_indices);
196 F_vector->add_vector (Fe, dof_indices);
197 }
198
199 A_matrix->close();
200 F_vector->close();
201}
unsigned int dim
NumericVector< Number > * F_vector
Vector for storing 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 handles the numbering of degrees of freedom on a mesh.
Definition dof_map.h:181
void dof_indices(const Elem *const elem, std::vector< dof_id_type > &di) const
Definition dof_map.C:2201
const FEType & variable_type(const unsigned int i) const
Definition dof_map.h:2388
bool is_constrained_dof(const dof_id_type dof) const
Definition dof_map.h:2426
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.
Definition dof_map.h:2498
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...
Definition fe_type.h:197
Order default_quadrature_order() const
Definition fe_type.h:415
This is the MeshBase class.
Definition mesh_base.h:81
unsigned int mesh_dimension() const
Definition mesh_base.C:430
virtual void close()=0
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
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.
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...
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.
unsigned int variable_number(std::string_view var) const
Definition system.C:1398
const DofMap & get_dof_map() const
Definition system.h:2417
const MeshBase & get_mesh() const
Definition system.h:2401
MeshBase & mesh
uint8_t dof_id_type
Definition id_types.h:67

References _sys, A_matrix, libMesh::SparseMatrix< T >::add_matrix(), libMesh::NumericVector< T >::add_vector(), libMesh::FEGenericBase< OutputType >::build(), libMesh::NumericVector< T >::close(), libMesh::SparseMatrix< T >::close(), libMesh::DofMap::constrain_element_matrix_and_vector(), libMesh::FEType::default_quadrature_order(), dim, libMesh::DofMap::dof_indices(), F_vector, libMesh::System::get_dof_map(), libMesh::System::get_mesh(), libMesh::DofMap::is_constrained_dof(), mesh, libMesh::MeshBase::mesh_dimension(), libMesh::QBase::n_points(), libMesh::DenseVector< T >::resize(), libMesh::DenseMatrix< T >::resize(), libMesh::System::variable_number(), libMesh::DofMap::variable_type(), libMesh::NumericVector< T >::zero(), and libMesh::SparseMatrix< T >::zero().

Referenced by main().

◆ assemble_A_and_F() [2/2]

void AssembleOptimization::assemble_A_and_F ( )

The optimization problem we consider here is: min_U 0.5*U^T A U - U^T F.

In this method, we assemble A and F.

◆ equality_constraints()

void AssembleOptimization::equality_constraints ( const NumericVector< Number > &  X,
NumericVector< Number > &  C_eq,
OptimizationSystem  
)
virtual

Evaluate the equality constraints.

Implements libMesh::OptimizationSystem::ComputeEqualityConstraints.

Definition at line 276 of file optimization_ex2.C.

279{
280 C_eq.zero();
281
282 std::unique_ptr<NumericVector<Number>> X_localized =
284 X_localized->init(X.size(), false, SERIAL);
285 X.localize(*X_localized);
286
287 std::vector<Number> constraint_values(3);
288 constraint_values[0] = (*X_localized)(17);
289 constraint_values[1] = (*X_localized)(23);
290 constraint_values[2] = (*X_localized)(98) + (*X_localized)(185);
291
292 for (std::size_t i=0; i<constraint_values.size(); i++)
293 if ((C_eq.first_local_index() <= i) &&
294 (i < C_eq.last_local_index()))
295 C_eq.set(i, constraint_values[i]);
296}
virtual void set(const numeric_index_type i, const T value)=0
Sets v(i) = value.
virtual numeric_index_type last_local_index() const =0
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, SolverPackage solver_package=libMesh::default_solver_package(), ParallelType parallel_type=AUTOMATIC)
Builds a NumericVector on the processors in communicator comm using the linear solver package specifi...
virtual void localize(std::vector< T > &v_local) const =0
Creates a copy of the global vector in the local vector v_local.
virtual numeric_index_type size() const =0
virtual numeric_index_type first_local_index() const =0
const Parallel::Communicator & comm() const

References libMesh::NumericVector< T >::build(), libMesh::ParallelObject::comm(), libMesh::NumericVector< T >::first_local_index(), libMesh::NumericVector< T >::last_local_index(), libMesh::NumericVector< T >::localize(), libMesh::SERIAL, libMesh::NumericVector< T >::set(), libMesh::NumericVector< T >::size(), and libMesh::NumericVector< T >::zero().

◆ equality_constraints_jacobian()

void AssembleOptimization::equality_constraints_jacobian ( const NumericVector< Number > &  X,
SparseMatrix< Number > &  C_eq_jac,
OptimizationSystem sys 
)
virtual

Evaluate the equality constraints Jacobian.

Implements libMesh::OptimizationSystem::ComputeEqualityConstraintsJacobian.

Definition at line 298 of file optimization_ex2.C.

301{
302 C_eq_jac.zero();
303
304 std::vector<std::vector<Number>> constraint_jac_values(3);
305 std::vector<std::vector<dof_id_type>> constraint_jac_indices(3);
306
307 constraint_jac_values[0].resize(1);
308 constraint_jac_indices[0].resize(1);
309 constraint_jac_values[0][0] = 1.;
310 constraint_jac_indices[0][0] = 17;
311
312 constraint_jac_values[1].resize(1);
313 constraint_jac_indices[1].resize(1);
314 constraint_jac_values[1][0] = 1.;
315 constraint_jac_indices[1][0] = 23;
316
317 constraint_jac_values[2].resize(2);
318 constraint_jac_indices[2].resize(2);
319 constraint_jac_values[2][0] = 1.;
320 constraint_jac_values[2][1] = 1.;
321 constraint_jac_indices[2][0] = 98;
322 constraint_jac_indices[2][1] = 185;
323
324 for (std::size_t i=0; i<constraint_jac_values.size(); i++)
325 for (std::size_t j=0; j<constraint_jac_values[i].size(); j++)
326 if ((sys.C_eq->first_local_index() <= i) &&
327 (i < sys.C_eq->last_local_index()))
328 {
329 dof_id_type col_index = constraint_jac_indices[i][j];
330 Number value = constraint_jac_values[i][j];
331 C_eq_jac.set(i, col_index, value);
332 }
333}
std::unique_ptr< NumericVector< Number > > C_eq
The vector that stores equality constraints.
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value)=0
Set the element (i,j) to value.
static const bool value
Definition xdr_io.C:55

References libMesh::OptimizationSystem::C_eq, libMesh::SparseMatrix< T >::set(), value, and libMesh::SparseMatrix< T >::zero().

◆ gradient() [1/2]

void AssembleOptimization::gradient ( const NumericVector< Number > &  soln,
NumericVector< Number > &  grad_f,
OptimizationSystem  
)
virtual

Evaluate the gradient.

Implements libMesh::OptimizationSystem::ComputeGradient.

Definition at line 217 of file optimization_ex1.C.

220{
221 grad_f.zero();
222
223 // Since we've enforced constraints on soln, A and F,
224 // this automatically sets grad_f to zero for constrained
225 // dofs.
226 A_matrix->vector_mult(grad_f, soln);
227 grad_f.add(-1, *F_vector);
228}
virtual void add(const numeric_index_type i, const T value)=0
Adds value to the vector entry specified by i.
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.

References A_matrix, libMesh::NumericVector< T >::add(), F_vector, libMesh::SparseMatrix< T >::vector_mult(), and libMesh::NumericVector< T >::zero().

◆ gradient() [2/2]

virtual void AssembleOptimization::gradient ( const NumericVector< Number > &  soln,
NumericVector< Number > &  grad_f,
OptimizationSystem  
)
virtual

Evaluate the gradient.

Implements libMesh::OptimizationSystem::ComputeGradient.

◆ hessian() [1/2]

void AssembleOptimization::hessian ( const NumericVector< Number > &  soln,
SparseMatrix< Number > &  H_f,
OptimizationSystem sys 
)
virtual

Evaluate the Hessian.

Implements libMesh::OptimizationSystem::ComputeHessian.

Definition at line 231 of file optimization_ex1.C.

234{
235 LOG_SCOPE("hessian()", "AssembleOptimization");
236 H_f.zero();
237 H_f.add(1., *A_matrix);
238}
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value)=0
Add value to the element (i,j).

References A_matrix, libMesh::SparseMatrix< T >::add(), and libMesh::SparseMatrix< T >::zero().

◆ hessian() [2/2]

virtual void AssembleOptimization::hessian ( const NumericVector< Number > &  soln,
SparseMatrix< Number > &  H_f,
OptimizationSystem  
)
virtual

Evaluate the Hessian.

Implements libMesh::OptimizationSystem::ComputeHessian.

◆ inequality_constraints()

void AssembleOptimization::inequality_constraints ( const NumericVector< Number > &  X,
NumericVector< Number > &  C_ineq,
OptimizationSystem  
)
virtual

Evaluate the inequality constraints.

Implements libMesh::OptimizationSystem::ComputeInequalityConstraints.

Definition at line 335 of file optimization_ex2.C.

338{
339 C_ineq.zero();
340
341 std::unique_ptr<NumericVector<Number>> X_localized =
343 X_localized->init(X.size(), false, SERIAL);
344 X.localize(*X_localized);
345
346 std::vector<Number> constraint_values(1);
347 constraint_values[0] = (*X_localized)(200)*(*X_localized)(200) + (*X_localized)(201) - 5.;
348
349 for (std::size_t i=0; i<constraint_values.size(); i++)
350 if ((C_ineq.first_local_index() <= i) && (i < C_ineq.last_local_index()))
351 C_ineq.set(i, constraint_values[i]);
352}

References libMesh::NumericVector< T >::build(), libMesh::ParallelObject::comm(), libMesh::NumericVector< T >::first_local_index(), libMesh::NumericVector< T >::last_local_index(), libMesh::NumericVector< T >::localize(), libMesh::SERIAL, libMesh::NumericVector< T >::set(), libMesh::NumericVector< T >::size(), and libMesh::NumericVector< T >::zero().

◆ inequality_constraints_jacobian()

void AssembleOptimization::inequality_constraints_jacobian ( const NumericVector< Number > &  X,
SparseMatrix< Number > &  C_ineq_jac,
OptimizationSystem sys 
)
virtual

Evaluate the inequality constraints Jacobian.

Implements libMesh::OptimizationSystem::ComputeInequalityConstraintsJacobian.

Definition at line 354 of file optimization_ex2.C.

357{
358 C_ineq_jac.zero();
359
360 std::unique_ptr<NumericVector<Number>> X_localized =
362 X_localized->init(X.size(), false, SERIAL);
363 X.localize(*X_localized);
364
365 std::vector<std::vector<Number>> constraint_jac_values(1);
366 std::vector<std::vector<dof_id_type>> constraint_jac_indices(1);
367
368 constraint_jac_values[0].resize(2);
369 constraint_jac_indices[0].resize(2);
370 constraint_jac_values[0][0] = 2.* (*X_localized)(200);
371 constraint_jac_values[0][1] = 1.;
372 constraint_jac_indices[0][0] = 200;
373 constraint_jac_indices[0][1] = 201;
374
375 for (std::size_t i=0; i<constraint_jac_values.size(); i++)
376 for (std::size_t j=0; j<constraint_jac_values[i].size(); j++)
377 if ((sys.C_ineq->first_local_index() <= i) &&
378 (i < sys.C_ineq->last_local_index()))
379 {
380 dof_id_type col_index = constraint_jac_indices[i][j];
381 Number value = constraint_jac_values[i][j];
382 C_ineq_jac.set(i, col_index, value);
383 }
384
385}
std::unique_ptr< NumericVector< Number > > C_ineq
The vector that stores inequality constraints.

References libMesh::NumericVector< T >::build(), libMesh::OptimizationSystem::C_ineq, libMesh::ParallelObject::comm(), libMesh::NumericVector< T >::localize(), libMesh::SERIAL, libMesh::SparseMatrix< T >::set(), libMesh::NumericVector< T >::size(), value, and libMesh::SparseMatrix< T >::zero().

◆ lower_and_upper_bounds()

void AssembleOptimization::lower_and_upper_bounds ( OptimizationSystem sys)
virtual

Evaluate the lower and upper bounds vectors.

Implements libMesh::OptimizationSystem::ComputeLowerAndUpperBounds.

Definition at line 387 of file optimization_ex2.C.

388{
389 for (unsigned int i=sys.get_dof_map().first_dof(); i<sys.get_dof_map().end_dof(); i++)
390 {
391 sys.get_vector("lower_bounds").set(i, -2.);
392 sys.get_vector("upper_bounds").set(i, 2.);
393 }
394}
dof_id_type first_dof(const processor_id_type proc) const
dof_id_type end_dof(const processor_id_type proc) const
const NumericVector< Number > & get_vector(std::string_view vec_name) const
Definition system.C:931

References libMesh::DofMapBase::end_dof(), libMesh::DofMapBase::first_dof(), libMesh::System::get_dof_map(), libMesh::System::get_vector(), and libMesh::NumericVector< T >::set().

◆ objective() [1/2]

Number AssembleOptimization::objective ( const NumericVector< Number > &  soln,
OptimizationSystem  
)
virtual

Evaluate the objective function.

Implements libMesh::OptimizationSystem::ComputeObjective.

Definition at line 203 of file optimization_ex1.C.

205{
206 std::unique_ptr<NumericVector<Number>> AxU = soln.zero_clone();
207
208 A_matrix->vector_mult(*AxU, soln);
209
210 Number
211 UTxAxU = AxU->dot(soln),
212 UTxF = F_vector->dot(soln);
213
214 return 0.5 * UTxAxU - UTxF;
215}
virtual T dot(const NumericVector< T > &v) const =0
virtual std::unique_ptr< NumericVector< T > > zero_clone() const =0

References A_matrix, libMesh::NumericVector< T >::dot(), F_vector, libMesh::SparseMatrix< T >::vector_mult(), and libMesh::NumericVector< T >::zero_clone().

◆ objective() [2/2]

virtual Number AssembleOptimization::objective ( const NumericVector< Number > &  soln,
OptimizationSystem  
)
virtual

Evaluate the objective function.

Implements libMesh::OptimizationSystem::ComputeObjective.

Member Data Documentation

◆ _sys

OptimizationSystem & AssembleOptimization::_sys
private

Keep a reference to the OptimizationSystem.

Definition at line 77 of file optimization_ex1.C.

Referenced by assemble_A_and_F().

◆ A_matrix

SparseMatrix< Number > * AssembleOptimization::A_matrix

Sparse matrix for storing the matrix A.

We use this to facilitate computation of objective, gradient and hessian.

Definition at line 118 of file optimization_ex1.C.

Referenced by assemble_A_and_F(), gradient(), hessian(), main(), and objective().

◆ F_vector

NumericVector< Number > * AssembleOptimization::F_vector

Vector for storing F.

We use this to facilitate computation of objective, gradient and hessian.

Definition at line 124 of file optimization_ex1.C.

Referenced by assemble_A_and_F(), gradient(), main(), and objective().


The documentation for this class was generated from the following files: