Skip to content

Increase use of SafeFilePtr class#4917

Merged
akohlmey merged 12 commits into
lammps:developfrom
akohlmey:more-use-of-safefileptr
Mar 18, 2026
Merged

Increase use of SafeFilePtr class#4917
akohlmey merged 12 commits into
lammps:developfrom
akohlmey:more-use-of-safefileptr

Conversation

@akohlmey

@akohlmey akohlmey commented Mar 16, 2026

Copy link
Copy Markdown
Member

Summary

The SafeFilePtr class will automatically close files when their file pointer goes out of scope.
This helps to simplify code. This pull request expands the usage and adds to the documentation.

Author(s)

Axel Kohlmeyer, Temple U

Licensing

By submitting this pull request, I agree, that my contribution will be included in LAMMPS and redistributed under either the GNU General Public License version 2 (GPL v2) or the GNU Lesser General Public License version 2.1 (LGPL v2.1).

Artificial Intelligence (AI) Tools Usage

A first draft of some of the additional uses was generated by Claude Opus 4.6.
The more complex changes were done manually.

Backward Compatibility

No known issues.

Implementation Notes

This extends the existing class to include a flag if a FILE * needs to be closed with pclose() instead of fclose() to avoid file descriptor leaks.

Post Submission Checklist

  • The feature or features in this pull request is complete
  • Licensing information is complete
  • Corresponding author information is complete
  • The source code follows the LAMMPS formatting guidelines
  • Suitable new documentation files and/or updates to the existing docs are included
  • The added/updated documentation is integrated and tested with the documentation build system
  • The feature has been verified to work with the conventional build system
  • The feature has been verified to work with the CMake based build system

@akohlmey
akohlmey marked this pull request as ready for review March 16, 2026 03:04
@akohlmey akohlmey moved this from In Progress to Ready for Review or Merge in LAMMPS Pull Requests Mar 16, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR expands adoption of the SafeFilePtr RAII wrapper across LAMMPS to simplify FILE* lifetime management and reduce file descriptor leaks, and extends the wrapper to support pclose() for streams opened via popen() (e.g., on-the-fly compression).

Changes:

  • Extend SafeFilePtr with a pclose() option and move destructor/assignment logic into a new safe_pointers.cpp.
  • Replace many FILE* members/locals with SafeFilePtr and remove corresponding manual fclose()/pclose() calls.
  • Add developer documentation and Doxygen input integration for SafeFilePtr.

Reviewed changes

Copilot reviewed 77 out of 77 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/write_restart.h Replace FILE* member with SafeFilePtr.
src/write_restart.cpp Use RAII close (fp=nullptr) and add earlier I/O error reduction.
src/write_data.h Replace FILE* member with SafeFilePtr.
src/write_data.cpp Use SafeFilePtr and mark compressed output for pclose().
src/write_coeff.cpp Use SafeFilePtr for temp/output coeff files.
src/variable.h Replace FILE* member with SafeFilePtr for VarReader.
src/variable.cpp Use SafeFilePtr for several temporary/utility file opens.
src/universe.cpp Use SafeFilePtr in reorder file reading.
src/safe_pointers.h Extend/document SafeFilePtr (including pclose() support).
src/safe_pointers.cpp New: implementation of SafeFilePtr destructor/assignment with pclose() support.
src/reader.h Replace FILE* with SafeFilePtr; remove explicit compressed member.
src/reader.cpp Use SafeFilePtr for compressed/uncompressed open/close.
src/read_restart.h Replace FILE* member with SafeFilePtr.
src/read_restart.cpp Remove manual fclose() in favor of SafeFilePtr reset.
src/read_data.h Replace FILE* member with SafeFilePtr; drop compressed flag.
src/read_data.cpp Use SafeFilePtr and mark compressed reads for pclose().
src/procmap.cpp Use SafeFilePtr and improve custom-grid error messages.
src/platform.cpp Use SafeFilePtr in file readability/writability helpers.
src/pair.cpp Use SafeFilePtr for pair-table writing.
src/molecule.h Replace FILE* member with SafeFilePtr.
src/molecule.cpp Remove manual fclose() calls; minor formatting and rename print() parameter.
src/min_hftn.h Replace progress FILE* with SafeFilePtr; remove explicit close helper decl.
src/min_hftn.cpp Remove explicit fclose() management for progress file.
src/label_map.cpp Use SafeFilePtr for label map writing.
src/fix_print.h Replace FILE* member with SafeFilePtr.
src/fix_print.cpp Remove manual fclose() in destructor; rely on RAII.
src/fix_ave_time.h Replace FILE* member with SafeFilePtr.
src/fix_ave_time.cpp Rely on RAII close; keep YAML trailer write logic.
src/fix_ave_histo.h Replace FILE* member with SafeFilePtr.
src/fix_ave_histo.cpp Remove manual fclose() in destructor.
src/fix_ave_correlate.h Replace FILE* member with SafeFilePtr.
src/fix_ave_correlate.cpp Remove manual fclose() in destructor.
src/fix_ave_chunk.h Replace FILE* member with SafeFilePtr.
src/fix_ave_chunk.cpp Remove manual fclose() in destructor.
src/dump.h Replace FILE* member with SafeFilePtr.
src/dump.cpp Remove manual close logic; use RAII and fp=nullptr on multifile writes.
src/citeme.h Replace FILE* member with SafeFilePtr.
src/citeme.cpp Remove manual fclose() in destructor.
src/balance.h Replace FILE* member with SafeFilePtr.
src/balance.cpp Remove manual fclose() in destructor.
src/RIGID/fix_rigid.cpp Use SafeFilePtr for rigid restart sidecar file write.
src/PYTHON/python_impl.cpp Use SafeFilePtr for Python file execution.
src/PHONON/fix_phonon.cpp Use SafeFilePtr for map/binary output files.
src/ORIENT/fix_orient_fcc.cpp Use SafeFilePtr for reading orientation data files.
src/ORIENT/fix_orient_eco.cpp Use SafeFilePtr for reading orientation data file.
src/ORIENT/fix_orient_bcc.cpp Use SafeFilePtr for reading orientation data files.
src/ML-SNAP/pair_snap.cpp Use SafeFilePtr for SNAP coeff/param reads.
src/ML-RANN/pair_rann.cpp Use SafeFilePtr for RANN potential file reads.
src/ML-POD/pair_pod.cpp Use SafeFilePtr for binary debug outputs.
src/ML-POD/fitpod_command.cpp Use SafeFilePtr for POD training IO and binary outputs.
src/ML-POD/eapod.cpp Use SafeFilePtr for POD model file reads.
src/ML-IAP/mliap_model_nn.cpp Use SafeFilePtr for NN coeff reads.
src/ML-IAP/mliap_descriptor_snap.cpp Use SafeFilePtr for SNAP param reads.
src/MBX/fix_mbx.cpp Use SafeFilePtr for JSON existence check.
src/MANYBODY/pair_eim.cpp Use SafeFilePtr for EIM potential read/parse.
src/MACHDYN/fix_smd_wall_surface.cpp Use SafeFilePtr for triangle mesh file reads.
src/KOKKOS/pair_pod_kokkos.cpp Use SafeFilePtr for POD Kokkos binary outputs.
src/KOKKOS/pair_exp6_rx_kokkos.cpp Use SafeFilePtr for EXP6-RX file reads.
src/INTEL/pair_snap_intel.cpp Use SafeFilePtr for SNAP Intel coeff/param reads.
src/GRAPHICS/fix_graphics_labels.cpp Use SafeFilePtr for image file reads.
src/GRAPHICS/fix_graphics_isosurface.cpp Use SafeFilePtr for isosurface output file writes (including compressed).
src/GRAPHICS/dump_image.cpp Use RAII close via fp=nullptr for multifile outputs.
src/EXTRA-FIX/fix_ttm_mod.cpp Use SafeFilePtr for electron temperature output.
src/EXTRA-FIX/fix_ttm_grid.cpp Use SafeFilePtr for grid file reads.
src/EXTRA-FIX/fix_ttm.cpp Use SafeFilePtr for electron temperature output.
src/EXTRA-DUMP/dump_xtc.cpp Remove explicit fp=nullptr handling (XTC uses separate handle).
src/EXTRA-COMMAND/geturl.cpp Use SafeFilePtr for output file in curl download.
src/DPD-REACT/pair_multi_lucy.cpp Use SafeFilePtr for table file reads.
src/DPD-REACT/pair_exp6_rx.cpp Use SafeFilePtr for EXP6-RX file reads.
src/DPD-REACT/fix_rx.cpp Use SafeFilePtr for RX kinetics/parameter file reads.
src/DPD-REACT/fix_eos_table_rx.cpp Use SafeFilePtr for EOS table reads.
src/BOCS/fix_bocs.cpp Use SafeFilePtr for reading BOCS table file.
src/ADIOS/reader_adios.cpp Use SafeFilePtr for adios2_config.xml existence/create.
src/ADIOS/dump_custom_adios.cpp Use SafeFilePtr for adios2_config.xml existence/create.
src/ADIOS/dump_atom_adios.cpp Use SafeFilePtr for adios2_config.xml existence/create.
doc/src/Developer_utils.rst Add Safe pointer documentation section and navigation tweaks.
doc/doxygen/Doxyfile.in Include safe_pointers.h in Doxygen input list.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/safe_pointers.h
Comment thread src/platform.cpp
Comment thread src/write_coeff.cpp
Comment thread doc/src/Developer_utils.rst Outdated
Comment thread src/safe_pointers.h
@akohlmey
akohlmey merged commit fc820b4 into lammps:develop Mar 18, 2026
14 checks passed
@github-project-automation github-project-automation Bot moved this from Ready for Review or Merge to Done in LAMMPS Pull Requests Mar 18, 2026
@akohlmey
akohlmey deleted the more-use-of-safefileptr branch March 18, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants