1010from dataclasses import dataclass
1111from typing import Optional
1212from pathlib import Path
13+ from itertools import count
1314import treelog as log
1415import 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
320308if __name__ == '__main__' :
0 commit comments