Skip to content

Unify reals#33

Merged
krystophny merged 3 commits intomainfrom
unify-reals
Jan 12, 2026
Merged

Unify reals#33
krystophny merged 3 commits intomainfrom
unify-reals

Conversation

@zandivx
Copy link
Copy Markdown
Contributor

@zandivx zandivx commented Jan 10, 2026

User description

Made all reals real(dp) where dp is real64 from iso_fortran_env. Made all floating point literals with suffix _dp. Closes #29.


PR Type

Enhancement


Description

  • Unified all floating-point precision declarations across the codebase to use real(dp) where dp is defined as real64 from iso_fortran_env

  • Standardized all floating-point literals to use the _dp suffix notation (e.g., 1.0d01.0_dp, 1d-61.0e-6_dp)

  • Replaced all real(8), complex(8), and double precision declarations with real(dp) and complex(dp) respectively

  • Updated 27 source and test files to ensure consistent precision handling throughout the codebase

  • Improved code clarity by renaming ambiguous variables (e.g., DpD_plateau) to avoid conflicts with the dp kind parameter

  • Added explicit use iso_fortran_env, only: dp => real64 imports to all affected modules


Diagram Walkthrough

flowchart LR
  A["Old precision declarations<br/>real8, complex8,<br/>double precision"] -- "Replace with" --> B["Unified precision<br/>real(dp), complex(dp)"]
  C["Old literal notation<br/>1d0, 1d-6, 1.0d0"] -- "Convert to" --> D["Standardized literals<br/>1.0_dp, 1.0e-6_dp"]
  E["iso_fortran_env<br/>real64"] -- "Alias as" --> F["dp kind parameter<br/>use iso_fortran_env,<br/>only: dp => real64"]
  B --> G["Consistent codebase<br/>27 files updated"]
  D --> G
  F --> G
Loading

File Walkthrough

Relevant files
Enhancement
26 files
do_magfie_standalone.f90
Unify floating-point precision to real64 via dp alias       

src/do_magfie_standalone.f90

  • Added use iso_fortran_env, only: dp => real64 to define dp as the
    standard real kind
  • Replaced all real(8) declarations with real(dp) throughout the module
  • Converted all floating-point literals from d0/d-N notation to _dp
    suffix (e.g., 1.0d01.0_dp)
  • Improved code formatting with consistent spacing around operators
+121/-121
freq.f90
Standardize real precision declarations to real64               

src/freq.f90

  • Added use iso_fortran_env, only: dp => real64 module import
  • Replaced all real(8) type declarations with real(dp)
  • Converted floating-point literals to use _dp suffix notation
  • Updated variable initializations and parameter declarations to use new
    notation
+98/-94 
orbit.f90
Unify real and complex precision to real64 standard           

src/orbit.f90

  • Added use iso_fortran_env, only: dp => real64 at module start
  • Replaced all real(8) and complex(8) declarations with real(dp) and
    complex(dp)
  • Converted floating-point literals from d0/d-N to _dp suffix format
  • Added import :: dp statement in interface block for proper kind
    parameter passing
+103/-102
test_omega_prime.f90
Standardize test program precision to real64                         

test/test_omega_prime.f90

  • Added use iso_fortran_env, only: dp => real64 import
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix notation throughout
  • Updated structure member access syntax for consistency
+94/-92 
transport.f90
Unify precision and improve variable naming in transport module

src/transport.f90

  • Added use iso_fortran_env, only: dp => real64 module import
  • Replaced all real(8) and complex(8) declarations with real(dp) and
    complex(dp)
  • Converted floating-point literals to _dp suffix notation
  • Renamed variable Dp to D_plateau for clarity in transport coefficient
    calculations
+59/-55 
neort.f90
Standardize precision and improve module dependencies       

src/neort.f90

  • Added use iso_fortran_env, only: dp => real64 import
  • Replaced all real(8) and complex(8) declarations with real(dp) and
    complex(dp)
  • Converted floating-point literals to _dp suffix notation
  • Renamed variable Dp to D_plateau for consistency
  • Added explicit use statements with only clause for better code clarity
+58/-52 
test_bounce.f90
Unify test program precision to real64 standard                   

