Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
steady_simcomp.f90
Go to the documentation of this file.
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 simulation_component, only: simulation_component_t
36 use num_types, only: rp, dp
37 use field, only: field_t
38 use json_module, only: json_file
39 use json_utils, only: json_get_or_default
40 use case, only: case_t
41 use field_math, only: field_sub2, field_glsc2, field_copy
42 implicit none
43 private
44
49 type, public, extends(simulation_component_t) :: steady_simcomp_t
50 private
51
52 ! Old fields
53 type(field_t) :: u_old, v_old, w_old, p_old, s_old
54 real(kind=dp) :: tol
55
56 logical :: have_scalar = .false.
57
58 contains
59 ! Constructor from json, wrapping the actual constructor.
60 procedure, public, pass(this) :: init => steady_simcomp_init_from_json
61 ! Actual constructor.
62 procedure, public, pass(this) :: init_from_attributes => &
64 ! Destructor.
65 procedure, public, pass(this) :: free => steady_simcomp_free
66 ! Compute the steady_simcomp field.
67 procedure, public, pass(this) :: compute_ => steady_simcomp_compute
68 end type steady_simcomp_t
69
70contains
71
72 ! Constructor from json.
73 subroutine steady_simcomp_init_from_json(this, json, case)
74 class(steady_simcomp_t), intent(inout) :: this
75 type(json_file), intent(inout) :: json
76 class(case_t), intent(inout), target :: case
77 real(kind=dp) :: tol
78
79 call this%init_base(json, case)
80
81 ! Read the tolerance
82 call json_get_or_default(json, "tol", tol, 1.0e-6_dp)
83
84 call this%init_from_attributes(tol)
85
87
88 ! Actual constructor.
90 class(steady_simcomp_t), intent(inout) :: this
91 real(kind=dp), intent(in) :: tol
92
93 this%tol = tol
94
95 ! Point local fields to the scratch fields
96 call this%u_old%init(this%case%fluid%u%dof)
97 call this%v_old%init(this%case%fluid%v%dof)
98 call this%w_old%init(this%case%fluid%w%dof)
99 call this%p_old%init(this%case%fluid%p%dof)
100
101 ! Check if the scalar field is allocated
102 if (allocated(this%case%scalar)) then
103 this%have_scalar = .true.
104 call this%s_old%init(this%case%scalar%s%dof)
105 end if
106
108
109 ! Destructor.
110 subroutine steady_simcomp_free(this)
111 class(steady_simcomp_t), intent(inout) :: this
112
113 call this%u_old%free()
114 call this%v_old%free()
115 call this%w_old%free()
116 call this%p_old%free()
117 if (this%have_scalar) then
118 call this%s_old%free()
119 end if
120
121 call this%free_base()
122
123 end subroutine steady_simcomp_free
124
125 ! Compute the steady_simcomp field.
126 subroutine steady_simcomp_compute(this, t, tstep)
127 class(steady_simcomp_t), intent(inout) :: this
128 real(kind=rp), intent(in) :: t
129 integer, intent(in) :: tstep
130
131 real(kind=rp), dimension(5) :: normed_diff
132 type(field_t), pointer :: u, v, w, p, s
133
134 ! A frozen field is not interesting to compute differences for.
135 if (this%case%fluid%freeze) return
136
137 ! ------------------------------------------------------------------------ !
138 ! Computation of the maximal normed difference.
139 !
140 ! Our goal is to freeze the simulation when the normed difference between
141 ! the old and new fields is below a certain tolerance.
142
143 u => this%case%fluid%u
144 v => this%case%fluid%v
145 w => this%case%fluid%w
146 p => this%case%fluid%p
147
148 if (this%have_scalar) then
149 s => this%case%scalar%s
150 else
151 s => null()
152 end if
153
154 ! Compute the difference between the old and new fields
155 call field_sub2(this%u_old, u)
156 call field_sub2(this%v_old, v)
157 call field_sub2(this%w_old, w)
158 call field_sub2(this%p_old, p)
159 if (this%have_scalar) then
160 call field_sub2(this%s_old, s)
161 end if
162
163 ! Here we compute the squared difference between the old and new fields
164 ! and store the result in the `normed_diff` array.
165 normed_diff(1) = field_glsc2(this%u_old, this%u_old)
166 normed_diff(2) = field_glsc2(this%v_old, this%v_old)
167 normed_diff(3) = field_glsc2(this%w_old, this%w_old)
168 normed_diff(4) = field_glsc2(this%p_old, this%p_old)
169 if (this%have_scalar) then
170 normed_diff(5) = field_glsc2(this%s_old, this%s_old)
171 else
172 normed_diff(5) = 0.0_rp
173 end if
174
175 ! If the normed difference is below the tolerance, we consider the
176 ! simulation to have converged. Otherwise, we copy the new fields to the
177 ! old fields and continue the simulation.
178 if (maxval(normed_diff) .gt. this%tol) then
179 call field_copy(this%u_old, u)
180 call field_copy(this%v_old, v)
181 call field_copy(this%w_old, w)
182 call field_copy(this%p_old, p)
183 if (this%have_scalar) then
184 call field_copy(this%s_old, s)
185 end if
186
187 else
188 this%case%fluid%freeze = .true.
189 end if
190
191
192 end subroutine steady_simcomp_compute
193
194end module steady_simcomp
195
Implements the steady_simcomp_t type.
subroutine steady_simcomp_free(this)
subroutine steady_simcomp_init_from_attributes(this, tol)
subroutine steady_simcomp_init_from_json(this, json, case)
subroutine steady_simcomp_compute(this, t, tstep)
The steady_simcomp_t type is a simulation component that terminates a simulation when the normed diff...