Skip to content

Commit 2c85e71

Browse files
fix(mfusg): use high-precision format only when FREE format is enabled
The previous fix used %16.9G unconditionally, which violates MODFLOW-USG's 10-character fixed-width field requirement for models without FREE format in the BAS file. This fix checks if FREE format is enabled: - With FREE: use %16.9G for full precision - Without FREE: use original %10.2e for compatibility Users who need to preserve coordinate precision (e.g., 57.465 vs 5.75e+01) should enable FREE format in their model. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 80c10a2 commit 2c85e71

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

flopy/mfusg/mfusg.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,22 +535,29 @@ def _send_load_messages(model, files_successfully_loaded, files_not_loaded):
535535
print(f" {os.path.basename(fname)}")
536536

537537

538-
def fmt_string(array):
538+
def fmt_string(array, free=False):
539539
"""
540540
Returns a C-style fmt string for numpy savetxt that corresponds to
541541
the dtype.
542542
543543
Parameters
544544
----------
545545
array : numpy array
546+
free : bool, optional
547+
If True, use high-precision format (%16.9G) for floats which requires
548+
FREE format in the BAS file. If False (default), use fixed 10-character
549+
format (%10.2e) compatible with MODFLOW-USG's fixed-width input format.
546550
"""
547551
fmts = []
552+
# When FREE format is enabled in BAS, we can use high-precision output.
553+
# Without FREE, MODFLOW-USG expects 10-character fixed-width fields.
554+
float_fmt = "%16.9G" if free else "%10.2e"
548555
for field in array.dtype.descr:
549556
vtype = field[1][1].lower()
550557
if vtype in {"i", "b"}:
551558
fmts.append("%10d")
552559
elif vtype == "f":
553-
fmts.append("%16.9G")
560+
fmts.append(float_fmt)
554561
elif vtype == "o":
555562
fmts.append("%10s")
556563
elif vtype == "s":

flopy/mfusg/mfusgcln.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,18 +543,23 @@ def write_file(self, f=None, check=False):
543543
f_cln.write(self.iac_cln.get_file_entry())
544544
f_cln.write(self.ja_cln.get_file_entry())
545545

546-
np.savetxt(f_cln, self.node_prop, fmt=fmt_string(self.node_prop), delimiter="")
546+
free = self.parent.free_format_input
547+
np.savetxt(
548+
f_cln, self.node_prop, fmt=fmt_string(self.node_prop, free), delimiter=""
549+
)
547550

548-
np.savetxt(f_cln, self.cln_gwc, fmt=fmt_string(self.cln_gwc), delimiter="")
551+
np.savetxt(
552+
f_cln, self.cln_gwc, fmt=fmt_string(self.cln_gwc, free), delimiter=""
553+
)
549554

550555
if self.nconduityp > 0:
551556
np.savetxt(
552-
f_cln, self.cln_circ, fmt=fmt_string(self.cln_circ), delimiter=""
557+
f_cln, self.cln_circ, fmt=fmt_string(self.cln_circ, free), delimiter=""
553558
)
554559

555560
if self.nrectyp > 0:
556561
np.savetxt(
557-
f_cln, self.cln_rect, fmt=fmt_string(self.cln_rect), delimiter=""
562+
f_cln, self.cln_rect, fmt=fmt_string(self.cln_rect, free), delimiter=""
558563
)
559564

560565
f_cln.write(self.ibound.get_file_entry())

0 commit comments

Comments
 (0)