Skip to content

Commit 6122f55

Browse files
authored
Merge pull request #172 from BerkeleyLab/fix-examples
fix(examples): time step stability calculation
2 parents a080500 + ddf4d29 commit 6122f55

3 files changed

Lines changed: 15 additions & 22 deletions

File tree

example/heat-equation.f90

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module subdomain_2D_m
1919
real, allocatable :: s_(:,:)
2020
contains
2121
procedure define
22-
procedure dx
23-
procedure dy
22+
procedure dt_stable
2423
procedure values
2524
procedure exchange_halo
2625
generic :: operator(.laplacian.) => laplacian
@@ -44,16 +43,11 @@ pure module function values(self) result(my_values)
4443
real, allocatable :: my_values(:,:)
4544
end function
4645

47-
pure module function dx(self) result(my_dx)
46+
pure module function dt_stable(self, alpha) result(my_dt)
4847
implicit none
4948
class(subdomain_2D_t), intent(in) :: self
50-
real my_dx
51-
end function
52-
53-
pure module function dy(self) result(my_dy)
54-
implicit none
55-
class(subdomain_2D_t), intent(in) :: self
56-
real my_dy
49+
real, intent(in) :: alpha
50+
real my_dt
5751
end function
5852

5953
pure module function laplacian(rhs) result(laplacian_rhs)
@@ -118,12 +112,11 @@ module subroutine exchange_halo(self)
118112
call self%exchange_halo
119113
end procedure
120114

121-
module procedure dx
122-
my_dx = dx_
123-
end procedure
124-
125-
module procedure dy
126-
my_dy = dy_
115+
module procedure dt_stable
116+
!! Set the time step at 90% of the stability limit obtained provided by
117+
!! Kassinos, S., & Alexiadis, A. (2024). Beyond Language: Applying MLX Transformers to Engineering Physics
118+
!! arXiv preprint arXiv:2410.04167.
119+
my_dt = 0.9 * (1./(1./dx_**2 + 1./dy_**2)) * (1./(2.*alpha))
127120
end procedure
128121

129122
module procedure laplacian
@@ -192,7 +185,7 @@ program main
192185
call T%define(side=1., boundary_val=T_boundary, internal_val=T_initial, n=nx) ! 2D step function
193186
sync all
194187

195-
associate(dt => T%dx()*T%dy()/(4*alpha))
188+
associate(dt => T%dt_stable(alpha))
196189
call cpu_time(t_start)
197190
do step = 1, steps
198191
T = T + dt * alpha * .laplacian. T

example/time-paradigm.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function functional_programming_time() result(system_time)
5858

5959
call system_clock(t_start_functional)
6060

61-
associate(dt => T%dx()*T%dy()/(4*alpha))
61+
associate(dt => T%dt_stable(alpha))
6262
functional_programming: &
6363
do step = 1, steps
6464
T = T + dt * alpha * .laplacian. T
@@ -80,7 +80,7 @@ function procedural_programming_time() result(system_time)
8080
real system_time
8181
type(subdomain_t) T
8282

83-
associate(dt => T%dx()*T%dy()/(4*alpha))
83+
associate(dt => T%dt_stable(alpha))
8484
call T%define(side=1., boundary_val=0., internal_val=1., n=resolution)
8585
call system_clock(t_start_procedural)
8686
procedural_programming: &

test/subdomain_test_m.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function correct_steady_state() result(test_passes)
160160

161161
call T%define(side=1., boundary_val=T_boundary, internal_val=T_initial, n=11) ! const. internally with a step down at boundaries
162162

163-
associate(dt => t%dt_stable(alpha))
163+
associate(dt => T%dt_stable(alpha))
164164
do step = 1, steps
165165
T = T + dt * alpha * .laplacian. T
166166
end do
@@ -193,7 +193,7 @@ function T_functional()
193193

194194
call T%define(side, boundary_val, internal_val, n)
195195

196-
associate(dt => t%dt_stable(alpha))
196+
associate(dt => T%dt_stable(alpha))
197197
do step = 1, steps
198198
T = T + dt * alpha * .laplacian. T
199199
end do
@@ -209,7 +209,7 @@ function T_procedural()
209209

210210
call T%define(side, boundary_val, internal_val, n)
211211

212-
associate(dt => t%dt_stable(alpha))
212+
associate(dt => T%dt_stable(alpha))
213213
do step = 1, steps
214214
call T%step(alpha*dt)
215215
end do

0 commit comments

Comments
 (0)