@@ -86,22 +86,23 @@ class Dynamic:
8686 gamma : float = .8
8787 beta : float = .4
8888
89- def set_timestep (self , timestep ):
90- self .timestep = timestep
91- self .timeseries = defaultdict (
92- deque (maxlen = round (self .window / timestep )).copy )
89+ def __post_init__ (self ):
90+ self .timeseries = defaultdict (deque )
9391
9492 def ramp_up (self , t ):
9593 return .5 - .5 * numpy .cos (numpy .pi * min (t / self .init , 1 ))
9694
97- def add_and_plot (self , name , t , v , ax ):
95+ def add_and_plot (self , name , t , tunit , v , vunit , ax ):
9896 'Add data point and plot time series for past window.'
9997
10098 d = self .timeseries [name ]
10199 d .append ((t , v ))
102- times , values = numpy .stack (d , axis = 1 )
100+ while t - d [0 ][0 ] > self .window :
101+ d .popleft ()
102+ times = numpy .array ([t / tunit for t , _ in d ])
103+ values = numpy .array ([v / vunit for _ , v in d ])
103104 ax .plot (times , values )
104- ax .set_ylabel (name )
105+ ax .set_ylabel (f' { name } [ { vunit } ]' )
105106 ax .grid ()
106107 ax .autoscale (enable = True , axis = 'x' , tight = True )
107108
@@ -123,19 +124,19 @@ def newmark_defo_args(self, d, d0=0., u0δt=0., a0δt2=0., **args):
123124 aδt2 = a0δt2 + δaδt2
124125 return dict (args , d = d + uδt + .5 * aδt2 , d0 = d , u0δt = uδt , a0δt2 = aδt2 )
125126
126- def newmark_defo (self , d ):
127+ def newmark_defo (self , d , timestep ):
127128 D = self .newmark_defo_args (
128129 d , * [function .replace_arguments (d , [('d' , t )]) for t in ('d0' , 'u0δt' , 'a0δt2' )])
129- return D ['u0δt' ] / self . timestep , D ['a0δt2' ] / self . timestep ** 2
130+ return D ['u0δt' ] / timestep , D ['a0δt2' ] / timestep ** 2
130131
131132 def newmark_velo_args (self , u , u0 = 0. , a0δt = 0. , ** args ):
132133 aδt = a0δt + (u - u0 - a0δt ) / self .gamma
133134 return dict (args , u = u + aδt , u0 = u , a0δt = aδt )
134135
135- def newmark_velo (self , u ):
136+ def newmark_velo (self , u , timestep ):
136137 D = self .newmark_velo_args (
137138 u , * [function .replace_arguments (u , [('u' , t )]) for t in ('u0' , 'a0δt' )])
138- return D ['a0δt' ] / self . timestep
139+ return D ['a0δt' ] / timestep
139140
140141
141142def main (domain : Domain = Domain (), fluid : Fluid = Fluid (), dynamic : Dynamic = Dynamic ()):
@@ -164,7 +165,6 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
164165 participant .initialize ()
165166
166167 timestep = participant .get_max_time_step_size ()
167- dynamic .set_timestep (timestep * precice_time )
168168
169169 ns = Namespace ()
170170 ns .δ = function .eye (2 )
@@ -184,8 +184,8 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
184184
185185 ns .urel = topo .field ('u' , btype = 'std' , degree = 2 ,
186186 shape = (2 ,)) * fluid .velocity
187- ns .v , ns .a = dynamic .newmark_defo (ns .d )
188- ns .arel = dynamic .newmark_velo (ns .urel )
187+ ns .v , ns .a = dynamic .newmark_defo (ns .d , timestep * precice_time )
188+ ns .arel = dynamic .newmark_velo (ns .urel , timestep * precice_time )
189189 ns .u_i = 'v_i + urel_i'
190190 ns .DuDt_i = 'a_i + arel_i + ∇_j(u_i) urel_j' # material derivative
191191
@@ -252,7 +252,7 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
252252 if participant .requires_writing_checkpoint ():
253253 checkpoint = t , args
254254
255- t += dynamic . timestep
255+ t += timestep * precice_time
256256
257257 cons ['d' ][r_where ] = participant .read_data (
258258 r_name , 'Displacement' , r_ids , timestep )
@@ -298,9 +298,9 @@ def main(domain: Domain = Domain(), fluid: Fluid = Fluid(), dynamic: Dynamic = D
298298 log .info (f'drag: { D :N/m} ' )
299299 with export .mplfigure ('force.jpg' , dpi = 150 ) as fig :
300300 dynamic .add_and_plot (
301- 'lift [N/m] ' , t / 's' , L / 'N/m' , ax = fig .add_subplot (211 ))
301+ 'lift' , t , 's' , L , 'N/m' , ax = fig .add_subplot (211 ))
302302 dynamic .add_and_plot (
303- 'drag [N/m] ' , t / 's' , D / 'N/m' , ax = fig .add_subplot (212 , xlabel = 'time [s]' ))
303+ 'drag' , t , 's' , D , 'N/m' , ax = fig .add_subplot (212 , xlabel = 'time [s]' ))
304304
305305 participant .finalize ()
306306
0 commit comments