-
Notifications
You must be signed in to change notification settings - Fork 9
(draft) Write dynamic jacobians #1218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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_modmodule 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.
| call write_jacobian_to_netcdf(self%ksshts%kssht, geom, str, "kssht_jacobian") | ||
| call write_jacobian_to_netcdf(self%ksshts%ksshs, geom, str, "ksshs_jacobian") |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
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.
| 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") |
|
|
||
| ! declarations related to the dynamic height Jacobians | ||
| character(len=:), allocatable :: filename | ||
| character(len=:), allocatable :: str |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
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.
src/soca/LinearVariableChange/Balance/soca_write_jacobian_mod.F90
Outdated
Show resolved
Hide resolved
| 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() |
Copilot
AI
Dec 22, 2025
There was a problem hiding this comment.
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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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!
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. |
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:
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 infv3-jediI'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: