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
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 this%read_base_settings(solver_parameters)
141 call this%init_from_components(
problem,
design, max_iterations, tolerance, &
142 enable_output, solver_parameters, simulation)
144 end subroutine mma_optimizer_init_from_json
147 subroutine mma_optimizer_init_from_components(this, problem, design, &
148 max_iterations, tolerance, enable_output, &
149 solver_parameters, simulation)
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
160 type(vector_t),
pointer :: x
162 character(len=1024) :: header
165 call neko_log%section(
'Optimizer Initialization')
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.')
174 select type (con => dummy_con)
176 call con%init_from_attributes(
design)
179 call problem%add_constraint(dummy_con)
180 if (
allocated(dummy_con))
deallocate(dummy_con)
185 call neko_scratch_registry%request(x, ind,
design%size(), .false.)
188 call this%mma%init(x,
design%size(),
problem%get_n_constraints(), &
189 solver_parameters, this%scale, this%auto_scale)
191 call neko_scratch_registry%relinquish(ind)
194 this%enable_output = enable_output
195 this%scaling_factor = this%scale
196 this%tolerance = tolerance
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())
205 call this%csv_log%set_header(trim(header))
208 call this%init_base(
'MMA', max_iterations)
210 call neko_log%end_section()
212 end subroutine mma_optimizer_init_from_components
215 subroutine mma_optimizer_free(this)
219 call this%free_base()
221 end subroutine mma_optimizer_free
227 subroutine mma_optimizer_initialize(this, problem, design, simulation)
229 class(
problem_t),
intent(inout) :: problem
230 class(
design_t),
intent(inout) :: design
231 type(
simulation_t),
optional,
intent(inout) :: simulation
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)
240 n_constraint =
problem%get_n_constraints()
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), &
248 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
249 n_constraint, n_design, .false.)
257 call problem%get_constraint_values(constraint_value)
259 select type (des =>
design)
261 call des%get_sensitivity(objective_sensitivities)
263 call problem%get_objective_sensitivities(objective_sensitivities)
266 call problem%get_constraint_sensitivities(constraint_sensitivities)
269 call this%mma%KKT(x, objective_sensitivities, &
270 constraint_value, constraint_sensitivities)
272 call neko_scratch_registry%relinquish(indices)
273 end subroutine mma_optimizer_initialize
276 function mma_optimizer_step(this, iter, problem, design, simulation) &
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
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)
293 n_constraint =
problem%get_n_constraints()
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), &
301 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
302 n_constraint, n_design, .false.)
306 call problem%get_constraint_values(constraint_value)
308 select type (des =>
design)
310 call des%get_sensitivity(objective_sensitivities)
312 call problem%get_objective_sensitivities(objective_sensitivities)
315 call problem%get_constraint_sensitivities(constraint_sensitivities)
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))
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)
329 call this%mma%update(iter, x, objective_sensitivities, &
330 constraint_value, constraint_sensitivities)
331 call design%update_design(x)
335 if (
present(simulation) .and. this%enable_output)
then
336 call simulation%write_forward(iter)
339 if (
present(simulation) .and. this%enable_output)
then
340 call simulation%write_adjoint(iter)
344 call problem%get_constraint_values(constraint_value)
346 select type (des =>
design)
348 call des%get_sensitivity(objective_sensitivities)
350 call problem%get_objective_sensitivities(objective_sensitivities)
353 call problem%get_constraint_sensitivities(constraint_sensitivities)
356 call this%mma%KKT(x, objective_sensitivities, &
357 constraint_value, constraint_sensitivities)
358 converged = this%mma%get_residumax() .lt. this%tolerance
361 call neko_scratch_registry%relinquish(indices)
363 end function mma_optimizer_step
366 subroutine mma_optimizer_validate(this, problem, design)
369 class(
design_t),
intent(in) :: design
371 type(vector_t),
pointer :: constraint_values
374 call neko_scratch_registry%request(constraint_values, ind, &
375 problem%get_n_constraints(), .false.)
377 call problem%get_constraint_values(constraint_values)
378 call constraint_values%copy_from(device_to_host, sync = .true.)
380 if (any(constraint_values%x .gt. 0.0_rp))
then
381 call neko_error(
'MMA optimizer validation failed: ' // &
382 'Constraints are not satisfied.')
386 call neko_scratch_registry%relinquish(ind)
388 end subroutine mma_optimizer_validate
399 subroutine mma_optimizer_write(this, iter, problem)
401 integer,
intent(in) :: iter
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
409 integer :: log_size, ind(3), n, m, i_tmp1, i_tmp2
411 if (.not. this%enable_output)
return
412 call profiler_start_region(
'Optimizer logging')
415 m =
problem%get_n_constraints()
416 if (this%unconstrained_problem)
then
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.)
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)
431 call all_objectives%copy_from(device_to_host, sync = .true.)
432 call constraint_value%copy_from(device_to_host, sync = .true.)
435 log_data%x(1) = real(iter, kind=rp)
438 log_data%x(2) = objective_value
442 i_tmp2 = i_tmp1 + n - 1
443 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
446 if (.not. this%unconstrained_problem)
then
448 i_tmp2 = i_tmp1 + m - 1
449 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
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
457 log_data%x(i_tmp2 + 1) = this%mma%get_residumax()
458 log_data%x(i_tmp2 + 2) = this%mma%get_residunorm()
460 log_data%x(i_tmp2 + 3) = this%scaling_factor
462 call this%csv_log%write(log_data)
465 call neko_scratch_registry%relinquish(ind)
467 call profiler_end_region(
'Optimizer logging')
468 end subroutine mma_optimizer_write
474 subroutine mma_optimizer_save_checkpoint_components(this, filename, overwrite)
476 character(len=*),
intent(in) :: filename
477 logical,
intent(in),
optional :: overwrite
479 call this%mma%save_checkpoint(filename, overwrite)
480 end subroutine mma_optimizer_save_checkpoint_components
483 subroutine mma_optimizer_load_checkpoint_components(this, filename)
485 character(len=*),
intent(in) :: filename
487 call this%mma%load_checkpoint(filename)
488 end subroutine mma_optimizer_load_checkpoint_components
490end module mma_optimizer
Implements the constraint_t type.
Implements the dummy_constraint_t type.
Defines the abstract type optimizer.
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.