6 use num_types,
only: rp
7 use utils,
only: neko_error
8 use json_module,
only: json_file
9 use json_utils,
only: json_get, json_get_or_default
13 use field,
only: field_t
14 use field_registry,
only: neko_field_registry
15 use vector_scratch_registry,
only: neko_vector_scratch_registry
16 use profiler,
only: profiler_start_region, profiler_end_region
18 use vector,
only: vector_t
19 use matrix,
only: matrix_t
22 use comm,
only: neko_comm, pe_rank
23 use mpi_f08,
only: mpi_integer, mpi_sum, mpi_allreduce
25 use neko_config,
only: neko_bcknd_device
27 use,
intrinsic :: iso_fortran_env, only: stderr => error_unit
29 use math,
only: copy, cmult
30 use device_math,
only: device_copy, device_cmult
31 use field_math,
only: field_rzero
32 use vector_math,
only: vector_cmult
35 use device,
only: device_memcpy, host_to_device, device_to_host
55 real(kind=rp) :: scale
59 logical :: enable_output
64 generic :: init => init_from_json, init_from_components
65 procedure, pass(this) :: init_from_json => mma_optimizer_init_from_json
66 procedure, pass(this) :: init_from_components => &
67 mma_optimizer_init_from_components
69 procedure, pass(this) :: run => mma_optimizer_run
70 procedure, pass(this) :: validate => mma_optimizer_validate
71 procedure, pass(this) :: free => mma_optimizer_free
78 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
81 type(json_file),
intent(inout) :: parameters
85 logical :: enable_output
86 integer :: max_iterations
87 real(kind=rp) :: tolerance
89 character(len=1024) :: optimization_header
90 character(len=1024) :: problem_header
92 type(vector_t),
pointer :: x
94 type(json_file) :: solver_parameters
95 logical :: unconstrained_problem
97 call neko_vector_scratch_registry%request_vector(
design%size(), x, ind)
101 if (pe_rank .eq. 0)
then
102 print *,
"Initializing mma_optimizer with steady_state_problem_t."
105 call json_get(parameters,
"optimization.solver", &
110 unconstrained_problem = (
problem%get_n_constraints() == 0)
111 if (unconstrained_problem)
then
112 call this%mma%init(x,
design%size(), 1, &
113 solver_parameters, this%scale, this%auto_scale)
115 call this%mma%init(x,
design%size(),
problem%get_n_constraints(), &
116 solver_parameters, this%scale, this%auto_scale)
119 call neko_vector_scratch_registry%relinquish_vector(ind)
121 call json_get_or_default(parameters,
"optimization.solver.max_iterations", &
123 call json_get_or_default(parameters,
"optimization.solver.tolerance", &
124 tolerance, 1.0e-3_rp)
125 call json_get_or_default(parameters,
"optimization.solver.enable_output", &
126 enable_output, .true.)
129 max_iterations, tolerance, enable_output, simulation)
132 call this%logger%init(
'optimization_data.csv')
135 problem_header =
problem%get_log_header()
136 optimization_header =
'iter, ' // trim(problem_header) // &
137 ', KKTmax, KKTnorm2, scaling factor, ' // &
138 this%mma%get_backend_and_subsolver()
139 call this%logger%set_header(trim(optimization_header))
140 end subroutine mma_optimizer_init_from_json
143 subroutine mma_optimizer_init_from_components(this, problem, design, &
144 max_iterations, tolerance, enable_output, simulation)
148 integer,
intent(in) :: max_iterations
149 real(kind=rp),
intent(in) :: tolerance
151 logical,
intent(in) :: enable_output
154 this%enable_output = enable_output
156 call this%init_base(max_iterations, tolerance)
158 end subroutine mma_optimizer_init_from_components
161 subroutine mma_optimizer_run(this, problem, design, simulation)
165 type(
simulation_t),
optional,
intent(inout) :: simulation
167 type(vector_t),
pointer :: x
169 integer :: iter, ierr, nglobal, n
170 real(kind=rp) :: scaling_factor
172 real(kind=rp) :: objective_value
173 type(vector_t),
pointer :: all_objectives
174 type(vector_t),
pointer :: constraint_value
175 type(vector_t),
pointer :: objective_sensitivities
176 type(matrix_t) :: constraint_sensitivities
179 type(vector_t) :: log_data
180 logical :: unconstrained_problem = .false.
182 type(json_file) :: parameters
185 call mpi_allreduce(n, nglobal, 1, mpi_integer, mpi_sum, neko_comm, ierr)
187 unconstrained_problem = (
problem%get_n_constraints() == 0)
188 if (unconstrained_problem)
then
190 call dummy_con%init(parameters,
design)
191 call problem%add_constraint(dummy_con)
195 call neko_vector_scratch_registry%request_vector(n, x, ind(1))
196 call neko_vector_scratch_registry%request_vector( &
197 problem%get_n_objectives(), all_objectives, ind(2))
198 call neko_vector_scratch_registry%request_vector( &
199 problem%get_n_constraints(), constraint_value, ind(3))
200 call neko_vector_scratch_registry%request_vector(n, &
201 objective_sensitivities, ind(4))
203 call constraint_sensitivities%init(
problem%get_n_constraints(), n)
206 scaling_factor = 1.0_rp
207 if (pe_rank .eq. 0)
then
208 print *,
"max_iterations for the optimization loop = ", &
211 call profiler_start_region(
"Optimizer iteration")
216 call problem%get_objective_value(objective_value)
217 call problem%get_constraint_values(constraint_value)
218 select type (des =>
design)
220 call des%get_sensitivity(objective_sensitivities)
222 call problem%get_objective_sensitivities(objective_sensitivities)
224 call problem%get_constraint_sensitivities(constraint_sensitivities)
226 call profiler_end_region(
"Optimizer iteration")
228 if (this%enable_output)
then
229 call profiler_start_region(
"Optimizer logging")
231 call problem%get_all_objective_values(all_objectives)
232 call mma_logger_assemble_data(log_data, 0, objective_value, &
233 all_objectives, constraint_value, 0.0_rp, 0.0_rp, scaling_factor, &
235 unconstrained_problem)
236 call this%logger%write(log_data)
238 if (
present(simulation))
then
239 call simulation%write(0)
242 call profiler_end_region(
"Optimizer logging")
246 do iter = 1, this%max_iterations
247 if (this%mma%get_residumax() .lt. this%tolerance)
exit
251 call profiler_start_region(
"Optimizer iteration")
254 if (this%auto_scale .eqv. .true.)
then
255 scaling_factor = abs(this%scale/constraint_value%x(1))
257 scaling_factor = abs(this%scale)
260 call vector_cmult(constraint_value, scaling_factor)
262 if (neko_bcknd_device .eq. 1)
then
263 call device_cmult(constraint_sensitivities%x_d, scaling_factor, &
264 constraint_sensitivities%size())
266 call cmult(constraint_sensitivities%x, scaling_factor, &
267 constraint_sensitivities%size())
271 call profiler_start_region(
"MMA update")
272 call this%mma%update(iter, x, objective_sensitivities, &
273 constraint_value, constraint_sensitivities)
274 call profiler_end_region(
"MMA update")
276 call design%update_design(x)
281 call problem%get_objective_value(objective_value)
282 call problem%get_constraint_values(constraint_value)
283 select type (des =>
design)
285 call des%get_sensitivity(objective_sensitivities)
287 call problem%get_objective_sensitivities(objective_sensitivities)
289 call problem%get_constraint_sensitivities(constraint_sensitivities)
291 call profiler_start_region(
"MMA KKT computation")
292 call this%mma%KKT(x, objective_sensitivities, &
293 constraint_value, constraint_sensitivities)
294 call profiler_end_region(
"MMA KKT computation")
296 call profiler_end_region(
"Optimizer iteration")
298 if (this%enable_output)
then
299 call profiler_start_region(
"Optimizer logging")
302 call problem%get_all_objective_values(all_objectives)
303 call mma_logger_assemble_data(log_data, iter, objective_value, &
304 all_objectives, constraint_value, this%mma%get_residumax(), &
305 this%mma%get_residunorm(), scaling_factor, &
307 unconstrained_problem)
308 call this%logger%write(log_data)
311 if (
present(simulation))
then
312 call simulation%write(iter)
315 call profiler_end_region(
"Optimizer logging")
323 if (pe_rank .eq. 0)
then
324 print *,
"MMA Optimization completed after", iter-1,
"iterations."
328 call neko_vector_scratch_registry%relinquish_vector(ind)
330 call constraint_sensitivities%free()
332 end subroutine mma_optimizer_run
335 subroutine mma_optimizer_validate(this, problem, design)
340 type(vector_t),
pointer :: constraint_values
343 call neko_vector_scratch_registry%request_vector( &
344 problem%get_n_constraints(), constraint_values, ind)
346 call problem%get_constraint_values(constraint_values)
347 if (neko_bcknd_device .eq. 1)
then
348 call device_memcpy(constraint_values%x, constraint_values%x_d, &
349 constraint_values%size(), host_to_device, .true.)
352 if (any(constraint_values%x .gt. 0.0_rp))
then
353 call neko_error(
"MMA optimizer validation failed: " // &
354 "Constraints are not satisfied.")
358 call neko_vector_scratch_registry%relinquish_vector(ind)
360 end subroutine mma_optimizer_validate
363 subroutine mma_optimizer_free(this)
368 end subroutine mma_optimizer_free
371 subroutine mma_logger_assemble_data(log_data, iter, objective_value, &
372 all_objectives, constraint_value, residumax, residunorm, &
373 scaling_factor, n, m, unconstrained_problem)
374 type(vector_t),
intent(inout) :: log_data
375 integer,
intent(in) :: iter
376 real(kind=rp),
intent(in) ::objective_value
377 type(vector_t),
intent(in) :: all_objectives
378 type(vector_t),
intent(in) :: constraint_value
379 real(kind=rp),
intent(in) :: residumax, residunorm, scaling_factor
380 logical,
intent(in) :: unconstrained_problem
381 integer,
intent(in) :: n, m
382 integer :: i_tmp1, i_tmp2
386 if (unconstrained_problem)
then
387 call log_data%init(5 + n)
389 call log_data%init(5 + n + m)
393 log_data%x(1) = real(iter, kind=rp)
396 log_data%x(2) = objective_value
400 i_tmp2 = i_tmp1 + n - 1
401 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
404 if (.not. unconstrained_problem)
then
406 i_tmp2 = i_tmp1 + m - 1
407 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
412 log_data%x(i_tmp2 + 1) = residumax
413 log_data%x(i_tmp2 + 2) = residunorm
414 log_data%x(i_tmp2 + 3) = scaling_factor
416 end subroutine mma_logger_assemble_data
417end module mma_optimizer
Implements the constraint_t type.
Implements the dummy_constraint_t type.
Some common Masking operations we may need.
Contains extensions to the neko library required to run the topology optimization code.
subroutine, public reset(neko_case)
Reset the case data structure.
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.