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
28 use field_math, only: field_rzero
29 use neko_ext, only: reset
31 use device, only: device_memcpy, host_to_device, device_to_host
32
33 use device_math, only: device_copy
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 :: run => mma_optimizer_run
61 procedure :: free => mma_optimizer_free
62
63 end type mma_optimizer_t
64
65contains
66
68 subroutine mma_optimizer_init_from_json(this, parameters, problem, design, &
69 max_iterations, tolerance, simulation)
70 class(mma_optimizer_t), intent(inout) :: this
71 type(json_file), intent(inout) :: parameters
72 class(problem_t), intent(in) :: problem
73 class(design_t), intent(in) :: design
74 integer, intent(in) :: max_iterations
75 real(kind=rp), intent(in) :: tolerance
76 type(simulation_t), optional, intent(in) :: simulation
77
78 character(len=1024) :: optimization_header
79 character(len=1024) :: problem_header
80
81 type(vector_t) :: x
82 type(json_file) :: solver_parameters
83
84 ! Initialize the logger
85 call this%logger%init('optimization_data.csv')
86
87 ! Write the header
88 problem_header = problem%get_log_header()
89 optimization_header = 'iter, ' // trim(problem_header) // &
90 ', KKTmax, KKTnorm2, scaling factor'
91 call this%logger%set_header(trim(optimization_header))
92
93 x = design%get_values()
94
95 if (pe_rank .eq. 0) then
96 print *, "Initializing mma_optimizer with steady_state_problem_t."
97 end if
98
99 call json_extract_object(parameters, "optimization.solver", &
100 solver_parameters)
101 call this%mma%init(x%x, design%size(), problem%get_n_constraints(), &
102 solver_parameters, this%scale, this%auto_scale)
103
104 call this%init_from_components(problem, design, &
105 max_iterations, tolerance, simulation)
106
107 call x%free()
108 end subroutine mma_optimizer_init_from_json
109
111 subroutine mma_optimizer_init_from_components(this, problem, design, &
112 max_iterations, tolerance, simulation)
113 class(mma_optimizer_t), intent(inout) :: this
114 class(problem_t), intent(in) :: problem
115 class(design_t), intent(in) :: design
116 integer, intent(in) :: max_iterations
117 real(kind=rp), intent(in) :: tolerance
118 type(simulation_t), intent(in), optional :: simulation
119
120 call this%init_base(max_iterations, tolerance)
121
122 end subroutine mma_optimizer_init_from_components
123
125 subroutine mma_optimizer_run(this, problem, design, simulation)
126 class(mma_optimizer_t), intent(inout) :: this
127 class(problem_t), intent(inout) :: problem
128 class(design_t), intent(inout) :: design
129 type(simulation_t), optional, intent(inout) :: simulation
130
131 type(vector_t) :: x
132
133 integer :: iter, ierr, nglobal, n
134 real(kind=rp) :: scaling_factor
135
136 real(kind=rp) :: objective_value
137 type(vector_t) :: all_objectives
138 type(vector_t) :: constraint_value
139 type(vector_t) :: objective_sensitivities
140 type(matrix_t) :: constraint_sensitivities
141
142 type(vector_t) :: log_data
143
144 n = design%size()
145 call mpi_allreduce(n, nglobal, 1, mpi_integer, mpi_sum, neko_comm, ierr)
146
148 scaling_factor = 1.0_rp
149 if (pe_rank .eq. 0) then
150 print *, "max_iterations for the optimization loop = ", &
151 this%max_iterations
152 end if
153
154 if (present(simulation)) call simulation%run_forward()
155
156 call problem%compute(design)
157
158 if (present(simulation)) call simulation%run_backward()
159 call problem%compute_sensitivity(design)
160
161 call problem%get_objective_value(objective_value)
162 call problem%get_constraint_values(constraint_value)
163 call problem%get_objective_sensitivities(objective_sensitivities)
164 call problem%get_constraint_sensitivities(constraint_sensitivities)
165 call problem%get_all_objective_values(all_objectives)
166
167 ! Stamp the initial condition
168 call mma_logger_assemble_data(log_data, 0, objective_value, &
169 all_objectives, constraint_value, 0.0_rp, 0.0_rp, scaling_factor, &
170 problem%get_n_objectives(), problem%get_n_constraints())
171 call this%logger%write(log_data)
172
173 if (present(simulation)) call simulation%write(0)
174
175 call design%write(0)
176
177 do iter = 1, this%max_iterations
178 if (this%mma%get_residumax() .lt. this%tolerance) exit
179
180 ! Scaling
181 if (this%auto_scale .eqv. .true.) then
182 scaling_factor = abs(this%scale/constraint_value%x(1))
183 else
184 scaling_factor = abs(this%scale)
185 end if
186
187 x = design%get_values()
188
189 constraint_value = scaling_factor * constraint_value
190 constraint_sensitivities = scaling_factor * constraint_sensitivities
191
192 ! Use scaled sensitivities to update the design variable
193 call this%mma%update(iter, x, objective_sensitivities, &
194 constraint_value, constraint_sensitivities)
195
196 call design%update_design(x)
197
198 if (present(simulation)) call simulation%run_forward()
199 call problem%compute(design)
200
201 if (present(simulation)) call simulation%run_backward()
202 call problem%compute_sensitivity(design)
203
204 call problem%get_objective_value(objective_value)
205 call problem%get_constraint_values(constraint_value)
206 call problem%get_objective_sensitivities(objective_sensitivities)
207 call problem%get_constraint_sensitivities(constraint_sensitivities)
208 call problem%get_all_objective_values(all_objectives)
209
210 call this%mma%KKT(x, objective_sensitivities, &
211 constraint_value, constraint_sensitivities)
212
213 ! Stamp the i^th iteration
214 call mma_logger_assemble_data(log_data, iter, objective_value, &
215 all_objectives, constraint_value, this%mma%get_residumax(), &
216 this%mma%get_residunorm(), scaling_factor, &
217 problem%get_n_objectives(), problem%get_n_constraints())
218 call this%logger%write(log_data)
219
220 if (present(simulation)) call simulation%write(iter)
221 call design%write(iter)
222 if (present(simulation)) call simulation%reset()
223 end do
224
225 ! Final state after optimization
226 if (pe_rank .eq. 0) then
227 print *, "MMA Optimization completed after", iter-1, "iterations."
228 end if
229
230 call constraint_value%free()
231 call objective_sensitivities%free()
232 call constraint_sensitivities%free()
233
234 end subroutine mma_optimizer_run
235
236 ! Free resources associated with the MMA optimizer
237 subroutine mma_optimizer_free(this)
238 class(mma_optimizer_t), intent(inout) :: this
239
240 ! Free MMA-specific data
241 call this%mma%free()
242 end subroutine mma_optimizer_free
243
244 ! package up the log data
245 subroutine mma_logger_assemble_data(log_data, iter, objective_value, &
246 all_objectives, constraint_value, residumax, residunorm, &
247 scaling_factor, n, m)
248 type(vector_t), intent(out) :: log_data
249 integer, intent(in) :: iter
250 real(kind=rp), intent(in) ::objective_value
251 type(vector_t), intent(in) :: all_objectives
252 type(vector_t), intent(in) :: constraint_value
253 real(kind=rp), intent(in) :: residumax, residunorm, scaling_factor
254 integer, intent(in) :: n, m
255 integer :: i_tmp1, i_tmp2
256
257 ! initialize the logger data
258 ! iter | tot F | F_1 | .. |F_n | C_1 | ... | C_n | KKT | KKT2 | scale |
259 call log_data%init(5 + n + m)
260
261 ! iteration
262 log_data%x(1) = real(iter, kind=rp)
263
264 ! total objective
265 log_data%x(2) = objective_value
266
267 ! individual objectives
268 i_tmp1 = 3
269 i_tmp2 = i_tmp1 + n - 1
270 log_data%x(i_tmp1 : i_tmp2) = all_objectives%x
271
272 ! constraints
273 i_tmp1 = i_tmp2 + 1
274 i_tmp2 = i_tmp1 + m - 1
275 log_data%x(i_tmp1 : i_tmp2) = constraint_value%x
276
277 ! convergence stuff
278 log_data%x(i_tmp2 + 1) = residumax
279 log_data%x(i_tmp2 + 2) = residunorm
280 log_data%x(i_tmp2 + 3) = scaling_factor
281
282 end subroutine mma_logger_assemble_data
283end module mma_optimizer
284
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:50
Module for handling the optimization problem.
Definition problem.f90:35
Implements the steady_problem_t type.
An abstract design type.
Definition design.f90:50
Abstract optimizer class.
Definition optimizer.f90:18
The abstract problem type.
Definition problem.f90:61