libMesh
optimization_system.C
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 // Local includes
21 #include "libmesh/equation_systems.h"
22 #include "libmesh/libmesh_logging.h"
23 #include "libmesh/sparse_matrix.h"
24 #include "libmesh/numeric_vector.h"
25 #include "libmesh/dof_map.h"
26 #include "libmesh/optimization_solver.h"
27 #include "libmesh/optimization_system.h"
28 
29 namespace libMesh
30 {
31 
33  const std::string & name_in,
34  const unsigned int number_in) :
35 
36  Parent(es, name_in, number_in),
37  optimization_solver(OptimizationSolver<Number>::build(*this)),
38  C_eq(NumericVector<Number>::build(this->comm())),
39  C_eq_jac(SparseMatrix<Number>::build(this->comm())),
40  C_ineq(NumericVector<Number>::build(this->comm())),
41  C_ineq_jac(SparseMatrix<Number>::build(this->comm())),
42  lambda_eq(NumericVector<Number>::build(this->comm())),
43  lambda_ineq(NumericVector<Number>::build(this->comm()))
44 {
45 }
46 
47 
48 
50 
51 
52 
54 {
55  // clear the optimization solver
56  optimization_solver->clear();
57 
58  // clear the parent data
59  Parent::clear();
60 }
61 
62 
64 {
65  this->add_vector("lower_bounds");
66  this->add_vector("upper_bounds");
67 
69 
70  optimization_solver->clear();
71 }
72 
73 
75 {
76  optimization_solver->clear();
77 
79 }
80 
81 
83 initialize_equality_constraints_storage(const std::vector<std::set<numeric_index_type>> & constraint_jac_sparsity)
84 {
85  numeric_index_type n_eq_constraints =
86  cast_int<numeric_index_type>(constraint_jac_sparsity.size());
87 
88  // Assign rows to each processor as evenly as possible
89  unsigned int n_procs = comm().size();
90  numeric_index_type n_local_rows = n_eq_constraints / n_procs;
91  if (comm().rank() < (n_eq_constraints % n_procs))
92  n_local_rows++;
93 
94  C_eq->init(n_eq_constraints, n_local_rows, false, PARALLEL);
95  lambda_eq->init(n_eq_constraints, n_local_rows, false, PARALLEL);
96 
97  // Get the maximum number of non-zeros per row
98  numeric_index_type max_nnz = 0;
99  for (numeric_index_type i=0; i<n_eq_constraints; i++)
100  {
101  numeric_index_type nnz =
102  cast_int<numeric_index_type>(constraint_jac_sparsity[i].size());
103  if (nnz > max_nnz)
104  max_nnz = nnz;
105  }
106 
107  C_eq_jac->init(n_eq_constraints,
108  get_dof_map().n_dofs(),
109  n_local_rows,
111  max_nnz,
112  max_nnz);
113 
114  eq_constraint_jac_sparsity = constraint_jac_sparsity;
115 }
116 
117 
119 initialize_inequality_constraints_storage(const std::vector<std::set<numeric_index_type>> & constraint_jac_sparsity)
120 {
121  numeric_index_type n_ineq_constraints =
122  cast_int<numeric_index_type>(constraint_jac_sparsity.size());
123 
124  // Assign rows to each processor as evenly as possible
125  unsigned int n_procs = comm().size();
126  numeric_index_type n_local_rows = n_ineq_constraints / n_procs;
127  if (comm().rank() < (n_ineq_constraints % n_procs))
128  n_local_rows++;
129 
130  C_ineq->init(n_ineq_constraints, n_local_rows, false, PARALLEL);
131  lambda_ineq->init(n_ineq_constraints, n_local_rows, false, PARALLEL);
132 
133  // Get the maximum number of non-zeros per row
134  numeric_index_type max_nnz = 0;
135  for (numeric_index_type i=0; i<n_ineq_constraints; i++)
136  {
137  numeric_index_type nnz =
138  cast_int<numeric_index_type>(constraint_jac_sparsity[i].size());
139  if (nnz > max_nnz)
140  max_nnz = nnz;
141  }
142 
143  C_ineq_jac->init(n_ineq_constraints,
144  get_dof_map().n_dofs(),
145  n_local_rows,
147  max_nnz,
148  max_nnz);
149 
150  ineq_constraint_jac_sparsity = constraint_jac_sparsity;
151 }
152 
153 
155 {
156  LOG_SCOPE("solve()", "OptimizationSystem");
157 
158  optimization_solver->init();
159  optimization_solver->solve ();
160 
161  this->update();
162 }
163 
164 
165 } // namespace libMesh
This is the EquationSystems class.
void initialize_inequality_constraints_storage(const std::vector< std::set< numeric_index_type >> &constraint_jac_sparsity)
Initialize storage for the inequality constraints, as per initialize_equality_constraints_storage.
std::unique_ptr< NumericVector< Number > > lambda_ineq
virtual void reinit()
Reinitializes degrees of freedom and other required data on the current mesh.
Definition: system.C:454
virtual void solve() override
Solves the optimization problem.
virtual void clear() override
Clear all the data structures associated with the system.
virtual void init_data() override
Initializes new data members of the system.
virtual void init_data()
Initializes the data for the system.
Definition: system.C:208
std::unique_ptr< NumericVector< Number > > C_eq
The vector that stores equality constraints.
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
Definition: vector_fe_ex5.C:44
const Parallel::Communicator & comm() 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.
Definition: system.C:768
The libMesh namespace provides an interface to certain functionality in the library.
dof_id_type n_local_dofs() const
Definition: system.C:158
std::unique_ptr< SparseMatrix< Number > > C_ineq_jac
The sparse matrix that stores the Jacobian of C_ineq.
dof_id_type n_dofs() const
Definition: system.C:121
Generic sparse matrix.
Definition: vector_fe_ex5.C:46
processor_id_type size() const
dof_id_type numeric_index_type
Definition: id_types.h:99
Manages consistently variables, degrees of freedom, and coefficient vectors.
Definition: system.h:96
virtual void reinit() override
Reinitializes the member data fields associated with the system, so that, e.g., assemble() may be use...
std::vector< std::set< numeric_index_type > > ineq_constraint_jac_sparsity
std::vector< std::set< numeric_index_type > > eq_constraint_jac_sparsity
A copy of the equality and inequality constraint Jacobian sparsity patterns.
std::unique_ptr< SparseMatrix< Number > > C_eq_jac
The sparse matrix that stores the Jacobian of C_eq.
std::unique_ptr< OptimizationSolver< Number > > optimization_solver
The OptimizationSolver that is used for performing the optimization.
std::unique_ptr< NumericVector< Number > > lambda_eq
Vectors to store the dual variables associated with equality and inequality constraints.
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition: system.C:510
void initialize_equality_constraints_storage(const std::vector< std::set< numeric_index_type >> &constraint_jac_sparsity)
Initialize storage for the equality constraints, and the corresponding Jacobian.
This base class can be inherited from to provide interfaces to optimization solvers from different pa...
virtual void clear() override
Clear all the data structures associated with the system.
OptimizationSystem(EquationSystems &es, const std::string &name, const unsigned int number)
Constructor.
const DofMap & get_dof_map() const
Definition: system.h:2374
std::unique_ptr< NumericVector< Number > > C_ineq
The vector that stores inequality constraints.