39 use num_types,
only: rp
40 use utils,
only: neko_error
41 use json_utils,
only: json_get, json_get_or_default
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
71 type(mma_t),
private :: mma
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
85 logical,
private :: unconstrained_problem = .false.
88 logical,
private :: enable_output = .true.
89 type(csv_file_t),
private :: csv_log
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
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
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
117 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
120 type(json_file),
intent(inout) :: parameters
121 class(
problem_t),
intent(inout) :: problem
122 class(
design_t),
intent(in) :: design
126 type(json_file) :: solver_parameters
127 logical :: enable_output
128 integer :: max_iterations
129 real(kind=rp) :: tolerance, max_runtime
132 call json_get(parameters,
'optimization.solver', solver_parameters)
133 call json_get_or_default(solver_parameters,
'max_iterations', &
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 json_get_or_default(solver_parameters,
'max_runtime', &
140 max_runtime, -1.0_rp)
142 call this%init_from_components(
problem,
design, max_iterations, tolerance, &
143 enable_output, solver_parameters, simulation, max_runtime)
145 end subroutine mma_optimizer_init_from_json
148 subroutine mma_optimizer_init_from_components(this, problem, design, &
149 max_iterations, tolerance, enable_output, &
150 solver_parameters, simulation, max_runtime)
152 class(
problem_t),
intent(inout) :: problem
153 class(
design_t),
intent(in) :: design
154 integer,
intent(in) :: max_iterations
155 real(kind=rp),
intent(in) :: tolerance
156 logical,
intent(in) :: enable_output
157 type(json_file),
intent(inout),
optional :: solver_parameters
159 real(kind=rp),
intent(in),
optional :: max_runtime
162 type(vector_t),
pointer :: x
164 character(len=1024) :: header
167 call neko_log%section(
'Optimizer Initialization')
170 this%unconstrained_problem =
problem%get_n_constraints() .eq. 0
171 if (this%unconstrained_problem)
then
172 call neko_log%message(
'Unconstrained problem detected. ' // &
173 'Adding a dummy constraint to enable MMA optimization.')
176 select type (con => dummy_con)
178 call con%init_from_attributes(
design)
181 call problem%add_constraint(dummy_con)
182 if (
allocated(dummy_con))
deallocate(dummy_con)
187 call neko_scratch_registry%request(x, ind,
design%size(), .false.)
190 call this%mma%init(x,
design%size(),
problem%get_n_constraints(), &
191 solver_parameters, this%scale, this%auto_scale)
193 call neko_scratch_registry%relinquish(ind)
196 this%enable_output = enable_output
197 this%scaling_factor = this%scale
198 this%tolerance = tolerance
201 if (this%enable_output)
then
202 call this%csv_log%init(
'optimization_data.csv')
203 header =
'iter, ' // trim(
problem%get_log_header()) // &
204 ', KKTmax, KKTnorm2, scaling factor, ' // &
205 trim(this%mma%get_backend_and_subsolver())
207 call this%csv_log%set_header(trim(header))
210 call this%init_base(
'MMA', max_iterations, max_runtime)
212 call neko_log%end_section()
214 end subroutine mma_optimizer_init_from_components
217 subroutine mma_optimizer_free(this)
221 call this%free_base()
223 end subroutine mma_optimizer_free
229 subroutine mma_optimizer_initialize(this, problem, design, simulation)
231 class(
problem_t),
intent(inout) :: problem
232 class(
design_t),
intent(inout) :: design
233 type(
simulation_t),
optional,
intent(inout) :: simulation
235 type(vector_t),
pointer :: x
236 type(vector_t),
pointer :: constraint_value
237 type(vector_t),
pointer :: objective_sensitivities
238 type(matrix_t),
pointer :: constraint_sensitivities
239 integer :: n_design, n_constraint, indices(4)
242 n_constraint =
problem%get_n_constraints()
245 call neko_scratch_registry%request(x, indices(1), n_design, .false.)
246 call neko_scratch_registry%request(constraint_value, indices(2), &
247 n_constraint, .false.)
248 call neko_scratch_registry%request(objective_sensitivities, indices(3), &
250 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
251 n_constraint, n_design, .false.)
259 call problem%get_constraint_values(constraint_value)
261 select type (des =>
design)
263 call des%get_sensitivity(objective_sensitivities)
265 call problem%get_objective_sensitivities(objective_sensitivities)
268 call problem%get_constraint_sensitivities(constraint_sensitivities)
271 call this%mma%KKT(x, objective_sensitivities, &
272 constraint_value, constraint_sensitivities)
274 call neko_scratch_registry%relinquish(indices)
275 end subroutine mma_optimizer_initialize
278 function mma_optimizer_step(this, iter, problem, design, simulation) &
281 integer,
intent(in) :: iter
282 class(
problem_t),
intent(inout) :: problem
283 class(
design_t),
intent(inout) :: design
284 type(
simulation_t),
optional,
intent(inout) :: simulation
286 type(vector_t),
pointer :: x
287 type(vector_t),
pointer :: constraint_value
288 type(vector_t),
pointer :: objective_sensitivities
289 type(matrix_t),
pointer :: constraint_sensitivities
290 integer :: n_design, n_constraint, indices(4)
295 n_constraint =
problem%get_n_constraints()
298 call neko_scratch_registry%request(x, indices(1), n_design, .false.)
299 call neko_scratch_registry%request(constraint_value, indices(2), &
300 n_constraint, .false.)
301 call neko_scratch_registry%request(objective_sensitivities, indices(3), &
303 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
304 n_constraint, n_design, .false.)
308 call problem%get_constraint_values(constraint_value)
310 select type (des =>
design)
312 call des%get_sensitivity(objective_sensitivities)
314 call problem%get_objective_sensitivities(objective_sensitivities)
317 call problem%get_constraint_sensitivities(constraint_sensitivities)
320 if (this%auto_scale)
then
321 call constraint_value%copy_from(device_to_host, sync = .true.)
322 this%scaling_factor = abs(this%scale / constraint_value%x(1))
325 if (.not. abscmp(this%scaling_factor, 1.0_rp))
then
326 call vector_cmult(constraint_value, this%scaling_factor)
327 call matrix_cmult(constraint_sensitivities, this%scaling_factor)
331 call this%mma%update(iter, x, objective_sensitivities, &
332 constraint_value, constraint_sensitivities)
333 call design%update_design(x)
337 if (
present(simulation) .and. this%enable_output)
then
338 call simulation%write_forward(iter)
341 if (
present(simulation) .and. this%enable_output)
then
342 call simulation%write_adjoint(iter)
346 call problem%get_constraint_values(constraint_value)
348 select type (des =>
design)
350 call des%get_sensitivity(objective_sensitivities)
352 call problem%get_objective_sensitivities(objective_sensitivities)
355 call problem%get_constraint_sensitivities(constraint_sensitivities)
358 call this%mma%KKT(x, objective_sensitivities, &
359 constraint_value, constraint_sensitivities)
360 converged = this%mma%get_residumax() .lt. this%tolerance
363 call neko_scratch_registry%relinquish(indices)
365 end function mma_optimizer_step
368 subroutine mma_optimizer_validate(this, problem, design)
371 class(
design_t),
intent(in) :: design
373 type(vector_t),
pointer :: constraint_values
376 call neko_scratch_registry%request(constraint_values, ind, &
377 problem%get_n_constraints(), .false.)
379 call problem%get_constraint_values(constraint_values)
380 call constraint_values%copy_from(device_to_host, sync = .true.)
382 if (any(constraint_values%x .gt. 0.0_rp))
then
383 call neko_error(
'MMA optimizer validation failed: ' // &
384 'Constraints are not satisfied.')
388 call neko_scratch_registry%relinquish(ind)
390 end subroutine mma_optimizer_validate
401 subroutine mma_optimizer_write(this, iter, problem)
403 integer,
intent(in) :: iter
406 type(vector_t),
pointer :: log_data
407 type(vector_t),
pointer :: all_objectives
408 type(vector_t),
pointer :: constraint_value
409 real(kind=rp) :: objective_value
411 integer :: log_size, ind(3), n, m, i_tmp1, i_tmp2
413 if (.not. this%enable_output)
return
414 call profiler_start_region(
'Optimizer logging')
417 m =
problem%get_n_constraints()
418 if (this%unconstrained_problem)
then
424 call neko_scratch_registry%request(log_data, ind(1), log_size, .false.)
425 call neko_scratch_registry%request(all_objectives, ind(2), n, .false.)
426 call neko_scratch_registry%request(constraint_value, ind(3), m, .false.)
429 call problem%get_objective_value(objective_value)
430 call problem%get_all_objective_values(all_objectives)
431 call problem%get_constraint_values(constraint_value)
433 call all_objectives%copy_from(device_to_host, sync = .true.)
434 call constraint_value%copy_from(device_to_host, sync = .true.)
437 log_data%x(1) = real(iter, kind=rp)
440 log_data%x(2) = objective_value
444 i_tmp2 = i_tmp1 + n - 1
445 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
448 if (.not. this%unconstrained_problem)
then
450 i_tmp2 = i_tmp1 + m - 1
451 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
455 if (iter .eq. 0)
then
456 log_data%x(i_tmp2 + 1) = 0.0_rp
457 log_data%x(i_tmp2 + 2) = 0.0_rp
459 log_data%x(i_tmp2 + 1) = this%mma%get_residumax()
460 log_data%x(i_tmp2 + 2) = this%mma%get_residunorm()
462 log_data%x(i_tmp2 + 3) = this%scaling_factor
464 call this%csv_log%write(log_data)
467 call neko_scratch_registry%relinquish(ind)
469 call profiler_end_region(
'Optimizer logging')
470 end subroutine mma_optimizer_write
476 subroutine mma_optimizer_save_checkpoint_components(this, filename, overwrite)
478 character(len=*),
intent(in) :: filename
479 logical,
intent(in),
optional :: overwrite
481 call this%mma%save_checkpoint(filename, overwrite)
482 end subroutine mma_optimizer_save_checkpoint_components
485 subroutine mma_optimizer_load_checkpoint_components(this, filename)
487 character(len=*),
intent(in) :: filename
489 call this%mma%load_checkpoint(filename)
490 end subroutine mma_optimizer_load_checkpoint_components
492end module mma_optimizer
Implements the constraint_t type.
Implements the dummy_constraint_t type.
Defines the abstract type optimizer The optimizer type is defined to provide a generic interface to u...
Module for handling the optimization problem.
Implements the steady_problem_t type.
A topology optimization design variable.
The abstract constraint type.
Abstract optimizer class.
The abstract problem type.