Neko 1.99.2
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
time_step_controller.f90
Go to the documentation of this file.
1! Copyright (c) 2022, 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 num_types, only : rp
36 use logger, only : neko_log, log_size
37 use json_module, only : json_file
39 use time_state, only : time_state_t
40 implicit none
41 private
42
44 type, public :: time_step_controller_t
45 logical :: is_variable_dt
46 real(kind=rp) :: cfl_trg = 0.0_rp
47 real(kind=rp) :: cfl_avg = 0.0_rp
48 real(kind=rp) :: max_dt = 0.0_rp
49 real(kind=rp) :: min_dt = 0.0_rp
50 integer :: max_update_frequency = 0
51 integer :: min_update_frequency = 0
52 integer :: dt_last_change = 0
53 real(kind=rp) :: alpha = 0.0_rp
54 real(kind=rp) :: max_dt_increase_factor = 0.0_rp
55 real(kind=rp) :: min_dt_decrease_factor = 0.0_rp
56 real(kind=rp) :: dev_tol = 0.0_rp
57 contains
59 procedure, pass(this) :: init => time_step_controller_init
61 procedure, pass(this) :: set_dt => time_step_controller_set_dt
62
64
65contains
66
69 subroutine time_step_controller_init(this, params)
70 class(time_step_controller_t), intent(inout) :: this
71 type(json_file), intent(inout) :: params
72
73 this%dt_last_change = -1
74 call json_get_or_default(params, 'variable_timestep', &
75 this%is_variable_dt, .false.)
76 if (this%is_variable_dt) then
77 call json_get_or_lookup_or_default(params, 'target_cfl', &
78 this%cfl_trg, 0.4_rp)
79 call json_get_or_lookup_or_default(params, 'max_timestep', &
80 this%max_dt, huge(0.0_rp))
81 call json_get_or_lookup_or_default(params, 'min_timestep', &
82 this%min_dt, 0.0_rp)
83 call json_get_or_lookup_or_default(params, 'max_update_frequency',&
84 this%max_update_frequency, 0)
85 call json_get_or_lookup_or_default(params, 'min_update_frequency',&
86 this%min_update_frequency, huge(0))
87 call json_get_or_lookup_or_default(params, 'cfl_running_avg_coeff', &
88 this%alpha, 0.5_rp)
89 call json_get_or_lookup_or_default(params, 'max_dt_increase_factor', &
90 this%max_dt_increase_factor, 1.2_rp)
91 call json_get_or_lookup_or_default(params, 'min_dt_decrease_factor', &
92 this%min_dt_decrease_factor, 0.5_rp)
93 call json_get_or_lookup_or_default(params, 'cfl_deviation_tolerance', &
94 this%dev_tol, 0.2_rp)
95 end if
96
97 end subroutine time_step_controller_init
98
106 subroutine time_step_controller_set_dt(this, time, cfl)
107 class(time_step_controller_t), intent(inout) :: this
108 type(time_state_t), intent(inout) :: time
109 real(kind=rp), intent(in) :: cfl
110 real(kind=rp) :: dt_old, scaling_factor
111 character(len=LOG_SIZE) :: log_buf
112
113 ! Check if variable dt is requested
114 if (.not. this%is_variable_dt) return
115
116 ! Reset the average cfl if it is the first time step since the last change
117 if (this%dt_last_change .eq. 0) then
118 this%cfl_avg = cfl
119 end if
120
121 if (this%dt_last_change .eq. -1) then
122
123 ! set the first dt for desired cfl
124 time%dt = max(min(this%cfl_trg / cfl * time%dt, &
125 this%max_dt), this%min_dt)
126 this%dt_last_change = 0
127 this%cfl_avg = cfl
128
129 else
130 ! Calculate the average of cfl over the desired interval
131 this%cfl_avg = this%alpha * cfl + (1 - this%alpha) * this%cfl_avg
132
133 if (abs(this%cfl_avg - this%cfl_trg) .ge. this%dev_tol * this%cfl_trg &
134 .and. this%dt_last_change .ge. this%max_update_frequency &
135 .or. this%dt_last_change .ge. this%min_update_frequency) then
136
137 if (this%cfl_trg/cfl .ge. 1) then
138 ! increase of time step
139 scaling_factor = min(this%max_dt_increase_factor, this%cfl_trg/cfl)
140 else
141 ! reduction of time step
142 scaling_factor = max(this%min_dt_decrease_factor, this%cfl_trg/cfl)
143 end if
144
145 dt_old = time%dt
146 time%dt = scaling_factor * dt_old
147 time%dt = max(min(time%dt, this%max_dt), this%min_dt)
148
149 write(log_buf, '(A,E15.7,1x,A,E15.7)') &
150 'Average CFL:', this%cfl_avg, &
151 'Target CFL:', this%cfl_trg
152 call neko_log%message(log_buf)
153
154 write(log_buf, '(A,E15.7,1x,A,E15.7)') 'Old dt:', dt_old, &
155 'New dt:', time%dt
156 call neko_log%message(log_buf)
157
158 this%dt_last_change = 0
159
160 else
161 this%dt_last_change = this%dt_last_change + 1
162 end if
163 end if
164
165 end subroutine time_step_controller_set_dt
166
167
168
169
170end module time_step_controller
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Utilities for retrieving parameters from the case files.
Logging routines.
Definition log.f90:34
type(log_t), public neko_log
Global log stream.
Definition log.f90:77
integer, parameter, public log_size
Definition log.f90:46
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:12
Module with things related to the simulation time.
Implements type time_step_controller.
subroutine time_step_controller_init(this, params)
Constructor.
subroutine time_step_controller_set_dt(this, time, cfl)
Set new dt based on cfl if requested.
A struct that contains all info about the time, expand as needed.
#define max(a, b)
Definition tensor.cu:40