Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
base_functional.f90
1! Copyright (c) 2024, The Neko Authors
2! All rights reserved.
3!
4! Redistribution and use in source and binary forms, with or without
5! modification, are permitted provided that the following conditions
6! are met:
7!
8! * Redistributions of source code must retain the above copyright
9! notice, this list of conditions and the following disclaimer.
10!
11! * Redistributions in binary form must reproduce the above
12! copyright notice, this list of conditions and the following
13! disclaimer in the documentation and/or other materials provided
14! with the distribution.
15!
16! * Neither the name of the authors nor the names of its
17! contributors may be used to endorse or promote products derived
18! from this software without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31! POSSIBILITY OF SUCH DAMAGE.
32
35 use design, only: design_t
36 use json_module, only: json_file
37 use json_utils, only: json_get
38 use num_types, only: rp
39 use point_zone, only: point_zone_t
40 use simulation_m, only: simulation_t
41 use vector, only: vector_t
42 use utils, only: neko_error
43 use vector_math, only: vector_add2s1
44 implicit none
45 private
46
57 type, abstract, public :: base_functional_t
58
60 real(kind=rp) :: value
62 type(vector_t) :: sensitivity
64 character(len=25) :: name
66 logical :: has_mask
68 class(point_zone_t), pointer :: mask => null()
69
70 contains
71
72 ! ----------------------------------------------------------------------- !
73 ! Derived class interfaces
74
75 generic :: init => init_json, init_json_sim
76
78 procedure, pass(this) :: init_json => functional_init_json
80 procedure, pass(this) :: init_json_sim => functional_init_json_sim
82 procedure(functional_free), pass(this), deferred :: free
83
85 procedure(functional_update_value), pass(this), deferred :: update_value
87 procedure(functional_update_sensitivity), pass(this), deferred :: &
88 update_sensitivity
89
91 procedure, pass(this) :: get_value => functional_get_value
93 procedure, pass(this) :: get_sensitivity => functional_get_sensitivity
95 procedure, pass(this) :: reset_value => functional_reset_value
97 procedure, pass(this) :: reset_sensitivity => functional_reset_sensitivity
99 procedure, pass(this) :: accumulate_value => functional_accumulate_value
101 procedure, pass(this) :: accumulate_sensitivity => &
102 functional_accumulate_sensitivity
103
104 end type base_functional_t
105
106 ! -------------------------------------------------------------------------- !
107 ! Interface specifications for the derived types, these are the constructors
108 ! for the different types of objective functions.
109
110 abstract interface
111
113 subroutine functional_free(this)
114 import base_functional_t
115 class(base_functional_t), intent(inout) :: this
116 end subroutine functional_free
117
119 subroutine functional_update_value(this, design)
121 class(base_functional_t), intent(inout) :: this
122 class(design_t), intent(in) :: design
123 end subroutine functional_update_value
124
126 subroutine functional_update_sensitivity(this, design)
128 class(base_functional_t), intent(inout) :: this
129 class(design_t), intent(in) :: design
130 end subroutine functional_update_sensitivity
131
132 end interface
133
134contains
135
137 subroutine functional_init_json(this, json, design)
138 class(base_functional_t), intent(inout) :: this
139 type(json_file), intent(inout) :: json
140 class(design_t), intent(in) :: design
141 character(len=:), allocatable :: type
142
143 call json_get(json, 'type', type)
144 call neko_error("Functional type: '" // type // &
145 "' does not support initialization without simulation")
146 end subroutine functional_init_json
147
149 subroutine functional_init_json_sim(this, json, design, simulation)
150 class(base_functional_t), intent(inout) :: this
151 type(json_file), intent(inout) :: json
152 class(design_t), intent(in) :: design
153 type(simulation_t), target, intent(inout) :: simulation
154 character(len=:), allocatable :: type
155
156 call json_get(json, 'type', type)
157 call neko_error("Functional type: '" // type // &
158 "' does not support initialization with simulation")
159 end subroutine functional_init_json_sim
160
161
163 function functional_get_value(this) result(v)
164 class(base_functional_t), intent(in) :: this
165 real(kind=rp) :: v
166
167 v = this%value
168 end function functional_get_value
169
171 subroutine functional_get_sensitivity(this, sensitivity)
172 class(base_functional_t), intent(in) :: this
173 type(vector_t), intent(inout) :: sensitivity
174
175 sensitivity = this%sensitivity
176 end subroutine functional_get_sensitivity
177
179 subroutine functional_reset_value(this)
180 class(base_functional_t), intent(inout) :: this
181
182 this%value = 0.0_rp
183 end subroutine functional_reset_value
184
186 subroutine functional_reset_sensitivity(this)
187 class(base_functional_t), intent(inout) :: this
188
189 this%sensitivity = 0.0_rp
190 end subroutine functional_reset_sensitivity
191
193 subroutine functional_accumulate_value(this, design, dt)
194 class(base_functional_t), intent(inout) :: this
195 class(design_t), intent(in) :: design
196 real(kind=rp), intent(in) :: dt
197 real(kind=rp) :: temp1, temp2
198
199 temp1 = this%value
200 call this%update_value(design)
201 temp2 = this%value
202 ! could potentially use higher order trapezoidal/Simpson etc, but this
203 ! should suffice
204 this%value = temp1 + temp2 * dt
205 end subroutine functional_accumulate_value
206
208 subroutine functional_accumulate_sensitivity(this, design, dt)
209 class(base_functional_t), intent(inout) :: this
210 class(design_t), intent(in) :: design
211 real(kind=rp), intent(in) :: dt
212 type(vector_t) :: temp
213
214 temp = this%sensitivity
215 call this%update_sensitivity(design)
216 ! could potentially use higher order trapezoidal/Simpson etc, but this
217 ! should suffice
218 call vector_add2s1(this%sensitivity, temp, dt)
219 call temp%free()
220 end subroutine functional_accumulate_sensitivity
221end module base_functional
Defines the abstract the base_functional_t type.
Implements the design_t.
Definition design.f90:34
Implements the steady_problem_t type.
An abstract design type.
Definition design.f90:52