@@ -1190,23 +1190,32 @@ def write_data(
11901190 assert isinstance (timestamps , np .ndarray )
11911191 assert isinstance (time , float )
11921192 time_exists = np .flatnonzero (np .isclose (timestamps , time ))
1193-
1193+ if len (time_exists ) > 1 :
1194+ raise ValueError ("Time-step has been written multiple times" )
1195+ pdo_u = _create_dataset (
1196+ pdo ,
1197+ array_data .name ,
1198+ shape = (1 ,),
1199+ dtype = np .int64 ,
1200+ chunks = True ,
1201+ maxshape = (None ,),
1202+ mode = h5_mode ,
1203+ resize = False ,
1204+ )
11941205 if len (time_exists ) == 1 :
1195- # If mesh time-step exists, update the data offsets for that step
11961206 idx = time_exists [0 ]
1197- pdo_u = _create_dataset (
1198- pdo ,
1199- array_data .name ,
1200- shape = (1 ,),
1201- dtype = np .int64 ,
1202- chunks = True ,
1203- maxshape = (None ,),
1204- mode = h5_mode ,
1205- resize = False ,
1206- )
1207- pdo_u [idx ] = dataset .shape [0 ] - array_data .global_shape [0 ]
12081207 elif len (time_exists ) == 0 :
12091208 # No mesh written at step, update mesh offsets
1209+ # Geometry has to be time-dependent
1210+ num_points = block ["NumberOfPoints" ]
1211+ num_points .resize (len (timestamps ) + 1 , axis = 0 )
1212+ num_points [- 1 ] = num_points [- 2 ]
1213+ # Even if topology cannot be time dep at the moment, number of cells
1214+ # must be updated
1215+ num_cells = block ["NumberOfCells" ]
1216+ num_cells .resize (len (timestamps ) + 1 , axis = 0 )
1217+ num_cells [- 1 ] = num_cells [- 2 ]
1218+
12101219 steps .attrs .create ("NSteps" , block ["Steps" ].attrs ["NSteps" ] + 1 )
12111220 step_vals = _create_dataset (
12121221 steps ,
@@ -1219,26 +1228,38 @@ def write_data(
12191228 resize = True ,
12201229 )
12211230 step_vals [- 1 ] = time
1231+ idx = step_vals .size - 1
12221232 for key in [
12231233 "PartOffsets" ,
12241234 "NumberOfParts" ,
12251235 "PointOffsets" ,
12261236 "CellOffsets" ,
12271237 "ConnectivityIdOffsets" ,
1228- "CellDataOffsets" ,
1229- "PointDataOffsets" ,
12301238 ]:
1231- if key in steps .keys ():
1232- comp = steps [key ]
1233- if isinstance (comp , h5py .Group ):
1234- for dname , dset in comp .items ():
1235- if dname != array_data .name and key != f"{ extension } DataOffsets" :
1236- dset .resize (dset .size + 1 , axis = 0 )
1237- dset [- 1 ] = dset [- 2 ]
1238- elif isinstance (comp , h5py .Dataset ):
1239- comp .resize (comp .size + 1 , axis = 0 )
1240- comp [- 1 ] = comp [- 2 ]
1241- else :
1242- raise NotImplementedError (f"Ubsupported type { type (comp )} " )
1239+ comp = steps [key ]
1240+ comp .resize (steps .attrs ["NSteps" ], axis = 0 )
1241+ if comp .size > 1 :
1242+ comp [- 1 ] = comp [- 2 ]
12431243 else :
12441244 raise ValueError (f"Time step found multiple times in { filename } " )
1245+
1246+ # Update offsets for current variable
1247+ pdo_u .resize (steps .attrs ["NSteps" ], axis = 0 )
1248+ pdo_u [idx ] = dataset .shape [0 ] - array_data .global_shape [0 ]
1249+
1250+ # Point and cell data of all other variables has to be updated to be synced
1251+ for key in [
1252+ "CellDataOffsets" ,
1253+ "PointDataOffsets" ,
1254+ ]:
1255+ comp = steps [key ]
1256+ num_steps = steps .attrs ["NSteps" ]
1257+ for dname , dset in comp .items ():
1258+ # Only resize other data arrays
1259+ if dname != array_data .name :
1260+ # Only resize if it hasn't already been resized
1261+ if dset .size != num_steps :
1262+ dset .resize (num_steps , axis = 0 )
1263+ # Only update data if we are further than first time step
1264+ if dset .size > 1 :
1265+ dset [- 1 ] = dset [- 2 ]
0 commit comments