Skip to content

Multicomponent diffusion fluxes, thermal conduction, and mixture viscosity #888

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

Open
wants to merge 36 commits into
base: master
Choose a base branch
from

Conversation

DimAdam-01
Copy link
Contributor

@DimAdam-01 DimAdam-01 commented Jun 16, 2025

User description

Description

Please include a summary of the changes and the related issue(s) if they exist.

This update implements the diffusive fluxes for chemical species, includes thermal conduction in the energy equation, and computes the mixture’s viscosity

Please also include relevant motivation and context.

Fixes #(issue) [optional]

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Something else

Scope

  • This PR comprises a set of related changes with a common goal

If you cannot check the above box, please split your PR into multiple PRs that each have a common goal.

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.
Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration

  • This test examines one-dimensional multicomponent diffusion of four distinct species, as detailed in DOI 10.2514/6.2020-1751.
image image
  • Test B examines a two-dimensional premixed flame with an imposed perturbation, then tracks the ensuing thermo-diffusive instability growth.
image image

Test Configuration:

  • What computers and compilers did you use to test this:

My Laptop, Delta (A100) and Delta AI (GH)

Checklist

  • I have added comments for the new code
  • I added Doxygen docstrings to the new code
  • I have made corresponding changes to the documentation (docs/)
  • I have added regression tests to the test suite so that people can verify in the future that the feature is behaving as expected
  • I have added example cases in examples/ that demonstrate my new feature performing as expected.
    They run to completion and demonstrate "interesting physics"
  • I ran ./mfc.sh format before committing my code
  • New and existing tests pass locally with my changes, including with GPU capability enabled (both NVIDIA hardware with NVHPC compilers and AMD hardware with CRAY compilers) and disabled
  • This PR does not introduce any repeated code (it follows the DRY principle)
  • I cannot think of a way to condense this code and reduce any introduced additional line count

If your code changes any code source files (anything in src/simulation)

To make sure the code is performing as expected on GPU devices, I have:

  • Checked that the code compiles using NVHPC compilers
  • Checked that the code compiles using CRAY compilers
  • Ran the code on either V100, A100, or H100 GPUs and ensured the new feature performed as expected (the GPU results match the CPU results)
  • Ran the code on MI200+ GPUs and ensure the new features performed as expected (the GPU results match the CPU results)
  • Enclosed the new feature via nvtx ranges so that they can be identified in profiles
  • Ran a Nsight Systems profile using ./mfc.sh run XXXX --gpu -t simulation --nsys, and have attached the output file (.nsys-rep) and plain text results to this PR
  • Ran a Rocprof Systems profile using ./mfc.sh run XXXX --gpu -t simulation --rsys --hip-trace, and have attached the output file and plain text results to this PR.
  • Ran my code using various numbers of different GPUs (1, 2, and 8, for example) in parallel and made sure that the results scale similarly to what happens if you run without the new code/feature

PR Type

Enhancement


Description

  • Implement multicomponent diffusion fluxes for chemical species

  • Add thermal conduction to energy equation

  • Integrate mixture viscosity computation for chemistry

  • Extend RHS computation for diffusion physics


Changes walkthrough 📝

Relevant files
Enhancement
m_chemistry.fpp
Add multicomponent diffusion flux computation                       