test/test_bounce.f90

  • Added use iso_fortran_env, only: dp => real64 import
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix notation
  • Updated parameter and variable initializations to use new notation
+27/-26 
test_misc.f90
Unify real precision to real64 with dp alias                         

test/test_misc.f90

  • Added use iso_fortran_env, only: dp => real64 import at module start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals from old format (e.g., 1d0, 1d-6) to
    new format with _dp suffix (e.g., 1.0_dp, 1.0e-6_dp)
  • Updated numeric literals in expressions and assignments throughout
    test subroutines
+34/-33 
collis_nbi.f90
Unify precision declarations and standardize floating-point literals

src/collis_nbi.f90

  • Added use iso_fortran_env, only: dp => real64 and implicit none at
    module start
  • Replaced all double precision declarations with real(dp)
  • Converted floating-point literals to _dp suffix format throughout
  • Renamed variable dp to d_p to avoid conflict with the dp kind
    parameter
  • Updated arithmetic expressions with consistent spacing and new literal
    format
+79/-73 
diag_contrib_map.f90
Standardize real precision alias and floating-point literals

src/diag/diag_contrib_map.f90

  • Changed import from use iso_fortran_env, only: real64 to use
    iso_fortran_env, only: dp => real64
  • Replaced all real(real64) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format (e.g.,
    1.0_real64 to 1.0_dp)
  • Updated real() type conversion calls to use dp instead of real64
+40/-40 
datatypes.f90
Unify all derived type declarations to use dp precision   

src/datatypes.f90

  • Added use iso_fortran_env, only: dp => real64 at module start
  • Replaced all real(8) and complex(8) declarations with real(dp) and
    complex(dp)
  • Updated all type definitions in derived types to use the unified
    precision
+57/-56 
diag_bounce_debug.f90
Standardize real precision alias and floating-point literals

src/diag/diag_bounce_debug.f90

  • Changed import from use iso_fortran_env, only: real64 to use
    iso_fortran_env, only: dp => real64
  • Replaced all real(real64) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format throughout
  • Updated real() type conversion calls to use dp instead of real64
+38/-38 
profiles.f90
Standardize floating-point literals to _dp suffix format 

src/profiles.f90

  • Converted floating-point literals from old format (0d0, 1d-24) to new
    format with _dp suffix
  • Updated variable initialization values with new literal format
  • Changed real(8) to real(dp) for Om_tE variable declaration
  • Updated arithmetic expressions with consistent spacing
+25/-24 
test_torque.f90
Unify real precision and standardize floating-point literals

test/test_torque.f90

  • Added use iso_fortran_env, only: dp => real64 import at program start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format
  • Renamed variable Dp to D_plateau to improve clarity
  • Updated arithmetic expressions with consistent spacing
+32/-32 
resonance.f90
Unify real precision and standardize floating-point literals

src/resonance.f90

  • Added use iso_fortran_env, only: dp => real64 import at module start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format (e.g., 1d0 to
    1.0_dp)
  • Updated arithmetic expressions with consistent spacing
+33/-32 
diag_bounce_nonlin.f90
Standardize real precision alias and floating-point literals

src/diag/diag_bounce_nonlin.f90

  • Changed import from use iso_fortran_env, only: real64 to use
    iso_fortran_env, only: dp => real64
  • Replaced all real(real64) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format
  • Updated real() type conversion calls to use dp instead of real64
+18/-18 
diag_atten_map.f90
Standardize real precision alias and floating-point literals

src/diag/diag_atten_map.f90

  • Changed import from use iso_fortran_env, only: real64 to use
    iso_fortran_env, only: dp => real64
  • Replaced all real(real64) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format
  • Updated real() type conversion calls to use dp instead of real64
+19/-19 
nonlin.f90
Unify real precision and standardize floating-point literals

src/nonlin.f90

  • Added use iso_fortran_env, only: dp => real64 import at module start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format
  • Updated arithmetic expressions with consistent spacing and new literal
    format
+26/-25 
test_frequencies.f90
Unify real precision and standardize floating-point literals

test/test_frequencies.f90

  • Added use iso_fortran_env, only: dp => real64 import at program start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format (e.g.,
    8.2512439552d-02 to 8.2512439552e-02_dp)
  • Updated arithmetic expressions with consistent spacing
