Skip to content

Commit

Permalink
Strict eqdsk precision (#17)
Browse files Browse the repository at this point in the history
* revert precision change

* Enforce strict specification
  • Loading branch information
je-cook authored Jul 4, 2024
1 parent a92112e commit 4176b3f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions eqdsk/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ def write(
file_path: str,
file_format: str = "json",
json_kwargs: dict | None = None,
*,
strict_spec: bool = True,
):
"""Write the EQDSK data to file in the given format.
Expand All @@ -296,6 +298,9 @@ def write(
json_kwargs:
Key word arguments to pass to the ``json.dump`` call. Only
used if ``format`` is 'json'.
strict_spec:
As https://w3.pppl.gov/ntcc/TORAY/G_EQDSK.pdf arrays have the format
5e16.9, disabling this changes the format to 5ES23.16e2
"""
if file_format == "json":
json_kwargs = {} if json_kwargs is None else json_kwargs
Expand All @@ -305,7 +310,7 @@ def write(
"You are in the 21st century. "
"Are you sure you want to be making an EDQSK in this day and age?"
)
_write_eqdsk(file_path, self.to_dict())
_write_eqdsk(file_path, self.to_dict(), strict_spec=strict_spec)

def update(self, eqdsk_data: dict[str, Any]):
"""Update this object's data with values from a dictionary.
Expand Down Expand Up @@ -516,7 +521,7 @@ def _derive_psinorm(fpol) -> np.ndarray:
return np.linspace(0, 1, len(fpol))


def _write_eqdsk(file_path: str | Path, data: dict):
def _write_eqdsk(file_path: str | Path, data: dict, *, strict_spec: bool = True):
"""Write data out to a text file in G-EQDSK format.
Parameters
Expand All @@ -525,6 +530,9 @@ def _write_eqdsk(file_path: str | Path, data: dict):
The full path string of the file to be created
data:
Dictionary of EQDSK data.
strict_spec:
As https://w3.pppl.gov/ntcc/TORAY/G_EQDSK.pdf arrays have the format
5e16.9, disabling this changes the format to 5ES23.16e2
"""
file_path = Path(file_path)
if file_path.suffix not in EQDSK_EXTENSIONS:
Expand Down Expand Up @@ -622,7 +630,7 @@ def write_array(fortran_format: ff.FortranRecordWriter, array: np.ndarray):
# Create FortranRecordWriter objects with the Fortran format
# edit descriptors to be used in the G-EQDSK output.
f2000 = ff.FortranRecordWriter("a48,3i4")
f2020 = ff.FortranRecordWriter("5ES23.16e2")
f2020 = ff.FortranRecordWriter("5e16.9" if strict_spec else "5ES23.16e2")
f2022 = ff.FortranRecordWriter("2i5")
fCSTM = ff.FortranRecordWriter("i5")

Expand All @@ -648,5 +656,6 @@ def write_array(fortran_format: ff.FortranRecordWriter, array: np.ndarray):

# Output of coilset information. This is an extension to the
# regular eqdsk format.
write_line(fCSTM, ["ncoil"])
write_array(f2020, coil)
if data["ncoil"] > 0:
write_line(fCSTM, ["ncoil"])
write_array(f2020, coil)
4 changes: 2 additions & 2 deletions tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_private_dir():
return None


def read_strict_geqdsk(file_path):
def read_strict_geqdsk(file_path, *, strict_spec=True):
"""
Reads an input EQDSK file in, assuming strict adherence to the
GEQDSK format. Used to check bluemira outputs can be read by
Expand All @@ -41,7 +41,7 @@ def read_strict_geqdsk(file_path):
# Create FortranRecordReader objects with the Fortran format
# edit descriptors to be used to parse the G-EQDSK input.
f2000 = ff.FortranRecordReader("a48,3i4")
f2020 = ff.FortranRecordReader("5ES23.16e2")
f2020 = ff.FortranRecordReader("5e16.9" if strict_spec else "5ES23.16e2")
f2022 = ff.FortranRecordReader("2i5")
fCSTM = ff.FortranRecordReader("i5")

Expand Down
2 changes: 1 addition & 1 deletion tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_read_write_doesnt_change_file_private(file, ftype, ind, tmp_path):
eqd_default.to_dict(), eqd_default_nc.to_dict(), verbose=True
)

eqd_default.write(tmp_path / "test", file_format=ftype)
eqd_default.write(tmp_path / "test", file_format=ftype, strict_spec=False)

eqd_test = EQDSKInterface.from_file(tmp_path / f"test.{ftype}", no_cocos=True)
eqd_test_d = eqd_test.to_dict()
Expand Down

0 comments on commit 4176b3f

Please sign in to comment.