38 use json_module,
only: json_file
39 use json_utils,
only: json_get
40 use num_types,
only: rp, dp, xp
41 use point_zone,
only: point_zone_t
43 use time_state,
only: time_state_t
44 use vector,
only: vector_t
45 use utils,
only: neko_error
46 use scratch_registry,
only: neko_scratch_registry
47 use vector_math,
only: vector_copy, vector_add3s2, vector_rzero
64 character(len=25) :: name =
""
67 real(kind=rp) ::
value = 0.0_dp
69 type(vector_t) :: sensitivity
71 logical :: has_mask = .false.
73 class(point_zone_t),
pointer :: mask => null()
80 real(kind=dp) :: start_time = 0.0_dp
82 real(kind=dp) :: end_time = huge(0.0_dp)
85 real(kind=xp) :: value_weight = 0.0_xp
88 real(kind=xp) :: sensitivity_weight = 0.0_xp
96 generic :: init => init_json, init_json_sim
99 procedure, pass(this) :: init_json => functional_init_json
101 procedure, pass(this) :: init_json_sim => functional_init_json_sim
106 procedure(functional_update_value), pass(this),
deferred :: update_value
108 procedure(functional_update_sensitivity), pass(this),
deferred :: &
112 procedure, pass(this) :: get_value => functional_get_value
114 procedure, pass(this) :: get_sensitivity => functional_get_sensitivity
116 procedure, pass(this) :: get_log_size => functional_get_log_size
118 procedure, pass(this) :: get_log_headers => functional_get_log_headers
120 procedure, pass(this) :: get_log_values => functional_get_log_values
122 procedure, pass(this) :: reset_value => functional_reset_value
124 procedure, pass(this) :: reset_sensitivity => functional_reset_sensitivity
126 procedure, pass(this) :: accumulate_value => functional_accumulate_value
128 procedure, pass(this) :: accumulate_sensitivity => &
129 functional_accumulate_sensitivity
132 procedure,
private, pass(this) :: in_window => functional_in_window
144 class(base_functional_t),
intent(inout) :: this
148 subroutine functional_update_value(this, design)
150 class(base_functional_t),
intent(inout) :: this
151 class(design_t),
intent(in) :: design
152 end subroutine functional_update_value
155 subroutine functional_update_sensitivity(this, design)
157 class(base_functional_t),
intent(inout) :: this
158 class(design_t),
intent(in) :: design
159 end subroutine functional_update_sensitivity
166 subroutine functional_init_json(this, json, design)
167 class(base_functional_t),
intent(inout) :: this
168 type(json_file),
intent(inout) :: json
169 class(design_t),
intent(in) :: design
170 character(len=:),
allocatable :: type
172 call json_get(json,
'type', type)
173 call neko_error(
"Functional type: '" //
type // &
174 "' does not support initialization without simulation")
175 end subroutine functional_init_json
178 subroutine functional_init_json_sim(this, json, design, simulation)
179 class(base_functional_t),
intent(inout) :: this
180 type(json_file),
intent(inout) :: json
181 class(design_t),
intent(in) :: design
182 type(simulation_t),
target,
intent(inout) :: simulation
183 character(len=:),
allocatable :: type
185 call json_get(json,
'type', type)
186 call neko_error(
"Functional type: '" //
type // &
187 "' does not support initialization with simulation")
188 end subroutine functional_init_json_sim
191 function functional_get_value(this)
result(v)
192 class(base_functional_t),
intent(in) :: this
196 end function functional_get_value
199 subroutine functional_get_sensitivity(this, sensitivity)
200 class(base_functional_t),
intent(in) :: this
201 type(vector_t),
intent(inout) :: sensitivity
203 sensitivity = this%sensitivity
204 end subroutine functional_get_sensitivity
209 function functional_get_log_size(this)
result(n)
210 class(base_functional_t),
intent(in) :: this
214 end function functional_get_log_size
219 subroutine functional_get_log_headers(this, headers)
220 class(base_functional_t),
intent(in) :: this
221 character(len=*),
intent(out) :: headers(:)
223 if (
size(headers) .eq. 0)
return
224 headers(1) = trim(this%name)
225 end subroutine functional_get_log_headers
230 subroutine functional_get_log_values(this, values)
231 class(base_functional_t),
intent(in) :: this
232 real(kind=rp),
intent(out) :: values(:)
234 if (
size(values) .eq. 0)
return
235 values(1) = this%value
236 end subroutine functional_get_log_values
239 subroutine functional_reset_value(this)
240 class(base_functional_t),
intent(inout) :: this
243 this%value_weight = 0.0_dp
244 end subroutine functional_reset_value
247 subroutine functional_reset_sensitivity(this)
248 class(base_functional_t),
intent(inout) :: this
250 call vector_rzero(this%sensitivity)
251 this%sensitivity_weight = 0.0_dp
252 end subroutine functional_reset_sensitivity
267 subroutine functional_accumulate_value(this, design, time)
268 class(base_functional_t),
intent(inout) :: this
269 class(design_t),
intent(in) :: design
270 type(time_state_t),
intent(in) :: time
271 real(kind=xp) :: value_new, value_old, weight_new, weight_old, dt
273 if (.not. this%in_window(time))
return
276 value_old = real(this%value, kind=xp)
277 call this%update_value(
design)
278 value_new = real(this%value, kind=xp)
281 dt = real(time%dt, kind=xp)
282 weight_new = dt / (this%value_weight + dt)
283 weight_old = this%value_weight / (this%value_weight + dt)
287 this%value = real(value_old * weight_old + value_new * weight_new, kind=rp)
288 this%value_weight = this%value_weight + dt
289 end subroutine functional_accumulate_value
298 subroutine functional_accumulate_sensitivity(this, design, time)
299 class(base_functional_t),
intent(inout) :: this
300 class(design_t),
intent(in) :: design
301 type(time_state_t),
intent(in) :: time
302 real(kind=xp) :: weight_new, weight_old, dt
303 type(vector_t),
pointer :: sensitivity_new, sensitivity_old
306 if (.not. this%in_window(time))
return
308 n = this%sensitivity%size()
309 call neko_scratch_registry%request(sensitivity_new, idx(1), n, .false.)
310 call neko_scratch_registry%request(sensitivity_old, idx(2), n, .false.)
313 call vector_copy(sensitivity_old, this%sensitivity)
314 call this%update_sensitivity(
design)
315 call vector_copy(sensitivity_new, this%sensitivity)
318 dt = real(time%dt, kind=xp)
319 weight_new = dt / (this%sensitivity_weight + dt)
320 weight_old = this%sensitivity_weight / (this%sensitivity_weight + dt)
324 call vector_add3s2(this%sensitivity, &
325 sensitivity_new, sensitivity_old, weight_new, weight_old)
326 this%sensitivity_weight = this%sensitivity_weight + dt
328 nullify(sensitivity_new)
329 nullify(sensitivity_old)
330 call neko_scratch_registry%relinquish(idx)
331 end subroutine functional_accumulate_sensitivity
345 pure function functional_in_window(this, time)
result(inside)
346 class(base_functional_t),
intent(in) :: this
347 type(time_state_t),
intent(in) :: time
351 tol = 1.0e-6_rp * time%dt
352 inside = time%t .ge. this%start_time - tol .and. &
353 time%t .le. this%end_time + tol
354 end function functional_in_window
Defines the abstract the base_functional_t type.
Implements the steady_problem_t type.
The base functional type.