Skip to content

Commit 8ed9e29

Browse files
Restructure time loop
This patch conforms the code to the standard control flow order of requires_writing_checkpoint, requires_reading_checkpoint and is_time_window_complete.
1 parent c0a89ee commit 8ed9e29

2 files changed

Lines changed: 26 additions & 45 deletions

File tree

turek-hron-fsi3/fluid-nutils/fluid.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dataclasses import dataclass
1111
from typing import Optional
1212
from pathlib import Path
13+
from itertools import count
1314
import treelog as log
1415
import numpy
1516

@@ -90,23 +91,9 @@ def set_timestep(self, timestep):
9091
self.timeseries = defaultdict(
9192
deque(maxlen=round(self.window / timestep)).copy)
9293

93-
@property
94-
def times(self):
95-
'Return all configured time steps for the simulation.'
96-
97-
t = Time('0s')
98-
while True:
99-
t += self.timestep
100-
yield t
101-
10294
def ramp_up(self, t):
10395
return .5 - .5 * numpy.cos(numpy.pi * min(t / self.init, 1))
10496

105-
def rate(self, v1, subs):
106-
dt = function.field('dt') * precice_time
107-
v0 = function.replace_arguments(v1, subs)
108-
return (v1 - v0) / dt
109-
11097
def add_and_plot(self, name, t, v, ax):
11198
'Add data point and plot time series for past window.'
11299

@@ -247,6 +234,7 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
247234

248235
# initial values
249236
args = {a: numpy.zeros(function.arguments_for(res)[a].shape) for a in 'ud'}
237+
t = Time('0s')
250238

251239
bezier = topo['fluid'].sample('bezier', 3)
252240
bezier = bezier.subset(bezier.eval(geom[0]) < 2.2 * domain.channel_height)
@@ -258,16 +246,13 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
258246
bbezier = topo['fluid'].boundary['cylinder,structure'].sample('bezier', 3)
259247
x_bbz = bbezier.bind(ns.x)
260248

261-
assert participant.requires_writing_checkpoint()
262-
checkpoint = args
263-
264-
with log.iter.plain('timestep', dynamic.times) as times:
265-
t = next(times)
266-
249+
with log.iter.plain('time step', count()) as iter_context:
267250
while participant.is_coupling_ongoing():
268251

269-
if participant.requires_reading_checkpoint():
270-
args = checkpoint
252+
if participant.requires_writing_checkpoint():
253+
checkpoint = t, args
254+
255+
t += dynamic.timestep
271256

272257
cons['d'][r_where] = participant.read_data(
273258
r_name, 'Displacement', r_ids, timestep)
@@ -286,8 +271,11 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
286271
w_name, 'Stress', w_ids, w_sample.eval(ns.t, args) / precice_stress)
287272
participant.advance(timestep)
288273

289-
if participant.requires_writing_checkpoint():
290-
checkpoint = args
274+
if participant.requires_reading_checkpoint():
275+
t, args = checkpoint
276+
277+
if participant.is_time_window_complete():
278+
next(iter_context)
291279

292280
x, xb, u, p = function.eval([x_bz, x_bbz, u_bz, p_bz], args)
293281
with export.mplfigure('solution.jpg', dpi=150) as fig:
@@ -314,7 +302,7 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
314302
dynamic.add_and_plot(
315303
'drag [N/m]', t / 's', D / 'N/m', ax=fig.add_subplot(212, xlabel='time [s]'))
316304

317-
t = next(times)
305+
participant.finalize()
318306

319307

320308
if __name__ == '__main__':

turek-hron-fsi3/solid-nutils/solid.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dataclasses import dataclass
1111
from typing import Optional
1212
from pathlib import Path
13+
from itertools import count
1314
import treelog as log
1415
import numpy
1516

@@ -81,15 +82,6 @@ def set_timestep(self, timestep):
8182
self.timeseries = defaultdict(
8283
deque(maxlen=round(self.window / timestep)).copy)
8384

84-
@property
85-
def times(self):
86-
'Return all configured time steps for the simulation.'
87-
88-
t = Time('0s')
89-
while True:
90-
t += self.timestep
91-
yield t
92-
9385
def add_and_plot(self, name, t, v, ax):
9486
'Add data point and plot time series for past window.'
9587

@@ -179,20 +171,18 @@ def main(domain: Domain = Domain(), solid: Solid = Solid(), dynamic: Dynamic = D
179171

180172
# initial values
181173
args = {'d': numpy.zeros(function.arguments_for(res)['d'].shape)}
174+
t = Time('0s')
182175

183176
bbezier = topo.boundary.sample('bezier', 3)
184177
x_bbz = bbezier.bind(ns.x)
185178

186-
assert participant.requires_writing_checkpoint()
187-
checkpoint = args
188-
189-
with log.iter.plain('timestep', dynamic.times) as times:
190-
t = next(times)
191-
179+
with log.iter.plain('time step', count()) as iter_context:
192180
while participant.is_coupling_ongoing():
193181

194-
if participant.requires_reading_checkpoint():
195-
args = checkpoint
182+
if participant.requires_writing_checkpoint():
183+
checkpoint = t, args
184+
185+
t += dynamic.timestep
196186

197187
args = dynamic.newmark_defo_args(**args)
198188
args['traction'] = participant.read_data(
@@ -203,8 +193,11 @@ def main(domain: Domain = Domain(), solid: Solid = Solid(), dynamic: Dynamic = D
203193
rw_name, 'Displacement', rw_ids, rw_sample.eval(ns.d, args) / precice_length)
204194
participant.advance(timestep)
205195

206-
if participant.requires_writing_checkpoint():
207-
checkpoint = args
196+
if participant.requires_reading_checkpoint():
197+
t, args = checkpoint
198+
199+
if participant.is_time_window_complete():
200+
next(iter_context)
208201

209202
xb = function.eval(x_bbz, args)
210203
with export.mplfigure('solution.jpg', dpi=150) as fig:
@@ -224,7 +217,7 @@ def main(domain: Domain = Domain(), solid: Solid = Solid(), dynamic: Dynamic = D
224217
dynamic.add_and_plot(
225218
'ux [mm]', t / 's', ux / 'mm', ax=fig.add_subplot(212, xlabel='time [s]'))
226219

227-
t = next(times)
220+
participant.finalize()
228221

229222

230223
if __name__ == '__main__':

0 commit comments

Comments
 (0)