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
68 type(mma_t),
private :: mma
76 real(kind=rp),
private :: scale = 1.0_rp
77 real(kind=rp),
private :: scaling_factor = 1.0_rp
78 logical,
private :: auto_scale = .false.
81 logical,
private :: unconstrained_problem = .false.
84 logical,
private :: enable_output = .true.
85 type(csv_file_t),
private :: csv_log
89 generic :: init => init_from_json, init_from_components
90 procedure, pass(this) :: init_from_json => mma_optimizer_init_from_json
91 procedure, pass(this) :: init_from_components => &
92 mma_optimizer_init_from_components
94 procedure, pass(this) :: initialize => mma_optimizer_initialize
95 procedure, pass(this) :: step => mma_optimizer_step
96 procedure, pass(this) :: validate => mma_optimizer_validate
97 procedure, pass(this) :: write => mma_optimizer_write
98 procedure, pass(this) :: free => mma_optimizer_free
108 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
111 type(json_file),
intent(inout) :: parameters
112 class(
problem_t),
intent(inout) :: problem
113 class(
design_t),
intent(in) :: design
117 type(json_file) :: solver_parameters
118 logical :: enable_output
119 integer :: max_iterations
120 real(kind=rp) :: tolerance, max_runtime
123 call json_get(parameters,
'optimization.solver', solver_parameters)
124 call json_get_or_default(solver_parameters,
'max_iterations', &
126 call json_get_or_default(solver_parameters,
'tolerance', &
127 tolerance, 1.0e-3_rp)
128 call json_get_or_default(solver_parameters,
'enable_output', &
129 enable_output, .true.)
130 call json_get_or_default(solver_parameters,
'max_runtime', &
131 max_runtime, -1.0_rp)
133 call this%init_from_components(
problem,
design, max_iterations, tolerance, &
134 enable_output, solver_parameters, simulation, max_runtime)
136 end subroutine mma_optimizer_init_from_json
139 subroutine mma_optimizer_init_from_components(this, problem, design, &
140 max_iterations, tolerance, enable_output, &
141 solver_parameters, simulation, max_runtime)
143 class(
problem_t),
intent(inout) :: problem
144 class(
design_t),
intent(in) :: design
145 integer,
intent(in) :: max_iterations
146 real(kind=rp),
intent(in) :: tolerance
147 logical,
intent(in) :: enable_output
148 type(json_file),
intent(inout),
optional :: solver_parameters
150 real(kind=rp),
intent(in),
optional :: max_runtime
153 type(vector_t),
pointer :: x
155 character(len=1024) :: header
158 call neko_log%section(
'Optimizer Initialization')
161 this%unconstrained_problem =
problem%get_n_constraints() .eq. 0
162 if (this%unconstrained_problem)
then
163 call neko_log%message(
'Unconstrained problem detected. ' // &
164 'Adding a dummy constraint to enable MMA optimization.')
167 select type (con => dummy_con)
169 call con%init_from_attributes(
design)
172 call problem%add_constraint(dummy_con)
173 if (
allocated(dummy_con))
deallocate(dummy_con)
178 call neko_scratch_registry%request(x, ind,
design%size(), .false.)
181 call this%mma%init(x,
design%size(),
problem%get_n_constraints(), &
182 solver_parameters, this%scale, this%auto_scale)
184 call neko_scratch_registry%relinquish(ind)
187 this%enable_output = enable_output
188 this%scaling_factor = this%scale
191 if (this%enable_output)
then
192 call this%csv_log%init(
'optimization_data.csv')
193 header =
'iter, ' // trim(
problem%get_log_header()) // &
194 ', KKTmax, KKTnorm2, scaling factor, ' // &
195 trim(this%mma%get_backend_and_subsolver())
197 call this%csv_log%set_header(trim(header))
200 call this%init_base(max_iterations, tolerance, max_runtime)
202 call neko_log%end_section()
204 end subroutine mma_optimizer_init_from_components
207 subroutine mma_optimizer_free(this)
211 call this%free_base()
213 end subroutine mma_optimizer_free
219 subroutine mma_optimizer_initialize(this, problem, design, simulation)
221 class(
problem_t),
intent(inout) :: problem
222 class(
design_t),
intent(inout) :: design
223 type(
simulation_t),
optional,
intent(inout) :: simulation
225 type(vector_t),
pointer :: x
226 type(vector_t),
pointer :: constraint_value
227 type(vector_t),
pointer :: objective_sensitivities
228 type(matrix_t),
pointer :: constraint_sensitivities
229 integer :: n_design, n_constraint, indices(4)
232 n_constraint =
problem%get_n_constraints()
235 call neko_scratch_registry%request(x, indices(1), n_design, .false.)
236 call neko_scratch_registry%request(constraint_value, indices(2), &
237 n_constraint, .false.)
238 call neko_scratch_registry%request(objective_sensitivities, indices(3), &
240 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
241 n_constraint, n_design, .false.)
249 call problem%get_constraint_values(constraint_value)
251 select type (des =>
design)
253 call des%get_sensitivity(objective_sensitivities)
255 call problem%get_objective_sensitivities(objective_sensitivities)
258 call problem%get_constraint_sensitivities(constraint_sensitivities)
261 call this%mma%KKT(x, objective_sensitivities, &
262 constraint_value, constraint_sensitivities)
264 call neko_scratch_registry%relinquish(indices)
265 end subroutine mma_optimizer_initialize
268 function mma_optimizer_step(this, iter, problem, design, simulation) &
271 integer,
intent(in) :: iter
272 class(
problem_t),
intent(inout) :: problem
273 class(
design_t),
intent(inout) :: design
274 type(
simulation_t),
optional,
intent(inout) :: simulation
276 type(vector_t),
pointer :: x
277 type(vector_t),
pointer :: constraint_value
278 type(vector_t),
pointer :: objective_sensitivities
279 type(matrix_t),
pointer :: constraint_sensitivities
280 integer :: n_design, n_constraint, indices(4)
285 n_constraint =
problem%get_n_constraints()
288 call neko_scratch_registry%request(x, indices(1), n_design, .false.)
289 call neko_scratch_registry%request(constraint_value, indices(2), &
290 n_constraint, .false.)
291 call neko_scratch_registry%request(objective_sensitivities, indices(3), &
293 call neko_scratch_registry%request(constraint_sensitivities, indices(4), &
294 n_constraint, n_design, .false.)
298 call problem%get_constraint_values(constraint_value)
300 select type (des =>
design)
302 call des%get_sensitivity(objective_sensitivities)
304 call problem%get_objective_sensitivities(objective_sensitivities)
307 call problem%get_constraint_sensitivities(constraint_sensitivities)
310 if (this%auto_scale)
then
311 call constraint_value%copy_from(device_to_host, sync = .true.)
312 this%scaling_factor = abs(this%scale / constraint_value%x(1))
315 if (.not. abscmp(this%scaling_factor, 1.0_rp))
then
316 call vector_cmult(constraint_value, this%scaling_factor)
317 call matrix_cmult(constraint_sensitivities, this%scaling_factor)
321 call this%mma%update(iter, x, objective_sensitivities, &
322 constraint_value, constraint_sensitivities)
323 call design%update_design(x)
327 if (
present(simulation) .and. this%enable_output)
then
328 call simulation%write_forward(iter)
331 if (
present(simulation) .and. this%enable_output)
then
332 call simulation%write_adjoint(iter)
336 call problem%get_constraint_values(constraint_value)
338 select type (des =>
design)
340 call des%get_sensitivity(objective_sensitivities)
342 call problem%get_objective_sensitivities(objective_sensitivities)
345 call problem%get_constraint_sensitivities(constraint_sensitivities)
348 call this%mma%KKT(x, objective_sensitivities, &
349 constraint_value, constraint_sensitivities)
350 converged = this%mma%get_residumax() .lt. this%tolerance
353 call neko_scratch_registry%relinquish(indices)
355 end function mma_optimizer_step
358 subroutine mma_optimizer_validate(this, problem, design)
361 class(
design_t),
intent(in) :: design
363 type(vector_t),
pointer :: constraint_values
366 call neko_scratch_registry%request(constraint_values, ind, &
367 problem%get_n_constraints(), .false.)
369 call problem%get_constraint_values(constraint_values)
370 call constraint_values%copy_from(device_to_host, sync = .true.)
372 if (any(constraint_values%x .gt. 0.0_rp))
then
373 call neko_error(
'MMA optimizer validation failed: ' // &
374 'Constraints are not satisfied.')
378 call neko_scratch_registry%relinquish(ind)
380 end subroutine mma_optimizer_validate
391 subroutine mma_optimizer_write(this, iter, problem)
393 integer,
intent(in) :: iter
396 type(vector_t),
pointer :: log_data
397 type(vector_t),
pointer :: all_objectives
398 type(vector_t),
pointer :: constraint_value
399 real(kind=rp) :: objective_value
401 integer :: log_size, ind(3), n, m, i_tmp1, i_tmp2
403 if (.not. this%enable_output)
return
404 call profiler_start_region(
'Optimizer logging')
407 m =
problem%get_n_constraints()
408 if (this%unconstrained_problem)
then
414 call neko_scratch_registry%request(log_data, ind(1), log_size, .false.)
415 call neko_scratch_registry%request(all_objectives, ind(2), n, .false.)
416 call neko_scratch_registry%request(constraint_value, ind(3), m, .false.)
419 call problem%get_objective_value(objective_value)
420 call problem%get_all_objective_values(all_objectives)
421 call problem%get_constraint_values(constraint_value)
423 call all_objectives%copy_from(device_to_host, sync = .true.)
424 call constraint_value%copy_from(device_to_host, sync = .true.)
427 log_data%x(1) = real(iter, kind=rp)
430 log_data%x(2) = objective_value
434 i_tmp2 = i_tmp1 + n - 1
435 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
438 if (.not. this%unconstrained_problem)
then
440 i_tmp2 = i_tmp1 + m - 1
441 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
445 if (iter .eq. 0)
then
446 log_data%x(i_tmp2 + 1) = 0.0_rp
447 log_data%x(i_tmp2 + 2) = 0.0_rp
449 log_data%x(i_tmp2 + 1) = this%mma%get_residumax()
450 log_data%x(i_tmp2 + 2) = this%mma%get_residunorm()
452 log_data%x(i_tmp2 + 3) = this%scaling_factor
454 call this%csv_log%write(log_data)
457 call neko_scratch_registry%relinquish(ind)
459 call profiler_end_region(
'Optimizer logging')
460 end subroutine mma_optimizer_write
462end 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.