Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
optimizer.f90
Go to the documentation of this file.
1
34
35module optimizer
36 !-----------------------------------------------------------!
37 ! An abstract type optimizer is defined to solve the design !
38 ! optimization problem using a specific type of optimizer !
39 ! algorithm, e.g. MMA. !
40 !-----------------------------------------------------------!
41
42 use json_module, only: json_file
43 use simulation_m, only: simulation_t
44 use problem, only: problem_t
45 use design, only: design_t
46 use num_types, only: rp
47 use logger, only: neko_log
48 use profiler, only: profiler_start_region, profiler_end_region
49
50 implicit none
51 private
52
54 type, abstract :: optimizer_t
55 private
56
58 integer, public :: max_iterations
60 real(kind=rp), public :: tolerance
61
62 contains
64 procedure(optimizer_init_from_json), pass(this), public, deferred :: &
65 init_from_json
67 procedure, pass(this), public :: run => optimizer_run
69 procedure(optimizer_initialize), pass(this), public, deferred :: &
70 initialize
72 procedure(optimizer_step), pass(this), public, deferred :: step
74 procedure(optimizer_free), pass(this), public, deferred :: free
75
77 procedure(optimizer_validate), pass(this), public, deferred :: validate
79 procedure(optimizer_write), pass(this), public, deferred :: write
80
82 procedure, pass(this) :: init_base => optimizer_init_base
84 procedure, pass(this) :: free_base => optimizer_free_base
85
86 end type optimizer_t
87
88 ! -------------------------------------------------------------------------- !
89 ! Interface for the optimizer module.
90
91 abstract interface
92
93 subroutine optimizer_init_from_json(this, parameters, problem, design, &
94 simulation)
95 import optimizer_t, json_file, simulation_t, problem_t, design_t, rp
96 class(optimizer_t), intent(inout) :: this
97 type(json_file), intent(inout) :: parameters
98 class(problem_t), intent(inout) :: problem
99 class(design_t), intent(in) :: design
100 type(simulation_t), optional, intent(in) :: simulation
101 end subroutine optimizer_init_from_json
102
106 subroutine optimizer_initialize(this, problem, design, simulation)
108 class(optimizer_t), intent(inout) :: this
109 class(problem_t), intent(inout) :: problem
110 class(design_t), intent(inout) :: design
111 type(simulation_t), optional, intent(inout) :: simulation
112 end subroutine optimizer_initialize
113
115 logical function optimizer_step(this, iter, problem, design, simulation)
117 integer, intent(in) :: iter
118 class(optimizer_t), intent(inout) :: this
119 class(problem_t), intent(inout) :: problem
120 class(design_t), intent(inout) :: design
121 type(simulation_t), optional, intent(inout) :: simulation
122 end function optimizer_step
123
125 subroutine optimizer_write(this, iter, problem)
127 class(optimizer_t), intent(inout) :: this
128 integer, intent(in) :: iter
129 class(problem_t), intent(in) :: problem
130 end subroutine optimizer_write
131
133 subroutine optimizer_free(this)
134 import optimizer_t
135 class(optimizer_t), intent(inout) :: this
136 end subroutine optimizer_free
137
139 subroutine optimizer_validate(this, problem, design)
141 class(optimizer_t), intent(inout) :: this
142 class(problem_t), intent(in) :: problem
143 class(design_t), intent(in) :: design
144 end subroutine optimizer_validate
145 end interface
146
147 ! -------------------------------------------------------------------------- !
148 ! Interfaces for the factory functions
149
157 module subroutine optimizer_factory(object, parameters, problem, design, &
158 simulation)
159 class(optimizer_t), allocatable, intent(inout) :: object
160 type(json_file), intent(inout) :: parameters
161 class(problem_t), intent(inout) :: problem
162 class(design_t), intent(in) :: design
163 type(simulation_t), optional, intent(in) :: simulation
164 end subroutine optimizer_factory
165 end interface optimizer_factory
166
167 public :: optimizer_t, optimizer_factory
168
169contains
170
171 ! -------------------------------------------------------------------------- !
172 ! Base initializer and free routines
173
178 subroutine optimizer_init_base(this, max_iterations, tolerance)
179 class(optimizer_t), intent(inout) :: this
180 integer, intent(in) :: max_iterations
181 real(kind=rp), intent(in) :: tolerance
182
183 this%max_iterations = max_iterations
184 this%tolerance = tolerance
185
186 end subroutine optimizer_init_base
187
190 subroutine optimizer_free_base(this)
191 class(optimizer_t), intent(inout) :: this
192 end subroutine optimizer_free_base
193
194 ! -------------------------------------------------------------------------- !
195 ! Optimization loop routine
196
205 subroutine optimizer_run(this, problem, design, simulation)
206 class(optimizer_t), intent(inout) :: this
207 class(problem_t), intent(inout) :: problem
208 class(design_t), intent(inout) :: design
209 type(simulation_t), optional, intent(inout) :: simulation
210 logical :: converged
211 character(len=256) :: msg
212 integer :: iter
213
214 ! Prepare the problem state before starting the optimization
215 call this%initialize(problem, design, simulation)
216 call this%write(0, problem)
217 call design%write(0)
218
219 call neko_log%section('Optimization Loop')
220
221 iter = 1
222 converged = .false.
223 do while (iter .le. this%max_iterations .and. .not. converged)
224
225 call profiler_start_region('Optimizer iteration')
226 converged = this%step(iter, problem, design, simulation)
227 call profiler_end_region('Optimizer iteration')
228
229 ! Log the progress and outputs
230 call this%write(iter, problem)
231 call design%write(iter)
232
233 iter = iter + 1
234 end do
235
236 call neko_log%end_section()
237
238 ! Check that the final design is valid
239 call this%validate(problem, design)
240
241 if (.not. converged) then
242 write(msg, '(A,I0,A)') 'Optimizer did not converge in ', &
243 this%max_iterations, ' iterations.'
244 call neko_log%warning(msg)
245 else
246 write(msg, '(A,I0,A)') 'Optimizer converged after ', iter, &
247 ' iterations.'
248 call neko_log%message(msg)
249 end if
250
251 end subroutine optimizer_run
252
253
254end module optimizer
Factory function for the optimizer.
Interface for optimizer initialization.
Definition optimizer.f90:93
Implements the design_t.
Definition design.f90:36
Module for handling the optimization problem.
Definition problem.f90:41
Implements the steady_problem_t type.
An abstract design type.
Definition design.f90:54
Abstract optimizer class.
Definition optimizer.f90:54
The abstract problem type.
Definition problem.f90:67