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_or_default, json_extract_object
12 use field,
only: field_t
13 use field_registry,
only: neko_field_registry
15 use vector,
only: vector_t
16 use matrix,
only: matrix_t
19 use comm,
only: neko_comm, pe_rank
20 use mpi_f08,
only: mpi_integer, mpi_sum, mpi_allreduce
22 use neko_config,
only: neko_bcknd_device
24 use,
intrinsic :: iso_fortran_env, only: stderr => error_unit
26 use math,
only: copy, cmult
27 use device_math,
only: device_copy, device_cmult
28 use field_math,
only: field_rzero
29 use vector_math,
only: vector_cmult
32 use device,
only: device_memcpy, host_to_device, device_to_host
49 real(kind=rp) :: scale
55 generic :: init => init_from_json, init_from_components
56 procedure, pass(this) :: init_from_json => mma_optimizer_init_from_json
57 procedure, pass(this) :: init_from_components => &
58 mma_optimizer_init_from_components
60 procedure, pass(this) :: run => mma_optimizer_run
61 procedure, pass(this) :: validate => mma_optimizer_validate
62 procedure, pass(this) :: free => mma_optimizer_free
69 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
70 max_iterations, tolerance, simulation)
72 type(json_file),
intent(inout) :: parameters
75 integer,
intent(in) :: max_iterations
76 real(kind=rp),
intent(in) :: tolerance
79 character(len=1024) :: optimization_header
80 character(len=1024) :: problem_header
83 type(json_file) :: solver_parameters
86 call this%logger%init(
'optimization_data.csv')
89 problem_header =
problem%get_log_header()
90 optimization_header =
'iter, ' // trim(problem_header) // &
91 ', KKTmax, KKTnorm2, scaling factor'
92 call this%logger%set_header(trim(optimization_header))
96 if (pe_rank .eq. 0)
then
97 print *,
"Initializing mma_optimizer with steady_state_problem_t."
100 call json_extract_object(parameters,
"optimization.solver", &
102 call this%mma%init(x%x,
design%size(),
problem%get_n_constraints(), &
103 solver_parameters, this%scale, this%auto_scale)
106 max_iterations, tolerance, simulation)
109 end subroutine mma_optimizer_init_from_json
112 subroutine mma_optimizer_init_from_components(this, problem, design, &
113 max_iterations, tolerance, simulation)
117 integer,
intent(in) :: max_iterations
118 real(kind=rp),
intent(in) :: tolerance
121 call this%init_base(max_iterations, tolerance)
123 end subroutine mma_optimizer_init_from_components
126 subroutine mma_optimizer_run(this, problem, design, simulation)
130 type(
simulation_t),
optional,
intent(inout) :: simulation
134 integer :: iter, ierr, nglobal, n
135 real(kind=rp) :: scaling_factor
137 real(kind=rp) :: objective_value
138 type(vector_t) :: all_objectives
139 type(vector_t) :: constraint_value
140 type(vector_t) :: objective_sensitivities
141 type(matrix_t) :: constraint_sensitivities
143 type(vector_t) :: log_data
146 call mpi_allreduce(n, nglobal, 1, mpi_integer, mpi_sum, neko_comm, ierr)
150 call all_objectives%init(
problem%get_n_objectives())
151 call constraint_value%init(
problem%get_n_constraints())
152 call objective_sensitivities%init(n)
153 call constraint_sensitivities%init(
problem%get_n_constraints(), n)
156 scaling_factor = 1.0_rp
157 if (pe_rank .eq. 0)
then
158 print *,
"max_iterations for the optimization loop = ", &
165 call problem%get_objective_value(objective_value)
166 call problem%get_constraint_values(constraint_value)
167 call problem%get_objective_sensitivities(objective_sensitivities)
168 call problem%get_constraint_sensitivities(constraint_sensitivities)
169 call problem%get_all_objective_values(all_objectives)
172 call mma_logger_assemble_data(log_data, 0, objective_value, &
173 all_objectives, constraint_value, 0.0_rp, 0.0_rp, scaling_factor, &
175 call this%logger%write(log_data)
177 if (
present(simulation))
call simulation%write(0)
180 do iter = 1, this%max_iterations
181 if (this%mma%get_residumax() .lt. this%tolerance)
exit
184 if (this%auto_scale .eqv. .true.)
then
185 scaling_factor = abs(this%scale/constraint_value%x(1))
187 scaling_factor = abs(this%scale)
192 call vector_cmult(constraint_value, scaling_factor)
194 if (neko_bcknd_device .eq. 1)
then
195 call device_cmult(constraint_sensitivities%x_d, scaling_factor, &
196 constraint_sensitivities%size())
198 call cmult(constraint_sensitivities%x, scaling_factor, &
199 constraint_sensitivities%size())
203 call this%mma%update(iter, x, objective_sensitivities, &
204 constraint_value, constraint_sensitivities)
206 call design%update_design(x)
211 call problem%get_objective_value(objective_value)
212 call problem%get_constraint_values(constraint_value)
213 call problem%get_objective_sensitivities(objective_sensitivities)
214 call problem%get_constraint_sensitivities(constraint_sensitivities)
215 call problem%get_all_objective_values(all_objectives)
217 call this%mma%KKT(x, objective_sensitivities, &
218 constraint_value, constraint_sensitivities)
221 call mma_logger_assemble_data(log_data, iter, objective_value, &
222 all_objectives, constraint_value, this%mma%get_residumax(), &
223 this%mma%get_residunorm(), scaling_factor, &
225 call this%logger%write(log_data)
227 if (
present(simulation))
call simulation%write(iter)
234 if (pe_rank .eq. 0)
then
235 print *,
"MMA Optimization completed after", iter-1,
"iterations."
241 call all_objectives%free()
242 call constraint_value%free()
243 call objective_sensitivities%free()
244 call constraint_sensitivities%free()
246 end subroutine mma_optimizer_run
249 subroutine mma_optimizer_validate(this, problem, design)
254 type(vector_t) :: constraint_values
256 call constraint_values%init(
problem%get_n_constraints())
257 call problem%get_constraint_values(constraint_values)
258 if (neko_bcknd_device .eq. 1)
then
259 call device_memcpy(constraint_values%x, constraint_values%x_d, &
260 constraint_values%size(), host_to_device, .true.)
263 if (any(constraint_values%x .gt. 0.0_rp))
then
264 call neko_error(
"MMA optimizer validation failed: " // &
265 "Constraints are not satisfied.")
269 call constraint_values%free()
271 end subroutine mma_optimizer_validate
274 subroutine mma_optimizer_free(this)
279 end subroutine mma_optimizer_free
282 subroutine mma_logger_assemble_data(log_data, iter, objective_value, &
283 all_objectives, constraint_value, residumax, residunorm, &
284 scaling_factor, n, m)
285 type(vector_t),
intent(out) :: log_data
286 integer,
intent(in) :: iter
287 real(kind=rp),
intent(in) ::objective_value
288 type(vector_t),
intent(in) :: all_objectives
289 type(vector_t),
intent(in) :: constraint_value
290 real(kind=rp),
intent(in) :: residumax, residunorm, scaling_factor
291 integer,
intent(in) :: n, m
292 integer :: i_tmp1, i_tmp2
296 call log_data%init(5 + n + m)
299 log_data%x(1) = real(iter, kind=rp)
302 log_data%x(2) = objective_value
306 i_tmp2 = i_tmp1 + n - 1
307 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
311 i_tmp2 = i_tmp1 + m - 1
312 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
315 log_data%x(i_tmp2 + 1) = residumax
316 log_data%x(i_tmp2 + 2) = residunorm
317 log_data%x(i_tmp2 + 3) = scaling_factor
319 end subroutine mma_logger_assemble_data
320end module mma_optimizer
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.
Abstract optimizer class.
The abstract problem type.