+10/-9   
test_neort_lib.f90
Standardize floating-point literals to _dp suffix format 

test/test_neort_lib.f90

  • Converted floating-point literals from old format (0.5d0, 1d-12) to
    new format with _dp suffix
  • Updated parameter declarations and conditional checks with new literal
    format
  • Maintained existing real(dp) declarations
+9/-9     
util.f90
Unify real precision and standardize floating-point literals

src/util.f90

  • Added use iso_fortran_env, only: dp => real64 import at module start
  • Replaced all real(8) and complex(8) declarations with real(dp) and
    complex(dp)
  • Converted floating-point literals to _dp suffix format (e.g.,
    4.803204d-10 to 4.803204e-10_dp)
  • Updated physical constants and parameter definitions with new literal
    format
+22/-20 
test_reslines.f90
Unify real precision and standardize floating-point literals

test/test_reslines.f90

  • Added use iso_fortran_env, only: dp => real64 import at program start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format
  • Updated arithmetic expressions with consistent spacing
+11/-10 
driftorbit.f90
Unify real precision and standardize floating-point literals

src/driftorbit.f90

  • Added use iso_fortran_env, only: dp => real64 import at module start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format (e.g., 1d0 to
    1.0_dp, 1d-6 to 1.0e-6_dp)
  • Updated parameter definitions with new literal format
+11/-10 
magfie.f90
Unify real precision and standardize floating-point literals

src/magfie.f90

  • Added use iso_fortran_env, only: dp => real64 import at module start
  • Replaced all real(8) declarations with real(dp)
  • Converted floating-point literals to _dp suffix format
  • Updated arithmetic expressions with consistent spacing and new literal
    format
+16/-15 
attenuation_factor.f90
Unify real precision and standardize floating-point literals

src/attenuation_factor.f90

  • Added use iso_fortran_env, only: dp => real64 import at module and
    subroutine start
  • Replaced all double precision declarations with real(dp)
  • Converted floating-point literals to _dp suffix format (e.g., 1.d0 to
    1.0_dp)
+11/-9   
test_parallel.f90
Standardize floating-point literals to _dp suffix format 

test/test_parallel.f90

  • Converted floating-point literals from old format (1.0d-12, 0.5d0) to
    new format with _dp suffix
  • Updated parameter declarations and conditional checks with new literal
    format
  • Maintained existing real(dp) declarations
+4/-4     

@qodo-code-review
Copy link
Copy Markdown

qodo-code-review bot commented Jan 10, 2026

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🟡
🎫 #29
🟢 Keep dp as a named kind imported from iso_fortran_env (i.e., use iso_fortran_env, only: dp
=> real64), rather than using real64 directly throughout the code.
Replace usage of real64-based kind naming (real64, real(real64), etc.) with the unified
kind name dp.
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Silent NaN fallback: The new logic continues to silently coerce NaN results to 0.0_dp in vpar/vperp without
propagating an error or recording enough context to diagnose invalid input ranges.

Referred Code
    real(dp) :: vpar
    real(dp), intent(in) :: v, eta, bmod
    vpar = v * sqrt(1.0_dp - eta * bmod)
    if (isnan(vpar)) then
        vpar = 0.0_dp
    end if
end function vpar

pure function vperp(v, eta, bmod)
    !   perpendicular velocity
    real(dp) :: vperp
    real(dp), intent(in) :: v, eta, bmod
    vperp = v*sqrt(eta*bmod)
    if (isnan(vperp)) then
        vperp = 0.0_dp
    end if

Learn more about managing compliance generic rules or creating your own custom rules

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link
Copy Markdown

qodo-code-review bot commented Jan 10, 2026

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix incorrect operator precedence change

Fix incorrect operator precedence in the omega_prime_old calculation. The
division by (2.0_dp * Omth) should be a multiplication to match the original
formula.

test/test_omega_prime.f90 [309-310]

-omega_prime_old = mth * (eta * dOmdeta - ux * vth / 2 * dOmdv) / (mi * (ux * vth)**2 / &
-                                                                  (2.0_dp * Omth)) + dOmdpph
+omega_prime_old = mth * (eta * dOmdeta - ux * vth / 2 * dOmdv) * (2.0_dp * Omth) / &
+                  (mi * (ux * vth)**2) + dOmdpph
  • Apply / Chat
