Skip to content

Commit b0bb4e9

Browse files
authored
Merge pull request #72 from JuliaSmoothOptimizers/fix-docstrings
Fix align in docstrings
2 parents 97c2bf1 + ae45226 commit b0bb4e9

File tree

7 files changed

+179
-80
lines changed

7 files changed

+179
-80
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ s.t. y solution of a PDE(κ,u)=0
2929
lvar <= (κ,y,u) <= uvar
3030
```
3131

32-
We refer to the the repository [PDEOptimizationProblems](https://github.com/tmigot/PDEOptimizationProblems) for examples of problems of different types: calculus of variations, optimal control problem, PDE-constrained problems, and mixed PDE-contrained problems with both function and vector unknowns.
32+
We refer to the the repository [PDEOptimizationProblems](https://github.com/tmigot/PDEOptimizationProblems) for examples of problems of different types: calculus of variations, optimal control problem, PDE-constrained problems, and mixed PDE-contrained problems with both function and algebraic unknowns.
3333

3434
## Installation
3535

@@ -66,7 +66,6 @@ using Gridap, PDENLPModels
6666
reffe_con = ReferenceFE(lagrangian, valuetype, 1)
6767
Xcon = TestFESpace(model, reffe_con; conformity = :H1)
6868
Ycon = TrialFESpace(Xcon)
69-
Y = MultiFieldFESpace([Ypde, Ycon])
7069

7170
# Integration machinery
7271
trian = Triangulation(model)
@@ -86,14 +85,13 @@ using Gridap, PDENLPModels
8685
function res(y, u, v)
8786
∫(∇(v) ∇(y) - v * u - v * h) *
8887
end
89-
op = FEOperator(res, Y, Xpde)
9088

9189
# initial guess
92-
npde = Gridap.FESpaces.num_free_dofs(Ypde)
93-
ncon = Gridap.FESpaces.num_free_dofs(Ycon)
90+
npde = num_free_dofs(Ypde)
91+
ncon = num_free_dofs(Ycon)
9492
xin = zeros(npde + ncon)
9593

96-
nlp = GridapPDENLPModel(xin, f, trian, Ypde, Ycon, Xpde, Xcon, op, name = "Control elastic membrane")
94+
nlp = GridapPDENLPModel(xin, f, trian, Ypde, Ycon, Xpde, Xcon, res, name = "Control elastic membrane")
9795
```
9896

9997
## References

src/GridapPDENLPModel.jl

