libMesh
Loading...
Searching...
No Matches
optimization_system.C
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 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
29namespace 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
57
58 // clear the parent data
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
80
81
83initialize_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 {
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,
109 n_local_rows,
111 max_nnz,
112 max_nnz);
113
114 eq_constraint_jac_sparsity = constraint_jac_sparsity;
115}
116
117
119initialize_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 {
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,
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
processor_id_type size() const
This is the EquationSystems class.
Manages consistently variables, degrees of freedom, coefficient vectors, and matrices for implicit sy...
virtual void clear() override
Clear all the data structures associated with the system.
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
This base class can be inherited from to provide interfaces to optimization solvers from different pa...
std::unique_ptr< NumericVector< Number > > C_ineq
The vector that stores inequality constraints.
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.
std::unique_ptr< NumericVector< Number > > lambda_ineq
OptimizationSystem(EquationSystems &es, const std::string &name, const unsigned int number)
Constructor.
virtual void solve() override
Solves the optimization problem.
std::vector< std::set< numeric_index_type > > eq_constraint_jac_sparsity
A copy of the equality and inequality constraint Jacobian sparsity patterns.
std::vector< std::set< numeric_index_type > > ineq_constraint_jac_sparsity
std::unique_ptr< SparseMatrix< Number > > C_eq_jac
The sparse matrix that stores the Jacobian of C_eq.
std::unique_ptr< NumericVector< Number > > C_eq
The vector that stores equality constraints.
virtual void reinit() override
Reinitializes the member data fields associated with the system, so that, e.g., assemble() may be use...
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< SparseMatrix< Number > > C_ineq_jac
The sparse matrix that stores the Jacobian of C_ineq.
virtual void init_data() override
Initializes new data members of the system.
std::unique_ptr< NumericVector< Number > > lambda_eq
Vectors to store the dual variables associated with equality and inequality constraints.
virtual void clear() override
Clear all the data structures associated with the system.
std::unique_ptr< OptimizationSolver< Number > > optimization_solver
The OptimizationSolver that is used for performing the optimization.
const Parallel::Communicator & comm() const
Generic sparse matrix.
virtual void reinit()
Reinitializes degrees of freedom and other required data on the current mesh.
Definition system.C:442
dof_id_type n_dofs() const
Definition system.C:118
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:756
dof_id_type n_local_dofs() const
Definition system.C:155
virtual void init_data()
Initializes the data for the system.
Definition system.C:207
virtual void update()
Update the local values to reflect the solution on neighboring processors.
Definition system.C:498
const DofMap & get_dof_map() const
Definition system.h:2417
The libMesh namespace provides an interface to certain functionality in the library.
dof_id_type numeric_index_type
Definition id_types.h:99