1+ import os
12import sys
3+ import glob
24import pytest
35
46if sys .version_info < (3 , 11 ):
@@ -153,6 +155,46 @@ def testRectilinear(dim, nVar, nSteps, dtypeIdx):
153155 assert np .allclose (u2 , u1 ), f"{ idx } 's fields in { f1 } has incorrect values"
154156
155157
158+ @pytest .mark .parametrize ("nSteps" , [1 , 10 ])
159+ @pytest .mark .parametrize ("nZ" , [1 , 5 , 16 ])
160+ @pytest .mark .parametrize ("nY" , [1 , 5 , 16 ])
161+ @pytest .mark .parametrize ("nX" , [1 , 5 , 16 ])
162+ @pytest .mark .parametrize ("nVar" , [1 , 2 , 3 ])
163+ def testToVTR (nVar , nX , nY , nZ , nSteps ):
164+
165+ from pySDC .helpers .fieldsIO import Rectilinear
166+ from pySDC .helpers .vtkIO import readFromVTR
167+
168+ coords = [np .linspace (0 , 1 , num = n , endpoint = False ) for n in [nX , nY , nZ ]]
169+ file = Rectilinear (np .float64 , "testToVTR.pysdc" )
170+ file .setHeader (nVar = nVar , coords = coords )
171+ file .initialize ()
172+ u0 = np .random .rand (nVar , nX , nY , nZ ).astype (file .dtype )
173+ times = np .arange (nSteps ) / nSteps
174+ for t in times :
175+ ut = (u0 * t ).astype (file .dtype )
176+ file .addField (t , ut )
177+
178+ # Cleaning after eventuall other tests ...
179+ for f in glob .glob ("testToVTR*.vtr" ):
180+ os .remove (f )
181+
182+ file .toVTR ("testToVTR" , varNames = [f"var{ i } " for i in range (nVar )])
183+
184+ vtrFiles = glob .glob ("testToVTR*.vtr" )
185+ assert len (vtrFiles ) == file .nFields
186+
187+ vtrFiles .sort (key = lambda name : int (name .split ("_" )[1 ]))
188+ for i , vFile in enumerate (vtrFiles ):
189+ uVTR , coords , _ = readFromVTR (vFile )
190+ tVTR = float (vFile .split ("_t=" )[- 1 ].split ("s.vtr" )[0 ])
191+ tFile , uFile = file .readField (i )
192+ assert np .allclose (tFile , tVTR ), "mismatch between field times"
193+ assert np .allclose (uFile , uVTR ), "mismatch between data"
194+ for i , (xVTR , xFile ) in enumerate (zip (coords , file .header ["coords" ])):
195+ assert np .allclose (xVTR , xFile ), f"coordinate mismatch in dir. { i } "
196+
197+
156198@pytest .mark .mpi4py
157199@pytest .mark .parametrize ("nVar" , [1 , 4 ])
158200@pytest .mark .parametrize ("nSteps" , [1 , 10 ])
@@ -175,7 +217,7 @@ def testRectilinear_MPI(dim, nProcs, dtypeIdx, algo, nSteps, nVar):
175217 p .wait ()
176218 assert p .returncode == 0 , f"MPI write with { nProcs } proc(s) did not return code 0, but { p .returncode } "
177219
178- from pySDC .helpers .fieldsIO import FieldsIO , Rectilinear , initGrid
220+ from pySDC .helpers .fieldsIO import Rectilinear , initGrid
179221
180222 f2 : Rectilinear = FieldsIO .fromFile (fileName )
181223
0 commit comments