Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
mma_optimizer.f90
1module mma_optimizer
2 use optimizer, only: optimizer_t
3 use problem, only : problem_t
4 use mma, only: mma_t
5 use problem, only: problem_t
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
10 use simulation_m, only: simulation_t
11 use design, only: design_t
12 use field, only: field_t
13 use field_registry, only: neko_field_registry
14
15 use vector, only: vector_t
16 use matrix, only: matrix_t
17
18 !only to print nglobal when running in parallel
19 use comm, only: neko_comm, pe_rank
20 use mpi_f08, only: mpi_integer, mpi_sum, mpi_allreduce
21
22 use neko_config, only: neko_bcknd_device
23 ! Inclusions from external dependencies and standard libraries
24 use, intrinsic :: iso_fortran_env, only: stderr => error_unit
25
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
30 use neko_ext, only: reset
32 use device, only: device_memcpy, host_to_device, device_to_host
33
34 implicit none
35 private
36 public :: mma_optimizer_t
37
38 ! Concrete type for MMA optimizer
39 type, extends(optimizer_t) :: mma_optimizer_t
40
41 type(mma_t) :: mma
42
49 real(kind=rp) :: scale
50 logical :: auto_scale
51
52 contains
53
54 ! Override the deferred methods
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
59
60 procedure, pass(this) :: run => mma_optimizer_run
61 procedure, pass(this) :: validate => mma_optimizer_validate
62 procedure, pass(this) :: free => mma_optimizer_free
63
64 end type mma_optimizer_t
65
66contains
67
69 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
70 max_iterations, tolerance, simulation)
71 class(mma_optimizer_t), intent(inout) :: this
72 type(json_file), intent(inout) :: parameters
73 class(problem_t), intent(in) :: problem
74 class(design_t), intent(in) :: design
75 integer, intent(in) :: max_iterations
76 real(kind=rp), intent(in) :: tolerance
77 type(simulation_t), optional, intent(in) :: simulation
78
79 character(len=1024) :: optimization_header
80 character(len=1024) :: problem_header
81
82 type(vector_t) :: x
83 type(json_file) :: solver_parameters
84
85 ! Initialize the logger
86 call this%logger%init('optimization_data.csv')
87
88 ! Write the header
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))
93
94 call design%get_values(x)
95
96 if (pe_rank .eq. 0) then
97 print *, "Initializing mma_optimizer with steady_state_problem_t."
98 end if
99
100 call json_extract_object(parameters, "optimization.solver", &
101 solver_parameters)
102 call this%mma%init(x%x, design%size(), problem%get_n_constraints(), &
103 solver_parameters, this%scale, this%auto_scale)
104
105 call this%init_from_components(problem, design, &
106 max_iterations, tolerance, simulation)
107
108 call x%free()
109 end subroutine mma_optimizer_init_from_json
110
112 subroutine mma_optimizer_init_from_components(this, problem, design, &
113 max_iterations, tolerance, simulation)
114 class(mma_optimizer_t), intent(inout) :: this
115 class(problem_t), intent(in) :: problem
116 class(design_t), intent(in) :: design
117 integer, intent(in) :: max_iterations
118 real(kind=rp), intent(in) :: tolerance
119 type(simulation_t), intent(in), optional :: simulation
120
121 call this%init_base(max_iterations, tolerance)
122
123 end subroutine mma_optimizer_init_from_components
124
126 subroutine mma_optimizer_run(this, problem, design, simulation)
127 class(mma_optimizer_t), intent(inout) :: this
128 class(problem_t), intent(inout) :: problem
129 class(design_t), intent(inout) :: design
130 type(simulation_t), optional, intent(inout) :: simulation
131
132 type(vector_t) :: x
133
134 integer :: iter, ierr, nglobal, n
135 real(kind=rp) :: scaling_factor
136
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
142
143 type(vector_t) :: log_data
144
145 n = design%size()
146 call mpi_allreduce(n, nglobal, 1, mpi_integer, mpi_sum, neko_comm, ierr)
147
148 ! Initialize the vectors
149 call x%init(n)
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)
154
156 scaling_factor = 1.0_rp
157 if (pe_rank .eq. 0) then
158 print *, "max_iterations for the optimization loop = ", &
159 this%max_iterations
160 end if
161
162 call problem%compute(design, simulation)
163 call problem%compute_sensitivity(design, simulation)
164
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)
170
171 ! Stamp the initial condition
172 call mma_logger_assemble_data(log_data, 0, objective_value, &
173 all_objectives, constraint_value, 0.0_rp, 0.0_rp, scaling_factor, &
174 problem%get_n_objectives(), problem%get_n_constraints())
175 call this%logger%write(log_data)
176
177 if (present(simulation)) call simulation%write(0)
178 call design%write(0)
179
180 do iter = 1, this%max_iterations
181 if (this%mma%get_residumax() .lt. this%tolerance) exit
182
183 ! Scaling
184 if (this%auto_scale .eqv. .true.) then
185 scaling_factor = abs(this%scale/constraint_value%x(1))
186 else
187 scaling_factor = abs(this%scale)
188 end if
189
190 call design%get_values(x)
191
192 call vector_cmult(constraint_value, scaling_factor)
193
194 if (neko_bcknd_device .eq. 1) then
195 call device_cmult(constraint_sensitivities%x_d, scaling_factor, &
196 constraint_sensitivities%size())
197 else
198 call cmult(constraint_sensitivities%x, scaling_factor, &
199 constraint_sensitivities%size())
200 end if
201
202 ! Use scaled sensitivities to update the design variable
203 call this%mma%update(iter, x, objective_sensitivities, &
204 constraint_value, constraint_sensitivities)
205
206 call design%update_design(x)
207
208 call problem%compute(design, simulation)
209 call problem%compute_sensitivity(design, simulation)
210
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)
216
217 call this%mma%KKT(x, objective_sensitivities, &
218 constraint_value, constraint_sensitivities)
219
220 ! Stamp the i^th iteration
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, &
224 problem%get_n_objectives(), problem%get_n_constraints())
225 call this%logger%write(log_data)
226
227 if (present(simulation)) call simulation%write(iter)
228 call design%write(iter)
229 end do
230
231 call this%validate(problem, design)
232
233 ! Final state after optimization
234 if (pe_rank .eq. 0) then
235 print *, "MMA Optimization completed after", iter-1, "iterations."
236 end if
237
238 ! Free local resources
239 call x%free()
240 call log_data%free()
241 call all_objectives%free()
242 call constraint_value%free()
243 call objective_sensitivities%free()
244 call constraint_sensitivities%free()
245
246 end subroutine mma_optimizer_run
247
249 subroutine mma_optimizer_validate(this, problem, design)
250 class(mma_optimizer_t), intent(inout) :: this
251 class(problem_t), intent(in) :: problem
252 class(design_t), intent(in) :: design
253
254 type(vector_t) :: constraint_values
255
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.)
261 end if
262
263 if (any(constraint_values%x .gt. 0.0_rp)) then
264 call neko_error("MMA optimizer validation failed: " // &
265 "Constraints are not satisfied.")
266 end if
267
268 ! Free local resources
269 call constraint_values%free()
270
271 end subroutine mma_optimizer_validate
272
273 ! Free resources associated with the MMA optimizer
274 subroutine mma_optimizer_free(this)
275 class(mma_optimizer_t), intent(inout) :: this
276
277 ! Free MMA-specific data
278 call this%mma%free()
279 end subroutine mma_optimizer_free
280
281 ! package up the log data
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
293
294 ! initialize the logger data
295 ! iter | tot F | F_1 | .. |F_n | C_1 | ... | C_n | KKT | KKT2 | scale |
296 call log_data%init(5 + n + m)
297
298 ! iteration
299 log_data%x(1) = real(iter, kind=rp)
300
301 ! total objective
302 log_data%x(2) = objective_value
303
304 ! individual objectives
305 i_tmp1 = 3
306 i_tmp2 = i_tmp1 + n - 1
307 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
308
309 ! constraints
310 i_tmp1 = i_tmp2 + 1
311 i_tmp2 = i_tmp1 + m - 1
312 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
313
314 ! convergence stuff
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
318
319 end subroutine mma_logger_assemble_data
320end module mma_optimizer
321
Implements the design_t.
Definition design.f90:34
Some common Masking operations we may need.
Definition mask_ops.f90:34
Contains extensions to the neko library required to run the topology optimization code.
Definition neko_ext.f90:9
subroutine, public reset(neko_case)
Reset the case data structure.
Definition neko_ext.f90:55
Module for handling the optimization problem.
Definition problem.f90:35
Implements the steady_problem_t type.
An abstract design type.
Definition design.f90:52
Abstract optimizer class.
Definition optimizer.f90:18
The abstract problem type.
Definition problem.f90:61