Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
simulation.f90
1! Copyright (c) 2023, 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!
34! Here, we simply march forward to steady state solutions
36 use case, only: case_t
37 use neko, only: neko_init, neko_finalize, neko_solve
38 use adjoint_case, only: adjoint_case_t, adjoint_init, adjoint_free
39 use fluid_scheme_incompressible, only: fluid_scheme_incompressible_t
42 use scalar_pnpn, only: scalar_pnpn_t
44 use fluid_pnpn, only: fluid_pnpn_t
45 use simulation_adjoint, only: solve_adjoint
46 use fld_file_output, only: fld_file_output_t
48 use simcomp_executor, only: neko_simcomps
49 use neko_ext, only: reset
50 use field_math, only: field_rzero
51 use json_file_module, only: json_file
52 use json_utils, only: json_extract_item
53 use num_types, only: rp, sp
54 use user_intf, only: user_t, simulation_component_user_settings
55 implicit none
56 private
57
60 type(case_t), public :: neko_case
62 type(adjoint_case_t), public :: adjoint_case
64 class(fluid_scheme_incompressible_t), public, pointer :: fluid => null()
66 type(scalar_pnpn_t), public, pointer :: scalar => null()
68 class(adjoint_fluid_scheme_t), public, pointer :: adjoint_fluid => null()
70 type(adjoint_scalar_pnpn_t), public, pointer :: adjoint_scalar => null()
73 type(fld_file_output_t), public :: output_forward
76 type(fld_file_output_t), public :: output_adjoint
77
78 contains
80 procedure, pass(this) :: init => simulation_init
82 procedure, pass(this) :: free => simulation_free
84 procedure, pass(this) :: run_forward => simulation_run_forward
86 procedure, pass(this) :: run_backward => simulation_run_backward
88 procedure, pass(this) :: reset => simulation_reset
90 procedure, pass(this) :: write => simulation_write
91 end type simulation_t
92
93 public :: simulation_t
94contains
95
96 subroutine user_simcomp(params)
97 type(json_file), intent(inout) :: params
98 type(steady_simcomp_t), allocatable :: steady_comp
99 type(json_file) :: simcomp_settings
100
101 ! Allocate a simulation component
102 allocate(steady_comp)
103 simcomp_settings = simulation_component_user_settings("steady", params)
104 call neko_simcomps%add_user_simcomp(steady_comp, simcomp_settings)
105
106 end subroutine user_simcomp
107
109 subroutine simulation_init(this, parameters)
110 class(simulation_t), intent(inout), target :: this
111 type(json_file), intent(inout) :: parameters
112
113 this%neko_case%usr%init_user_simcomp => user_simcomp
114
115 ! initialize the primal
116 call neko_init(this%neko_case)
117 ! initialize the adjoint
118 call adjoint_init(this%adjoint_case, this%neko_case)
119
120 select type (fluid => this%neko_case%fluid)
121 type is (fluid_pnpn_t)
122 this%fluid => fluid
123
124 end select
125
126 select type (adjoint_fluid => this%adjoint_case%fluid_adj)
127 type is (adjoint_fluid_pnpn_t)
128 this%adjoint_fluid => adjoint_fluid
129
130 end select
131
132 if (allocated(this%neko_case%scalar)) then
133 this%scalar => this%neko_case%scalar
134 end if
135
136 if (allocated(this%adjoint_case%scalar_adj)) then
137 this%adjoint_scalar => this%adjoint_case%scalar_adj
138 end if
139
140 ! init the sampler
141 !---------------------------------------------------------
142 ! Allocate the output type
143 if (allocated(this%neko_case%scalar)) then
144 call this%output_forward%init(sp, 'forward_fields', 5)
145 call this%output_forward%fields%assign(5, this%scalar%s)
146 else
147 call this%output_forward%init(sp, 'forward_fields', 4)
148 end if
149
150 call this%output_forward%fields%assign(1, this%fluid%p)
151 call this%output_forward%fields%assign(2, this%fluid%u)
152 call this%output_forward%fields%assign(3, this%fluid%v)
153 call this%output_forward%fields%assign(4, this%fluid%w)
154
155 if (allocated(this%adjoint_case%scalar_adj)) then
156 call this%output_adjoint%init(sp, 'adjoint_fields', 5)
157 call this%output_adjoint%fields%assign(5, this%adjoint_scalar%s_adj)
158 else
159 call this%output_adjoint%init(sp, 'adjoint_fields', 4)
160 end if
161 call this%output_adjoint%fields%assign(1, this%adjoint_fluid%p_adj)
162 call this%output_adjoint%fields%assign(2, this%adjoint_fluid%u_adj)
163 call this%output_adjoint%fields%assign(3, this%adjoint_fluid%v_adj)
164 call this%output_adjoint%fields%assign(4, this%adjoint_fluid%w_adj)
165
166 end subroutine simulation_init
167
169 subroutine simulation_free(this)
170 class(simulation_t), intent(inout) :: this
171
172 call adjoint_free(this%adjoint_case)
173 call neko_finalize(this%neko_case)
174
175 end subroutine simulation_free
176
178 subroutine simulation_run_forward(this)
179 class(simulation_t), intent(inout) :: this
180
181 ! run the primal
182 call neko_solve(this%neko_case)
183
184 end subroutine simulation_run_forward
185
187 subroutine simulation_run_backward(this)
188 class(simulation_t), intent(inout) :: this
189
190 ! run the adjoint
191 call solve_adjoint(this%adjoint_case)
192
193 end subroutine simulation_run_backward
194
196 subroutine simulation_reset(this)
197 class(simulation_t), intent(inout) :: this
198
199 call reset(this%neko_case)
200
201 ! TODO
202 ! reset for the adjoint
203 ! call reset(this%adjoint_case)
204 call field_rzero(this%adjoint_case%fluid_adj%u_adj)
205 call field_rzero(this%adjoint_case%fluid_adj%v_adj)
206 call field_rzero(this%adjoint_case%fluid_adj%w_adj)
207 if (allocated(this%neko_case%scalar)) then
208 call field_rzero(this%adjoint_case%scalar_adj%s_adj)
209 end if
210
211 end subroutine simulation_reset
212
214 subroutine simulation_write(this, idx)
215 class(simulation_t), intent(inout) :: this
216 integer, intent(in) :: idx
217
218 call this%output_forward%sample(real(idx, kind=rp))
219 call this%output_adjoint%sample(real(idx, kind=rp))
220
221 end subroutine simulation_write
222
223end module simulation_m
Adjoint Pn/Pn formulation.
Contains the adjoint_scalar_pnpn_t type.
Contains extensions to the neko library required to run the topology optimization code.
Definition neko_ext.f90:9
subroutine, public reset(neko_case)
Reset the case data structure.
Definition neko_ext.f90:50
Adjoint simulation driver.
Implements the steady_problem_t type.
Implements the steady_simcomp_t type.
Adjoint case type. Todo: This should Ideally be a subclass of case_t, however, that is not yet suppor...
The steady_simcomp_t type is a simulation component that terminates a simulation when the normed diff...