Skip to content

refactor(tr): split trcomm.f90 into 6 logical submodules (Phase 2)#11

Merged
k-yoshimi merged 2 commits into
developfrom
feature/tr-phase2-trcomm-split
Apr 18, 2026
Merged

refactor(tr): split trcomm.f90 into 6 logical submodules (Phase 2)#11
k-yoshimi merged 2 commits into
developfrom
feature/tr-phase2-trcomm-split

Conversation

@k-yoshimi
Copy link
Copy Markdown
Owner

@k-yoshimi k-yoshimi commented Apr 18, 2026

Summary

Phase 2 of the TR refactoring (spec section 5.2): split the 817-line monolithic MODULE TRCOMM (~580 declared symbols) into six logical submodules.

Submodule Contents
trcomm_const Physical constants (PI, AEE, AME, AMM, VC, RMU0, EPS0, RKEV, NPSCM, VOID)
trcomm_param Namelist input parameters (RR, RA, BB, DT, profile shape, model selection, source/heating PNB*/PEC*/PLH*/PIC*/PEL*, etc.)
trcomm_ctrl Control variables (NT, NRAMAX, NTMAX_SAVE, T, IREAD, PNSS, equation-selection NSS/NSV/NNS/NST/NEA, MDLEQ*, UFILE control, file-name strings)
trcomm_mtx Matrix variables (XV, YV, AY, Y, ZV, AZ, Z, AX, X)
trcomm_profile Profile / source / coefficient / equilibrium / NCLASS / UFILE-storage / graphic / Honda-additional plasma variables
trcomm_globals Derived global quantities (WPT, AJT, BETA0, TAUE1, ANS0, TS0, WST, etc.)

