Skip to content

Commit 70a5411

Browse files
committed
Fix writing failure if rbdry is None
Fixes #26
1 parent 10165c1 commit 70a5411

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

freeqdsk/geqdsk.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,10 @@ def write(
441441
# Get dimensions and check data is correct
442442
nx = data.get("nx", np.shape(data["psi"])[0])
443443
ny = data.get("ny", np.shape(data["psi"])[1])
444-
nbdry = data.get("nbdry", len(data.get("rbdry", [])))
445-
nlim = data.get("nlim", len(data.get("rlim", [])))
444+
rbdry = data.get("rbdry")
445+
nbdry = data.get("nbdry", len(rbdry) if rbdry is not None else 0)
446+
rlim = data.get("rlim")
447+
nlim = data.get("nlim", len(rlim) if rlim is not None else 0)
446448
for grid in ("fpol", "pres", "ffprime", "pprime", "qpsi"):
447449
if grid not in data:
448450
if grid in ("ffprime", "pprime"):

tests/test_geqdsk.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,50 @@ def test_setitem():
330330

331331
data["limitr"] = 12
332332
assert data.nlim == 12
333+
334+
335+
def test_writing_no_boundary_file(tmp_path):
336+
"""https://github.com/freegs-plasma/FreeQDSK/issues/26"""
337+
338+
nx = 16
339+
ny = 16
340+
data = geqdsk.GEQDSKFile(
341+
comment="Noboundary test",
342+
shot=0,
343+
nx=nx,
344+
ny=ny,
345+
rdim=2 + rand(),
346+
zdim=1 + rand(),
347+
rcentr=1.5 + 0.1 * rand(),
348+
bcentr=2 + rand(),
349+
rleft=rand(),
350+
zmid=0.1 * rand(),
351+
rmagx=1 + rand(),
352+
zmagx=0.1 + 0.05 * (1 - rand()),
353+
simagx=-rand(),
354+
sibdry=rand(),
355+
cpasma=1e6 * (1 + rand()),
356+
fpol=rand(nx),
357+
pres=rand(nx),
358+
qpsi=rand(nx),
359+
psi=rand(nx, ny),
360+
nbdry=0,
361+
nlim=0,
362+
ffprime=np.zeros(nx),
363+
pprime=np.zeros(nx),
364+
)
365+
366+
filename = tmp_path / "nobdry.geqdsk"
367+
with filename.open("w") as f:
368+
geqdsk.write(data, f)
369+
370+
with filename.open() as f:
371+
roundtrip = geqdsk.read(f)
372+
373+
for key in asdict(data):
374+
if key in ("comment", "shot"):
375+
continue
376+
if data[key] is None:
377+
assert roundtrip[key] is None, key
378+
else:
379+
assert_allclose(data[key], roundtrip[key], err_msg=key)

0 commit comments

Comments
 (0)