Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
time_state.f90
Go to the documentation of this file.
1! Copyright (c) 2025, 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
33!> Module with things related to the simulation time
35 use num_types, only : rp, dp
37 use json_module, only : json_file
40 implicit none
41 private
42
43 !> A struct that contains all info about the time, expand as needed
44 type, public :: time_state_t
45 real(kind=dp), dimension(10) :: tlag = 0.0_dp !< Old times
46 real(kind=dp), dimension(10) :: dtlag = 0.0_dp !< Old dts
47 real(kind=dp) :: t = 0.0_dp !< Current time
48 real(kind=dp) :: dt = 0.0_dp !< Current dt
49 real(kind=dp) :: start_time = 0.0_dp !< Start time
50 real(kind=dp) :: end_time = 0.0_dp !< End time
51 integer :: tstep = 0 !< Current timestep
52
53 contains
54 generic :: init => init_from_components, init_from_json
55 procedure, pass(this) :: init_from_components => &
57 procedure, pass(this) :: init_from_json => time_state_init_from_json
58 procedure, pass(this) :: reset => time_state_reset
59 procedure, pass(this) :: status => time_state_status
60 procedure, pass(this) :: is_done => time_state_is_done
61 procedure, pass(this) :: tolerance => time_state_tolerance
62 end type time_state_t
63
64contains
65
66 !> Initialize time state from JSON
67 subroutine time_state_init_from_json(this, params)
68 class(time_state_t), intent(inout) :: this
69 type(json_file), intent(inout) :: params
70
71 real(kind=dp) :: time_step
72 real(kind=dp) :: start_time
73 real(kind=dp) :: end_time
74 logical :: is_variable
75
76 call json_get_or_lookup_or_default(params, 'start_time', start_time, 0.0_dp)
77 call json_get_or_lookup(params, 'end_time', end_time)
78 call json_get_or_default(params, 'variable_timestep', is_variable, .false.)
79 if (.not. is_variable) then
80 call json_get_or_lookup(params, 'timestep', time_step)
81 else
82 ! randomly set an initial dt to get cfl when dt is variable
83 time_step = 1.0_dp
84 end if
85
86 call this%init_from_components(start_time, end_time, time_step)
87
88 end subroutine time_state_init_from_json
89
90 !> Initialize time state
91 subroutine time_state_init_from_components(this, start_time, end_time, dt)
92 class(time_state_t), intent(inout) :: this
93 real(kind=dp), intent(in) :: start_time
94 real(kind=dp), intent(in) :: end_time
95 real(kind=dp), intent(in) :: dt
96
97 if (dt .gt. 0.0_dp .and. start_time .gt. end_time .or. &
98 dt .lt. 0.0_dp .and. start_time .lt. end_time) then
99 call neko_log%error('Time step size must match direction of time.')
100 end if
101
102 this%start_time = start_time
103 this%end_time = end_time
104 this%dt = dt
105
106 this%t = start_time
107 this%tstep = 0
108 this%tlag = start_time
109 this%dtlag = dt
110
112
113 !> Reset time state
114 subroutine time_state_reset(this)
115 class(time_state_t), intent(inout) :: this
116
117 this%t = this%start_time
118 this%tstep = 0
119 this%tlag = this%start_time
120 this%dtlag = this%dt
121
122 end subroutine time_state_reset
123
124 !> Write status banner
125 subroutine time_state_status(this)
126 class(time_state_t), intent(in) :: this
127 character(len=LOG_SIZE) :: log_buf
128 character(len=64) :: log_fmt
129 real(kind=dp) :: t_prog
130 integer :: time_digits, time_width, pad_width
131
132 t_prog = 100.0_dp * (this%t - this%start_time) / &
133 (this%end_time - this%start_time)
134
135 time_digits = precision(this%t)
136 time_width = time_digits + 8
137 pad_width = max(1, log_size - (34 + time_width) - 1)
138
139 write(log_fmt, '(A,I0,A,I0,A,I0,A,I0,A)') &
140 '(A7,1X,I10,1X,A4,ES', time_width, '.', time_digits, ',', &
141 pad_width, 'X,A2,F6.2,A3)'
142 write(log_buf, log_fmt) 'Step = ', this%tstep, 't = ', this%t, &
143 '[ ', t_prog, '% ]'
144
145 call neko_log%message(repeat('-', log_size - 1))
146 call neko_log%message(log_buf, neko_log_quiet)
147 call neko_log%message(repeat('-', log_size - 1))
148
149 end subroutine time_state_status
150
151 !> Check if the simulation is done
152 !! @note The comparison is made on the progress from the start time in the
153 !! direction of the simulation, so that it also holds for a simulation
154 !! marching backwards in time. The tolerance keeps a time a few ulps short
155 !! of the end time from adding one more time step, which would overshoot
156 !! the end time by almost `dt` and produce an output past it.
157 pure function time_state_is_done(this) result(is_done)
158 class(time_state_t), intent(in) :: this
159 logical :: is_done
160 real(kind=dp) :: span, tol
161
162 span = abs(this%end_time - this%start_time)
163 tol = this%tolerance()
164
165 is_done = sign(1.0_dp, this%end_time - this%start_time) * &
166 (this%t - this%start_time) .ge. span - tol &
167 .and. this%tstep .gt. 0
168
169 end function time_state_is_done
170
171 !> Tolerance for comparing the current time to a fixed time, such as the
172 !! end time of the simulation or a bound of a time window.
173 !! @details The current time is accumulated step by step and carries the
174 !! round-off of the additions, so a time step meant to land on a fixed time
175 !! can end up a few ulps on either side of it. Within this tolerance the two
176 !! are considered equal. It is relative to the span of the simulation is
177 !! always non-negative, also for a simulation marching backwards in time.
178 !! To prevent issues it is also limited to a fraction of the time step size.
179 pure function time_state_tolerance(this) result(tol)
180 class(time_state_t), intent(in) :: this
181 real(kind=dp) :: tol
182
183 tol = 1.0e-9_dp * abs(this%end_time - this%start_time)
184 tol = min(tol, 1.0e-2_dp * abs(this%dt))
185
186 end function time_state_tolerance
187
188end module time_state
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
integer, parameter, public neko_log_quiet
Always.
Definition log.f90:52
type(log_t), public neko_log
Global log stream.
Definition log.f90:92
integer, parameter, public log_size
Definition log.f90:46
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Module with things related to the simulation time.
subroutine time_state_status(this)
Write status banner.
pure real(kind=dp) function time_state_tolerance(this)
Tolerance for comparing the current time to a fixed time, such as the end time of the simulation or a...
subroutine time_state_init_from_json(this, params)
Initialize time state from JSON.
pure logical function time_state_is_done(this)
Check if the simulation is done.
subroutine time_state_init_from_components(this, start_time, end_time, dt)
Initialize time state.
subroutine time_state_reset(this)
Reset time state.
A struct that contains all info about the time, expand as needed.
#define max(a, b)
Definition tensor.cu:40