36 use neko_config,
only: neko_bcknd_device
37 use num_types,
only: rp
40 use field,
only: field_t
41 use field_list,
only: field_list_t
42 use json_utils,
only: json_get, json_extract_item, json_get_or_default
43 use json_module,
only: json_file
44 use coefs,
only: coef_t
45 use user_intf,
only: user_t
46 use field_math,
only: field_rzero, field_copy
48 use device_math,
only: device_col2
49 use scratch_registry,
only: neko_scratch_registry
50 use utils,
only: neko_warning
51 use vector,
only:vector_t
68 type(coef_t),
pointer :: coef
72 procedure, pass(this) :: init_base => mapping_handler_init_base
74 procedure, pass(this) :: free => mapping_handler_free
76 generic :: apply_forward => mapping_handler_apply_forward_field, &
77 mapping_handler_apply_forward_vector
78 procedure, pass(this) :: mapping_handler_apply_forward_field
79 procedure, pass(this) :: mapping_handler_apply_forward_vector
82 generic :: apply_backward => mapping_handler_apply_backward_field, &
83 mapping_handler_apply_backward_vector
84 procedure, pass(this) :: mapping_handler_apply_backward_field
85 procedure, pass(this) :: mapping_handler_apply_backward_vector
87 generic :: add => add_mapping, add_json_mappings
89 procedure, pass(this) :: add_mapping => &
90 mapping_handler_add_mapping
92 procedure, pass(this) :: add_json_mappings => &
93 mapping_handler_add_json_mappings
99 subroutine mapping_handler_init_base(this, coef)
101 type(coef_t),
target,
intent(in) :: coef
107 end subroutine mapping_handler_init_base
111 subroutine mapping_handler_free(this)
115 if (
allocated(this%mapping_cascade))
then
116 do i = 1,
size(this%mapping_cascade)
117 call this%mapping_cascade(i)%free()
119 deallocate(this%mapping_cascade)
122 end subroutine mapping_handler_free
128 subroutine mapping_handler_apply_forward_field(this, X_out, X_in)
130 type(field_t),
intent(in) :: X_in
131 type(field_t),
intent(inout) :: X_out
133 type(field_t),
pointer :: tmp_fld_in, tmp_fld_out
134 integer :: temp_indices(2)
136 call neko_scratch_registry%request_field(tmp_fld_in, temp_indices(1))
137 call neko_scratch_registry%request_field(tmp_fld_out, temp_indices(2))
141 call field_copy(tmp_fld_out, x_in)
144 if (
allocated(this%mapping_cascade))
then
145 do i = 1,
size(this%mapping_cascade)
147 call field_copy(tmp_fld_in, tmp_fld_out)
149 call this%mapping_cascade(i)%mapping%apply_forward(tmp_fld_out, &
157 call field_copy(x_out, tmp_fld_out)
160 call neko_scratch_registry%relinquish_field(temp_indices)
162 end subroutine mapping_handler_apply_forward_field
168 subroutine mapping_handler_apply_forward_vector(this, X_out, X_in)
170 type(vector_t),
intent(in) :: X_in
171 type(vector_t),
intent(inout) :: X_out
172 type(field_t),
pointer :: tmp_fld_in, tmp_fld_out
173 integer :: temp_indices(2)
175 call neko_scratch_registry%request_field(tmp_fld_in, temp_indices(1))
176 call neko_scratch_registry%request_field(tmp_fld_out, temp_indices(2))
179 call mapping_handler_apply_forward_field(this, tmp_fld_out, tmp_fld_in)
183 call neko_scratch_registry%relinquish_field(temp_indices)
185 end subroutine mapping_handler_apply_forward_vector
193 subroutine mapping_handler_apply_backward_field(this, sens_out, sens_in)
195 type(field_t),
intent(inout) :: sens_out
196 type(field_t),
intent(in) :: sens_in
198 type(field_t),
pointer :: tmp_fld_in, tmp_fld_out
199 integer :: temp_indices(2)
201 call neko_scratch_registry%request_field(tmp_fld_in, temp_indices(1))
202 call neko_scratch_registry%request_field(tmp_fld_out, temp_indices(2))
206 call field_copy(tmp_fld_out, sens_in)
209 if (
allocated(this%mapping_cascade))
then
210 do i =
size(this%mapping_cascade), 1, -1
212 call field_copy(tmp_fld_in, tmp_fld_out)
217 call this%mapping_cascade(i)%mapping%apply_backward(tmp_fld_out, &
225 if (neko_bcknd_device .eq. 1)
then
226 call device_col2(tmp_fld_out%x_d, this%coef%B_d, tmp_fld_out%size())
228 call col2(tmp_fld_out%x, this%coef%B, tmp_fld_out%size())
232 call field_copy(sens_out, tmp_fld_out)
235 call neko_scratch_registry%relinquish_field(temp_indices)
238 end subroutine mapping_handler_apply_backward_field
246 subroutine mapping_handler_apply_backward_vector(this, X_out, X_in)
248 type(vector_t),
intent(in) :: X_in
249 type(vector_t),
intent(inout) :: X_out
250 type(field_t),
pointer :: tmp_fld_in, tmp_fld_out
251 integer :: temp_indices(2)
253 call neko_scratch_registry%request_field(tmp_fld_in, temp_indices(1))
254 call neko_scratch_registry%request_field(tmp_fld_out, temp_indices(2))
257 call mapping_handler_apply_backward_field(this, tmp_fld_out, tmp_fld_in)
261 call neko_scratch_registry%relinquish_field(temp_indices)
263 end subroutine mapping_handler_apply_backward_vector
266 subroutine mapping_handler_add_json_mappings(this, json, name)
268 type(json_file),
intent(inout) :: json
269 character(len=*),
intent(in) :: name
274 type(json_file) :: mapping_subdict
275 integer :: n_mappings, i, i0
277 if (json%valid_path(name))
then
279 call json%info(name, n_children = n_mappings)
281 if (
allocated(this%mapping_cascade))
then
282 i0 =
size(this%mapping_cascade)
283 call move_alloc(this%mapping_cascade, temp)
284 allocate(this%mapping_cascade(i0 + n_mappings))
285 if (
allocated(temp))
then
287 call move_alloc(temp(i)%mapping, &
288 this%mapping_cascade(i)%mapping)
293 allocate(this%mapping_cascade(n_mappings))
298 call json_extract_item(json, name, i, mapping_subdict)
300 mapping_subdict, this%coef)
304 call neko_warning(
"No mappings selected")
307 end subroutine mapping_handler_add_json_mappings
312 subroutine mapping_handler_add_mapping(this, mapping)
317 integer :: n_mappings, i
319 if (
allocated(this%mapping_cascade))
then
320 n_mappings =
size(this%mapping_cascade)
325 call move_alloc(this%mapping_cascade, temp)
326 allocate(this%mapping_cascade(n_mappings + 1))
328 if (
allocated(temp))
then
330 call move_alloc(temp(i)%mapping, this%mapping_cascade(i)%mapping)
334 this%mapping_cascade(n_mappings + 1)%mapping =
mapping
336 end subroutine mapping_handler_add_mapping
mapping factory. Both constructs and initializes the object.
Implements the mapping_handler_t type.
Mappings to be applied to a scalar field.
Contains extensions to the neko library required to run the topology optimization code.
subroutine, public field_to_vector(vector, field)
Field to vector.
subroutine, public vector_to_field(field, vector)
Vector to field.
Base abstract class for mapping.
A helper type that is needed to have an array of polymorphic objects.
Abstract class for handling mapping_cascade.