Lines changed: 146 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
A composite type that represents the main features of the PDE-constrained optimization problem
44
55
---
6-
The following arguments are accepted:
7-
- tnrj : structure representing the objective term
8-
- Ypde : TrialFESpace for the solution of the PDE
9-
- Ycon : TrialFESpace for the parameter
10-
- Xpde : TestFESpace for the solution of the PDE
11-
- Xcon : TestFESpace for the parameter
12-
- Y : concatenated TrialFESpace
13-
- X : concatenated TestFESpace
14-
- op : operator representing the PDE-constraint (nothing if no constraints)
15-
- nvar_pde :number of dofs in the solution functions
16-
- nvar_con : number of dofs in the control functions
17-
- nparam: : number of real unknowns
18-
- nnzh_obj : number of nonzeros elements in the objective hessian
19-
- Hrows : store the structure for the hessian of the lagrangian
20-
- Hcols : store the structure for the hessian of the lagrangian
21-
- Jrows : store the structure for the hessian of the jacobian
22-
- Jcols : store the structure for the hessian of the jacobian
6+
`PDENLPMeta` contains the following attributes:
7+
- `tnrj` : structure representing the objective term
8+
- `Ypde` : TrialFESpace for the solution of the PDE
9+
- `Ycon` : TrialFESpace for the parameter
10+
- `Xpde` : TestFESpace for the solution of the PDE
11+
- `Xcon` : TestFESpace for the parameter
12+
- `Y` : concatenated TrialFESpace
13+
- `X` : concatenated TestFESpace
14+
- `op` : operator representing the PDE-constraint (nothing if no constraints)
15+
- `nvar_pde` :number of dofs in the solution functions
16+
- `nvar_con` : number of dofs in the control functions
17+
- `nparam` : number of real unknowns
18+
- `nnzh_obj` : number of nonzeros elements in the objective hessian
19+
- `Hrows` : store the structure for the hessian of the lagrangian
20+
- `Hcols` : store the structure for the hessian of the lagrangian
21+
- `Jrows` : store the structure for the hessian of the jacobian
22+
- `Jcols` : store the structure for the hessian of the jacobian
2323
"""
2424
struct PDENLPMeta{NRJ <: AbstractEnergyTerm, Op <: Union{FEOperator, Nothing}}
2525
# For the objective function
@@ -48,6 +48,11 @@ struct PDENLPMeta{NRJ <: AbstractEnergyTerm, Op <: Union{FEOperator, Nothing}}
4848
Jcols::AbstractVector{Int}
4949
end
5050

51+
"""
52+
PDEWorkspace
53+
54+
Pre-allocated memory for `GridapPDENLPModel`.
55+
"""
5156
struct PDEWorkspace{T, S, M}
5257
Hvals::S # vector of values of the Hessian matrix
5358
Jvals::S # vector of values of the Jacobian matrix
@@ -68,53 +73,148 @@ function PDEWorkspace(T, S, nvar, ncon, nparam, nnzh, nnzj)
6873
end
6974

7075
@doc raw"""
71-
PDENLPModels using Gridap.jl
76+
`GridapPDENLPModel` returns an instance of an [`AbstractNLPModel`](https://github.com/JuliaSmoothOptimizers/NLPModels.jl) using [Gridap.jl](https://github.com/gridap/Gridap.jl) for the discretization of the domain with finite-elements.
77+
Given a domain `Ω ⊂ ℜᵈ` Find a state function y: `Ω -> Y`, a control function u: `Ω -> U` and an algebraic vector κ ∈ ℜⁿ satisfying
78+
```math
79+
\begin{aligned}
80+
\min_{κ,y,u} \ & ∫_Ω​ f(κ,y,u) dΩ​ \\
81+
\mbox{ s.t. } & y \mbox{ solution of } PDE(κ,u)=0, \\
82+
& lcon <= c(κ,y,u) <= ucon, \\
83+
& lvar <= (κ,y,u) <= uvar.
84+
\end{aligned}
85+
```
86+
87+
The [weak formulation of the PDE](https://en.wikipedia.org/wiki/Weak_formulation) is then:
88+
`res((y,u),(v,q)) = ∫ v PDE(κ,y,u) + ∫ q c(κ,y,u)`
89+
90+
where the unknown `(y,u)` is a `MultiField` see [Tutorials 7](https://gridap.github.io/Tutorials/stable/pages/t007_darcy/)
91+
and [8](https://gridap.github.io/Tutorials/stable/pages/t008_inc_navier_stokes/) of Gridap.
7292
73-
https://github.com/gridap/Gridap.jl
74-
Cite: Badia, S., & Verdugo, F. (2020). Gridap: An extensible Finite Element toolbox in Julia.
75-
Journal of Open Source Software, 5(52), 2520.
93+
## Constructors
7694
77-
Find functions (y,u): Y -> ℜⁿ x ℜⁿ and κ ∈ ℜⁿ satisfying
95+
Main constructor:
7896
79-
min ∫_Ω​ f(κ,y,u) dΩ​
80-
s.t. y solution of a PDE(κ,u)=0
81-
lcon <= c(κ,y,u) <= ucon
82-
lvar <= (κ,y,u) <= uvar
97+
GridapPDENLPModel(::NLPModelMeta, ::Counters, ::PDENLPMeta, ::PDEWorkspace)
8398
84-
```math
85-
\begin{aligned}
86-
\min_{κ,y,u} \ & ∫_Ω​ f(κ,y,u) dΩ​ \\
87-
\mbox{ s.t. } & y \mbox{ solution of } PDE(κ,u)=0, \\
88-
& lcon <= c(κ,y,u) <= ucon, \\
89-
& lvar <= (κ,y,u) <= uvar.
90-
\end{aligned}
91-
```
99+
This is the main constructors with the attributes of the `GridapPDENLPModel`:
100+
- `meta::NLPModelMeta`: usual `meta` for NLPModels, see [doc here](https://juliasmoothoptimizers.github.io/NLPModels.jl/dev/models/);
101+
- `counters::Counters`: usual `counters` for NLPModels, see [doc here](https://juliasmoothoptimizers.github.io/NLPModels.jl/dev/tools/);
102+
- `pdemeta::PDENLPMeta`: metadata specific to `GridapPDENLPModel`;
103+
- `workspace::PDEWorkspace`: Pre-allocated memory for `GridapPDENLPModel`.
92104
93-
The weak formulation is then:
94-
res((y,u),(v,q)) = ∫ v PDE(κ,y,u) + ∫ q c(κ,y,u)
105+
More practical constructors are also available.
95106
96-
where the unknown (y,u) is a MultiField see [Tutorials 7](https://gridap.github.io/Tutorials/stable/pages/t007_darcy/)
97-
and [8](https://gridap.github.io/Tutorials/stable/pages/t008_inc_navier_stokes/) of Gridap.
107+
- For unconstrained problems:
98108
99-
Main constructor:
109+
GridapPDENLPModel(x0, ::Function, ::Union{Measure, Triangulation}, Ypde::FESpace, Xpde::FESpace; kwargs...)
110+
111+
GridapPDENLPModel(x0, ::AbstractEnergyTerm, ::Union{Measure, Triangulation}, Ypde::FESpace, Xpde::FESpace; kwargs...)
112+
113+
- For constrained problems without controls:
114+
115+
GridapPDENLPModel(x0, ::Function, ::Union{Measure, Triangulation}, Ypde::FESpace, Xpde::FESpace, c::Union{Function, FEOperator}; kwargs...)
116+
117+
GridapPDENLPModel(x0, ::AbstractEnergyTerm, Ypde::FESpace, Xpde::FESpace, c::Union{Function, FEOperator}; kwargs...)
118+
119+
- For general constrained problems:
120+
121+
GridapPDENLPModel(x0, ::Function, ::Union{Measure, Triangulation}, Ypde::FESpace, Ycon::FESpace, Xpde::FESpace, Xcon::FESpace, c::Union{Function, FEOperator}; kwargs...)
100122
101-
`GridapPDENLPModel(:: NLPModelMeta, :: Counters, :: AbstractEnergyTerm, :: FESpace, :: Union{FESpace,Nothing}, :: FESpace, :: Union{FESpace,Nothing}, :: FESpace, :: Union{FESpace,Nothing}, :: Union{FEOperator, Nothing}, :: Int, :: Int, :: Int)`
123+
GridapPDENLPModel(x0, ::AbstractEnergyTerm, Ypde::FESpace, Ycon::FESpace, Xpde::FESpace, Xcon::FESpace, c::Union{Function, FEOperator}; kwargs...)
124+
125+
where the different arguments are:
126+
- `x0`: initial guess for the system of size `≥ num_free_dofs(Ypde) + num_free_dofs(Ycon)`;
127+
- `f`: objective function, the number of arguments depend on the application `(y)` or `(y,u)` or `(y,u,θ)`;
128+
- `Ypde`: trial space for the state;
129+
- `Ycon`: trial space for the control (`VoidFESpace` if none);
130+
- `Xpde`: test space for the state;
131+
- `Xcon`: test space for the control (`VoidFESpace` if none);
132+
- `c`: operator/function for the PDE-constraint, were we assume by default that the right-hand side is zero (otw. use `lcon` and `ucon` keywords), the number of arguments depend on the application `(y,v)` or `(y,u,v)` or `(y,u,θ,v)`.
133+
134+
If `length(x0) > num_free_dofs(Ypde) + num_free_dofs(Ycon)`, then the additional components are considered algebraic variables.
135+
136+
The function `f` and `c` must return integrals complying with Gridap's functions with a `Measure/Triangulation` given in the arguments of `GridapPDENLPModel`.
137+
Internally, the objective function `f` and the `Measure/Triangulation` are combined to instantiate an `AbstractEnergyTerm`.
102138
103139
The following keyword arguments are available to all constructors:
104140
- `name`: The name of the model (default: "Generic")
105141
The following keyword arguments are available to the constructors for
106142
constrained problems:
107-
- `lin`: An array of indexes of the linear constraints
108-
(default: `Int[]` or 1:ncon if c is an AffineFEOperator)
143+
- `lin`: An array of indexes of the linear constraints (default: `Int[]`)
109144
110145
The following keyword arguments are available to the constructors for
111146
constrained problems explictly giving lcon and ucon:
112147
- `y0`: An inital estimate to the Lagrangian multipliers (default: zeros)
113148
149+
The bounds on the variables are given as `AbstractVector` via keywords arguments as well:
150+
- either with `lvar` and `uvar`, or,
151+
- `lvary`, `lvaru`, `lvark`, and `uvary`, `uvaru`, `uvark`.
152+
114153
Notes:
115-
- We handle two types of FEOperator: AffineFEOperator, and FEOperatorFromWeakForm
116-
- If lcon and ucon are not given, they are assumed zeros.
117-
- If the type can't be deduced from the argument, it is Float64.
154+
- We handle two types of `FEOperator`: `AffineFEOperator`, and `FEOperatorFromWeakForm`.
155+
- If `lcon` and `ucon` are not given, they are assumed zeros.
156+
- If the type can't be deduced from the argument, it is `Float64`.
157+
158+
## Example
159+
160+
```julia
161+
using Gridap, PDENLPModels
162+
163+
# Definition of the domain
164+
n = 100
165+
domain = (-1, 1, -1, 1)
166+
partition = (n, n)
167+
model = CartesianDiscreteModel(domain, partition)
168+
169+
# Definition of the spaces:
170+
valuetype = Float64
171+
reffe = ReferenceFE(lagrangian, valuetype, 2)
172+
Xpde = TestFESpace(model, reffe; conformity = :H1, dirichlet_tags = "boundary")
173+
y0(x) = 0.0
174+
Ypde = TrialFESpace(Xpde, y0)
175+
176+
reffe_con = ReferenceFE(lagrangian, valuetype, 1)
177+
Xcon = TestFESpace(model, reffe_con; conformity = :H1)
178+
Ycon = TrialFESpace(Xcon)
179+
180+
# Integration machinery
181+
trian = Triangulation(model)
182+
degree = 1
183+
dΩ = Measure(trian, degree)
184+
185+
# Objective function:
186+
yd(x) = -x[1]^2
187+
α = 1e-2
188+
function f(y, u)
189+
∫(0.5 * (yd - y) * (yd - y) + 0.5 * α * u * u) * dΩ
190+
end
191+
192+
# Definition of the constraint operator
193+
ω = π - 1 / 8
194+
h(x) = -sin(ω * x[1]) * sin(ω * x[2])
195+
function res(y, u, v)
196+
∫(∇(v) ⊙ ∇(y) - v * u - v * h) * dΩ
197+
end
198+
199+
# initial guess
200+
npde = num_free_dofs(Ypde)
201+
ncon = num_free_dofs(Ycon)
202+
xin = zeros(npde + ncon)
203+
204+
nlp = GridapPDENLPModel(xin, f, trian, Ypde, Ycon, Xpde, Xcon, res, name = "Control elastic membrane")
205+
```
206+
207+
You can also check the tutorial [Solve a PDE-constrained optimization problem](https://jso-docs.github.io/solve-pdenlpmodels-with-jsosolvers/) on JSO's website, [juliasmoothoptimizers.github.io](https://juliasmoothoptimizers.github.io).
208+
209+
We refer to the folder `test/problems` for more examples of problems of different types:
210+
- calculus of variations,
211+
- optimal control problem,
212+
- PDE-constrained problems,
213+
- mixed PDE-contrained problems with both function and algebraic unknowns.
214+
An alternative is to visit the repository [PDEOptimizationProblems](https://github.com/tmigot/PDEOptimizationProblems) that contains a collection of test problems.
215+
216+
Without objective function, the problem reduces to a classical PDE and we refer to [Gridap tutorials](https://github.com/gridap/Tutorials) for examples.
217+
118218
"""
119219
mutable struct GridapPDENLPModel{T, S, NRJ, Op} <: AbstractNLPModel{T, S}
120220
meta::NLPModelMeta{T, S}
@@ -129,7 +229,7 @@ for field in fieldnames(PDENLPMeta)
129229
@doc """
130230
$($meth)(nlp)
131231
$($meth)(meta)
132-
Return the value $($(QuoteNode(field))) from meta or nlp.meta.
232+
Return the value `$($(QuoteNode(field)))` from meta or nlp.meta.
133233
"""
134234
$meth(meta::PDENLPMeta) = getproperty(meta, $(QuoteNode(field)))
135235
end

src/PDENLPModels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export reset!
163163
export split_vectors
164164

165165
"""
166-
`split_vectors(::GridapPDENLPModel, x)`
166+
split_vectors(::GridapPDENLPModel, x)
167167
168168
Take a vector x and returns a splitting in terms of `y`, `u` and `θ`.
169169
"""

src/additional_obj_terms.jl

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ abstract type AbstractEnergyTerm end
33
"""
44
Return the integral of the objective function
55
6-
`_obj_integral(:: AbstractEnergyTerm, :: FEFunctionType, :: AbstractVector)`
6+
_obj_integral(:: AbstractEnergyTerm, :: FEFunctionType, :: AbstractVector)
77
88
See also: `MixedEnergyFETerm`, `EnergyFETerm`, `NoFETerm`,
99
`_obj_cell_integral`, `_compute_gradient_k`,
@@ -14,7 +14,7 @@ function _obj_integral end
1414
"""
1515
Return the derivative of the objective function w.r.t. κ.
1616
17-
`_compute_gradient_k!(::AbstractVector, :: AbstractEnergyTerm, :: FEFunctionType, :: AbstractVector)`
17+
_compute_gradient_k!(::AbstractVector, :: AbstractEnergyTerm, :: FEFunctionType, :: AbstractVector)
1818
1919
See also: `MixedEnergyFETerm`, `EnergyFETerm`, `NoFETerm`, `_obj_integral`,
2020
`_obj_cell_integral`, `_compute_hess_k_coo`
@@ -24,7 +24,7 @@ function _compute_gradient_k! end
2424
"""
2525
Return the gradient of the objective function and set it in place.
2626
27-
`_compute_gradient!(:: AbstractVector, :: EnergyFETerm, :: AbstractVector, :: FEFunctionType, :: FESpace, :: FESpace)`
27+
_compute_gradient!(:: AbstractVector, :: EnergyFETerm, :: AbstractVector, :: FEFunctionType, :: FESpace, :: FESpace)
2828
2929
See also: `MixedEnergyFETerm`, `EnergyFETerm`, `NoFETerm`, `_obj_integral`,
3030
`_obj_cell_integral`, `_compute_hess_k_coo`
@@ -34,27 +34,25 @@ function _compute_gradient! end
3434
"""
3535
Return the values of the hessian w.r.t. κ of the objective function.
3636
37-
`_compute_hess_k_vals!(:: AbstractVector, :: AbstractNLPModel, :: AbstractEnergyTerm, :: AbstractVector, :: AbstractVector)`
37+
_compute_hess_k_vals!(:: AbstractVector, :: AbstractNLPModel, :: AbstractEnergyTerm, :: AbstractVector, :: AbstractVector)
3838
3939
See also: `MixedEnergyFETerm`, `EnergyFETerm`, `NoFETerm`, `_obj_integral`,
4040
`_obj_cell_integral`, `_compute_gradient_k`
4141
"""
4242
function _compute_hess_k_vals! end
4343

4444
@doc raw"""
45-
FETerm modeling the objective function when there are no integral objective.
45+
`AbstractEnergyTerm` modeling the objective function when there are no integral objective
4646
4747
```math
4848
\begin{aligned}
49-
f(\kappa)
49+
f(\kappa).
5050
\end{aligned}
51-
```
51+
```
5252
5353
Constructors:
54-
55-
`NoFETerm()`
56-
57-
`NoFETerm(:: Function)`
54+
NoFETerm()
55+
NoFETerm(:: Function)
5856
5957
See also: `MixedEnergyFETerm`, `EnergyFETerm`, `_obj_cell_integral`, `_obj_integral`, `_compute_gradient_k!`
6058
"""
@@ -108,19 +106,19 @@ function _compute_hess_k_vals!(
108106
end
109107

110108
@doc raw"""
111-
FETerm modeling the objective function of the optimization problem.
109+
`AbstractEnergyTerm` modeling the objective function of the optimization problem
112110
113111
```math
114112
\begin{aligned}
115-
\int_{\Omega} f(y,u) d\Omega,
113+
\int_{\Omega} f(y,u) d\Omega.
116114
\end{aligned}
117115
```
118116
119117
Constructor:
120118
121-
`EnergyFETerm(:: Function, :: Triangulation, :: Measure)`
119+
EnergyFETerm(:: Function, :: Triangulation, :: Measure)
122120
123-
See also: MixedEnergyFETerm, NoFETerm, `_obj_cell_integral`, `_obj_integral`,
121+
See also: `MixedEnergyFETerm`, `NoFETerm`, `_obj_cell_integral`, `_obj_integral`,
124122
`_compute_gradient_k!`
125123
"""
126124
struct EnergyFETerm <: AbstractEnergyTerm
@@ -183,18 +181,18 @@ function _compute_hess_k_vals!(
183181
end
184182

185183
@doc raw"""
186-
FETerm modeling the objective function of the optimization problem with
187-
functional and discrete unknowns.
184+
`AbstractEnergyTerm` modeling the objective function of the optimization problem with
185+
functional and discrete unknowns
188186
189187
```math
190188
\begin{aligned}
191-
\int_{\Omega} f(y,u,\kappa) d\Omega,
189+
\int_{\Omega} f(y,u,\kappa) d\Omega.
192190
\end{aligned}
193191
```
194192
195193
Constructor:
196194
197-
`MixedEnergyFETerm(:: Function, :: Triangulation, :: Int)`
195+
MixedEnergyFETerm(:: Function, :: Triangulation, :: Int)
198196
199197
See also: `EnergyFETerm`, `NoFETerm`, `_obj_cell_integral`, `_obj_integral`,
200198
`_compute_gradient_k!`

0 commit comments

Comments
 (0)