Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
mma.f90
1!===========================================================================!
2! Method of Moving Asymptotes !
3! This implementation is based on the following documents: !
4! 1-https://people.kth.se/~krille/mmagcmma.pdf !
5! 2-https://people.kth.se/~krille/originalmma.pdf !
6! 2-https://comsolyar.com/wp-content/uploads/2020/03/gcmma.pdf !
7! ------------------------------------------------------------------------- !
8! !
9! This module solves the following original optimization problem: !
10! !
11! Minimize f_0(x) + a_0*z + sum( c_i*y_i + 0.5*d_i*(y_i)^2 ) !
12! subject to f_i(x) - a_i*z - y_i <= 0, i = 1,...,m !
13! xmin_j <= x_j <= xmax_j, j = 1,...,n !
14! z >= 0, y_i >= 0, i = 1,...,m !
15! !
16! by first creating the following convex approximation of the original !
17! problem: !
18! !
19! Minimize sum_{j = 1,...,n} (p0j / (upp_j-x_j) + q0j / (x_j-low_j)) + !
20! a0*z + sum_i = 1,...,m(c_i*y_i + 0.5*d_i*y_i^2) !
21! subject to sum_{j = 1,...,n} (pij / (upp_j-x_j) + qij / (x_j-low_j)) + !
22! a_i*z + y_i <= b_i, i = 1,...,m !
23! xmin_j <= alpha_j <= x_j <= beta_j <= xmax_j j = 1,...,n !
24! y_i>=0 i = 1,...,m !
25! z>=0 !
26! !
27! note that based on eq(3.5) there should be r0 in the approximated problem !
28! however since it is just a constant added to a minimization problem, it !
29! is ignored. !
30! A primal-dual algorithm is then employed to solve the aproximated problem !
31! using interior point method. !
32!===========================================================================!
33
34module mma
35 ! Inclusions from Neko
36 use num_types, only: rp
37 use json_module, only: json_file
38 use json_utils, only: json_get_or_default
39 use vector, only: vector_t
40 use matrix, only: matrix_t
41 use mpi_f08, only: mpi_allreduce, mpi_integer, mpi_sum, mpi_comm_world
42 use comm, only: pe_rank
43 use utils, only: neko_error
44 use neko_config, only: neko_bcknd_device, neko_bcknd_cuda, neko_bcknd_hip, &
45 neko_bcknd_opencl
46 use device, only: device_memcpy, host_to_device, device_to_host
47 use, intrinsic :: iso_c_binding, only: c_ptr
48
49 implicit none
50 private
51
52 type, public :: mma_t
53 private
54 integer :: n, m, max_iter
55 real(kind=rp) :: a0, f0val, asyinit, asyincr, asydecr, epsimin, &
56 residumax, residunorm
57 type(vector_t) :: xold1, xold2, low, upp, alpha, beta, a, c, d, xmax, xmin
58 logical :: is_initialized = .false.
59 logical :: is_updated = .false.
60 character(len=:), allocatable :: bcknd, subsolver
61
62 ! Internal dummy variables for MMA
63 type(vector_t) :: p0j, q0j
64 type(matrix_t) :: pij, qij
65 type(vector_t) :: bi
66
67 !---nesessary for KKT check after updating df0dx, fval, dfdx --------
68 real(kind=rp) :: z, zeta
69 type(vector_t) :: y, lambda, s, mu
70 type(vector_t) :: xsi, eta
71 contains
73 generic, public :: init => init_from_json, init_from_components
74 procedure, public, pass(this) :: init_from_json => mma_init_from_json
75 procedure, public, pass(this) :: init_from_components => &
76 mma_init_from_components
77 procedure, public, pass(this) :: free => mma_free
78 procedure, public, pass(this) :: get_n => mma_get_n
79 procedure, public, pass(this) :: get_m => mma_get_m
80 procedure, public, pass(this) :: get_residumax => mma_get_residumax
81 procedure, public, pass(this) :: get_residunorm => mma_get_residunorm
82 procedure, public, pass(this) :: get_max_iter => mma_get_max_iter
83
84 generic, public :: update => update_vector, update_cpu, update_device
85 procedure, pass(this) :: update_vector => mma_update_vector
86 procedure, pass(this) :: update_cpu => mma_update_cpu
87 procedure, pass(this) :: update_device => mma_update_device
88
89 generic, public :: kkt => kkt_vector, kkt_cpu, kkt_device
90 procedure, pass(this) :: kkt_vector => mma_kkt_vector
91 procedure, pass(this) :: kkt_cpu => mma_kkt_cpu
92 procedure, pass(this) :: kkt_device => mma_kkt_device
93
94 end type mma_t
95
96 interface
97 ! ======================================================================= !
98 ! interface for cpu backend module subroutines
99
101 module subroutine mma_update_cpu(this, iter, x, df0dx, fval, dfdx)
102 class(mma_t), intent(inout) :: this
103 integer, intent(in) :: iter
104 real(kind=rp), dimension(this%n), intent(inout) :: x
105 real(kind=rp), dimension(this%n), intent(in) :: df0dx
106 real(kind=rp), dimension(this%m), intent(in) :: fval
107 real(kind=rp), dimension(this%m, this%n), intent(in) :: dfdx
108 end subroutine mma_update_cpu
109
111 module subroutine mma_kkt_cpu(this, x, df0dx, fval, dfdx)
112 class(mma_t), intent(inout) :: this
113 real(kind=rp), dimension(this%n), intent(in) :: x
114 real(kind=rp), dimension(this%n), intent(in) :: df0dx
115 real(kind=rp), dimension(this%m), intent(in) :: fval
116 real(kind=rp), dimension(this%m, this%n), intent(in) :: dfdx
117 end subroutine mma_kkt_cpu
118
119 ! ======================================================================= !
120 ! interface for device backend module subroutines
121
123 module subroutine mma_update_device(this, iter, x, df0dx, fval, dfdx)
124 class(mma_t), intent(inout) :: this
125 integer, intent(in) :: iter
126 type(c_ptr), intent(inout) :: x
127 type(c_ptr), intent(in) :: df0dx, fval, dfdx
128 end subroutine mma_update_device
129
131 module subroutine mma_kkt_device(this, x, df0dx, fval, dfdx)
132 class(mma_t), intent(inout) :: this
133 type(c_ptr), intent(in) :: x, df0dx, fval, dfdx
134 end subroutine mma_kkt_device
135
136 end interface
137
138contains
139
141 subroutine mma_init_from_json(this, x, n, m, json, scale, auto_scale)
142 ! ----------------------------------------------------- !
143 ! Initializing the mma object and all the parameters !
144 ! required for MMA method. (a_i, c_i, d_i, ...) !
145 ! x: the design varaibles(DV), n: number of DV, !
146 ! m: number of constraints !
147 ! !
148 ! Note that residumax & residunorm of the KKT conditions!
149 ! are initialized with 10^5. This is done to avoid !
150 ! unnecessary extera computation of KKT norms for the !
151 ! initial design. !
152 ! ----------------------------------------------------- !
153 class(mma_t), intent(inout) :: this
154 integer, intent(in) :: n, m
155 real(kind=rp), intent(in), dimension(n) :: x
156
157 type(json_file), intent(inout) :: json
158
159 ! Read the scaling info for fval and dfdx from json
160 real(kind=rp), intent(out) :: scale
161 logical, intent(out) :: auto_scale
162 ! -------------------------------------------------------------------!
163 ! Internal parameters for MMA !
164 ! Minimize f_0(x) + a_0*z + sum( c_i*y_i + 0.5*d_i*(y_i)^2 ) !
165 ! subject to f_i(x) - a_i*z - y_i <= 0, i = 1,...,m !
166 ! xmin_j <= x_j <= xmax_j, j = 1,...,n !
167 ! z >= 0, y_i >= 0, i = 1,...,m !
168 ! -------------------------------------------------------------------!
169 real(kind=rp), dimension(n) :: xmax, xmin
170 real(kind=rp), dimension(m) :: a, c, d
171 character(len=:), allocatable :: subsolver, bcknd, bcknd_default
172
173 ! For reading the values from json and then set the value for the arrays
174 real(kind=rp) :: a0 , xmax_const, xmin_const, a_const, c_const, d_const
175
176 integer :: max_iter, n_global, ierr
177 real(kind=rp) :: epsimin, asyinit, asyincr, asydecr
178
179 call mpi_allreduce(n, n_global, 1, mpi_integer, &
180 mpi_sum, mpi_comm_world, ierr)
181
182 ! Assign default values for the backend based on the NEKO_BCKND_DEVICE
183 if (neko_bcknd_device .eq. 1) then
184 bcknd_default = "device"
185 else
186 bcknd_default = "cpu"
187 end if
188
189 ! ------------------------------------------------------------------------ !
190 ! Assign defaults if nothing is parsed
191 ! based on the Cpp Code by Niels
192 call json_get_or_default(json, 'mma.epsimin', epsimin, &
193 1.0e-9_rp * sqrt(real(m + n_global, rp)))
194 call json_get_or_default(json, 'mma.max_iter', max_iter, 100)
195
196 ! Following parameters are set based on eq.3.8:--------
197 call json_get_or_default(json, 'mma.asyinit', asyinit, 0.5_rp)
198 call json_get_or_default(json, 'mma.asyincr', asyincr, 1.2_rp)
199 call json_get_or_default(json, 'mma.asydecr', asydecr, 0.7_rp)
200
201 call json_get_or_default(json, 'mma.backend', bcknd, bcknd_default)
202 call json_get_or_default(json, 'mma.subsolver', subsolver, 'dip')
203
204 call json_get_or_default(json, 'mma.xmin', xmin_const, 0.0_rp)
205 call json_get_or_default(json, 'mma.xmax', xmax_const, 1.0_rp)
206 call json_get_or_default(json, 'mma.a0', a0, 1.0_rp)
207 call json_get_or_default(json, 'mma.a', a_const, 0.0_rp)
208 call json_get_or_default(json, 'mma.c', c_const, 100.0_rp)
209 call json_get_or_default(json, 'mma.d', d_const, 0.0_rp)
210
211 call json_get_or_default(json, 'mma.scale', scale, 10.0_rp)
212 call json_get_or_default(json, 'mma.auto_scale', auto_scale, .false.)
213
214 call json_get_or_default(json, 'mma.epsimin', epsimin, 1.0e-9_rp)
215 ! Initialize the MMA object with the parsed parameters
216 a = a_const
217 c = c_const
218 d = d_const
219 xmin = xmin_const
220 xmax = xmax_const
221 ! initializing the mma concrete type (mma_cpu_t or mma_device_t)
222 if (pe_rank .eq. 0) then
223 print *, "Initializing MMA backend to >>> ", bcknd
224 end if
225
226 ! ------------------------------------------------------------------------ !
227 ! Initialize the MMA object with the parameters read from json
228 ! call this%init(x, n, m, a0, a, c, d, xmin, xmax, &
229 ! max_iter, epsimin, asyinit, asyincr, asydecr, bcknd)
230 call this%init(x, n, m, a0, a, c, d, xmin, xmax, &
231 max_iter, epsimin, asyinit, asyincr, asydecr, bcknd, subsolver)
232
233 end subroutine mma_init_from_json
234
236 subroutine mma_free(this)
237 class(mma_t), intent(inout) :: this
238 ! Deallocate the internal vectors
239 call this%xold1%free()
240 call this%xold2%free()
241 call this%alpha%free()
242 call this%beta%free()
243 call this%a%free()
244 call this%c%free()
245 call this%d%free()
246 call this%low%free()
247 call this%upp%free()
248 call this%xmax%free()
249 call this%xmin%free()
250 call this%p0j%free()
251 call this%q0j%free()
252 call this%bi%free()
253 call this%y%free()
254 call this%lambda%free()
255 call this%s%free()
256 call this%mu%free()
257 call this%xsi%free()
258 call this%eta%free()
259
260 ! Deallocate the internal dummy matrices
261 call this%pij%free()
262 call this%qij%free()
263
264 this%is_initialized = .false.
265 this%is_updated = .false.
266 end subroutine mma_free
267
269 subroutine mma_init_from_components(this, x, n, m, a0, a, c, d, xmin, xmax, &
270 max_iter, epsimin, asyinit, asyincr, asydecr, bcknd, subsolver)
271 ! ----------------------------------------------------- !
272 ! Initializing the mma object and all the parameters !
273 ! required for MMA method. (a_i, c_i, d_i, ...) !
274 ! x: the design varaibles(DV), n: number of DV, !
275 ! m: number of constraints !
276 ! !
277 ! Note that residumax & residunorm of the KKT conditions!
278 ! are initialized with 10^5. This is done to avoid !
279 ! unnecessary extera computation of KKT norms for the !
280 ! initial design. !
281 ! ----------------------------------------------------- !
282 class(mma_t), intent(inout) :: this
283 integer, intent(in) :: n, m
284 real(kind=rp), intent(in), dimension(n) :: x
285 ! -------------------------------------------------------------------!
286 ! Internal parameters for MMA !
287 ! Minimize f_0(x) + a_0*z + sum( c_i*y_i + 0.5*d_i*(y_i)^2 ) !
288 ! subject to f_i(x) - a_i*z - y_i <= 0, i = 1,...,m !
289 ! xmin_j <= x_j <= xmax_j, j = 1,...,n !
290 ! z >= 0, y_i >= 0, i = 1,...,m !
291 ! -------------------------------------------------------------------!
292 real(kind=rp), intent(in), dimension(n) :: xmax, xmin
293 real(kind=rp), intent(in), dimension(m) :: a, c, d
294 real(kind=rp), intent(in) :: a0
295 integer, intent(in), optional :: max_iter
296 real(kind=rp), intent(in), optional :: epsimin, asyinit, asyincr, asydecr
297 character(len=:), intent(in), allocatable :: bcknd, subsolver
298
299 call this%free()
300
301 this%n = n
302 this%m = m
303
304 call this%xold1%init(n)
305 call this%xold2%init(n)
306 this%xold1%x = x
307 this%xold2%x = x
308
309 call this%alpha%init(n)
310 call this%beta%init(n)
311
312 call this%a%init(m)
313 call this%c%init(m)
314 call this%d%init(m)
315 call this%low%init(n)
316 call this%upp%init(n)
317 call this%xmax%init(n)
318 call this%xmin%init(n)
319
320 !internal dummy variables for MMA
321 call this%p0j%init(n)
322 call this%q0j%init(n)
323 call this%pij%init(m, n)
324 call this%qij%init(m, n)
325 call this%bi%init(m)
326
327 !---nesessary for KKT check after updating df0dx, fval, dfdx --------
328 call this%y%init(m)
329 call this%lambda%init(m)
330 call this%s%init(m)
331 call this%mu%init(m)
332 call this%xsi%init(n)
333 call this%eta%init(n)
334
335 this%a0 = a0
336 this%a%x = a
337 this%c%x = c
338 this%d%x = d
339
340 !setting the bounds for the design variable based on the problem
341 this%xmax%x = xmax
342 this%xmin%x = xmin
343
344 this%low%x(:) = minval(x)
345 this%upp%x(:) = maxval(x)
346
347 !setting KKT norms to a large number for the initial design
348 this%residumax = huge(0.0_rp)
349 this%residunorm = huge(0.0_rp)
350
351 ! Select backend type
352 select case (bcknd)
353 case ("cpu")
354 if (pe_rank == 0) then
355 print *, "MMA initialized with CPU backend!"
356 end if
357 case ("device")
358 ! upload all init values to device pointers
359 call device_memcpy(this%xold1%x, this%xold1%x_d, this%n, &
360 host_to_device, sync = .false.)
361 call device_memcpy(this%xold1%x, this%xold2%x_d, this%n, &
362 host_to_device, sync = .false.)
363 call device_memcpy(this%a%x, this%a%x_d, this%m, host_to_device, &
364 sync = .false.)
365 call device_memcpy(this%c%x, this%c%x_d, this%m, host_to_device, &
366 sync = .false.)
367 call device_memcpy(this%d%x, this%d%x_d, this%m, host_to_device, &
368 sync = .false.)
369 call device_memcpy(this%xmax%x, this%xmax%x_d, this%n, host_to_device, &
370 sync = .false.)
371 call device_memcpy(this%xmin%x, this%xmin%x_d, this%n, host_to_device, &
372 sync = .false.)
373 if (pe_rank == 0) then
374 if (neko_bcknd_cuda .eq. 1) then
375 print *, "MMA initialized with CUDA backend!"
376 else if (neko_bcknd_hip .eq. 1) then
377 print *, "MMA initialized with HIP backend!"
378 else if (neko_bcknd_opencl .eq. 1) then
379 print *, "MMA initialized with OPENCL backend!"
380 else
381 call neko_error('Unknown backend device in mma_init_components')
382 end if
383 end if
384 case default
385 call neko_error('Unknown backend in mma_init_attributes')
386 end select
387
388 ! ------------------------------------------------------------------------ !
389 ! Assign defaults if nothing is parsed
390
391 ! based on the Cpp Code by Niels
392 if (.not. present(epsimin)) this%epsimin = 1.0e-9_rp * sqrt(real(m + n, rp))
393 if (.not. present(max_iter)) this%max_iter = 100
394
395 ! Following parameters are set based on eq.3.8:--------
396 if (.not. present(asyinit)) this%asyinit = 0.5_rp
397 if (.not. present(asyincr)) this%asyincr = 1.2_rp
398 if (.not. present(asydecr)) this%asydecr = 0.7_rp
399
400 ! Assign values from inputs when present
401 if (present(max_iter)) this%max_iter = max_iter
402 if (present(epsimin)) this%epsimin = epsimin
403 if (present(asyinit)) this%asyinit = asyinit
404 if (present(asyincr)) this%asyincr = asyincr
405 if (present(asydecr)) this%asydecr = asydecr
406 this%bcknd = bcknd
407 this%subsolver = subsolver
408 if (pe_rank == 0) then
409 if (this%subsolver .eq. "dip") then
410 print *, "Using dual solver for MMA subsolve."
411 elseif (this%subsolver .eq. "dpip") then
412 print *, "Using dual-primal solver for MMA subsolve."
413 else
414 call neko_error('Unknown subsolver for MMA, mma_init_from_components')
415 end if
416 end if
417
418 if (pe_rank .eq. 0) then
419 print *, "MMA is initialized with a0 = ", a0, ", a = ", a, ", c = ", c, &
420 ", d = ", d, "epsimin = ", this%epsimin
421 end if
422 !the object is correctly initialized
423 this%is_initialized = .true.
424 end subroutine mma_init_from_components
425
427 subroutine mma_update_vector(this, iter, x, df0dx, fval, dfdx)
428 class(mma_t), intent(inout) :: this
429 integer, intent(in) :: iter
430 type(vector_t), intent(inout) :: x
431 type(vector_t), intent(inout) :: df0dx, fval
432 type(matrix_t), intent(inout) :: dfdx
433
434 ! Select backend type
435 select case (this%bcknd)
436 case ("cpu")
437 if (neko_bcknd_device .eq. 1) then
438 call device_memcpy(x%x, x%x_d, this%n, device_to_host, &
439 sync = .false.)
440 call device_memcpy(df0dx%x, df0dx%x_d, this%n, device_to_host, &
441 sync = .false.)
442 call device_memcpy(fval%x, fval%x_d, this%m, device_to_host, &
443 sync = .false.)
444 call device_memcpy(dfdx%x, dfdx%x_d, this%m * this%n, device_to_host,&
445 sync = .true.)
446 end if
447
448 call mma_update_cpu(this, iter, x%x, df0dx%x, fval%x, dfdx%x)
449
450 if (neko_bcknd_device .eq. 1) then
451 call device_memcpy(x%x, x%x_d, this%n, host_to_device, sync = .true.)
452 end if
453
454 case ("device")
455 call mma_update_device(this, iter, x%x_d, df0dx%x_d, fval%x_d, dfdx%x_d)
456 call device_memcpy(x%x, x%x_d, this%n, device_to_host, sync = .true.)
457 end select
458
459 end subroutine mma_update_vector
460
462 subroutine mma_kkt_vector(this, x, df0dx, fval, dfdx)
463 class(mma_t), intent(inout) :: this
464 type(vector_t), intent(inout) :: x, df0dx, fval
465 type(matrix_t), intent(inout) :: dfdx
466
467 ! Select backend type
468 select case (this%bcknd )
469 case ("cpu")
470 if (neko_bcknd_device .eq. 1) then
471 call device_memcpy(x%x, x%x_d, this%n, device_to_host, &
472 sync = .false.)
473 call device_memcpy(df0dx%x, df0dx%x_d, this%n, device_to_host, &
474 sync = .false.)
475 call device_memcpy(fval%x, fval%x_d, this%m, device_to_host, &
476 sync = .false.)
477 call device_memcpy(dfdx%x, dfdx%x_d, this%m * this%n, device_to_host,&
478 sync = .true.)
479 end if
480
481 call mma_kkt_cpu(this, x%x, df0dx%x, fval%x, dfdx%x)
482 case ("device")
483 call mma_kkt_device(this, x%x_d, df0dx%x_d, fval%x_d, dfdx%x_d)
484 end select
485 end subroutine mma_kkt_vector
486
487 ! ========================================================================== !
488 ! Getters and setters
489
491 pure function mma_get_n(this) result(n)
492 class(mma_t), intent(in) :: this
493 integer :: n
494 n = this%n
495 end function mma_get_n
496
498 pure function mma_get_m(this) result(m)
499 class(mma_t), intent(in) :: this
500 integer :: m
501 m = this%m
502 end function mma_get_m
503
505 pure function mma_get_residumax(this) result(residumax)
506 class(mma_t), intent(in) :: this
507 real(kind=rp) :: residumax
508 residumax = this%residumax
509 end function mma_get_residumax
510
512 pure function mma_get_residunorm(this) result(residunorm)
513 class(mma_t), intent(in) :: this
514 real(kind=rp) :: residunorm
515 residunorm = this%residunorm
516 end function mma_get_residunorm
517
519 pure function mma_get_max_iter(this) result(max_iter_value)
520 class(mma_t), intent(in) :: this
521 integer :: max_iter_value
522 max_iter_value = this%max_iter
523 end function mma_get_max_iter
524
525end module mma