Skip to content

Conversation

@yingjie1234
Copy link

@yingjie1234 yingjie1234 commented Nov 19, 2025

#308 This is starting point to fix gauge format. Please give me suggestions on this.

New per-gauge option:
rundata.gaugedata.file_format = 'binary32' (or 'binary64', 'ascii'); defaults to ASCII if unspecified.

New file types:
Each gauge can now write to gaugeXXXXXX.bin with a short ASCII header (e.g., # file_format: binary32).

Binary layout:
[level :: int32] [time :: float32/float64] [q(1:nvar) :: float32/float64]

Buffer management:

Added flush_all_gauges() to write any buffered data during checkpoints.

Updated print_gauges_and_reset_nextLoc() to handle binary output modes.

Example & test:
Updated examples/advection_1d_example1/setrun.py to demonstrate binary mode.
Added src/1d/verify.py — a small Python script using NumPy to read and verify .bin gauge files.

@mandli mandli marked this pull request as draft November 19, 2025 12:56
@mandli mandli self-assigned this Nov 19, 2025
@mandli mandli requested a review from rjleveque November 19, 2025 12:57
@yingjie1234
Copy link
Author

@mandli I uploaded my code again, how to test it again on your side?

@mandli
Copy link
Member

mandli commented Dec 2, 2025

@yingjie1234 I just approved the testing. You should get the results shortly.

@rjleveque
Copy link
Member

@yingjie1234 - Thanks for working on this and sorry to be slow to look at this. I haven't gone through the code carefully, but I tried running the example in examples/advection_1d_example1 and noted the following:

It runs fine when I set

rundata.gaugedata.file_format = 'binary32'     # <= single string

or the other options as a string, but setting

rundata.gaugedata.file_format = 2

throws an error

 File "/Users/rjl/git/clawpack/amrclaw/src/python/amrclaw/data.py", line 446, in write
    file_format = format_map[self.file_format[gauge_num].lower()]
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'lower'

with a similar error if it's set to any list.

Also, running pytest, the test passes for 'ascii' but not for the binary choices.

@yingjie1234
Copy link
Author

@mandli could you guide on the testing on this part? I can fix this error. thanks!

@mandli
Copy link
Member

mandli commented Dec 3, 2025

What we need is to modify one of the tests to also write out the gauges in every format and compare them. I would take the test that you have modified and add a gauge for each format and write code that then reads in the gauges and compares them to each other. Does that make sense?

@yingjie1234
Copy link
Author

Yes, I will test it then get back to you soon.

@yingjie1234
Copy link
Author

yingjie1234 commented Dec 7, 2025

Summary: Add Binary Gauge Output Support (binary32 / binary64)

This PR introduces optional binary gauge output to AMRClaw, while keeping ASCII as the default and fully backward-compatible.
It adds finer control over gauge file formats, improves performance for large simulations, and includes a verification tool.


Key Features

1. New file_format options (scalar or per-gauge)

Users can now specify gauge output formats using either strings or integers:

# Global format
rundata.gaugedata.file_format = "ascii"      # or 1
rundata.gaugedata.file_format = "binary32"   # or 2
rundata.gaugedata.file_format = "binary64"   # or 3

# Per-gauge format
rundata.gaugedata.file_format = ["ascii", "binary32"]
rundata.gaugedata.file_format = [1, 2]

2. Updated gauges.data writer

# File format (1=ascii, 2=binary32, 3=binary64)
2 2

3. Fortran support for binary gauge writers

Binary files (gaugeXXXXX.bin) use unformatted stream output with layout:

  • level :: int32
  • time :: float32 / float64
  • q(1:nvar) :: float32 / float64

Each file contains a one-line ASCII header:

# file_format: binary32

4. Added verification tool

src/1d/verify.py reads .bin gauge files using NumPy and prints:

_output/gauge00000.bin 4950 rows; first3: [...]
_output/gauge00001.bin 5160 rows; first3: [...]

Testing

  • Ran examples/advection_1d_example1 with:
    • default ASCII,
    • file_format = ["binary32", "binary32"],
    • file_format = [2, 2].
  • Verified correct gauges.data output (2 2) and .bin files.
  • Confirmed binary layout via verify.py.
  • Restored example defaults to ASCII so regression tests remain valid.
  • Full AMRClaw test suite:
14 passed, 0 failed

@mandli mandli marked this pull request as ready for review December 8, 2025 14:10
@mandli
Copy link
Member

mandli commented Dec 8, 2025

Thanks @yingjie1234 ! @rjleveque and I will review this and give you feedback if necessary.

Copy link
Member

@mandli mandli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is some basic feedback from a quick read-through.


# Per-gauge formats: apply same to all gauges
rundata.gaugedata.file_format = 'binary32' # <= single string
rundata.gaugedata.display_format = 'e15.7' # only used for ascii; harmless to keep
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May want to clean this up for the example

self.rundata.gaugedata.file_format = "ascii" # or 1
if self.rundata.gaugedata.file_format in ("binary32", 2):
rtol, atol = 1e-7, 5e-8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest writing a gauge out for every format available and comparing them all as a test, perhaps with unique tolerances if need be. Each gauge could actually be at the same location, just different formats (just for the test).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I will do that

@mandli mandli changed the title WIP: add binary file_format for gauge Add binary file_format for gauge in 1D Dec 8, 2025
@mandli
Copy link
Member

mandli commented Dec 8, 2025

I should note that the other dimensions could be added to this PR, but as it only modifies the 1D code so far, I wanted to make sure that the current state, if merged, is reflected in the title.

@yingjie1234
Copy link
Author

Add per-gauge binary format support and comprehensive tests

  • Support per-gauge file formats (ascii, binary32, binary64)
  • Move load_bin to amrclaw/util.py for reusability
  • Add tests for gauge formats in 1D, 2D, and 3D
  • Update example to test all formats simultaneously
  • Fix pytest warnings in test functions

- Support per-gauge file formats (ascii, binary32, binary64)
- Move load_bin to amrclaw/util.py for reusability
- Add tests for gauge formats in 1D, 2D, and 3D
- Update example to test all formats simultaneously
- Fix pytest warnings in test functions

# ipython
.ipynb_checkpoints
TESTING_GAUGE_FORMATS.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this generated somewhere?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I did not include it, it only generated on my end.

@mandli
Copy link
Member

mandli commented Dec 15, 2025

Can you move the tests to src/python/amrclaw? Were they not being discovered otherwise?

@yingjie1234
Copy link
Author

Can you move the tests to src/python/amrclaw? Were they not being discovered otherwise?

yes, I already move the tests to src/python/amrclaw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants