Skip to content

Conversation

@Dooruk
Copy link
Collaborator

@Dooruk Dooruk commented Dec 22, 2025

I made this minor change a while back but forgot to create a PR, not sure if this is the way we want to go about this. I see Travis added a note:

  ! TODO the jacobians should really be stored in atlas fields, but
  !  I didn't feel like dealing with all that refactoring

Is that something I could tackle with reasonable amount of effort? I'm not familiar with the atlas layer.. I would like to learn though so I could use some guidance on that?

This unfortunately doesn't write another variable to the same file. While looking for solutions to write two steric jacobian fields to the same file, gpt suggested using fms2_io_mod. I see this is used in fv3-jedi I'm wondering if fms2_io is something we want to use in SOCA? I also saw this comment from last year.

Relevant issue #1106

Todo:

  • edit some test ymls

Copy link
Contributor

Copilot AI left a comment

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 adds functionality to optionally write dynamic height Jacobians (Kst and Ksshts) to NetCDF files during the balance operator setup phase. The implementation introduces a new utility module for writing Jacobians using FMS I/O routines.

  • Introduces soca_write_jacobian_mod module with a function to write 3D Jacobian arrays to NetCDF files
  • Adds optional Jacobian output for Kst and Ksshts balance operators controlled by configuration
  • Enables diagnostic output of computed Jacobian fields for analysis and debugging

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/soca/LinearVariableChange/Balance/soca_write_jacobian_mod.F90 New module providing write_jacobian_to_netcdf function using FMS I/O routines
src/soca/LinearVariableChange/Balance/soca_balance_mod.F90 Adds optional Jacobian output blocks for Kst and Ksshts operators with configuration-based file writing
src/soca/LinearVariableChange/Balance/CMakeLists.txt Registers new soca_write_jacobian_mod.F90 source file in build system

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +207 to +208
call write_jacobian_to_netcdf(self%ksshts%kssht, geom, str, "kssht_jacobian")
call write_jacobian_to_netcdf(self%ksshts%ksshs, geom, str, "ksshs_jacobian")
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Writing both kssht_jacobian and ksshs_jacobian to the same file by calling write_jacobian_to_netcdf twice with the same filename will likely result in the second call overwriting the first, or potentially causing errors. The FMS write_data function may not support appending multiple variables to the same file in this manner. Consider either using different filenames for each Jacobian or implementing a mechanism to write both variables in a single call.

Suggested change
call write_jacobian_to_netcdf(self%ksshts%kssht, geom, str, "kssht_jacobian")
call write_jacobian_to_netcdf(self%ksshts%ksshs, geom, str, "ksshs_jacobian")
filename = str
call write_jacobian_to_netcdf(self%ksshts%kssht, geom, filename, "kssht_jacobian")
filename = trim(str)//"_ksshs"
call write_jacobian_to_netcdf(self%ksshts%ksshs, geom, filename, "ksshs_jacobian")

Copilot uses AI. Check for mistakes.

! declarations related to the dynamic height Jacobians
character(len=:), allocatable :: filename
character(len=:), allocatable :: str
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

The variable 'str' is declared but never deallocated. Since it's an allocatable character string that gets assigned values from f_conf%get_or_die, it should be deallocated after use to prevent memory leaks, or the allocation should be handled more carefully within the scope where it's used.

Copilot uses AI. Check for mistakes.
Comment on lines +27 to +33
call fms_io_init()
call set_domain(geom%Domain%mpp_domain)

! Write the jacobian data from all PEs
call write_data(filename, varname, jacobian, geom%Domain%mpp_domain)

call fms_io_exit()
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

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

Calling fms_io_init and fms_io_exit on every invocation can be problematic. In soca_geom_mod, fms_io_init is guarded by a counter check to ensure it's only called once per geometry initialization. Repeatedly initializing and finalizing the FMS I/O subsystem for each Jacobian write could lead to resource management issues or conflicts, especially if multiple Jacobians are written in sequence. Consider checking if FMS I/O is already initialized or coordinating with the existing initialization in soca_geom_mod.

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@shlyaeva
Copy link
Collaborator

shlyaeva commented Jan 7, 2026

Is that something I could tackle with reasonable amount of effort? I'm not familiar with the atlas layer.. I would like to learn though so I could use some guidance on that?

I don't have a good estimate as I don't know this part of the code well enough myself. I think it's a good idea though, and I would be happy to look into it together and provide some guidance, let me know!

fms2_io_mod. I see this is used in fv3-jedi I'm wondering if fms2_io is something we want to use in SOCA? I also saw https://github.com/JCSDA-internal/fv3-jedi-linearmodel/issues/18#issuecomment-2403328653 from last year.

For the comment, we've resolved this, I think, with #1122 (and a follow-up bugfix). But yes, we may want to change our I/O. I'm not familiar enough with fms_io or fms2_io and personally currently feeling lukewarm about getting into it 😆 I did just spend a few days looking at refactoring I/O completely (not using fms io, at least for state/increment I/O, just using direct netcdf read/writes), but I have temporarily given up on that.

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