Suggestion importance[1-10]: 10

__

Why: The suggestion correctly identifies a critical bug where a change in parentheses and line breaks altered the mathematical logic, leading to incorrect calculations. This is a silent correctness issue.

High
Fix incorrect real array initialization

Fix a type mismatch error by initializing the real arrays costerm_ and sinterm_
with a scalar real value instead of complex constructor syntax.

src/do_magfie_standalone.f90 [330-331]

-costerm_ = (0.0_dp, 0.0_dp)
-sinterm_ = (0.0_dp, 0.0_dp)
+costerm_ = 0.0_dp
+sinterm_ = 0.0_dp
  • Apply / Chat
Suggestion importance[1-10]: 9

__

Why: The suggestion correctly identifies a type mismatch error where real arrays costerm_ and sinterm_ are initialized with complex literal syntax, which would cause a compilation failure.

High
Guard against division by zero

Add checks to prevent division by zero when calculating etatp and etadt if Bmax
or Bmin are zero.

src/magfie.f90 [61-62]

-etatp = 1.0_dp / Bmax
-etadt = 1.0_dp / Bmin
+if (Bmax /= 0.0_dp) then
+    etatp = 1.0_dp / Bmax
+else
+    ! Prevent division by zero
+    etatp = huge(0.0_dp)
+endif
+if (Bmin /= 0.0_dp) then
+    etadt = 1.0_dp / Bmin
+else
+    etadt = huge(0.0_dp)
+endif
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies a potential division-by-zero error and proposes a reasonable fix, improving the code's robustness.

Medium
General
Remove non-standard import from interface

Remove the non-standard import :: dp statement from the timestep_i interface
block to improve code portability.

src/orbit.f90 [23-32]

 interface
     subroutine timestep_i(v, eta, neq, t, y, ydot)
-        import :: dp
         real(dp), intent(in) :: v, eta
         integer, intent(in) :: neq
         real(dp), intent(in) :: t
         real(dp), intent(in) :: y(neq)
         real(dp), intent(out) :: ydot(neq)
     end subroutine timestep_i
 end interface
  • Apply / Chat
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies that import :: dp is non-standard within an interface block and improves code portability by removing it, as dp is already in scope.

Low
Use dp suffix on constants
Suggestion Impact:Updated vnorm initialization from sqrt(0.15d0) to sqrt(0.15_dp), matching the suggested dp-suffixed literal for consistency.

code diff:

-  real(dp), parameter  :: vnorm = sqrt(0.15d0)
+  real(dp), parameter  :: vnorm = sqrt(0.15_dp)

Update the literal in the vnorm parameter initialization to use the _dp suffix
for type consistency.

test/test_reslines.f90 [13]

-real(dp), parameter  :: vnorm = sqrt(0.15d0)
+real(dp), parameter  :: vnorm = sqrt(0.15_dp)

[Suggestion processed]

Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies an inconsistent literal format that was missed during the refactoring, and fixing it improves code consistency and correctness.

Low
Remove redundant self-assignment statement

Remove the redundant self-assignment Tphi = Tphi at the end of the
torque_from_transport function.

test/test_torque.f90 [84-101]

 function torque_from_transport(D) result(Tphi)
     real(dp), intent(in) :: D(2)
     real(dp) :: Tphi
     real(dp) :: D_plateau
     real(dp) :: bmod, sqrtg, x(3), hder(3), hcovar(3), hctrvr(3), hcurl(3)
     real(dp) :: sqrtgBth
 
     x(1) = s
     x(2) = 0.0_dp
     x(3) = 0.0_dp
     call do_magfie(x, bmod, sqrtg, hder, hcovar, hctrvr, hcurl)
     sqrtgBth = sqrtg * hctrvr(3) * bmod * AVG_NABLA_S
     print *, "sqrtgBth = ", sqrtgBth
 
     D_plateau = pi * vth**3 / (16.0_dp * R0 * iota * (qi * B0 / (mi * c))**2)
     Tphi = (sqrtgBth / c) * qe * (-ni1 * D_plateau * (D(1) * A1 + D(2) * A2)) * AVG_NABLA_S
-    Tphi = Tphi
 end function torque_from_transport
  • Apply / Chat
Suggestion importance[1-10]: 3

__

Why: The suggestion correctly identifies a redundant self-assignment and removing it improves code clarity, which is a minor but useful improvement.

Low
  • Update

Copy link
Copy Markdown
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 pull request unifies all real number types across the codebase to use real(dp) where dp is an alias for real64 from iso_fortran_env. All floating-point literals have been updated to use the _dp suffix instead of the older d0 notation.

Key Changes:

  • Replaced real(8) and real(real64) declarations with real(dp)
  • Converted floating-point literals from d0, d-N, and dN suffixes to _dp and e-N_dp format
  • Added use iso_fortran_env, only: dp => real64 to all relevant modules
  • Renamed some variables (e.g., DpD_plateau, dpd_p) to avoid naming conflicts with the new dp type parameter

Reviewed changes

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

Show a summary per file
File Description
test/test_torque.f90 Updated real types and literals; renamed variable Dp to D_plateau
test/test_reslines.f90 Updated real types and literals
test/test_parallel.f90 Updated real types and literals
test/test_omega_prime.f90 Updated real types and literals
test/test_neort_lib.f90 Updated real types and literals
test/test_misc.f90 Updated real types and literals
test/test_frequencies.f90 Updated real types and literals
test/test_bounce.f90 Updated real types and literals
src/util.f90 Added dp type; updated all real declarations and literals
src/transport.f90 Updated real types; renamed Dp to D_plateau
src/resonance.f90 Updated real types and literals
src/profiles.f90 Updated real types and literals
src/orbit.f90 Updated real types and literals; added import for interface
src/nonlin.f90 Updated real types and literals
src/neort.f90 Updated real types; renamed Dp to D_plateau
src/magfie.f90 Updated real types and literals
src/freq.f90 Updated real types and literals
src/driftorbit.f90 Updated real types and literals
src/do_magfie_standalone.f90 Updated real types and literals
src/diag/*.f90 Updated all diagnostic modules to use dp type
src/datatypes.f90 Updated all data structure fields to use dp
src/collis_nbi.f90 Updated real types; renamed dpd_p to avoid conflict
src/attenuation_factor.f90 Updated real types and literals

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

Comment on lines +98 to +99
D_plateau = pi * vth**3 / (16.0_dp * R0 * iota * (qi * B0 / (mi * c))**2)
Tphi = (sqrtgBth / c) * qe * (-ni1 * D_plateau * (D(1) * A1 + D(2) * A2)) * AVG_NABLA_S
Copy link

Copilot AI Jan 10, 2026

Choose a reason for hiding this comment

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

The variable name was changed from dp to D_plateau on line 87, but the variable name Dp (without underscore) is still used on line 98. This should be D_plateau to match the variable declaration.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback

Dp = pi*vth**3/(16d0*R0*iota*(qi*B0/(mi*c))**2)
dsdreff = 2d0/a*sqrt(s) ! TODO: Use exact value instead of this approximation
D = dsdreff**(-2)*D/Dp
D_plateau = pi * vth**3 / (16.0_dp * R0 * iota * (qi * B0 / (mi * c))**2)
Copy link

Copilot AI Jan 10, 2026

Choose a reason for hiding this comment

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

The variable name in the comment should be D_plateau instead of Dp to match the actual variable name declared on line 77.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 10, 2026

@zandivx I've opened a new pull request, #34, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 10, 2026

@zandivx I've opened a new pull request, #35, to work on those changes. Once the pull request is ready, I'll request review from you.

@zandivx
Copy link
Copy Markdown
Contributor Author

zandivx commented Jan 10, 2026

I couldn't believe that these suggestions were reasonable (as make test did succeed, which it wouldn't with these issues still hanging around). Turns out they weren't reasonable, Copilot just hallucinated. Everything's fine, ready for review.

@krystophny krystophny merged commit 45efee2 into main Jan 12, 2026
@krystophny krystophny deleted the unify-reals branch January 12, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unify naming convention for double precision.

4 participants