36 use num_types,
only: rp
37 use fld_file_output,
only: fld_file_output_t
41 use vector,
only: vector_t
42 use matrix,
only: matrix_t
43 use device,
only: device_memcpy, host_to_device, device_to_host
44 use neko_config,
only: neko_bcknd_device
45 use json_module,
only: json_file
46 use json_utils,
only: json_extract_item, json_get
48 use logger,
only: neko_log
49 use device_math,
only: device_copy
50 use vector,
only: vector_t
67 integer :: n_objectives
69 integer :: n_constraints
94 procedure, pass(this),
public :: compute_sensitivity => &
103 procedure, pass(this),
public :: read_constraints => &
121 procedure, pass(this) :: update_objectives => &
124 procedure, pass(this) :: update_constraints => &
127 procedure, pass(this) :: update_objective_sensitivities => &
130 procedure, pass(this) :: update_constraint_sensitivities => &
137 procedure, pass(this),
public :: get_objective_value => &
140 procedure, pass(this),
public :: get_all_objective_values => &
143 procedure, pass(this),
public :: get_constraint_values => &
146 procedure, pass(this),
public :: get_objective_sensitivities => &
149 procedure, pass(this),
public :: get_constraint_sensitivities => &
170 type(json_file),
intent(inout) :: parameters
171 class(
design_t),
intent(in) :: design
174 this%n_design =
design%size()
175 this%n_objectives = 0
176 this%n_constraints = 0
192 if (
allocated(this%objective_list))
then
193 do i = 1,
size(this%objective_list)
194 call this%objective_list(i)%free()
196 deallocate(this%objective_list)
200 if (
allocated(this%constraint_list))
then
201 do i = 1,
size(this%constraint_list)
202 call this%constraint_list(i)%free()
204 deallocate(this%constraint_list)
211 integer,
intent(in) :: idx
221 type(json_file),
intent(inout) :: parameters
223 class(
design_t),
intent(in) :: design
227 character(len=:),
allocatable :: path, type
228 type(json_file) :: objective_json
229 integer :: n_objectives, i
231 call neko_log%section(
"Reading objectives")
234 path =
"optimization.objectives"
235 call parameters%info(path, n_children = n_objectives)
238 do i = 1, n_objectives
239 call json_extract_item(parameters, path, i, objective_json)
240 call json_get(objective_json,
"type", type)
241 call neko_log%message(type)
248 call neko_log%end_section()
255 type(json_file),
intent(inout) :: parameters
257 class(
design_t),
intent(in) :: design
261 character(len=:),
allocatable :: path, type
262 type(json_file) :: constraint_json
263 integer :: n_constraints, i
265 call neko_log%section(
"Reading constraints")
268 path =
"optimization.constraints"
269 call parameters%info(path, n_children = n_constraints)
272 do i = 1, n_constraints
273 call json_extract_item(parameters, path, i, constraint_json)
274 call json_get(constraint_json,
"type", type)
275 call neko_log%message(type)
282 call neko_log%end_section()
289 class(
objective_t),
allocatable,
intent(inout) :: objective
294 if (
allocated(this%objective_list))
then
295 n =
size(this%objective_list)
296 call move_alloc(this%objective_list, temp_list)
297 allocate(this%objective_list(n + 1))
298 if (
allocated(temp_list))
then
300 call move_alloc(temp_list(i)%objective, &
301 this%objective_list(i)%objective)
305 allocate(this%objective_list(1))
308 call move_alloc(
objective, this%objective_list(n + 1)%objective)
309 this%n_objectives = n + 1
315 class(
constraint_t),
allocatable,
intent(inout) :: constraint
320 if (
allocated(this%constraint_list))
then
321 n =
size(this%constraint_list)
322 call move_alloc(this%constraint_list, temp_list)
323 allocate(this%constraint_list(n + 1))
324 if (
allocated(temp_list))
then
326 call move_alloc(temp_list(i)%constraint, &
327 this%constraint_list(i)%constraint)
331 allocate(this%constraint_list(1))
334 call move_alloc(
constraint, this%constraint_list(n + 1)%constraint)
335 this%n_constraints = n + 1
344 class(
design_t),
intent(inout) :: design
346 call this%update_objectives(
design)
347 call this%update_constraints(
design)
354 class(
design_t),
intent(inout) :: design
356 type(vector_t) :: objective_sensitivity
358 call this%update_objective_sensitivities(
design)
359 call this%update_constraint_sensitivities(
design)
361 call this%get_objective_sensitivities(objective_sensitivity)
363 call design%map_backward(objective_sensitivity)
377 class(
design_t),
intent(in) :: design
380 do i = 1, this%n_objectives
381 call this%objective_list(i)%objective%update_value(
design)
392 class(
design_t),
intent(in) :: design
395 do i = 1, this%n_constraints
396 call this%constraint_list(i)%constraint%update_value(
design)
407 class(
design_t),
intent(in) :: design
410 do i = 1, this%n_objectives
411 call this%objective_list(i)%objective%update_sensitivity(
design)
422 class(
design_t),
intent(in) :: design
425 do i = 1, this%n_constraints
426 call this%constraint_list(i)%constraint%update_sensitivity(
design)
440 real(kind=rp),
intent(out) :: objective_value
443 objective_value = 0.0_rp
444 do i = 1, this%n_objectives
445 objective_value = objective_value + &
446 this%objective_list(i)%objective%weight * &
447 this%objective_list(i)%objective%value
459 type(vector_t),
intent(out) :: all_objective_values
462 call all_objective_values%init(this%n_objectives)
464 do i = 1, this%n_objectives
465 all_objective_values%x(i) = this%objective_list(i)%objective%value
468 if (neko_bcknd_device .eq. 1)
then
469 call device_memcpy(all_objective_values%x, all_objective_values%x_d, &
470 this%n_objectives, host_to_device, sync = .true.)
482 type(vector_t),
intent(out) :: constraint_value
485 call constraint_value%init(this%n_constraints)
487 do i = 1, this%n_constraints
488 constraint_value%x(i) = this%constraint_list(i)%constraint%value
491 if (neko_bcknd_device .eq. 1)
then
492 call device_memcpy(constraint_value%x, constraint_value%x_d, &
493 this%n_constraints, host_to_device, sync = .true.)
505 type(vector_t),
intent(out) :: sensitivity
510 do i = 1, this%n_objectives
523 type(matrix_t),
intent(out) :: sensitivity
526 call sensitivity%init(this%n_constraints, this%n_design)
528 do i = 1, this%n_constraints
529 if (neko_bcknd_device .eq. 1)
then
530 call device_memcpy( &
531 this%constraint_list(i)%constraint%sensitivity%x, &
532 this%constraint_list(i)%constraint%sensitivity%x_d, &
533 this%n_design, device_to_host, sync = .true.)
535 do j = 1, this%n_design
537 this%constraint_list(i)%constraint%sensitivity%x(j)
541 if (neko_bcknd_device .eq. 1)
then
543 this%n_design * this%n_constraints, host_to_device, sync = .true.)
556 n = this%n_objectives
564 n = this%n_constraints
570 character(len=1024) :: buff
571 character(len=50) :: mini_buff
586 buff =
"Total objective function"
587 do i = 1, this%get_n_objectives()
589 write(mini_buff,
'(", ", A)') this%objective_list(i)%objective%name
590 buff = trim(buff)//trim(mini_buff)
593 do i = 1, this%get_n_constraints()
595 write(mini_buff,
'(", ", A)') &
596 this%constraint_list(i)%constraint%name
597 buff = trim(buff)//trim(mini_buff)
Implements the constraint_t type.
Implements the objective_t type.
Module for handling the optimization problem.
pure integer function problem_get_num_objectives(this)
Return the number of objectives.
pure integer function problem_get_num_constraints(this)
Return the number of constraints.
subroutine problem_update_constraints(this, design)
Update the constraints.
subroutine problem_get_all_objective_values(this, all_objective_values)
Construct and get the objective.
subroutine problem_get_objective_value(this, objective_value)
Construct and get the objective.
subroutine problem_write(this, idx)
Sample the fields/design.
subroutine problem_get_constraint_sensitivities(this, sensitivity)
Construct and get the sensitivity of the constraints.
subroutine problem_compute_sensitivity(this, design)
The computation of the objective function and constraints.
subroutine problem_add_constraint(this, constraint)
Add an objective to the list.
subroutine problem_update_objective_sensitivities(this, design)
Update the sensitivity of the objectives.
character(len=1024) function problem_get_log_header(this)
Return the header for the problem.
subroutine problem_free(this)
Destructor for the base class.
subroutine problem_update_objectives(this, design)
Update the objectives.
subroutine problem_init(this, parameters, design, simulation)
The constructor for the base problem.
subroutine problem_get_constraint_values(this, constraint_value)
Construct and get the constraints.
subroutine problem_get_objective_sensitivities(this, sensitivity)
Construct and get the sensitivity of the objective.
subroutine problem_read_constraints(this, parameters, simulation, design)
Read the constraint from a parameters file.
subroutine problem_read_objectives(this, parameters, simulation, design)
Read the objective from a parameters file.
subroutine problem_update_constraint_sensitivities(this, design)
Update the sensitivity of the constraints.
subroutine problem_add_objective(this, objective)
Add an objective to the list.
subroutine problem_compute(this, design)
The computation of the objective function and constraints.
Sensitivity module. This module contains the sensitivity computation of the topology optimization.
Implements the steady_problem_t type.
The abstract constraint type.
Wrapper for constraints for use in lists.
The abstract objective type.
Wrapper for objectives for use in lists.
The abstract problem type.