Skip to content

Commit bf7e7d2

Browse files
committed
TL: yet another try ...
1 parent 166a0e4 commit bf7e7d2

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

pySDC/helpers/fieldsIO.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,61 @@ def readField(self, idx):
638638
self.MPI_FILE_CLOSE()
639639

640640
return t, field
641+
642+
643+
# Utility function used for testing
644+
def initGrid(nVar, gridSizes):
645+
dim = len(gridSizes)
646+
coords = [np.linspace(0, 1, num=n, endpoint=False) for n in gridSizes]
647+
s = [None] * dim
648+
u0 = np.array(np.arange(nVar) + 1)[:, *s]
649+
for x in np.meshgrid(*coords, indexing="ij"):
650+
u0 = u0 * x
651+
return coords, u0
652+
653+
654+
def writeFields_MPI(fileName, dtypeIdx, algo, nSteps, nVar, nX):
655+
coords, u0 = initGrid(nVar, nX)
656+
657+
from mpi4py import MPI
658+
from pySDC.helpers.blocks import BlockDecomposition
659+
from pySDC.helpers.fieldsIO import Rectilinear
660+
661+
comm = MPI.COMM_WORLD
662+
MPI_SIZE = comm.Get_size()
663+
MPI_RANK = comm.Get_rank()
664+
665+
blocks = BlockDecomposition(MPI_SIZE, nX, algo, MPI_RANK)
666+
667+
iLoc, nLoc = blocks.localBounds
668+
Rectilinear.setupMPI(comm, iLoc, nLoc)
669+
s = [slice(i, i + n) for i, n in zip(iLoc, nLoc)]
670+
u0 = u0[:, *s]
671+
print(MPI_RANK, u0.shape)
672+
673+
f1 = Rectilinear(DTYPES[dtypeIdx], fileName)
674+
f1.setHeader(nVar=nVar, coords=coords)
675+
676+
u0 = np.asarray(u0, dtype=f1.dtype)
677+
f1.initialize()
678+
679+
times = np.arange(nSteps) / nSteps
680+
for t in times:
681+
ut = (u0 * t).astype(f1.dtype)
682+
f1.addField(t, ut)
683+
684+
return u0
685+
686+
687+
def compareFields_MPI(fileName, u0, nSteps):
688+
from pySDC.helpers.fieldsIO import FieldsIO
689+
690+
f2 = FieldsIO.fromFile(fileName)
691+
692+
times = np.arange(nSteps) / nSteps
693+
for idx, t in enumerate(times):
694+
u1 = u0 * t
695+
t2, u2 = f2.readField(idx)
696+
assert t2 == t, f"fields[{idx}] in {f2} has incorrect time ({t2} instead of {t})"
697+
assert u2.shape == u1.shape, f"{idx}'s fields in {f2} has incorrect shape"
698+
assert np.allclose(u2, u1), f"{idx}'s fields in {f2} has incorrect values"

pySDC/tests/test_helpers/test_fieldsIO.py

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -153,63 +153,6 @@ def testRectilinear(dim, nVar, nSteps, dtypeIdx):
153153
assert np.allclose(u2, u1), f"{idx}'s fields in {f1} has incorrect values"
154154

155155

156-
def initGrid(nVar, gridSizes):
157-
dim = len(gridSizes)
158-
coords = [np.linspace(0, 1, num=n, endpoint=False) for n in gridSizes]
159-
s = [None] * dim
160-
u0 = np.array(np.arange(nVar) + 1)[:, *s]
161-
for x in np.meshgrid(*coords, indexing="ij"):
162-
u0 = u0 * x
163-
return coords, u0
164-
165-
166-
def writeFields_MPI(fileName, dtypeIdx, algo, nSteps, nVar, nX):
167-
coords, u0 = initGrid(nVar, nX)
168-
169-
from mpi4py import MPI
170-
from pySDC.helpers.blocks import BlockDecomposition
171-
from pySDC.helpers.fieldsIO import Rectilinear
172-
173-
comm = MPI.COMM_WORLD
174-
MPI_SIZE = comm.Get_size()
175-
MPI_RANK = comm.Get_rank()
176-
177-
blocks = BlockDecomposition(MPI_SIZE, nX, algo, MPI_RANK)
178-
179-
iLoc, nLoc = blocks.localBounds
180-
Rectilinear.setupMPI(comm, iLoc, nLoc)
181-
s = [slice(i, i + n) for i, n in zip(iLoc, nLoc)]
182-
u0 = u0[:, *s]
183-
print(MPI_RANK, u0.shape)
184-
185-
f1 = Rectilinear(DTYPES[dtypeIdx], fileName)
186-
f1.setHeader(nVar=nVar, coords=coords)
187-
188-
u0 = np.asarray(u0, dtype=f1.dtype)
189-
f1.initialize()
190-
191-
times = np.arange(nSteps) / nSteps
192-
for t in times:
193-
ut = (u0 * t).astype(f1.dtype)
194-
f1.addField(t, ut)
195-
196-
return u0
197-
198-
199-
def compareFields_MPI(fileName, u0, nSteps):
200-
from pySDC.helpers.fieldsIO import FieldsIO
201-
202-
f2 = FieldsIO.fromFile(fileName)
203-
204-
times = np.arange(nSteps) / nSteps
205-
for idx, t in enumerate(times):
206-
u1 = u0 * t
207-
t2, u2 = f2.readField(idx)
208-
assert t2 == t, f"fields[{idx}] in {f2} has incorrect time ({t2} instead of {t})"
209-
assert u2.shape == u1.shape, f"{idx}'s fields in {f2} has incorrect shape"
210-
assert np.allclose(u2, u1), f"{idx}'s fields in {f2} has incorrect values"
211-
212-
213156
@pytest.mark.mpi4py
214157
@pytest.mark.parametrize("nVar", [1, 4])
215158
@pytest.mark.parametrize("nSteps", [1, 10])
@@ -232,7 +175,7 @@ def testRectilinear_MPI(dim, nProcs, dtypeIdx, algo, nSteps, nVar):
232175
p.wait()
233176
assert p.returncode == 0, f"MPI write with {nProcs} proc(s) did not return code 0, but {p.returncode}"
234177

235-
from pySDC.helpers.fieldsIO import FieldsIO, Rectilinear
178+
from pySDC.helpers.fieldsIO import FieldsIO, Rectilinear, initGrid
236179

237180
f2: Rectilinear = FieldsIO.fromFile(fileName)
238181

@@ -266,5 +209,8 @@ def testRectilinear_MPI(dim, nProcs, dtypeIdx, algo, nSteps, nVar):
266209
parser.add_argument('--nX', type=int, nargs='+', help="number of grid points in each dimensions")
267210
args = parser.parse_args()
268211

269-
u0 = writeFields_MPI(**args.__dict__)
270-
compareFields_MPI(args.fileName, u0, args.nSteps)
212+
if sys.version_info >= (3, 11):
213+
from pySDC.helpers.fieldsIO import writeFields_MPI, compareFields_MPI
214+
215+
u0 = writeFields_MPI(**args.__dict__)
216+
compareFields_MPI(args.fileName, u0, args.nSteps)

0 commit comments

Comments
 (0)