src/common/m_chemistry.fpp

  • Add new imports for thermochemical properties and transport
    calculations
  • Implement s_compute_chemistry_diffusion_flux subroutine for species
    diffusion
  • Calculate mass diffusion fluxes using mixture-averaged diffusivities
  • Include thermal conduction contribution to energy equation
  • +149/-1 
    m_rhs.fpp
    Integrate diffusion physics into RHS computation                 

    src/simulation/m_rhs.fpp

  • Allocate flux arrays for diffusion when chemistry enabled
  • Add diffusion flux computation call in RHS calculation
  • Separate RHS updates for viscous and diffusion physics
  • Handle energy equation updates for non-viscous diffusion cases
  • +123/-34
    m_riemann_solvers.fpp
    Add mixture viscosity and diffusion flux initialization   

    src/simulation/m_riemann_solvers.fpp

  • Import mixture viscosity calculation function
  • Compute mixture viscosity for chemistry cases in Riemann solvers
  • Initialize diffusion flux arrays to zero
  • Add diffusion flux initialization for all coordinate directions
  • +59/-1   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • @DimAdam-01 DimAdam-01 changed the title Diffusion for Chemistry Multicomponent diffusion fluxes, thermal conduction, and mixture viscosity Jun 16, 2025
    Copy link

    codecov bot commented Jun 16, 2025

    Codecov Report

    Attention: Patch coverage is 5.88235% with 144 lines in your changes missing coverage. Please review.

    Project coverage is 45.42%. Comparing base (2aad1d4) to head (59ca1c7).
    Report is 1 commits behind head on master.

    Files with missing lines Patch % Lines
    src/common/m_chemistry.fpp 0.00% 59 Missing ⚠️
    src/simulation/m_rhs.fpp 13.84% 49 Missing and 7 partials ⚠️
    src/simulation/m_riemann_solvers.fpp 0.00% 26 Missing and 3 partials ⚠️
    Additional details and impacted files
    @@            Coverage Diff             @@
    ##           master     #888      +/-   ##
    ==========================================
    - Coverage   45.76%   45.42%   -0.34%     
    ==========================================
      Files          68       68              
      Lines       18668    18798     +130     
      Branches     2251     2266      +15     
    ==========================================
    - Hits         8543     8539       -4     
    - Misses       8767     8892     +125     
    - Partials     1358     1367       +9     

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    🚀 New features to boost your workflow:
    • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

    @sbryngelson sbryngelson requested a review from Copilot June 21, 2025 16:26
    Copy link

    @Copilot 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 integrates multicomponent diffusion fluxes, adds thermal conduction to the energy equation, and computes mixture viscosity throughout the solver.

    • Added calls to compute and invert mixture viscosity in the Riemann solver
    • Extended the RHS assembly to allocate, compute, and apply chemistry diffusion fluxes
    • Introduced s_compute_chemistry_diffusion_flux in m_chemistry.fpp to calculate species diffusion and thermal conduction

    Reviewed Changes

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

    File Description
    src/simulation/m_riemann_solvers.fpp Imported get_mixture_viscosity_mixavg, added viscosity logic and zeroing loops for diffusion flux sources
    src/simulation/m_rhs.fpp Allocated diffusion buffers, invoked diffusion RHS routines, and merged diffusion into physics RHS
    src/common/m_chemistry.fpp Added s_compute_chemistry_diffusion_flux subroutine and updated imports for thermal conduction and diffusivities
    Comments suppressed due to low confidence (2)

    src/simulation/m_rhs.fpp:535

    • This allocation isn’t paired with a deallocation, risking a memory leak and persistent GPU buffers. Consider deallocating these arrays when no longer needed or using automatic sizing within the data structure.
                            @:ALLOCATE(flux_src_n(i)%vf(E_idx)%sf( &
    

    src/common/m_chemistry.fpp:13

    • The imported get_species_binary_mass_diffusivities is not used in this module. Remove the unused import to keep dependencies minimal.
            get_mole_fractions, get_species_binary_mass_diffusivities, &
    

    Comment on lines 680 to 683
    call get_mixture_viscosity_mixavg(T_L, Ys_L, Re_L(1))
    call get_mixture_viscosity_mixavg(T_R, Ys_R, Re_R(1))
    Re_L(1) = 1.0_wp/Re_L(1)
    Re_R(1) = 1.0_wp/Re_R(1)
    Copy link
    Preview

    Copilot AI Jun 21, 2025

    Choose a reason for hiding this comment

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

    [nitpick] The viscosity calculation and inversion is duplicated in multiple solver branches. Refactor this into a helper subroutine to avoid copy–paste and ease future updates.

    Suggested change
    call get_mixture_viscosity_mixavg(T_L, Ys_L, Re_L(1))
    call get_mixture_viscosity_mixavg(T_R, Ys_R, Re_R(1))
    Re_L(1) = 1.0_wp/Re_L(1)
    Re_R(1) = 1.0_wp/Re_R(1)
    call compute_viscosity_and_inversion(T_L, Ys_L, T_R, Ys_R, Re_L, Re_R)

    Copilot uses AI. Check for mistakes.

    Comment on lines +3863 to +3870

    !$acc parallel loop collapse(4) gang vector default(present)
    do i = E_idx, chemxe
    do l = is3%beg, is3%end
    do k = is2%beg, is2%end
    do j = is1%beg, is1%end
    if (i == E_idx .or. i >= chemxb) then
    flux_src_vf(i)%sf(j, k, l) = 0._wp
    Copy link
    Preview

    Copilot AI Jun 21, 2025

    Choose a reason for hiding this comment

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

    [nitpick] The three separate nested loops zeroing out flux_src_vf for each coordinate ordering are nearly identical. Consolidate into a single parametrized kernel or subroutine to reduce duplication and improve readability.

    Suggested change
    !$acc parallel loop collapse(4) gang vector default(present)
    do i = E_idx, chemxe
    do l = is3%beg, is3%end
    do k = is2%beg, is2%end
    do j = is1%beg, is1%end
    if (i == E_idx .or. i >= chemxb) then
    flux_src_vf(i)%sf(j, k, l) = 0._wp
    !$acc parallel loop collapse(4) gang vector default(present)
    do i = E_idx, chemxe
    do l = is3%beg, is3%end
    do j = is1%beg, is1%end
    do k = is2%beg, is2%end
    if (i == E_idx .or. i >= chemxb) then
    flux_src_vf(i)%sf(k, j, l) = 0._wp

    Copilot uses AI. Check for mistakes.

    Copy link
    Member

    Choose a reason for hiding this comment

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

    pretty good comments! also you need to add test cases for diffusion, right?

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    The deallocation fix and the refactoring is a good catch. The loop structure suggestion is strange because I'm following exactly the same pattern used for the viscous implementation just a few lines above there's no logical reason to change the loop ordering.
    Regarding testing, I've already generated the initial test file for diffusion and now I am working on a boundary test case, trying to design a scenario where the method demonstrates its effectiveness within a short timeframe.

    Copy link
    Member

    Choose a reason for hiding this comment

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

    it only needs to be using this feature (diffusion) for a few time steps, long enough that it meaningfully changes the results.

    Copy link
    Member

    Choose a reason for hiding this comment

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

    The loop structure suggestion is strange because I'm following exactly the same pattern used for the viscous implementation just a few lines above there's no logical reason to change the loop ordering.

    @DimAdam-01 yes copilot is just pointing is where THIS PR is doing something that is not best practices, it isn't reviewing the entire code (which also has a lot of redundant code)

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    @sbryngelson, Ok I checked the golden text file that was produced, and there's only a change at the 4th decimal place. Should I extend the simulation time to see more significant differences?

    Copy link
    Member

    Choose a reason for hiding this comment

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

    i'm not sure tbh

    @sbryngelson sbryngelson self-requested a review June 21, 2025 17:03
    @sbryngelson sbryngelson marked this pull request as ready for review June 21, 2025 17:27
    @sbryngelson sbryngelson requested a review from a team as a code owner June 21, 2025 17:27
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Possible Issue

    The mass diffusion flux calculation uses mixture-averaged diffusivities with a correction factor that may lead to incorrect results. The formula on line 240 applies a correction factor (1-Xs)/(1-Ys) which could cause division by zero or numerical instability when Ys approaches 1.0, and the physical validity of this correction needs verification.

        mass_diffusivities_mixavg_Cell(i - chemxb + 1) = &
            (mass_diffusivities_mixavg2(i - chemxb + 1) + mass_diffusivities_mixavg1(i - chemxb + 1))/ &
            2.0_wp*(1.0_wp - Xs_cell(i - chemxb + 1))/(1.0_wp - Ys_cell(i - chemxb + 1))
    end do
    Code Duplication

    The diffusion flux computation contains significant code duplication across the three coordinate directions. The select case statement for grid spacing calculation and the repetitive flux calculations could be refactored into a more maintainable structure to reduce duplication and potential inconsistencies.

    select case (idir)
    case (1)
        grid_spacing = x_cc(x + 1) - x_cc(x)
    case (2)
        grid_spacing = y_cc(y + 1) - y_cc(y)
    case (3)
        grid_spacing = z_cc(z + 1) - z_cc(z)
    end select
    Logic Complexity

    The RHS update logic has become complex with multiple conditional blocks for viscous, surface_tension, and diffusion cases. The nested conditions and separate handling of energy equation updates when viscous is false could lead to maintenance issues and potential bugs in edge cases.

    if (surface_tension .or. viscous) then
        !$acc parallel loop collapse(3) gang vector default(present)
        do l = 0, p
            do k = 0, n
                do j = 0, m
                    !$acc loop seq
                    do i = momxb, E_idx
                        rhs_vf(i)%sf(j, k, l) = &
                            rhs_vf(i)%sf(j, k, l) + 1._wp/dx(j)* &
                            (flux_src_n(i)%sf(j - 1, k, l) &
                             - flux_src_n(i)%sf(j, k, l))
                    end do
                end do
            end do
        end do
    end if
    
    if (chem_params%diffusion) then
        !$acc parallel loop collapse(3) gang vector default(present)
        do l = 0, p
            do k = 0, n
                do j = 0, m
                    !$acc loop seq
                    do i = chemxb, chemxe
                        rhs_vf(i)%sf(j, k, l) = &
                            rhs_vf(i)%sf(j, k, l) + 1._wp/dx(j)* &
                            (flux_src_n(i)%sf(j - 1, k, l) &
                             - flux_src_n(i)%sf(j, k, l))
                    end do
    
                    if (.not. viscous) then
                        rhs_vf(E_idx)%sf(j, k, l) = &
                            rhs_vf(E_idx)%sf(j, k, l) + 1._wp/dx(j)* &
                            (flux_src_n(E_idx)%sf(j - 1, k, l) &
                             - flux_src_n(E_idx)%sf(j, k, l))
                    end if
                end do
            end do
        end do
    end if

    @DimAdam-01 DimAdam-01 requested a review from a team as a code owner June 21, 2025 17:39
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Development

    Successfully merging this pull request may close these issues.

    2 participants