Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
mma_optimizer.f90
Go to the documentation of this file.
1
34
35module mma_optimizer
36 use optimizer, only: optimizer_t
37 use mma, only: mma_t
38 use problem, only: problem_t
39 use num_types, only: rp
40 use utils, only: neko_error
41 use json_utils, only: json_get, json_get_or_default
42 use simulation_m, only: simulation_t
43 use design, only: design_t
44 use brinkman_design, only: brinkman_design_t
45 use constraint, only: constraint_t
47
48 ! External modules
49 use json_module, only: json_file
50 use vector, only: vector_t
51 use matrix, only: matrix_t
52 use math, only: abscmp
53 use profiler, only: profiler_start_region, profiler_end_region
54 use logger, only: neko_log
55 use csv_file, only: csv_file_t
56 use vector_math, only: vector_cmult
57 use matrix_math, only: matrix_cmult
58 use device, only: device_memcpy, device_to_host
59 use scratch_registry, only: neko_scratch_registry
60 use comm, only: pe_rank, neko_comm
61 use mpi_f08, only: mpi_barrier
62
63 implicit none
64 private
65
66 public :: mma_optimizer_t
67
68 ! Concrete type for MMA optimizer
69 type, extends(optimizer_t) :: mma_optimizer_t
70
71 type(mma_t), private :: mma
72
79 real(kind=rp), private :: scale = 1.0_rp
80 real(kind=rp), private :: scaling_factor = 1.0_rp
81 logical, private :: auto_scale = .false.
82 real(kind=rp) :: tolerance = 0.0_rp
83
84 ! Set to flags to remove logging for optimal performance
85 logical, private :: unconstrained_problem = .false.
86
88 logical, private :: enable_output = .true.
89 type(csv_file_t), private :: csv_log
90 contains
91
92 ! Override the deferred methods
93 generic :: init => init_from_json, init_from_components
94 procedure, pass(this) :: init_from_json => mma_optimizer_init_from_json
95 procedure, pass(this) :: init_from_components => &
96 mma_optimizer_init_from_components
97
98 procedure, pass(this) :: initialize => mma_optimizer_initialize
99 procedure, pass(this) :: step => mma_optimizer_step
100 procedure, pass(this) :: validate => mma_optimizer_validate
101 procedure, pass(this) :: write => mma_optimizer_write
102 procedure, pass(this) :: free => mma_optimizer_free
103
104 procedure, pass(this) :: save_checkpoint_components => &
105 mma_optimizer_save_checkpoint_components
106 procedure, pass(this) :: load_checkpoint_components => &
107 mma_optimizer_load_checkpoint_components
108
109 end type mma_optimizer_t
110
111contains
112
113 ! -------------------------------------------------------------------------- !
114 ! Allocator and deallocator methods for the MMA optimizer
115
117 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
118 simulation)
119 class(mma_optimizer_t), intent(inout) :: this
120 type(json_file), intent(inout) :: parameters
121 class(problem_t), intent(inout) :: problem
122 class(design_t), intent(in) :: design
123 type(simulation_t), optional, intent(in) :: simulation
124
125 ! Variables for settings
126 type(json_file) :: solver_parameters
127 logical :: enable_output
128 integer :: max_iterations
129 real(kind=rp) :: tolerance
130
131 ! Read the solver properties from the JSON file
132 call json_get(parameters, 'optimization.solver', solver_parameters)
133 call json_get_or_default(solver_parameters, 'max_iterations', &
134 max_iterations, 100)
135 call json_get_or_default(solver_parameters, 'tolerance', &
136 tolerance, 1.0e-3_rp)
137 call json_get_or_default(solver_parameters, 'enable_output', &
138 enable_output, .true.)
139 call this%read_base_settings(solver_parameters)
140
141 call this%init_from_components(problem, design, max_iterations, tolerance, &
142 enable_output, solver_parameters, simulation)
143
144 end subroutine mma_optimizer_init_from_json
145
147 subroutine mma_optimizer_init_from_components(this, problem, design, &
148 max_iterations, tolerance, enable_output, &
149 solver_parameters, simulation)
150 class(mma_optimizer_t), intent(inout) :: this
151 class(problem_t), intent(inout) :: problem
152 class(design_t), intent(in) :: design
153 integer, intent(in) :: max_iterations
154 real(kind=rp), intent(in) :: tolerance
155 logical, intent(in) :: enable_output
156 type(json_file), intent(inout), optional :: solver_parameters
157 type(simulation_t), intent(in), optional :: simulation
158
159 ! Local variables
160 type(vector_t), pointer :: x
161 integer :: ind
162 character(len=1024) :: header
163 class(constraint_t), allocatable :: dummy_con
164
165 call neko_log%section('Optimizer Initialization')
166
167 ! Check if the problem is unconstrained
168 this%unconstrained_problem = problem%get_n_constraints() .eq. 0
169 if (this%unconstrained_problem) then
170 call neko_log%message('Unconstrained problem detected. ' // &
171 'Adding a dummy constraint to enable MMA optimization.')
172
173 allocate(dummy_constraint_t::dummy_con)
174 select type (con => dummy_con)
175 type is (dummy_constraint_t)
176 call con%init_from_attributes(design)
177 end select
178
179 call problem%add_constraint(dummy_con)
180 if (allocated(dummy_con)) deallocate(dummy_con)
181 end if
182
183 ! Initialize mma_t, handling the dummy_constraint added for unconstrained
184 ! problems in mma_optimizer_run()
185 call neko_scratch_registry%request(x, ind, design%size(), .false.)
186
187 call design%get_values(x)
188 call this%mma%init(x, design%size(), problem%get_n_constraints(), &
189 solver_parameters, this%scale, this%auto_scale)
190
191 call neko_scratch_registry%relinquish(ind)
192
193 !set the enable_output flag
194 this%enable_output = enable_output
195 this%scaling_factor = this%scale
196 this%tolerance = tolerance
197
198 ! Initialize the logger
199 if (this%enable_output) then
200 call this%csv_log%init('optimization_data.csv')
201 header = 'iter, ' // trim(problem%get_log_header()) // &
202 ', KKTmax, KKTnorm2, scaling factor, ' // &
203 trim(this%mma%get_backend_and_subsolver())
204
205 call this%csv_log%set_header(trim(header))
206 end if
207
208 call this%init_base('MMA', max_iterations)
209
210 call neko_log%end_section()
211
212 end subroutine mma_optimizer_init_from_components
213
214 ! Free resources associated with the MMA optimizer
215 subroutine mma_optimizer_free(this)
216 class(mma_optimizer_t), intent(inout) :: this
217
218 ! Free MMA-specific data
219 call this%free_base()
220 call this%mma%free()
221 end subroutine mma_optimizer_free
222
223 ! -------------------------------------------------------------------------- !
224 ! Implementation of the deferred methods for the MMA optimizer
225
227 subroutine mma_optimizer_initialize(this, problem, design, simulation)
228 class(mma_optimizer_t), intent(inout) :: this
229 class(problem_t), intent(inout) :: problem
230 class(design_t), intent(inout) :: design
231 type(simulation_t), optional, intent(inout) :: simulation
232
233 type(vector_t), pointer :: x
234 type(vector_t), pointer :: constraint_value
235 type(vector_t), pointer :: objective_sensitivities
236 type(matrix_t), pointer :: constraint_sensitivities
237 integer :: n_design, n_constraint, indices(4)
238
239 n_design = design%size()
240 n_constraint = problem%get_n_constraints()
241
242 ! Grab some local pointers
243 call neko_scratch_registry%request(x, indices(1), n_design, .false.)
244 call neko_scratch_registry%request(constraint_value, indices(2), &
245 n_constraint, .false.)
246 call neko_scratch_registry%request(objective_sensitivities, indices(3), &
247 n_design, .false.)
248 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
249 n_constraint, n_design, .false.)
250
251 ! Evaluate the problem based on the updated design
252 call problem%compute(design, simulation)
253 call problem%compute_sensitivity(design, simulation)
254
255 ! Retrieve the updated objective and constraint values and sensitivities
256 call design%get_values(x)
257 call problem%get_constraint_values(constraint_value)
258
259 select type (des => design)
260 type is (brinkman_design_t)
261 call des%get_sensitivity(objective_sensitivities)
262 class default
263 call problem%get_objective_sensitivities(objective_sensitivities)
264 end select
265
266 call problem%get_constraint_sensitivities(constraint_sensitivities)
267
268 ! Check the KKT conditions and check for convergence
269 call this%mma%KKT(x, objective_sensitivities, &
270 constraint_value, constraint_sensitivities)
271
272 call neko_scratch_registry%relinquish(indices)
273 end subroutine mma_optimizer_initialize
274
276 function mma_optimizer_step(this, iter, problem, design, simulation) &
277 result(converged)
278 class(mma_optimizer_t), intent(inout) :: this
279 integer, intent(in) :: iter
280 class(problem_t), intent(inout) :: problem
281 class(design_t), intent(inout) :: design
282 type(simulation_t), optional, intent(inout) :: simulation
283
284 type(vector_t), pointer :: x
285 type(vector_t), pointer :: constraint_value
286 type(vector_t), pointer :: objective_sensitivities
287 type(matrix_t), pointer :: constraint_sensitivities
288 integer :: n_design, n_constraint, indices(4)
289
290 logical :: converged
291
292 n_design = design%size()
293 n_constraint = problem%get_n_constraints()
294
295 ! Grab some local pointers
296 call neko_scratch_registry%request(x, indices(1), n_design, .false.)
297 call neko_scratch_registry%request(constraint_value, indices(2), &
298 n_constraint, .false.)
299 call neko_scratch_registry%request(objective_sensitivities, indices(3), &
300 n_design, .false.)
301 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
302 n_constraint, n_design, .false.)
303
304 ! Retrieve the current objective and constraint values and sensitivities
305 call design%get_values(x)
306 call problem%get_constraint_values(constraint_value)
307
308 select type (des => design)
309 type is (brinkman_design_t)
310 call des%get_sensitivity(objective_sensitivities)
311 class default
312 call problem%get_objective_sensitivities(objective_sensitivities)
313 end select
314
315 call problem%get_constraint_sensitivities(constraint_sensitivities)
316
317 ! Execute the scaling
318 if (this%auto_scale) then
319 call constraint_value%copy_from(device_to_host, sync = .true.)
320 this%scaling_factor = abs(this%scale / constraint_value%x(1))
321 end if
322
323 if (.not. abscmp(this%scaling_factor, 1.0_rp)) then
324 call vector_cmult(constraint_value, this%scaling_factor)
325 call matrix_cmult(constraint_sensitivities, this%scaling_factor)
326 end if
327
328 ! Update the design variable
329 call this%mma%update(iter, x, objective_sensitivities, &
330 constraint_value, constraint_sensitivities)
331 call design%update_design(x)
332
333 ! Evaluate the problem based on the updated design
334 call problem%compute(design, simulation)
335 if (present(simulation) .and. this%enable_output) then
336 call simulation%write_forward(iter)
337 end if
338 call problem%compute_sensitivity(design, simulation)
339 if (present(simulation) .and. this%enable_output) then
340 call simulation%write_adjoint(iter)
341 end if
342
343 ! Retrieve the updated objective and constraint values and sensitivities
344 call problem%get_constraint_values(constraint_value)
345
346 select type (des => design)
347 type is (brinkman_design_t)
348 call des%get_sensitivity(objective_sensitivities)
349 class default
350 call problem%get_objective_sensitivities(objective_sensitivities)
351 end select
352
353 call problem%get_constraint_sensitivities(constraint_sensitivities)
354
355 ! Check the KKT conditions and check for convergence
356 call this%mma%KKT(x, objective_sensitivities, &
357 constraint_value, constraint_sensitivities)
358 converged = this%mma%get_residumax() .lt. this%tolerance
359
360 ! Free local resources
361 call neko_scratch_registry%relinquish(indices)
362
363 end function mma_optimizer_step
364
366 subroutine mma_optimizer_validate(this, problem, design)
367 class(mma_optimizer_t), intent(inout) :: this
368 class(problem_t), intent(in) :: problem
369 class(design_t), intent(in) :: design
370
371 type(vector_t), pointer :: constraint_values
372 integer :: ind
373
374 call neko_scratch_registry%request(constraint_values, ind, &
375 problem%get_n_constraints(), .false.)
376
377 call problem%get_constraint_values(constraint_values)
378 call constraint_values%copy_from(device_to_host, sync = .true.)
379
380 if (any(constraint_values%x .gt. 0.0_rp)) then
381 call neko_error('MMA optimizer validation failed: ' // &
382 'Constraints are not satisfied.')
383 end if
384
385 ! Free local resources
386 call neko_scratch_registry%relinquish(ind)
387
388 end subroutine mma_optimizer_validate
389
390 ! -------------------------------------------------------------------------- !
391 ! Logging and IO methods for the MMA optimizer
392
399 subroutine mma_optimizer_write(this, iter, problem)
400 class(mma_optimizer_t), intent(inout) :: this
401 integer, intent(in) :: iter
402 class(problem_t), intent(in) :: problem
403
404 type(vector_t), pointer :: log_data
405 type(vector_t), pointer :: all_objectives
406 type(vector_t), pointer :: constraint_value
407 real(kind=rp) :: objective_value
408
409 integer :: log_size, ind(3), n, m, i_tmp1, i_tmp2
410
411 if (.not. this%enable_output) return
412 call profiler_start_region('Optimizer logging')
413
414 n = problem%get_n_objectives()
415 m = problem%get_n_constraints()
416 if (this%unconstrained_problem) then
417 log_size = 5 + n
418 else
419 log_size = 5 + n + m
420 endif
421
422 call neko_scratch_registry%request(log_data, ind(1), log_size, .false.)
423 call neko_scratch_registry%request(all_objectives, ind(2), n, .false.)
424 call neko_scratch_registry%request(constraint_value, ind(3), m, .false.)
425
426 ! Prepare data for logging
427 call problem%get_objective_value(objective_value)
428 call problem%get_all_objective_values(all_objectives)
429 call problem%get_constraint_values(constraint_value)
430
431 call all_objectives%copy_from(device_to_host, sync = .true.)
432 call constraint_value%copy_from(device_to_host, sync = .true.)
433
434 ! Assemble the log data
435 log_data%x(1) = real(iter, kind=rp)
436
437 ! total objective
438 log_data%x(2) = objective_value
439
440 ! individual objectives
441 i_tmp1 = 3
442 i_tmp2 = i_tmp1 + n - 1
443 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
444
445 ! constraints
446 if (.not. this%unconstrained_problem) then
447 i_tmp1 = i_tmp2 + 1
448 i_tmp2 = i_tmp1 + m - 1
449 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
450 end if
451
452 ! convergence stuff
453 if (iter .eq. 0) then
454 log_data%x(i_tmp2 + 1) = 0.0_rp
455 log_data%x(i_tmp2 + 2) = 0.0_rp
456 else
457 log_data%x(i_tmp2 + 1) = this%mma%get_residumax()
458 log_data%x(i_tmp2 + 2) = this%mma%get_residunorm()
459 end if
460 log_data%x(i_tmp2 + 3) = this%scaling_factor
461
462 call this%csv_log%write(log_data)
463
464 ! Free local resources
465 call neko_scratch_registry%relinquish(ind)
466
467 call profiler_end_region('Optimizer logging')
468 end subroutine mma_optimizer_write
469
470 ! -------------------------------------------------------------------------- !
471 ! Checkpointing methods for the MMA optimizer
472
474 subroutine mma_optimizer_save_checkpoint_components(this, filename, overwrite)
475 class(mma_optimizer_t), intent(inout) :: this
476 character(len=*), intent(in) :: filename
477 logical, intent(in), optional :: overwrite
478
479 call this%mma%save_checkpoint(filename, overwrite)
480 end subroutine mma_optimizer_save_checkpoint_components
481
483 subroutine mma_optimizer_load_checkpoint_components(this, filename)
484 class(mma_optimizer_t), intent(inout) :: this
485 character(len=*), intent(in) :: filename
486
487 call this%mma%load_checkpoint(filename)
488 end subroutine mma_optimizer_load_checkpoint_components
489
490end module mma_optimizer
491
Implements the constraint_t type.
Implements the design_t.
Definition design.f90:36
Implements the dummy_constraint_t type.
MMA module.
Definition mma.f90:69
Defines the abstract type optimizer.
Definition optimizer.f90:40
Module for handling the optimization problem.
Definition problem.f90:41
Implements the steady_problem_t type.
A topology optimization design variable.
The abstract constraint type.
An abstract design type.
Definition design.f90:54
MMA type.
Definition mma.f90:90
Abstract optimizer class.
Definition optimizer.f90:56
The abstract problem type.
Definition problem.f90:67