MODULE TRCOMM becomes a thin wrapper that USEs all six submodules and re-exports their PUBLIC symbols. Existing callers using USE TRCOMM or USE TRCOMM, ONLY: SOME_SYMBOL (including the four files updated in PR #4 plus all other tr/*.f90) compile unchanged.

ALLOCATE_TRCOMM / DEALLOCATE_TRCOMM dispatch in dependency order to per-submodule allocate_<name> / deallocate_<name> / deallocate_err_<name> routines that preserve the original allocation behaviour exactly (PNSS-allocation guard, sizing variables NSTMAX/NEQMAXM/NVM/MWM/MLM/NRMP/NGLF/LDAB still set in the wrapper).

Files

  • New: tr/trcomm_const.f90, tr/trcomm_param.f90, tr/trcomm_ctrl.f90, tr/trcomm_mtx.f90, tr/trcomm_profile.f90, tr/trcomm_globals.f90
  • Modified: tr/trcomm.f90 (wrapper, 817 -> 137 lines)
  • Modified: tr/Makefile (SRCM extended; per-file deps for new submodules added)
  • No other tr/*.f90 files touched.

Test plan

Spec: docs/superpowers/specs/2026-04-17-tr-refactoring-design.md section 5.2.


Note

Medium Risk
Moderate risk because it refactors a central shared state module and rewires allocation/deallocation paths; mistakes could surface as runtime allocation errors despite intent to preserve behavior.

Overview
Refactors MODULE TRCOMM into 6 new modules (trcomm_const, trcomm_param, trcomm_ctrl, trcomm_mtx, trcomm_profile, trcomm_globals) and turns trcomm.f90 into a wrapper that USEs them and re-exports symbols.

Reworks memory lifecycle: ALLOCATE_TRCOMM, DEALLOCATE_TRCOMM, and DEALLOCATE_ERR_TRCOMM now delegate to per-submodule allocate_*/deallocate_*/deallocate_err_* routines (with deallocation reversed by dependency).

Build/test updates: tr/Makefile is updated to compile the new submodule sources with explicit object dependencies, and a new tr/tests/submodule_unit harness is added to smoke-test constants/params and verify alloc/dealloc, idempotency, and array shapes for each allocatable submodule.

Reviewed by Cursor Bugbot for commit 1b6fe5f. Bugbot is set up for automated code reviews on this repo. Configure here.

Split the 817-line monolithic MODULE TRCOMM into six logical submodules
per Phase 2 of the TR refactoring design (spec section 5.2):

  trcomm_const   - physical constants (PI, AEE, AME, AMM, VC, RMU0,
                   EPS0, RKEV, NPSCM, VOID)
  trcomm_param   - namelist input parameters (RR, RA, BB, DT, profile
                   shape parameters, model selection MDL*, source/heating
                   parameters PNB*/PEC*/PLH*/PIC*/PEL*, etc.)
  trcomm_ctrl    - control variables (NT, NRAMAX, NTMAX_SAVE, T, IREAD,
                   PNSS, equation-selection NSS/NSV/NNS/NST/NEA, MDLEQ*,
                   UFILE control MDLUF/MDLXP/etc., file-name strings)
  trcomm_mtx     - matrix variables (XV, YV, AY, Y, ZV, AZ, Z, AX, X)
  trcomm_profile - profile/source/coefficient/equilibrium/NCLASS/UFILE
                   storage/graphic/Honda-additional plasma variables
                   (RN, RT, AJ, RG, BP, QP, AK, ETA, RHO* metrics, AKDW*,
                   GRM/GVR graphic arrays, etc.)
  trcomm_globals - derived global quantities (WPT, AJT, BETA0, TAUE1,
                   ANS0, TS0, WST, etc.)

MODULE TRCOMM is now a thin wrapper that USEs all six submodules and
re-exports their PUBLIC entities, so existing callers (`USE TRCOMM` and
`USE TRCOMM, ONLY: ...`) compile unchanged.  ALLOCATE_TRCOMM /
DEALLOCATE_TRCOMM dispatch in dependency order to per-submodule
allocate/deallocate routines that preserve the original allocation
behaviour exactly.

Verification: tr_m0904 regression metrics match pre-Phase-2 baseline
within 1e-10 (compare_metrics.py). No other tr/*.f90 callers were
modified.

Spec: docs/superpowers/specs/2026-04-17-tr-refactoring-design.md (5.2)
@k-yoshimi
Copy link
Copy Markdown
Owner Author

@cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit a848ae3. Configure here.

@k-yoshimi
Copy link
Copy Markdown
Owner Author

Added Phase 2 submodule unit tests in tr/tests/submodule_unit/ (commit 1b6fe5f).

14 tests, all PASS:

  • trcomm_const — smoke check on PI / AEE / AME / AMM / VC / RMU0 / EPS0 / RKEV
  • trcomm_param — scalar assignment / readback (RR, RA, BB, DT)
  • trcomm_ctrl, trcomm_mtx, trcomm_profile, trcomm_globals — allocate, deallocate, idempotent (3 cycles), and shape-vs-TRCOM0 checks each

Each submodule's allocate_* / deallocate_* is invoked directly (without going through ALLOCATE_TRCOMM / DEALLOCATE_TRCOMM) so a regression in any single submodule is caught in isolation. Sizing variables in TRCOM0 are wired up by a small local setup_sizes helper that mirrors the derivation logic from ALLOCATE_TRCOMM.

Build + run:

cd tr/tests/submodule_unit
make run    # 'OK: all 14 submodule tests passed' / STOP 0

Verified locally: tr2 still builds; the new test program returns exit code 0 with all 14 tests passing.

Re-running @cursor review.

…odule)

Each new TRCOMM submodule (trcomm_const, trcomm_param, trcomm_ctrl,
trcomm_mtx, trcomm_profile, trcomm_globals) gets its allocate/
deallocate routine exercised in isolation by a standalone Fortran
test program in tr/tests/submodule_unit/.

The driver wires up TRCOM0 sizing variables itself (mirroring the
derivation logic from ALLOCATE_TRCOMM) and then calls the per-submodule
allocate/deallocate routines directly.  This catches regressions if
future Phase L work changes a single submodule independently.

Coverage: 14 tests total (smoke checks for const/param + alloc/dealloc/
idempotent/shape for each allocating submodule).  Run with:

    cd tr/tests/submodule_unit && make run

Expected: 'OK: all 14 submodule tests passed' / STOP 0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@k-yoshimi
Copy link
Copy Markdown
Owner Author

@cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1b6fe5f. Configure here.

@k-yoshimi k-yoshimi merged commit 9f73b0b into develop Apr 18, 2026
1 check passed
@k-yoshimi k-yoshimi deleted the feature/tr-phase2-trcomm-split branch April 18, 2026 15:44
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.

1 participant