Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
neko_ext.f90
Go to the documentation of this file.
1
6
9module neko_ext
10 use case, only: case_t
11 use json_utils, only: json_get, json_get_or_default, json_extract_object
12 use num_types, only: rp
13 use simcomp_executor, only: neko_simcomps
14 use flow_ic, only: set_flow_ic
15 use scalar_ic, only: set_scalar_ic
16 use field, only: field_t
17 use chkp_output, only: chkp_output_t
18 use output_controller, only: output_controller_t
19 ! for vector/field math
20 use math, only: copy
21 use device_math, only: device_copy
22 use neko_config, only : neko_bcknd_device
23 use vector, only: vector_t
24 use field, only: field_t
25 use utils, only: neko_error
26 use json_module, only : json_file
27 implicit none
28
29 ! ========================================================================= !
30 ! Module interface
31 ! ========================================================================= !
32 private
34
35contains
36
37 ! ========================================================================= !
38 ! Public routines
39 ! ========================================================================= !
40
49 subroutine reset(neko_case)
50 type(case_t), intent(inout) :: neko_case
51 real(kind=rp) :: t
52 integer :: i
53 character(len=:), allocatable :: string_val
54 logical :: has_scalar, freezeflow
55 type(field_t), pointer :: u, v, w, p, s
56 type(json_file) :: json_subdict
57
58 t = 0.0_rp
59
60 ! ------------------------------------------------------------------------ !
61 ! Setup shorthand notation
62 ! ------------------------------------------------------------------------ !
63
64 u => neko_case%fluid%u
65 v => neko_case%fluid%v
66 w => neko_case%fluid%w
67 p => neko_case%fluid%p
68 if (allocated(neko_case%scalar)) then
69 s => neko_case%scalar%s
70 else
71 nullify(s)
72 end if
73
74 ! ------------------------------------------------------------------------ !
75 ! Reset the timing parameters
76 ! ------------------------------------------------------------------------ !
77
78 ! Setup lagged time step parameters
79 neko_case%time%tlag(:) = t
80 neko_case%time%dtlag(:) = neko_case%time%dt
81 do i = 1, size(neko_case%time%tlag)
82 neko_case%time%tlag(i) = t - i*neko_case%time%dtlag(i)
83 end do
84
85 ! Reset the time step counter
86 call neko_case%output_controller%set_counter(neko_case%time)
87
88 ! Restart the fields
89 call neko_case%fluid%restart(neko_case%chkp)
90 if (allocated(neko_case%scalar)) then
91 call neko_case%scalar%restart(neko_case%chkp)
92 end if
93
94 ! Reset the external BDF coefficients
95 do i = 1, size(neko_case%time%dtlag)
96 call neko_case%fluid%ext_bdf%set_coeffs(neko_case%time%dtlag)
97 end do
98
99 ! Restart the simulation components
100 call neko_simcomps%restart(neko_case%time)
101
102 ! ------------------------------------------------------------------------ !
103 ! Reset the fluid field to the initial condition
104 ! ------------------------------------------------------------------------ !
105
106 call json_get(neko_case%params, &
107 'case.fluid.initial_condition.type', string_val)
108 call json_extract_object(neko_case%params, 'case.fluid.initial_condition', &
109 json_subdict)
110
111 if (trim(string_val) .ne. 'user') then
112 call set_flow_ic(u, v, w, p, &
113 neko_case%fluid%c_Xh, neko_case%fluid%gs_Xh, &
114 string_val, json_subdict)
115 else
116 call set_flow_ic(u, v, w, p, &
117 neko_case%fluid%c_Xh, neko_case%fluid%gs_Xh, &
118 neko_case%usr%fluid_user_ic, neko_case%params)
119 end if
120
121 ! ------------------------------------------------------------------------ !
122 ! Reset the scalar field to the initial condition
123 ! ------------------------------------------------------------------------ !
124
125 call json_get_or_default(neko_case%params, &
126 'case.scalar.enabled', has_scalar, .false.)
127
128 if (has_scalar) then
129 call json_get(neko_case%params, &
130 'case.scalar.initial_condition.type', string_val)
131 call json_extract_object(neko_case%params, &
132 'case.scalar.initial_condition', json_subdict)
133
134 if (trim(string_val) .ne. 'user') then
135 call set_scalar_ic(s, &
136 neko_case%scalar%c_Xh, neko_case%scalar%gs_Xh, &
137 string_val, &
138 json_subdict)
139 else
140 call set_scalar_ic(s, &
141 neko_case%scalar%c_Xh, neko_case%scalar%gs_Xh, &
142 neko_case%usr%scalar_user_ic, &
143 neko_case%params)
144 end if
145 end if
146
147 ! ------------------------------------------------------------------------ !
148 ! Reset the "freeze" parameter of the flow
149 ! ------------------------------------------------------------------------ !
150
151 call json_get_or_default(neko_case%params, &
152 'case.fluid.freeze_flow', freezeflow, .false.)
153
154 neko_case%fluid%freeze = freezeflow
155
156 end subroutine reset
157
166 subroutine setup_iteration(neko_case, iter)
167 type(case_t), intent(inout) :: neko_case
168 integer, intent(in) :: iter
169
170 character(len=:), allocatable :: dirname
171 character(len=80) :: file_name
172
173 if (iter .ne. 1) then
174 call reset(neko_case)
175 end if
176
177 call json_get_or_default(neko_case%params, &
178 'case.output_directory', dirname, './')
179
180 write (file_name, '(a,a,i5.5,a)') &
181 trim(adjustl(dirname)), '/topopt_', iter, '_.fld'
182
183 neko_case%f_out%output_t%file_%file_type%fname = trim(file_name)
184 neko_case%f_out%output_t%file_%file_type%counter = 0
185 neko_case%f_out%output_t%file_%file_type%start_counter = 0
186 call neko_case%output_controller%execute(neko_case%time, .true.)
187
188 end subroutine setup_iteration
189
197 subroutine vector_to_field(field, vector)
198 type(field_t), intent(inout) :: field
199 type(vector_t), intent(in) :: vector
200
201 ! first check they're the same size
202 if (field%size() .ne. vector%size()) then
203 call neko_error("vector and field are not the same size")
204 end if
205
206 if (neko_bcknd_device .eq. 1) then
207 call device_copy(field%x_d, vector%x_d, field%size())
208 else
209 call copy(field%x, vector%x, field%size())
210 end if
211
212 end subroutine vector_to_field
213
221 subroutine field_to_vector(vector, field)
222 type(vector_t), intent(inout) :: vector
223 type(field_t), intent(in) :: field
224
225 ! first check they're the same size
226 if (field%size() .ne. vector%size()) then
227 call neko_error("vector and field are not the same size")
228 end if
229
230 if (neko_bcknd_device .eq. 1) then
231 call device_copy(vector%x_d, field%x_d, field%size())
232 else
233 call copy(vector%x, field%x, field%size())
234 end if
235
236 end subroutine field_to_vector
237
238end module neko_ext
Contains extensions to the neko library required to run the topology optimization code.
Definition neko_ext.f90:9
subroutine, public field_to_vector(vector, field)
Field to vector.
Definition neko_ext.f90:222
subroutine, public reset(neko_case)
Reset the case data structure.
Definition neko_ext.f90:50
subroutine, public vector_to_field(field, vector)
Vector to field.
Definition neko_ext.f90:198
subroutine, public setup_iteration(neko_case, iter)
Setup the iteration.
Definition neko_ext.f90:167